Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Merge branch 'pr/662' into batch1

Brandon Thomas89e9abbc8d46
diff --cc crates/engine/src/cmd/object.rs
index 7a65bce7d,21362ef9a..aa5a2cc01
--- a/crates/engine/src/cmd/object.rs
+++ b/crates/engine/src/cmd/object.rs
@@@ -3244,60 -2814,28 +3244,86 @@@ mod swatch_name_tests
}
}
+#[cfg(test)]
+mod path_edit_tests {
+ use designcraft_doc::ItemId;
+ use designcraft_geom::Point;
+ use serde_json::json;
+
+ fn session_with_path() -> (crate::Session, u64) {
+ let mut s = crate::Session::new();
+ s.execute("file.new", &json!({})).unwrap();
+ let id = s
+ .execute(
+ "path.create",
+ &json!({"anchors": [{"p": [100, 100]}, {"p": [200, 100], "in": [170, 80], "out": [230, 120]}, {"p": [300, 200]}]}),
+ )
+ .unwrap()["id"]
+ .as_u64()
+ .unwrap();
+ (s, id)
+ }
+
+ fn anchors(s: &crate::Session, id: u64) -> Vec<designcraft_geom::Anchor> {
+ let d = &s.doc().unwrap().doc;
+ let it = d.item(ItemId(id)).unwrap();
+ let xf = d.parent_xf(&d.find(ItemId(id)).unwrap()) * it.xf;
+ it.path.subpaths[0].anchors.iter().map(|a| a.transform(xf)).collect()
+ }
+
+ #[test]
+ fn reverse_path_swaps_ends_and_direction_lines() {
+ let (mut s, id) = session_with_path();
+ s.execute("path.reverse", &json!({"ids": [id]})).unwrap();
+ let a = anchors(&s, id);
+ assert_eq!(a.iter().map(|a| a.p).collect::<Vec<_>>(), [Point::new(300.0, 200.0), Point::new(200.0, 100.0), Point::new(100.0, 100.0)]);
+ assert_eq!((a[1].h_in, a[1].h_out), (Point::new(230.0, 120.0), Point::new(170.0, 80.0)));
+ assert!(s.execute("path.reverse", &json!({"ids": [id], "subpath": 3})).is_err());
+ }
+
+ #[test]
+ fn anchors_go_on_the_named_open_subpath_and_set_anchor_reshapes_one() {
+ let (mut s, id) = session_with_path();
+ s.execute("path.appendAnchor", &json!({"id": id, "subpath": 0, "anchor": {"p": [400, 250]}})).unwrap();
+ assert_eq!(anchors(&s, id).last().unwrap().p, Point::new(400.0, 250.0));
+ assert!(s.execute("path.appendAnchor", &json!({"id": id, "subpath": 1, "anchor": {"p": [0, 0]}})).is_err());
+ // Direction lines on the last anchor: collinear and opposite make it smooth.
+ s.execute("path.setAnchor", &json!({"id": id, "anchor": "last", "in": [380, 230], "out": [420, 270]})).unwrap();
+ let last = *anchors(&s, id).last().unwrap();
+ assert_eq!((last.h_in, last.h_out, last.kind), (Point::new(380.0, 230.0), Point::new(420.0, 270.0), designcraft_geom::AnchorKind::Smooth));
+ s.execute("path.setAnchor", &json!({"id": id, "anchor": 0, "p": [90, 110]})).unwrap();
+ assert_eq!(anchors(&s, id)[0].p, Point::new(90.0, 110.0));
+ assert!(s.execute("path.setAnchor", &json!({"id": id, "anchor": 9, "p": [0, 0]})).is_err());
+ assert!(s.execute("path.setAnchor", &json!({"id": id, "anchor": "last"})).is_err(), "nothing to set");
+ // Closing one subpath; then it takes no more anchors.
+ s.execute("path.close", &json!({"id": id, "subpath": 0})).unwrap();
+ assert!(s.execute("path.appendAnchor", &json!({"id": id, "anchor": {"p": [0, 0]}})).is_err());
+ assert!(s.execute("path.close", &json!({"id": id, "subpath": 4})).is_err());
+ }
+}
++
+ #[cfg(test)]
+ mod spread_target_tests {
+ use super::*;
+
+ #[test]
+ fn creating_on_a_parent_spread_and_rejecting_unknown_spreads() {
+ let mut s = Session::new();
+ s.execute("file.new", &json!({"pages": 1, "facingPages": false})).unwrap();
+ let parent = json!({"kind": "parent", "index": 0});
+ let r = s.execute("frame.create", &json!({"spread": parent, "rect": [0, 0, 100, 100]})).unwrap();
+ let id = ItemId(r["id"].as_u64().unwrap());
+ assert_eq!(s.doc().unwrap().doc.find(id).unwrap().spread, SpreadRef::Parent(0));
+ s.execute("line.create", &json!({"spread": parent, "a": [0, 0], "b": [50, 50]})).unwrap();
+ assert_eq!(s.doc().unwrap().doc.parents[0].items.len(), 2);
+
+ // Each of these used to land on the first body page.
+ for bad in
+ [json!("A-Parent"), json!("0"), json!(-1), json!(1.5), json!({"kind": "master", "index": 0}), json!({"kind": "parent"}), json!([0])]
+ {
+ assert!(s.execute("frame.create", &json!({"spread": bad, "rect": [0, 0, 100, 100]})).is_err(), "frame.create spread {bad}");
+ assert!(s.execute("line.create", &json!({"spread": bad, "a": [0, 0], "b": [50, 50]})).is_err(), "line.create spread {bad}");
+ }
+ assert!(s.doc().unwrap().doc.spreads[0].items.is_empty());
+ }
+ }