@@ -188,7 +188,8 @@ class TruetypeFonts(Fonts):
188188 """
189189 def __init__ (self , default_font_prop , mathtext_backend ):
190190 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 )
192193 self ._fonts = {}
193194
194195 filename = findfont (default_font_prop )
@@ -214,15 +215,10 @@ def _get_offset(self, font, glyph, fontsize, dpi):
214215 return (glyph .height / 64 / 2 ) + (fontsize / 3 * dpi / 72 )
215216 return 0.
216217
218+ # The return value of _get_info is cached per-instance.
217219 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-
223220 font , num , slanted = self ._get_glyph (
224221 fontname , font_class , sym , fontsize )
225-
226222 font .set_size (fontsize , dpi )
227223 glyph = font .load_char (
228224 num , flags = self .mathtext_backend .get_hinting_type ())
@@ -242,7 +238,7 @@ def _get_info(self, fontname, font_class, sym, fontsize, dpi):
242238 slanted = slanted
243239 )
244240
245- result = self . glyphd [ key ] = types .SimpleNamespace (
241+ return types .SimpleNamespace (
246242 font = font ,
247243 fontsize = fontsize ,
248244 postscript_name = font .postscript_name ,
@@ -251,7 +247,6 @@ def _get_info(self, fontname, font_class, sym, fontsize, dpi):
251247 glyph = glyph ,
252248 offset = offset
253249 )
254- return result
255250
256251 def get_xheight (self , fontname , fontsize , dpi ):
257252 font = self ._get_font (fontname )
0 commit comments