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

Skip to content

Commit bd1b3c7

Browse files
committed
Deduplicate RendererAgg.draw_mathtext and RendererAgg.draw_text
All the glyph drawing loop is the same, other than a flexible size in the former.
1 parent 03792bd commit bd1b3c7

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
@@ -171,14 +171,12 @@ def draw_path(self, gc, path, transform, rgbFace=None):
171171

172172
raise OverflowError(msg) from None
173173

174-
def draw_mathtext(self, gc, x, y, s, prop, angle):
175-
"""Draw mathtext using :mod:`matplotlib.mathtext`."""
174+
def _draw_text_glyphs(self, gc, x, y, angle, glyphs):
176175
# y is downwards.
177-
parse = self.mathtext_parser.parse(
178-
s, self.dpi, prop, antialiased=gc.get_antialiased())
179176
cos = math.cos(math.radians(angle))
180177
sin = math.sin(math.radians(angle))
181-
for font, size, _char, glyph_index, dx, dy in parse.glyphs: # dy is upwards.
178+
load_flags = get_hinting_flag()
179+
for font, size, glyph_index, dx, dy in glyphs: # dy is upwards.
182180
font.set_size(size, self.dpi)
183181
hf = font._hinting_factor
184182
font._set_transform(
@@ -189,16 +187,25 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
189187
round(0x40 * (self.height - y + dx * sin + dy * cos))]
190188
)
191189
bitmap = font._render_glyph(
192-
glyph_index, get_hinting_flag(),
190+
glyph_index, load_flags,
193191
RenderMode.NORMAL if gc.get_antialiased() else RenderMode.MONO)
194-
buffer = np.asarray(bitmap.buffer)
192+
buffer = bitmap.buffer
195193
if not gc.get_antialiased():
196194
buffer *= 0xff
197195
# draw_text_image's y is downwards & the bitmap bottom side.
198196
self._renderer.draw_text_image(
199197
buffer,
200198
bitmap.left, int(self.height) - bitmap.top + buffer.shape[0],
201199
0, gc)
200+
201+
def draw_mathtext(self, gc, x, y, s, prop, angle):
202+
"""Draw mathtext using :mod:`matplotlib.mathtext`."""
203+
parse = self.mathtext_parser.parse(
204+
s, self.dpi, prop, antialiased=gc.get_antialiased())
205+
self._draw_text_glyphs(
206+
gc, x, y, angle,
207+
((font, size, glyph_index, dx, dy)
208+
for font, size, _char, glyph_index, dx, dy in parse.glyphs))
202209
rgba = gc.get_rgb()
203210
if len(rgba) == 3 or gc.get_forced_alpha():
204211
rgba = rgba[:3] + (gc.get_alpha(),)
@@ -228,32 +235,15 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
228235
if ismath:
229236
return self.draw_mathtext(gc, x, y, s, prop, angle)
230237
font = self._prepare_font(prop)
231-
cos = math.cos(math.radians(angle))
232-
sin = math.sin(math.radians(angle))
233-
load_flags = get_hinting_flag()
234238
items = font._layout(
235-
s, flags=load_flags,
239+
s, flags=get_hinting_flag(),
236240
features=mtext.get_fontfeatures() if mtext is not None else None,
237241
language=mtext.get_language() if mtext is not None else None)
238-
for item in items:
239-
hf = item.ft_object._hinting_factor
240-
item.ft_object._set_transform(
241-
[[round(0x10000 * cos / hf), round(0x10000 * -sin)],
242-
[round(0x10000 * sin / hf), round(0x10000 * cos)]],
243-
[round(0x40 * (x + item.x * cos - item.y * sin)),
244-
# FreeType's y is upwards.
245-
round(0x40 * (self.height - y + item.x * sin + item.y * cos))]
246-
)
247-
bitmap = item.ft_object._render_glyph(
248-
item.glyph_index, load_flags,
249-
RenderMode.NORMAL if gc.get_antialiased() else RenderMode.MONO)
250-
buffer = bitmap.buffer
251-
if not gc.get_antialiased():
252-
buffer *= 0xff
253-
self._renderer.draw_text_image(
254-
buffer,
255-
bitmap.left, int(self.height) - bitmap.top + buffer.shape[0],
256-
0, gc)
242+
size = prop.get_size_in_points()
243+
self._draw_text_glyphs(
244+
gc, x, y, angle,
245+
((item.ft_object, size, item.glyph_index, item.x, item.y)
246+
for item in items))
257247

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

0 commit comments

Comments
 (0)