T_S_I__5.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. See also https://learn.microsoft.com/en-us/typography/tools/vtt/tsi-tables
  5. """
  6. from fontTools.misc.textTools import safeEval
  7. from . import DefaultTable
  8. import sys
  9. import array
  10. class table_T_S_I__5(DefaultTable.DefaultTable):
  11. def decompile(self, data, ttFont):
  12. numGlyphs = ttFont["maxp"].numGlyphs
  13. assert len(data) == 2 * numGlyphs
  14. a = array.array("H")
  15. a.frombytes(data)
  16. if sys.byteorder != "big":
  17. a.byteswap()
  18. self.glyphGrouping = {}
  19. for i in range(numGlyphs):
  20. self.glyphGrouping[ttFont.getGlyphName(i)] = a[i]
  21. def compile(self, ttFont):
  22. glyphNames = ttFont.getGlyphOrder()
  23. a = array.array("H")
  24. for i in range(len(glyphNames)):
  25. a.append(self.glyphGrouping.get(glyphNames[i], 0))
  26. if sys.byteorder != "big":
  27. a.byteswap()
  28. return a.tobytes()
  29. def toXML(self, writer, ttFont):
  30. names = sorted(self.glyphGrouping.keys())
  31. for glyphName in names:
  32. writer.simpletag(
  33. "glyphgroup", name=glyphName, value=self.glyphGrouping[glyphName]
  34. )
  35. writer.newline()
  36. def fromXML(self, name, attrs, content, ttFont):
  37. if not hasattr(self, "glyphGrouping"):
  38. self.glyphGrouping = {}
  39. if name != "glyphgroup":
  40. return
  41. self.glyphGrouping[attrs["name"]] = safeEval(attrs["value"])