Reserve floatsep between stacked floats

This commit is contained in:
gloamxun committed 2026-10-21 12:46:24 +08:00
1 parent 521c87d952
commit 1014713ec0
4 files changed
+167 -56

No files matched your search

+162 -54
View File
@@ -191,6 +191,10 @@ pub struct Float {
pub placement: FloatPlacement,
pub width: i64,
pub height: i64,
/// TeX floatsep: space inserted before this float when another float of
/// the same placement already sits on the page.
#[serde(default)]
pub gap_before: i64,
/// Vertical material carried by the float. Empty for a dimension-only float.
#[serde(default)]
pub content: Vec<VerticalItem>,
@@ -405,6 +409,7 @@ impl Unit {
placement,
width,
height,
gap_before: 0,
content: Vec::new(),
})],
)
@@ -573,12 +578,20 @@ pub struct Document {
/// first insertion class on a page. Zero by default for compatibility.
#[serde(default)]
pub insertion_skip: i64,
/// TeX floatsep: vertical space between two floats of the same placement
/// on one page. Defaults to 12pt.
#[serde(default = "default_float_sep")]
pub float_sep: i64,
}
fn default_text_fraction() -> u8 {
50
}
fn default_float_sep() -> i64 {
12 * 65_536
}
fn default_bottom_fraction() -> u8 {
30
}
@@ -614,6 +627,7 @@ impl Document {
total_number: default_total_number(),
insertion_classes: Vec::new(),
insertion_skip: 0,
float_sep: default_float_sep(),
}
}
@@ -704,7 +718,7 @@ fn top_float_height(page: &Page) -> i64 {
page.floats
.iter()
.filter(|float| float.placement == FloatPlacement::Top)
.map(|float| float.height)
.map(|float| float.height + float.gap_before)
.sum()
}
@@ -712,7 +726,7 @@ fn bottom_float_height(page: &Page) -> i64 {
page.floats
.iter()
.filter(|float| float.placement == FloatPlacement::Bottom)
.map(|float| float.height)
.map(|float| float.height + float.gap_before)
.sum()
}
@@ -1064,23 +1078,31 @@ impl Engine {
.floats
.iter()
.filter(|entry| entry.placement == FloatPlacement::Top)
.map(|entry| entry.height)
.map(|entry| entry.height + entry.gap_before)
.sum();
let top_count = current
.floats
.iter()
.filter(|entry| entry.placement == FloatPlacement::Top)
.count();
let top_gap = if top_count > 0 {
document.float_sep.max(0)
} else {
0
};
let fits_on_current = !current.items.is_empty()
&& top_count < usize::from(document.top_number)
&& current.floats.len() < usize::from(document.total_number)
&& used + float.height <= document.page_goal - insertion_height
&& (existing_top + float.height) * 100
&& used + top_gap + float.height
<= document.page_goal - insertion_height
&& (existing_top + top_gap + float.height) * 100
<= i64::from(document.top_fraction_percent)
* document.page_goal;
if fits_on_current {
used += float.height;
current.floats.push(float.clone());
let mut placed = float.clone();
placed.gap_before = top_gap;
used += top_gap + float.height;
current.floats.push(placed);
continue;
}
if !current.items.is_empty() || !current.insertions.is_empty() {
@@ -1091,31 +1113,50 @@ impl Engine {
if current.items.is_empty() {
used = used.max(document.top_skip);
}
used += float.height;
current.floats.push(float.clone());
} else if !current.items.is_empty()
&& current
let gap = if current
.floats
.iter()
.any(|entry| entry.placement == FloatPlacement::Top)
{
document.float_sep.max(0)
} else {
0
};
let mut placed = float.clone();
placed.gap_before = gap;
used += gap + float.height;
current.floats.push(placed);
} else {
let existing_bottom = bottom_float_height(&current);
let bottom_count = current
.floats
.iter()
.filter(|entry| entry.placement == FloatPlacement::Bottom)
.count()
< usize::from(document.bottom_number)
&& current.floats.len() < usize::from(document.total_number)
&& used + float.height
<= document.page_goal
- insertion_height
- bottom_float_height(&current)
&& (used - document.top_skip - bottom_float_height(&current)) * 100
>= document.text_fraction_percent as i64 * document.page_goal
&& (float.height + bottom_float_height(&current)) * 100
<= document.bottom_fraction_percent as i64 * document.page_goal
{
current.floats.push(float.clone());
} else {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
insertion_height = 0;
current.floats.push(float.clone());
.count();
let bottom_gap = if bottom_count > 0 {
document.float_sep.max(0)
} else {
0
};
if !current.items.is_empty()
&& bottom_count < usize::from(document.bottom_number)
&& current.floats.len() < usize::from(document.total_number)
&& used + bottom_gap + float.height
<= document.page_goal - insertion_height - existing_bottom
&& (used - document.top_skip - existing_bottom) * 100
>= document.text_fraction_percent as i64 * document.page_goal
&& (float.height + bottom_gap + existing_bottom) * 100
<= document.bottom_fraction_percent as i64 * document.page_goal
{
let mut placed = float.clone();
placed.gap_before = bottom_gap;
current.floats.push(placed);
} else {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
insertion_height = 0;
current.floats.push(float.clone());
}
}
}
}
@@ -1443,22 +1484,30 @@ impl Engine {
.floats
.iter()
.filter(|entry| entry.placement == FloatPlacement::Top)
.map(|entry| entry.height)
.map(|entry| entry.height + entry.gap_before)
.sum();
let top_count = current
.floats
.iter()
.filter(|entry| entry.placement == FloatPlacement::Top)
.count();
let top_gap = if top_count > 0 {
next.float_sep.max(0)
} else {
0
};
let fits_on_current = !current.items.is_empty()
&& top_count < usize::from(next.top_number)
&& current.floats.len() < usize::from(next.total_number)
&& used + float.height <= next.page_goal - insertion_height
&& (existing_top + float.height) * 100
&& used + top_gap + float.height
<= next.page_goal - insertion_height
&& (existing_top + top_gap + float.height) * 100
<= i64::from(next.top_fraction_percent) * next.page_goal;
if fits_on_current {
used += float.height;
current.floats.push(float.clone());
let mut placed = float.clone();
placed.gap_before = top_gap;
used += top_gap + float.height;
current.floats.push(placed);
continue;
}
if !current.items.is_empty() || !current.insertions.is_empty() {
@@ -1466,29 +1515,50 @@ impl Engine {
used = next.top_skip;
insertion_height = 0;
}
used += float.height;
current.floats.push(float.clone());
} else if !current.items.is_empty()
&& current
let gap = if current
.floats
.iter()
.any(|entry| entry.placement == FloatPlacement::Top)
{
next.float_sep.max(0)
} else {
0
};
let mut placed = float.clone();
placed.gap_before = gap;
used += gap + float.height;
current.floats.push(placed);
} else {
let existing_bottom = bottom_float_height(&current);
let bottom_count = current
.floats
.iter()
.filter(|entry| entry.placement == FloatPlacement::Bottom)
.count()
< usize::from(next.bottom_number)
&& current.floats.len() < usize::from(next.total_number)
&& used + float.height
<= next.page_goal - insertion_height - bottom_float_height(&current)
&& (used - next.top_skip - bottom_float_height(&current)) * 100
>= next.text_fraction_percent as i64 * next.page_goal
&& (float.height + bottom_float_height(&current)) * 100
<= next.bottom_fraction_percent as i64 * next.page_goal
{
current.floats.push(float.clone());
} else {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = next.top_skip;
insertion_height = 0;
current.floats.push(float.clone());
.count();
let bottom_gap = if bottom_count > 0 {
next.float_sep.max(0)
} else {
0
};
if !current.items.is_empty()
&& bottom_count < usize::from(next.bottom_number)
&& current.floats.len() < usize::from(next.total_number)
&& used + bottom_gap + float.height
<= next.page_goal - insertion_height - existing_bottom
&& (used - next.top_skip - existing_bottom) * 100
>= next.text_fraction_percent as i64 * next.page_goal
&& (float.height + bottom_gap + existing_bottom) * 100
<= next.bottom_fraction_percent as i64 * next.page_goal
{
let mut placed = float.clone();
placed.gap_before = bottom_gap;
current.floats.push(placed);
} else {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = next.top_skip;
insertion_height = 0;
current.floats.push(float.clone());
}
}
}
}
@@ -1826,6 +1896,7 @@ mod tests {
height: 10,
}),
VerticalItem::Float(Float {
gap_before: 0,
placement: FloatPlacement::Top,
width: 40,
height: 95,
@@ -1857,6 +1928,7 @@ mod tests {
height: 10,
}),
VerticalItem::Float(Float {
gap_before: 0,
placement: FloatPlacement::Top,
width: 40,
height: 20,
@@ -2320,6 +2392,7 @@ mod tests {
height: 30,
}),
VerticalItem::Float(Float {
gap_before: 0,
placement: FloatPlacement::Bottom,
width: 40,
height: 30,
@@ -2352,6 +2425,7 @@ mod tests {
height: 80,
}),
VerticalItem::Float(Float {
gap_before: 0,
placement: FloatPlacement::Bottom,
width: 40,
height: 50,
@@ -2378,6 +2452,7 @@ mod tests {
paragraph_style: None,
items: vec![
VerticalItem::Float(Float {
gap_before: 0,
placement: FloatPlacement::Top,
width: 40,
height: 80,
@@ -2704,12 +2779,14 @@ mod tests {
marks: Vec::new(),
floats: vec![
Float {
gap_before: 0,
placement: FloatPlacement::Top,
width: 50 * 65_536,
height: 15 * 65_536,
content: Vec::new(),
},
Float {
gap_before: 0,
placement: FloatPlacement::Bottom,
width: 60 * 65_536,
height: 10 * 65_536,
@@ -3322,6 +3399,37 @@ mod tests {
assert!(svg.contains("<rect"));
}
#[test]
fn floatsep_separates_stacked_top_floats() {
let mut document = Document::new(
1000,
vec![
Unit::float(1, FloatPlacement::Top, 100, 100),
Unit::float(2, FloatPlacement::Top, 100, 100),
Unit::paragraph(3, "body"),
],
);
document.top_skip = 0;
document.float_sep = 50;
let mut engine = Engine::layout(&document);
let page = &engine.pages()[0];
assert_eq!(page.floats.len(), 2);
assert_eq!(page.floats[0].gap_before, 0);
assert_eq!(page.floats[1].gap_before, 50);
let ys: Vec<i64> = page
.display_list()
.items
.iter()
.filter_map(|item| match item {
DisplayItem::Rule { y, height: 100, .. } => Some(*y),
_ => None,
})
.collect();
assert_eq!(ys, vec![0, 150]);
engine.edit(&document, 3, "changed body");
assert_eq!(engine.pages()[0].floats[1].gap_before, 50);
}
#[test]
fn float_height_covers_its_content() {
let unit = Unit::float_with_content(