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

Skip to content

Commit ebf9b14

Browse files
Merge pull request #30991 from anntzer/fpcache
Improve findfont cache invalidation.
2 parents bfebada + 4823c0d commit ebf9b14

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

lib/matplotlib/font_manager.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,9 +1344,11 @@ def findfont(self, prop, fontext='ttf', directory=None,
13441344
# Pass the relevant rcParams (and the font manager, as `self`) to
13451345
# _findfont_cached so to prevent using a stale cache entry after an
13461346
# rcParam was changed.
1347-
rc_params = tuple(tuple(mpl.rcParams[key]) for key in [
1348-
"font.serif", "font.sans-serif", "font.cursive", "font.fantasy",
1349-
"font.monospace"])
1347+
rc_params = [mpl.rcParams[f"font.{key}"] for key in [
1348+
"family", "style", "variant", "weight", "stretch", "size",
1349+
"serif", "sans-serif", "cursive", "fantasy", "monospace"]]
1350+
rc_params = tuple(tuple(e) if isinstance(e, list) else e
1351+
for e in rc_params) # Make this hashable.
13501352
ret = self._findfont_cached(
13511353
prop, fontext, directory, fallback_to_default, rebuild_if_missing,
13521354
rc_params)

lib/matplotlib/tests/test_font_manager.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,3 +434,17 @@ def test_font_match_warning(caplog):
434434
findfont(FontProperties(family=["DejaVu Sans"], weight=750))
435435
logs = [rec.message for rec in caplog.records]
436436
assert 'findfont: Failed to find font weight 750, now using 700.' in logs
437+
438+
439+
def test_mutable_fontproperty_cache_invalidation():
440+
fp = FontProperties()
441+
assert findfont(fp).endswith("DejaVuSans.ttf")
442+
fp.set_weight("bold")
443+
assert findfont(fp).endswith("DejaVuSans-Bold.ttf")
444+
445+
446+
def test_fontproperty_default_cache_invalidation():
447+
mpl.rcParams["font.weight"] = "normal"
448+
assert findfont("DejaVu Sans").endswith("DejaVuSans.ttf")
449+
mpl.rcParams["font.weight"] = "bold"
450+
assert findfont("DejaVu Sans").endswith("DejaVuSans-Bold.ttf")

0 commit comments

Comments
 (0)