• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from fontTools.misc.textTools import safeEval
2from . import DefaultTable
3import struct
4import array
5
6# XXX I've lowered the strictness, to make sure Apple's own Chicago
7# XXX gets through. They're looking into it, I hope to raise the standards
8# XXX back to normal eventually.
9
10class table_L_T_S_H_(DefaultTable.DefaultTable):
11
12	def decompile(self, data, ttFont):
13		version, numGlyphs = struct.unpack(">HH", data[:4])
14		data = data[4:]
15		assert version == 0, "unknown version: %s" % version
16		assert (len(data) % numGlyphs) < 4, "numGlyphs doesn't match data length"
17		# ouch: the assertion is not true in Chicago!
18		#assert numGlyphs == ttFont['maxp'].numGlyphs
19		yPels = array.array("B")
20		yPels.frombytes(data)
21		self.yPels = {}
22		for i in range(numGlyphs):
23			self.yPels[ttFont.getGlyphName(i)] = yPels[i]
24
25	def compile(self, ttFont):
26		version = 0
27		names = list(self.yPels.keys())
28		numGlyphs = len(names)
29		yPels = [0] * numGlyphs
30		# ouch: the assertion is not true in Chicago!
31		#assert len(self.yPels) == ttFont['maxp'].numGlyphs == numGlyphs
32		for name in names:
33			yPels[ttFont.getGlyphID(name)] = self.yPels[name]
34		yPels = array.array("B", yPels)
35		return struct.pack(">HH", version, numGlyphs) + yPels.tobytes()
36
37	def toXML(self, writer, ttFont):
38		names = sorted(self.yPels.keys())
39		for name in names:
40			writer.simpletag("yPel", name=name, value=self.yPels[name])
41			writer.newline()
42
43	def fromXML(self, name, attrs, content, ttFont):
44		if not hasattr(self, "yPels"):
45			self.yPels = {}
46		if name != "yPel":
47			return # ignore unknown tags
48		self.yPels[attrs["name"]] = safeEval(attrs["value"])
49