gasp.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import struct
  2. from lxml.etree import Element
  3. from transform.bytes import outputTableBytes
  4. class GaspRange:
  5. """
  6. Class representing a placeholder GaspRange record.
  7. """
  8. def __init__(self):
  9. self.rangeMaxPPEM = 65535
  10. self.rangeGaspBehavior = 0x0f
  11. def toTTX(self):
  12. return Element("gaspRange", {'rangeMaxPPEM': str(self.rangeMaxPPEM) # Decimal string
  13. ,'rangeGaspBehavior': hex(self.rangeGaspBehavior) # Hex string
  14. })
  15. def toBytes(self):
  16. return struct.pack( ">HH"
  17. , self.rangeMaxPPEM # UInt16
  18. , self.rangeGaspBehavior # UInt16
  19. )
  20. class gasp:
  21. """
  22. Class representing a really basic gasp table.
  23. """
  24. def __init__(self):
  25. self.version = 1
  26. self.gaspRanges = []
  27. self.gaspRanges.append(GaspRange())
  28. def toTTX(self):
  29. gasp = Element("gasp")
  30. # - TTX doesnt have version for gasp table.
  31. for gr in self.gaspRanges:
  32. gasp.append(gr.toTTX())
  33. return gasp
  34. def toBytes(self):
  35. gasp = struct.pack( ">HH"
  36. , self.version # UInt16
  37. , len(self.gaspRanges) # UInt16 (numRanges)
  38. )
  39. for gr in self.gaspRanges:
  40. gasp += gr.toBytes()
  41. return outputTableBytes(gasp)