@@ -70,7 +70,7 @@ def __init__(self, width, height, dpi):
7070 self ._filter_renderers = []
7171
7272 self ._update_methods ()
73- self .mathtext_parser = MathTextParser ('agg ' )
73+ self .mathtext_parser = MathTextParser ('path ' )
7474
7575 self .bbox = Bbox .from_bounds (0 , 0 , self .width , self .height )
7676
@@ -172,36 +172,58 @@ def draw_path(self, gc, path, transform, rgbFace=None):
172172
173173 def draw_mathtext (self , gc , x , y , s , prop , angle ):
174174 """Draw mathtext using :mod:`matplotlib.mathtext`."""
175- ox , oy , width , height , descent , font_image = \
176- self .mathtext_parser .parse (s , self .dpi , prop ,
177- antialiased = gc .get_antialiased ())
178-
179- xd = descent * sin (radians (angle ))
180- yd = descent * cos (radians (angle ))
181- x = round (x + ox + xd )
182- y = round (y - oy + yd )
183- self ._renderer .draw_text_image (font_image , x , y + 1 , angle , gc )
175+ # y is downwards.
176+ parse = self .mathtext_parser .parse (
177+ s , self .dpi , prop , antialiased = gc .get_antialiased ())
178+ c = cos (radians (angle ))
179+ s = sin (radians (angle ))
180+ for font , size , char , dx , dy in parse .glyphs : # dy is upwards.
181+ font .set_size (size , self .dpi )
182+ bitmap = font ._render_glyph (
183+ font .get_char_index (char ),
184+ # The "y" parameter is upwards (per FreeType).
185+ x + dx * c - dy * s , self .height - y + dx * s + dy * c , angle ,
186+ get_hinting_flag ())
187+ # draw_text_image's y is downwards & the bitmap bottom side.
188+ self ._renderer .draw_text_image (
189+ bitmap ["buffer" ],
190+ bitmap ["left" ],
191+ int (self .height ) - bitmap ["top" ] + bitmap ["buffer" ].shape [0 ],
192+ 0 , gc )
193+ if not angle :
194+ for dx , dy , w , h in parse .rects : # dy is upwards & the rect top side.
195+ self ._renderer .draw_text_image (
196+ np .full ((round (h ), round (w )), np .uint8 (0xff )),
197+ round (x + dx ), round (y - dy - h ),
198+ 0 , gc )
199+ else :
200+ rgba = gc .get_rgb ()
201+ if len (rgba ) == 3 or gc .get_forced_alpha ():
202+ rgba = rgba [:3 ] + (gc .get_alpha (),)
203+ gc1 = self .new_gc ()
204+ gc1 .set_linewidth (0 )
205+ for dx , dy , w , h in parse .rects : # dy is upwards & the rect top side.
206+ path = Path ._create_closed (
207+ [(dx , dy ), (dx + w , dy ), (dx + w , dy + h ), (dx , dy + h )])
208+ self ._renderer .draw_path (
209+ gc1 , path ,
210+ mpl .transforms .Affine2D ()
211+ .rotate_deg (angle ).translate (x , self .height - y ),
212+ rgba )
213+ gc1 .restore ()
184214
185215 def draw_text (self , gc , x , y , s , prop , angle , ismath = False , mtext = None ):
186216 # docstring inherited
187217 if ismath :
188218 return self .draw_mathtext (gc , x , y , s , prop , angle )
189219 font = self ._prepare_font (prop )
190- # We pass '0' for angle here, since it will be rotated (in raster
191- # space) in the following call to draw_text_image).
192- font .set_text (s , 0 , flags = get_hinting_flag ())
193- font .draw_glyphs_to_bitmap (
194- antialiased = gc .get_antialiased ())
195- d = font .get_descent () / 64.0
196- # The descent needs to be adjusted for the angle.
197- xo , yo = font .get_bitmap_offset ()
198- xo /= 64.0
199- yo /= 64.0
200- xd = d * sin (radians (angle ))
201- yd = d * cos (radians (angle ))
202- x = round (x + xo + xd )
203- y = round (y + yo + yd )
204- self ._renderer .draw_text_image (font , x , y + 1 , angle , gc )
220+ font .set_text (s , angle , flags = get_hinting_flag ())
221+ for bitmap in font ._render_glyphs (x , self .height - y ):
222+ self ._renderer .draw_text_image (
223+ bitmap ["buffer" ],
224+ bitmap ["left" ],
225+ int (self .height ) - bitmap ["top" ] + bitmap ["buffer" ].shape [0 ],
226+ 0 , gc )
205227
206228 def get_text_width_height_descent (self , s , prop , ismath ):
207229 # docstring inherited
@@ -211,9 +233,8 @@ def get_text_width_height_descent(self, s, prop, ismath):
211233 return super ().get_text_width_height_descent (s , prop , ismath )
212234
213235 if ismath :
214- ox , oy , width , height , descent , font_image = \
215- self .mathtext_parser .parse (s , self .dpi , prop )
216- return width , height , descent
236+ parse = self .mathtext_parser .parse (s , self .dpi , prop )
237+ return parse .width , parse .height , parse .depth
217238
218239 font = self ._prepare_font (prop )
219240 font .set_text (s , 0.0 , flags = get_hinting_flag ())
0 commit comments