@@ -188,7 +188,8 @@ class TruetypeFonts(Fonts):
188
188
"""
189
189
def __init__ (self , default_font_prop , mathtext_backend ):
190
190
super ().__init__ (default_font_prop , mathtext_backend )
191
- self .glyphd = {}
191
+ # Per-instance cache.
192
+ self ._get_info = functools .lru_cache (None )(self ._get_info )
192
193
self ._fonts = {}
193
194
194
195
filename = findfont (default_font_prop )
@@ -214,15 +215,10 @@ def _get_offset(self, font, glyph, fontsize, dpi):
214
215
return (glyph .height / 64 / 2 ) + (fontsize / 3 * dpi / 72 )
215
216
return 0.
216
217
218
+ # The return value of _get_info is cached per-instance.
217
219
def _get_info (self , fontname , font_class , sym , fontsize , dpi ):
218
- key = fontname , font_class , sym , fontsize , dpi
219
- bunch = self .glyphd .get (key )
220
- if bunch is not None :
221
- return bunch
222
-
223
220
font , num , slanted = self ._get_glyph (
224
221
fontname , font_class , sym , fontsize )
225
-
226
222
font .set_size (fontsize , dpi )
227
223
glyph = font .load_char (
228
224
num , flags = self .mathtext_backend .get_hinting_type ())
@@ -242,7 +238,7 @@ def _get_info(self, fontname, font_class, sym, fontsize, dpi):
242
238
slanted = slanted
243
239
)
244
240
245
- result = self . glyphd [ key ] = types .SimpleNamespace (
241
+ return types .SimpleNamespace (
246
242
font = font ,
247
243
fontsize = fontsize ,
248
244
postscript_name = font .postscript_name ,
@@ -251,7 +247,6 @@ def _get_info(self, fontname, font_class, sym, fontsize, dpi):
251
247
glyph = glyph ,
252
248
offset = offset
253
249
)
254
- return result
255
250
256
251
def get_xheight (self , fontname , fontsize , dpi ):
257
252
font = self ._get_font (fontname )
0 commit comments