Support per-class insertion skip

InsertionClass gains skip, matching TeX skip registers for insertion classes. The page builder prefers that value over Document.insertion_skip for the class, in both full and incremental layout; regressions cover a skip that pushes an insertion to the next page and the edit path.
This commit is contained in:
gloamxun committed 2026-10-21 09:01:12 +08:00
1 parent 7385b8862a
commit 06779fb8dd
3 files changed
+88 -6

No files matched your search

+1 -1
View File
@@ -576,7 +576,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`.
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
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.
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, and `InsertionClass::skip` overrides it for one 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; text edits and structural edits with an unchanged paragraph prefix rebuild from the first difference through `Engine::relayout_from`, reusing the sealed pages before it, while edits without a reusable prefix fall back to a full layout. `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
+83 -2
View File
@@ -522,6 +522,10 @@ pub struct InsertionClass {
/// page. `None` uses only the percentage share.
#[serde(default)]
pub max_height: Option<i64>,
/// TeX's `\skip<number>`: space inserted before this class on a page.
/// `None` falls back to `Document::insertion_skip`.
#[serde(default)]
pub skip: Option<i64>,
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
@@ -621,6 +625,16 @@ impl Document {
self.top_skip = top_skip;
self
}
/// TeX's `\skip<insert class>`, falling back to the document-wide
/// `insertion_skip` for classes without an explicit skip.
pub fn insertion_skip_for(&self, class: u8) -> i64 {
self.insertion_classes
.iter()
.find(|entry| entry.number == class)
.and_then(|entry| entry.skip)
.unwrap_or(self.insertion_skip)
}
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
@@ -951,7 +965,7 @@ impl Engine {
}
VerticalItem::Insertion(insertion) => {
let gap = if current.insertions.is_empty() {
document.insertion_skip.max(0)
document.insertion_skip_for(insertion.class).max(0)
} else {
0
};
@@ -1374,7 +1388,7 @@ impl Engine {
}
VerticalItem::Insertion(insertion) => {
let gap = if current.insertions.is_empty() {
next.insertion_skip.max(0)
next.insertion_skip_for(insertion.class).max(0)
} else {
0
};
@@ -1847,6 +1861,7 @@ mod tests {
number: 1,
fraction_percent: 30,
max_height: None,
skip: None,
}];
let layout = Engine::layout(&document);
assert_eq!(layout.pages().len(), 2);
@@ -1873,11 +1888,13 @@ mod tests {
number: 1,
fraction_percent: 60,
max_height: None,
skip: None,
},
InsertionClass {
number: 2,
fraction_percent: 60,
max_height: None,
skip: None,
},
];
let layout = Engine::layout(&document);
@@ -1903,6 +1920,7 @@ mod tests {
number: 1,
fraction_percent: 100,
max_height: Some(150),
skip: None,
}];
let layout = Engine::layout(&document);
assert_eq!(layout.pages().len(), 2);
@@ -2045,6 +2063,7 @@ mod tests {
number: 1,
fraction_percent: 30,
max_height: None,
skip: None,
}];
let mut engine = Engine::layout(&document);
assert_eq!(engine.pages().len(), 2);
@@ -2055,6 +2074,66 @@ mod tests {
assert_eq!(engine.pages()[1].insertions.len(), 1);
}
#[test]
fn insertion_class_skip_reserves_space_for_that_class() {
let insertion = |id| {
Unit::insertion(
id,
1,
480,
vec![VerticalItem::Rule {
width: 65_536,
height: 480,
}],
)
};
let mut document = Document::new(1000, vec![insertion(1), insertion(2)]);
document.insertion_classes = vec![InsertionClass {
number: 1,
fraction_percent: 100,
max_height: None,
skip: Some(50),
}];
let layout = Engine::layout(&document);
assert_eq!(layout.pages().len(), 2);
assert_eq!(layout.pages()[0].insertions.len(), 1);
assert_eq!(layout.pages()[1].insertions.len(), 1);
document.insertion_classes[0].skip = None;
assert_eq!(Engine::layout(&document).pages().len(), 1);
}
#[test]
fn incremental_layout_respects_insertion_class_skip() {
let mut style = ParagraphStyle::default();
style.line_height = 500;
let mut document = Document::new(
1000,
vec![
Unit::paragraph_with_style(1, "body", style),
Unit::insertion(
2,
1,
480,
vec![VerticalItem::Rule {
width: 65_536,
height: 480,
}],
),
],
);
document.insertion_classes = vec![InsertionClass {
number: 1,
fraction_percent: 100,
max_height: None,
skip: Some(50),
}];
let mut engine = Engine::layout(&document);
assert_eq!(engine.pages().len(), 2);
engine.edit(&document, 0, "changed body");
assert_eq!(engine.pages().len(), 2);
}
#[test]
fn insertion_skip_reserves_space_before_the_first_insertion() {
let insertion = || {
@@ -2073,6 +2152,7 @@ mod tests {
number: 1,
fraction_percent: 100,
max_height: None,
skip: None,
}];
assert_eq!(Engine::layout(&document).pages().len(), 1);
@@ -2104,6 +2184,7 @@ mod tests {
number: 1,
fraction_percent: 100,
max_height: None,
skip: None,
}];
assert_eq!(Engine::layout(&exact).pages().len(), 1);
}
+4 -3
View File
@@ -171,7 +171,8 @@ a single cost function.
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.
`Document::insertion_skip` models TeX's `\skip\footins` and is reserved once
before the first insertion class on a page; it defaults to zero.
before the first insertion class on a page; it defaults to zero, and
`InsertionClass::skip` overrides it for a single insertion class.
Insertions taller than a page are split at item boundaries across pages.
- Page breaking handles vertical boxes/glue, `Document::top_skip` and forced
vertical penalties (`penalty <= -10000`); positive penalties are recorded
@@ -263,13 +264,13 @@ 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 `insertion_skip` rules as full layout.
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.
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.
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 159 unit tests plus three route-D integration tests. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 259 tests (159 route-D unit, 3 route-D integration, 71 v1 integration, 19 server, 4 v1 unit, 1 core unit, 2 binary units). v1 remains the live server path.
The crate currently has 161 unit tests plus three route-D integration tests. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 261 tests (161 route-D unit, 3 route-D integration, 71 v1 integration, 19 server, 4 v1 unit, 1 core unit, 2 binary units). v1 remains the live server path.
### M0 incremental demo