From d11a67010ffbcfcdc09af1112a7b2202c02ec414 Mon Sep 17 00:00:00 2001 From: Ben Kiel Date: Thu, 25 May 2023 10:30:28 -0500 Subject: [PATCH 1/4] privData needs to pad with bytes not str --- Lib/fontTools/ttLib/sfnt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/fontTools/ttLib/sfnt.py b/Lib/fontTools/ttLib/sfnt.py index 0923ffd39..354fb85ea 100644 --- a/Lib/fontTools/ttLib/sfnt.py +++ b/Lib/fontTools/ttLib/sfnt.py @@ -345,7 +345,7 @@ class SFNTWriter(object): self.file.seek(0, 2) off = self.file.tell() paddedOff = (off + 3) & ~3 - self.file.write("\0" * (paddedOff - off)) + self.file.write(b"\0" * (paddedOff - off)) self.privOffset = self.file.tell() self.privLength = len(data.privData) self.file.write(data.privData) From bdc3afc46850be02855a61ff7f285b2edbb0db0e Mon Sep 17 00:00:00 2001 From: Ben Kiel Date: Sun, 28 May 2023 08:12:11 -0500 Subject: [PATCH 2/4] Add in a test for writing private data --- Tests/ttLib/sfnt_test.py | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/Tests/ttLib/sfnt_test.py b/Tests/ttLib/sfnt_test.py index 9f8174446..9a4310ce5 100644 --- a/Tests/ttLib/sfnt_test.py +++ b/Tests/ttLib/sfnt_test.py @@ -1,9 +1,24 @@ import io import copy import pickle -from fontTools.ttLib.sfnt import calcChecksum, SFNTReader +import tempfile +from fontTools.ttLib import TTFont +from fontTools.ttLib.sfnt import calcChecksum, SFNTReader, WOFFFlavorData +from pathlib import Path import pytest +TEST_DATA = Path(__file__).parent / "data" + + +@pytest.fixture +def ttfont_path(): + font = TTFont() + font.importXML(TEST_DATA / "TestTTF-Regular.ttx") + with tempfile.NamedTemporaryFile(suffix=".ttf", delete=False) as fp: + font_path = Path(fp.name) + font.save(font_path) + yield font_path + font_path.unlink() def test_calcChecksum(): assert calcChecksum(b"abcd") == 1633837924 @@ -57,3 +72,21 @@ class SFNTReaderTest: if k == "file": continue assert getattr(reader2, k) == v + +def test_ttLib_sfnt_write_privData(tmp_path, ttfont_path): + output_path = tmp_path / "TestTTF-Regular.woff" + font = TTFont(ttfont_path) + + privData = "Private Eyes".encode() + + data = WOFFFlavorData() + head = font['head'] + data.majorVersion, data.minorVersion =map(int, format(head.fontRevision, '.3f').split('.')) + + data.privData = privData + font.flavor = "woff" + font.flavorData = data + font.save(output_path) + + assert output_path.exists() + assert TTFont(output_path).flavorData.privData == privData \ No newline at end of file From c30f4e5074c3b48c9273abae07a9b317df260d47 Mon Sep 17 00:00:00 2001 From: Ben Kiel Date: Sun, 28 May 2023 08:19:54 -0500 Subject: [PATCH 3/4] fix lint error --- Tests/ttLib/sfnt_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/ttLib/sfnt_test.py b/Tests/ttLib/sfnt_test.py index 9a4310ce5..7eb4dbe3c 100644 --- a/Tests/ttLib/sfnt_test.py +++ b/Tests/ttLib/sfnt_test.py @@ -89,4 +89,4 @@ def test_ttLib_sfnt_write_privData(tmp_path, ttfont_path): font.save(output_path) assert output_path.exists() - assert TTFont(output_path).flavorData.privData == privData \ No newline at end of file + assert TTFont(output_path).flavorData.privData == privData From fb74c16d47c26dcd1e9ccce3dc483d7a4ba83a0d Mon Sep 17 00:00:00 2001 From: Ben Kiel Date: Sun, 28 May 2023 08:28:28 -0500 Subject: [PATCH 4/4] Ran black on code --- Tests/ttLib/sfnt_test.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/Tests/ttLib/sfnt_test.py b/Tests/ttLib/sfnt_test.py index 7eb4dbe3c..7832a2ff2 100644 --- a/Tests/ttLib/sfnt_test.py +++ b/Tests/ttLib/sfnt_test.py @@ -20,6 +20,7 @@ def ttfont_path(): yield font_path font_path.unlink() + def test_calcChecksum(): assert calcChecksum(b"abcd") == 1633837924 assert calcChecksum(b"abcdxyz") == 3655064932 @@ -73,20 +74,23 @@ class SFNTReaderTest: continue assert getattr(reader2, k) == v + def test_ttLib_sfnt_write_privData(tmp_path, ttfont_path): output_path = tmp_path / "TestTTF-Regular.woff" - font = TTFont(ttfont_path) - + font = TTFont(ttfont_path) + privData = "Private Eyes".encode() - + data = WOFFFlavorData() - head = font['head'] - data.majorVersion, data.minorVersion =map(int, format(head.fontRevision, '.3f').split('.')) - + head = font["head"] + data.majorVersion, data.minorVersion = map( + int, format(head.fontRevision, ".3f").split(".") + ) + data.privData = privData font.flavor = "woff" font.flavorData = data font.save(output_path) - - assert output_path.exists() + + assert output_path.exists() assert TTFont(output_path).flavorData.privData == privData