[cmap] changed some uses of list(map(...)) to list comprehensions an explicit loops, avoiding possibly large unneeded tmp lists; replaced use of oval() with int(); adding tests to verify all changes are covered

This commit is contained in:
justvanrossum 2019-03-03 10:30:05 +01:00
parent 835ee4a84b
commit d4a2d935c4
2 changed files with 45 additions and 12 deletions

View File

@ -444,11 +444,11 @@ class cmap_format_2(CmapSubtable):
nameMap = ttFont.getReverseGlyphMap()
lenCharCodes = len(charCodes)
try:
gids = list(map(operator.getitem, [nameMap]*lenCharCodes, names))
gids = [nameMap[name] for name in names]
except KeyError:
nameMap = ttFont.getReverseGlyphMap(rebuild=True)
try:
gids = list(map(operator.getitem, [nameMap]*lenCharCodes, names))
gids = [nameMap[name] for name in names]
except KeyError:
# allow virtual GIDs in format 2 tables
gids = []
@ -458,7 +458,7 @@ class cmap_format_2(CmapSubtable):
except KeyError:
try:
if (name[:3] == 'gid'):
gid = eval(name[3:])
gid = int(name[3:])
else:
gid = ttFont.getGlyphID(name)
except:
@ -750,14 +750,14 @@ class cmap_format_4(CmapSubtable):
endCode = [0xffff]
else:
charCodes.sort()
names = list(map(operator.getitem, [self.cmap]*lenCharCodes, charCodes))
names = [self.cmap[code] for code in charCodes]
nameMap = ttFont.getReverseGlyphMap()
try:
gids = list(map(operator.getitem, [nameMap]*lenCharCodes, names))
gids = [nameMap[name] for name in names]
except KeyError:
nameMap = ttFont.getReverseGlyphMap(rebuild=True)
try:
gids = list(map(operator.getitem, [nameMap]*lenCharCodes, names))
gids = [nameMap[name] for name in names]
except KeyError:
# allow virtual GIDs in format 4 tables
gids = []
@ -767,7 +767,7 @@ class cmap_format_4(CmapSubtable):
except KeyError:
try:
if (name[:3] == 'gid'):
gid = eval(name[3:])
gid = int(name[3:])
else:
gid = ttFont.getGlyphID(name)
except:
@ -775,7 +775,8 @@ class cmap_format_4(CmapSubtable):
gids.append(gid)
cmap = {} # code:glyphID mapping
list(map(operator.setitem, [cmap]*len(charCodes), charCodes, gids))
for code, gid in zip(charCodes, gids):
cmap[code] = gid
# Build startCode and endCode lists.
# Split the char codes in ranges of consecutive char codes, then split
@ -959,11 +960,11 @@ class cmap_format_12_or_13(CmapSubtable):
names = list(self.cmap.values())
nameMap = ttFont.getReverseGlyphMap()
try:
gids = list(map(operator.getitem, [nameMap]*lenCharCodes, names))
gids = [nameMap[name] for name in names]
except KeyError:
nameMap = ttFont.getReverseGlyphMap(rebuild=True)
try:
gids = list(map(operator.getitem, [nameMap]*lenCharCodes, names))
gids = [nameMap[name] for name in names]
except KeyError:
# allow virtual GIDs in format 12 tables
gids = []
@ -973,7 +974,7 @@ class cmap_format_12_or_13(CmapSubtable):
except KeyError:
try:
if (name[:3] == 'gid'):
gid = eval(name[3:])
gid = int(name[3:])
else:
gid = ttFont.getGlyphID(name)
except:
@ -982,7 +983,8 @@ class cmap_format_12_or_13(CmapSubtable):
gids.append(gid)
cmap = {} # code:glyphID mapping
list(map(operator.setitem, [cmap]*len(charCodes), charCodes, gids))
for code, gid in zip(charCodes, gids):
cmap[code] = gid
charCodes.sort()
index = 0

View File

@ -51,6 +51,37 @@ class CmapSubtableTest(unittest.TestCase):
self.assertEqual(subtable.getEncoding("ascii"), "ascii")
self.assertEqual(subtable.getEncoding(default="xyz"), "xyz")
def test_compile_2(self):
subtable = self.makeSubtable(2, 1, 2, 0)
subtable.cmap = {c: "cid%05d" % c for c in range(32, 8192)}
font = ttLib.TTFont()
font.setGlyphOrder([".notdef"] + list(subtable.cmap.values()))
data = subtable.compile(font)
subtable2 = CmapSubtable.newSubtable(2)
subtable2.decompile(data, font)
self.assertEqual(subtable2.cmap, subtable.cmap)
def test_compile_2_rebuild_rev_glyph_order(self):
for fmt in [2, 4, 12]:
subtable = self.makeSubtable(fmt, 1, 2, 0)
subtable.cmap = {c: "cid%05d" % c for c in range(32, 8192)}
font = ttLib.TTFont()
font.setGlyphOrder([".notdef"] + list(subtable.cmap.values()))
font._reverseGlyphOrderDict = {} # force first KeyError branch in subtable.compile()
data = subtable.compile(font)
subtable2 = CmapSubtable.newSubtable(fmt)
subtable2.decompile(data, font)
self.assertEqual(subtable2.cmap, subtable.cmap, str(fmt))
def test_compile_2_gids(self):
for fmt in [2, 4, 12]:
subtable = self.makeSubtable(fmt, 1, 3, 0)
subtable.cmap = {0x0041:'gid001', 0x0042:'gid002'}
font = ttLib.TTFont()
font.setGlyphOrder([".notdef"])
data = subtable.compile(font)
def test_decompile_4(self):
subtable = CmapSubtable.newSubtable(4)
font = ttLib.TTFont()