From bed9c71f61edd8534dd47e1fd268f12e226a39f7 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 15 Nov 2026 23:45:35 -0800 Subject: [PATCH] Effects: a group's copies keep their random moves when the raster budget leaves one Expand Appearance gave each copy of a group's or layer's Transform effect new ids after expanding it. When the raster budget refused the image for a copy that held a random Transform effect, the copy stayed unexpanded, but its new ids gave that effect a new seed, so the copy moved. The group or layer now stays as it is in that case, as it does when its own raster effects go past the budget. That rollback also removes the images its earlier copies added to the document and gives their charge back to the budget, so later objects in the same command can still use it. --- crates/engine/src/cmd/effectcmd.rs | 44 +++++++++++++++++------- crates/engine/src/cmd/effectcmd/tests.rs | 39 +++++++++++++++++++++ 2 files changed, 71 insertions(+), 12 deletions(-) diff --git a/crates/engine/src/cmd/effectcmd.rs b/crates/engine/src/cmd/effectcmd.rs index 0dfb967422..f21adcbc10 100644 --- a/crates/engine/src/cmd/effectcmd.rs +++ b/crates/engine/src/cmd/effectcmd.rs @@ -1246,7 +1246,9 @@ fn expand_node_except( /// ([`has_seeded`]) expands before its ids change ([`expand_in_place`]), as [`expand_pieces`] does, /// and `members` gets what that expansion lists, by the copy's new id. `kept` gets the new id of /// each copy of an object `skip` lists, and `out` gets `n` unless `listed` says it is there. -/// Returns whether `n` changed; any other object stays as it is. +/// Returns whether `n` changed; any other object stays as it is. `Err`, with `n` as it is, when +/// such a copy still holds a random Transform effect after the raster budget left part of it as +/// it is: its new ids would give that effect another seed. fn copies_carrying_raster( d: &mut Document, n: &mut Node, @@ -1255,15 +1257,19 @@ fn copies_carrying_raster( members: &mut std::collections::HashMap>, (out, listed): (&mut Vec, &mut bool), raster: &mut RasterBudget, -) -> bool { - if !is_container(n) || effects::outer_effects(n).len() == n.appearance.effects.len() { - return false; +) -> std::result::Result { + if !carries_copies(n) { + return Ok(false); } - let Some(mut m) = effects::evaluate_container(Some(&*d), n) else { return false }; + let Some(mut m) = effects::evaluate_container(Some(&*d), n) else { return Ok(false) }; let mut seen = std::collections::HashSet::from([n.id]); for c in m.children_mut().into_iter().flatten().filter(|c| !c.is_ruler_guide()) { let c = Arc::make_mut(c); + let left = raster.left; let mut expanded = (has_seeded(c) && repeats_an_id(c, &seen)).then(|| expand_in_place(d, c, brushes, skip, raster)); + if expanded.is_some() && raster.left > left && has_seeded(c) { + return Err(OverBudget); + } let mut was = vec![]; if !skip.is_empty() { c.walk(&mut |n| was.push(n.id)); @@ -1286,7 +1292,13 @@ fn copies_carrying_raster( *listed = true; } *n = m; - true + Ok(true) +} + +/// Is `n` a group or layer whose Transform effect's copies each carry the raster effects listed +/// before it ([`copies_carrying_raster`])? +fn carries_copies(n: &Node) -> bool { + is_container(n) && effects::outer_effects(n).len() != n.appearance.effects.len() } /// [`expand_node`] of `n`, which need not be in `d`: `n` with its appearance expanded, `None` when @@ -1318,10 +1330,13 @@ fn expanded_node( } _ => None, }; - // The embedded images before the adjustment, when the raster budget may yet leave `n` as it - // is: the recolored copies the adjustment adds then go again. - let images_before = (effects::has_adjustment(&n) && !rasterfx::raster_fx(&n).is_empty()) + // The embedded images before the adjustment and the expansions of the copies + // ([`copies_carrying_raster`]), when the raster budget may yet leave `n` as it is: the + // recolored copies and the images those add then go again. + let images_before = ((effects::has_adjustment(&n) && !rasterfx::raster_fx(&n).is_empty()) || carries_copies(&n)) .then(|| d.images.keys().cloned().collect::>()); + // What the raster budget had left before those expansions: it gets back what they took. + let budget_before = (raster.images, raster.pixels); if effects::has_adjustment(&n) && let Some(m) = effects::adjust_in_document(d, &n, symbol_art) { @@ -1372,14 +1387,19 @@ fn expanded_node( // Members of a group or layer that `expand_pieces` or `copies_carrying_raster` expanded, by id, // with what their expansion lists. let mut members = std::collections::HashMap::new(); - changed |= copies_carrying_raster(d, &mut n, (brushes, skip), &mut kept, &mut members, (out, &mut listed_n), raster); + let copies = copies_carrying_raster(d, &mut n, (brushes, skip), &mut kept, &mut members, (out, &mut listed_n), raster); // Raster effects whose image would go past the budget stay: so does the object, with what is - // inside it and its color adjustments (a symbol instance stays an instance of its symbol). - let Ok(image) = raster_image(d, &n, raster) else { + // inside it and its color adjustments (a symbol instance stays an instance of its symbol), and + // so does a group or layer whose copies keep a random Transform effect the budget left. + let Ok(image) = copies.and_then(|copied| { + changed |= copied; + raster_image(d, &n, raster) + }) else { out.truncate(listed_before); if let Some(keys) = images_before { d.images.retain(|k, _| keys.contains(k)); } + (raster.images, raster.pixels) = budget_before; return None; }; let mut v = n.clone(); diff --git a/crates/engine/src/cmd/effectcmd/tests.rs b/crates/engine/src/cmd/effectcmd/tests.rs index 5a1146f28f..d035c24860 100644 --- a/crates/engine/src/cmd/effectcmd/tests.rs +++ b/crates/engine/src/cmd/effectcmd/tests.rs @@ -1150,3 +1150,42 @@ fn an_image_past_the_pixels_left_stays_unmade() { let mut budget = super::RasterBudget { images: 0, pixels, left: 0 }; assert!(super::raster_image(&mut d, &n, &mut budget).is_err(), "no image is left"); } + +/// A group whose Transform effect's copies each carry the drop shadow listed before it, with a +/// symbol instance whose random Transform effect takes its seed from the instance's id, stays as it +/// is when the raster budget has no image left for a copy's shadow, with none left or with one for +/// the first copy's: the copies keep the ids their random moves are drawn with, the group draws as +/// before, and the document holds no image the first copy's expansion made, nor the budget its +/// charge. +#[test] +fn copies_the_raster_budget_leaves_keep_their_random_moves() { + let (mut s, id) = session_with_rect(); + s.execute("select.set", &json!({"ids": [id.0]})).unwrap(); + let id = NodeId(s.execute("symbol.new", &json!({"name": "Box"})).unwrap()["id"].as_u64().unwrap()); + let random = json!({"moveH": 40, "moveV": 40, "random": true}); + s.execute("effect.apply", &json!({"ids": [id.0], "effect": "distort.transform", "params": random})).unwrap(); + s.execute("select.set", &json!({"ids": [id.0]})).unwrap(); + let g = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap()); + let shadow = json!({"x": 10, "y": 0, "blur": 0, "opacity": 100, "mode": "normal", "color": "#00ff00"}); + s.execute("effect.apply", &json!({"ids": [g.0], "effect": "stylize.dropShadow", "params": shadow})).unwrap(); + s.execute("effect.apply", &json!({"ids": [g.0], "effect": "distort.transform", "params": {"moveV": 150, "copies": 1}})).unwrap(); + let draw = |d: &vectorcraft_doc::Document| { + vectorcraft_render::Renderer::new().render(d, 400, 400, vectorcraft_geom::Affine::IDENTITY, &Default::default()).pixels + }; + for images in [0, 1] { + let mut d = s.doc().unwrap().doc.as_ref().clone(); + let before = d.node(g).cloned().unwrap(); + assert!(effects::outer_effects(&before).len() < before.appearance.effects.len(), "each copy carries the shadow"); + let (drawn, keys) = (draw(&d), d.images.keys().cloned().collect::>()); + + let mut budget = super::RasterBudget { images, pixels: u64::MAX, left: 0 }; + let mut out = vec![]; + super::expand_node(&mut d, g, &[], &mut out, &mut budget); + assert!(budget.left > 0, "{images}: a copy's shadow is left"); + assert!(out.is_empty(), "{images}: nothing expands: {out:?}"); + assert_eq!(d.node(g), Some(&before), "{images}: the group stays as it is"); + assert!(draw(&d) == drawn, "{images}: the group draws as before"); + assert_eq!(d.images.keys().cloned().collect::>(), keys, "{images}: no image is added"); + assert_eq!((budget.images, budget.pixels), (images, u64::MAX), "{images}: the budget gets back what the copies took"); + } +}