T_S_I__5.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. """ TSI{0,1,2,3,5} are private tables used by Microsoft Visual TrueType (VTT)
  2. tool to store its hinting source data.
  3. TSI5 contains the VTT character groups.
  4. """
  5. from fontTools.misc.textTools import safeEval
  6. from . import DefaultTable
  7. import sys
  8. import array
  9. class table_T_S_I__5(DefaultTable.DefaultTable):
  10. def decompile(self, data, ttFont):
  11. numGlyphs = ttFont["maxp"].numGlyphs
  12. assert len(data) == 2 * numGlyphs
  13. a = array.array("H")
  14. a.frombytes(data)
  15. if sys.byteorder != "big":
  16. a.byteswap()
  17. self.glyphGrouping = {}
  18. for i in range(numGlyphs):
  19. self.glyphGrouping[ttFont.getGlyphName(i)] = a[i]
  20. def compile(self, ttFont):
  21. glyphNames = ttFont.getGlyphOrder()
  22. a = array.array("H")
  23. for i in range(len(glyphNames)):
  24. a.append(self.glyphGrouping.get(glyphNames[i], 0))
  25. if sys.byteorder != "big":
  26. a.byteswap()
  27. return a.tobytes()
  28. def toXML(self, writer, ttFont):
  29. names = sorted(self.glyphGrouping.keys())
  30. for glyphName in names:
  31. writer.simpletag(
  32. "glyphgroup", name=glyphName, value=self.glyphGrouping[glyphName]
  33. )
  34. writer.newline()
  35. def fromXML(self, name, attrs, content, ttFont):
  36. if not hasattr(self, "glyphGrouping"):
  37. self.glyphGrouping = {}
  38. if name != "glyphgroup":
  39. return
  40. self.glyphGrouping[attrs["name"]] = safeEval(attrs["value"])