Split paragraphs at line boundaries across pages
This commit is contained in:
1 parent
daf9f5c57e
commit
b7b75fbff1
8 files changed
+263
-77
No files matched your search
@@ -856,6 +856,77 @@ fn bottom_float_area(page: &Page, text_float_sep: i64) -> i64 {
|
||||
|
||||
/// TeX `\topfraction`: a top float taller than this share of the page keeps
|
||||
/// the rest of the page free of body text.
|
||||
|
||||
/// Place a paragraph into the vertical flow, splitting it at line boundaries
|
||||
/// when it cannot fit on the current page. TeX breaks between lines; the
|
||||
/// scrolled page builder used to move whole paragraphs and silently overflow.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn place_paragraph_flow(
|
||||
pages: &mut Vec<Page>,
|
||||
current: &mut Page,
|
||||
used: &mut i64,
|
||||
insertion_height: &mut i64,
|
||||
paragraph: &Paragraph,
|
||||
page_goal: i64,
|
||||
top_skip: i64,
|
||||
par_skip: i64,
|
||||
bottom: i64,
|
||||
) {
|
||||
let mut pending = paragraph.clone();
|
||||
loop {
|
||||
let at_top = current.items.is_empty() && !page_has_top_float_material(current);
|
||||
let skip = if current.items.is_empty() {
|
||||
0
|
||||
} else {
|
||||
par_skip
|
||||
};
|
||||
let base = if at_top { top_skip } else { *used + skip };
|
||||
let available = page_goal - *insertion_height - bottom - base;
|
||||
let lines = pending.lines.len();
|
||||
if pending.height <= available.max(0) {
|
||||
if at_top {
|
||||
*used = top_skip.max(pending.height);
|
||||
} else {
|
||||
if skip > 0 {
|
||||
current.items.push(VerticalItem::Glue(skip));
|
||||
}
|
||||
*used = base + pending.height;
|
||||
}
|
||||
current.items.push(VerticalItem::Paragraph(pending));
|
||||
return;
|
||||
}
|
||||
let line_height = (pending.height / lines as i64).max(1);
|
||||
let fit = (available.max(0) / line_height) as usize;
|
||||
if fit == 0 {
|
||||
if at_top {
|
||||
// A single line cannot be split; keep TeX's overfull page.
|
||||
*used = top_skip.max(pending.height);
|
||||
current.items.push(VerticalItem::Paragraph(pending));
|
||||
return;
|
||||
}
|
||||
pages.push(std::mem::replace(current, Page::empty()));
|
||||
*used = top_skip;
|
||||
*insertion_height = 0;
|
||||
continue;
|
||||
}
|
||||
let mut head = pending.clone();
|
||||
let tail = head.split_at(fit);
|
||||
if at_top {
|
||||
*used = top_skip.max(head.height);
|
||||
} else {
|
||||
if skip > 0 {
|
||||
current.items.push(VerticalItem::Glue(skip));
|
||||
}
|
||||
*used = base + head.height;
|
||||
}
|
||||
current.items.push(VerticalItem::Paragraph(head));
|
||||
pages.push(std::mem::replace(current, Page::empty()));
|
||||
*used = top_skip;
|
||||
*insertion_height = 0;
|
||||
pending = tail;
|
||||
}
|
||||
}
|
||||
|
||||
fn top_float_exceeds_page_fraction(page: &Page, page_goal: i64, percent: u8) -> bool {
|
||||
let height = top_float_height(page);
|
||||
height > 0 && page.items.is_empty() && height * 100 > i64::from(percent) * page_goal
|
||||
@@ -1079,28 +1150,18 @@ impl Engine {
|
||||
used = document.top_skip;
|
||||
insertion_height = 0;
|
||||
}
|
||||
let par_skip = if current.items.is_empty() {
|
||||
0
|
||||
} else {
|
||||
document.par_skip.max(0)
|
||||
};
|
||||
if current.items.is_empty() && !page_has_top_float_material(¤t) {
|
||||
used = document.top_skip.max(paragraph.height);
|
||||
} else if used + par_skip + paragraph.height
|
||||
> document.page_goal
|
||||
- insertion_height
|
||||
- bottom_float_area(¤t, document.text_float_sep)
|
||||
{
|
||||
pages.push(std::mem::replace(&mut current, Page::empty()));
|
||||
used = document.top_skip.max(paragraph.height);
|
||||
insertion_height = 0;
|
||||
} else {
|
||||
if par_skip > 0 {
|
||||
current.items.push(VerticalItem::Glue(par_skip));
|
||||
}
|
||||
used += par_skip + paragraph.height;
|
||||
}
|
||||
current.items.push(item.clone());
|
||||
let bottom = bottom_float_area(¤t, document.text_float_sep);
|
||||
place_paragraph_flow(
|
||||
&mut pages,
|
||||
&mut current,
|
||||
&mut used,
|
||||
&mut insertion_height,
|
||||
paragraph,
|
||||
document.page_goal,
|
||||
document.top_skip,
|
||||
document.par_skip.max(0),
|
||||
bottom,
|
||||
);
|
||||
}
|
||||
VerticalItem::Math { height, depth, .. } => {
|
||||
if top_float_exceeds_page_fraction(
|
||||
@@ -1613,7 +1674,16 @@ impl Engine {
|
||||
VerticalItem::Math { height, depth, .. } => height + depth,
|
||||
})
|
||||
.sum();
|
||||
if checkpoint.used + edited_height > next.page_goal && !checkpoint.page_items.is_empty() {
|
||||
// A paragraph is rebuilt line by line, so it may fill the remaining
|
||||
// space of the checkpoint page instead of being pushed to a new one.
|
||||
let splits = matches!(
|
||||
next.units[start_index].items.first(),
|
||||
Some(VerticalItem::Paragraph(_))
|
||||
);
|
||||
if !splits
|
||||
&& checkpoint.used + edited_height > next.page_goal
|
||||
&& !checkpoint.page_items.is_empty()
|
||||
{
|
||||
checkpoint.page_index += 1;
|
||||
checkpoint.used = 0;
|
||||
checkpoint.page_items.clear();
|
||||
@@ -1740,28 +1810,18 @@ impl Engine {
|
||||
used = next.top_skip;
|
||||
insertion_height = 0;
|
||||
}
|
||||
let par_skip = if current.items.is_empty() {
|
||||
0
|
||||
} else {
|
||||
next.par_skip.max(0)
|
||||
};
|
||||
if current.items.is_empty() && !page_has_top_float_material(¤t) {
|
||||
used = next.top_skip.max(paragraph.height);
|
||||
} else if used + par_skip + paragraph.height
|
||||
> next.page_goal
|
||||
- insertion_height
|
||||
- bottom_float_area(¤t, next.text_float_sep)
|
||||
{
|
||||
pages.push(std::mem::replace(&mut current, Page::empty()));
|
||||
used = next.top_skip.max(paragraph.height);
|
||||
insertion_height = 0;
|
||||
} else {
|
||||
if par_skip > 0 {
|
||||
current.items.push(VerticalItem::Glue(par_skip));
|
||||
}
|
||||
used += par_skip + paragraph.height;
|
||||
}
|
||||
current.items.push(item.clone());
|
||||
let bottom = bottom_float_area(¤t, next.text_float_sep);
|
||||
place_paragraph_flow(
|
||||
&mut pages,
|
||||
&mut current,
|
||||
&mut used,
|
||||
&mut insertion_height,
|
||||
paragraph,
|
||||
next.page_goal,
|
||||
next.top_skip,
|
||||
next.par_skip.max(0),
|
||||
bottom,
|
||||
);
|
||||
}
|
||||
VerticalItem::Math { height, depth, .. } => {
|
||||
let amount = height + depth;
|
||||
@@ -3926,6 +3986,62 @@ mod tests {
|
||||
assert_eq!(Engine::layout(&document).pages().len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paragraphs_split_at_line_boundaries_across_pages() {
|
||||
let lines: Vec<Vec<crate::paragraph::HorizontalItem>> =
|
||||
(0..10).map(|_| Vec::new()).collect();
|
||||
let paragraph = Paragraph {
|
||||
lines,
|
||||
height: 100,
|
||||
width: 50,
|
||||
font: ParagraphFont::Roman,
|
||||
left_skip: 0,
|
||||
line_indents: vec![0; 10],
|
||||
line_baselines: (0..10).map(|line| 8 + line * 10).collect(),
|
||||
centered: false,
|
||||
right_aligned: false,
|
||||
};
|
||||
let mut document = Document::new(
|
||||
45,
|
||||
vec![Unit::vertical(1, vec![VerticalItem::Paragraph(paragraph)])],
|
||||
);
|
||||
document.top_skip = 5;
|
||||
let engine = Engine::layout(&document);
|
||||
let counts: Vec<usize> = engine
|
||||
.pages()
|
||||
.iter()
|
||||
.map(|page| {
|
||||
page.items
|
||||
.iter()
|
||||
.filter_map(|item| match item {
|
||||
VerticalItem::Paragraph(paragraph) => Some(paragraph.lines.len()),
|
||||
_ => None,
|
||||
})
|
||||
.sum()
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(counts, vec![4, 4, 2]);
|
||||
// A split paragraph does not collect a parskip: continuation pages
|
||||
// start with the next line fragment, not a glue item.
|
||||
for page in engine.pages().iter().skip(1) {
|
||||
assert!(matches!(
|
||||
page.items.first(),
|
||||
Some(VerticalItem::Paragraph(_))
|
||||
));
|
||||
}
|
||||
let baselines: Vec<i64> = engine
|
||||
.pages()
|
||||
.iter()
|
||||
.flat_map(|page| {
|
||||
page.items.iter().filter_map(|item| match item {
|
||||
VerticalItem::Paragraph(paragraph) => paragraph.line_baselines.first().copied(),
|
||||
_ => None,
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(baselines, vec![8, 8, 8]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn floatsep_separates_stacked_top_floats() {
|
||||
let mut document = Document::new(
|
||||
|
||||
Reference in new issue
Block a user