Files
vectorcraft/crates/engine/src/cmd/stepandrepeat/tests.rs
T

665 lines
34 KiB
Rust

use serde_json::json;
use super::*;
const A4: (f64, f64) = (210.0, 297.0);
#[test]
fn fills_a4_with_50_by_30_items_and_3_gap() {
assert_eq!(fill_counts(A4.0, A4.1, 0.0, 50.0, 30.0, 3.0, 3.0), (9, 4));
assert_eq!(fill_counts(A4.0, A4.1, 5.0, 50.0, 30.0, 3.0, 3.0), (8, 3));
assert_eq!(fill_counts(40.0, 297.0, 0.0, 50.0, 30.0, 3.0, 3.0), (9, 0));
}
#[test]
fn layout_places_cells_at_the_pitch_and_alternates_rotation() {
let c = layout(50.0, 30.0, 3.0, 3.0, 9, 4, Alternate::Columns);
assert_eq!(c.len(), 36);
let at = |r: usize, k: usize| c[r * 4 + k];
assert_eq!((at(0, 0).dx, at(0, 0).dy), (0.0, 0.0));
assert_eq!((at(2, 3).dx, at(2, 3).dy), (159.0, 66.0));
assert_eq!(c.iter().filter(|c| c.turned).count(), 18);
assert!(at(4, 1).turned && at(4, 3).turned && !at(4, 0).turned && !at(4, 2).turned);
let rows = layout(50.0, 30.0, 3.0, 3.0, 3, 2, Alternate::Rows);
assert_eq!(rows.iter().map(|c| c.turned).collect::<Vec<_>>(), [false, false, true, true, false, false]);
assert!(layout(50.0, 30.0, 3.0, 3.0, 3, 2, Alternate::None).iter().all(|c| !c.turned));
}
fn setup() -> (Session, u64) {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 210, "height": 297})).unwrap();
let id = s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 50, "height": 30})).unwrap()["id"].as_u64().unwrap();
(s, id)
}
fn bounds(s: &Session, id: u64) -> (f64, f64, f64, f64) {
let b = s.doc().unwrap().doc.node(NodeId(id)).unwrap().geometric_bounds().unwrap();
(b.x0, b.y0, b.x1, b.y1)
}
fn undo_steps(s: &Session) -> usize {
s.doc().unwrap().history.undo.len()
}
#[test]
fn creates_the_grid_in_one_undo_step_and_undoes_cleanly() {
let (mut s, _) = setup();
let (before, steps) = (s.doc().unwrap().doc.clone(), undo_steps(&s));
let r = s.execute(CMD, &json!({"rows": 2, "columns": 3, "gap": 3})).unwrap();
assert_eq!((r["count"].clone(), r["rows"].clone(), r["columns"].clone()), (json!(6), json!(2), json!(3)));
assert_eq!(undo_steps(&s), steps + 1);
let ids: Vec<u64> = r["ids"].as_array().unwrap().iter().map(|i| i.as_u64().unwrap()).collect();
assert_eq!(bounds(&s, ids[0]), (0.0, 0.0, 50.0, 30.0));
assert_eq!(bounds(&s, ids[4]), (53.0, 33.0, 103.0, 63.0));
assert_eq!(bounds(&s, ids[5]), (106.0, 33.0, 156.0, 63.0));
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(undo_steps(&s), steps);
let d = &s.doc().unwrap().doc;
assert_eq!(d.artboards.len(), before.artboards.len());
assert!(d.node(NodeId(ids[0])).is_some());
assert!(ids[1..].iter().all(|i| d.node(NodeId(*i)).is_none()));
}
#[test]
fn a_single_unchanged_cell_keeps_undo_and_redo() {
for fill in [false, true] {
let (mut s, id) = setup();
let copies = s.execute(CMD, &json!({"columns": 2})).unwrap();
s.execute("edit.undo", &json!({})).unwrap();
let steps = undo_steps(&s);
let redo = s.doc().unwrap().history.redo.len();
assert!(redo > 0);
let r = s.execute(CMD, &json!({"fill": fill, "sheet": {"x": 0, "y": 0, "width": 50, "height": 30}})).unwrap();
assert_eq!(r, json!({"ids": [id], "rows": 1, "columns": 1, "count": 1}));
assert_eq!(bounds(&s, id), (0.0, 0.0, 50.0, 30.0));
assert_eq!(undo_steps(&s), steps);
assert_eq!(s.doc().unwrap().history.redo.len(), redo);
s.execute("edit.redo", &json!({})).unwrap();
let copy = copies["ids"][1].as_u64().unwrap();
assert_eq!(bounds(&s, copy), (50.0, 0.0, 100.0, 30.0));
}
}
#[test]
fn a_single_unchanged_cell_clears_the_layer_target_in_one_undo_step() {
for fill in [false, true] {
let (mut s, id) = setup();
let layer = s.doc().unwrap().doc.layers[0].id;
s.execute("layer.target", &json!({"id": layer.0})).unwrap();
let before = s.doc().unwrap().selection.clone();
assert_eq!(before.objects, [NodeId(id)]);
assert_eq!(before.target, Some(layer));
let steps = undo_steps(&s);
let sheet = json!({"x": 0, "y": 0, "width": 50, "height": 30});
let r = s.execute(CMD, &json!({"fill": fill, "sheet": sheet})).unwrap();
assert_eq!(r, json!({"ids": [id], "rows": 1, "columns": 1, "count": 1}));
let mut expected = before.clone();
expected.set([NodeId(id)]);
assert_eq!(s.doc().unwrap().selection, expected);
assert_eq!(bounds(&s, id), (0.0, 0.0, 50.0, 30.0));
assert_eq!(undo_steps(&s), steps + 1);
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(s.doc().unwrap().selection, before);
assert_eq!(undo_steps(&s), steps);
s.execute("edit.redo", &json!({})).unwrap();
assert_eq!(s.doc().unwrap().selection, expected);
assert_eq!(undo_steps(&s), steps + 1);
}
}
#[test]
fn a_single_unchanged_cell_records_the_resolved_patterns_setting() {
for fill in [false, true] {
for patterns in [false, true] {
let (mut s, id) = setup();
s.execute("object.pattern.make", &json!({"name": "Dots", "width": 50, "height": 30})).unwrap();
s.execute("object.pattern.done", &json!({})).unwrap();
s.execute("paint.setFill", &json!({"ids": [id], "swatch": "Dots"})).unwrap();
s.execute("select.set", &json!({"ids": [id]})).unwrap();
s.prefs.transform_pattern_tiles = patterns;
let sheet = json!({"x": 0, "y": 0, "width": 50, "height": 30});
for explicit in [false, true] {
let mut params = json!({"fill": fill, "sheet": sheet});
let expected = if explicit { !patterns } else { patterns };
if explicit {
params["patterns"] = json!(expected);
}
let start = s.journal.len();
let steps = undo_steps(&s);
let r = s.execute(CMD, &params).unwrap();
assert_eq!(r, json!({"ids": [id], "rows": 1, "columns": 1, "count": 1}));
params["patterns"] = json!(expected);
assert_eq!(s.journal_for_action(start), [(CMD.to_string(), params)]);
assert_eq!(undo_steps(&s), steps);
}
}
}
}
#[test]
fn a_single_unchanged_cell_selects_the_compound_in_one_undo_step() {
for fill in [false, true] {
let (mut s, member) = setup();
let inner_params = json!({"x": 10, "y": 5, "width": 20, "height": 10});
let inner = s.execute("shape.rectangle", &inner_params).unwrap()["id"].as_u64().unwrap();
s.execute("select.set", &json!({"ids": [member, inner]})).unwrap();
let compound = s.execute("object.compoundPath.make", &json!({})).unwrap()["id"].as_u64().unwrap();
let before = bounds(&s, compound);
s.execute(CMD, &json!({"columns": 2})).unwrap();
s.execute("edit.undo", &json!({})).unwrap();
s.select(|_, sel| sel.set([NodeId(member)])).unwrap();
assert_eq!(s.doc().unwrap().selection.objects, [NodeId(member)]);
let steps = undo_steps(&s);
let redo = s.doc().unwrap().history.redo.len();
assert!(redo > 0);
let sheet = json!({"x": 0, "y": 0, "width": 50, "height": 30});
let r = s.execute(CMD, &json!({"fill": fill, "sheet": sheet})).unwrap();
assert_eq!(r, json!({"ids": [compound], "rows": 1, "columns": 1, "count": 1}));
assert_eq!(s.doc().unwrap().selection.objects, [NodeId(compound)]);
assert_eq!(bounds(&s, compound), before);
assert_eq!(undo_steps(&s), steps + 1);
assert!(s.doc().unwrap().history.redo.is_empty());
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(s.doc().unwrap().selection.objects, [NodeId(member)]);
assert_eq!(bounds(&s, compound), before);
assert_eq!(undo_steps(&s), steps);
s.execute("edit.redo", &json!({})).unwrap();
assert_eq!(s.doc().unwrap().selection.objects, [NodeId(compound)]);
assert_eq!(bounds(&s, compound), before);
assert_eq!(undo_steps(&s), steps + 1);
}
}
#[test]
fn a_single_unchanged_cell_selects_the_whole_path_in_one_undo_step() {
for fill in [false, true] {
let (mut s, id) = setup();
let id = NodeId(id);
s.select(|_, sel| {
sel.set([id]);
sel.anchors.insert(id, [(0, 0), (0, 1)].into_iter().collect());
})
.unwrap();
let before = s.doc().unwrap().selection.clone();
let steps = undo_steps(&s);
let sheet = json!({"x": 0, "y": 0, "width": 50, "height": 30});
let r = s.execute(CMD, &json!({"fill": fill, "sheet": sheet})).unwrap();
assert_eq!(r, json!({"ids": [id.0], "rows": 1, "columns": 1, "count": 1}));
assert_eq!(s.doc().unwrap().selection.objects, [id]);
assert!(s.doc().unwrap().selection.anchors.is_empty());
assert!(s.doc().unwrap().selection.segments.is_empty());
assert_eq!(bounds(&s, id.0), (0.0, 0.0, 50.0, 30.0));
assert_eq!(undo_steps(&s), steps + 1);
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(s.doc().unwrap().selection, before);
assert_eq!(undo_steps(&s), steps);
s.execute("edit.redo", &json!({})).unwrap();
assert_eq!(s.doc().unwrap().selection.objects, [id]);
assert!(s.doc().unwrap().selection.anchors.is_empty());
assert_eq!(undo_steps(&s), steps + 1);
}
}
#[test]
fn a_single_moved_cell_records_an_undo_step() {
let (mut s, id) = setup();
let steps = undo_steps(&s);
let r = s.execute(CMD, &json!({"fill": true, "sheet": {"x": 5, "y": 5, "width": 50, "height": 30}})).unwrap();
assert_eq!(r, json!({"ids": [id], "rows": 1, "columns": 1, "count": 1}));
assert_eq!(bounds(&s, id), (5.0, 5.0, 55.0, 35.0));
assert_eq!(undo_steps(&s), steps + 1);
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(bounds(&s, id), (0.0, 0.0, 50.0, 30.0));
}
#[test]
fn fill_sheet_with_margin_moves_the_first_copy_to_the_corner() {
let (mut s, _) = setup();
let r = s.execute(CMD, &json!({"fill": true, "gap": 3, "margin": 0})).unwrap();
assert_eq!((r["rows"].clone(), r["columns"].clone(), r["count"].clone()), (json!(9), json!(4), json!(36)));
let (mut s, _) = setup();
let r = s.execute(CMD, &json!({"fill": true, "gap": 3, "margin": 5})).unwrap();
assert_eq!(r["count"], json!(24));
let first = r["ids"][0].as_u64().unwrap();
assert_eq!(bounds(&s, first), (5.0, 5.0, 55.0, 35.0));
}
#[test]
fn fill_places_every_cell_from_the_original_when_it_starts_away_from_the_margin() {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 210, "height": 297})).unwrap();
s.execute("shape.rectangle", &json!({"x": 20, "y": 40, "width": 50, "height": 30})).unwrap();
let r = s.execute(CMD, &json!({"fill": true, "gap": 3, "margin": 5, "rotate": "columns"})).unwrap();
let ids: Vec<u64> = r["ids"].as_array().unwrap().iter().map(|i| i.as_u64().unwrap()).collect();
assert_eq!((r["rows"].clone(), r["columns"].clone()), (json!(8), json!(3)));
assert_eq!(bounds(&s, ids[0]), (5.0, 5.0, 55.0, 35.0));
assert_eq!(bounds(&s, ids[1]), (58.0, 5.0, 108.0, 35.0));
assert_eq!(bounds(&s, ids[2]), (111.0, 5.0, 161.0, 35.0));
assert_eq!(bounds(&s, ids[3]), (5.0, 38.0, 55.0, 68.0));
}
#[test]
fn alternate_columns_turn_by_180_degrees_about_the_cell_center() {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 210, "height": 297})).unwrap();
let id = s.execute("shape.polygon", &json!({"cx": 25, "cy": 15, "radius": 10, "sides": 3})).unwrap()["id"].as_u64().unwrap();
let b0 = bounds(&s, id);
let r = s.execute(CMD, &json!({"rows": 1, "columns": 2, "gap": 4, "rotate": "columns"})).unwrap();
let ids: Vec<u64> = r["ids"].as_array().unwrap().iter().map(|i| i.as_u64().unwrap()).collect();
let (w, b1) = (b0.2 - b0.0, bounds(&s, ids[1]));
assert!((b1.0 - (b0.0 + w + 4.0)).abs() < 1e-6 && ((b1.2 - b1.0) - w).abs() < 1e-6);
assert!((b1.1 - b0.1).abs() < 1e-6 && (b1.3 - b0.3).abs() < 1e-6);
let tri = |s: &Session, id: u64| format!("{:?}", s.doc().unwrap().doc.node(NodeId(id)).unwrap().kind);
assert_ne!(tri(&s, ids[0]), tri(&s, ids[1]));
}
#[test]
fn a_cut_contour_stays_grouped_with_its_artwork_in_every_copy() {
let (mut s, art) = setup();
let cut = s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 50, "height": 30})).unwrap()["id"].as_u64().unwrap();
s.execute("select.set", &json!({"ids": [art, cut]})).unwrap();
let r = s.execute(CMD, &json!({"rows": 1, "columns": 3, "gap": 2, "cutId": cut})).unwrap();
assert_eq!(r["count"], json!(3));
let d = &s.doc().unwrap().doc;
for id in r["ids"].as_array().unwrap() {
let n = d.node(NodeId(id.as_u64().unwrap())).unwrap();
assert!(matches!(n.kind, vectorcraft_doc::NodeKind::Group { .. }));
let mut paths = 0;
n.walk(&mut |c| paths += usize::from(matches!(c.kind, vectorcraft_doc::NodeKind::Path { .. })));
assert_eq!(paths, 2);
}
}
#[test]
fn rejects_bad_params_and_a_selection_that_does_not_fit() {
let (mut s, _) = setup();
assert!(s.execute(CMD, &json!({"rows": 0})).is_err());
assert!(s.execute(CMD, &json!({"rows": 1000, "columns": 1000})).is_err());
assert!(s.execute(CMD, &json!({"rotate": "diagonal"})).is_err());
assert!(s.execute(CMD, &json!({"cutId": 999999})).is_err());
assert!(s.execute(CMD, &json!({"fill": true, "sheet": {"x": 0, "y": 0, "width": 40, "height": 40}})).is_err());
let mut e = Session::new();
e.execute("file.new", &json!({"width": 100, "height": 100})).unwrap();
assert!(e.execute(CMD, &json!({})).is_err());
}
#[test]
fn rejects_a_grid_whose_copies_would_hold_too_many_objects() {
let (mut s, _) = setup();
for i in 1..25 {
s.execute("shape.rectangle", &json!({"x": i, "y": 0, "width": 10, "height": 10})).unwrap();
}
s.execute("select.all", &json!({})).unwrap();
let (count, steps) = (s.doc().unwrap().doc.node_count(), undo_steps(&s));
// 25 objects and the group they go into, in each of 9,999 copies, are more than the limit,
// though the cells are not.
let e = s.execute(CMD, &json!({"rows": 100, "columns": 100})).unwrap_err();
assert!(e.to_string().contains("objects"), "{e}");
assert_eq!((s.doc().unwrap().doc.node_count(), undo_steps(&s)), (count, steps));
}
#[test]
fn rejects_a_negative_margin_and_gaps_that_fold_the_grid_back() {
let (mut s, _) = setup();
let steps = undo_steps(&s);
assert!(s.execute(CMD, &json!({"fill": true, "margin": -5})).is_err());
assert!(s.execute(CMD, &json!({"rows": 1, "columns": 3, "gapX": -50})).is_err());
assert!(s.execute(CMD, &json!({"rows": 3, "columns": 1, "gapY": -40})).is_err());
assert!(s.execute(CMD, &json!({"fill": true, "gap": -50})).is_err());
assert_eq!(undo_steps(&s), steps, "a refusal records no undo step");
let r = s.execute(CMD, &json!({"rows": 1, "columns": 2, "gapX": -10})).unwrap();
assert_eq!(bounds(&s, r["ids"][1].as_u64().unwrap()), (40.0, 0.0, 90.0, 30.0), "a gap above minus the width overlaps neighbors");
}
#[test]
fn copies_go_in_with_one_walk_of_the_document_whatever_the_count() {
let walks = |columns: u64| {
let (mut s, _) = setup();
for i in 0..50 {
s.execute("shape.rectangle", &json!({"x": 0, "y": 100 + i, "width": 5, "height": 5})).unwrap();
}
let id = s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 2, "height": 2})).unwrap()["id"].as_u64().unwrap();
s.execute("select.set", &json!({"ids": [id]})).unwrap();
let before = vectorcraft_doc::tree_walks();
let r = s.execute(CMD, &json!({"rows": 1, "columns": columns, "gap": 1})).unwrap();
assert_eq!(r["count"], json!(columns));
vectorcraft_doc::tree_walks() - before
};
assert_eq!(walks(40), walks(2), "the walks of the document do not grow with the number of copies");
}
#[test]
fn the_last_cells_copy_sits_directly_above_the_original() {
let (mut s, id) = setup();
let r = s.execute(CMD, &json!({"rows": 1, "columns": 3, "gap": 1})).unwrap();
let ids: Vec<u64> = r["ids"].as_array().unwrap().iter().map(|i| i.as_u64().unwrap()).collect();
let d = &s.doc().unwrap().doc;
let order: Vec<u64> = d.layers[0].children().unwrap().iter().map(|n| n.id.0).collect();
assert_eq!(order, [id, ids[2], ids[1]]);
}
#[test]
fn fill_ignores_explicit_counts() {
let (mut s, _) = setup();
let r = s.execute(CMD, &json!({"fill": true, "rows": 0, "columns": 0, "gap": 3})).unwrap();
assert_eq!((r["rows"].clone(), r["columns"].clone()), (json!(9), json!(4)));
assert!(s.execute(CMD, &json!({"rows": 0})).is_err());
}
#[test]
fn fractional_gaps_are_exact_except_for_pixel_aligned_objects_which_stay_on_the_grid() {
let gap = |aligned: bool| {
let (mut s, id) = setup();
if aligned {
s.execute("object.setPixelAligned", &json!({"ids": [id], "on": true})).unwrap();
}
let r = s.execute(CMD, &json!({"rows": 1, "columns": 2, "gapX": 0.5})).unwrap();
let ids: Vec<u64> = r["ids"].as_array().unwrap().iter().map(|i| i.as_u64().unwrap()).collect();
let (a, c) = (bounds(&s, ids[0]), bounds(&s, ids[1]));
(c.0 - a.2, c.0 - a.0)
};
assert!((gap(false).0 - 0.5).abs() < 1e-9);
let pitch = gap(true).1;
assert!((pitch - pitch.round()).abs() < 1e-9);
}
#[test]
fn pixel_aligned_pitch_preserves_requested_gaps_near_whole_points() {
for gap in [0.0, 0.25, -0.25] {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 210, "height": 297})).unwrap();
let params = json!({"x": 0, "y": 0, "width": 50.0000005 - gap, "height": 30.0000005 - gap});
let cut = s.execute("shape.rectangle", &params).unwrap()["id"].as_u64().unwrap();
let art = s.execute("shape.rectangle", &json!({"x": 10, "y": 5, "width": 20, "height": 20})).unwrap()["id"].as_u64().unwrap();
s.execute("object.setPixelAligned", &json!({"ids": [art], "on": true})).unwrap();
s.execute("select.set", &json!({"ids": [cut, art]})).unwrap();
let r = s.execute(CMD, &json!({"rows": 2, "columns": 2, "gap": gap, "cutId": cut})).unwrap();
assert_eq!(r["count"], json!(4));
let d = &s.doc().unwrap().doc;
let cut_bounds = |id: &Value| {
let group = d.node(NodeId(id.as_u64().unwrap())).unwrap();
group.children().unwrap()[0].geometric_bounds().unwrap()
};
let ids = r["ids"].as_array().unwrap();
for row in 0..2 {
let left = cut_bounds(&ids[row * 2]);
let right = cut_bounds(&ids[row * 2 + 1]);
assert!(right.x0 - left.x1 >= gap - EPS, "the horizontal gap shrank: {left:?} {right:?}");
let pitch = right.x0 - left.x0;
assert!((pitch - pitch.round()).abs() < EPS);
}
for col in 0..2 {
let top = cut_bounds(&ids[col]);
let bottom = cut_bounds(&ids[col + 2]);
assert!(bottom.y0 - top.y1 >= gap - EPS, "the vertical gap shrank: {top:?} {bottom:?}");
let pitch = bottom.y0 - top.y0;
assert!((pitch - pitch.round()).abs() < EPS);
}
}
}
#[test]
fn a_pixel_aligned_cut_keeps_its_place_against_its_artwork_in_every_copy() {
let (mut s, cut) = setup();
s.execute("object.setPixelAligned", &json!({"ids": [cut], "on": true})).unwrap();
let art = s.execute("shape.rectangle", &json!({"x": 1, "y": 1, "width": 48, "height": 28})).unwrap()["id"].as_u64().unwrap();
s.execute("select.set", &json!({"ids": [cut, art]})).unwrap();
let r = s.execute(CMD, &json!({"rows": 2, "columns": 3, "gapX": 0.6, "gapY": 1.3, "cutId": cut})).unwrap();
let d = &s.doc().unwrap().doc;
let inset = |id: &Value| {
let mut rects = vec![];
d.node(NodeId(id.as_u64().unwrap())).unwrap().walk(&mut |c| {
if matches!(c.kind, vectorcraft_doc::NodeKind::Path { .. }) {
rects.push(c.geometric_bounds().unwrap());
}
});
(rects[1].x0 - rects[0].x0, rects[1].y0 - rects[0].y0)
};
let ids = r["ids"].as_array().unwrap();
let first = inset(&ids[0]);
for id in &ids[1..] {
let (x, y) = inset(id);
assert!((x - first.0).abs() < 1e-9 && (y - first.1).abs() < 1e-9, "the artwork moved against the cut: {:?} against {first:?}", (x, y));
}
}
#[test]
fn pixel_aligned_art_stays_registered_in_turned_cells_and_gaps_never_shrink() {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 210, "height": 297})).unwrap();
let cut = s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 50.25, "height": 30})).unwrap()["id"].as_u64().unwrap();
let art = s.execute("shape.rectangle", &json!({"x": 10, "y": 5, "width": 20, "height": 20})).unwrap()["id"].as_u64().unwrap();
s.execute("object.setPixelAligned", &json!({"ids": [art], "on": true})).unwrap();
s.execute("select.set", &json!({"ids": [cut, art]})).unwrap();
let r = s.execute(CMD, &json!({"columns": 3, "gapX": 0.1, "rotate": "columns", "cutId": cut})).unwrap();
let d = &s.doc().unwrap().doc;
let rects = |id: &Value| {
let mut rects = vec![];
d.node(NodeId(id.as_u64().unwrap())).unwrap().walk(&mut |c| {
if matches!(c.kind, vectorcraft_doc::NodeKind::Path { .. }) {
rects.push(c.geometric_bounds().unwrap());
}
});
(rects[0], rects[1])
};
let ids = r["ids"].as_array().unwrap();
let (c0, a0) = rects(&ids[0]);
let (c1, a1) = rects(&ids[1]);
let (c2, a2) = rects(&ids[2]);
// The turned cell mirrors the art inside its cut; the next unturned cell repeats it.
assert!(((c1.x1 - a1.x1) - (a0.x0 - c0.x0)).abs() < 1e-9, "turned art moved against its cut: {c1:?} {a1:?}");
assert!(((a2.x0 - c2.x0) - (a0.x0 - c0.x0)).abs() < 1e-9);
assert!(c2.x0 - c0.x0 > 2.0 * (50.25 + 0.1) - 1e-9, "the pitch was rounded down: {c2:?}");
assert!(c1.x0 - c0.x1 >= 0.1 - 1e-9 && c2.x0 - c1.x1 >= 0.1 - 1e-9, "a gap next to the turned cell shrank: {c0:?} {c1:?} {c2:?}");
}
#[test]
fn turned_pixel_aligned_cells_stay_on_the_sheet() {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 210, "height": 297})).unwrap();
let cut = s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 50, "height": 30.75})).unwrap()["id"].as_u64().unwrap();
let art = s.execute("shape.rectangle", &json!({"x": 10, "y": 5, "width": 20, "height": 20})).unwrap()["id"].as_u64().unwrap();
s.execute("object.setPixelAligned", &json!({"ids": [art], "on": true})).unwrap();
s.execute("select.set", &json!({"ids": [cut, art]})).unwrap();
let sheet = json!({"x": 0, "y": 0, "width": 101, "height": 30.75});
let steps = undo_steps(&s);
let r = s.execute(CMD, &json!({"fill": true, "rotate": "columns", "cutId": cut, "sheet": sheet})).unwrap();
assert_eq!(r["columns"], json!(1));
assert_eq!(r["count"], json!(1));
assert_eq!(undo_steps(&s), steps + 1);
let group = NodeId(r["ids"][0].as_u64().unwrap());
assert!(matches!(s.doc().unwrap().doc.node(group).unwrap().kind, vectorcraft_doc::NodeKind::Group { .. }));
s.execute("edit.undo", &json!({})).unwrap();
s.execute("select.set", &json!({"ids": [cut, art]})).unwrap();
let sheet = json!({"x": 0, "y": 0, "width": 102, "height": 31.75});
let r = s.execute(CMD, &json!({"fill": true, "rotate": "columns", "cutId": cut, "sheet": sheet})).unwrap();
assert_eq!(r["columns"], json!(2));
let d = &s.doc().unwrap().doc;
for id in r["ids"].as_array().unwrap() {
let b = d.node(NodeId(id.as_u64().unwrap())).unwrap().geometric_bounds().unwrap();
assert!(b.x0 >= -1e-9 && b.y0 >= -1e-9 && b.x1 <= 102.0 + 1e-9 && b.y1 <= 31.75 + 1e-9, "a cell runs past the sheet: {b:?}");
}
}
#[test]
fn fill_keeps_rounded_turn_offsets_inside_the_sheet_near_whole_points() {
for (height, columns) in [(30.0000005, 1), (31.0000005, 2)] {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 210, "height": 297})).unwrap();
let cut = s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 50, "height": 30.0000005})).unwrap()["id"].as_u64().unwrap();
let art = s.execute("shape.rectangle", &json!({"x": 10, "y": 5, "width": 20, "height": 20})).unwrap()["id"].as_u64().unwrap();
s.execute("object.setPixelAligned", &json!({"ids": [art], "on": true})).unwrap();
s.execute("select.set", &json!({"ids": [cut, art]})).unwrap();
let sheet = json!({"x": 0, "y": 0, "width": 100, "height": height});
let r = s.execute(CMD, &json!({"fill": true, "rotate": "columns", "cutId": cut, "sheet": sheet})).unwrap();
assert_eq!((r["rows"].clone(), r["columns"].clone(), r["count"].clone()), (json!(1), json!(columns), json!(columns)));
for id in r["ids"].as_array().unwrap() {
let (x0, y0, x1, y1) = bounds(&s, id.as_u64().unwrap());
assert!(x0 >= -1e-9 && y0 >= -1e-9 && x1 <= 100.0 + 1e-9 && y1 <= height + 1e-9, "a cell runs past the sheet");
}
}
}
#[test]
fn fill_accepts_exact_fit_when_the_doubled_turn_center_has_roundoff() {
for (rotate, x, y, width, height, sw, sh, rows, columns) in
[("columns", 0.0, 0.1, 50.0, 29.8, 100.0, 29.8, 1, 2), ("rows", 0.1, 0.0, 29.8, 50.0, 29.8, 100.0, 2, 1)]
{
let mut s = Session::new();
s.execute("file.new", &json!({"width": 210, "height": 297})).unwrap();
let params = json!({"x": x, "y": y, "width": width, "height": height});
let cut = s.execute("shape.rectangle", &params).unwrap()["id"].as_u64().unwrap();
let art = s.execute("shape.rectangle", &json!({"x": 10, "y": 5, "width": 10, "height": 10})).unwrap()["id"].as_u64().unwrap();
s.execute("object.setPixelAligned", &json!({"ids": [art], "on": true})).unwrap();
s.execute("select.set", &json!({"ids": [cut, art]})).unwrap();
let b = s.doc().unwrap().doc.node(NodeId(cut)).unwrap().geometric_bounds().unwrap();
let c2 = if rotate == "columns" { b.center().y * 2.0 } else { b.center().x * 2.0 };
assert!(c2 > c2.round() && c2 - c2.round() < EPS);
let sheet = json!({"x": x, "y": y, "width": sw, "height": sh});
let r = s.execute(CMD, &json!({"fill": true, "rotate": rotate, "cutId": cut, "sheet": sheet})).unwrap();
assert_eq!((r["rows"].clone(), r["columns"].clone(), r["count"].clone()), (json!(rows), json!(columns), json!(2)));
for id in r["ids"].as_array().unwrap() {
let (x0, y0, x1, y1) = bounds(&s, id.as_u64().unwrap());
assert!(x0 >= x - EPS && y0 >= y - EPS && x1 <= x + sw + EPS && y1 <= y + sh + EPS, "a cell runs past the sheet");
}
}
}
#[test]
fn fill_reserves_turn_clearance_only_for_turned_cells() {
for (rotate, width, height, rows, columns) in [
("columns", 101.25, 31.0, 1, 1),
("columns", 102.0, 31.0, 1, 2),
("columns", 152.25, 31.0, 1, 3),
("columns", 152.25, 30.75, 1, 1),
("rows", 51.0, 61.75, 1, 1),
("rows", 51.0, 62.0, 2, 1),
("rows", 51.0, 92.75, 3, 1),
("rows", 50.25, 92.75, 1, 1),
] {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 210, "height": 297})).unwrap();
let cut = s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 50.25, "height": 30.75})).unwrap()["id"].as_u64().unwrap();
let art = s.execute("shape.rectangle", &json!({"x": 10, "y": 5, "width": 20, "height": 20})).unwrap()["id"].as_u64().unwrap();
s.execute("object.setPixelAligned", &json!({"ids": [art], "on": true})).unwrap();
s.execute("select.set", &json!({"ids": [cut, art]})).unwrap();
let sheet = json!({"x": 0, "y": 0, "width": width, "height": height});
let r = s.execute(CMD, &json!({"fill": true, "rotate": rotate, "cutId": cut, "sheet": sheet})).unwrap();
assert_eq!((r["rows"].clone(), r["columns"].clone()), (json!(rows), json!(columns)), "{rotate} on {width} by {height}");
let d = &s.doc().unwrap().doc;
for id in r["ids"].as_array().unwrap() {
let b = d.node(NodeId(id.as_u64().unwrap())).unwrap().geometric_bounds().unwrap();
assert!(b.x0 >= -1e-9 && b.y0 >= -1e-9 && b.x1 <= width + 1e-9 && b.y1 <= height + 1e-9, "a cell runs past the sheet: {b:?}");
}
}
}
#[test]
fn turned_columns_keep_the_vertical_pitch_without_turn_clearance() {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 210, "height": 297})).unwrap();
let cut = s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 50, "height": 30.75})).unwrap()["id"].as_u64().unwrap();
let art = s.execute("shape.rectangle", &json!({"x": 10, "y": 5, "width": 20, "height": 20})).unwrap()["id"].as_u64().unwrap();
s.execute("object.setPixelAligned", &json!({"ids": [art], "on": true})).unwrap();
s.execute("select.set", &json!({"ids": [cut, art]})).unwrap();
let r = s.execute(CMD, &json!({"rows": 2, "columns": 2, "gapY": 0.2, "rotate": "columns", "cutId": cut})).unwrap();
let d = &s.doc().unwrap().doc;
let cut_bounds = |id: &Value| {
let mut rects = vec![];
d.node(NodeId(id.as_u64().unwrap())).unwrap().walk(&mut |c| {
if matches!(c.kind, vectorcraft_doc::NodeKind::Path { .. }) {
rects.push(c.geometric_bounds().unwrap());
}
});
rects[0]
};
let ids = r["ids"].as_array().unwrap();
for col in 0..2 {
let first = cut_bounds(&ids[col]);
let second = cut_bounds(&ids[col + 2]);
assert!((second.y0 - first.y0 - 31.0).abs() < 1e-9, "the vertical pitch includes turn clearance: {first:?} {second:?}");
assert!(second.y0 - first.y1 >= 0.2 - 1e-9, "the vertical gap shrank: {first:?} {second:?}");
}
}
#[test]
fn fill_counts_only_the_pixel_aligned_copies_that_stay_on_the_sheet() {
let (mut s, id) = setup();
// The 1 pt stroke puts the snapped rectangle at (0.5, 0.5), 50 by 30.
s.execute("object.setPixelAligned", &json!({"ids": [id], "on": true})).unwrap();
assert_eq!(bounds(&s, id), (0.5, 0.5, 50.5, 30.5));
let steps = undo_steps(&s);
let r = s.execute(CMD, &json!({"fill": true, "gapX": 0.6, "sheet": {"x": 0.5, "y": 0.5, "width": 100.6, "height": 30}})).unwrap();
assert_eq!(r["columns"], json!(1));
assert_eq!(undo_steps(&s), steps);
let (mut s, id) = setup();
s.execute("object.setPixelAligned", &json!({"ids": [id], "on": true})).unwrap();
let r = s.execute(CMD, &json!({"fill": true, "gapX": 0.6, "sheet": {"x": 0.5, "y": 0.5, "width": 101, "height": 30}})).unwrap();
assert_eq!(r["columns"], json!(2));
let d = &s.doc().unwrap().doc;
for id in r["ids"].as_array().unwrap() {
let b = d.node(NodeId(id.as_u64().unwrap())).unwrap().geometric_bounds().unwrap();
assert!(b.x1 <= 101.5 + 1e-9, "a copy runs past the sheet: {b:?}");
}
}
#[test]
fn fill_rounds_pixel_aligned_origins_inside_the_sheet_near_whole_points() {
for (dx, dy) in [(0.0000005, 0.0), (0.0, 0.0000005), (-0.9999995, -0.9999995)] {
let (mut s, id) = setup();
s.execute("object.setPixelAligned", &json!({"ids": [id], "on": true})).unwrap();
let (x, y, _, _) = bounds(&s, id);
let (sx, sy) = (x + dx, y + dy);
let steps = undo_steps(&s);
let tight = json!({"x": sx, "y": sy, "width": 50, "height": 30});
assert!(s.execute(CMD, &json!({"fill": true, "sheet": tight})).is_err());
assert_eq!(undo_steps(&s), steps);
let sheet = json!({"x": sx, "y": sy, "width": 51, "height": 31});
let r = s.execute(CMD, &json!({"fill": true, "sheet": sheet})).unwrap();
assert_eq!(r, json!({"ids": [id], "rows": 1, "columns": 1, "count": 1}));
let (x0, y0, x1, y1) = bounds(&s, id);
assert!(x0 >= sx && y0 >= sy && x1 <= sx + 51.0 && y1 <= sy + 31.0);
}
}
#[test]
fn fill_accepts_exact_fit_when_the_origin_subtraction_has_roundoff() {
for (x, y, sx, sy) in [(1.2, 0.0, 2.2, 0.0), (0.0, 1.2, 0.0, 2.2), (1.2, 1.2, 2.2, 2.2)] {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 210, "height": 297})).unwrap();
let cut_params = json!({"x": x, "y": y, "width": 50, "height": 30});
let cut = s.execute("shape.rectangle", &cut_params).unwrap()["id"].as_u64().unwrap();
let art_params = json!({"x": 10, "y": 5, "width": 20, "height": 20});
let art = s.execute("shape.rectangle", &art_params).unwrap()["id"].as_u64().unwrap();
s.execute("object.setPixelAligned", &json!({"ids": [art], "on": true})).unwrap();
s.execute("select.set", &json!({"ids": [cut, art]})).unwrap();
let sheet = json!({"x": sx, "y": sy, "width": 50, "height": 30});
let r = s.execute(CMD, &json!({"fill": true, "cutId": cut, "sheet": sheet})).unwrap();
assert_eq!((r["rows"].clone(), r["columns"].clone(), r["count"].clone()), (json!(1), json!(1), json!(1)));
let (x0, y0, x1, y1) = bounds(&s, cut);
assert!((x0 - sx).abs() < EPS && (y0 - sy).abs() < EPS);
assert!((x1 - (sx + 50.0)).abs() < EPS && (y1 - (sy + 30.0)).abs() < EPS);
}
}
#[test]
fn fill_picks_the_artboard_of_the_selection_not_of_the_cut_contour() {
let first_x = |sheet: Option<Value>| {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 100, "height": 100})).unwrap();
s.execute("artboard.new", &json!({"x": 100, "y": 0, "width": 100, "height": 100})).unwrap();
let art = s.execute("shape.rectangle", &json!({"x": 10, "y": 10, "width": 60, "height": 20})).unwrap()["id"].as_u64().unwrap();
let cut = s.execute("shape.rectangle", &json!({"x": 105, "y": 10, "width": 20, "height": 20})).unwrap()["id"].as_u64().unwrap();
s.execute("select.set", &json!({"ids": [art, cut]})).unwrap();
let mut p = json!({"fill": true, "cutId": cut});
if let Some(sh) = sheet {
p["sheet"] = sh;
}
let r = s.execute(CMD, &p).unwrap();
bounds(&s, r["ids"][0].as_u64().unwrap()).0
};
let default = first_x(None);
let explicit = first_x(Some(json!({"x": 100, "y": 0, "width": 100, "height": 100})));
assert!((explicit - default - 100.0).abs() < 1e-9);
}