boundsPen: add 'ignoreSinglePoints' argument to do like robofab.pens.boundsPen

if `ignoreSinglePoints` is True, single points are excluded from the computation of the bounds.
The default value (False) keeps the current behaviour (i.e., single points are not ignored).

https://github.com/robofab-developers/robofab/blob/master/Lib/robofab/pens/boundsPen.py

See discussion at: https://github.com/typemytype/booleanOperations/pull/20
This commit is contained in:
Cosimo Lupo 2016-01-07 15:47:31 +00:00
parent 4daf0601b2
commit dc2ef2e341

View File

@ -17,25 +17,39 @@ class ControlBoundsPen(BasePen):
When the shape has been drawn, the bounds are available as the When the shape has been drawn, the bounds are available as the
'bounds' attribute of the pen object. It's a 4-tuple: 'bounds' attribute of the pen object. It's a 4-tuple:
(xMin, yMin, xMax, yMax) (xMin, yMin, xMax, yMax).
If 'ignoreSinglePoints' is True, single points are ignored.
""" """
def __init__(self, glyphSet): def __init__(self, glyphSet, ignoreSinglePoints=False):
BasePen.__init__(self, glyphSet) BasePen.__init__(self, glyphSet)
self.ignoreSinglePoints = ignoreSinglePoints
self.bounds = None self.bounds = None
self._start = None
def _moveTo(self, pt): def _moveTo(self, pt):
self._start = pt
if not self.ignoreSinglePoints:
self._addMoveTo()
def _addMoveTo(self):
if self._start is None:
return
bounds = self.bounds bounds = self.bounds
if bounds: if bounds:
self.bounds = updateBounds(bounds, pt) self.bounds = updateBounds(bounds, self._start)
else: else:
x, y = pt x, y = self._start
self.bounds = (x, y, x, y) self.bounds = (x, y, x, y)
self._start = None
def _lineTo(self, pt): def _lineTo(self, pt):
self._addMoveTo()
self.bounds = updateBounds(self.bounds, pt) self.bounds = updateBounds(self.bounds, pt)
def _curveToOne(self, bcp1, bcp2, pt): def _curveToOne(self, bcp1, bcp2, pt):
self._addMoveTo()
bounds = self.bounds bounds = self.bounds
bounds = updateBounds(bounds, bcp1) bounds = updateBounds(bounds, bcp1)
bounds = updateBounds(bounds, bcp2) bounds = updateBounds(bounds, bcp2)
@ -43,6 +57,7 @@ class ControlBoundsPen(BasePen):
self.bounds = bounds self.bounds = bounds
def _qCurveToOne(self, bcp, pt): def _qCurveToOne(self, bcp, pt):
self._addMoveTo()
bounds = self.bounds bounds = self.bounds
bounds = updateBounds(bounds, bcp) bounds = updateBounds(bounds, bcp)
bounds = updateBounds(bounds, pt) bounds = updateBounds(bounds, pt)
@ -62,6 +77,7 @@ class BoundsPen(ControlBoundsPen):
""" """
def _curveToOne(self, bcp1, bcp2, pt): def _curveToOne(self, bcp1, bcp2, pt):
self._addMoveTo()
bounds = self.bounds bounds = self.bounds
bounds = updateBounds(bounds, pt) bounds = updateBounds(bounds, pt)
if not pointInRect(bcp1, bounds) or not pointInRect(bcp2, bounds): if not pointInRect(bcp1, bounds) or not pointInRect(bcp2, bounds):
@ -70,6 +86,7 @@ class BoundsPen(ControlBoundsPen):
self.bounds = bounds self.bounds = bounds
def _qCurveToOne(self, bcp, pt): def _qCurveToOne(self, bcp, pt):
self._addMoveTo()
bounds = self.bounds bounds = self.bounds
bounds = updateBounds(bounds, pt) bounds = updateBounds(bounds, pt)
if not pointInRect(bcp, bounds): if not pointInRect(bcp, bounds):