• 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    dependencies = ["glyf"]
12
13    def decompile(self, data, ttFont):
14        longFormat = ttFont["head"].indexToLocFormat
15        if longFormat:
16            format = "I"
17        else:
18            format = "H"
19        locations = array.array(format)
20        locations.frombytes(data)
21        if sys.byteorder != "big":
22            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(
30                "corrupt 'loca' table, or wrong numGlyphs in 'maxp': %d %d",
31                len(locations) - 1,
32                ttFont["maxp"].numGlyphs,
33            )
34        self.locations = locations
35
36    def compile(self, ttFont):
37        try:
38            max_location = max(self.locations)
39        except AttributeError:
40            self.set([])
41            max_location = 0
42        if max_location < 0x20000 and all(l % 2 == 0 for l in self.locations):
43            locations = array.array("H")
44            for i in range(len(self.locations)):
45                locations.append(self.locations[i] // 2)
46            ttFont["head"].indexToLocFormat = 0
47        else:
48            locations = array.array("I", self.locations)
49            ttFont["head"].indexToLocFormat = 1
50        if sys.byteorder != "big":
51            locations.byteswap()
52        return locations.tobytes()
53
54    def set(self, locations):
55        self.locations = array.array("I", locations)
56
57    def toXML(self, writer, ttFont):
58        writer.comment("The 'loca' table will be calculated by the compiler")
59        writer.newline()
60
61    def __getitem__(self, index):
62        return self.locations[index]
63
64    def __len__(self):
65        return len(self.locations)
66