diff --git a/README.md b/README.md index e65d50b..f33efba 100644 --- a/README.md +++ b/README.md @@ -472,7 +472,8 @@ Route-D float placement mirrors TeX fractions: - `Document::top_fraction_percent` defaults to 70 (`\topfraction`); a taller top float keeps body text off its page. - `Document::bottom_fraction_percent` defaults to 30 (`\bottomfraction`); a taller bottom float moves to the next page. -- `Engine::edit` applies `top_fraction_percent`, `text_fraction_percent`, `bottom_fraction_percent`, and `float_page_fraction_percent`, so incremental layout matches full layout. +- `Document::top_number`, `bottom_number` and `total_number` default to TeX's 2, 1 and 12; they cap top floats, bottom floats and all floats on one page. +- `Engine::edit` applies the float fractions and number limits, so incremental layout matches full layout. These glyphs are rasterized at 600 dpi and shared by PDF and SVG. Ordinary math atom glyphs are rasterized from diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index b34bd9a..fb8b5ab 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -545,6 +545,15 @@ pub struct Document { /// `\topfraction`, in percent). #[serde(default = "default_top_fraction")] pub top_fraction_percent: u8, + /// TeX's `\topnumber`: maximum top floats on one page. + #[serde(default = "default_top_number")] + pub top_number: u8, + /// TeX's `\bottomnumber`: maximum bottom floats on one page. + #[serde(default = "default_bottom_number")] + pub bottom_number: u8, + /// TeX's `\totalnumber`: maximum floats of any placement on one page. + #[serde(default = "default_total_number")] + pub total_number: u8, /// Per-class insertion limits. Missing classes use the full page. #[serde(default)] pub insertion_classes: Vec, @@ -566,6 +575,18 @@ fn default_top_fraction() -> u8 { 70 } +fn default_top_number() -> u8 { + 2 +} + +fn default_bottom_number() -> u8 { + 1 +} + +fn default_total_number() -> u8 { + 12 +} + impl Document { pub fn new(page_goal: i64, units: Vec) -> Self { Self { @@ -576,6 +597,9 @@ impl Document { float_page_fraction_percent: 50, bottom_fraction_percent: default_bottom_fraction(), top_fraction_percent: default_top_fraction(), + top_number: default_top_number(), + bottom_number: default_bottom_number(), + total_number: default_total_number(), insertion_classes: Vec::new(), insertion_skip: 0, } @@ -1020,7 +1044,14 @@ impl Engine { .filter(|entry| entry.placement == FloatPlacement::Top) .map(|entry| entry.height) .sum(); + let top_count = current + .floats + .iter() + .filter(|entry| entry.placement == FloatPlacement::Top) + .count(); let fits_on_current = !current.items.is_empty() + && top_count < usize::from(document.top_number) + && current.floats.len() < usize::from(document.total_number) && used + float.height <= document.page_goal - insertion_height && (existing_top + float.height) * 100 <= i64::from(document.top_fraction_percent) @@ -1041,6 +1072,13 @@ impl Engine { used += float.height; current.floats.push(float.clone()); } else if !current.items.is_empty() + && current + .floats + .iter() + .filter(|entry| entry.placement == FloatPlacement::Bottom) + .count() + < usize::from(document.bottom_number) + && current.floats.len() < usize::from(document.total_number) && used + float.height <= document.page_goal - insertion_height @@ -1377,7 +1415,14 @@ impl Engine { .filter(|entry| entry.placement == FloatPlacement::Top) .map(|entry| entry.height) .sum(); + let top_count = current + .floats + .iter() + .filter(|entry| entry.placement == FloatPlacement::Top) + .count(); let fits_on_current = !current.items.is_empty() + && top_count < usize::from(next.top_number) + && current.floats.len() < usize::from(next.total_number) && used + float.height <= next.page_goal - insertion_height && (existing_top + float.height) * 100 <= i64::from(next.top_fraction_percent) * next.page_goal; @@ -1394,6 +1439,13 @@ impl Engine { used += float.height; current.floats.push(float.clone()); } else if !current.items.is_empty() + && current + .floats + .iter() + .filter(|entry| entry.placement == FloatPlacement::Bottom) + .count() + < usize::from(next.bottom_number) + && current.floats.len() < usize::from(next.total_number) && used + float.height <= next.page_goal - insertion_height - bottom_float_height(¤t) && (used - next.top_skip - bottom_float_height(¤t)) * 100 @@ -1858,6 +1910,95 @@ mod tests { assert_eq!(layout.pages()[1].insertions.len(), 1); } + #[test] + fn top_number_limits_top_floats_per_page() { + let mut document = Document::new( + 1000, + vec![ + Unit::paragraph(0, "alpha"), + Unit::float(1, FloatPlacement::Top, 10, 100), + Unit::paragraph(2, "beta"), + Unit::float(3, FloatPlacement::Top, 10, 100), + Unit::paragraph(4, "gamma"), + Unit::float(5, FloatPlacement::Top, 10, 100), + ], + ); + document.top_number = 1; + document.total_number = 10; + let engine = Engine::layout(&document); + assert_eq!(engine.pages().len(), 3); + for page in engine.pages() { + assert!( + page.floats + .iter() + .filter(|float| float.placement == FloatPlacement::Top) + .count() + <= 1 + ); + } + } + + #[test] + fn total_number_limits_mixed_float_placements() { + let mut document = Document::new( + 1000, + vec![ + Unit::float(0, FloatPlacement::Top, 10, 100), + Unit::paragraph(1, "alpha"), + Unit::float(2, FloatPlacement::Bottom, 10, 100), + ], + ); + document.top_number = 5; + document.bottom_number = 5; + document.total_number = 1; + document.text_fraction_percent = 0; + let engine = Engine::layout(&document); + assert_eq!(engine.pages().len(), 2); + assert_eq!(engine.pages()[0].floats.len(), 1); + assert_eq!(engine.pages()[0].floats[0].placement, FloatPlacement::Top); + assert_eq!(engine.pages()[1].floats.len(), 1); + assert_eq!( + engine.pages()[1].floats[0].placement, + FloatPlacement::Bottom + ); + } + + #[test] + fn edited_layout_respects_float_number_limits() { + let mut document = Document::new( + 1000, + vec![ + Unit::paragraph(0, "alpha"), + Unit::float(1, FloatPlacement::Top, 10, 100), + Unit::paragraph(2, "beta"), + Unit::float(3, FloatPlacement::Top, 10, 100), + ], + ); + document.top_number = 1; + document.total_number = 10; + let mut engine = Engine::layout(&document); + let edited = Document::new( + 1000, + vec![ + Unit::paragraph(0, "alpha changed"), + Unit::float(1, FloatPlacement::Top, 10, 100), + Unit::paragraph(2, "beta"), + Unit::float(3, FloatPlacement::Top, 10, 100), + ], + ); + engine.edit(&edited, 1, "alpha changed"); + assert_eq!(engine.pages().len(), 2); + for page in engine.pages() { + assert!( + page.floats + .iter() + .filter(|float| float.placement == FloatPlacement::Top) + .count() + <= 1 + ); + } + } + #[test] fn relayout_from_keeps_the_unchanged_page_prefix() { let first = Unit::paragraph(0, "alpha"); diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index 14863db..7cc3182 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -263,13 +263,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. The incremental edit path applies the same `top_fraction_percent`, `text_fraction_percent`, `bottom_fraction_percent`, `float_page_fraction_percent`, insertion-class limits and `insertion_skip` rules as full layout. +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 float fractions, float number limits, insertion-class limits and `insertion_skip` rules 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 156 unit tests plus three route-D integration tests. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 256 tests (156 route-D unit, 3 route-D integration, 71 v1 integration, 19 server, 4 v1 unit, 1 core unit, 2 binary units). v1 remains the live server path. +The crate currently has 159 unit tests plus three route-D integration tests. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 259 tests (159 route-D unit, 3 route-D integration, 71 v1 integration, 19 server, 4 v1 unit, 1 core unit, 2 binary units). v1 remains the live server path. ### M0 incremental demo