From: Vladimír Vondruš Date: Mon, 18 Jun 2018 09:21:09 +0000 (+0200) Subject: m.math: use em units instead of pt. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~cjwatson/git?a=commitdiff_plain;h=6e7796c94f5cef2c92a8e432cd88f836dd364ee5;p=blog.git m.math: use em units instead of pt. So it's possible to actually scale the formulas relative to surrounding text. --- diff --git a/doxygen/dox2html5.py b/doxygen/dox2html5.py index 3df5c408..474ecf0a 100755 --- a/doxygen/dox2html5.py +++ b/doxygen/dox2html5.py @@ -1225,15 +1225,13 @@ def parse_desc_internal(state: State, element: ET.Element, immediate_parent: ET. has_block_elements = True out.parsed += '
{}
'.format( ' ' + add_css_class if add_css_class else '', - latex2svgextra.patch(i.text, svg, '')) + latex2svgextra.patch(i.text, svg, None, '')) else: # CSS classes and styling for proper vertical alignment. Depth is relative # to font size, describes how below the line the text is. Scaling it back # to 12pt font, scaled by 125% as set above in the config. - attribs = ' class="m-math{}" style="vertical-align: -{:.1f}pt;"'.format( - ' ' + add_inline_css_class if add_inline_css_class else '', - (depth or 0.0)*12*1.25) - out.parsed += latex2svgextra.patch(i.text, svg, attribs) + attribs = ' class="m-math{}"'.format(' ' + add_inline_css_class if add_inline_css_class else '') + out.parsed += latex2svgextra.patch(i.text, svg, depth, attribs) # Inline elements elif i.tag == 'linebreak': diff --git a/doxygen/test/contents_custom/math.html b/doxygen/test/contents_custom/math.html index ed83d1ea..49b9c541 100644 --- a/doxygen/test/contents_custom/math.html +++ b/doxygen/test/contents_custom/math.html @@ -22,7 +22,7 @@

Math

-

A green formula:

+

A green formula:

\[ \pi^2 \] @@ -34,7 +34,7 @@ -

A yellow +

A yellow $ \Sigma $ diff --git a/doxygen/test/contents_math/index.html b/doxygen/test/contents_math/index.html index 46dc9a1f..6c9b9592 100644 --- a/doxygen/test/contents_math/index.html +++ b/doxygen/test/contents_math/index.html @@ -22,7 +22,7 @@

My Project

-

Here's a block formula:

+

Here's a block formula:

\[ \hat q = [\boldsymbol 0, 1] + \epsilon [\frac{\boldsymbol v}{2}, 0] \] @@ -60,7 +60,7 @@ -

And +

And $ \hat q $ @@ -72,7 +72,7 @@ $ \hat q $ - is how quaternion is denoted.

+ is how quaternion is denoted.

\[ a^2 + b^2 = c^2 \] @@ -94,7 +94,7 @@ $ \hat q $ -

+

$c^2$ @@ -106,7 +106,7 @@ $c^2$ - should be part of a new paragraph, not stuck out of it. The following formula has a custom environment:

+ should be part of a new paragraph, not stuck out of it. The following formula has a custom environment:

\begin{eqnarray*} g &=& \frac{Gm_2}{r^2} \\ &=& 9.82066032\,\mbox{m/s}^2 \end{eqnarray*} diff --git a/doxygen/test/contents_math_cached/math.html b/doxygen/test/contents_math_cached/math.html index 8e56824b..d1a28679 100644 --- a/doxygen/test/contents_math_cached/math.html +++ b/doxygen/test/contents_math_cached/math.html @@ -22,7 +22,7 @@

Math

-

In order to actually test use of the cache, I need to cheat a bit. Inline formula which is pi in the source but +

In order to actually test use of the cache, I need to cheat a bit. Inline formula which is pi in the source but $ \pi $ @@ -35,7 +35,7 @@ $ \pi $ - here. Then a block formula which is a Pythagorean theorem in source but not in the output:

+ here. Then a block formula which is a Pythagorean theorem in source but not in the output:

