@@ -985,6 +985,7 @@ def _normalize_font_family(family):
985985 return family
986986
987987
988+ @cbook .deprecated ("2.2" )
988989class TempCache (object ):
989990 """
990991 A class to store temporary caches that are (a) not saved to disk
@@ -1270,6 +1271,20 @@ def findfont(self, prop, fontext='ttf', directory=None,
12701271 <http://www.w3.org/TR/1998/REC-CSS2-19980512/>`_ documentation
12711272 for a description of the font finding algorithm.
12721273 """
1274+ # Pass the relevant rcParams (and the font manager, as `self`) to
1275+ # _findfont_cached so to prevent using a stale cache entry after an
1276+ # rcParam was changed.
1277+ rc_params = tuple (tuple (rcParams [key ]) for key in [
1278+ "font.serif" , "font.sans-serif" , "font.cursive" , "font.fantasy" ,
1279+ "font.monospace" ])
1280+ return self ._findfont_cached (
1281+ prop , fontext , directory , fallback_to_default , rebuild_if_missing ,
1282+ rc_params )
1283+
1284+ @lru_cache ()
1285+ def _findfont_cached (self , prop , fontext , directory , fallback_to_default ,
1286+ rebuild_if_missing , rc_params ):
1287+
12731288 if not isinstance (prop , FontProperties ):
12741289 prop = FontProperties (prop )
12751290 fname = prop .get_file ()
@@ -1282,11 +1297,7 @@ def findfont(self, prop, fontext='ttf', directory=None,
12821297 else :
12831298 fontlist = self .ttflist
12841299
1285- if directory is None :
1286- cached = _lookup_cache [fontext ].get (prop )
1287- if cached is not None :
1288- return cached
1289- else :
1300+ if directory is not None :
12901301 directory = os .path .normcase (directory )
12911302
12921303 best_score = 1e64
@@ -1344,26 +1355,20 @@ def findfont(self, prop, fontext='ttf', directory=None,
13441355 else :
13451356 raise ValueError ("No valid font could be found" )
13461357
1347- if directory is None :
1348- _lookup_cache [fontext ].set (prop , result )
13491358 return result
13501359
1351- _is_opentype_cff_font_cache = {}
1360+ @ lru_cache ()
13521361def is_opentype_cff_font (filename ):
13531362 """
13541363 Returns True if the given font is a Postscript Compact Font Format
13551364 Font embedded in an OpenType wrapper. Used by the PostScript and
13561365 PDF backends that can not subset these fonts.
13571366 """
13581367 if os .path .splitext (filename )[1 ].lower () == '.otf' :
1359- result = _is_opentype_cff_font_cache .get (filename )
1360- if result is None :
1361- with open (filename , 'rb' ) as fd :
1362- tag = fd .read (4 )
1363- result = (tag == b'OTTO' )
1364- _is_opentype_cff_font_cache [filename ] = result
1365- return result
1366- return False
1368+ with open (filename , 'rb' ) as fd :
1369+ return fd .read (4 ) == b"OTTO"
1370+ else :
1371+ return False
13671372
13681373fontManager = None
13691374_fmcache = None
@@ -1431,11 +1436,6 @@ def findfont(prop, fontext='ttf'):
14311436
14321437 fontManager = None
14331438
1434- _lookup_cache = {
1435- 'ttf' : TempCache (),
1436- 'afm' : TempCache ()
1437- }
1438-
14391439 def _rebuild ():
14401440 global fontManager
14411441
0 commit comments