T_S_I__0.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. TSI0 is the index table containing the lengths and offsets for the glyph
  4. programs and 'extra' programs ('fpgm', 'prep', and 'cvt') that are contained
  5. in the TSI1 table.
  6. See also https://learn.microsoft.com/en-us/typography/tools/vtt/tsi-tables
  7. """
  8. from . import DefaultTable
  9. import struct
  10. tsi0Format = ">HHL"
  11. def fixlongs(glyphID, textLength, textOffset):
  12. return int(glyphID), int(textLength), textOffset
  13. class table_T_S_I__0(DefaultTable.DefaultTable):
  14. dependencies = ["TSI1"]
  15. def decompile(self, data, ttFont):
  16. numGlyphs = ttFont["maxp"].numGlyphs
  17. indices = []
  18. size = struct.calcsize(tsi0Format)
  19. for i in range(numGlyphs + 5):
  20. glyphID, textLength, textOffset = fixlongs(
  21. *struct.unpack(tsi0Format, data[:size])
  22. )
  23. indices.append((glyphID, textLength, textOffset))
  24. data = data[size:]
  25. assert len(data) == 0
  26. assert indices[-5] == (0xFFFE, 0, 0xABFC1F34), "bad magic number"
  27. self.indices = indices[:-5]
  28. self.extra_indices = indices[-4:]
  29. def compile(self, ttFont):
  30. if not hasattr(self, "indices"):
  31. # We have no corresponding table (TSI1 or TSI3); let's return
  32. # no data, which effectively means "ignore us".
  33. return b""
  34. data = b""
  35. for index, textLength, textOffset in self.indices:
  36. data = data + struct.pack(tsi0Format, index, textLength, textOffset)
  37. data = data + struct.pack(tsi0Format, 0xFFFE, 0, 0xABFC1F34)
  38. for index, textLength, textOffset in self.extra_indices:
  39. data = data + struct.pack(tsi0Format, index, textLength, textOffset)
  40. return data
  41. def set(self, indices, extra_indices):
  42. # gets called by 'TSI1' or 'TSI3'
  43. self.indices = indices
  44. self.extra_indices = extra_indices
  45. def toXML(self, writer, ttFont):
  46. writer.comment("This table will be calculated by the compiler")
  47. writer.newline()