Honour topfraction for top floats

This commit is contained in:
gloamxun committed 2026-10-20 23:37:47 +08:00
1 parent 14a3775206
commit d81021d1a3
3 files changed
+96 -3

No files matched your search

+3 -1
View File
@@ -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`: `Document::bottom_fraction_percent` defaults to 30, so a bottom float taller than 30% of the page moves to the following 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: `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.
route_d_page_matches_pdftex_raster_geometry`, which renders the route-D PDF and a pdfTeX page at the same geometry, rasterizes both with `pdftoppm`, and compares ink line and edge positions. It is skipped when `pdflatex`/`pdftoppm` are unavailable.
+91
View File
@@ -537,6 +537,10 @@ pub struct Document {
/// `\bottomfraction`, in percent).
#[serde(default = "default_bottom_fraction")]
pub bottom_fraction_percent: u8,
/// Maximum percentage of a text page that top floats may occupy (TeX's
/// `\topfraction`, in percent).
#[serde(default = "default_top_fraction")]
pub top_fraction_percent: u8,
/// Per-class insertion limits. Missing classes use the full page.
#[serde(default)]
pub insertion_classes: Vec<InsertionClass>,
@@ -550,6 +554,10 @@ fn default_bottom_fraction() -> u8 {
30
}
fn default_top_fraction() -> u8 {
70
}
impl Document {
pub fn new(page_goal: i64, units: Vec<Unit>) -> Self {
Self {
@@ -559,6 +567,7 @@ impl Document {
text_fraction_percent: default_text_fraction(),
float_page_fraction_percent: 50,
bottom_fraction_percent: default_bottom_fraction(),
top_fraction_percent: default_top_fraction(),
insertion_classes: Vec::new(),
}
}
@@ -649,6 +658,13 @@ fn bottom_float_height(page: &Page) -> i64 {
.sum()
}
/// TeX `\topfraction`: a top float taller than this share of the page keeps
/// the rest of the page free of body text.
fn top_float_exceeds_page_fraction(page: &Page, page_goal: i64, percent: u8) -> bool {
let height = top_float_height(page);
height > 0 && page.items.is_empty() && height * 100 > i64::from(percent) * page_goal
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Checkpoint {
/// Index of the next unit to execute.
@@ -762,6 +778,15 @@ impl Engine {
}
match item {
VerticalItem::Rule { height, .. } => {
if top_float_exceeds_page_fraction(
&current,
document.page_goal,
document.top_fraction_percent,
) {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
insertion_height = 0;
}
if current.items.is_empty() {
used = document.top_skip.max(*height);
} else if used + *height
@@ -776,6 +801,15 @@ impl Engine {
current.items.push(item.clone());
}
VerticalItem::Box(vertical_box) => {
if top_float_exceeds_page_fraction(
&current,
document.page_goal,
document.top_fraction_percent,
) {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
insertion_height = 0;
}
if current.items.is_empty() && top_float_height(&current) == 0 {
used = document.top_skip.max(vertical_box.height);
} else if used + vertical_box.height
@@ -820,6 +854,15 @@ impl Engine {
}
}
VerticalItem::Paragraph(paragraph) => {
if top_float_exceeds_page_fraction(
&current,
document.page_goal,
document.top_fraction_percent,
) {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
insertion_height = 0;
}
if current.items.is_empty() && top_float_height(&current) == 0 {
used = document.top_skip.max(paragraph.height);
} else if used + paragraph.height
@@ -834,6 +877,15 @@ impl Engine {
current.items.push(item.clone());
}
VerticalItem::Math { height, depth, .. } => {
if top_float_exceeds_page_fraction(
&current,
document.page_goal,
document.top_fraction_percent,
) {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
insertion_height = 0;
}
let amount = height + depth;
if current.items.is_empty() && top_float_height(&current) == 0 {
used = document.top_skip.max(amount);
@@ -1683,6 +1735,45 @@ mod tests {
assert_eq!(engine.layout.pages[1].floats.len(), 1);
}
#[test]
fn top_fraction_moves_body_off_a_tall_top_float() {
let unit = Unit {
id: 0,
source: "top fraction".to_owned(),
paragraph_style: None,
items: vec![
VerticalItem::Float(Float {
placement: FloatPlacement::Top,
width: 40,
height: 80,
content: Vec::new(),
}),
VerticalItem::Box(VerticalBox {
width: 10,
height: 5,
}),
],
};
let mut document = Document::new(100, vec![unit]);
document.top_skip = 0;
document.float_page_fraction_percent = 10;
let engine = Engine::layout(&document);
assert_eq!(
engine.layout.pages.len(),
2,
"80% top float exceeds 70% limit"
);
assert_eq!(engine.layout.pages[0].floats.len(), 1);
assert!(engine.layout.pages[0].items.is_empty());
assert_eq!(engine.layout.pages[1].items.len(), 1);
document.top_fraction_percent = 100;
let engine = Engine::layout(&document);
assert_eq!(engine.layout.pages.len(), 1);
assert_eq!(engine.layout.pages[0].floats.len(), 1);
assert_eq!(engine.layout.pages[0].items.len(), 1);
}
#[test]
fn edit_ignores_structural_units() {
let rows = vec![HalignRow {
+2 -2
View File
@@ -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 `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.
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 143 unit tests plus two route-D integration tests. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 240 tests (143 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 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.
### M0 incremental demo