Automate Optimal Sidebearings in FontForge
Yes, you can use scripts to automatically calculate and apply optimal sidebearings in FontForge. By leveraging FontForge's built-in Python scripting engine, font developers can bypass tedious manual spacing for large glyph sets. Whether using native auto-spacing functions or designing custom algorithms that account for optical volume, scripting provides an efficient, reproducible way to achieve balanced horizontal metrics across an entire typeface.
Native FontForge Auto-Width Scripting
FontForge includes a native routine for calculating glyph widths and
sidebearings, which can be executed directly through Python via the
autoWidth method. This function evaluates adjacent
character pairs and spaces glyphs based on a target separation
value.
A basic implementation looks like this:
import fontforge
# Open the font file
font = fontforge.open("MyFont.sfd")
# Select glyphs to space (e.g., lowercase alphabet)
font.selection.select(("ranges", "unicode"), ord("a"), ord("z"))
# Apply native auto-spacing with a desired average space (in font units)
font.autoWidth(120)
# Save the updated font
font.save()While native auto-spacing is fast, it relies on simple geometric approximations and may require manual refinement on glyphs with complex cutouts (such as "T", "L", or diagonal letters like "v").
Creating Custom Optical Spacing Scripts
To achieve truly optimal sidebearings, type designers often script
custom algorithms that evaluate optical volume rather than raw bounding
boxes. Because the fontforge Python API exposes detailed
contour and point data, you can write mathematical models to calculate
the visual center of mass:
- Horizontal Slicing: A script can sample the horizontal profile of a glyph at multiple vertical intervals (e.g., across the x-height). By measuring the distance between the bounding box and the actual contour points at each slice, the script calculates the optical white space surrounding the character.
- Weighted Averages: Assign higher weights to the middle zone of the x-height and lower weights to areas near the baseline or cap-height, compensating for how the human eye perceives density.
- Setting Sidebearings: Once the optical offset is
computed, directly set the left sidebearing
(
glyph.left_side_bearing) and right sidebearing (glyph.right_side_bearing) via script:
for glyph in font.glyphs():
if glyph.isWorthOutputting():
# Example: Setting explicit values calculated by custom logic
optical_left = calculate_optical_offset(glyph, side="left")
optical_right = calculate_optical_offset(glyph, side="right")
glyph.left_side_bearing = int(optical_left)
glyph.right_side_bearing = int(optical_right)Standardizing Sidebearings via Reference Glyphs
A practical hybrid approach is to programmatically link or copy sidebearings from master reference glyphs. In Latin typography, the straight sides of "H" and "n" and the round sides of "O" and "o" form the foundation of consistent spacing.
A script can automate this by:
- Calculating or manually perfecting the metrics for "H", "O", "n", and "o".
- Automatically applying the left sidebearing of "n" to glyphs sharing the same vertical stem (such as "m", "p", "r", and "b").
- Applying the sidebearings of "o" to rounded glyphs (such as "c", "e", and "d").
This method combines automated speed with the precision of human design rules, preventing the inconsistencies that purely automated algorithms sometimes produce.