Auto Generate FontForge Sidebearings from Bounding Box
FontForge provides native tools and scripting capabilities to automatically generate and adjust sidebearings based on glyph bounding boxes. While FontForge includes an algorithmic "Auto Width" feature in its graphical interface to space glyphs optically, achieving exact, uniform margins derived strictly from bounding box boundaries is best accomplished using FontForge's built-in Python scripting engine.
Using the Built-In Auto Width Feature
FontForge includes an automated metrics generator accessible directly from the graphical user interface:
- Select the glyphs you want to space in the Font View window.
- Navigate to the Metrics menu in the top bar.
- Select Auto Width... (or press
Ctrl + Shift + K). - Enter your desired spacing parameters. FontForge will evaluate the contours and bounding limits of the selected glyphs to calculate balanced left and right sidebearings automatically.
While convenient, Auto Width uses an optical algorithm that considers shape complexity rather than applying a strict, fixed mathematical offset from the glyph's minimum and maximum horizontal coordinates.
Applying Exact Bounding Box Sidebearings via Python
If you require precise margins directly mapped to the horizontal extremes of each glyph's bounding box, Python scripting inside FontForge is the most effective approach. FontForge defines glyph metrics relative to the bounding box:
- Left Sidebearing (LSB): The distance from the origin (\(x = 0\)) to the leftmost point of the glyph's bounding box (\(x_{\min}\)).
- Right Sidebearing (RSB): The distance from the rightmost point of the glyph's bounding box (\(x_{\max}\)) to the advance width marker.
To set fixed sidebearings for all selected glyphs based on their bounding boxes, run the following script via File > Execute Script:
import fontforge
font = fontforge.activeFont()
target_lsb = 50 # Desired left sidebearing in font units
target_rsb = 50 # Desired right sidebearing in font units
for glyph in font.selection.byGlyphs:
# Check if the glyph contains outlines
bbox = glyph.boundingBox()
if bbox == (0.0, 0.0, 0.0, 0.0):
continue
# Set the sidebearings explicitly relative to the bounding box
glyph.left_side_bearing = target_lsb
glyph.right_side_bearing = target_rsbWhen you modify left_side_bearing, FontForge shifts the
glyph horizontally so that its minimum \(x\) coordinate matches the specified value.
When you modify right_side_bearing, FontForge recalculates
the total advance width (\(x_{\max} +
\text{RSB}\)), ensuring exact sidebearings around the bounding
box.