Defer bottom floats inside the page builder
This commit is contained in:
1 parent
fd9790cf28
commit
17638996a1
3 files changed
+145
-7
No files matched your search
@@ -204,7 +204,9 @@ impl PageBuilder {
|
|||||||
let mut held = BTreeMap::new();
|
let mut held = BTreeMap::new();
|
||||||
let mut start = self.skip_leading(nodes, 0);
|
let mut start = self.skip_leading(nodes, 0);
|
||||||
while start < nodes.len() {
|
while start < nodes.len() {
|
||||||
let Some((fragment, next, next_held)) = self.best_fragment(nodes, start, &held) else {
|
let Some((fragment, next, next_held, _, _)) =
|
||||||
|
self.best_fragment(nodes, start, &held, &[], false)
|
||||||
|
else {
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
let next = next.max(start + 1);
|
let next = next.max(start + 1);
|
||||||
@@ -224,7 +226,8 @@ impl PageBuilder {
|
|||||||
let mut pages = Vec::new();
|
let mut pages = Vec::new();
|
||||||
let mut next = self.skip_leading(nodes, start);
|
let mut next = self.skip_leading(nodes, start);
|
||||||
while next < nodes.len() {
|
while next < nodes.len() {
|
||||||
let Some((fragment, after, _)) = self.best_fragment(nodes, next, &BTreeMap::new())
|
let Some((fragment, after, _, _, _)) =
|
||||||
|
self.best_fragment(nodes, next, &BTreeMap::new(), &[], false)
|
||||||
else {
|
else {
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
@@ -235,6 +238,58 @@ impl PageBuilder {
|
|||||||
pages
|
pages
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Split `nodes` into pages while deferring bottom floats that do not
|
||||||
|
/// fit: the page keeps filling with body material and the float lands at
|
||||||
|
/// the bottom of the following page (TeX's deferred float queue).
|
||||||
|
pub fn build_with_deferred_bottom_floats(&self, nodes: &[VerticalNode]) -> Vec<PageFragment> {
|
||||||
|
let mut pages = Vec::new();
|
||||||
|
let mut carried: Vec<i64> = Vec::new();
|
||||||
|
let mut start = self.skip_leading(nodes, 0);
|
||||||
|
while start < nodes.len() {
|
||||||
|
let Some((fragment, next, _, _, deferred)) =
|
||||||
|
self.best_fragment(nodes, start, &BTreeMap::new(), &carried, true)
|
||||||
|
else {
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
let next = next.max(start + 1);
|
||||||
|
carried = deferred;
|
||||||
|
pages.push(fragment);
|
||||||
|
start = self.skip_leading(nodes, next);
|
||||||
|
}
|
||||||
|
if !carried.is_empty() {
|
||||||
|
pages.push(self.float_page_fragment(nodes.len(), &carried));
|
||||||
|
}
|
||||||
|
pages
|
||||||
|
}
|
||||||
|
|
||||||
|
fn float_page_fragment(&self, at: usize, floats: &[i64]) -> PageFragment {
|
||||||
|
let mut bottom = 0_i64;
|
||||||
|
for (index, height) in floats.iter().enumerate() {
|
||||||
|
let gap = if index == 0 {
|
||||||
|
self.text_float_sep.max(0)
|
||||||
|
} else {
|
||||||
|
self.float_sep.max(0)
|
||||||
|
};
|
||||||
|
bottom += (*height).max(0) + gap;
|
||||||
|
}
|
||||||
|
PageFragment {
|
||||||
|
start: at,
|
||||||
|
end: at,
|
||||||
|
cost: 0,
|
||||||
|
penalty: 10_000,
|
||||||
|
badness: 0,
|
||||||
|
height: 0,
|
||||||
|
bottom,
|
||||||
|
depth: 0,
|
||||||
|
stretch: 0,
|
||||||
|
stretch_order: 0,
|
||||||
|
shrink: 0,
|
||||||
|
stretches: [0; 4],
|
||||||
|
set: 0,
|
||||||
|
insertions: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn skip_leading(&self, nodes: &[VerticalNode], mut index: usize) -> usize {
|
fn skip_leading(&self, nodes: &[VerticalNode], mut index: usize) -> usize {
|
||||||
while index < nodes.len() {
|
while index < nodes.len() {
|
||||||
match nodes[index] {
|
match nodes[index] {
|
||||||
@@ -250,7 +305,9 @@ impl PageBuilder {
|
|||||||
nodes: &[VerticalNode],
|
nodes: &[VerticalNode],
|
||||||
start: usize,
|
start: usize,
|
||||||
held: &BTreeMap<u8, i64>,
|
held: &BTreeMap<u8, i64>,
|
||||||
) -> Option<(PageFragment, usize, BTreeMap<u8, i64>)> {
|
carried_floats: &[i64],
|
||||||
|
defer_floats: bool,
|
||||||
|
) -> Option<(PageFragment, usize, BTreeMap<u8, i64>, Vec<i64>, Vec<i64>)> {
|
||||||
let mut height = 0_i64;
|
let mut height = 0_i64;
|
||||||
let mut stretch_orders = [0_i64; 4];
|
let mut stretch_orders = [0_i64; 4];
|
||||||
let mut shrink = 0_i64;
|
let mut shrink = 0_i64;
|
||||||
@@ -266,6 +323,20 @@ impl PageBuilder {
|
|||||||
let mut reserved: i64 = usage.values().sum();
|
let mut reserved: i64 = usage.values().sum();
|
||||||
let mut candidates: Vec<Candidate> = Vec::new();
|
let mut candidates: Vec<Candidate> = Vec::new();
|
||||||
|
|
||||||
|
let mut bottom_floats: Vec<i64> = Vec::new();
|
||||||
|
let mut deferred_floats: Vec<i64> = Vec::new();
|
||||||
|
for height in carried_floats {
|
||||||
|
let gap = if bottom_count == 0 {
|
||||||
|
self.text_float_sep.max(0)
|
||||||
|
} else {
|
||||||
|
self.float_sep.max(0)
|
||||||
|
};
|
||||||
|
bottom += (*height).max(0) + gap;
|
||||||
|
bottom_count += 1;
|
||||||
|
bottom_floats.push((*height).max(0));
|
||||||
|
float_total += 1;
|
||||||
|
}
|
||||||
|
|
||||||
for index in start..nodes.len() {
|
for index in start..nodes.len() {
|
||||||
let (stretch, stretch_order) = effective_stretch(&stretch_orders);
|
let (stretch, stretch_order) = effective_stretch(&stretch_orders);
|
||||||
match &nodes[index] {
|
match &nodes[index] {
|
||||||
@@ -302,6 +373,8 @@ impl PageBuilder {
|
|||||||
fragment,
|
fragment,
|
||||||
index,
|
index,
|
||||||
self.insertion_overflow(nodes, start, end, held),
|
self.insertion_overflow(nodes, start, end, held),
|
||||||
|
bottom_floats.clone(),
|
||||||
|
deferred_floats.clone(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
let contribution = if first_box {
|
let contribution = if first_box {
|
||||||
@@ -354,6 +427,8 @@ impl PageBuilder {
|
|||||||
fragment,
|
fragment,
|
||||||
index,
|
index,
|
||||||
self.insertion_overflow(nodes, start, end, held),
|
self.insertion_overflow(nodes, start, end, held),
|
||||||
|
bottom_floats.clone(),
|
||||||
|
deferred_floats.clone(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
// TeX installs a `[t]` float at the top of the page it is
|
// TeX installs a `[t]` float at the top of the page it is
|
||||||
@@ -385,6 +460,21 @@ impl PageBuilder {
|
|||||||
};
|
};
|
||||||
let needed = (*float_height).max(0) + gap;
|
let needed = (*float_height).max(0) + gap;
|
||||||
let allowed = self.page_goal * i64::from(self.bottom_fraction_percent) / 100;
|
let allowed = self.page_goal * i64::from(self.bottom_fraction_percent) / 100;
|
||||||
|
// TeX defers a bottom float that does not fit and keeps
|
||||||
|
// filling the page with body material; the float moves to
|
||||||
|
// the next page's bottom area.
|
||||||
|
if defer_floats
|
||||||
|
&& height > 0
|
||||||
|
&& needed <= self.page_goal
|
||||||
|
&& needed <= allowed
|
||||||
|
&& (height + reserved + bottom + needed > self.page_goal
|
||||||
|
|| bottom + needed > allowed
|
||||||
|
|| bottom_count >= self.bottom_number
|
||||||
|
|| float_total >= self.total_number)
|
||||||
|
{
|
||||||
|
deferred_floats.push((*float_height).max(0));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if height > 0
|
if height > 0
|
||||||
&& (height + reserved + bottom + needed > self.page_goal
|
&& (height + reserved + bottom + needed > self.page_goal
|
||||||
|| bottom + needed > allowed
|
|| bottom + needed > allowed
|
||||||
@@ -413,11 +503,14 @@ impl PageBuilder {
|
|||||||
fragment,
|
fragment,
|
||||||
index,
|
index,
|
||||||
self.insertion_overflow(nodes, start, end, held),
|
self.insertion_overflow(nodes, start, end, held),
|
||||||
|
bottom_floats.clone(),
|
||||||
|
deferred_floats.clone(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
bottom += needed;
|
bottom += needed;
|
||||||
bottom_count += 1;
|
bottom_count += 1;
|
||||||
float_total += 1;
|
float_total += 1;
|
||||||
|
bottom_floats.push((*float_height).max(0));
|
||||||
}
|
}
|
||||||
VerticalNode::Glue(glue) => {
|
VerticalNode::Glue(glue) => {
|
||||||
// TeX only allows a break at glue that follows a
|
// TeX only allows a break at glue that follows a
|
||||||
@@ -470,6 +563,8 @@ impl PageBuilder {
|
|||||||
fragment,
|
fragment,
|
||||||
index + 1,
|
index + 1,
|
||||||
self.insertion_overflow(nodes, start, end, held),
|
self.insertion_overflow(nodes, start, end, held),
|
||||||
|
bottom_floats.clone(),
|
||||||
|
deferred_floats.clone(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
if index > start {
|
if index > start {
|
||||||
@@ -533,6 +628,8 @@ impl PageBuilder {
|
|||||||
fragment,
|
fragment,
|
||||||
best.next,
|
best.next,
|
||||||
self.insertion_overflow(nodes, start, best.end, held),
|
self.insertion_overflow(nodes, start, best.end, held),
|
||||||
|
bottom_floats.clone(),
|
||||||
|
deferred_floats.clone(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -753,6 +850,40 @@ mod tests {
|
|||||||
VerticalNode::Glue(VerticalGlue::new(width, stretch, shrink))
|
VerticalNode::Glue(VerticalGlue::new(width, stretch, shrink))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn deferred_bottom_float_continues_the_page() {
|
||||||
|
let mut builder = PageBuilder::new(100);
|
||||||
|
builder.text_float_sep = 0;
|
||||||
|
let nodes = vec![
|
||||||
|
box_(30, 0),
|
||||||
|
VerticalNode::BottomFloat { height: 80 },
|
||||||
|
glue(0, 0, 0),
|
||||||
|
box_(30, 0),
|
||||||
|
];
|
||||||
|
let pages = builder.build_with_deferred_bottom_floats(&nodes);
|
||||||
|
assert_eq!(pages.len(), 2);
|
||||||
|
assert_eq!(
|
||||||
|
pages[0].height, 60,
|
||||||
|
"body continues past the deferred float"
|
||||||
|
);
|
||||||
|
assert_eq!(pages[0].bottom, 0);
|
||||||
|
assert_eq!(pages[1].bottom, 80, "float lands on the next page");
|
||||||
|
assert_eq!((pages[1].start, pages[1].end), (4, 4));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn fitting_bottom_float_stays_on_its_page() {
|
||||||
|
let builder = PageBuilder::new(100);
|
||||||
|
let nodes = vec![
|
||||||
|
box_(30, 0),
|
||||||
|
VerticalNode::BottomFloat { height: 50 },
|
||||||
|
box_(10, 0),
|
||||||
|
];
|
||||||
|
let pages = builder.build_with_deferred_bottom_floats(&nodes);
|
||||||
|
assert_eq!(pages.len(), 1);
|
||||||
|
assert_eq!(pages[0].bottom, 50);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn forced_penalty_breaks_record_the_penalty() {
|
fn forced_penalty_breaks_record_the_penalty() {
|
||||||
let builder = PageBuilder::new(100);
|
let builder = PageBuilder::new(100);
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ floats (`\floatsep`, `\textfloatsep`, `\intextsep`, `\topfraction`,
|
|||||||
`\bottomnumber`, `\totalnumber`), insertion classes (`\skip`, fraction and
|
`\bottomnumber`, `\totalnumber`), insertion classes (`\skip`, fraction and
|
||||||
absolute limits, splitting), marks, Rust output hook, multi-page PDF, and the
|
absolute limits, splitting), marks, Rust output hook, multi-page PDF, and the
|
||||||
route-D service endpoints (`/api/route-d/preview|pdf|edit`, project preview and
|
route-D service endpoints (`/api/route-d/preview|pdf|edit`, project preview and
|
||||||
PDF opt-in with `?engine=route-d`). 459 workspace tests are green and no oracle
|
PDF opt-in with `?engine=route-d`). 461 workspace tests are green and no oracle
|
||||||
fixture is ignored.
|
fixture is ignored.
|
||||||
|
|
||||||
Known gaps, in dependency order:
|
Known gaps, in dependency order:
|
||||||
@@ -254,6 +254,11 @@ body break matches pdfLaTeX. When it does not fit the remaining body space,
|
|||||||
the paged builder ends the current page and defers the float, while TeX
|
the paged builder ends the current page and defers the float, while TeX
|
||||||
keeps filling the page before placing the float on the next one; that
|
keeps filling the page before placing the float on the next one; that
|
||||||
deferral-fill case stays documented as a known approximation.
|
deferral-fill case stays documented as a known approximation.
|
||||||
|
`PageBuilder::build_with_deferred_bottom_floats` now implements TeX's
|
||||||
|
behaviour for that case: the current page keeps filling, the float is
|
||||||
|
carried to the next page's bottom, and a float larger than the page still
|
||||||
|
ends the page rather than looping. The route-D service still calls `build`,
|
||||||
|
so endpoint behaviour is unchanged until that wiring lands.
|
||||||
|
|
||||||
### P2 — Vertical list, page builder, `\output` (L2)
|
### P2 — Vertical list, page builder, `\output` (L2)
|
||||||
|
|
||||||
|
|||||||
+5
-3
@@ -266,8 +266,10 @@ oracle fixture is ignored.
|
|||||||
offer the page builder a legal breakpoint. The routine cannot yet
|
offer the page builder a legal breakpoint. The routine cannot yet
|
||||||
change page goals. The paged builder places top `Float` material at the top
|
change page goals. The paged builder places top `Float` material at the top
|
||||||
of a page and bottom `Float` material at the page bottom: a late top float
|
of a page and bottom `Float` material at the page bottom: a late top float
|
||||||
starts the next page, a bottom float that cannot fit the remaining body
|
starts the next page, a bottom float that cannot fit is deferred to the next page's
|
||||||
space ends the current page and is deferred to the next page's bottom
|
bottom by `PageBuilder::build_with_deferred_bottom_floats`, which keeps
|
||||||
|
filling the current page (the default `build` path still ends the page at
|
||||||
|
the float until the service adopts the new entry point)
|
||||||
(TeX instead keeps filling the current page before deferring the float,
|
(TeX instead keeps filling the current page before deferring the float,
|
||||||
which remains a documented approximation), stacked bottom floats reserve `\floatsep`, and
|
which remains a documented approximation), stacked bottom floats reserve `\floatsep`, and
|
||||||
`\bottomfraction` caps the bottom float share, and a page whose top floats reach `\floatpagefraction` ships without body material. Route-D preview/PDF draw
|
`\bottomfraction` caps the bottom float share, and a page whose top floats reach `\floatpagefraction` ships without body material. Route-D preview/PDF draw
|
||||||
@@ -363,7 +365,7 @@ Math coverage includes fractions/roots, scripts, `\left...\right` with body-scal
|
|||||||
|
|
||||||
Paragraph units carry a `ParagraphFont`; `Unit::paragraph_with_face` and `Unit::paragraph_with_style_and_face` select Roman, bold, italic, mono, or sans. The corresponding Computer Modern TFM (`cmr10`, `cmbx10`, `cmti10`, `cmbxti10`, `cmtt10`, `cmss10`) supplies advances, kerns, ligatures, and italic correction for layout, while the display list and PDF select the matching Liberation face. `Engine::edit` preserves the face when reflowing an edited paragraph.
|
Paragraph units carry a `ParagraphFont`; `Unit::paragraph_with_face` and `Unit::paragraph_with_style_and_face` select Roman, bold, italic, mono, or sans. The corresponding Computer Modern TFM (`cmr10`, `cmbx10`, `cmti10`, `cmbxti10`, `cmtt10`, `cmss10`) supplies advances, kerns, ligatures, and italic correction for layout, while the display list and PDF select the matching Liberation face. `Engine::edit` preserves the face when reflowing an edited paragraph.
|
||||||
|
|
||||||
The crate currently has 304 unit tests plus seven route-D integration tests and thirty-three oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 459 tests (304 route-D unit, 7 route-D integration, 33 oracle, 71 v1 integration, 26 server, 4 v1 unit, 1 core unit, 13 binary units), with none ignored. v1 remains the live server path.
|
The crate currently has 306 unit tests plus seven route-D integration tests and thirty-three oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 461 tests (306 route-D unit, 7 route-D integration, 33 oracle, 71 v1 integration, 26 server, 4 v1 unit, 1 core unit, 13 binary units), with none ignored. v1 remains the live server path.
|
||||||
|
|
||||||
### M0 incremental demo
|
### M0 incremental demo
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user