1from fontTools.misc.testTools import FakeFont, getXML, parseXML
2from fontTools.misc.textTools import deHexStr, hexStr
3from fontTools.ttLib import newTable
4import unittest
5
6
7# On macOS X 10.12.6, the first font in /System/Library/Fonts/PingFang.ttc
8# has a ‘cidg’ table with a similar structure as this test data, just larger.
9CIDG_DATA = deHexStr(
10    "0000 0000 "          #   0: Format=0, Flags=0
11    "0000 0098 "          #   4: StructLength=152
12    "0000 "               #   8: Registry=0
13    "41 64 6F 62 65 "     #  10: RegistryName="Adobe"
14    + ("00" * 59) +       #  15: <padding>
15    "0002 "               #  74: Order=2
16    "43 4E 53 31 "        #  76: Order="CNS1"
17    + ("00" * 60) +       #  80: <padding>
18    "0000 "               # 140: SupplementVersion=0
19    "0004 "               # 142: Count
20    "0000 "               # 144: GlyphID[0]=.notdef
21    "FFFF "               # 146: CIDs[1]=<None>
22    "0003 "               # 148: CIDs[2]=C
23    "0001 "               # 150: CIDs[3]=A
24)                         # 152: <end>
25assert len(CIDG_DATA) == 152, len(CIDG_DATA)
26
27
28CIDG_XML = [
29   '<CIDGlyphMapping Format="0">',
30   '  <DataFormat value="0"/>',
31   '  <!-- StructLength=152 -->',
32   '  <Registry value="0"/>',
33   '  <RegistryName value="Adobe"/>',
34   '  <Order value="2"/>',
35   '  <OrderName value="CNS1"/>',
36   '  <SupplementVersion value="0"/>',
37   '  <Mapping>',
38   '    <CID cid="0" glyph=".notdef"/>',
39   '    <CID cid="2" glyph="C"/>',
40   '    <CID cid="3" glyph="A"/>',
41   '  </Mapping>',
42   '</CIDGlyphMapping>',
43]
44
45
46class GCIDTest(unittest.TestCase):
47    @classmethod
48    def setUpClass(cls):
49        cls.maxDiff = None
50        cls.font = FakeFont(['.notdef', 'A', 'B', 'C', 'D'])
51
52    def testDecompileToXML(self):
53        table = newTable('cidg')
54        table.decompile(CIDG_DATA, self.font)
55        self.assertEqual(getXML(table.toXML, self.font), CIDG_XML)
56
57    def testCompileFromXML(self):
58        table = newTable('cidg')
59        for name, attrs, content in parseXML(CIDG_XML):
60            table.fromXML(name, attrs, content, font=self.font)
61        self.assertEqual(hexStr(table.compile(self.font)),
62                         hexStr(CIDG_DATA))
63
64
65if __name__ == '__main__':
66    import sys
67    sys.exit(unittest.main())
68