Automate Kerning in FontForge Using Python
Yes, you can automate kerning adjustments in FontForge using its
built-in Python scripting environment. FontForge exposes a powerful
Python module (fontforge) that allows you to manipulate
glyph metrics, create OpenType kerning features, and assign specific
kerning values to hundreds of character pairs automatically. This
approach eliminates the tedious process of manual kerning by allowing
you to calculate adjustments based on bounding boxes or import
predefined kerning pairs from external datasets.
Understanding FontForge Kerning via Python
Kerning in modern fonts is managed through OpenType GPOS
(Glyph Positioning) feature tables. In FontForge, this involves creating
a pair-positioning lookup table, adding a subtable, and assigning
offsets to specific pairs of glyphs.
FontForge provides two primary mechanisms for automated kerning:
- Algorithmic Kerning: Calculating optical distance by analyzing the outlines or bounding boxes of adjacent glyphs.
- Data-Driven Kerning: Reading values from an external file (such as a CSV or JSON containing common pairs like "AV", "To", or "Wa") and applying them directly.
Step-by-Step Implementation
To automate kerning, you can write a standalone script executed by the FontForge interpreter. Below is a concise example showing how to create an OpenType kerning subtable and assign values to a list of glyph pairs.
import fontforge
# Open the target font file
font = fontforge.open("MyCustomFont.sfd")
# Define the kerning lookup and subtable names
lookup_name = "'kern' Horizontal Kerning"
subtable_name = "'kern' Horizontal Kerning subtable"
# Check if the lookup exists; if not, create it
if lookup_name not in font.gpos_lookups:
font.addLookup(lookup_name, "gpos_pair", (), [["kern", [["latn", ["dflt"]]]]])
font.addKerningSubtable(lookup_name, subtable_name)
# Define kerning pairs and their metric adjustments (in font units)
# Negative values pull glyphs closer together; positive values push them apart.
kerning_data = {
("A", "V"): -60,
("T", "o"): -45,
("A", "W"): -50,
("F", "a"): -30,
("V", "A"): -60,
}
# Apply the kerning values to the subtable
for (first_glyph, second_glyph), offset in kerning_data.items():
if first_glyph in font and second_glyph in font:
g1 = font[first_glyph]
g2 = font[second_glyph]
# Add the pair kerning offset to the subtable
g1.addPosSub(subtable_name, g2.glyphname, 0, 0, offset, 0, 0, 0, 0, 0)
# Save the updated font
font.save("MyCustomFont_Kerned.sfd")
font.generate("MyCustomFont_Kerned.otf")
font.close()Auto-Generating Values Based on Metrics
If you want to fully automate value generation without manually defining offsets, you can calculate the distance between glyph contours using FontForge's contour measurement functions.
You can sample horizontal distances at various vertical points using
glyph.layers['Fore'].boundingBox() and the
glyph.intersect() method. By identifying the minimum
horizontal gap between two adjacent shapes at shared vertical
coordinates, a script can dynamically calculate an offset that
normalizes the whitespace between characters.
How to Run the Script
You can execute your automation script without launching the graphical user interface by running it directly via the terminal:
fontforge -script kern_script.pyAlternatively, if you are working inside the FontForge GUI, you can open File > Execute Script and paste your Python code directly into the window to apply the adjustments to the currently active font.