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:
parent
9ffb2e0f75
commit
7ee2c9d891
@ -3,6 +3,7 @@
|
||||
from collections.abc import Iterable
|
||||
from io import BytesIO
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import sys
|
||||
import tempfile
|
||||
@ -133,6 +134,31 @@ def getXML(func, ttFont=None):
|
||||
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):
|
||||
"""A font-like object that automatically adds any looked up glyphname
|
||||
to its glyphOrder."""
|
||||
|
@ -1,13 +1,13 @@
|
||||
|
||||
import os
|
||||
import pytest
|
||||
import re
|
||||
from fontTools.ttLib import TTFont
|
||||
from fontTools.pens.ttGlyphPen import TTGlyphPen
|
||||
from fontTools.pens.t2CharStringPen import T2CharStringPen
|
||||
from fontTools.fontBuilder import FontBuilder
|
||||
from fontTools.ttLib.tables.TupleVariation import TupleVariation
|
||||
from fontTools.misc.psCharStrings import T2CharString
|
||||
from fontTools.misc.testTools import stripVariableItemsFromTTX
|
||||
|
||||
|
||||
def getTestData(fileName, mode="r"):
|
||||
@ -16,16 +16,6 @@ def getTestData(fileName, mode="r"):
|
||||
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):
|
||||
pen.moveTo((100, 100))
|
||||
pen.lineTo((100, 1000))
|
||||
@ -91,8 +81,8 @@ def _verifyOutput(outPath, tables=None):
|
||||
f = TTFont(outPath)
|
||||
f.saveXML(outPath + ".ttx", tables=tables)
|
||||
with open(outPath + ".ttx") as f:
|
||||
testData = strip_VariableItems(f.read())
|
||||
refData = strip_VariableItems(getTestData(os.path.basename(outPath) + ".ttx"))
|
||||
testData = stripVariableItemsFromTTX(f.read())
|
||||
refData = stripVariableItemsFromTTX(getTestData(os.path.basename(outPath) + ".ttx"))
|
||||
assert refData == testData
|
||||
|
||||
|
||||
|
@ -12,5 +12,5 @@
|
||||
</Carets>
|
||||
</LigatureCarets>
|
||||
</lcar>
|
||||
|
||||
|
||||
</ttFont>
|
||||
|
@ -12,5 +12,5 @@
|
||||
</Carets>
|
||||
</LigatureCarets>
|
||||
</lcar>
|
||||
|
||||
|
||||
</ttFont>
|
||||
|
@ -14,5 +14,5 @@
|
||||
</OpticalBoundsDeltas>
|
||||
</OpticalBounds>
|
||||
</opbd>
|
||||
|
||||
|
||||
</ttFont>
|
||||
|
@ -14,5 +14,5 @@
|
||||
</OpticalBoundsPoints>
|
||||
</OpticalBounds>
|
||||
</opbd>
|
||||
|
||||
|
||||
</ttFont>
|
||||
|
@ -7,5 +7,5 @@
|
||||
<DefaultProperties value="3"/>
|
||||
</GlyphProperties>
|
||||
</prop>
|
||||
|
||||
|
||||
</ttFont>
|
||||
|
@ -10,5 +10,5 @@
|
||||
</Properties>
|
||||
</GlyphProperties>
|
||||
</prop>
|
||||
|
||||
|
||||
</ttFont>
|
||||
|
@ -1,5 +1,5 @@
|
||||
import io
|
||||
from fontTools.misc.testTools import getXML
|
||||
from fontTools.misc.testTools import getXML, stripVariableItemsFromTTX
|
||||
from fontTools.misc.textTools import tobytes, tostr
|
||||
from fontTools import subset
|
||||
from fontTools.fontBuilder import FontBuilder
|
||||
@ -48,15 +48,10 @@ class SubsetTest(unittest.TestCase):
|
||||
"tmp%d%s" % (self.num_tempfiles, suffix))
|
||||
|
||||
def read_ttx(self, path):
|
||||
lines = []
|
||||
with open(path, "r", encoding="utf-8") as ttx:
|
||||
for line in ttx.readlines():
|
||||
# Elide ttFont attributes because ttLibVersion may change.
|
||||
if line.startswith("<ttFont "):
|
||||
lines.append("<ttFont>\n")
|
||||
else:
|
||||
lines.append(line.rstrip() + "\n")
|
||||
return lines
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
ttx = f.read()
|
||||
# don't care whether TTF or OTF, thus strip sfntVersion as well
|
||||
return stripVariableItemsFromTTX(ttx, sfntVersion=True).splitlines(True)
|
||||
|
||||
def expect_ttx(self, font, expected_ttx, tables=None):
|
||||
path = self.temp_path(suffix=".ttx")
|
||||
|
@ -1,4 +1,5 @@
|
||||
from fontTools.misc.fixedTools import floatToFixedToFloat
|
||||
from fontTools.misc.testTools import stripVariableItemsFromTTX
|
||||
from fontTools.misc.textTools import Tag
|
||||
from fontTools import ttLib
|
||||
from fontTools import designspaceLib
|
||||
@ -1386,10 +1387,6 @@ def test_setMacOverlapFlags():
|
||||
assert b.components[0].flags & flagOverlapCompound != 0
|
||||
|
||||
|
||||
def _strip_ttLibVersion(string):
|
||||
return re.sub(' ttLibVersion=".*"', "", string)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def varfont2():
|
||||
f = ttLib.TTFont(recalcTimestamp=False)
|
||||
@ -1412,7 +1409,7 @@ def _dump_ttx(ttFont):
|
||||
ttFont2 = ttLib.TTFont(tmp, recalcBBoxes=False, recalcTimestamp=False)
|
||||
s = StringIO()
|
||||
ttFont2.saveXML(s)
|
||||
return _strip_ttLibVersion(s.getvalue())
|
||||
return stripVariableItemsFromTTX(s.getvalue())
|
||||
|
||||
|
||||
def _get_expected_instance_ttx(
|
||||
@ -1428,7 +1425,7 @@ def _get_expected_instance_ttx(
|
||||
"r",
|
||||
encoding="utf-8",
|
||||
) as fp:
|
||||
return _strip_ttLibVersion(fp.read())
|
||||
return stripVariableItemsFromTTX(fp.read())
|
||||
|
||||
|
||||
class InstantiateVariableFontTest(object):
|
||||
|
Loading…
x
Reference in New Issue
Block a user