1from __future__ import print_function, division, absolute_import, unicode_literals
2from fontTools.misc.py23 import *
3from fontTools.misc.testTools import FakeFont, getXML, parseXML
4from fontTools.misc.textTools import deHexStr, hexStr
5from fontTools.ttLib import newTable
6import unittest
7
8
9PROP_FORMAT_0_DATA = deHexStr(
10    '0001 0000 0000 '  #  0: Version=1.0, Format=0
11    '0005 '            #  6: DefaultProperties=European number terminator
12)                      #  8: <end>
13assert(len(PROP_FORMAT_0_DATA) == 8)
14
15
16PROP_FORMAT_0_XML = [
17    '<Version value="1.0"/>',
18    '<GlyphProperties Format="0">',
19    '  <DefaultProperties value="5"/>',
20    '</GlyphProperties>',
21]
22
23
24PROP_FORMAT_1_DATA = deHexStr(
25    '0003 0000 0001 '  #  0: Version=3.0, Format=1
26    '0000 '            #  6: DefaultProperties=left-to-right; non-whitespace
27    '0008 0003 0004 '  #  8: LookupFormat=8, FirstGlyph=3, GlyphCount=4
28    '000B '            # 14: Properties[C]=other neutral
29    '000A '            # 16: Properties[D]=whitespace
30    '600B '            # 18: Properties[E]=other neutral; hanging punct
31    '0005 '            # 20: Properties[F]=European number terminator
32)                      # 22: <end>
33assert(len(PROP_FORMAT_1_DATA) == 22)
34
35
36PROP_FORMAT_1_XML = [
37    '<Version value="3.0"/>',
38    '<GlyphProperties Format="1">',
39    '  <DefaultProperties value="0"/>',
40    '  <Properties>',
41    '    <Lookup glyph="C" value="11"/>',
42    '    <Lookup glyph="D" value="10"/>',
43    '    <Lookup glyph="E" value="24587"/>',
44    '    <Lookup glyph="F" value="5"/>',
45    '  </Properties>',
46    '</GlyphProperties>',
47]
48
49
50class PROPTest(unittest.TestCase):
51
52    @classmethod
53    def setUpClass(cls):
54        cls.maxDiff = None
55        cls.font = FakeFont(['.notdef', 'A', 'B', 'C', 'D', 'E', 'F', 'G'])
56
57    def test_decompile_toXML_format0(self):
58        table = newTable('prop')
59        table.decompile(PROP_FORMAT_0_DATA, self.font)
60        self.assertEqual(getXML(table.toXML), PROP_FORMAT_0_XML)
61
62    def test_compile_fromXML_format0(self):
63        table = newTable('prop')
64        for name, attrs, content in parseXML(PROP_FORMAT_0_XML):
65            table.fromXML(name, attrs, content, font=self.font)
66        self.assertEqual(hexStr(table.compile(self.font)),
67                         hexStr(PROP_FORMAT_0_DATA))
68
69    def test_decompile_toXML_format1(self):
70        table = newTable('prop')
71        table.decompile(PROP_FORMAT_1_DATA, self.font)
72        self.assertEqual(getXML(table.toXML), PROP_FORMAT_1_XML)
73
74    def test_compile_fromXML_format1(self):
75        table = newTable('prop')
76        for name, attrs, content in parseXML(PROP_FORMAT_1_XML):
77            table.fromXML(name, attrs, content, font=self.font)
78        self.assertEqual(hexStr(table.compile(self.font)),
79                         hexStr(PROP_FORMAT_1_DATA))
80
81
82if __name__ == '__main__':
83    import sys
84    sys.exit(unittest.main())
85