Create Overlapping Shapes with FontForge Python
Yes, you can create overlapping compound shapes using Python scripts in FontForge. FontForge provides a comprehensive Python API that enables you to programmatically draw vector paths, layer multiple intersecting contours within a single glyph, merge shapes using boolean operations, or assemble complex glyphs using references. This guide explains how to construct overlapping paths and manage their interactions via scripting.
Drawing Overlapping Contours in a Single Glyph
In FontForge, a glyph's outline consists of one or more closed contours. You can add multiple overlapping paths directly to the glyph's foreground layer. By default, these paths remain distinct entities within the layer unless you explicitly choose to combine them.
Here is an example of creating a glyph with two overlapping rectangular contours:
import fontforge
# Create a new font and a target glyph
font = fontforge.font()
glyph = font.createChar(65, "A")
# Create the first contour (a rectangle)
c1 = fontforge.contour()
c1.moveTo(100, 100)
c1.lineTo(100, 500)
c1.lineTo(400, 500)
c1.lineTo(400, 100)
c1.closed = True
# Create the second overlapping contour
c2 = fontforge.contour()
c2.moveTo(300, 300)
c2.lineTo(300, 700)
c2.lineTo(600, 700)
c2.lineTo(600, 300)
c2.closed = True
# Append both contours to the glyph layer
layer = glyph.layers['Fore']
layer += c1
layer += c2
glyph.layers['Fore'] = layerMerging Overlaps with Boolean Operations
If your goal is to eliminate intersecting paths and convert them into
a single, seamless compound boundary, FontForge provides the
removeOverlap() method. This function performs a boolean
union on all intersecting contours in the active layer.
To merge the overlapping geometry created above, execute:
# Merge overlapping contours into a single unified outline
glyph.removeOverlap()
# Optional: Ensure winding directions are correct for font rendering
glyph.correctDirection()This transforms the intersecting paths into a single combined outline, removing inner intersecting lines.
Creating Compound Glyphs Using References
FontForge supports "compound glyphs," which are glyphs constructed from references (components) of other glyphs. You can overlap multiple referenced glyphs to build a compound structure.
# Create base component glyphs
base1 = font.createChar(1000, "comp_rect")
base1.layers['Fore'] += c1
base2 = font.createChar(1001, "comp_circle")
# (Define and add geometry to base2 here)
# Create a compound glyph that references both
compound = font.createChar(66, "B")
compound.addReference("comp_rect")
compound.addReference("comp_circle")
# Optional: Flatten the references into native editable paths
compound.unlinkRef()
# Optional: Merge the flattened overlaps
compound.removeOverlap()Path Direction and Fill Behavior
When keeping paths overlapping without calling
removeOverlap(), the visual fill depends on the path
direction (clockwise vs. counter-clockwise) and the target font format's
fill rule:
- TrueType (Non-Zero Winding Rule): Overlapping contours drawn in the same direction will fill completely. Contours drawn in opposite directions will create a cutout (hole).
- PostScript / OpenType (Even-Odd Rule): Intersecting areas may invert depending on how many paths overlap at any given point.
Use glyph.correctDirection() to automatically orient
outer boundaries counter-clockwise (or clockwise depending on format
requirements) to maintain consistent fill appearance across
rasterizers.