glyf.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from lxml.etree import Element, ElementTree
  2. from transform.bytes import outputTableBytes
  3. class glyf:
  4. """
  5. Class representing a glyf table.
  6. (this currently doesn't accurately represent a glyf table in bytes)
  7. """
  8. def __init__(self, m, glyphs):
  9. self.glyphs = []
  10. self.xMin = m['metrics']['xMin']
  11. self.yMin = m['metrics']['yMin']
  12. self.xMax = m['metrics']['xMax']
  13. self.yMax = m['metrics']['yMax']
  14. for g in glyphs["img_empty"]:
  15. self.glyphs.append(g)
  16. def toTTX(self):
  17. glyf = Element("glyf")
  18. for g in self.glyphs:
  19. # if it's not a whitespace character or a service glyph....
  20. if g.glyphType is "empty":
  21. glyf.append(Element("TTGlyph", {"name": g.name() }))
  22. # if it's not one of these, it needs some dummy glyf contours
  23. else:
  24. # These attributes will be calculated by the TTX compiler,
  25. # but I'm doing them manually anyway.
  26. dummyData = Element("TTGlyph", {"name": g.name()
  27. ,"xMin": str(self.xMin)
  28. ,"xMax": str(self.xMax)
  29. ,"yMin": str(self.yMin)
  30. ,"yMax": str(self.yMax)
  31. })
  32. # these dummy contours are designed to establish a bounding box to trick
  33. # the TTX compiler to making sure the head.xMin/head.yMin/etc. parameters
  34. # of the font are set to what we want them to actually be set to.
  35. #
  36. # (This is because TTX will overwrite the user's head.xMin etc. parameters
  37. # if what's in the glyf table doesn't agree.)
  38. dummyContour1 = Element("contour")
  39. dummyContour1.append(Element("pt", {"x": str(self.xMin), "y": str(self.yMin), "on": "1"}))
  40. dummyData.append(dummyContour1)
  41. dummyContour2 = Element("contour")
  42. dummyContour2.append(Element("pt", {"x": str(self.xMax), "y": str(self.yMax), "on": "1"}))
  43. dummyData.append(dummyContour2)
  44. # this is important, even though I don't know what it's for
  45. dummyData.append(Element("instructions"))
  46. glyf.append(dummyData)
  47. return glyf
  48. def toBytes(self):
  49. return outputTableBytes(b'\0') # placeholder
  50. # TODO: learn how to convert glyf to bytes.