Defer top floats to the next page

This commit is contained in:
gloamxun committed 2026-10-20 12:41:39 +08:00
1 parent 17bd2d6237
commit d973b2d1a0
2 files changed
+83 -7

No files matched your search

+79 -4
View File
@@ -25,6 +25,7 @@ pub enum VerticalItem {
Penalty(i64),
Insertion(Insertion),
Mark(Mark),
Float(Float),
}
/// Vertical material reserved at the bottom of a page (footnotes etc.).
@@ -42,6 +43,20 @@ pub struct Mark {
pub text: String,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub enum FloatPlacement {
Top,
Bottom,
}
/// Deferred float material. Only top placement is implemented so far.
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct Float {
pub placement: FloatPlacement,
pub width: i64,
pub height: i64,
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Unit {
pub id: UnitId,
@@ -105,6 +120,8 @@ pub struct Page {
pub insertions: Vec<Insertion>,
#[serde(default)]
pub marks: Vec<Mark>,
#[serde(default)]
pub floats: Vec<Float>,
pub sources: Vec<(UnitId, u64)>,
pub hash: u64,
}
@@ -115,6 +132,7 @@ impl Page {
items: Vec::new(),
insertions: Vec::new(),
marks: Vec::new(),
floats: Vec::new(),
sources: Vec::new(),
hash: 0,
}
@@ -125,6 +143,7 @@ impl Page {
self.items.hash(&mut hasher);
self.insertions.hash(&mut hasher);
self.marks.hash(&mut hasher);
self.floats.hash(&mut hasher);
self.sources.hash(&mut hasher);
self.hash = hasher.finish();
}
@@ -155,6 +174,9 @@ pub struct Checkpoint {
/// Running marks already collected on the current page.
#[serde(default)]
pub page_marks: Vec<Mark>,
/// Floats already attached to the current page.
#[serde(default)]
pub page_floats: Vec<Float>,
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
@@ -211,11 +233,12 @@ impl Engine {
page_insertions: current.insertions.clone(),
insertion_height,
page_marks: current.marks.clone(),
page_floats: current.floats.clone(),
});
for item in &unit.items {
match item {
VerticalItem::Box(vertical_box) => {
if current.items.is_empty() {
if current.items.is_empty() && current.floats.is_empty() {
used = document.top_skip.max(vertical_box.height);
} else if used + vertical_box.height > document.page_goal - insertion_height
{
@@ -253,10 +276,20 @@ impl Engine {
VerticalItem::Mark(mark) => {
current.marks.push(mark.clone());
}
VerticalItem::Float(float) => {
if !current.items.is_empty() || !current.insertions.is_empty() {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = 0;
insertion_height = 0;
}
used += float.height;
current.floats.push(float.clone());
}
}
}
}
if !current.items.is_empty() {
if !current.items.is_empty() || !current.insertions.is_empty() || !current.floats.is_empty()
{
pages.push(current);
}
for (index, unit) in document.units.iter().enumerate() {
@@ -314,6 +347,7 @@ impl Engine {
page_insertions: Vec::new(),
insertion_height: 0,
page_marks: Vec::new(),
page_floats: Vec::new(),
});
let edited_height: i64 = next.units[unit_index]
.items
@@ -324,6 +358,7 @@ impl Engine {
VerticalItem::Penalty(_) => 0,
VerticalItem::Insertion(insertion) => insertion.height,
VerticalItem::Mark(_) => 0,
VerticalItem::Float(float) => float.height,
})
.sum();
if checkpoint.used + edited_height > next.page_goal && !checkpoint.page_items.is_empty() {
@@ -337,6 +372,7 @@ impl Engine {
items: checkpoint.page_items,
insertions: checkpoint.page_insertions,
marks: checkpoint.page_marks,
floats: checkpoint.page_floats,
sources: Vec::new(),
hash: 0,
};
@@ -353,11 +389,12 @@ impl Engine {
page_insertions: current.insertions.clone(),
insertion_height,
page_marks: current.marks.clone(),
page_floats: current.floats.clone(),
});
for item in &unit.items {
match item {
VerticalItem::Box(vertical_box) => {
if current.items.is_empty() {
if current.items.is_empty() && current.floats.is_empty() {
used = next.top_skip.max(vertical_box.height);
} else if used + vertical_box.height > next.page_goal - insertion_height {
pages.push(std::mem::replace(&mut current, Page::empty()));
@@ -394,10 +431,20 @@ impl Engine {
VerticalItem::Mark(mark) => {
current.marks.push(mark.clone());
}
VerticalItem::Float(float) => {
if !current.items.is_empty() || !current.insertions.is_empty() {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = 0;
insertion_height = 0;
}
used += float.height;
current.floats.push(float.clone());
}
}
}
}
if !current.items.is_empty() {
if !current.items.is_empty() || !current.insertions.is_empty() || !current.floats.is_empty()
{
pages.push(current);
}
for (index, unit) in next.units.iter().enumerate() {
@@ -612,6 +659,34 @@ mod tests {
assert_eq!(engine.layout.pages[0].marks[0].text, "page 1");
}
#[test]
fn top_float_starts_a_new_page_when_it_arrives_late() {
let unit = Unit {
id: 0,
source: "float".to_owned(),
items: vec![
VerticalItem::Box(VerticalBox {
width: 10,
height: 10,
}),
VerticalItem::Float(Float {
placement: FloatPlacement::Top,
width: 40,
height: 20,
}),
VerticalItem::Box(VerticalBox {
width: 10,
height: 10,
}),
],
};
let engine = Engine::layout(&Document::new(100, vec![unit]));
assert_eq!(engine.layout.pages.len(), 2);
assert_eq!(engine.layout.pages[0].items.len(), 1);
assert_eq!(engine.layout.pages[1].floats.len(), 1);
assert_eq!(engine.layout.pages[1].items.len(), 1);
}
#[test]
fn checkpoint_round_trips_through_serde() {
let engine = Engine::layout(&document());
+4 -3
View File
@@ -71,7 +71,7 @@ next paragraph milestones.
| --- | --- | --- |
| M0 | vertical list, page builder, checkpoint, page hash, edit demo | edit on page 2 rebuilds only page 2 |
| M1 | TeX box/glue/penalty + paragraph builder (`\par`, `\hsize`, `\parindent`) | **partial**: item stream, CMR10 metrics, pdfTeX fixture and minimum line fill; hyphenation next |
| M2 | page builder + `\output`, `\vsize`, `\topskip`, footnotes, floats | **partial**: `\vsize` page goal, `\topskip`, forced penalties, `Insertion` reserve, running `Mark`s, Rust `OutputRoutine` shipout hook; interpreted `\output`, float placements next |
| M2 | page builder + `\output`, `\vsize`, `\topskip`, footnotes, floats | **partial**: `\vsize` page goal, `\topskip`, forced penalties, `Insertion` reserve, running `Mark`s, `OutputRoutine` hook, top `Float` queue; interpreted `\output`, bottom floats next |
| M3 | `\halign`, `\omit`, `\span`, `\multispan`, booktabs/longtable | table width/rules match reference |
| M4 | math lists, scripts, fractions, radicals, accents, delimiters | arXiv formulas render without overlap |
| M5 | NFSS/TFM metrics, CM/Latin Modern | same line/page breaks as reference |
@@ -108,8 +108,9 @@ a single cost function.
`VerticalItem::Mark` material is collected into `Page::marks` and included
in the page hash. `OutputRoutine::shipout` runs before sealing and may
mutate each page; this models the `\output` stage in Rust rather than as
an interpreted token list, and cannot yet change page goals or place
floats.
an interpreted token list, and cannot yet change page goals. `Float`
material is deferred to the top of the next page when it arrives late;
bottom placement and float-page policies are not implemented yet.
- No macro state, output routine, math, fonts or PDF output in the new core yet.
- The v1 server and project API still use `src/rust_typeset` and are unchanged.