How to Set Sidebearings in FontForge Using Python

Setting sidebearings programmatically in FontForge allows type designers to automate the spacing process, apply uniform metrics, and batch-process entire font families efficiently. This article provides a direct guide to accessing glyphs and adjusting Left Sidebearings (LSB) and Right Sidebearings (RSB) using the FontForge Python API, covering both direct value assignments and batch operations.

Understanding the Properties

In the FontForge Python module, each glyph object provides direct attributes to read and modify sidebearings:

  • glyph.left_side_bearing: Sets the distance from the glyph origin (x = 0) to the leftmost point of the glyph outline.
  • glyph.right_side_bearing: Sets the distance from the rightmost point of the glyph outline to the advance width marker.
  • glyph.width: Represents the total advance width of the glyph, which is calculated as left_side_bearing + bounding_box_width + right_side_bearing. Changing a sidebearing automatically adjusts the width, while changing the width alters the right sidebearing by default.

Setting Sidebearings on an Individual Glyph

To modify sidebearings, open a font file, retrieve the target glyph by its name or unicode value, assign the new metrics values, and save the font.

import fontforge

# Open the font file
font = fontforge.open("MyFont.sfd")

# Access a specific glyph
glyph = font["A"]

# Set absolute sidebearing values in font design units
glyph.left_side_bearing = 40
glyph.right_side_bearing = 40

# Save changes to the original file
font.save("MyFont_Spaced.sfd")
font.close()

Batch Setting Sidebearings Across Multiple Glyphs

To apply consistent sidebearings across a set of glyphs or the entire font, iterate through the glyphs in the font object.

import fontforge

font = fontforge.open("MyFont.sfd")

# List of glyph names to modify
target_glyphs = ["H", "I", "M", "N"]

for glyph_name in target_glyphs:
    if glyph_name in font:
        glyph = font[glyph_name]
        glyph.left_side_bearing = 60
        glyph.right_side_bearing = 60

# Alternatively, iterate through all printable glyphs
for glyph in font.glyphs():
    # Avoid modifying empty spaces or non-marking glyphs if desired
    if glyph.isWorthOutputting():
        # Example: Add 10 units of padding to existing sidebearings
        glyph.left_side_bearing += 10
        glyph.right_side_bearing += 10

# Generate a new font binary
font.generate("MyFont_Spaced.otf")
font.close()

Automating Sidebearings with Auto-Spacing

If you prefer FontForge to calculate spacing automatically using optical algorithms rather than static values, use the built-in autoWidth method:

import fontforge

font = fontforge.open("MyFont.sfd")

# Automatically calculate the width based on an optical target spacing value
for glyph in font.glyphs():
    if glyph.isWorthOutputting():
        glyph.autoWidth(100)  # 100 is the desired optical separation

font.save()
font.close()

Running the Script

Run the script directly through the FontForge command-line interface to ensure the native fontforge module is available:

fontforge -script set_spacing.py