From ae8b501c314f94d01eee1345cbbb3a0824274975 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 13 Nov 2026 16:36:46 -0800 Subject: [PATCH] Concept to Vector: results go beside an image in a group with distant art Insert measured a group around the image as one obstacle, so the empty space between its members pushed the row past the group's farthest art, or failed with "no room". A group with no fills, strokes or effects of its own now counts each member where it paints; a clip group counts its clipped members within what it shows, and its clipping path's fill and stroke when they are set. --- crates/engine/src/cmd/conceptvector.rs | 40 +++++++++++++++++++---- crates/engine/src/concept_vector/tests.rs | 38 +++++++++++++++++++++ 2 files changed, 71 insertions(+), 7 deletions(-) diff --git a/crates/engine/src/cmd/conceptvector.rs b/crates/engine/src/cmd/conceptvector.rs index 1092b1111c..85daa223ce 100644 --- a/crates/engine/src/cmd/conceptvector.rs +++ b/crates/engine/src/cmd/conceptvector.rs @@ -489,17 +489,43 @@ fn insert(s: &mut Session, p: &Value) -> Result { }) .collect() }; - // The painted bounds of every other visible object, by its outermost object in each layer, - // and the image's own box. + // The painted bounds of every other visible object, and the image's own box. A layer, and a + // group that paints nothing of its own, is measured by its members (an opacity mask only hides + // them); one that clips, by its members after the clipping path, cut to what it paints. let mut obstacles: Vec = vec![src]; - let mut stack: Vec<&Arc> = st.doc.layers.iter().collect(); - while let Some(node) = stack.pop() { + let mut stack: Vec<(&Arc, Option)> = st.doc.layers.iter().map(|l| (l, None)).collect(); + while let Some((node, within)) = stack.pop() { if !node.visible || node.id == source { continue; } - if node.is_layer() { - stack.extend(node.children().into_iter().flatten()); - } else if let Some(b) = vectorcraft_render::painted_bounds(&st.doc, node) { + let cut = |b: Rect| match within { + Some(w) => Some(b.intersect(w)).filter(|i| i.width() > 0.0 && i.height() > 0.0), + None => Some(b), + }; + let painted = || vectorcraft_render::painted_bounds(&st.doc, node).and_then(cut); + let clips = matches!(node.kind, NodeKind::Group { clip: true, .. } | NodeKind::Layer { clip: true, .. }); + let by_members = node.is_layer() + || (matches!(node.kind, NodeKind::Group { .. }) && node.appearance.items.is_empty() && node.appearance.effects.is_empty()); + if by_members { + // A clipping path paints its fills and strokes when they are set (`Node::clip_paint`), + // its strokes outside the clip too, whether or not it is shown. + if clips && let Some(path) = node.children().into_iter().flatten().next() { + let paint = path.clip_paint(); + for part in [paint.fill, paint.stroke].into_iter().flatten() { + if let Some(b) = vectorcraft_render::painted_bounds(&st.doc, &part).and_then(cut) { + obstacles.push(b); + } + } + } + let within = if clips { + // Nothing of a clipping group shows outside what it paints. + let Some(shown) = painted() else { continue }; + Some(shown) + } else { + within + }; + stack.extend(node.children().into_iter().flatten().skip(usize::from(clips)).map(|c| (c, within))); + } else if let Some(b) = painted() { obstacles.push(b); } } diff --git a/crates/engine/src/concept_vector/tests.rs b/crates/engine/src/concept_vector/tests.rs index 9d13acaf43..7309eeb6ae 100644 --- a/crates/engine/src/concept_vector/tests.rs +++ b/crates/engine/src/concept_vector/tests.rs @@ -594,6 +594,44 @@ fn insert_moves_the_row_past_art_in_its_way() { assert!(placed.x0 >= bounds(&s, block).x1 + GAP - 2.0, "{placed:?}"); } +/// In a group with art far to its right, the row goes beside the image: the empty space between +/// the group's members is not in its way. +#[test] +fn insert_goes_beside_the_image_in_a_group_with_art_far_away() { + let (mut s, img) = doc_with(&art()); + let far = s.execute("shape.rectangle", &json!({"x": 900, "y": 150, "width": 20, "height": 20})).unwrap()["id"].as_u64().unwrap(); + s.execute("select.set", &json!({"ids": [img.0, far]})).unwrap(); + s.execute("object.group", &json!({})).unwrap(); + let job = generate(&mut s, json!({"provider": ID, "styles": ["flatColor"], "id": img.0})).unwrap()["job"].as_u64().unwrap(); + let r = s.execute("conceptVector.insert", &json!({"job": job, "variations": ["flatColor"]})).unwrap(); + let (placed, image) = (bounds(&s, r["ids"][0].as_u64().unwrap()), bounds(&s, img.0)); + assert!(placed.x0 < image.x1 + GAP + 2.0 && placed.x1 < bounds(&s, far).x0, "{placed:?} after {image:?}"); + + // The same in a clip group whose clipping path reaches past the far art. + let (mut s, img) = doc_with(&art()); + let far = s.execute("shape.rectangle", &json!({"x": 900, "y": 150, "width": 20, "height": 20})).unwrap()["id"].as_u64().unwrap(); + let mask = s.execute("shape.rectangle", &json!({"x": 100, "y": 100, "width": 900, "height": 180})).unwrap()["id"].as_u64().unwrap(); + s.execute("select.set", &json!({"ids": [img.0, far, mask]})).unwrap(); + s.execute("object.clippingMask.make", &json!({})).unwrap(); + let job = generate(&mut s, json!({"provider": ID, "styles": ["flatColor"], "id": img.0})).unwrap()["job"].as_u64().unwrap(); + let r = s.execute("conceptVector.insert", &json!({"job": job, "variations": ["flatColor"]})).unwrap(); + let (placed, image) = (bounds(&s, r["ids"][0].as_u64().unwrap()), bounds(&s, img.0)); + assert!(placed.x0 < image.x1 + GAP + 2.0 && placed.x1 < bounds(&s, far).x0, "clip group: {placed:?} after {image:?}"); + + // A clipping path given a stroke again paints it: the row goes past it. + let (mut s, img) = doc_with(&art()); + let mask = s.execute("shape.rectangle", &json!({"x": 100, "y": 100, "width": 900, "height": 180})).unwrap()["id"].as_u64().unwrap(); + s.execute("select.set", &json!({"ids": [img.0, mask]})).unwrap(); + s.execute("object.clippingMask.make", &json!({})).unwrap(); + s.execute("select.set", &json!({"ids": [mask]})).unwrap(); + s.execute("paint.setStroke", &json!({"color": "#000000"})).unwrap(); + assert!(s.doc().unwrap().doc.node(NodeId(mask)).unwrap().clip_paint().stroke.is_some()); + let job = generate(&mut s, json!({"provider": ID, "styles": ["flatColor"], "id": img.0})).unwrap()["job"].as_u64().unwrap(); + let r = s.execute("conceptVector.insert", &json!({"job": job, "variations": ["flatColor"]})).unwrap(); + let placed = bounds(&s, r["ids"][0].as_u64().unwrap()); + assert!(placed.x0 >= 1000.0 + GAP, "painted clipping path: {placed:?}"); +} + /// A variation's paths reaching past the image's box keep clear of art there too. #[test] fn insert_moves_the_row_past_art_that_a_variation_reaches_beyond_the_image_box() {