Skip to content

Commit

Permalink
Make imgmath_use_preview work also for svg output
Browse files Browse the repository at this point in the history
  • Loading branch information
jfbu committed Aug 1, 2019
1 parent f050092 commit 1adf12b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
5 changes: 3 additions & 2 deletions doc/usage/extensions/math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
44 changes: 40 additions & 4 deletions sphinx/ext/imgmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,25 @@ class InvokeError(SphinxError):
'''

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:
Expand All @@ -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
}

Expand Down Expand Up @@ -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]:
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sphinx/templates/imgmath/preview.tex_t
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
\pagestyle{empty}
<%= preamble %>

\usepackage[active]{preview}
\usepackage[active<%= tightpage %>]{preview}

\begin{document}
\begin{preview}
Expand Down

0 comments on commit 1adf12b

Please sign in to comment.