143143from matplotlib .afm import AFM
144144from matplotlib .cbook import enumerate , iterable , Bunch , get_realpath_and_stat , \
145145 is_string_like
146- from matplotlib .ft2font import FT2Font , KERNING_DEFAULT , LOAD_DEFAULT , LOAD_NO_HINTING
146+ from matplotlib .ft2font import FT2Font , FT2Image , KERNING_DEFAULT , LOAD_DEFAULT , LOAD_NO_HINTING
147147from matplotlib .font_manager import findfont , FontProperties
148148from matplotlib ._mathtext_data import latex_to_bakoma , \
149149 latex_to_standard , tex2uni , type12uni , tex2type1 , uni2type1
@@ -288,28 +288,27 @@ class MathtextBackendAggRender(MathtextBackend):
288288 def __init__ (self ):
289289 self .ox = 0
290290 self .oy = 0
291+ self .image = None
291292 MathtextBackend .__init__ (self )
292293
293294 def set_canvas_size (self , w , h , d ):
294295 MathtextBackend .set_canvas_size (self , w , h , d )
295- for font in self .fonts_object .get_fonts ():
296- font .set_bitmap_size (int (w ), int (h ) + int (d ))
296+ self .image = FT2Image (ceil (w ), ceil (h + d ))
297297
298298 def render_glyph (self , ox , oy , info ):
299299 info .font .draw_glyph_to_bitmap (
300- ox , oy - info .metrics .ymax , info .glyph )
300+ self . image , ox , oy - info .metrics .ymax , info .glyph )
301301
302302 def render_rect_filled (self , x1 , y1 , x2 , y2 ):
303- font = self .fonts_object .get_fonts ()[0 ]
304- font .draw_rect_filled (x1 , y1 , x2 , max (y2 - 1 , y1 ))
303+ self .image .draw_rect_filled (x1 , y1 , x2 , max (y2 - 1 , y1 ))
305304
306305 def get_results (self , box ):
307306 return (self .ox ,
308307 self .oy ,
309308 self .width ,
310309 self .height + self .depth ,
311310 self .depth ,
312- self .fonts_object . get_fonts () ,
311+ self .image ,
313312 self .fonts_object .get_used_characters ())
314313
315314 def get_hinting_type (self ):
@@ -318,6 +317,13 @@ def get_hinting_type(self):
318317def MathtextBackendAgg ():
319318 return MathtextBackendBbox (MathtextBackendAggRender ())
320319
320+ class MathtextBackendBitmapRender (MathtextBackendAggRender ):
321+ def get_results (self , box ):
322+ return self .image
323+
324+ def MathtextBackendBitmap ():
325+ return MathtextBackendBbox (MathtextBackendBitmapRender ())
326+
321327class MathtextBackendPs (MathtextBackend ):
322328 def __init__ (self ):
323329 self .pswriter = StringIO ()
@@ -2443,6 +2449,7 @@ class MathTextParser(object):
24432449 _parser = None
24442450
24452451 _backend_mapping = {
2452+ 'Bitmap' : MathtextBackendBitmap ,
24462453 'Agg' : MathtextBackendAgg ,
24472454 'PS' : MathtextBackendPs ,
24482455 'Pdf' : MathtextBackendPdf ,
@@ -2454,7 +2461,10 @@ def __init__(self, output):
24542461 self ._output = output
24552462 self ._cache = {}
24562463
2457- def parse (self , s , dpi , prop ):
2464+ def parse (self , s , dpi = 72 , prop = None ):
2465+ if prop is None :
2466+ prop = FontProperties ()
2467+
24582468 cacheKey = (s , dpi , hash (prop ))
24592469 result = self ._cache .get (cacheKey )
24602470 if result is not None :
0 commit comments