2013-11-27 15:16:28 -05:00
|
|
|
from fontTools.misc.py23 import *
|
2017-03-23 15:35:13 +00:00
|
|
|
from fontTools.pens.filterPen import FilterPen
|
2003-08-23 20:19:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
__all__ = ["TransformPen"]
|
|
|
|
|
|
|
|
|
2017-03-23 15:35:13 +00:00
|
|
|
class TransformPen(FilterPen):
|
2003-08-23 20:19:33 +00:00
|
|
|
|
2003-09-16 10:14:48 +00:00
|
|
|
"""Pen that transforms all coordinates using a Affine transformation,
|
|
|
|
and passes them to another pen.
|
2003-08-23 20:19:33 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, outPen, transformation):
|
2003-09-16 10:14:48 +00:00
|
|
|
"""The 'outPen' argument is another pen object. It will receive the
|
|
|
|
transformed coordinates. The 'transformation' argument can either
|
|
|
|
be a six-tuple, or a fontTools.misc.transform.Transform object.
|
|
|
|
"""
|
2017-03-23 15:35:13 +00:00
|
|
|
super(TransformPen, self).__init__(outPen)
|
2003-08-23 20:19:33 +00:00
|
|
|
if not hasattr(transformation, "transformPoint"):
|
|
|
|
from fontTools.misc.transform import Transform
|
|
|
|
transformation = Transform(*transformation)
|
|
|
|
self._transformation = transformation
|
|
|
|
self._transformPoint = transformation.transformPoint
|
|
|
|
self._stack = []
|
|
|
|
|
|
|
|
def moveTo(self, pt):
|
|
|
|
self._outPen.moveTo(self._transformPoint(pt))
|
|
|
|
|
|
|
|
def lineTo(self, pt):
|
2003-08-24 09:48:49 +00:00
|
|
|
self._outPen.lineTo(self._transformPoint(pt))
|
2003-08-23 20:19:33 +00:00
|
|
|
|
|
|
|
def curveTo(self, *points):
|
|
|
|
self._outPen.curveTo(*self._transformPoints(points))
|
|
|
|
|
|
|
|
def qCurveTo(self, *points):
|
2003-09-09 23:29:45 +00:00
|
|
|
if points[-1] is None:
|
|
|
|
points = self._transformPoints(points[:-1]) + [None]
|
|
|
|
else:
|
|
|
|
points = self._transformPoints(points)
|
|
|
|
self._outPen.qCurveTo(*points)
|
2003-08-23 20:19:33 +00:00
|
|
|
|
|
|
|
def _transformPoints(self, points):
|
|
|
|
transformPoint = self._transformPoint
|
2017-02-20 19:57:29 -06:00
|
|
|
return [transformPoint(pt) for pt in points]
|
2003-08-23 20:19:33 +00:00
|
|
|
|
|
|
|
def closePath(self):
|
|
|
|
self._outPen.closePath()
|
|
|
|
|
2015-11-06 11:25:48 -08:00
|
|
|
def endPath(self):
|
|
|
|
self._outPen.endPath()
|
|
|
|
|
2003-08-23 20:19:33 +00:00
|
|
|
def addComponent(self, glyphName, transformation):
|
|
|
|
transformation = self._transformation.transform(transformation)
|
|
|
|
self._outPen.addComponent(glyphName, transformation)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
from fontTools.pens.basePen import _TestPen
|
|
|
|
pen = TransformPen(_TestPen(None), (2, 0, 0.5, 2, -10, 0))
|
|
|
|
pen.moveTo((0, 0))
|
|
|
|
pen.lineTo((0, 100))
|
|
|
|
pen.curveTo((50, 75), (60, 50), (50, 25), (0, 0))
|
|
|
|
pen.closePath()
|