Check If a Glyph Is Empty in FontForge Python

This guide explains how to determine whether a glyph contains no visual vector data using FontForge's Python scripting interface. Checking for empty glyphs is essential when auditing fonts, cleaning up unused unicode slots, or automating font generation pipelines. You will learn the most reliable ways to detect empty glyphs by inspecting outline contours, component references, and glyph bounding boxes.

In FontForge, a glyph is visually empty if it contains neither native contours in its foreground layer nor references to other glyphs (components).

You can check both properties directly on the glyph object:

import fontforge

def is_glyph_empty(glyph):
    # Check if the foreground layer contains any contours
    has_contours = len(glyph.foreground) > 0
    
    # Check if the glyph references other glyphs
    has_references = len(glyph.references) > 0
    
    # The glyph is empty if it has neither contours nor references
    return not (has_contours or has_references)

# Example usage:
font = fontforge.open("MyFont.sfd")

for glyph in font.glyphs():
    if is_glyph_empty(glyph):
        print(f"Glyph '{glyph.glyphname}' (Unicode: {glyph.unicode}) is empty.")

Method 2: Checking the Bounding Box

Another quick method to determine if a glyph is empty is to inspect its bounding box coordinates using the boundingBox() method. A glyph with no drawn content has a bounding box of all zeros: (0.0, 0.0, 0.0, 0.0).

def is_bbox_empty(glyph):
    xmin, ymin, xmax, ymax = glyph.boundingBox()
    return xmin == 0.0 and ymin == 0.0 and xmax == 0.0 and ymax == 0.0

Note: Standard whitespace characters (such as the space character U+0020) also return (0.0, 0.0, 0.0, 0.0) for their bounding box, even though they possess a positive advance width. If you want to preserve whitespace characters, filter them out by Unicode codepoint before checking.

Method 3: Using isWorthOutputting()

FontForge provides a built-in method called isWorthOutputting(). This method checks whether FontForge considers the glyph necessary to include in an exported font file.

if not glyph.isWorthOutputting():
    print(f"Glyph '{glyph.glyphname}' has no graphical data and is not worth outputting.")

isWorthOutputting() returns False for empty glyphs, but returns True if the glyph has contours, references, or is a functional whitespace character (such as space or non-breaking space) with an advance width.

Batch Processing Example

To iterate through a font, identify empty non-whitespace glyphs, and optionally clear or report them:

import fontforge

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

# Set of standard whitespace codepoints to ignore
WHITESPACE_UNICODES = {0x0020, 0x00A0, 0x2000, 0x2001, 0x2002, 0x2003, 0x2004, 0x2005, 0x2006, 0x2007, 0x2008, 0x2009, 0x200A, 0x200B, 0x3000}

empty_glyphs = []

for glyph in font.glyphs():
    if glyph.unicode in WHITESPACE_UNICODES:
        continue

    if len(glyph.foreground) == 0 and len(glyph.references) == 0:
        empty_glyphs.append(glyph.glyphname)

print(f"Found {len(empty_glyphs)} empty glyphs:")
print(", ".join(empty_glyphs))