L_T_S_H_.py 1.8 KB

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