From 795bccd966b8dd251af737b7a04c2e94c5a99009 Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Thu, 17 Mar 2022 13:00:30 +0000 Subject: [PATCH] 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. --- Lib/fontTools/ttLib/ttFont.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Lib/fontTools/ttLib/ttFont.py b/Lib/fontTools/ttLib/ttFont.py index 692f99ac8..4adf34af1 100644 --- a/Lib/fontTools/ttLib/ttFont.py +++ b/Lib/fontTools/ttLib/ttFont.py @@ -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()))