Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2d43e5b

Browse files
committed
Align inline mathtext correctly in HTML docs (by adjusting for the baseline)
svn path=/trunk/matplotlib/; revision=5577
1 parent a8bb2bf commit 2d43e5b

3 files changed

Lines changed: 62 additions & 17 deletions

File tree

doc/sphinxext/mathpng.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,19 @@ def latex2png(latex, name):
113113

114114
# This uses mathtext to render the expression
115115
def latex2png(latex, filename, fontset='cm'):
116+
latex = "$%s$" % latex
116117
if os.path.exists(filename):
117-
return
118+
return mathtext_parser.get_depth(latex, dpi=120)
118119
orig_fontset = rcParams['mathtext.fontset']
119120
rcParams['mathtext.fontset'] = fontset
120121
try:
121-
mathtext_parser.to_png(filename, "$%s$" % latex, dpi=120)
122+
depth = mathtext_parser.to_png(filename, latex, dpi=120)
122123
except:
123-
warnings.warn("Could not render math expression $%s$" % latex,
124-
warnings.Warning)
124+
warnings.warn("Could not render math expression %s" % latex,
125+
Warning)
126+
depth = 0
125127
rcParams['mathtext.fontset'] = orig_fontset
128+
return depth
126129

127130
# LaTeX to HTML translation stuff:
128131
def latex2html(node, source):
@@ -131,8 +134,7 @@ def latex2html(node, source):
131134
print latex.encode("ascii", "backslashreplace")
132135
name = 'math-%s' % md5(latex).hexdigest()[-10:]
133136
dest = '_static/%s.png' % name
134-
if not isfile(dest):
135-
latex2png(latex, dest, node['fontset'])
137+
depth = latex2png(latex, dest, node['fontset'])
136138

137139
path = '_static'
138140
count = source.split('/doc/')[-1].count('/')
@@ -148,5 +150,10 @@ def latex2html(node, source):
148150
cls = ''
149151
else:
150152
cls = 'class="center" '
151-
return '<img src="%s/%s.png" %s%s/>' % (path, name, align, cls)
153+
if inline and depth != 0:
154+
style = 'style="position: relative; bottom: -%dpx"' % (depth + 1)
155+
else:
156+
style = ''
157+
158+
return '<img src="%s/%s.png" %s%s%s/>' % (path, name, align, cls, style)
152159

examples/user_interfaces/mathtext_wx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from matplotlib.mathtext import MathTextParser
2222
mathtext_parser = MathTextParser("Bitmap")
2323
def mathtext_to_wxbitmap(s):
24-
ftimage = mathtext_parser.parse(s, 150)
24+
ftimage, depth = mathtext_parser.parse(s, 150)
2525
return wx.BitmapFromBufferRGBA(
2626
ftimage.get_width(), ftimage.get_height(),
2727
ftimage.as_rgba_str())

lib/matplotlib/mathtext.py

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ def MathtextBackendAgg():
341341

342342
class MathtextBackendBitmapRender(MathtextBackendAggRender):
343343
def get_results(self, box):
344-
return self.image
344+
return self.image, self.depth
345345

346346
def MathtextBackendBitmap():
347347
return MathtextBackendBbox(MathtextBackendBitmapRender())
@@ -2726,7 +2726,13 @@ def parse(self, s, dpi = 72, prop = None):
27262726

27272727
def to_mask(self, texstr, dpi=120, fontsize=14):
27282728
"""
2729-
return an NxM uint8 alpha ubyte mask array of rasterized tex
2729+
Returns a tuple (*array*, *depth*)
2730+
2731+
- *array* is an NxM uint8 alpha ubyte mask array of
2732+
rasterized tex.
2733+
2734+
- depth is the offset of the baseline from the bottom of the
2735+
image in pixels.
27302736
27312737
''texstr''
27322738
A valid mathtext string, eg r'IQ: $\sigma_i=15$'
@@ -2739,14 +2745,22 @@ def to_mask(self, texstr, dpi=120, fontsize=14):
27392745
"""
27402746
assert(self._output=="bitmap")
27412747
prop = FontProperties(size=fontsize)
2742-
ftimage = self.parse(texstr, dpi=dpi, prop=prop)
2748+
ftimage, depth = self.parse(texstr, dpi=dpi, prop=prop)
27432749

27442750
x = ftimage.as_array()
2745-
return x
2751+
return x, depth
27462752

27472753
def to_rgba(self, texstr, color='black', dpi=120, fontsize=14):
27482754
"""
2749-
return an NxMx4 RGBA array of ubyte rasterized tex
2755+
Returns a tuple (*array*, *depth*)
2756+
2757+
- *array* is an NxMx4 RGBA array of ubyte rasterized tex.
2758+
2759+
- depth is the offset of the baseline from the bottom of the
2760+
image in pixels.
2761+
2762+
Returns a tuple (array, depth), where depth is the offset of
2763+
the baseline from the bottom of the image.
27502764
27512765
''texstr''
27522766
A valid mathtext string, eg r'IQ: $\sigma_i=15$'
@@ -2760,18 +2774,22 @@ def to_rgba(self, texstr, color='black', dpi=120, fontsize=14):
27602774
''fontsize''
27612775
The font size in points
27622776
"""
2763-
x = self.to_mask(texstr, dpi=dpi, fontsize=fontsize)
2777+
x, depth = self.to_mask(texstr, dpi=dpi, fontsize=fontsize)
27642778

27652779
r, g, b = mcolors.colorConverter.to_rgb(color)
27662780
RGBA = np.zeros((x.shape[0], x.shape[1], 4), dtype=np.uint8)
27672781
RGBA[:,:,0] = int(255*r)
27682782
RGBA[:,:,1] = int(255*g)
27692783
RGBA[:,:,2] = int(255*b)
27702784
RGBA[:,:,3] = x
2771-
return RGBA
2785+
return RGBA, depth
27722786

27732787
def to_png(self, filename, texstr, color='black', dpi=120, fontsize=14):
27742788
"""
2789+
Writes a tex expression to a PNG file.
2790+
2791+
Returns the offset of the baseline from the bottom of the
2792+
image in pixels.
27752793
27762794
''filename''
27772795
A writable filename or fileobject
@@ -2790,7 +2808,27 @@ def to_png(self, filename, texstr, color='black', dpi=120, fontsize=14):
27902808
27912809
"""
27922810

2793-
rgba = self.to_rgba(texstr, color=color, dpi=dpi, fontsize=fontsize)
2811+
rgba, depth = self.to_rgba(texstr, color=color, dpi=dpi, fontsize=fontsize)
27942812
numrows, numcols, tmp = rgba.shape
2795-
return _png.write_png(rgba.tostring(), numcols, numrows, filename)
2813+
_png.write_png(rgba.tostring(), numcols, numrows, filename)
2814+
return depth
2815+
2816+
def get_depth(self, texstr, dpi=120, fontsize=14):
2817+
"""
2818+
Returns the offset of the baseline from the bottom of the
2819+
image in pixels.
2820+
2821+
''texstr''
2822+
A valid mathtext string, eg r'IQ: $\sigma_i=15$'
2823+
2824+
''dpi''
2825+
The dots-per-inch to render the text
2826+
2827+
''fontsize''
2828+
The font size in points
27962829
2830+
"""
2831+
assert(self._output=="bitmap")
2832+
prop = FontProperties(size=fontsize)
2833+
ftimage, depth = self.parse(texstr, dpi=dpi, prop=prop)
2834+
return depth

0 commit comments

Comments
 (0)