diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index 87e2364..cfb8568 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -517,6 +517,14 @@ pub struct Document { /// TeX-like `\topskip`: distance from the page top to the first baseline. #[serde(default)] pub top_skip: i64, + /// Minimum percentage of the page that text must occupy before a bottom + /// float is allowed onto the page (TeX's `\textfraction`, in percent). + #[serde(default = "default_text_fraction")] + pub text_fraction_percent: u8, +} + +fn default_text_fraction() -> u8 { + 50 } impl Document { @@ -525,6 +533,7 @@ impl Document { units, page_goal, top_skip: 0, + text_fraction_percent: default_text_fraction(), } } @@ -806,6 +815,8 @@ impl Engine { <= 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 { current.floats.push(float.clone()); } else { @@ -1319,6 +1330,28 @@ mod tests { assert_eq!(engine.layout.pages[1].items.len(), 1); } + #[test] + fn text_fraction_keeps_a_bottom_float_off_a_mostly_empty_page() { + let mut document = Document::new( + 100, + vec![ + Unit::vertical( + 1, + vec![VerticalItem::Box(VerticalBox { + width: 10, + height: 10, + })], + ), + Unit::float(2, FloatPlacement::Bottom, 10, 50), + ], + ); + document.top_skip = 0; + document.text_fraction_percent = 50; + let engine = Engine::layout(&document); + assert_eq!(engine.pages().len(), 2); + assert!(engine.pages()[0].floats.is_empty()); + assert_eq!(engine.pages()[1].floats.len(), 1); + } #[test] fn bottom_float_reserves_space_on_its_page() { let unit = Unit {