FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Rework/fix Text layout cache. by anntzer · Pull Request #22271 · matplotlib/matplotlib · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
24 changes: 24 additions & 0 deletions lib/matplotlib/tests/test_text.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -764,3 +764,27 @@ def test_pdf_chars_beyond_bmp():
plt.rcParams['mathtext.fontset'] = 'stixsans'
plt.figure()
plt.figtext(0.1, 0.5, "Mass $m$ \U00010308", size=30)


@needs_usetex
def test_metrics_cache():
fig = plt.figure()
fig.text(.3, .5, "foo\nbar")
fig.text(.3, .5, "foo\nbar", usetex=True)
fig.text(.5, .5, "foo\nbar", usetex=True)
fig.canvas.draw()
renderer = fig._cachedRenderer
ys = {} # mapping of strings to where they were drawn in y with draw_tex.

def call(*args, **kwargs):
renderer, x, y, s, *_ = args
ys.setdefault(s, set()).add(y)

renderer.draw_tex = call
fig.canvas.draw()
assert [*ys] == ["foo", "bar"]
# Check that both TeX strings were drawn with the same y-position for both
# single-line substrings. Previously, there used to be an incorrect cache
# collision with the non-TeX string (drawn first here) whose metrics would
# get incorrectly reused by the first TeX string.
assert len(ys["foo"]) == len(ys["bar"]) == 1
43 changes: 20 additions & 23 deletions lib/matplotlib/text.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -273,30 +273,29 @@ def update_from(self, other):
self._linespacing = other._linespacing
self.stale = True

def _get_layout_cache_key(self, renderer):
"""
Return a hashable tuple of properties that lets `_get_layout` know
whether a previously computed layout can be reused.
"""
return (
self.get_unitless_position(), self.get_text(),
hash(self._fontproperties),
self._verticalalignment, self._horizontalalignment,
self._linespacing,
self._rotation, self._rotation_mode, self._transform_rotates_text,
self.figure.dpi, weakref.ref(renderer),
def _get_text_metrics_with_cache(
self, renderer, text, fontproperties, ismath):
"""
Call ``renderer.get_text_width_height_descent``, caching the results.
"""
cache_key = (
weakref.ref(renderer),
text,
hash(fontproperties),
ismath,
self.figure.dpi,
)
if cache_key not in self._cached:
self._cached[cache_key] = renderer.get_text_width_height_descent(
text, fontproperties, ismath)
return self._cached[cache_key]

def _get_layout(self, renderer):
"""
Return the extent (bbox) of the text together with
multiple-alignment information. Note that it returns an extent
of a rotated text when necessary.
"""
key = self._get_layout_cache_key(renderer=renderer)
if key in self._cached:
return self._cached[key]

thisx, thisy = 0.0, 0.0
lines = self.get_text().split("\n") # Ensures lines is not empty.

Expand All @@ -306,16 +305,16 @@ def _get_layout(self, renderer):
ys = []

# Full vertical extent of font, including ascenders and descenders:
_, lp_h, lp_d = renderer.get_text_width_height_descent(
"lp", self._fontproperties,
_, lp_h, lp_d = self._get_text_metrics_with_cache(
renderer, "lp", self._fontproperties,
ismath="TeX" if self.get_usetex() else False)
min_dy = (lp_h - lp_d) * self._linespacing

for i, line in enumerate(lines):
clean_line, ismath = self._preprocess_math(line)
if clean_line:
w, h, d = renderer.get_text_width_height_descent(
clean_line, self._fontproperties, ismath=ismath)
w, h, d = self._get_text_metrics_with_cache(
renderer, clean_line, self._fontproperties, ismath)
else:
w = h = d = 0

Expand Down Expand Up @@ -439,9 +438,7 @@ def _get_layout(self, renderer):
# now rotate the positions around the first (x, y) position
xys = M.transform(offset_layout) - (offsetx, offsety)

ret = bbox, list(zip(lines, zip(ws, hs), *xys.T)), descent
self._cached[key] = ret
return ret
return bbox, list(zip(lines, zip(ws, hs), *xys.T)), descent

def set_bbox(self, rectprops):
"""
Expand Down

Back | FazBrowse Home | New Git URL