TTFont: load all on open w/ lazy=False; add ensureDecompiled

Make lazy=False actually do what it says, 'load everything eagerly'. It feels weird that one has to, not only say, open with lazy=False, but also have to load each tables individually... Didn't I say don't be lazy?!

Also it can be useful to get to a eager, non-lazy font whether or not it was originally loaded lazily, so I added an ensureDecompiled method that decompiles all the tables and calls ensureDecompiled for those (e.g. cmap, glyf and otData-driven tables like GSUB, GPOS, etc.) that respect the lazy attribute.
This commit is contained in:
Cosimo Lupo 2022-03-17 13:00:30 +00:00
parent ee27b73d7c
commit 795bccd966

View File

@ -129,7 +129,7 @@ class TTFont(object):
closeStream = False
file.seek(0)
if not self.lazy:
if self.lazy is None:
# read input file in memory and wrap a stream around it to allow overwriting
file.seek(0)
tmp = BytesIO(file.read())
@ -139,12 +139,19 @@ class TTFont(object):
if closeStream:
file.close()
file = tmp
self._tableCache = _tableCache
self.reader = SFNTReader(file, checkChecksums, fontNumber=fontNumber)
self.sfntVersion = self.reader.sfntVersion
self.flavor = self.reader.flavor
self.flavorData = self.reader.flavorData
if self.lazy is False:
# if lazy=False immediately load all the tables
self.ensureDecompiled()
if closeStream:
file.close()
def __enter__(self):
return self
@ -378,6 +385,14 @@ class TTFont(object):
keys = sortedTagList(keys)
return ["GlyphOrder"] + keys
def ensureDecompiled(self):
"""Decompile all the tables, even if a TTFont was opened in 'lazy' mode."""
for tag in self.keys():
table = self[tag]
if self.lazy is not False and hasattr(table, "ensureDecompiled"):
table.ensureDecompiled()
self.lazy = False
def __len__(self):
return len(list(self.keys()))