DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Merge PR #307
Brandon Thomasf62985064d6f
diff --cc crates/engine/src/cmd/layout.rsindex 632b6655e,bf967483d..fe06ad3ee--- a/crates/engine/src/cmd/layout.rs+++ b/crates/engine/src/cmd/layout.rs@@@ -1126,111 -926,28 +1132,135 @@@ mod adjust_testslet b2 = s.doc().unwrap().doc.item(designcraft_doc::ItemId(id)).unwrap().bounds();assert_eq!(b, b2);}++ #[test]+ fn parent_margins_change_the_parent_and_follow_unoverridden_pages() {+ let mut s = Session::new();+ s.execute("file.new", &json!({"pages": 3, "facingPages": false})).unwrap();+ // Page 3 has its own margins (an override); pages 1 and 2 still follow the parent.+ s.execute("layout.marginsAndColumns", &json!({"pages": [2], "margins": 100})).unwrap();+ let parent_before = s.doc().unwrap().doc.parents[0].pages[0].margins;+ assert_eq!(parent_before, Margins::uniform(36.0));+ s.execute("layout.marginsAndColumns", &json!({"parent": 0, "margins": 60, "columns": 2})).unwrap();+ let d = &s.doc().unwrap().doc;+ // The parent page itself changed (this is what was drawn on screen).+ assert_eq!(d.parents[0].pages[0].margins, Margins::uniform(60.0));+ assert_eq!(d.parents[0].pages[0].columns.count, 2);+ // Pages that matched the parent follow; the overridden page keeps its margins.+ assert_eq!(d.page(0).unwrap().margins, Margins::uniform(60.0));+ assert_eq!(d.page(1).unwrap().margins, Margins::uniform(60.0));+ assert_eq!(d.page(2).unwrap().margins, Margins::uniform(100.0));+ // An unknown parent is an error, not a silent no-op.+ assert!(s.execute("layout.marginsAndColumns", &json!({"parent": 9, "margins": 10})).is_err());+ }+}++#[cfg(test)]+mod section_side_tests {+ use serde_json::json;++ use crate::Session;++ /// Each spread's pages as (name, side), side `L`, `R` or `S`.+ fn spreads(s: &Session) -> Vec<Vec<(String, char)>> {+ let d = &s.doc().unwrap().doc;+ let mut abs = 0;+ d.spreads+ .iter()+ .map(|sp| {+ sp.pages+ .iter()+ .map(|p| {+ let side = match p.side {+ designcraft_doc::PageSide::Left => 'L',+ designcraft_doc::PageSide::Right => 'R',+ designcraft_doc::PageSide::Single => 'S',+ };+ abs += 1;+ (d.page_name(abs - 1), side)+ })+ .collect()+ })+ .collect()+ }++ fn pages(v: &[&[(&str, char)]]) -> Vec<Vec<(String, char)>> {+ v.iter().map(|sp| sp.iter().map(|(n, c)| (n.to_string(), *c)).collect()).collect()+ }++ #[test]+ fn a_section_starting_at_one_on_a_left_page_moves_it_to_the_right() {+ // Chris Nielsen's "Numbering Page Sections" (7:03): with the spread 8–9 selected, the+ // section starts on page 8, which becomes a lone right-hand page 1; the pages after it+ // shuffle and their objects go with them. Undo puts them back.+ let mut s = Session::new();+ s.execute("file.new", &json!({"width": 612, "height": 792, "facingPages": true, "pages": 10})).unwrap();+ let id = s.execute("frame.create", &json!({"spread": 4, "rect": [700, 100, 800, 200], "content": "text", "text": "TOPIC"})).unwrap()["id"]+ .as_u64()+ .unwrap();+ let page_of = |s: &Session| {+ let d = &s.doc().unwrap().doc;+ d.page_of_item(designcraft_doc::ItemId(id)).unwrap()+ };+ assert_eq!(page_of(&s), 8, "page 9, the right page of the spread 8–9");+ let before = spreads(&s);+ s.execute("layout.section", &json!({"page": 8, "startNumber": 1})).unwrap();+ assert_eq!(+ spreads(&s),+ pages(&[+ &[("1", 'R')],+ &[("2", 'L'), ("3", 'R')],+ &[("4", 'L'), ("5", 'R')],+ &[("6", 'L'), ("7", 'R')],+ &[("1", 'R')],+ &[("2", 'L'), ("3", 'R')],+ ])+ );+ assert_eq!(page_of(&s), 8, "the object stays on its page, now the left page of a spread");+ s.execute("edit.undo", &json!({})).unwrap();+ assert_eq!(spreads(&s), before);+ // On a right page (the video's page 9) nothing moves; an even start on a right page moves+ // it to the left.+ s.execute("layout.section", &json!({"page": 9, "startNumber": 1})).unwrap();+ assert_eq!(spreads(&s)[4], pages(&[&[("8", 'L'), ("1", 'R')]])[0]);+ s.execute("layout.section", &json!({"page": 9, "startNumber": 2})).unwrap();+ assert_eq!(spreads(&s)[4..], pages(&[&[("8", 'L')], &[("2", 'L'), ("3", 'R')]]));+ // Removing the section numbers the pages on from 9 again.+ s.execute("layout.section", &json!({"page": 9, "remove": true})).unwrap();+ assert_eq!(spreads(&s), before);+ }++ #[test]+ fn single_sided_documents_keep_their_pages() {+ let mut s = Session::new();+ s.execute("file.new", &json!({"facingPages": false, "pages": 4})).unwrap();+ s.execute("layout.section", &json!({"page": 2, "startNumber": 1})).unwrap();+ let sp = spreads(&s);+ assert_eq!(sp.len(), 4);+ assert!(sp.iter().all(|p| p.len() == 1 && p[0].1 == 'S'), "{sp:?}");+ }}++ #[cfg(test)]+ mod spread_rotation_input_tests {+ use serde_json::json;++ #[test]+ fn quarter_turns_work_and_other_angles_are_rejected_without_mutation() {+ let mut s = crate::Session::new();+ s.execute("file.new", &json!({})).unwrap();+ let rotation = |s: &crate::Session| s.doc().unwrap().doc.spreads[0].pages[0].view_rotation;+ for bad in [json!(45), json!(-45), json!(91), json!("90"), json!(0.5)] {+ assert!(s.execute("layout.rotateSpreadView", &json!({"angle": bad})).is_err(), "{bad}");+ assert_eq!(rotation(&s), 0, "invalid input must not change the view");+ }+ s.execute("layout.rotateSpreadView", &json!({"angle": 90})).unwrap();+ assert_eq!(rotation(&s), 1);+ s.execute("layout.rotateSpreadView", &json!({"angle": -90})).unwrap();+ assert_eq!(rotation(&s), 0);+ s.execute("layout.rotateSpreadView", &json!({"angle": 180})).unwrap();+ assert_eq!(rotation(&s), 2);+ s.execute("layout.rotateSpreadView", &json!({"angle": 0})).unwrap();+ assert_eq!(rotation(&s), 0, "zero resets the spread view");+ }+ }