Cap page depth at Document max_depth
This commit is contained in:
1 parent
9599982b91
commit
169119a48d
5 files changed
+66
-10
No files matched your search
@@ -583,7 +583,7 @@ execution, service and incrementality targets) is in
|
|||||||
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, 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 `\maxdepth`; `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::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.
|
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::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
|
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
|
document and returns the same page list plus `changedPages` and
|
||||||
|
|||||||
@@ -557,6 +557,10 @@ pub struct Document {
|
|||||||
/// TeX-like `\topskip`: distance from the page top to the first baseline.
|
/// TeX-like `\topskip`: distance from the page top to the first baseline.
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub top_skip: i64,
|
pub top_skip: i64,
|
||||||
|
/// TeX-like `\maxdepth`: depth a page box keeps below the baseline. Deeper
|
||||||
|
/// material moves into the page height, as in TeX's page builder.
|
||||||
|
#[serde(default)]
|
||||||
|
pub max_depth: i64,
|
||||||
/// Minimum percentage of the page that text must occupy before a bottom
|
/// Minimum percentage of the page that text must occupy before a bottom
|
||||||
/// float is allowed onto the page (TeX's `\textfraction`, in percent).
|
/// float is allowed onto the page (TeX's `\textfraction`, in percent).
|
||||||
#[serde(default = "default_text_fraction")]
|
#[serde(default = "default_text_fraction")]
|
||||||
@@ -646,6 +650,7 @@ impl Document {
|
|||||||
units,
|
units,
|
||||||
page_goal,
|
page_goal,
|
||||||
top_skip: 0,
|
top_skip: 0,
|
||||||
|
max_depth: 0,
|
||||||
text_fraction_percent: default_text_fraction(),
|
text_fraction_percent: default_text_fraction(),
|
||||||
float_page_fraction_percent: 50,
|
float_page_fraction_percent: 50,
|
||||||
bottom_fraction_percent: default_bottom_fraction(),
|
bottom_fraction_percent: default_bottom_fraction(),
|
||||||
@@ -1390,7 +1395,7 @@ impl Engine {
|
|||||||
let builder = PageBuilder {
|
let builder = PageBuilder {
|
||||||
page_goal: document.page_goal,
|
page_goal: document.page_goal,
|
||||||
top_skip: document.top_skip,
|
top_skip: document.top_skip,
|
||||||
max_depth: 0,
|
max_depth: document.max_depth,
|
||||||
};
|
};
|
||||||
let mut pages = Vec::new();
|
let mut pages = Vec::new();
|
||||||
for fragment in builder.build(&nodes) {
|
for fragment in builder.build(&nodes) {
|
||||||
@@ -4368,6 +4373,37 @@ mod tests {
|
|||||||
assert!(engine.pages().iter().all(|page| page.hash != 0));
|
assert!(engine.pages().iter().all(|page| page.hash != 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn paged_layout_limits_page_depth() {
|
||||||
|
let math = Unit {
|
||||||
|
id: 1,
|
||||||
|
source: String::new(),
|
||||||
|
items: vec![VerticalItem::Math {
|
||||||
|
atom: crate::math::MathAtom::Symbol('x'),
|
||||||
|
width: 10,
|
||||||
|
height: 70,
|
||||||
|
depth: 30,
|
||||||
|
}],
|
||||||
|
paragraph_style: None,
|
||||||
|
};
|
||||||
|
let mut document =
|
||||||
|
Document::new(100, vec![math, Unit::vskip(2, 0), Unit::rule(3, 100, 20)]);
|
||||||
|
let deep = Engine::layout_paged(&document).expect("math is page-buildable");
|
||||||
|
assert_eq!(
|
||||||
|
deep.pages().len(),
|
||||||
|
2,
|
||||||
|
"depth beyond maxdepth pushes the rule onto its own page"
|
||||||
|
);
|
||||||
|
|
||||||
|
document.max_depth = 30;
|
||||||
|
let capped = Engine::layout_paged(&document).expect("math is page-buildable");
|
||||||
|
assert_eq!(
|
||||||
|
capped.pages().len(),
|
||||||
|
1,
|
||||||
|
"allowed depth keeps the rule on the first page"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn paged_layout_refuses_floats_and_insertions() {
|
fn paged_layout_refuses_floats_and_insertions() {
|
||||||
let floated = Document::new(100, vec![Unit::float(1, FloatPlacement::Top, 100, 20)]);
|
let floated = Document::new(100, vec![Unit::float(1, FloatPlacement::Top, 100, 20)]);
|
||||||
|
|||||||
@@ -8,7 +8,6 @@
|
|||||||
//! `Engine`; wiring this module into `Document` is the next P2 step.
|
//! `Engine`; wiring this module into `Document` is the next P2 step.
|
||||||
|
|
||||||
use crate::Mark;
|
use crate::Mark;
|
||||||
use std::cmp::Reverse;
|
|
||||||
|
|
||||||
/// Vertical glue with the stretch and shrink TeX uses to set a page.
|
/// Vertical glue with the stretch and shrink TeX uses to set a page.
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
@@ -250,7 +249,7 @@ impl PageBuilder {
|
|||||||
feasible
|
feasible
|
||||||
};
|
};
|
||||||
pool.into_iter()
|
pool.into_iter()
|
||||||
.min_by_key(|candidate| (candidate.cost, Reverse(candidate.height)))
|
.min_by_key(|candidate| (candidate.cost, (candidate.height - self.page_goal).abs()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -347,6 +346,25 @@ mod tests {
|
|||||||
assert_eq!(pages[0].depth, 4);
|
assert_eq!(pages[0].depth, 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn overfull_pages_take_the_least_overfull_break() {
|
||||||
|
let builder = PageBuilder::new(50);
|
||||||
|
let nodes = [
|
||||||
|
VerticalNode::Box {
|
||||||
|
height: 60,
|
||||||
|
depth: 0,
|
||||||
|
},
|
||||||
|
VerticalNode::Glue(VerticalGlue::new(0, 0, 0)),
|
||||||
|
VerticalNode::Box {
|
||||||
|
height: 40,
|
||||||
|
depth: 0,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
let pages = builder.build(&nodes);
|
||||||
|
assert_eq!(pages.len(), 2);
|
||||||
|
assert_eq!(pages[0].height, 60, "the shorter overfull page wins");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn marks_do_not_change_page_heights() {
|
fn marks_do_not_change_page_heights() {
|
||||||
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`). 311 workspace tests are green and no oracle
|
PDF opt-in with `?engine=route-d`). 313 workspace tests are green and no oracle
|
||||||
fixture is ignored.
|
fixture is ignored.
|
||||||
|
|
||||||
Known gaps, in dependency order:
|
Known gaps, in dependency order:
|
||||||
@@ -243,9 +243,11 @@ applying `\topskip` to the first box and `\maxdepth` to the page depth. Seven
|
|||||||
unit tests cover overfull avoidance, exact glue filling, forced penalties,
|
unit tests cover overfull avoidance, exact glue filling, forced penalties,
|
||||||
penalty-driven earlier breaks, `\topskip`, `\maxdepth` and marks.
|
penalty-driven earlier breaks, `\topskip`, `\maxdepth` and marks.
|
||||||
`Engine::layout_paged` now feeds it every document whose units are single
|
`Engine::layout_paged` now feeds it every document whose units are single
|
||||||
vertical items and materialises the chosen fragments as hashed pages; floats,
|
vertical items (`Document::max_depth` supplies `\maxdepth`) and materialises the
|
||||||
insertions and checkpoint replay still fall back to the scrolling builder, which
|
chosen fragments as hashed pages; ties go to the breakpoint closest to the page
|
||||||
stays the default until it has a paged equivalent. Next: port those paths, then
|
goal, so overfull pages take the least overfull break. 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
|
||||||
`\voffset`, insertion registers and the interpreted `\output`.
|
`\voffset`, insertion registers and the interpreted `\output`.
|
||||||
|
|
||||||
Acceptance: 10 page fixtures (article text, footnotes, floats, forced breaks);
|
Acceptance: 10 page fixtures (article text, footnotes, floats, forced breaks);
|
||||||
|
|||||||
+2
-2
@@ -299,7 +299,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.
|
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); 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. 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.
|
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.
|
||||||
|
|
||||||
@@ -307,7 +307,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 200 unit tests plus six route-D integration tests and eight oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 311 tests (200 route-D unit, 6 route-D integration, 8 oracle, 71 v1 integration, 19 server, 4 v1 unit, 1 core unit, 2 binary units), with none ignored. v1 remains the live server path.
|
The crate currently has 202 unit tests plus six route-D integration tests and eight oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 313 tests (202 route-D unit, 6 route-D integration, 8 oracle, 71 v1 integration, 19 server, 4 v1 unit, 1 core unit, 2 binary units), with none ignored. v1 remains the live server path.
|
||||||
|
|
||||||
### M0 incremental demo
|
### M0 incremental demo
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user