Revert "[glyf] Load using LazyDict"

This reverts commit b493729eff954909672694b67a453964f33ac893.
This commit is contained in:
Behdad Esfahbod 2023-12-16 21:37:05 -07:00
parent cae76d540a
commit 0a7993998d
2 changed files with 31 additions and 21 deletions

View File

@ -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):

View File

@ -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()