Carry vertical content on route-D floats

This commit is contained in:
gloamxun committed 2026-10-20 18:37:25 +08:00
1 parent 25adbc7bf0
commit fb22be1a49
1 file changed
+44
+44
View File
@@ -189,6 +189,9 @@ pub struct Float {
pub placement: FloatPlacement,
pub width: i64,
pub height: i64,
/// Vertical material carried by the float. Empty for a dimension-only float.
#[serde(default)]
pub content: Vec<VerticalItem>,
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
@@ -382,10 +385,26 @@ impl Unit {
placement,
width,
height,
content: Vec::new(),
})],
)
}
/// Build a float that carries vertical material.
pub fn float_with_content(
id: UnitId,
placement: FloatPlacement,
width: i64,
height: i64,
content: Vec<VerticalItem>,
) -> Self {
let mut unit = Self::float(id, placement, width, height);
if let Some(VerticalItem::Float(float)) = unit.items.first_mut() {
float.content = content;
}
unit
}
/// A vertical penalty item, corresponding to `\penalty`.
pub fn penalty(id: UnitId, value: i64) -> Self {
Self::vertical(id, vec![VerticalItem::Penalty(value)])
@@ -1266,6 +1285,7 @@ mod tests {
placement: FloatPlacement::Top,
width: 40,
height: 20,
content: Vec::new(),
}),
VerticalItem::Box(VerticalBox {
width: 10,
@@ -1295,6 +1315,7 @@ mod tests {
placement: FloatPlacement::Bottom,
width: 40,
height: 30,
content: Vec::new(),
}),
VerticalItem::Box(VerticalBox {
width: 10,
@@ -1461,11 +1482,13 @@ mod tests {
placement: FloatPlacement::Top,
width: 50 * 65_536,
height: 15 * 65_536,
content: Vec::new(),
},
Float {
placement: FloatPlacement::Bottom,
width: 60 * 65_536,
height: 10 * 65_536,
content: Vec::new(),
},
],
sources: Vec::new(),
@@ -1780,6 +1803,27 @@ mod tests {
assert!(svg.contains(">A</text>"));
assert!(svg.contains("<rect"));
}
#[test]
fn float_carries_vertical_content() {
let unit = Unit::float_with_content(
1,
FloatPlacement::Top,
100,
20,
vec![VerticalItem::Rule {
width: 100,
height: 2,
}],
);
let VerticalItem::Float(float) = &unit.items[0] else {
panic!("expected float");
};
assert_eq!(float.content.len(), 1);
let encoded = serde_json::to_string(float).unwrap();
let decoded: Float = serde_json::from_str(&encoded).unwrap();
assert_eq!(decoded.content.len(), 1);
}
#[test]
fn mark_and_float_constructors_build_page_items() {
let document = Document::new(