@@ -1246,10 +1246,7 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
1246
1246
if fname is not None :
1247
1247
return fname
1248
1248
1249
- if fontext == 'afm' :
1250
- fontlist = self .afmlist
1251
- else :
1252
- fontlist = self .ttflist
1249
+ fontlist = self .afmlist if fontext == 'afm' else self .ttflist
1253
1250
1254
1251
best_score = 1e64
1255
1252
best_font = None
@@ -1273,36 +1270,37 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
1273
1270
if score == 0 :
1274
1271
break
1275
1272
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 ):
1291
1275
_log .debug ('findfont: Matching %s to %s (%r) with score of %f.' ,
1292
1276
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 ,
1285
+ fallback_to_default = True ,
1286
+ rebuild_if_missing = False )
1287
+ elif fallback_to_default :
1288
+ _log .warning (
1289
+ 'findfont: Font family %s not found. Falling back to %s.' ,
1290
+ prop .get_family (), self .defaultFamily [fontext ])
1291
+ default_prop = prop .copy ()
1292
+ default_prop .set_family (self .defaultFamily [fontext ])
1293
+ return self .findfont (default_prop , fontext , directory ,
1294
+ fallback_to_default = False )
1295
+ else :
1296
+ # This is a hard fail -- we can't find anything reasonable,
1297
+ # so just return the DejaVuSans.ttf
1298
+ _log .warning ('findfont: Could not match %s. Returning %s.' ,
1299
+ prop , self .defaultFont [fontext ])
1300
+ font_filename = self .defaultFont [fontext ]
1301
+ if os .path .isfile (font_filename ):
1302
+ return font_filename
1303
+ raise ValueError ("No valid font could be found" )
1306
1304
1307
1305
1308
1306
@lru_cache ()
0 commit comments