1 from __future__ import print_function, division, absolute_import 2 from fontTools.misc.py23 import * 3 from fontTools.misc import sstruct 4 from fontTools.misc.textTools import safeEval 5 from . import DefaultTable 6 from . import grUtils 7 import struct 8 9 Sill_hdr = ''' 10 > 11 version: 16.16F 12 ''' 13 14 class table_S__i_l_l(DefaultTable.DefaultTable): 15 16 def __init__(self, tag=None): 17 DefaultTable.DefaultTable.__init__(self, tag) 18 self.langs = {} 19 20 def decompile(self, data, ttFont): 21 (_, data) = sstruct.unpack2(Sill_hdr, data, self) 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, 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 = 0 48 for c, inf in sorted(self.langs.items()): 49 ldat += struct.pack(">4sHH", c.encode('utf8'), len(inf), 8 * (offset + len(self.langs) + 1)) 50 for fid, val in inf: 51 fdat += struct.pack(">LHH", fid, val, 0) 52 offset += len(inf) 53 return sstruct.pack(Sill_hdr, self) + grUtils.bininfo(len(self.langs)) + \ 54 ldat + fdat 55 56 def toXML(self, writer, ttFont): 57 writer.simpletag('version', version=self.version) 58 writer.newline() 59 for c, inf in sorted(self.langs.items()): 60 writer.begintag('lang', name=c) 61 writer.newline() 62 for fid, val in inf: 63 writer.simpletag('feature', fid=grUtils.num2tag(fid), val=val) 64 writer.newline() 65 writer.endtag('lang') 66 writer.newline() 67 68 def fromXML(self, name, attrs, content, ttFont): 69 if name == 'version': 70 self.version = float(safeEval(attrs['version'])) 71 elif name == 'lang': 72 c = attrs['name'] 73 self.langs[c] = [] 74 for element in content: 75 if not isinstance(element, tuple): continue 76 tag, a, subcontent = element 77 if tag == 'feature': 78 self.langs[c].append((grUtils.tag2num(a['fid']), 79 int(safeEval(a['val'])))) 80