12
12
from bisect import bisect_left
13
13
from collections import defaultdict
14
14
from dataclasses import dataclass , replace
15
+ from functools import lru_cache
15
16
from typing import List , Optional , Tuple , Union
16
17
17
18
from fontTools import ttLib
@@ -449,8 +450,6 @@ def __init__(self, font: TTFFont, identities: List[int]):
449
450
glyph = self .get_glyph (unicode = x )
450
451
if glyph :
451
452
self ._char_id_per_glyph [glyph ] = int (x )
452
- # This is a cache to speed things up:
453
- self ._char_id_per_unicode = {}
454
453
455
454
def __repr__ (self ):
456
455
return (
@@ -465,10 +464,8 @@ def items(self):
465
464
for glyph , char_id in self ._char_id_per_glyph .items ():
466
465
yield glyph , char_id
467
466
467
+ @lru_cache (maxsize = 128 )
468
468
def pick (self , unicode : int ):
469
- cache_hit = self ._char_id_per_unicode .get (unicode )
470
- if cache_hit :
471
- return cache_hit
472
469
glyph = self .get_glyph (unicode = unicode )
473
470
if glyph is None and unicode not in self .font .missing_glyphs :
474
471
self .font .missing_glyphs .append (unicode )
@@ -484,10 +481,9 @@ def pick_glyph(self, glyph):
484
481
char_id = self ._next
485
482
self ._char_id_per_glyph [glyph ] = char_id
486
483
self ._next += 1
487
- # Fill cache:
488
- self ._char_id_per_unicode [glyph .unicode ] = char_id
489
484
return char_id
490
485
486
+ @lru_cache (maxsize = 128 )
491
487
def get_glyph (
492
488
self , glyph = None , unicode = None , glyph_name = None , glyph_width = None
493
489
) -> Glyph :
0 commit comments