mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-17 11:58:06 +00:00
Merge pull request #8013 from dotCooCoo/fix/transform-copies-raster-effects-groups
Effects: each copy of a group's or layer's Transform effect carries the raster effects before it
This commit is contained in:
@@ -1238,6 +1238,69 @@ fn expand_node_except(
|
||||
}
|
||||
}
|
||||
|
||||
/// Replace `n`, a group or layer whose Transform effect's copies each carry the raster effects
|
||||
/// listed before it ([`effects::outer_effects`]), with its art ([`effects::evaluate_container`]):
|
||||
/// the copies become groups of their own with those effects, and they and what they hold get ids
|
||||
/// of their own, so they expand as its members do; the raster effects after the Transform effect
|
||||
/// stay on `n`. A copy that repeats an id ([`repeats_an_id`]) and holds a random Transform effect
|
||||
/// ([`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. `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,
|
||||
(brushes, skip): (&[vectorcraft_brush::Brush], &[NodeId]),
|
||||
kept: &mut Vec<NodeId>,
|
||||
members: &mut std::collections::HashMap<NodeId, Vec<Listed>>,
|
||||
(out, listed): (&mut Vec<Listed>, &mut bool),
|
||||
raster: &mut RasterBudget,
|
||||
) -> std::result::Result<bool, OverBudget> {
|
||||
if !carries_copies(n) {
|
||||
return Ok(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));
|
||||
}
|
||||
fresh_ids_listed(d, c, &mut seen, expanded.as_deref_mut().unwrap_or_default());
|
||||
// A copy of an object `skip` lists is left as it is too.
|
||||
let mut at = 0;
|
||||
c.walk(&mut |n| {
|
||||
if was.get(at).is_some_and(|old| *old != n.id && skip.contains(old)) {
|
||||
kept.push(n.id);
|
||||
}
|
||||
at += 1;
|
||||
});
|
||||
if let Some(expanded) = expanded {
|
||||
members.insert(c.id, expanded);
|
||||
}
|
||||
}
|
||||
if !*listed {
|
||||
out.push((n.id, 0));
|
||||
*listed = true;
|
||||
}
|
||||
*n = m;
|
||||
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
|
||||
/// it has nothing to expand. `out` gets the objects whose appearance expands, by places in the walk
|
||||
/// of the expanded `n`, `n`'s first when its own does. The objects `skip` lists are not expanded.
|
||||
@@ -1267,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::<std::collections::HashSet<_>>());
|
||||
// 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)
|
||||
{
|
||||
@@ -1316,23 +1382,30 @@ fn expanded_node(
|
||||
}
|
||||
return Some(m);
|
||||
}
|
||||
// New ids of the copies of objects `skip` lists among the members.
|
||||
let mut kept = vec![];
|
||||
// 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();
|
||||
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();
|
||||
rasterfx::strip_raster(&mut v, false);
|
||||
let mut stroke_art =
|
||||
|d: &mut Document, path: &PathData, rule: FillRule, st: &StrokeLayer| super::pathops::outlined_stroke(d, brushes, path, rule, st);
|
||||
// Members of a group or layer that `expand_pieces` expanded, by id, with what their expansion lists.
|
||||
let mut members = std::collections::HashMap::new();
|
||||
// New ids of the copies of objects `skip` lists among those members.
|
||||
let mut kept = vec![];
|
||||
let expanded = match image {
|
||||
// Blur, feather and inner glow change the object itself: it becomes the image (a layer
|
||||
// keeps it as its only member).
|
||||
@@ -1344,7 +1417,7 @@ fn expanded_node(
|
||||
} else if container {
|
||||
// A group's or layer's own fills and strokes become art among its members.
|
||||
effects::evaluate_container(Some(&*d), &v).map(|mut m| {
|
||||
members = expand_pieces(d, &mut m, false, brushes, skip, &mut kept, &mut stroke_art, raster);
|
||||
members.extend(expand_pieces(d, &mut m, false, brushes, skip, &mut kept, &mut stroke_art, raster));
|
||||
m
|
||||
})
|
||||
} else if matches!(n.kind, NodeKind::Path { .. } | NodeKind::Compound { .. })
|
||||
|
||||
@@ -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::<Vec<_>>());
|
||||
|
||||
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::<Vec<_>>(), keys, "{images}: no image is added");
|
||||
assert_eq!((budget.images, budget.pixels), (images, u64::MAX), "{images}: the budget gets back what the copies took");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -302,6 +302,136 @@ fn each_copys_fill_shadow_expands_as_the_canvas_draws_it() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Each copy of a group's Transform effect draws the drop shadow listed before the effect under
|
||||
/// itself, of both its squares together and scaled with it, and Expand Appearance keeps those
|
||||
/// shadows: the squares (20..40 together) have their shadow 20 pt to their right, and their copy
|
||||
/// scaled 200 % and moved 80 pt down (10..50 across, 90..130 down) has its own 40 pt to its right.
|
||||
/// A copy moved 30 pt right (50..70) lies under the squares' shadow (40..60), which still paints
|
||||
/// over it once expanded. The expanded art has no effects left, and each object in it an id of its
|
||||
/// own.
|
||||
#[test]
|
||||
fn each_copys_shadow_on_a_group_expands_as_the_canvas_draws_it() {
|
||||
let cases = [
|
||||
(json!({"scaleH": 200, "scaleV": 200, "moveV": 80, "copies": 1}), (80, 110), "the copy's shadow"),
|
||||
(json!({"moveH": 30, "copies": 1}), (55, 30), "the squares' shadow over the copy"),
|
||||
];
|
||||
for (params, (x, y), what) in cases {
|
||||
let mut s = session();
|
||||
let mut ids = vec![];
|
||||
for x in [20, 30] {
|
||||
ids.push(run(&mut s, "shape.rectangle", json!({"x": x, "y": 20, "width": 10, "height": 20}))["id"].as_u64().unwrap());
|
||||
run(&mut s, "paint.setFill", json!({"color": "#ff0000"}));
|
||||
run(&mut s, "paint.setStroke", json!({"none": true}));
|
||||
}
|
||||
run(&mut s, "select.set", json!({"ids": ids}));
|
||||
run(&mut s, "object.group", json!({}));
|
||||
let shadow = json!({"x": 20, "y": 0, "blur": 0, "opacity": 100, "mode": "normal", "color": "#00ff00"});
|
||||
run(&mut s, "effect.apply", json!({"effect": "stylize.dropShadow", "params": shadow}));
|
||||
run(&mut s, "effect.apply", json!({"effect": "distort.transform", "params": params}));
|
||||
let before = render(&s);
|
||||
assert_eq!(before.pixel(x, y), [0, 255, 0, 255], "{params}: {what}");
|
||||
run(&mut s, "effect.expandAppearance", json!({}));
|
||||
let after = render(&s);
|
||||
let diff = differing(&before, &after, 64);
|
||||
assert_eq!(diff, 0, "{params}: {diff} pixels differ");
|
||||
let doc = &s.doc().unwrap().doc;
|
||||
let (mut seen, mut count, mut effects) = (std::collections::HashSet::new(), 0, 0);
|
||||
for l in &doc.layers {
|
||||
l.walk(&mut |n| {
|
||||
seen.insert(n.id);
|
||||
count += 1;
|
||||
effects += n.appearance.effects.len();
|
||||
});
|
||||
}
|
||||
assert_eq!(seen.len(), count, "{params}: each object has an id of its own");
|
||||
assert_eq!(effects, 0, "{params}: no effects are left");
|
||||
}
|
||||
}
|
||||
|
||||
/// A member's random Transform effect takes its seed from the member's id, and the canvas draws
|
||||
/// every copy of the group with that id: Expand Appearance of a group with a stroke of its own, a
|
||||
/// shadow and a copying Transform effect gives each copy's symbol instance the same random move, so
|
||||
/// the copies, 60 pt apart, still draw alike.
|
||||
#[test]
|
||||
fn each_copys_random_member_expands_as_the_canvas_draws_it() {
|
||||
let mut s = session();
|
||||
let mut ids = vec![];
|
||||
for x in [20, 50] {
|
||||
ids.push(run(&mut s, "shape.rectangle", json!({"x": x, "y": 20, "width": 20, "height": 20}))["id"].as_u64().unwrap());
|
||||
run(&mut s, "paint.setFill", json!({"color": "#ff0000"}));
|
||||
run(&mut s, "paint.setStroke", json!({"none": true}));
|
||||
}
|
||||
run(&mut s, "select.set", json!({"ids": [ids[0]]}));
|
||||
ids[0] = run(&mut s, "symbol.new", json!({"name": "Box"}))["id"].as_u64().unwrap();
|
||||
run(&mut s, "select.set", json!({"ids": [ids[0]]}));
|
||||
run(&mut s, "effect.apply", json!({"effect": "distort.transform", "params": {"moveH": 16, "moveV": 16, "random": true}}));
|
||||
run(&mut s, "select.set", json!({"ids": ids}));
|
||||
let g = run(&mut s, "object.group", json!({}))["id"].as_u64().unwrap();
|
||||
run(&mut s, "select.set", json!({"ids": [g]}));
|
||||
run(&mut s, "appearance.addStroke", json!({}));
|
||||
assert!(node(&s, NodeId(g)).appearance.items.iter().any(|i| i.visible()), "the group has a stroke of its own");
|
||||
let shadow = json!({"x": 10, "y": 0, "blur": 0, "opacity": 100, "mode": "normal", "color": "#00ff00"});
|
||||
run(&mut s, "effect.apply", json!({"effect": "stylize.dropShadow", "params": shadow}));
|
||||
run(&mut s, "effect.apply", json!({"effect": "distort.transform", "params": {"moveV": 60, "copies": 2}}));
|
||||
// The pixels of each copy's 60 pt band that differ from the first copy's by more than 64.
|
||||
let unlike = |r: &vectorcraft_render::Rendered| {
|
||||
let px = |x: usize, y: usize| {
|
||||
let i = (y * r.width as usize + x) * 4;
|
||||
r.pixels.get(i..i + 4).map(<[u8]>::to_vec).unwrap_or_default()
|
||||
};
|
||||
let mut n = 0;
|
||||
for k in [60, 120] {
|
||||
for y in 0..60 {
|
||||
for x in 0..r.width as usize {
|
||||
n += usize::from(px(x, y).iter().zip(px(x, y + k)).any(|(a, b)| a.abs_diff(b) > 64));
|
||||
}
|
||||
}
|
||||
}
|
||||
n
|
||||
};
|
||||
let before = render(&s);
|
||||
assert_eq!(unlike(&before), 0, "the canvas draws the copies alike");
|
||||
let art = vectorcraft_render::effects::evaluate_container(Some(&s.doc().unwrap().doc), &node(&s, NodeId(g))).unwrap();
|
||||
let mut live = 0;
|
||||
art.walk(&mut |n| live += n.appearance.effects.iter().filter(|e| e.id == "distort.transform").count());
|
||||
assert_eq!(live, 3, "each copy's instance keeps its random effect live");
|
||||
run(&mut s, "effect.expandAppearance", json!({}));
|
||||
let after = render(&s);
|
||||
assert_eq!(unlike(&after), 0, "the expanded copies draw alike");
|
||||
assert!(differing(&before, &after, 64) < 200, "the copies stay where they are drawn");
|
||||
}
|
||||
|
||||
/// A member's own Offset Path applies before the copying Transform effect of its group, which has
|
||||
/// a stroke of its own, whether or not a shadow is listed before that Transform effect: the copy
|
||||
/// scaled 200 % draws the offset square scaled, with or without a shadow at 0 % opacity.
|
||||
#[test]
|
||||
fn a_members_offset_path_applies_before_its_painted_groups_copies() {
|
||||
let draw = |shadow: bool| {
|
||||
let mut s = session();
|
||||
let mut ids = vec![];
|
||||
for x in [20, 40] {
|
||||
ids.push(run(&mut s, "shape.rectangle", json!({"x": x, "y": 20, "width": 10, "height": 10}))["id"].as_u64().unwrap());
|
||||
run(&mut s, "paint.setFill", json!({"color": "#ff0000"}));
|
||||
run(&mut s, "paint.setStroke", json!({"none": true}));
|
||||
}
|
||||
run(&mut s, "select.set", json!({"ids": [ids[0]]}));
|
||||
run(&mut s, "effect.apply", json!({"effect": "path.offsetPath", "params": {"offset": 5}}));
|
||||
run(&mut s, "select.set", json!({"ids": ids}));
|
||||
let g = NodeId(run(&mut s, "object.group", json!({}))["id"].as_u64().unwrap());
|
||||
run(&mut s, "appearance.addStroke", json!({}));
|
||||
if shadow {
|
||||
let fx = json!({"x": 10, "y": 0, "blur": 0, "opacity": 0, "mode": "normal", "color": "#00ff00"});
|
||||
run(&mut s, "effect.apply", json!({"effect": "stylize.dropShadow", "params": fx}));
|
||||
}
|
||||
run(&mut s, "effect.apply", json!({"effect": "distort.transform", "params": {"scaleH": 200, "scaleV": 200, "moveV": 60, "copies": 1}}));
|
||||
let n = node(&s, g);
|
||||
assert_eq!(vectorcraft_render::effects::outer_effects(&n).len() < n.appearance.effects.len(), shadow, "each copy carries the shadow");
|
||||
render(&s)
|
||||
};
|
||||
let diff = differing(&draw(false), &draw(true), 64);
|
||||
assert_eq!(diff, 0, "{diff} pixels differ");
|
||||
}
|
||||
|
||||
/// Each copy of type's Transform effect draws the drop shadow listed before the effect under
|
||||
/// itself, scaled with it, and Expand Appearance keeps those shadows: the type's copy, scaled 200 %
|
||||
/// and moved 80 pt down, has its shadow 40 pt to the right of its letters rather than the type's
|
||||
|
||||
@@ -224,6 +224,45 @@ fn shadows_and_glows_keep_their_blend_modes() {
|
||||
assert_eq!(modes, [vectorcraft_color::BlendMode::Multiply, vectorcraft_color::BlendMode::Screen]);
|
||||
}
|
||||
|
||||
/// Each copy of a group's Transform effect has its own image of the shadow listed before the
|
||||
/// effect, under that copy: the squares' shadow (40..60) paints over their copy moved 30 pt right
|
||||
/// (50..70), as on the canvas, and the copies' objects have ids of their own.
|
||||
#[test]
|
||||
fn each_copys_shadow_on_a_group_flattens_under_that_copy() {
|
||||
let mut d = Document::new(200.0, 200.0);
|
||||
let members = [20.0, 30.0].map(|x| Arc::new(rect(&mut d, Rect::new(x, 40.0, x + 10.0, 60.0), Color::rgb(1.0, 0.0, 0.0))));
|
||||
let mut g = Node::group(d.alloc_id(), members.to_vec());
|
||||
g.appearance.effects = vec![
|
||||
Effect {
|
||||
id: "stylize.dropShadow".into(),
|
||||
params: json!({"x": 20.0, "y": 0.0, "blur": 0.0, "opacity": 100.0, "mode": "normal", "color": "#00ff00"}),
|
||||
visible: true,
|
||||
},
|
||||
Effect { id: "distort.transform".into(), params: json!({"moveH": 30.0, "copies": 1}), visible: true },
|
||||
];
|
||||
add(&mut d, g);
|
||||
let canvas = render(&d);
|
||||
let flat = crate::flatten_raster_effects(&d).unwrap();
|
||||
let back = render(&flat);
|
||||
let at = |r: &Rendered, x: usize| {
|
||||
let i = (50 * r.width as usize + x) * 4;
|
||||
(r.pixels[i], r.pixels[i + 1], r.pixels[i + 2])
|
||||
};
|
||||
for x in [55, 65, 85] {
|
||||
let (want, got) = (at(&canvas, x), at(&back, x));
|
||||
assert!(want.0.abs_diff(got.0) < 32 && want.1.abs_diff(got.1) < 32 && want.2.abs_diff(got.2) < 32, "x {x}: {want:?} {got:?}");
|
||||
}
|
||||
assert!(at(&back, 55).1 > 200 && at(&back, 55).0 < 60, "the squares' shadow over the copy: {:?}", at(&back, 55));
|
||||
let (mut seen, mut count) = (std::collections::HashSet::new(), 0);
|
||||
for l in &flat.layers {
|
||||
l.walk(&mut |n| {
|
||||
seen.insert(n.id);
|
||||
count += 1;
|
||||
});
|
||||
}
|
||||
assert_eq!(seen.len(), count, "each object has an id of its own");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pdf_resolution_cap_warns_once_for_original_object_when_appearance_items_split() {
|
||||
let mut d = Document::new(300.0, 300.0);
|
||||
|
||||
Reference in New Issue
Block a user