Size floats from their vertical content

This commit is contained in:
gloamxun committed 2026-10-20 18:42:25 +08:00
1 parent e791c94cfe
commit ea037d66bb
1 file changed
+37
+37
View File
@@ -194,6 +194,24 @@ pub struct Float {
pub content: Vec<VerticalItem>,
}
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("<rect"));
}
#[test]
fn float_height_covers_its_content() {
let unit = Unit::float_with_content(
1,
FloatPlacement::Top,
100,
5,
vec![VerticalItem::Rule {
width: 100,
height: 30,
}],
);
let VerticalItem::Float(float) = &unit.items[0] else {
panic!("expected float");
};
assert_eq!(float.height, 30);
assert_eq!(float.content_height(), 30);
}
#[test]
fn float_carries_vertical_content() {
let unit = Unit::float_with_content(