1from __future__ import print_function, division, absolute_import
2from fontTools.misc.py23 import SimpleNamespace
3from fontTools.misc.textTools import deHexStr
4from fontTools.misc.testTools import getXML
5from fontTools.ttLib.tables.T_S_I__0 import table_T_S_I__0
6import pytest
7
8
9# (gid, length, offset) for glyph programs
10TSI0_INDICES = [
11    (0, 1, 0),
12    (1, 5, 1),
13    (2, 0, 1),
14    (3, 0, 1),
15    (4, 8, 6)]
16
17# (type, length, offset) for 'extra' programs
18TSI0_EXTRA_INDICES = [
19    (0xFFFA, 2, 14),          # ppgm
20    (0xFFFB, 4, 16),          # cvt
21    (0xFFFC, 6, 20),          # reserved
22    (0xFFFD, 10, 26)]         # fpgm
23
24# compiled TSI0 table from data above
25TSI0_DATA = deHexStr(
26    "0000 0001 00000000"
27    "0001 0005 00000001"
28    "0002 0000 00000001"
29    "0003 0000 00000001"
30    "0004 0008 00000006"
31    "FFFE 0000 ABFC1F34"      # 'magic' separates glyph from extra programs
32    "FFFA 0002 0000000E"
33    "FFFB 0004 00000010"
34    "FFFC 0006 00000014"
35    "FFFD 000A 0000001A")
36
37# empty font has no glyph programs but 4 extra programs are always present
38EMPTY_TSI0_EXTRA_INDICES = [
39    (0xFFFA, 0, 0),
40    (0xFFFB, 0, 0),
41    (0xFFFC, 0, 0),
42    (0xFFFD, 0, 0)]
43
44EMPTY_TSI0_DATA = deHexStr(
45    "FFFE 0000 ABFC1F34"
46    "FFFA 0000 00000000"
47    "FFFB 0000 00000000"
48    "FFFC 0000 00000000"
49    "FFFD 0000 00000000")
50
51
52@pytest.fixture
53def table():
54    return table_T_S_I__0()
55
56
57@pytest.mark.parametrize(
58    "numGlyphs, data, expected_indices, expected_extra_indices",
59    [
60        (5, TSI0_DATA, TSI0_INDICES, TSI0_EXTRA_INDICES),
61        (0, EMPTY_TSI0_DATA, [], EMPTY_TSI0_EXTRA_INDICES)
62    ],
63    ids=["simple", "empty"]
64)
65def test_decompile(table, numGlyphs, data, expected_indices,
66                   expected_extra_indices):
67    font = {'maxp': SimpleNamespace(numGlyphs=numGlyphs)}
68
69    table.decompile(data, font)
70
71    assert len(table.indices) == numGlyphs
72    assert table.indices == expected_indices
73    assert len(table.extra_indices) == 4
74    assert table.extra_indices == expected_extra_indices
75
76
77@pytest.mark.parametrize(
78    "numGlyphs, indices, extra_indices, expected_data",
79    [
80        (5, TSI0_INDICES, TSI0_EXTRA_INDICES, TSI0_DATA),
81        (0, [], EMPTY_TSI0_EXTRA_INDICES, EMPTY_TSI0_DATA)
82    ],
83    ids=["simple", "empty"]
84)
85def test_compile(table, numGlyphs, indices, extra_indices, expected_data):
86    assert table.compile(ttFont=None) == b""
87
88    table.set(indices, extra_indices)
89    data = table.compile(ttFont=None)
90    assert data == expected_data
91
92
93def test_set(table):
94    table.set(TSI0_INDICES, TSI0_EXTRA_INDICES)
95    assert table.indices == TSI0_INDICES
96    assert table.extra_indices == TSI0_EXTRA_INDICES
97
98
99def test_toXML(table):
100    assert getXML(table.toXML, ttFont=None) == [
101        '<!-- This table will be calculated by the compiler -->']
102
103
104if __name__ == "__main__":
105    import sys
106    sys.exit(pytest.main(sys.argv))
107