this_file: README.md
fontlab-python-pens
The fontTools Pen and PointPen protocol for FontLab 9, with no dependencies.
For the selected APIs listed below, protocol-compatible callers use the same
class names and calls as fontTools.pens, with parity tests comparing recordings.
This package does not implement every fontTools pen API. Inside FontLab you do
not have to install fontTools to draw, measure, transform or reverse outlines. The native pens for
FontGate and fontlab layers import the native modules lazily, so
import fontlab_pens works anywhere.
Install
The package is not on PyPI. Install a tagged release from GitHub:
uv pip install "fontlab-python-pens @ git+https://github.com/fontlabcom/fontlab-python-pens@v1.0.3"
To use it inside FontLab 9, open install.vfpy from a checkout in FontLab
(double-click it or drop it onto FontLab), then restart FontLab. The file is a
short stub that hands the work to
fontlab-instamix; if
instamix is not installed yet, the stub installs it from PyPI first. Instamix
installs the package into FontLab's own site-packages and records the install
under the extension id com.fontlab.pens.
Python 3.11 or later. Runtime dependencies: none.
Use
The examples and Scripts require FontLab Fly (fontlab-fly>=1.3.0) with its fly.pens
alias for this package and fly.fl for the native FontLab API. Install the
Fly 1.3.0 or later as well; older Fly releases without fly.pens cannot run
these examples. The Pens library itself retains zero runtime dependencies.
from fly.pens import BoundsPen, RecordingPen, TransformPen
rec = RecordingPen()
pen = TransformPen(rec, (2, 0, 0, 2, 10, 0))
pen.moveTo((0, 0))
pen.curveTo((0, 50), (50, 100), (100, 100))
pen.closePath()
rec.value
# [('moveTo', ((10, 0),)), ('curveTo', ((10, 100), (110, 200), (210, 200))),
# ('closePath', ())]
bounds = BoundsPen(None)
rec.replay(bounds)
bounds.bounds # (10, 0, 210, 200)
Native pens: layers and elements
In FontLab a glyph holds layers, and each layer holds elements (shapes). The
native pens and readers take a layer or an element. If you pass a glyph, they
raise TypeError. To pick a layer explicitly, use layer_of(glyph, master).
from fly.pens import FlPointPen, RecordingPointPen, layer_of
from fly.pens.fontlab import draw_points
layer = layer_of(glyph, "Regular") # an flGlyph's flLayer
with FlPointPen(layer, notify="Draw") as pen:
pen.beginPath()
pen.addPoint((0, 0), "line")
pen.addPoint((0, 100), "line")
pen.addPoint((100, 100), "line")
pen.endPath() # one new element on close
rec = RecordingPointPen()
draw_points(layer, rec) # components reported via addComponent
| Module | Writers | Readers |
|---|---|---|
fontgate |
FontGatePointPen, FontGatePen (fgLayer/fgShape) |
draw_points, draw, draw_contour_points, edit_geometry_ready |
fontlab |
FlPointPen, FlPen (flLayer/flShape) |
draw_points, draw |
How the native pens behave:
- Writing to a layer adds one new element for the pen session. Writing to an element appends contours to that element.
- Coordinates are mapped through the inverse of the target's matrix, so reading the same target returns what you wrote.
- Identifiers raise
PenError, because FontLab stores none. - History is opt-in:
notify="Draw"requires an attachedflLayerand initializes the layer and glyph history before writing, then notifies once. A bare element withnotifyis refused; use its explicitly known layer. Nonempty writes to a direct automatic layer are refused, even without notify. - Components:
flLayeradds a live component element.- An element target raises
ComponentError. - Every FontGate target raises
ComponentError, because FontGate components are glyph-level only.
The fast path over the native fgPen is not implemented. That pen binds to a
glyph's work shape, which conflicts with the layer/element rule.
Strokes
StrokePlan appends source open contours to named layers with one notification.
StrokeStyle validates parameters; unsupported fields raise UnsupportedStroke before mutation.
from fly.pens import StrokePlan, StrokeStyle, apply_plan
source = glyph.activeLayer.name
styles = {source: StrokeStyle(thickness=20)}
plan = StrokePlan(styles)
counts = apply_plan(glyph, plan, source=source)
# counts maps each target layer name to its new open-contour count.
Full strokes and expansion need upstream penParameters / expandStroke. apply_stroke writes the join as an integer, because FontLab misreads join names (fl#8630). It also writes both caps explicitly, applies the native parameters before the QPen so a refusal leaves the shape unchanged, and expand_stroke raises PenError when FontLab reports a failed expansion.
Variants retain rotation; native persistence remains unresolved.
Percentage rounding loses finer precision. Live Undo/Redo and save/reopen acceptance remain open.
FontLab notes
- A native commit can leave partial geometry if a later write or notification fails. The exception propagates and the pen stays spent; repeating the drawing with another pen can duplicate geometry. A drawing-body exception commits nothing.
- With default
notify=None, verified layer appends preserve existing raw element payloads. Cachedglyph.fgGlyphsnapshots and expanded dependent component views may stay stale until native reconstruction. Fresh per-layer FontGate conversion reflects current FontLab shapes without refreshing dependent views. - History initialization makes the first Undo after a notified write restore the previous geometry. It does not make whole-font history safe: FontLab's notification can change the timestamps of unrelated glyphs, and history is scoped to the glyph window.
- FontLab's smooth property setter can move neighboring handles. FlPointPen writes the named raw Smooth flag after contour construction to keep supplied coordinates unchanged. Advanced G2/Hobby and raw control-node flags are not represented by the point protocol's boolean smooth value.
- A closed quadratic contour whose first point is an on-curve
qcurvewith no preceding off-curves reads back asline. The geometry is identical; FontLab stores both as "on". - To move a FontGate-pen result into the live font, wrap the element:
layer.addShape(fly.fl.flShape(fg_shape))(afterimport fly.fl).flGlyph.fgGlyphis a detached copy, andflLayer.assignFgLayercurrently raisesIndexError: Unexpected SeqBase<T> length.
What is included
| Module | Names |
|---|---|
base |
AbstractPen, NullPen, BasePen, DecomposingPen, PenError, OpenContourError, MissingComponentError, decomposeSuperBezierSegment, decomposeQuadraticSegment |
point |
AbstractPointPen, BasePointToSegmentPen, PointToSegmentPen, SegmentToPointPen, GuessSmoothPointPen, ReverseContourPointPen, DecomposingPointPen, ReverseFlipped |
transform |
Transform, Identity, Offset, Scale, TransformPen, TransformPointPen |
recording |
RecordingPen, RecordingPointPen, DecomposingRecordingPen, DecomposingRecordingPointPen, replayRecording, lerpRecordings |
filter |
FilterPen, ContourFilterPen, FilterPointPen, ContourFilterPointPen, OnCurveFirstPointPen |
measure |
ControlBoundsPen, BoundsPen, AreaPen, PointInsidePen |
reverse |
ReverseContourPen, reversedContour |
rounding |
RoundingPen, RoundingPointPen, otRound, noRound |
qt |
QPainterPathPen, draw_onoff_contours |
Everything is also exported from fontlab_pens itself.
Building native adapters
Code that resolves its own native target, such as a glyph-level adapter, can build on the staging layer the native pens use:
| Name | Purpose |
|---|---|
StagingPointPen |
Point pen that validates each contour and passes it, complete, to append_contour(points). check_component(transformation) validates a component call. |
StagedPoint |
One staged point: (position, segment_type, smooth, name). |
staged_node_kinds(points) |
Native node kind per staged point: move, on, cubic or quad. |
FONTGATE_NODE_TYPES |
FontGate fgNode type for each of those kinds. |
invert_transform(transform) |
Inverse of an affine transform; raises ValueError if it is singular. |
FontGatePointPen(target, kind="layer") or kind="element" states the
target kind instead of detecting it from the native type name, for wrappers
and test doubles. PenError subclasses fontTools.pens.basePen.PenError when
fontTools is installed, so either except clause catches it.
Not ported: DecomposedTransform, the decomposing filter pens, and the pens
that need a graphics toolkit, cu2qu or a TrueType glyph object.
Parity with fontTools
tests/test_parity.py sends the same scripted drawings to the fontTools class
and to the fontlab_pens class, then compares the recordings, bounds and
areas. The drawings cover lines, cubics, super-beziers, multi-off quadratic
strings, off-curve-only (None-terminated) TrueType contours, open contours,
components and transforms. The relevant upstream fontTools tests are ported in
tests/ported/.
Development
./test.sh
This runs pytest on Python 3.11 through uv. fontTools is installed only for
the tests, and coverage must stay at 80% or above. The test environment must
install this checkout (the command uses --with-editable): pytest
pythonpath = ["src"] does not reach subprocesses, and isolated Python
(-I) also ignores PYTHONPATH. Running pytest without that installation
causes seven subprocess import failures; keep their isolation checks intact.
Licence
Apache-2.0. Ported fontTools code stays under its MIT notice; see NOTICE.
Scripts demos edit the open glyph with history; Measurements prints to Output.
Metadata
Release files for fontlab-python-pens 1.0.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| fontlab_python_pens-1.0.8.tar.gz | 100.9 kB | Without provenance | Details for fontlab_python_pens-1.0.8.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| fontlab_python_pens-1.0.8-py3-none-any.whl | Python 3 | none | any | Details for fontlab_python_pens-1.0.8-py3-none-any.whl |
Total release size: 149.6 kB
Release files / fontlab_python_pens-1.0.8.tar.gz
| Download URL | fontlab_python_pens-1.0.8.tar.gz |
|---|---|
| Size | 100.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
015357fd16e79abb9c83f005bfbae21b215e3e0e3af31c1e953a422a31eadb07
|
|
BLAKE2b-256 checksum How to use checksums |
dee2825ddd7bf0868ecdfae50b922dab6b78f997138d3a9fd5a1a69c7a1abb5a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / fontlab_python_pens-1.0.8-py3-none-any.whl
| Download URL | fontlab_python_pens-1.0.8-py3-none-any.whl |
|---|---|
| Size | 48.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4b129667dbf1b32eb2fbbc1b676a0a0c5bf2fa63c22c1f10ffe74521738e223d
|
|
BLAKE2b-256 checksum How to use checksums |
6f585bd05cfc210918d720310c1ed4a44d00bb44b56fb0a751c3780bada8c34d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for fontlab-python-pens 1.0.8
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.