diff --git a/README.md b/README.md index 154322e..794eccd 100644 --- a/README.md +++ b/README.md @@ -577,6 +577,6 @@ It accepts blank-line-separated source with a small `\section`/`\textbf`/`\emph` 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. -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 one or more paragraph edits return the changed/recomputed page set after replaying `Engine::edit` from the earliest changed paragraph. `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 `recomputedPages`, exposing the checkpoint/page-hash diff from the core. diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index aceb0c7..763aea2 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -269,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. -The crate currently has 155 unit tests plus three route-D integration tests. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 253 tests (155 route-D unit, 3 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 155 unit tests plus three route-D integration tests. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 254 tests (155 route-D unit, 3 route-D integration, 71 v1 integration, 19 server, 4 v1 unit, 1 core unit, 1 binary unit, 1 doctest). v1 remains the live server path. ### M0 incremental demo @@ -307,9 +307,10 @@ deliberately open rather than approximated: project preview can opt in with `POST /api/projects/{id}/preview?engine=route-d`; `route_d::project_preview_json` strips the preamble/body wrapper and keeps a project-scoped `(paragraphs, Engine)` session. An unchanged source returns - `changedPages: []` and `recomputedPages: 0`; a single-paragraph edit goes - through `Engine::edit` and reports the changed/recomputed pages, while - structural edits fall back to a full layout. The v1 semantic pipeline remains + `changedPages: []` and `recomputedPages: 0`; one or more same-count paragraph + edits are applied sequentially through `Engine::edit` from the earliest + changed paragraph and report the changed/recomputed pages, while structural + edits fall back to a full layout. The v1 semantic pipeline remains the default fallback. LaTeX-to-route-D semantic mapping is still a subset, and `route_d_page_matches_pdftex_raster_geometry` lays out a 480pt CMR10 paragraph at the same scaled-point geometry the server uses, rasterizes the route-D PDF and a pdfTeX Letter page at 72 dpi, and asserts matching ink line count plus left/right/bottom edges. It skips on hosts without `pdflatex`/`pdftoppm`. Exact glyph-outline pixel equality is still open because the two paths use different font outlines. - `\middle` does not scale with its enclosing delimiter; math delimiters are diff --git a/src/route_d.rs b/src/route_d.rs index 1d935de..eb0b4bf 100755 --- a/src/route_d.rs +++ b/src/route_d.rs @@ -5,7 +5,7 @@ //! core. The v1 preview remains the default and the fallback. use std::cell::RefCell; -use std::collections::HashMap; +use std::collections::{BTreeSet, HashMap}; use std::fmt::Write as _; use std::sync::{Mutex, OnceLock}; @@ -40,9 +40,9 @@ fn latex_body(source: &str) -> &str { } /// Project-scoped route-D preview. Reuses the previous engine when the -/// paragraph list is unchanged and applies exactly one paragraph edit through -/// `Engine::edit`; multi-paragraph or structural edits fall back to a full -/// layout. The v1 project preview remains the default. +/// paragraph list is unchanged and applies each changed paragraph through +/// `Engine::edit`, starting from the earliest changed paragraph. Structural +/// edits fall back to a full layout. The v1 project preview remains the default. pub fn project_preview_json(project_id: &str, source: &str) -> Result { let paragraphs = paragraphs(latex_body(source))?; let texts: Vec = paragraphs.iter().map(|entry| entry.text.clone()).collect(); @@ -66,26 +66,32 @@ pub fn project_preview_json(project_id: &str, source: &str) -> Result + && session.paragraphs.iter().zip(&texts).any(|(a, b)| a != b) => { - let index = session + let changed_indices: Vec = session .paragraphs .iter() .zip(&texts) - .position(|(a, b)| a != b) - .expect("one changed paragraph"); - session - .engine - .edit(&document, index as u64, texts[index].clone()); - let changed = session.engine.changed_pages().to_vec(); - let recomputed = session.engine.recomputed_pages(); - (session.engine, changed, recomputed) + .enumerate() + .filter(|(_, (a, b))| a != b) + .map(|(index, _)| index) + .collect(); + let mut changed = BTreeSet::new(); + let mut recomputed = 0; + for (edit, &index) in changed_indices.iter().enumerate() { + session + .engine + .edit(&document, index as u64, texts[index].clone()); + if edit == 0 { + recomputed = session.engine.recomputed_pages(); + } + changed.extend(session.engine.changed_pages().iter().copied()); + } + ( + session.engine, + changed.into_iter().collect::>(), + recomputed, + ) } _ => { let engine = Engine::layout(&document); @@ -412,3 +418,48 @@ fn escape_json(value: &str) -> String { } escaped } + +#[cfg(test)] +mod tests { + use super::*; + + fn multi_page_source() -> String { + let mut source = String::from("\\documentclass{article}\n\\begin{document}\n\n"); + for index in 0..36 { + source.push_str(&format!("Paragraph {index:02} ")); + for _ in 0..4 { + source.push_str( + "lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor. ", + ); + } + source.push_str("\n\n"); + } + source.push_str("\\end{document}\n"); + source + } + + #[test] + fn multi_paragraph_edits_reuse_earlier_pages() { + let source = multi_page_source(); + let first = project_preview_json("route-d-multi-edit", &source).expect("first preview"); + let first: serde_json::Value = serde_json::from_str(&first).expect("first JSON"); + let page_count = first["pageCount"].as_u64().expect("page count"); + assert!( + page_count >= 2, + "expected a multi-page preview, got {page_count}" + ); + + let edited = source + .replace("Paragraph 30 ", "Paragraph 30 changed ") + .replace("Paragraph 33 ", "Paragraph 33 changed "); + let second = project_preview_json("route-d-multi-edit", &edited).expect("edited preview"); + let second: serde_json::Value = serde_json::from_str(&second).expect("edited JSON"); + assert!( + second["changedPages"] + .as_array() + .is_some_and(|pages| !pages.is_empty()) + ); + let recomputed = second["recomputedPages"].as_u64().expect("recomputed"); + assert!(recomputed < page_count, "changed pages should be a subset"); + } +}