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