From 0f17da083669d110b3b9c0a309d540321be18c00 Mon Sep 17 00:00:00 2001 From: Olli Meier Date: Thu, 10 Feb 2022 13:44:42 +0100 Subject: [PATCH] Bugfix: The script to create a STAt table created multiple unnecessary name table entries, because it did not take care a bout existing entries with the same name. --- Lib/fontTools/otlLib/builder.py | 3 ++ Lib/fontTools/ttLib/tables/_n_a_m_e.py | 11 ++++++++ Tests/otlLib/builder_test.py | 38 +++++++++++++------------- 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/Lib/fontTools/otlLib/builder.py b/Lib/fontTools/otlLib/builder.py index cafb8ba0a..6ceebac44 100644 --- a/Lib/fontTools/otlLib/builder.py +++ b/Lib/fontTools/otlLib/builder.py @@ -2831,6 +2831,9 @@ def _addName(nameTable, value, minNameID=0): # Already a nameID return value if isinstance(value, str): + nameID = nameTable.addName(value, platforms=((3, 1, 0x409), ), minNameID=minNameID) + if nameID is not None: + return nameID names = dict(en=value) elif isinstance(value, dict): names = value diff --git a/Lib/fontTools/ttLib/tables/_n_a_m_e.py b/Lib/fontTools/ttLib/tables/_n_a_m_e.py index 9558addbb..c779f77d0 100644 --- a/Lib/fontTools/ttLib/tables/_n_a_m_e.py +++ b/Lib/fontTools/ttLib/tables/_n_a_m_e.py @@ -350,6 +350,17 @@ class table__n_a_m_e(DefaultTable.DefaultTable): if not isinstance(string, str): raise TypeError( "expected str, found %s: %r" % (type(string).__name__, string)) + + for platform in platforms: + for name_rec in self.names: + name_rec_platform = (name_rec.platformID, name_rec.platEncID, name_rec.langID) + if name_rec_platform != platform: + continue + if name_rec.string == string: + # if name ID exists already, don't create a new one. + # Instead return the name ID of the existing one. + return name_rec.nameID + nameID = self._findUnusedNameID(minNameID + 1) for platformID, platEncID, langID in platforms: self.names.append(makeName(string, nameID, platformID, platEncID, langID)) diff --git a/Tests/otlLib/builder_test.py b/Tests/otlLib/builder_test.py index 663524deb..9e8020d80 100644 --- a/Tests/otlLib/builder_test.py +++ b/Tests/otlLib/builder_test.py @@ -1,6 +1,6 @@ import io import struct -from fontTools.misc.fixedTools import floatToFixed +from fontTools.misc.fixedTools import floatToFixed, fixedToFloat from fontTools.misc.testTools import getXML from fontTools.otlLib import builder, error from fontTools import ttLib @@ -1409,8 +1409,6 @@ def test_buildStatTable_name_duplicates(): even if the name exists in the name table already. ''' - from fontTools.misc.fixedTools import fixedToFloat - font_obj = ttLib.TTFont() font_obj["name"] = ttLib.newTable("name") font_obj["name"].names = [] @@ -1458,27 +1456,29 @@ def test_buildStatTable_name_duplicates(): ), ] - font_obj["name"].setName('ExtraLight', 260, 1, 0, 0) - font_obj["name"].setName('Light', 261, 1, 0, 0) - font_obj["name"].setName('Regular', 262, 1, 0, 0) - font_obj["name"].setName('Medium', 263, 1, 0, 0) - font_obj["name"].setName('Bold', 264, 1, 0, 0) - font_obj["name"].setName('ExtraBold', 265, 1, 0, 0) - font_obj["name"].setName('Black', 266, 1, 0, 0) + font_obj["name"].setName('ExtraLight', 260, 3, 1, 0x409) + font_obj["name"].setName('Light', 261, 3, 1, 0x409) + font_obj["name"].setName('Regular', 262, 3, 1, 0x409) + font_obj["name"].setName('Medium', 263, 3, 1, 0x409) + font_obj["name"].setName('Bold', 264, 3, 1, 0x409) + font_obj["name"].setName('ExtraBold', 265, 3, 1, 0x409) + font_obj["name"].setName('Black', 266, 3, 1, 0x409) - font_obj["name"].setName('Micro', 270, 1, 0, 0) - font_obj["name"].setName('Text', 271, 1, 0, 0) - font_obj["name"].setName('Headline', 272, 1, 0, 0) - font_obj["name"].setName('Poster', 273, 1, 0, 0) + font_obj["name"].setName('Micro', 270, 3, 1, 0x409) + font_obj["name"].setName('Text', 271, 3, 1, 0x409) + font_obj["name"].setName('Headline', 272, 3, 1, 0x409) + font_obj["name"].setName('Poster', 273, 3, 1, 0x409) - font_obj["name"].setName('Optical Size', 280, 1, 0, 0) - font_obj["name"].setName('Weight', 281, 1, 0, 0) - font_obj["name"].setName('Italic', 281, 1, 0, 0) + font_obj["name"].setName('Optical Size', 280, 3, 1, 0x409) + font_obj["name"].setName('Weight', 281, 3, 1, 0x409) + font_obj["name"].setName('Italic', 282, 3, 1, 0x409) + + expected_names = [x.string for x in font_obj["name"].names] - expected_names_len = len(font_obj["name"].names) builder.buildStatTable(font_obj, AXES) + actual_names = [x.string for x in font_obj["name"].names] - assert expected_names_len == len(font_obj["name"].names) + assert expected_names == actual_names def test_stat_infinities():