How to Scale All Glyphs in FontForge Using Python

Scaling every glyph in a font manually is tedious and error-prone, but FontForge’s built-in Python scripting environment allows you to automate this task entirely. By iterating through the font’s glyph collection, applying a transformation matrix from the psMat module, and updating the advance widths proportionally, you can resize an entire typeface uniformly in seconds.

The Python Script

To scale all glyphs, use the following Python script. It loads a font, constructs a 2D scaling matrix, loops through every glyph in the font to scale the outlines and advance widths, and saves the modified font to a new file.

import fontforge
import psMat

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

# Define the scale factor (e.g., 1.2 for 120%, 0.8 for 80%)
scale_factor = 1.2

# Create the scale transformation matrix
# psMat.scale(x_scale, y_scale) allows non-uniform scaling;
# passing a single float scales both X and Y uniformly.
scale_matrix = psMat.scale(scale_factor)

# Loop through all glyphs in the font
for glyph in font.glyphs():
    # Scale the visual outline coordinates
    glyph.transform(scale_matrix)
    
    # Scale the horizontal advance width to match the new size
    glyph.width = round(glyph.width * scale_factor)

# Save the modifications
font.generate("MyFont-Scaled.ttf")
font.close()

Key Elements of the Process

  • psMat.scale(): This function from FontForge's PostScript matrix module generates a transformation matrix. Passing one argument scales uniformly along both axes. You can also specify distinct horizontal and vertical scaling factors using psMat.scale(x_factor, y_factor).
  • font.glyphs(): This generator iterates over every glyph present in the font database, including unencoded glyphs and components.
  • glyph.transform(): Applying the matrix to the glyph alters the coordinate positions of its bezier curves and points.
  • glyph.width: The outline transformation does not automatically adjust the glyph's horizontal advance width. Multiplying glyph.width by your scale factor ensures that character spacing and tracking remain proportional to the resized artwork.

How to Run the Script

You can execute this script using one of two methods:

  1. Via the Command Line:
    Run the script using FontForge's embedded Python interpreter by running:

    fontforge -script scale_font.py
  2. Via the FontForge GUI:
    Open your font in FontForge, navigate to File > Execute Script, choose the Python radio button, paste the inner loop logic (replacing font = fontforge.open(...) with font = fontforge.activeFont()), and click OK.