Skip to content

Instantly share code, notes, and snippets.

@arrowtype
Created June 23, 2023 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arrowtype/9bed02d38b6a321a9c0af822638c3345 to your computer and use it in GitHub Desktop.
Save arrowtype/9bed02d38b6a321a9c0af822638c3345 to your computer and use it in GitHub Desktop.
Basic example of how to use the FontTools Recording Pen
"""
A basic example of how to open a UFO and print out the bezier curve values from a glyph.
Example expanded from:
https://fonttools.readthedocs.io/en/latest/pens/recordingPen.html
Requires FontTools and FontParts.
"""
from fontParts.fontshell import RFont as Font
from fontTools.pens.recordingPen import RecordingPen
# add a relative path to a UFO font
fontPath = "source/masters/Name_Sans-Display_Bold.ufo"
# open the font path as a font object
font = Font(fontPath)
# get a glyph object
glyph = font["a"]
# create a "pen" from the RecordingPen class - this is like an in-memory version of a drawing app’s pen tool
# the variable here would usually just be called "pen," but that can get confusing
virtualPenToDrawWith = RecordingPen()
# draw the glyph object into the pen object
glyph.draw(virtualPenToDrawWith)
# print the value of the pen object
print(virtualPenToDrawWith.value)
@arrowtype
Copy link
Author

This can be useful in the middle of a bigger script, e.g. to see whether points coordinates are rounded or not.

I’m placing this here because it’s easy to forget how to use pens.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment