Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6ec8fc8

Browse files
committed
Deduplicate RendererAgg.draw_{mathtext,text}
All the glyph drawing loop is the same, other than a flexible size in the former.
1 parent 931bcf3 commit 6ec8fc8

1 file changed

Lines changed: 20 additions & 30 deletions

File tree

lib/matplotlib/backends/backend_agg.py

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,12 @@ def draw_path(self, gc, path, transform, rgbFace=None):
172172

173173
raise OverflowError(msg) from None
174174

175-
def draw_mathtext(self, gc, x, y, s, prop, angle):
176-
"""Draw mathtext using :mod:`matplotlib.mathtext`."""
175+
def _draw_text_glyphs(self, gc, x, y, angle, glyphs):
177176
# y is downwards.
178-
parse = self.mathtext_parser.parse(
179-
s, self.dpi, prop, antialiased=gc.get_antialiased())
180177
cos = math.cos(math.radians(angle))
181178
sin = math.sin(math.radians(angle))
182-
for font, size, _char, glyph_index, dx, dy in parse.glyphs: # dy is upwards.
179+
load_flags = get_hinting_flag()
180+
for font, size, glyph_index, dx, dy in glyphs: # dy is upwards.
183181
font.set_size(size, self.dpi)
184182
hf = font._hinting_factor
185183
font._set_transform(
@@ -190,16 +188,25 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
190188
round(0x40 * (self.height - y + dx * sin + dy * cos))]
191189
)
192190
bitmap = font._render_glyph(
193-
glyph_index, get_hinting_flag(),
191+
glyph_index, load_flags,
194192
RenderMode.NORMAL if gc.get_antialiased() else RenderMode.MONO)
195-
buffer = np.asarray(bitmap.buffer)
193+
buffer = bitmap.buffer
196194
if not gc.get_antialiased():
197195
buffer *= 0xff
198196
# draw_text_image's y is downwards & the bitmap bottom side.
199197
self._renderer.draw_text_image(
200198
buffer,
201199
bitmap.left, int(self.height) - bitmap.top + buffer.shape[0],
202200
0, gc)
201+
202+
def draw_mathtext(self, gc, x, y, s, prop, angle):
203+
"""Draw mathtext using :mod:`matplotlib.mathtext`."""
204+
parse = self.mathtext_parser.parse(
205+
s, self.dpi, prop, antialiased=gc.get_antialiased())
206+
self._draw_text_glyphs(
207+
gc, x, y, angle,
208+
((font, size, glyph_index, dx, dy)
209+
for font, size, _char, glyph_index, dx, dy in parse.glyphs))
203210
rgba = gc.get_rgb()
204211
if len(rgba) == 3 or gc.get_forced_alpha():
205212
rgba = rgba[:3] + (gc.get_alpha(),)
@@ -229,32 +236,15 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
229236
if ismath:
230237
return self.draw_mathtext(gc, x, y, s, prop, angle)
231238
font = self._prepare_font(prop)
232-
cos = math.cos(math.radians(angle))
233-
sin = math.sin(math.radians(angle))
234-
load_flags = get_hinting_flag()
235239
items = font._layout(
236-
s, flags=load_flags,
240+
s, flags=get_hinting_flag(),
237241
features=mtext.get_fontfeatures() if mtext is not None else None,
238242
language=mtext.get_language() if mtext is not None else None)
239-
for item in items:
240-
hf = item.ft_object._hinting_factor
241-
item.ft_object._set_transform(
242-
[[round(0x10000 * cos / hf), round(0x10000 * -sin)],
243-
[round(0x10000 * sin / hf), round(0x10000 * cos)]],
244-
[round(0x40 * (x + item.x * cos - item.y * sin)),
245-
# FreeType's y is upwards.
246-
round(0x40 * (self.height - y + item.x * sin + item.y * cos))]
247-
)
248-
bitmap = item.ft_object._render_glyph(
249-
item.glyph_index, load_flags,
250-
RenderMode.NORMAL if gc.get_antialiased() else RenderMode.MONO)
251-
buffer = bitmap.buffer
252-
if not gc.get_antialiased():
253-
buffer *= 0xff
254-
self._renderer.draw_text_image(
255-
buffer,
256-
bitmap.left, int(self.height) - bitmap.top + buffer.shape[0],
257-
0, gc)
243+
size = prop.get_size_in_points()
244+
self._draw_text_glyphs(
245+
gc, x, y, angle,
246+
((item.ft_object, size, item.glyph_index, item.x, item.y)
247+
for item in items))
258248

259249
def get_text_width_height_descent(self, s, prop, ismath):
260250
# docstring inherited

0 commit comments

Comments
 (0)