@@ -983,6 +983,7 @@ def _normalize_font_family(family):
983
983
return family
984
984
985
985
986
+ @cbook .deprecated ("2.2" )
986
987
class TempCache (object ):
987
988
"""
988
989
A class to store temporary caches that are (a) not saved to disk
@@ -1268,6 +1269,20 @@ def findfont(self, prop, fontext='ttf', directory=None,
1268
1269
<http://www.w3.org/TR/1998/REC-CSS2-19980512/>`_ documentation
1269
1270
for a description of the font finding algorithm.
1270
1271
"""
1272
+ # Pass the relevant rcParams (and the font manager, as `self`) to
1273
+ # _findfont_cached so to prevent using a stale cache entry after an
1274
+ # rcParam was changed.
1275
+ rc_params = tuple (tuple (rcParams [key ]) for key in [
1276
+ "font.serif" , "font.sans-serif" , "font.cursive" , "font.fantasy" ,
1277
+ "font.monospace" ])
1278
+ return self ._findfont_cached (
1279
+ prop , fontext , directory , fallback_to_default , rebuild_if_missing ,
1280
+ rc_params )
1281
+
1282
+ @lru_cache ()
1283
+ def _findfont_cached (self , prop , fontext , directory , fallback_to_default ,
1284
+ rebuild_if_missing , rc_params ):
1285
+
1271
1286
if not isinstance (prop , FontProperties ):
1272
1287
prop = FontProperties (prop )
1273
1288
fname = prop .get_file ()
@@ -1280,11 +1295,7 @@ def findfont(self, prop, fontext='ttf', directory=None,
1280
1295
else :
1281
1296
fontlist = self .ttflist
1282
1297
1283
- if directory is None :
1284
- cached = _lookup_cache [fontext ].get (prop )
1285
- if cached is not None :
1286
- return cached
1287
- else :
1298
+ if directory is not None :
1288
1299
directory = os .path .normcase (directory )
1289
1300
1290
1301
best_score = 1e64
@@ -1342,26 +1353,20 @@ def findfont(self, prop, fontext='ttf', directory=None,
1342
1353
else :
1343
1354
raise ValueError ("No valid font could be found" )
1344
1355
1345
- if directory is None :
1346
- _lookup_cache [fontext ].set (prop , result )
1347
1356
return result
1348
1357
1349
- _is_opentype_cff_font_cache = {}
1358
+ @ lru_cache ()
1350
1359
def is_opentype_cff_font (filename ):
1351
1360
"""
1352
1361
Returns True if the given font is a Postscript Compact Font Format
1353
1362
Font embedded in an OpenType wrapper. Used by the PostScript and
1354
1363
PDF backends that can not subset these fonts.
1355
1364
"""
1356
1365
if os .path .splitext (filename )[1 ].lower () == '.otf' :
1357
- result = _is_opentype_cff_font_cache .get (filename )
1358
- if result is None :
1359
- with open (filename , 'rb' ) as fd :
1360
- tag = fd .read (4 )
1361
- result = (tag == b'OTTO' )
1362
- _is_opentype_cff_font_cache [filename ] = result
1363
- return result
1364
- return False
1366
+ with open (filename , 'rb' ) as fd :
1367
+ return fd .read (4 ) == b"OTTO"
1368
+ else :
1369
+ return False
1365
1370
1366
1371
fontManager = None
1367
1372
_fmcache = None
@@ -1424,11 +1429,6 @@ def findfont(prop, fontext='ttf'):
1424
1429
1425
1430
fontManager = None
1426
1431
1427
- _lookup_cache = {
1428
- 'ttf' : TempCache (),
1429
- 'afm' : TempCache ()
1430
- }
1431
-
1432
1432
def _rebuild ():
1433
1433
global fontManager
1434
1434
0 commit comments