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

Skip to content

Commit 7e5f7d3

Browse files
committed
If a font file is looked up in the cache, but that font file no longer exists on disk, rebuild the cache.
svn path=/branches/v1_0_maint/; revision=8712
1 parent 525c39c commit 7e5f7d3

1 file changed

Lines changed: 21 additions & 11 deletions

File tree

lib/matplotlib/font_manager.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ def score_size(self, size1, size2):
11691169
return abs(sizeval1 - sizeval2) / 72.0
11701170

11711171
def findfont(self, prop, fontext='ttf', directory=None,
1172-
fallback_to_default=True):
1172+
fallback_to_default=True, rebuild_if_missing=True):
11731173
"""
11741174
Search the font list for the font that most closely matches
11751175
the :class:`FontProperties` *prop*.
@@ -1257,6 +1257,16 @@ def findfont(self, prop, fontext='ttf', directory=None,
12571257
(prop, best_font.name, best_font.fname, best_score))
12581258
result = best_font.fname
12591259

1260+
if not os.path.isfile(result):
1261+
if rebuild_if_missing:
1262+
verbose.report(
1263+
'findfont: Found a missing font file. Rebuilding cache.')
1264+
_rebuild()
1265+
return fontManager.findfont(
1266+
prop, fontext, directory, True, False)
1267+
else:
1268+
raise ValueError("No valid font could be found")
1269+
12601270
if directory is None:
12611271
font_cache[hash(prop)] = result
12621272
return result
@@ -1280,6 +1290,16 @@ def is_opentype_cff_font(filename):
12801290
return result
12811291
return False
12821292

1293+
fontManager = None
1294+
1295+
_fmcache = os.path.join(get_configdir(), 'fontList.cache')
1296+
1297+
def _rebuild():
1298+
global fontManager
1299+
fontManager = FontManager()
1300+
pickle_dump(fontManager, _fmcache)
1301+
verbose.report("generated new fontManager")
1302+
12831303
# The experimental fontconfig-based backend.
12841304
if USE_FONTCONFIG and sys.platform != 'win32':
12851305
import re
@@ -1317,16 +1337,6 @@ def findfont(prop, fontext='ttf'):
13171337
return result
13181338

13191339
else:
1320-
_fmcache = os.path.join(get_configdir(), 'fontList.cache')
1321-
1322-
fontManager = None
1323-
1324-
def _rebuild():
1325-
global fontManager
1326-
fontManager = FontManager()
1327-
pickle_dump(fontManager, _fmcache)
1328-
verbose.report("generated new fontManager")
1329-
13301340
try:
13311341
fontManager = pickle_load(_fmcache)
13321342
if (not hasattr(fontManager, '_version') or

0 commit comments

Comments
 (0)