areaPen.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. """Calculate the area of a glyph."""
  2. from fontTools.pens.basePen import BasePen
  3. __all__ = ["AreaPen"]
  4. class AreaPen(BasePen):
  5. def __init__(self, glyphset=None):
  6. BasePen.__init__(self, glyphset)
  7. self.value = 0
  8. def _moveTo(self, p0):
  9. self._p0 = self._startPoint = p0
  10. def _lineTo(self, p1):
  11. x0, y0 = self._p0
  12. x1, y1 = p1
  13. self.value -= (x1 - x0) * (y1 + y0) * 0.5
  14. self._p0 = p1
  15. def _qCurveToOne(self, p1, p2):
  16. # https://github.com/Pomax/bezierinfo/issues/44
  17. p0 = self._p0
  18. x0, y0 = p0[0], p0[1]
  19. x1, y1 = p1[0] - x0, p1[1] - y0
  20. x2, y2 = p2[0] - x0, p2[1] - y0
  21. self.value -= (x2 * y1 - x1 * y2) / 3
  22. self._lineTo(p2)
  23. self._p0 = p2
  24. def _curveToOne(self, p1, p2, p3):
  25. # https://github.com/Pomax/bezierinfo/issues/44
  26. p0 = self._p0
  27. x0, y0 = p0[0], p0[1]
  28. x1, y1 = p1[0] - x0, p1[1] - y0
  29. x2, y2 = p2[0] - x0, p2[1] - y0
  30. x3, y3 = p3[0] - x0, p3[1] - y0
  31. self.value -= (x1 * (-y2 - y3) + x2 * (y1 - 2 * y3) + x3 * (y1 + 2 * y2)) * 0.15
  32. self._lineTo(p3)
  33. self._p0 = p3
  34. def _closePath(self):
  35. self._lineTo(self._startPoint)
  36. del self._p0, self._startPoint
  37. def _endPath(self):
  38. if self._p0 != self._startPoint:
  39. # Area is not defined for open contours.
  40. raise NotImplementedError
  41. del self._p0, self._startPoint