@@ -1011,7 +1011,7 @@ class FontManager:
1011
1011
# Increment this version number whenever the font cache data
1012
1012
# format or behavior has changed and requires an existing font
1013
1013
# cache files to be rebuilt.
1014
- __version__ = 330
1014
+ __version__ = 335
1015
1015
1016
1016
def __init__ (self , size = None , weight = 'normal' ):
1017
1017
self ._version = self .__version__
@@ -1031,6 +1031,9 @@ def __init__(self, size=None, weight='normal'):
1031
1031
self .afmlist = []
1032
1032
self .ttflist = []
1033
1033
1034
+ # keep track of fonts
1035
+ self ._not_found = {'generic' : [], 'family' : [], 'fallback' : []}
1036
+
1034
1037
# Delay the warning by 5s.
1035
1038
timer = threading .Timer (5 , lambda : _log .warning (
1036
1039
'Matplotlib is building the font cache; this may take a moment.' ))
@@ -1049,6 +1052,12 @@ def __init__(self, size=None, weight='normal'):
1049
1052
finally :
1050
1053
timer .cancel ()
1051
1054
1055
+ def _warn_not_found (self , family , warning_id , warning_msg ):
1056
+ """Log missing fonts once per warning message"""
1057
+ if family not in self ._not_found [warning_id ]:
1058
+ _log .warning (warning_msg )
1059
+ self ._not_found [warning_id ].append (family )
1060
+
1052
1061
def addfont (self , path ):
1053
1062
"""
1054
1063
Cache the properties of the font at *path* to make it available to the
@@ -1362,13 +1371,14 @@ def _find_fonts_by_props(self, prop, fontext='ttf', directory=None,
1362
1371
)
1363
1372
except ValueError :
1364
1373
if family in font_family_aliases :
1365
- _log . warning (
1366
- "findfont: Generic family %r not found because "
1367
- "none of the following families were found: %s" ,
1368
- family , ", " . join ( self . _expand_aliases ( family ) )
1369
- )
1374
+ msg = ( f"findfont: Generic family ' { family } ' not found because "
1375
+ "none of the following families were found: "
1376
+ f" { ',' . join ( self . _expand_aliases ( family )) } " )
1377
+ self . _warn_not_found ( family , 'generic' , msg )
1378
+
1370
1379
else :
1371
- _log .warning ("findfont: Font family %r not found." , family )
1380
+ msg = f"findfont: Font family '{ family } ' not found."
1381
+ self ._warn_not_found (family , 'family' , msg )
1372
1382
1373
1383
# only add default family if no other font was found and
1374
1384
# fallback_to_default is enabled
@@ -1429,15 +1439,15 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
1429
1439
1430
1440
if best_font is None or best_score >= 10.0 :
1431
1441
if fallback_to_default :
1432
- _log . warning (
1433
- 'findfont: Font family %s not found. Falling back to %s.' ,
1434
- prop .get_family (), self . defaultFamily [ fontext ] )
1442
+ msg = ( f"findfont: Font family { prop . get_family () } not found. "
1443
+ f" Falling back to { self . defaultFamily [ fontext ] } ." )
1444
+ self . _warn_not_found ( prop .get_family (), 'fallback' , msg )
1435
1445
for family in map (str .lower , prop .get_family ()):
1436
1446
if family in font_family_aliases :
1437
- _log . warning (
1438
- "findfont: Generic family %r not found because "
1439
- "none of the following families were found: %s" ,
1440
- family , ", " . join ( self . _expand_aliases ( family )) )
1447
+ msg = ( f"findfont: Generic family ' { family } ' not found because "
1448
+ "none of the following families were found: "
1449
+ f" { ',' . join ( self . _expand_aliases ( family )) } " )
1450
+ self . _warn_not_found ( family , 'generic' , msg )
1441
1451
default_prop = prop .copy ()
1442
1452
default_prop .set_family (self .defaultFamily [fontext ])
1443
1453
return self .findfont (default_prop , fontext , directory ,
0 commit comments