diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index 63067ec..ef201b6 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -839,11 +839,7 @@ fn document_nodes( let mut previous_paragraph = false; for (index, unit) in document.units.iter().enumerate() { for (position, item) in unit.items.iter().enumerate() { - if position == 0 - && document.par_skip > 0 - && previous_paragraph - && matches!(item, VerticalItem::Paragraph(_)) - { + if position == 0 && previous_paragraph && matches!(item, VerticalItem::Paragraph(_)) { nodes.push(VerticalNode::Glue(VerticalGlue::new( document.par_skip, 0, @@ -2043,13 +2039,6 @@ impl Engine { if starts.is_empty() || document.output_routine.is_some() { return false; } - if document.units.iter().any(|unit| { - unit.items - .iter() - .any(|item| matches!(item, VerticalItem::Insertion(_))) - }) { - return false; - } let Some((nodes, owners, node_items)) = document_nodes(document) else { return false; }; @@ -2074,6 +2063,14 @@ impl Engine { .rposition(|start| *start <= first_node) .unwrap_or(0); let start_node = starts[page_index]; + if nodes[..start_node] + .iter() + .any(|node| matches!(node, VerticalNode::Insertion { .. })) + { + // Insertion material raised before the replay start may still be + // held over; without its state a suffix rebuild would drop it. + return false; + } let fragments = Self::paged_builder(document).build_from(&nodes, start_node); let rebuilt = Self::paged_pages(document, &owners, &node_items, &fragments); let previous = std::mem::take(&mut self.layout.pages); @@ -5908,6 +5905,29 @@ mod tests { assert!(Engine::layout_paged(&document).is_none()); } + #[test] + fn paragraphs_break_pages_without_par_skip() { + let style = ParagraphStyle { + line_width: 100 * 65_536, + line_height: 12 * 65_536, + ..ParagraphStyle::default() + }; + let units = [1_u64, 2, 3] + .into_iter() + .map(|id| { + Unit::paragraph_with_style_and_face( + id, + "alpha", + style.clone(), + ParagraphFont::Roman, + ) + }) + .collect(); + let document = Document::new(20 * 65_536, units); + let engine = Engine::layout_paged(&document).expect("paged"); + assert_eq!(engine.pages().len(), 3); + } + #[test] fn paged_layout_edit_relayouts_and_reports_changes() { let style = ParagraphStyle { @@ -5937,6 +5957,77 @@ mod tests { assert_eq!(engine.recomputed_pages(), 1); } + #[test] + fn paged_edit_replays_when_insertions_start_later() { + let style = ParagraphStyle { + line_width: 100 * 65_536, + line_height: 12 * 65_536, + ..ParagraphStyle::default() + }; + let paragraph = |id, text: &str| { + Unit::paragraph_with_style_and_face(id, text, style.clone(), ParagraphFont::Roman) + }; + let mut document = Document::new( + 20 * 65_536, + vec![ + paragraph(1, "alpha"), + paragraph(2, "beta"), + paragraph(3, "gamma"), + paragraph(4, "delta"), + Unit::insertion( + 5, + 0, + 5 * 65_536, + vec![VerticalItem::Rule { + width: 0, + height: 5 * 65_536, + }], + ), + ], + ); + document.par_skip = 2 * 65_536; + let mut engine = Engine::layout_paged(&document).expect("paged"); + assert_eq!(engine.pages().len(), 4); + let first = engine.pages()[0].hash; + engine.edit(&document, 3, "epsilon"); + assert_eq!(engine.pages()[0].hash, first); + assert!(engine.recomputed_pages() < 4); + } + + #[test] + fn paged_edit_rebuilds_when_insertions_precede_the_edit() { + let style = ParagraphStyle { + line_width: 100 * 65_536, + line_height: 12 * 65_536, + ..ParagraphStyle::default() + }; + let paragraph = |id, text: &str| { + Unit::paragraph_with_style_and_face(id, text, style.clone(), ParagraphFont::Roman) + }; + let mut document = Document::new( + 20 * 65_536, + vec![ + Unit::insertion( + 1, + 0, + 5 * 65_536, + vec![VerticalItem::Rule { + width: 0, + height: 5 * 65_536, + }], + ), + paragraph(2, "alpha"), + paragraph(3, "beta"), + paragraph(4, "gamma"), + ], + ); + document.par_skip = 2 * 65_536; + let mut engine = Engine::layout_paged(&document).expect("paged"); + let total = engine.pages().len(); + engine.edit(&document, 4, "delta"); + assert_eq!(engine.recomputed_pages(), total); + } + #[test] fn paged_edit_keeps_untouched_pages() { let style = ParagraphStyle { diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index 163b3b7..c4ca68c 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -248,7 +248,9 @@ oracle fixture is ignored. `PageFragment` also reports the page's total glue stretch and shrink. In the paged builder, `Document::par_skip` is inserted as vertical glue between consecutive paragraph units and is discarded when it would start a page, - matching TeX's paragraph spacing in vertical lists. The routine cannot yet + matching TeX's paragraph spacing in vertical lists; a zero-extent glue node + is still recorded when `\parskip` is zero, so consecutive paragraphs always + offer the page builder a legal breakpoint. The routine cannot yet change page goals. The paged builder places top `Float` material at the top of a page and bottom `Float` material at the page bottom: a late top float starts the next page, a bottom float that cannot fit the remaining body @@ -336,7 +338,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 unit tests covering overfull avoidance, exact glue filling, forced and costed breaks, `\topskip`, `\maxdepth`, marks, insertion limits and top-float placement; every fragment records the penalty that broke it. `Engine::layout_paged` now builds pages through it for documents whose units are single vertical items (boxes, rules, paragraphs, math, haligns, glue, penalties, marks and insertion classes, including per-class limits and held-over material; held-over material starts with `Document::split_top_skip` (TeX `\splittopskip`, default 10pt) when the class capacity can also hold its first item); `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. Paragraphs also split across pages at line boundaries, so body material no longer overflows a page as one box. `Document::h_offset` and `v_offset` (TeX `\hoffset`/`\voffset`, default zero) feed `pdf::PageOrigin`, and `display_lists_to_pdf_with_paper` translates the page body without moving the sheet; a route-D fixture checks a 10pt/20pt shift against `pdftotext -bbox`. `Document::paper_width` and `paper_height` (TeX `\paperwidth`/`\paperheight`, default US Letter) set the PDF MediaBox and the preview page box, and both route-D endpoints strip leading `\hoffset`/`\voffset`/`\paperwidth`/`\paperheight` dimension assignments from the source and apply them. `Engine::top_mark`, `first_mark` and `bot_mark` implement TeX's carried mark registers per class. `Document::output_routine` runs an interpreted `\output` program over the paged fragments (`output::OutputProgram`: `\shipout\box255`, `\relax`, `\message`, `\ifdim`, `\ifnum`, `\ifodd` and `\ifcase` over the page registers, now including `\pageno`, `\outputpenalty` and `\deadcycles`), dropping pages the routine does not ship. The `\pagegoal` register handed to the output routine is reduced by the insertion space reserved on that page. Paged layouts record the node index that starts each page, so `Engine::edit` on a paged layout reuses the sealed prefix and rebuilds only from the edited unit's page onward (documents with insertions or an interpreted `\output` routine still fall back to a full rebuild, because held-over material and dropped pages move page boundaries). The scrolling builder's float path remains to be ported. The oracle harness also compares whole pages: three page fixtures (single-line paragraphs, `\parskip=6pt`, and multi-line paragraphs split across pages) assert the same page count and the same per-page alphanumeric text as pdfLaTeX at 345pt measure, 550pt `\vsize`, 12pt `\topskip` and 15pt `\parindent`. +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 unit tests covering overfull avoidance, exact glue filling, forced and costed breaks, `\topskip`, `\maxdepth`, marks, insertion limits and top-float placement; every fragment records the penalty that broke it. `Engine::layout_paged` now builds pages through it for documents whose units are single vertical items (boxes, rules, paragraphs, math, haligns, glue, penalties, marks and insertion classes, including per-class limits and held-over material; held-over material starts with `Document::split_top_skip` (TeX `\splittopskip`, default 10pt) when the class capacity can also hold its first item); `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. Paragraphs also split across pages at line boundaries, so body material no longer overflows a page as one box. `Document::h_offset` and `v_offset` (TeX `\hoffset`/`\voffset`, default zero) feed `pdf::PageOrigin`, and `display_lists_to_pdf_with_paper` translates the page body without moving the sheet; a route-D fixture checks a 10pt/20pt shift against `pdftotext -bbox`. `Document::paper_width` and `paper_height` (TeX `\paperwidth`/`\paperheight`, default US Letter) set the PDF MediaBox and the preview page box, and both route-D endpoints strip leading `\hoffset`/`\voffset`/`\paperwidth`/`\paperheight` dimension assignments from the source and apply them. `Engine::top_mark`, `first_mark` and `bot_mark` implement TeX's carried mark registers per class. `Document::output_routine` runs an interpreted `\output` program over the paged fragments (`output::OutputProgram`: `\shipout\box255`, `\relax`, `\message`, `\ifdim`, `\ifnum`, `\ifodd` and `\ifcase` over the page registers, now including `\pageno`, `\outputpenalty` and `\deadcycles`), dropping pages the routine does not ship. The `\pagegoal` register handed to the output routine is reduced by the insertion space reserved on that page. Paged layouts record the node index that starts each page, so `Engine::edit` on a paged layout reuses the sealed prefix and rebuilds only from the edited unit's page onward; an interpreted `\output` routine still forces a full rebuild, and an edit whose replay would begin before the first insertion does too, because held-over material and dropped pages move page boundaries. The scrolling builder's float path remains to be ported. The oracle harness also compares whole pages: three page fixtures (single-line paragraphs, `\parskip=6pt`, and multi-line paragraphs split across pages) assert the same page count and the same per-page alphanumeric text as pdfLaTeX at 345pt measure, 550pt `\vsize`, 12pt `\topskip` and 15pt `\parindent`. 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. @@ -344,7 +346,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 264 unit tests plus seven route-D integration tests and seventeen oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 388 tests (264 route-D unit, 7 route-D integration, 17 oracle, 71 v1 integration, 22 server, 4 v1 unit, 1 core unit, 2 binary units), with none ignored. v1 remains the live server path. +The crate currently has 267 unit tests plus seven route-D integration tests and seventeen oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 391 tests (267 route-D unit, 7 route-D integration, 17 oracle, 71 v1 integration, 22 server, 4 v1 unit, 1 core unit, 2 binary units), with none ignored. v1 remains the live server path. ### M0 incremental demo