From d3055e264eece79561080c41777744c762c34d7e Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 10 Nov 2026 00:44:03 -0800 Subject: [PATCH] 3D: an image a clipping group hides wholly does not refuse its 3D effect An image in a clipping group whose bounds lie wholly outside the clipping path's bounds draws nothing, so a 3D effect leaves it out instead of refusing it. An image with effects, fills or strokes of its own, or one in a group with effects inside the clipping group, is still refused, since those can draw it inside the clip. --- crates/effects/src/solids/source.rs | 43 +++++++++++++++++++--- crates/effects/src/solids/tests_sources.rs | 37 +++++++++++++++++-- docs/three-d.md | 4 +- 3 files changed, 74 insertions(+), 10 deletions(-) diff --git a/crates/effects/src/solids/source.rs b/crates/effects/src/solids/source.rs index 4103a453c5..933abecff3 100644 --- a/crates/effects/src/solids/source.rs +++ b/crates/effects/src/solids/source.rs @@ -8,7 +8,7 @@ use std::borrow::Cow; use std::sync::Arc; use vectorcraft_doc::{Appearance, AppearanceItem, Document, FillLayer, Node, NodeKind}; -use vectorcraft_geom::{FillRule, PathData}; +use vectorcraft_geom::{FillRule, PathData, Rect}; use vectorcraft_pathops as po; use super::{MAX_MEMBERS, Scope, geometry, is_container, members, profile, region, reshape_members, solid_paints}; @@ -57,7 +57,7 @@ pub(crate) fn resolve<'a>(doc: Option<&Document>, n: &'a Node, label: &str) -> R if !needs(n) { return Ok(Cow::Borrowed(n)); } - let mut r = Resolver { doc, label, leaves: 0, work: 0 }; + let mut r = Resolver { doc, label, leaves: 0, work: 0, shown: None }; let children = r.content(n, 0); #[cfg(test)] WORK.with(|w| w.set(r.work)); @@ -79,6 +79,8 @@ struct Resolver<'a> { leaves: usize, /// The objects visited or made so far, counted against [`MAX_WORK`]. work: usize, + /// Inside clipping groups, the box of what their clipping paths show together. + shown: Option, } impl Resolver<'_> { @@ -94,6 +96,18 @@ impl Resolver<'_> { Ok(()) } + /// `f` run on the art of `c`. The effects of an object or group can move its art into a clip, + /// so inside one with effects no image is left out for lying outside the clip ([`Self::shown`]). + fn inside(&mut self, c: &Node, f: impl FnOnce(&mut Self) -> Result) -> Result { + let outer = self.shown; + if has_effects(c) { + self.shown = None; + } + let out = f(self); + self.shown = outer; + out + } + /// The art `n`, a symbol instance, a blend, a clipping group or layer, or a group holding one, /// shows, as the members of a plain group. fn content(&mut self, n: &Node, depth: usize) -> Result>, String> { @@ -122,7 +136,13 @@ impl Resolver<'_> { _ if n.clips() => { let Some((first, rest)) = kids(n).split_first() else { return Ok(vec![]) }; let clip = clip_area(first); - let (inner, _) = self.list(rest, depth + 1)?; + // An image wholly outside every clip around it shows nothing and is left out. + let outer = self.shown; + let here = clip.0.bounds().unwrap_or(Rect::ZERO); + self.shown = Some(outer.map_or(here, |o| o.intersect(here))); + let listed = self.list(rest, depth + 1); + self.shown = outer; + let (inner, _) = listed?; let mut out = vec![]; for c in &inner { if let Some(c) = self.clipped(c, &clip, depth + 1)? { @@ -148,10 +168,18 @@ impl Resolver<'_> { continue; } if matches!(c.kind, NodeKind::Image(_)) { + // Only an image with no effects or fills and strokes of its own is measured by its + // frame; any other one may draw inside the clip. + let plain = !has_effects(c) && c.appearance.items.iter().all(|i| !i.visible()); + let hidden = plain && self.shown.is_some_and(|k| c.visual_bounds().is_none_or(|b| b.intersect(k).area() <= 0.0)); + if hidden { + changed = true; + continue; + } return Err(image_refusal(self.label)); } if resolves(c) { - let kids = self.content(c, depth)?; + let kids = self.inside(c, |r| r.content(c, depth))?; out.push(Arc::new(with_effects(wrap(c, kids), c))); changed = true; continue; @@ -166,7 +194,7 @@ impl Resolver<'_> { out.push(c.clone()); continue; } - let (kids, inner) = self.list(kids(c), depth + 1)?; + let (kids, inner) = self.inside(c, |r| r.list(kids(c), depth + 1))?; if inner { out.push(Arc::new(with_effects(wrap(c, kids), c))); changed = true; @@ -314,6 +342,11 @@ fn nest(n: &Node, pieces: Vec<(Vec, Node)>, level: usize) -> Vec bool { + n.appearance.effects.iter().any(|e| e.visible) +} + /// Does `n` hold visible groups or layers nested more than `levels` deep? Walked without recursion. fn nested_past(n: &Node, levels: usize) -> bool { let mut stack = vec![(n, 0usize)]; diff --git a/crates/effects/src/solids/tests_sources.rs b/crates/effects/src/solids/tests_sources.rs index 01840c7e48..d77e9f7c86 100644 --- a/crates/effects/src/solids/tests_sources.rs +++ b/crates/effects/src/solids/tests_sources.rs @@ -41,13 +41,18 @@ fn document() -> Document { d } -/// A clipping group: `art` inside a rectangular clipping path covering `clip`. -fn clip_group(id: u64, clip: Rect, art: Node) -> Node { - let mut path = Node::path(NodeId(id + 1), shapes::rectangle(clip), Appearance::default()); +/// A rectangular clipping path covering `clip`. +fn clipping_path(id: u64, clip: Rect) -> Node { + let mut path = Node::path(NodeId(id), shapes::rectangle(clip), Appearance::default()); if let NodeKind::Path { clipping, .. } = &mut path.kind { *clipping = true; } - Node::new(NodeId(id), NodeKind::Group { children: vec![Arc::new(path), Arc::new(art)], clip: true }) + path +} + +/// A clipping group: `art` inside a rectangular clipping path covering `clip`. +fn clip_group(id: u64, clip: Rect, art: Node) -> Node { + Node::new(NodeId(id), NodeKind::Group { children: vec![Arc::new(clipping_path(id + 1, clip)), Arc::new(art)], clip: true }) } fn blend(id: u64, steps: u32) -> Node { @@ -219,6 +224,30 @@ fn images_are_refused_alone_and_in_a_group_with_image_trace_named() { assert!(art(&d, &n, id).is_none(), "{id}"); assert!(three_d_meshes_in(Some(&d), &n).is_err_and(|e| e.contains("Image Trace")), "{id}"); } + // An image wholly outside the clip, alone or in a group, shows nothing and is left out; + // one the clip shows part of is refused. + let clip = Rect::new(100.0, 100.0, 150.0, 150.0); + let masked = |inside: bool| { + let mut img = image(63); + if let NodeKind::Image(i) = &mut img.kind { + i.xf = Affine::translate(if inside { (95.0, 95.0) } else { (300.0, 300.0) }); + } + img + }; + for img in [masked(false), Node::group(NodeId(64), vec![Arc::new(masked(false))])] { + let children = vec![Arc::new(clipping_path(61, clip)), Arc::new(rect(62, 100.0, 100.0, 200.0, 200.0, 0.0)), Arc::new(img)]; + let n = with(Node::new(NodeId(60), NodeKind::Group { children, clip: true }), id, json!({})); + assert_eq!(validate_three_d_in(Some(&d), &n, id, &json!({})), Ok(()), "{id}"); + assert!(art(&d, &n, id).is_some(), "{id}"); + } + let partly = with(clip_group(60, clip, masked(true)), id, json!({})); + assert!(validate_three_d_in(Some(&d), &partly, id, &json!({})).is_err_and(|e| e.contains("Image Trace")), "{id}"); + // An effect on the image, or on a group holding it, can move it into the clip: refused. + let moved = |n: Node| with(n, "distort.transform", json!({"moveH": -200.0, "moveV": -200.0})); + for img in [moved(masked(false)), moved(Node::group(NodeId(64), vec![Arc::new(masked(false))]))] { + let n = with(clip_group(60, clip, img), id, json!({})); + assert!(validate_three_d_in(Some(&d), &n, id, &json!({})).is_err_and(|e| e.contains("Image Trace")), "{id}"); + } // A hidden image is not drawn, so it builds nothing and refuses nothing. let mut hidden = image(53); hidden.visible = false; diff --git a/docs/three-d.md b/docs/three-d.md index b7bbde245b..b07aabb3ac 100644 --- a/docs/three-d.md +++ b/docs/three-d.md @@ -152,7 +152,9 @@ solids are ([Groups](#groups)), turned about one shared axis. - The objects stay live: editing the clipping path, the blend's keys or the symbol reshapes the solid. - An image is refused, alone or in a group: trace it first with Object › Image Trace › Make. The help page routes images to 3D through Image Trace ([Create 3D vector artwork](https://helpx.adobe.com/illustrator/desktop/special-effects-styles/create-3d-graphics/create-3d-vector-artwork.html)). - A hidden image in a group is left out like any hidden member. + A hidden image in a group is left out like any hidden member, and so is an image in a clipping group whose bounds + lie wholly outside the clipping path's bounds, when neither the image nor a group holding it inside the clipping + group has effects. ## Rotate