Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 68d24f6

Browse filesBrowse files
committed
Rely more on lru_cache in font_manager rather than custom caching.
1 parent bb8969b commit 68d24f6
Copy full SHA for 68d24f6

File tree

Expand file treeCollapse file tree

1 file changed

+21
-21
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+21
-21
lines changed

‎lib/matplotlib/font_manager.py

Copy file name to clipboardExpand all lines: lib/matplotlib/font_manager.py
+21-21Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,7 @@ def _normalize_font_family(family):
983983
return family
984984

985985

986+
@cbook.deprecated("2.2")
986987
class TempCache(object):
987988
"""
988989
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,
12681269
<http://www.w3.org/TR/1998/REC-CSS2-19980512/>`_ documentation
12691270
for a description of the font finding algorithm.
12701271
"""
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+
12711286
if not isinstance(prop, FontProperties):
12721287
prop = FontProperties(prop)
12731288
fname = prop.get_file()
@@ -1280,11 +1295,7 @@ def findfont(self, prop, fontext='ttf', directory=None,
12801295
else:
12811296
fontlist = self.ttflist
12821297

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:
12881299
directory = os.path.normcase(directory)
12891300

12901301
best_score = 1e64
@@ -1342,26 +1353,20 @@ def findfont(self, prop, fontext='ttf', directory=None,
13421353
else:
13431354
raise ValueError("No valid font could be found")
13441355

1345-
if directory is None:
1346-
_lookup_cache[fontext].set(prop, result)
13471356
return result
13481357

1349-
_is_opentype_cff_font_cache = {}
1358+
@lru_cache()
13501359
def is_opentype_cff_font(filename):
13511360
"""
13521361
Returns True if the given font is a Postscript Compact Font Format
13531362
Font embedded in an OpenType wrapper. Used by the PostScript and
13541363
PDF backends that can not subset these fonts.
13551364
"""
13561365
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
13651370

13661371
fontManager = None
13671372
_fmcache = None
@@ -1424,11 +1429,6 @@ def findfont(prop, fontext='ttf'):
14241429

14251430
fontManager = None
14261431

1427-
_lookup_cache = {
1428-
'ttf': TempCache(),
1429-
'afm': TempCache()
1430-
}
1431-
14321432
def _rebuild():
14331433
global fontManager
14341434

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.