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"""
8from . import DefaultTable
9import struct
10
11tsi0Format = '>HHL'
12
13def fixlongs(glyphID, textLength, textOffset):
14	return int(glyphID), int(textLength), textOffset
15
16
17class table_T_S_I__0(DefaultTable.DefaultTable):
18
19	dependencies = ["TSI1"]
20
21	def decompile(self, data, ttFont):
22		numGlyphs = ttFont['maxp'].numGlyphs
23		indices = []
24		size = struct.calcsize(tsi0Format)
25		for i in range(numGlyphs + 5):
26			glyphID, textLength, textOffset = fixlongs(*struct.unpack(tsi0Format, data[:size]))
27			indices.append((glyphID, textLength, textOffset))
28			data = data[size:]
29		assert len(data) == 0
30		assert indices[-5] == (0XFFFE, 0, 0xABFC1F34), "bad magic number"
31		self.indices = indices[:-5]
32		self.extra_indices = indices[-4:]
33
34	def compile(self, ttFont):
35		if not hasattr(self, "indices"):
36			# We have no corresponding table (TSI1 or TSI3); let's return
37			# no data, which effectively means "ignore us".
38			return b""
39		data = b""
40		for index, textLength, textOffset in self.indices:
41			data = data + struct.pack(tsi0Format, index, textLength, textOffset)
42		data = data + struct.pack(tsi0Format, 0XFFFE, 0, 0xABFC1F34)
43		for index, textLength, textOffset in self.extra_indices:
44			data = data + struct.pack(tsi0Format, index, textLength, textOffset)
45		return data
46
47	def set(self, indices, extra_indices):
48		# gets called by 'TSI1' or 'TSI3'
49		self.indices = indices
50		self.extra_indices = extra_indices
51
52	def toXML(self, writer, ttFont):
53		writer.comment("This table will be calculated by the compiler")
54		writer.newline()
55