fonttools/Tests/ttLib/tables/_m_e_t_a_test.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

112 lines
3.8 KiB
Python
Raw Permalink Normal View History

from fontTools.misc.testTools import parseXML
from fontTools.misc.textTools import deHexStr
from fontTools.misc.xmlWriter import XMLWriter
from fontTools.ttLib import TTLibError
from fontTools.ttLib.tables._m_e_t_a import table__m_e_t_a
from io import BytesIO
import unittest
# From a real font on MacOS X, but substituted 'bild' tag by 'TEST',
# and shortened the payload.
META_DATA = deHexStr(
"00 00 00 01 00 00 00 00 00 00 00 1C 00 00 00 01 "
"54 45 53 54 00 00 00 1C 00 00 00 04 CA FE BE EF"
)
2015-09-24 17:01:36 +01:00
# The 'dlng' and 'slng' tag with text data containing "augmented" BCP 47
# comma-separated or comma-space-separated tags. These should be UTF-8 encoded
# text.
2015-09-24 11:00:42 +01:00
META_DATA_TEXT = deHexStr(
2015-09-24 17:01:36 +01:00
"00 00 00 01 00 00 00 00 00 00 00 28 00 00 00 02 "
"64 6C 6E 67 00 00 00 28 00 00 00 0E 73 6C 6E 67 "
"00 00 00 36 00 00 00 0E 4C 61 74 6E 2C 47 72 65 "
"6B 2C 43 79 72 6C 4C 61 74 6E 2C 47 72 65 6B 2C "
"43 79 72 6C"
)
2022-12-13 11:26:36 +00:00
class MetaTableTest(unittest.TestCase):
def test_decompile(self):
table = table__m_e_t_a()
table.decompile(META_DATA, ttFont={"meta": table})
self.assertEqual({"TEST": b"\xCA\xFE\xBE\xEF"}, table.data)
def test_compile(self):
table = table__m_e_t_a()
table.data["TEST"] = b"\xCA\xFE\xBE\xEF"
self.assertEqual(META_DATA, table.compile(ttFont={"meta": table}))
2015-09-24 11:00:42 +01:00
def test_decompile_text(self):
table = table__m_e_t_a()
table.decompile(META_DATA_TEXT, ttFont={"meta": table})
2015-09-24 17:01:36 +01:00
self.assertEqual(
{"dlng": "Latn,Grek,Cyrl", "slng": "Latn,Grek,Cyrl"}, table.data
)
2015-09-24 11:00:42 +01:00
def test_compile_text(self):
table = table__m_e_t_a()
2015-09-24 15:44:53 +01:00
table.data["dlng"] = "Latn,Grek,Cyrl"
2015-09-24 17:01:36 +01:00
table.data["slng"] = "Latn,Grek,Cyrl"
2015-09-24 11:00:42 +01:00
self.assertEqual(META_DATA_TEXT, table.compile(ttFont={"meta": table}))
def test_toXML(self):
table = table__m_e_t_a()
table.data["TEST"] = b"\xCA\xFE\xBE\xEF"
2015-08-07 16:16:49 +01:00
writer = XMLWriter(BytesIO())
table.toXML(writer, {"meta": table})
xml = writer.file.getvalue().decode("utf-8")
self.assertEqual(
['<hexdata tag="TEST">', "cafebeef", "</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(
'<hexdata tag="TEST">' " cafebeef" "</hexdata>"
):
table.fromXML(name, attrs, content, ttFont=None)
self.assertEqual({"TEST": b"\xCA\xFE\xBE\xEF"}, table.data)
2015-09-24 11:00:42 +01:00
def test_toXML_text(self):
table = table__m_e_t_a()
2015-09-24 15:44:53 +01:00
table.data["dlng"] = "Latn,Grek,Cyrl"
2015-09-24 11:00:42 +01:00
writer = XMLWriter(BytesIO())
table.toXML(writer, {"meta": table})
xml = writer.file.getvalue().decode("utf-8")
self.assertEqual(
['<text tag="dlng">', "Latn,Grek,Cyrl", "</text>"],
[line.strip() for line in xml.splitlines()][1:],
)
def test_fromXML_text(self):
table = table__m_e_t_a()
for name, attrs, content in parseXML(
'<text tag="dlng">' " Latn,Grek,Cyrl" "</text>"
):
table.fromXML(name, attrs, content, ttFont=None)
2015-09-24 15:44:53 +01:00
self.assertEqual({"dlng": "Latn,Grek,Cyrl"}, table.data)
2015-09-24 11:00:42 +01:00
if __name__ == "__main__":
import sys
2022-12-13 11:26:36 +00:00
sys.exit(unittest.main())