basePen

fontTools.pens.basePen.py – Tools and base classes to build pen objects.

The Pen Protocol

A Pen is a kind of object that standardizes the way how to “draw” outlines: it is a middle man between an outline and a drawing. In other words: it is an abstraction for drawing outlines, making sure that outline objects don’t need to know the details about how and where they’re being drawn, and that drawings don’t need to know the details of how outlines are stored.

The most basic pattern is this:

outline.draw(pen) # ‘outline’ draws itself onto ‘pen’

Pens can be used to render outlines to the screen, but also to construct new outlines. Eg. an outline object can be both a drawable object (it has a draw() method) as well as a pen itself: you build an outline using pen methods.

The AbstractPen class defines the Pen protocol. It implements almost nothing (only no-op closePath() and endPath() methods), but is useful for documentation purposes. Subclassing it basically tells the reader: “this class implements the Pen protocol.”. An examples of an AbstractPen subclass is fontTools.pens.transformPen.TransformPen.

The BasePen class is a base implementation useful for pens that actually draw (for example a pen renders outlines using a native graphics engine). BasePen contains a lot of base functionality, making it very easy to build a pen that fully conforms to the pen protocol. Note that if you subclass BasePen, you _don’t_ override moveTo(), lineTo(), etc., but _moveTo(), _lineTo(), etc. See the BasePen doc string for details. Examples of BasePen subclasses are fontTools.pens.boundsPen.BoundsPen and fontTools.pens.cocoaPen.CocoaPen.

Coordinates are usually expressed as (x, y) tuples, but generally any sequence of length 2 will do.

class fontTools.pens.basePen.AbstractPen

Bases: object

moveTo(pt)

Begin a new sub path, set the current point to ‘pt’. You must end each sub path with a call to pen.closePath() or pen.endPath().

lineTo(pt)

Draw a straight line from the current point to ‘pt’.

curveTo(*points)

Draw a cubic bezier with an arbitrary number of control points.

The last point specified is on-curve, all others are off-curve (control) points. If the number of control points is > 2, the segment is split into multiple bezier segments. This works like this:

Let n be the number of control points (which is the number of arguments to this call minus 1). If n==2, a plain vanilla cubic bezier is drawn. If n==1, we fall back to a quadratic segment and if n==0 we draw a straight line. It gets interesting when n>2: n-1 PostScript-style cubic segments will be drawn as if it were one curve. See decomposeSuperBezierSegment().

The conversion algorithm used for n>2 is inspired by NURB splines, and is conceptually equivalent to the TrueType “implied points” principle. See also decomposeQuadraticSegment().

qCurveTo(*points)

Draw a whole string of quadratic curve segments.

The last point specified is on-curve, all others are off-curve points.

This method implements TrueType-style curves, breaking up curves using ‘implied points’: between each two consequtive off-curve points, there is one implied point exactly in the middle between them. See also decomposeQuadraticSegment().

The last argument (normally the on-curve point) may be None. This is to support contours that have NO on-curve points (a rarely seen feature of TrueType outlines).

closePath()

Close the current sub path. You must call either pen.closePath() or pen.endPath() after each sub path.

endPath()

End the current sub path, but don’t close it. You must call either pen.closePath() or pen.endPath() after each sub path.

addComponent(glyphName, transformation)

Add a sub glyph. The ‘transformation’ argument must be a 6-tuple containing an affine transformation, or a Transform object from the fontTools.misc.transform module. More precisely: it should be a sequence containing 6 numbers.

class fontTools.pens.basePen.BasePen(glyphSet)

Bases: fontTools.pens.basePen.AbstractPen

Base class for drawing pens. You must override _moveTo, _lineTo and _curveToOne. You may additionally override _closePath, _endPath, addComponent and/or _qCurveToOne. You should not override any other methods.

_moveTo(pt)
_lineTo(pt)
_curveToOne(pt1, pt2, pt3)
_closePath()
_endPath()
_qCurveToOne(pt1, pt2)

This method implements the basic quadratic curve type. The default implementation delegates the work to the cubic curve function. Optionally override with a native implementation.

addComponent(glyphName, transformation)

This default implementation simply transforms the points of the base glyph and draws it onto self.

_getCurrentPoint()

Return the current point. This is not part of the public interface, yet is useful for subclasses.

closePath()
endPath()
moveTo(pt)
lineTo(pt)
curveTo(*points)
qCurveTo(*points)
fontTools.pens.basePen.decomposeSuperBezierSegment(points)

Split the SuperBezier described by ‘points’ into a list of regular bezier segments. The ‘points’ argument must be a sequence with length 3 or greater, containing (x, y) coordinates. The last point is the destination on-curve point, the rest of the points are off-curve points. The start point should not be supplied.

This function returns a list of (pt1, pt2, pt3) tuples, which each specify a regular curveto-style bezier segment.

fontTools.pens.basePen.decomposeQuadraticSegment(points)

Split the quadratic curve segment described by ‘points’ into a list of “atomic” quadratic segments. The ‘points’ argument must be a sequence with length 2 or greater, containing (x, y) coordinates. The last point is the destination on-curve point, the rest of the points are off-curve points. The start point should not be supplied.

This function returns a list of (pt1, pt2) tuples, which each specify a plain quadratic bezier segment.