Expose TeX carried marks for page headers
This commit is contained in:
1 parent
8d319b3b0c
commit
daf9f5c57e
4 files changed
+82
-5
No files matched your search
@@ -586,7 +586,8 @@ 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, the breaker follows TeX's `\pretolerance`/`\tolerance`/`\emergencystretch` passes with demerits (falling back to TeX's overfull fill when no feasible break exists), and the PDF `/W` array reports the same advances, so extracted text keeps word boundaries. Non-final lines bake their justification into the glue widths, so the PDF and the SVG preview are flush to the measure instead of showing natural spaces. Interline spacing follows TeX (`\baselineskip` unless `\lineskiplimit` forces `\lineskip`), and `Document::par_skip` adds `\parskip` between paragraph units. `page::PageBuilder` selects page breaks by TeX's badness-plus-penalty cost, including glue setting, `\topskip` and `Document::max_depth` (`\maxdepth`), and breaks ties toward the page closest to the goal; `Engine::layout_paged` exposes it for documents whose units are single vertical items, while the scrolling builder remains the default until floats, insertions and incremental replay gain paged equivalents. Embedded text fonts are subset per document to the glyphs actually used (retaining glyph IDs and composite components), and each subset carries a ToUnicode CMap covering its used characters. `Document::h_offset` and `v_offset` (`\hoffset`/`\voffset`) shift the exported
|
||||
page body through `pdf::PageOrigin`, leaving the sheet size unchanged; the
|
||||
route-D PDF endpoint reads leading `\hoffset`/`\voffset` dimension assignments
|
||||
from the submitted source.
|
||||
from the submitted source. Page marks collected by `\mark` are exposed through
|
||||
`Engine::top_mark`, `first_mark` and `bot_mark` with TeX's carry-forward rules.
|
||||
`Document::insertion_classes` caps each insertion class to a page fraction and defers overflow to the next page, and `InsertionClass::multiplier` applies TeX's `\count<number>` reserve factor; `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
|
||||
|
||||
@@ -1415,6 +1415,44 @@ impl Engine {
|
||||
&self.layout.pages
|
||||
}
|
||||
|
||||
/// TeX `\topmark` when shipping out `page`: the last mark left by an
|
||||
/// earlier page, carried forward until a page defines its own.
|
||||
pub fn top_mark(&self, page: usize, class: u8) -> Option<&Mark> {
|
||||
(0..page).rev().find_map(|index| {
|
||||
self.layout.pages.get(index).and_then(|candidate| {
|
||||
candidate
|
||||
.marks
|
||||
.iter()
|
||||
.rev()
|
||||
.find(|mark| mark.class == class)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/// TeX `\firstmark` when shipping out `page`.
|
||||
pub fn first_mark(&self, page: usize, class: u8) -> Option<&Mark> {
|
||||
self.layout
|
||||
.pages
|
||||
.get(page)
|
||||
.and_then(|candidate| candidate.marks.iter().find(|mark| mark.class == class))
|
||||
.or_else(|| self.top_mark(page, class))
|
||||
}
|
||||
|
||||
/// TeX `\botmark` when shipping out `page`.
|
||||
pub fn bot_mark(&self, page: usize, class: u8) -> Option<&Mark> {
|
||||
self.layout
|
||||
.pages
|
||||
.get(page)
|
||||
.and_then(|candidate| {
|
||||
candidate
|
||||
.marks
|
||||
.iter()
|
||||
.rev()
|
||||
.find(|mark| mark.class == class)
|
||||
})
|
||||
.or_else(|| self.top_mark(page, class))
|
||||
}
|
||||
|
||||
pub fn checkpoints(&self) -> &[Checkpoint] {
|
||||
&self.layout.checkpoints
|
||||
}
|
||||
@@ -2206,6 +2244,42 @@ mod tests {
|
||||
assert_eq!(engine.layout.pages[1].items.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn marks_carry_to_following_pages() {
|
||||
let document = Document::new(
|
||||
100,
|
||||
vec![
|
||||
Unit::mark(0, 1, "first"),
|
||||
Unit::mark(1, 1, "second"),
|
||||
Unit::rule(2, 10, 80),
|
||||
Unit::rule(3, 10, 80),
|
||||
],
|
||||
);
|
||||
let engine = Engine::layout(&document);
|
||||
assert_eq!(engine.pages().len(), 2);
|
||||
assert!(engine.top_mark(0, 1).is_none());
|
||||
assert_eq!(
|
||||
engine.first_mark(0, 1).map(|mark| mark.text.as_str()),
|
||||
Some("first")
|
||||
);
|
||||
assert_eq!(
|
||||
engine.bot_mark(0, 1).map(|mark| mark.text.as_str()),
|
||||
Some("second")
|
||||
);
|
||||
assert_eq!(
|
||||
engine.top_mark(1, 1).map(|mark| mark.text.as_str()),
|
||||
Some("second")
|
||||
);
|
||||
assert_eq!(
|
||||
engine.first_mark(1, 1).map(|mark| mark.text.as_str()),
|
||||
Some("second")
|
||||
);
|
||||
assert_eq!(
|
||||
engine.bot_mark(1, 1).map(|mark| mark.text.as_str()),
|
||||
Some("second")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn marks_are_attached_to_pages_and_affect_the_hash() {
|
||||
let document = |text: &str| {
|
||||
|
||||
@@ -48,7 +48,7 @@ floats (`\floatsep`, `\textfloatsep`, `\intextsep`, `\topfraction`,
|
||||
`\bottomnumber`, `\totalnumber`), insertion classes (`\skip`, fraction and
|
||||
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
|
||||
PDF opt-in with `?engine=route-d`). 317 workspace tests are green and no oracle
|
||||
PDF opt-in with `?engine=route-d`). 318 workspace tests are green and no oracle
|
||||
fixture is ignored.
|
||||
|
||||
Known gaps, in dependency order:
|
||||
@@ -228,7 +228,9 @@ Scope:
|
||||
recomputation as insertions arrive, `\pagegoal`, `\pagetotal`,
|
||||
`\pagestretch`, `\pagefilstretch`, `\pagefillstretch`, `\pagefilllstretch`,
|
||||
`\pageshrink`, `\pagedepth`;
|
||||
- marks: `\mark`, `\botmark`, `\firstmark`, `\topmark`, `\marks`;
|
||||
- marks: `\mark`, `\botmark`, `\firstmark`, `\topmark`, `\marks` (marks are
|
||||
collected per page; `Engine::top_mark`/`first_mark`/`bot_mark` now carry the
|
||||
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
|
||||
|
||||
+2
-2
@@ -304,7 +304,7 @@ 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 per-class `skip` rules as full layout. `Document::float_sep` (TeX `\floatsep`, default 12pt) reserves a gap between stacked floats of the same placement in both paths. `Document::text_float_sep` (TeX `\textfloatsep`, default 20pt) reserves exactly one gap between the float block and the body text on a page that has both, again in both paths; `Page::display_list_with_layout(page_goal, text_float_sep)` applies the same gap when rendering, while `display_list_with_goal` stays gap-free for callers that do not know the document settings. Float material at the top of a page starts at the top margin: `\topskip` is consumed only by body text, matching TeX float pages. `FloatPlacement::Here` models `[h]` floats: they keep their place in the body flow with `Document::in_text_sep` (TeX `\intextsep`, default 12pt) above and below, move to the next page when they do not fit, and count against `\totalnumber`. The page stores the float's `gap_after` so the display list advances the flow by the same amount the builder reserved. A standalone `page::PageBuilder` implements TeX's candidate-breakpoint cost model (page badness from accumulated glue stretch/shrink plus break penalties, `\topskip` for the first box, `\maxdepth` depth shifting) with seven unit tests. `Engine::layout_paged` now builds pages through it for documents whose units are single vertical items (boxes, rules, paragraphs, math, haligns, glue, penalties and marks); `Document::max_depth` feeds TeX's `\maxdepth` into the builder, and among equal-cost breakpoints the candidate closest to the page goal wins, so an overfull page takes the least overfull break. `Document::h_offset` and `v_offset` (TeX `\hoffset`/`\voffset`, default zero) feed `pdf::PageOrigin`, and `display_lists_to_pdf_with_origin` translates the page body without moving the sheet; a route-D fixture checks a 10pt/20pt shift against `pdftotext -bbox`, and the route-D PDF endpoint strips leading `\hoffset`/`\voffset` dimension assignments from the source and applies them. Floats, insertions and the incremental checkpoint replay still use the scrolling builder, which remains the default until those pieces have a paged equivalent.
|
||||
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. `Document::float_sep` (TeX `\floatsep`, default 12pt) reserves a gap between stacked floats of the same placement in both paths. `Document::text_float_sep` (TeX `\textfloatsep`, default 20pt) reserves exactly one gap between the float block and the body text on a page that has both, again in both paths; `Page::display_list_with_layout(page_goal, text_float_sep)` applies the same gap when rendering, while `display_list_with_goal` stays gap-free for callers that do not know the document settings. Float material at the top of a page starts at the top margin: `\topskip` is consumed only by body text, matching TeX float pages. `FloatPlacement::Here` models `[h]` floats: they keep their place in the body flow with `Document::in_text_sep` (TeX `\intextsep`, default 12pt) above and below, move to the next page when they do not fit, and count against `\totalnumber`. The page stores the float's `gap_after` so the display list advances the flow by the same amount the builder reserved. A standalone `page::PageBuilder` implements TeX's candidate-breakpoint cost model (page badness from accumulated glue stretch/shrink plus break penalties, `\topskip` for the first box, `\maxdepth` depth shifting) with seven unit tests. `Engine::layout_paged` now builds pages through it for documents whose units are single vertical items (boxes, rules, paragraphs, math, haligns, glue, penalties and marks); `Document::max_depth` feeds TeX's `\maxdepth` into the builder, and among equal-cost breakpoints the candidate closest to the page goal wins, so an overfull page takes the least overfull break. `Document::h_offset` and `v_offset` (TeX `\hoffset`/`\voffset`, default zero) feed `pdf::PageOrigin`, and `display_lists_to_pdf_with_origin` translates the page body without moving the sheet; a route-D fixture checks a 10pt/20pt shift against `pdftotext -bbox`, and the route-D PDF endpoint strips leading `\hoffset`/`\voffset` dimension assignments from the source and applies them. `Engine::top_mark`, `first_mark` and `bot_mark` implement TeX's carried mark registers per class. Floats, insertions and the incremental checkpoint replay still use the scrolling builder, which remains the default until those pieces have a paged equivalent.
|
||||
|
||||
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.
|
||||
|
||||
@@ -312,7 +312,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.
|
||||
|
||||
The crate currently has 204 unit tests plus seven route-D integration tests and eight oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 317 tests (204 route-D unit, 7 route-D integration, 8 oracle, 71 v1 integration, 20 server, 4 v1 unit, 1 core unit, 2 binary units), with none ignored. v1 remains the live server path.
|
||||
The crate currently has 205 unit tests plus seven route-D integration tests and eight oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 318 tests (205 route-D unit, 7 route-D integration, 8 oracle, 71 v1 integration, 20 server, 4 v1 unit, 1 core unit, 2 binary units), with none ignored. v1 remains the live server path.
|
||||
|
||||
### M0 incremental demo
|
||||
|
||||
|
||||
Reference in new issue
Block a user