Skip to content

Commit a929f2b

Browse files
committed
FontManager fixes.
Previously, when using findfont with rebuild_is_missing (the default) _rebuild() would generate a new fontManager instance, but because findfont is defined as `findfont = fontManager.findfont`, this would keep using the old fontManager instance; we can't just fix this with `def findfont(...): return fontManager.findfont(...)` because people may be importing the fontManager instance anyways and not benefitting from the rebuilt one. Instead, overwrite(!) the contents of the existing fontManager instance with the new one as needed.
1 parent b12c761 commit a929f2b

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

lib/matplotlib/font_manager.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,8 +1302,11 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
13021302
if rebuild_if_missing:
13031303
_log.info(
13041304
'findfont: Found a missing font file. Rebuilding cache.')
1305-
_rebuild()
1306-
return fontManager.findfont(
1305+
new_fm = _load_fontmanager(try_read_cache=False)
1306+
# Replace self by the new fontmanager, because users may have
1307+
# a reference to this specific instance.
1308+
vars(self).update(vars(new_fm))
1309+
return self.findfont(
13071310
prop, fontext, directory, rebuild_if_missing=False)
13081311
else:
13091312
raise ValueError("No valid font could be found")
@@ -1325,11 +1328,6 @@ def is_opentype_cff_font(filename):
13251328
return False
13261329

13271330

1328-
_fmcache = os.path.join(
1329-
mpl.get_cachedir(), 'fontlist-v{}.json'.format(FontManager.__version__))
1330-
fontManager = None
1331-
1332-
13331331
_get_font = lru_cache(64)(ft2font.FT2Font)
13341332
# FT2Font objects cannot be used across fork()s because they reference the same
13351333
# FT_Library object. While invalidating *all* existing FT2Fonts after a fork
@@ -1349,23 +1347,24 @@ def get_font(filename, hinting_factor=None):
13491347
_kerning_factor=rcParams['text.kerning_factor'])
13501348

13511349

1352-
def _rebuild():
1353-
global fontManager
1354-
fontManager = FontManager()
1355-
with cbook._lock_path(_fmcache):
1356-
json_dump(fontManager, _fmcache)
1350+
def _load_fontmanager(*, try_read_cache=True):
1351+
if try_read_cache:
1352+
fm_path = Path(
1353+
mpl.get_cachedir(), f"fontlist-v{FontManager.__version__}.json")
1354+
try:
1355+
fm = json_load(fm_path)
1356+
except Exception as exc:
1357+
pass
1358+
else:
1359+
if getattr(fm, "_version", object()) == FontManager.__version__:
1360+
_log.debug("Using fontManager instance from %s", fm_path)
1361+
return fm
1362+
fm = FontManager()
1363+
with cbook._lock_path(fm_path):
1364+
json_dump(fm, fm_path)
13571365
_log.info("generated new fontManager")
1366+
return fm
13581367

13591368

1360-
try:
1361-
fontManager = json_load(_fmcache)
1362-
except Exception:
1363-
_rebuild()
1364-
else:
1365-
if getattr(fontManager, '_version', object()) != FontManager.__version__:
1366-
_rebuild()
1367-
else:
1368-
_log.debug("Using fontManager instance from %s", _fmcache)
1369-
1370-
1369+
fontManager = _load_fontmanager()
13711370
findfont = fontManager.findfont

0 commit comments

Comments
 (0)