• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from __future__ import print_function, division, absolute_import
2from fontTools.misc.py23 import *
3from fontTools.pens.filterPen import FilterPen
4
5
6__all__ = ["TransformPen"]
7
8
9class TransformPen(FilterPen):
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		super(TransformPen, self).__init__(outPen)
21		if not hasattr(transformation, "transformPoint"):
22			from fontTools.misc.transform import Transform
23			transformation = Transform(*transformation)
24		self._transformation = transformation
25		self._transformPoint = transformation.transformPoint
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		transformPoint = self._transformPoint
46		return [transformPoint(pt) for pt in points]
47
48	def closePath(self):
49		self._outPen.closePath()
50
51	def endPath(self):
52		self._outPen.endPath()
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