[ttFont] Add getGlyphNameMany()

Part of fixing https://github.com/fonttools/fonttools/pull/1654
Related https://github.com/fonttools/fonttools/issues/2334
This commit is contained in:
Behdad Esfahbod 2021-08-20 16:03:03 -06:00
parent 89fe3bd3c5
commit 3ec769907b
5 changed files with 21 additions and 24 deletions

View File

@ -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_

View File

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

View File

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

View File

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

View File

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