1from __future__ import print_function, division, absolute_import
2from fontTools.misc.py23 import *
3from . import DefaultTable
4import sys
5import array
6import warnings
7
8class table__l_o_c_a(DefaultTable.DefaultTable):
9
10	dependencies = ['glyf']
11
12	def decompile(self, data, ttFont):
13		longFormat = ttFont['head'].indexToLocFormat
14		if longFormat:
15			format = "I"
16		else:
17			format = "H"
18		locations = array.array(format)
19		locations.fromstring(data)
20		if sys.byteorder != "big":
21			locations.byteswap()
22		if not longFormat:
23			l = array.array("I")
24			for i in range(len(locations)):
25				l.append(locations[i] * 2)
26			locations = l
27		if len(locations) < (ttFont['maxp'].numGlyphs + 1):
28			warnings.warn("corrupt 'loca' table, or wrong numGlyphs in 'maxp': %d %d" % (len(locations) - 1, ttFont['maxp'].numGlyphs))
29		self.locations = locations
30
31	def compile(self, ttFont):
32		try:
33			max_location = max(self.locations)
34		except AttributeError:
35			self.set([])
36			max_location = 0
37		if max_location < 0x20000:
38			locations = array.array("H")
39			for i in range(len(self.locations)):
40				locations.append(self.locations[i] // 2)
41			ttFont['head'].indexToLocFormat = 0
42		else:
43			locations = array.array("I", self.locations)
44			ttFont['head'].indexToLocFormat = 1
45		if sys.byteorder != "big":
46			locations.byteswap()
47		return locations.tostring()
48
49	def set(self, locations):
50		self.locations = array.array("I", locations)
51
52	def toXML(self, writer, ttFont):
53		writer.comment("The 'loca' table will be calculated by the compiler")
54		writer.newline()
55
56	def __getitem__(self, index):
57		return self.locations[index]
58
59	def __len__(self):
60		return len(self.locations)
61
62