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

Skip to content

Commit eaf1641

Browse files
committed
Make FontManager.defaultFont a property, to avoid hardcoding the prefix.
When serialized to json, FontManager stores paths that are relative to mpl-data (i.e. fonts shipped by Matplotlib) to relative paths; this ensures that the resulting fontList.json stays valid across multiple venvs (as the venv prefix does not end up in the json file). The same issue happens with defaultFont: currently, it includes the venv prefix. Instead of adding more layers to the json serialization/deserialization, just don't store it into fontList.json but compute defaultFont dynamically instead.
1 parent e08d7ba commit eaf1641

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

lib/matplotlib/font_manager.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ class FontManager(object):
947947
# Increment this version number whenever the font cache data
948948
# format or behavior has changed and requires a existing font
949949
# cache files to be rebuilt.
950-
__version__ = 300
950+
__version__ = 310
951951

952952
def __init__(self, size=None, weight='normal'):
953953
self._version = self.__version__
@@ -975,19 +975,13 @@ def __init__(self, size=None, weight='normal'):
975975
self.defaultFamily = {
976976
'ttf': 'DejaVu Sans',
977977
'afm': 'Helvetica'}
978-
self.defaultFont = {}
979978

980979
ttffiles = findSystemFonts(paths) + findSystemFonts()
981-
self.defaultFont['ttf'] = next(
982-
(fname for fname in ttffiles
983-
if fname.lower().endswith("dejavusans.ttf")),
984-
ttffiles[0])
985980
self.ttflist = createFontList(ttffiles)
986981

987982
afmfiles = (findSystemFonts(paths, fontext='afm')
988983
+ findSystemFonts(fontext='afm'))
989984
self.afmlist = createFontList(afmfiles, fontext='afm')
990-
self.defaultFont['afm'] = afmfiles[0] if afmfiles else None
991985

992986
@cbook.deprecated("3.0")
993987
@property
@@ -999,6 +993,13 @@ def ttffiles(self):
999993
def afmfiles(self):
1000994
return [font.fname for font in self.afmlist]
1001995

996+
@property
997+
def defaultFont(self):
998+
# Lazily evaluated (findfont then caches the result) to avoid including
999+
# the venv path in the json serialization.
1000+
return {ext: self.findfont(family, fontext=ext)
1001+
for ext, family in self.defaultFamily.items()}
1002+
10021003
def get_default_weight(self):
10031004
"""
10041005
Return the default font weight.

0 commit comments

Comments
 (0)