2016-06-09 22:49:38 +01:00
|
|
|
"""Calculate the area of a glyph."""
|
|
|
|
|
2016-06-09 23:03:50 +01:00
|
|
|
from fontTools.misc.py23 import *
|
2016-06-09 22:49:38 +01:00
|
|
|
from fontTools.pens.basePen import BasePen
|
|
|
|
|
|
|
|
|
2017-02-20 20:01:06 -06:00
|
|
|
__all__ = ["AreaPen"]
|
|
|
|
|
|
|
|
|
2016-06-09 22:49:38 +01:00
|
|
|
class AreaPen(BasePen):
|
|
|
|
|
2016-06-12 03:13:32 +01:00
|
|
|
def __init__(self, glyphset=None):
|
2016-06-09 23:28:06 +01:00
|
|
|
BasePen.__init__(self, glyphset)
|
|
|
|
self.value = 0
|
2016-06-09 22:49:38 +01:00
|
|
|
|
2016-06-09 23:28:06 +01:00
|
|
|
def _moveTo(self, p0):
|
2016-06-15 14:10:44 +04:00
|
|
|
self._p0 = self._startPoint = p0
|
2016-06-09 22:49:38 +01:00
|
|
|
|
2016-06-09 23:28:06 +01:00
|
|
|
def _lineTo(self, p1):
|
2016-06-15 14:10:44 +04:00
|
|
|
x0, y0 = self._p0
|
|
|
|
x1, y1 = p1
|
|
|
|
self.value -= (x1 - x0) * (y1 + y0) * .5
|
|
|
|
self._p0 = p1
|
2016-06-09 23:12:11 +01:00
|
|
|
|
2016-06-09 23:33:36 +01:00
|
|
|
def _qCurveToOne(self, p1, p2):
|
2017-02-20 17:23:59 -06:00
|
|
|
# https://github.com/Pomax/bezierinfo/issues/44
|
2016-06-15 14:10:44 +04:00
|
|
|
p0 = self._p0
|
|
|
|
x0, y0 = p0[0], p0[1]
|
|
|
|
x1, y1 = p1[0] - x0, p1[1] - y0
|
|
|
|
x2, y2 = p2[0] - x0, p2[1] - y0
|
|
|
|
self.value -= (x2 * y1 - x1 * y2) / 3
|
|
|
|
self._lineTo(p2)
|
|
|
|
self._p0 = p2
|
|
|
|
|
|
|
|
def _curveToOne(self, p1, p2, p3):
|
2017-02-20 17:23:59 -06:00
|
|
|
# https://github.com/Pomax/bezierinfo/issues/44
|
2016-06-15 14:10:44 +04:00
|
|
|
p0 = self._p0
|
|
|
|
x0, y0 = p0[0], p0[1]
|
|
|
|
x1, y1 = p1[0] - x0, p1[1] - y0
|
|
|
|
x2, y2 = p2[0] - x0, p2[1] - y0
|
|
|
|
x3, y3 = p3[0] - x0, p3[1] - y0
|
|
|
|
self.value -= (
|
|
|
|
x1 * ( - y2 - y3) +
|
|
|
|
x2 * (y1 - 2*y3) +
|
|
|
|
x3 * (y1 + 2*y2 )
|
|
|
|
) * 0.15
|
|
|
|
self._lineTo(p3)
|
|
|
|
self._p0 = p3
|
2016-06-12 03:13:32 +01:00
|
|
|
|
|
|
|
def _closePath(self):
|
2016-06-15 14:10:44 +04:00
|
|
|
self._lineTo(self._startPoint)
|
|
|
|
del self._p0, self._startPoint
|
|
|
|
|
|
|
|
def _endPath(self):
|
2016-10-04 17:32:49 -07:00
|
|
|
if self._p0 != self._startPoint:
|
2017-02-20 17:23:59 -06:00
|
|
|
# Area is not defined for open contours.
|
2016-10-04 16:10:46 -07:00
|
|
|
raise NotImplementedError
|
|
|
|
del self._p0, self._startPoint
|