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

Skip to content

Commit a0065c3

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 7d6f355 commit a0065c3

1 file changed

Lines changed: 24 additions & 23 deletions

File tree

lib/matplotlib/font_manager.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,8 +1371,13 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
13711371
if rebuild_if_missing:
13721372
_log.info(
13731373
'findfont: Found a missing font file. Rebuilding cache.')
1374-
_rebuild()
1375-
return fontManager.findfont(
1374+
new_fm = _load_fontmanager(try_read_cache=False)
1375+
# Replace self by the new fontmanager, because users may have
1376+
# a reference to this specific instance.
1377+
# TODO: _load_fontmanager should really be (used by) a method
1378+
# modifying the instance in place.
1379+
vars(self).update(vars(new_fm))
1380+
return self.findfont(
13761381
prop, fontext, directory, rebuild_if_missing=False)
13771382
else:
13781383
raise ValueError("No valid font could be found")
@@ -1394,11 +1399,6 @@ def is_opentype_cff_font(filename):
13941399
return False
13951400

13961401

1397-
_fmcache = os.path.join(
1398-
mpl.get_cachedir(), 'fontlist-v{}.json'.format(FontManager.__version__))
1399-
fontManager = None
1400-
1401-
14021402
_get_font = lru_cache(64)(ft2font.FT2Font)
14031403
# FT2Font objects cannot be used across fork()s because they reference the same
14041404
# FT_Library object. While invalidating *all* existing FT2Fonts after a fork
@@ -1418,22 +1418,23 @@ def get_font(filename, hinting_factor=None):
14181418
_kerning_factor=rcParams['text.kerning_factor'])
14191419

14201420

1421-
def _rebuild():
1422-
global fontManager
1423-
_log.info("Generating new fontManager, this may take some time...")
1424-
fontManager = FontManager()
1425-
json_dump(fontManager, _fmcache)
1426-
1427-
1428-
try:
1429-
fontManager = json_load(_fmcache)
1430-
except Exception:
1431-
_rebuild()
1432-
else:
1433-
if getattr(fontManager, '_version', object()) != FontManager.__version__:
1434-
_rebuild()
1435-
else:
1436-
_log.debug("Using fontManager instance from %s", _fmcache)
1421+
def _load_fontmanager(*, try_read_cache=True):
1422+
fm_path = Path(
1423+
mpl.get_cachedir(), f"fontlist-v{FontManager.__version__}.json")
1424+
if try_read_cache:
1425+
try:
1426+
fm = json_load(fm_path)
1427+
except Exception as exc:
1428+
pass
1429+
else:
1430+
if getattr(fm, "_version", object()) == FontManager.__version__:
1431+
_log.debug("Using fontManager instance from %s", fm_path)
1432+
return fm
1433+
fm = FontManager()
1434+
json_dump(fm, fm_path)
1435+
_log.info("generated new fontManager")
1436+
return fm
14371437

14381438

1439+
fontManager = _load_fontmanager()
14391440
findfont = fontManager.findfont

0 commit comments

Comments
 (0)