Make imgmath_use_preview work also for svg output

This commit is contained in:
jfbu 2019-04-16 16:43:08 +02:00
parent f050092f42
commit 1adf12bfa7
3 changed files with 44 additions and 7 deletions

View File

@ -106,8 +106,9 @@ built:
installed (on Ubuntu xenial, it is available as `preview-latex-style`_).
Therefore, the default for this option is ``False``.
Currently this option is only used when ``imgmath_image_format`` is
``'png'``.
.. versionchanged:: 2.1.0
This option can also be used with ``imgmath_image_format`` set to ``'svg'``.
.. confval:: imgmath_add_tooltips

View File

@ -87,6 +87,25 @@ DOC_BODY_PREVIEW = r'''
'''
depth_re = re.compile(br'\[\d+ depth=(-?\d+)\]')
depthsvg_re = re.compile(br'.*, depth=(.*)pt')
def read_svg_depth(filename):
# type: (str) -> int
"""Read the depth from comment at last line of SVG file
"""
with open(filename, 'r') as f:
for line in f:
pass
return int(line[11:-4])
def write_svg_depth(filename, depth):
# type: (str, int) -> None
"""Write the depth to SVG file as a comment at end of file
"""
with open(filename, 'a') as f:
f.write('\n<!-- DEPTH=%s -->' % depth)
def generate_latex_macro(math: str, config: Config, confdir: str = '') -> str:
@ -95,6 +114,7 @@ def generate_latex_macro(math: str, config: Config, confdir: str = '') -> str:
'fontsize': config.imgmath_font_size,
'baselineskip': int(round(config.imgmath_font_size * 1.2)),
'preamble': config.imgmath_latex_preamble,
'tightpage': '' if image_format == 'png' else ',tightpage',
'math': math
}
@ -201,8 +221,18 @@ def convert_dvi_to_svg(dvipath: str, builder: Builder) -> Tuple[str, int]:
command.extend(builder.config.imgmath_dvisvgm_args)
command.append(dvipath)
convert_dvi_to_image(command, name)
return filename, None
stdout, stderr = convert_dvi_to_image(command, name)
depth = None
if builder.config.imgmath_use_preview:
for line in stderr.splitlines(): # not stdout !
matched = depthsvg_re.match(line)
if matched:
depth = round(float(matched.group(1)) * 100 / 72.27) # assume 100ppi
write_svg_depth(filename, depth)
break
return filename, depth
def render_math(self: HTMLTranslator, math: str) -> Tuple[str, int]:
@ -223,13 +253,19 @@ def render_math(self: HTMLTranslator, math: str) -> Tuple[str, int]:
if image_format not in SUPPORT_FORMAT:
raise MathExtError('imgmath_image_format must be either "png" or "svg"')
latex = generate_latex_macro(math, self.builder.config, self.builder.confdir)
latex = generate_latex_macro(image_format,
math,
self.builder.config,
self.builder.confdir)
filename = "%s.%s" % (sha1(latex.encode()).hexdigest(), image_format)
relfn = posixpath.join(self.builder.imgpath, 'math', filename)
outfn = path.join(self.builder.outdir, self.builder.imagedir, 'math', filename)
if path.isfile(outfn):
depth = read_png_depth(outfn)
if image_format == 'png':
depth = read_png_depth(outfn)
elif image_format == 'svg':
depth = read_svg_depth(outfn)
return relfn, depth
# if latex or dvipng (dvisvgm) has failed once, don't bother to try again

View File

@ -9,7 +9,7 @@
\pagestyle{empty}
<%= preamble %>
\usepackage[active]{preview}
\usepackage[active<%= tightpage %>]{preview}
\begin{document}
\begin{preview}