mirror of
https://github.com/storytold/wordcraft.git
synced 2026-12-03 22:40:27 +00:00
Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4d124845e9 | ||
|
|
08eb9ccbb2 | ||
|
|
859e03246f | ||
|
|
0c7ea7278c | ||
|
|
8619e257a7 |
@@ -652,6 +652,39 @@ impl<'a> Flow<'a> {
|
||||
true
|
||||
}
|
||||
|
||||
/// Keep with next for the end of a paragraph begun in an earlier column (#6829): in columns
|
||||
/// being balanced, the piece holding its last line must leave room below for the next
|
||||
/// paragraph to start (as [`Self::keep_together`] asks of a paragraph's start), or its last
|
||||
/// lines (two under widow/orphan control) go over with that paragraph, as in Word. All of
|
||||
/// them going over returns `l0`, and the caller moves on even from a column's top: the
|
||||
/// balancing pass then runs onto another page and the columns are tried taller. Nothing
|
||||
/// moves in the full-height pass that is the balancing's fallback, nor when the paragraph's
|
||||
/// tail and the next paragraph's start could not fit a full column.
|
||||
fn kept_tail_end(&self, ctx: &mut Ctx, pb: &PageBuilder, rooms: Rooms, fit: &Fit) -> usize {
|
||||
let (rp, n, l0) = (&self.pl.rp, self.n(), self.l0);
|
||||
// Not in the full-height fallback pass, which must keep the original layout.
|
||||
let column = pb.uncapped_bottom() - pb.top;
|
||||
let balancing = pb.balance.is_some_and(|(page, h)| page + 1 == pb.pages.len() && h + 0.01 < column);
|
||||
if l0 == 0 || fit.l1 < n || fit.forced.is_some() || !rp.keep_next || !balancing || pb.web {
|
||||
return fit.l1;
|
||||
}
|
||||
let (Some(first), Some(last)) = (self.pl.lines.get(l0), self.pl.lines.last()) else { return fit.l1 };
|
||||
let tail = last.top + last.height - first.top + rooms.bottom;
|
||||
let need = tail
|
||||
+ gap_after(ctx, self.block, rp, self.label.is_some())
|
||||
+ next_first_line(ctx, self.block, self.width, 0, pb.next_starts_page, &mut ctx.counters.clone());
|
||||
// A need no column can hold is left alone, as `keep_together` leaves it.
|
||||
if pb.y + need <= pb.bottom + 0.01 || need > column + 0.01 {
|
||||
return fit.l1;
|
||||
}
|
||||
let mut l1 = n.saturating_sub(if rp.widow_control { 2 } else { 1 }).max(l0);
|
||||
// A row (text both sides of a floating object) stays together (`settled_end`).
|
||||
while l1 > l0 && self.pl.lines.get(l1).is_some_and(|l| l.beside) {
|
||||
l1 -= 1;
|
||||
}
|
||||
l1
|
||||
}
|
||||
|
||||
/// The end of the lines on this page after widow/orphan control, and keeping the rows beside
|
||||
/// floating objects together.
|
||||
/// Also says whether, balancing from Word 2013's layout, widow/orphan control alone moved every
|
||||
@@ -1081,10 +1114,13 @@ fn place_para_in_flow(ctx: &mut Ctx, pb: &mut PageBuilder, p: &Paragraph, block:
|
||||
let mut fit = f.fit_lines(ctx, pb, ¬es, lead);
|
||||
let (end, widowed) = f.settled_end(ctx, pb, &fit);
|
||||
fit.l1 = end;
|
||||
fit.l1 = f.kept_tail_end(ctx, pb, rooms, &fit);
|
||||
let kept_over = fit.l1 < end;
|
||||
// On a fresh column an unfitting line can overflow. Strict widow control instead moves
|
||||
// the lines on without emitting anything and must release their late floats as well.
|
||||
let overflow = end == f.l0 && f.fresh(pb) && !widowed;
|
||||
if f.settle_late(pb, end, overflow) {
|
||||
// the lines on without emitting anything and must release their late floats as well, and
|
||||
// so does a kept tail going over to the next paragraph.
|
||||
let overflow = fit.l1 == f.l0 && f.fresh(pb) && !widowed && !kept_over;
|
||||
if f.settle_late(pb, fit.l1, overflow) {
|
||||
continue;
|
||||
}
|
||||
if fit.l1 == f.l0 {
|
||||
@@ -1226,6 +1262,24 @@ fn space_after_kept(ctx: &Ctx, block: usize, rp: &wordcraft_doc::resolve::Resolv
|
||||
after
|
||||
}
|
||||
|
||||
/// The room the space after body paragraph `block` adds between it and the paragraph after it,
|
||||
/// beyond that paragraph's own space before (which [`next_first_line`] counts): the merged gap
|
||||
/// (the larger of the two, ISO/IEC 29500-1 §17.3.1.9), or both when they are summed. Nothing when
|
||||
/// contextual spacing or automatic spacing between list items drops it.
|
||||
fn gap_after(ctx: &Ctx, block: usize, rp: &wordcraft_doc::resolve::ResolvedPara, listed: bool) -> f32 {
|
||||
let after = space_after_kept(ctx, block, rp, listed);
|
||||
if after <= 0.0 {
|
||||
return 0.0;
|
||||
}
|
||||
let Some(Block::Para(next)) = ctx.doc.body.get(block + 1).map(|b| &**b) else { return after };
|
||||
let nrp = ctx.doc.styles.resolve_para(&next.props);
|
||||
// The next paragraph's contextual spacing, and automatic spacing between two list items,
|
||||
// drop its own space before.
|
||||
let dropped = (nrp.style == rp.style && nrp.contextual_spacing) || (listed && nrp.before_auto && ctx.has_label(next));
|
||||
let before = if dropped { 0.0 } else { nrp.space_before.max(0.0) };
|
||||
if ctx.doc.settings.sum_paragraph_spacing { after } else { (after - before).max(0.0) }
|
||||
}
|
||||
|
||||
/// The room the paragraphs after `block` need on the page for keep with next: enough of the
|
||||
/// next one to start there — its first line, two of its lines with widow/orphan control (it
|
||||
/// doesn't leave a single line at the bottom of a page; it moves over whole instead), all three
|
||||
|
||||
@@ -12924,6 +12924,211 @@ fn columns_before_a_continuous_break_are_balanced() {
|
||||
}
|
||||
}
|
||||
|
||||
/// #6829's case: Letter page, three columns, exact 14 pt lines. 135 one-line filler paragraphs,
|
||||
/// then P (5 lines, kept with the next) with `space_after` after it, then Q, then a section after
|
||||
/// Q starting continuously, so the columns of the section ending at Q are balanced.
|
||||
fn continued_tail_doc(space_after: f32) -> (Document, usize) {
|
||||
use wordcraft_doc::props::LineSpacing;
|
||||
use wordcraft_doc::section::{SectionProps, SectionStart};
|
||||
let lb = wordcraft_doc::para::LINE_BREAK;
|
||||
let filler = 135;
|
||||
let mut text: Vec<String> = (0..filler).map(|i| format!("F{i}")).collect();
|
||||
text.push("P1".into());
|
||||
text.push("Q".into());
|
||||
text.push("After".into());
|
||||
let mut d = Document::from_text(&text.join("\n"));
|
||||
for block in &mut d.body {
|
||||
if let Block::Para(para) = Arc::make_mut(block) {
|
||||
para.props.line_spacing = Some(LineSpacing::Exactly(14.0));
|
||||
para.props.space_before = Some(0.0);
|
||||
para.props.space_after = Some(0.0);
|
||||
}
|
||||
}
|
||||
let p = d.para_mut(StoryRef::Body, &Path::top(filler)).unwrap();
|
||||
p.insert_text(2, &format!("{lb}P2{lb}P3{lb}P4{lb}P5"), &Default::default()).unwrap();
|
||||
p.props.keep_next = Some(true);
|
||||
p.props.space_after = Some(space_after);
|
||||
let mut s = SectionProps::default();
|
||||
s.columns.count = 3;
|
||||
d.para_mut(StoryRef::Body, &Path::top(filler + 1)).unwrap().section = Some(Box::new(s));
|
||||
d.last_section.start = SectionStart::Continuous;
|
||||
(d, filler)
|
||||
}
|
||||
|
||||
/// Body paragraph `block`'s pieces in `l`: (page, x, top, line count).
|
||||
fn pieces_of(l: &DocLayout, block: usize) -> Vec<(usize, f32, f32, usize)> {
|
||||
l.pages
|
||||
.iter()
|
||||
.enumerate()
|
||||
.flat_map(|(pi, pg)| {
|
||||
pg.items.iter().filter_map(move |it| match it {
|
||||
Placed::Lines { story: StoryRef::Body, path, l0, l1, x, y, .. } if path.0[..] == [block as u32] => Some((pi, *x, *y, l1 - l0)),
|
||||
_ => None,
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// #6829: in balanced columns, the last lines of a paragraph begun on the page before stay with
|
||||
/// the paragraph after it when the paragraph is kept with the next (Word 16.0, compatibility
|
||||
/// mode 15: three columns, exact 14 pt lines, the tail P4–P5 and Q all in column 1).
|
||||
#[test]
|
||||
fn balanced_columns_keep_a_continued_tail_with_the_next_paragraph() {
|
||||
crate::test_fonts();
|
||||
let (d, filler) = continued_tail_doc(0.0);
|
||||
let l = lay(&d);
|
||||
let pieces = pieces_of(&l, filler);
|
||||
assert_eq!(pieces.len(), 2, "P begins on page 1 and ends on page 2: {pieces:?}");
|
||||
let (tail_page, tail_x, tail_top, tail_lines) = pieces[1];
|
||||
assert_eq!((pieces[0].0, tail_page, tail_lines), (0, 1, 2), "P4 and P5 on page 2: {pieces:?}");
|
||||
let q = para_boxes(&l)[filler + 1];
|
||||
assert_eq!(q.0, 1, "Q on page 2: {q:?}");
|
||||
assert!((q.1 - tail_x).abs() < 0.5, "Q in the column of P4 and P5: Q {q:?}, P {pieces:?}");
|
||||
assert!((q.2 - (tail_top + 28.0)).abs() < 0.5, "Q right under P5: Q {q:?}, P {pieces:?}");
|
||||
}
|
||||
|
||||
/// #6829: the space after the continued paragraph counts too. With 28 pt after P, leaving room
|
||||
/// for P4–P5 and Q's first line alone takes Q past the end of the column, and the tail stays
|
||||
/// behind while Q moves on. The tail must go over with Q, and the columns grow to hold both.
|
||||
#[test]
|
||||
fn balanced_columns_keep_a_continued_tail_with_the_space_after_it() {
|
||||
crate::test_fonts();
|
||||
let (d, filler) = continued_tail_doc(28.0);
|
||||
let l = lay(&d);
|
||||
let pieces = pieces_of(&l, filler);
|
||||
assert_eq!(pieces.len(), 2, "P begins on page 1 and ends on page 2: {pieces:?}");
|
||||
let (tail_page, tail_x, tail_top, tail_lines) = pieces[1];
|
||||
assert_eq!((pieces[0].0, tail_page, tail_lines), (0, 1, 2), "P4 and P5 on page 2: {pieces:?}");
|
||||
let q = para_boxes(&l)[filler + 1];
|
||||
assert_eq!(q.0, 1, "Q on page 2: {q:?}");
|
||||
assert!((q.1 - tail_x).abs() < 0.5, "Q in the column of P4 and P5: Q {q:?}, P {pieces:?}");
|
||||
assert!((q.2 - (tail_top + 28.0 + 28.0)).abs() < 0.5, "Q under P5's space after: Q {q:?}, P {pieces:?}");
|
||||
}
|
||||
|
||||
/// #6829: the lines going over with the next paragraph keep their row together. Text on the far
|
||||
/// side of a floating object is part of the same row (`settled_end`), and the end
|
||||
/// `kept_tail_end` picks must not split it: the whole row goes over with the next paragraph.
|
||||
#[test]
|
||||
fn balanced_columns_keep_a_continued_tail_row_together() {
|
||||
crate::test_fonts();
|
||||
use wordcraft_doc::para::{Anchor, Float, Wrap};
|
||||
use wordcraft_doc::props::LineSpacing;
|
||||
use wordcraft_doc::section::{SectionProps, SectionStart};
|
||||
let lb = wordcraft_doc::para::LINE_BREAK;
|
||||
let filler = 135;
|
||||
let mut text: Vec<String> = (0..filler).map(|i| format!("F{i}")).collect();
|
||||
text.push("P1".into());
|
||||
text.push("Q1".into());
|
||||
text.push("After".into());
|
||||
let mut d = Document::from_text(&text.join("\n"));
|
||||
for block in &mut d.body {
|
||||
if let Block::Para(para) = Arc::make_mut(block) {
|
||||
para.props.line_spacing = Some(LineSpacing::Exactly(14.0));
|
||||
para.props.space_before = Some(0.0);
|
||||
para.props.space_after = Some(0.0);
|
||||
}
|
||||
}
|
||||
let p = d.para_mut(StoryRef::Body, &Path::top(filler)).unwrap();
|
||||
p.insert_text(2, &format!("{lb}P2{lb}P3{lb}word word word word word"), &Default::default()).unwrap();
|
||||
p.props.keep_next = Some(true);
|
||||
let q = d.para_mut(StoryRef::Body, &Path::top(filler + 1)).unwrap();
|
||||
q.insert_text(2, &format!("{lb}Q2{lb}Q3{lb}Q4{lb}Q5"), &Default::default()).unwrap();
|
||||
let mut s = SectionProps::default();
|
||||
s.columns.count = 3;
|
||||
d.para_mut(StoryRef::Body, &Path::top(filler + 1)).unwrap().section = Some(Box::new(s));
|
||||
d.last_section.start = SectionStart::Continuous;
|
||||
// A float in the middle of the column, anchored at P's tail: each of its first rows holds a
|
||||
// word on each side of it.
|
||||
let f = Float { wrap: Wrap::Square, h_rel: Anchor::Column, v_rel: Anchor::Paragraph, x: 36.0, ..Default::default() };
|
||||
d.insert_object(&Pos::body(filler, 9), rect_shape(60.0, 42.0, f), &Default::default()).unwrap();
|
||||
let l = lay(&d);
|
||||
let mut continued = false;
|
||||
let mut row = false;
|
||||
for (pi, pg) in l.pages.iter().enumerate() {
|
||||
for it in &pg.items {
|
||||
let Placed::Lines { story: StoryRef::Body, path, para, l0, l1, .. } = it else { continue };
|
||||
if path.0[..] != [filler as u32] || *l0 == 0 {
|
||||
continue;
|
||||
}
|
||||
continued = true;
|
||||
if pi == 1 {
|
||||
row |= para.lines[*l0..].iter().any(|l| l.beside);
|
||||
}
|
||||
if *l1 < para.lines.len() {
|
||||
assert!(!para.lines[*l1].beside, "a piece ends in the middle of a row: l0 {l0}, l1 {l1} of {}", para.lines.len());
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(continued, "P continues on page 2");
|
||||
assert!(row, "the float splits a row beside the tail on page 2");
|
||||
assert_eq!(para_boxes(&l)[filler + 1].0, 1, "Q on page 2");
|
||||
}
|
||||
|
||||
/// Lines of body paragraph `block` placed in `l`, over all its pieces.
|
||||
fn placed_lines(l: &DocLayout, block: usize) -> usize {
|
||||
pieces_of(l, block).iter().map(|p| p.3).sum()
|
||||
}
|
||||
|
||||
/// #6829: when the tail of a continued paragraph and the next paragraph could never fit one
|
||||
/// column (a 60-line Q kept together), the tail stays where it is: nothing is pushed over to a
|
||||
/// third page and the columns are balanced on page 2 as without the rule.
|
||||
#[test]
|
||||
fn a_continued_tail_that_cannot_fit_with_the_next_paragraph_stays_in_place() {
|
||||
crate::test_fonts();
|
||||
let lb = wordcraft_doc::para::LINE_BREAK;
|
||||
let (mut d, filler) = continued_tail_doc(0.0);
|
||||
let q = d.para_mut(StoryRef::Body, &Path::top(filler + 1)).unwrap();
|
||||
let more: String = (2..=60).map(|i| format!("{lb}Q{i}")).collect();
|
||||
q.insert_text(1, &more, &Default::default()).unwrap();
|
||||
q.props.keep_lines = Some(true);
|
||||
let l = lay(&d);
|
||||
assert_eq!(l.pages.len(), 2, "everything on two pages");
|
||||
assert_eq!(placed_lines(&l, filler), 5, "P is placed whole");
|
||||
assert_eq!(placed_lines(&l, filler + 1), 60, "Q is placed whole");
|
||||
assert!(pieces_of(&l, filler + 1).iter().all(|p| p.0 == 1), "Q on page 2");
|
||||
assert!(pieces_of(&l, filler)[1..].iter().all(|p| p.0 == 1), "P's tail on page 2");
|
||||
}
|
||||
|
||||
/// #6829: when the rule cannot be satisfied at any height, the balancing's fallback (the original
|
||||
/// full-height columns) keeps the original layout and every paragraph is placed: a 49-line
|
||||
/// paragraph kept with the next fills column 1 of page 2, then Q and 91 one-line paragraphs
|
||||
/// fill the page.
|
||||
#[test]
|
||||
fn balancing_fallback_places_every_paragraph() {
|
||||
crate::test_fonts();
|
||||
use wordcraft_doc::props::LineSpacing;
|
||||
use wordcraft_doc::section::{SectionProps, SectionStart};
|
||||
let lb = wordcraft_doc::para::LINE_BREAK;
|
||||
let (filler, after) = (135, 91);
|
||||
let mut text: Vec<String> = (0..filler).map(|i| format!("F{i}")).collect();
|
||||
text.push("P1".into());
|
||||
text.push("Q".into());
|
||||
text.extend((0..after).map(|i| format!("A{i}")));
|
||||
text.push("Next".into());
|
||||
let mut d = Document::from_text(&text.join("\n"));
|
||||
for block in &mut d.body {
|
||||
if let Block::Para(para) = Arc::make_mut(block) {
|
||||
para.props.line_spacing = Some(LineSpacing::Exactly(14.0));
|
||||
para.props.space_before = Some(0.0);
|
||||
para.props.space_after = Some(0.0);
|
||||
}
|
||||
}
|
||||
let p = d.para_mut(StoryRef::Body, &Path::top(filler)).unwrap();
|
||||
let more: String = (2..=49).map(|i| format!("{lb}P{i}")).collect();
|
||||
p.insert_text(2, &more, &Default::default()).unwrap();
|
||||
p.props.keep_next = Some(true);
|
||||
let last = filler + 1 + after;
|
||||
let mut s = SectionProps::default();
|
||||
s.columns.count = 3;
|
||||
d.para_mut(StoryRef::Body, &Path::top(last)).unwrap().section = Some(Box::new(s));
|
||||
d.last_section.start = SectionStart::Continuous;
|
||||
let l = lay(&d);
|
||||
for block in 0..=last + 1 {
|
||||
let want = if block == filler { 49 } else { 1 };
|
||||
assert_eq!(placed_lines(&l, block), want, "block {block} is placed");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn columns_before_a_page_break_or_a_column_break_are_not_balanced() {
|
||||
crate::test_fonts();
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user