aboutsummaryrefslogtreecommitdiff
path: root/Tests/ttLib/tables/_l_c_a_r_test.py
blob: 288092f01394330b79d1537b90fc537083a10b97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from fontTools.misc.py23 import *
from fontTools.misc.testTools import FakeFont, getXML, parseXML
from fontTools.misc.textTools import deHexStr, hexStr
from fontTools.ttLib import newTable
import unittest


# Example: Format 0 Ligature Caret Table
# https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6lcar.html
LCAR_FORMAT_0_DATA = deHexStr(
    '0001 0000 0000 '  #  0: Version=1.0, Format=0
    '0006 0004 0002 '  #  6: LookupFormat=6, UnitSize=4, NUnits=2
    '0008 0001 0000 '  # 12: SearchRange=8, EntrySelector=1, RangeShift=0
    '0001 001E '       # 18: Glyph=1 (f_r), OffsetOfLigCaretEntry=30
    '0003 0022 '       # 22: Glyph=3 (f_f_l), OffsetOfLigCaretEntry=34
    'FFFF 0000 '       # 26: Glyph=<end>, OffsetOfLigCaretEntry=0
    '0001 00DC '       # 30: DivisionPointCount=1, DivisionPoint=[220]
    '0002 00EF 01D8 '  # 34: DivisionPointCount=2, DivisionPoint=[239, 475]
)                      # 40: <end>
assert(len(LCAR_FORMAT_0_DATA) == 40)


LCAR_FORMAT_0_XML = [
    '<Version value="0x00010000"/>',
    '<LigatureCarets Format="0">',
    '  <Carets>',
    '    <Lookup glyph="f_f_l">',
    '      <!-- DivsionPointCount=2 -->',
    '      <DivisionPoint index="0" value="239"/>',
    '      <DivisionPoint index="1" value="472"/>',
    '    </Lookup>',
    '    <Lookup glyph="f_r">',
    '      <!-- DivsionPointCount=1 -->',
    '      <DivisionPoint index="0" value="220"/>',
    '    </Lookup>',
    '  </Carets>',
    '</LigatureCarets>',
]


# Example: Format 1 Ligature Caret Table
# https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6lcar.html
LCAR_FORMAT_1_DATA = deHexStr(
    '0001 0000 0001 '  #  0: Version=1.0, Format=1
    '0006 0004 0002 '  #  6: LookupFormat=6, UnitSize=4, NUnits=2
    '0008 0001 0000 '  # 12: SearchRange=8, EntrySelector=1, RangeShift=0
    '0001 001E '       # 18: Glyph=1 (f_r), OffsetOfLigCaretEntry=30
    '0003 0022 '       # 22: Glyph=3 (f_f_l), OffsetOfLigCaretEntry=34
    'FFFF 0000 '       # 26: Glyph=<end>, OffsetOfLigCaretEntry=0
    '0001 0032 '       # 30: DivisionPointCount=1, DivisionPoint=[50]
    '0002 0037 004B '  # 34: DivisionPointCount=2, DivisionPoint=[55, 75]
)                      # 40: <end>
assert(len(LCAR_FORMAT_1_DATA) == 40)


LCAR_FORMAT_1_XML = [
    '<Version value="0x00010000"/>',
    '<LigatureCarets Format="1">',
    '  <Carets>',
    '    <Lookup glyph="f_f_l">',
    '      <!-- DivsionPointCount=2 -->',
    '      <DivisionPoint index="0" value="55"/>',
    '      <DivisionPoint index="1" value="75"/>',
    '    </Lookup>',
    '    <Lookup glyph="f_r">',
    '      <!-- DivsionPointCount=1 -->',
    '      <DivisionPoint index="0" value="50"/>',
    '    </Lookup>',
    '  </Carets>',
    '</LigatureCarets>',
]


class LCARTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.maxDiff = None
        cls.font = FakeFont(['.notdef', 'f_r', 'X', 'f_f_l'])

    def test_decompile_toXML_format0(self):
        table = newTable('lcar')
        table.decompile(LCAR_FORMAT_0_DATA, self.font)
        self.assertEqual(getXML(table.toXML), LCAR_FORMAT_0_XML)

    def test_compile_fromXML_format0(self):
        table = newTable('lcar')
        for name, attrs, content in parseXML(LCAR_FORMAT_0_XML):
            table.fromXML(name, attrs, content, font=self.font)
        self.assertEqual(hexStr(table.compile(self.font)),
                         hexStr(LCAR_FORMAT_0_DATA))

    def test_decompile_toXML_format1(self):
        table = newTable('lcar')
        table.decompile(LCAR_FORMAT_1_DATA, self.font)
        self.assertEqual(getXML(table.toXML), LCAR_FORMAT_1_XML)

    def test_compile_fromXML_format1(self):
        table = newTable('lcar')
        for name, attrs, content in parseXML(LCAR_FORMAT_1_XML):
            table.fromXML(name, attrs, content, font=self.font)
        self.assertEqual(hexStr(table.compile(self.font)),
                         hexStr(LCAR_FORMAT_1_DATA))


if __name__ == '__main__':
    import sys
    sys.exit(unittest.main())