from __future__ import print_function, division, absolute_import
from fontTools.misc.py23 import *
from fontTools.pens.basePen import AbstractPen


class FilterPen(AbstractPen):

    """ Base class for pens that apply some transformation to the coordinates
    they receive and pass them to another pen.

    You can override any of its methods. The default implementation does
    nothing, but passes the commands unmodified to the other pen.

    >>> from fontTools.pens.recordingPen import RecordingPen
    >>> rec = RecordingPen()
    >>> pen = FilterPen(rec)
    >>> v = iter(rec.value)

    >>> pen.moveTo((0, 0))
    >>> next(v)
    ('moveTo', ((0, 0),))

    >>> pen.lineTo((1, 1))
    >>> next(v)
    ('lineTo', ((1, 1),))

    >>> pen.curveTo((2, 2), (3, 3), (4, 4))
    >>> next(v)
    ('curveTo', ((2, 2), (3, 3), (4, 4)))

    >>> pen.qCurveTo((5, 5), (6, 6), (7, 7), (8, 8))
    >>> next(v)
    ('qCurveTo', ((5, 5), (6, 6), (7, 7), (8, 8)))

    >>> pen.closePath()
    >>> next(v)
    ('closePath', ())

    >>> pen.moveTo((9, 9))
    >>> next(v)
    ('moveTo', ((9, 9),))

    >>> pen.endPath()
    >>> next(v)
    ('endPath', ())

    >>> pen.addComponent('foo', (1, 0, 0, 1, 0, 0))
    >>> next(v)
    ('addComponent', ('foo', (1, 0, 0, 1, 0, 0)))
    """

    def __init__(self, outPen):
        self._outPen = outPen

    def moveTo(self, pt):
        self._outPen.moveTo(pt)

    def lineTo(self, pt):
        self._outPen.lineTo(pt)

    def curveTo(self, *points):
        self._outPen.curveTo(*points)

    def qCurveTo(self, *points):
        self._outPen.qCurveTo(*points)

    def closePath(self):
        self._outPen.closePath()

    def endPath(self):
        self._outPen.endPath()

    def addComponent(self, glyphName, transformation):
        self._outPen.addComponent(glyphName, transformation)

@@ -143,26 +143,264 @@
 Pen%0A
-%0A%0Aclass FilterPen(
+from fontTools.pens.recordingPen import RecordingPen%0A%0A%0Aclass _PassThruComponentsMixin(object):%0A%0A    def addComponent(self, glyphName, transformation):%0A        self._outPen.addComponent(glyphName, transformation)%0A%0A%0Aclass FilterPen(_PassThruComponentsMixin, 
 Abst
@@ -1950,61 +1950,708 @@
 ()%0A%0A
-    def addComponent(self, glyphName, transformation)
+%0Aclass ContourFilterPen(_PassThruComponentsMixin, RecordingPen):%0A    %22%22%22A %22buffered%22 filter pen that accumulates contour data, passes%0A    it through a %60%60filterContour%60%60 method when the contour is closed or ended,%0A    and finally draws the result with the output pen.%0A    %22%22%22%0A%0A    def __init__(self, outPen):%0A        super(ContourFilterPen, self).__init__()%0A        self._outPen = outPen%0A%0A    def closePath(self):%0A        super(ContourFilterPen, self).closePath()%0A        self._flushContour()%0A%0A    def endPath(self):%0A        super(ContourFilterPen, self).endPath()%0A        self._flushContour()%0A%0A    def _flushContour(self):%0A        result = self.filterContour(self.value)%0A        if result is not None
 :%0A  
@@ -2660,57 +2660,759 @@
     
-self._outPen.addComponent(glyphName, transformation)
+    self.value = result%0A        self.replay(self._outPen)%0A        self.value = %5B%5D%0A%0A    def filterContour(self, contour):%0A        %22%22%22Subclasses must override this to perform the filtering.%0A%0A        The contour is a list of pen (operator, operands) tuples.%0A        Operators are strings corresponding to the AbstractPen methods:%0A        %22moveTo%22, %22lineTo%22, %22curveTo%22, %22qCurveTo%22, %22closePath%22,%0A        %22endPath%22, and %22addComponent%22. The operands are the positional%0A        arguments that are passed to each method.%0A%0A        If the method doesn't return a value (i.e. returns None), it's%0A        assumed that the argument was modified in-place.%0A        Otherwise, the return value is drawn with the output pen.%0A        %22%22%22%0A        return  # or return contour
 %0A

add ContourFilterPen