1""" TSI{0,1,2,3,5} are private tables used by Microsoft Visual TrueType (VTT) 2tool to store its hinting source data. 3 4TSI0 is the index table containing the lengths and offsets for the glyph 5programs and 'extra' programs ('fpgm', 'prep', and 'cvt') that are contained 6in the TSI1 table. 7""" 8 9from . import DefaultTable 10import struct 11 12tsi0Format = ">HHL" 13 14 15def fixlongs(glyphID, textLength, textOffset): 16 return int(glyphID), int(textLength), textOffset 17 18 19class table_T_S_I__0(DefaultTable.DefaultTable): 20 dependencies = ["TSI1"] 21 22 def decompile(self, data, ttFont): 23 numGlyphs = ttFont["maxp"].numGlyphs 24 indices = [] 25 size = struct.calcsize(tsi0Format) 26 for i in range(numGlyphs + 5): 27 glyphID, textLength, textOffset = fixlongs( 28 *struct.unpack(tsi0Format, data[:size]) 29 ) 30 indices.append((glyphID, textLength, textOffset)) 31 data = data[size:] 32 assert len(data) == 0 33 assert indices[-5] == (0xFFFE, 0, 0xABFC1F34), "bad magic number" 34 self.indices = indices[:-5] 35 self.extra_indices = indices[-4:] 36 37 def compile(self, ttFont): 38 if not hasattr(self, "indices"): 39 # We have no corresponding table (TSI1 or TSI3); let's return 40 # no data, which effectively means "ignore us". 41 return b"" 42 data = b"" 43 for index, textLength, textOffset in self.indices: 44 data = data + struct.pack(tsi0Format, index, textLength, textOffset) 45 data = data + struct.pack(tsi0Format, 0xFFFE, 0, 0xABFC1F34) 46 for index, textLength, textOffset in self.extra_indices: 47 data = data + struct.pack(tsi0Format, index, textLength, textOffset) 48 return data 49 50 def set(self, indices, extra_indices): 51 # gets called by 'TSI1' or 'TSI3' 52 self.indices = indices 53 self.extra_indices = extra_indices 54 55 def toXML(self, writer, ttFont): 56 writer.comment("This table will be calculated by the compiler") 57 writer.newline() 58