aboutsummaryrefslogtreecommitdiff
path: root/Tests/ttLib/tables/T_S_I__0_test.py
blob: 44ca44ed3a59632ef4fd45273315b1bf9aaed883 (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
from types import SimpleNamespace
from fontTools.misc.textTools import deHexStr
from fontTools.misc.testTools import getXML
from fontTools.ttLib.tables.T_S_I__0 import table_T_S_I__0
import pytest


# (gid, length, offset) for glyph programs
TSI0_INDICES = [
    (0, 1, 0),
    (1, 5, 1),
    (2, 0, 1),
    (3, 0, 1),
    (4, 8, 6)]

# (type, length, offset) for 'extra' programs
TSI0_EXTRA_INDICES = [
    (0xFFFA, 2, 14),          # ppgm
    (0xFFFB, 4, 16),          # cvt
    (0xFFFC, 6, 20),          # reserved
    (0xFFFD, 10, 26)]         # fpgm

# compiled TSI0 table from data above
TSI0_DATA = deHexStr(
    "0000 0001 00000000"
    "0001 0005 00000001"
    "0002 0000 00000001"
    "0003 0000 00000001"
    "0004 0008 00000006"
    "FFFE 0000 ABFC1F34"      # 'magic' separates glyph from extra programs
    "FFFA 0002 0000000E"
    "FFFB 0004 00000010"
    "FFFC 0006 00000014"
    "FFFD 000A 0000001A")

# empty font has no glyph programs but 4 extra programs are always present
EMPTY_TSI0_EXTRA_INDICES = [
    (0xFFFA, 0, 0),
    (0xFFFB, 0, 0),
    (0xFFFC, 0, 0),
    (0xFFFD, 0, 0)]

EMPTY_TSI0_DATA = deHexStr(
    "FFFE 0000 ABFC1F34"
    "FFFA 0000 00000000"
    "FFFB 0000 00000000"
    "FFFC 0000 00000000"
    "FFFD 0000 00000000")


@pytest.fixture
def table():
    return table_T_S_I__0()


@pytest.mark.parametrize(
    "numGlyphs, data, expected_indices, expected_extra_indices",
    [
        (5, TSI0_DATA, TSI0_INDICES, TSI0_EXTRA_INDICES),
        (0, EMPTY_TSI0_DATA, [], EMPTY_TSI0_EXTRA_INDICES)
    ],
    ids=["simple", "empty"]
)
def test_decompile(table, numGlyphs, data, expected_indices,
                   expected_extra_indices):
    font = {'maxp': SimpleNamespace(numGlyphs=numGlyphs)}

    table.decompile(data, font)

    assert len(table.indices) == numGlyphs
    assert table.indices == expected_indices
    assert len(table.extra_indices) == 4
    assert table.extra_indices == expected_extra_indices


@pytest.mark.parametrize(
    "numGlyphs, indices, extra_indices, expected_data",
    [
        (5, TSI0_INDICES, TSI0_EXTRA_INDICES, TSI0_DATA),
        (0, [], EMPTY_TSI0_EXTRA_INDICES, EMPTY_TSI0_DATA)
    ],
    ids=["simple", "empty"]
)
def test_compile(table, numGlyphs, indices, extra_indices, expected_data):
    assert table.compile(ttFont=None) == b""

    table.set(indices, extra_indices)
    data = table.compile(ttFont=None)
    assert data == expected_data


def test_set(table):
    table.set(TSI0_INDICES, TSI0_EXTRA_INDICES)
    assert table.indices == TSI0_INDICES
    assert table.extra_indices == TSI0_EXTRA_INDICES


def test_toXML(table):
    assert getXML(table.toXML, ttFont=None) == [
        '<!-- This table will be calculated by the compiler -->']


if __name__ == "__main__":
    import sys
    sys.exit(pytest.main(sys.argv))