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