Apply topfraction in incremental layout

This commit is contained in:
gloamxun committed 2026-10-20 23:41:31 +08:00
1 parent d81021d1a3
commit 48d10f901d
3 files changed
+68 -3

No files matched your search

+63
View File
@@ -1146,6 +1146,15 @@ impl Engine {
}
match item {
VerticalItem::Rule { height, .. } => {
if top_float_exceeds_page_fraction(
&current,
next.page_goal,
next.top_fraction_percent,
) {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = next.top_skip;
insertion_height = 0;
}
if current.items.is_empty() {
used = next.top_skip.max(*height);
} else if used + *height
@@ -1160,6 +1169,15 @@ impl Engine {
current.items.push(item.clone());
}
VerticalItem::Box(vertical_box) => {
if top_float_exceeds_page_fraction(
&current,
next.page_goal,
next.top_fraction_percent,
) {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = next.top_skip;
insertion_height = 0;
}
if current.items.is_empty() && top_float_height(&current) == 0 {
used = next.top_skip.max(vertical_box.height);
} else if used + vertical_box.height
@@ -1188,6 +1206,15 @@ impl Engine {
current.items.push(item.clone());
}
VerticalItem::Paragraph(paragraph) => {
if top_float_exceeds_page_fraction(
&current,
next.page_goal,
next.top_fraction_percent,
) {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = next.top_skip;
insertion_height = 0;
}
if current.items.is_empty() && top_float_height(&current) == 0 {
used = next.top_skip.max(paragraph.height);
} else if used + paragraph.height
@@ -1203,6 +1230,15 @@ impl Engine {
}
VerticalItem::Math { height, depth, .. } => {
let amount = height + depth;
if top_float_exceeds_page_fraction(
&current,
next.page_goal,
next.top_fraction_percent,
) {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = next.top_skip;
insertion_height = 0;
}
if current.items.is_empty() && top_float_height(&current) == 0 {
used = next.top_skip.max(amount);
} else if used + amount
@@ -1774,6 +1810,33 @@ mod tests {
assert_eq!(engine.layout.pages[0].items.len(), 1);
}
#[test]
fn edited_layout_respects_top_fraction() {
let document = {
let mut document = Document::new(
100,
vec![
Unit::float(0, FloatPlacement::Top, 40, 80),
Unit::paragraph(1, "body text"),
],
);
document.top_skip = 0;
document.text_fraction_percent = 0;
document.float_page_fraction_percent = 10;
document
};
let mut engine = Engine::layout(&document);
assert_eq!(engine.pages().len(), 2);
engine.edit(&document, 1, "edited body text".to_string());
assert_eq!(
engine.pages().len(),
2,
"edited layout must keep topfraction"
);
assert_eq!(engine.pages()[0].floats.len(), 1);
assert!(engine.pages()[0].items.is_empty());
}
#[test]
fn edit_ignores_structural_units() {
let rows = vec![HalignRow {