maxp.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from lxml.etree import Element
  2. import struct
  3. from data import VFixed
  4. from transform.bytes import outputTableBytes
  5. class maxp:
  6. def __init__(self, glyphs):
  7. self.version = VFixed('1.0') # hard-coded
  8. self.numGlyphs = len(glyphs["all"])
  9. # sbix determines the number of glyphs from this data point.
  10. self.maxPoints = 0
  11. self.maxContours = 0
  12. self.maxCompositePoints = 0
  13. self.maxCompositeContours = 0
  14. self.maxZones = 0
  15. self.maxTwilightPoints = 0
  16. self.maxStorage = 1
  17. self.maxFunctionDefs = 1
  18. self.maxInstructionDefs = 0
  19. self.maxStackElements = 64
  20. self.maxSizeOfInstructions = 0
  21. self.maxComponentElements = 0
  22. self.maxComponentDepth = 0
  23. def toTTX(self):
  24. # TTX re-calculates most, if not all of these values by itself, but it's still worth giving them anyway.
  25. maxp = Element("maxp")
  26. maxp.append(Element("tableVersion", {'value': self.version.toHexStr() })) # TTX wants the version in this format.
  27. maxp.append(Element("numGlyphs", {'value': str(self.numGlyphs) })) # TTX re-calculates this, but I'm giving it the right value anyway.
  28. maxp.append(Element("maxPoints", {'value': str(self.maxPoints) }))
  29. maxp.append(Element("maxContours", {'value': str(self.maxContours) }))
  30. maxp.append(Element("maxCompositePoints", {'value': str(self.maxCompositePoints) }))
  31. maxp.append(Element("maxCompositeContours", {'value': str(self.maxCompositeContours) }))
  32. maxp.append(Element("maxZones", {'value': str(self.maxZones) }))
  33. maxp.append(Element("maxTwilightPoints", {'value': str(self.maxTwilightPoints) }))
  34. maxp.append(Element("maxStorage", {'value': str(self.maxStorage) }))
  35. maxp.append(Element("maxFunctionDefs", {'value': str(self.maxFunctionDefs) }))
  36. maxp.append(Element("maxInstructionDefs", {'value': str(self.maxInstructionDefs) }))
  37. maxp.append(Element("maxStackElements", {'value': str(self.maxStackElements) }))
  38. maxp.append(Element("maxSizeOfInstructions", {'value': str(self.maxSizeOfInstructions) }))
  39. maxp.append(Element("maxComponentElements", {'value': str(self.maxComponentElements) }))
  40. maxp.append(Element("maxComponentDepth", {'value': str(self.maxComponentDepth) }))
  41. return maxp
  42. def toBytes(self):
  43. maxp = struct.pack( ">iHHHHHHHHHHHHHH"
  44. , int(self.version) # Fixed (Int32)
  45. , self.numGlyphs # UInt16
  46. , self.maxPoints # UInt16
  47. , self.maxContours # UInt16
  48. , self.maxCompositePoints # UInt16
  49. , self.maxCompositeContours # UInt16
  50. , self.maxZones # UInt16
  51. , self.maxTwilightPoints # UInt16
  52. , self.maxStorage # UInt16
  53. , self.maxFunctionDefs # UInt16
  54. , self.maxInstructionDefs # UInt16
  55. , self.maxStackElements # UInt16
  56. , self.maxSizeOfInstructions # UInt16
  57. , self.maxComponentElements # UInt16
  58. , self.maxComponentDepth # UInt16
  59. )
  60. return outputTableBytes(maxp)