From 17638996a107b4a56121abeda1aa82428df49578 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Fri, 23 Oct 2026 23:43:32 +0800 Subject: [PATCH] Defer bottom floats inside the page builder --- crates/texrra-tex-core/src/page.rs | 137 ++++++++++++++++++++++++++++- docs/ROUTE_D_PLAN.md | 7 +- docs/TEX_CORE.md | 8 +- 3 files changed, 145 insertions(+), 7 deletions(-) diff --git a/crates/texrra-tex-core/src/page.rs b/crates/texrra-tex-core/src/page.rs index fed0466..40f73db 100644 --- a/crates/texrra-tex-core/src/page.rs +++ b/crates/texrra-tex-core/src/page.rs @@ -204,7 +204,9 @@ impl PageBuilder { let mut held = BTreeMap::new(); let mut start = self.skip_leading(nodes, 0); while start < nodes.len() { - let Some((fragment, next, next_held)) = self.best_fragment(nodes, start, &held) else { + let Some((fragment, next, next_held, _, _)) = + self.best_fragment(nodes, start, &held, &[], false) + else { break; }; let next = next.max(start + 1); @@ -224,7 +226,8 @@ impl PageBuilder { let mut pages = Vec::new(); let mut next = self.skip_leading(nodes, start); while next < nodes.len() { - let Some((fragment, after, _)) = self.best_fragment(nodes, next, &BTreeMap::new()) + let Some((fragment, after, _, _, _)) = + self.best_fragment(nodes, next, &BTreeMap::new(), &[], false) else { break; }; @@ -235,6 +238,58 @@ impl PageBuilder { pages } + /// Split `nodes` into pages while deferring bottom floats that do not + /// fit: the page keeps filling with body material and the float lands at + /// the bottom of the following page (TeX's deferred float queue). + pub fn build_with_deferred_bottom_floats(&self, nodes: &[VerticalNode]) -> Vec { + let mut pages = Vec::new(); + let mut carried: Vec = Vec::new(); + let mut start = self.skip_leading(nodes, 0); + while start < nodes.len() { + let Some((fragment, next, _, _, deferred)) = + self.best_fragment(nodes, start, &BTreeMap::new(), &carried, true) + else { + break; + }; + let next = next.max(start + 1); + carried = deferred; + pages.push(fragment); + start = self.skip_leading(nodes, next); + } + if !carried.is_empty() { + pages.push(self.float_page_fragment(nodes.len(), &carried)); + } + pages + } + + fn float_page_fragment(&self, at: usize, floats: &[i64]) -> PageFragment { + let mut bottom = 0_i64; + for (index, height) in floats.iter().enumerate() { + let gap = if index == 0 { + self.text_float_sep.max(0) + } else { + self.float_sep.max(0) + }; + bottom += (*height).max(0) + gap; + } + PageFragment { + start: at, + end: at, + cost: 0, + penalty: 10_000, + badness: 0, + height: 0, + bottom, + depth: 0, + stretch: 0, + stretch_order: 0, + shrink: 0, + stretches: [0; 4], + set: 0, + insertions: Vec::new(), + } + } + fn skip_leading(&self, nodes: &[VerticalNode], mut index: usize) -> usize { while index < nodes.len() { match nodes[index] { @@ -250,7 +305,9 @@ impl PageBuilder { nodes: &[VerticalNode], start: usize, held: &BTreeMap, - ) -> Option<(PageFragment, usize, BTreeMap)> { + carried_floats: &[i64], + defer_floats: bool, + ) -> Option<(PageFragment, usize, BTreeMap, Vec, Vec)> { let mut height = 0_i64; let mut stretch_orders = [0_i64; 4]; let mut shrink = 0_i64; @@ -266,6 +323,20 @@ impl PageBuilder { let mut reserved: i64 = usage.values().sum(); let mut candidates: Vec = Vec::new(); + let mut bottom_floats: Vec = Vec::new(); + let mut deferred_floats: Vec = Vec::new(); + for height in carried_floats { + let gap = if bottom_count == 0 { + self.text_float_sep.max(0) + } else { + self.float_sep.max(0) + }; + bottom += (*height).max(0) + gap; + bottom_count += 1; + bottom_floats.push((*height).max(0)); + float_total += 1; + } + for index in start..nodes.len() { let (stretch, stretch_order) = effective_stretch(&stretch_orders); match &nodes[index] { @@ -302,6 +373,8 @@ impl PageBuilder { fragment, index, self.insertion_overflow(nodes, start, end, held), + bottom_floats.clone(), + deferred_floats.clone(), )); } let contribution = if first_box { @@ -354,6 +427,8 @@ impl PageBuilder { fragment, index, self.insertion_overflow(nodes, start, end, held), + bottom_floats.clone(), + deferred_floats.clone(), )); } // TeX installs a `[t]` float at the top of the page it is @@ -385,6 +460,21 @@ impl PageBuilder { }; let needed = (*float_height).max(0) + gap; let allowed = self.page_goal * i64::from(self.bottom_fraction_percent) / 100; + // TeX defers a bottom float that does not fit and keeps + // filling the page with body material; the float moves to + // the next page's bottom area. + if defer_floats + && height > 0 + && needed <= self.page_goal + && needed <= allowed + && (height + reserved + bottom + needed > self.page_goal + || bottom + needed > allowed + || bottom_count >= self.bottom_number + || float_total >= self.total_number) + { + deferred_floats.push((*float_height).max(0)); + continue; + } if height > 0 && (height + reserved + bottom + needed > self.page_goal || bottom + needed > allowed @@ -413,11 +503,14 @@ impl PageBuilder { fragment, index, self.insertion_overflow(nodes, start, end, held), + bottom_floats.clone(), + deferred_floats.clone(), )); } bottom += needed; bottom_count += 1; float_total += 1; + bottom_floats.push((*float_height).max(0)); } VerticalNode::Glue(glue) => { // TeX only allows a break at glue that follows a @@ -470,6 +563,8 @@ impl PageBuilder { fragment, index + 1, self.insertion_overflow(nodes, start, end, held), + bottom_floats.clone(), + deferred_floats.clone(), )); } if index > start { @@ -533,6 +628,8 @@ impl PageBuilder { fragment, best.next, self.insertion_overflow(nodes, start, best.end, held), + bottom_floats.clone(), + deferred_floats.clone(), )) } @@ -753,6 +850,40 @@ mod tests { VerticalNode::Glue(VerticalGlue::new(width, stretch, shrink)) } + #[test] + fn deferred_bottom_float_continues_the_page() { + let mut builder = PageBuilder::new(100); + builder.text_float_sep = 0; + let nodes = vec![ + box_(30, 0), + VerticalNode::BottomFloat { height: 80 }, + glue(0, 0, 0), + box_(30, 0), + ]; + let pages = builder.build_with_deferred_bottom_floats(&nodes); + assert_eq!(pages.len(), 2); + assert_eq!( + pages[0].height, 60, + "body continues past the deferred float" + ); + assert_eq!(pages[0].bottom, 0); + assert_eq!(pages[1].bottom, 80, "float lands on the next page"); + assert_eq!((pages[1].start, pages[1].end), (4, 4)); + } + + #[test] + fn fitting_bottom_float_stays_on_its_page() { + let builder = PageBuilder::new(100); + let nodes = vec![ + box_(30, 0), + VerticalNode::BottomFloat { height: 50 }, + box_(10, 0), + ]; + let pages = builder.build_with_deferred_bottom_floats(&nodes); + assert_eq!(pages.len(), 1); + assert_eq!(pages[0].bottom, 50); + } + #[test] fn forced_penalty_breaks_record_the_penalty() { let builder = PageBuilder::new(100); diff --git a/docs/ROUTE_D_PLAN.md b/docs/ROUTE_D_PLAN.md index a425780..649d320 100644 --- a/docs/ROUTE_D_PLAN.md +++ b/docs/ROUTE_D_PLAN.md @@ -48,7 +48,7 @@ floats (`\floatsep`, `\textfloatsep`, `\intextsep`, `\topfraction`, `\bottomnumber`, `\totalnumber`), insertion classes (`\skip`, fraction and absolute limits, splitting), marks, Rust output hook, multi-page PDF, and the route-D service endpoints (`/api/route-d/preview|pdf|edit`, project preview and -PDF opt-in with `?engine=route-d`). 459 workspace tests are green and no oracle +PDF opt-in with `?engine=route-d`). 461 workspace tests are green and no oracle fixture is ignored. Known gaps, in dependency order: @@ -254,6 +254,11 @@ body break matches pdfLaTeX. When it does not fit the remaining body space, the paged builder ends the current page and defers the float, while TeX keeps filling the page before placing the float on the next one; that deferral-fill case stays documented as a known approximation. +`PageBuilder::build_with_deferred_bottom_floats` now implements TeX's +behaviour for that case: the current page keeps filling, the float is +carried to the next page's bottom, and a float larger than the page still +ends the page rather than looping. The route-D service still calls `build`, +so endpoint behaviour is unchanged until that wiring lands. ### P2 — Vertical list, page builder, `\output` (L2) diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index 0d5214f..9422d56 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -266,8 +266,10 @@ oracle fixture is ignored. offer the page builder a legal breakpoint. The routine cannot yet change page goals. The paged builder places top `Float` material at the top of a page and bottom `Float` material at the page bottom: a late top float - starts the next page, a bottom float that cannot fit the remaining body - space ends the current page and is deferred to the next page's bottom + starts the next page, a bottom float that cannot fit is deferred to the next page's + bottom by `PageBuilder::build_with_deferred_bottom_floats`, which keeps + filling the current page (the default `build` path still ends the page at + the float until the service adopts the new entry point) (TeX instead keeps filling the current page before deferring the float, which remains a documented approximation), stacked bottom floats reserve `\floatsep`, and `\bottomfraction` caps the bottom float share, and a page whose top floats reach `\floatpagefraction` ships without body material. Route-D preview/PDF draw @@ -363,7 +365,7 @@ Math coverage includes fractions/roots, scripts, `\left...\right` with body-scal Paragraph units carry a `ParagraphFont`; `Unit::paragraph_with_face` and `Unit::paragraph_with_style_and_face` select Roman, bold, italic, mono, or sans. The corresponding Computer Modern TFM (`cmr10`, `cmbx10`, `cmti10`, `cmbxti10`, `cmtt10`, `cmss10`) supplies advances, kerns, ligatures, and italic correction for layout, while the display list and PDF select the matching Liberation face. `Engine::edit` preserves the face when reflowing an edited paragraph. -The crate currently has 304 unit tests plus seven route-D integration tests and thirty-three oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 459 tests (304 route-D unit, 7 route-D integration, 33 oracle, 71 v1 integration, 26 server, 4 v1 unit, 1 core unit, 13 binary units), with none ignored. v1 remains the live server path. +The crate currently has 306 unit tests plus seven route-D integration tests and thirty-three oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 461 tests (306 route-D unit, 7 route-D integration, 33 oracle, 71 v1 integration, 26 server, 4 v1 unit, 1 core unit, 13 binary units), with none ignored. v1 remains the live server path. ### M0 incremental demo