mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-22 14:09:20 -06:00
A Transform effect with copies on a group copied each member on its own, so one member's copies lay over the fills of the next member. Each copy is now a whole group, the original on top and each copy below the one before. A path's copies follow the same order. Guides are not copied, and the copies stop at the node limit. A group with its own fill or stroke, a clipping group and live type keep their previous copies.
823 lines
47 KiB
Rust
823 lines
47 KiB
Rust
use super::*;
|
||
use vectorcraft_color::{BlendMode, Color, Paint};
|
||
use vectorcraft_doc::{CharStyle, Effect, NodeId, TextObject};
|
||
use vectorcraft_geom::{Point, Rect, shapes};
|
||
|
||
/// Type tests need glyph outlines, which only the optional craft-fonts build input provides.
|
||
fn skip_without_craft_fonts() -> bool {
|
||
let skip = vectorcraft_text::CRAFT_FONTS.is_empty();
|
||
if skip {
|
||
eprintln!("skipped: built without craft-fonts (set CRAFT_FONTS_DIR to a craft-fonts checkout to run it)");
|
||
}
|
||
skip
|
||
}
|
||
|
||
fn sq(id: u64, x: f64, color: Color) -> Arc<Node> {
|
||
Arc::new(Node::path(NodeId(id), shapes::rectangle(Rect::new(x, 0.0, x + 10.0, 10.0)), Appearance::basic(Paint::solid(color), Paint::None, 0.0)))
|
||
}
|
||
|
||
fn group_with(effect: &str) -> Node {
|
||
let mut g = Node::group(NodeId(10), vec![sq(1, 0.0, Color::rgb(1.0, 0.0, 0.0)), sq(2, 5.0, Color::rgb(0.0, 0.0, 1.0))]);
|
||
g.appearance.effects.push(Effect { id: effect.into(), params: serde_json::json!({}), visible: true });
|
||
g
|
||
}
|
||
|
||
fn area(n: &Node) -> f64 {
|
||
po::area(n.path_data().unwrap(), FillRule::NonZero).abs()
|
||
}
|
||
|
||
fn overlap_cmyk(g: &Node) -> [f32; 4] {
|
||
let faces = pathfinder_children(None, g, None).unwrap();
|
||
assert_eq!(faces.len(), 3);
|
||
let overlap = faces
|
||
.iter()
|
||
.find(|n| {
|
||
let b = n.geometric_bounds().unwrap();
|
||
b.x0 >= 4.9 && b.x1 <= 10.1 && b.y0 <= 0.1 && b.y1 >= 9.9
|
||
})
|
||
.expect("overlap face");
|
||
assert!(overlap.appearance.stroke_paint().is_none());
|
||
overlap.appearance.fill_paint().color().unwrap().to_cmyk()
|
||
}
|
||
|
||
fn two_overlapping_cmyk_squares() -> Node {
|
||
let a = Appearance::basic(Paint::solid(Color::cmyk(0.2, 0.66, 0.4, 0.0)), Paint::solid(Color::BLACK), 2.0);
|
||
let b = Appearance::basic(Paint::solid(Color::cmyk(0.4, 0.2, 0.3, 0.1)), Paint::solid(Color::BLACK), 2.0);
|
||
Node::group(
|
||
NodeId(10),
|
||
vec![
|
||
Arc::new(Node::path(NodeId(1), shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)), a)),
|
||
Arc::new(Node::path(NodeId(2), shapes::rectangle(Rect::new(5.0, 0.0, 15.0, 10.0)), b)),
|
||
],
|
||
)
|
||
}
|
||
|
||
/// Effect › Pathfinder › Trap keeps the members and adds an overprinting strip of the lighter
|
||
/// colour inside the darker one along the edge where they meet.
|
||
#[test]
|
||
fn trap_effect_adds_an_overprinting_strip_above_the_members() {
|
||
let yellow = Color::cmyk(0.0, 0.0, 1.0, 0.0);
|
||
let blue = Color::cmyk(1.0, 0.6, 0.0, 0.0);
|
||
let mut g = Node::group(NodeId(10), vec![sq(1, 0.0, yellow), sq(2, 10.0, blue)]);
|
||
let params = serde_json::json!({"thickness": 1.0, "tintReduction": 100});
|
||
g.appearance.effects.push(Effect { id: crate::trap::TRAP.into(), params, visible: true });
|
||
let out = pathfinder_children(None, &g, None).unwrap();
|
||
assert_eq!(out.len(), 3, "both squares and one trap");
|
||
assert_eq!((out[0].id, out[1].id), (NodeId(1), NodeId(2)));
|
||
let b = out[2].geometric_bounds().unwrap();
|
||
assert!((b.x0 - 10.0).abs() < 0.02 && (b.x1 - 11.0).abs() < 0.02 && b.y0.abs() < 0.02 && (b.y1 - 10.0).abs() < 0.02, "{b:?}");
|
||
let fill = out[2].appearance.fill().unwrap();
|
||
assert!(fill.overprint);
|
||
assert_eq!(fill.paint.color(), Some(yellow));
|
||
}
|
||
|
||
#[test]
|
||
fn hard_mix_takes_the_highest_of_each_cmyk_channel_and_drops_strokes() {
|
||
let mut g = two_overlapping_cmyk_squares();
|
||
g.appearance.effects.push(Effect { id: "pathfinder.hardMix".into(), params: serde_json::json!({}), visible: true });
|
||
let [c, m, y, k] = overlap_cmyk(&g);
|
||
assert!((c - 0.4).abs() < 0.02 && (m - 0.66).abs() < 0.02 && (y - 0.4).abs() < 0.02 && (k - 0.1).abs() < 0.02, "{c} {m} {y} {k}");
|
||
}
|
||
|
||
#[test]
|
||
fn soft_mix_blends_cmyk_bottom_to_top_and_drops_strokes() {
|
||
let mut g = two_overlapping_cmyk_squares();
|
||
g.appearance.effects.push(Effect { id: "pathfinder.softMix".into(), params: serde_json::json!({"mixingRate": 50.0}), visible: true });
|
||
let [c, m, y, k] = overlap_cmyk(&g);
|
||
assert!((c - 0.3).abs() < 0.02 && (m - 0.43).abs() < 0.02 && (y - 0.35).abs() < 0.02 && (k - 0.05).abs() < 0.02, "{c} {m} {y} {k}");
|
||
}
|
||
|
||
#[test]
|
||
fn shape_modes_follow_the_pathfinder_panel() {
|
||
let add = pathfinder_children(None, &group_with("pathfinder.add"), None).unwrap();
|
||
assert_eq!(add.len(), 1);
|
||
assert!((area(&add[0]) - 150.0).abs() < 1e-3);
|
||
// Add keeps the front object's paint, Subtract the back one's.
|
||
assert_eq!(add[0].id, NodeId(2));
|
||
let sub = pathfinder_children(None, &group_with("pathfinder.subtract"), None).unwrap();
|
||
assert_eq!(sub[0].id, NodeId(1));
|
||
assert!((area(&sub[0]) - 50.0).abs() < 1e-3);
|
||
let int = pathfinder_children(None, &group_with("pathfinder.intersect"), None).unwrap();
|
||
assert!((area(&int[0]) - 50.0).abs() < 1e-3);
|
||
let div = pathfinder_children(None, &group_with("pathfinder.divide"), None).unwrap();
|
||
assert_eq!(div.len(), 3);
|
||
}
|
||
|
||
/// Divide and Outline draw no piece of a member without fill or stroke, the same as when such
|
||
/// pieces are left out.
|
||
#[test]
|
||
fn divide_and_outline_draw_nothing_for_unpainted_members() {
|
||
let unpainted =
|
||
Arc::new(Node::path(NodeId(2), shapes::rectangle(Rect::new(5.0, 0.0, 15.0, 10.0)), Appearance::basic(Paint::None, Paint::None, 0.0)));
|
||
for effect in ["pathfinder.divide", "pathfinder.outline"] {
|
||
let mut g = Node::group(NodeId(10), vec![sq(1, 0.0, Color::rgb(1.0, 0.0, 0.0)), unpainted.clone()]);
|
||
g.appearance.effects.push(Effect { id: effect.into(), params: serde_json::json!({}), visible: true });
|
||
let out = pathfinder_children(None, &g, None).unwrap();
|
||
assert!(out.iter().any(|n| n.id == NodeId(1)) && out.iter().any(|n| n.id == NodeId(2)), "{effect}: {out:#?}");
|
||
for n in out.iter().filter(|n| n.id == NodeId(2)) {
|
||
assert!(n.appearance.items.iter().all(|i| i.paint().is_none()), "{effect}: {n:#?}");
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Returns an "H" in blue type at 100 pt on a baseline at y = 80, with its stems on either
|
||
/// side of x = 100.
|
||
fn type_h() -> Node {
|
||
let style = CharStyle { size: 100.0, fill: Paint::solid(Color::rgb(0.0, 0.0, 1.0)), ..CharStyle::default() };
|
||
Node::new(NodeId(2), NodeKind::Text(Box::new(TextObject::point(Point::new(70.0, 80.0), "H", style))))
|
||
}
|
||
|
||
/// Returns a group with `effect` that holds a red 100 x 100 square and `front` in front of it.
|
||
fn square_and(front: Option<Node>, effect: &str) -> Node {
|
||
let red = Appearance::basic(Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
|
||
let square = Node::path(NodeId(1), shapes::rectangle(Rect::new(0.0, 0.0, 100.0, 100.0)), red);
|
||
let mut g = Node::group(NodeId(10), [Some(square), front].into_iter().flatten().map(Arc::new).collect());
|
||
g.appearance.effects.push(Effect { id: effect.into(), params: serde_json::json!({}), visible: true });
|
||
g
|
||
}
|
||
|
||
/// Returns the Pathfinder results of `g`, with type outlined as the renderer outlines it.
|
||
fn results(g: &Node) -> Vec<Arc<Node>> {
|
||
pathfinder_children(None, g, crate::text_outliner()).unwrap()
|
||
}
|
||
|
||
#[test]
|
||
fn type_changes_the_result_of_every_pathfinder_effect() {
|
||
if skip_without_craft_fonts() {
|
||
return;
|
||
}
|
||
let glyphs = crate::outline_text(&type_h()).unwrap();
|
||
let b = glyphs.geometric_bounds().unwrap();
|
||
assert!(b.x0 < 100.0 && b.x1 > 100.0 && b.y0 > 0.0 && b.y1 < 100.0, "{b:?}");
|
||
// `measure` gives the number of results and their total area.
|
||
let measure = |front: Option<Node>, effect: &str| {
|
||
let r = results(&square_and(front, effect));
|
||
(r.len(), r.iter().map(|n| area(n)).sum::<f64>())
|
||
};
|
||
let unchanged: Vec<_> = PATHFINDER_EFFECTS
|
||
.iter()
|
||
.map(|(id, label, _)| (label, measure(Some(type_h()), id), measure(None, id)))
|
||
.filter(|(_, with, without)| with.0 == without.0 && (with.1 - without.1).abs() <= 1.0)
|
||
.collect();
|
||
assert!(unchanged.is_empty(), "(label, with the type, without): {unchanged:?}");
|
||
// The shape modes take the glyph's whole area: the part inside the square and the part outside.
|
||
let glyph_area: f64 = glyphs.children().unwrap().iter().map(|n| area(n)).sum();
|
||
let with_type = |id: &str| measure(Some(type_h()), id).1;
|
||
assert!((with_type("pathfinder.add") - with_type("pathfinder.subtract") - glyph_area).abs() < 0.5, "{glyph_area}");
|
||
assert!((with_type("pathfinder.intersect") + with_type("pathfinder.minusBack") - glyph_area).abs() < 0.5, "{glyph_area}");
|
||
// Outline art nested one group deeper takes part as well.
|
||
let nested: OutlineHook = &|n: &Node| crate::outline_text(n).map(|g| Node::group(n.id, vec![Arc::new(g)]));
|
||
let sub = pathfinder_children(None, &square_and(Some(type_h()), "pathfinder.subtract"), Some(nested)).unwrap();
|
||
assert!((sub.iter().map(|n| area(n)).sum::<f64>() - with_type("pathfinder.subtract")).abs() < 1e-6);
|
||
}
|
||
|
||
#[test]
|
||
fn pieces_of_type_keep_its_paint_opacity_and_blend_mode() {
|
||
if skip_without_craft_fonts() {
|
||
return;
|
||
}
|
||
let mut t = type_h();
|
||
(t.opacity, t.blend) = (0.5, BlendMode::Multiply);
|
||
let look = |p: &Arc<Node>| (p.appearance.fill_paint(), p.opacity, p.blend);
|
||
let square = (Paint::solid(Color::rgb(1.0, 0.0, 0.0)), 1.0, BlendMode::Normal);
|
||
let glyph = (Paint::solid(Color::rgb(0.0, 0.0, 1.0)), 0.5, BlendMode::Multiply);
|
||
let looks: Vec<_> = results(&square_and(Some(t.clone()), "pathfinder.divide")).iter().map(look).collect();
|
||
assert!(looks.contains(&square) && looks.contains(&glyph) && looks.iter().all(|l| *l == square || *l == glyph), "{looks:?}");
|
||
// In the shape modes a group with the type in front is one shape, styled like the type.
|
||
let green = Appearance::basic(Paint::solid(Color::rgb(0.0, 1.0, 0.0)), Paint::None, 0.0);
|
||
let behind = Node::path(NodeId(3), shapes::rectangle(Rect::new(50.0, 50.0, 150.0, 150.0)), green);
|
||
let both = Node::group(NodeId(4), vec![Arc::new(behind), Arc::new(t)]);
|
||
let added: Vec<_> = results(&square_and(Some(both), "pathfinder.add")).iter().map(look).collect();
|
||
assert_eq!(added, [glyph]);
|
||
}
|
||
|
||
#[test]
|
||
fn a_random_transform_on_a_group_moves_its_members_together() {
|
||
let mut g = group_with("distort.transform");
|
||
g.appearance.effects[0].params = serde_json::json!({"moveH": 100, "moveV": 100, "random": true});
|
||
let art = evaluate_container(None, &g).unwrap();
|
||
let corner = |i: usize| art.children().and_then(|c| c.get(i)).and_then(|n| n.geometric_bounds()).map(|b| (b.x0, b.y0)).unwrap();
|
||
let (a, b) = (corner(0), corner(1));
|
||
assert!(a.0 > 0.0 && a.0 < 100.0, "moved by a share of the value: {a:?}");
|
||
assert!((b.0 - a.0 - 5.0).abs() < 1e-9 && (b.1 - a.1).abs() < 1e-9, "{a:?} {b:?}");
|
||
}
|
||
|
||
/// What [`evaluate_container`] makes of black 40 pt type "IIII" (id 3), changed by `style`,
|
||
/// in a group with a square and Offset Path 0, which leaves every path as it is.
|
||
fn type_in_group(style: impl FnOnce(&mut Node)) -> Node {
|
||
let ts = vectorcraft_doc::CharStyle { size: 40.0, fill: Paint::solid(Color::BLACK), ..Default::default() };
|
||
let text = vectorcraft_doc::TextObject::point(vectorcraft_geom::Point::new(5.0, 60.0), "IIII", ts);
|
||
let mut t = Node::new(NodeId(3), NodeKind::Text(Box::new(text)));
|
||
style(&mut t);
|
||
let mut g = Node::group(NodeId(10), vec![sq(1, 0.0, Color::BLACK), Arc::new(t)]);
|
||
g.appearance.effects.push(Effect { id: "path.offsetPath".into(), params: serde_json::json!({"offset": 0}), visible: true });
|
||
let art = evaluate_container(None, &g).unwrap();
|
||
(*art.children().unwrap()[1]).clone()
|
||
}
|
||
|
||
#[test]
|
||
fn type_in_a_group_keeps_its_look_when_the_groups_geometry_effects_outline_it() {
|
||
if skip_without_craft_fonts() {
|
||
return;
|
||
}
|
||
use vectorcraft_color::BlendMode;
|
||
let art = type_in_group(|t| (t.opacity, t.blend) = (0.5, BlendMode::Multiply));
|
||
assert!(art.id == NodeId(3) && !matches!(art.kind, NodeKind::Text(_)), "{art:?}");
|
||
assert_eq!((art.opacity, art.blend), (0.5, BlendMode::Multiply));
|
||
// Its opacity mask, its own shadow, its knockout shape option and its visibility stay.
|
||
let mask_art = Node::path(NodeId(4), shapes::rectangle(Rect::new(0.0, 0.0, 50.0, 50.0)), Appearance::default());
|
||
let mask = vectorcraft_doc::OpacityMask::new(mask_art, true);
|
||
assert_eq!(type_in_group(|t| t.extras_mut().mask = Some(Box::new(mask.clone()))).extras().mask.as_deref(), Some(&mask));
|
||
let shadow = Effect { id: "stylize.dropShadow".into(), params: serde_json::json!({}), visible: true };
|
||
assert_eq!(type_in_group(|t| t.appearance.effects.push(shadow.clone())).appearance.effects, vec![shadow]);
|
||
assert!(type_in_group(|t| t.knockout_shape = true).knockout_shape);
|
||
assert!(!type_in_group(|t| t.visible = false).visible);
|
||
// Its own Transform moves its outlines 30 pt before the group's effect reshapes them.
|
||
let x0 = |n: Node| n.geometric_bounds().unwrap().x0;
|
||
let transform = Effect { id: "distort.transform".into(), params: serde_json::json!({"moveH": 30}), visible: true };
|
||
let moved = x0(type_in_group(|t| t.appearance.effects.push(transform)));
|
||
assert!((moved - x0(type_in_group(|_| {})) - 30.0).abs() < 1e-6, "{moved}");
|
||
}
|
||
|
||
#[test]
|
||
fn hidden_effect_or_plain_group_is_untouched() {
|
||
let mut g = group_with("pathfinder.add");
|
||
g.appearance.effects[0].visible = false;
|
||
assert!(pathfinder_children(None, &g, None).is_none());
|
||
assert!(!has_pathfinder(&sq(1, 0.0, Color::BLACK)));
|
||
}
|
||
|
||
#[test]
|
||
fn a_transform_on_a_group_moves_the_gradients_of_its_type_and_those_placed_on_its_paths() {
|
||
if skip_without_craft_fonts() {
|
||
return;
|
||
}
|
||
use vectorcraft_color::{Gradient, GradientGeom, GradientKind, GradientPaint, GradientStop};
|
||
let stops = vec![GradientStop::new(0.0, Color::rgb(1.0, 0.0, 0.0)), GradientStop::new(1.0, Color::rgb(0.0, 0.0, 1.0))];
|
||
let ramp = GradientPaint::new(Gradient::new(GradientKind::Linear, stops));
|
||
// The type's characters fit the gradient to its layout; the square has it placed across itself.
|
||
let style = vectorcraft_doc::CharStyle { size: 40.0, fill: Paint::Gradient(Box::new(ramp.clone())), ..Default::default() };
|
||
let text = vectorcraft_doc::TextObject::point(vectorcraft_geom::Point::new(5.0, 60.0), "IIII", style);
|
||
let t = Arc::new(Node::new(NodeId(3), NodeKind::Text(Box::new(text))));
|
||
let geom = GradientGeom { start: (0.0, 5.0).into(), end: (10.0, 5.0).into(), aspect: 1.0, focal: None };
|
||
let placed = Paint::Gradient(Box::new(GradientPaint { geom: Some(geom), ..ramp }));
|
||
let square = Arc::new(Node::path(NodeId(1), shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)), Appearance::basic(placed, Paint::None, 0.0)));
|
||
let start = |n: &Node| match n.appearance.fill_paint() {
|
||
Paint::Gradient(g) => g.geom.unwrap().start.x,
|
||
p => panic!("a gradient: {p:?}"),
|
||
};
|
||
let before = start(&crate::outline_text(&t).unwrap().children().unwrap()[0]);
|
||
let mut g = Node::group(NodeId(10), vec![square, t]);
|
||
g.appearance.effects.push(Effect { id: "distort.transform".into(), params: serde_json::json!({"moveH": 30}), visible: true });
|
||
let art = evaluate_container(None, &g).unwrap();
|
||
let members = art.children().unwrap();
|
||
let after = start(&members[1].children().unwrap()[0]);
|
||
assert!((after - before - 30.0).abs() < 1e-6, "the type's gradient starts at {before}, then {after}");
|
||
assert!((start(&members[0]) - 30.0).abs() < 1e-6, "the square's gradient moves with it");
|
||
}
|
||
|
||
#[test]
|
||
fn a_groups_transform_moves_the_gradient_of_its_own_fill() {
|
||
use vectorcraft_color::{Gradient, GradientGeom, GradientKind, GradientPaint, GradientStop};
|
||
let stops = vec![GradientStop::new(0.0, Color::rgb(1.0, 0.0, 0.0)), GradientStop::new(1.0, Color::rgb(0.0, 0.0, 1.0))];
|
||
let ramp = GradientPaint::new(Gradient::new(GradientKind::Linear, stops));
|
||
let geom = GradientGeom { start: (0.0, 5.0).into(), end: (15.0, 5.0).into(), aspect: 1.0, focal: None };
|
||
let mut g = Node::group(NodeId(10), vec![sq(1, 0.0, Color::BLACK), sq(2, 5.0, Color::BLACK)]);
|
||
g.appearance
|
||
.items
|
||
.push(AppearanceItem::Fill(vectorcraft_doc::FillLayer::new(Paint::Gradient(Box::new(GradientPaint { geom: Some(geom), ..ramp })))));
|
||
g.appearance.effects.push(Effect { id: "distort.transform".into(), params: serde_json::json!({"moveH": 30}), visible: true });
|
||
let art = evaluate_container(None, &g).unwrap();
|
||
let mut starts = vec![];
|
||
art.walk(&mut |c: &Node| {
|
||
if let Paint::Gradient(p) = c.appearance.fill_paint() {
|
||
starts.push(p.geom.unwrap().start.x);
|
||
}
|
||
});
|
||
assert!(!starts.is_empty() && starts.iter().all(|s| (s - 30.0).abs() < 1e-6), "{starts:?}");
|
||
}
|
||
|
||
/// The art Pathfinder and compound shapes take (`shape_art`) paints as the canvas does: a path's
|
||
/// gradient and a group's own fill move with a Transform effect.
|
||
#[test]
|
||
fn shape_art_moves_gradients_with_a_transform_effect() {
|
||
use vectorcraft_color::{Gradient, GradientGeom, GradientKind, GradientPaint, GradientStop};
|
||
let stops = vec![GradientStop::new(0.0, Color::rgb(1.0, 0.0, 0.0)), GradientStop::new(1.0, Color::rgb(0.0, 0.0, 1.0))];
|
||
let ramp = GradientPaint::new(Gradient::new(GradientKind::Linear, stops));
|
||
let geom = GradientGeom { start: (0.0, 5.0).into(), end: (10.0, 5.0).into(), aspect: 1.0, focal: None };
|
||
let placed = Paint::Gradient(Box::new(GradientPaint { geom: Some(geom), ..ramp }));
|
||
let moved = || Effect { id: "distort.transform".into(), params: serde_json::json!({"moveH": 40}), visible: true };
|
||
let starts = |art: &Node| {
|
||
let mut out = vec![];
|
||
art.walk(&mut |c: &Node| {
|
||
for item in &c.appearance.items {
|
||
if let Paint::Gradient(p) = item.paint() {
|
||
out.push(p.geom.unwrap().start.x);
|
||
}
|
||
}
|
||
});
|
||
out
|
||
};
|
||
let mut path = Node::path(NodeId(1), shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)), Appearance::basic(placed.clone(), Paint::None, 0.0));
|
||
path.appearance.effects.push(moved());
|
||
let got = starts(&shape_art(None, &path, None).unwrap());
|
||
assert!(got.len() == 1 && (got[0] - 40.0).abs() < 1e-6, "the path's gradient: {got:?}");
|
||
let mut g = Node::group(NodeId(10), vec![sq(2, 0.0, Color::BLACK)]);
|
||
g.appearance.items.push(AppearanceItem::Fill(vectorcraft_doc::FillLayer::new(placed)));
|
||
g.appearance.effects.push(moved());
|
||
let got = starts(&shape_art(None, &g, None).unwrap());
|
||
assert!(!got.is_empty() && got.iter().all(|s| (s - 40.0).abs() < 1e-6), "the group's own fill: {got:?}");
|
||
}
|
||
|
||
/// A symbol instance's Transform effect moves the gradient placed on its art's own fill in the
|
||
/// art Pathfinder takes, as it moves its members.
|
||
#[test]
|
||
fn shape_art_moves_the_gradient_of_a_symbol_instances_art_with_its_transform() {
|
||
use vectorcraft_color::{Gradient, GradientGeom, GradientKind, GradientPaint, GradientStop};
|
||
let stops = vec![GradientStop::new(0.0, Color::rgb(1.0, 0.0, 0.0)), GradientStop::new(1.0, Color::rgb(0.0, 0.0, 1.0))];
|
||
let ramp = GradientPaint::new(Gradient::new(GradientKind::Linear, stops));
|
||
let geom = GradientGeom { start: (0.0, 5.0).into(), end: (10.0, 5.0).into(), aspect: 1.0, focal: None };
|
||
let placed = Paint::Gradient(Box::new(GradientPaint { geom: Some(geom), ..ramp }));
|
||
let mut art = Node::group(NodeId(20), vec![sq(21, 0.0, Color::BLACK)]);
|
||
art.appearance.items.push(AppearanceItem::Fill(vectorcraft_doc::FillLayer::new(placed)));
|
||
let hook = move |_: &Node| Some(art.clone());
|
||
let mut instance = Node::new(NodeId(1), NodeKind::SymbolInstance { symbol: "s".into(), xf: vectorcraft_geom::Affine::IDENTITY });
|
||
instance.appearance.effects.push(Effect { id: "distort.transform".into(), params: serde_json::json!({"moveH": 30}), visible: true });
|
||
let out = shape_art(None, &instance, Some(&hook)).unwrap();
|
||
let Paint::Gradient(g) = out.appearance.fill_paint() else { panic!("a gradient: {:?}", out.appearance) };
|
||
assert!((g.geom.unwrap().start.x - 30.0).abs() < 1e-6, "{:?}", g.geom);
|
||
assert!((out.geometric_bounds().unwrap().x0 - 30.0).abs() < 1e-6, "the art moves");
|
||
}
|
||
|
||
/// Under a Warp effect, the gradient placed on a symbol instance's art's own fill follows the art's
|
||
/// bounds, and the gradient placed on a compound shape follows its area's bounds, each from before
|
||
/// the warp to after it, in the art Pathfinder takes.
|
||
#[test]
|
||
fn shape_art_moves_the_gradients_of_a_symbol_instances_art_and_a_compound_shape_with_a_warp() {
|
||
use vectorcraft_color::{Gradient, GradientGeom, GradientKind, GradientPaint, GradientStop};
|
||
let stops = vec![GradientStop::new(0.0, Color::rgb(1.0, 0.0, 0.0)), GradientStop::new(1.0, Color::rgb(0.0, 0.0, 1.0))];
|
||
let ramp = GradientPaint::new(Gradient::new(GradientKind::Linear, stops));
|
||
let geom = GradientGeom { start: (2.0, 3.0).into(), end: (9.0, 3.0).into(), aspect: 1.0, focal: None };
|
||
let placed = Paint::Gradient(Box::new(GradientPaint { geom: Some(geom), ..ramp }));
|
||
let follow = |p: Point, from: Rect, to: Rect| {
|
||
Point::new(to.x0 + (p.x - from.x0) / from.width() * to.width(), to.y0 + (p.y - from.y0) / from.height() * to.height())
|
||
};
|
||
let check = |out: &Node, from: Rect, what: &str| {
|
||
let to = out.geometric_bounds().unwrap();
|
||
assert_ne!(from, to, "{what}: the warp changes the bounds");
|
||
let Paint::Gradient(g) = out.appearance.fill_paint() else { panic!("{what}: a gradient: {:?}", out.appearance) };
|
||
let got = g.geom.unwrap();
|
||
let (s, e) = (follow(geom.start, from, to), follow(geom.end, from, to));
|
||
assert!(got.start.distance(s) < 1e-6 && got.end.distance(e) < 1e-6, "{what}: {got:?}, want {s:?} {e:?}");
|
||
};
|
||
for style in ["arc", "rise", "bulge"] {
|
||
let warp = || Effect { id: format!("warp.{style}"), params: serde_json::json!({"bend": 40.0}), visible: true };
|
||
let mut art = Node::group(NodeId(20), vec![sq(21, 0.0, Color::BLACK)]);
|
||
art.appearance.items.push(AppearanceItem::Fill(vectorcraft_doc::FillLayer::new(placed.clone())));
|
||
let hook = move |_: &Node| Some(art.clone());
|
||
let mut instance = Node::new(NodeId(1), NodeKind::SymbolInstance { symbol: "s".into(), xf: vectorcraft_geom::Affine::IDENTITY });
|
||
instance.appearance.effects.push(warp());
|
||
check(&shape_art(None, &instance, Some(&hook)).unwrap(), Rect::new(0.0, 0.0, 10.0, 10.0), &format!("warp.{style} on a symbol instance"));
|
||
let mut shape = compound_shape(ShapeMode::Add);
|
||
shape.appearance = Appearance::basic(placed.clone(), Paint::None, 0.0).into();
|
||
shape.appearance.effects.push(warp());
|
||
check(&shape_art(None, &shape, None).unwrap(), Rect::new(0.0, 0.0, 15.0, 10.0), &format!("warp.{style} on a compound shape"));
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn a_groups_transform_and_free_distort_move_the_gradients_placed_on_its_paths() {
|
||
use vectorcraft_color::{Gradient, GradientGeom, GradientKind, GradientPaint, GradientStop};
|
||
let stops = vec![GradientStop::new(0.0, Color::rgb(1.0, 0.0, 0.0)), GradientStop::new(1.0, Color::rgb(0.0, 0.0, 1.0))];
|
||
let ramp = GradientPaint::new(Gradient::new(GradientKind::Linear, stops));
|
||
let geom = GradientGeom { start: (0.0, 5.0).into(), end: (10.0, 5.0).into(), aspect: 1.0, focal: None };
|
||
let placed = Paint::Gradient(Box::new(GradientPaint { geom: Some(geom), ..ramp }));
|
||
let ends = |e: Effect| {
|
||
let square =
|
||
Arc::new(Node::path(NodeId(1), shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)), Appearance::basic(placed.clone(), Paint::None, 0.0)));
|
||
let mut g = Node::group(NodeId(10), vec![square, sq(2, 20.0, Color::BLACK)]);
|
||
g.appearance.effects.push(e);
|
||
let art = evaluate_container(None, &g).unwrap();
|
||
match art.children().unwrap()[0].appearance.fill_paint() {
|
||
Paint::Gradient(g) => g.geom.map(|g| (g.start, g.end)).unwrap(),
|
||
p => panic!("a gradient: {p:?}"),
|
||
}
|
||
};
|
||
let near = |a: Point, b: Point| a.distance(b) < 1e-6;
|
||
// A quarter turn about the group's center (15, 5) takes (0, 5) to (15, 20) and (10, 5) to (15, 10).
|
||
let (s, e) = ends(Effect { id: "distort.transform".into(), params: serde_json::json!({"rotate": 90}), visible: true });
|
||
assert!(near(s, Point::new(15.0, 20.0)) && near(e, Point::new(15.0, 10.0)), "{s:?} {e:?}");
|
||
// Free Distort moving the group's top corners 10 pt right slants the square to 0..20 across:
|
||
// its gradient follows its own bounds (0..10 to 0..20), not the group's (0..30 to 0..40).
|
||
let corners = serde_json::json!({"corners": [[1.0 / 3.0, 0.0], [4.0 / 3.0, 0.0], [1.0, 1.0], [0.0, 1.0]]});
|
||
let (s, e) = ends(Effect { id: "distort.freeDistort".into(), params: corners, visible: true });
|
||
assert!(near(s, Point::new(0.0, 5.0)) && near(e, Point::new(20.0, 5.0)), "{s:?} {e:?}");
|
||
}
|
||
|
||
/// A group's Warp effect moves and scales the gradient placed on each of its paths from that path's
|
||
/// bounds before the warp to its bounds after it, not by the group's bounds.
|
||
#[test]
|
||
fn a_groups_warp_moves_the_gradients_placed_on_its_paths_by_each_paths_bounds() {
|
||
use vectorcraft_color::{Gradient, GradientGeom, GradientKind, GradientPaint, GradientStop};
|
||
let stops = vec![GradientStop::new(0.0, Color::rgb(1.0, 0.0, 0.0)), GradientStop::new(1.0, Color::rgb(0.0, 0.0, 1.0))];
|
||
let ramp = GradientPaint::new(Gradient::new(GradientKind::Linear, stops));
|
||
let geom = |x: f64| GradientGeom { start: (x + 2.0, 3.0).into(), end: (x + 8.0, 3.0).into(), aspect: 1.0, focal: None };
|
||
let placed = |x: f64| Paint::Gradient(Box::new(GradientPaint { geom: Some(geom(x)), ..ramp.clone() }));
|
||
let square = |id: u64, x: f64| {
|
||
Arc::new(Node::path(NodeId(id), shapes::rectangle(Rect::new(x, 0.0, x + 10.0, 10.0)), Appearance::basic(placed(x), Paint::None, 0.0)))
|
||
};
|
||
for style in ["arc", "bulge", "rise", "twist"] {
|
||
let mut g = Node::group(NodeId(10), vec![square(1, 0.0), square(2, 20.0)]);
|
||
g.appearance.effects.push(Effect { id: format!("warp.{style}"), params: serde_json::json!({"bend": 40.0}), visible: true });
|
||
let art = evaluate_container(None, &g).unwrap();
|
||
let mut moved = false;
|
||
for (i, x) in [0.0, 20.0].into_iter().enumerate() {
|
||
let m = &art.children().unwrap()[i];
|
||
let b = m.geometric_bounds().unwrap();
|
||
let follow = |p: Point| Point::new(b.x0 + (p.x - x) / 10.0 * b.width(), b.y0 + p.y / 10.0 * b.height());
|
||
let Paint::Gradient(gp) = m.appearance.fill_paint() else { panic!("a gradient") };
|
||
let got = gp.geom.unwrap();
|
||
let want = geom(x);
|
||
assert!(
|
||
got.start.distance(follow(want.start)) < 1e-6 && got.end.distance(follow(want.end)) < 1e-6,
|
||
"warp.{style} path {i}: {got:?} in {b:?}"
|
||
);
|
||
moved |= got.start.distance(want.start) > 0.1;
|
||
}
|
||
assert!(moved, "warp.{style} moves a gradient");
|
||
}
|
||
}
|
||
|
||
/// Under a group's Warp effect, the group's own placed gradients follow the group's bounds, a
|
||
/// nested group's own placed gradients follow the nested group's bounds, and a compound path's
|
||
/// placed gradients follow the compound path's bounds, each from before the warp to after it (a
|
||
/// stroke's: those bounds inflated by the stroke).
|
||
#[test]
|
||
fn a_groups_warp_moves_its_own_a_nested_groups_and_a_compound_paths_gradients_by_their_bounds() {
|
||
use vectorcraft_color::{Gradient, GradientGeom, GradientKind, GradientPaint, GradientStop};
|
||
let stops = vec![GradientStop::new(0.0, Color::rgb(1.0, 0.0, 0.0)), GradientStop::new(1.0, Color::rgb(0.0, 0.0, 1.0))];
|
||
let ramp = GradientPaint::new(Gradient::new(GradientKind::Linear, stops));
|
||
let geom = GradientGeom { start: (12.0, 4.0).into(), end: (26.0, 4.0).into(), aspect: 1.0, focal: None };
|
||
let placed = Paint::Gradient(Box::new(GradientPaint { geom: Some(geom), ..ramp }));
|
||
let own_fill = |n: &mut Node| {
|
||
n.appearance.items.push(AppearanceItem::Fill(vectorcraft_doc::FillLayer::new(placed.clone())));
|
||
n.appearance.items.push(AppearanceItem::Stroke(vectorcraft_doc::StrokeLayer::new(placed.clone(), 3.0)));
|
||
};
|
||
let follow = |p: Point, from: Rect, to: Rect| {
|
||
Point::new(to.x0 + (p.x - from.x0) / from.width() * to.width(), to.y0 + (p.y - from.y0) / from.height() * to.height())
|
||
};
|
||
for style in ["arc", "bulge", "rise", "flag"] {
|
||
let mut inner = Node::group(NodeId(5), vec![sq(1, 0.0, Color::BLACK)]);
|
||
own_fill(&mut inner);
|
||
let ring = vec![
|
||
Arc::new(Node::path(NodeId(6), shapes::rectangle(Rect::new(20.0, 0.0, 30.0, 14.0)), Appearance::default())),
|
||
Arc::new(Node::path(NodeId(7), shapes::rectangle(Rect::new(23.0, 3.0, 27.0, 7.0)), Appearance::default())),
|
||
];
|
||
let mut compound = Node::new(NodeId(3), NodeKind::Compound { children: ring, rule: FillRule::EvenOdd });
|
||
compound.appearance = Appearance::basic(placed.clone(), placed.clone(), 2.0).into();
|
||
let mut g = Node::group(NodeId(10), vec![Arc::new(inner), Arc::new(compound), sq(2, 40.0, Color::BLACK)]);
|
||
own_fill(&mut g);
|
||
let mut before = std::collections::HashMap::new();
|
||
g.walk(&mut |c: &Node| {
|
||
before.insert(c.id, c.geometric_bounds().unwrap());
|
||
});
|
||
g.appearance.effects.push(Effect { id: format!("warp.{style}"), params: serde_json::json!({"bend": 40.0}), visible: true });
|
||
let art = evaluate_container(None, &g).unwrap();
|
||
// The bounds after the warp of the group, the nested group and the compound path (the paths
|
||
// a group's own fill paints carry the group's id).
|
||
let mut after = std::collections::HashMap::new();
|
||
art.walk(&mut |c: &Node| {
|
||
if matches!(c.kind, NodeKind::Group { .. } | NodeKind::Compound { .. }) {
|
||
after.entry(c.id).or_insert_with(|| c.geometric_bounds().unwrap());
|
||
}
|
||
});
|
||
let mut checked = vec![];
|
||
art.walk(&mut |c: &Node| {
|
||
if ![NodeId(3), NodeId(5), NodeId(10)].contains(&c.id) {
|
||
return;
|
||
}
|
||
for item in &c.appearance.items {
|
||
let Paint::Gradient(gp) = item.paint() else { continue };
|
||
let (from, to) = (before[&c.id], after[&c.id]);
|
||
assert_ne!(from, to, "warp.{style} {:?}: the warp changes the bounds", c.id);
|
||
let stroke = match item {
|
||
AppearanceItem::Stroke(s) => Some(s),
|
||
AppearanceItem::Fill(_) => None,
|
||
};
|
||
let (from, to) = match stroke {
|
||
Some(s) => (s.paint_bounds(from), s.paint_bounds(to)),
|
||
None => (from, to),
|
||
};
|
||
let got = gp.geom.unwrap();
|
||
let (s, e) = (follow(geom.start, from, to), follow(geom.end, from, to));
|
||
assert!(
|
||
got.start.distance(s) < 1e-6 && got.end.distance(e) < 1e-6,
|
||
"warp.{style} {:?} (stroke: {}): {got:?}, want {s:?} {e:?} ({from:?} to {to:?})",
|
||
c.id,
|
||
stroke.is_some()
|
||
);
|
||
checked.push((c.id, stroke.is_some()));
|
||
}
|
||
});
|
||
checked.sort();
|
||
checked.dedup();
|
||
let want: Vec<_> = [NodeId(3), NodeId(5), NodeId(10)].into_iter().flat_map(|id| [(id, false), (id, true)]).collect();
|
||
assert_eq!(checked, want, "warp.{style}");
|
||
}
|
||
}
|
||
|
||
/// A group nested in a group with a Transform effect or Free Distort keeps its own placed gradient
|
||
/// on its members: the Transform's map moves it, and Free Distort moves and scales it from the
|
||
/// nested group's bounds before the effect to its bounds after it.
|
||
#[test]
|
||
fn a_nested_groups_own_gradient_follows_the_outer_groups_transform_and_free_distort() {
|
||
use vectorcraft_color::{Gradient, GradientGeom, GradientKind, GradientPaint, GradientStop};
|
||
let stops = vec![GradientStop::new(0.0, Color::rgb(1.0, 0.0, 0.0)), GradientStop::new(1.0, Color::rgb(0.0, 0.0, 1.0))];
|
||
let ramp = GradientPaint::new(Gradient::new(GradientKind::Linear, stops));
|
||
let geom = GradientGeom { start: (0.0, 5.0).into(), end: (10.0, 5.0).into(), aspect: 1.0, focal: None };
|
||
let placed = Paint::Gradient(Box::new(GradientPaint { geom: Some(geom), ..ramp }));
|
||
let ends = |e: Effect| {
|
||
let mut inner = Node::group(NodeId(5), vec![sq(1, 0.0, Color::BLACK)]);
|
||
inner.appearance.items.push(AppearanceItem::Fill(vectorcraft_doc::FillLayer::new(placed.clone())));
|
||
let mut g = Node::group(NodeId(10), vec![Arc::new(inner), sq(2, 20.0, Color::BLACK)]);
|
||
g.appearance.effects.push(e);
|
||
let art = evaluate_container(None, &g).unwrap();
|
||
match art.children().unwrap()[0].appearance.fill_paint() {
|
||
Paint::Gradient(g) => g.geom.map(|g| (g.start, g.end)).unwrap(),
|
||
p => panic!("a gradient: {p:?}"),
|
||
}
|
||
};
|
||
let near = |a: Point, b: Point| a.distance(b) < 1e-6;
|
||
let (s, e) = ends(Effect { id: "distort.transform".into(), params: serde_json::json!({"moveH": 30}), visible: true });
|
||
assert!(near(s, Point::new(30.0, 5.0)) && near(e, Point::new(40.0, 5.0)), "{s:?} {e:?}");
|
||
let corners = serde_json::json!({"corners": [[1.0 / 3.0, 0.0], [4.0 / 3.0, 0.0], [1.0, 1.0], [0.0, 1.0]]});
|
||
let (s, e) = ends(Effect { id: "distort.freeDistort".into(), params: corners, visible: true });
|
||
assert!(near(s, Point::new(0.0, 5.0)) && near(e, Point::new(20.0, 5.0)), "{s:?} {e:?}");
|
||
}
|
||
|
||
/// An image or a symbol instance that a group's Transform effect moves takes the placed gradients
|
||
/// of its own fills along, as a path does, and each copy takes them as many times as it takes the
|
||
/// transform.
|
||
#[test]
|
||
fn a_groups_transform_moves_the_gradients_placed_on_an_images_own_fills() {
|
||
use vectorcraft_color::{Gradient, GradientGeom, GradientKind, GradientPaint, GradientStop};
|
||
use vectorcraft_doc::ImageObject;
|
||
let stops = vec![GradientStop::new(0.0, Color::rgb(1.0, 0.0, 0.0)), GradientStop::new(1.0, Color::rgb(0.0, 0.0, 1.0))];
|
||
let ramp = GradientPaint::new(Gradient::new(GradientKind::Linear, stops));
|
||
let geom = GradientGeom { start: (0.0, 5.0).into(), end: (10.0, 5.0).into(), aspect: 1.0, focal: None };
|
||
let placed = Paint::Gradient(Box::new(GradientPaint { geom: Some(geom), ..ramp }));
|
||
let image = || {
|
||
NodeKind::Image(Box::new(ImageObject {
|
||
key: "k".into(),
|
||
width: 10,
|
||
height: 10,
|
||
xf: vectorcraft_geom::Affine::IDENTITY,
|
||
link: None,
|
||
placement: Default::default(),
|
||
pixels: None,
|
||
}))
|
||
};
|
||
let instance = || NodeKind::SymbolInstance { symbol: "s".into(), xf: vectorcraft_geom::Affine::IDENTITY };
|
||
let starts = |kind: NodeKind, params: serde_json::Value| {
|
||
let mut object = Node::new(NodeId(1), kind);
|
||
object.appearance = Appearance::basic(placed.clone(), Paint::None, 0.0).into();
|
||
let mut g = Node::group(NodeId(10), vec![Arc::new(object), sq(2, 20.0, Color::BLACK)]);
|
||
g.appearance.effects.push(Effect { id: "distort.transform".into(), params, visible: true });
|
||
let art = evaluate_container(None, &g).unwrap();
|
||
let mut out = vec![];
|
||
art.walk(&mut |c: &Node| {
|
||
if let (NodeKind::Image(_) | NodeKind::SymbolInstance { .. }, Paint::Gradient(p)) = (&c.kind, c.appearance.fill_paint()) {
|
||
out.push(p.geom.unwrap().start.x);
|
||
}
|
||
});
|
||
out
|
||
};
|
||
for kind in [image as fn() -> NodeKind, instance] {
|
||
assert_eq!(starts(kind(), serde_json::json!({"moveH": 30})), [30.0], "{:?}", kind());
|
||
assert_eq!(starts(kind(), serde_json::json!({"moveH": 30, "copies": 2})), [60.0, 30.0, 0.0], "{:?}", kind());
|
||
}
|
||
}
|
||
|
||
/// A group's Transform effect that turns its art turns the gradients fitted to its paths, to the
|
||
/// group's own fill, to a nested group's own fill and to an image's and a symbol instance's own
|
||
/// fills: each is fixed to its fit on its own object first. A move leaves the members' gradients
|
||
/// fitted.
|
||
#[test]
|
||
fn a_groups_transform_turns_the_gradients_fitted_to_its_members_and_its_own_fill() {
|
||
use vectorcraft_color::{Gradient, GradientKind, GradientPaint, GradientStop};
|
||
use vectorcraft_doc::ImageObject;
|
||
let stops = vec![GradientStop::new(0.0, Color::rgb(1.0, 0.0, 0.0)), GradientStop::new(1.0, Color::rgb(0.0, 0.0, 1.0))];
|
||
let fitted = Paint::Gradient(Box::new(GradientPaint::new(Gradient::new(GradientKind::Linear, stops))));
|
||
let square = |id: u64, x: f64| {
|
||
Arc::new(Node::path(NodeId(id), shapes::rectangle(Rect::new(x, 0.0, x + 10.0, 10.0)), Appearance::basic(fitted.clone(), Paint::None, 0.0)))
|
||
};
|
||
let mut image = Node::new(
|
||
NodeId(4),
|
||
NodeKind::Image(Box::new(ImageObject {
|
||
key: "k".into(),
|
||
width: 10,
|
||
height: 10,
|
||
xf: vectorcraft_geom::Affine::translate((30.0, 0.0)),
|
||
link: None,
|
||
placement: Default::default(),
|
||
pixels: None,
|
||
})),
|
||
);
|
||
image.appearance = Appearance::basic(fitted.clone(), Paint::None, 0.0).into();
|
||
// A symbol instance whose symbol is not in a document is measured as the 20 pt square about
|
||
// its origin: here 22.5..27.5 across and 2.5..7.5 down.
|
||
let at = vectorcraft_geom::Affine::translate((25.0, 5.0)) * vectorcraft_geom::Affine::scale(0.25);
|
||
let mut instance = Node::new(NodeId(6), NodeKind::SymbolInstance { symbol: "s".into(), xf: at });
|
||
instance.appearance = Appearance::basic(fitted.clone(), Paint::None, 0.0).into();
|
||
// Ends of each gradient by object id; none for a gradient still fitted.
|
||
let ends = |params: serde_json::Value| {
|
||
let mut inner = Node::group(NodeId(5), vec![square(2, 10.0)]);
|
||
inner.appearance.items.push(AppearanceItem::Fill(vectorcraft_doc::FillLayer::new(fitted.clone())));
|
||
let mut g = Node::group(NodeId(10), vec![square(1, 0.0), Arc::new(inner), Arc::new(image.clone()), Arc::new(instance.clone())]);
|
||
g.appearance.items.push(AppearanceItem::Fill(vectorcraft_doc::FillLayer::new(fitted.clone())));
|
||
g.appearance.effects.push(Effect { id: "distort.transform".into(), params, visible: true });
|
||
let art = evaluate_container(None, &g).unwrap();
|
||
let mut out = std::collections::BTreeMap::new();
|
||
art.walk(&mut |c: &Node| {
|
||
if let Paint::Gradient(p) = c.appearance.fill_paint() {
|
||
out.insert(c.id.0, p.geom.map(|g| (g.start, g.end)));
|
||
}
|
||
});
|
||
out
|
||
};
|
||
let near = |a: Point, b: Point| a.distance(b) < 1e-6;
|
||
// A quarter turn about the group's center (20, 5): (x, 5) goes to (20, 5 + 20 - x), so a ramp
|
||
// from (x0, 5) to (x1, 5) runs from (20, 25 - x0) up to (20, 25 - x1).
|
||
let turned = ends(serde_json::json!({"rotate": 90}));
|
||
let want = [(1, 0.0, 10.0), (2, 10.0, 20.0), (5, 10.0, 20.0), (4, 30.0, 40.0), (6, 22.5, 27.5), (10, 0.0, 40.0)];
|
||
for (id, x0, x1) in want {
|
||
let got = turned.get(&id).copied().flatten();
|
||
let ok = got.is_some_and(|(s, e)| near(s, Point::new(20.0, 25.0 - x0)) && near(e, Point::new(20.0, 25.0 - x1)));
|
||
assert!(ok, "object {id}: {got:?} in {turned:?}");
|
||
}
|
||
// The group's own fill is fixed to its fit on the art it paints, which the move took to 30..70.
|
||
let moved = ends(serde_json::json!({"moveH": 30}));
|
||
let group = moved.get(&10).copied().flatten();
|
||
assert!(group.is_some_and(|(s, e)| near(s, Point::new(30.0, 5.0)) && near(e, Point::new(70.0, 5.0))), "{moved:?}");
|
||
assert!(moved.len() == 6 && [1, 2, 4, 5, 6].iter().all(|id| moved[id].is_none()), "{moved:?}");
|
||
}
|
||
|
||
#[test]
|
||
fn a_transform_on_a_group_paints_each_copy_with_its_own_gradient() {
|
||
use vectorcraft_color::{Gradient, GradientGeom, GradientKind, GradientPaint, GradientStop};
|
||
let stops = vec![GradientStop::new(0.0, Color::rgb(1.0, 0.0, 0.0)), GradientStop::new(1.0, Color::rgb(0.0, 0.0, 1.0))];
|
||
let ramp = GradientPaint::new(Gradient::new(GradientKind::Linear, stops));
|
||
let geom = GradientGeom { start: (0.0, 5.0).into(), end: (10.0, 5.0).into(), aspect: 1.0, focal: None };
|
||
let paints = [Paint::Gradient(Box::new(ramp.clone())), Paint::Gradient(Box::new(GradientPaint { geom: Some(geom), ..ramp }))];
|
||
for (k, paint) in paints.into_iter().enumerate() {
|
||
let square = Arc::new(Node::path(NodeId(1), shapes::rectangle(Rect::new(0.0, 0.0, 10.0, 10.0)), Appearance::basic(paint, Paint::None, 0.0)));
|
||
let mut g = Node::group(NodeId(10), vec![square]);
|
||
let params = serde_json::json!({"moveH": 30, "copies": 2});
|
||
g.appearance.effects.push(Effect { id: "distort.transform".into(), params, visible: true });
|
||
let art = evaluate_container(None, &g).unwrap();
|
||
let copies = art.children().unwrap()[0].children().expect("the object and its copies are objects of their own");
|
||
assert_eq!(copies.len(), 3);
|
||
// The original is on top, after the copies below it.
|
||
for (i, c) in copies.iter().rev().enumerate() {
|
||
// Each copy's own box: an unplaced gradient fits it, a placed one moved with it.
|
||
assert!((c.geometric_bounds().unwrap().x0 - 30.0 * i as f64).abs() < 1e-6);
|
||
match c.appearance.fill_paint() {
|
||
Paint::Gradient(g) if k == 0 => assert!(g.geom.is_none(), "copy {i} fits its own box"),
|
||
Paint::Gradient(g) => assert!((g.geom.unwrap().start.x - 30.0 * i as f64).abs() < 1e-6, "copy {i}"),
|
||
p => panic!("a gradient: {p:?}"),
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// A group with `effect` holding `n` vertical then `n` horizontal bars, 2 wide on a 4-unit
|
||
/// pitch, each crossing every bar of the other direction: n² crossings.
|
||
fn crossing_bars(n: usize, effect: &str) -> Node {
|
||
let len = 4.0 * n as f64;
|
||
let paint = Appearance::basic(Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
|
||
let mut children = vec![];
|
||
for i in 0..n {
|
||
let at = 4.0 * i as f64;
|
||
children.push(Arc::new(Node::path(NodeId(100 + i as u64), shapes::rectangle(Rect::new(at, 0.0, at + 2.0, len)), paint.clone())));
|
||
}
|
||
for i in 0..n {
|
||
let at = 4.0 * i as f64;
|
||
let id = NodeId(100 + (n + i) as u64);
|
||
children.push(Arc::new(Node::path(id, shapes::rectangle(Rect::new(0.0, at, len, at + 2.0)), paint.clone())));
|
||
}
|
||
let mut g = Node::group(NodeId(10), children);
|
||
g.appearance.effects.push(Effect { id: effect.into(), params: serde_json::json!({}), visible: true });
|
||
g
|
||
}
|
||
|
||
#[test]
|
||
fn pathfinder_past_its_budget_draws_the_source_art() {
|
||
// n² crossings past the cap: no evaluation (#3591), the source art as it is.
|
||
for n in [130, 800] {
|
||
let g = crossing_bars(n, "pathfinder.divide");
|
||
let out = pathfinder_children(None, &g, None).unwrap();
|
||
assert_eq!(out.len(), 2 * n, "{n} x {n} bars");
|
||
let ids: Vec<NodeId> = out.iter().map(|c| c.id).collect();
|
||
let src: Vec<NodeId> = g.children().unwrap().iter().map(|c| c.id).collect();
|
||
assert_eq!(ids, src, "{n} x {n} bars");
|
||
assert!(out.iter().zip(g.children().unwrap()).all(|(a, b)| a.path_data() == b.path_data()));
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn pathfinder_within_its_budget_still_divides() {
|
||
// Each bar is cut into one piece per crossing plus the n pieces between and past them, so
|
||
// n x n bars divide into 3·n² pieces, and 128 x 128 bars, which overlap in exactly
|
||
// `PATHFINDER_MAX_OVERLAPS` pairs, must still divide.
|
||
for n in [8, 128] {
|
||
let out = pathfinder_children(None, &crossing_bars(n, "pathfinder.divide"), None).unwrap();
|
||
assert_eq!(out.len(), 3 * n * n, "{n} x {n} bars");
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn pathfinder_on_many_stacked_bars_still_runs() {
|
||
// 3000 bars stacked in y all overlap in x, but each overlaps only the next one in y: 2999
|
||
// overlapping pairs are within the budget, so Add still merges them into one path.
|
||
let paint = Appearance::basic(Paint::solid(Color::rgb(1.0, 0.0, 0.0)), Paint::None, 0.0);
|
||
let children = (0..3000)
|
||
.map(|i| {
|
||
let at = 1.5 * i as f64;
|
||
Arc::new(Node::path(NodeId(100 + i as u64), shapes::rectangle(Rect::new(0.0, at, 100.0, at + 2.0)), paint.clone()))
|
||
})
|
||
.collect();
|
||
let mut g = Node::group(NodeId(10), children);
|
||
g.appearance.effects.push(Effect { id: "pathfinder.add".into(), params: serde_json::json!({}), visible: true });
|
||
let out = pathfinder_children(None, &g, None).unwrap();
|
||
assert_eq!(out.len(), 1);
|
||
}
|
||
|
||
/// Two overlapping squares (0–10 and 5–15 by 10 high) as a compound shape, the front one in `mode`.
|
||
fn compound_shape(mode: ShapeMode) -> Node {
|
||
let mut front = (*sq(2, 5.0, Color::rgb(0.0, 0.0, 1.0))).clone();
|
||
front.extras_mut().shape_mode = mode;
|
||
let mut g = Node::group(NodeId(10), vec![sq(1, 0.0, Color::rgb(1.0, 0.0, 0.0)), Arc::new(front)]);
|
||
g.extras_mut().compound_shape = true;
|
||
g.appearance = Appearance::basic(Paint::solid(Color::rgb(0.0, 1.0, 0.0)), Paint::None, 0.0).into();
|
||
g
|
||
}
|
||
|
||
/// A compound shape draws the area its members' modes make, one unpainted path that its own
|
||
/// appearance paints; evaluated, it is plain art painted with that appearance.
|
||
#[test]
|
||
fn compound_shapes_draw_the_area_their_shape_modes_make() {
|
||
for (mode, want) in [(ShapeMode::Add, 150.0), (ShapeMode::Subtract, 50.0), (ShapeMode::Intersect, 50.0), (ShapeMode::Exclude, 100.0)] {
|
||
let g = compound_shape(mode);
|
||
assert!(has_pathfinder(&g), "{mode:?}");
|
||
let area_path = compound_shape_area(None, &g, None).unwrap();
|
||
assert!((po::area(&area_path, FillRule::NonZero).abs() - want).abs() < 0.01, "{mode:?}");
|
||
let out = pathfinder_children(None, &g, None).unwrap();
|
||
assert_eq!(out.len(), 1, "{mode:?}");
|
||
assert!(out[0].appearance.items.is_empty(), "the group's own appearance paints it");
|
||
let art = evaluate_container(None, &g).unwrap();
|
||
assert!(!art.is_compound_shape(), "evaluated, it is art");
|
||
let painted: Vec<&Arc<Node>> = art.children().unwrap().iter().filter(|c| !c.appearance.items.is_empty()).collect();
|
||
assert_eq!(painted.len(), 1);
|
||
assert_eq!(painted[0].appearance.fill_paint().color(), Some(Color::rgb(0.0, 1.0, 0.0)), "{mode:?}");
|
||
assert!((area(painted[0]) - want).abs() < 0.01, "{mode:?}");
|
||
}
|
||
// The backmost member's mode doesn't change the area.
|
||
let mut g = compound_shape(ShapeMode::Add);
|
||
if let Some(ch) = g.children_mut() {
|
||
let mut back = (*ch[0]).clone();
|
||
back.extras_mut().shape_mode = ShapeMode::Subtract;
|
||
ch[0] = Arc::new(back);
|
||
}
|
||
assert!((po::area(&compound_shape_area(None, &g, None).unwrap(), FillRule::NonZero).abs() - 150.0).abs() < 0.01);
|
||
// Hidden members take no part; the mark on a clip group is ignored.
|
||
let mut g = compound_shape(ShapeMode::Subtract);
|
||
if let Some(ch) = g.children_mut() {
|
||
let mut front = (*ch[1]).clone();
|
||
front.visible = false;
|
||
ch[1] = Arc::new(front);
|
||
}
|
||
assert!((po::area(&compound_shape_area(None, &g, None).unwrap(), FillRule::NonZero).abs() - 100.0).abs() < 0.01);
|
||
let mut clip = compound_shape(ShapeMode::Add);
|
||
clip.kind = NodeKind::Group { children: clip.children().unwrap().clone(), clip: true };
|
||
assert!(!clip.is_compound_shape() && compound_shape_area(None, &clip, None).is_none());
|
||
}
|
||
|
||
/// A compound shape inside another (and inside an Effect › Pathfinder group) takes part as its area,
|
||
/// styled like it.
|
||
#[test]
|
||
fn nested_compound_shapes_take_part_as_their_area() {
|
||
let inner = compound_shape(ShapeMode::Subtract);
|
||
let mut cutter = (*sq(3, 2.0, Color::BLACK)).clone();
|
||
cutter.extras_mut().shape_mode = ShapeMode::Intersect;
|
||
let mut outer = Node::group(NodeId(20), vec![Arc::new(inner.clone()), Arc::new(cutter)]);
|
||
outer.extras_mut().compound_shape = true;
|
||
// (0–5 of the inner area) ∩ (2–12) = 2–5 by 10 high.
|
||
assert!((po::area(&compound_shape_area(None, &outer, None).unwrap(), FillRule::NonZero).abs() - 30.0).abs() < 0.01);
|
||
let mut effect = Node::group(NodeId(30), vec![Arc::new(inner)]);
|
||
effect.appearance.effects.push(Effect { id: "pathfinder.add".into(), params: serde_json::json!({}), visible: true });
|
||
let out = pathfinder_children(None, &effect, None).unwrap();
|
||
assert_eq!(out.len(), 1);
|
||
assert!((area(&out[0]) - 50.0).abs() < 0.01);
|
||
assert_eq!(out[0].appearance.fill_paint().color(), Some(Color::rgb(0.0, 1.0, 0.0)), "styled like the compound shape");
|
||
}
|