1from fontTools.misc import sstruct 2from fontTools.misc.textTools import safeEval 3from . import DefaultTable 4import array 5import sys 6 7 8Gloc_header = ''' 9 > # big endian 10 version: 16.16F # Table version 11 flags: H # bit 0: 1=long format, 0=short format 12 # bit 1: 1=attribute names, 0=no names 13 numAttribs: H # NUmber of attributes 14''' 15 16class table_G__l_o_c(DefaultTable.DefaultTable): 17 """ 18 Support Graphite Gloc tables 19 """ 20 dependencies = ['Glat'] 21 22 def __init__(self, tag=None): 23 DefaultTable.DefaultTable.__init__(self, tag) 24 self.attribIds = None 25 self.numAttribs = 0 26 27 def decompile(self, data, ttFont): 28 _, data = sstruct.unpack2(Gloc_header, data, self) 29 flags = self.flags 30 del self.flags 31 self.locations = array.array('I' if flags & 1 else 'H') 32 self.locations.frombytes(data[:len(data) - self.numAttribs * (flags & 2)]) 33 if sys.byteorder != "big": self.locations.byteswap() 34 self.attribIds = array.array('H') 35 if flags & 2: 36 self.attribIds.frombytes(data[-self.numAttribs * 2:]) 37 if sys.byteorder != "big": self.attribIds.byteswap() 38 39 def compile(self, ttFont): 40 data = sstruct.pack(Gloc_header, dict(version=1.0, 41 flags=(bool(self.attribIds) << 1) + (self.locations.typecode == 'I'), 42 numAttribs=self.numAttribs)) 43 if sys.byteorder != "big": self.locations.byteswap() 44 data += self.locations.tobytes() 45 if sys.byteorder != "big": self.locations.byteswap() 46 if self.attribIds: 47 if sys.byteorder != "big": self.attribIds.byteswap() 48 data += self.attribIds.tobytes() 49 if sys.byteorder != "big": self.attribIds.byteswap() 50 return data 51 52 def set(self, locations): 53 long_format = max(locations) >= 65536 54 self.locations = array.array('I' if long_format else 'H', locations) 55 56 def toXML(self, writer, ttFont): 57 writer.simpletag("attributes", number=self.numAttribs) 58 writer.newline() 59 60 def fromXML(self, name, attrs, content, ttFont): 61 if name == 'attributes': 62 self.numAttribs = int(safeEval(attrs['number'])) 63 64 def __getitem__(self, index): 65 return self.locations[index] 66 67 def __len__(self): 68 return len(self.locations) 69 70 def __iter__(self): 71 return iter(self.locations) 72