• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from fontTools.misc import sstruct
2from fontTools.misc.fixedTools import floatToFixedToStr
3from fontTools.misc.textTools import safeEval
4from . import DefaultTable
5from . import grUtils
6import struct
7
8Sill_hdr = '''
9    >
10    version:    16.16F
11'''
12
13class table_S__i_l_l(DefaultTable.DefaultTable):
14
15    def __init__(self, tag=None):
16        DefaultTable.DefaultTable.__init__(self, tag)
17        self.langs = {}
18
19    def decompile(self, data, ttFont):
20        (_, data) = sstruct.unpack2(Sill_hdr, data, self)
21        self.version = float(floatToFixedToStr(self.version, precisionBits=16))
22        numLangs, = struct.unpack('>H', data[:2])
23        data = data[8:]
24        maxsetting = 0
25        langinfo = []
26        for i in range(numLangs):
27            (langcode, numsettings, offset) = struct.unpack(">4sHH",
28                                                        data[i * 8:(i+1) * 8])
29            offset = int(offset / 8) - (numLangs + 1)
30            langcode = langcode.replace(b'\000', b'')
31            langinfo.append((langcode.decode("utf-8"), numsettings, offset))
32            maxsetting = max(maxsetting, offset + numsettings)
33        data = data[numLangs * 8:]
34        finfo = []
35        for i in range(maxsetting):
36            (fid, val, _) = struct.unpack(">LHH", data[i * 8:(i+1) * 8])
37            finfo.append((fid, val))
38        self.langs = {}
39        for c, n, o in langinfo:
40            self.langs[c] = []
41            for i in range(o, o+n):
42                self.langs[c].append(finfo[i])
43
44    def compile(self, ttFont):
45        ldat = b""
46        fdat = b""
47        offset = len(self.langs)
48        for c, inf in sorted(self.langs.items()):
49            ldat += struct.pack(">4sHH", c.encode('utf8'), len(inf), 8 * offset + 20)
50            for fid, val in inf:
51                fdat += struct.pack(">LHH", fid, val, 0)
52            offset += len(inf)
53        ldat += struct.pack(">LHH", 0x80808080, 0, 8 * offset + 20)
54        return sstruct.pack(Sill_hdr, self) + grUtils.bininfo(len(self.langs)) + \
55                ldat + fdat
56
57    def toXML(self, writer, ttFont):
58        writer.simpletag('version', version=self.version)
59        writer.newline()
60        for c, inf in sorted(self.langs.items()):
61            writer.begintag('lang', name=c)
62            writer.newline()
63            for fid, val in inf:
64                writer.simpletag('feature', fid=grUtils.num2tag(fid), val=val)
65                writer.newline()
66            writer.endtag('lang')
67            writer.newline()
68
69    def fromXML(self, name, attrs, content, ttFont):
70        if name == 'version':
71            self.version = float(safeEval(attrs['version']))
72        elif name == 'lang':
73            c = attrs['name']
74            self.langs[c] = []
75            for element in content:
76                if not isinstance(element, tuple): continue
77                tag, a, subcontent = element
78                if tag == 'feature':
79                    self.langs[c].append((grUtils.tag2num(a['fid']),
80                                            int(safeEval(a['val']))))
81