Using transformations

Matrix transformations in RoboFab are done with the help of the matrix module from fontTools. RContour, RPoint, RbPoint, RGlyph all have transform() methods which accept a matrix object.

Transform objects and the Identity transform

A Transform object from fontTools is a 2x2 transformation matrix plus offset, a.k.a. Affine transform. Transform instances are "immutable": all transforming methods, eg. rotate(), return a new Transform instance. 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 rotate(), 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

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)

Example

# robofab manual
#     Usetransformations howto
#    usage examples


from fontTools.misc.transform import Identity
from robofab.world import CurrentFont

m = Identity
print m

m = m.rotate(math.radians(20))
print m

f = CurrentFont()
for c in f:
    c.transform(m)
    c.update()
➔ Download this.
< Transform [1 0 0 1 0 0] >
< Transform [0.939692620786 0.342020143326
    -0.342020143326 0.939692620786 0 0] >
   
# whammo

Methods of Transform object

See the source code of fontTools.misc.transform for detailed descriptions and examples.