mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 03:34:23 +00:00
Merge main into feat/native-save-precision
EPS saves, new on main, name `precision` in their warnings as SVG, PDF and .ai saves do.
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
//! Blend Options (defaults with nothing selected, `object.blend.info`) and the Blend tool:
|
||||
//! start points from clicked anchors, adding keys to a blend instead of nesting it (M8.18).
|
||||
//! start points from clicked anchors, adding keys to a blend instead of nesting it (M8.18), and
|
||||
//! Escape dropping the first pick.
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_doc::live::{BlendOrientation, BlendSpacing, BlendSpec};
|
||||
use vectorcraft_doc::{Node, NodeKind};
|
||||
use vectorcraft_tools::{Mods, PointerEvent, PointerKind};
|
||||
use vectorcraft_tools::{Mods, PointerEvent, PointerKind, ToolKey};
|
||||
|
||||
use crate::*;
|
||||
|
||||
@@ -140,6 +141,32 @@ fn the_blend_tool_blends_from_clicked_anchors_and_keeps_adding() {
|
||||
assert!(matches!(r.unwrap().as_slice(), [UiRequest::Dialog(k, _)] if k == "blendOptions"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn escape_drops_the_blend_tools_first_pick() {
|
||||
let mut s = session();
|
||||
let (a, b, c) = (rect(&mut s, 0.0, 0.0, 40.0), rect(&mut s, 200.0, 0.0, 40.0), rect(&mut s, 400.0, 0.0, 40.0));
|
||||
s.select_tool("blend", ViewInfo::default()).unwrap();
|
||||
let click = |s: &mut Session, x: f64, y: f64| s.pointer(&PointerEvent::new(PointerKind::Down, x, y), ViewInfo::default()).unwrap();
|
||||
let esc = |s: &mut Session| s.tool_key(ToolKey::Escape, Mods::default(), ViewInfo::default()).unwrap();
|
||||
click(&mut s, 20.0, 20.0);
|
||||
assert!(s.tool_busy());
|
||||
let (layers, depth) = (s.doc().unwrap().doc.layers.clone(), s.doc().unwrap().history.undo.len());
|
||||
assert!(esc(&mut s).is_empty());
|
||||
assert!(!s.tool_busy(), "Escape drops the pick");
|
||||
assert_eq!((&s.doc().unwrap().doc.layers, s.doc().unwrap().history.undo.len()), (&layers, depth), "and changes nothing");
|
||||
// The next two clicks blend b and c; a is not part of it.
|
||||
click(&mut s, 220.0, 20.0);
|
||||
assert_eq!(s.doc().unwrap().doc.layers, layers, "b is picked first");
|
||||
click(&mut s, 420.0, 20.0);
|
||||
let g = s.doc().unwrap().selection.objects[0];
|
||||
assert_eq!(spec(&s, g).0, vec![b, c]);
|
||||
// Escape after the blend is made: the next click starts a new blend instead of adding to it.
|
||||
esc(&mut s);
|
||||
click(&mut s, 20.0, 20.0);
|
||||
assert_eq!(s.doc().unwrap().selection.objects, vec![a]);
|
||||
assert_eq!(spec(&s, g).0, vec![b, c], "a was not added");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn start_points_round_trip_and_old_blends_load() {
|
||||
let mut s = session();
|
||||
|
||||
@@ -297,6 +297,85 @@ fn shape_builder_fill_option_and_untouched_shapes_kept() {
|
||||
assert_eq!(outline(&node(&s, sq)).0.anchor_count(), 4);
|
||||
}
|
||||
|
||||
fn parent(s: &Session, id: NodeId) -> Option<NodeId> {
|
||||
s.doc().unwrap().doc.parent_of(id)
|
||||
}
|
||||
|
||||
/// The merge replaces only the shapes it uses: a selected type object stays as it was.
|
||||
#[test]
|
||||
fn shape_builder_merge_keeps_a_selected_type_object() {
|
||||
let mut s = session();
|
||||
let (a, b) = two(&mut s);
|
||||
let t = NodeId(s.execute("text.create", &json!({"x": 300, "y": 300, "text": "Label"})).unwrap()["id"].as_u64().unwrap());
|
||||
s.execute("select.set", &json!({"ids": [a.0, b.0, t.0]})).unwrap();
|
||||
let before = node(&s, t);
|
||||
let r = s.execute("shapeBuilder.merge", &json!({"points": [[25, 50], [125, 50]]})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().doc.node(t), Some(&before));
|
||||
assert_eq!(layer_children(&s), 2, "the merged shape and the type");
|
||||
assert!(ids(&r).contains(&t), "{r}");
|
||||
}
|
||||
|
||||
/// A hidden member of a selected group stays hidden in its group, with the merged shape.
|
||||
#[test]
|
||||
fn shape_builder_merge_keeps_a_hidden_group_member() {
|
||||
let mut s = session();
|
||||
let (a, b) = two(&mut s);
|
||||
let c = rect(&mut s, 300.0, 300.0, 50.0, 50.0);
|
||||
s.execute("select.set", &json!({"ids": [a.0, b.0, c.0]})).unwrap();
|
||||
let g = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap());
|
||||
s.execute("object.hide", &json!({"ids": [c.0]})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [g.0]})).unwrap();
|
||||
let r = s.execute("shapeBuilder.merge", &json!({"points": [[25, 50], [125, 50]]})).unwrap();
|
||||
let merged = NodeId(r["merged"].as_u64().unwrap());
|
||||
assert_eq!((parent(&s, c), node(&s, c).visible, parent(&s, merged)), (Some(g), false, Some(g)));
|
||||
assert_eq!(node(&s, g).children().unwrap().len(), 2);
|
||||
}
|
||||
|
||||
/// A locked member of a selected group is neither cut nor moved, and does not cut the others.
|
||||
#[test]
|
||||
fn shape_builder_merge_leaves_a_locked_group_member() {
|
||||
let mut s = session();
|
||||
let (_, b) = two(&mut s);
|
||||
let g = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap());
|
||||
s.execute("object.lock", &json!({"ids": [b.0]})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [g.0]})).unwrap();
|
||||
let before = node(&s, b);
|
||||
let r = s.execute("shapeBuilder.merge", &json!({"points": [[25, 50], [75, 50]]})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().doc.node(b), Some(&before));
|
||||
assert_eq!(parent(&s, b), Some(g));
|
||||
let merged = NodeId(r["merged"].as_u64().unwrap());
|
||||
assert!((area(&s, merged) - 10000.0).abs() < 1.0, "A whole: {}", area(&s, merged));
|
||||
}
|
||||
|
||||
/// In a clip group, the clipping path stays and the merged shape stays inside the group.
|
||||
#[test]
|
||||
fn shape_builder_merge_keeps_a_clip_groups_clipping_path() {
|
||||
let mut s = session();
|
||||
let (a, b) = two(&mut s);
|
||||
let m = rect(&mut s, 0.0, 0.0, 120.0, 100.0);
|
||||
s.execute("select.set", &json!({"ids": [a.0, b.0, m.0]})).unwrap();
|
||||
let g = NodeId(s.execute("object.clippingMask.make", &json!({})).unwrap()["id"].as_u64().unwrap());
|
||||
let r = s.execute("shapeBuilder.merge", &json!({"ids": [g.0], "points": [[25, 50], [125, 50]]})).unwrap();
|
||||
let merged = NodeId(r["merged"].as_u64().unwrap());
|
||||
let gn = node(&s, g);
|
||||
let NodeKind::Group { children, clip: true } = &gn.kind else { panic!("a clip group: {:?}", gn.kind) };
|
||||
assert_eq!(children.first().map(|c| c.id), Some(m), "the clipping path");
|
||||
assert_eq!(parent(&s, merged), Some(g));
|
||||
}
|
||||
|
||||
/// A group goes when deleting regions leaves it empty, and stays while anything is left in it.
|
||||
#[test]
|
||||
fn shape_builder_erase_removes_only_a_group_it_empties() {
|
||||
let mut s = session();
|
||||
two(&mut s);
|
||||
let g = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap());
|
||||
let r = s.execute("shapeBuilder.merge", &json!({"points": [[25, 50]], "erase": true})).unwrap();
|
||||
assert_eq!((ids(&r), node(&s, g).children().unwrap().len()), (vec![g], 2));
|
||||
let r = s.execute("shapeBuilder.merge", &json!({"points": [[60, 50], [140, 50]], "erase": true})).unwrap();
|
||||
assert!(ids(&r).is_empty() && s.doc().unwrap().doc.node(g).is_none(), "{r}");
|
||||
assert_eq!(layer_children(&s), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shape_builder_miss_is_an_error_and_undo_restores() {
|
||||
let mut s = session();
|
||||
@@ -2230,12 +2309,45 @@ fn live_paint_bucket_tool_fills_face() {
|
||||
let gn = node(&s, g);
|
||||
let f = vectorcraft_tools::builder::face_at(&gn, vectorcraft_geom::Point::new(125.0, 50.0)).unwrap();
|
||||
assert_eq!(f.appearance.fill_paint(), s.paint.fill);
|
||||
// Live Paint Selection selects the face.
|
||||
// A Live Paint Selection click selects the face.
|
||||
s.select_tool("livePaintSelection", v).unwrap();
|
||||
s.pointer(&PointerEvent::new(PointerKind::Down, 125.0, 50.0), v).unwrap();
|
||||
s.pointer(&PointerEvent::new(PointerKind::Up, 125.0, 50.0), v).unwrap();
|
||||
assert_eq!(s.doc().unwrap().selection.objects, vec![f.id]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn live_paint_selection_marquee_selects_what_it_touches() {
|
||||
let mut s = session();
|
||||
let g = live(&mut s);
|
||||
let v = ViewInfo::default();
|
||||
s.select_tool("livePaintSelection", v).unwrap();
|
||||
let drag = |s: &mut Session, from: (f64, f64), to: (f64, f64), m: Mods, escape: bool| {
|
||||
s.pointer(&PointerEvent::new(PointerKind::Down, from.0, from.1).with_mods(m), v).unwrap();
|
||||
s.pointer(&PointerEvent::new(PointerKind::Drag, to.0, to.1).with_mods(m), v).unwrap();
|
||||
if escape {
|
||||
s.tool_key(ToolKey::Escape, Mods::default(), v).unwrap();
|
||||
}
|
||||
s.pointer(&PointerEvent::new(PointerKind::Up, to.0, to.1).with_mods(m), v).unwrap();
|
||||
s.doc().unwrap().selection.objects.clone()
|
||||
};
|
||||
let gn = node(&s, g);
|
||||
let face = |x: f64| vectorcraft_tools::builder::face_at(&gn, Point::new(x, 50.0)).unwrap().id;
|
||||
let edge = |x: f64| vectorcraft_tools::builder::edge_near(&gn, Point::new(x, 50.0), 0.5).unwrap().id;
|
||||
let shift = Mods { shift: true, ..Default::default() };
|
||||
// Across the left side: the left face and that side.
|
||||
assert_eq!(drag(&mut s, (-10.0, 40.0), (40.0, 60.0), Mods::default(), false), vec![face(25.0), edge(0.0)]);
|
||||
// Shift-drag across the right side adds the right face and that side.
|
||||
assert_eq!(drag(&mut s, (160.0, 40.0), (140.0, 60.0), shift, false), vec![face(25.0), edge(0.0), face(125.0), edge(150.0)]);
|
||||
// Shift-drag across the left side again takes those out.
|
||||
assert_eq!(drag(&mut s, (-10.0, 40.0), (40.0, 60.0), shift, false), vec![face(125.0), edge(150.0)]);
|
||||
// Escape during the drag, with or without Shift, from a press on a face or outside the group:
|
||||
// the selection stays as it was.
|
||||
for (from, m) in [((-10.0, 40.0), shift), ((25.0, 50.0), Mods::default()), ((25.0, 50.0), shift)] {
|
||||
assert_eq!(drag(&mut s, from, (40.0, 60.0), m, true), vec![face(125.0), edge(150.0)], "{from:?} {m:?}");
|
||||
}
|
||||
}
|
||||
|
||||
fn line(s: &mut Session, a: (f64, f64), b: (f64, f64)) -> NodeId {
|
||||
let r = s.execute("shape.line", &json!({"x1": a.0, "y1": a.1, "x2": b.0, "y2": b.1})).unwrap();
|
||||
NodeId(r["id"].as_u64().unwrap())
|
||||
@@ -2378,11 +2490,77 @@ fn shape_builder_keeps_the_edges_of_an_unfilled_live_paint_group() {
|
||||
let c = rect(&mut s, 160.0, 20.0, 100.0, 20.0);
|
||||
set_fill(&mut s, c, Color::rgb(0.0, 1.0, 0.0));
|
||||
s.execute("select.set", &json!({"ids": [g.0, c.0]})).unwrap();
|
||||
let before = node(&s, g);
|
||||
let r = s.execute("shapeBuilder.merge", &json!({"points": [[200, 30]]})).unwrap();
|
||||
let out = ids(&r);
|
||||
// The merge does not use the group, so it stays as it was, with every edge: the outline of
|
||||
// 0..150 × 0..100 and the edges at x = 50 and 100.
|
||||
assert_eq!(s.doc().unwrap().doc.node(g), Some(&before));
|
||||
assert!(ids(&r).contains(&g), "{r}");
|
||||
let edges: Vec<NodeId> = vectorcraft_tools::builder::edges(&before).iter().map(|e| e.id).collect();
|
||||
assert!((stroked_length(&s, &edges) - 700.0).abs() < 1.0, "{}", stroked_length(&s, &edges));
|
||||
}
|
||||
|
||||
/// A locked face of a Live Paint group the merge replaces is not a region, and a copy of it stays,
|
||||
/// still locked.
|
||||
#[test]
|
||||
fn shape_builder_merge_keeps_a_locked_live_paint_face() {
|
||||
let mut s = session();
|
||||
let g = live(&mut s);
|
||||
let locked = vectorcraft_tools::builder::face_at(&node(&s, g), vectorcraft_geom::Point::new(125.0, 50.0)).unwrap().id;
|
||||
s.execute("object.lock", &json!({"ids": [locked.0]})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [g.0]})).unwrap();
|
||||
let map = s.execute("shapeBuilder.regions", &json!({})).unwrap();
|
||||
assert!(map["regions"].as_array().unwrap().iter().all(|r| !r["sources"].as_array().unwrap().iter().any(|i| *i == locked.0)), "{map}");
|
||||
let r = s.execute("shapeBuilder.merge", &json!({"points": [[25, 50], [75, 50]]})).unwrap();
|
||||
assert_eq!(r["regions"], 2, "{r}");
|
||||
assert!(s.doc().unwrap().doc.node(g).is_none());
|
||||
// Every edge of the group: the outline of 0..150 × 0..100 and the edges at x = 50 and 100.
|
||||
assert!((stroked_length(&s, &out) - 700.0).abs() < 1.0, "{}", stroked_length(&s, &out));
|
||||
let kept: Vec<Node> = ids(&r).into_iter().map(|i| node(&s, i)).filter(|n| n.locked).collect();
|
||||
assert_eq!(kept.len(), 1, "{r}");
|
||||
assert!((area(&s, kept[0].id) - 5000.0).abs() < 1.0, "{}", area(&s, kept[0].id));
|
||||
}
|
||||
|
||||
/// A hidden face of a Live Paint group the merge replaces is not a region, and a copy of it stays,
|
||||
/// still hidden.
|
||||
#[test]
|
||||
fn shape_builder_merge_keeps_a_hidden_live_paint_face() {
|
||||
let mut s = session();
|
||||
let g = live(&mut s);
|
||||
let hidden = vectorcraft_tools::builder::face_at(&node(&s, g), vectorcraft_geom::Point::new(125.0, 50.0)).unwrap().id;
|
||||
s.execute("object.hide", &json!({"ids": [hidden.0]})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [g.0]})).unwrap();
|
||||
let r = s.execute("shapeBuilder.merge", &json!({"points": [[25, 50], [75, 50]]})).unwrap();
|
||||
assert_eq!(r["regions"], 2, "{r}");
|
||||
assert!(s.doc().unwrap().doc.node(g).is_none());
|
||||
let kept: Vec<Node> = ids(&r).into_iter().map(|i| node(&s, i)).filter(|n| !n.visible).collect();
|
||||
assert_eq!(kept.len(), 1, "{r}");
|
||||
assert!((area(&s, kept[0].id) - 5000.0).abs() < 1.0, "{}", area(&s, kept[0].id));
|
||||
}
|
||||
|
||||
/// A locked face without a fill and a locked edge without a stroke, in a Live Paint group the
|
||||
/// merge replaces, each keep a copy, still locked.
|
||||
#[test]
|
||||
fn shape_builder_merge_keeps_unpainted_locked_live_paint_members() {
|
||||
let mut s = session();
|
||||
let g = live(&mut s);
|
||||
s.execute("livePaint.fill", &json!({"group": g.0, "point": [125, 50], "none": true})).unwrap();
|
||||
let n = node(&s, g);
|
||||
let face = vectorcraft_tools::builder::face_at(&n, Point::new(125.0, 50.0)).unwrap().id;
|
||||
let edge = vectorcraft_tools::builder::visible_edge_near(&n, Point::new(150.0, 50.0), 4.0).unwrap().id;
|
||||
let names = (node(&s, face).name, node(&s, edge).name);
|
||||
assert_ne!(names.0, names.1);
|
||||
s.edit("t", |d, _| {
|
||||
d.node_mut(edge).unwrap().appearance.set_stroke(Paint::None);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
s.execute("object.lock", &json!({"ids": [face.0, edge.0]})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [g.0]})).unwrap();
|
||||
let r = s.execute("shapeBuilder.merge", &json!({"points": [[25, 50], [75, 50]]})).unwrap();
|
||||
assert!(s.doc().unwrap().doc.node(g).is_none());
|
||||
let kept: Vec<Node> = ids(&r).into_iter().map(|i| node(&s, i)).filter(|n| n.locked).collect();
|
||||
let faces = kept.iter().filter(|n| n.name == names.0 && n.appearance.fill_paint().is_none() && (area(&s, n.id) - 5000.0).abs() < 1.0).count();
|
||||
let edges = kept.iter().filter(|n| n.name == names.1 && n.appearance.stroke_paint().is_none()).count();
|
||||
assert_eq!((kept.len(), faces, edges), (2, 1, 1), "{r}");
|
||||
}
|
||||
|
||||
/// A plain group whose name starts with "Live Paint" is not a Live Paint group: the Shape Builder
|
||||
@@ -2406,7 +2584,9 @@ fn shape_builder_takes_the_paths_of_a_plain_group_named_live_paint() {
|
||||
assert_eq!(map["regions"].as_array().unwrap().len(), 2, "{map}");
|
||||
let r = s.execute("shapeBuilder.merge", &json!({"points": [[200, 30]]})).unwrap();
|
||||
let out = ids(&r);
|
||||
assert!(out.iter().any(|i| (area(&s, *i) - 10000.0).abs() < 1.0), "the group's rectangle is kept: {r}");
|
||||
// The merge keeps the unused group whole, so its rectangle is inside an id of the reply.
|
||||
let kept_area = |n: &Node| n.children().map_or_else(|| area(&s, n.id), |c| c.iter().map(|c| area(&s, c.id)).sum());
|
||||
assert!(out.iter().any(|i| (kept_area(&node(&s, *i)) - 10000.0).abs() < 1.0), "the group's rectangle is kept: {r}");
|
||||
// A stroke of the group's rectangle splits as a plain path's does.
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [g.0]})).unwrap();
|
||||
|
||||
@@ -47,6 +47,19 @@ fn harmony_query_returns_the_group_and_its_grid() {
|
||||
assert!(s.execute("color.harmony", &json!({"color": "#ff0000", "rule": "triad", "variation": "loud"})).is_err());
|
||||
}
|
||||
|
||||
/// The Color Guide refuses `steps` or `amount` that is not a number, which it would otherwise
|
||||
/// replace with its default.
|
||||
#[test]
|
||||
fn harmony_refuses_steps_or_amount_that_is_not_a_number() {
|
||||
let mut s = Session::new();
|
||||
for p in [json!({"color": "#ff0000", "rule": "triad", "steps": "2"}), json!({"color": "#ff0000", "rule": "triad", "amount": "100"})] {
|
||||
let e = s.execute("color.harmony", &p).unwrap_err().to_string();
|
||||
assert!(e.contains("must be a number"), "{p}: {e}");
|
||||
}
|
||||
let r = s.execute("color.harmony", &json!({"color": "#ff0000", "rule": "triad", "steps": null, "amount": null})).unwrap();
|
||||
assert_eq!(r["grid"][0].as_array().unwrap().len(), 9, "a null is read as a key left out");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_swatch_saves_several_colours_in_one_step() {
|
||||
let mut s = session();
|
||||
|
||||
@@ -130,6 +130,28 @@ fn set_units_is_an_alias() {
|
||||
assert!(s.execute("document.setUnits", &json!({"units": "parsecs"})).is_err());
|
||||
}
|
||||
|
||||
/// Switch Units (Cmd+Alt+Shift+U) steps the document's units through every unit in the order a
|
||||
/// right-click on a ruler lists them, Feet going back to Points, one undo step each.
|
||||
#[test]
|
||||
fn switch_units_steps_through_the_units_and_wraps() {
|
||||
let spec = command_specs().iter().find(|c| c.id == "document.switchUnits").expect("registered");
|
||||
assert_eq!((spec.label, spec.shortcut), ("Switch Units", Some("Cmd+Alt+Shift+U")));
|
||||
assert!(Session::new().execute("document.switchUnits", &json!({})).is_err(), "no document");
|
||||
let mut s = session();
|
||||
assert_eq!(s.doc().unwrap().doc.units, Unit::Points);
|
||||
let order = ["Picas", "Inches", "Millimeters", "Centimeters", "Pixels", "Feet & Inches", "Meters", "Yards", "Feet", "Points"];
|
||||
for (i, label) in order.iter().enumerate() {
|
||||
assert_eq!(s.execute("document.switchUnits", &json!({})).unwrap(), json!({"units": label}));
|
||||
assert_eq!(s.doc().unwrap().doc.units.label(), *label);
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), i + 1, "one undo step each");
|
||||
}
|
||||
s.execute("document.setUnits", &json!({"units": "Feet"})).unwrap();
|
||||
s.execute("document.switchUnits", &json!({})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().doc.units, Unit::Points, "after Feet comes Points");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().doc.units, Unit::Feet);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn language_picks_its_quotes_unless_quotes_are_given() {
|
||||
let mut s = session();
|
||||
|
||||
@@ -465,6 +465,132 @@ fn blob_brush_merges_only_with_the_selection_and_keep_selected_off_deselects() {
|
||||
assert!(s.doc().unwrap().doc.node(all).is_some() && s.doc().unwrap().doc.node(other).is_some());
|
||||
}
|
||||
|
||||
/// A Blob Brush stroke merges with the topmost matching path it touches and with the touched
|
||||
/// matching paths next to it in stacking order in the same group or layer; matching paths in other
|
||||
/// layers or groups, or below an object of another kind, are left alone.
|
||||
#[test]
|
||||
fn blob_brush_merges_with_the_topmost_match_and_its_neighbors() {
|
||||
let blob = |s: &mut Session, x: f64| {
|
||||
NodeId(s.execute("path.blob", &json!({"points": [[x, 60], [x, 140]], "size": 20})).unwrap()["id"].as_u64().unwrap())
|
||||
};
|
||||
let across = json!({"points": [[50, 100], [450, 100]], "size": 10});
|
||||
let parent = |s: &Session, id: NodeId| s.doc().unwrap().doc.parent_of(id);
|
||||
|
||||
// Two layers: only the path on the upper layer merges.
|
||||
let mut s = session();
|
||||
let low = blob(&mut s, 100.0);
|
||||
let upper = NodeId(s.execute("layer.new", &json!({})).unwrap()["id"].as_u64().unwrap());
|
||||
let high = blob(&mut s, 200.0);
|
||||
let area_low = area(&path(&s, low));
|
||||
let id = NodeId(s.execute("path.blob", &across).unwrap()["id"].as_u64().unwrap());
|
||||
assert_eq!(id, high);
|
||||
assert_eq!(parent(&s, high), Some(upper));
|
||||
assert_eq!(paths(&s).len(), 2);
|
||||
assert_eq!(area(&path(&s, low)), area_low, "the path on the lower layer is left as it was");
|
||||
|
||||
// A path of another color between two matches: only the upper one merges.
|
||||
let mut s = session();
|
||||
let below = blob(&mut s, 100.0);
|
||||
s.execute("paint.setStroke", &json!({"color": "#ff0000"})).unwrap();
|
||||
let red = NodeId(s.execute("path.blob", &json!({"points": [[100, 400], [200, 400]], "size": 10})).unwrap()["id"].as_u64().unwrap());
|
||||
s.execute("paint.setStroke", &json!({"color": "#000000"})).unwrap();
|
||||
let above = blob(&mut s, 200.0);
|
||||
let (area_below, area_red) = (area(&path(&s, below)), area(&path(&s, red)));
|
||||
let id = NodeId(s.execute("path.blob", &across).unwrap()["id"].as_u64().unwrap());
|
||||
assert_eq!(id, above);
|
||||
assert_eq!(paths(&s).len(), 3);
|
||||
assert_eq!(area(&path(&s, below)), area_below, "the path below the red one is left as it was");
|
||||
assert_eq!(area(&path(&s, red)), area_red);
|
||||
|
||||
// A matching path the stroke does not touch, between two it touches: both touched ones merge,
|
||||
// and the untouched one is left as it was.
|
||||
let mut s = session();
|
||||
let lower = blob(&mut s, 100.0);
|
||||
let untouched = NodeId(s.execute("path.blob", &json!({"points": [[200, 300], [200, 380]], "size": 20})).unwrap()["id"].as_u64().unwrap());
|
||||
let upper = blob(&mut s, 300.0);
|
||||
let area_untouched = area(&path(&s, untouched));
|
||||
let id = NodeId(s.execute("path.blob", &across).unwrap()["id"].as_u64().unwrap());
|
||||
assert_eq!(id, upper);
|
||||
assert!(s.doc().unwrap().doc.node(lower).is_none(), "the lower path joined the upper one");
|
||||
assert_eq!(area(&path(&s, untouched)), area_untouched);
|
||||
assert_eq!(paths(&s).len(), 2);
|
||||
|
||||
// Two adjacent matches in a group both merge; a match outside the group is left alone.
|
||||
let mut s = session();
|
||||
let outside = blob(&mut s, 100.0);
|
||||
let a = blob(&mut s, 200.0);
|
||||
let b = blob(&mut s, 300.0);
|
||||
s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap();
|
||||
let g = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap());
|
||||
s.execute("select.none", &json!({})).unwrap();
|
||||
let area_outside = area(&path(&s, outside));
|
||||
let id = NodeId(s.execute("path.blob", &across).unwrap()["id"].as_u64().unwrap());
|
||||
assert_eq!(id, b, "the topmost match keeps its id");
|
||||
assert!(s.doc().unwrap().doc.node(a).is_none(), "a joined b");
|
||||
assert_eq!(parent(&s, b), Some(g));
|
||||
assert_eq!(paths(&s).len(), 2);
|
||||
assert_eq!(area(&path(&s, outside)), area_outside, "the path outside the group is left as it was");
|
||||
}
|
||||
|
||||
/// Blob Brush Fidelity: the brush of `path.blob` follows the stroke fitted at `fidelity` points, as
|
||||
/// `path.freehand` fits a Pencil stroke; without `fidelity` it follows every point. On a jittery
|
||||
/// stroke, 0.01 pt keeps the jitter and 1.5 pt and 10 pt smooth it away, with fewer anchors and a
|
||||
/// shape of about the same area. The tool's Fidelity is 1.5 pt by default.
|
||||
#[test]
|
||||
fn blob_brush_fidelity_fits_the_stroke_the_brush_follows() {
|
||||
let mut s = session();
|
||||
// A wave drawn with a hand's jitter: every other point 1 pt off.
|
||||
let wave: Vec<[f64; 2]> =
|
||||
(0..=80).map(|i| [20.0 + f64::from(i) * 4.0, 200.0 + 30.0 * (f64::from(i) * 0.125).sin() + if i % 2 == 0 { 1.0 } else { -1.0 }]).collect();
|
||||
// The anchors and area of the blob the wave paints at height `dy` with `fidelity` (None: left out).
|
||||
let mut blob = |dy: f64, fidelity: Option<f64>| {
|
||||
let pts: Vec<[f64; 2]> = wave.iter().map(|q| [q[0], q[1] + dy]).collect();
|
||||
let mut p = json!({"points": pts, "size": 20, "merge": false});
|
||||
if let Some(f) = fidelity {
|
||||
p["fidelity"] = json!(f);
|
||||
}
|
||||
let id = NodeId(s.execute("path.blob", &p).unwrap()["id"].as_u64().unwrap());
|
||||
let pd = path(&s, id);
|
||||
(pd.subpaths.iter().map(|sp| sp.anchors.len()).sum::<usize>(), area(&pd))
|
||||
};
|
||||
let (unfitted, unfitted_area) = blob(0.0, None);
|
||||
let (default, default_area) = blob(100.0, Some(1.5));
|
||||
let (smooth, smooth_area) = blob(200.0, Some(10.0));
|
||||
let (accurate, _) = blob(300.0, Some(0.01));
|
||||
assert!(default < unfitted, "{default} anchors at 1.5 pt, {unfitted} unfitted");
|
||||
assert!(smooth < default, "{smooth} anchors at 10 pt, {default} at 1.5 pt, {unfitted} unfitted");
|
||||
assert!(default < accurate, "{default} anchors at 1.5 pt, {accurate} at 0.01 pt");
|
||||
for (a, f) in [(default_area, 1.5), (smooth_area, 10.0)] {
|
||||
assert!((a - unfitted_area).abs() < unfitted_area * 0.05, "{a} pt² at {f} pt, {unfitted_area} pt² unfitted");
|
||||
}
|
||||
// A short stroke back to its start that the fit leaves with no length still paints.
|
||||
let back = json!({"points": [[100, 600], [110, 600], [100, 600]], "size": 10, "fidelity": 100, "merge": false});
|
||||
let dab = NodeId(s.execute("path.blob", &back).unwrap()["id"].as_u64().unwrap());
|
||||
assert!(area(&path(&s, dab)) > 70.0, "{}", area(&path(&s, dab)));
|
||||
s.select_tool("blobBrush", view()).unwrap();
|
||||
assert_eq!(s.tool_options()["fidelity"], json!(1.5));
|
||||
}
|
||||
|
||||
/// A stroke fitted at its Fidelity can curve outside its points, and it still merges with a blob
|
||||
/// of its color that the curve covers there.
|
||||
#[test]
|
||||
fn blob_brush_merges_with_a_blob_its_fitted_stroke_curves_over() {
|
||||
let stroke = json!({"points": [[100, 100], [600, 100], [600, 600]], "size": 10, "fidelity": 1.5});
|
||||
// Where the fitted shape reaches farthest outside the points' bounds padded by the size.
|
||||
let mut first = session();
|
||||
let id = NodeId(first.execute("path.blob", &with(&stroke, json!({"merge": false}))).unwrap()["id"].as_u64().unwrap());
|
||||
let outside = |p: Point| (90.0 - p.x).max(p.x - 610.0).max(90.0 - p.y).max(p.y - 610.0);
|
||||
let far =
|
||||
path(&first, id).subpaths.iter().flat_map(|sp| sp.anchors.iter().map(|a| a.p)).max_by(|a, b| outside(*a).total_cmp(&outside(*b))).unwrap();
|
||||
assert!(outside(far) > 5.0, "the fitted stroke curves {} pt past the padded bounds", outside(far));
|
||||
// A small blob there merges with the stroke.
|
||||
let mut s = session();
|
||||
let small = NodeId(s.execute("path.blob", &json!({"points": [[far.x, far.y]], "size": 6, "merge": false})).unwrap()["id"].as_u64().unwrap());
|
||||
let merged = NodeId(s.execute("path.blob", &stroke).unwrap()["id"].as_u64().unwrap());
|
||||
assert_eq!(merged, small);
|
||||
assert_eq!(paths(&s).len(), 1);
|
||||
}
|
||||
|
||||
/// Three points already curve in the saved path: both ends carry a handle into the curve.
|
||||
#[test]
|
||||
fn path_curvature_with_three_points_saves_end_handles() {
|
||||
@@ -597,6 +723,40 @@ fn curvature_click_first_point_closes() {
|
||||
assert_eq!(p.anchor_count(), 3);
|
||||
}
|
||||
|
||||
/// Delete on a point of a finished Curvature path re-curves the rest through the remaining points,
|
||||
/// as if they had been drawn without it, in one undo step.
|
||||
#[test]
|
||||
fn curvature_delete_curves_through_the_remaining_points() {
|
||||
let mut s = session();
|
||||
let v = view();
|
||||
s.select_tool("curvature", v).unwrap();
|
||||
let pts = [(100.0, 300.0), (200.0, 200.0), (300.0, 320.0), (400.0, 220.0), (500.0, 300.0)];
|
||||
for (x, y) in pts {
|
||||
gesture(&mut s, &[(x, y)], Mods::default());
|
||||
}
|
||||
s.tool_key(ToolKey::Escape, Mods::default(), v).unwrap();
|
||||
let id = paths(&s)[0].0;
|
||||
let before = path(&s, id).subpaths[0].clone();
|
||||
let remaining = |gone: usize| -> Vec<(Point, bool)> {
|
||||
pts.iter().enumerate().filter(|&(i, _)| i != gone).map(|(_, &(x, y))| (Point::new(x, y), false)).collect()
|
||||
};
|
||||
for (gone, &(x, y)) in pts.iter().enumerate() {
|
||||
s.execute("select.set", &json!({"ids": [id.0]})).unwrap();
|
||||
let steps = undo_steps(&s);
|
||||
gesture(&mut s, &[(x, y)], Mods::default());
|
||||
s.tool_key(ToolKey::Delete, Mods::default(), v).unwrap();
|
||||
assert_eq!(undo_steps(&s), steps + 1, "one step");
|
||||
let want = vectorcraft_tools::draw2::curvature_spline(&remaining(gone), false);
|
||||
let got = path(&s, id).subpaths[0].clone();
|
||||
assert_eq!(got.anchors.len(), want.anchors.len());
|
||||
for (a, b) in got.anchors.iter().zip(&want.anchors) {
|
||||
assert!(a.p.distance(b.p) < 1e-9 && a.h_in.distance(b.h_in) < 1e-6 && a.h_out.distance(b.h_out) < 1e-6, "point {gone}: {a:?} != {b:?}");
|
||||
}
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(path(&s, id).subpaths[0], before);
|
||||
}
|
||||
}
|
||||
|
||||
/// The Curvature tool edits a path another tool drew (#798), one undo step per edit, keeping its
|
||||
/// shape except where edited.
|
||||
#[test]
|
||||
@@ -740,6 +900,26 @@ fn scissors_opens_closed_path() {
|
||||
assert!(near(p.subpaths[0].anchors[5].p, Point::new(200.0, 150.0)));
|
||||
}
|
||||
|
||||
/// Alt held with the Scissors tool switches it to the Add Anchor Point tool: a click adds an anchor
|
||||
/// in one undo step and the path stays closed. Once Alt is let go, a click cuts again.
|
||||
#[test]
|
||||
fn scissors_with_alt_adds_an_anchor_point() {
|
||||
let mut s = session();
|
||||
let id = rect(&mut s, 100.0, 100.0, 100.0, 100.0);
|
||||
s.select_tool("scissors", view()).unwrap();
|
||||
assert_eq!(gesture(&mut s, &[(200.0, 150.0)], Mods { alt: true, ..Mods::default() }), 1);
|
||||
let p = path(&s, id);
|
||||
assert_eq!(paths(&s).len(), 1);
|
||||
assert!(p.is_closed());
|
||||
assert_eq!(p.anchor_count(), 5);
|
||||
assert!(near(p.subpaths[0].anchors[2].p, Point::new(200.0, 150.0)));
|
||||
assert_eq!(s.tool_id(), "scissors");
|
||||
gesture(&mut s, &[(150.0, 200.0)], Mods::default());
|
||||
let p = path(&s, id);
|
||||
assert!(!p.is_closed());
|
||||
assert_eq!(p.anchor_count(), 7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scissors_splits_open_path_in_two() {
|
||||
let mut s = session();
|
||||
@@ -809,6 +989,52 @@ fn eraser_removes_small_shape_entirely() {
|
||||
assert!(paths(&s).is_empty());
|
||||
}
|
||||
|
||||
/// #4495: the Eraser cuts a compound path as one shape: its hole stays empty, the erased area is
|
||||
/// empty, and the rest stays filled, with either fill rule. A notch inside the ring keeps one
|
||||
/// compound path; a drag through the whole ring leaves compound paths whose holes stay holes.
|
||||
#[test]
|
||||
fn eraser_keeps_the_holes_of_a_compound_path() {
|
||||
for rule in ["nonZero", "evenOdd"] {
|
||||
for (drag, erased) in [([[200, 110], [200, 120]], (200.0, 115.0)), ([[200, 80], [200, 320]], (200.0, 120.0))] {
|
||||
let mut s = session();
|
||||
let outer = rect(&mut s, 100.0, 100.0, 200.0, 200.0);
|
||||
let inner = rect(&mut s, 150.0, 150.0, 100.0, 100.0);
|
||||
s.execute("select.set", &json!({"ids": [outer.0, inner.0]})).unwrap();
|
||||
s.execute("object.compoundPath.make", &json!({})).unwrap();
|
||||
s.execute("path.setFillRule", &json!({"rule": rule})).unwrap();
|
||||
s.execute("select.none", &json!({})).unwrap();
|
||||
s.execute("path.eraseRegion", &json!({"points": drag, "size": 10})).unwrap();
|
||||
// Whether any top-level shape fills `p`, a compound path with all its members at once.
|
||||
let filled = |x: f64, y: f64| {
|
||||
let doc = &s.doc().unwrap().doc;
|
||||
let mut hit = false;
|
||||
for layer in &doc.layers {
|
||||
for n in layer.children().unwrap() {
|
||||
let (subs, rule) = match &n.kind {
|
||||
NodeKind::Path { path, rule, .. } => (path.subpaths.clone(), *rule),
|
||||
NodeKind::Compound { children, rule } => {
|
||||
(children.iter().flat_map(|c| c.path_data().unwrap().subpaths.clone()).collect(), *rule)
|
||||
}
|
||||
_ => continue,
|
||||
};
|
||||
hit |= vectorcraft_geom::hit::fill_contains(&PathData::new(subs).to_bezpath(), rule, Point::new(x, y));
|
||||
}
|
||||
}
|
||||
hit
|
||||
};
|
||||
assert!(!filled(170.0, 200.0), "{rule} {drag:?}: the hole stays empty");
|
||||
assert!(!filled(erased.0, erased.1), "{rule} {drag:?}: the erased area is empty");
|
||||
assert!(filled(120.0, 200.0) && filled(280.0, 200.0), "{rule} {drag:?}: the ring stays filled");
|
||||
if drag[1][1] == 120 {
|
||||
let doc = &s.doc().unwrap().doc;
|
||||
let tops: Vec<_> = doc.layers.iter().flat_map(|l| l.children().unwrap().iter().cloned()).collect();
|
||||
assert_eq!(tops.len(), 1, "{rule}: one object");
|
||||
assert!(matches!(tops[0].kind, NodeKind::Compound { .. }), "{rule}: still a compound path");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The Eraser's tip is an ellipse `size` across its long axis, at `angle` counter-clockwise from
|
||||
/// horizontal, with a short axis `roundness` percent as long: a vertical drag across a square
|
||||
/// removes a band as wide as the tip is across, and a press on a horizontal line removes the
|
||||
@@ -974,6 +1200,22 @@ fn line_family_tools_draw_and_click_asks_dialog() {
|
||||
assert_eq!(ui, vec![UiRequest::Dialog("polarGrid".into(), json!({"x": 10.0, "y": 10.0}))]);
|
||||
}
|
||||
|
||||
/// A closed `shape.arc` with Base Along Y runs from the box's bottom-left corner to its top-right
|
||||
/// one and closes through the top-left corner, the X-base arc's sector mirrored top to bottom.
|
||||
#[test]
|
||||
fn shape_arc_closed_along_y_closes_through_the_top_left_corner() {
|
||||
let mut s = session();
|
||||
let p = json!({"x": 100, "y": 50, "lengthX": 80, "lengthY": 40, "baseAlong": "y", "slope": 100, "closed": true});
|
||||
let id = NodeId(s.execute("shape.arc", &p).unwrap()["id"].as_u64().unwrap());
|
||||
let path = s.doc().unwrap().doc.node(id).unwrap().path_data().unwrap().clone();
|
||||
let sp = &path.subpaths[0];
|
||||
assert!(sp.closed);
|
||||
let ends: Vec<Point> = sp.anchors.iter().map(|a| a.p).collect();
|
||||
assert_eq!(ends, [Point::new(100.0, 90.0), Point::new(180.0, 50.0), Point::new(100.0, 50.0)]);
|
||||
let area = sp.signed_area().abs() / (80.0 * 40.0);
|
||||
assert!((area - std::f64::consts::FRAC_PI_4).abs() < 0.01, "{area}");
|
||||
}
|
||||
|
||||
/// The children of the grid a `shape.rectangularGrid` / `shape.polarGrid` call made, bottom first.
|
||||
fn grid(s: &mut Session, cmd: &str, p: serde_json::Value) -> Vec<std::sync::Arc<vectorcraft_doc::Node>> {
|
||||
let id = NodeId(s.execute(cmd, &p).unwrap()["id"].as_u64().unwrap());
|
||||
@@ -1076,6 +1318,22 @@ fn polar_grid_options_compound_fill_and_skew() {
|
||||
assert!(tips(&inward)[1..].iter().all(|t| t.x < 50.0), "counterclockwise of the top: {:?}", tips(&inward));
|
||||
}
|
||||
|
||||
/// A divider count written as a whole float (`3.0`) is the same as `3`; a fraction gives the default 5.
|
||||
#[test]
|
||||
fn grid_divider_counts_accept_whole_floats() {
|
||||
let mut s = session();
|
||||
let size = json!({"x": 0, "y": 0, "width": 100, "height": 100});
|
||||
let float = grid(&mut s, "shape.rectangularGrid", with(&size, json!({"rows": 3.0, "columns": 3.0})));
|
||||
let int = grid(&mut s, "shape.rectangularGrid", with(&size, json!({"rows": 3, "columns": 3})));
|
||||
assert_eq!(float.len(), int.len());
|
||||
assert_eq!(float.len(), 7);
|
||||
let float = grid(&mut s, "shape.polarGrid", with(&size, json!({"concentric": 2.0, "radial": 3.0})));
|
||||
let int = grid(&mut s, "shape.polarGrid", with(&size, json!({"concentric": 2, "radial": 3})));
|
||||
assert_eq!(float.len(), int.len());
|
||||
let frac = grid(&mut s, "shape.rectangularGrid", with(&size, json!({"rows": 2.5, "columns": 2.5})));
|
||||
assert_eq!(frac.len(), 11);
|
||||
}
|
||||
|
||||
/// The grid options are checked: a divider count or a skew is a number and a switch true or false,
|
||||
/// or the command fails without an undo step.
|
||||
#[test]
|
||||
@@ -1190,6 +1448,7 @@ fn drawing_commands_refuse_a_size_given_as_text() {
|
||||
("path.smoothRegion", "fidelity"),
|
||||
("path.joinScrub", "tolerance"),
|
||||
("path.blob", "size"),
|
||||
("path.blob", "fidelity"),
|
||||
] {
|
||||
refuses_a_number_given_as_text(&mut s, cmd, &stroke, key);
|
||||
}
|
||||
@@ -1737,6 +1996,30 @@ fn remove_anchor_points_keeps_the_paths_closed() {
|
||||
assert_eq!(spec.menu, &["Object", "Path"][..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_anchor_point_selects_the_path_it_was_added_to() {
|
||||
// #4237: an anchor added to a path that wasn't selected selects that path, the new anchor
|
||||
// direct-selected, so it shows at once; undo puts the old selection back. A path already
|
||||
// selected (the Pen's Auto Add) keeps its selection.
|
||||
let mut s = session();
|
||||
let other = rect(&mut s, 300.0, 0.0, 50.0, 50.0);
|
||||
let line = |s: &mut Session| {
|
||||
NodeId(s.execute("path.create", &json!({"anchors": [{"x": 0, "y": 0}, {"x": 100, "y": 0}]})).unwrap()["id"].as_u64().unwrap())
|
||||
};
|
||||
let id = line(&mut s);
|
||||
s.execute("select.set", &json!({"ids": [other.0]})).unwrap();
|
||||
s.execute("path.insertAnchor", &json!({"id": id.0, "segment": 0, "t": 0.5})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().selection.objects, [id]);
|
||||
assert_eq!(selected_anchors(&s, id), [(0, 1)]);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().selection.objects, [other]);
|
||||
let kept = line(&mut s);
|
||||
s.execute("select.set", &json!({"ids": [kept.0]})).unwrap();
|
||||
s.execute("path.insertAnchor", &json!({"id": kept.0, "segment": 0, "t": 0.5})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().selection.objects, [kept]);
|
||||
assert!(selected_anchors(&s, kept).is_empty(), "a path selected as a whole stays so");
|
||||
}
|
||||
|
||||
fn selected_anchors(s: &Session, id: NodeId) -> Vec<(usize, usize)> {
|
||||
s.doc().unwrap().selection.anchors.get(&id).map(|set| set.iter().copied().collect()).unwrap_or_default()
|
||||
}
|
||||
|
||||
@@ -70,6 +70,60 @@ fn balance_previews_and_commits_one_step() {
|
||||
assert_eq!(fill_of(&s, a).to_hex(), "#808080");
|
||||
}
|
||||
|
||||
/// Saturate needs its intensity: a call without one, or with one that is not a number, is an error
|
||||
/// that changes nothing and records no undo step. A number outside −100..100 counts as its nearest end.
|
||||
#[test]
|
||||
fn saturate_needs_a_number_intensity() {
|
||||
let mut s = session();
|
||||
let a = rect(&mut s, json!("#cc6666"));
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
for p in [json!({}), json!({"intensity": "40"}), json!({"intensity": null})] {
|
||||
let e = s.execute("edit.colors.saturate", &p).unwrap_err().to_string();
|
||||
assert!(e.contains("missing number `intensity`"), "{p}: {e}");
|
||||
}
|
||||
assert_eq!(fill_of(&s, a).to_hex(), "#cc6666");
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo, "no undo step");
|
||||
s.execute("edit.colors.saturate", &json!({"intensity": 100})).unwrap();
|
||||
let full = fill_of(&s, a).to_hex();
|
||||
assert_ne!(full, "#cc6666");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
s.execute("edit.colors.saturate", &json!({"intensity": 250})).unwrap();
|
||||
assert_eq!(fill_of(&s, a).to_hex(), full, "250 counts as 100");
|
||||
}
|
||||
|
||||
/// Saturate at 0 leaves every color as it was: a swatch-linked fill keeps its exact color and its link.
|
||||
#[test]
|
||||
fn saturate_keeps_a_color_it_does_not_change() {
|
||||
let mut s = session();
|
||||
s.execute("swatch.new", &json!({"name": "Sky", "color": "#3a7bd5", "global": true})).unwrap();
|
||||
let a = rect(&mut s, json!("#3a7bd5"));
|
||||
s.execute("paint.setFill", &json!({"ids": [a.0], "swatch": "Sky"})).unwrap();
|
||||
let b = rect(&mut s, json!({"c": 20, "m": 40, "y": 0, "k": 10}));
|
||||
let paint = |s: &Session, id: NodeId| s.doc().unwrap().doc.node(id).unwrap().appearance.fill_paint().clone();
|
||||
let (sky, cmyk) = (paint(&s, a), paint(&s, b));
|
||||
let v = s.execute("edit.colors.saturate", &json!({"ids": [a.0, b.0], "intensity": 0})).unwrap();
|
||||
assert_eq!(v["changed"], 0);
|
||||
assert_eq!(paint(&s, a), sky);
|
||||
assert_eq!(paint(&s, b), cmyk);
|
||||
}
|
||||
|
||||
/// Adjust Color Balance refuses a channel that is not a number, which it would otherwise read as 0.
|
||||
#[test]
|
||||
fn adjust_balance_refuses_a_channel_that_is_not_a_number() {
|
||||
let mut s = session();
|
||||
let a = rect(&mut s, json!("#808080"));
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
for p in [json!({"mode": "rgb", "r": "40"}), json!({"c": "10"}), json!({"gray": true}), json!({"tint": "5"})] {
|
||||
let e = s.execute("edit.colors.adjustBalance", &p).unwrap_err().to_string();
|
||||
assert!(e.contains("must be a number"), "{p}: {e}");
|
||||
}
|
||||
assert_eq!(fill_of(&s, a).to_hex(), "#808080");
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo, "no undo step");
|
||||
// A null is read as a channel left out.
|
||||
s.execute("edit.colors.adjustBalance", &json!({"mode": "rgb", "r": 20, "g": null})).unwrap();
|
||||
assert_eq!(fill_of(&s, a).to_hex(), "#b38080");
|
||||
}
|
||||
|
||||
// ---------- meshes, images and patterns ----------
|
||||
|
||||
fn mesh_colors(s: &Session, id: NodeId) -> Vec<Color> {
|
||||
@@ -133,6 +187,52 @@ fn a_clicked_mesh_point_takes_the_fill_colour() {
|
||||
assert_eq!((after[0], after[3]), (before[0], before[3]));
|
||||
}
|
||||
|
||||
fn mesh_opacities(s: &Session, id: NodeId) -> Vec<f32> {
|
||||
match &s.doc().unwrap().doc.node(id).unwrap().kind {
|
||||
NodeKind::Mesh(m) => m.points.iter().map(|p| p.opacity).collect(),
|
||||
_ => panic!("not a mesh"),
|
||||
}
|
||||
}
|
||||
|
||||
/// The Transparency panel's opacity on a clicked mesh point fades that point alone, not the whole
|
||||
/// object (#4206).
|
||||
#[test]
|
||||
fn a_clicked_mesh_point_takes_the_opacity() {
|
||||
let mut s = session();
|
||||
let m = rect(&mut s, json!("#ff0000"));
|
||||
s.execute("object.mesh.create", &json!({"rows": 2, "cols": 2})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [m.0]})).unwrap();
|
||||
let v = ViewInfo::default();
|
||||
let click = |s: &mut Session, i: usize| {
|
||||
let NodeKind::Mesh(g) = &s.doc().unwrap().doc.node(m).unwrap().kind else { panic!("not a mesh") };
|
||||
let q = g.points[i].p;
|
||||
for kind in [PointerKind::Down, PointerKind::Up] {
|
||||
s.pointer(&PointerEvent::new(kind, q.x, q.y), v).unwrap();
|
||||
}
|
||||
};
|
||||
let object_opacity = |s: &Session| s.doc().unwrap().doc.node(m).unwrap().opacity;
|
||||
for (tool, point) in [("mesh", 4), ("directSelection", 4)] {
|
||||
s.select_tool(tool, v).unwrap();
|
||||
click(&mut s, point);
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
s.execute("transparency.set", &json!({"opacity": 30})).unwrap();
|
||||
let o = mesh_opacities(&s, m);
|
||||
assert!((o[4] - 0.3).abs() < 1e-6, "{tool}: the clicked point fades: {o:?}");
|
||||
assert!(o.iter().enumerate().all(|(i, a)| i == 4 || *a == 1.0), "{tool}: the other points stay opaque: {o:?}");
|
||||
assert_eq!(object_opacity(&s), 1.0, "{tool}: the object stays opaque");
|
||||
assert_eq!(s.transparency_info().opacity, Some(30.0), "{tool}: the panel shows the point's opacity");
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo + 1, "one undo step");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert!(mesh_opacities(&s, m).iter().all(|a| *a == 1.0));
|
||||
}
|
||||
// With no point focused the object takes the opacity.
|
||||
s.select_tool("selection", v).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [m.0]})).unwrap();
|
||||
s.execute("transparency.set", &json!({"opacity": 30})).unwrap();
|
||||
assert!((object_opacity(&s) - 0.3).abs() < 1e-6);
|
||||
assert!(mesh_opacities(&s, m).iter().all(|a| *a == 1.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blend_moves_a_mesh_to_the_blended_colour() {
|
||||
let mut s = session();
|
||||
|
||||
@@ -154,6 +154,115 @@ fn images_bend_as_a_raster_mesh_warp() {
|
||||
assert_eq!(svg.matches("<image").count(), 1, "the pieces share the image's pixels");
|
||||
}
|
||||
|
||||
/// A selected embedded 2×2 image, 80×80 pt at (100, 100): a red column left of a blue one.
|
||||
fn two_color_image(s: &mut Session) -> NodeId {
|
||||
let img = image::RgbaImage::from_fn(2, 2, |x, _| if x == 0 { image::Rgba([255, 0, 0, 255]) } else { image::Rgba([0, 0, 255, 255]) });
|
||||
let mut png = vec![];
|
||||
img.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png).unwrap();
|
||||
s.edit("Place", |d, sel| {
|
||||
d.images.insert("two".into(), vectorcraft_doc::ImageBlob::png(png));
|
||||
let id = d.alloc_id();
|
||||
let xf = Affine::translate((100.0, 100.0)) * Affine::scale(40.0);
|
||||
let im = vectorcraft_doc::ImageObject { key: "two".into(), width: 2, height: 2, xf, link: None, placement: Default::default(), pixels: None };
|
||||
d.insert(d.default_layer(), 0, Node::new(id, NodeKind::Image(Box::new(im))))?;
|
||||
sel.set([id]);
|
||||
Ok(id)
|
||||
})
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
/// The pixels well inside the image's frame (the clip edges left out) that are a mix of its red
|
||||
/// and blue rather than one of them.
|
||||
fn mixed_pixels(s: &Session) -> usize {
|
||||
let img = vectorcraft_render::Renderer::new().render_region(&s.doc().unwrap().doc, Rect::new(110.0, 110.0, 170.0, 170.0), 2.0, true);
|
||||
let mut mixed = 0;
|
||||
for y in 0..img.height {
|
||||
for x in 0..img.width {
|
||||
let p = img.pixel(x, y);
|
||||
mixed += (p[0] > 40 && p[2] > 40) as usize;
|
||||
}
|
||||
}
|
||||
mixed
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn anti_alias_off_draws_distorted_images_without_smoothing() {
|
||||
let mut s = session();
|
||||
let im = two_color_image(&mut s);
|
||||
sel(&mut s, &[im]);
|
||||
let e = arch(&mut s, 2.0, json!({}));
|
||||
let smooth = mixed_pixels(&s);
|
||||
assert!(smooth > 400, "with Anti-Alias on the colors blend where the pixels meet: {smooth}");
|
||||
s.execute("object.envelope.options", &json!({"antiAlias": false})).unwrap();
|
||||
let mut pieces = 0;
|
||||
for n in evaluated(&s, e) {
|
||||
n.walk(&mut |c| pieces += (matches!(c.kind, NodeKind::Image(_)) && c.extras().pixelated) as usize);
|
||||
}
|
||||
assert!(pieces >= 32, "{pieces} pixelated pieces");
|
||||
let hard = mixed_pixels(&s);
|
||||
assert!(hard * 50 < smooth, "with Anti-Alias off each pixel is red or blue: {hard} mixed of {smooth}");
|
||||
// The SVG asks viewers not to smooth the pieces either.
|
||||
let svg = |s: &mut Session| {
|
||||
let b64 = s.execute("document.export", &json!({"format": "svg"})).unwrap()["dataBase64"].as_str().unwrap().to_string();
|
||||
String::from_utf8(vectorcraft_format::base64_decode(&b64).unwrap()).unwrap()
|
||||
};
|
||||
assert!(svg(&mut s).matches("image-rendering").count() >= 32);
|
||||
s.execute("object.envelope.options", &json!({"antiAlias": true})).unwrap();
|
||||
assert!(!svg(&mut s).contains("image-rendering"));
|
||||
s.execute("object.envelope.options", &json!({"antiAlias": false})).unwrap();
|
||||
// Expanded, the pieces keep it through a native save.
|
||||
s.execute("object.envelope.expand", &json!({})).unwrap();
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let back = vectorcraft_format::load(&vectorcraft_format::save(d, false)).unwrap();
|
||||
let mut kept = 0;
|
||||
back.walk(|c| kept += (matches!(c.kind, NodeKind::Image(_)) && c.extras().pixelated) as usize);
|
||||
assert_eq!(kept, pieces);
|
||||
}
|
||||
|
||||
fn clip_groups_in(nodes: &[Node]) -> usize {
|
||||
let mut n = 0;
|
||||
for c in nodes {
|
||||
c.walk(&mut |c| n += matches!(c.kind, NodeKind::Group { clip: true, .. }) as usize);
|
||||
}
|
||||
n
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transparency_masks_distorted_images_instead_of_clipping_them() {
|
||||
let mut s = session();
|
||||
let im = red_image(&mut s);
|
||||
sel(&mut s, &[im]);
|
||||
let e = arch(&mut s, 80.0, json!({}));
|
||||
let region = Rect::new(80.0, 60.0, 160.0, 160.0);
|
||||
let clipped = vectorcraft_render::Renderer::new().render_region(&s.doc().unwrap().doc, region, 2.0, true);
|
||||
assert!(clip_groups_in(&evaluated(&s, e)) >= 32);
|
||||
s.execute("object.envelope.options", &json!({"preserveShape": "transparency"})).unwrap();
|
||||
let out = evaluated(&s, e);
|
||||
assert_eq!(clip_groups_in(&out), 0, "no clipping masks");
|
||||
let (mut images, mut masked) = (0, 0);
|
||||
for n in &out {
|
||||
n.walk(&mut |c| {
|
||||
if matches!(c.kind, NodeKind::Image(_)) {
|
||||
images += 1;
|
||||
masked += c.extras().mask.as_ref().is_some_and(|m| m.clip && !m.invert) as usize;
|
||||
}
|
||||
});
|
||||
}
|
||||
assert!(images >= 32 && masked == images, "{masked} of {images} pieces masked");
|
||||
// It draws as the clipping masks did.
|
||||
let alpha = vectorcraft_render::Renderer::new().render_region(&s.doc().unwrap().doc, region, 2.0, true);
|
||||
let (mut sum, mut n) = (0u64, 0u64);
|
||||
for y in 0..clipped.height {
|
||||
for x in 0..clipped.width {
|
||||
let (a, b) = (clipped.pixel(x, y), alpha.pixel(x, y));
|
||||
sum += (0..4).map(|i| (a[i] as i32 - b[i] as i32).unsigned_abs() as u64).sum::<u64>();
|
||||
n += 4;
|
||||
}
|
||||
}
|
||||
assert!(sum < n, "mean difference {} per channel", sum as f64 / n as f64);
|
||||
assert!(alpha.pixel(80, 74)[0] > 200 && alpha.pixel(80, 74)[1] < 60, "the bent image shows: {:?}", alpha.pixel(80, 74));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn symbols_bend_with_the_envelope() {
|
||||
let mut s = session();
|
||||
|
||||
@@ -403,6 +403,58 @@ fn new_layers_go_above_the_current_one_at_its_level() {
|
||||
assert_eq!(unique.len(), names.len(), "{names:?}");
|
||||
}
|
||||
|
||||
/// `below` puts a new layer directly below the current layer, at its level, and `bottom` puts a
|
||||
/// new sublayer under the parent's contents, each in one undo step.
|
||||
#[test]
|
||||
fn new_layers_go_below_the_current_one_and_sublayers_under_the_contents() {
|
||||
let mut s = session();
|
||||
let (layer, sub, a, g, _, c) = tree(&mut s);
|
||||
run(&mut s, "layer.setCurrent", json!({"id": sub.0}));
|
||||
let before = undos(&s);
|
||||
let n = id(&run(&mut s, "layer.new", json!({"below": true})));
|
||||
assert_eq!(children(&s, layer), vec![c, n, sub], "a sublayer directly below the current sublayer");
|
||||
assert_eq!(undos(&s), before + 1);
|
||||
assert_eq!(s.doc().unwrap().highlighted_rows(), vec![n]);
|
||||
assert_eq!(s.doc().unwrap().current_layer(), Some(n));
|
||||
run(&mut s, "edit.undo", json!({}));
|
||||
assert_eq!(children(&s, layer), vec![c, sub]);
|
||||
// At the top level: below the current layer, above the layers under it.
|
||||
let top = id(&run(&mut s, "layer.new", json!({"top": true, "name": "Top"})));
|
||||
let middle = id(&run(&mut s, "layer.new", json!({"below": true, "name": "Middle", "locked": true})));
|
||||
let order = s.doc().unwrap().doc.layers.iter().map(|l| l.id).collect::<Vec<_>>();
|
||||
assert_eq!(order, vec![layer, middle, top]);
|
||||
assert!(node(&s, middle).locked, "options as without `below`");
|
||||
run(&mut s, "layer.setCurrent", json!({"id": layer.0}));
|
||||
let bottom = id(&run(&mut s, "layer.new", json!({"below": true})));
|
||||
assert_eq!(s.doc().unwrap().doc.layers[0].id, bottom, "below the bottom layer");
|
||||
// Sublayers: on top of the parent's contents, or under them with `bottom`.
|
||||
let before = undos(&s);
|
||||
let under = id(&run(&mut s, "layer.newSublayer", json!({"parent": sub.0, "bottom": true})));
|
||||
assert_eq!(children(&s, sub), vec![under, a, g]);
|
||||
assert_eq!(undos(&s), before + 1);
|
||||
assert_eq!(s.doc().unwrap().highlighted_rows(), vec![under]);
|
||||
let over = id(&run(&mut s, "layer.newSublayer", json!({"parent": sub.0, "bottom": false})));
|
||||
assert_eq!(children(&s, sub), vec![under, a, g, over]);
|
||||
// Bad values change nothing.
|
||||
let count = s.doc().unwrap().doc.node_count();
|
||||
for (cmd, p) in [
|
||||
("layer.new", json!({"below": true, "top": true})),
|
||||
("layer.new", json!({"below": "yes"})),
|
||||
("layer.newSublayer", json!({"parent": sub.0, "bottom": 1})),
|
||||
] {
|
||||
assert!(s.execute(cmd, &p).is_err(), "{cmd} {p}");
|
||||
}
|
||||
assert_eq!(s.doc().unwrap().doc.node_count(), count);
|
||||
// In a clipping layer the clipping path stays at the bottom.
|
||||
let mut s = session();
|
||||
let layer = s.doc().unwrap().doc.layers[0].id;
|
||||
let (back, front) = (rect(&mut s, 0.0), rect(&mut s, 100.0));
|
||||
run(&mut s, "layer.clippingMask.toggle", json!({"id": layer.0}));
|
||||
assert_eq!(children(&s, layer), vec![front, back], "the top object clips");
|
||||
let under = id(&run(&mut s, "layer.newSublayer", json!({"parent": layer.0, "bottom": true})));
|
||||
assert_eq!(children(&s, layer), vec![front, under, back]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rows_move_above_below_and_into_layers_and_groups() {
|
||||
let mut s = session();
|
||||
@@ -472,6 +524,60 @@ fn moves_are_validated() {
|
||||
assert_eq!(parent(&s, sub), None);
|
||||
}
|
||||
|
||||
/// The Layers panel's commands refuse a switch given as text or as another value that is not true
|
||||
/// or false, and leave the document, the selection, the highlighted rows and the undo history as
|
||||
/// they were; true, false and null still work.
|
||||
#[test]
|
||||
fn layer_commands_refuse_a_switch_given_as_text() {
|
||||
let fixture = || {
|
||||
let mut s = session();
|
||||
let (layer, sub, a, g, _, c) = tree(&mut s);
|
||||
run(&mut s, "layer.setCurrent", json!({"id": sub.0}));
|
||||
(s, layer, sub, a, g, c)
|
||||
};
|
||||
let (_, layer, sub, a, g, _) = fixture();
|
||||
let mut unexpected = vec![];
|
||||
for (cmd, p, key) in [
|
||||
("layer.new", json!({"top": "true"}), "top"),
|
||||
("layer.new", json!({"top": 1, "name": "Top"}), "top"),
|
||||
("layer.newSublayer", json!({"top": "false"}), "top"),
|
||||
("layer.selectAll", json!({"id": layer.0, "add": "true"}), "add"),
|
||||
("layer.target", json!({"id": layer.0, "add": 1}), "add"),
|
||||
("layer.move", json!({"ids": [a.0], "target": layer.0, "copy": "true"}), "copy"),
|
||||
("layer.move", json!({"ids": [a.0], "target": g.0, "copy": {}}), "copy"),
|
||||
("layer.template", json!({"ids": [layer.0], "on": "false"}), "on"),
|
||||
("layer.pasteRemembersLayers", json!({"on": "false"}), "on"),
|
||||
("layer.pasteRemembersLayers", json!({"on": 0}), "on"),
|
||||
("layer.releaseToLayers", json!({"id": sub.0, "build": [true]}), "build"),
|
||||
] {
|
||||
// Each call on a fresh copy of the same tree, so one that goes through does not change the next.
|
||||
let (mut s, l, sl, o, gr, _) = fixture();
|
||||
assert_eq!((l, sl, o, gr), (layer, sub, a, g), "the same ids");
|
||||
let before = layer_target_snapshot(&s);
|
||||
let r = s.execute(cmd, &p);
|
||||
let want = format!("`{key}` must be true or false");
|
||||
if !matches!(&r, Err(EngineError::BadParams { cmd: got, msg }) if got == cmd && *msg == want) {
|
||||
unexpected.push(format!("{cmd} {p}: {r:?}"));
|
||||
} else if layer_target_snapshot(&s) != before {
|
||||
unexpected.push(format!("{cmd} {p}: refused, but the document, selection, rows or history changed"));
|
||||
}
|
||||
}
|
||||
assert!(unexpected.is_empty(), "not refused: {}", unexpected.join("; "));
|
||||
let (mut s, layer, sub, a, g, c) = fixture();
|
||||
// A null reads as the key left out.
|
||||
let n = id(&run(&mut s, "layer.new", json!({"top": null})));
|
||||
assert_eq!(parent(&s, n), Some(layer), "beside the current sublayer");
|
||||
run(&mut s, "layer.selectAll", json!({"id": sub.0, "add": null}));
|
||||
run(&mut s, "layer.selectAll", json!({"id": c.0, "add": true}));
|
||||
assert_eq!(s.doc().unwrap().selection.objects, vec![a, g, c]);
|
||||
run(&mut s, "layer.move", json!({"ids": [a.0], "target": g.0, "copy": false}));
|
||||
assert_eq!(parent(&s, a), Some(g));
|
||||
assert_eq!(run(&mut s, "layer.template", json!({"ids": [layer.0], "on": null}))["on"], true);
|
||||
assert_eq!(run(&mut s, "layer.template", json!({"ids": [layer.0], "on": false}))["on"], false);
|
||||
assert_eq!(run(&mut s, "layer.pasteRemembersLayers", json!({"on": null}))["on"], true, "null toggles");
|
||||
assert_eq!(ids(&run(&mut s, "layer.releaseToLayers", json!({"id": g.0, "build": null}))).len(), 2);
|
||||
}
|
||||
|
||||
/// `node.move` into a hidden or locked layer deselects the object it moves, here the key object.
|
||||
/// Undo puts the object back and selects it again as the key object.
|
||||
#[test]
|
||||
|
||||
@@ -286,6 +286,145 @@ fn lock_and_hide_other_layers() {
|
||||
assert!(!s.doc().unwrap().doc.node(l2).unwrap().visible);
|
||||
}
|
||||
|
||||
/// Whether `id` is visible and whether it is locked.
|
||||
fn shown_and_locked(s: &Session, id: NodeId) -> (bool, bool) {
|
||||
let n = node(s, id);
|
||||
(n.visible, n.locked)
|
||||
}
|
||||
|
||||
/// Each of `commands` is off in isolation mode: the command list reports it disabled, its error
|
||||
/// names isolation mode, and so does the error of a call that gives the selection as `ids` (for a
|
||||
/// command that takes them).
|
||||
fn off_in_isolation(s: &mut Session, commands: &[&str]) {
|
||||
let ids: Vec<u64> = selected(s).iter().map(|i| i.0).collect();
|
||||
for c in commands {
|
||||
let spec = find_command(c).unwrap();
|
||||
assert_eq!(spec.info(s).disabled_reason.as_deref(), Some("exit isolation mode first"), "{c}");
|
||||
let given = cmd::params::takes_key(spec.params, "ids").then(|| json!({"ids": ids}));
|
||||
for p in std::iter::once(json!({})).chain(given) {
|
||||
let e = s.execute(c, &p).unwrap_err().to_string();
|
||||
assert!(e.contains("exit isolation mode first"), "{c} {p}: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// In isolation mode, Lock › Selection and Hide › Selection are off and change nothing, in an
|
||||
/// isolated group and in an isolated compound path with one of its paths selected. After Exit
|
||||
/// Isolation Mode they lock and hide again.
|
||||
#[test]
|
||||
fn lock_and_hide_selection_are_off_in_isolation_mode() {
|
||||
let mut s = session();
|
||||
let a = rect(&mut s, 0.0, 0.0, 10.0, 10.0);
|
||||
let b = rect(&mut s, 20.0, 0.0, 10.0, 10.0);
|
||||
sel(&mut s, &[a, b]);
|
||||
let g = id_of(&s.execute("object.group", &json!({})).unwrap());
|
||||
let outer = rect(&mut s, 100.0, 100.0, 100.0, 100.0);
|
||||
let hole = rect(&mut s, 120.0, 120.0, 20.0, 20.0);
|
||||
sel(&mut s, &[outer, hole]);
|
||||
let compound = id_of(&s.execute("object.compoundPath.make", &json!({})).unwrap());
|
||||
let ids = [a, b, g, outer, hole, compound];
|
||||
let commands = ["object.lock", "object.hide"];
|
||||
for (isolated, member) in [(g, a), (compound, hole)] {
|
||||
s.execute("object.isolate", &json!({"id": isolated.0})).unwrap();
|
||||
sel(&mut s, &[member]);
|
||||
off_in_isolation(&mut s, &commands);
|
||||
for id in ids {
|
||||
assert_eq!(shown_and_locked(&s, id), (true, false), "{id:?} in isolation of {isolated:?}");
|
||||
}
|
||||
assert_eq!(selected(&s), [member]);
|
||||
s.execute("object.exitIsolation", &json!({})).unwrap();
|
||||
}
|
||||
sel(&mut s, &[a]);
|
||||
s.execute("object.lock", &json!({})).unwrap();
|
||||
sel(&mut s, &[outer]);
|
||||
s.execute("object.hide", &json!({})).unwrap();
|
||||
assert_eq!(shown_and_locked(&s, a), (true, true));
|
||||
assert_eq!(shown_and_locked(&s, compound), (false, false));
|
||||
}
|
||||
|
||||
/// In isolation mode, Unlock All and Show All are off: the locked and the hidden objects outside
|
||||
/// and inside the isolated group stay as they are. Pattern editing mode and opacity mask editing
|
||||
/// are not isolation mode for them.
|
||||
#[test]
|
||||
fn unlock_all_and_show_all_are_off_in_isolation_mode() {
|
||||
let mut s = session();
|
||||
let a = rect(&mut s, 0.0, 0.0, 10.0, 10.0);
|
||||
let b = rect(&mut s, 20.0, 0.0, 10.0, 10.0);
|
||||
let c = rect(&mut s, 40.0, 0.0, 10.0, 10.0);
|
||||
sel(&mut s, &[a, b, c]);
|
||||
let g = id_of(&s.execute("object.group", &json!({})).unwrap());
|
||||
let locked = rect(&mut s, 100.0, 0.0, 10.0, 10.0);
|
||||
let hidden = rect(&mut s, 120.0, 0.0, 10.0, 10.0);
|
||||
s.execute("layer.setProps", &json!({"id": locked.0, "locked": true})).unwrap();
|
||||
s.execute("layer.setProps", &json!({"id": hidden.0, "visible": false})).unwrap();
|
||||
s.execute("layer.setProps", &json!({"id": b.0, "locked": true})).unwrap();
|
||||
s.execute("layer.setProps", &json!({"id": c.0, "visible": false})).unwrap();
|
||||
s.execute("object.isolate", &json!({"id": g.0})).unwrap();
|
||||
sel(&mut s, &[a]);
|
||||
off_in_isolation(&mut s, &["object.unlockAll", "object.showAll"]);
|
||||
for (id, want) in [(locked, (true, true)), (hidden, (false, false)), (b, (true, true)), (c, (false, false))] {
|
||||
assert_eq!(shown_and_locked(&s, id), want, "{id:?}");
|
||||
}
|
||||
s.execute("object.exitIsolation", &json!({})).unwrap();
|
||||
s.execute("object.unlockAll", &json!({})).unwrap();
|
||||
s.execute("object.showAll", &json!({})).unwrap();
|
||||
for id in [locked, hidden, b, c] {
|
||||
assert_eq!(shown_and_locked(&s, id), (true, false), "{id:?}");
|
||||
}
|
||||
|
||||
// Pattern editing mode: the tile's art hides and shows again.
|
||||
let r = rect(&mut s, 300.0, 300.0, 10.0, 10.0);
|
||||
sel(&mut s, &[r]);
|
||||
s.execute("object.pattern.make", &json!({"name": "P"})).unwrap();
|
||||
let tile = selected(&s);
|
||||
s.execute("object.hide", &json!({})).unwrap();
|
||||
assert!(!node(&s, tile[0]).visible, "hidden in pattern editing mode");
|
||||
s.execute("object.showAll", &json!({})).unwrap();
|
||||
assert!(node(&s, tile[0]).visible, "shown in pattern editing mode");
|
||||
// An object isolated inside pattern editing mode is isolation mode again.
|
||||
s.execute("object.isolate", &json!({"id": tile[0].0})).unwrap();
|
||||
sel(&mut s, &[tile[0]]);
|
||||
off_in_isolation(&mut s, &["object.unlockAll", "object.showAll"]);
|
||||
s.execute("object.pattern.cancel", &json!({})).unwrap();
|
||||
|
||||
// Opacity mask editing: the mask's art locks and unlocks again.
|
||||
let obj = rect(&mut s, 0.0, 300.0, 200.0, 200.0);
|
||||
let m = rect(&mut s, 0.0, 300.0, 100.0, 200.0);
|
||||
sel(&mut s, &[obj, m]);
|
||||
s.execute("transparency.makeOpacityMask", &json!({})).unwrap();
|
||||
s.execute("transparency.editOpacityMask", &json!({})).unwrap();
|
||||
let mask = selected(&s);
|
||||
s.execute("object.lock", &json!({})).unwrap();
|
||||
assert!(node(&s, mask[0]).locked, "locked in opacity mask editing");
|
||||
s.execute("object.unlockAll", &json!({})).unwrap();
|
||||
assert!(!node(&s, mask[0]).locked, "unlocked in opacity mask editing");
|
||||
}
|
||||
|
||||
/// In isolation mode, Lock › All Artwork Above, Lock › Other Layers, Hide › All Artwork Above and
|
||||
/// Hide › Other Layers are off: the art above and the other layer stay as they are.
|
||||
#[test]
|
||||
fn lock_and_hide_above_and_other_layers_are_off_in_isolation_mode() {
|
||||
let mut s = session();
|
||||
let a = rect(&mut s, 0.0, 0.0, 100.0, 100.0);
|
||||
let b = rect(&mut s, 200.0, 0.0, 10.0, 10.0);
|
||||
sel(&mut s, &[a, b]);
|
||||
let g = id_of(&s.execute("object.group", &json!({})).unwrap());
|
||||
let over = rect(&mut s, 50.0, 50.0, 100.0, 100.0);
|
||||
let l2 = id_of(&s.execute("layer.new", &json!({})).unwrap());
|
||||
s.execute("object.isolate", &json!({"id": g.0})).unwrap();
|
||||
sel(&mut s, &[a]);
|
||||
off_in_isolation(&mut s, &["object.lock.above", "object.lock.otherLayers", "object.hide.above", "object.hide.otherLayers"]);
|
||||
for id in [a, b, g, over, l2] {
|
||||
assert_eq!(shown_and_locked(&s, id), (true, false), "{id:?}");
|
||||
}
|
||||
s.execute("object.exitIsolation", &json!({})).unwrap();
|
||||
sel(&mut s, &[g]);
|
||||
assert_eq!(s.execute("object.lock.above", &json!({})).unwrap(), json!({"count": 1}));
|
||||
s.execute("object.hide.otherLayers", &json!({})).unwrap();
|
||||
assert_eq!(shown_and_locked(&s, over), (true, true));
|
||||
assert_eq!(shown_and_locked(&s, l2), (false, false));
|
||||
}
|
||||
|
||||
/// How many steps there are to undo and to redo, and whether the document has unsaved changes.
|
||||
fn history(s: &Session) -> (usize, usize, bool) {
|
||||
let st = s.doc().unwrap();
|
||||
@@ -353,6 +492,112 @@ fn lock_and_hide_with_nothing_to_lock_or_hide_record_no_undo_step() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Hide › Unselected Artwork hides, in one undo step, each object that is not selected and holds no selected object:
|
||||
/// here `b`, the member `n` of the group that holds the selected `m`, and `p` on another layer. The selected objects,
|
||||
/// that group, the clip group that holds the selected `c2` with all its members, a locked object, the art on a locked
|
||||
/// layer and an object hidden before stay as they are, and so does the selection. Show All shows them again.
|
||||
#[test]
|
||||
fn hide_unselected_artwork_hides_every_object_that_is_not_selected() {
|
||||
let mut s = session();
|
||||
let a = rect(&mut s, 0.0, 0.0, 10.0, 10.0);
|
||||
let b = rect(&mut s, 20.0, 0.0, 10.0, 10.0);
|
||||
let m = rect(&mut s, 40.0, 0.0, 10.0, 10.0);
|
||||
let n = rect(&mut s, 60.0, 0.0, 10.0, 10.0);
|
||||
sel(&mut s, &[m, n]);
|
||||
let g = id_of(&s.execute("object.group", &json!({})).unwrap());
|
||||
let c1 = rect(&mut s, 80.0, 0.0, 10.0, 10.0);
|
||||
let c2 = rect(&mut s, 85.0, 5.0, 10.0, 10.0);
|
||||
let c3 = rect(&mut s, 80.0, 0.0, 30.0, 30.0);
|
||||
sel(&mut s, &[c1, c2, c3]);
|
||||
let clip = id_of(&s.execute("object.clippingMask.make", &json!({})).unwrap());
|
||||
let k = rect(&mut s, 120.0, 0.0, 10.0, 10.0);
|
||||
s.execute("layer.setProps", &json!({"id": k.0, "locked": true})).unwrap();
|
||||
let h = rect(&mut s, 140.0, 0.0, 10.0, 10.0);
|
||||
s.execute("layer.setProps", &json!({"id": h.0, "visible": false})).unwrap();
|
||||
s.execute("layer.new", &json!({})).unwrap();
|
||||
let p = rect(&mut s, 0.0, 100.0, 10.0, 10.0);
|
||||
let locked_layer = id_of(&s.execute("layer.new", &json!({})).unwrap());
|
||||
let q = rect(&mut s, 20.0, 100.0, 10.0, 10.0);
|
||||
s.execute("layer.setProps", &json!({"id": locked_layer.0, "locked": true})).unwrap();
|
||||
let shown = |s: &Session, ids: &[NodeId]| ids.iter().map(|id| node(s, *id).visible).collect::<Vec<_>>();
|
||||
sel(&mut s, &[a, m, c2]);
|
||||
let steps = undo_len(&s);
|
||||
assert_eq!(s.execute("object.hide.unselected", &json!({})).unwrap(), json!({"count": 3}));
|
||||
assert_eq!(shown(&s, &[b, n, p, h]), [false; 4]);
|
||||
assert_eq!(shown(&s, &[a, m, g, c1, c2, c3, clip, k, q]), [true; 9]);
|
||||
assert!(node(&s, k).locked);
|
||||
assert_eq!((undo_len(&s), selected(&s)), (steps + 1, vec![a, m, c2]));
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(shown(&s, &[b, n, p]), [true; 3]);
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
s.execute("object.showAll", &json!({})).unwrap();
|
||||
assert_eq!(shown(&s, &[b, n, p, h]), [true; 4]);
|
||||
}
|
||||
|
||||
/// Hide › Unselected Artwork leaves the art on a locked artboard as it is, as Select All leaves it unselected.
|
||||
#[test]
|
||||
fn hide_unselected_artwork_leaves_the_art_on_a_locked_artboard() {
|
||||
let mut s = session();
|
||||
let (a, b) = (rect(&mut s, 0.0, 0.0, 10.0, 10.0), rect(&mut s, 20.0, 0.0, 10.0, 10.0));
|
||||
let on_locked = rect(&mut s, 2050.0, 50.0, 10.0, 10.0);
|
||||
s.execute("artboard.new", &json!({"x": 2000, "y": 0, "width": 400, "height": 400})).unwrap();
|
||||
s.execute("artboard.setProps", &json!({"index": 1, "locked": true})).unwrap();
|
||||
sel(&mut s, &[a]);
|
||||
assert_eq!(s.execute("object.hide.unselected", &json!({})).unwrap(), json!({"count": 1}));
|
||||
assert_eq!([a, b, on_locked].map(|id| node(&s, id).visible), [true, false, true]);
|
||||
}
|
||||
|
||||
/// In isolation mode Hide › Unselected Artwork is disabled: the menu item is off, and the command returns an error that
|
||||
/// names isolation mode and changes nothing. While Lock Guides is on it leaves the guides as they are. With nothing
|
||||
/// left to hide it records no undo step and returns a count of 0. A compound path that holds a selected path stays as
|
||||
/// it is.
|
||||
#[test]
|
||||
fn hide_unselected_artwork_keeps_locked_guides_and_is_disabled_in_isolation_mode() {
|
||||
let mut s = session();
|
||||
let guide = rect(&mut s, 0.0, 0.0, 10.0, 10.0);
|
||||
sel(&mut s, &[guide]);
|
||||
s.execute("view.guides.make", &json!({})).unwrap();
|
||||
s.execute("view.guides.lock", &json!({"locked": true})).unwrap();
|
||||
let out = rect(&mut s, 20.0, 0.0, 10.0, 10.0);
|
||||
let m = rect(&mut s, 40.0, 0.0, 10.0, 10.0);
|
||||
let n = rect(&mut s, 60.0, 0.0, 10.0, 10.0);
|
||||
sel(&mut s, &[m, n]);
|
||||
let g = id_of(&s.execute("object.group", &json!({})).unwrap());
|
||||
let hide = |s: &mut Session| s.execute("object.hide.unselected", &json!({})).unwrap();
|
||||
let shown = |s: &Session, ids: &[NodeId]| ids.iter().map(|id| node(s, *id).visible).collect::<Vec<_>>();
|
||||
s.execute("object.isolate", &json!({"id": g.0})).unwrap();
|
||||
select_with_a_step_to_redo(&mut s, &[m]);
|
||||
let before = history(&s);
|
||||
let info = find_command("object.hide.unselected").unwrap().info(&s);
|
||||
assert!(!info.enabled && info.disabled_reason.as_deref() == Some("exit isolation mode first"), "{info:?}");
|
||||
let e = s.execute("object.hide.unselected", &json!({})).unwrap_err();
|
||||
assert!(matches!(e, EngineError::Disabled(..)) && e.to_string().contains("isolation mode"), "{e}");
|
||||
assert_eq!(shown(&s, &[m, n, out, guide]), [true; 4]);
|
||||
assert_eq!((history(&s), selected(&s)), (before, vec![m]));
|
||||
s.execute("object.exitIsolation", &json!({})).unwrap();
|
||||
sel(&mut s, &[m]);
|
||||
assert!(find_command("object.hide.unselected").unwrap().info(&s).enabled);
|
||||
assert_eq!(hide(&mut s), json!({"count": 2}));
|
||||
assert_eq!(shown(&s, &[n, out, guide]), [false, false, true]);
|
||||
select_with_a_step_to_redo(&mut s, &[m]);
|
||||
let before = history(&s);
|
||||
assert_eq!(hide(&mut s), json!({"count": 0}));
|
||||
assert_eq!(history(&s), before);
|
||||
// With Lock Guides off, the guide path is hidden like other art.
|
||||
s.execute("view.guides.lock", &json!({"locked": false})).unwrap();
|
||||
sel(&mut s, &[m]);
|
||||
assert_eq!(hide(&mut s), json!({"count": 1}));
|
||||
assert_eq!(shown(&s, &[guide]), [false]);
|
||||
// A compound path that holds the selected path stays as it is with its paths.
|
||||
let outer = rect(&mut s, 0.0, 200.0, 100.0, 100.0);
|
||||
let hole = rect(&mut s, 40.0, 240.0, 20.0, 20.0);
|
||||
sel(&mut s, &[outer, hole]);
|
||||
let c = id_of(&s.execute("object.compoundPath.make", &json!({})).unwrap());
|
||||
sel(&mut s, &[hole]);
|
||||
assert_eq!(hide(&mut s), json!({"count": 1}));
|
||||
assert_eq!(shown(&s, &[g, c, outer, hole]), [false, true, true, true]);
|
||||
}
|
||||
|
||||
// ---------- Transform Each ----------
|
||||
|
||||
#[test]
|
||||
@@ -645,6 +890,31 @@ fn expand_live_shape_and_text_in_one_undo_step() {
|
||||
assert!(s.doc().unwrap().doc.node(t).is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn expand_text_outlines_its_stroke_too() {
|
||||
let mut s = session();
|
||||
let t = text(&mut s, "Hi");
|
||||
sel(&mut s, &[t]);
|
||||
s.execute("paint.setStroke", &json!({"color": "#ff0000"})).unwrap();
|
||||
let info = s.execute("object.expand.info", &json!({})).unwrap();
|
||||
assert_eq!(info["stroke"], json!(true), "stroked type has a stroke to expand");
|
||||
let n = undo_len(&s);
|
||||
s.execute("object.expand", &json!({})).unwrap();
|
||||
assert_eq!(undo_len(&s), n + 1);
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let (mut paths, mut stroked) = (0, 0);
|
||||
for id in &s.doc().unwrap().selection.objects {
|
||||
d.node(*id).unwrap().walk(&mut |c| {
|
||||
if matches!(c.kind, NodeKind::Path { .. } | NodeKind::Compound { .. }) {
|
||||
paths += 1;
|
||||
stroked += usize::from(!c.appearance.stroke_paint().is_none());
|
||||
}
|
||||
});
|
||||
}
|
||||
assert!(paths > 0);
|
||||
assert_eq!(stroked, 0, "every glyph's stroke became a filled outline ({paths} paths)");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn expand_nothing_errors() {
|
||||
let mut s = session();
|
||||
@@ -1577,6 +1847,20 @@ fn layers_panel_selects_nothing_outside_the_isolated_group() {
|
||||
assert_eq!(selected(&s), vec![b]);
|
||||
}
|
||||
|
||||
/// In isolation mode the selection column of the isolated layer's own row selects the art on it.
|
||||
#[test]
|
||||
fn layers_panel_selects_the_art_of_the_isolated_layer() {
|
||||
let mut s = session();
|
||||
let a = rect(&mut s, 0.0, 0.0, 50.0, 50.0);
|
||||
let b = rect(&mut s, 100.0, 0.0, 50.0, 50.0);
|
||||
let layer = s.active().unwrap().doc.layers[0].id;
|
||||
s.execute("object.isolate", &json!({"id": layer.0})).unwrap();
|
||||
s.execute("layer.selectAll", &json!({"id": layer.0})).unwrap();
|
||||
let mut got = selected(&s);
|
||||
got.sort_by_key(|i| i.0);
|
||||
assert_eq!(got, vec![a, b]);
|
||||
}
|
||||
|
||||
/// In isolation mode a recalled selection leaves out the objects outside the isolated group, and
|
||||
/// Next Object Above selects nothing outside it.
|
||||
#[test]
|
||||
@@ -2601,13 +2885,18 @@ fn guides_and_art_move_together_when_selected() {
|
||||
assert!(s.doc().unwrap().doc.node(copied_id).is_none());
|
||||
assert!(s.doc().unwrap().doc.node(a).is_some());
|
||||
|
||||
guides_and_art_later_cases(&mut s, a);
|
||||
}
|
||||
|
||||
/// Cases 5 to 8 of [`guides_and_art_move_together_when_selected`]: explicit ids, preview copies and moving a guide.
|
||||
fn guides_and_art_later_cases(s: &mut Session, a: NodeId) {
|
||||
// 5. Explicit ids: only the specified art moves, guides stay put.
|
||||
sel(&mut s, &[a]);
|
||||
sel(s, &[a]);
|
||||
s.execute("guide.select", &json!({"indexes": [0, 1], "toggle": true})).unwrap();
|
||||
s.execute("object.transform", &json!({"ids": [a.0], "matrix": [1, 0, 0, 1, 40, 50]})).unwrap();
|
||||
let b = s.doc().unwrap().doc.bounds_of(&[a], false).unwrap();
|
||||
assert_eq!((b.x0, b.y0), (50.0, 60.0));
|
||||
assert_eq!(guides(&s), vec![(true, 200.0), (false, 300.0)]);
|
||||
assert_eq!(guides(s), vec![(true, 200.0), (false, 300.0)]);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
|
||||
// 6. Interactive preview copies: dragging with Alt previews the copies and commit keeps them.
|
||||
@@ -2615,32 +2904,32 @@ fn guides_and_art_move_together_when_selected() {
|
||||
s.preview("object.transform", &json!({"matrix": [1, 0, 0, 1, 10, 20], "copy": true})).unwrap();
|
||||
s.preview("object.transform", &json!({"matrix": [1, 0, 0, 1, 30, 40], "copy": true})).unwrap();
|
||||
s.commit_interaction().unwrap();
|
||||
assert_eq!(guides(&s).len(), 4);
|
||||
assert_eq!(guides(&s)[2], (true, 230.0));
|
||||
assert_eq!(guides(&s)[3], (false, 340.0));
|
||||
assert_eq!(guides(s).len(), 4);
|
||||
assert_eq!(guides(s)[2], (true, 230.0));
|
||||
assert_eq!(guides(s)[3], (false, 340.0));
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(guides(&s), vec![(true, 200.0), (false, 300.0)]);
|
||||
assert_eq!(guides(s), vec![(true, 200.0), (false, 300.0)]);
|
||||
|
||||
// 7. Dragging a guide (guide.move) when art is also selected moves both art and guides.
|
||||
sel(&mut s, &[a]);
|
||||
sel(s, &[a]);
|
||||
s.execute("guide.select", &json!({"indexes": [0, 1], "toggle": true})).unwrap();
|
||||
s.execute("guide.move", &json!({"dx": 10, "dy": 20})).unwrap();
|
||||
let b = s.doc().unwrap().doc.bounds_of(&[a], false).unwrap();
|
||||
assert_eq!((b.x0, b.y0), (20.0, 30.0));
|
||||
assert_eq!(guides(&s), vec![(true, 210.0), (false, 320.0)]);
|
||||
assert_eq!(guides(s), vec![(true, 210.0), (false, 320.0)]);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
let b = s.doc().unwrap().doc.bounds_of(&[a], false).unwrap();
|
||||
assert_eq!((b.x0, b.y0), (10.0, 10.0));
|
||||
assert_eq!(guides(&s), vec![(true, 200.0), (false, 300.0)]);
|
||||
assert_eq!(guides(s), vec![(true, 200.0), (false, 300.0)]);
|
||||
|
||||
// 8. Copy-dragging a guide (guide.move { copy: true }) when art is also selected duplicates both.
|
||||
s.execute("guide.move", &json!({"dx": 10, "dy": 20, "copy": true})).unwrap();
|
||||
assert_eq!(guides(&s).len(), 4);
|
||||
assert_eq!(guides(&s)[2], (true, 210.0));
|
||||
assert_eq!(guides(&s)[3], (false, 320.0));
|
||||
assert_eq!(guides(s).len(), 4);
|
||||
assert_eq!(guides(s)[2], (true, 210.0));
|
||||
assert_eq!(guides(s)[3], (false, 320.0));
|
||||
assert_eq!(s.doc().unwrap().doc.layers[0].children().unwrap().len(), 2);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(guides(&s).len(), 2);
|
||||
assert_eq!(guides(s).len(), 2);
|
||||
assert_eq!(s.doc().unwrap().doc.layers[0].children().unwrap().len(), 1);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
//! Opacity-mask editing and the Transparency panel's command reach (M3.25): commands act on the
|
||||
//! masked object while its mask is edited, accept ids without a selection, take opacity in percent,
|
||||
//! report mixed values, and saves and exports never carry the editing layer.
|
||||
//! report mixed values, and saves and exports never carry the editing layer. Mask editing is not
|
||||
//! entered in isolation mode, and isolation mode is not entered while a mask is edited.
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_geom::Affine;
|
||||
@@ -228,6 +229,103 @@ fn one_object_gets_an_empty_mask_drawn_in_editing_mode() {
|
||||
assert_eq!((s.doc().unwrap().isolation, s.doc().unwrap().active_layer), (Some(layer), Some(layer)));
|
||||
}
|
||||
|
||||
/// The command `cmd` with `p` is off, for `why`.
|
||||
fn off(s: &mut Session, cmd: &str, p: Value, why: &str) {
|
||||
let err = s.execute(cmd, &p).unwrap_err();
|
||||
assert!(matches!(&err, EngineError::Disabled(id, w) if id == cmd && w == why), "{cmd} {p}: {err}");
|
||||
}
|
||||
|
||||
/// Isolation mode is not entered while an opacity mask is edited: Enter Isolation Mode is off for
|
||||
/// a group in the mask art and for one outside it, and mask editing goes on. After Stop Editing
|
||||
/// Opacity Mask it isolates again.
|
||||
#[test]
|
||||
fn isolation_mode_is_not_entered_while_an_opacity_mask_is_edited() {
|
||||
let mut s = session();
|
||||
let group = |s: &mut Session, x: f64| {
|
||||
let (a, b) = (rect(s, x, 20.0), rect(s, x + 30.0, 20.0));
|
||||
s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap();
|
||||
NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap())
|
||||
};
|
||||
let outside = group(&mut s, 220.0);
|
||||
editing_mask(&mut s);
|
||||
let layer = s.doc().unwrap().isolation;
|
||||
let inside = group(&mut s, 0.0);
|
||||
for id in [inside, outside] {
|
||||
off(&mut s, "object.isolate", json!({"id": id.0}), "stop editing the opacity mask first");
|
||||
assert_eq!(s.doc().unwrap().isolation, layer);
|
||||
assert!(doc(&s).mask_edit.is_some(), "still editing");
|
||||
}
|
||||
s.execute("select.set", &json!({"ids": [inside.0]})).unwrap();
|
||||
off(&mut s, "object.isolate", json!({}), "stop editing the opacity mask first");
|
||||
s.execute("transparency.stopEditingOpacityMask", &json!({})).unwrap();
|
||||
s.execute("object.isolate", &json!({"id": outside.0})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().isolation, Some(outside));
|
||||
}
|
||||
|
||||
/// Opacity mask editing is not entered in isolation mode: Edit Opacity Mask and View Opacity Mask
|
||||
/// are off, and so is Make Opacity Mask for one object, which would edit its new empty mask. Make
|
||||
/// Opacity Mask with two objects still masks one with the other. After Exit Isolation Mode the mask
|
||||
/// is edited again.
|
||||
#[test]
|
||||
fn opacity_mask_editing_is_not_entered_in_isolation_mode() {
|
||||
let mut s = session();
|
||||
let (obj, m) = (rect(&mut s, 0.0, 200.0), rect(&mut s, 0.0, 100.0));
|
||||
s.execute("transparency.makeOpacityMask", &json!({"ids": [obj.0, m.0]})).unwrap();
|
||||
let other = rect(&mut s, 220.0, 30.0);
|
||||
s.execute("select.set", &json!({"ids": [obj.0, other.0]})).unwrap();
|
||||
let g = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap());
|
||||
s.execute("object.isolate", &json!({"id": g.0})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [obj.0]})).unwrap();
|
||||
let why = "exit isolation mode first";
|
||||
off(&mut s, "transparency.editOpacityMask", json!({}), why);
|
||||
off(&mut s, "transparency.editOpacityMask", json!({"id": obj.0}), why);
|
||||
off(&mut s, "transparency.viewOpacityMask", json!({}), why);
|
||||
off(&mut s, "transparency.viewOpacityMask", json!({"on": true, "id": obj.0}), why);
|
||||
s.execute("select.set", &json!({"ids": [other.0]})).unwrap();
|
||||
off(&mut s, "transparency.makeOpacityMask", json!({}), why);
|
||||
off(&mut s, "transparency.makeOpacityMask", json!({"ids": [other.0]}), why);
|
||||
// A layer given as the one target is refused too.
|
||||
let layer = doc(&s).layers[0].id;
|
||||
let err = s.execute("transparency.makeOpacityMask", &json!({"ids": [layer.0]})).unwrap_err();
|
||||
assert!(err.to_string().ends_with(why), "{err}");
|
||||
let st = s.doc().unwrap();
|
||||
assert!(st.doc.mask_edit.is_none() && st.mask_view.is_none() && mask(&s, other).is_none());
|
||||
assert_eq!(s.doc().unwrap().isolation, Some(g));
|
||||
// Two objects: the top one masks the other, without mask editing.
|
||||
let top = rect(&mut s, 220.0, 10.0);
|
||||
let r = s.execute("transparency.makeOpacityMask", &json!({"ids": [other.0, top.0]})).unwrap();
|
||||
assert_eq!(r, json!({"id": other.0, "editing": false}));
|
||||
assert_eq!(s.doc().unwrap().isolation, Some(g));
|
||||
// Two members of a compound path given in `ids` are two objects: one masks the other.
|
||||
let (c1, c2) = (rect(&mut s, 250.0, 10.0), rect(&mut s, 265.0, 10.0));
|
||||
s.execute("select.set", &json!({"ids": [c1.0, c2.0]})).unwrap();
|
||||
s.execute("object.compoundPath.make", &json!({})).unwrap();
|
||||
let r = s.execute("transparency.makeOpacityMask", &json!({"ids": [c1.0, c2.0]})).unwrap();
|
||||
assert_eq!(r["editing"], json!(false));
|
||||
assert!(doc(&s).mask_edit.is_none());
|
||||
s.execute("object.exitIsolation", &json!({})).unwrap();
|
||||
s.execute("transparency.editOpacityMask", &json!({"id": obj.0})).unwrap();
|
||||
assert_eq!(doc(&s).mask_edit.map(|e| e.object), Some(obj));
|
||||
}
|
||||
|
||||
/// In isolation mode, Make Opacity Mask is off for a targeted sublayer that holds two objects:
|
||||
/// the sublayer is its one target, which would get an empty mask and enter mask editing.
|
||||
#[test]
|
||||
fn make_opacity_mask_is_off_for_a_targeted_sublayer_in_isolation_mode() {
|
||||
let mut s = session();
|
||||
let layer = doc(&s).layers[0].id;
|
||||
let sub = NodeId(s.execute("layer.newSublayer", &json!({"parent": layer.0})).unwrap()["id"].as_u64().unwrap());
|
||||
s.execute("layer.setCurrent", &json!({"id": sub.0})).unwrap();
|
||||
let (a, b) = (rect(&mut s, 0.0, 20.0), rect(&mut s, 40.0, 20.0));
|
||||
s.execute("object.isolate", &json!({"id": layer.0})).unwrap();
|
||||
s.execute("layer.target", &json!({"id": sub.0})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().selection.target, Some(sub));
|
||||
off(&mut s, "transparency.makeOpacityMask", json!({}), "exit isolation mode first");
|
||||
// Two objects given in `ids` still mask one with the other.
|
||||
let r = s.execute("transparency.makeOpacityMask", &json!({"ids": [a.0, b.0]})).unwrap();
|
||||
assert_eq!(r["editing"], json!(false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mask_and_transparency_commands_take_ids_without_a_selection() {
|
||||
let mut s = session();
|
||||
|
||||
@@ -311,6 +311,39 @@ fn artboard_commands_refuse_a_boolean_or_number_given_as_text() {
|
||||
assert_eq!(node(&s, r).geometric_bounds().map(|b| b.x0), Some(20.0));
|
||||
}
|
||||
|
||||
/// New Artboard refuses a position or size given as text or as another value that is not a number,
|
||||
/// such as "10", and New Artboard and Artboard Options refuse a name that is not text, such as 5.
|
||||
/// The artboards and the undo history stay as they were; a null reads as the key left out.
|
||||
#[test]
|
||||
fn artboard_commands_refuse_a_number_or_name_of_the_wrong_type() {
|
||||
use crate::tests::{not_a_number, not_refused};
|
||||
let mut s = session();
|
||||
s.execute("artboard.new", &json!({})).unwrap();
|
||||
let mut calls: Vec<_> = [
|
||||
("artboard.new", "x", json!({"x": "10"})),
|
||||
("artboard.new", "y", json!({"x": 2000, "y": [0]})),
|
||||
("artboard.new", "width", json!({"width": "200"})),
|
||||
("artboard.new", "height", json!({"height": true})),
|
||||
]
|
||||
.into_iter()
|
||||
.map(|(cmd, key, p)| not_a_number(cmd, p, key))
|
||||
.collect();
|
||||
let not_text = |cmd: &str, p: serde_json::Value| (cmd.to_string(), p, "`name` must be a string".to_string());
|
||||
calls.extend([
|
||||
not_text("artboard.new", json!({"name": 5})),
|
||||
not_text("artboard.setProps", json!({"index": 0, "name": 5})),
|
||||
not_text("artboard.setProps", json!({"index": 0, "x": 50, "name": true})),
|
||||
not_text("artboard.setProps", json!({"artboards": [{"index": 0, "name": "Front"}, {"index": 1, "name": ["Back"]}]})),
|
||||
]);
|
||||
let wrong = not_refused(&mut s, &calls);
|
||||
assert!(wrong.is_empty(), "not refused: {wrong:#?}");
|
||||
s.execute("artboard.new", &json!({"x": null, "y": null, "width": null, "height": null, "name": null})).unwrap();
|
||||
s.execute("artboard.setProps", &json!({"index": 0, "name": "Front"})).unwrap();
|
||||
s.execute("artboard.setProps", &json!({"index": 0, "name": null})).unwrap();
|
||||
let names: Vec<String> = boards(&s).into_iter().map(|(_, n)| n).collect();
|
||||
assert_eq!(names, ["Front", "Artboard 2", "Artboard 3"]);
|
||||
}
|
||||
|
||||
/// Artboards 1 and 2, each with a square on it, and an artboard 3 below artboard 1 with a square on
|
||||
/// it, with the Artboard tool selecting artboards 1 and 2 → the squares' ids, artboard by artboard.
|
||||
fn two_selected_artboards(s: &mut Session) -> [u64; 3] {
|
||||
@@ -656,23 +689,30 @@ fn new_art_on_a_locked_artboard_stays_editable_until_the_selection_leaves_it() {
|
||||
assert_eq!(selected(&s), pasted);
|
||||
s.execute("object.move", &json!({"dx": 10, "dy": 0})).unwrap();
|
||||
assert_eq!(left(&s, pasted[0]), 1160.0);
|
||||
batches_and_isolation_keep_new_art_protected(&mut s, pasted);
|
||||
}
|
||||
|
||||
/// The batch, isolation and group cases of [`new_art_on_a_locked_artboard_stays_editable_until_the_selection_leaves_it`],
|
||||
/// with the pasted art `pasted` selected.
|
||||
fn batches_and_isolation_keep_new_art_protected(s: &mut Session, pasted: Vec<u64>) {
|
||||
let selected = |s: &Session| s.doc().unwrap().selection.objects.iter().map(|id| id.0).collect::<Vec<_>>();
|
||||
let editable = |s: &Session| s.doc().unwrap().doc.is_editable(NodeId(pasted[0]));
|
||||
// A batch that deselects it and fails brings it back selected and editable, and so does
|
||||
// undoing a batch that deselects it and draws.
|
||||
let failed = json!({"commands": [{"command": "select.none"}, {"command": "no.such.command"}]});
|
||||
assert!(s.execute("command.batch", &failed).is_err());
|
||||
assert_eq!((selected(&s), editable(&s)), (pasted.clone(), true), "after a failed batch");
|
||||
assert_eq!((selected(s), editable(s)), (pasted.clone(), true), "after a failed batch");
|
||||
let drawn =
|
||||
json!({"commands": [{"command": "select.none"}, {"command": "shape.rectangle", "params": {"x": 300, "y": 300, "width": 20, "height": 20}}]});
|
||||
s.execute("command.batch", &drawn).unwrap();
|
||||
assert!(!editable(&s), "deselected by the batch");
|
||||
assert!(!editable(s), "deselected by the batch");
|
||||
// Undo inside a later batch gives the document the new art of the selection it brings back:
|
||||
// the unlocked artboard over that art then moves it along.
|
||||
let up = json!({"command": "artboard.move", "params": {"index": 2, "dx": 0, "dy": 1000, "moveArt": true}});
|
||||
let r = s.execute("command.batch", &json!({"commands": [{"command": "edit.undo"}, up.clone()]})).unwrap();
|
||||
assert!(ids(&r["results"][1]["moved"]).contains(&pasted[0]), "undone inside a batch: {r}");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!((selected(&s), editable(&s)), (pasted.clone(), true), "the batch undone");
|
||||
assert_eq!((selected(s), editable(s)), (pasted.clone(), true), "the batch undone");
|
||||
// Isolation mode inside a batch protects it before the next step.
|
||||
let isolated = json!({"commands": [{"command": "object.isolate", "params": {"id": pasted[0]}}, up]});
|
||||
let r = s.execute("command.batch", &isolated).unwrap();
|
||||
@@ -681,21 +721,21 @@ fn new_art_on_a_locked_artboard_stays_editable_until_the_selection_leaves_it() {
|
||||
s.execute("object.exitIsolation", &json!({})).unwrap();
|
||||
// Isolation mode starts with nothing selected, so leaving it doesn't select the art again,
|
||||
// and the art, protected again, isn't isolated, as locked art isn't.
|
||||
assert!(selected(&s).is_empty(), "after isolation mode");
|
||||
assert!(!editable(&s), "after isolation mode");
|
||||
assert!(selected(s).is_empty(), "after isolation mode");
|
||||
assert!(!editable(s), "after isolation mode");
|
||||
assert!(s.execute("object.isolate", &json!({"id": pasted[0]})).is_err(), "isolating it again");
|
||||
// A member of a new group can be selected while the group is new art.
|
||||
let copy = ids(&s.execute("edit.paste", &json!({"center": [1200, 150]})).unwrap()["ids"]);
|
||||
s.execute("object.group", &json!({})).unwrap();
|
||||
let group = selected(&s);
|
||||
let group = selected(s);
|
||||
s.execute("select.set", &json!({"ids": copy})).unwrap();
|
||||
assert_eq!(selected(&s), copy, "a member of a new group");
|
||||
assert_eq!(selected(s), copy, "a member of a new group");
|
||||
s.execute("select.set", &json!({"ids": group})).unwrap();
|
||||
assert_eq!(selected(&s), group, "from the member back to the group");
|
||||
assert_eq!(selected(s), group, "from the member back to the group");
|
||||
s.execute("object.ungroup", &json!({})).unwrap();
|
||||
assert_eq!(selected(&s), copy, "ungrouped");
|
||||
assert_eq!(selected(s), copy, "ungrouped");
|
||||
s.execute("object.move", &json!({"dx": 10, "dy": 0})).unwrap();
|
||||
assert_eq!(selected(&s), copy, "the ungrouped art moves");
|
||||
assert_eq!(selected(s), copy, "the ungrouped art moves");
|
||||
}
|
||||
|
||||
/// When an artboard moves, scales or is cut with its art, the art inside it that is also on a
|
||||
@@ -933,7 +973,9 @@ fn artboard_display_marks_stay_out_of_exports() {
|
||||
let on = doc(&s);
|
||||
assert_ne!(on.artboards, off.artboards);
|
||||
assert_eq!(vectorcraft_svg::export(&on, &Default::default()), vectorcraft_svg::export(&off, &Default::default()));
|
||||
assert_eq!(crate::export_pdf(&on, &Default::default()).unwrap(), crate::export_pdf(&off, &Default::default()).unwrap());
|
||||
// Fix the export date so metadata does not change between the two exports.
|
||||
let options = vectorcraft_pdf::PdfOptions { created: Some(0), ..Default::default() };
|
||||
assert_eq!(crate::export_pdf(&on, &options).unwrap(), crate::export_pdf(&off, &options).unwrap());
|
||||
}
|
||||
|
||||
/// Artboard Options' video display options: `artboard.setProps {showVideoSafeAreas,
|
||||
|
||||
@@ -430,6 +430,17 @@ fn offset_rect_grows_bounds() {
|
||||
assert!(close(bounds(&s, r[0]), Rect::new(105.0, 105.0, 145.0, 135.0), 1e-2));
|
||||
}
|
||||
|
||||
/// #3773: an open path whose last anchor sits on its first is offset as the closed shape, not as a
|
||||
/// ring around its outline.
|
||||
#[test]
|
||||
fn offset_open_path_whose_ends_meet_grows_the_shape() {
|
||||
let mut s = session();
|
||||
s.execute("path.create", &json!({"d": "M0 0 L40 0 L40 40 L0 40 L0 0"})).unwrap();
|
||||
let r = ids(&s.execute("object.path.offsetPath", &json!({"offset": 2, "joins": "miter"})).unwrap());
|
||||
assert!((area_of(&s, r[0]) - 44.0 * 44.0).abs() < 1e-3, "{}", area_of(&s, r[0]));
|
||||
assert_eq!(path_of(&s, r[0]).0.subpaths.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn outline_stroke_of_line() {
|
||||
let mut s = session();
|
||||
|
||||
@@ -141,6 +141,23 @@ fn edit_text_shows_it_flat_in_isolation_then_projects_it_again() {
|
||||
assert!(s2.execute("perspective.editText", &json!({"id": rect.0})).is_err());
|
||||
}
|
||||
|
||||
/// Edit Text, which shows the type in isolation mode, is off while an opacity mask is edited.
|
||||
#[test]
|
||||
fn edit_text_is_off_while_an_opacity_mask_is_edited() {
|
||||
let (mut s, rect) = persp_session();
|
||||
let id = persp_text(&mut s);
|
||||
s.execute("transparency.makeOpacityMask", &json!({"ids": [rect.0]})).unwrap();
|
||||
let layer = s.doc().unwrap().isolation;
|
||||
assert!(layer.is_some(), "editing the new empty mask");
|
||||
let err = s.execute("perspective.editText", &json!({"id": id.0})).unwrap_err();
|
||||
assert!(matches!(&err, EngineError::Disabled(_, why) if why == "stop editing the opacity mask first"), "{err}");
|
||||
assert_eq!(s.doc().unwrap().isolation, layer);
|
||||
assert!(node(&s, id).projection().is_some(), "still projected");
|
||||
s.execute("transparency.stopEditingOpacityMask", &json!({})).unwrap();
|
||||
s.execute("perspective.editText", &json!({"id": id.0})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().isolation, Some(id));
|
||||
}
|
||||
|
||||
/// Edit Text ending while a background save writes the file leaves the document saved once the
|
||||
/// file is written: the flat view is never saved. Saved while editing and edited after: undoing
|
||||
/// the edit once Edit Text has ended is back at what the file holds.
|
||||
|
||||
@@ -998,6 +998,114 @@ fn whole_objects_moved_with_selected_anchors_follow_transform_pattern_tiles() {
|
||||
}
|
||||
}
|
||||
|
||||
/// A Direct Selection or Group Selection drag that starts on the fill of a path selected whole,
|
||||
/// while another path has an anchor selected, moves the path whole and only that anchor of the
|
||||
/// other path, in one undo step, as a drag of the anchor does. With Transform Pattern Tiles on,
|
||||
/// the tiles of the path selected whole move with it. Alt pressed during the drag copies the
|
||||
/// selected objects instead.
|
||||
#[test]
|
||||
fn a_drag_from_a_fill_moves_only_the_selected_anchors_of_partly_selected_paths() {
|
||||
use vectorcraft_tools::PointerKind::{Down, Drag, Up};
|
||||
let mut s = new_doc();
|
||||
let filled = patterned_square(&mut s);
|
||||
let other = square(&mut s, 300.0, 100.0);
|
||||
let v = ViewInfo { smart_guides: false, snap_to_point: false, corner_widgets: false, ..ViewInfo::default() };
|
||||
set_pref(&mut s, "transformPatternTiles", json!(true));
|
||||
let xs = |s: &Session| -> Vec<f64> {
|
||||
s.doc().unwrap().doc.node(other).unwrap().path_data().unwrap().subpaths[0].anchors.iter().map(|a| a.p.x).collect()
|
||||
};
|
||||
let before = xs(&s);
|
||||
for tool in ["directSelection", "groupSelection"] {
|
||||
s.select_tool(tool, v).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [filled.0]})).unwrap();
|
||||
s.execute("select.anchors", &json!({"id": other.0, "anchors": [[0, 0]], "mode": "add"})).unwrap();
|
||||
gesture(&mut s, v, &[(Down, 125.0, 125.0), (Drag, 130.0, 125.0), (Drag, 135.0, 125.0), (Up, 135.0, 125.0)]);
|
||||
assert_eq!(top_left(&s, filled), (110.0, 100.0), "{tool}: the square moves whole");
|
||||
let mut moved = before.clone();
|
||||
moved[0] += 10.0;
|
||||
assert_eq!(xs(&s), moved, "{tool}: the selected anchor moves alone");
|
||||
assert_eq!(s.journal.last().unwrap().0, "path.moveAnchors", "{tool}");
|
||||
assert_eq!(s.doc().unwrap().history.undo.last().map(|e| e.label.as_str()), Some("Move"), "{tool}");
|
||||
assert_eq!(pattern_tiles(&s, filled), [1.0, 0.0, 0.0, 1.0, 10.0, 0.0], "{tool}: the tiles move with the square");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!((top_left(&s, filled), xs(&s)), ((100.0, 100.0), before.clone()), "{tool}: one undo step");
|
||||
// Alt pressed during the drag copies the selected objects and leaves the originals.
|
||||
let count = |s: &Session| s.doc().unwrap().doc.layers[0].children().map_or(0, |c| c.len());
|
||||
let objects = count(&s);
|
||||
s.execute("select.set", &json!({"ids": [filled.0]})).unwrap();
|
||||
s.execute("select.anchors", &json!({"id": other.0, "anchors": [[0, 0]], "mode": "add"})).unwrap();
|
||||
let alt = vectorcraft_tools::Mods { alt: true, ..vectorcraft_tools::Mods::default() };
|
||||
s.pointer(&PointerEvent::new(Down, 125.0, 125.0), v).unwrap();
|
||||
s.pointer(&PointerEvent::new(Drag, 130.0, 125.0), v).unwrap();
|
||||
s.pointer(&PointerEvent::new(Drag, 135.0, 125.0).with_mods(alt), v).unwrap();
|
||||
s.pointer(&PointerEvent::new(Up, 135.0, 125.0).with_mods(alt), v).unwrap();
|
||||
assert_eq!((top_left(&s, filled), xs(&s), count(&s)), ((100.0, 100.0), before.clone(), objects + 2), "{tool}: copied");
|
||||
assert_eq!(s.doc().unwrap().history.undo.last().map(|e| e.label.as_str()), Some("Copy"), "{tool}");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(count(&s), objects, "{tool}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A path selected whole inside a selected group moves once, with the group, when the selection
|
||||
/// also holds a direct-selected anchor: a Group Selection drag from the path's fill (which adds
|
||||
/// the group) and an arrow key move both by 10 pt.
|
||||
#[test]
|
||||
fn a_path_in_a_selected_group_moves_once_with_selected_anchors() {
|
||||
use vectorcraft_tools::PointerKind::{Down, Drag, Up};
|
||||
let mut s = new_doc();
|
||||
let (a, b) = (square(&mut s, 100.0, 100.0), square(&mut s, 160.0, 100.0));
|
||||
s.execute("select.set", &json!({"ids": [a.0, b.0]})).unwrap();
|
||||
let g = NodeId(s.execute("object.group", &json!({})).unwrap()["id"].as_u64().unwrap());
|
||||
let other = square(&mut s, 300.0, 100.0);
|
||||
let v = ViewInfo { smart_guides: false, snap_to_point: false, corner_widgets: false, ..ViewInfo::default() };
|
||||
let first_x = |s: &Session| s.doc().unwrap().doc.node(other).unwrap().path_data().unwrap().subpaths[0].anchors[0].p.x;
|
||||
s.select_tool("groupSelection", v).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [a.0]})).unwrap();
|
||||
s.execute("select.anchors", &json!({"id": other.0, "anchors": [[0, 0]], "mode": "add"})).unwrap();
|
||||
gesture(&mut s, v, &[(Down, 125.0, 125.0), (Drag, 130.0, 125.0), (Drag, 135.0, 125.0), (Up, 135.0, 125.0)]);
|
||||
assert!(s.doc().unwrap().selection.contains(g), "the press added the group");
|
||||
assert_eq!((top_left(&s, a), top_left(&s, b), first_x(&s)), ((110.0, 100.0), (170.0, 100.0), 310.0), "the drag");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [g.0, a.0]})).unwrap();
|
||||
s.execute("select.anchors", &json!({"id": other.0, "anchors": [[0, 0]], "mode": "add"})).unwrap();
|
||||
s.execute("object.nudge", &json!({"dx": 1, "dy": 0, "big": true})).unwrap();
|
||||
assert_eq!((top_left(&s, a), top_left(&s, b), first_x(&s)), ((110.0, 100.0), (170.0, 100.0), 310.0), "the arrow key");
|
||||
}
|
||||
|
||||
/// Transform Again after a Direct Selection drag from a fill with an anchor of another path
|
||||
/// selected moves the path selected whole and only that anchor again, by the same distance. A
|
||||
/// drag back to where it began leaves that move to repeat, and `path.moveAnchors` run as a
|
||||
/// command is repeated too.
|
||||
#[test]
|
||||
fn transform_again_repeats_a_drag_from_a_fill_with_anchors_selected() {
|
||||
use vectorcraft_tools::PointerKind::{Down, Drag, Up};
|
||||
let mut s = new_doc();
|
||||
let filled = square(&mut s, 100.0, 100.0);
|
||||
let other = square(&mut s, 300.0, 100.0);
|
||||
let v = ViewInfo { smart_guides: false, snap_to_point: false, corner_widgets: false, ..ViewInfo::default() };
|
||||
let xs = |s: &Session| -> Vec<f64> {
|
||||
s.doc().unwrap().doc.node(other).unwrap().path_data().unwrap().subpaths[0].anchors.iter().map(|a| a.p.x).collect()
|
||||
};
|
||||
let mut moved = xs(&s);
|
||||
s.select_tool("directSelection", v).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [filled.0]})).unwrap();
|
||||
s.execute("select.anchors", &json!({"id": other.0, "anchors": [[0, 0]], "mode": "add"})).unwrap();
|
||||
gesture(&mut s, v, &[(Down, 125.0, 125.0), (Drag, 130.0, 125.0), (Drag, 135.0, 125.0), (Up, 135.0, 125.0)]);
|
||||
s.execute("object.transformAgain", &json!({})).unwrap();
|
||||
moved[0] += 20.0;
|
||||
assert_eq!((top_left(&s, filled), xs(&s)), ((120.0, 100.0), moved.clone()));
|
||||
assert_eq!(s.doc().unwrap().history.undo.last().map(|e| e.label.as_str()), Some("Transform Again"));
|
||||
// A drag that ends where it began leaves the move to repeat.
|
||||
gesture(&mut s, v, &[(Down, 145.0, 125.0), (Drag, 155.0, 125.0), (Drag, 145.0, 125.0), (Up, 145.0, 125.0)]);
|
||||
s.execute("object.transformAgain", &json!({})).unwrap();
|
||||
moved[0] += 10.0;
|
||||
assert_eq!((top_left(&s, filled), xs(&s)), ((130.0, 100.0), moved), "after a drag back to its start");
|
||||
// The command, as an action plays it back, is repeated the same way.
|
||||
s.execute("path.moveAnchors", &json!({"dx": 0, "dy": 5})).unwrap();
|
||||
s.execute("object.transformAgain", &json!({})).unwrap();
|
||||
assert_eq!(top_left(&s, filled), (130.0, 110.0), "path.moveAnchors");
|
||||
}
|
||||
|
||||
/// General › Select Same Tint % (#394): off, Select › Same › Fill Color takes every tint of the
|
||||
/// swatch; on, only the same tint.
|
||||
#[test]
|
||||
|
||||
@@ -84,7 +84,7 @@ fn save_as_takes_on_path_format_and_options() {
|
||||
let svg = s.execute("file.formatOptions", &json!({"format": "svg"})).unwrap();
|
||||
assert_eq!(svg["options"]["outlineText"]["value"], false, "another format shows its defaults");
|
||||
let ids: Vec<&str> = o["saveFormats"].as_array().unwrap().iter().map(|f| f["id"].as_str().unwrap()).collect();
|
||||
assert_eq!(ids, ["vectorcraft", "template", "pdf", "svg", "svgz", "ai"]);
|
||||
assert_eq!(ids, ["vectorcraft", "template", "pdf", "svg", "svgz", "ai", "eps"]);
|
||||
// SVGZ is gzip-compressed SVG and reopens as SVGZ.
|
||||
let svgz = path(&d, "doc.svgz");
|
||||
s.execute("file.saveAs", &json!({"path": svgz})).unwrap();
|
||||
@@ -405,3 +405,141 @@ fn transparency_grid_round_trips_without_dirtying() {
|
||||
assert!(!s.doc().unwrap().transparency_grid);
|
||||
let _ = std::fs::remove_dir_all(d);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn opened_eps_saves_to_its_own_file_with_editable_data_and_page_margins() {
|
||||
let d = dir("eps-original-format");
|
||||
let eps_path = path(&d, "Artwork.eps");
|
||||
// Original, contributor-authored PostScript: the artwork occupies only part of the page.
|
||||
let source = b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 200 100\n0 0 1 setrgbcolor 30 20 40 30 rectfill\nshowpage\n%%EOF\n";
|
||||
std::fs::write(&eps_path, source).unwrap();
|
||||
let mut s = Session::new();
|
||||
s.execute("document.open", &json!({"path": eps_path})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().format, "eps");
|
||||
assert_eq!(s.doc().unwrap().path.as_deref(), Some(eps_path.as_str()));
|
||||
let board = s.doc().unwrap().doc.artboards[0].rect;
|
||||
assert_eq!((board.width(), board.height()), (200.0, 100.0));
|
||||
rect(&mut s);
|
||||
let edited_layers = s.doc().unwrap().doc.layers.clone();
|
||||
let r = s.execute("document.save", &json!({"options": {"previewFormat": "none", "thumbnails": false}})).unwrap();
|
||||
assert_eq!((r["path"].as_str(), r["format"].as_str()), (Some(eps_path.as_str()), Some("eps")));
|
||||
let bytes = std::fs::read(&eps_path).unwrap();
|
||||
assert!(bytes.starts_with(b"%!PS-Adobe-3.0 EPSF-3.0"));
|
||||
assert!(std::str::from_utf8(vectorcraft_eps::sections(&bytes).unwrap().0).unwrap().contains("%%BoundingBox: 0 0 200 100"));
|
||||
let editing = vectorcraft_format::load(&vectorcraft_eps::native(&bytes).unwrap()).unwrap();
|
||||
assert_eq!(editing.artboards[0].rect, board);
|
||||
assert_eq!(editing.layers, edited_layers);
|
||||
assert!(!s.doc().unwrap().is_dirty());
|
||||
let opened = s.execute("document.open", &json!({"path": eps_path})).unwrap();
|
||||
assert_eq!(opened["restored"], true);
|
||||
assert_eq!(s.doc().unwrap().doc.layers, edited_layers);
|
||||
assert_eq!(s.doc().unwrap().doc.artboards[0].rect, board);
|
||||
rect(&mut s);
|
||||
s.execute("document.save", &json!({})).unwrap();
|
||||
assert_eq!(s.doc().unwrap().path.as_deref(), Some(eps_path.as_str()));
|
||||
std::fs::remove_dir_all(d).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eps_save_keeps_all_editing_artboards_while_printing_one_page() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 200, "height": 100, "artboards": 2})).unwrap();
|
||||
rect(&mut s);
|
||||
let r = s.execute("document.save", &json!({"format": "eps", "artboard": 1, "previewFormat": "none", "thumbnails": false})).unwrap();
|
||||
let bytes = b64(&r);
|
||||
let native = vectorcraft_format::load(&vectorcraft_eps::native(&bytes).unwrap()).unwrap();
|
||||
assert_eq!(native.artboards.len(), 2);
|
||||
assert_eq!(native.layers, s.doc().unwrap().doc.layers);
|
||||
assert!(s.execute("document.save", &json!({"format": "eps", "range": "all"})).is_err(), "EPS is one printed page");
|
||||
for options in [json!({"useArtboards": false}), json!({"selectedOnly": true})] {
|
||||
assert!(s.execute("document.save", &json!({"format": "eps", "options": options})).is_err(), "cropping is Export's job");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eps_save_retains_lossy_import_overwrite_protection_and_transparency_warning() {
|
||||
let d = dir("eps-import-warning");
|
||||
let eps_path = path(&d, "Artwork.eps");
|
||||
let mut s = session();
|
||||
rect(&mut s);
|
||||
s.execute("transparency.set", &json!({"opacity": 50})).unwrap();
|
||||
let r = s.execute("document.save", &json!({"path": eps_path, "previewFormat": "none", "thumbnails": false})).unwrap();
|
||||
assert!(r["warnings"].as_array().unwrap().iter().any(|w| w.as_str().unwrap().contains("flatten")), "{r}");
|
||||
let original = std::fs::read(&eps_path).unwrap();
|
||||
let st = s.doc_mut().unwrap();
|
||||
st.imported_from = Some(eps_path.clone());
|
||||
st.import_losses.push("test import omitted artwork".into());
|
||||
rect(&mut s);
|
||||
assert!(s.execute("document.save", &json!({})).unwrap_err().to_string().contains("acknowledgeLoss"));
|
||||
assert_eq!(std::fs::read(&eps_path).unwrap(), original);
|
||||
s.execute("document.save", &json!({"acknowledgeLoss": true})).unwrap();
|
||||
assert_ne!(std::fs::read(&eps_path).unwrap(), original);
|
||||
std::fs::remove_dir_all(d).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eps_save_supports_an_empty_artboard_without_cropping_its_page() {
|
||||
let mut s = session();
|
||||
let r = s.execute("document.save", &json!({"format": "eps", "previewFormat": "none", "thumbnails": false})).unwrap();
|
||||
let bytes = b64(&r);
|
||||
let native = vectorcraft_format::load(&vectorcraft_eps::native(&bytes).unwrap()).unwrap();
|
||||
assert_eq!(native.artboards[0].rect, s.doc().unwrap().doc.artboards[0].rect);
|
||||
assert_eq!(native.node_count(), s.doc().unwrap().doc.node_count());
|
||||
assert!(std::str::from_utf8(vectorcraft_eps::sections(&bytes).unwrap().0).unwrap().contains("%%BoundingBox: 0 0 200 100"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn partially_read_eps_requires_confirmation_before_save_back() {
|
||||
let d = dir("eps-partial-import");
|
||||
let eps_path = path(&d, "partial.eps");
|
||||
let original = b"%!PS-Adobe-3.0 EPSF-3.0\n%%BoundingBox: 0 0 200 100\n0 0 1 setrgbcolor 30 20 40 30 rectfill\nunsupportedMockyOperator\n1 0 0 setrgbcolor 80 20 40 30 rectfill\nshowpage\n%%EOF\n";
|
||||
std::fs::write(&eps_path, original).unwrap();
|
||||
let mut s = Session::new();
|
||||
let opened = s.execute("document.open", &json!({"path": eps_path})).unwrap();
|
||||
assert!(opened["warnings"].as_array().unwrap().iter().any(|w| w.as_str().unwrap().starts_with("the file was read up to an error")), "{opened}");
|
||||
rect(&mut s);
|
||||
assert!(s.execute("document.save", &json!({})).unwrap_err().to_string().contains("acknowledgeLoss"));
|
||||
assert_eq!(std::fs::read(&eps_path).unwrap(), original);
|
||||
s.execute("document.save", &json!({"acknowledgeLoss": true})).unwrap();
|
||||
assert!(vectorcraft_eps::native(&std::fs::read(&eps_path).unwrap()).is_some());
|
||||
std::fs::remove_dir_all(d).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eps_with_damaged_editing_data_requires_confirmation_before_save_back() {
|
||||
let d = dir("eps-damaged-editing");
|
||||
let eps_path = path(&d, "damaged.eps");
|
||||
let mut s = session();
|
||||
rect(&mut s);
|
||||
let r = s.execute("document.exportEps", &json!({"previewFormat": "none", "thumbnails": false, "cmykPostScript": false})).unwrap();
|
||||
let mut bytes = b64(&r);
|
||||
let at = bytes.windows(9).position(|w| w == b"native\n% ").unwrap() + 12;
|
||||
bytes[at] = b'z';
|
||||
std::fs::write(&eps_path, &bytes).unwrap();
|
||||
let opened = s.execute("document.open", &json!({"path": eps_path})).unwrap();
|
||||
assert_eq!(opened["restored"], false);
|
||||
assert!(opened["warnings"].as_array().unwrap().iter().any(|w| w.as_str().unwrap().contains("editing data can't be read")), "{opened}");
|
||||
rect(&mut s);
|
||||
assert!(s.execute("document.save", &json!({})).unwrap_err().to_string().contains("acknowledgeLoss"));
|
||||
assert_eq!(std::fs::read(&eps_path).unwrap(), bytes);
|
||||
std::fs::remove_dir_all(d).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eps_save_bounds_large_page_preview_without_cropping_vector_or_editing_data() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 4000, "height": 4000})).unwrap();
|
||||
rect(&mut s);
|
||||
// Explicit colour preview remains a colour preview; Save bounds its resolution, not the art.
|
||||
let r = s.execute("document.save", &json!({"format": "eps", "previewFormat": "tiffColor"})).unwrap();
|
||||
let bytes = b64(&r);
|
||||
let (ps, tiff) = vectorcraft_eps::sections(&bytes).unwrap();
|
||||
let tiff = tiff.unwrap();
|
||||
assert!(tiff.len() < 4 * 1024 * 1024 + 64 * 1024, "compatibility preview grew to {} bytes", tiff.len());
|
||||
let preview = image::load_from_memory_with_format(tiff, image::ImageFormat::Tiff).unwrap();
|
||||
assert_eq!((preview.width(), preview.height()), (1024, 1024));
|
||||
assert!(std::str::from_utf8(ps).unwrap().contains("%%BoundingBox: 0 0 4000 4000"));
|
||||
let native = vectorcraft_format::load(&vectorcraft_eps::native(&bytes).unwrap()).unwrap();
|
||||
assert_eq!(native.artboards, s.doc().unwrap().doc.artboards);
|
||||
assert_eq!(native.layers, s.doc().unwrap().doc.layers);
|
||||
}
|
||||
|
||||
@@ -356,6 +356,50 @@ fn shaper_isolate_refuses_a_locked_or_hidden_source_group() {
|
||||
}
|
||||
}
|
||||
|
||||
/// While an opacity mask or a pattern is edited, its editing layer stays isolated: a Shaper source
|
||||
/// pick selects the original shape without isolating it, and a scribble, Release and Expand keep the
|
||||
/// layer isolated. Leaving the mode then ends isolation.
|
||||
#[test]
|
||||
fn shaper_keeps_the_mask_and_pattern_editing_layers_isolated() {
|
||||
for mode in ["mask", "pattern"] {
|
||||
let mut s = session();
|
||||
let object = rectangle(&mut s, 300.0, "#000000");
|
||||
let layer = if mode == "mask" {
|
||||
let m = rectangle(&mut s, 300.0, "#ffffff");
|
||||
s.execute("select.set", &json!({"ids": [object.0, m.0]})).unwrap();
|
||||
s.execute("transparency.makeOpacityMask", &json!({})).unwrap();
|
||||
s.execute("transparency.editOpacityMask", &json!({})).unwrap()["layer"].as_u64().map(NodeId)
|
||||
} else {
|
||||
s.execute("object.pattern.make", &json!({"name": "P"})).unwrap();
|
||||
s.doc().unwrap().doc.pattern_edit.as_ref().map(|e| e.layer)
|
||||
};
|
||||
let isolation = |s: &Session| s.doc().unwrap().isolation;
|
||||
assert!(layer.is_some() && isolation(&s) == layer, "{mode}");
|
||||
let a = rectangle(&mut s, 0.0, "#ff0000");
|
||||
let b = rectangle(&mut s, 50.0, "#0000ff");
|
||||
let make = |s: &mut Session| {
|
||||
let pts = [[25, 20], [75, 35], [30, 50], [125, 65]];
|
||||
NodeId(s.execute("shaper.scribble", &json!({"points": pts, "ids": [a.0, b.0]})).unwrap()["id"].as_u64().unwrap())
|
||||
};
|
||||
let g = make(&mut s);
|
||||
assert_eq!(s.doc().unwrap().doc.parent_of(g), layer, "{mode}");
|
||||
assert_eq!(isolation(&s), layer, "{mode}: scribble");
|
||||
let picked = s.execute("shaper.select", &json!({"point": [25, 50], "source": true})).unwrap();
|
||||
assert_eq!(picked["id"], json!(a.0), "{mode}");
|
||||
assert_eq!(s.doc().unwrap().selection.objects, vec![a], "{mode}");
|
||||
assert_eq!(isolation(&s), layer, "{mode}: source pick");
|
||||
s.execute("shaper.release", &json!({"id": g.0})).unwrap();
|
||||
assert_eq!(isolation(&s), layer, "{mode}: release");
|
||||
let g = make(&mut s);
|
||||
s.execute("shaper.expand", &json!({"id": g.0})).unwrap();
|
||||
assert_eq!(isolation(&s), layer, "{mode}: expand");
|
||||
let leave = if mode == "mask" { "transparency.stopEditingOpacityMask" } else { "object.pattern.done" };
|
||||
s.execute(leave, &json!({})).unwrap();
|
||||
let st = s.doc().unwrap();
|
||||
assert!(st.doc.mask_edit.is_none() && st.doc.pattern_edit.is_none() && st.isolation.is_none(), "{mode}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shaper_lines_keep_free_angles_with_or_without_shift() {
|
||||
for shift in [false, true] {
|
||||
@@ -567,3 +611,162 @@ fn shaper_joined_outline_keeps_its_stroke_paint_after_source_edits() {
|
||||
assert_ne!(node(&s, id).appearance.stroke_paint().label(), "#00ff00");
|
||||
}
|
||||
}
|
||||
|
||||
fn source_ids(s: &Session, g: NodeId) -> Vec<NodeId> {
|
||||
node(s, g).children().unwrap()[0].children().unwrap().iter().map(|n| n.id).collect()
|
||||
}
|
||||
/// A Selection-tool drag from `from` to `to`.
|
||||
fn drag(s: &mut Session, v: ViewInfo, from: [f64; 2], to: [f64; 2]) {
|
||||
s.pointer(&PointerEvent::new(PointerKind::Down, from[0], from[1]), v).unwrap();
|
||||
for t in [0.25, 0.5, 1.0] {
|
||||
let p = [from[0] + (to[0] - from[0]) * t, from[1] + (to[1] - from[1]) * t];
|
||||
s.pointer(&PointerEvent::new(PointerKind::Drag, p[0], p[1]), v).unwrap();
|
||||
}
|
||||
s.pointer(&PointerEvent::new(PointerKind::Up, to[0], to[1]), v).unwrap();
|
||||
}
|
||||
/// Two overlapping rectangles in a Shaper Group, in Construction mode with the left one selected
|
||||
/// and the Selection tool chosen → (left, right, group).
|
||||
fn construction(s: &mut Session, v: ViewInfo) -> (NodeId, NodeId, NodeId) {
|
||||
let a = rectangle(s, 0.0, "#ff0000");
|
||||
let b = rectangle(s, 50.0, "#0000ff");
|
||||
let g = scribble(s, &[[25.0, 20.0], [75.0, 35.0], [30.0, 50.0], [125.0, 65.0]]);
|
||||
s.execute("object.isolate", &json!({"id": g.0})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [a.0]})).unwrap();
|
||||
s.select_tool("selection", v).unwrap();
|
||||
(a, b, g)
|
||||
}
|
||||
|
||||
/// In Construction mode, a Selection-tool drag that drops an original shape wholly outside the
|
||||
/// bounds of the group's original shapes takes it out of the group, in one undo step: it becomes a
|
||||
/// path just above the group with its own look, and stays selected. Undo puts it back. With
|
||||
/// Real-time Drawing off the drop does the same.
|
||||
#[test]
|
||||
fn shaper_dragging_an_original_shape_out_of_the_bounds_takes_it_out_of_the_group() {
|
||||
for real_time in [true, false] {
|
||||
let mut s = session();
|
||||
s.prefs.real_time_drawing = real_time;
|
||||
let v = ViewInfo { smart_guides: false, ..Default::default() };
|
||||
let (a, b, g) = construction(&mut s, v);
|
||||
let look = node(&s, a).appearance.clone();
|
||||
let steps = s.doc().unwrap().history.undo.len();
|
||||
drag(&mut s, v, [25.0, 50.0], [25.0, 200.0]);
|
||||
let st = s.doc().unwrap();
|
||||
assert_eq!(source_ids(&s, g), vec![b], "real-time {real_time}");
|
||||
let (parent, index, _) = st.doc.position(a).unwrap();
|
||||
assert_eq!((parent, index), { st.doc.position(g).map(|(p, i, _)| (p, i + 1)).unwrap() });
|
||||
assert_eq!(node(&s, a).appearance, look);
|
||||
assert_eq!(node(&s, a).geometric_bounds().unwrap().y0, 150.0);
|
||||
assert_eq!(st.selection.objects, vec![a]);
|
||||
assert_eq!(st.isolation, None);
|
||||
assert_eq!(st.history.undo.len(), steps + 1);
|
||||
assert_eq!(st.history.undo.last().unwrap().label, "Remove from Shaper Group");
|
||||
// The group's result is the right rectangle alone.
|
||||
assert_eq!(fill(&s, g, 25.0, 50.0), None);
|
||||
assert_eq!(fill(&s, g, 125.0, 50.0).as_deref(), Some("#0000ff"));
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(source_ids(&s, g), vec![a, b]);
|
||||
assert_eq!(node(&s, a).geometric_bounds().unwrap().y0, 0.0);
|
||||
assert_eq!(fill(&s, g, 25.0, 50.0).as_deref(), Some("#ff0000"));
|
||||
}
|
||||
}
|
||||
|
||||
/// A drop that leaves the original shape across the bounds of the group's original shapes only
|
||||
/// moves it, as before.
|
||||
#[test]
|
||||
fn shaper_dragging_an_original_shape_across_the_bounds_only_moves_it() {
|
||||
let mut s = session();
|
||||
let v = ViewInfo { smart_guides: false, ..Default::default() };
|
||||
let (a, b, g) = construction(&mut s, v);
|
||||
let sources = node(&s, g).children().unwrap()[0].id;
|
||||
drag(&mut s, v, [25.0, 50.0], [25.0, 100.0]);
|
||||
let st = s.doc().unwrap();
|
||||
assert_eq!(source_ids(&s, g), vec![a, b]);
|
||||
assert_eq!(node(&s, a).geometric_bounds().unwrap().y0, 50.0);
|
||||
assert_eq!(st.isolation, Some(sources));
|
||||
assert_eq!(st.history.undo.last().unwrap().label, "Move");
|
||||
// Outside Construction mode the same drop moves the group as usual.
|
||||
s.execute("object.exitIsolation", &json!({})).unwrap();
|
||||
drag(&mut s, v, [25.0, 100.0], [25.0, 400.0]);
|
||||
assert_eq!(source_ids(&s, g), vec![a, b]);
|
||||
}
|
||||
|
||||
/// Taking the last original shape out removes the group, and the native file keeps the result.
|
||||
#[test]
|
||||
fn shaper_taking_the_last_original_shape_out_removes_the_group_and_saves() {
|
||||
let mut s = session();
|
||||
let v = ViewInfo { smart_guides: false, ..Default::default() };
|
||||
let (a, b, g) = construction(&mut s, v);
|
||||
drag(&mut s, v, [25.0, 50.0], [25.0, 200.0]);
|
||||
s.execute("object.isolate", &json!({"id": g.0})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [b.0]})).unwrap();
|
||||
drag(&mut s, v, [125.0, 50.0], [400.0, 50.0]);
|
||||
let doc = &s.doc().unwrap().doc;
|
||||
assert!(doc.node(g).is_none());
|
||||
assert_eq!(doc.parent_of(a), doc.parent_of(b));
|
||||
assert_eq!(node(&s, b).geometric_bounds().unwrap().x0, 325.0);
|
||||
let back = vectorcraft_format::load(&vectorcraft_format::save(doc, false)).unwrap();
|
||||
assert_eq!(back.layers, doc.layers);
|
||||
}
|
||||
|
||||
/// Flat bounds of horizontal or vertical lines on the line of a group's flat bounds, and bounds that
|
||||
/// touch the group's bounds, are not outside them.
|
||||
#[test]
|
||||
fn shaper_outside_test_handles_flat_and_touching_bounds() {
|
||||
use crate::cmd::shaper::wholly_outside;
|
||||
use vectorcraft_geom::Rect;
|
||||
let row = Rect::new(0.0, 150.0, 200.0, 150.0);
|
||||
assert!(!wholly_outside(Rect::new(50.0, 150.0, 120.0, 150.0), row));
|
||||
assert!(!wholly_outside(Rect::new(150.0, 150.0, 300.0, 150.0), row));
|
||||
assert!(wholly_outside(Rect::new(250.0, 150.0, 300.0, 150.0), row));
|
||||
assert!(wholly_outside(Rect::new(50.0, 160.0, 120.0, 160.0), row));
|
||||
let column = Rect::new(80.0, 0.0, 80.0, 100.0);
|
||||
assert!(!wholly_outside(Rect::new(80.0, 50.0, 80.0, 150.0), column));
|
||||
assert!(wholly_outside(Rect::new(90.0, 50.0, 90.0, 150.0), column));
|
||||
let square = Rect::new(0.0, 0.0, 100.0, 100.0);
|
||||
assert!(!wholly_outside(Rect::new(100.0, 0.0, 200.0, 100.0), square));
|
||||
assert!(wholly_outside(Rect::new(100.5, 0.0, 200.0, 100.0), square));
|
||||
}
|
||||
|
||||
/// `shaper.releaseSource` takes the named original shape out, refuses a shape of another group or
|
||||
/// none, and `object.transform`'s `releaseOutside` makes one undo step outside a drag too.
|
||||
#[test]
|
||||
fn shaper_release_source_command_and_transform_flag() {
|
||||
let mut s = session();
|
||||
let a = rectangle(&mut s, 0.0, "#ff0000");
|
||||
let b = rectangle(&mut s, 50.0, "#0000ff");
|
||||
let g = scribble(&mut s, &[[25.0, 20.0], [75.0, 35.0], [30.0, 50.0], [125.0, 65.0]]);
|
||||
let other = rectangle(&mut s, 300.0, "#00ff00");
|
||||
let untouched = s.doc().unwrap().doc.layers.clone();
|
||||
for p in [json!({"source": other.0}), json!({"source": a.0, "group": other.0}), json!({"source": "x"}), json!({})] {
|
||||
assert!(s.execute("shaper.releaseSource", &p).is_err(), "{p}");
|
||||
assert_eq!(s.doc().unwrap().doc.layers, untouched);
|
||||
}
|
||||
s.execute("shaper.releaseSource", &json!({"source": b.0, "group": g.0})).unwrap();
|
||||
assert_eq!(source_ids(&s, g), vec![a]);
|
||||
assert_eq!(s.doc().unwrap().selection.objects, vec![b]);
|
||||
assert_eq!(s.doc().unwrap().history.undo.last().unwrap().label, "Remove from Shaper Group");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
let steps = s.doc().unwrap().history.undo.len();
|
||||
s.execute("object.transform", &json!({"matrix": [1, 0, 0, 1, 0, 150], "ids": [a.0], "releaseOutside": true})).unwrap();
|
||||
assert_eq!(source_ids(&s, g), vec![b]);
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), steps + 1);
|
||||
assert_eq!(s.doc().unwrap().history.undo.last().unwrap().label, "Remove from Shaper Group");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(source_ids(&s, g), vec![a, b]);
|
||||
assert_eq!(node(&s, a).geometric_bounds().unwrap().y0, 0.0);
|
||||
// With a history of one step, that step still goes back to before the move.
|
||||
s.doc_mut().unwrap().history.limit = 1;
|
||||
s.execute("object.transform", &json!({"matrix": [1, 0, 0, 1, 0, 150], "ids": [a.0], "releaseOutside": true})).unwrap();
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(source_ids(&s, g), vec![a, b]);
|
||||
assert_eq!(node(&s, a).geometric_bounds().unwrap().y0, 0.0);
|
||||
// Shapes taken out together keep their stacking order, whatever order they are named in.
|
||||
for ids in [[a.0, b.0], [b.0, a.0]] {
|
||||
s.execute("object.transform", &json!({"matrix": [1, 0, 0, 1, 0, 150], "ids": ids, "releaseOutside": true})).unwrap();
|
||||
let doc = &s.doc().unwrap().doc;
|
||||
assert!(doc.node(g).is_none());
|
||||
assert!(doc.position(a).unwrap().1 < doc.position(b).unwrap().1, "{ids:?}");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(source_ids(&s, g), vec![a, b]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,163 @@
|
||||
//! Object › Path › Smooth (`object.path.smooth`): the slider from minimum to maximum smoothing and
|
||||
//! Auto-Smooth, as the Smooth slider help page describes
|
||||
//! (https://helpx.adobe.com/illustrator/desktop/draw-shapes-and-paths/modify-paths/refine-path-segments-with-the-smooth-slider.html).
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_doc::NodeKind;
|
||||
use vectorcraft_geom::kurbo::ParamCurveNearest;
|
||||
use vectorcraft_geom::{ParamCurve, PathData, Point};
|
||||
|
||||
use crate::*;
|
||||
|
||||
fn session() -> Session {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 800, "height": 600})).unwrap();
|
||||
s
|
||||
}
|
||||
|
||||
fn path_of(s: &Session, id: NodeId) -> PathData {
|
||||
match &s.doc().unwrap().doc.node(id).unwrap().kind {
|
||||
NodeKind::Path { path, .. } => path.clone(),
|
||||
_ => panic!("not a path"),
|
||||
}
|
||||
}
|
||||
|
||||
/// A path through `points`, selected: its id.
|
||||
fn polyline(s: &mut Session, points: &[(f64, f64)], closed: bool) -> NodeId {
|
||||
let anchors: Vec<Value> = points.iter().map(|&(x, y)| json!({"x": x, "y": y})).collect();
|
||||
NodeId(s.execute("path.create", &json!({"anchors": anchors, "closed": closed})).unwrap()["id"].as_u64().unwrap())
|
||||
}
|
||||
|
||||
/// A sine wave 300 pt long with an anchor every 3 pt, its anchors 1.5 pt above and below it in turn.
|
||||
fn jagged_wave(s: &mut Session) -> NodeId {
|
||||
let pts: Vec<(f64, f64)> = (0..=100u32)
|
||||
.map(|i| (f64::from(i) * 3.0, 200.0 + 40.0 * (f64::from(i) * 3.0 / 50.0).sin() + if i.is_multiple_of(2) { 1.5 } else { -1.5 }))
|
||||
.collect();
|
||||
polyline(s, &pts, false)
|
||||
}
|
||||
|
||||
/// A closed ring of radius `r` around (`cx`, 300) with 120 anchors, `jag` pt in and out in turn.
|
||||
fn jagged_ring(s: &mut Session, cx: f64, r: f64, jag: f64) -> NodeId {
|
||||
let pts: Vec<(f64, f64)> = (0..120u32)
|
||||
.map(|i| {
|
||||
let a = f64::from(i) * std::f64::consts::TAU / 120.0;
|
||||
let rr = r + if i.is_multiple_of(2) { jag } else { -jag };
|
||||
(cx + rr * a.cos(), 300.0 + rr * a.sin())
|
||||
})
|
||||
.collect();
|
||||
polyline(s, &pts, true)
|
||||
}
|
||||
|
||||
/// The farthest that 11 points on each segment of `from` lie from the nearest point of `to`.
|
||||
fn farthest(from: &PathData, to: &PathData) -> f64 {
|
||||
let off = |p: Point| {
|
||||
to.subpaths
|
||||
.iter()
|
||||
.flat_map(|sp| (0..sp.segment_count()).map(move |i| sp.segment(i).nearest(p, 1e-9).distance_sq.sqrt()))
|
||||
.fold(f64::INFINITY, f64::min)
|
||||
};
|
||||
from.subpaths
|
||||
.iter()
|
||||
.flat_map(|sp| (0..sp.segment_count()).flat_map(move |i| (0..=10).map(move |k| sp.segment(i).eval(f64::from(k) / 10.0))))
|
||||
.map(off)
|
||||
.fold(0.0, f64::max)
|
||||
}
|
||||
|
||||
fn undo_len(s: &Session) -> usize {
|
||||
s.doc().unwrap().history.undo.len()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minimum_smoothing_leaves_the_path_where_it_was() {
|
||||
let mut s = session();
|
||||
let id = jagged_wave(&mut s);
|
||||
let before = path_of(&s, id);
|
||||
let out = s.execute("object.path.smooth", &json!({"level": 0})).unwrap();
|
||||
assert_eq!(out["level"], json!(0.0));
|
||||
let after = path_of(&s, id);
|
||||
// 0.1 % of the longer side (300 pt), both ways.
|
||||
assert!(farthest(&before, &after) <= 0.3 + 1e-6 && farthest(&after, &before) <= 0.3 + 1e-6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn more_smoothing_removes_the_jags_and_keeps_the_shape() {
|
||||
let mut s = session();
|
||||
let id = jagged_wave(&mut s);
|
||||
let before = path_of(&s, id);
|
||||
let mut counts = vec![];
|
||||
for level in [25, 50, 100] {
|
||||
let out = s.execute("object.path.smooth", &json!({ "level": level })).unwrap();
|
||||
let after = path_of(&s, id);
|
||||
assert_eq!(out["after"].as_u64().unwrap() as usize, after.anchor_count());
|
||||
counts.push(after.anchor_count());
|
||||
// Within the tolerance (2.5 % of 300 pt at most) plus the jag the refit smooths away.
|
||||
assert!(farthest(&before, &after) <= 7.5 + 1.5, "level {level}: {}", farthest(&before, &after));
|
||||
// The end points of an open path stay put.
|
||||
let (a, b) = (&before.subpaths[0].anchors, &after.subpaths[0].anchors);
|
||||
assert_eq!((a[0].p, a[a.len() - 1].p), (b[0].p, b[b.len() - 1].p));
|
||||
assert!(!after.subpaths[0].closed);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
}
|
||||
assert!(counts[0] < 101 / 2 && counts[2] <= 10, "{counts:?}");
|
||||
assert!(counts.windows(2).all(|w| w[0] >= w[1]), "{counts:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn auto_smooth_uses_the_middle_level() {
|
||||
let mut s = session();
|
||||
let id = jagged_wave(&mut s);
|
||||
let out = s.execute("object.path.smooth", &json!({"auto": true, "level": 0})).unwrap();
|
||||
assert_eq!(out["level"], json!(50.0));
|
||||
let auto = path_of(&s, id);
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
s.execute("object.path.smooth", &json!({"level": 50})).unwrap();
|
||||
assert_eq!(path_of(&s, id), auto);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn closed_paths_and_compound_path_members_smooth_in_one_undo_step() {
|
||||
let mut s = session();
|
||||
let outer = jagged_ring(&mut s, 300.0, 100.0, 2.0);
|
||||
let inner = jagged_ring(&mut s, 300.0, 40.0, 1.0);
|
||||
s.execute("select.set", &json!({"ids": [outer.0, inner.0]})).unwrap();
|
||||
s.execute("object.compoundPath.make", &json!({})).unwrap();
|
||||
let (o, i) = (path_of(&s, outer), path_of(&s, inner));
|
||||
let steps = undo_len(&s);
|
||||
let out = s.execute("object.path.smooth", &json!({"level": 100})).unwrap();
|
||||
assert_eq!(out["ids"].as_array().unwrap().len(), 2);
|
||||
assert_eq!(undo_len(&s), steps + 1);
|
||||
for id in [outer, inner] {
|
||||
let now = path_of(&s, id);
|
||||
assert!(now.subpaths[0].closed);
|
||||
assert!(now.anchor_count() < 120 / 4, "{}", now.anchor_count());
|
||||
}
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!((path_of(&s, outer), path_of(&s, inner)), (o, i));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn smooths_the_objects_given() {
|
||||
let mut s = session();
|
||||
let a = jagged_wave(&mut s);
|
||||
let b = jagged_ring(&mut s, 300.0, 50.0, 1.0);
|
||||
let ring = path_of(&s, b);
|
||||
// `b` is selected; `ids` names `a` instead.
|
||||
let out = s.execute("object.path.smooth", &json!({"ids": [a.0], "level": 100})).unwrap();
|
||||
assert_eq!(out["ids"], json!([a.0]));
|
||||
assert_eq!(path_of(&s, b), ring);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bad_params_are_refused_and_change_nothing() {
|
||||
let mut s = session();
|
||||
let id = jagged_wave(&mut s);
|
||||
let before = path_of(&s, id);
|
||||
let steps = undo_len(&s);
|
||||
for p in [json!({"level": "50"}), json!({"auto": true, "level": "max"}), json!({"auto": "yes"}), json!({"ids": "all"}), json!({"ids": [999_999]})]
|
||||
{
|
||||
assert!(s.execute("object.path.smooth", &p).is_err(), "{p}");
|
||||
}
|
||||
assert_eq!((path_of(&s, id), undo_len(&s)), (before, steps));
|
||||
// Levels past the ends count as the ends.
|
||||
assert_eq!(s.execute("object.path.smooth", &json!({"level": 250})).unwrap()["level"], json!(100.0));
|
||||
}
|
||||
@@ -47,6 +47,151 @@ fn document_color_mode_converts_grouped_swatches() {
|
||||
assert!(matches!(doc(&s).swatch("Bright Red").unwrap().paint.color(), Some(Color::Cmyk { .. })));
|
||||
}
|
||||
|
||||
/// The first solid fill color in the subtree of `n`.
|
||||
fn first_fill(n: &vectorcraft_doc::Node) -> Color {
|
||||
let mut out = None;
|
||||
n.walk(&mut |m| {
|
||||
if out.is_none() {
|
||||
out = m.appearance.fill_paint().color();
|
||||
}
|
||||
});
|
||||
out.unwrap_or_else(|| panic!("no fill in {n:?}"))
|
||||
}
|
||||
|
||||
/// The fill of graph design "Bar", the fill of brush "Holder"'s art and its Hue Shift key color,
|
||||
/// the fill of graphic style "Red" and the fill of the art of `masked`'s opacity mask.
|
||||
fn colors_outside_the_layers(s: &Session, masked: NodeId) -> Vec<Color> {
|
||||
let d = doc(s);
|
||||
let brush = vectorcraft_brush::find(d, "Holder").unwrap();
|
||||
let vectorcraft_brush::Colorization::HueShift { key } = brush.kind.colorization() else { panic!("not Hue Shift") };
|
||||
vec![
|
||||
first_fill(&d.graph_designs.iter().find(|g| g.name == "Bar").unwrap().art),
|
||||
first_fill(brush.art()[0]),
|
||||
key,
|
||||
d.graphic_style("Red").unwrap().appearance.fill_paint().color().unwrap(),
|
||||
first_fill(&d.node(masked).unwrap().extras().mask.as_deref().unwrap().art),
|
||||
]
|
||||
}
|
||||
|
||||
/// Document Color Mode converts the colors of graph designs, of the art of brushes (with a Hue
|
||||
/// Shift brush's key color), of graphic styles and of opacity-mask art as it converts the
|
||||
/// layers' art, tints of global swatches included, in one undo step.
|
||||
#[test]
|
||||
fn document_color_mode_converts_art_outside_the_layers() {
|
||||
let mut s = session();
|
||||
run(&mut s, "swatch.new", json!({"name": "Tone", "color": "#ff0000", "global": true}));
|
||||
let hue_shift = json!({"colorization": {"method": "hueShift"}});
|
||||
for (cmd, p, paint) in [
|
||||
("graph.design", json!({"new": "Bar"}), json!({"swatch": "Tone", "tint": 40})),
|
||||
("brush.new", json!({"type": "art", "name": "Holder", "params": hue_shift}), json!({"color": "#ff0000"})),
|
||||
("graphicStyle.new", json!({"name": "Red"}), json!({"color": "#ff0000"})),
|
||||
] {
|
||||
let id = rect(&mut s);
|
||||
let mut paint = paint;
|
||||
paint["ids"] = json!([id.0]);
|
||||
run(&mut s, "paint.setFill", paint);
|
||||
run(&mut s, "paint.setStroke", json!({"none": true, "ids": [id.0]}));
|
||||
run(&mut s, "select.set", json!({"ids": [id.0]}));
|
||||
run(&mut s, cmd, p);
|
||||
run(&mut s, "edit.clear", json!({"ids": [id.0]}));
|
||||
}
|
||||
let masked = rect(&mut s);
|
||||
run(&mut s, "paint.setFill", json!({"color": "#ff0000", "ids": [masked.0]}));
|
||||
let m = rect(&mut s);
|
||||
run(&mut s, "paint.setFill", json!({"color": "#808080", "ids": [m.0]}));
|
||||
run(&mut s, "select.set", json!({"ids": [masked.0, m.0]}));
|
||||
run(&mut s, "transparency.makeOpacityMask", json!({"clip": true, "invert": false}));
|
||||
let before = colors_outside_the_layers(&s, masked);
|
||||
assert!(before.iter().all(|c| matches!(c, Color::Rgb { .. })), "{before:?}");
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
|
||||
run(&mut s, "file.documentColorMode", json!({"mode": "cmyk"}));
|
||||
let after = colors_outside_the_layers(&s, masked);
|
||||
assert!(after.iter().all(|c| matches!(c, Color::Cmyk { .. })), "{after:?}");
|
||||
// The same red converts to the same inks wherever it is.
|
||||
let red = first_fill(doc(&s).node(masked).unwrap());
|
||||
assert_eq!(after[1..4], [red; 3]);
|
||||
// A tint of a global swatch is the converted swatch's color at that tint.
|
||||
let tone = doc(&s).swatch("Tone").unwrap().paint.color().unwrap();
|
||||
assert_eq!(after[0], tone.tinted(0.4));
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo + 1);
|
||||
run(&mut s, "edit.undo", json!({}));
|
||||
assert_eq!(colors_outside_the_layers(&s, masked), before);
|
||||
}
|
||||
|
||||
/// A Hue Shift brush whose art is painted with a tint of a global swatch keeps its key color equal
|
||||
/// to that art color after Document Color Mode.
|
||||
#[test]
|
||||
fn document_color_mode_keeps_a_hue_shift_key_on_its_tinted_art() {
|
||||
let mut s = session();
|
||||
run(&mut s, "swatch.new", json!({"name": "Tone", "color": "#ff0000", "global": true}));
|
||||
let id = rect(&mut s);
|
||||
run(&mut s, "paint.setFill", json!({"swatch": "Tone", "tint": 40, "ids": [id.0]}));
|
||||
run(&mut s, "paint.setStroke", json!({"none": true, "ids": [id.0]}));
|
||||
run(&mut s, "select.set", json!({"ids": [id.0]}));
|
||||
run(&mut s, "brush.new", json!({"type": "art", "name": "Holder", "params": {"colorization": {"method": "hueShift"}}}));
|
||||
run(&mut s, "edit.clear", json!({"ids": [id.0]}));
|
||||
let key_and_art = |s: &Session| {
|
||||
let brush = vectorcraft_brush::find(doc(s), "Holder").unwrap();
|
||||
let vectorcraft_brush::Colorization::HueShift { key } = brush.kind.colorization() else { panic!("not Hue Shift") };
|
||||
(key, first_fill(brush.art()[0]))
|
||||
};
|
||||
let (key, art) = key_and_art(&s);
|
||||
assert_eq!(key, art);
|
||||
run(&mut s, "file.documentColorMode", json!({"mode": "cmyk"}));
|
||||
let (key, art) = key_and_art(&s);
|
||||
assert!(matches!(art, Color::Cmyk { .. }), "{art:?}");
|
||||
assert_eq!(key, art);
|
||||
}
|
||||
|
||||
/// The reply's `changed` counts the colors of the document's art; the colors of brush art and
|
||||
/// graphic styles convert without being counted.
|
||||
#[test]
|
||||
fn document_color_mode_counts_the_colors_of_the_art_only() {
|
||||
let changed = |library: bool| {
|
||||
let mut s = session();
|
||||
if library {
|
||||
for (cmd, p) in [("brush.new", json!({"type": "art", "name": "Holder"})), ("graphicStyle.new", json!({"name": "Red"}))] {
|
||||
let id = rect(&mut s);
|
||||
run(&mut s, "paint.setFill", json!({"color": "#ff0000", "ids": [id.0]}));
|
||||
run(&mut s, "select.set", json!({"ids": [id.0]}));
|
||||
run(&mut s, cmd, p);
|
||||
run(&mut s, "edit.clear", json!({"ids": [id.0]}));
|
||||
}
|
||||
}
|
||||
let id = rect(&mut s);
|
||||
run(&mut s, "paint.setFill", json!({"color": "#ff0000", "ids": [id.0]}));
|
||||
let r = run(&mut s, "file.documentColorMode", json!({"mode": "cmyk"}));
|
||||
if library {
|
||||
let brush = vectorcraft_brush::find(doc(&s), "Holder").unwrap();
|
||||
assert!(matches!(first_fill(brush.art()[0]), Color::Cmyk { .. }));
|
||||
}
|
||||
r["changed"].as_u64().unwrap()
|
||||
};
|
||||
let in_art = changed(false);
|
||||
assert!(in_art > 0);
|
||||
assert_eq!(changed(true), in_art);
|
||||
}
|
||||
|
||||
/// Document Color Mode chosen while a pattern is being edited also converts the tile that pattern
|
||||
/// editing's Cancel puts back.
|
||||
#[test]
|
||||
fn document_color_mode_converts_the_tile_that_pattern_cancel_puts_back() {
|
||||
let mut s = session();
|
||||
let id = rect(&mut s);
|
||||
run(&mut s, "paint.setFill", json!({"color": "#ff0000", "ids": [id.0]}));
|
||||
run(&mut s, "paint.setStroke", json!({"none": true, "ids": [id.0]}));
|
||||
run(&mut s, "select.set", json!({"ids": [id.0]}));
|
||||
let pattern = run(&mut s, "object.pattern.make", json!({"edit": false}))["name"].as_str().unwrap().to_string();
|
||||
run(&mut s, "object.pattern.edit", json!({"name": pattern}));
|
||||
run(&mut s, "file.documentColorMode", json!({"mode": "cmyk"}));
|
||||
run(&mut s, "object.pattern.cancel", json!({}));
|
||||
let red = first_fill(doc(&s).node(id).unwrap());
|
||||
assert!(matches!(red, Color::Cmyk { .. }), "{red:?}");
|
||||
let tile: Vec<Color> = doc(&s).pattern(&pattern).unwrap().art.iter().map(|n| first_fill(n)).collect();
|
||||
assert_eq!(tile, [red]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn grouped_spot_swatches_print_on_their_own_plate() {
|
||||
let mut s = session();
|
||||
@@ -383,3 +528,203 @@ fn global_swatch_edits_reach_opacity_mask_art() {
|
||||
run(&mut s, "edit.undo", json!({}));
|
||||
assert_eq!(mask_fill(&s).color(), Some(Color::rgb(1.0, 1.0, 1.0)));
|
||||
}
|
||||
|
||||
/// The fills of graph design "Bar" and of the art of brush "Holder", as `place: color, link`.
|
||||
fn fills_outside_the_layers(s: &Session) -> Vec<String> {
|
||||
let d = doc(s);
|
||||
let mut out = vec![];
|
||||
let mut tree = |place: &str, n: &vectorcraft_doc::Node| {
|
||||
n.walk(&mut |m| {
|
||||
if let Paint::Solid { color, swatch, .. } = m.appearance.fill_paint() {
|
||||
out.push(format!("{place}: {}, {}", color.to_hex(), swatch.as_deref().unwrap_or("-")));
|
||||
}
|
||||
})
|
||||
};
|
||||
tree("design", &d.graph_designs.iter().find(|g| g.name == "Bar").unwrap().art);
|
||||
vectorcraft_brush::find(d, "Holder").unwrap().art().into_iter().for_each(|n| tree("brush art", n));
|
||||
out
|
||||
}
|
||||
|
||||
/// A global swatch edit that recolors a Hue Shift brush's key color in its art moves the key to the
|
||||
/// new color, so strokes still paint that art with the stroke color.
|
||||
#[test]
|
||||
fn global_swatch_edits_move_a_hue_shift_key() {
|
||||
let mut s = session();
|
||||
run(&mut s, "swatch.new", json!({"name": "Tone", "color": "#ff0000", "global": true}));
|
||||
let id = rect(&mut s);
|
||||
run(&mut s, "paint.setFill", json!({"swatch": "Tone", "ids": [id.0]}));
|
||||
run(&mut s, "paint.setStroke", json!({"none": true, "ids": [id.0]}));
|
||||
run(&mut s, "brush.new", json!({"type": "art", "name": "Key", "ids": [id.0], "params": {"colorization": {"method": "hueShift"}}}));
|
||||
let key = |s: &mut Session| run(s, "brush.get", json!({"name": "Key"}))["colorization"]["key"].clone();
|
||||
let (red, blue) = (json!(Color::rgb(1.0, 0.0, 0.0)), json!(Color::rgb(0.0, 0.0, 1.0)));
|
||||
assert_eq!(key(&mut s), red);
|
||||
run(&mut s, "swatch.edit", json!({"name": "Tone", "color": "#0000ff"}));
|
||||
assert_eq!(key(&mut s), blue);
|
||||
run(&mut s, "edit.undo", json!({}));
|
||||
assert_eq!(key(&mut s), red);
|
||||
|
||||
// A paint in the key color that only an opacity mask uses leaves the key where it is.
|
||||
run(&mut s, "swatch.new", json!({"name": "MaskTone", "color": "#ff0000", "global": true}));
|
||||
let obj = rect(&mut s);
|
||||
run(&mut s, "paint.setFill", json!({"color": "#ff0000", "ids": [obj.0]}));
|
||||
run(&mut s, "paint.setStroke", json!({"none": true, "ids": [obj.0]}));
|
||||
let m = rect(&mut s);
|
||||
run(&mut s, "paint.setFill", json!({"swatch": "MaskTone", "ids": [m.0]}));
|
||||
run(&mut s, "select.set", json!({"ids": [obj.0, m.0]}));
|
||||
run(&mut s, "transparency.makeOpacityMask", json!({"clip": true, "invert": false}));
|
||||
run(&mut s, "brush.new", json!({"type": "art", "name": "Masked", "ids": [obj.0], "params": {"colorization": {"method": "hueShift"}}}));
|
||||
let masked = |s: &mut Session| run(s, "brush.get", json!({"name": "Masked"}))["colorization"]["key"].clone();
|
||||
assert_eq!(masked(&mut s), red);
|
||||
run(&mut s, "swatch.edit", json!({"name": "MaskTone", "color": "#0000ff"}));
|
||||
assert_eq!(masked(&mut s), red);
|
||||
|
||||
// With the art and its mask on one swatch, the key takes the art's new color, not the mask's.
|
||||
run(&mut s, "swatch.new", json!({"name": "Both", "color": "#ffffff", "global": true}));
|
||||
let obj = rect(&mut s);
|
||||
run(&mut s, "paint.setFill", json!({"swatch": "Both", "ids": [obj.0]}));
|
||||
run(&mut s, "paint.setStroke", json!({"none": true, "ids": [obj.0]}));
|
||||
let m = rect(&mut s);
|
||||
run(&mut s, "paint.setFill", json!({"swatch": "Both", "tint": 50, "ids": [m.0]}));
|
||||
run(&mut s, "select.set", json!({"ids": [obj.0, m.0]}));
|
||||
run(&mut s, "transparency.makeOpacityMask", json!({"clip": true, "invert": false}));
|
||||
run(&mut s, "brush.new", json!({"type": "art", "name": "Both", "ids": [obj.0], "params": {"colorization": {"method": "hueShift"}}}));
|
||||
run(&mut s, "swatch.edit", json!({"name": "Both", "color": "#0000ff"}));
|
||||
assert_eq!(run(&mut s, "brush.get", json!({"name": "Both"}))["colorization"]["key"], blue);
|
||||
}
|
||||
|
||||
/// A global swatch edit that recolors brush art keeps the stored library entries that do not parse
|
||||
/// as brushes.
|
||||
#[test]
|
||||
fn global_swatch_edits_keep_unreadable_brush_entries() {
|
||||
let mut s = session();
|
||||
run(&mut s, "swatch.new", json!({"name": "Tone", "color": "#ff0000", "global": true}));
|
||||
let id = rect(&mut s);
|
||||
run(&mut s, "paint.setFill", json!({"swatch": "Tone", "ids": [id.0]}));
|
||||
run(&mut s, "brush.new", json!({"type": "art", "name": "Holder", "ids": [id.0]}));
|
||||
let junk = json!({"name": "Unreadable", "kind": 7});
|
||||
let doc = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc);
|
||||
let Some(Value::Array(entries)) = doc.unknown.get_mut(vectorcraft_brush::DOC_KEY) else { panic!("no stored brushes") };
|
||||
entries.push(junk.clone());
|
||||
run(&mut s, "swatch.edit", json!({"name": "Tone", "color": "#0000ff"}));
|
||||
let stored = &s.doc().unwrap().doc.unknown[vectorcraft_brush::DOC_KEY];
|
||||
assert!(stored.as_array().is_some_and(|a| a.contains(&junk)), "{stored}");
|
||||
assert!(vectorcraft_brush::find(&s.doc().unwrap().doc, "Holder").is_some());
|
||||
}
|
||||
|
||||
/// A global swatch edit that recolors brush art keeps the fields of stored brush entries that the
|
||||
/// brush definition does not read, both on the brush it recolors and on the brushes it leaves alone,
|
||||
/// and inside the art too. A swatch delete that unlinks the art drops the link and keeps those fields.
|
||||
#[test]
|
||||
fn global_swatch_edits_keep_unread_brush_fields() {
|
||||
// Adds `"extra": 8` to every object under `v` that links a swatch.
|
||||
fn tag_links(v: &mut Value) {
|
||||
match v {
|
||||
Value::Object(o) => {
|
||||
if o.contains_key("swatch") {
|
||||
o.insert("extra".into(), json!(8));
|
||||
}
|
||||
o.values_mut().for_each(tag_links);
|
||||
}
|
||||
Value::Array(a) => a.iter_mut().for_each(tag_links),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
// The objects under `v` that carry `"extra": 8`.
|
||||
fn tagged(v: &Value) -> Vec<&Value> {
|
||||
match v {
|
||||
Value::Object(o) => o.get("extra").filter(|e| **e == json!(8)).map(|_| v).into_iter().chain(o.values().flat_map(tagged)).collect(),
|
||||
Value::Array(a) => a.iter().flat_map(tagged).collect(),
|
||||
_ => vec![],
|
||||
}
|
||||
}
|
||||
let mut s = session();
|
||||
run(&mut s, "swatch.new", json!({"name": "Tone", "color": "#ff0000", "global": true}));
|
||||
let id = rect(&mut s);
|
||||
run(&mut s, "paint.setFill", json!({"swatch": "Tone", "ids": [id.0]}));
|
||||
run(&mut s, "brush.new", json!({"type": "art", "name": "Holder", "ids": [id.0]}));
|
||||
let doc = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc);
|
||||
let Some(Value::Array(entries)) = doc.unknown.get_mut(vectorcraft_brush::DOC_KEY) else { panic!("no stored brushes") };
|
||||
for e in entries.iter_mut() {
|
||||
if let Some(o) = e.as_object_mut() {
|
||||
o.insert("extra".into(), json!(7));
|
||||
}
|
||||
tag_links(e);
|
||||
}
|
||||
let links = tagged(&doc.unknown[vectorcraft_brush::DOC_KEY]).len();
|
||||
assert!(links > 0, "{}", doc.unknown[vectorcraft_brush::DOC_KEY]);
|
||||
run(&mut s, "swatch.edit", json!({"name": "Tone", "color": "#0000ff"}));
|
||||
let stored = &s.doc().unwrap().doc.unknown[vectorcraft_brush::DOC_KEY];
|
||||
let entries = stored.as_array().map(Vec::as_slice).unwrap_or_default();
|
||||
assert!(entries.len() > 1, "{stored}");
|
||||
assert!(entries.iter().all(|e| e["extra"] == json!(7)), "{stored}");
|
||||
assert_eq!(tagged(stored).len(), links, "{stored}");
|
||||
let holder = vectorcraft_brush::find(&s.doc().unwrap().doc, "Holder").unwrap();
|
||||
let fill = holder.art().first().map(|n| n.appearance.fill_paint().color().map(|c| c.to_hex()));
|
||||
assert_eq!(fill, Some(Some("#0000ff".to_string())));
|
||||
|
||||
run(&mut s, "swatch.delete", json!({"name": "Tone"}));
|
||||
let stored = &s.doc().unwrap().doc.unknown[vectorcraft_brush::DOC_KEY];
|
||||
let unlinked = tagged(stored);
|
||||
assert_eq!(unlinked.len(), links, "{stored}");
|
||||
assert!(unlinked.iter().all(|o| o.get("swatch").is_none()), "{stored}");
|
||||
let holder = vectorcraft_brush::find(&s.doc().unwrap().doc, "Holder").unwrap();
|
||||
let fill = holder.art().first().map(|n| n.appearance.fill_paint().color().map(|c| c.to_hex()));
|
||||
assert_eq!(fill, Some(Some("#0000ff".to_string())));
|
||||
}
|
||||
|
||||
/// A global swatch edit, rename, delete and Default Swatches with replace change the art of graph
|
||||
/// designs and brushes: the art follows the swatch's color and name, or keeps its color unlinked,
|
||||
/// each in one undo step.
|
||||
#[test]
|
||||
fn global_swatch_edits_reach_graph_designs_and_brush_art() {
|
||||
let mut s = session();
|
||||
run(&mut s, "swatch.new", json!({"name": "Tone", "color": "#ff0000", "global": true}));
|
||||
for (cmd, p) in [("graph.design", json!({"new": "Bar"})), ("brush.new", json!({"type": "art", "name": "Holder"}))] {
|
||||
let id = rect(&mut s);
|
||||
run(&mut s, "paint.setFill", json!({"swatch": "Tone", "ids": [id.0]}));
|
||||
run(&mut s, "select.set", json!({"ids": [id.0]}));
|
||||
run(&mut s, cmd, p);
|
||||
run(&mut s, "edit.clear", json!({"ids": [id.0]}));
|
||||
}
|
||||
let both = |fill: &str| vec![format!("design: {fill}"), format!("brush art: {fill}")];
|
||||
assert_eq!(fills_outside_the_layers(&s), both("#ff0000, Tone"));
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
|
||||
let r = run(&mut s, "swatch.edit", json!({"name": "Tone", "color": "#0000ff"}));
|
||||
assert_eq!(r["relinked"], 2, "{r}");
|
||||
assert_eq!(fills_outside_the_layers(&s), both("#0000ff, Tone"));
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo + 1);
|
||||
run(&mut s, "edit.undo", json!({}));
|
||||
assert_eq!(fills_outside_the_layers(&s), both("#ff0000, Tone"));
|
||||
|
||||
run(&mut s, "swatch.edit", json!({"name": "Tone", "newName": "Hue"}));
|
||||
assert_eq!(fills_outside_the_layers(&s), both("#ff0000, Hue"));
|
||||
// Default Swatches with replace drops the links to the swatches it removes.
|
||||
run(&mut s, "swatch.resetDefaults", json!({"replace": true}));
|
||||
assert_eq!(fills_outside_the_layers(&s), both("#ff0000, -"));
|
||||
run(&mut s, "edit.undo", json!({}));
|
||||
let r = run(&mut s, "swatch.delete", json!({"name": "Hue"}));
|
||||
assert_eq!(r["unlinked"], 2, "{r}");
|
||||
assert_eq!(fills_outside_the_layers(&s), both("#ff0000, -"));
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo + 2);
|
||||
}
|
||||
|
||||
/// A global swatch edited while a pattern is being edited also changes the tile that pattern
|
||||
/// editing's Cancel puts back.
|
||||
#[test]
|
||||
fn global_swatch_edits_reach_the_tile_that_pattern_cancel_puts_back() {
|
||||
let mut s = session();
|
||||
run(&mut s, "swatch.new", json!({"name": "Tone", "color": "#ff0000", "global": true}));
|
||||
let id = rect(&mut s);
|
||||
run(&mut s, "paint.setFill", json!({"swatch": "Tone", "ids": [id.0]}));
|
||||
run(&mut s, "select.set", json!({"ids": [id.0]}));
|
||||
let pattern = run(&mut s, "object.pattern.make", json!({"edit": false}))["name"].as_str().unwrap().to_string();
|
||||
run(&mut s, "object.pattern.edit", json!({"name": pattern}));
|
||||
run(&mut s, "swatch.edit", json!({"name": "Tone", "color": "#0000ff"}));
|
||||
run(&mut s, "object.pattern.cancel", json!({}));
|
||||
let mut fills = vec![];
|
||||
for n in &doc(&s).pattern(&pattern).unwrap().art {
|
||||
n.walk(&mut |m| fills.push(m.appearance.fill_paint()));
|
||||
}
|
||||
assert_eq!(fills, [Paint::Solid { color: Color::rgb(0.0, 0.0, 1.0), swatch: Some("Tone".into()), tint: 1.0 }]);
|
||||
}
|
||||
|
||||
@@ -994,6 +994,41 @@ fn a_frame_dragged_with_the_type_tools_is_the_dragged_rectangle() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Through the session, a click with Shift held and the Type tool chosen makes vertical type and
|
||||
/// edits it with the Type tool still chosen, and the pointer with Shift held is the vertical
|
||||
/// I-beam. A click with Alt held and the Type on a Path tool chosen turns a rectangle into area
|
||||
/// type.
|
||||
#[test]
|
||||
fn shift_and_alt_switch_the_type_tools_through_the_session() {
|
||||
let mut s = session();
|
||||
let v = ViewInfo::default();
|
||||
let (shift, alt) = (Mods { shift: true, ..Mods::default() }, Mods { alt: true, ..Mods::default() });
|
||||
s.select_tool("type", v).unwrap();
|
||||
assert_eq!(s.cursor(Point::new(400.0, 300.0), shift, v), vectorcraft_tools::Cursor::TypeVertical);
|
||||
assert_eq!(s.cursor(Point::new(400.0, 300.0), Mods::default(), v), vectorcraft_tools::Cursor::Text);
|
||||
for k in [PointerKind::Down, PointerKind::Up] {
|
||||
s.pointer(&PointerEvent::new(k, 400.0, 300.0).with_mods(shift), v).unwrap();
|
||||
}
|
||||
assert_eq!(s.tool_id(), "type");
|
||||
let id = s.doc().unwrap().selection.objects[0];
|
||||
assert!(obj(&s, id).vertical);
|
||||
assert!(s.tool_wants_text());
|
||||
s.tool_text("縦", v).unwrap();
|
||||
assert_eq!(obj(&s, id).plain_text(), "縦");
|
||||
key(&mut s, ToolKey::Escape, Mods::default());
|
||||
|
||||
let rect = NodeId(s.execute("shape.rectangle", &json!({"x": 100, "y": 100, "width": 200, "height": 100})).unwrap()["id"].as_u64().unwrap());
|
||||
s.select_tool("typeOnPath", v).unwrap();
|
||||
for k in [PointerKind::Down, PointerKind::Up] {
|
||||
s.pointer(&PointerEvent::new(k, 100.0, 150.0).with_mods(alt), v).unwrap();
|
||||
}
|
||||
assert_eq!(s.tool_id(), "typeOnPath");
|
||||
assert!(s.doc().unwrap().doc.node(rect).is_none(), "the rectangle became the frame");
|
||||
let id = s.doc().unwrap().selection.objects[0];
|
||||
assert!(matches!(obj(&s, id).kind, TextKind::Area { .. }));
|
||||
assert!(!obj(&s, id).vertical);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn area_type_from_a_circle_keeps_glyphs_inside() {
|
||||
let mut s = session();
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
//! Edit › Undo and Redo while the Type tool edits: the caret goes to where the step changed the text.
|
||||
|
||||
use serde_json::json;
|
||||
use vectorcraft_doc::{NodeId, NodeKind};
|
||||
use vectorcraft_tools::{Mods, PointerEvent, PointerKind, ToolKey};
|
||||
|
||||
use crate::*;
|
||||
|
||||
fn session() -> Session {
|
||||
let mut s = Session::new();
|
||||
s.prefs.placeholder_text = false;
|
||||
s.execute("file.new", &json!({"width": 800, "height": 600})).unwrap();
|
||||
s
|
||||
}
|
||||
|
||||
fn plain(s: &Session, id: NodeId) -> String {
|
||||
match &s.doc().unwrap().doc.node(id).unwrap().kind {
|
||||
NodeKind::Text(t) => t.plain_text(),
|
||||
_ => panic!("not text"),
|
||||
}
|
||||
}
|
||||
|
||||
fn caret(s: &Session) -> (u64, u64) {
|
||||
let o = s.tool_options();
|
||||
(o["caret"].as_u64().unwrap(), o["anchor"].as_u64().unwrap())
|
||||
}
|
||||
|
||||
/// "abc" typed before "XYZ": Undo puts the caret before "XYZ" and Redo after "abc", from the
|
||||
/// menu command and from the History panel's steps alike (both run `edit.undo` and `edit.redo`).
|
||||
#[test]
|
||||
fn undo_and_redo_put_the_caret_where_the_step_changed_the_text() {
|
||||
let v = ViewInfo::default();
|
||||
let mut s = session();
|
||||
let r = s.execute("text.create", &json!({"x": 100, "y": 100, "text": "XYZ", "size": 20})).unwrap();
|
||||
let id = NodeId(r["id"].as_u64().unwrap());
|
||||
s.select_tool("type", v).unwrap();
|
||||
for k in [PointerKind::Down, PointerKind::Up] {
|
||||
s.pointer(&PointerEvent::new(k, 101.0, 95.0), v).unwrap();
|
||||
}
|
||||
s.set_tool_option("select", &json!({"start": 0, "end": 0}));
|
||||
s.tool_text("abc", v).unwrap();
|
||||
assert_eq!((plain(&s, id).as_str(), caret(&s)), ("abcXYZ", (3, 3)));
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!((plain(&s, id).as_str(), caret(&s)), ("XYZ", (0, 0)));
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
assert_eq!((plain(&s, id).as_str(), caret(&s)), ("abcXYZ", (3, 3)));
|
||||
// A deleted character comes back with the caret after it, wherever the caret went since.
|
||||
s.tool_key(ToolKey::End, Mods::default(), v).unwrap();
|
||||
s.tool_key(ToolKey::Backspace, Mods::default(), v).unwrap();
|
||||
assert_eq!(plain(&s, id), "abcXY");
|
||||
s.tool_key(ToolKey::Home, Mods::default(), v).unwrap();
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!((plain(&s, id).as_str(), caret(&s)), ("abcXYZ", (6, 6)));
|
||||
}
|
||||
|
||||
/// Undo and Redo run as steps of `command.batch` put the caret where they do run alone.
|
||||
#[test]
|
||||
fn undo_and_redo_in_a_batch_put_the_caret_where_the_step_changed_the_text() {
|
||||
let v = ViewInfo::default();
|
||||
let mut s = session();
|
||||
let r = s.execute("text.create", &json!({"x": 100, "y": 100, "text": "XYZ", "size": 20})).unwrap();
|
||||
let id = NodeId(r["id"].as_u64().unwrap());
|
||||
s.select_tool("type", v).unwrap();
|
||||
for k in [PointerKind::Down, PointerKind::Up] {
|
||||
s.pointer(&PointerEvent::new(k, 101.0, 95.0), v).unwrap();
|
||||
}
|
||||
s.set_tool_option("select", &json!({"start": 0, "end": 0}));
|
||||
s.tool_text("abc", v).unwrap();
|
||||
s.execute("command.batch", &json!({"commands": [{"command": "edit.undo"}]})).unwrap();
|
||||
assert_eq!((plain(&s, id).as_str(), caret(&s)), ("XYZ", (0, 0)));
|
||||
s.execute("command.batch", &json!({"commands": [{"command": "edit.redo"}]})).unwrap();
|
||||
assert_eq!((plain(&s, id).as_str(), caret(&s)), ("abcXYZ", (3, 3)));
|
||||
// A batch whose later step fails is rolled back: the text and the caret are as they were, and
|
||||
// Undo still takes back the typing.
|
||||
let failed = json!({"commands": [{"command": "edit.undo"}, {"command": "no.suchCommand"}]});
|
||||
assert!(s.execute("command.batch", &failed).is_err());
|
||||
assert_eq!((plain(&s, id).as_str(), caret(&s)), ("abcXYZ", (3, 3)));
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!((plain(&s, id).as_str(), caret(&s)), ("XYZ", (0, 0)));
|
||||
}
|
||||
Reference in New Issue
Block a user