Skip to content

Commit ae21192

Browse files
authored
Merge 335c4e7 into 958721f
2 parents 958721f + 335c4e7 commit ae21192

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

fpdf/fonts.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from bisect import bisect_left
1313
from collections import defaultdict
1414
from dataclasses import dataclass, replace
15+
from functools import lru_cache
1516
from typing import List, Optional, Tuple, Union
1617

1718
from fontTools import ttLib
@@ -449,8 +450,6 @@ def __init__(self, font: TTFFont, identities: List[int]):
449450
glyph = self.get_glyph(unicode=x)
450451
if glyph:
451452
self._char_id_per_glyph[glyph] = int(x)
452-
# This is a cache to speed things up:
453-
self._char_id_per_unicode = {}
454453

455454
def __repr__(self):
456455
return (
@@ -465,10 +464,8 @@ def items(self):
465464
for glyph, char_id in self._char_id_per_glyph.items():
466465
yield glyph, char_id
467466

467+
@lru_cache(maxsize=128)
468468
def pick(self, unicode: int):
469-
cache_hit = self._char_id_per_unicode.get(unicode)
470-
if cache_hit:
471-
return cache_hit
472469
glyph = self.get_glyph(unicode=unicode)
473470
if glyph is None and unicode not in self.font.missing_glyphs:
474471
self.font.missing_glyphs.append(unicode)
@@ -484,10 +481,9 @@ def pick_glyph(self, glyph):
484481
char_id = self._next
485482
self._char_id_per_glyph[glyph] = char_id
486483
self._next += 1
487-
# Fill cache:
488-
self._char_id_per_unicode[glyph.unicode] = char_id
489484
return char_id
490485

486+
@lru_cache(maxsize=128)
491487
def get_glyph(
492488
self, glyph=None, unicode=None, glyph_name=None, glyph_width=None
493489
) -> Glyph:

0 commit comments

Comments
 (0)