Merge pull request #6759 from RickiNano/fix/6757-unequal-formats-linear

Layout: a paragraph over many pages lays out in linear time (#6757)
This commit is contained in:
Dylan M. Taylor authored and GitHub committed 2026-11-16 07:38:06 -05:00
commit cdf146cabd
2 files changed
+27 -5

No files matched your search

+11 -5
View File
@@ -718,12 +718,18 @@ pub(crate) fn place_objects(ctx: &mut Ctx, req: &ObjectPlacement) -> (Vec<Placed
// (#4250). Inline objects go with their own line.
let first = l0 == 0;
let late = at.late.filter(|l| !l.placed.is_empty());
let late_lines = late.map(LateFloats::lines_here).unwrap_or_default();
for li in 0..pl.lines.len() {
// A later piece looks only at its own lines and those holding the anchors of the late floats
// it draws, in order: walking every line of the paragraph for each piece made a paragraph of
// many pages quadratic (#6757).
let (piece, mut late_lines) =
if first { (0..pl.lines.len(), Vec::new()) } else { (l0..l1.min(pl.lines.len()), late.map(LateFloats::lines_here).unwrap_or_default()) };
late_lines.retain(|li| !piece.contains(li));
late_lines.sort_unstable();
late_lines.dedup();
let (before, after) = late_lines.split_at(late_lines.partition_point(|li| *li < piece.start));
for li in before.iter().copied().chain(piece).chain(after.iter().copied()) {
crate::work::steps(1);
let in_piece = (l0..l1).contains(&li);
if !in_piece && !first && !late_lines.contains(&li) {
continue;
}
let Some(line) = pl.lines.get(li) else { continue };
for k in line.c0..line.c1 {
let Some(c) = pl.clusters.get(k) else { continue };
+16
View File
@@ -223,6 +223,22 @@ fn selecting_a_long_endnote_takes_linear_time() {
assert_linear_steps("endnote selection", 20, laid, |(d, l, (a, b))| drop(std::hint::black_box(l.selection_rects(d, a, b, 0))));
}
/// One paragraph of `n` lines (a line break after each word), so it runs over many pages.
fn many_lines(n: usize) -> Document {
let mut d = Document::from_text("");
d.body = vec![wordcraft_doc::para_block(Paragraph::with_text(&"x\n".repeat(n), CharProps::default()))];
d
}
/// The piece of a long paragraph on each page looked through every line of the paragraph for the
/// pictures and shapes to draw with it (#6757).
#[test]
fn a_paragraph_over_many_pages_lays_out_in_linear_time() {
let pages = lay(&many_lines(SCALE * 400), &LayoutOptions::default()).pages.len();
assert!(pages > 40, "the paragraph runs over many pages: {pages}");
assert_layout_linear("a paragraph over many pages", 400, many_lines, &LayoutOptions::default());
}
/// A table of `n` rows of 60 cells, each row exactly half a point high, so every cell clips its
/// (empty) paragraph and the whole table fits on the first page.
fn clipped_table(n: usize) -> Document {