mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-28 09:21:55 +01:00
Merge main into fix/the-liquify-warning-s-tool-name-and-object-kinds
This commit is contained in:
commit
fa53bc2940
118 files changed
+4731
-311
No files matched your search
@@ -612,6 +612,100 @@ fn eraser_size_follows_the_pen_pressure() {
|
||||
assert!(err.to_string().contains("sizeMode"), "{err}");
|
||||
}
|
||||
|
||||
/// Eraser Tool Options' Angle, Roundness and Size variation menus: a value on Rotation, Bearing,
|
||||
/// Tilt or Pressure takes what the pen read along the drag, Random stays within its Variation, a
|
||||
/// drag without the reading erases at the value itself, an open path is cut where the varied tip
|
||||
/// covers it, and an unknown mode or a variation out of its range is refused with nothing erased.
|
||||
#[test]
|
||||
fn the_eraser_tip_varies_with_its_modes() {
|
||||
// Which of `probes` a level drag through the middle of a 400 by 200 rectangle erases, each of
|
||||
// its points `[x, 100]` followed by `pen` ([pressure, tiltX, tiltY, rotation]).
|
||||
let erased = |tip: serde_json::Value, pen: serde_json::Value, probes: &[(f64, f64)]| -> Vec<bool> {
|
||||
let mut s = session();
|
||||
rect(&mut s, 0.0, 0.0, 400.0, 200.0);
|
||||
let points: Vec<_> = (0..=20)
|
||||
.map(|i| {
|
||||
let mut p = vec![json!(100.0 + f64::from(i) * 10.0), json!(100.0)];
|
||||
p.extend(pen.as_array().cloned().unwrap_or_default());
|
||||
serde_json::Value::Array(p)
|
||||
})
|
||||
.collect();
|
||||
s.execute("path.eraseRegion", &with(&json!({"points": points, "size": 20}), tip.clone())).unwrap();
|
||||
let filled = |x: f64, y: f64| {
|
||||
paths(&s).iter().any(|(_, pd)| vectorcraft_geom::hit::fill_contains(&pd.to_bezpath(), FillRule::NonZero, Point::new(x, y)))
|
||||
};
|
||||
assert!(filled(200.0, 150.0), "{tip} {pen}: the rest of the rectangle stays");
|
||||
probes.iter().map(|&(x, y)| !filled(x, y)).collect()
|
||||
};
|
||||
let none = json!(null);
|
||||
// A 20 by 2 pt tip lying level erases a band 2 pt tall; standing upright it erases one 20 pt tall.
|
||||
let level = json!({"roundness": 10});
|
||||
let probes = [(200.0, 100.5), (200.0, 108.0)];
|
||||
assert_eq!(erased(level.clone(), json!([]), &probes), [true, false], "Fixed");
|
||||
let on_rotation = with(&level, json!({"angleMode": "rotation", "angleVariation": 90}));
|
||||
// The barrel turned half way round reads the most, which turns the tip upright.
|
||||
assert_eq!(erased(on_rotation.clone(), json!([none, none, none, 180]), &probes), [true, true], "Rotation");
|
||||
assert_eq!(erased(on_rotation.clone(), json!([]), &probes), [true, false], "Rotation without a reading");
|
||||
// Leaning to the left (a bearing of 180°) reads the most too.
|
||||
let on_bearing = with(&level, json!({"angleMode": "bearing", "angleVariation": 90}));
|
||||
assert_eq!(erased(on_bearing.clone(), json!([none, -45, 0, none]), &probes), [true, true], "Bearing");
|
||||
assert_eq!(erased(on_bearing, json!([none, 45, 0, none]), &probes), [true, false], "Bearing at 0°");
|
||||
// A round tip: upright the pen gives 28 pt, nearly flat about 12 pt.
|
||||
let on_tilt = json!({"sizeMode": "tilt", "sizeVariation": 8});
|
||||
let wide = [(200.0, 105.0), (200.0, 113.0)];
|
||||
assert_eq!(erased(on_tilt.clone(), json!([none, 0, 0, none]), &wide), [true, true], "Tilt upright");
|
||||
assert_eq!(erased(on_tilt, json!([none, 89, 0, none]), &wide), [true, false], "Tilt flat");
|
||||
// Roundness from 10 % less 90 (kept to 1 %) at the lightest pressure to 100 % at the heaviest.
|
||||
let on_pressure = with(&level, json!({"roundnessMode": "pressure", "roundnessVariation": 90}));
|
||||
assert_eq!(erased(on_pressure.clone(), json!([1.0]), &probes), [true, true], "Pressure at the heaviest");
|
||||
// At the lightest the tip is 0.2 pt across, narrower than the 2 pt band Fixed erases.
|
||||
assert_eq!(erased(on_pressure, json!([0.0]), &[(200.0, 100.0), (200.0, 100.5)]), [true, false], "Pressure at the lightest");
|
||||
// Random: one size for the drag, from 16 to 24 pt.
|
||||
let random = json!({"sizeMode": "random", "sizeVariation": 4});
|
||||
assert_eq!(erased(random, json!([]), &[(200.0, 107.5), (200.0, 112.5)]), [true, false], "Random");
|
||||
// A press on a line with the tip turned upright by its rotation cuts 5 pt of it, not 20.
|
||||
let left_of_line = |pen: serde_json::Value| {
|
||||
let mut s = session();
|
||||
line(&mut s, 300.0, 150.0, 500.0, 150.0);
|
||||
let mut point = vec![json!(400), json!(150)];
|
||||
point.extend(pen.as_array().cloned().unwrap_or_default());
|
||||
let tip = json!({"points": [point], "size": 20, "roundness": 25, "angleMode": "rotation", "angleVariation": 90});
|
||||
s.execute("path.eraseRegion", &tip).unwrap();
|
||||
paths(&s).iter().map(|p| p.1.length()).sum::<f64>()
|
||||
};
|
||||
let upright = left_of_line(json!([none, none, none, 180]));
|
||||
assert!((upright - 195.0).abs() < 0.3, "the upright tip left {upright} pt of the line");
|
||||
let level_tip = left_of_line(json!([]));
|
||||
assert!((level_tip - 180.0).abs() < 0.3, "the level tip left {level_tip} pt of the line");
|
||||
// A 1500 pt tip on Tilt erases 1500 pt across with a mouse, as it does on Fixed.
|
||||
let mut s = session();
|
||||
rect(&mut s, 0.0, 0.0, 4000.0, 4000.0);
|
||||
let big = json!({"points": [[1000, 2000], [3000, 2000]], "size": 1500, "sizeMode": "tilt", "sizeVariation": 10});
|
||||
s.execute("path.eraseRegion", &big).unwrap();
|
||||
let filled =
|
||||
|x: f64, y: f64| paths(&s).iter().any(|(_, pd)| vectorcraft_geom::hit::fill_contains(&pd.to_bezpath(), FillRule::NonZero, Point::new(x, y)));
|
||||
assert_eq!((filled(2000.0, 2740.0), filled(2000.0, 2760.0)), (false, true), "a 1500 pt tip");
|
||||
// A Size Variation kept while Size is Fixed leaves the size alone.
|
||||
let kept = with(&on_rotation, json!({"sizeMode": "fixed", "sizeVariation": 1000000}));
|
||||
assert_eq!(erased(kept, json!([]), &probes), [true, false], "Size on Fixed with a Variation");
|
||||
let mut s = session();
|
||||
let id = rect(&mut s, 0.0, 0.0, 100.0, 100.0);
|
||||
s.execute("select.set", &json!({"ids": [id.0]})).unwrap();
|
||||
let stroke = json!({"points": [[-10, 50], [110, 50]]});
|
||||
let modes = "\"fixed\", \"random\", \"pressure\", \"tilt\", \"bearing\" or \"rotation\"";
|
||||
for (extra, want) in [
|
||||
(json!({"angleMode": "wheel"}), format!("angleMode is one of {modes}, not `wheel`")),
|
||||
(json!({"roundnessMode": 3}), format!("roundnessMode is one of {modes}, not 3")),
|
||||
(json!({"sizeMode": "stylusWheel"}), format!("sizeMode is one of {modes}, not `stylusWheel`")),
|
||||
(json!({"angleVariation": 181}), "angleVariation must be at most 180".to_string()),
|
||||
(json!({"roundnessVariation": 101}), "roundnessVariation must be at most 100".to_string()),
|
||||
(json!({"roundnessVariation": -1}), "roundnessVariation must be 0 or more".to_string()),
|
||||
] {
|
||||
refuses_with(&mut s, "path.eraseRegion", &with(&stroke, extra), &want);
|
||||
}
|
||||
refuses_a_number_given_as_text(&mut s, "path.eraseRegion", &stroke, "angleVariation");
|
||||
}
|
||||
|
||||
/// 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.
|
||||
|
||||
@@ -492,6 +492,111 @@ fn options_set_tile_type_spacing_and_overlap() {
|
||||
assert!(s.execute("pattern.options", &json!({"name": "Dots", "width": -1})).is_err());
|
||||
}
|
||||
|
||||
/// Move Tile with Art: in pattern editing mode, a move of all of the tile art moves the tile by the
|
||||
/// same offset, through `object.move`, `object.nudge` and `object.transform`. With the option off
|
||||
/// (a new pattern's default), a move of part of the art, a rotation or a copy, the tile stays.
|
||||
/// Undo puts the tile back, and Done keeps the moved tile.
|
||||
#[test]
|
||||
fn move_tile_with_art_moves_the_tile_when_all_of_the_art_moves() {
|
||||
let mut s = session();
|
||||
let a = rect(&mut s, 0.0, 0.0, 10.0, 10.0);
|
||||
let b = rect(&mut s, 12.0, 0.0, 6.0, 6.0);
|
||||
sel(&mut s, &[a, b]);
|
||||
s.execute("object.pattern.make", &json!({"name": "P"})).unwrap();
|
||||
let tile = |s: &Session| doc(s).pattern("P").unwrap().tile;
|
||||
let start = tile(&s);
|
||||
let art: Vec<NodeId> = s.doc().unwrap().selection.objects.to_vec();
|
||||
assert_eq!(art.len(), 2);
|
||||
assert!(!doc(&s).pattern("P").unwrap().move_tile_with_art, "off for a new pattern");
|
||||
s.execute("object.move", &json!({"dx": 5, "dy": 0})).unwrap();
|
||||
assert_eq!(tile(&s), start, "with the option off the tile stays");
|
||||
s.execute("object.move", &json!({"dx": -5, "dy": 0})).unwrap();
|
||||
|
||||
s.execute("pattern.options", &json!({"moveTileWithArt": true})).unwrap();
|
||||
assert_eq!(s.execute("pattern.list", &json!({})).unwrap()["patterns"][0]["moveTileWithArt"], true);
|
||||
s.execute("object.move", &json!({"dx": 3, "dy": 4})).unwrap();
|
||||
assert_eq!(tile(&s), start + vectorcraft_geom::Vec2::new(3.0, 4.0));
|
||||
s.execute("object.transform", &json!({"matrix": [1, 0, 0, 1, -1, 2]})).unwrap();
|
||||
assert_eq!(tile(&s), start + vectorcraft_geom::Vec2::new(2.0, 6.0));
|
||||
let k = s.prefs.keyboard_increment;
|
||||
s.execute("object.nudge", &json!({"dx": 1, "dy": 0})).unwrap();
|
||||
assert_eq!(tile(&s), start + vectorcraft_geom::Vec2::new(2.0 + k, 6.0));
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert_eq!(tile(&s), start + vectorcraft_geom::Vec2::new(3.0, 4.0), "undo puts the tile back");
|
||||
let moved = tile(&s);
|
||||
|
||||
sel(&mut s, &art[..1]);
|
||||
s.execute("object.move", &json!({"dx": 7, "dy": 0})).unwrap();
|
||||
assert_eq!(tile(&s), moved, "a move of part of the art leaves the tile");
|
||||
sel(&mut s, &art);
|
||||
s.execute("object.rotate", &json!({"angle": 90})).unwrap();
|
||||
assert_eq!(tile(&s), moved, "a rotation leaves the tile");
|
||||
s.execute("object.move", &json!({"dx": 1, "dy": 1, "copy": true})).unwrap();
|
||||
assert_eq!(tile(&s), moved, "a copy leaves the tile");
|
||||
|
||||
s.execute("object.pattern.done", &json!({})).unwrap();
|
||||
assert_eq!(tile(&s), moved, "Done keeps the moved tile");
|
||||
assert!(doc(&s).pattern("P").unwrap().move_tile_with_art);
|
||||
}
|
||||
|
||||
/// Move Tile with Art counts a group in the tile art as moved when each of its members moved, one
|
||||
/// by one selected.
|
||||
#[test]
|
||||
fn move_tile_with_art_counts_a_group_whose_members_all_moved() {
|
||||
let mut s = session();
|
||||
let a = rect(&mut s, 0.0, 0.0, 10.0, 10.0);
|
||||
let b = rect(&mut s, 12.0, 0.0, 6.0, 6.0);
|
||||
sel(&mut s, &[a, b]);
|
||||
s.execute("object.group", &json!({})).unwrap();
|
||||
s.execute("object.pattern.make", &json!({"name": "G"})).unwrap();
|
||||
s.execute("pattern.options", &json!({"moveTileWithArt": true})).unwrap();
|
||||
let start = doc(&s).pattern("G").unwrap().tile;
|
||||
let layer = doc(&s).pattern_edit.as_ref().unwrap().layer;
|
||||
let group = doc(&s).node(layer).unwrap().children().unwrap()[0].clone();
|
||||
let members: Vec<NodeId> = group.children().unwrap().iter().map(|c| c.id).collect();
|
||||
assert_eq!(members.len(), 2);
|
||||
sel(&mut s, &members[..1]);
|
||||
s.execute("object.move", &json!({"dx": 4, "dy": 0})).unwrap();
|
||||
assert_eq!(doc(&s).pattern("G").unwrap().tile, start, "one member of two");
|
||||
sel(&mut s, &members);
|
||||
s.execute("object.move", &json!({"dx": 0, "dy": 5})).unwrap();
|
||||
assert_eq!(doc(&s).pattern("G").unwrap().tile, start + vectorcraft_geom::Vec2::new(0.0, 5.0), "every member");
|
||||
}
|
||||
|
||||
/// Move Tile with Art moves the tile as far as the art moved: when the art is aligned to the pixel
|
||||
/// grid and a move snaps it to the grid, the tile moves by the snapped distance.
|
||||
#[test]
|
||||
fn move_tile_with_art_follows_art_that_snaps_to_the_pixel_grid() {
|
||||
let mut s = session();
|
||||
let a = rect(&mut s, 0.0, 0.0, 10.0, 10.0);
|
||||
sel(&mut s, &[a]);
|
||||
s.execute("object.pattern.make", &json!({"name": "P"})).unwrap();
|
||||
s.execute("pattern.options", &json!({"moveTileWithArt": true})).unwrap();
|
||||
let art = s.doc().unwrap().selection.objects[0];
|
||||
std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc).node_mut(art).unwrap().extras_mut().pixel_aligned = true;
|
||||
let start = doc(&s).pattern("P").unwrap().tile;
|
||||
let origin = |s: &Session| doc(s).node(art).unwrap().geometric_bounds().unwrap().origin();
|
||||
let before = origin(&s);
|
||||
s.execute("object.move", &json!({"dx": 0.3, "dy": 0})).unwrap();
|
||||
let moved = origin(&s) - before;
|
||||
assert!((moved.x - 0.3).abs() > 1e-6, "the art snapped to the grid: {moved:?}");
|
||||
assert_eq!(doc(&s).pattern("P").unwrap().tile, start + moved, "the tile moved as far");
|
||||
}
|
||||
|
||||
/// Pattern Options that change nothing record no undo step.
|
||||
#[test]
|
||||
fn options_that_change_nothing_record_no_undo_step() {
|
||||
let mut s = session();
|
||||
make_dots(&mut s);
|
||||
let steps = |s: &Session| s.doc().unwrap().history.undo.len();
|
||||
let before = steps(&s);
|
||||
s.execute("pattern.options", &json!({"name": "Dots", "moveTileWithArt": false, "copies": 5})).unwrap();
|
||||
assert_eq!(steps(&s), before);
|
||||
s.execute("pattern.options", &json!({"name": "Dots", "moveTileWithArt": true})).unwrap();
|
||||
assert_eq!(steps(&s), before + 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn options_rename_updates_swatch_and_paints() {
|
||||
let mut s = session();
|
||||
@@ -1625,6 +1730,7 @@ fn pattern_commands_refuse_a_param_of_the_wrong_type() {
|
||||
("pattern.options", options(json!({"copies": "5"})), number("copies", json!("5"))),
|
||||
("pattern.options", options(json!({"dimCopies": "50"})), number("dimCopies", json!("50"))),
|
||||
("pattern.options", options(json!({"sizeTileToArt": "true"})), switch("sizeTileToArt")),
|
||||
("pattern.options", options(json!({"moveTileWithArt": "yes"})), switch("moveTileWithArt")),
|
||||
("pattern.options", options(json!({"showTileEdge": 1})), switch("showTileEdge")),
|
||||
("pattern.options", options(json!({"showSwatchBounds": "no"})), switch("showSwatchBounds")),
|
||||
("pattern.options", options(json!({"overlap": "right"})), "`overlap` must be an object, not \"right\"".into()),
|
||||
|
||||
@@ -666,6 +666,26 @@ fn linked_pages_share_one_copy_of_the_file_and_open_in_the_pages_color_mode() {
|
||||
assert_eq!(s.doc().unwrap().doc.color_mode, vectorcraft_doc::ColorMode::Rgb, "colorMode wins");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn linked_pages_read_the_file_once_and_a_blank_page_keeps_its_crop_box() {
|
||||
use vectorcraft_testkit::pdf::{PdfPage, pdf};
|
||||
let dir = Folder::new("open-pdf-link-pages-blank");
|
||||
let src = dir.file("blank.pdf");
|
||||
let art = || PdfPage::new(100.0, 50.0, "1 0 0 rg 10 10 20 20 re f");
|
||||
write(&src, &pdf(&[art(), PdfPage::new(80.0, 40.0, ""), art()], None));
|
||||
let mut s = Session::new();
|
||||
s.execute("document.open", &json!({"path": src, "linkPages": true, "cropTo": "bounding"})).unwrap();
|
||||
let d = &s.doc().unwrap().doc;
|
||||
let boards: Vec<Rect> = d.artboards.iter().map(|a| a.rect).collect();
|
||||
assert_eq!(boards.len(), 3, "{boards:?}");
|
||||
assert_eq!((boards[0].width(), boards[0].height()), (20.0, 20.0), "the art's bounds");
|
||||
assert_eq!((boards[1].width(), boards[1].height()), (80.0, 40.0), "the blank page's crop box");
|
||||
// The pages are read from the one copy every page's blob keeps: a page that read the file
|
||||
// again would count here.
|
||||
let blob = d.images.values().next().map(|b| b.bytes.clone()).unwrap();
|
||||
assert_eq!(vectorcraft_pdf::reads_of(&blob), 0, "the pages are read from the file read once");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn link_pages_is_refused_without_a_path_for_other_files_and_with_a_password_or_outlines() {
|
||||
let dir = Folder::new("open-pdf-link-pages-refused");
|
||||
|
||||
@@ -300,6 +300,71 @@ fn an_installed_script_is_read_as_script_run_reads_a_file() {
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_installed_run_runs_the_text_its_own_read_checked() {
|
||||
let dir = folder("installed-run");
|
||||
let root = dir.join("Scripts");
|
||||
let script = touch(&root, "draw.jsx");
|
||||
std::fs::write(&script, "#include \"lib.jsxinc\"\nvc.log(x);").unwrap();
|
||||
std::fs::write(root.join("lib.jsxinc"), "var x = 1;").unwrap();
|
||||
// A folder in the Scripts folder that is a link: script.run by path alone follows it.
|
||||
let elsewhere = dir.join("elsewhere");
|
||||
std::fs::create_dir_all(&elsewhere).unwrap();
|
||||
std::fs::write(elsewhere.join("other.jsx"), "vc.log(2);").unwrap();
|
||||
link_folder(&elsewhere, &root.join("linked"));
|
||||
let linked = root.join("linked").join("other.jsx").to_string_lossy().into_owned();
|
||||
let mut s = Session::new();
|
||||
s.attach_scripts_dir(root.clone());
|
||||
let path = script.to_string_lossy().into_owned();
|
||||
let r = s.execute("script.run", &json!({"path": path, "installed": true})).unwrap();
|
||||
assert_eq!(r["log"], json!([1]));
|
||||
assert_eq!(s.execute("script.run", &json!({"path": linked})).unwrap()["log"], json!([2]));
|
||||
// With `installed`, the run's own read is the check: a script behind a link is not run.
|
||||
let e = s.execute("script.run", &json!({"path": linked, "installed": true})).unwrap_err();
|
||||
assert_eq!(e.to_string(), cmd::scripts::NOT_LISTED);
|
||||
let outside = touch(&dir, "outside.js");
|
||||
let e = s.execute("script.run", &json!({"path": outside.to_string_lossy(), "installed": true})).unwrap_err();
|
||||
assert_eq!(e.to_string(), cmd::scripts::NOT_LISTED);
|
||||
// `installed` is true or false, and takes a path.
|
||||
assert!(
|
||||
s.execute("script.run", &json!({"path": path, "installed": "yes"})).unwrap_err().to_string().contains("`installed` must be true or false")
|
||||
);
|
||||
assert!(s.execute("script.run", &json!({"source": "1", "installed": true})).unwrap_err().to_string().contains("`installed` takes a `path`"));
|
||||
assert_eq!(s.execute("script.run", &json!({"source": "vc.log(3);", "installed": false})).unwrap()["log"], json!([3]));
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_installed_run_reads_its_includes_in_the_folder_its_read_found() {
|
||||
let dir = folder("installed-scope");
|
||||
let checked = dir.join("Scripts").join("tools");
|
||||
let swapped = dir.join("swapped");
|
||||
for (folder, x) in [(&checked, 1), (&swapped, 2)] {
|
||||
std::fs::create_dir_all(folder).unwrap();
|
||||
std::fs::write(folder.join("draw.jsx"), "").unwrap();
|
||||
std::fs::write(folder.join("lib.jsxinc"), format!("var x = {x};")).unwrap();
|
||||
}
|
||||
// The path now leads through a link to another folder, as after a link was put in place of the
|
||||
// folder once the script's text was read.
|
||||
let link = dir.join("link");
|
||||
link_folder(&swapped, &link);
|
||||
let mut s = Session::new();
|
||||
let request = script::Request {
|
||||
name: "draw.jsx".into(),
|
||||
name_given: false,
|
||||
file: Some(link.join("draw.jsx").to_string_lossy().into_owned()),
|
||||
checked_real: Some(std::fs::canonicalize(checked.join("draw.jsx")).unwrap()),
|
||||
source: "#include \"lib.jsxinc\"\nvc.log(x);".into(),
|
||||
args: Default::default(),
|
||||
fuel: script::DEFAULT_FUEL,
|
||||
locale: "en_US".into(),
|
||||
answers: Vec::new(),
|
||||
};
|
||||
s.script_start(request).unwrap();
|
||||
assert_eq!(s.script_run_to_end().unwrap()["log"], json!([1]));
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_automation_read_root_keeps_an_agent_out_of_the_scripts_folder() {
|
||||
let dir = folder("roots");
|
||||
|
||||
Reference in new issue
Block a user