diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index 592d584..6f684ae 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -1962,6 +1962,9 @@ impl Engine { text_float_sep: document.text_float_sep.max(0), float_sep: document.float_sep.max(0), bottom_fraction_percent: document.bottom_fraction_percent, + top_number: usize::from(document.top_number), + bottom_number: usize::from(document.bottom_number), + total_number: usize::from(document.total_number), ragged_bottom: document.ragged_bottom, } } @@ -5815,6 +5818,27 @@ mod tests { assert_eq!(body, Some(30)); } + #[test] + fn paged_layout_limits_floats_per_page() { + let mut document = Document::new( + 100, + vec![ + Unit::float(1, FloatPlacement::Top, 100, 20), + Unit::float(2, FloatPlacement::Top, 100, 20), + Unit::float(3, FloatPlacement::Top, 100, 20), + ], + ); + document.text_float_sep = 0; + document.top_number = 1; + let layout = Engine::layout_paged(&document).expect("floats"); + assert_eq!(layout.pages().len(), 3); + assert!(layout.pages().iter().all(|page| page.floats.len() == 1)); + + document.top_number = 3; + let layout = Engine::layout_paged(&document).expect("floats"); + assert_eq!(layout.pages().len(), 1); + } + #[test] fn paged_layout_defers_a_late_top_float() { let mut document = Document::new( diff --git a/crates/texrra-tex-core/src/page.rs b/crates/texrra-tex-core/src/page.rs index 018d8a5..179b1b5 100644 --- a/crates/texrra-tex-core/src/page.rs +++ b/crates/texrra-tex-core/src/page.rs @@ -153,6 +153,12 @@ pub struct PageBuilder { pub float_sep: i64, /// TeX bottomfraction: percent of the page bottom floats may occupy. pub bottom_fraction_percent: u8, + /// TeX `\topnumber`: maximum top floats on one page. + pub top_number: usize, + /// TeX `\bottomnumber`: maximum bottom floats on one page. + pub bottom_number: usize, + /// TeX `\totalnumber`: maximum floats, of any placement, on one page. + pub total_number: usize, /// TeX `\raggedbottom` (default) versus `\flushbottom`. pub ragged_bottom: bool, } @@ -167,6 +173,9 @@ impl PageBuilder { text_float_sep: 0, float_sep: 0, bottom_fraction_percent: 100, + top_number: 2, + bottom_number: 1, + total_number: 12, ragged_bottom: true, } } @@ -238,6 +247,9 @@ impl PageBuilder { let mut depth = 0_i64; let mut first_box = true; let mut has_float = false; + let mut has_body = false; + let mut top_count = 0_usize; + let mut float_total = 0_usize; let mut bottom = 0_i64; let mut bottom_count = 0_usize; let mut usage: BTreeMap = held.clone(); @@ -264,12 +276,18 @@ impl PageBuilder { height += contribution; depth = *box_depth; first_box = false; + has_body = true; } VerticalNode::Float { height: float_height, } => { - if height > 0 { - // TeX never places a top float below body material: + if height > 0 + && (has_body + || top_count >= self.top_number + || float_total >= self.total_number) + { + // TeX never places a top float below body material, + // nor above the `\topnumber`/`\totalnumber` limits: // the float starts the next page instead. let end = index.max(start); let badness = @@ -297,6 +315,8 @@ impl PageBuilder { } height += (*float_height).max(0); has_float = true; + top_count += 1; + float_total += 1; } VerticalNode::BottomFloat { height: float_height, @@ -310,7 +330,9 @@ impl PageBuilder { let allowed = self.page_goal * i64::from(self.bottom_fraction_percent) / 100; if height > 0 && (height + reserved + bottom + needed > self.page_goal - || bottom + needed > allowed) + || bottom + needed > allowed + || bottom_count >= self.bottom_number + || float_total >= self.total_number) { let end = index.max(start); let badness = @@ -338,6 +360,7 @@ impl PageBuilder { } bottom += needed; bottom_count += 1; + float_total += 1; } VerticalNode::Glue(glue) => { if index > start { @@ -948,6 +971,7 @@ mod tests { fn stacked_bottom_floats_add_float_separators() { let builder = PageBuilder { float_sep: 5, + bottom_number: 2, ..PageBuilder::new(100) }; let nodes = vec![ @@ -960,6 +984,57 @@ mod tests { assert_eq!(pages[0].bottom, 45); } + #[test] + fn top_number_limits_stacked_top_floats() { + let builder = PageBuilder { + top_number: 1, + ..PageBuilder::new(100) + }; + let nodes = vec![ + VerticalNode::Float { height: 20 }, + VerticalNode::Float { height: 20 }, + box_(10, 0), + ]; + let pages = builder.build(&nodes); + assert_eq!(pages.len(), 2); + assert_eq!(pages[0].height, 20); + assert_eq!(pages[1].height, 30); + } + + #[test] + fn bottom_number_limits_stacked_bottom_floats() { + let builder = PageBuilder { + bottom_number: 1, + ..PageBuilder::new(100) + }; + let nodes = vec![ + box_(50, 0), + VerticalNode::BottomFloat { height: 20 }, + VerticalNode::BottomFloat { height: 20 }, + ]; + let pages = builder.build(&nodes); + assert_eq!(pages.len(), 2); + assert_eq!(pages[0].bottom, 20); + assert_eq!(pages[1].bottom, 20); + } + + #[test] + fn total_number_counts_every_float_placement() { + let builder = PageBuilder { + total_number: 1, + ..PageBuilder::new(100) + }; + let nodes = vec![ + VerticalNode::Float { height: 10 }, + VerticalNode::BottomFloat { height: 10 }, + box_(10, 0), + ]; + let pages = builder.build(&nodes); + assert_eq!(pages.len(), 2); + assert_eq!(pages[0].bottom, 0); + assert_eq!(pages[1].bottom, 10); + } + #[test] fn bottom_fraction_caps_bottom_float_space() { let builder = PageBuilder { diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index f61749a..5657414 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -116,7 +116,7 @@ and justification. | --- | --- | --- | | M0 | vertical list, page builder, checkpoint, page hash, edit demo | edit on page 2 rebuilds only page 2 | | M1 | TeX box/glue/penalty + paragraph builder (`\par`, `\hsize`, `\parindent`) | **partial**: item stream, CMR10 metrics, TeX Live en-US hyphenation, TeX demerits and tolerance passes, `\hangindent`/`\parshape`, `\parfillskip`, sentence spacing, `\spaceskip`/`\xspaceskip`, `\raggedright` (infinite `\rightskip`), `\looseness`, `\baselineskip`/`\lineskip`/`\parskip` interline glue, pdfLaTeX oracle matrix 90-240pt plus register, geometry, `\sfcode` and vertical-glue fixtures; generic fil glue and `\prevgraf` next | -| M2 | page builder + `\output`, `\vsize`, `\topskip`, footnotes, floats | **partial**: `\vsize` page goal, `\topskip`, forced penalties, `Insertion` reserve, running `Mark`s, `OutputRoutine` hook, top/bottom `Float` placement; `page::PageBuilder` scores candidate breakpoints with TeX page badness, glue setting (with `\raggedbottom`/`\flushbottom` applied when pages are materialised, and TeX glue orders `\vfil`/`\vfill`/`\vfilll` honoured so only the highest order with stretch is used), `\topskip`/`\maxdepth`, and `Engine::layout_paged` uses it for documents whose units are single vertical items, placing top and bottom floats (with `\floatsep` and a `\bottomfraction` cap) and insertion classes on that path, while in-text floats flow inline as body material; paged layouts replay edits from the edited unit's page and fall back to a full rebuild only when insertions or an interpreted `\output` are present; `\output` is interpreted for a `\shipout`/`\ifdim`/`\ifnum`/`\ifodd`/`\ifcase` subset; `\pageno`, `\outputpenalty` and `\deadcycles` are readable registers, insertion class registers `\count`/`\dimen`/`\skip` read the configured multiplier, maximum height and skip of class `n` (with `\count0` reading `\pageno`), `PageFragment::penalty` carries the break penalty, and `PageFragment::stretches` records glue stretch per TeX order so `\pagefilstretch`/`\pagefillstretch`/`\pagefilllstretch` are readable in the output routine | +| M2 | page builder + `\output`, `\vsize`, `\topskip`, footnotes, floats | **partial**: `\vsize` page goal, `\topskip`, forced penalties, `Insertion` reserve, running `Mark`s, `OutputRoutine` hook, top/bottom `Float` placement; `page::PageBuilder` scores candidate breakpoints with TeX page badness, glue setting (with `\raggedbottom`/`\flushbottom` applied when pages are materialised, and TeX glue orders `\vfil`/`\vfill`/`\vfilll` honoured so only the highest order with stretch is used), `\topskip`/`\maxdepth`, and `Engine::layout_paged` uses it for documents whose units are single vertical items, placing top and bottom floats (with `\floatsep`, a `\bottomfraction` cap and the `\topnumber`/`\bottomnumber`/`\totalnumber` limits) and insertion classes on that path, while in-text floats flow inline as body material; paged layouts replay edits from the edited unit's page and fall back to a full rebuild only when insertions or an interpreted `\output` are present; `\output` is interpreted for a `\shipout`/`\ifdim`/`\ifnum`/`\ifodd`/`\ifcase` subset; `\pageno`, `\outputpenalty` and `\deadcycles` are readable registers, insertion class registers `\count`/`\dimen`/`\skip` read the configured multiplier, maximum height and skip of class `n` (with `\count0` reading `\pageno`), `PageFragment::penalty` carries the break penalty, and `PageFragment::stretches` records glue stretch per TeX order so `\pagefilstretch`/`\pagefillstretch`/`\pagefilllstretch` are readable in the output routine | | M3 | `\halign`, `\omit`, `\span`, `\multispan`, booktabs/longtable | **partial**: source parsing, natural column widths, spans, rule rows, 10pt display-list painting and row-level page splitting; no booktabs/longtable parity yet | | M4 | math lists, scripts, fractions, radicals, accents, delimiters | **partial**: token parser, atom boxes with CMR10 metrics, delimiter scaling, accents and a display-list painter; no CMEX size variants or TeX math spacing classes yet | | M5 | NFSS/TFM metrics, CM/Latin Modern | same line/page breaks as reference | @@ -346,7 +346,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 269 unit tests plus seven route-D integration tests and seventeen oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 393 tests (269 route-D unit, 7 route-D integration, 17 oracle, 71 v1 integration, 22 server, 4 v1 unit, 1 core unit, 2 binary units), with none ignored. v1 remains the live server path. +The crate currently has 273 unit tests plus seven route-D integration tests and seventeen oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 397 tests (273 route-D unit, 7 route-D integration, 17 oracle, 71 v1 integration, 22 server, 4 v1 unit, 1 core unit, 2 binary units), with none ignored. v1 remains the live server path. ### M0 incremental demo