testTools: factor our method to strip variable items from ttx dumps

it was scattered here and there, so define it once and reuse everywhere needed
This commit is contained in:
Cosimo Lupo 2022-04-05 18:34:30 +01:00
parent 9ffb2e0f75
commit 7ee2c9d891
10 changed files with 43 additions and 35 deletions

View File

@ -3,6 +3,7 @@
from collections.abc import Iterable from collections.abc import Iterable
from io import BytesIO from io import BytesIO
import os import os
import re
import shutil import shutil
import sys import sys
import tempfile import tempfile
@ -133,6 +134,31 @@ def getXML(func, ttFont=None):
return xml.splitlines() return xml.splitlines()
def stripVariableItemsFromTTX(
string: str,
ttLibVersion: bool = True,
checkSumAdjustment: bool = True,
modified: bool = True,
created: bool = True,
sfntVersion: bool = False, # opt-in only
) -> str:
"""Strip stuff like ttLibVersion, checksums, timestamps, etc. from TTX dumps."""
# ttlib changes with the fontTools version
if ttLibVersion:
string = re.sub(' ttLibVersion="[^"]+"', "", string)
# sometimes (e.g. some subsetter tests) we don't care whether it's OTF or TTF
if sfntVersion:
string = re.sub(' sfntVersion="[^"]+"', "", string)
# head table checksum and creation and mod date changes with each save.
if checkSumAdjustment:
string = re.sub('<checkSumAdjustment value="[^"]+"/>', "", string)
if modified:
string = re.sub('<modified value="[^"]+"/>', "", string)
if created:
string = re.sub('<created value="[^"]+"/>', "", string)
return string
class MockFont(object): class MockFont(object):
"""A font-like object that automatically adds any looked up glyphname """A font-like object that automatically adds any looked up glyphname
to its glyphOrder.""" to its glyphOrder."""

View File

@ -1,13 +1,13 @@
import os import os
import pytest import pytest
import re
from fontTools.ttLib import TTFont from fontTools.ttLib import TTFont
from fontTools.pens.ttGlyphPen import TTGlyphPen from fontTools.pens.ttGlyphPen import TTGlyphPen
from fontTools.pens.t2CharStringPen import T2CharStringPen from fontTools.pens.t2CharStringPen import T2CharStringPen
from fontTools.fontBuilder import FontBuilder from fontTools.fontBuilder import FontBuilder
from fontTools.ttLib.tables.TupleVariation import TupleVariation from fontTools.ttLib.tables.TupleVariation import TupleVariation
from fontTools.misc.psCharStrings import T2CharString from fontTools.misc.psCharStrings import T2CharString
from fontTools.misc.testTools import stripVariableItemsFromTTX
def getTestData(fileName, mode="r"): def getTestData(fileName, mode="r"):
@ -16,16 +16,6 @@ def getTestData(fileName, mode="r"):
return f.read() return f.read()
def strip_VariableItems(string):
# ttlib changes with the fontTools version
string = re.sub(' ttLibVersion=".*"', '', string)
# head table checksum and creation and mod date changes with each save.
string = re.sub('<checkSumAdjustment value="[^"]+"/>', '', string)
string = re.sub('<modified value="[^"]+"/>', '', string)
string = re.sub('<created value="[^"]+"/>', '', string)
return string
def drawTestGlyph(pen): def drawTestGlyph(pen):
pen.moveTo((100, 100)) pen.moveTo((100, 100))
pen.lineTo((100, 1000)) pen.lineTo((100, 1000))
@ -91,8 +81,8 @@ def _verifyOutput(outPath, tables=None):
f = TTFont(outPath) f = TTFont(outPath)
f.saveXML(outPath + ".ttx", tables=tables) f.saveXML(outPath + ".ttx", tables=tables)
with open(outPath + ".ttx") as f: with open(outPath + ".ttx") as f:
testData = strip_VariableItems(f.read()) testData = stripVariableItemsFromTTX(f.read())
refData = strip_VariableItems(getTestData(os.path.basename(outPath) + ".ttx")) refData = stripVariableItemsFromTTX(getTestData(os.path.basename(outPath) + ".ttx"))
assert refData == testData assert refData == testData

View File

@ -12,5 +12,5 @@
</Carets> </Carets>
</LigatureCarets> </LigatureCarets>
</lcar> </lcar>
</ttFont> </ttFont>

