T_S_I__0.py 2.0 KB

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