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