From 4e316cae575136fa0527e57e470e7afd38c3f6c9 Mon Sep 17 00:00:00 2001 From: Marc Foley Date: Tue, 10 Sep 2019 09:45:13 +0100 Subject: [PATCH] [name] add removeNames method to table__n_a_m_e --- Lib/fontTools/ttLib/tables/_n_a_m_e.py | 25 +++++++++++++++++++ Tests/otlLib/__init__.py | 0 Tests/ttLib/tables/_n_a_m_e_test.py | 33 ++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 Tests/otlLib/__init__.py diff --git a/Lib/fontTools/ttLib/tables/_n_a_m_e.py b/Lib/fontTools/ttLib/tables/_n_a_m_e.py index 6bca7f24d..a25f85b13 100644 --- a/Lib/fontTools/ttLib/tables/_n_a_m_e.py +++ b/Lib/fontTools/ttLib/tables/_n_a_m_e.py @@ -147,6 +147,31 @@ class table__n_a_m_e(DefaultTable.DefaultTable): else: self.names.append(makeName(string, nameID, platformID, platEncID, langID)) + def removeNames(self, nameID=None, platformID=None, platEncID=None, langID=None): + """Remove any name records identified by 'nameID', 'platformID', 'platEncID' + or 'langID'. + """ + args = { + argName: argValue + for argName, argValue in ( + ("nameID", nameID), + ("platformID", platformID), + ("platEncID", platEncID), + ("langID", langID), + ) + if argValue is not None + } + if not args: + # no arguments, nothing to do + return + self.names = [ + rec for rec in self.names + if any( + argValue != getattr(rec, argName) + for argName, argValue in args.items() + ) + ] + def _findUnusedNameID(self, minNameID=256): """Finds an unused name id. diff --git a/Tests/otlLib/__init__.py b/Tests/otlLib/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/Tests/ttLib/tables/_n_a_m_e_test.py b/Tests/ttLib/tables/_n_a_m_e_test.py index 6ffed87b4..67d8db4cc 100644 --- a/Tests/ttLib/tables/_n_a_m_e_test.py +++ b/Tests/ttLib/tables/_n_a_m_e_test.py @@ -75,6 +75,39 @@ class NameTableTest(unittest.TestCase): with self.assertRaises(TypeError): table.addName(b"abc") # must be unicode string + def test_removeNames(self): + table = table__n_a_m_e() + table.setName("Regular", 2, 1, 0, 0) + table.setName("Regular", 2, 3, 1, 0x409) + table.removeNames(nameID=2) + self.assertEqual(table.names, []) + + table = table__n_a_m_e() + table.setName("FamilyName", 1, 1, 0, 0) + table.setName("Regular", 2, 1, 0, 0) + table.setName("FamilyName", 1, 3, 1, 0x409) + table.setName("Regular", 2, 3, 1, 0x409) + table.removeNames(platformID=1) + self.assertEqual(len(table.names), 2) + + table = table__n_a_m_e() + table.setName("FamilyName", 1, 1, 0, 0) + table.setName("Regular", 2, 1, 0, 0) + table.removeNames(nameID=1) + self.assertEqual(len(table.names), 1) + + table = table__n_a_m_e() + table.setName("FamilyName", 1, 1, 0, 0) + table.setName("Regular", 2, 1, 0, 0) + table.removeNames(2, 1, 0, 0) + self.assertEqual(len(table.names), 1) + + table = table__n_a_m_e() + table.setName("FamilyName", 1, 1, 0, 0) + table.setName("Regular", 2, 1, 0, 0) + table.removeNames() + self.assertEqual(len(table.names), 2) + def test_addMultilingualName(self): # Microsoft Windows has language codes for “English” (en) # and for “Standard German as used in Switzerland” (de-CH).