@@ -341,7 +341,7 @@ def MathtextBackendAgg():
341341
342342class MathtextBackendBitmapRender (MathtextBackendAggRender ):
343343 def get_results (self , box ):
344- return self .image
344+ return self .image , self . depth
345345
346346def 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