From 1014713ec07bb7f98446b70497101e3a7666e830 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Wed, 21 Oct 2026 12:46:24 +0800 Subject: [PATCH] Reserve floatsep between stacked floats --- README.md | 3 +- crates/texrra-tex-core/src/lib.rs | 216 ++++++++++++++++++++------- crates/texrra-tex-core/src/render.rs | 2 + docs/TEX_CORE.md | 2 +- 4 files changed, 167 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index 8c8128c..f78d82c 100644 --- a/README.md +++ b/README.md @@ -473,7 +473,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. - `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. +- `Document::float_sep` defaults to 12pt (`\floatsep`); stacked top floats or stacked bottom floats reserve that gap between each other, in full layout and after `Engine::edit`. +- `Engine::edit` applies the float fractions, number limits and separations, 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 fb70561..f9c5d05 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -191,6 +191,10 @@ pub struct Float { pub placement: FloatPlacement, pub width: i64, pub height: i64, + /// TeX floatsep: space inserted before this float when another float of + /// the same placement already sits on the page. + #[serde(default)] + pub gap_before: i64, /// Vertical material carried by the float. Empty for a dimension-only float. #[serde(default)] pub content: Vec, @@ -405,6 +409,7 @@ impl Unit { placement, width, height, + gap_before: 0, content: Vec::new(), })], ) @@ -573,12 +578,20 @@ pub struct Document { /// first insertion class on a page. Zero by default for compatibility. #[serde(default)] pub insertion_skip: i64, + /// TeX floatsep: vertical space between two floats of the same placement + /// on one page. Defaults to 12pt. + #[serde(default = "default_float_sep")] + pub float_sep: i64, } fn default_text_fraction() -> u8 { 50 } +fn default_float_sep() -> i64 { + 12 * 65_536 +} + fn default_bottom_fraction() -> u8 { 30 } @@ -614,6 +627,7 @@ impl Document { total_number: default_total_number(), insertion_classes: Vec::new(), insertion_skip: 0, + float_sep: default_float_sep(), } } @@ -704,7 +718,7 @@ fn top_float_height(page: &Page) -> i64 { page.floats .iter() .filter(|float| float.placement == FloatPlacement::Top) - .map(|float| float.height) + .map(|float| float.height + float.gap_before) .sum() } @@ -712,7 +726,7 @@ fn bottom_float_height(page: &Page) -> i64 { page.floats .iter() .filter(|float| float.placement == FloatPlacement::Bottom) - .map(|float| float.height) + .map(|float| float.height + float.gap_before) .sum() } @@ -1064,23 +1078,31 @@ impl Engine { .floats .iter() .filter(|entry| entry.placement == FloatPlacement::Top) - .map(|entry| entry.height) + .map(|entry| entry.height + entry.gap_before) .sum(); let top_count = current .floats .iter() .filter(|entry| entry.placement == FloatPlacement::Top) .count(); + let top_gap = if top_count > 0 { + document.float_sep.max(0) + } else { + 0 + }; 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 + && used + top_gap + float.height + <= document.page_goal - insertion_height + && (existing_top + top_gap + float.height) * 100 <= i64::from(document.top_fraction_percent) * document.page_goal; if fits_on_current { - used += float.height; - current.floats.push(float.clone()); + let mut placed = float.clone(); + placed.gap_before = top_gap; + used += top_gap + float.height; + current.floats.push(placed); continue; } if !current.items.is_empty() || !current.insertions.is_empty() { @@ -1091,31 +1113,50 @@ impl Engine { if current.items.is_empty() { used = used.max(document.top_skip); } - used += float.height; - current.floats.push(float.clone()); - } else if !current.items.is_empty() - && current + let gap = if current + .floats + .iter() + .any(|entry| entry.placement == FloatPlacement::Top) + { + document.float_sep.max(0) + } else { + 0 + }; + let mut placed = float.clone(); + placed.gap_before = gap; + used += gap + float.height; + current.floats.push(placed); + } else { + let existing_bottom = bottom_float_height(¤t); + let bottom_count = 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 - - bottom_float_height(¤t) - && (used - document.top_skip - bottom_float_height(¤t)) * 100 - >= document.text_fraction_percent as i64 * document.page_goal - && (float.height + bottom_float_height(¤t)) * 100 - <= document.bottom_fraction_percent as i64 * document.page_goal - { - current.floats.push(float.clone()); - } else { - pages.push(std::mem::replace(&mut current, Page::empty())); - used = document.top_skip; - insertion_height = 0; - current.floats.push(float.clone()); + .count(); + let bottom_gap = if bottom_count > 0 { + document.float_sep.max(0) + } else { + 0 + }; + if !current.items.is_empty() + && bottom_count < usize::from(document.bottom_number) + && current.floats.len() < usize::from(document.total_number) + && used + bottom_gap + float.height + <= document.page_goal - insertion_height - existing_bottom + && (used - document.top_skip - existing_bottom) * 100 + >= document.text_fraction_percent as i64 * document.page_goal + && (float.height + bottom_gap + existing_bottom) * 100 + <= document.bottom_fraction_percent as i64 * document.page_goal + { + let mut placed = float.clone(); + placed.gap_before = bottom_gap; + current.floats.push(placed); + } else { + pages.push(std::mem::replace(&mut current, Page::empty())); + used = document.top_skip; + insertion_height = 0; + current.floats.push(float.clone()); + } } } } @@ -1443,22 +1484,30 @@ impl Engine { .floats .iter() .filter(|entry| entry.placement == FloatPlacement::Top) - .map(|entry| entry.height) + .map(|entry| entry.height + entry.gap_before) .sum(); let top_count = current .floats .iter() .filter(|entry| entry.placement == FloatPlacement::Top) .count(); + let top_gap = if top_count > 0 { + next.float_sep.max(0) + } else { + 0 + }; 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 + && used + top_gap + float.height + <= next.page_goal - insertion_height + && (existing_top + top_gap + float.height) * 100 <= i64::from(next.top_fraction_percent) * next.page_goal; if fits_on_current { - used += float.height; - current.floats.push(float.clone()); + let mut placed = float.clone(); + placed.gap_before = top_gap; + used += top_gap + float.height; + current.floats.push(placed); continue; } if !current.items.is_empty() || !current.insertions.is_empty() { @@ -1466,29 +1515,50 @@ impl Engine { used = next.top_skip; insertion_height = 0; } - used += float.height; - current.floats.push(float.clone()); - } else if !current.items.is_empty() - && current + let gap = if current + .floats + .iter() + .any(|entry| entry.placement == FloatPlacement::Top) + { + next.float_sep.max(0) + } else { + 0 + }; + let mut placed = float.clone(); + placed.gap_before = gap; + used += gap + float.height; + current.floats.push(placed); + } else { + let existing_bottom = bottom_float_height(¤t); + let bottom_count = 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 - >= next.text_fraction_percent as i64 * next.page_goal - && (float.height + bottom_float_height(¤t)) * 100 - <= next.bottom_fraction_percent as i64 * next.page_goal - { - current.floats.push(float.clone()); - } else { - pages.push(std::mem::replace(&mut current, Page::empty())); - used = next.top_skip; - insertion_height = 0; - current.floats.push(float.clone()); + .count(); + let bottom_gap = if bottom_count > 0 { + next.float_sep.max(0) + } else { + 0 + }; + if !current.items.is_empty() + && bottom_count < usize::from(next.bottom_number) + && current.floats.len() < usize::from(next.total_number) + && used + bottom_gap + float.height + <= next.page_goal - insertion_height - existing_bottom + && (used - next.top_skip - existing_bottom) * 100 + >= next.text_fraction_percent as i64 * next.page_goal + && (float.height + bottom_gap + existing_bottom) * 100 + <= next.bottom_fraction_percent as i64 * next.page_goal + { + let mut placed = float.clone(); + placed.gap_before = bottom_gap; + current.floats.push(placed); + } else { + pages.push(std::mem::replace(&mut current, Page::empty())); + used = next.top_skip; + insertion_height = 0; + current.floats.push(float.clone()); + } } } } @@ -1826,6 +1896,7 @@ mod tests { height: 10, }), VerticalItem::Float(Float { + gap_before: 0, placement: FloatPlacement::Top, width: 40, height: 95, @@ -1857,6 +1928,7 @@ mod tests { height: 10, }), VerticalItem::Float(Float { + gap_before: 0, placement: FloatPlacement::Top, width: 40, height: 20, @@ -2320,6 +2392,7 @@ mod tests { height: 30, }), VerticalItem::Float(Float { + gap_before: 0, placement: FloatPlacement::Bottom, width: 40, height: 30, @@ -2352,6 +2425,7 @@ mod tests { height: 80, }), VerticalItem::Float(Float { + gap_before: 0, placement: FloatPlacement::Bottom, width: 40, height: 50, @@ -2378,6 +2452,7 @@ mod tests { paragraph_style: None, items: vec![ VerticalItem::Float(Float { + gap_before: 0, placement: FloatPlacement::Top, width: 40, height: 80, @@ -2704,12 +2779,14 @@ mod tests { marks: Vec::new(), floats: vec![ Float { + gap_before: 0, placement: FloatPlacement::Top, width: 50 * 65_536, height: 15 * 65_536, content: Vec::new(), }, Float { + gap_before: 0, placement: FloatPlacement::Bottom, width: 60 * 65_536, height: 10 * 65_536, @@ -3322,6 +3399,37 @@ mod tests { assert!(svg.contains(" = page + .display_list() + .items + .iter() + .filter_map(|item| match item { + DisplayItem::Rule { y, height: 100, .. } => Some(*y), + _ => None, + }) + .collect(); + assert_eq!(ys, vec![0, 150]); + engine.edit(&document, 3, "changed body"); + assert_eq!(engine.pages()[0].floats[1].gap_before, 50); + } + #[test] fn float_height_covers_its_content() { let unit = Unit::float_with_content( diff --git a/crates/texrra-tex-core/src/render.rs b/crates/texrra-tex-core/src/render.rs index 04a8bde..15dc9c8 100755 --- a/crates/texrra-tex-core/src/render.rs +++ b/crates/texrra-tex-core/src/render.rs @@ -326,6 +326,7 @@ impl Page { .iter() .filter(|float| float.placement == FloatPlacement::Top) { + float_y += float.gap_before; Self::paint_float(float, 0, float_y, &mut list); float_y += float.height; } @@ -503,6 +504,7 @@ impl Page { .iter() .filter(|float| float.placement == FloatPlacement::Bottom) { + float_y += float.gap_before; Self::paint_float(float, 0, float_y, &mut list); float_y += float.height; } diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index c0078a0..d86bafc 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -264,7 +264,7 @@ 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 float fractions, float number limits, insertion-class limits and per-class `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 per-class `skip` rules as full layout. `Document::float_sep` (TeX `\floatsep`, default 12pt) reserves a gap between stacked floats of the same placement in both paths. 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.