@@ -994,7 +994,6 @@ def _normalize_font_family(family):
994
994
return family
995
995
996
996
997
- @cbook .deprecated ("2.2" )
998
997
class TempCache (object ):
999
998
"""
1000
999
A class to store temporary caches that are (a) not saved to disk
@@ -1279,20 +1278,6 @@ def findfont(self, prop, fontext='ttf', directory=None,
1279
1278
<http://www.w3.org/TR/1998/REC-CSS2-19980512/>`_ documentation
1280
1279
for a description of the font finding algorithm.
1281
1280
"""
1282
- # Pass the relevant rcParams (and the font manager, as `self`) to
1283
- # _findfont_cached so to prevent using a stale cache entry after an
1284
- # rcParam was changed.
1285
- rc_params = tuple (tuple (rcParams [key ]) for key in [
1286
- "font.serif" , "font.sans-serif" , "font.cursive" , "font.fantasy" ,
1287
- "font.monospace" ])
1288
- return self ._findfont_cached (
1289
- prop , fontext , directory , fallback_to_default , rebuild_if_missing ,
1290
- rc_params )
1291
-
1292
- @lru_cache ()
1293
- def _findfont_cached (self , prop , fontext , directory , fallback_to_default ,
1294
- rebuild_if_missing , rc_params ):
1295
-
1296
1281
if not isinstance (prop , FontProperties ):
1297
1282
prop = FontProperties (prop )
1298
1283
fname = prop .get_file ()
@@ -1306,7 +1291,11 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
1306
1291
else :
1307
1292
fontlist = self .ttflist
1308
1293
1309
- if directory is not None :
1294
+ if directory is None :
1295
+ cached = _lookup_cache [fontext ].get (prop )
1296
+ if cached is not None :
1297
+ return cached
1298
+ else :
1310
1299
directory = os .path .normcase (directory )
1311
1300
1312
1301
best_score = 1e64
@@ -1364,20 +1353,26 @@ def _findfont_cached(self, prop, fontext, directory, fallback_to_default,
1364
1353
else :
1365
1354
raise ValueError ("No valid font could be found" )
1366
1355
1356
+ if directory is None :
1357
+ _lookup_cache [fontext ].set (prop , result )
1367
1358
return result
1368
1359
1369
- @ lru_cache ()
1360
+ _is_opentype_cff_font_cache = {}
1370
1361
def is_opentype_cff_font (filename ):
1371
1362
"""
1372
1363
Returns True if the given font is a Postscript Compact Font Format
1373
1364
Font embedded in an OpenType wrapper. Used by the PostScript and
1374
1365
PDF backends that can not subset these fonts.
1375
1366
"""
1376
1367
if os .path .splitext (filename )[1 ].lower () == '.otf' :
1377
- with open (filename , 'rb' ) as fd :
1378
- return fd .read (4 ) == b"OTTO"
1379
- else :
1380
- return False
1368
+ result = _is_opentype_cff_font_cache .get (filename )
1369
+ if result is None :
1370
+ with open (filename , 'rb' ) as fd :
1371
+ tag = fd .read (4 )
1372
+ result = (tag == b'OTTO' )
1373
+ _is_opentype_cff_font_cache [filename ] = result
1374
+ return result
1375
+ return False
1381
1376
1382
1377
fontManager = None
1383
1378
_fmcache = None
@@ -1444,6 +1439,11 @@ def findfont(prop, fontext='ttf'):
1444
1439
1445
1440
fontManager = None
1446
1441
1442
+ _lookup_cache = {
1443
+ 'ttf' : TempCache (),
1444
+ 'afm' : TempCache ()
1445
+ }
1446
+
1447
1447
def _rebuild ():
1448
1448
global fontManager
1449
1449
0 commit comments