Reserve skip before the first insertion class

This commit is contained in:
gloamxun committed 2026-10-21 08:28:34 +08:00
1 parent e052a0d0f3
commit 180c05e425
3 files changed
+91 -10

No files matched your search

+1 -1
View File
@@ -575,7 +575,7 @@ default until the new core reaches layout parity. See `docs/TEX_CORE.md`.
The first M7 service integration is available as `POST /api/route-d/preview`, with PDF export at `POST /api/route-d/pdf`. The first M7 service integration is available as `POST /api/route-d/preview`, with PDF export at `POST /api/route-d/pdf`.
It accepts blank-line-separated source with a small `\section`/`\textbf`/`\emph`/display-math/list subset, lays it out with the route-D page It accepts blank-line-separated source with a small `\section`/`\textbf`/`\emph`/display-math/list subset, lays it out with the route-D page
builder, and returns the v1-compatible preview shape plus per-page hashes and builder, and returns the v1-compatible preview shape plus per-page hashes and
SVG. Route-D paragraphs are laid out with CMR10 advances, kerns and ligatures; runs of two or three hyphens become en/em dashes, and the PDF `/W` array reports the same advances, so extracted text keeps word boundaries. `Document::insertion_classes` caps each insertion class to a page fraction and defers overflow to the next page. The v1 `/preview` and project APIs are unchanged and remain the default. SVG. Route-D paragraphs are laid out with CMR10 advances, kerns and ligatures; runs of two or three hyphens become en/em dashes, and the PDF `/W` array reports the same advances, so extracted text keeps word boundaries. `Document::insertion_classes` caps each insertion class to a page fraction and defers overflow to the next page; `Document::insertion_skip` models `\skip\footins` once per page before the first insertion class. The v1 `/preview` and project APIs are unchanged and remain the default.
Project previews can opt into the new core with `POST /api/projects/{id}/preview?engine=route-d`; the same project preview without the query keeps the v1 pipeline. The web client exposes this as the `引擎: v1` / `引擎: Route-D` toggle in the preview toolbar; the choice is stored in `localStorage` and switches both the preview request and `POST /api/projects/{id}/pdf`, since the PDF handler honors the same `engine=route-d` query. Route-D project previews keep a session, so an unchanged source reports zero recomputed pages and a one-paragraph edit returns the changed/recomputed page set. `POST /api/route-d/edit/{unit}` applies one paragraph edit to the last route-D Project previews can opt into the new core with `POST /api/projects/{id}/preview?engine=route-d`; the same project preview without the query keeps the v1 pipeline. The web client exposes this as the `引擎: v1` / `引擎: Route-D` toggle in the preview toolbar; the choice is stored in `localStorage` and switches both the preview request and `POST /api/projects/{id}/pdf`, since the PDF handler honors the same `engine=route-d` query. Route-D project previews keep a session, so an unchanged source reports zero recomputed pages and a one-paragraph edit returns the changed/recomputed page set. `POST /api/route-d/edit/{unit}` applies one paragraph edit to the last route-D
document and returns the same page list plus `changedPages` and document and returns the same page list plus `changedPages` and
+87 -8
View File
@@ -548,6 +548,10 @@ pub struct Document {
/// Per-class insertion limits. Missing classes use the full page. /// Per-class insertion limits. Missing classes use the full page.
#[serde(default)] #[serde(default)]
pub insertion_classes: Vec<InsertionClass>, pub insertion_classes: Vec<InsertionClass>,
/// TeX's `\skip\footins`: space reserved between the page body and the
/// first insertion class on a page. Zero by default for compatibility.
#[serde(default)]
pub insertion_skip: i64,
} }
fn default_text_fraction() -> u8 { fn default_text_fraction() -> u8 {
@@ -573,6 +577,7 @@ impl Document {
bottom_fraction_percent: default_bottom_fraction(), bottom_fraction_percent: default_bottom_fraction(),
top_fraction_percent: default_top_fraction(), top_fraction_percent: default_top_fraction(),
insertion_classes: Vec::new(), insertion_classes: Vec::new(),
insertion_skip: 0,
} }
} }
@@ -921,7 +926,16 @@ impl Engine {
} }
} }
VerticalItem::Insertion(insertion) => { VerticalItem::Insertion(insertion) => {
let capacity = document.page_goal.saturating_sub(document.top_skip).max(1); let gap = if current.insertions.is_empty() {
document.insertion_skip.max(0)
} else {
0
};
let capacity = document
.page_goal
.saturating_sub(document.top_skip)
.saturating_sub(gap)
.max(1);
if insertion_height + insertion.height > capacity if insertion_height + insertion.height > capacity
&& (!current.items.is_empty() || !current.insertions.is_empty()) && (!current.items.is_empty() || !current.insertions.is_empty())
{ {
@@ -931,7 +945,12 @@ impl Engine {
} }
let class_limit = document let class_limit = document
.insertion_class_limit(insertion.class) .insertion_class_limit(insertion.class)
.min(document.page_goal.saturating_sub(document.top_skip)) .min(
document
.page_goal
.saturating_sub(document.top_skip)
.saturating_sub(gap),
)
.max(1); .max(1);
let class_height: i64 = current let class_height: i64 = current
.insertions .insertions
@@ -950,6 +969,7 @@ impl Engine {
} }
let mut chunk = Vec::new(); let mut chunk = Vec::new();
let mut chunk_height = 0; let mut chunk_height = 0;
let mut chunk_gap = gap;
for sub_item in &insertion.items { for sub_item in &insertion.items {
let sub_height = item_height(sub_item); let sub_height = item_height(sub_item);
if chunk_height + sub_height > class_limit && !chunk.is_empty() { if chunk_height + sub_height > class_limit && !chunk.is_empty() {
@@ -958,10 +978,11 @@ impl Engine {
height: chunk_height, height: chunk_height,
items: std::mem::take(&mut chunk), items: std::mem::take(&mut chunk),
}); });
insertion_height = chunk_height; insertion_height = chunk_gap + chunk_height;
chunk_height = 0; chunk_height = 0;
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;
chunk_gap = gap;
} }
chunk.push(sub_item.clone()); chunk.push(sub_item.clone());
chunk_height += sub_height; chunk_height += sub_height;
@@ -972,19 +993,19 @@ impl Engine {
height: chunk_height, height: chunk_height,
items: chunk, items: chunk,
}); });
insertion_height = chunk_height; insertion_height = chunk_gap + chunk_height;
} }
} else { } else {
let goal = document.page_goal - insertion_height; let goal = document.page_goal - insertion_height;
if (!current.items.is_empty() || !current.insertions.is_empty()) if (!current.items.is_empty() || !current.insertions.is_empty())
&& (used + insertion.height > goal && (used + gap + insertion.height > goal
|| class_height + insertion.height > class_limit) || class_height + insertion.height > class_limit)
{ {
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;
insertion_height = 0; insertion_height = 0;
} }
insertion_height += insertion.height; insertion_height += gap + insertion.height;
current.insertions.push(insertion.clone()); current.insertions.push(insertion.clone());
} }
} }
@@ -1297,13 +1318,18 @@ impl Engine {
} }
} }
VerticalItem::Insertion(insertion) => { VerticalItem::Insertion(insertion) => {
let gap = if current.insertions.is_empty() {
next.insertion_skip.max(0)
} else {
0
};
let goal = next.page_goal - insertion_height; let goal = next.page_goal - insertion_height;
if !current.items.is_empty() && used + insertion.height > goal { if !current.items.is_empty() && used + gap + insertion.height > goal {
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;
insertion_height = 0; insertion_height = 0;
} }
insertion_height += insertion.height; insertion_height += gap + insertion.height;
current.insertions.push(insertion.clone()); current.insertions.push(insertion.clone());
} }
VerticalItem::Mark(mark) => { VerticalItem::Mark(mark) => {
@@ -1798,6 +1824,59 @@ mod tests {
assert_eq!(layout.pages()[1].insertions.len(), 1); assert_eq!(layout.pages()[1].insertions.len(), 1);
} }
#[test]
fn insertion_skip_reserves_space_before_the_first_insertion() {
let insertion = || {
Unit::insertion(
2,
1,
80,
vec![VerticalItem::Rule {
width: 65_536,
height: 80,
}],
)
};
let mut document = Document::new(100, vec![Unit::rule(1, 10, 10), insertion()]);
document.insertion_classes = vec![InsertionClass {
number: 1,
fraction_percent: 100,
max_height: None,
}];
assert_eq!(Engine::layout(&document).pages().len(), 1);
document.insertion_skip = 20;
let layout = Engine::layout(&document);
assert_eq!(layout.pages().len(), 2);
assert_eq!(layout.pages()[0].insertions.len(), 0);
assert_eq!(layout.pages()[1].insertions.len(), 1);
// A body plus insertion plus skip that exactly fills the page must not
// be pushed to a second page.
let mut exact = Document::new(
100,
vec![
Unit::rule(1, 10, 10),
Unit::insertion(
2,
1,
70,
vec![VerticalItem::Rule {
width: 65_536,
height: 70,
}],
),
],
);
exact.insertion_skip = 20;
exact.insertion_classes = vec![InsertionClass {
number: 1,
fraction_percent: 100,
max_height: None,
}];
assert_eq!(Engine::layout(&exact).pages().len(), 1);
}
#[test] #[test]
fn incremental_layout_places_a_fitting_late_top_float_the_same_way() { fn incremental_layout_places_a_fitting_late_top_float_the_same_way() {
let mut document = Document::new( let mut document = Document::new(
+3 -1
View File
@@ -170,6 +170,8 @@ a single cost function.
`Document::insertion_classes` sets a per-class page fraction; an insertion `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 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. one, splitting at item boundaries when the insertion itself is too tall.
`Document::insertion_skip` models TeX's `\skip\footins` and is reserved once
before the first insertion class on a page; it defaults to zero.
Insertions taller than a page are split at item boundaries across pages. Insertions taller than a page are split at item boundaries across pages.
- Page breaking handles vertical boxes/glue, `Document::top_skip` and forced - Page breaking handles vertical boxes/glue, `Document::top_skip` and forced
vertical penalties (`penalty <= -10000`); positive penalties are recorded vertical penalties (`penalty <= -10000`); positive penalties are recorded
@@ -267,7 +269,7 @@ M3 (partial): `Unit::halign` normalizes column widths, intercolumn gaps, row hei
Math coverage includes fractions/roots, scripts, `\left...\right` with body-scaled delimiters, `\middle`, `\big`/`\Big`/`\bigg`/`\Bigg` and their `l`/`r` forms, control-sequence delimiters, accents (dot/check/breve/acute/grave/ring), `\text`/`\mathrm`, `\overbrace`/`\underbrace`, `\overset`/`\underset`, `\binom`, `\cancel`/`\bcancel`/`\xcancel`, `\bmod`/`\pmod`, fence symbols, `\operatorname*` and `\mathop` limits. Math coverage includes fractions/roots, scripts, `\left...\right` with body-scaled delimiters, `\middle`, `\big`/`\Big`/`\bigg`/`\Bigg` and their `l`/`r` forms, control-sequence delimiters, accents (dot/check/breve/acute/grave/ring), `\text`/`\mathrm`, `\overbrace`/`\underbrace`, `\overset`/`\underset`, `\binom`, `\cancel`/`\bcancel`/`\xcancel`, `\bmod`/`\pmod`, fence symbols, `\operatorname*` and `\mathop` limits.
The crate currently has 153 unit tests plus two route-D integration tests. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 250 tests (153 route-D unit, 2 route-D integration, 71 v1 integration, 19 server, 4 v1 unit, 1 core unit, 1 doctest). v1 remains the live server path. The crate currently has 154 unit tests plus two route-D integration tests. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 251 tests (154 route-D unit, 2 route-D integration, 71 v1 integration, 19 server, 4 v1 unit, 1 core unit, 1 doctest). v1 remains the live server path.
### M0 incremental demo ### M0 incremental demo