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