Skip to content

Commit

Permalink
Merge pull request #1938 from justvanrossum/meta-comment
Browse files Browse the repository at this point in the history
[ttLib.table._m_e_t_a] if data happens to be ascii, emit comment in TTX
  • Loading branch information
justvanrossum committed May 11, 2020
2 parents b81d139 + 0dc0222 commit a53bb37
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Lib/fontTools/ttLib/tables/_m_e_t_a.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ def toXML(self, writer, ttFont):
else:
writer.begintag("hexdata", tag=tag)
writer.newline()
writer.dumphex(self.data[tag])
data = self.data[tag]
if min(data) >= 0x20 and max(data) <= 0x7E:
writer.comment("ascii: " + data.decode("ascii"))
writer.newline()
writer.dumphex(data)
writer.endtag("hexdata")
writer.newline()

Expand Down
13 changes: 13 additions & 0 deletions Tests/ttLib/tables/_m_e_t_a_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ def test_toXML(self):
'</hexdata>'
], [line.strip() for line in xml.splitlines()][1:])

def test_toXML_ascii_data(self):
table = table__m_e_t_a()
table.data["TEST"] = b"Hello!"
writer = XMLWriter(BytesIO())
table.toXML(writer, {"meta": table})
xml = writer.file.getvalue().decode("utf-8")
self.assertEqual([
'<hexdata tag="TEST">',
'<!-- ascii: Hello! -->',
'48656c6c 6f21',
'</hexdata>'
], [line.strip() for line in xml.splitlines()][1:])

def test_fromXML(self):
table = table__m_e_t_a()
for name, attrs, content in parseXML(
Expand Down

0 comments on commit a53bb37

Please sign in to comment.