tableRecord.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from data import Tag
  2. import struct
  3. class TableRecord:
  4. """
  5. Simple class representing a single Table Record in a font.
  6. These are only relevant for bytes compilation, so there's no TTX output.
  7. """
  8. def __init__(self, tag, checkSum, offset, length):
  9. try:
  10. self.tag = Tag(tag)
  11. except ValueError as e:
  12. raise ValueError(f"Creating tableRecord failed. -> {e}")
  13. self.checkSum = checkSum
  14. self.offset = offset # from the very beginning of the TrueType file.
  15. self.length = length
  16. def toBytes(self):
  17. return struct.pack (">4sIII"
  18. , self.tag.toBytes() # 4 bytes (UInt32)
  19. , self.checkSum # UInt32
  20. , self.offset # UInt32 (Offset32)
  21. , self.length # UInt32
  22. )
  23. # this is not a normal table, and thus should not be padded.
  24. def __lt__(self, other):
  25. """
  26. Required because TableRecords need to be sorted from lowest
  27. to highest tag value when in use in an actual font file.
  28. """
  29. if int(self.tag) < int(other.tag):
  30. return True
  31. else:
  32. return False
  33. def __repr__(self):
  34. return f"TableRecord for {self.tag} - offset: {self.offset} - length: {self.length} - checksum: {self.checkSum}\n"