1from __future__ import print_function, division, absolute_import 2from fontTools.misc.py23 import * 3from fontTools.misc.textTools import safeEval 4from . import DefaultTable 5import struct 6 7# https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6ltag.html 8 9class table__l_t_a_g(DefaultTable.DefaultTable): 10 def __init__(self, tag=None): 11 DefaultTable.DefaultTable.__init__(self, tag) 12 self.version, self.flags = 1, 0 13 self.tags = [] 14 15 def addTag(self, tag): 16 """Add 'tag' to the list of langauge tags if not already there. 17 18 Returns the integer index of 'tag' in the list of all tags. 19 """ 20 try: 21 return self.tags.index(tag) 22 except ValueError: 23 self.tags.append(tag) 24 return len(self.tags) - 1 25 26 def decompile(self, data, ttFont): 27 self.version, self.flags, numTags = struct.unpack(">LLL", data[:12]) 28 assert self.version == 1 29 self.tags = [] 30 for i in range(numTags): 31 pos = 12 + i * 4 32 offset, length = struct.unpack(">HH", data[pos:pos+4]) 33 tag = data[offset:offset+length].decode("ascii") 34 self.tags.append(tag) 35 36 def compile(self, ttFont): 37 dataList = [struct.pack(">LLL", self.version, self.flags, len(self.tags))] 38 stringPool = "" 39 for tag in self.tags: 40 offset = stringPool.find(tag) 41 if offset < 0: 42 offset = len(stringPool) 43 stringPool = stringPool + tag 44 offset = offset + 12 + len(self.tags) * 4 45 dataList.append(struct.pack(">HH", offset, len(tag))) 46 dataList.append(tobytes(stringPool)) 47 return bytesjoin(dataList) 48 49 def toXML(self, writer, ttFont): 50 writer.simpletag("version", value=self.version) 51 writer.newline() 52 writer.simpletag("flags", value=self.flags) 53 writer.newline() 54 for tag in self.tags: 55 writer.simpletag("LanguageTag", tag=tag) 56 writer.newline() 57 58 def fromXML(self, name, attrs, content, ttFont): 59 if not hasattr(self, "tags"): 60 self.tags = [] 61 if name == "LanguageTag": 62 self.tags.append(attrs["tag"]) 63 elif "value" in attrs: 64 value = safeEval(attrs["value"]) 65 setattr(self, name, value) 66