Based on the discussion with Just and anthrotype: Undo the changes in the name table and extend unittests. If there are inconsistencies in the name table, it's ok to create new name IDs, even if this might not be the most efficient way of creating the name table.

This commit is contained in:
Olli Meier 2022-02-10 21:24:51 +01:00
parent 513307237b
commit e968e8fd7f
3 changed files with 24 additions and 20 deletions

View File

@ -311,16 +311,6 @@ class table__n_a_m_e(DefaultTable.DefaultTable):
if nameID is not None:
return nameID
nameID = self._findUnusedNameID()
nameID_win = self.findMultilingualName(names, windows=True, mac=False)
nameID_mac = self.findMultilingualName(names, windows=False, mac=True)
if nameID_win and nameID_mac is None:
nameID = nameID_win
windows = False
if nameID_mac and nameID_win is None:
nameID = nameID_mac
mac = False
# TODO: Should minimize BCP 47 language codes.
# https://github.com/fonttools/fonttools/issues/930
for lang, name in sorted(names.items()):

View File

@ -1462,15 +1462,16 @@ def test_buildStatTable_name_duplicates():
builder.buildStatTable(font_obj, AXES, windowsNames=True, macNames=True)
actual_names = [x.string for x in font_obj["name"].names]
expected_names = ['Weight', 'Weight',
'ExtraLight', 'ExtraLight',
'Light', 'Light',
'Regular', 'Regular',
'Medium', 'Medium',
'Bold', 'Bold',
'ExtraBold', 'ExtraBold',
'Black', 'Black']
# Multiple name records were added by buildStatTable
expected_names = ['Weight', 'Weight', 'Weight',
'ExtraLight', 'ExtraLight', 'ExtraLight',
'Light', 'Light', 'Light',
'Regular', 'Regular', 'Regular',
'Medium', 'Medium', 'Medium',
'Bold', 'Bold', 'Bold',
'ExtraBold', 'ExtraBold', 'ExtraBold',
'Black', 'Black', 'Black']
# Because there is an inconsistency in the names add new name IDs
# for each platform -> windowsNames=True, macNames=True
assert sorted(expected_names) == sorted(actual_names)

View File

@ -317,11 +317,24 @@ class NameTableTest(unittest.TestCase):
font_obj["name"].names = []
font_obj["name"].setName('Weight', 270, 3, 1, 0x409)
font_obj["name"].setName('Weight', 270, 1, 0, 0)
names = {'en': 'Weight', }
nameID = font_obj["name"].addMultilingualName(names, minNameID=256)
self.assertEqual(270, nameID)
font_obj["name"].removeNames(nameID=270, platformID=1) # remove Mac name
nameID = font_obj["name"].addMultilingualName(names, minNameID=256)
# Because there is an inconsistency in the names add a new name ID
self.assertEqual(271, nameID)
font_obj["name"].removeNames(platformID=3) # remove all Windows names
font_obj["name"].removeNames(platformID=1) # remove all Mac names
nameID = font_obj["name"].addMultilingualName(names, minNameID=256)
#Because there is no name ID with the name 'Weight',
# take the next available name ID -> minNameID=256
self.assertEqual(256, nameID)
def test_decompile_badOffset(self):
# https://github.com/fonttools/fonttools/issues/525