Skip to content

Commit db77e33

Browse files
committed
Fix rebuilding font cache
1 parent a6e3e56 commit db77e33

File tree

1 file changed

+27
-32
lines changed

1 file changed

+27
-32
lines changed

lib/matplotlib/font_manager.py

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,10 +1246,7 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
12461246
if fname is not None:
12471247
return fname
12481248

1249-
if fontext == 'afm':
1250-
fontlist = self.afmlist
1251-
else:
1252-
fontlist = self.ttflist
1249+
fontlist = self.afmlist if fontext == 'afm' else self.ttflist
12531250

12541251
best_score = 1e64
12551252
best_font = None
@@ -1273,36 +1270,34 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
12731270
if score == 0:
12741271
break
12751272

1276-
if best_font is None or best_score >= 10.0:
1277-
if fallback_to_default:
1278-
_log.warning(
1279-
'findfont: Font family %s not found. Falling back to %s.',
1280-
prop.get_family(), self.defaultFamily[fontext])
1281-
default_prop = prop.copy()
1282-
default_prop.set_family(self.defaultFamily[fontext])
1283-
return self.findfont(default_prop, fontext, directory, False)
1284-
else:
1285-
# This is a hard fail -- we can't find anything reasonable,
1286-
# so just return the DejaVuSans.ttf
1287-
_log.warning('findfont: Could not match %s. Returning %s.',
1288-
prop, self.defaultFont[fontext])
1289-
result = self.defaultFont[fontext]
1290-
else:
1273+
if (best_font is not None and os.path.isfile( best_font.fname)
1274+
and best_score < 10):
12911275
_log.debug('findfont: Matching %s to %s (%r) with score of %f.',
12921276
prop, best_font.name, best_font.fname, best_score)
1293-
result = best_font.fname
1294-
1295-
if not os.path.isfile(result):
1296-
if rebuild_if_missing:
1297-
_log.info(
1298-
'findfont: Found a missing font file. Rebuilding cache.')
1299-
_rebuild()
1300-
return fontManager.findfont(
1301-
prop, fontext, directory, True, False)
1302-
else:
1303-
raise ValueError("No valid font could be found")
1304-
1305-
return result
1277+
return best_font.fname
1278+
1279+
# No sufficently good font found.
1280+
if rebuild_if_missing:
1281+
_log.info(
1282+
'findfont: Found a missing font file. Rebuilding cache.')
1283+
_rebuild()
1284+
return self.findfont(prop, fontext, directory, True, False)
1285+
elif fallback_to_default:
1286+
_log.warning(
1287+
'findfont: Font family %s not found. Falling back to %s.',
1288+
prop.get_family(), self.defaultFamily[fontext])
1289+
default_prop = prop.copy()
1290+
default_prop.set_family(self.defaultFamily[fontext])
1291+
return self.findfont(default_prop, fontext, directory, False)
1292+
else:
1293+
# This is a hard fail -- we can't find anything reasonable,
1294+
# so just return the DejaVuSans.ttf
1295+
_log.warning('findfont: Could not match %s. Returning %s.',
1296+
prop, self.defaultFont[fontext])
1297+
font_filename = self.defaultFont[fontext]
1298+
if os.path.isfile(font_filename):
1299+
return font_filename
1300+
raise ValueError("No valid font could be found")
13061301

13071302

13081303
@lru_cache()

0 commit comments

Comments
 (0)