From 0e9c48e6ab11c49e78e3609000cbbb5b21948946 Mon Sep 17 00:00:00 2001 From: Miguel Sousa Date: Mon, 29 Aug 2022 06:35:45 -0700 Subject: [PATCH] Don't crash when `post` format 1 is improperly used Fixes #2736 --- Lib/fontTools/ttLib/ttFont.py | 7 +++++++ Tests/ttLib/data/bogus_post_format_1.ttf | Bin 0 -> 3840 bytes Tests/ttLib/ttFont_test.py | 8 ++++++++ 3 files changed, 15 insertions(+) create mode 100644 Tests/ttLib/data/bogus_post_format_1.ttf diff --git a/Lib/fontTools/ttLib/ttFont.py b/Lib/fontTools/ttLib/ttFont.py index dbfcb6e89..e6fe9a673 100644 --- a/Lib/fontTools/ttLib/ttFont.py +++ b/Lib/fontTools/ttLib/ttFont.py @@ -487,6 +487,13 @@ class TTFont(object): # in combination with the Adobe Glyph List (AGL). # self._getGlyphNamesFromCmap() + elif len(glyphOrder) < self['maxp'].numGlyphs: + # + # Not enough names found in the 'post' table. + # Can happen when 'post' format 1 is improperly used on a font that + # has more than 258 glyphs (the lenght of 'standardGlyphOrder'). + # + self._getGlyphNamesFromCmap() else: self.glyphOrder = glyphOrder else: diff --git a/Tests/ttLib/data/bogus_post_format_1.ttf b/Tests/ttLib/data/bogus_post_format_1.ttf new file mode 100644 index 0000000000000000000000000000000000000000..62b80213c7d48e8ff7ecc90329c3c53d21aa628c GIT binary patch literal 3840 zcmeH}jbE148OMLuxgUXy6w!PEA9g>FhzSUYh=8x4Oh6Px#TOD|3POUQs9%59(rR-(t)WnCBl@!nIzO`v0>pvsO zWqjc{8MKS_*GXhq<>I<8mt+=mXb=$4yP|3i>$==}2rxn_i|eZ80q8%TCT4Y2&0@QS zPR+V)*F~)hTho+1yYsNGVHkQGj?Q|X^8ud33m4SV4YMO(OIP+ z#Mo?IQXk^W`<~ z{rzo{KGuG=(SB}^Yy2Tk1HKp?P156FV=Lt*=Rj3S^HI-RjyJ=f>my05M6(TacW_(kN%XyNd!qIc_g3c zG2x#|%IR<9@c?NF(9WfLv*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)`bmF@<>^<99r%W) p&sBVhuW%6;@GpGL-FzD?nJwAe$#-5C88@3ZEpXEU`qaKb`7h%nrDp&D literal 0 HcmV?d00001 diff --git a/Tests/ttLib/ttFont_test.py b/Tests/ttLib/ttFont_test.py index ad0810612..3cf7075e3 100644 --- a/Tests/ttLib/ttFont_test.py +++ b/Tests/ttLib/ttFont_test.py @@ -4,6 +4,7 @@ import re import random from fontTools.feaLib.builder import addOpenTypeFeaturesFromString from fontTools.ttLib import TTFont, TTLibError, newTable, registerCustomTableClass, unregisterCustomTableClass +from fontTools.ttLib.standardGlyphOrder import standardGlyphOrder from fontTools.ttLib.tables.DefaultTable import DefaultTable from fontTools.ttLib.tables._c_m_a_p import CmapSubtable import pytest @@ -143,6 +144,13 @@ def test_setGlyphOrder_also_updates_glyf_glyphOrder(): assert font["glyf"].glyphOrder == new_order +def test_getGlyphOrder_not_true_post_format_1(): + # https://github.com/fonttools/fonttools/issues/2736 + font = TTFont(os.path.join(DATA_DIR, "bogus_post_format_1.ttf")) + hmtx = font["hmtx"] + assert len(hmtx.metrics) > len(standardGlyphOrder) + + @pytest.mark.parametrize("lazy", [None, True, False]) def test_ensureDecompiled(lazy): # test that no matter the lazy value, ensureDecompiled decompiles all tables