booleanOperations

raw JSON →
0.10.0 verified Fri May 01 auth: no python

Boolean operations on paths (union, intersection, difference, xor) for font glyph contours. Version 0.10.0, released 2024-04-29. Uses pyclipper (Clipper) for computation. Requires Python >=3.9. Maintained by TypeMyType.

pip install booleanoperations
error ModuleNotFoundError: No module named 'booleanOperations'
cause Wrong import path; installed package is booleanoperations (lowercase) but import is case-sensitive. Usually import 'from booleanOperations' correct.
fix
pip install booleanoperations; then use 'from booleanOperations import ...'
error AttributeError: module 'booleanOperations' has no attribute 'BooleanOperations'
cause Imported the module itself (import booleanOperations) instead of the class.
fix
Use 'from booleanOperations import BooleanOperations'
error ValueError: The operation must be one of: union, intersection, difference, xor
cause Attempted to use a method that does not exist (e.g., 'booleanOperations.BooleanOperations.XOR' with wrong case).
fix
Use correct method names: union, intersection, difference, xor (all lowercase).
breaking Support for Python 2.7 dropped in v0.9.0. Requires Python >=3.6 (v0.9.0) or >=3.9 (v0.10.0).
fix Upgrade to Python 3.9+ for latest version.
gotcha Open contours (not closed) raise OpenContourError. Only closed paths are supported.
fix Ensure all input paths are closed (last point equals first).
gotcha Self-intersecting contours with zero area are dropped (may produce unexpected results).
fix Upgrade to 0.8.2 or later to avoid dropped contours.
breaking ufoLib dependency removed in v0.8.1; now requires fonttools >=3.32.0.
fix Ensure fonttools is 3.32.0 or higher.

Basic union of a glyph outline and a rectangle. Output is a recording pen value.

from fontTools.pens.t2Pen import T2Pen
from fontTools.pens.recordingPen import RecordingPen
from fontTools.ttLib import TTFont
from fontTools.pens.pointPen import PointToSegmentPen
from booleanOperations import BooleanOperations

# Load a font, get a glyph
font = TTFont('MyFont.otf')
glyph = font.getGlyphSet()['A']

# Create a rectangle as a closed path
rect_pen = RecordingPen()
rect_pen.moveTo((0, 0))
rect_pen.lineTo((100, 0))
rect_pen.lineTo((100, 100))
rect_pen.lineTo((0, 100))
rect_pen.closePath()

# Get glyph outline
glyph_pen = RecordingPen()
glyph.draw(glyph_pen)

# Perform union
op = BooleanOperations()
result_pen = RecordingPen()
op.union(glyph_pen.value, rect_pen.value, result_pen)
print('Union result:', result_pen.value)