From eaf16411e1090d311dff9e8ec72aefd6d30851ce Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 10 Oct 2018 11:39:34 +0200 Subject: [PATCH] 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. --- lib/matplotlib/font_manager.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/font_manager.py b/lib/matplotlib/font_manager.py index 6f8dfe315cbd..1015800cbece 100644 --- a/lib/matplotlib/font_manager.py +++ b/lib/matplotlib/font_manager.py @@ -947,7 +947,7 @@ class FontManager(object): # Increment this version number whenever the font cache data # format or behavior has changed and requires a existing font # cache files to be rebuilt. - __version__ = 300 + __version__ = 310 def __init__(self, size=None, weight='normal'): self._version = self.__version__ @@ -975,19 +975,13 @@ def __init__(self, size=None, weight='normal'): self.defaultFamily = { 'ttf': 'DejaVu Sans', 'afm': 'Helvetica'} - self.defaultFont = {} ttffiles = findSystemFonts(paths) + findSystemFonts() - self.defaultFont['ttf'] = next( - (fname for fname in ttffiles - if fname.lower().endswith("dejavusans.ttf")), - ttffiles[0]) self.ttflist = createFontList(ttffiles) afmfiles = (findSystemFonts(paths, fontext='afm') + findSystemFonts(fontext='afm')) self.afmlist = createFontList(afmfiles, fontext='afm') - self.defaultFont['afm'] = afmfiles[0] if afmfiles else None @cbook.deprecated("3.0") @property @@ -999,6 +993,13 @@ def ttffiles(self): def afmfiles(self): return [font.fname for font in self.afmlist] + @property + def defaultFont(self): + # Lazily evaluated (findfont then caches the result) to avoid including + # the venv path in the json serialization. + return {ext: self.findfont(family, fontext=ext) + for ext, family in self.defaultFamily.items()} + def get_default_weight(self): """ Return the default font weight.