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