Created
June 23, 2023 22:05
-
-
Save arrowtype/9bed02d38b6a321a9c0af822638c3345 to your computer and use it in GitHub Desktop.
Basic example of how to use the FontTools Recording Pen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.