Skip to content

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

Integration: #384 rebreak check (from #1086 by moppeeer)

moppeeera254f0e6e094
diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs
index 249603eb9..d736de9c9 100644
--- a/crates/compose/src/lib.rs
+++ b/crates/compose/src/lib.rs
@@ -1574,7 +1574,10 @@ fn compose_with_db(doc: &Document, story: &Story, frames: &[FrameSpec], opts: &C
// paragraph from here, starting with this line's segments as they are. A pass
// re-breaks at its first line only for a new line, whose segments it carries, so
// every re-break makes progress; their number is capped too.
- if (k > 0 || !continues) && rebreaks < 256 && slots.get(k).is_some_and(|&(a, b)| ((b - a) - (x1 - x0)).abs() > 0.5) {
+ // Past the estimated slots the breaker assumes the whole column. Check that
+ // fallback too: narrow wrap segments may need more lines than the estimate.
+ let (estimated_x0, estimated_x1) = slots.get(k).copied().unwrap_or((col.x0, col.x1));
+ if (k > 0 || !continues) && rebreaks < 256 && ((estimated_x1 - estimated_x0) - (x1 - x0)).abs() > 0.5 {
rebreaks += 1;
carry.insert(0, (x0, x1));
g0 = s;
diff --git a/crates/compose/src/tests.rs b/crates/compose/src/tests.rs
index ad22f577b..0e4553441 100644
--- a/crates/compose/src/tests.rs
+++ b/crates/compose/src/tests.rs
@@ -4,6 +4,34 @@ use designcraft_geom::Rect;
use super::*;
+#[test]
+fn long_wrapped_paragraph_rechecks_width_after_estimated_slots_end() {
+ let text="Distinctive shapes and warm colours set the rhythm of the page. The story flows around the illustration while the frame keeps its original dimensions. ".repeat(14);
+ let (mut d, sid, _) = doc_with(&text, Rect::new(36.0, 36.0, 576.0, 756.0), ParaAttrs::default());
+ d.story_mut(sid).unwrap().format_chars(0..text.len(), |f| {
+ f.over.font_family = Some("Source Serif 4".into());
+ f.over.size = Some(13.0);
+ f.over.leading = Some(designcraft_doc::Leading::Points(17.0));
+ });
+ add_wrap_objects(&mut d, &[(Rect::new(180.0, 135.0, 440.0, 568.3333333333334), [13.5; 4], WrapMode::Contour)]);
+ let composed = compose_story(&d, sid, &ComposeOptions::default());
+ let lines = &composed.frames[0].lines;
+ assert!(lines.iter().any(|l| l.baseline > 450.0 && l.baseline < 560.0));
+ for line in lines {
+ for glyph in line.glyphs.iter().filter(|g| g.visible && text.as_bytes().get(g.byte).is_some_and(|b| !b.is_ascii_whitespace())) {
+ assert!(
+ glyph.x + glyph.adv <= line.x1 + 0.5,
+ "glyph {} ends at {} outside the segment {}..{} on baseline {}",
+ glyph.byte,
+ glyph.x + glyph.adv,
+ line.x0,
+ line.x1,
+ line.baseline
+ );
+ }
+ }
+}
+
const LOREM: &str = "Typography is the art and technique of arranging type to make written language legible, readable and appealing when displayed. \
The arrangement of type involves selecting typefaces, point sizes, line lengths, line spacing, and letter spacing, and adjusting the space between pairs of letters.";