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