DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Merge remote-tracking branch 'origin/main' into text-frame-options-layout
moppeeer1fd7cff87ef3
diff --cc crates/compose/src/tests.rsindex de4510ca5,5d6989de5..f1e6f04e3--- a/crates/compose/src/tests.rs+++ b/crates/compose/src/tests.rs@@@ -7155,52 -7431,44 +7431,90 @@@ fn anchored_object_wraps_its_own_storysassert!((o.origin.y - top).abs() < 1e-6);}+/// Vertical Justification › Justify with a Paragraph Spacing Limit: the space goes between the+/// paragraphs (up to the limit each), the lines of a paragraph keep their leading; past the limit+/// the rest spreads over every line. Without a limit it spreads over every line.+#[test]+fn justify_paragraph_spacing_limit() {+ let text = "One line.+Two+Three";+ let baselines = |limit: f64| {+ let (mut d, sid, fid) = doc_with(text, Rect::new(0.0, 0.0, 300.0, 400.0), ParaAttrs::default());+ let o = &mut d.item_mut(fid).unwrap().text_frame_mut().unwrap().options;+ o.vertical_justification = designcraft_doc::VerticalJustification::Justify;+ o.vj_paragraph_spacing_limit = limit;+ let cs = compose_story(&d, sid, &ComposeOptions::default());+ all_lines(&cs).iter().map(|l| l.baseline).collect::<Vec<_>>()+ };+ let (none, big, small) = (baselines(0.0), baselines(1000.0), baselines(20.0));+ assert_eq!(none.len(), 3);+ let gaps = |b: &[f64]| (b[1] - b[0], b[2] - b[1]);+ // No limit: equal steps. A big limit: all of it between paragraphs (here every line is one).+ assert!((gaps(&none).0 - gaps(&none).1).abs() < 1e-6, "{none:?}");+ assert!((gaps(&big).0 - gaps(&big).1).abs() < 1e-6, "{big:?}");+ // A small limit: each paragraph gap gets at most 20 pt more than the even share's base.+ assert!((none[2] - big[2]).abs() < 1e-6 && (none[2] - small[2]).abs() < 1e-6, "the last line stays at the bottom");+ // A paragraph of two lines keeps its leading when the limit absorbs the space.+ let (mut d, sid, fid) = doc_with(+ &[LOREM, "End."].join(+ "+",+ ),+ Rect::new(0.0, 0.0, 300.0, 600.0),+ ParaAttrs::default(),+ );+ let o = &mut d.item_mut(fid).unwrap().text_frame_mut().unwrap().options;+ o.vertical_justification = designcraft_doc::VerticalJustification::Justify;+ o.vj_paragraph_spacing_limit = 10_000.0;+ let cs = compose_story(&d, sid, &ComposeOptions::default());+ let lines = all_lines(&cs);+ let first: Vec<f64> = lines.iter().filter(|l| l.para == 0).map(|l| l.baseline).collect();+ assert!(first.len() > 2, "LOREM runs over several lines");+ let lead = first[1] - first[0];+ assert!(first.windows(2).all(|w| (w[1] - w[0] - lead).abs() < 1e-6), "the paragraph's lines keep their leading: {first:?}");+ let last = lines.iter().find(|l| l.para == 1).unwrap().baseline;+ assert!(last - first[first.len() - 1] > lead + 50.0, "the space went between the paragraphs");+}++ /// Each object's wrap pushes the anchors after it down, so where each object lands depends on the+ /// wraps of all those before it; every object still ends up wrapping the lines it hangs over.+ #[test]+ fn chained_anchored_objects_all_wrap_their_own_storys_lines() {+ const OBJECTS: usize = 8;+ let text = [LOREM; 12].join(" ");+ let (mut d, sid, _) = doc_with(&text, Rect::new(0.0, 0.0, 300.0, 3000.0), ParaAttrs::default());+ let plain = compose_story(&d, sid, &ComposeOptions::default());+ let lines = all_lines(&plain);+ assert!(lines.len() > 2 + 6 * OBJECTS);+ let left = lines[0].x0;+ // One object every six lines, each hanging from its anchor's baseline over the lines below;+ // inserted last first so the earlier offsets still hold.+ let ats: Vec<usize> = (0..OBJECTS).map(|k| lines[2 + 6 * k].range.start + 3).collect();+ for &at in ats.iter().rev() {+ anchor_box(&mut d, sid, at, 0.0, 0.0, true, WrapMode::BoundingBox);+ }+ let cs = compose_story(&d, sid, &ComposeOptions::default());+ assert!(!cs.is_overset());+ let lines = all_lines(&cs);+ let anchors: Vec<usize> = d.story(sid).unwrap().text.match_indices(designcraft_doc::OBJECT_MARK).map(|(i, _)| i).collect();+ let objects = &cs.frames[0].objects;+ assert_eq!(objects.len(), OBJECTS);+ for o in objects {+ let anchor_line = lines.iter().find(|l| l.range.contains(&anchors[o.index])).unwrap();+ assert!((o.origin.y - anchor_line.baseline).abs() < 1e-6, "object {} at {}, anchor line at {}", o.index, o.origin.y, anchor_line.baseline);+ let under = lines_in_band(&cs, o.origin.y, o.origin.y + 60.0);+ assert!(under.len() >= 2);+ assert!(+ under.iter().all(|l| l.x0 >= left + 100.0 - 1e-6),+ "object {} at {}: {:?}",+ o.index,+ o.origin.y,+ under.iter().map(|l| (l.baseline, l.x0)).collect::<Vec<_>>()+ );+ }+ }+#[test]fn anchored_object_without_wrap_leaves_layout_unchanged() {let long = [LOREM; 3].join(" ");