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
+149 -38

No files matched your search

+2 -1
View File
@@ -473,7 +473,8 @@ Route-D float placement mirrors TeX fractions:
- `Document::top_fraction_percent` defaults to 70 (`\topfraction`); a taller top float keeps body text off its page. - `Document::top_fraction_percent` defaults to 70 (`\topfraction`); a taller top float keeps body text off its page.
- `Document::bottom_fraction_percent` defaults to 30 (`\bottomfraction`); a taller bottom float moves to the next page. - `Document::bottom_fraction_percent` defaults to 30 (`\bottomfraction`); a taller bottom float moves to the next page.
- `Document::top_number`, `bottom_number` and `total_number` default to TeX's 2, 1 and 12; they cap top floats, bottom floats and all floats on one page. - `Document::top_number`, `bottom_number` and `total_number` default to TeX's 2, 1 and 12; they cap top floats, bottom floats and all floats on one page.
- `Engine::edit` applies the float fractions and number limits, so incremental layout matches full layout. - `Document::float_sep` defaults to 12pt (`\floatsep`); stacked top floats or stacked bottom floats reserve that gap between each other, in full layout and after `Engine::edit`.
- `Engine::edit` applies the float fractions, number limits and separations, so incremental layout matches full layout.
These glyphs are rasterized at These glyphs are rasterized at
600 dpi and shared by PDF and SVG. Ordinary math atom glyphs are rasterized from 600 dpi and shared by PDF and SVG. Ordinary math atom glyphs are rasterized from
+144 -36
View File
@@ -191,6 +191,10 @@ pub struct Float {
pub placement: FloatPlacement, pub placement: FloatPlacement,
pub width: i64, pub width: i64,
pub height: 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. /// Vertical material carried by the float. Empty for a dimension-only float.
#[serde(default)] #[serde(default)]
pub content: Vec<VerticalItem>, pub content: Vec<VerticalItem>,
@@ -405,6 +409,7 @@ impl Unit {
placement, placement,
width, width,
height, height,
gap_before: 0,
content: Vec::new(), content: Vec::new(),
})], })],
) )
@@ -573,12 +578,20 @@ pub struct Document {
/// first insertion class on a page. Zero by default for compatibility. /// first insertion class on a page. Zero by default for compatibility.
#[serde(default)] #[serde(default)]
pub insertion_skip: i64, 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 { fn default_text_fraction() -> u8 {
50 50
} }
fn default_float_sep() -> i64 {
12 * 65_536
}
fn default_bottom_fraction() -> u8 { fn default_bottom_fraction() -> u8 {
30 30
} }
@@ -614,6 +627,7 @@ impl Document {
total_number: default_total_number(), total_number: default_total_number(),
insertion_classes: Vec::new(), insertion_classes: Vec::new(),
insertion_skip: 0, insertion_skip: 0,
float_sep: default_float_sep(),
} }
} }
@@ -704,7 +718,7 @@ fn top_float_height(page: &Page) -> i64 {
page.floats page.floats
.iter() .iter()
.filter(|float| float.placement == FloatPlacement::Top) .filter(|float| float.placement == FloatPlacement::Top)
.map(|float| float.height) .map(|float| float.height + float.gap_before)
.sum() .sum()
} }
@@ -712,7 +726,7 @@ fn bottom_float_height(page: &Page) -> i64 {
page.floats page.floats
.iter() .iter()
.filter(|float| float.placement == FloatPlacement::Bottom) .filter(|float| float.placement == FloatPlacement::Bottom)
.map(|float| float.height) .map(|float| float.height + float.gap_before)
.sum() .sum()
} }
@@ -1064,23 +1078,31 @@ impl Engine {
.floats .floats
.iter() .iter()
.filter(|entry| entry.placement == FloatPlacement::Top) .filter(|entry| entry.placement == FloatPlacement::Top)
.map(|entry| entry.height) .map(|entry| entry.height + entry.gap_before)
.sum(); .sum();
let top_count = current let top_count = current
.floats .floats
.iter() .iter()
.filter(|entry| entry.placement == FloatPlacement::Top) .filter(|entry| entry.placement == FloatPlacement::Top)
.count(); .count();
let top_gap = if top_count > 0 {
document.float_sep.max(0)
} else {
0
};
let fits_on_current = !current.items.is_empty() let fits_on_current = !current.items.is_empty()
&& top_count < usize::from(document.top_number) && top_count < usize::from(document.top_number)
&& current.floats.len() < usize::from(document.total_number) && current.floats.len() < usize::from(document.total_number)
&& used + float.height <= document.page_goal - insertion_height && used + top_gap + float.height
&& (existing_top + float.height) * 100 <= document.page_goal - insertion_height
&& (existing_top + top_gap + float.height) * 100
<= i64::from(document.top_fraction_percent) <= i64::from(document.top_fraction_percent)
* document.page_goal; * document.page_goal;
if fits_on_current { if fits_on_current {
used += float.height; let mut placed = float.clone();
current.floats.push(float.clone()); placed.gap_before = top_gap;
used += top_gap + float.height;
current.floats.push(placed);
continue; continue;
} }
if !current.items.is_empty() || !current.insertions.is_empty() { if !current.items.is_empty() || !current.insertions.is_empty() {
@@ -1091,26 +1113,44 @@ impl Engine {
if current.items.is_empty() { if current.items.is_empty() {
used = used.max(document.top_skip); used = used.max(document.top_skip);
} }
used += float.height; let gap = if current
current.floats.push(float.clone()); .floats
} else if !current.items.is_empty() .iter()
&& current .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 .floats
.iter() .iter()
.filter(|entry| entry.placement == FloatPlacement::Bottom) .filter(|entry| entry.placement == FloatPlacement::Bottom)
.count() .count();
< usize::from(document.bottom_number) 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) && current.floats.len() < usize::from(document.total_number)
&& used + float.height && used + bottom_gap + float.height
<= document.page_goal <= document.page_goal - insertion_height - existing_bottom
- insertion_height && (used - document.top_skip - existing_bottom) * 100
- bottom_float_height(&current)
&& (used - document.top_skip - bottom_float_height(&current)) * 100
>= document.text_fraction_percent as i64 * document.page_goal >= document.text_fraction_percent as i64 * document.page_goal
&& (float.height + bottom_float_height(&current)) * 100 && (float.height + bottom_gap + existing_bottom) * 100
<= document.bottom_fraction_percent as i64 * document.page_goal <= document.bottom_fraction_percent as i64 * document.page_goal
{ {
current.floats.push(float.clone()); let mut placed = float.clone();
placed.gap_before = bottom_gap;
current.floats.push(placed);
} else { } else {
pages.push(std::mem::replace(&mut current, Page::empty())); pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip; used = document.top_skip;
@@ -1121,6 +1161,7 @@ impl Engine {
} }
} }
} }
}
if !current.items.is_empty() || !current.insertions.is_empty() || !current.floats.is_empty() if !current.items.is_empty() || !current.insertions.is_empty() || !current.floats.is_empty()
{ {
pages.push(current); pages.push(current);
@@ -1443,22 +1484,30 @@ impl Engine {
.floats .floats
.iter() .iter()
.filter(|entry| entry.placement == FloatPlacement::Top) .filter(|entry| entry.placement == FloatPlacement::Top)
.map(|entry| entry.height) .map(|entry| entry.height + entry.gap_before)
.sum(); .sum();
let top_count = current let top_count = current
.floats .floats
.iter() .iter()
.filter(|entry| entry.placement == FloatPlacement::Top) .filter(|entry| entry.placement == FloatPlacement::Top)
.count(); .count();
let top_gap = if top_count > 0 {
next.float_sep.max(0)
} else {
0
};
let fits_on_current = !current.items.is_empty() let fits_on_current = !current.items.is_empty()
&& top_count < usize::from(next.top_number) && top_count < usize::from(next.top_number)
&& current.floats.len() < usize::from(next.total_number) && current.floats.len() < usize::from(next.total_number)
&& used + float.height <= next.page_goal - insertion_height && used + top_gap + float.height
&& (existing_top + float.height) * 100 <= next.page_goal - insertion_height
&& (existing_top + top_gap + float.height) * 100
<= i64::from(next.top_fraction_percent) * next.page_goal; <= i64::from(next.top_fraction_percent) * next.page_goal;
if fits_on_current { if fits_on_current {
used += float.height; let mut placed = float.clone();
current.floats.push(float.clone()); placed.gap_before = top_gap;
used += top_gap + float.height;
current.floats.push(placed);
continue; continue;
} }
if !current.items.is_empty() || !current.insertions.is_empty() { if !current.items.is_empty() || !current.insertions.is_empty() {
@@ -1466,24 +1515,44 @@ impl Engine {
used = next.top_skip; used = next.top_skip;
insertion_height = 0; insertion_height = 0;
} }
used += float.height; let gap = if current
current.floats.push(float.clone()); .floats
} else if !current.items.is_empty() .iter()
&& current .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 .floats
.iter() .iter()
.filter(|entry| entry.placement == FloatPlacement::Bottom) .filter(|entry| entry.placement == FloatPlacement::Bottom)
.count() .count();
< usize::from(next.bottom_number) 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) && current.floats.len() < usize::from(next.total_number)
&& used + float.height && used + bottom_gap + float.height
<= next.page_goal - insertion_height - bottom_float_height(&current) <= next.page_goal - insertion_height - existing_bottom
&& (used - next.top_skip - bottom_float_height(&current)) * 100 && (used - next.top_skip - existing_bottom) * 100
>= next.text_fraction_percent as i64 * next.page_goal >= next.text_fraction_percent as i64 * next.page_goal
&& (float.height + bottom_float_height(&current)) * 100 && (float.height + bottom_gap + existing_bottom) * 100
<= next.bottom_fraction_percent as i64 * next.page_goal <= next.bottom_fraction_percent as i64 * next.page_goal
{ {
current.floats.push(float.clone()); let mut placed = float.clone();
placed.gap_before = bottom_gap;
current.floats.push(placed);
} else { } else {
pages.push(std::mem::replace(&mut current, Page::empty())); pages.push(std::mem::replace(&mut current, Page::empty()));
used = next.top_skip; used = next.top_skip;
@@ -1494,6 +1563,7 @@ impl Engine {
} }
} }
} }
}
if !current.items.is_empty() || !current.insertions.is_empty() || !current.floats.is_empty() if !current.items.is_empty() || !current.insertions.is_empty() || !current.floats.is_empty()
{ {
pages.push(current); pages.push(current);
@@ -1826,6 +1896,7 @@ mod tests {
height: 10, height: 10,
}), }),
VerticalItem::Float(Float { VerticalItem::Float(Float {
gap_before: 0,
placement: FloatPlacement::Top, placement: FloatPlacement::Top,
width: 40, width: 40,
height: 95, height: 95,
@@ -1857,6 +1928,7 @@ mod tests {
height: 10, height: 10,
}), }),
VerticalItem::Float(Float { VerticalItem::Float(Float {
gap_before: 0,
placement: FloatPlacement::Top, placement: FloatPlacement::Top,
width: 40, width: 40,
height: 20, height: 20,
@@ -2320,6 +2392,7 @@ mod tests {
height: 30, height: 30,
}), }),
VerticalItem::Float(Float { VerticalItem::Float(Float {
gap_before: 0,
placement: FloatPlacement::Bottom, placement: FloatPlacement::Bottom,
width: 40, width: 40,
height: 30, height: 30,
@@ -2352,6 +2425,7 @@ mod tests {
height: 80, height: 80,
}), }),
VerticalItem::Float(Float { VerticalItem::Float(Float {
gap_before: 0,
placement: FloatPlacement::Bottom, placement: FloatPlacement::Bottom,
width: 40, width: 40,
height: 50, height: 50,
@@ -2378,6 +2452,7 @@ mod tests {
paragraph_style: None, paragraph_style: None,
items: vec![ items: vec![
VerticalItem::Float(Float { VerticalItem::Float(Float {
gap_before: 0,
placement: FloatPlacement::Top, placement: FloatPlacement::Top,
width: 40, width: 40,
height: 80, height: 80,
@@ -2704,12 +2779,14 @@ mod tests {
marks: Vec::new(), marks: Vec::new(),
floats: vec![ floats: vec![
Float { Float {
gap_before: 0,
placement: FloatPlacement::Top, placement: FloatPlacement::Top,
width: 50 * 65_536, width: 50 * 65_536,
height: 15 * 65_536, height: 15 * 65_536,
content: Vec::new(), content: Vec::new(),
}, },
Float { Float {
gap_before: 0,
placement: FloatPlacement::Bottom, placement: FloatPlacement::Bottom,
width: 60 * 65_536, width: 60 * 65_536,
height: 10 * 65_536, height: 10 * 65_536,
@@ -3322,6 +3399,37 @@ mod tests {
assert!(svg.contains("<rect")); 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] #[test]
fn float_height_covers_its_content() { fn float_height_covers_its_content() {
let unit = Unit::float_with_content( let unit = Unit::float_with_content(
+2
View File
@@ -326,6 +326,7 @@ impl Page {
.iter() .iter()
.filter(|float| float.placement == FloatPlacement::Top) .filter(|float| float.placement == FloatPlacement::Top)
{ {
float_y += float.gap_before;
Self::paint_float(float, 0, float_y, &mut list); Self::paint_float(float, 0, float_y, &mut list);
float_y += float.height; float_y += float.height;
} }
@@ -503,6 +504,7 @@ impl Page {
.iter() .iter()
.filter(|float| float.placement == FloatPlacement::Bottom) .filter(|float| float.placement == FloatPlacement::Bottom)
{ {
float_y += float.gap_before;
Self::paint_float(float, 0, float_y, &mut list); Self::paint_float(float, 0, float_y, &mut list);
float_y += float.height; float_y += float.height;
} }
+1 -1
View File
@@ -264,7 +264,7 @@ M5 (partial): display-list text carries `FontFace::{Roman,Bold,Italic,BoldItalic
M6 (partial): `DisplayItem::Image` becomes a PDF XObject; `DisplayItem::Link` becomes `/Annots /Subtype /Link /URI`; `DisplayItem::Special` injects raw content-stream operators. SVG previews show images as grey rectangles and specials as comments. M6 (partial): `DisplayItem::Image` becomes a PDF XObject; `DisplayItem::Link` becomes `/Annots /Subtype /Link /URI`; `DisplayItem::Special` injects raw content-stream operators. SVG previews show images as grey rectangles and specials as comments.
M2 (partial): `Page::display_list_with_goal(page_goal)` places insertion material at `page_goal - insertion_height`, adds a 0.4pt separator above footnotes, splits oversized insertions at item boundaries, and paints top/bottom floats from their vertical `content` at the page edges; empty floats keep a dimension rectangle. Floats also size themselves from content, respect `text_fraction_percent`, respect `top_fraction_percent` (TeX `\topfraction`, default 70%) and `bottom_fraction_percent` (TeX `\bottomfraction`, default 30%), and can take a dedicated float page. The incremental edit path applies the same float fractions, float number limits, insertion-class limits and per-class `skip` rules as full layout. M2 (partial): `Page::display_list_with_goal(page_goal)` places insertion material at `page_goal - insertion_height`, adds a 0.4pt separator above footnotes, splits oversized insertions at item boundaries, and paints top/bottom floats from their vertical `content` at the page edges; empty floats keep a dimension rectangle. Floats also size themselves from content, respect `text_fraction_percent`, respect `top_fraction_percent` (TeX `\topfraction`, default 70%) and `bottom_fraction_percent` (TeX `\bottomfraction`, default 30%), and can take a dedicated float page. The incremental edit path applies the same float fractions, float number limits, insertion-class limits and per-class `skip` rules as full layout. `Document::float_sep` (TeX `\floatsep`, default 12pt) reserves a gap between stacked floats of the same placement in both paths.
M3 (partial): `Unit::halign` normalizes column widths, intercolumn gaps, row heights and cell text widths to scaled points; the painter uses 10pt glyphs in 12pt rows. Rules and spans are painted; tall tables split at row boundaries (`Halign::split_for_height`) with column widths preserved. M3 (partial): `Unit::halign` normalizes column widths, intercolumn gaps, row heights and cell text widths to scaled points; the painter uses 10pt glyphs in 12pt rows. Rules and spans are painted; tall tables split at row boundaries (`Halign::split_for_height`) with column widths preserved.