@@ -111,7 +111,8 @@ def latex_to_png(s, encode=False, backend=None, wrap=False, color='Black',
111111
112112def latex_to_png_mpl (s , wrap , color = 'Black' , scale = 1.0 ):
113113 try :
114- from matplotlib import mathtext
114+ from matplotlib import figure , font_manager , mathtext
115+ from matplotlib .backends import backend_agg
115116 from pyparsing import ParseFatalException
116117 except ImportError :
117118 return None
@@ -122,11 +123,18 @@ def latex_to_png_mpl(s, wrap, color='Black', scale=1.0):
122123 s = u'${0}$' .format (s )
123124
124125 try :
125- mt = mathtext .MathTextParser ('bitmap' )
126- f = BytesIO ()
127- dpi = 120 * scale
128- mt .to_png (f , s , fontsize = 12 , dpi = dpi , color = color )
129- return f .getvalue ()
126+ prop = font_manager .FontProperties (size = 12 )
127+ dpi = 120 * scale
128+ buffer = BytesIO ()
129+
130+ # Adapted from mathtext.math_to_image
131+ parser = mathtext .MathTextParser ("path" )
132+ width , height , depth , _ , _ = parser .parse (s , dpi = 72 , prop = prop )
133+ fig = figure .Figure (figsize = (width / 72 , height / 72 ))
134+ fig .text (0 , depth / height , s , fontproperties = prop , color = color )
135+ backend_agg .FigureCanvasAgg (fig )
136+ fig .savefig (buffer , dpi = dpi , format = "png" , transparent = True )
137+ return buffer .getvalue ()
130138 except (ValueError , RuntimeError , ParseFatalException ):
131139 return None
132140
0 commit comments