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

Skip to content

Commit 9a185c6

Browse files
committed
Deduplicate RendererAgg.draw_tex with draw_{mathtext,text}
All the glyph drawing loop is the same, other than a slight transform difference with slant/extend in the former.
1 parent 6ec8fc8 commit 9a185c6

1 file changed

Lines changed: 12 additions & 37 deletions

File tree

lib/matplotlib/backends/backend_agg.py

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,13 @@ def _draw_text_glyphs(self, gc, x, y, angle, glyphs):
177177
cos = math.cos(math.radians(angle))
178178
sin = math.sin(math.radians(angle))
179179
load_flags = get_hinting_flag()
180-
for font, size, glyph_index, dx, dy in glyphs: # dy is upwards.
180+
for font, size, glyph_index, slant, extend, dx, dy in glyphs: # dy is upwards.
181181
font.set_size(size, self.dpi)
182182
hf = font._hinting_factor
183183
font._set_transform(
184-
[[round(0x10000 * cos / hf), round(0x10000 * -sin)],
185-
[round(0x10000 * sin / hf), round(0x10000 * cos)]],
184+
(0x10000 * np.array([[cos, -sin], [sin, cos]])
185+
@ [[extend, extend * slant], [0, 1]]
186+
@ [[1 / hf, 0], [0, 1]]).round().astype(int),
186187
[round(0x40 * (x + dx * cos - dy * sin)),
187188
# FreeType's y is upwards.
188189
round(0x40 * (self.height - y + dx * sin + dy * cos))]
@@ -205,7 +206,7 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
205206
s, self.dpi, prop, antialiased=gc.get_antialiased())
206207
self._draw_text_glyphs(
207208
gc, x, y, angle,
208-
((font, size, glyph_index, dx, dy)
209+
((font, size, glyph_index, 0, 1, dx, dy)
209210
for font, size, _char, glyph_index, dx, dy in parse.glyphs))
210211
rgba = gc.get_rgb()
211212
if len(rgba) == 3 or gc.get_forced_alpha():
@@ -243,7 +244,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
243244
size = prop.get_size_in_points()
244245
self._draw_text_glyphs(
245246
gc, x, y, angle,
246-
((item.ft_object, size, item.glyph_index, item.x, item.y)
247+
((item.ft_object, size, item.glyph_index, 0, 1, item.x, item.y)
247248
for item in items))
248249

249250
def get_text_width_height_descent(self, s, prop, ismath):
@@ -287,38 +288,12 @@ def draw_tex(self, gc, x, y, s, prop, angle, *, mtext=None):
287288
with Dvi(dvifile, self.dpi) as dvi:
288289
page, = dvi
289290

290-
cos = math.cos(math.radians(angle))
291-
sin = math.sin(math.radians(angle))
292-
293-
for text in page.text:
294-
hf = mpl.rcParams["text.hinting_factor"]
295-
# Resolving text.index will implicitly call get_font(), which
296-
# resets the font transform, so it has to be done before explicitly
297-
# setting the font transform below.
298-
index = text.index
299-
font = get_font(text.font_path)
300-
font.set_size(text.font_size, self.dpi)
301-
slant = text.font_effects.get("slant", 0)
302-
extend = text.font_effects.get("extend", 1)
303-
font._set_transform(
304-
(0x10000 * np.array([[cos, -sin], [sin, cos]])
305-
@ [[extend, extend * slant], [0, 1]]
306-
@ [[1 / hf, 0], [0, 1]]).round().astype(int),
307-
[round(0x40 * (x + text.x * cos - text.y * sin)),
308-
# FreeType's y is upwards.
309-
round(0x40 * (self.height - y + text.x * sin + text.y * cos))]
310-
)
311-
bitmap = font._render_glyph(
312-
index, get_hinting_flag(),
313-
RenderMode.NORMAL if gc.get_antialiased() else RenderMode.MONO)
314-
buffer = np.asarray(bitmap.buffer)
315-
if not gc.get_antialiased():
316-
buffer *= 0xff
317-
# draw_text_image's y is downwards & the bitmap bottom side.
318-
self._renderer.draw_text_image(
319-
buffer,
320-
bitmap.left, int(self.height) - bitmap.top + buffer.shape[0],
321-
0, gc)
291+
self._draw_text_glyphs(
292+
gc, x, y, angle,
293+
((get_font(text.font_path), text.font_size, text.index,
294+
text.font_effects.get('slant', 0), text.font_effects.get('extend', 1),
295+
text.x, text.y)
296+
for text in page.text))
322297

323298
rgba = gc.get_rgb()
324299
if len(rgba) == 3 or gc.get_forced_alpha():

0 commit comments

Comments
 (0)