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

Skip to content

Commit 49d5ac9

Browse files
committed
Also deduplicate box rendering in RendererAgg.draw_{mathtext,tex}
1 parent 0aac6a6 commit 49d5ac9

1 file changed

Lines changed: 18 additions & 42 deletions

File tree

lib/matplotlib/backends/backend_agg.py

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

173173
raise OverflowError(msg) from None
174174

175-
def _draw_text_glyphs(self, gc, x, y, angle, glyphs):
175+
def _draw_text_glyphs_and_boxes(self, gc, x, y, angle, glyphs, boxes):
176176
# y is downwards.
177177
cos = math.cos(math.radians(angle))
178178
sin = math.sin(math.radians(angle))
@@ -200,21 +200,13 @@ def _draw_text_glyphs(self, gc, x, y, angle, glyphs):
200200
bitmap.left, int(self.height) - bitmap.top + buffer.shape[0],
201201
0, gc)
202202

203-
def draw_mathtext(self, gc, x, y, s, prop, angle):
204-
"""Draw mathtext using :mod:`matplotlib.mathtext`."""
205-
parse = self.mathtext_parser.parse(
206-
s, self.dpi, prop, antialiased=gc.get_antialiased())
207-
self._draw_text_glyphs(
208-
gc, x, y, angle,
209-
((font, size, glyph_index, 0, 1, dx, dy)
210-
for font, size, _char, glyph_index, dx, dy in parse.glyphs))
211203
rgba = gc.get_rgb()
212204
if len(rgba) == 3 or gc.get_forced_alpha():
213205
rgba = rgba[:3] + (gc.get_alpha(),)
214206
gc1 = self.new_gc()
215207
gc1.set_linewidth(0)
216208
gc1.set_snap(gc.get_snap())
217-
for dx, dy, w, h in parse.rects: # dy is upwards.
209+
for dx, dy, w, h in boxes: # dy is upwards.
218210
if gc1.get_snap() in [None, True]:
219211
# Prevent thin bars from disappearing by growing symmetrically.
220212
if w < 1:
@@ -232,6 +224,16 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
232224
rgba)
233225
gc1.restore()
234226

227+
def draw_mathtext(self, gc, x, y, s, prop, angle):
228+
"""Draw mathtext using :mod:`matplotlib.mathtext`."""
229+
parse = self.mathtext_parser.parse(
230+
s, self.dpi, prop, antialiased=gc.get_antialiased())
231+
self._draw_text_glyphs_and_boxes(
232+
gc, x, y, angle,
233+
((font, size, glyph_index, 0, 1, dx, dy)
234+
for font, size, _char, glyph_index, dx, dy in parse.glyphs),
235+
parse.rects)
236+
235237
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
236238
# docstring inherited
237239
if ismath:
@@ -242,10 +244,11 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
242244
features=mtext.get_fontfeatures() if mtext is not None else None,
243245
language=mtext.get_language() if mtext is not None else None)
244246
size = prop.get_size_in_points()
245-
self._draw_text_glyphs(
247+
self._draw_text_glyphs_and_boxes(
246248
gc, x, y, angle,
247249
((item.ft_object, size, item.glyph_index, 0, 1, item.x, item.y)
248-
for item in items))
250+
for item in items),
251+
[])
249252

250253
def get_text_width_height_descent(self, s, prop, ismath):
251254
# docstring inherited
@@ -288,40 +291,13 @@ def draw_tex(self, gc, x, y, s, prop, angle, *, mtext=None):
288291
with Dvi(dvifile, self.dpi) as dvi:
289292
page, = dvi
290293

291-
self._draw_text_glyphs(
294+
self._draw_text_glyphs_and_boxes(
292295
gc, x, y, angle,
293296
((get_font(text.font_path), text.font_size, text.index,
294297
text.font_effects.get('slant', 0), text.font_effects.get('extend', 1),
295298
text.x, text.y)
296-
for text in page.text))
297-
298-
rgba = gc.get_rgb()
299-
if len(rgba) == 3 or gc.get_forced_alpha():
300-
rgba = rgba[:3] + (gc.get_alpha(),)
301-
gc1 = self.new_gc()
302-
gc1.set_linewidth(0)
303-
gc1.set_snap(gc.get_snap())
304-
for box in page.boxes:
305-
bx = box.x
306-
by = box.y
307-
bw = box.width
308-
bh = box.height
309-
if gc1.get_snap() in [None, True]:
310-
# Prevent thin bars from disappearing by growing symmetrically.
311-
if bw < 1:
312-
bx -= (1 - bw) / 2
313-
bw = 1
314-
if bh < 1:
315-
by -= (1 - bh) / 2
316-
bh = 1
317-
path = Path._create_closed([
318-
(bx, by), (bx + bw, by), (bx + bw, by + bh), (bx, by + bh)])
319-
self._renderer.draw_path(
320-
gc1, path,
321-
mpl.transforms.Affine2D()
322-
.rotate_deg(angle).translate(x, self.height - y),
323-
rgba)
324-
gc1.restore()
299+
for text in page.text),
300+
((box.x, box.y, box.width, box.height) for box in page.boxes))
325301

326302
def get_canvas_width_height(self):
327303
# docstring inherited

0 commit comments

Comments
 (0)