@@ -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,34 @@ 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 , 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" )
1306
1301
1307
1302
1308
1303
@lru_cache ()
0 commit comments