\[ a^2 + b^2 = c^2 \] diff --git a/pelican-plugins/latex2svgextra.py b/pelican-plugins/latex2svgextra.py index 5cca1da5..a782d30e 100644 --- a/pelican-plugins/latex2svgextra.py +++ b/pelican-plugins/latex2svgextra.py @@ -49,15 +49,20 @@ params.update({ _patch_src = re.compile(r"""<\?xml version='1.0' encoding='UTF-8'\?> -.+) xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'> + """) -_patch_dst = r"""> +# version ignored by all UAs, safe to drop https://stackoverflow.com/a/18468348 +_patch_dst = r""" {formula} """ +# 1 pt is 1.333 px, base font size is 16px. TODO: make this configurable, +# remove the 1.25 scaling +pt2em = 1.333333/16.0 + _unique_src = re.compile(r"""(?P id|xlink:href)='(?P#?)(?Pg\d+-\d+|page\d+)'""") _unique_dst = r"""\g='\geq{counter}-\g'""" @@ -122,8 +127,22 @@ def pickle_cache(file): # Patches the output from dvisvgm # - w/o the XML preamble and needless xmlns attributes # - unique element IDs (see `counter`) +# - adjusts vertical align if depth is not none # - adds additional `attribs` to the element -def patch(formula, svg, attribs): +def patch(formula, svg, depth, attribs): global counter counter += 1 - return _unique_src.sub(_unique_dst.format(counter=counter), _patch_src.sub(_patch_dst.format(attribs=attribs, formula=formula.replace('\\', '\\\\')), svg)) + + if depth is None: style = '' + else: style = ' vertical-align: -{:.3f}em;'.format(depth*1.25) + + def repl(match): + return _patch_dst.format( + width=pt2em*float(match.group('width')), + height=pt2em*float(match.group('height')), + style=style, + viewBox=match.group('viewBox'), + attribs=attribs, + formula=formula) + + return _unique_src.sub(_unique_dst.format(counter=counter), _patch_src.sub(repl, svg)) diff --git a/pelican-plugins/m/math.py b/pelican-plugins/m/math.py index a0108c4d..28010dfb 100644 --- a/pelican-plugins/m/math.py +++ b/pelican-plugins/m/math.py @@ -64,7 +64,7 @@ class Math(rst.Directive): container = nodes.container(**self.options) container['classes'] += ['m-math'] - node = nodes.raw(self.block_text, latex2svgextra.patch(block, svg, ''), format='html') + node = nodes.raw(self.block_text, latex2svgextra.patch(block, svg, None, ''), format='html') node.line = self.content_offset + 1 self.add_name(node) container.append(node) @@ -101,12 +101,8 @@ def math(role, rawtext, text, lineno, inliner, options={}, content=[]): depth, svg = latex2svgextra.fetch_cached_or_render("$" + text + "$") - # CSS classes and styling for proper vertical alignment. Depth is relative - # to font size, describes how below the line the text is. Scaling it back - # to 12pt font, scaled by 125% as set above in the config. - attribs = ' class="{}" style="vertical-align: -{:.1f}pt;"'.format(classes, depth*12*1.25) - - node = nodes.raw(rawtext, latex2svgextra.patch(text, svg, attribs), format='html', **options) + attribs = ' class="{}"'.format(classes) + node = nodes.raw(rawtext, latex2svgextra.patch(text, svg, depth, attribs), format='html', **options) return [node], [] def configure_pelican(pelicanobj): diff --git a/pelican-plugins/m/test/math/page.html b/pelican-plugins/m/test/math/page.html index 035c39b3..8c1669a3 100644 --- a/pelican-plugins/m/test/math/page.html +++ b/pelican-plugins/m/test/math/page.html @@ -23,7 +23,7 @@

m.math

-

Inline colored math +

Inline colored math a^2 @@ -37,7 +37,7 @@ a^2 and colored math block:

- + a^2 + b^2 = c^2 @@ -61,7 +61,7 @@ a^2 + b^2 = c^2

Properly align huge formulas vertically on a line: - + \hat q^{-1} = \frac{\hat q^*}{|\hat q|^2} @@ -94,7 +94,7 @@ a^2 + b^2 = c^2 -and make sure there's enough space for all the complex +and make sure there's enough space for all the complex W @@ -105,7 +105,7 @@ W things between -the lines +the lines W = \sum_{i=0}^{n} \frac{w_i}{h_i} @@ -136,7 +136,7 @@ W = \sum_{i=0}^{n} \frac{w_i}{h_i} because - + Y = \sum_{i=0}^{n} B @@ -161,7 +161,7 @@ Y = \sum_{i=0}^{n} B

-

The \cfrac thing doesn't align well: +

The \cfrac thing doesn't align well: W = \sum_{i=0}^{n} \cfrac{w_i}{h_i} diff --git a/pelican-plugins/m/test/math_cached/page.html b/pelican-plugins/m/test/math_cached/page.html index 3a729836..5c438ab5 100644 --- a/pelican-plugins/m/test/math_cached/page.html +++ b/pelican-plugins/m/test/math_cached/page.html @@ -24,7 +24,7 @@

Math

In order to actually test use of the cache, I need to cheat a bit. Inline -formula which is pi in the source but +formula which is pi in the source but \pi @@ -40,7 +40,7 @@ formula which is pi in the source but - + a^2 + b^2 = c^2