@@ -71,7 +71,7 @@ def __init__(self, width, height, dpi):
71
71
self ._filter_renderers = []
72
72
73
73
self ._update_methods ()
74
- self .mathtext_parser = MathTextParser ('agg ' )
74
+ self .mathtext_parser = MathTextParser ('path ' )
75
75
76
76
self .bbox = Bbox .from_bounds (0 , 0 , self .width , self .height )
77
77
@@ -173,48 +173,60 @@ def draw_path(self, gc, path, transform, rgbFace=None):
173
173
174
174
def draw_mathtext (self , gc , x , y , s , prop , angle ):
175
175
"""Draw mathtext using :mod:`matplotlib.mathtext`."""
176
- ox , oy , width , height , descent , font_image = \
177
- self .mathtext_parser .parse (s , self .dpi , prop ,
178
- antialiased = gc .get_antialiased ())
179
-
180
- xd = descent * sin (radians (angle ))
181
- yd = descent * cos (radians (angle ))
182
- x = round (x + ox + xd )
183
- y = round (y - oy + yd )
184
- self ._renderer .draw_text_image (font_image , x , y + 1 , angle , gc )
176
+ # y is downwards.
177
+ parse = self .mathtext_parser .parse (
178
+ s , self .dpi , prop , antialiased = gc .get_antialiased ())
179
+ c = cos (radians (angle ))
180
+ s = sin (radians (angle ))
181
+ for font , size , char , dx , dy in parse .glyphs : # dy is upwards.
182
+ font .set_size (size , self .dpi )
183
+ bitmap = font ._render_glyph (
184
+ font .get_char_index (char ),
185
+ # The "y" parameter is upwards (per FreeType).
186
+ x + dx * c - dy * s , self .height - y + dx * s + dy * c , angle ,
187
+ get_hinting_flag ())
188
+ # draw_text_image's y is downwards & the bitmap bottom side.
189
+ self ._renderer .draw_text_image (
190
+ bitmap ["buffer" ],
191
+ bitmap ["left" ],
192
+ int (self .height ) - bitmap ["top" ] + bitmap ["buffer" ].shape [0 ],
193
+ 0 , gc )
194
+ if not angle :
195
+ for dx , dy , w , h in parse .rects : # dy is upwards & the rect top side.
196
+ self ._renderer .draw_text_image (
197
+ np .full ((round (h ), round (w )), np .uint8 (0xff )),
198
+ round (x + dx ), round (y - dy - h ),
199
+ 0 , gc )
200
+ else :
201
+ rgba = gc .get_rgb ()
202
+ if len (rgba ) == 3 or gc .get_forced_alpha ():
203
+ rgba = rgba [:3 ] + (gc .get_alpha (),)
204
+ gc1 = self .new_gc ()
205
+ gc1 .set_linewidth (0 )
206
+ for dx , dy , w , h in parse .rects : # dy is upwards & the rect top side.
207
+ path = Path ._create_closed (
208
+ [(dx , dy ), (dx + w , dy ), (dx + w , dy + h ), (dx , dy + h )])
209
+ self ._renderer .draw_path (
210
+ gc1 , path ,
211
+ mpl .transforms .Affine2D ()
212
+ .rotate_deg (angle ).translate (x , self .height - y ),
213
+ rgba )
214
+ gc1 .restore ()
185
215
186
216
def draw_text (self , gc , x , y , s , prop , angle , ismath = False , mtext = None ):
187
217
# docstring inherited
188
218
if ismath :
189
219
return self .draw_mathtext (gc , x , y , s , prop , angle )
190
220
font = self ._prepare_font (prop )
191
- # We pass '0' for angle here, since it will be rotated (in raster
192
- # space) in the following call to draw_text_image).
193
221
font .set_text (s , 0 , flags = get_hinting_flag (),
194
222
features = mtext .get_fontfeatures () if mtext is not None else None ,
195
223
language = mtext .get_language () if mtext is not None else None )
196
- font .draw_glyphs_to_bitmap (
197
- antialiased = gc .get_antialiased ())
198
- d = font .get_descent () / 64.0
199
- # The descent needs to be adjusted for the angle.
200
- xo , yo = font .get_bitmap_offset ()
201
- xo /= 64.0
202
- yo /= 64.0
203
-
204
- rad = radians (angle )
205
- xd = d * sin (rad )
206
- yd = d * cos (rad )
207
- # Rotating the offset vector ensures text rotates around the anchor point.
208
- # Without this, rotated text offsets incorrectly, causing a horizontal shift.
209
- # Applying the 2D rotation matrix.
210
- rotated_xo = xo * cos (rad ) - yo * sin (rad )
211
- rotated_yo = xo * sin (rad ) + yo * cos (rad )
212
- # Subtract rotated_yo to account for the inverted y-axis in computer graphics,
213
- # compared to the mathematical convention.
214
- x = round (x + rotated_xo + xd )
215
- y = round (y - rotated_yo + yd )
216
-
217
- self ._renderer .draw_text_image (font , x , y + 1 , angle , gc )
224
+ for bitmap in font ._render_glyphs (x , self .height - y ):
225
+ self ._renderer .draw_text_image (
226
+ bitmap ["buffer" ],
227
+ bitmap ["left" ],
228
+ int (self .height ) - bitmap ["top" ] + bitmap ["buffer" ].shape [0 ],
229
+ 0 , gc )
218
230
219
231
def get_text_width_height_descent (self , s , prop , ismath ):
220
232
# docstring inherited
@@ -224,9 +236,8 @@ def get_text_width_height_descent(self, s, prop, ismath):
224
236
return super ().get_text_width_height_descent (s , prop , ismath )
225
237
226
238
if ismath :
227
- ox , oy , width , height , descent , font_image = \
228
- self .mathtext_parser .parse (s , self .dpi , prop )
229
- return width , height , descent
239
+ parse = self .mathtext_parser .parse (s , self .dpi , prop )
240
+ return parse .width , parse .height , parse .depth
230
241
231
242
font = self ._prepare_font (prop )
232
243
font .set_text (s , 0.0 , flags = get_hinting_flag ())
0 commit comments