aboutsummaryrefslogtreecommitdiff
path: root/Tests/ttLib/tables/_m_e_t_a_test.py
blob: 3a4f2f5b2ac70b5c9abce0f082d7cde0d7615d54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from fontTools.misc.py23 import *
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
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")

# 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.
META_DATA_TEXT = deHexStr(
    "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")

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}))

    def test_decompile_text(self):
        table = table__m_e_t_a()
        table.decompile(META_DATA_TEXT, ttFont={"meta": table})
        self.assertEqual({"dlng": u"Latn,Grek,Cyrl",
                          "slng": u"Latn,Grek,Cyrl"}, table.data)

    def test_compile_text(self):
        table = table__m_e_t_a()
        table.data["dlng"] = u"Latn,Grek,Cyrl"
        table.data["slng"] = u"Latn,Grek,Cyrl"
        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"
        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)

    def test_toXML_text(self):
        table = table__m_e_t_a()
        table.data["dlng"] = u"Latn,Grek,Cyrl"
        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)
        self.assertEqual({"dlng": u"Latn,Grek,Cyrl"}, table.data)


if __name__ == "__main__":
    import sys
    sys.exit(unittest.main())