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