From df3d86a37f82e78cd59b8497a3117f756020d4aa Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Mon, 30 Sep 2024 09:00:32 +0100 Subject: [PATCH 1/2] Rewrite this in terms of the more general function --- Lib/gftools/fix.py | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/Lib/gftools/fix.py b/Lib/gftools/fix.py index c2dfe878..2f273fd3 100644 --- a/Lib/gftools/fix.py +++ b/Lib/gftools/fix.py @@ -671,18 +671,10 @@ def drop_superfluous_mac_names(ttfont) -> FixResult: such as Word 2011. IDs 1-6 are very common, > 16 are edge cases. https://www.microsoft.com/typography/otspec/name.htm""" - keep_ids = [1, 2, 3, 4, 5, 6, 16, 17, 18, 20, 21, 22, 25] - messages = [] - for n in range(255): - if n not in keep_ids: - name = ttfont["name"].getName(n, 1, 0, 0) - if name: - messages.append(f"Removed nameID {n}: {name.toStr()}") - ttfont["name"].names.remove(name) - return ttfont, messages + drop_mac_names(ttfont, keep_ids=[1, 2, 3, 4, 5, 6, 16, 17, 18, 20, 21, 22, 25]) -def drop_mac_names(ttfont) -> FixResult: +def drop_mac_names(ttfont, keep_ids=[]) -> FixResult: """Drop all mac names""" messages = [] for n in range(255): From 7c91435d9b06e50030e100720f3bc568f7e3c183 Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Mon, 30 Sep 2024 09:01:04 +0100 Subject: [PATCH 2/2] Check all name records, not just first 255 --- Lib/gftools/fix.py | 16 +++++++++++----- Lib/gftools/utils.py | 10 ++++++---- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Lib/gftools/fix.py b/Lib/gftools/fix.py index 2f273fd3..bc22c096 100644 --- a/Lib/gftools/fix.py +++ b/Lib/gftools/fix.py @@ -677,11 +677,17 @@ def drop_superfluous_mac_names(ttfont) -> FixResult: def drop_mac_names(ttfont, keep_ids=[]) -> FixResult: """Drop all mac names""" messages = [] - for n in range(255): - name = ttfont["name"].getName(n, 1, 0, 0) - if name: - ttfont["name"].names.remove(name) - messages.append(f"Removed nameID {n}: {name.toStr()}") + for namerecord in list( # list() to avoid removing while iterating + ttfont["name"].names + ): + if namerecord.nameID not in keep_ids: + messages.append(f"Removed nameID {namerecord.nameID}: {namerecord.toStr()}") + if ( + namerecord.platformID == 1 + and namerecord.platEncID == 0 + and namerecord.langID == 0 + ): + ttfont["name"].names.remove(namerecord) return ttfont, messages diff --git a/Lib/gftools/utils.py b/Lib/gftools/utils.py index 75fdff46..70dca913 100644 --- a/Lib/gftools/utils.py +++ b/Lib/gftools/utils.py @@ -520,10 +520,12 @@ def has_mac_names(ttfont): """Check if a font has Mac names. Mac names have the following field values: platformID: 1, encodingID: 0, LanguageID: 0""" - for i in range(255): - if ttfont["name"].getName(i, 1, 0, 0): - return True - return False + return any( + namerecord.platformID == 1 + and namerecord.platEncID == 0 + and namerecord.langID == 0 + for namerecord in ttfont["name"].names + ) def font_is_italic(ttfont):