|
From: <md...@us...> - 2008-06-17 15:44:21
|
Revision: 5577
http://matplotlib.svn.sourceforge.net/matplotlib/?rev=5577&view=rev
Author: mdboom
Date: 2008-06-17 08:42:12 -0700 (Tue, 17 Jun 2008)
Log Message:
-----------
Align inline mathtext correctly in HTML docs (by adjusting for the baseline)
Modified Paths:
--------------
trunk/matplotlib/doc/sphinxext/mathpng.py
trunk/matplotlib/examples/user_interfaces/mathtext_wx.py
trunk/matplotlib/lib/matplotlib/mathtext.py
Modified: trunk/matplotlib/doc/sphinxext/mathpng.py
===================================================================
--- trunk/matplotlib/doc/sphinxext/mathpng.py 2008-06-17 15:09:09 UTC (rev 5576)
+++ trunk/matplotlib/doc/sphinxext/mathpng.py 2008-06-17 15:42:12 UTC (rev 5577)
@@ -113,16 +113,19 @@
# This uses mathtext to render the expression
def latex2png(latex, filename, fontset='cm'):
+ latex = "$%s$" % latex
if os.path.exists(filename):
- return
+ return mathtext_parser.get_depth(latex, dpi=120)
orig_fontset = rcParams['mathtext.fontset']
rcParams['mathtext.fontset'] = fontset
try:
- mathtext_parser.to_png(filename, "$%s$" % latex, dpi=120)
+ depth = mathtext_parser.to_png(filename, latex, dpi=120)
except:
- warnings.warn("Could not render math expression $%s$" % latex,
- warnings.Warning)
+ warnings.warn("Could not render math expression %s" % latex,
+ Warning)
+ depth = 0
rcParams['mathtext.fontset'] = orig_fontset
+ return depth
# LaTeX to HTML translation stuff:
def latex2html(node, source):
@@ -131,8 +134,7 @@
print latex.encode("ascii", "backslashreplace")
name = 'math-%s' % md5(latex).hexdigest()[-10:]
dest = '_static/%s.png' % name
- if not isfile(dest):
- latex2png(latex, dest, node['fontset'])
+ depth = latex2png(latex, dest, node['fontset'])
path = '_static'
count = source.split('/doc/')[-1].count('/')
@@ -148,5 +150,10 @@
cls = ''
else:
cls = 'class="center" '
- return '<img src="%s/%s.png" %s%s/>' % (path, name, align, cls)
+ if inline and depth != 0:
+ style = 'style="position: relative; bottom: -%dpx"' % (depth + 1)
+ else:
+ style = ''
+ return '<img src="%s/%s.png" %s%s%s/>' % (path, name, align, cls, style)
+
Modified: trunk/matplotlib/examples/user_interfaces/mathtext_wx.py
===================================================================
--- trunk/matplotlib/examples/user_interfaces/mathtext_wx.py 2008-06-17 15:09:09 UTC (rev 5576)
+++ trunk/matplotlib/examples/user_interfaces/mathtext_wx.py 2008-06-17 15:42:12 UTC (rev 5577)
@@ -21,7 +21,7 @@
from matplotlib.mathtext import MathTextParser
mathtext_parser = MathTextParser("Bitmap")
def mathtext_to_wxbitmap(s):
- ftimage = mathtext_parser.parse(s, 150)
+ ftimage, depth = mathtext_parser.parse(s, 150)
return wx.BitmapFromBufferRGBA(
ftimage.get_width(), ftimage.get_height(),
ftimage.as_rgba_str())
Modified: trunk/matplotlib/lib/matplotlib/mathtext.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/mathtext.py 2008-06-17 15:09:09 UTC (rev 5576)
+++ trunk/matplotlib/lib/matplotlib/mathtext.py 2008-06-17 15:42:12 UTC (rev 5577)
@@ -341,7 +341,7 @@
class MathtextBackendBitmapRender(MathtextBackendAggRender):
def get_results(self, box):
- return self.image
+ return self.image, self.depth
def MathtextBackendBitmap():
return MathtextBackendBbox(MathtextBackendBitmapRender())
@@ -2726,8 +2726,14 @@
def to_mask(self, texstr, dpi=120, fontsize=14):
"""
- return an NxM uint8 alpha ubyte mask array of rasterized tex
+ Returns a tuple (*array*, *depth*)
+ - *array* is an NxM uint8 alpha ubyte mask array of
+ rasterized tex.
+
+ - depth is the offset of the baseline from the bottom of the
+ image in pixels.
+
''texstr''
A valid mathtext string, eg r'IQ: $\sigma_i=15$'
@@ -2739,15 +2745,23 @@
"""
assert(self._output=="bitmap")
prop = FontProperties(size=fontsize)
- ftimage = self.parse(texstr, dpi=dpi, prop=prop)
+ ftimage, depth = self.parse(texstr, dpi=dpi, prop=prop)
x = ftimage.as_array()
- return x
+ return x, depth
def to_rgba(self, texstr, color='black', dpi=120, fontsize=14):
"""
- return an NxMx4 RGBA array of ubyte rasterized tex
+ Returns a tuple (*array*, *depth*)
+ - *array* is an NxMx4 RGBA array of ubyte rasterized tex.
+
+ - depth is the offset of the baseline from the bottom of the
+ image in pixels.
+
+ Returns a tuple (array, depth), where depth is the offset of
+ the baseline from the bottom of the image.
+
''texstr''
A valid mathtext string, eg r'IQ: $\sigma_i=15$'
@@ -2760,7 +2774,7 @@
''fontsize''
The font size in points
"""
- x = self.to_mask(texstr, dpi=dpi, fontsize=fontsize)
+ x, depth = self.to_mask(texstr, dpi=dpi, fontsize=fontsize)
r, g, b = mcolors.colorConverter.to_rgb(color)
RGBA = np.zeros((x.shape[0], x.shape[1], 4), dtype=np.uint8)
@@ -2768,11 +2782,15 @@
RGBA[:,:,1] = int(255*g)
RGBA[:,:,2] = int(255*b)
RGBA[:,:,3] = x
- return RGBA
+ return RGBA, depth
def to_png(self, filename, texstr, color='black', dpi=120, fontsize=14):
"""
+ Writes a tex expression to a PNG file.
+ Returns the offset of the baseline from the bottom of the
+ image in pixels.
+
''filename''
A writable filename or fileobject
@@ -2790,7 +2808,27 @@
"""
- rgba = self.to_rgba(texstr, color=color, dpi=dpi, fontsize=fontsize)
+ rgba, depth = self.to_rgba(texstr, color=color, dpi=dpi, fontsize=fontsize)
numrows, numcols, tmp = rgba.shape
- return _png.write_png(rgba.tostring(), numcols, numrows, filename)
+ _png.write_png(rgba.tostring(), numcols, numrows, filename)
+ return depth
+ def get_depth(self, texstr, dpi=120, fontsize=14):
+ """
+ Returns the offset of the baseline from the bottom of the
+ image in pixels.
+
+ ''texstr''
+ A valid mathtext string, eg r'IQ: $\sigma_i=15$'
+
+ ''dpi''
+ The dots-per-inch to render the text
+
+ ''fontsize''
+ The font size in points
+
+ """
+ assert(self._output=="bitmap")
+ prop = FontProperties(size=fontsize)
+ ftimage, depth = self.parse(texstr, dpi=dpi, prop=prop)
+ return depth
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|