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