From e791c94cfea7ff5670f797dd218ff70d81e2d42e Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 18:40:37 +0800 Subject: [PATCH] Paint float content in the route-D display list --- crates/texrra-tex-core/src/lib.rs | 38 ++++++++++++++++++++++++ crates/texrra-tex-core/src/render.rs | 44 +++++++++++++++++++--------- 2 files changed, 68 insertions(+), 14 deletions(-) diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index 369a866..e69452f 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -1753,6 +1753,44 @@ mod tests { let svg = list.to_svg(); assert!(svg.contains(">x")); } + #[test] + fn float_content_is_painted_at_the_top_of_the_page() { + let mut document = Document::new(1000, Vec::new()); + document.top_skip = 0; + document.units.push(Unit::float_with_content( + 1, + FloatPlacement::Top, + 100, + 20, + vec![VerticalItem::Rule { + width: 80, + height: 2, + }], + )); + document.units.push(Unit::paragraph(2, "hello world")); + let engine = Engine::layout(&document); + let list = engine.pages()[0].display_list(); + assert!(list.items.iter().any(|item| matches!( + item, + DisplayItem::Rule { + y: 0, + width: 80, + height: 2, + .. + } + ))); + let text_y = list + .items + .iter() + .filter_map(|item| match item { + DisplayItem::Text { y, text, .. } if text == "h" => Some(y), + _ => None, + }) + .min() + .unwrap(); + assert!(*text_y >= 20, "body text starts below the top float"); + } + #[test] fn paragraph_display_list_paints_characters() { let document = Document::new(400, vec![Unit::paragraph(1, "hello world")]); diff --git a/crates/texrra-tex-core/src/render.rs b/crates/texrra-tex-core/src/render.rs index 2a0472d..5bbe52a 100755 --- a/crates/texrra-tex-core/src/render.rs +++ b/crates/texrra-tex-core/src/render.rs @@ -8,7 +8,7 @@ use crate::cmr10::Cmr10Metrics; use crate::math::{self, MathAtom}; use crate::paragraph::ParagraphFont; -use crate::{FloatPlacement, Halign, HorizontalItem, Page, VerticalItem}; +use crate::{Float, FloatPlacement, Halign, HorizontalItem, Page, VerticalItem}; /// One absolute drawing primitive in scaled points. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] @@ -283,12 +283,7 @@ impl Page { .iter() .filter(|float| float.placement == FloatPlacement::Top) { - list.push(DisplayItem::Rule { - x: 0, - y: float_y, - width: float.width, - height: float.height, - }); + Self::paint_float(float, 0, float_y, &mut list); float_y += float.height; } y = top_height; @@ -435,14 +430,8 @@ impl Page { .iter() .filter(|float| float.placement == FloatPlacement::Bottom) { - list.push(DisplayItem::Rule { - x: 0, - y: float_y, - width: float.width, - height: float.height, - }); + Self::paint_float(float, 0, float_y, &mut list); float_y += float.height; - list.width = list.width.max(float.width); } y = y.max(float_y); } @@ -462,6 +451,33 @@ impl Page { list } + /// Paint a float's vertical material, or a dimension rectangle when the + /// float carries no content. + fn paint_float(float: &Float, x: i64, y: i64, list: &mut DisplayList) { + if float.content.is_empty() { + list.push(DisplayItem::Rule { + x, + y, + width: float.width, + height: float.height, + }); + list.width = list.width.max(float.width); + return; + } + let page = Page { + items: float.content.clone(), + insertions: Vec::new(), + marks: Vec::new(), + floats: Vec::new(), + sources: Vec::new(), + hash: 0, + }; + let mut content = page.display_list(); + content.place(x, y); + list.items.extend(content.items); + list.images.extend(content.images); + list.width = list.width.max(content.width); + } fn paint_math( atom: &MathAtom, font: &Cmr10Metrics,