1from __future__ import print_function, division, absolute_import
2from fontTools.misc.py23 import *
3from fontTools.pens.basePen import AbstractPen
4
5
6__all__ = ["TransformPen"]
7
8
9class TransformPen(AbstractPen):
10
11	"""Pen that transforms all coordinates using a Affine transformation,
12	and passes them to another pen.
13	"""
14
15	def __init__(self, outPen, transformation):
16		"""The 'outPen' argument is another pen object. It will receive the
17		transformed coordinates. The 'transformation' argument can either
18		be a six-tuple, or a fontTools.misc.transform.Transform object.
19		"""
20		if not hasattr(transformation, "transformPoint"):
21			from fontTools.misc.transform import Transform
22			transformation = Transform(*transformation)
23		self._transformation = transformation
24		self._transformPoint = transformation.transformPoint
25		self._outPen = outPen
26		self._stack = []
27
28	def moveTo(self, pt):
29		self._outPen.moveTo(self._transformPoint(pt))
30
31	def lineTo(self, pt):
32		self._outPen.lineTo(self._transformPoint(pt))
33
34	def curveTo(self, *points):
35		self._outPen.curveTo(*self._transformPoints(points))
36
37	def qCurveTo(self, *points):
38		if points[-1] is None:
39			points = self._transformPoints(points[:-1]) + [None]
40		else:
41			points = self._transformPoints(points)
42		self._outPen.qCurveTo(*points)
43
44	def _transformPoints(self, points):
45		new = []
46		transformPoint = self._transformPoint
47		for pt in points:
48			new.append(transformPoint(pt))
49		return new
50
51	def closePath(self):
52		self._outPen.closePath()
53
54	def addComponent(self, glyphName, transformation):
55		transformation = self._transformation.transform(transformation)
56		self._outPen.addComponent(glyphName, transformation)
57
58
59if __name__ == "__main__":
60	from fontTools.pens.basePen import _TestPen
61	pen = TransformPen(_TestPen(None), (2, 0, 0.5, 2, -10, 0))
62	pen.moveTo((0, 0))
63	pen.lineTo((0, 100))
64	pen.curveTo((50, 75), (60, 50), (50, 25), (0, 0))
65	pen.closePath()
66