Start float pages at the top margin

This commit is contained in:
gloamxun committed 2026-10-21 12:57:51 +08:00
1 parent d2df1907be
commit 93330a85ef
3 files changed
+61 -5

No files matched your search

+59 -4
View File
@@ -1183,8 +1183,8 @@ impl Engine {
used = document.top_skip;
insertion_height = 0;
}
if current.items.is_empty() {
used = used.max(document.top_skip);
if current.items.is_empty() && current.floats.is_empty() {
used = 0;
}
let gap = if current
.floats
@@ -1230,7 +1230,11 @@ impl Engine {
let mut placed = float.clone();
placed.gap_before = 0;
placed.gap_after = sep_out;
used = used.max(float.height) + sep_out;
used = if current.floats.is_empty() {
float.height
} else {
used + float.height
} + sep_out;
current.items.push(VerticalItem::Float(placed.clone()));
current.floats.push(placed);
}
@@ -1647,6 +1651,9 @@ impl Engine {
used = next.top_skip;
insertion_height = 0;
}
if current.items.is_empty() && current.floats.is_empty() {
used = 0;
}
let gap = if current
.floats
.iter()
@@ -1691,7 +1698,11 @@ impl Engine {
let mut placed = float.clone();
placed.gap_before = 0;
placed.gap_after = sep_out;
used = used.max(float.height) + sep_out;
used = if current.floats.is_empty() {
float.height
} else {
used + float.height
} + sep_out;
current.items.push(VerticalItem::Float(placed.clone()));
current.floats.push(placed);
}
@@ -4138,4 +4149,48 @@ mod tests {
assert_eq!(engine.pages()[0].floats.len(), 1);
assert_eq!(engine.pages()[0].floats[0].placement, FloatPlacement::Here);
}
#[test]
fn top_float_pages_do_not_consume_topskip() {
let unit = Unit {
id: 0,
source: "top float".to_owned(),
paragraph_style: None,
items: vec![
VerticalItem::Float(Float {
placement: FloatPlacement::Top,
width: 40,
height: 20,
gap_before: 0,
gap_after: 0,
content: Vec::new(),
}),
VerticalItem::Box(VerticalBox {
width: 10,
height: 60,
}),
],
};
let mut document = Document::new(100, vec![unit]);
document.top_skip = 20;
document.text_float_sep = 10;
document.float_page_fraction_percent = 10;
let engine = Engine::layout(&document);
assert_eq!(
engine.pages().len(),
1,
"topskip must not push the body off a float page"
);
let list = engine.pages()[0].display_list_with_layout(100, 10);
let float_y = list
.items
.iter()
.filter_map(|item| match item {
DisplayItem::Rule { y, height: 20, .. } => Some(*y),
_ => None,
})
.min()
.unwrap();
assert_eq!(float_y, 0, "the float starts at the top margin");
}
}