mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-18 05:29:53 +00:00
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.
This commit is contained in:
@@ -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<Rect>,
|
||||
}
|
||||
|
||||
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<T>(&mut self, c: &Node, f: impl FnOnce(&mut Self) -> Result<T, String>) -> Result<T, String> {
|
||||
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<Vec<Arc<Node>>, 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<Scope>, Node)>, level: usize) -> Vec<Arc<Node
|
||||
out
|
||||
}
|
||||
|
||||
/// Does `n` carry a visible effect?
|
||||
fn has_effects(n: &Node) -> 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)];
|
||||
|
||||
@@ -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;
|
||||
|
||||
+3
-1
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user