• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from fontTools.misc.testTools import parseXML
2from fontTools.misc.textTools import deHexStr
3from fontTools.misc.xmlWriter import XMLWriter
4from fontTools.ttLib import TTLibError
5from fontTools.ttLib.tables._m_e_t_a import table__m_e_t_a
6from io import BytesIO
7import unittest
8
9
10# From a real font on MacOS X, but substituted 'bild' tag by 'TEST',
11# and shortened the payload.
12META_DATA = deHexStr(
13    "00 00 00 01 00 00 00 00 00 00 00 1C 00 00 00 01 "
14    "54 45 53 54 00 00 00 1C 00 00 00 04 CA FE BE EF")
15
16# The 'dlng' and 'slng' tag with text data containing "augmented" BCP 47
17# comma-separated or comma-space-separated tags. These should be UTF-8 encoded
18# text.
19META_DATA_TEXT = deHexStr(
20    "00 00 00 01 00 00 00 00 00 00 00 28 00 00 00 02 "
21    "64 6C 6E 67 00 00 00 28 00 00 00 0E 73 6C 6E 67 "
22    "00 00 00 36 00 00 00 0E 4C 61 74 6E 2C 47 72 65 "
23    "6B 2C 43 79 72 6C 4C 61 74 6E 2C 47 72 65 6B 2C "
24    "43 79 72 6C")
25
26class MetaTableTest(unittest.TestCase):
27    def test_decompile(self):
28        table = table__m_e_t_a()
29        table.decompile(META_DATA, ttFont={"meta": table})
30        self.assertEqual({"TEST": b"\xCA\xFE\xBE\xEF"}, table.data)
31
32    def test_compile(self):
33        table = table__m_e_t_a()
34        table.data["TEST"] = b"\xCA\xFE\xBE\xEF"
35        self.assertEqual(META_DATA, table.compile(ttFont={"meta": table}))
36
37    def test_decompile_text(self):
38        table = table__m_e_t_a()
39        table.decompile(META_DATA_TEXT, ttFont={"meta": table})
40        self.assertEqual({"dlng": u"Latn,Grek,Cyrl",
41                          "slng": u"Latn,Grek,Cyrl"}, table.data)
42
43    def test_compile_text(self):
44        table = table__m_e_t_a()
45        table.data["dlng"] = u"Latn,Grek,Cyrl"
46        table.data["slng"] = u"Latn,Grek,Cyrl"
47        self.assertEqual(META_DATA_TEXT, table.compile(ttFont={"meta": table}))
48
49    def test_toXML(self):
50        table = table__m_e_t_a()
51        table.data["TEST"] = b"\xCA\xFE\xBE\xEF"
52        writer = XMLWriter(BytesIO())
53        table.toXML(writer, {"meta": table})
54        xml = writer.file.getvalue().decode("utf-8")
55        self.assertEqual([
56            '<hexdata tag="TEST">',
57                'cafebeef',
58            '</hexdata>'
59        ], [line.strip() for line in xml.splitlines()][1:])
60
61    def test_toXML_ascii_data(self):
62        table = table__m_e_t_a()
63        table.data["TEST"] = b"Hello!"
64        writer = XMLWriter(BytesIO())
65        table.toXML(writer, {"meta": table})
66        xml = writer.file.getvalue().decode("utf-8")
67        self.assertEqual([
68            '<hexdata tag="TEST">',
69                '<!-- ascii: Hello! -->',
70                '48656c6c 6f21',
71            '</hexdata>'
72        ], [line.strip() for line in xml.splitlines()][1:])
73
74    def test_fromXML(self):
75        table = table__m_e_t_a()
76        for name, attrs, content in parseXML(
77                '<hexdata tag="TEST">'
78                '    cafebeef'
79                '</hexdata>'):
80            table.fromXML(name, attrs, content, ttFont=None)
81        self.assertEqual({"TEST": b"\xCA\xFE\xBE\xEF"}, table.data)
82
83    def test_toXML_text(self):
84        table = table__m_e_t_a()
85        table.data["dlng"] = u"Latn,Grek,Cyrl"
86        writer = XMLWriter(BytesIO())
87        table.toXML(writer, {"meta": table})
88        xml = writer.file.getvalue().decode("utf-8")
89        self.assertEqual([
90            '<text tag="dlng">',
91            'Latn,Grek,Cyrl',
92            '</text>'
93        ], [line.strip() for line in xml.splitlines()][1:])
94
95    def test_fromXML_text(self):
96        table = table__m_e_t_a()
97        for name, attrs, content in parseXML(
98                '<text tag="dlng">'
99                '    Latn,Grek,Cyrl'
100                '</text>'):
101            table.fromXML(name, attrs, content, ttFont=None)
102        self.assertEqual({"dlng": u"Latn,Grek,Cyrl"}, table.data)
103
104
105if __name__ == "__main__":
106    import sys
107    sys.exit(unittest.main())
108