[ttLib] when importing XML, only set sfntVersion if the font has no reader and is empty (#2376)

* Only set sfntVersion if the font has no reader and is empty

* test that sfntVersion is only set if the TTFont instance is new/empty
This commit is contained in:
Just van Rossum 2021-07-24 11:12:37 +02:00 committed by GitHub
parent c96c3ef8fa
commit b01ea60e9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 5 deletions

View File

@ -93,6 +93,7 @@ class XMLReader(object):
if not stackSize:
if name != "ttFont":
raise TTXParseError("illegal root tag: %s" % name)
if self.ttFont.reader is None and not self.ttFont.tables:
sfntVersion = attrs.get("sfntVersion")
if sfntVersion is not None:
if len(sfntVersion) != 4:

View File

@ -46,3 +46,35 @@ def test_registerCustomTableClassStandardName():
assert font[TABLETAG].compile(font) == b"\x04\x05\x06"
finally:
unregisterCustomTableClass(TABLETAG)
ttxTTF = r"""<?xml version="1.0" encoding="UTF-8"?>
<ttFont sfntVersion="\x00\x01\x00\x00" ttLibVersion="4.9.0">
<hmtx>
<mtx name=".notdef" width="300" lsb="0"/>
</hmtx>
</ttFont>
"""
ttxOTF = """<?xml version="1.0" encoding="UTF-8"?>
<ttFont sfntVersion="OTTO" ttLibVersion="4.9.0">
<hmtx>
<mtx name=".notdef" width="300" lsb="0"/>
</hmtx>
</ttFont>
"""
def test_sfntVersionFromTTX():
# https://github.com/fonttools/fonttools/issues/2370
font = TTFont()
assert font.sfntVersion == "\x00\x01\x00\x00"
ttx = io.StringIO(ttxOTF)
# Font is "empty", TTX file will determine sfntVersion
font.importXML(ttx)
assert font.sfntVersion == "OTTO"
ttx = io.StringIO(ttxTTF)
# Font is not "empty", sfntVersion in TTX file will be ignored
font.importXML(ttx)
assert font.sfntVersion == "OTTO"