- Transform objects and the Identity transform
- Methods of the Transform object
Matrix transformations in RoboFab and FontParts are done with the help of the matrix module from FontTools.
RGlyph all have
transform() methods which accept a matrix object.
Transform objects and the Identity transform
A Transform object from FontTools is a 2×2 transformation matrix plus offset, a.k.a. Affine transform. Transform instances are “immutable”: all transforming methods, eg.
rotate(), return a new
One way to make a new transform object is by importing the Identity transform from
fontTools.misc.transform. Identity is an instance of
Transform and it is initialised to transform to itself (i.e. no visible transformation). The you modify the Identity object: each method (such as
translate() etc) returns a new
Transform object with the last transformation added to it.
Oh no! Math!
Transform objects are a fast and powerful way to do math with matrices. If you don’t know what that means, don’t worry. If you follow the examples you can build Transform objects that rotate, translate and skew. Of course you can look up affine transform on Google for some more in-depth explanation of transformations.
Angles in all Python’s math modules are measured in radians by default. A radian is a (perhaps) more scientific unit for angles than the regular “degrees”, measuring the “distance travelled” over the circle rather than the angle. The length of a full circle (with radius 1) is 2 * pi. 90 degrees = 1/2 pi radians. One can easily be converted to the other using conversion methods from the math module:
import math # example of a conversion of degrees to radians print(math.radians(90)) # example of a conversion from radians to degrees print(math.degrees(math.pi))
import math from fontTools.misc.transform import Identity m = Identity print(m) m = m.rotate(math.radians(20)) print(m) m = tuple(m) g = CurrentGlyph() g.transformBy(m)
<Transform [1 0 0 1 0 0]> <Transform [0.939693 0.34202 -0.34202 0.939693 0 0]>
Methods of the Transform object
See the source code of
fontTools.misc.transform for detailed descriptions and examples.
- Return a new transformation, which is the other transformation transformed by
self.reverseTransform(other)is equivalent to
- Return a new transformation, rotated by
- skew(x, y)
- Return a new transformation, skewed by
- scale(x=1, y=None)
- Return a new transformation, scaled by
yargument may be
None, which implies to use the
- Return a string with the values of the transform written out in the PostScript manner:
[1 0 0 1 0 0].
- Return a new transformation, transformed by another transformation.
- Return the inverse transformation.
- transformPoint((x, y))
- Transform a point, i.e. apply the transformation to the point.
- Transform a list of points.
Adapted from the RoboFab documentation.