Split insertions at the page break and hold over the tail

This commit is contained in:
gloamxun committed 2026-10-23 02:26:56 +08:00
1 parent b7b75fbff1
commit b5ec36ae2a
4 files changed
+202 -144

No files matched your search

+3 -1
View File
@@ -577,7 +577,9 @@ edit demo. M1 adds the horizontal box/glue/penalty model, an item-stream Knuth-P
paragraph builder, CMR10 metrics extracted from `cmr10.tfm`, and a matching pdfTeX line-break fixture. Paragraphs are split at line
boundaries when they cross a page, and the pdfLaTeX oracle covers the
100-240pt measure matrix plus register, `\hangindent`/`\parshape`,
`\parfillskip`, sentence-spacing, `\looseness` and vertical-glue fixtures. The production v1 engine remains the
`\parfillskip`, sentence-spacing, `\looseness` and vertical-glue fixtures. Overflowing
insertion material is split at item boundaries, with the tail held over to the
next page. The production v1 engine remains the
default until the new core reaches layout parity. See `docs/TEX_CORE.md`. The complete phased plan (P0 oracle harness, TeX-exact paragraphs,
page builder and `\\output`, `\\halign`, math, Computer Modern fonts, class
execution, service and incrementality targets) is in
+189 -138
View File
@@ -927,6 +927,122 @@ fn place_paragraph_flow(
}
}
/// TeX insertion placement shared by the initial and incremental builders.
///
/// Insertion material stays on the page where its anchor text ends. When the
/// page cannot hold all of it, the part that fits at an item boundary stays
/// and the remainder is held over to the next page (TeX's split insertions).
fn place_insertion(
document: &Document,
pages: &mut Vec<Page>,
current: &mut Page,
used: &mut i64,
insertion_height: &mut i64,
insertion: &Insertion,
) {
let mut pending = insertion.clone();
loop {
let class = pending.class;
let gap = if current.insertions.is_empty() {
document.insertion_skip_for(class).max(0)
} else {
0
};
let capacity = document
.page_goal
.saturating_sub(document.top_skip)
.saturating_sub(gap)
.max(1);
let class_limit = document.insertion_class_limit(class).min(capacity).max(1);
let class_height: i64 = current
.insertions
.iter()
.filter(|entry| entry.class == class)
.map(|entry| document.insertion_reserve(entry.class, entry.height))
.sum();
let reserved = document.insertion_reserve(class, pending.height);
let room = document
.page_goal
.saturating_sub(*used)
.saturating_sub(gap)
.saturating_sub(*insertion_height)
.max(0);
if class_height + reserved <= class_limit && reserved <= room {
*insertion_height += gap + reserved;
current.insertions.push(pending);
return;
}
let page_has_content = !current.items.is_empty() || !current.insertions.is_empty();
if page_has_content {
let available = room.min(class_limit.saturating_sub(class_height));
if available > 0
&& let Some((head, tail)) = split_insertion(document, &pending, available)
{
*insertion_height += gap + document.insertion_reserve(class, head.height);
current.insertions.push(head);
pages.push(std::mem::replace(current, Page::empty()));
*used = document.top_skip;
*insertion_height = 0;
pending = tail;
continue;
}
pages.push(std::mem::replace(current, Page::empty()));
*used = document.top_skip;
*insertion_height = 0;
continue;
}
if let Some((head, tail)) = split_insertion(document, &pending, class_limit) {
*insertion_height = gap + document.insertion_reserve(class, head.height);
current.insertions.push(head);
pages.push(std::mem::replace(current, Page::empty()));
*used = document.top_skip;
*insertion_height = 0;
pending = tail;
continue;
}
*insertion_height = gap + reserved;
current.insertions.push(pending);
return;
}
}
/// Split an insertion at an item boundary, returning the part that fits in
/// `available` reserved space and the held-over remainder.
fn split_insertion(
document: &Document,
insertion: &Insertion,
available: i64,
) -> Option<(Insertion, Insertion)> {
let class = insertion.class;
let mut head = Vec::new();
let mut head_height = 0i64;
for (index, item) in insertion.items.iter().enumerate() {
let height = item_height(item);
if head.is_empty() && document.insertion_reserve(class, height) > available {
return None;
}
if !head.is_empty() && document.insertion_reserve(class, head_height + height) > available {
let tail_items = insertion.items[index..].to_vec();
let tail_height: i64 = tail_items.iter().map(item_height).sum();
return Some((
Insertion {
class,
height: head_height,
items: head,
},
Insertion {
class,
height: tail_height,
items: tail_items,
},
));
}
head.push(item.clone());
head_height += height;
}
None
}
fn top_float_exceeds_page_fraction(page: &Page, page_goal: i64, percent: u8) -> bool {
let height = top_float_height(page);
height > 0 && page.items.is_empty() && height * 100 > i64::from(percent) * page_goal
@@ -1203,106 +1319,14 @@ impl Engine {
}
}
VerticalItem::Insertion(insertion) => {
let gap = if current.insertions.is_empty() {
document.insertion_skip_for(insertion.class).max(0)
} else {
0
};
let capacity = document
.page_goal
.saturating_sub(document.top_skip)
.saturating_sub(gap)
.max(1);
if insertion_height
+ document.insertion_reserve(insertion.class, insertion.height)
> capacity
&& (!current.items.is_empty() || !current.insertions.is_empty())
{
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
insertion_height = 0;
}
let class_limit = document
.insertion_class_limit(insertion.class)
.min(
document
.page_goal
.saturating_sub(document.top_skip)
.saturating_sub(gap),
)
.max(1);
let class_height: i64 = current
.insertions
.iter()
.filter(|entry| entry.class == insertion.class)
.map(|entry| document.insertion_reserve(entry.class, entry.height))
.sum();
if (class_height
+ document.insertion_reserve(insertion.class, insertion.height)
> class_limit
|| document.insertion_reserve(insertion.class, insertion.height)
> class_limit)
&& !insertion.items.is_empty()
{
if !current.items.is_empty() || !current.insertions.is_empty() {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
insertion_height = 0;
}
let mut chunk = Vec::new();
let mut chunk_height = 0;
let mut chunk_gap = gap;
for sub_item in &insertion.items {
let sub_height = item_height(sub_item);
if document
.insertion_reserve(insertion.class, chunk_height + sub_height)
> class_limit
&& !chunk.is_empty()
{
current.insertions.push(Insertion {
class: insertion.class,
height: chunk_height,
items: std::mem::take(&mut chunk),
});
insertion_height = chunk_gap
+ document.insertion_reserve(insertion.class, chunk_height);
chunk_height = 0;
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
chunk_gap = gap;
}
chunk.push(sub_item.clone());
chunk_height += sub_height;
}
if !chunk.is_empty() {
current.insertions.push(Insertion {
class: insertion.class,
height: chunk_height,
items: chunk,
});
insertion_height = chunk_gap
+ document.insertion_reserve(insertion.class, chunk_height);
}
} else {
let goal = document.page_goal - insertion_height;
if (!current.items.is_empty() || !current.insertions.is_empty())
&& (used
+ gap
+ document.insertion_reserve(insertion.class, insertion.height)
> goal
|| class_height
+ document
.insertion_reserve(insertion.class, insertion.height)
> class_limit)
{
pages.push(std::mem::replace(&mut current, Page::empty()));
used = document.top_skip;
insertion_height = 0;
}
insertion_height +=
gap + document.insertion_reserve(insertion.class, insertion.height);
current.insertions.push(insertion.clone());
}
place_insertion(
document,
&mut pages,
&mut current,
&mut used,
&mut insertion_height,
insertion,
);
}
VerticalItem::Mark(mark) => {
current.marks.push(mark.clone());
@@ -1863,44 +1887,14 @@ impl Engine {
}
}
VerticalItem::Insertion(insertion) => {
let gap = if current.insertions.is_empty() {
next.insertion_skip_for(insertion.class).max(0)
} else {
0
};
let capacity = next
.page_goal
.saturating_sub(next.top_skip)
.saturating_sub(gap)
.max(1);
let class_limit = next
.insertion_class_limit(insertion.class)
.min(capacity)
.max(1);
let class_height: i64 = current
.insertions
.iter()
.filter(|entry| entry.class == insertion.class)
.map(|entry| next.insertion_reserve(entry.class, entry.height))
.sum();
let exceeds = class_height
+ next.insertion_reserve(insertion.class, insertion.height)
> class_limit
|| insertion_height
+ next.insertion_reserve(insertion.class, insertion.height)
> capacity
|| used
+ gap
+ next.insertion_reserve(insertion.class, insertion.height)
> next.page_goal;
if exceeds && !current.items.is_empty() {
pages.push(std::mem::replace(&mut current, Page::empty()));
used = next.top_skip;
insertion_height = 0;
}
insertion_height +=
gap + next.insertion_reserve(insertion.class, insertion.height);
current.insertions.push(insertion.clone());
place_insertion(
next,
&mut pages,
&mut current,
&mut used,
&mut insertion_height,
insertion,
);
}
VerticalItem::Mark(mark) => {
current.marks.push(mark.clone());
@@ -2699,6 +2693,63 @@ mod tests {
assert!(engine.changed_pages().iter().all(|page| *page >= 1));
}
#[test]
fn insertion_splits_at_the_page_break_and_holds_over_the_tail() {
let rule = |height| VerticalItem::Rule {
width: 65_536,
height,
};
let mut document = Document::new(
100,
vec![
Unit::vertical(1, vec![rule(40)]),
Unit::insertion(2, 1, 80, vec![rule(40), rule(40)]),
Unit::vertical(3, vec![rule(20)]),
],
);
document.insertion_skip = 0;
let layout = Engine::layout(&document);
assert_eq!(layout.pages().len(), 2);
assert_eq!(layout.pages()[0].items.len(), 1);
assert_eq!(layout.pages()[0].insertions.len(), 1);
assert_eq!(layout.pages()[0].insertions[0].height, 40);
assert_eq!(layout.pages()[1].insertions.len(), 1);
assert_eq!(layout.pages()[1].insertions[0].height, 40);
// The held-over tail sits above the text that follows the insertion.
assert_eq!(layout.pages()[1].items.len(), 1);
}
#[test]
fn incremental_layout_holds_over_the_insertion_tail() {
let rule = |height| VerticalItem::Rule {
width: 65_536,
height,
};
let mut document = Document::new(
100,
vec![
Unit::paragraph(1, "body"),
Unit::insertion(2, 1, 80, vec![rule(40), rule(40)]),
],
);
document.insertion_skip = 0;
document.insertion_classes = vec![InsertionClass {
number: 1,
fraction_percent: 60,
max_height: None,
skip: None,
multiplier: 1000,
}];
let mut engine = Engine::layout(&document);
assert_eq!(engine.pages().len(), 2);
assert_eq!(engine.pages()[0].insertions[0].height, 40);
assert_eq!(engine.pages()[1].insertions[0].height, 40);
engine.edit(&document, 1, "changed body");
assert_eq!(engine.pages().len(), 2);
assert_eq!(engine.pages()[0].insertions[0].height, 40);
assert_eq!(engine.pages()[1].insertions[0].height, 40);
}
#[test]
fn incremental_layout_respects_insertion_count() {
let rule = VerticalItem::Rule {
+5 -4
View File
@@ -234,8 +234,9 @@ Scope:
TeX register semantics, `\marks` remains open);
- insertions: `\insert` with `\count`, `\dimen`, `\skip` per class, split
insertions, held-over material, `\footins` defaults (the three registers are
now modelled by `InsertionClass::multiplier`, `max_height` and `skip`; TeX's
register arithmetic and held-over bookkeeping remain);
now modelled by `InsertionClass::multiplier`, `max_height` and `skip`, and
the fitting head stays on the anchor page with the tail held over; TeX's
register arithmetic remains);
- output: `\output` as an interpreted token list, `\box255`, `\shipout`,
`\outputpenalty`, `\deadcycles`, page display list assembly;
- floats: TeX-compatible policy module used by the default output routine
@@ -257,8 +258,8 @@ body without moving the sheet; a route-D integration fixture checks the shift
with `pdftotext -bbox`, and the PDF endpoint reads leading `\hoffset`/`\voffset`
dimension assignments from the source. Floats, insertions and checkpoint replay still fall back
to the scrolling builder, which stays the default until it has a paged
equivalent. Next: port those paths, then paper margins, held-over insertion
material and the interpreted `\output`.
equivalent. Next: port those paths, then paper margins and the interpreted
`\output`.
Acceptance: 10 page fixtures (article text, footnotes, floats, forced breaks);
page counts identical and first/last baselines within 1pt of pdfLaTeX.
+5 -1
View File
@@ -207,6 +207,9 @@ oracle fixture is ignored.
`Document::insertion_classes` sets a per-class page fraction; an insertion
that would exceed the class limit ships the page and continues on the next
one, splitting at item boundaries when the insertion itself is too tall.
When only part of an insertion fits on the page where its anchor text ends,
that part stays there and the remainder is held over to the next page, so
later body text follows the held-over tail.
`Document::insertion_skip` models TeX's `\skip\footins` and is reserved once
before the first insertion class on a page; it defaults to zero, and
`InsertionClass::skip` overrides it for a single insertion class.
@@ -215,7 +218,8 @@ oracle fixture is ignored.
insertion cost 60pt of page height. `max_height` is the `\dimen<number>`
absolute cap. Full layout, incremental replay and the item-boundary splitter
all use the same reserved height.
Insertions taller than a page are split at item boundaries across pages.
Overflowing insertions are split at item boundaries: the fitting head stays
on the anchor page and the tail is held over to the following page.
- Page breaking handles vertical boxes/glue, `Document::top_skip` and forced
vertical penalties (`penalty <= -10000`); positive penalties are recorded
but not yet part of page badness. `VerticalItem::Insertion` material is