diff --git a/Lib/fontTools/misc/testTools.py b/Lib/fontTools/misc/testTools.py index 0978ed040..dcd82cb56 100644 --- a/Lib/fontTools/misc/testTools.py +++ b/Lib/fontTools/misc/testTools.py @@ -62,6 +62,8 @@ class FakeFont: return self.glyphOrder_[glyphID] else: return "glyph%.5d" % glyphID + def getGlyphNameMany(self, lst): + return [self.getGlyphName(gid) for gid in lst] def getGlyphOrder(self): return self.glyphOrder_ diff --git a/Lib/fontTools/ttLib/tables/_c_m_a_p.py b/Lib/fontTools/ttLib/tables/_c_m_a_p.py index 19b5998d6..d129391ee 100644 --- a/Lib/fontTools/ttLib/tables/_c_m_a_p.py +++ b/Lib/fontTools/ttLib/tables/_c_m_a_p.py @@ -14,15 +14,12 @@ log = logging.getLogger(__name__) def _make_map(font, chars, gids): assert len(chars) == len(gids) + glyphNames = font.getGlyphNameMany(gids) cmap = {} glyphOrder = font.getGlyphOrder() - for char,gid in zip(chars,gids): + for char,gid,name in zip(chars,gids,glyphNames): if gid == 0: continue - try: - name = glyphOrder[gid] - except IndexError: - name = font.getGlyphName(gid) cmap[char] = name return cmap diff --git a/Lib/fontTools/ttLib/tables/otConverters.py b/Lib/fontTools/ttLib/tables/otConverters.py index 3733c079d..953df5778 100644 --- a/Lib/fontTools/ttLib/tables/otConverters.py +++ b/Lib/fontTools/ttLib/tables/otConverters.py @@ -347,14 +347,8 @@ class GlyphID(SimpleValue): staticSize = 2 typecode = "H" def readArray(self, reader, font, tableDict, count): - glyphOrder = font.getGlyphOrder() gids = reader.readArray(self.typecode, self.staticSize, count) - try: - l = [glyphOrder[gid] for gid in gids] - except IndexError: - # Slower, but will not throw an IndexError on an invalid glyph id. - l = [font.getGlyphName(gid) for gid in gids] - return l + return font.getGlyphNameMany(gids) def read(self, reader, font, tableDict): return font.getGlyphName(reader.readValue(self.typecode, self.staticSize)) def writeArray(self, writer, font, tableDict, values): @@ -1222,8 +1216,7 @@ class STXHeader(BaseConverter): def _readLigatures(self, reader, font): limit = len(reader.data) numLigatureGlyphs = (limit - reader.pos) // 2 - return [font.getGlyphName(g) - for g in reader.readUShortArray(numLigatureGlyphs)] + return font.getGlyphNameMany (reader.readUShortArray(numLigatureGlyphs)) def _countPerGlyphLookups(self, table): # Somewhat annoyingly, the morx table does not encode diff --git a/Lib/fontTools/ttLib/tables/otTables.py b/Lib/fontTools/ttLib/tables/otTables.py index c6ec31e4e..9dbbe8153 100644 --- a/Lib/fontTools/ttLib/tables/otTables.py +++ b/Lib/fontTools/ttLib/tables/otTables.py @@ -425,8 +425,7 @@ class InsertionMorphAction(AATAction): return [] reader = actionReader.getSubReader( actionReader.pos + index * 2) - return [font.getGlyphName(glyphID) - for glyphID in reader.readUShortArray(count)] + return font.getGlyphNameMany(reader.readUShortArray(count)) def toXML(self, xmlWriter, font, attrs, name): xmlWriter.begintag(name, **attrs) @@ -538,8 +537,7 @@ class Coverage(FormatSwitchingBaseTable): end = r.End startID = font.getGlyphID(start) endID = font.getGlyphID(end) + 1 - for glyphID in range(startID, endID): - glyphs.append(font.getGlyphName(glyphID)) + glyphs.extend(font.getGlyphNameMany(range(startID, endID))) else: self.glyphs = [] log.warning("Unknown Coverage format: %s", self.Format) @@ -755,7 +753,7 @@ class SingleSubst(FormatSwitchingBaseTable): delta = rawTable["DeltaGlyphID"] inputGIDS = [ font.getGlyphID(name) for name in input ] outGIDS = [ (glyphID + delta) % 65536 for glyphID in inputGIDS ] - outNames = [ font.getGlyphName(glyphID) for glyphID in outGIDS ] + outNames = font.getGlyphNameMany(outGIDS) for inp, out in zip(input, outNames): mapping[inp] = out elif self.Format == 2: @@ -915,9 +913,10 @@ class ClassDef(FormatSwitchingBaseTable): classList = rawTable["ClassValueArray"] startID = font.getGlyphID(start) endID = startID + len(classList) - for glyphID, cls in zip(range(startID, endID), classList): + glyphNames = font.getGlyphNameMany(range(startID, endID)) + for glyphName, cls in zip(glyphNames, classList): if cls: - classDefs[font.getGlyphName(glyphID)] = cls + classDefs[glyphName] = cls elif self.Format == 2: records = rawTable["ClassRangeRecord"] @@ -927,9 +926,10 @@ class ClassDef(FormatSwitchingBaseTable): cls = rec.Class startID = font.getGlyphID(start) endID = font.getGlyphID(end) + 1 - for glyphID in range(startID, endID): - if cls: - classDefs[font.getGlyphName(glyphID)] = cls + glyphNames = font.getGlyphNameMany(range(startID, endID)) + if cls: + for glyphName in glyphNames: + classDefs[glyphName] = cls else: log.warning("Unknown ClassDef format: %s", self.Format) self.classDefs = classDefs diff --git a/Lib/fontTools/ttLib/ttFont.py b/Lib/fontTools/ttLib/ttFont.py index 1df67b58d..8ea0c5f81 100644 --- a/Lib/fontTools/ttLib/ttFont.py +++ b/Lib/fontTools/ttLib/ttFont.py @@ -533,6 +533,11 @@ class TTFont(object): return self.getGlyphOrder()[glyphID] except IndexError: return "glyph%.5d" % glyphID + def getGlyphNameMany(self, lst): + glyphOrder = self.getGlyphOrder(); + cnt = len(glyphOrder) + return [glyphOrder[gid] if gid < cnt else "glyph%.5d" % gid + for gid in lst] def getGlyphID(self, glyphName): if not hasattr(self, "_reverseGlyphOrderDict"):