craft-silhouette-studio: segments after a close start a new contour instead of being dropped

WriteContour::from_path ended the current contour at ClosePath, and a
LineTo, QuadTo or CurveTo that followed without a MoveTo found no contour
and was skipped, so the written file lost those segments. Such a segment
now starts a new contour at the closed contour's first node, which is
where kurbo and SVG place the pen after a close.
This commit is contained in:
dotCooCoo committed 2026-11-29 20:01:09 -08:00
1 parent 6a4729d5af
commit 7c55aea94e
1 file changed
+37
+37
View File
@@ -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<kurbo::Point> = 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 =