cbdt.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import struct
  2. from lxml.etree import Element
  3. from tables.common.ebxBitmaps import EBDTBitmapFormat17
  4. from transform.bytes import outputTableBytes
  5. class CBDTStrike:
  6. """
  7. A class representing a CBDT strike in a CBDT strike.
  8. """
  9. def __init__(self, glyphs, metrics, strikeRes):
  10. self.glyphs = []
  11. for g in glyphs["img"]: #img is used here because CBDT bitmaps are identified by glyph name.
  12. self.glyphs.append(EBDTBitmapFormat17(metrics, strikeRes, g))
  13. def toTTX(self, index):
  14. strikedata = Element("strikedata", {"index": str(index)})
  15. for g in self.glyphs:
  16. strikedata.append(g.toTTX())
  17. return strikedata
  18. class CBDT:
  19. """
  20. A class representing a CBDT table.
  21. """
  22. def __init__(self, m, glyphs):
  23. self.majorVersion = 3
  24. self.minorVersion = 0
  25. # hard-coded. the only version available right now.
  26. self.strikes = []
  27. # iterate over each strike.
  28. strikeIndex = 0
  29. for imageFormat, image in glyphs["img"][0].imgDict.items():
  30. if imageFormat.split('-')[0] == "png":
  31. strikeRes = imageFormat.split('-')[1]
  32. self.strikes.append(CBDTStrike(glyphs, m["metrics"], strikeRes))
  33. strikeIndex += 1
  34. def toTTX(self):
  35. cbdt = Element("CBDT")
  36. cbdt.append(Element("header", {"version": f"{self.majorVersion}.{self.minorVersion}"}))
  37. strikeIndex = 0
  38. for strike in self.strikes:
  39. cbdt.append(strike.toTTX(strikeIndex))
  40. strikeIndex += 1
  41. return cbdt
  42. def toBytes(self):
  43. cbdt = struct.pack( ">HH"
  44. , self.majorVersion # UInt16
  45. , self.minorVersion # UInt16
  46. )
  47. return outputTableBytes(cbdt) # placeholder
  48. # TODO: pack all of the image data immediately after~