From 107bf48ffe9b269b52d73327f364874e7ceb58e8 Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Thu, 23 Mar 2017 15:16:20 +0000 Subject: [PATCH 1/2] [filterPen] add FilterPen base class Can be useful for others, e.g.: https://github.com/googlei18n/ufo2ft/issues/120#issuecomment-288503784 --- Lib/fontTools/pens/filterPen.py | 74 +++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 Lib/fontTools/pens/filterPen.py diff --git a/Lib/fontTools/pens/filterPen.py b/Lib/fontTools/pens/filterPen.py new file mode 100644 index 000000000..4b6ff8f45 --- /dev/null +++ b/Lib/fontTools/pens/filterPen.py @@ -0,0 +1,74 @@ +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) From 980ac7d4158697a8fa47e907c4552eea0ca5e28a Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Thu, 23 Mar 2017 15:35:13 +0000 Subject: [PATCH 2/2] [transformPen] use FilterPen as the base class instead of AbstractPen --- Lib/fontTools/pens/transformPen.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/fontTools/pens/transformPen.py b/Lib/fontTools/pens/transformPen.py index b8a2093a9..8f4fcd08f 100644 --- a/Lib/fontTools/pens/transformPen.py +++ b/Lib/fontTools/pens/transformPen.py @@ -1,12 +1,12 @@ from __future__ import print_function, division, absolute_import from fontTools.misc.py23 import * -from fontTools.pens.basePen import AbstractPen +from fontTools.pens.filterPen import FilterPen __all__ = ["TransformPen"] -class TransformPen(AbstractPen): +class TransformPen(FilterPen): """Pen that transforms all coordinates using a Affine transformation, and passes them to another pen. @@ -17,12 +17,12 @@ class TransformPen(AbstractPen): transformed coordinates. The 'transformation' argument can either be a six-tuple, or a fontTools.misc.transform.Transform object. """ + super(TransformPen, self).__init__(outPen) if not hasattr(transformation, "transformPoint"): from fontTools.misc.transform import Transform transformation = Transform(*transformation) self._transformation = transformation self._transformPoint = transformation.transformPoint - self._outPen = outPen self._stack = [] def moveTo(self, pt):