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[source]
addComponent(glyphName: str, transformation: Tuple[float, float, float, float, float, float]) None[source]

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.

addVarComponent(glyphName: str, transformation: DecomposedTransform, location: Dict[str, float]) None[source]

Add a VarComponent sub glyph. The ‘transformation’ argument must be a DecomposedTransform from the fontTools.misc.transform module, and the ‘location’ argument must be a dictionary mapping axis tags to their locations.

closePath() None[source]

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

curveTo(*points: Tuple[float, float]) None[source]

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().

endPath() None[source]

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

lineTo(pt: Tuple[float, float]) None[source]

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

moveTo(pt: Tuple[float, float]) None[source]

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().

qCurveTo(*points: Tuple[float, float]) None[source]

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).

class fontTools.pens.basePen.BasePen(glyphSet=None)[source]

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

exception MissingComponentError

Indicates a component pointing to a non-existent glyph in the glyphset.

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

addComponent(glyphName, transformation)

Transform the points of the base glyph and draw it onto self.

addVarComponent(glyphName, transformation, location)

Add a VarComponent sub glyph. The ‘transformation’ argument must be a DecomposedTransform from the fontTools.misc.transform module, and the ‘location’ argument must be a dictionary mapping axis tags to their locations.

closePath()[source]

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

curveTo(*points)[source]

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().

endPath()[source]

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

lineTo(pt)[source]

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

property log
moveTo(pt)[source]

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().

qCurveTo(*points)[source]

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).

skipMissingComponents = True
class fontTools.pens.basePen.NullPen[source]

A pen that does nothing.

addComponent(glyphName, transformation)[source]

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.

addVarComponent(glyphName, transformation, location)[source]

Add a VarComponent sub glyph. The ‘transformation’ argument must be a DecomposedTransform from the fontTools.misc.transform module, and the ‘location’ argument must be a dictionary mapping axis tags to their locations.

closePath()[source]

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

curveTo(*points)[source]

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().

endPath()[source]

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

lineTo(pt)[source]

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

moveTo(pt)[source]

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().

qCurveTo(*points)[source]

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).

exception fontTools.pens.basePen.PenError[source]

Represents an error during penning.

args
with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

fontTools.pens.basePen.decomposeQuadraticSegment(points)[source]

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.

fontTools.pens.basePen.decomposeSuperBezierSegment(points)[source]

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.