8000 Make FontManager.defaultFont a property, to avoid hardcoding the prefix. · matplotlib/matplotlib@1b422f9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1b422f9

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 1b422f9

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

lib/matplotlib/font_manager.py

Lines changed: 6 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,11 @@ def ttffiles(self):
999993
def afmfiles(self):
1000994
return [font.fname for font in self.afmlist]
1001995

996+
@property
997+
def defaultFont(self):
998+
return {ext: self.findfont(family, fontext=ext)
999+
for ext, family in self.defaultFamily.items()}
1000+
10021001
def get_default_weight(self):
10031002
"""
10041003
Return the default font weight.

0 commit comments

Comments
 (0)
0