diff --git a/crates/silhouette-studio/src/write.rs b/crates/silhouette-studio/src/write.rs index c8b5ce0353..8d58106c47 100644 --- a/crates/silhouette-studio/src/write.rs +++ b/crates/silhouette-studio/src/write.rs @@ -70,10 +70,19 @@ impl WriteContour { } c.nodes.push(WriteNode { x: to.x, y: to.y, incoming: Some((c2.x - to.x, c2.y - to.y)), outgoing: None }); }; + // Set by a close: a segment that follows without a move starts a new contour at the + // closed contour's first node. + let mut closed_at: Option = None; for el in path.elements() { + if !matches!(el, PathEl::MoveTo(_) | PathEl::ClosePath) + && let Some(start) = closed_at.take() + { + current = Some(WriteContour { nodes: vec![WriteNode::corner(start.x, start.y)], closed: false }); + } match *el { PathEl::MoveTo(p) => { out.extend(current.take()); + closed_at = None; current = Some(WriteContour { nodes: vec![WriteNode::corner(p.x, p.y)], closed: false }); last = p; } @@ -112,6 +121,7 @@ impl WriteContour { } if let Some(first) = c.nodes.first() { last = kurbo::Point::new(first.x, first.y); + closed_at = Some(last); } out.push(c); } @@ -1102,6 +1112,33 @@ mod tests { assert_eq!(curves, 2); } + #[test] + fn segments_after_a_close_start_a_contour_at_the_closed_contours_first_node() { + use kurbo::PathEl; + let mut path = kurbo::BezPath::new(); + for el in [ + PathEl::MoveTo((10.0, 10.0).into()), + PathEl::LineTo((20.0, 10.0).into()), + PathEl::LineTo((20.0, 20.0).into()), + PathEl::ClosePath, + PathEl::LineTo((30.0, 30.0).into()), + PathEl::QuadTo((40.0, 30.0).into(), (40.0, 40.0).into()), + PathEl::ClosePath, + PathEl::CurveTo((0.0, 20.0).into(), (0.0, 30.0).into(), (5.0, 40.0).into()), + ] { + path.push(el); + } + let contours = WriteContour::from_path(&path); + assert_eq!(contours.len(), 3, "{contours:?}"); + let starts: Vec<(f64, f64)> = contours.iter().map(|c| (c.nodes[0].x, c.nodes[0].y)).collect(); + assert_eq!(starts, vec![(10.0, 10.0), (10.0, 10.0), (10.0, 10.0)]); + assert_eq!(contours[1].nodes.len(), 3); + assert!(contours[1].closed); + assert_eq!(contours[2].nodes.len(), 2); + assert!(!contours[2].closed); + assert!(contours[2].nodes[0].outgoing.is_some() && contours[2].nodes[1].incoming.is_some()); + } + #[test] fn studio_v2_documents_read_back() { let doc =