From 48d10f901df618ac60a228f11b66877c2072dab3 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 23:41:31 +0800 Subject: [PATCH] Apply topfraction in incremental layout --- README.md | 4 +- crates/texrra-tex-core/src/lib.rs | 63 +++++++++++++++++++++++++++++++ docs/TEX_CORE.md | 4 +- 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d9de9f9..e0c2aa0 100644 --- a/README.md +++ b/README.md @@ -465,7 +465,9 @@ variant for delimiters and large display operators, and builds taller glyphs from the font's MATH assembly parts. `\\left...\\right` target sizing follows the assignable TeX parameters `\\delimiterfactor` (default 901) and `\\delimitershortfall` (default 5pt), including local and global assignments. -Route-D integration can also be checked against a real TeX page: `cargo test -p texrra-tex-core --test route_d` includes `Route-D float placement also mirrors `\bottomfraction`: Route-D top floats follow `\topfraction` as well: `Document::top_fraction_percent` defaults to 70, so a top float taller than 70% of the page keeps the body on the next page. +Route-D integration can also be checked against a real TeX page: `cargo test -p texrra-tex-core --test route_d` includes `Route-D float placement also mirrors `\bottomfraction`: Route-D top floats follow `\topfraction` as well: The same rule is applied by `Engine::edit`, so a body unit that starts after an oversized top float stays on the next page after an incremental edit. + +`Document::top_fraction_percent` defaults to 70, so a top float taller than 70% of the page keeps the body on the next page. `Document::bottom_fraction_percent` defaults to 30, so a bottom float taller than 30% of the page moves to the following page. diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index 0df458f..cc0dfd6 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -1146,6 +1146,15 @@ impl Engine { } match item { VerticalItem::Rule { height, .. } => { + if top_float_exceeds_page_fraction( + ¤t, + 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( + ¤t, + 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(¤t) == 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( + ¤t, + 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(¤t) == 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( + ¤t, + 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(¤t) == 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 { diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index 5f48a15..ead6346 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -261,13 +261,13 @@ M5 (partial): display-list text carries `FontFace::{Roman,Bold,Italic,BoldItalic M6 (partial): `DisplayItem::Image` becomes a PDF XObject; `DisplayItem::Link` becomes `/Annots /Subtype /Link /URI`; `DisplayItem::Special` injects raw content-stream operators. SVG previews show images as grey rectangles and specials as comments. -M2 (partial): `Page::display_list_with_goal(page_goal)` places insertion material at `page_goal - insertion_height`, adds a 0.4pt separator above footnotes, splits oversized insertions at item boundaries, and paints top/bottom floats from their vertical `content` at the page edges; empty floats keep a dimension rectangle. Floats also size themselves from content, respect `text_fraction_percent`, respect `top_fraction_percent` (TeX `\topfraction`, default 70%) and `bottom_fraction_percent` (TeX `\bottomfraction`, default 30%), and can take a dedicated float page. +M2 (partial): `Page::display_list_with_goal(page_goal)` places insertion material at `page_goal - insertion_height`, adds a 0.4pt separator above footnotes, splits oversized insertions at item boundaries, and paints top/bottom floats from their vertical `content` at the page edges; empty floats keep a dimension rectangle. Floats also size themselves from content, respect `text_fraction_percent`, respect `top_fraction_percent` (TeX `\topfraction`, default 70%) and `bottom_fraction_percent` (TeX `\bottomfraction`, default 30%), and can take a dedicated float page. The incremental edit path applies the same `top_fraction_percent` rule as full layout. M3 (partial): `Unit::halign` normalizes column widths, intercolumn gaps, row heights and cell text widths to scaled points; the painter uses 10pt glyphs in 12pt rows. Rules and spans are painted; tall tables split at row boundaries (`Halign::split_for_height`) with column widths preserved. Math coverage includes fractions/roots, scripts, `\left...\right` with body-scaled delimiters, `\middle`, `\big`/`\Big`/`\bigg`/`\Bigg` and their `l`/`r` forms, control-sequence delimiters, accents (dot/check/breve/acute/grave/ring), `\text`/`\mathrm`, `\overbrace`/`\underbrace`, `\overset`/`\underset`, `\binom`, `\cancel`/`\bcancel`/`\xcancel`, `\bmod`/`\pmod`, fence symbols, `\operatorname*` and `\mathop` limits. -The crate currently has 144 unit tests plus two route-D integration tests. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 241 tests (144 route-D unit, 2 route-D integration, 71 v1 integration, 19 server, 4 v1 unit, 1 doctest). v1 remains the live server path. +The crate currently has 145 unit tests plus two route-D integration tests. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 242 tests (145 route-D unit, 2 route-D integration, 71 v1 integration, 19 server, 4 v1 unit, 1 doctest). v1 remains the live server path. ### M0 incremental demo