diff --git a/README.md b/README.md index f78d82c..1eec1d2 100644 --- a/README.md +++ b/README.md @@ -474,6 +474,7 @@ Route-D float placement mirrors TeX fractions: - `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. - `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`. +- `Document::text_float_sep` defaults to 20pt (`\textfloatsep`); a page that carries both floats and body text reserves one such gap between them. `Page::display_list_with_layout(page_goal, text_float_sep)` mirrors that in rendered output, while `display_list_with_goal` keeps the historic no-gap behaviour for callers without document settings. - `Engine::edit` applies the float fractions, number limits and separations, so incremental layout matches full layout. These glyphs are rasterized at diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index f9c5d05..7a47267 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -582,6 +582,10 @@ pub struct Document { /// on one page. Defaults to 12pt. #[serde(default = "default_float_sep")] pub float_sep: i64, + /// TeX's `\textfloatsep`: space between body text and the float area at + /// the top or bottom of a text page. Defaults to 20pt. + #[serde(default = "default_text_float_sep")] + pub text_float_sep: i64, } fn default_text_fraction() -> u8 { @@ -592,6 +596,10 @@ fn default_float_sep() -> i64 { 12 * 65_536 } +fn default_text_float_sep() -> i64 { + 20 * 65_536 +} + fn default_bottom_fraction() -> u8 { 30 } @@ -628,6 +636,7 @@ impl Document { insertion_classes: Vec::new(), insertion_skip: 0, float_sep: default_float_sep(), + text_float_sep: default_text_float_sep(), } } @@ -730,6 +739,17 @@ fn bottom_float_height(page: &Page) -> i64 { .sum() } +/// Space reserved above the bottom float area: the floats themselves plus +/// `\textfloatsep` once the page carries a bottom float. +fn bottom_float_area(page: &Page, text_float_sep: i64) -> i64 { + let height = bottom_float_height(page); + if height > 0 { + height + text_float_sep.max(0) + } else { + height + } +} + /// 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 { @@ -737,6 +757,17 @@ fn top_float_exceeds_page_fraction(page: &Page, page_goal: i64, percent: u8) -> height > 0 && page.items.is_empty() && height * 100 > i64::from(percent) * page_goal } +fn is_body_item(item: &VerticalItem) -> bool { + matches!( + item, + VerticalItem::Box(_) + | VerticalItem::Rule { .. } + | VerticalItem::Halign(_) + | VerticalItem::Paragraph(_) + | VerticalItem::Math { .. } + ) +} + #[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub struct Checkpoint { /// Index of the next unit to execute. @@ -848,6 +879,10 @@ impl Engine { used = document.top_skip; insertion_height = 0; } + if is_body_item(item) && current.items.is_empty() && top_float_height(¤t) > 0 + { + used += document.text_float_sep.max(0); + } match item { VerticalItem::Rule { height, .. } => { if top_float_exceeds_page_fraction( @@ -862,7 +897,9 @@ impl Engine { if current.items.is_empty() { used = document.top_skip.max(*height); } else if used + *height - > document.page_goal - insertion_height - bottom_float_height(¤t) + > document.page_goal + - insertion_height + - bottom_float_area(¤t, document.text_float_sep) { pages.push(std::mem::replace(&mut current, Page::empty())); used = document.top_skip.max(*height); @@ -885,7 +922,9 @@ impl Engine { if current.items.is_empty() && top_float_height(¤t) == 0 { used = document.top_skip.max(vertical_box.height); } else if used + vertical_box.height - > document.page_goal - insertion_height - bottom_float_height(¤t) + > document.page_goal + - insertion_height + - bottom_float_area(¤t, document.text_float_sep) { pages.push(std::mem::replace(&mut current, Page::empty())); used = document.top_skip.max(vertical_box.height); @@ -902,7 +941,7 @@ impl Engine { document.page_goal - used - insertion_height - - bottom_float_height(¤t) + - bottom_float_area(¤t, document.text_float_sep) }; if !current.items.is_empty() && available < halign.line_height { pages.push(std::mem::replace(&mut current, Page::empty())); @@ -938,7 +977,9 @@ impl Engine { if current.items.is_empty() && top_float_height(¤t) == 0 { used = document.top_skip.max(paragraph.height); } else if used + paragraph.height - > document.page_goal - insertion_height - bottom_float_height(¤t) + > document.page_goal + - insertion_height + - bottom_float_area(¤t, document.text_float_sep) { pages.push(std::mem::replace(&mut current, Page::empty())); used = document.top_skip.max(paragraph.height); @@ -962,7 +1003,9 @@ impl Engine { if current.items.is_empty() && top_float_height(¤t) == 0 { used = document.top_skip.max(amount); } else if used + amount - > document.page_goal - insertion_height - bottom_float_height(¤t) + > document.page_goal + - insertion_height + - bottom_float_area(¤t, document.text_float_sep) { pages.push(std::mem::replace(&mut current, Page::empty())); used = document.top_skip.max(amount); @@ -1090,10 +1133,15 @@ impl Engine { } else { 0 }; + let text_gap = if top_count == 0 { + document.text_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 + top_gap + float.height + && used + text_gap + top_gap + float.height <= document.page_goal - insertion_height && (existing_top + top_gap + float.height) * 100 <= i64::from(document.top_fraction_percent) @@ -1101,7 +1149,7 @@ impl Engine { if fits_on_current { let mut placed = float.clone(); placed.gap_before = top_gap; - used += top_gap + float.height; + used += text_gap + top_gap + float.height; current.floats.push(placed); continue; } @@ -1138,10 +1186,15 @@ impl Engine { } else { 0 }; + let text_gap = if bottom_count == 0 { + document.text_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 + && used + text_gap + 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 @@ -1322,6 +1375,10 @@ impl Engine { used = next.top_skip; insertion_height = 0; } + if is_body_item(item) && current.items.is_empty() && top_float_height(¤t) > 0 + { + used += next.text_float_sep.max(0); + } match item { VerticalItem::Rule { height, .. } => { if top_float_exceeds_page_fraction( @@ -1336,7 +1393,9 @@ impl Engine { if current.items.is_empty() { used = next.top_skip.max(*height); } else if used + *height - > next.page_goal - insertion_height - bottom_float_height(¤t) + > next.page_goal + - insertion_height + - bottom_float_area(¤t, next.text_float_sep) { pages.push(std::mem::replace(&mut current, Page::empty())); used = next.top_skip.max(*height); @@ -1359,7 +1418,9 @@ impl Engine { if current.items.is_empty() && top_float_height(¤t) == 0 { used = next.top_skip.max(vertical_box.height); } else if used + vertical_box.height - > next.page_goal - insertion_height - bottom_float_height(¤t) + > next.page_goal + - insertion_height + - bottom_float_area(¤t, next.text_float_sep) { pages.push(std::mem::replace(&mut current, Page::empty())); used = next.top_skip.max(vertical_box.height); @@ -1373,7 +1434,9 @@ impl Engine { if current.items.is_empty() && top_float_height(¤t) == 0 { used = next.top_skip.max(halign.height); } else if used + halign.height - > next.page_goal - insertion_height - bottom_float_height(¤t) + > next.page_goal + - insertion_height + - bottom_float_area(¤t, next.text_float_sep) { pages.push(std::mem::replace(&mut current, Page::empty())); used = next.top_skip.max(halign.height); @@ -1396,7 +1459,9 @@ impl Engine { if current.items.is_empty() && top_float_height(¤t) == 0 { used = next.top_skip.max(paragraph.height); } else if used + paragraph.height - > next.page_goal - insertion_height - bottom_float_height(¤t) + > next.page_goal + - insertion_height + - bottom_float_area(¤t, next.text_float_sep) { pages.push(std::mem::replace(&mut current, Page::empty())); used = next.top_skip.max(paragraph.height); @@ -1420,7 +1485,9 @@ impl Engine { if current.items.is_empty() && top_float_height(¤t) == 0 { used = next.top_skip.max(amount); } else if used + amount - > next.page_goal - insertion_height - bottom_float_height(¤t) + > next.page_goal + - insertion_height + - bottom_float_area(¤t, next.text_float_sep) { pages.push(std::mem::replace(&mut current, Page::empty())); used = next.top_skip.max(amount); @@ -1496,17 +1563,22 @@ impl Engine { } else { 0 }; + let text_gap = if top_count == 0 { + next.text_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 + top_gap + float.height + && used + text_gap + 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 { let mut placed = float.clone(); placed.gap_before = top_gap; - used += top_gap + float.height; + used += text_gap + top_gap + float.height; current.floats.push(placed); continue; } @@ -1540,10 +1612,15 @@ impl Engine { } else { 0 }; + let text_gap = if bottom_count == 0 { + next.text_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 + && used + text_gap + 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 @@ -1940,7 +2017,9 @@ mod tests { }), ], }; - let engine = Engine::layout(&Document::new(100, vec![unit])); + let mut document = Document::new(100, vec![unit]); + document.text_float_sep = 0; + 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(), 2); @@ -1963,6 +2042,7 @@ mod tests { ); document.float_page_fraction_percent = 60; document.text_fraction_percent = 10; + document.text_float_sep = 0; assert_eq!(Engine::layout(&document).pages().len(), 2); document.float_page_fraction_percent = 10; assert_eq!(Engine::layout(&document).pages().len(), 1); @@ -2067,6 +2147,7 @@ mod tests { ); document.top_number = 1; document.total_number = 10; + document.text_float_sep = 0; let engine = Engine::layout(&document); assert_eq!(engine.pages().len(), 3); for page in engine.pages() { @@ -2094,6 +2175,7 @@ mod tests { document.bottom_number = 5; document.total_number = 1; document.text_fraction_percent = 0; + document.text_float_sep = 0; let engine = Engine::layout(&document); assert_eq!(engine.pages().len(), 2); assert_eq!(engine.pages()[0].floats.len(), 1); @@ -2118,8 +2200,9 @@ mod tests { ); document.top_number = 1; document.total_number = 10; + document.text_float_sep = 0; let mut engine = Engine::layout(&document); - let edited = Document::new( + let mut edited = Document::new( 1000, vec![ Unit::paragraph(0, "alpha changed"), @@ -2128,6 +2211,7 @@ mod tests { Unit::float(3, FloatPlacement::Top, 10, 100), ], ); + edited.text_float_sep = 0; engine.edit(&edited, 1, "alpha changed"); assert_eq!(engine.pages().len(), 2); for page in engine.pages() { @@ -2406,6 +2490,7 @@ mod tests { }; let mut document = Document::new(60, vec![unit]); document.bottom_fraction_percent = 100; + document.text_float_sep = 0; let engine = Engine::layout(&document); assert_eq!(engine.layout.pages.len(), 2); assert_eq!(engine.layout.pages[0].floats.len(), 1); @@ -2467,6 +2552,7 @@ mod tests { let mut document = Document::new(100, vec![unit]); document.top_skip = 0; document.float_page_fraction_percent = 10; + document.text_float_sep = 0; let engine = Engine::layout(&document); assert_eq!( engine.layout.pages.len(), @@ -3281,6 +3367,7 @@ mod tests { let mut document = Document::new(100, Vec::new()); document.top_skip = 0; document.text_fraction_percent = 0; + document.text_float_sep = 0; document.units.push(Unit::vertical( 1, vec![VerticalItem::Box(VerticalBox { @@ -3314,6 +3401,7 @@ mod tests { fn float_content_is_painted_at_the_top_of_the_page() { let mut document = Document::new(1000, Vec::new()); document.top_skip = 0; + document.text_float_sep = 0; document.units.push(Unit::float_with_content( 1, FloatPlacement::Top, @@ -3430,6 +3518,143 @@ mod tests { assert_eq!(engine.pages()[0].floats[1].gap_before, 50); } + #[test] + fn text_float_sep_keeps_body_below_top_floats() { + let mut document = Document::new( + 1000, + vec![ + Unit::float(1, FloatPlacement::Top, 100, 100), + Unit::float(2, FloatPlacement::Top, 100, 100), + Unit::rule(3, 100, 10), + ], + ); + document.top_skip = 0; + document.float_sep = 50; + document.text_float_sep = 30; + let engine = Engine::layout(&document); + let page = &engine.pages()[0]; + assert_eq!(page.floats.len(), 2); + let ys: Vec = page + .display_list_with_layout(1000, document.text_float_sep) + .items + .iter() + .filter_map(|item| match item { + DisplayItem::Rule { y, height: 10, .. } => Some(*y), + _ => None, + }) + .collect(); + assert_eq!( + ys, + vec![280], + "body starts after both floats, floatsep, and textfloatsep" + ); + } + + #[test] + fn text_float_sep_separates_body_from_bottom_floats() { + fn document(text_float_sep: i64) -> Document { + let mut document = Document::new( + 1000, + vec![ + Unit::rule(1, 100, 800), + Unit::float_with_content( + 2, + FloatPlacement::Bottom, + 100, + 80, + vec![VerticalItem::Rule { + width: 100, + height: 80, + }], + ), + ], + ); + document.top_skip = 0; + document.text_float_sep = text_float_sep; + document + } + assert_eq!(Engine::layout(&document(0)).pages().len(), 1); + assert_eq!( + Engine::layout(&document(200)).pages().len(), + 2, + "a 200pt separation no longer leaves room for the bottom float" + ); + let document = document(30); + let engine = Engine::layout(&document); + assert_eq!(engine.pages().len(), 1); + let ys: Vec = engine.pages()[0] + .display_list_with_layout(1000, 30) + .items + .iter() + .filter_map(|item| match item { + DisplayItem::Rule { y, height: 80, .. } => Some(*y), + _ => None, + }) + .collect(); + assert_eq!(ys, vec![920], "bottom float sits below the text separation"); + } + + #[test] + fn text_float_sep_is_counted_once_for_a_late_top_float() { + let mut document = Document::new( + 1000, + vec![ + Unit::rule(1, 100, 10), + Unit::float(2, FloatPlacement::Top, 100, 100), + Unit::rule(3, 100, 10), + ], + ); + document.top_skip = 0; + document.text_float_sep = 30; + let engine = Engine::layout(&document); + let page = &engine.pages()[0]; + assert_eq!(page.floats.len(), 1); + let ys: Vec = page + .display_list_with_layout(1000, document.text_float_sep) + .items + .iter() + .filter_map(|item| match item { + DisplayItem::Rule { y, height: 10, .. } => Some(*y), + _ => None, + }) + .collect(); + assert_eq!( + ys, + vec![130, 140], + "the late float reserves one textfloatsep" + ); + } + + #[test] + fn incremental_layout_honours_text_float_sep() { + let mut document = Document::new( + 1000, + vec![ + Unit::float(1, FloatPlacement::Top, 100, 100), + Unit::paragraph(2, "body"), + ], + ); + document.top_skip = 0; + document.text_float_sep = 30; + let mut engine = Engine::layout(&document); + fn body_y(engine: &Engine) -> i64 { + engine.pages()[0] + .display_list_with_layout(1000, 30) + .items + .iter() + .filter_map(|item| match item { + DisplayItem::Text { y, text, .. } if text == "b" => Some(*y), + _ => None, + }) + .min() + .expect("body glyph") + } + let initial = body_y(&engine); + assert!(initial >= 130, "body starts below the reserved separation"); + engine.edit(&document, 2, "changed body"); + assert!(body_y(&engine) >= 130); + } + #[test] fn float_height_covers_its_content() { let unit = Unit::float_with_content( @@ -3470,7 +3695,7 @@ mod tests { } #[test] fn mark_and_float_constructors_build_page_items() { - let document = Document::new( + let mut document = Document::new( 100, vec![ Unit::mark(1, 0, "Intro"), @@ -3484,6 +3709,7 @@ mod tests { ), ], ); + document.text_float_sep = 0; let engine = Engine::layout(&document); assert_eq!(engine.pages().len(), 1); assert_eq!(engine.pages()[0].marks[0].text, "Intro"); diff --git a/crates/texrra-tex-core/src/render.rs b/crates/texrra-tex-core/src/render.rs index 15dc9c8..916fc84 100755 --- a/crates/texrra-tex-core/src/render.rs +++ b/crates/texrra-tex-core/src/render.rs @@ -312,13 +312,19 @@ impl Page { /// Paint the page, placing insertion material at `page_goal` when it is /// positive. A zero goal keeps insertion material in normal flow. pub fn display_list_with_goal(&self, page_goal: i64) -> DisplayList { + self.display_list_with_layout(page_goal, 0) + } + + /// Paint the page with TeX's `\textfloatsep` kept between body text and a + /// float area, matching the space the page builder reserved. + pub fn display_list_with_layout(&self, page_goal: i64, text_float_sep: i64) -> DisplayList { let mut list = DisplayList::default(); let mut y; let top_height: i64 = self .floats .iter() .filter(|float| float.placement == FloatPlacement::Top) - .map(|float| float.height) + .map(|float| float.height + float.gap_before) .sum(); let mut float_y = 0_i64; for float in self @@ -330,7 +336,22 @@ impl Page { Self::paint_float(float, 0, float_y, &mut list); float_y += float.height; } - y = top_height; + let has_body = self.items.iter().any(|item| { + matches!( + item, + VerticalItem::Box(_) + | VerticalItem::Rule { .. } + | VerticalItem::Halign(_) + | VerticalItem::Paragraph(_) + | VerticalItem::Math { .. } + | VerticalItem::Glue(_) + ) + }); + y = if top_height > 0 && has_body { + top_height + text_float_sep.max(0) + } else { + top_height + }; let body_width = self .items .iter() @@ -495,10 +516,11 @@ impl Page { .floats .iter() .filter(|float| float.placement == FloatPlacement::Bottom) - .map(|float| float.height) + .map(|float| float.height + float.gap_before) .sum(); if bottom_height > 0 { - let mut float_y = (page_goal - bottom_height).max(y); + let bottom_area = bottom_height + text_float_sep.max(0); + let mut float_y = (page_goal - bottom_area).max(y) + text_float_sep.max(0); for float in self .floats .iter() diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index d86bafc..c79623a 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. `Document::float_sep` (TeX `\floatsep`, default 12pt) reserves a gap between stacked floats of the same placement in both paths. +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. `Document::text_float_sep` (TeX `\textfloatsep`, default 20pt) reserves exactly one gap between the float block and the body text on a page that has both, again in both paths; `Page::display_list_with_layout(page_goal, text_float_sep)` applies the same gap when rendering, while `display_list_with_goal` stays gap-free for callers that do not know the document settings. 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.