From ea037d66bb861b88485d424ff3ca7ecb7a23d407 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 18:42:25 +0800 Subject: [PATCH] Size floats from their vertical content --- crates/texrra-tex-core/src/lib.rs | 37 +++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index e69452f..87e2364 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -194,6 +194,24 @@ pub struct Float { pub content: Vec, } +impl Float { + /// Natural height of the float's vertical material. + pub fn content_height(&self) -> i64 { + self.content + .iter() + .map(|item| match item { + VerticalItem::Box(box_) => box_.height, + VerticalItem::Rule { height, .. } => *height, + VerticalItem::Glue(height) => *height, + VerticalItem::Halign(halign) => halign.height, + VerticalItem::Paragraph(paragraph) => paragraph.height, + VerticalItem::Math { height, depth, .. } => height + depth, + _ => 0, + }) + .sum() + } +} + #[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub struct Unit { pub id: UnitId, @@ -401,6 +419,7 @@ impl Unit { let mut unit = Self::float(id, placement, width, height); if let Some(VerticalItem::Float(float)) = unit.items.first_mut() { float.content = content; + float.height = float.height.max(float.content_height()); } unit } @@ -1842,6 +1861,24 @@ mod tests { assert!(svg.contains("