From ef95a58c3c23066afd52fc45b6b6560af5337898 Mon Sep 17 00:00:00 2001 From: NightFurySL2001 <33471049+NightFurySL2001@users.noreply.github.com> Date: Fri, 7 Jul 2023 21:36:40 +0800 Subject: [PATCH 1/5] Convert panoseDefault to namespace object --- Lib/fontTools/fontBuilder.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/fontTools/fontBuilder.py b/Lib/fontTools/fontBuilder.py index 15a550e22..bd7d6bb44 100644 --- a/Lib/fontTools/fontBuilder.py +++ b/Lib/fontTools/fontBuilder.py @@ -135,7 +135,7 @@ from .ttLib.tables._g_l_y_f import flagCubic from .misc.timeTools import timestampNow import struct from collections import OrderedDict - +from types import SimpleNamespace _headDefaults = dict( tableVersion=1.0, @@ -263,7 +263,7 @@ _nameIDs = dict( # to insert in setupNameTable doc string: # print("\n".join(("%s (nameID %s)" % (k, v)) for k, v in sorted(_nameIDs.items(), key=lambda x: x[1]))) -_panoseDefaults = dict( +_panoseDefaults = SimpleNamespace( bFamilyType=0, bSerifStyle=0, bWeight=0, From ef8e15a08c0775e6394aee38d962aa5c574fc80f Mon Sep 17 00:00:00 2001 From: NightFurySL2001 <33471049+NightFurySL2001@users.noreply.github.com> Date: Fri, 7 Jul 2023 22:09:58 +0800 Subject: [PATCH 2/5] Use Panose class --- Lib/fontTools/fontBuilder.py | 16 +++------------- Lib/fontTools/ttLib/tables/O_S_2f_2.py | 8 ++++++++ 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/Lib/fontTools/fontBuilder.py b/Lib/fontTools/fontBuilder.py index bd7d6bb44..8f83ea800 100644 --- a/Lib/fontTools/fontBuilder.py +++ b/Lib/fontTools/fontBuilder.py @@ -132,10 +132,11 @@ fb.save("test.otf") from .ttLib import TTFont, newTable from .ttLib.tables._c_m_a_p import cmap_classes from .ttLib.tables._g_l_y_f import flagCubic +from .ttLib.tables.O_S_2f_2 import Panose from .misc.timeTools import timestampNow import struct from collections import OrderedDict -from types import SimpleNamespace + _headDefaults = dict( tableVersion=1.0, @@ -263,18 +264,7 @@ _nameIDs = dict( # to insert in setupNameTable doc string: # print("\n".join(("%s (nameID %s)" % (k, v)) for k, v in sorted(_nameIDs.items(), key=lambda x: x[1]))) -_panoseDefaults = SimpleNamespace( - bFamilyType=0, - bSerifStyle=0, - bWeight=0, - bProportion=0, - bContrast=0, - bStrokeVariation=0, - bArmStyle=0, - bLetterForm=0, - bMidline=0, - bXHeight=0, -) +_panoseDefaults = Panose() _OS2Defaults = dict( version=3, diff --git a/Lib/fontTools/ttLib/tables/O_S_2f_2.py b/Lib/fontTools/ttLib/tables/O_S_2f_2.py index fc50b228a..bebcf8bf7 100644 --- a/Lib/fontTools/ttLib/tables/O_S_2f_2.py +++ b/Lib/fontTools/ttLib/tables/O_S_2f_2.py @@ -25,6 +25,14 @@ panoseFormat = """ class Panose(object): + def __init__(self, panoseDict=None): + formatstring, names, fixes = sstruct.getformat(panoseFormat) + for name in names: + if panoseDict is not None and name in panoseDict: + setattr(self, name, safeEval(panoseDict[name])) + else: + setattr(self, name, 0) + def toXML(self, writer, ttFont): formatstring, names, fixes = sstruct.getformat(panoseFormat) for name in names: From be44c92cfa68d1adecf026608fc02cd2357f2546 Mon Sep 17 00:00:00 2001 From: NFSL2001 <33471049+NightFurySL2001@users.noreply.github.com> Date: Fri, 7 Jul 2023 23:12:44 +0800 Subject: [PATCH 3/5] Update Lib/fontTools/ttLib/tables/O_S_2f_2.py Co-authored-by: Cosimo Lupo --- Lib/fontTools/ttLib/tables/O_S_2f_2.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Lib/fontTools/ttLib/tables/O_S_2f_2.py b/Lib/fontTools/ttLib/tables/O_S_2f_2.py index bebcf8bf7..7b403026a 100644 --- a/Lib/fontTools/ttLib/tables/O_S_2f_2.py +++ b/Lib/fontTools/ttLib/tables/O_S_2f_2.py @@ -25,13 +25,12 @@ panoseFormat = """ class Panose(object): - def __init__(self, panoseDict=None): - formatstring, names, fixes = sstruct.getformat(panoseFormat) + def __init__(self, **kwargs): + _, names, _ = sstruct.getformat(panoseFormat) for name in names: - if panoseDict is not None and name in panoseDict: - setattr(self, name, safeEval(panoseDict[name])) - else: - setattr(self, name, 0) + setattr(self, name, kwargs.pop(name, 0)) + for k in kwargs: + raise TypeError(f"Panose() got an unexpected keyword argument {k!r}") def toXML(self, writer, ttFont): formatstring, names, fixes = sstruct.getformat(panoseFormat) From 20236624e1d0049a2ca2ca58c439628f7d8c63ca Mon Sep 17 00:00:00 2001 From: NightFurySL2001 <33471049+NightFurySL2001@users.noreply.github.com> Date: Fri, 7 Jul 2023 23:34:02 +0800 Subject: [PATCH 4/5] Add panose test for fontbuilder --- Tests/fontBuilder/fontBuilder_test.py | 46 +++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/Tests/fontBuilder/fontBuilder_test.py b/Tests/fontBuilder/fontBuilder_test.py index a2e58be8a..53fefa927 100644 --- a/Tests/fontBuilder/fontBuilder_test.py +++ b/Tests/fontBuilder/fontBuilder_test.py @@ -420,3 +420,49 @@ def test_unicodeVariationSequences(tmpdir): fb.setupCharacterMap(cmap, uvs) fb.save(outPath) _verifyOutput(outPath, tables=["cmap"]) + + +def test_setupPanose(is_ttf, keep_glyph_names, make_cff2, post_format): + from fontTools.ttLib.tables.O_S_2f_2 import Panose + + fb, advanceWidths, nameStrings = _setupFontBuilder(True) + + pen = TTGlyphPen(None) + drawTestGlyph(pen) + glyph = pen.glyph() + glyphs = {".notdef": glyph, "A": glyph, "a": glyph, ".null": glyph} + fb.setupGlyf(glyphs) + metrics = {} + glyphTable = fb.font["glyf"] + for gn, advanceWidth in advanceWidths.items(): + metrics[gn] = (advanceWidth, glyphTable[gn].xMin) + fb.setupHorizontalMetrics(metrics) + + fb.setupHorizontalHeader(ascent=824, descent=200) + fb.setupNameTable(nameStrings) + fb.setupOS2() + fb.setupPost() + + panoseValues = { # sample value of Times New Roman from https://www.w3.org/Printing/stevahn.html + "bFamilyType": 2, + "bSerifStyle": 2, + "bWeight": 6, + "bProportion": 3, + "bContrast": 5, + "bStrokeVariation": 4, + "bArmStyle": 5, + "bLetterForm": 2, + "bMidline": 3, + "bXHeight": 4, + } + + for name in panoseValues: + assert getattr(fb.font["OS/2"].panose, name) == 0 + + fb.setupOS2(panoseValues) + fb.setupPost() + + for name, value in panoseValues.items(): + assert getattr(fb.font["OS/2"].panose, name) == value + + From e97aab69eb114cf020de22a5e1cd00bb9fff2e44 Mon Sep 17 00:00:00 2001 From: NightFurySL2001 <33471049+NightFurySL2001@users.noreply.github.com> Date: Sat, 8 Jul 2023 00:10:12 +0800 Subject: [PATCH 5/5] Fix test bug --- Tests/fontBuilder/fontBuilder_test.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Tests/fontBuilder/fontBuilder_test.py b/Tests/fontBuilder/fontBuilder_test.py index 53fefa927..c831d02e8 100644 --- a/Tests/fontBuilder/fontBuilder_test.py +++ b/Tests/fontBuilder/fontBuilder_test.py @@ -422,7 +422,7 @@ def test_unicodeVariationSequences(tmpdir): _verifyOutput(outPath, tables=["cmap"]) -def test_setupPanose(is_ttf, keep_glyph_names, make_cff2, post_format): +def test_setupPanose(): from fontTools.ttLib.tables.O_S_2f_2 import Panose fb, advanceWidths, nameStrings = _setupFontBuilder(True) @@ -443,7 +443,7 @@ def test_setupPanose(is_ttf, keep_glyph_names, make_cff2, post_format): fb.setupOS2() fb.setupPost() - panoseValues = { # sample value of Times New Roman from https://www.w3.org/Printing/stevahn.html + panoseValues = { # sample value of Times New Roman from https://www.w3.org/Printing/stevahn.html "bFamilyType": 2, "bSerifStyle": 2, "bWeight": 6, @@ -455,14 +455,13 @@ def test_setupPanose(is_ttf, keep_glyph_names, make_cff2, post_format): "bMidline": 3, "bXHeight": 4, } + panoseObj = Panose(**panoseValues) for name in panoseValues: assert getattr(fb.font["OS/2"].panose, name) == 0 - fb.setupOS2(panoseValues) + fb.setupOS2(panose=panoseObj) fb.setupPost() for name, value in panoseValues.items(): assert getattr(fb.font["OS/2"].panose, name) == value - -