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(
|
||||
|
||||
@@ -294,6 +294,43 @@ impl Paragraph {
|
||||
pub fn line_count(&self) -> usize {
|
||||
self.lines.len()
|
||||
}
|
||||
|
||||
/// Move every line after `lines` into a new paragraph that starts at the
|
||||
/// remaining baselines. Used to continue a paragraph on the next page.
|
||||
pub fn split_at(&mut self, lines: usize) -> Paragraph {
|
||||
let lines = lines.min(self.lines.len().saturating_sub(1));
|
||||
let total = self.lines.len().max(1) as i64;
|
||||
let line_height = (self.height / total).max(1);
|
||||
let head_height = line_height * lines as i64;
|
||||
let tail_lines = self.lines.split_off(lines);
|
||||
let tail_indents = if self.line_indents.len() > lines {
|
||||
self.line_indents.split_off(lines)
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
let tail_baselines = if self.line_baselines.len() > lines {
|
||||
self.line_baselines
|
||||
.split_off(lines)
|
||||
.into_iter()
|
||||
.map(|baseline| baseline - head_height)
|
||||
.collect()
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
let tail_height = (self.height - head_height).max(1);
|
||||
self.height = head_height;
|
||||
Paragraph {
|
||||
lines: tail_lines,
|
||||
height: tail_height,
|
||||
width: self.width,
|
||||
font: self.font,
|
||||
left_skip: self.left_skip,
|
||||
line_indents: tail_indents,
|
||||
line_baselines: tail_baselines,
|
||||
centered: self.centered,
|
||||
right_aligned: self.right_aligned,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert text into a horizontal item stream: one box per character, one
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//! incremental paragraph edit.
|
||||
|
||||
use texrra_tex_core::{
|
||||
Document, Engine, ParagraphFont, ParagraphStyle, Unit,
|
||||
Document, Engine, ParagraphFont, ParagraphStyle, Unit, VerticalItem,
|
||||
cmr10::Cmr10Metrics,
|
||||
pdf::{PageOrigin, display_lists_to_pdf, display_lists_to_pdf_with_origin},
|
||||
};
|
||||
@@ -47,19 +47,29 @@ fn body_style() -> ParagraphStyle {
|
||||
|
||||
fn paragraph(id: u64, marker: &str) -> Unit {
|
||||
let text = format!("{} {}", marker, "alpha beta gamma delta ".repeat(80));
|
||||
Unit::paragraph_with_style(id, text, ParagraphStyle::default())
|
||||
Unit::paragraph_with_style(id, text, body_style())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn route_d_article_exports_pdf_and_reflows_only_the_edited_page() {
|
||||
let document = Document::new(
|
||||
20,
|
||||
let mut document = Document::new(
|
||||
1,
|
||||
vec![
|
||||
paragraph(1, "ONE"),
|
||||
paragraph(2, "TWO"),
|
||||
paragraph(3, "THREE"),
|
||||
],
|
||||
);
|
||||
// One full paragraph is exactly one page, so the test is about the
|
||||
// incremental checkpoint boundary rather than page overflow.
|
||||
document.page_goal = document
|
||||
.units
|
||||
.iter()
|
||||
.find_map(|unit| match &unit.items[0] {
|
||||
VerticalItem::Paragraph(paragraph) => Some(paragraph.height),
|
||||
_ => None,
|
||||
})
|
||||
.expect("paragraph units");
|
||||
let mut engine = Engine::layout(&document);
|
||||
assert!(engine.pages().len() >= 3, "expected one page per paragraph");
|
||||
|
||||
|
||||
Reference in new issue
Block a user