mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-18 01:36:57 +00:00
Merge main into fix/fileinfo-control-characters-message
This commit is contained in:
@@ -301,6 +301,42 @@ fn an_export_preset_sets_the_shared_settings() {
|
||||
assert_eq!(run(&mut s, "document.exportSettings", json!({}))["settings"]["prefix"], "p_");
|
||||
}
|
||||
|
||||
/// `openLocation` (Open Location after Export) is remembered in the document's export settings,
|
||||
/// outside presets; null removes it, and only true or false is taken.
|
||||
#[test]
|
||||
fn open_location_is_remembered_in_the_export_settings() {
|
||||
let mut s = session();
|
||||
let open = |s: &mut Session| run(s, "document.exportSettings", json!({}))["settings"]["openLocation"].clone();
|
||||
let r = run(&mut s, "assets.settings.set", json!({"openLocation": false}));
|
||||
assert_eq!(open(&mut s), false);
|
||||
assert!(r["settings"].get("openLocation").is_none(), "{r}");
|
||||
// Create Sub-folders, on for a document without settings, stays on.
|
||||
assert_eq!(r["settings"]["subfolders"], true, "{r}");
|
||||
run(&mut s, "document.exportPresets.save", json!({"name": "Kept", "settings": {"formats": [{"format": "svg"}]}}));
|
||||
run(&mut s, "assets.settings.set", json!({"exportPreset": "Kept"}));
|
||||
assert_eq!(open(&mut s), false, "a preset leaves it as it is");
|
||||
run(&mut s, "assets.settings.set", json!({"openLocation": null}));
|
||||
assert_eq!(open(&mut s), Value::Null);
|
||||
for bad in [json!({"openLocation": "yes"}), json!({"openLocation": 1}), json!({"subfolders": "no"})] {
|
||||
assert!(s.execute("assets.settings.set", &bad).is_err(), "{bad}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Create Sub-folders turned off on its own ends a remembered folder per format or item, so Export
|
||||
/// for Screens opens with it off too; given with an export preset, the preset's folders stay.
|
||||
#[test]
|
||||
fn create_sub_folders_off_alone_ends_a_folder_per_format_or_item() {
|
||||
let mut s = session();
|
||||
let folders = |s: &mut Session| run(s, "document.exportSettings", json!({}))["settings"]["folders"].clone();
|
||||
run(&mut s, "document.exportPresets.save", json!({"name": "Items", "settings": {"formats": [{"format": "svg"}], "folders": "item"}}));
|
||||
for preset in ["Logo Package", "Items"] {
|
||||
run(&mut s, "assets.settings.set", json!({"exportPreset": preset, "subfolders": false}));
|
||||
assert_ne!(folders(&mut s), "none", "{preset}");
|
||||
run(&mut s, "assets.settings.set", json!({"subfolders": false}));
|
||||
assert_eq!(folders(&mut s), "none", "{preset}");
|
||||
}
|
||||
}
|
||||
|
||||
/// An export preset applied to the shared settings keeps its folders, with Create Sub-folders on
|
||||
/// for a folder per row; with Create Sub-folders off every file goes into the folder itself,
|
||||
/// mobile and density rows too.
|
||||
|
||||
@@ -3018,6 +3018,76 @@ fn live_paint_expand_keeps_the_faces_and_edges_that_draw() {
|
||||
assert_eq!(rows(&n, lower_stroke), rows(&before, lower_stroke));
|
||||
}
|
||||
|
||||
/// `livePaint.info` lists every fill and stroke row of each face and edge, bottom first, and says
|
||||
/// which rows draw by Expand's rule, while `fill`, `stroke` and `width` stay the top rows.
|
||||
#[test]
|
||||
fn live_paint_info_lists_every_appearance_row() {
|
||||
use vectorcraft_doc::{FillLayer, StrokeLayer};
|
||||
let fill = |p: Paint| AppearanceItem::Fill(FillLayer::new(p));
|
||||
let stroke = |p: Paint, w: f64| AppearanceItem::Stroke(StrokeLayer::new(p, w));
|
||||
let red = || Paint::solid(Color::rgb(1.0, 0.0, 0.0));
|
||||
let blue = || Paint::solid(Color::rgb(0.0, 0.0, 1.0));
|
||||
let green = || Paint::solid(Color::rgb(0.0, 1.0, 0.0));
|
||||
let hidden = |mut i: AppearanceItem| {
|
||||
match &mut i {
|
||||
AppearanceItem::Fill(f) => f.visible = false,
|
||||
AppearanceItem::Stroke(st) => st.visible = false,
|
||||
}
|
||||
i
|
||||
};
|
||||
let faded = |mut i: AppearanceItem, o: f32| {
|
||||
match &mut i {
|
||||
AppearanceItem::Fill(f) => f.opacity = o,
|
||||
AppearanceItem::Stroke(st) => st.opacity = o,
|
||||
}
|
||||
i
|
||||
};
|
||||
let mut s = session();
|
||||
let g = live(&mut s);
|
||||
// A drawn lower fill and a half-opaque stroke under an unpainted top fill.
|
||||
let lower = set_live_part(&mut s, g, Some((25.0, 50.0)), None, vec![fill(red()), faded(stroke(blue(), 2.0), 0.5), fill(Paint::None)], 1.0);
|
||||
let quiet = set_live_part(&mut s, g, Some((125.0, 50.0)), None, vec![hidden(fill(red())), faded(fill(red()), 0.0)], 1.0);
|
||||
let bare = set_live_part(&mut s, g, Some((75.0, 50.0)), None, vec![], 1.0);
|
||||
// Two strokes with a fill between them.
|
||||
let two = set_live_part(&mut s, g, None, Some((0.0, 50.0)), vec![stroke(red(), 4.0), fill(green()), stroke(blue(), 2.0)], 1.0);
|
||||
let thin = set_live_part(&mut s, g, None, Some((100.0, 50.0)), vec![stroke(red(), 0.0)], 1.0);
|
||||
let clear_edge = set_live_part(&mut s, g, None, Some((75.0, 0.0)), vec![stroke(red(), 2.0)], 0.0);
|
||||
let hidden_edge = set_live_part(&mut s, g, None, Some((150.0, 50.0)), vec![stroke(red(), 2.0)], 1.0);
|
||||
s.edit("t", |d, _| {
|
||||
d.node_mut(hidden_edge).unwrap().visible = false;
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
let parts = [lower, quiet, bare, two, thin, clear_edge, hidden_edge];
|
||||
assert_eq!(parts.iter().collect::<std::collections::HashSet<_>>().len(), parts.len());
|
||||
|
||||
let info = s.execute("livePaint.info", &json!({"group": g.0})).unwrap();
|
||||
let part = |key: &str, id: NodeId| info[key].as_array().unwrap().iter().find(|p| p["id"] == id.0).unwrap().clone();
|
||||
let fill_row = |i: usize, p: Paint, o: f64, v: bool, drawn: bool| json!({"index": i, "kind": "fill", "paint": p.label(), "opacity": o, "visible": v, "drawn": drawn});
|
||||
let stroke_row = |i: usize, p: Paint, o: f64, v: bool, w: f64, drawn: bool| json!({"index": i, "kind": "stroke", "paint": p.label(), "opacity": o, "visible": v, "width": w, "drawn": drawn});
|
||||
|
||||
let f = part("faces", lower);
|
||||
assert_eq!(f["fill"], json!(Paint::None.label()), "fill stays the top fill");
|
||||
assert!(f["bounds"].is_array());
|
||||
assert_eq!(
|
||||
f["appearance"],
|
||||
json!([fill_row(0, red(), 1.0, true, true), stroke_row(1, blue(), 0.5, true, 2.0, true), fill_row(2, Paint::None, 1.0, true, false)])
|
||||
);
|
||||
assert_eq!(part("faces", quiet)["appearance"], json!([fill_row(0, red(), 1.0, false, false), fill_row(1, red(), 0.0, true, false)]));
|
||||
assert_eq!(part("faces", bare)["appearance"], json!([]));
|
||||
|
||||
let e = part("edges", two);
|
||||
assert_eq!((e["stroke"].clone(), e["width"].clone()), (json!(blue().label()), json!(2.0)), "stroke and width stay the top stroke's");
|
||||
assert_eq!(
|
||||
e["appearance"],
|
||||
json!([stroke_row(0, red(), 1.0, true, 4.0, true), fill_row(1, green(), 1.0, true, true), stroke_row(2, blue(), 1.0, true, 2.0, true)])
|
||||
);
|
||||
assert_eq!(part("edges", thin)["appearance"], json!([stroke_row(0, red(), 1.0, true, 0.0, false)]));
|
||||
for id in [clear_edge, hidden_edge] {
|
||||
assert_eq!(part("edges", id)["appearance"], json!([stroke_row(0, red(), 1.0, true, 2.0, false)]), "a part that is not shown draws no row");
|
||||
}
|
||||
}
|
||||
|
||||
/// Merge carries a face's whole appearance when one of its rows draws, and carries nothing from a
|
||||
/// face that draws nothing.
|
||||
#[test]
|
||||
|
||||
@@ -1034,14 +1034,39 @@ fn in_place_pastes_put_a_copy_on_each_artboard_named() {
|
||||
// `artboard` wins over `artboards`.
|
||||
let ids = ids_of(&s.execute(cmd, &json!({"artboard": 2, "artboards": [0, 3]})).unwrap());
|
||||
assert_eq!(doc(&s).bounds_of(&ids, false).unwrap().origin(), Point::new(boards[2].x0 + 10.0, boards[2].y0 + 5.0), "{cmd}");
|
||||
// A list that is empty, names an artboard the document lacks or names one twice, or isn't a
|
||||
// list: an error, and nothing pasted.
|
||||
let before = doc(&s).clone();
|
||||
for bad in [json!([]), json!([0, 4]), json!([1, 1]), json!([0, -1]), json!(2), json!("0,1")] {
|
||||
assert!(s.execute(cmd, &json!({"artboards": bad})).is_err(), "{cmd} with artboards {bad}");
|
||||
// A list that is empty, names an artboard the document lacks or names one twice, holds a
|
||||
// value that is not a whole number, or isn't a list, and an `artboard` that is not a whole
|
||||
// number or not an artboard: an error naming the fault, and nothing pasted. Both keys are
|
||||
// checked when both are given.
|
||||
let (before, undo) = (doc(&s).clone(), s.doc().unwrap().history.undo.len());
|
||||
let (not_list, missing, not_whole) = (
|
||||
"`artboards` must be a list of artboard indices",
|
||||
"`artboards` names an artboard the document does not have",
|
||||
"`artboards` must contain whole numbers",
|
||||
);
|
||||
let cases = [
|
||||
(json!({"artboards": []}), not_list),
|
||||
(json!({"artboards": 2}), not_list),
|
||||
(json!({"artboards": "0,1"}), not_list),
|
||||
(json!({"artboards": [0, 4]}), missing),
|
||||
(json!({"artboards": [1, 1]}), "`artboards` names an artboard twice"),
|
||||
(json!({"artboards": [0, -1]}), not_whole),
|
||||
(json!({"artboards": [0, 1.5]}), not_whole),
|
||||
(json!({"artboards": [4, "1"]}), not_whole),
|
||||
(json!({"artboard": 1, "artboards": [7]}), missing),
|
||||
(json!({"artboard": 1, "artboards": ["1"]}), not_whole),
|
||||
(json!({"artboard": 4}), "`artboard` must be the index of an artboard of the document"),
|
||||
(json!({"artboard": -1}), "`artboard` must be a whole number"),
|
||||
(json!({"artboard": 1.5}), "`artboard` must be a whole number"),
|
||||
(json!({"artboard": "1"}), "`artboard` must be a whole number"),
|
||||
(json!({"artboard": null}), "`artboard` must be a whole number"),
|
||||
(json!({"artboard": "1", "artboards": [9]}), "`artboard` must be a whole number"),
|
||||
];
|
||||
for (p, want) in cases {
|
||||
let e = s.execute(cmd, &p).unwrap_err().to_string();
|
||||
assert_eq!(e, format!("invalid parameters for `{cmd}`: {want}"), "{cmd} {p}");
|
||||
}
|
||||
assert!(s.execute(cmd, &json!({"artboard": 1, "artboards": [7]})).is_err(), "{cmd}: a bad list with `artboard`");
|
||||
assert_eq!(doc(&s), &before, "{cmd}");
|
||||
assert_eq!((doc(&s), s.doc().unwrap().history.undo.len()), (&before, undo), "{cmd}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2023,6 +2023,260 @@ fn smoothing_leaves_paths_out_of_reach_and_straight_segments_alone() {
|
||||
assert_eq!(path(&s, line), before);
|
||||
}
|
||||
|
||||
/// The turn, in degrees, of `sp` at each of its anchors: round a closed path, and 0 at an open
|
||||
/// path's first and last anchors.
|
||||
fn smooth_anchor_turns(sp: &vectorcraft_geom::SubPath) -> Vec<f64> {
|
||||
let (n, segs) = (sp.anchors.len(), sp.segment_count());
|
||||
(0..n)
|
||||
.map(|i| {
|
||||
if segs == 0 || !sp.closed && (i == 0 || i == n - 1) {
|
||||
return 0.0;
|
||||
}
|
||||
let (a, b) = (sp.segment((i + segs - 1) % segs), sp.segment(i % segs));
|
||||
let tin = [a.p3 - a.p2, a.p3 - a.p1, a.p3 - a.p0].into_iter().find(|d| d.hypot() > 1e-9).unwrap_or_default();
|
||||
let tout = [b.p1 - b.p0, b.p2 - b.p0, b.p3 - b.p0].into_iter().find(|d| d.hypot() > 1e-9).unwrap_or_default();
|
||||
tin.cross(tout).atan2(tin.dot(tout)).abs().to_degrees()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The turn of `sp` at its anchor on `at`, `None` when no anchor is there.
|
||||
fn smooth_turn_at(sp: &vectorcraft_geom::SubPath, at: Point) -> Option<f64> {
|
||||
let i = sp.anchors.iter().position(|a| a.p.distance(at) < 1e-9)?;
|
||||
smooth_anchor_turns(sp).get(i).copied()
|
||||
}
|
||||
|
||||
/// An open polyline with a corner at (300, 300) whose angle inside is `inside` degrees: a 100 pt
|
||||
/// leg along y = 300 from the left, then a 100 pt leg turning by 180 - `inside` degrees, each leg cut
|
||||
/// into `per_leg` segments. Returns the path, the corner, and a drag along the path from 40 pt
|
||||
/// before the corner to 40 pt after it.
|
||||
fn smooth_corner_path(s: &mut Session, inside: f64, per_leg: usize) -> (NodeId, Point, Vec<Point>) {
|
||||
let v = Point::new(300.0, 300.0);
|
||||
let d = kurbo::Vec2::from_angle(-(180.0 - inside).to_radians());
|
||||
let back = kurbo::Vec2::new(-100.0, 0.0);
|
||||
let legs = per_leg as f64;
|
||||
let mut pts: Vec<Point> = (0..per_leg).map(|k| v + back * (1.0 - k as f64 / legs)).collect();
|
||||
pts.extend((0..=per_leg).map(|k| v + d * (100.0 * k as f64 / legs)));
|
||||
let anchors: Vec<_> = pts.iter().map(|p| json!({"x": p.x, "y": p.y})).collect();
|
||||
let id = smooth_test_path(s, &anchors, false);
|
||||
let mut drag: Vec<Point> = (0..8).map(|k| v - kurbo::Vec2::new(40.0 - 5.0 * f64::from(k), 0.0)).collect();
|
||||
drag.extend((0..=8).map(|k| v + d * (5.0 * f64::from(k))));
|
||||
(id, v, drag)
|
||||
}
|
||||
|
||||
/// A Smooth drag over a corner keeps it where the path turns there by more than 57.5° (an angle
|
||||
/// inside the corner under 122.5°), whether each leg is one segment or ten and at any Fidelity. A
|
||||
/// wider corner is not kept: when the drag changes the path, no anchor is left turning as sharply
|
||||
/// there, and a leg of ten segments at the default Fidelity always rounds it.
|
||||
#[test]
|
||||
fn the_smooth_tool_keeps_corners_sharper_than_its_threshold() {
|
||||
let mut wrong = vec![];
|
||||
for inside in [30.0f64, 60.0, 90.0, 120.0, 122.4, 122.6, 125.0, 135.0, 150.0, 170.0] {
|
||||
let kept = inside < 122.5;
|
||||
for per_leg in [1, 10] {
|
||||
for fidelity in [0.5, 2.5, 10.0] {
|
||||
let mut s = session();
|
||||
let (id, v, drag) = smooth_corner_path(&mut s, inside, per_leg);
|
||||
let before = path(&s, id);
|
||||
let pts: Vec<_> = drag.iter().map(|p| json!([p.x, p.y])).collect();
|
||||
s.execute("path.smoothRegion", &json!({"points": pts, "radius": 12, "fidelity": fidelity})).unwrap();
|
||||
let p = path(&s, id);
|
||||
let turn = 180.0 - inside;
|
||||
let at = smooth_turn_at(&p.subpaths[0], v);
|
||||
let changed = p != before;
|
||||
let rounded = changed && at.is_none_or(|t| t < turn - 1.0);
|
||||
let right = if kept { at.is_some_and(|t| (t - turn).abs() < 0.01) } else { rounded || !changed && per_leg == 1 };
|
||||
let most = smooth_anchor_turns(&p.subpaths[0]).into_iter().fold(0.0, f64::max);
|
||||
if !right || !kept && most > 57.5 || !kept && per_leg == 10 && fidelity == 2.5 && !rounded {
|
||||
wrong.push(format!(
|
||||
"{inside}° inside, {per_leg} segments a leg, Fidelity {fidelity}: turn {at:?}, changed {changed}, sharpest {most}°"
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(wrong.is_empty(), "{wrong:#?}");
|
||||
}
|
||||
|
||||
/// Three Smooth drags over a 90° and a 120° corner leave the corner where it was and as sharp, while
|
||||
/// the first one smooths a wobble on the same leg.
|
||||
#[test]
|
||||
fn smooth_drags_again_and_again_keep_a_corner_in_place() {
|
||||
for inside in [90.0f64, 120.0] {
|
||||
let mut s = session();
|
||||
let v = Point::new(300.0, 300.0);
|
||||
let d = kurbo::Vec2::from_angle(-(180.0 - inside).to_radians());
|
||||
let mut anchors: Vec<_> = (0..=18).map(|k| json!({"x": 180.0 + 5.0 * f64::from(k), "y": if k % 2 == 1 { 301.5 } else { 300.0 }})).collect();
|
||||
anchors.push(json!({"x": v.x, "y": v.y}));
|
||||
let c = v + d * 100.0;
|
||||
anchors.push(json!({"x": c.x, "y": c.y}));
|
||||
let id = smooth_test_path(&mut s, &anchors, false);
|
||||
let mut drag = smooth_drag_along(300.0, 175.0, 300.0);
|
||||
drag.extend((1..=8).map(|k| v + d * (5.0 * f64::from(k))));
|
||||
let mut before = path(&s, id);
|
||||
for pass in 1..=3 {
|
||||
smooth_pass(&mut s, &drag);
|
||||
let p = path(&s, id);
|
||||
let at = smooth_turn_at(&p.subpaths[0], v);
|
||||
assert!(at.is_some_and(|t| (t - (180.0 - inside)).abs() < 0.01), "{inside}°, pass {pass}: {at:?}");
|
||||
if pass == 1 {
|
||||
assert!(p.anchor_count() < before.anchor_count(), "{inside}°: the wobble is smoothed: {} anchors", p.anchor_count());
|
||||
assert!(total_turning(&p) < total_turning(&before), "{inside}°: the path turns less");
|
||||
}
|
||||
before = p;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A Smooth drag along a line with 1 pt jags, each turning by 90°, smooths them all: none is kept
|
||||
/// as a corner, since the line hardly turns over the tool's reach.
|
||||
#[test]
|
||||
fn smoothing_keeps_no_small_jag_as_a_corner() {
|
||||
let mut s = session();
|
||||
let anchors: Vec<_> = (0..=200).map(|i| json!({"x": 100.0 + f64::from(i), "y": if i % 2 == 1 { 201.0 } else { 200.0 }})).collect();
|
||||
let id = smooth_test_path(&mut s, &anchors, false);
|
||||
assert!(smooth_anchor_turns(&path(&s, id).subpaths[0]).iter().skip(1).take(199).all(|t| (t - 90.0).abs() < 1e-6));
|
||||
smooth_pass(&mut s, &smooth_drag_along(200.5, 90.0, 310.0));
|
||||
let p = path(&s, id);
|
||||
let sp = &p.subpaths[0];
|
||||
assert!(sp.anchors.len() < 50, "the jags are smoothed: {} anchors", sp.anchors.len());
|
||||
let most = smooth_anchor_turns(sp).into_iter().fold(0.0, f64::max);
|
||||
assert!(most <= 57.5, "a jag is kept, turning {most}°");
|
||||
}
|
||||
|
||||
/// A closed path with 90° corners at (200, 200), where it starts, and at (400, 200), 75° ones at
|
||||
/// (400, 400) and (200, 400), a 30° bend at (300, 426.79), and a top side jagged by 1 pt. With
|
||||
/// `from_bend` the path starts at the bend instead.
|
||||
fn smooth_house(s: &mut Session, from_bend: bool) -> (NodeId, [Point; 4], Point) {
|
||||
let corners = [Point::new(200.0, 200.0), Point::new(400.0, 200.0), Point::new(400.0, 400.0), Point::new(200.0, 400.0)];
|
||||
let bend = Point::new(300.0, 400.0 + 100.0 / 75f64.to_radians().tan());
|
||||
let mut anchors = vec![json!({"x": 200.0, "y": 200.0})];
|
||||
anchors.extend((1..40).map(|k| json!({"x": 200.0 + 5.0 * f64::from(k), "y": if k % 2 == 1 { 201.0 } else { 200.0 }})));
|
||||
for p in [corners[1], corners[2], bend, corners[3]] {
|
||||
anchors.push(json!({"x": p.x, "y": p.y}));
|
||||
}
|
||||
if from_bend {
|
||||
let at_bend = anchors.len() - 2;
|
||||
anchors.rotate_left(at_bend);
|
||||
}
|
||||
(smooth_test_path(s, &anchors, true), corners, bend)
|
||||
}
|
||||
|
||||
/// A Smooth drag round the whole of a closed path, and one along its top side only, keep its corners,
|
||||
/// the one where the path starts included, and smooth the jags; the path still starts where it did,
|
||||
/// at a corner or at the bend, which the refit keeps, and the bend out of the second drag's reach
|
||||
/// stays as it was.
|
||||
#[test]
|
||||
fn smoothing_a_closed_path_keeps_its_corners() {
|
||||
let turns: Vec<f64> = {
|
||||
let mut s = session();
|
||||
let (id, corners, _) = smooth_house(&mut s, false);
|
||||
corners.iter().map(|c| smooth_turn_at(&path(&s, id).subpaths[0], *c).unwrap()).collect()
|
||||
};
|
||||
for from_bend in [false, true] {
|
||||
let mut s = session();
|
||||
let (id, corners, bend) = smooth_house(&mut s, from_bend);
|
||||
let before = path(&s, id);
|
||||
let mut ring: Vec<Point> = vec![];
|
||||
for (a, b) in [(corners[0], corners[1]), (corners[1], corners[2]), (corners[2], bend), (bend, corners[3]), (corners[3], corners[0])] {
|
||||
let n = (a.distance(b) / 5.0).ceil();
|
||||
ring.extend((0..n as usize).map(|k| a.lerp(b, k as f64 / n)));
|
||||
}
|
||||
ring.push(corners[0]);
|
||||
smooth_pass(&mut s, &ring);
|
||||
let p = path(&s, id);
|
||||
let sp = &p.subpaths[0];
|
||||
assert!(sp.closed && sp.anchors.len() < before.subpaths[0].anchors.len(), "{} anchors", sp.anchors.len());
|
||||
assert_eq!(sp.anchors[0].p, before.subpaths[0].anchors[0].p, "the path still starts where it did");
|
||||
for (c, t) in corners.iter().zip(&turns) {
|
||||
let at = smooth_turn_at(sp, *c);
|
||||
assert!(at.is_some_and(|a| (a - t).abs() < 0.01), "the corner at {c:?} turning {t}° is kept: {at:?}");
|
||||
}
|
||||
}
|
||||
|
||||
let mut s = session();
|
||||
let (id, corners, bend) = smooth_house(&mut s, false);
|
||||
let before = path(&s, id);
|
||||
smooth_pass(&mut s, &smooth_drag_along(200.0, 190.0, 410.0));
|
||||
let p = path(&s, id);
|
||||
let sp = &p.subpaths[0];
|
||||
assert!(sp.closed && sp.anchors.len() < before.subpaths[0].anchors.len(), "the top is smoothed: {} anchors", sp.anchors.len());
|
||||
for (c, t) in corners.iter().zip(&turns) {
|
||||
let at = smooth_turn_at(sp, *c);
|
||||
assert!(at.is_some_and(|a| (a - t).abs() < 0.01), "part of the path: the corner at {c:?} turning {t}° is kept: {at:?}");
|
||||
}
|
||||
assert!(smooth_turn_at(sp, bend).is_some_and(|t| (t - 30.0).abs() < 0.01), "the bend out of reach stays");
|
||||
}
|
||||
|
||||
/// A Smooth drag over an anchor whose curved legs meet at a corner keeps the corner, taking the turn
|
||||
/// from the directions the curves arrive and leave along, a handle of no length included.
|
||||
#[test]
|
||||
fn smoothing_keeps_a_corner_between_curves() {
|
||||
use kurbo::ParamCurve;
|
||||
let v = Point::new(200.0, 300.0);
|
||||
let with_in = json!({"x": 200.0, "y": 300.0, "in": [170.0, 270.0], "out": [230.0, 270.0]});
|
||||
let no_in = json!({"x": 200.0, "y": 300.0, "out": [230.0, 270.0]});
|
||||
for (corner, first_out) in [(with_in, [130.0, 260.0]), (no_in, [170.0, 230.0])] {
|
||||
let mut s = session();
|
||||
let anchors = [json!({"x": 100.0, "y": 300.0, "out": first_out}), corner, json!({"x": 300.0, "y": 300.0, "in": [270.0, 260.0]})];
|
||||
let id = smooth_test_path(&mut s, &anchors, false);
|
||||
let before = path(&s, id);
|
||||
let turn = smooth_turn_at(&before.subpaths[0], v).unwrap();
|
||||
assert!(turn > 80.0, "{turn}");
|
||||
let (a, b) = (before.subpaths[0].segment(0), before.subpaths[0].segment(1));
|
||||
let mut drag: Vec<Point> = (0..=10).map(|k| a.eval(0.6 + 0.04 * f64::from(k))).collect();
|
||||
drag.extend((1..=10).map(|k| b.eval(0.04 * f64::from(k))));
|
||||
smooth_pass(&mut s, &drag);
|
||||
let at = smooth_turn_at(&path(&s, id).subpaths[0], v);
|
||||
assert!(at.is_some_and(|t| (t - turn).abs() < 0.01), "the corner turning {turn}° is kept: {at:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A corner at the first anchor of the stretch a drag smooths, just out of its reach, keeps its
|
||||
/// place and its turn.
|
||||
#[test]
|
||||
fn a_corner_at_the_end_of_the_smoothed_stretch_stays() {
|
||||
let mut s = session();
|
||||
let mut anchors = vec![json!({"x": 100.0, "y": 300.0}), json!({"x": 200.0, "y": 300.0})];
|
||||
anchors.extend((1..=20).map(|k| json!({"x": if k % 2 == 1 { 201.0 } else { 200.0 }, "y": 300.0 - 5.0 * f64::from(k)})));
|
||||
let id = smooth_test_path(&mut s, &anchors, false);
|
||||
let before = path(&s, id);
|
||||
let turn = smooth_turn_at(&before.subpaths[0], Point::new(200.0, 300.0)).unwrap();
|
||||
let drag: Vec<Point> = (0..=19).map(|k| Point::new(200.0, 287.0 - 5.0 * f64::from(k))).collect();
|
||||
smooth_pass(&mut s, &drag);
|
||||
let p = path(&s, id);
|
||||
let sp = &p.subpaths[0];
|
||||
assert_ne!(p, before, "the jagged leg is smoothed");
|
||||
assert_eq!(sp.anchors[0], before.subpaths[0].anchors[0]);
|
||||
let at = smooth_turn_at(sp, Point::new(200.0, 300.0));
|
||||
assert!(at.is_some_and(|t| (t - turn).abs() < 0.01), "the corner turning {turn}° stays: {at:?}");
|
||||
}
|
||||
|
||||
/// A Smooth drag over corners it keeps, with nothing else to smooth, changes nothing and adds no
|
||||
/// undo step: an open path's corner and a square's four corners.
|
||||
#[test]
|
||||
fn a_smooth_drag_that_only_meets_corners_adds_no_undo_step() {
|
||||
let mut s = session();
|
||||
let (id, v, drag) = smooth_corner_path(&mut s, 90.0, 1);
|
||||
let before = path(&s, id);
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
assert_eq!(smooth_pass_ids(&mut s, &drag), json!([]), "a corner at {v:?}");
|
||||
assert_eq!(path(&s, id), before);
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo);
|
||||
|
||||
let square = [(100.0, 100.0), (200.0, 100.0), (200.0, 200.0), (100.0, 200.0)].map(|(x, y)| json!({"x": x, "y": y}));
|
||||
let id = smooth_test_path(&mut s, &square, true);
|
||||
let before = path(&s, id);
|
||||
let undo = s.doc().unwrap().history.undo.len();
|
||||
let mut ring = smooth_drag_along(100.0, 100.0, 200.0);
|
||||
ring.extend((1..=20).map(|k| Point::new(200.0, 100.0 + 5.0 * f64::from(k))));
|
||||
ring.extend(smooth_drag_along(200.0, 100.0, 200.0).into_iter().rev().skip(1));
|
||||
ring.extend((1..=20).map(|k| Point::new(100.0, 200.0 - 5.0 * f64::from(k))));
|
||||
assert_eq!(smooth_pass_ids(&mut s, &ring), json!([]), "a square");
|
||||
assert_eq!(path(&s, id), before);
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), undo);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn join_tool_joins_two_lines() {
|
||||
let mut s = session();
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_doc::{Node, NodeKind};
|
||||
use vectorcraft_geom::{Affine, PathData, Rect};
|
||||
use vectorcraft_geom::{Affine, PathData, Point, Rect};
|
||||
|
||||
use crate::*;
|
||||
|
||||
@@ -320,16 +320,16 @@ fn global_edit_needs_one_supported_object() {
|
||||
let t = NodeId(s.execute("text.create", &json!({"x": 50, "y": 500, "text": "Hi"})).unwrap()["id"].as_u64().unwrap());
|
||||
sel(&mut s, &[t]);
|
||||
assert!(matches!(s.execute("select.globalEdit", &json!({})), Err(EngineError::Disabled(..))), "type");
|
||||
let lone = rect(&mut s, 0.0, 500.0, 30.0, 70.0);
|
||||
// A copy turned a quarter turn (70 × 30) is not taken: rotated copies are not matched, and the
|
||||
// message says so.
|
||||
let turned = rect(&mut s, 200.0, 500.0, 30.0, 70.0);
|
||||
sel(&mut s, &[turned]);
|
||||
s.execute("object.rotate", &json!({"angle": 90})).unwrap();
|
||||
// A reflected copy is not taken, and the message says so.
|
||||
let lone = kite(&mut s, 0.0, 500.0, 1.0);
|
||||
let mirrored = kite(&mut s, 200.0, 500.0, 1.0);
|
||||
sel(&mut s, &[mirrored]);
|
||||
s.execute("object.reflect", &json!({"axis": "vertical"})).unwrap();
|
||||
sel(&mut s, &[lone]);
|
||||
let e = s.execute("select.globalEdit", &json!({})).unwrap_err().to_string();
|
||||
assert!(e.contains("no similar objects") && e.contains("rotated"), "{e}");
|
||||
assert!(e.contains("no similar objects") && e.contains("reflected copies are not matched") && !e.contains("rotated"), "{e}");
|
||||
assert!(s.doc().unwrap().global_edit.is_none());
|
||||
assert!(crate::cmd::globaledit::highlights(&s).is_empty(), "and it is not highlighted");
|
||||
// A group holding type is not offered, though another such group exists.
|
||||
let group = |s: &mut Session, x: f64| {
|
||||
let p = rect(s, x, 100.0, 10.0, 10.0);
|
||||
@@ -851,3 +851,159 @@ fn groups_nested_as_deep_as_a_document_allows_are_edited_together() {
|
||||
s.execute("paint.setFill", &json!({"color": "#12ab34"})).unwrap();
|
||||
assert_eq!(fill(&s, other_inner), "#12ab34");
|
||||
}
|
||||
|
||||
/// A four-sided path with no symmetry, `k` times the size, its box's top left near (`x`, `y`): a
|
||||
/// 30 × 70 rectangle whose first anchor is moved.
|
||||
fn kite(s: &mut Session, x: f64, y: f64, k: f64) -> NodeId {
|
||||
let id = rect(s, x, y, 30.0 * k, 70.0 * k);
|
||||
s.execute("select.anchors", &json!({"id": id.0, "anchors": [[0, 0]]})).unwrap();
|
||||
s.execute("path.moveAnchors", &json!({"dx": 10.0 * k, "dy": -20.0 * k})).unwrap();
|
||||
id
|
||||
}
|
||||
|
||||
/// Rotate `id` by `deg` (counter-clockwise) about the centre of its box → that rotation.
|
||||
fn rotate(s: &mut Session, id: NodeId, deg: f64) -> Affine {
|
||||
let c = bounds(s, id).center();
|
||||
sel(s, &[id]);
|
||||
s.execute("object.rotate", &json!({"angle": deg})).unwrap();
|
||||
Affine::rotate_about(-deg.to_radians(), c)
|
||||
}
|
||||
|
||||
/// A kite at the origin side of the page, a copy of it turned a quarter turn, and a copy at half
|
||||
/// the size turned by 30° → the three and the transforms that lay the first on the other two.
|
||||
fn turned_kites(s: &mut Session) -> ([NodeId; 3], [Affine; 2]) {
|
||||
let a = kite(s, 0.0, 100.0, 1.0);
|
||||
let (b, c) = (kite(s, 200.0, 100.0, 1.0), kite(s, 400.0, 100.0, 0.5));
|
||||
let quarter = rotate(s, b, 90.0) * Affine::translate((200.0, 0.0));
|
||||
let small = rotate(s, c, 30.0) * Affine::translate((400.0, 100.0)) * Affine::scale(0.5) * Affine::translate((0.0, -100.0));
|
||||
assert!(placed(s, a, b, quarter) && placed(s, a, c, small), "the copies as made");
|
||||
([a, b, c], [quarter, small])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn start_global_edit_takes_rotated_copies_and_an_edit_reaches_each_turned_by_its_angle() {
|
||||
let mut s = session();
|
||||
let ([a, b, c], [quarter, small]) = turned_kites(&mut s);
|
||||
sel(&mut s, &[a]);
|
||||
assert_eq!(crate::cmd::globaledit::highlights(&s).len(), 2, "both are highlighted before the start");
|
||||
assert_eq!(s.execute("select.globalEditOptions", &json!({})).unwrap()["count"], 3);
|
||||
assert_eq!(start(&mut s, a, json!({})), [a, b, c]);
|
||||
let before = (*s.doc().unwrap().doc).clone();
|
||||
let steps = s.doc().unwrap().history.undo.len();
|
||||
// An anchor moved on the selected kite moves on each copy along the copy's own directions.
|
||||
s.execute("select.anchors", &json!({"id": a.0, "anchors": [[0, 1]]})).unwrap();
|
||||
s.execute("path.moveAnchors", &json!({"dx": 12, "dy": 5})).unwrap();
|
||||
assert!(placed(&s, a, b, quarter), "the quarter-turned copy");
|
||||
assert!(placed(&s, a, c, small), "the smaller copy turned by 30°");
|
||||
assert!(!placed(&s, a, b, Affine::translate((200.0, 0.0))), "the copy is still turned");
|
||||
assert_eq!(s.doc().unwrap().history.undo.len(), steps + 1, "one undo step");
|
||||
crate::tests_large_edits::undo_gives_back(&mut s, &before);
|
||||
// A move of the whole kite to the right moves the quarter-turned copy up (counter-clockwise).
|
||||
sel(&mut s, &[a]);
|
||||
let was = bounds(&s, b);
|
||||
s.execute("object.move", &json!({"dx": 10, "dy": 0})).unwrap();
|
||||
let now = bounds(&s, b);
|
||||
assert!((now.x0 - was.x0).abs() < 1e-6 && (now.y0 - (was.y0 - 10.0)).abs() < 1e-6, "{was:?} → {now:?}");
|
||||
assert!(placed(&s, a, b, quarter) && placed(&s, a, c, small), "each copy lies on the kite as before");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_rotated_copy_leads_an_edit_as_any_other_member_does() {
|
||||
let mut s = session();
|
||||
let ([a, b, c], [quarter, small]) = turned_kites(&mut s);
|
||||
// Started on the turned copy, and edited from the smaller one.
|
||||
assert_eq!(start(&mut s, b, json!({})), [b, a, c]);
|
||||
s.execute("select.anchors", &json!({"id": c.0, "anchors": [[0, 2]]})).unwrap();
|
||||
s.execute("path.moveAnchors", &json!({"dx": 3, "dy": -4})).unwrap();
|
||||
assert!(placed(&s, a, b, quarter) && placed(&s, a, c, small));
|
||||
// A fill reaches the turned copies as it is.
|
||||
s.execute("paint.setFill", &json!({"color": "#12ab34"})).unwrap();
|
||||
assert_eq!((fill(&s, a), fill(&s, b)), ("#12ab34".to_string(), "#12ab34".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn size_takes_a_rotated_copy_of_the_same_size_and_other_shapes_stay_out() {
|
||||
let mut s = session();
|
||||
let ([a, b, c], _) = turned_kites(&mut s);
|
||||
// A quarter-turned copy is 70 × 30 by its box: it has the size of the 30 × 70 kite all the same.
|
||||
assert_eq!(start(&mut s, a, json!({"size": true})), [a, b], "not the half-size copy {c}");
|
||||
s.execute("select.globalEdit", &json!({})).unwrap();
|
||||
// A copy turned and then stretched, and one whose anchor was moved, are other shapes.
|
||||
let stretched = kite(&mut s, 0.0, 300.0, 1.0);
|
||||
rotate(&mut s, stretched, 30.0);
|
||||
s.execute("object.scale", &json!({"sx": 150, "sy": 100})).unwrap();
|
||||
let bent = kite(&mut s, 200.0, 300.0, 1.0);
|
||||
rotate(&mut s, bent, 45.0);
|
||||
s.execute("select.anchors", &json!({"id": bent.0, "anchors": [[0, 2]]})).unwrap();
|
||||
s.execute("path.moveAnchors", &json!({"dx": 1, "dy": 0})).unwrap();
|
||||
// A copy turned by half a turn and by a full turn in two steps.
|
||||
let half = kite(&mut s, 400.0, 300.0, 1.0);
|
||||
rotate(&mut s, half, 180.0);
|
||||
let full = kite(&mut s, 600.0, 300.0, 1.0);
|
||||
rotate(&mut s, full, 135.0);
|
||||
rotate(&mut s, full, 225.0);
|
||||
assert_eq!(start(&mut s, a, json!({})), [a, b, c, half, full], "not the stretched {stretched} nor the bent {bent}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_square_whose_anchors_were_turned_is_a_rotated_copy() {
|
||||
let mut s = session();
|
||||
let [a, b, ..] = squares(&mut s);
|
||||
let turn = rotate(&mut s, b, 90.0) * Affine::translate((300.0, 0.0));
|
||||
assert_eq!(start(&mut s, a, json!({})).len(), 3, "the turned square is still taken, with the small one");
|
||||
// The first anchor of the turned square lies at another corner, and that is the one that moves.
|
||||
s.execute("select.anchors", &json!({"id": a.0, "anchors": [[0, 0]]})).unwrap();
|
||||
s.execute("path.moveAnchors", &json!({"dx": 10, "dy": 30})).unwrap();
|
||||
assert!(placed(&s, a, b, turn));
|
||||
assert!(!placed(&s, a, b, Affine::translate((300.0, 0.0))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rotated_groups_are_like_the_group_and_an_edit_inside_one_reaches_the_others_turned() {
|
||||
let mut s = session();
|
||||
let (g, [sq, dot, _]) = card(&mut s, 0.0, 1.0);
|
||||
let (copy, [csq, cdot, cbar]) = card(&mut s, 300.0, 1.0);
|
||||
let turn = rotate(&mut s, copy, 90.0) * Affine::translate((300.0, 0.0));
|
||||
// The same three objects, each turned in place: not the group turned as a whole.
|
||||
let (apart, objects) = card(&mut s, 500.0, 1.0);
|
||||
for id in objects {
|
||||
rotate(&mut s, id, 90.0);
|
||||
}
|
||||
assert_eq!(start(&mut s, g, json!({})), [g, copy], "not the group {apart} whose objects were turned one by one");
|
||||
sel(&mut s, &[dot]);
|
||||
s.execute("paint.setFill", &json!({"color": "#12ab34"})).unwrap();
|
||||
assert_eq!(fill(&s, cdot), "#12ab34");
|
||||
sel(&mut s, &[sq]);
|
||||
s.execute("object.setBounds", &json!({"height": 20, "reference": 0})).unwrap();
|
||||
assert!(placed(&s, sq, csq, turn), "the square in the turned group is halved along the group's own direction");
|
||||
assert_eq!(kids(&s, copy), [csq, cdot, cbar], "each object keeps its id");
|
||||
s.execute("select.globalEdit", &json!({})).unwrap();
|
||||
// An object inside one group takes the object at its place in the turned group, as the
|
||||
// objects of rotated copies of a group are edited together.
|
||||
assert_eq!(start(&mut s, sq, json!({})), [sq, csq]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_rotated_symbol_instance_is_taken_and_a_reflected_one_is_not() {
|
||||
let mut s = session();
|
||||
let r = rect(&mut s, 0.0, 0.0, 40.0, 40.0);
|
||||
let first = NodeId(s.execute("symbol.new", &json!({"ids": [r.0], "name": "Tile"})).unwrap()["id"].as_u64().unwrap());
|
||||
let place =
|
||||
|s: &mut Session, x: f64| NodeId(s.execute("symbol.place", &json!({"name": "Tile", "x": x, "y": 300})).unwrap()["id"].as_u64().unwrap());
|
||||
let (turned, mirrored) = (place(&mut s, 200.0), place(&mut s, 400.0));
|
||||
sel(&mut s, &[turned]);
|
||||
s.execute("object.rotate", &json!({"angle": 30, "origin": [220, 320]})).unwrap();
|
||||
sel(&mut s, &[mirrored]);
|
||||
s.execute("object.reflect", &json!({"axis": "vertical", "origin": [420, 320]})).unwrap();
|
||||
assert_eq!(start(&mut s, first, json!({})), [first, turned], "not the reflected instance {mirrored}");
|
||||
let xf = |s: &Session, id: NodeId| match node(s, id).kind {
|
||||
NodeKind::SymbolInstance { xf, .. } => xf,
|
||||
_ => panic!("not an instance"),
|
||||
};
|
||||
let was = xf(&s, turned);
|
||||
s.execute("object.move", &json!({"dx": 10, "dy": 0})).unwrap();
|
||||
// The instance turned by 30° counter-clockwise moves along its own direction.
|
||||
let moved = Affine::rotate((-30f64).to_radians()) * Point::new(10.0, 0.0);
|
||||
let (now, want) = (xf(&s, turned).as_coeffs(), (Affine::translate(moved.to_vec2()) * was).as_coeffs());
|
||||
assert!(now.iter().zip(want).all(|(p, q)| (p - q).abs() < 1e-6), "{now:?} vs {want:?}");
|
||||
}
|
||||
|
||||
@@ -79,18 +79,29 @@ fn the_source_stays_as_it_is() {
|
||||
assert!(s.doc().unwrap().is_dirty(), "the new document is unsaved");
|
||||
}
|
||||
|
||||
/// An index that names no state, or is not a whole number, is an error and changes nothing: no
|
||||
/// document, no step and no Untitled number is used up.
|
||||
/// An index that names no state (`no history state N`), or is not a whole number ("`index` must be
|
||||
/// a whole number"), is an error and changes nothing: no document, no step and no Untitled number
|
||||
/// is used up.
|
||||
#[test]
|
||||
fn a_bad_index_is_an_error_and_changes_nothing() {
|
||||
let mut s = three_rectangles_one_undone();
|
||||
let source = s.doc().unwrap().doc.clone();
|
||||
let untitled = s.peek_untitled();
|
||||
let bad: [Value; 8] = [json!(4), json!(99), json!(-1), json!(1.5), json!("1"), json!(true), json!([1]), json!(u64::MAX)];
|
||||
for index in bad {
|
||||
let bad: [(Value, bool); 8] = [
|
||||
(json!(4), true),
|
||||
(json!(99), true),
|
||||
(json!(u64::MAX), true),
|
||||
(json!(-1), false),
|
||||
(json!(1.5), false),
|
||||
(json!("1"), false),
|
||||
(json!(true), false),
|
||||
(json!([1]), false),
|
||||
];
|
||||
for (index, whole) in bad {
|
||||
let e = s.execute(CMD, &json!({ "index": index })).unwrap_err();
|
||||
assert!(matches!(e, EngineError::BadParams { .. }), "{index}: {e}");
|
||||
assert!(e.to_string().contains(&format!("no history state {index}")), "{e}");
|
||||
let want = if whole { format!("no history state {index}") } else { "`index` must be a whole number".to_string() };
|
||||
assert_eq!(e.to_string(), format!("invalid parameters for `{CMD}`: {want}"), "{index}");
|
||||
assert_eq!(s.documents().len(), 1, "{index}");
|
||||
assert_eq!(steps(&s), (2, 1), "{index}");
|
||||
assert!(Arc::ptr_eq(&s.doc().unwrap().doc, &source), "{index}");
|
||||
|
||||
@@ -611,15 +611,18 @@ fn edit_copy_and_cut_read_the_recorded_artboards() {
|
||||
s.execute("select.none", &json!({})).unwrap();
|
||||
assert!(matches!(s.execute(id, &json!({})), Err(crate::EngineError::Disabled(..))), "{id}");
|
||||
let before = documents(&s);
|
||||
for bad in [
|
||||
json!({"selectedArtboards": [9]}),
|
||||
json!({"selectedArtboards": []}),
|
||||
json!({"selectedArtboards": "1"}),
|
||||
json!({"selectedArtboards": null}),
|
||||
json!({"selectedArtboards": [1], "art": "yes"}),
|
||||
for (bad, want) in [
|
||||
(json!({"selectedArtboards": [9]}), "no such artboard"),
|
||||
(json!({"selectedArtboards": []}), "`selectedArtboards` must be a non-empty list of artboard indices"),
|
||||
(json!({"selectedArtboards": "1"}), "`selectedArtboards` must be a list of artboard indices"),
|
||||
(json!({"selectedArtboards": null}), "`selectedArtboards` must be a list of artboard indices"),
|
||||
(json!({"selectedArtboards": [1, "0"]}), "`selectedArtboards` must contain whole numbers"),
|
||||
(json!({"selectedArtboards": [1, -1]}), "`selectedArtboards` must contain whole numbers"),
|
||||
(json!({"selectedArtboards": [1], "art": "yes"}), "`art` must be true or false"),
|
||||
] {
|
||||
let err = s.execute(id, &bad).unwrap_err().to_string();
|
||||
assert!(err.contains(id) || err.contains("no such artboard"), "{id} {bad}: {err}");
|
||||
assert!(err.ends_with(want), "{id} {bad}: {err}");
|
||||
assert!(err.contains(id) || want == "no such artboard", "{id} {bad}: {err}");
|
||||
assert_eq!(documents(&s), before, "{id} {bad}");
|
||||
assert!(s.clipboard.artboards.is_empty(), "{id} {bad}");
|
||||
}
|
||||
|
||||
@@ -1979,9 +1979,10 @@ fn fit_to_artwork_bounds_fits_each_artboard_to_its_own_art() {
|
||||
assert_eq!(boards(&s), [Rect::new(100.0, 100.0, 1050.0, 280.0), was[1], was[2]], "the first artboard by default");
|
||||
}
|
||||
|
||||
/// Artboard Options and Fit to Selected Art fail with `no such artboard` on an `index` that names
|
||||
/// no artboard (past the last, negative, fractional, a string or null), and the artboards stay as
|
||||
/// they were. Artboard Options needs an `index`; Fit to Selected Art without one fits the first
|
||||
/// Artboard Options and Fit to Selected Art fail with `no such artboard` on a whole-number `index`
|
||||
/// past the last artboard, with "`index` must be a whole number" on one that is negative,
|
||||
/// fractional or a string, and with `missing index` on a null one, and the artboards stay as they
|
||||
/// were. Artboard Options needs an `index`; Fit to Selected Art without one fits the first
|
||||
/// artboard.
|
||||
#[test]
|
||||
fn artboard_options_and_fit_to_selected_art_refuse_an_index_that_names_no_artboard() {
|
||||
@@ -1992,9 +1993,16 @@ fn artboard_options_and_fit_to_selected_art_refuse_an_index_that_names_no_artboa
|
||||
s.execute("paint.setStroke", &json!({"none": true})).unwrap();
|
||||
let boards = |s: &Session| s.doc().unwrap().doc.artboards.iter().map(|a| a.rect).collect::<Vec<_>>();
|
||||
let (was, n) = (boards(&s), undo_len(&s));
|
||||
for index in [json!(99), json!(-1), json!(1.5), json!("1"), Value::Null] {
|
||||
for (index, error) in [
|
||||
(json!(99), None),
|
||||
(json!(-1), Some("`index` must be a whole number")),
|
||||
(json!(1.5), Some("`index` must be a whole number")),
|
||||
(json!("1"), Some("`index` must be a whole number")),
|
||||
(Value::Null, Some("missing index")),
|
||||
] {
|
||||
for (cmd, p) in [("artboard.setProps", json!({"index": index, "width": 300})), ("artboard.fitToSelection", json!({"index": index}))] {
|
||||
assert_eq!(s.execute(cmd, &p).unwrap_err().to_string(), "no such artboard", "{cmd} {p}");
|
||||
let want = error.map_or_else(|| "no such artboard".to_string(), |e| format!("invalid parameters for `{cmd}`: {e}"));
|
||||
assert_eq!(s.execute(cmd, &p).unwrap_err().to_string(), want, "{cmd} {p}");
|
||||
}
|
||||
}
|
||||
let e = s.execute("artboard.setProps", &json!({"width": 300})).unwrap_err().to_string();
|
||||
|
||||
@@ -257,6 +257,27 @@ fn shadows_under_a_groups_or_an_objects_transform_expand_appearance_as_the_canva
|
||||
}
|
||||
}
|
||||
|
||||
/// Each copy of a Transform effect draws the drop shadow listed before the effect under itself,
|
||||
/// scaled with it, and Expand Appearance keeps those shadows: the square (20..40) has its shadow
|
||||
/// 20 pt to its right, and its copy scaled 200 % and moved 80 pt down (10..50 across, 90..130
|
||||
/// down) has its own 40 pt to its right.
|
||||
#[test]
|
||||
fn each_copys_shadow_expands_as_the_canvas_draws_it() {
|
||||
let mut s = session();
|
||||
run(&mut s, "shape.rectangle", json!({"x": 20, "y": 20, "width": 20, "height": 20}));
|
||||
run(&mut s, "paint.setFill", json!({"color": "#ff0000"}));
|
||||
run(&mut s, "paint.setStroke", json!({"none": true}));
|
||||
let shadow = json!({"x": 20, "y": 0, "blur": 0, "opacity": 100, "mode": "normal", "color": "#00ff00"});
|
||||
run(&mut s, "effect.apply", json!({"effect": "stylize.dropShadow", "params": shadow}));
|
||||
run(&mut s, "effect.apply", json!({"effect": "distort.transform", "params": {"scaleH": 200, "scaleV": 200, "moveV": 80, "copies": 1}}));
|
||||
let before = render(&s);
|
||||
assert_eq!(before.pixel(70, 110), [0, 255, 0, 255], "the copy's shadow");
|
||||
run(&mut s, "effect.expandAppearance", json!({}));
|
||||
let after = render(&s);
|
||||
let diff = differing(&before, &after, 64);
|
||||
assert_eq!(diff, 0, "{diff} pixels differ");
|
||||
}
|
||||
|
||||
/// A group's Transform effect runs after its member's own Offset Path in Expand Appearance as on
|
||||
/// the canvas: the 100 pt square offset to 120 pt and scaled by half expands 60 pt wide.
|
||||
#[test]
|
||||
|
||||
@@ -492,9 +492,11 @@ fn new_and_renamed_artboards_trim_their_names() {
|
||||
assert_eq!(names(&s), ["Back", "Artboard 2", "Artboard 3", "Artboard 4"]);
|
||||
}
|
||||
|
||||
/// Duplicate, Copy and Cut Artboard and Move Artboard Up/Down fail with `no such artboard` on an
|
||||
/// `index` that names no artboard (past the last, negative, fractional, a string or null), and the
|
||||
/// artboards, the art and the clipboard stay as they were.
|
||||
/// Duplicate, Copy and Cut Artboard and Move Artboard Up/Down fail with `no such artboard` on a
|
||||
/// whole-number `index` past the last artboard, with "`index` must be a whole number" on one that
|
||||
/// is negative, fractional, a string, true or a list, and with `missing index` on a null one,
|
||||
/// instead of acting on the Artboard tool's active artboard. The artboards, the art and the
|
||||
/// clipboard stay as they were.
|
||||
#[test]
|
||||
fn artboard_copies_refuse_an_index_that_names_no_artboard() {
|
||||
let mut s = session();
|
||||
@@ -502,10 +504,22 @@ fn artboard_copies_refuse_an_index_that_names_no_artboard() {
|
||||
s.execute("artboard.new", &json!({})).unwrap();
|
||||
s.set_tool_options(Some("artboard"), json!({"active": 1}).as_object().unwrap());
|
||||
let (was, n) = (boards(&s), s.doc().unwrap().history.undo.len());
|
||||
for index in [json!(99), json!(-1), json!(1.5), json!("1"), serde_json::Value::Null] {
|
||||
let not_whole = Some("`index` must be a whole number");
|
||||
let cases = [
|
||||
(json!(99), None),
|
||||
(json!(u64::MAX), None),
|
||||
(json!(-1), not_whole),
|
||||
(json!(1.5), not_whole),
|
||||
(json!("1"), not_whole),
|
||||
(json!(true), not_whole),
|
||||
(json!([1]), not_whole),
|
||||
(serde_json::Value::Null, Some("missing index")),
|
||||
];
|
||||
for (index, error) in cases {
|
||||
for cmd in ["artboard.duplicate", "artboard.copy", "artboard.cut", "artboard.reorder"] {
|
||||
let p = if cmd == "artboard.reorder" { json!({"index": index, "to": 0}) } else { json!({"index": index}) };
|
||||
assert_eq!(s.execute(cmd, &p).unwrap_err().to_string(), "no such artboard", "{cmd} {p}");
|
||||
let want = error.map_or_else(|| "no such artboard".to_string(), |e| format!("invalid parameters for `{cmd}`: {e}"));
|
||||
assert_eq!(s.execute(cmd, &p).unwrap_err().to_string(), want, "{cmd} {p}");
|
||||
}
|
||||
}
|
||||
let st = s.doc().unwrap();
|
||||
@@ -513,6 +527,90 @@ fn artboard_copies_refuse_an_index_that_names_no_artboard() {
|
||||
assert!(s.clipboard.nodes.is_empty() && s.clipboard.artboards.is_empty(), "nothing copied");
|
||||
}
|
||||
|
||||
/// The artboard commands that take `indices` (and `selectedArtboards`, a replay of the journal)
|
||||
/// name the fault: a value that is not a list, an empty list and a list with an entry that is not a
|
||||
/// whole number each have their own message naming the key, and a whole number past the last
|
||||
/// artboard is `no such artboard`, also when another entry is malformed after it. The artboards,
|
||||
/// the art, the undo history and the clipboard stay as they were.
|
||||
#[test]
|
||||
fn artboard_lists_name_the_fault_and_the_key() {
|
||||
let mut s = session();
|
||||
rect(&mut s);
|
||||
s.execute("artboard.new", &json!({})).unwrap();
|
||||
let (was, n) = (boards(&s), s.doc().unwrap().history.undo.len());
|
||||
let cmds = [
|
||||
("artboard.duplicate", json!({})),
|
||||
("artboard.copy", json!({})),
|
||||
("artboard.cut", json!({})),
|
||||
("artboard.rename", json!({"name": "A"})),
|
||||
("artboard.switchOrientation", json!({})),
|
||||
("artboard.delete", json!({})),
|
||||
("artboard.fitToArt", json!({})),
|
||||
("artboard.move", json!({"dx": 10, "dy": 0})),
|
||||
("artboard.align", json!({"horizontal": "left"})),
|
||||
];
|
||||
for key in ["indices", "selectedArtboards"] {
|
||||
let cases = [
|
||||
(json!(null), format!("`{key}` must be a list of artboard indices")),
|
||||
(json!(1), format!("`{key}` must be a list of artboard indices")),
|
||||
(json!("0,1"), format!("`{key}` must be a list of artboard indices")),
|
||||
(json!([]), format!("`{key}` must be a non-empty list of artboard indices")),
|
||||
(json!([0, -1]), format!("`{key}` must contain whole numbers")),
|
||||
(json!([0, 1.5]), format!("`{key}` must contain whole numbers")),
|
||||
(json!(["1"]), format!("`{key}` must contain whole numbers")),
|
||||
(json!([null]), format!("`{key}` must contain whole numbers")),
|
||||
(json!([9, "1"]), format!("`{key}` must contain whole numbers")),
|
||||
(json!([0, 9]), "no such artboard".to_string()),
|
||||
];
|
||||
for (cmd, base) in &cmds {
|
||||
if key == "selectedArtboards" && matches!(*cmd, "artboard.delete" | "artboard.fitToArt" | "artboard.move") {
|
||||
continue;
|
||||
}
|
||||
for (value, want) in &cases {
|
||||
let mut p = base.clone();
|
||||
p[key] = value.clone();
|
||||
let want = if want.starts_with('`') { format!("invalid parameters for `{cmd}`: {want}") } else { want.clone() };
|
||||
assert_eq!(s.execute(cmd, &p).unwrap_err().to_string(), want, "{cmd} {p}");
|
||||
}
|
||||
}
|
||||
}
|
||||
let st = s.doc().unwrap();
|
||||
assert_eq!((boards(&s), st.doc.layers[0].children().unwrap().len(), st.history.undo.len()), (was, 1, n));
|
||||
assert!(s.clipboard.nodes.is_empty() && s.clipboard.artboards.is_empty(), "nothing copied");
|
||||
}
|
||||
|
||||
/// Align, Distribute and Distribute Spacing for artboards name the key that holds a malformed
|
||||
/// artboard to align to (`artboard` or `activeArtboard`), not `index`; a null one is left out, and
|
||||
/// a whole number past the last artboard is `no such artboard`.
|
||||
#[test]
|
||||
fn artboard_align_names_the_key_of_a_malformed_reference() {
|
||||
let mut s = session();
|
||||
s.execute("artboard.new", &json!({})).unwrap();
|
||||
s.execute("artboard.new", &json!({})).unwrap();
|
||||
let cmds = [
|
||||
("artboard.align", json!({"indices": [0, 1, 2], "horizontal": "left", "to": "artboard"})),
|
||||
("artboard.distribute", json!({"indices": [0, 1, 2], "horizontal": "left", "to": "artboard"})),
|
||||
("artboard.distributeSpacing", json!({"indices": [0, 1, 2], "to": "artboard"})),
|
||||
];
|
||||
for (cmd, base) in &cmds {
|
||||
for key in ["artboard", "activeArtboard"] {
|
||||
let (was, n) = (boards(&s), s.doc().unwrap().history.undo.len());
|
||||
for value in [json!(-1), json!(1.5), json!("1"), json!(true)] {
|
||||
let mut p = base.clone();
|
||||
p[key] = value;
|
||||
let want = format!("invalid parameters for `{cmd}`: `{key}` must be a whole number");
|
||||
assert_eq!(s.execute(cmd, &p).unwrap_err().to_string(), want, "{cmd} {p}");
|
||||
}
|
||||
let mut p = base.clone();
|
||||
p[key] = json!(9);
|
||||
assert_eq!(s.execute(cmd, &p).unwrap_err().to_string(), "no such artboard", "{cmd} {p}");
|
||||
assert_eq!((boards(&s), s.doc().unwrap().history.undo.len()), (was, n), "{cmd} {key}: nothing moved");
|
||||
p[key] = json!(null);
|
||||
s.execute(cmd, &p).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Artboard Options, Move Artboard, Duplicate Artboards, Copy Artboard and Cut Artboard refuse an
|
||||
/// option given as text or as another value that is not true or false, such as "true", and
|
||||
/// Artboard Options and Move Artboard a position, size or move given as text, such as "10". The
|
||||
|
||||
@@ -1978,6 +1978,26 @@ fn highlight_alternate_glyphs_preference_is_off_by_default() {
|
||||
assert!(!s.prefs.highlight_alternate_glyphs);
|
||||
}
|
||||
|
||||
/// Preferences › Type › Show Character Alternates: on by default, listed in the Type category,
|
||||
/// saved with the other preferences, and a value that is not true or false is refused.
|
||||
#[test]
|
||||
fn show_character_alternates_preference_is_on_by_default_and_saved() {
|
||||
let mut s = Session::new();
|
||||
assert!(s.prefs.show_character_alternates, "on by default");
|
||||
let spec = crate::cmd::prefscmds::PREF_SPECS.iter().find(|p| p.key == "showCharacterAlternates").expect("listed");
|
||||
assert_eq!((spec.category, spec.label), ("Type", "Show Character Alternates"));
|
||||
assert_eq!(s.execute("prefs.get", &json!({"key": "showCharacterAlternates"})).unwrap(), json!(true));
|
||||
set_pref(&mut s, "showCharacterAlternates", json!(false));
|
||||
assert!(!s.prefs.show_character_alternates);
|
||||
assert!(s.execute("prefs.set", &json!({"key": "showCharacterAlternates", "value": "yes"})).is_err(), "not a boolean");
|
||||
assert!(!s.prefs.show_character_alternates, "a refused value changes nothing");
|
||||
let saved: Prefs = serde_json::from_value(serde_json::to_value(&s.prefs).unwrap()).unwrap();
|
||||
assert!(!saved.show_character_alternates);
|
||||
// Preferences saved before the key existed read it as on.
|
||||
let old: Prefs = serde_json::from_value(json!({"highlightAlternateGlyphs": true})).unwrap();
|
||||
assert!(old.show_character_alternates);
|
||||
}
|
||||
|
||||
/// Preferences › Type › Highlight Substituted Fonts: on by default, listed in the Type category,
|
||||
/// and saved with the other preferences.
|
||||
/// <https://helpx.adobe.com/illustrator/desktop/design-with-text/fonts-and-scripts/edit-text-without-replacing-missing-fonts.html>
|
||||
|
||||
Reference in New Issue
Block a user