aboutsummaryrefslogtreecommitdiff
path: root/Lib/fontTools/ttLib/tables/_l_o_c_a.py
blob: 5884cef45f8ce3027bdff680f1325ed8b8409048 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from . import DefaultTable
import sys
import array
import logging


log = logging.getLogger(__name__)


class table__l_o_c_a(DefaultTable.DefaultTable):
    dependencies = ["glyf"]

    def decompile(self, data, ttFont):
        longFormat = ttFont["head"].indexToLocFormat
        if longFormat:
            format = "I"
        else:
            format = "H"
        locations = array.array(format)
        locations.frombytes(data)
        if sys.byteorder != "big":
            locations.byteswap()
        if not longFormat:
            l = array.array("I")
            for i in range(len(locations)):
                l.append(locations[i] * 2)
            locations = l
        if len(locations) < (ttFont["maxp"].numGlyphs + 1):
            log.warning(
                "corrupt 'loca' table, or wrong numGlyphs in 'maxp': %d %d",
                len(locations) - 1,
                ttFont["maxp"].numGlyphs,
            )
        self.locations = locations

    def compile(self, ttFont):
        try:
            max_location = max(self.locations)
        except AttributeError:
            self.set([])
            max_location = 0
        if max_location < 0x20000 and all(l % 2 == 0 for l in self.locations):
            locations = array.array("H")
            for i in range(len(self.locations)):
                locations.append(self.locations[i] // 2)
            ttFont["head"].indexToLocFormat = 0
        else:
            locations = array.array("I", self.locations)
            ttFont["head"].indexToLocFormat = 1
        if sys.byteorder != "big":
            locations.byteswap()
        return locations.tobytes()

    def set(self, locations):
        self.locations = array.array("I", locations)

    def toXML(self, writer, ttFont):
        writer.comment("The 'loca' table will be calculated by the compiler")
        writer.newline()

    def __getitem__(self, index):
        return self.locations[index]

    def __len__(self):
        return len(self.locations)