1from fontTools.misc.textTools import safeEval 2from . import DefaultTable 3import struct 4 5 6GASP_SYMMETRIC_GRIDFIT = 0x0004 7GASP_SYMMETRIC_SMOOTHING = 0x0008 8GASP_DOGRAY = 0x0002 9GASP_GRIDFIT = 0x0001 10 11class table__g_a_s_p(DefaultTable.DefaultTable): 12 13 def decompile(self, data, ttFont): 14 self.version, numRanges = struct.unpack(">HH", data[:4]) 15 assert 0 <= self.version <= 1, "unknown 'gasp' format: %s" % self.version 16 data = data[4:] 17 self.gaspRange = {} 18 for i in range(numRanges): 19 rangeMaxPPEM, rangeGaspBehavior = struct.unpack(">HH", data[:4]) 20 self.gaspRange[int(rangeMaxPPEM)] = int(rangeGaspBehavior) 21 data = data[4:] 22 assert not data, "too much data" 23 24 def compile(self, ttFont): 25 version = 0 # ignore self.version 26 numRanges = len(self.gaspRange) 27 data = b"" 28 items = sorted(self.gaspRange.items()) 29 for rangeMaxPPEM, rangeGaspBehavior in items: 30 data = data + struct.pack(">HH", rangeMaxPPEM, rangeGaspBehavior) 31 if rangeGaspBehavior & ~(GASP_GRIDFIT | GASP_DOGRAY): 32 version = 1 33 data = struct.pack(">HH", version, numRanges) + data 34 return data 35 36 def toXML(self, writer, ttFont): 37 items = sorted(self.gaspRange.items()) 38 for rangeMaxPPEM, rangeGaspBehavior in items: 39 writer.simpletag("gaspRange", [ 40 ("rangeMaxPPEM", rangeMaxPPEM), 41 ("rangeGaspBehavior", rangeGaspBehavior)]) 42 writer.newline() 43 44 def fromXML(self, name, attrs, content, ttFont): 45 if name != "gaspRange": 46 return 47 if not hasattr(self, "gaspRange"): 48 self.gaspRange = {} 49 self.gaspRange[safeEval(attrs["rangeMaxPPEM"])] = safeEval(attrs["rangeGaspBehavior"]) 50