View File

@ -12,5 +12,5 @@
</Carets> </Carets>
</LigatureCarets> </LigatureCarets>
</lcar> </lcar>
</ttFont> </ttFont>

View File

@ -14,5 +14,5 @@
</OpticalBoundsDeltas> </OpticalBoundsDeltas>
</OpticalBounds> </OpticalBounds>
</opbd> </opbd>
</ttFont> </ttFont>

View File

@ -14,5 +14,5 @@
</OpticalBoundsPoints> </OpticalBoundsPoints>
</OpticalBounds> </OpticalBounds>
</opbd> </opbd>
</ttFont> </ttFont>

View File

@ -7,5 +7,5 @@
<DefaultProperties value="3"/> <DefaultProperties value="3"/>
</GlyphProperties> </GlyphProperties>
</prop> </prop>
</ttFont> </ttFont>

View File

@ -10,5 +10,5 @@
</Properties> </Properties>
</GlyphProperties> </GlyphProperties>
</prop> </prop>
</ttFont> </ttFont>

View File

@ -1,5 +1,5 @@
import io import io
from fontTools.misc.testTools import getXML from fontTools.misc.testTools import getXML, stripVariableItemsFromTTX
from fontTools.misc.textTools import tobytes, tostr from fontTools.misc.textTools import tobytes, tostr
from fontTools import subset from fontTools import subset
from fontTools.fontBuilder import FontBuilder from fontTools.fontBuilder import FontBuilder
@ -48,15 +48,10 @@ class SubsetTest(unittest.TestCase):
"tmp%d%s" % (self.num_tempfiles, suffix)) "tmp%d%s" % (self.num_tempfiles, suffix))
def read_ttx(self, path): def read_ttx(self, path):
lines = [] with open(path, "r", encoding="utf-8") as f:
with open(path, "r", encoding="utf-8") as ttx: ttx = f.read()
for line in ttx.readlines(): # don't care whether TTF or OTF, thus strip sfntVersion as well
# Elide ttFont attributes because ttLibVersion may change. return stripVariableItemsFromTTX(ttx, sfntVersion=True).splitlines(True)
if line.startswith("<ttFont "):
lines.append("<ttFont>\n")
else:
lines.append(line.rstrip() + "\n")
return lines
def expect_ttx(self, font, expected_ttx, tables=None): def expect_ttx(self, font, expected_ttx, tables=None):
path = self.temp_path(suffix=".ttx") path = self.temp_path(suffix=".ttx")

View File

@ -1,4 +1,5 @@
from fontTools.misc.fixedTools import floatToFixedToFloat from fontTools.misc.fixedTools import floatToFixedToFloat
from fontTools.misc.testTools import stripVariableItemsFromTTX
from fontTools.misc.textTools import Tag from fontTools.misc.textTools import Tag
from fontTools import ttLib from fontTools import ttLib
from fontTools import designspaceLib from fontTools import designspaceLib
@ -1386,10 +1387,6 @@ def test_setMacOverlapFlags():
assert b.components[0].flags & flagOverlapCompound != 0 assert b.components[0].flags & flagOverlapCompound != 0
def _strip_ttLibVersion(string):
return re.sub(' ttLibVersion=".*"', "", string)
@pytest.fixture @pytest.fixture
def varfont2(): def varfont2():
f = ttLib.TTFont(recalcTimestamp=False) f = ttLib.TTFont(recalcTimestamp=False)
@ -1412,7 +1409,7 @@ def _dump_ttx(ttFont):
ttFont2 = ttLib.TTFont(tmp, recalcBBoxes=False, recalcTimestamp=False) ttFont2 = ttLib.TTFont(tmp, recalcBBoxes=False, recalcTimestamp=False)
s = StringIO() s = StringIO()
ttFont2.saveXML(s) ttFont2.saveXML(s)
return _strip_ttLibVersion(s.getvalue()) return stripVariableItemsFromTTX(s.getvalue())
def _get_expected_instance_ttx( def _get_expected_instance_ttx(
@ -1428,7 +1425,7 @@ def _get_expected_instance_ttx(
"r", "r",
encoding="utf-8", encoding="utf-8",
) as fp: ) as fp:
return _strip_ttLibVersion(fp.read()) return stripVariableItemsFromTTX(fp.read())
class InstantiateVariableFontTest(object): class InstantiateVariableFontTest(object):