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