From b01ea60e9ecde13dcc0b7d5823a43553de4b5401 Mon Sep 17 00:00:00 2001 From: Just van Rossum Date: Sat, 24 Jul 2021 11:12:37 +0200 Subject: [PATCH] [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 --- Lib/fontTools/misc/xmlReader.py | 11 ++++++----- Tests/ttLib/ttFont_test.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/Lib/fontTools/misc/xmlReader.py b/Lib/fontTools/misc/xmlReader.py index b2707e993..6ec50de46 100644 --- a/Lib/fontTools/misc/xmlReader.py +++ b/Lib/fontTools/misc/xmlReader.py @@ -93,11 +93,12 @@ class XMLReader(object): if not stackSize: if name != "ttFont": raise TTXParseError("illegal root tag: %s" % name) - sfntVersion = attrs.get("sfntVersion") - if sfntVersion is not None: - if len(sfntVersion) != 4: - sfntVersion = safeEval('"' + sfntVersion + '"') - self.ttFont.sfntVersion = sfntVersion + if self.ttFont.reader is None and not self.ttFont.tables: + sfntVersion = attrs.get("sfntVersion") + if sfntVersion is not None: + if len(sfntVersion) != 4: + sfntVersion = safeEval('"' + sfntVersion + '"') + self.ttFont.sfntVersion = sfntVersion self.contentStack.append([]) elif stackSize == 1: if subFile is not None: diff --git a/Tests/ttLib/ttFont_test.py b/Tests/ttLib/ttFont_test.py index 47cedeb73..33dcc090e 100644 --- a/Tests/ttLib/ttFont_test.py +++ b/Tests/ttLib/ttFont_test.py @@ -46,3 +46,35 @@ def test_registerCustomTableClassStandardName(): assert font[TABLETAG].compile(font) == b"\x04\x05\x06" finally: unregisterCustomTableClass(TABLETAG) + + +ttxTTF = r""" + + + + + +""" + + +ttxOTF = """ + + + + + +""" + + +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"