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

+1
View File
@@ -475,6 +475,7 @@ Route-D float placement mirrors TeX fractions:
- `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::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`.
- `Document::text_float_sep` defaults to 20pt (`\textfloatsep`); a page that carries both floats and body text reserves one such gap between them. `Page::display_list_with_layout(page_goal, text_float_sep)` mirrors that in rendered output, while `display_list_with_goal` keeps the historic no-gap behaviour for callers without document settings.
- Floats that start a page sit at the top margin: float material does not consume `\topskip`; only body text does.
- `FloatPlacement::Here` keeps an `[h]` float in the body flow: `Document::in_text_sep` (TeX `\intextsep`, default 12pt) separates it from the surrounding text, the float moves to the next page when it does not fit, and it counts against `\totalnumber`. `Page::display_list_with_layout` paints the float at the same flow position the page builder reserved.
- `Engine::edit` applies the float fractions, number limits and separations, so incremental layout matches full layout.
+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");
}
}
+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.
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. `Document::text_float_sep` (TeX `\textfloatsep`, default 20pt) reserves exactly one gap between the float block and the body text on a page that has both, again in both paths; `Page::display_list_with_layout(page_goal, text_float_sep)` applies the same gap when rendering, while `display_list_with_goal` stays gap-free for callers that do not know the document settings. `FloatPlacement::Here` models `[h]` floats: they keep their place in the body flow with `Document::in_text_sep` (TeX `\intextsep`, default 12pt) above and below, move to the next page when they do not fit, and count against `\totalnumber`. The page stores the float's `gap_after` so the display list advances the flow by the same amount the builder reserved.
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. `Document::text_float_sep` (TeX `\textfloatsep`, default 20pt) reserves exactly one gap between the float block and the body text on a page that has both, again in both paths; `Page::display_list_with_layout(page_goal, text_float_sep)` applies the same gap when rendering, while `display_list_with_goal` stays gap-free for callers that do not know the document settings. Float material at the top of a page starts at the top margin: `\topskip` is consumed only by body text, matching TeX float pages. `FloatPlacement::Here` models `[h]` floats: they keep their place in the body flow with `Document::in_text_sep` (TeX `\intextsep`, default 12pt) above and below, move to the next page when they do not fit, and count against `\totalnumber`. The page stores the float's `gap_after` so the display list advances the flow by the same amount the builder reserved.
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.