Skip to content

fix(ops): resolve self-intersecting contours before assembling contour ops - #22

Open
knipknap wants to merge 1 commit into
mainfrom
fix/contour-self-intersection
Open

knipknap wants to merge 1 commit into
mainfrom
fix/contour-self-intersection

Conversation

@knipknap

Copy link
Copy Markdown
Contributor

Problem

Some fonts contain glyphs whose outlines self-intersect or have overlapping contours. The resulting Geometry renders fine (fill rules handle it), which is why the sketcher and the workpiece look correct, but the contour assembler relies on winding analysis, containment hierarchies and polygon offsetting — all of which break on such input. Text set in those fonts could produce incomplete or even empty ops from the Contour step.

Scanning 528 system font families, 38 produce self-intersecting text geometry (e.g. Kustom Kar, Gunplay 3D, Zekton Dots, German Participants).

Solution

  • New geo::algo::self_intersect::resolve_self_intersections: detects closed contours that intersect themselves or each other, linearizes only those at a given tolerance, and rebuilds them via a Clipper2 NonZero union. Clean contours pass through untouched, so arcs and Béziers are preserved for well-behaved input. Properly wound holes survive; if the union fails the original geometry is kept as fallback.
  • assemble_contour applies the resolution to its input before splitting and winding normalization.
  • Exposed as Geometry.remove_self_intersections(tolerance=0.01) (stubs regenerated).

Verification

  • With cut_side="outside", offset_mm=0.25, text in German Participants went from 95.8 mm to 122.8 mm of cut path (~27 mm was silently missing); Peatloaf from 27.2 to 31.3 mm.
  • A bow-tie polygon previously produced zero ops; it now cuts both loops.
  • After the fix, none of the 38 affected fonts produce empty/incomplete ops.
  • Full test suite: 3853 passed; clippy/fmt clean.

@knipknap
knipknap force-pushed the fix/contour-self-intersection branch from c042f87 to 5d395d8 Compare August 24, 2026 19:29
Some fonts contain glyphs whose outlines self-intersect or consist
of overlapping contours (e.g. a plus sign drawn as two crossing
rectangles).  The resulting geometry renders fine with fill rules,
but breaks winding analysis, containment hierarchies and polygon
offsetting downstream — e.g. the contour assembler could emit
incomplete or empty ops, and face classification could split a
crossed glyph into separate pockets.

Add geo::algo::self_intersect::resolve_self_intersections, which
detects affected closed contours and rebuilds them as the Clipper2
NonZero union of their linearised outlines.  Unaffected contours
(including their arcs and Beziers) pass through untouched.
text_to_geometry now applies it to each glyph outline individually,
so adjacent glyphs are never merged and text geometry is clean from
the start.

Expose it as Geometry.remove_self_intersections() and pin the
behaviour with geo and text-level tests.
@knipknap
knipknap force-pushed the fix/contour-self-intersection branch 3 times, most recently from 637cc8f to ad26ed4 Compare August 26, 2026 10:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant