[glyf] Load using LazyDict

Going to revert. No speedup since glyphs are loaded lazy
anyway.  And one test fails I have no idea why:

Tests/ttLib/woff2_test.py::WOFF2GlyfTableTest::test_reconstruct_glyf_padded_4
This commit is contained in:
Behdad Esfahbod 2023-12-16 21:15:20 -07:00
parent 3ff2ee61e1
commit cae76d540a
2 changed files with 21 additions and 31 deletions

View File

@ -4,10 +4,6 @@ __all__ = ["LazyDict", "LazyList"]
class LazyDict(UserDict): class LazyDict(UserDict):
def __init__(self, data):
super().__init__()
self.data = data
def __getitem__(self, k): def __getitem__(self, k):
v = self.data[k] v = self.data[k]
if callable(v): if callable(v):

View File

@ -8,6 +8,7 @@ from fontTools.misc.transform import DecomposedTransform
from fontTools.misc.textTools import tostr, safeEval, pad from fontTools.misc.textTools import tostr, safeEval, pad
from fontTools.misc.arrayTools import updateBounds, pointInRect from fontTools.misc.arrayTools import updateBounds, pointInRect
from fontTools.misc.bezierTools import calcQuadraticBounds from fontTools.misc.bezierTools import calcQuadraticBounds
from fontTools.misc.lazyTools import LazyDict
from fontTools.misc.fixedTools import ( from fontTools.misc.fixedTools import (
fixedToFloat as fi2fl, fixedToFloat as fi2fl,
floatToFixed as fl2fi, floatToFixed as fl2fi,
@ -53,6 +54,19 @@ version = ".".join(version.split(".")[:2])
SCALE_COMPONENT_OFFSET_DEFAULT = 0 # 0 == MS, 1 == Apple 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): class table__g_l_y_f(DefaultTable.DefaultTable):
"""Glyph Data Table """Glyph Data Table
@ -97,33 +111,13 @@ class table__g_l_y_f(DefaultTable.DefaultTable):
[axis.axisTag for axis in ttFont["fvar"].axes] if "fvar" in ttFont else [] [axis.axisTag for axis in ttFont["fvar"].axes] if "fvar" in ttFont else []
) )
loca = ttFont["loca"] loca = ttFont["loca"]
pos = int(loca[0]) self.glyphOrder = ttFont.getGlyphOrder()
nextPos = 0
noname = 0 l = self.glyphs = LazyDict({k: _load_glyph for k in self.glyphOrder})
self.glyphs = {} l.reverseGlyphMap = self._reverseGlyphOrder = ttFont.getReverseGlyphMap()
self.glyphOrder = glyphOrder = ttFont.getGlyphOrder() l.loca = loca
self._reverseGlyphOrder = {} l.glyphData = data
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 if ttFont.lazy is False: # Be lazy for None and True
self.ensureDecompiled() self.ensureDecompiled()