From d4a2d935c407ad4ed7309e8c1ce8c96fdaeea415 Mon Sep 17 00:00:00 2001 From: justvanrossum Date: Sun, 3 Mar 2019 10:30:05 +0100 Subject: [PATCH] [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 --- Lib/fontTools/ttLib/tables/_c_m_a_p.py | 26 +++++++++++---------- Tests/ttLib/tables/_c_m_a_p_test.py | 31 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/Lib/fontTools/ttLib/tables/_c_m_a_p.py b/Lib/fontTools/ttLib/tables/_c_m_a_p.py index 1767ad4c6..9923a1645 100644 --- a/Lib/fontTools/ttLib/tables/_c_m_a_p.py +++ b/Lib/fontTools/ttLib/tables/_c_m_a_p.py @@ -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 diff --git a/Tests/ttLib/tables/_c_m_a_p_test.py b/Tests/ttLib/tables/_c_m_a_p_test.py index 306d04894..0d055a13a 100644 --- a/Tests/ttLib/tables/_c_m_a_p_test.py +++ b/Tests/ttLib/tables/_c_m_a_p_test.py @@ -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()