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