diff --git a/crates/effects/src/group.rs b/crates/effects/src/group.rs index 1e8d75f52e..9531159e01 100644 --- a/crates/effects/src/group.rs +++ b/crates/effects/src/group.rs @@ -920,13 +920,17 @@ fn copied_container(doc: Option<&Document>, n: &Node, t: usize) -> Node { take_sublayer_guides(&mut m, &mut guides, &Node::is_ruler_guide); let mut source = m.clone(); source.appearance.effects = n.appearance.effects.get(..t).unwrap_or_default().to_vec(); + // With no geometry effect before the Transform effect, `evaluate_container` leaves the members' + // own effects live (it only paints the group's own fills and strokes): the members are drawn + // first, so their effects apply before the copies are made. + if crate::reshape::geometry_effects(&source).is_empty() { + source = crate::reshape::outlined_members(doc, &source); + // The members' raster effects follow the group's own Transform effects as well. + let _later = crate::raster::LaterGroups::enter(&n.appearance.effects); + members_as_drawn(doc, &mut source); + } let mut piece = evaluate_container(doc, &source).unwrap_or_else(|| { - let mut p = crate::reshape::outlined_members(doc, &source); - { - // The members' raster effects follow the group's own Transform effects as well. - let _later = crate::raster::LaterGroups::enter(&n.appearance.effects); - members_as_drawn(doc, &mut p); - } + let mut p = source.clone(); p.appearance.effects = crate::raster::without_geometry(&p.appearance.effects); p }); @@ -936,11 +940,14 @@ fn copied_container(doc: Option<&Document>, n: &Node, t: usize) -> Node { piece.locked = false; (piece.opacity, piece.blend, piece.isolate) = (1.0, Default::default(), false); piece.extras_mut().mask = None; + // The copies are made about the piece's bounds, measured first: a layer leaves its hidden + // members out of them, and a group does not. + let bounds = piece.geometric_bounds(); // A layer's copies are groups in it. if let NodeKind::Layer { children, clip, .. } = &mut piece.kind { piece.kind = NodeKind::Group { children: std::mem::take(children), clip: *clip }; } - let (art, effects) = crate::reshape::copied_container_art(doc, n, t, piece); + let (art, effects) = crate::reshape::copied_container_art(doc, n, t, piece, bounds); let copies = art.children().cloned().unwrap_or_default(); if let NodeKind::Group { children, clip } | NodeKind::Layer { children, clip, .. } = &mut m.kind { *children = copies; diff --git a/crates/effects/src/reshape.rs b/crates/effects/src/reshape.rs index c6278f3e4f..584e98055b 100644 --- a/crates/effects/src/reshape.rs +++ b/crates/effects/src/reshape.rs @@ -191,13 +191,17 @@ pub(crate) fn copied_container_raster_at(n: &Node) -> Option { /// turned, reflected and scaled with it ([`crate::raster::follow_contents`]), once, whatever Scale /// Strokes & Effects says. The members' own raster effects in each copy stay as a Transform effect /// that copies a group leaves them. The geometry effects after `t` then reshape all the copies as -/// one piece. The copies stop where [`copy_art`]'s do, each with its raster effects. Returns the art +/// one piece. The copies stop where [`copy_art`]'s do, each with its raster effects. The copies +/// inside `piece` that an earlier copying Transform effect of `n` made, which carry `n`'s id, have +/// their raster effects moved, turned, reflected and scaled with each copy in the same way +/// ([`follow_carried`]). The copies are made about `bounds`, the piece's bounds. Returns the art /// and the raster effects from `t` on, as they ran. -pub(crate) fn copied_container_art(doc: Option<&Document>, n: &Node, t: usize, piece: Node) -> (Node, Vec) { +pub(crate) fn copied_container_art(doc: Option<&Document>, n: &Node, t: usize, piece: Node, bounds: Option) -> (Node, Vec) { let rest = n.appearance.effects.get(t..).unwrap_or_default(); let own = piece.appearance.effects.clone(); + let original = piece.clone(); let mut art = Node::group(n.id, vec![Arc::new(piece)]); - if let (Some(e), Some(bounds)) = (rest.first(), art.geometric_bounds()) { + if let (Some(e), Some(bounds)) = (rest.first(), bounds) { let params = merged_params(&e.id, &e.params); let (m, copies) = crate::distort::transform_map(bounds, ¶ms, n.id.0); let max = Budget::EFFECTS.at_least(art_cost(&art)); @@ -218,6 +222,7 @@ pub(crate) fn copied_container_art(doc: Option<&Document>, n: &Node, t: usize, p let copy = Arc::make_mut(c); copy.appearance.effects = own.clone(); crate::raster::follow_contents(&mut copy.appearance.effects, a, Affine::IDENTITY); + follow_carried(copy, &original, n.id, a); } } } @@ -232,6 +237,20 @@ pub(crate) fn copied_container_art(doc: Option<&Document>, n: &Node, t: usize, p (art, effects) } +/// Give the members of `copy` with `id` (the copies that an earlier copying Transform effect of a +/// group or layer made, [`copied_container_art`]), at every depth below such members, the raster +/// effects of the matching members of `original`, the piece `copy` was made from, made to follow +/// `a` ([`crate::raster::follow_contents`]). +fn follow_carried(copy: &mut Node, original: &Node, id: NodeId, a: Affine) { + let (Some(ch), Some(was)) = (copy.children_mut(), original.children()) else { return }; + for (c, w) in ch.iter_mut().zip(was).filter(|(c, w)| w.id == id && c.id == id) { + let c = Arc::make_mut(c); + c.appearance.effects = w.appearance.effects.clone(); + crate::raster::follow_contents(&mut c.appearance.effects, a, Affine::IDENTITY); + follow_carried(c, w, id, a); + } +} + /// Is `e` a visible live-effect plug-in whose manifest declares `paintOutput`: its id's plug-in id, /// else `None`. fn painted_plugin(e: &Effect) -> Option<&str> { diff --git a/crates/effects/src/tests/rasterfollow.rs b/crates/effects/src/tests/rasterfollow.rs index 0dbbe401fc..3c59d3e773 100644 --- a/crates/effects/src/tests/rasterfollow.rs +++ b/crates/effects/src/tests/rasterfollow.rs @@ -907,6 +907,71 @@ fn a_layers_guides_stay_once_above_copies_that_carry_its_shadow() { assert_eq!(shadows_in(&art).len(), 3); } +/// The bounds of every node with `id` under `art`, in order. +fn bounds_of(art: &Node, id: u64) -> Vec { + let mut out = vec![]; + art.walk(&mut |m| { + if m.id == NodeId(id) + && let Some(b) = m.geometric_bounds() + { + out.push(b); + } + }); + out +} + +fn rects_near(a: &[Rect], b: &[Rect]) -> bool { + let near = |p: f64, q: f64| (p - q).abs() < 1e-6; + a.len() == b.len() && a.iter().zip(b).all(|(p, q)| near(p.x0, q.x0) && near(p.y0, q.y0) && near(p.x1, q.x1) && near(p.y1, q.y1)) +} + +/// A group with a fill of its own and only a shadow before its copying Transform effect applies +/// its members' own effects before it copies them, as a group without a fill does: a member's own +/// move is turned with each copy. +#[test] +fn a_painted_groups_copies_take_their_members_own_effects_first() { + let members = || vec![square(171, vec![transform(json!({"moveH": 40}))]), square(172, vec![])]; + let effects = || vec![shadow(10.0, 0.0, 0.0), transform(json!({"rotate": 90, "copies": 1}))]; + let plain = crate::evaluate_container(None, &group(170, members(), effects())).unwrap(); + let mut painted = group(170, members(), effects()); + *painted.appearance = Appearance::basic(Paint::solid(Color::rgb(0.0, 0.0, 1.0)), Paint::None, 0.0); + painted.appearance.effects = effects(); + let art = crate::evaluate_container(None, &painted).unwrap(); + let (want, got) = (bounds_of(&plain, 171), bounds_of(&art, 171)); + assert!(!want.is_empty()); + assert!(rects_near(&got, &want), "{got:?} {want:?}"); +} + +/// A layer's copies are made about the bounds of its visible members, as the layer measures them: +/// a hidden member does not move the center that a reflected copy turns about. +#[test] +fn a_layers_copies_leave_its_hidden_members_out_of_their_reference() { + let mut hidden = path_node(182, shapes::rectangle(Rect::new(1000.0, 0.0, 1050.0, 50.0)), vec![]); + hidden.visible = false; + let l = layer(180, vec![square(181, vec![]), hidden], vec![shadow(10.0, 0.0, 0.0), transform(json!({"reflectX": true, "copies": 1}))]); + let art = crate::evaluate_container(None, &l).unwrap(); + let got = bounds_of(&art, 181); + assert!(rects_near(&got, &[Rect::new(0.0, 0.0, 50.0, 50.0); 2]), "{got:?}"); +} + +/// A shadow carried by the copies of an earlier copying Transform effect of a group turns with each +/// copy of a later one, drawn and baked. +#[test] +fn a_groups_later_copying_transform_turns_the_shadows_of_earlier_copies() { + let g = || { + group( + 190, + vec![square(191, vec![])], + vec![shadow(10.0, 0.0, 0.0), transform(json!({"moveH": 100, "copies": 1})), transform(json!({"rotate": 90, "copies": 1}))], + ) + }; + let want = [(0.0, -10.0), (0.0, -10.0), (10.0, 0.0), (10.0, 0.0)]; + let art = crate::evaluate_container(None, &g()).unwrap(); + assert!(near(&shadows_in(&art), &want), "{:?}", shadows_in(&art)); + let b = baked(g()); + assert!(near(&shadows_in(&b), &want), "baked {:?}", shadows_in(&b)); +} + /// A group holding type whose Transform effect only moves its copies, baked for export, gives each /// copy the shadow listed before that effect, as the canvas draws it, rather than keeping the type /// as type under one shadow around all the copies. Built without fonts, type lays out no glyphs