How to Delete a Glyph in FontForge Using Python
This guide explains how to programmatically remove or clear a glyph from a font file using FontForge’s Python scripting API. Whether you need to strip specific characters from a web font, clean up unwanted contours, or automate batch font modifications, FontForge provides straightforward methods to access glyph slots and wipe their contents.
Basic Glyph Clearing with
glyph.clear()
In FontForge, standard font formats maintain specific encoding slots. In most cases, "deleting" a glyph means clearing its foreground layers, references, and contours rather than shifting the entire font's encoding map.
The clear() method resets the glyph’s foreground layer
completely.
import fontforge
# Open the font file
font = fontforge.open("MyFont.ttf")
# Select the glyph by name or Unicode code point
glyph_name = "dollar" # or use font[0x0024]
if glyph_name in font:
glyph = font[glyph_name]
glyph.clear() # Removes contours, references, and layers
# Save or generate the updated font
font.generate("MyFont_modified.ttf")
font.close()Deleting via Selection
If you want to simulate the GUI action of selecting a glyph and deleting it (which can also clear metadata and advance widths), use FontForge’s selection mechanism:
import fontforge
font = fontforge.open("MyFont.ttf")
# Select the target glyph by name
font.selection.select("dollar")
# Clear the selected glyph
font.clear()
# Generate the output font
font.generate("MyFont_modified.ttf")
font.close()Deleting Multiple Glyphs in a Loop
To strip a batch of glyphs—such as non-ASCII characters or a predefined blacklist—iterate over a list of names or code points:
import fontforge
font = fontforge.open("MyFont.ttf")
glyphs_to_remove = ["A", "B", "C", "exclam", "question"]
for name in glyphs_to_remove:
if name in font:
font[name].clear()
font.generate("MyFont_cleaned.ttf")
font.close()Unlinking References Before Clearing
If the glyph you are deleting is used as a component (reference) in
composite glyphs (for example, the letter A inside
À), deleting it directly may cause broken references in
dependent characters. To prevent this, unlink references across the font
before clearing:
import fontforge
font = fontforge.open("MyFont.ttf")
target_glyph = "acute"
if target_glyph in font:
# Unlink any glyphs that use this glyph as a component
for glyph in font.glyphs():
glyph.unlinkRef(target_glyph)
# Now clear the target glyph safely
font[target_glyph].clear()
font.generate("MyFont_fixed.ttf")
font.close()