Automate FontForge Font Generation via CLI
FontForge allows complete automation of font generation through its
command-line interface. While FontForge does not use dedicated flags for
every single export setting (such as --export-ttf), it
provides flags that allow you to execute inline commands or standalone
scripts non-interactively. By combining FontForge CLI flags with Python
or native FontForge scripts, you can seamlessly convert source files,
modify glyphs, and batch-produce fonts in formats like TTF, OTF, and
WOFF2 directly in your terminal or CI/CD pipeline.
Essential FontForge CLI Flags
To automate font generation, FontForge provides specific flags to bypass the graphical interface and execute instructions:
-script [file]: Runs an external script file using Python or FontForge's legacy scripting engine.-c "[commands]": Executes a string of scripting commands directly from the terminal without needing an external script file.-lang=py: Explicitly instructs FontForge to interpret commands using Python syntax (recommended for modern workflows).
Method 1:
Using the -c Flag for Inline Generation
You can generate a font in a single command using the -c
flag combined with inline Python. This approach is ideal for quick
conversions and shell scripts.
fontforge -lang=py -c 'import fontforge; font = fontforge.open("input.sfd"); font.generate("output.ttf")'In this command:
fontforge -lang=pyinitiates FontForge in Python mode without a GUI.fontforge.open("input.sfd")loads the source font file.font.generate("output.ttf")exports the font to TrueType format. FontForge automatically determines the output format based on the file extension.
Method
2: Using the -script Flag with an External Script
For more complex tasks, such as generating multiple web font formats or modifying metadata, use an external Python script.
Create a file named generate.py:
import sys
import fontforge
def build_fonts(source_path, output_name):
# Open the source font file (SFD, UFO, etc.)
font = fontforge.open(source_path)
# Generate standard formats
font.generate(f"{output_name}.ttf")
font.generate(f"{output_name}.otf")
font.generate(f"{output_name}.woff2")
font.close()
if __name__ == "__main__":
if len(sys.argv) > 2:
build_fonts(sys.argv[1], sys.argv[2])
else:
print("Usage: fontforge -script generate.py <source> <output_basename>")Run the script from the command line:
fontforge -script generate.py input.sfd MyFontThis generates MyFont.ttf, MyFont.otf, and
MyFont.woff2 without launching the FontForge application
window.
Automating Batch Processing with Shell Loops
To process multiple font files in a directory, integrate FontForge with a standard Bash loop:
for file in *.sfd; do
fontforge -lang=py -c "import fontforge; f = fontforge.open('${file}'); f.generate('${file%.sfd}.ttf')"
doneThis loop scans the current directory for .sfd files and
compiles each one into a .ttf binary, allowing for fully
automated build pipelines.