transform¶
Affine 2D transformation matrix class.
The Transform class implements various transformation matrix operations, both on the matrix itself, as well as on 2D coordinates.
Transform instances are effectively immutable: all methods that operate on the transformation itself always return a new instance. This has as the interesting side effect that Transform instances are hashable, ie. they can be used as dictionary keys.
This module exports the following symbols:
Transform – this is the main class Identity – Transform instance set to the identity transformation Offset – Convenience function that returns a translating transformation Scale – Convenience function that returns a scaling transformation
Examples:
>>> t = Transform(2, 0, 0, 3, 0, 0)
>>> t.transformPoint((100, 100))
(200, 300)
>>> t = Scale(2, 3)
>>> t.transformPoint((100, 100))
(200, 300)
>>> t.transformPoint((0, 0))
(0, 0)
>>> t = Offset(2, 3)
>>> t.transformPoint((100, 100))
(102, 103)
>>> t.transformPoint((0, 0))
(2, 3)
>>> t2 = t.scale(0.5)
>>> t2.transformPoint((100, 100))
(52.0, 53.0)
>>> import math
>>> t3 = t2.rotate(math.pi / 2)
>>> t3.transformPoint((0, 0))
(2.0, 3.0)
>>> t3.transformPoint((100, 100))
(-48.0, 53.0)
>>> t = Identity.scale(0.5).translate(100, 200).skew(0.1, 0.2)
>>> t.transformPoints([(0, 0), (1, 1), (100, 100)])
[(50.0, 100.0), (50.550167336042726, 100.60135501775433), (105.01673360427253, 160.13550177543362)]
>>>
-
class
fontTools.misc.transform.
Transform
(xx=1, xy=0, yx=0, yy=1, dx=0, dy=0)[source]¶ 2x2 transformation matrix plus offset, a.k.a. Affine transform. Transform instances are immutable: all transforming methods, eg. rotate(), return a new Transform instance.
- Examples:
>>> t = Transform() >>> t <Transform [1 0 0 1 0 0]> >>> t.scale(2) <Transform [2 0 0 2 0 0]> >>> t.scale(2.5, 5.5) <Transform [2.5 0 0 5.5 0 0]> >>> >>> t.scale(2, 3).transformPoint((100, 100)) (200, 300)
-
inverse
()[source]¶ Return the inverse transformation.
- Example:
>>> t = Identity.translate(2, 3).scale(4, 5) >>> t.transformPoint((10, 20)) (42, 103) >>> it = t.inverse() >>> it.transformPoint((42, 103)) (10.0, 20.0) >>>
-
reverseTransform
(other)[source]¶ Return a new transformation, which is the other transformation transformed by self. self.reverseTransform(other) is equivalent to other.transform(self).
- Example:
>>> t = Transform(2, 0, 0, 3, 1, 6) >>> t.reverseTransform((4, 3, 2, 1, 5, 6)) <Transform [8 6 6 3 21 15]> >>> Transform(4, 3, 2, 1, 5, 6).transform((2, 0, 0, 3, 1, 6)) <Transform [8 6 6 3 21 15]> >>>
-
rotate
(angle)[source]¶ Return a new transformation, rotated by ‘angle’ (radians).
- Example:
>>> import math >>> t = Transform() >>> t.rotate(math.pi / 2) <Transform [0 1 -1 0 0 0]> >>>
-
scale
(x=1, y=None)[source]¶ Return a new transformation, scaled by x, y. The ‘y’ argument may be None, which implies to use the x value for y as well.
- Example:
>>> t = Transform() >>> t.scale(5) <Transform [5 0 0 5 0 0]> >>> t.scale(5, 6) <Transform [5 0 0 6 0 0]> >>>
-
skew
(x=0, y=0)[source]¶ Return a new transformation, skewed by x and y.
- Example:
>>> import math >>> t = Transform() >>> t.skew(math.pi / 4) <Transform [1 0 1 1 0 0]> >>>
-
toPS
()[source]¶ Return a PostScript representation: >>> t = Identity.scale(2, 3).translate(4, 5) >>> t.toPS() ‘[2 0 0 3 8 15]’ >>>
-
transform
(other)[source]¶ Return a new transformation, transformed by another transformation.
- Example:
>>> t = Transform(2, 0, 0, 3, 1, 6) >>> t.transform((4, 3, 2, 1, 5, 6)) <Transform [8 9 4 3 11 24]> >>>
-
transformPoint
(p)[source]¶ Transform a point.
- Example:
>>> t = Transform() >>> t = t.scale(2.5, 5.5) >>> t.transformPoint((100, 100)) (250.0, 550.0)