From 0a7993998d9ca6841a579796dfaa30f31d163815 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 16 Dec 2023 21:37:05 -0700 Subject: [PATCH] Revert "[glyf] Load using LazyDict" This reverts commit b493729eff954909672694b67a453964f33ac893. --- Lib/fontTools/misc/lazyTools.py | 4 +++ Lib/fontTools/ttLib/tables/_g_l_y_f.py | 48 +++++++++++++++----------- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/Lib/fontTools/misc/lazyTools.py b/Lib/fontTools/misc/lazyTools.py index 846757d7e..a3cc52e53 100644 --- a/Lib/fontTools/misc/lazyTools.py +++ b/Lib/fontTools/misc/lazyTools.py @@ -4,6 +4,10 @@ __all__ = ["LazyDict", "LazyList"] class LazyDict(UserDict): + def __init__(self, data): + super().__init__() + self.data = data + def __getitem__(self, k): v = self.data[k] if callable(v): diff --git a/Lib/fontTools/ttLib/tables/_g_l_y_f.py b/Lib/fontTools/ttLib/tables/_g_l_y_f.py index 83dd0bc41..fa11cf8f4 100644 --- a/Lib/fontTools/ttLib/tables/_g_l_y_f.py +++ b/Lib/fontTools/ttLib/tables/_g_l_y_f.py @@ -8,7 +8,6 @@ from fontTools.misc.transform import DecomposedTransform from fontTools.misc.textTools import tostr, safeEval, pad from fontTools.misc.arrayTools import updateBounds, pointInRect from fontTools.misc.bezierTools import calcQuadraticBounds -from fontTools.misc.lazyTools import LazyDict from fontTools.misc.fixedTools import ( fixedToFloat as fi2fl, floatToFixed as fl2fi, @@ -54,19 +53,6 @@ version = ".".join(version.split(".")[:2]) SCALE_COMPONENT_OFFSET_DEFAULT = 0 # 0 == MS, 1 == Apple -def _load_glyph(self, glyphName): - gid = self.reverseGlyphMap[glyphName] - offsets = self.loca[gid : gid + 2] - glyphdata = self.glyphData[offsets[0] : offsets[1]] - if len(glyphdata) != offsets[1] - offsets[0]: - raise ttLib.TTLibError( - "not enough glyph data for glyph '%s' at " - "offset %d: expected %d bytes, found %d" - % (glyphName, offsets[0], offsets[1] - offsets[0], len(glyphdata)) - ) - return Glyph(glyphdata) - - class table__g_l_y_f(DefaultTable.DefaultTable): """Glyph Data Table @@ -111,13 +97,33 @@ class table__g_l_y_f(DefaultTable.DefaultTable): [axis.axisTag for axis in ttFont["fvar"].axes] if "fvar" in ttFont else [] ) loca = ttFont["loca"] - self.glyphOrder = ttFont.getGlyphOrder() - - l = self.glyphs = LazyDict({k: _load_glyph for k in self.glyphOrder}) - l.reverseGlyphMap = self._reverseGlyphOrder = ttFont.getReverseGlyphMap() - l.loca = loca - l.glyphData = data - + pos = int(loca[0]) + nextPos = 0 + noname = 0 + self.glyphs = {} + self.glyphOrder = glyphOrder = ttFont.getGlyphOrder() + self._reverseGlyphOrder = {} + for i in range(0, len(loca) - 1): + try: + glyphName = glyphOrder[i] + except IndexError: + noname = noname + 1 + glyphName = "ttxautoglyph%s" % i + nextPos = int(loca[i + 1]) + glyphdata = data[pos:nextPos] + if len(glyphdata) != (nextPos - pos): + raise ttLib.TTLibError("not enough 'glyf' table data") + glyph = Glyph(glyphdata) + self.glyphs[glyphName] = glyph + pos = nextPos + if len(data) - nextPos >= 4: + log.warning( + "too much 'glyf' table data: expected %d, received %d bytes", + nextPos, + len(data), + ) + if noname: + log.warning("%s glyphs have no name", noname) if ttFont.lazy is False: # Be lazy for None and True self.ensureDecompiled()