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.3, the font /Library/Fonts/AppleGothic.ttf has a ‘gcid’
8# table with a similar structure as this test data, just more CIDs.
9GCID_DATA = deHexStr(
10    "0000 0000 "          #   0: Format=0, Flags=0
11    "0000 0098 "          #   4: Size=152
12    "0000 "               #   8: Registry=0
13    "41 64 6F 62 65 "     #  10: RegistryName="Adobe"
14    + ("00" * 59) +       #  15: <padding>
15    "0003 "               #  74: Order=3
16    "4B 6F 72 65 61 31 "  #  76: Order="Korea1"
17    + ("00" * 58) +       #  82: <padding>
18    "0001 "               # 140: SupplementVersion
19    "0004 "               # 142: Count
20    "1234 "               # 144: CIDs[0/.notdef]=4660
21    "FFFF "               # 146: CIDs[1/A]=None
22    "0007 "               # 148: CIDs[2/B]=7
23    "DEF0 "               # 150: CIDs[3/C]=57072
24)                         # 152: <end>
25assert len(GCID_DATA) == 152, len(GCID_DATA)
26
27
28GCID_XML = [
29   '<GlyphCIDMapping Format="0">',
30   '  <DataFormat value="0"/>',
31   '  <!-- StructLength=152 -->',
32   '  <Registry value="0"/>',
33   '  <RegistryName value="Adobe"/>',
34   '  <Order value="3"/>',
35   '  <OrderName value="Korea1"/>',
36   '  <SupplementVersion value="1"/>',
37   '  <Mapping>',
38   '    <CID glyph=".notdef" value="4660"/>',
39   '    <CID glyph="B" value="7"/>',
40   '    <CID glyph="C" value="57072"/>',
41   '  </Mapping>',
42   '</GlyphCIDMapping>',
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('gcid')
54        table.decompile(GCID_DATA, self.font)
55        self.assertEqual(getXML(table.toXML, self.font), GCID_XML)
56
57    def testCompileFromXML(self):
58        table = newTable('gcid')
59        for name, attrs, content in parseXML(GCID_XML):
60            table.fromXML(name, attrs, content, font=self.font)
61        self.assertEqual(hexStr(table.compile(self.font)),
62                         hexStr(GCID_DATA))
63
64
65if __name__ == '__main__':
66    import sys
67    sys.exit(unittest.main())
68