1from __future__ import print_function, division, absolute_import
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
9# Example: Format 0 Ligature Caret Table
10# https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6lcar.html
11LCAR_FORMAT_0_DATA = deHexStr(
12    '0001 0000 0000 '  #  0: Version=1.0, Format=0
13    '0006 0004 0002 '  #  6: LookupFormat=6, UnitSize=4, NUnits=2
14    '0008 0001 0000 '  # 12: SearchRange=8, EntrySelector=1, RangeShift=0
15    '0001 001E '       # 18: Glyph=1 (f_r), OffsetOfLigCaretEntry=30
16    '0003 0022 '       # 22: Glyph=3 (f_f_l), OffsetOfLigCaretEntry=34
17    'FFFF 0000 '       # 26: Glyph=<end>, OffsetOfLigCaretEntry=0
18    '0001 00DC '       # 30: DivisionPointCount=1, DivisionPoint=[220]
19    '0002 00EF 01D8 '  # 34: DivisionPointCount=2, DivisionPoint=[239, 475]
20)                      # 40: <end>
21assert(len(LCAR_FORMAT_0_DATA) == 40)
22
23
24LCAR_FORMAT_0_XML = [
25    '<Version value="0x00010000"/>',
26    '<LigatureCarets Format="0">',
27    '  <Carets>',
28    '    <Lookup glyph="f_f_l">',
29    '      <!-- DivsionPointCount=2 -->',
30    '      <DivisionPoint index="0" value="239"/>',
31    '      <DivisionPoint index="1" value="472"/>',
32    '    </Lookup>',
33    '    <Lookup glyph="f_r">',
34    '      <!-- DivsionPointCount=1 -->',
35    '      <DivisionPoint index="0" value="220"/>',
36    '    </Lookup>',
37    '  </Carets>',
38    '</LigatureCarets>',
39]
40
41
42# Example: Format 1 Ligature Caret Table
43# https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6lcar.html
44LCAR_FORMAT_1_DATA = deHexStr(
45    '0001 0000 0001 '  #  0: Version=1.0, Format=1
46    '0006 0004 0002 '  #  6: LookupFormat=6, UnitSize=4, NUnits=2
47    '0008 0001 0000 '  # 12: SearchRange=8, EntrySelector=1, RangeShift=0
48    '0001 001E '       # 18: Glyph=1 (f_r), OffsetOfLigCaretEntry=30
49    '0003 0022 '       # 22: Glyph=3 (f_f_l), OffsetOfLigCaretEntry=34
50    'FFFF 0000 '       # 26: Glyph=<end>, OffsetOfLigCaretEntry=0
51    '0001 0032 '       # 30: DivisionPointCount=1, DivisionPoint=[50]
52    '0002 0037 004B '  # 34: DivisionPointCount=2, DivisionPoint=[55, 75]
53)                      # 40: <end>
54assert(len(LCAR_FORMAT_1_DATA) == 40)
55
56
57LCAR_FORMAT_1_XML = [
58    '<Version value="0x00010000"/>',
59    '<LigatureCarets Format="1">',
60    '  <Carets>',
61    '    <Lookup glyph="f_f_l">',
62    '      <!-- DivsionPointCount=2 -->',
63    '      <DivisionPoint index="0" value="55"/>',
64    '      <DivisionPoint index="1" value="75"/>',
65    '    </Lookup>',
66    '    <Lookup glyph="f_r">',
67    '      <!-- DivsionPointCount=1 -->',
68    '      <DivisionPoint index="0" value="50"/>',
69    '    </Lookup>',
70    '  </Carets>',
71    '</LigatureCarets>',
72]
73
74
75class LCARTest(unittest.TestCase):
76
77    @classmethod
78    def setUpClass(cls):
79        cls.maxDiff = None
80        cls.font = FakeFont(['.notdef', 'f_r', 'X', 'f_f_l'])
81
82    def test_decompile_toXML_format0(self):
83        table = newTable('lcar')
84        table.decompile(LCAR_FORMAT_0_DATA, self.font)
85        self.assertEqual(getXML(table.toXML), LCAR_FORMAT_0_XML)
86
87    def test_compile_fromXML_format0(self):
88        table = newTable('lcar')
89        for name, attrs, content in parseXML(LCAR_FORMAT_0_XML):
90            table.fromXML(name, attrs, content, font=self.font)
91        self.assertEqual(hexStr(table.compile(self.font)),
92                         hexStr(LCAR_FORMAT_0_DATA))
93
94    def test_decompile_toXML_format1(self):
95        table = newTable('lcar')
96        table.decompile(LCAR_FORMAT_1_DATA, self.font)
97        self.assertEqual(getXML(table.toXML), LCAR_FORMAT_1_XML)
98
99    def test_compile_fromXML_format1(self):
100        table = newTable('lcar')
101        for name, attrs, content in parseXML(LCAR_FORMAT_1_XML):
102            table.fromXML(name, attrs, content, font=self.font)
103        self.assertEqual(hexStr(table.compile(self.font)),
104                         hexStr(LCAR_FORMAT_1_DATA))
105
106
107if __name__ == '__main__':
108    import sys
109    sys.exit(unittest.main())
110