G_M_A_P_.py revision 7cc6d271ac955782d730161b27e728001fb5f347
1from . import DefaultTable 2from fontTools.misc import sstruct 3from types import StringType 4from fontTools.misc.textTools import safeEval, num2binary, binary2num 5 6GMAPFormat = """ 7 > # big endian 8 tableVersionMajor: H 9 tableVersionMinor: H 10 flags: H 11 recordsCount: H 12 recordsOffset: H 13 fontNameLength: H 14""" 15# psFontName is a byte string which follows the record above. This is zero padded 16# to the beginning of the records array. The recordsOffsst is 32 bit aligned. 17 18GMAPRecordFormat1 = """ 19 > # big endian 20 UV: L 21 cid: H 22 gid: H 23 ggid: H 24 name: 32s 25""" 26 27 28 29class GMAPRecord: 30 def __init__(self, uv = 0, cid = 0, gid = 0, ggid = 0, name = ""): 31 self.UV = uv 32 self.cid = cid 33 self.gid = gid 34 self.ggid = ggid 35 self.name = name 36 37 def toXML(self, writer, ttFont): 38 writer.begintag("GMAPRecord") 39 writer.newline() 40 writer.simpletag("UV", value=self.UV) 41 writer.newline() 42 writer.simpletag("cid", value=self.cid) 43 writer.newline() 44 writer.simpletag("gid", value=self.gid) 45 writer.newline() 46 writer.simpletag("glyphletGid", value=self.gid) 47 writer.newline() 48 writer.simpletag("GlyphletName", value=self.name) 49 writer.newline() 50 writer.endtag("GMAPRecord") 51 writer.newline() 52 53 54 def fromXML(self, name, attrs, content, ttFont): 55 value = attrs["value"] 56 if name == "GlyphletName": 57 self.name = value 58 else: 59 setattr(self, name, safeEval(value)) 60 61 62 def compile(self, ttFont): 63 if self.UV == None: 64 self.UV = 0 65 nameLen = len(self.name) 66 if nameLen < 32: 67 self.name = self.name + "\0"*(32 - nameLen) 68 data = sstruct.pack(GMAPRecordFormat1, self) 69 return data 70 71 def __repr__(self): 72 return "GMAPRecord[ UV: " + str(self.UV) + ", cid: " + str(self.cid) + ", gid: " + str(self.gid) + ", ggid: " + str(self.ggid) + ", Glyphlet Name: " + str(self.name) + " ]" 73 74 75class table_G_M_A_P_(DefaultTable.DefaultTable): 76 77 dependencies = [] 78 79 def decompile(self, data, ttFont): 80 dummy, newData = sstruct.unpack2(GMAPFormat, data, self) 81 self.psFontName = newData[:self.fontNameLength] 82 assert (self.recordsOffset % 4) == 0, "GMAP error: recordsOffset is not 32 bit aligned." 83 newData = data[self.recordsOffset:] 84 self.gmapRecords = [] 85 for i in range (self.recordsCount): 86 gmapRecord, newData = sstruct.unpack2(GMAPRecordFormat1, newData, GMAPRecord()) 87 gmapRecord.name = gmapRecord.name.strip('\0') 88 self.gmapRecords.append(gmapRecord) 89 90 91 def compile(self, ttFont): 92 self.recordsCount = len(self.gmapRecords) 93 self.fontNameLength = len(self.psFontName) 94 self.recordsOffset = 4 *(((self.fontNameLength + 12) + 3) /4) 95 data = sstruct.pack(GMAPFormat, self) 96 data = data + self.psFontName 97 data = data + "\0" * (self.recordsOffset - len(data)) 98 for record in self.gmapRecords: 99 data = data + record.compile(ttFont) 100 return data 101 102 103 def toXML(self, writer, ttFont): 104 writer.comment("Most of this table will be recalculated by the compiler") 105 writer.newline() 106 formatstring, names, fixes = sstruct.getformat(GMAPFormat) 107 for name in names: 108 value = getattr(self, name) 109 writer.simpletag(name, value=value) 110 writer.newline() 111 writer.simpletag("PSFontName", value=self.psFontName) 112 writer.newline() 113 for gmapRecord in self.gmapRecords: 114 gmapRecord.toXML(writer, ttFont) 115 116 def fromXML(self, name, attrs, content, ttFont): 117 if name == "GMAPRecord": 118 if not hasattr(self, "gmapRecords"): 119 self.gmapRecords = [] 120 gmapRecord = GMAPRecord() 121 self.gmapRecords.append(gmapRecord) 122 for element in content: 123 if isinstance(element, StringType): 124 continue 125 name, attrs, content = element 126 gmapRecord.fromXML(name, attrs, content, ttFont) 127 else: 128 value = attrs["value"] 129 if name == "PSFontName": 130 self.psFontName = value 131 else: 132 setattr(self, name, safeEval(value)) 133