Replay multiple paragraph edits in route-D previews
Apply same-count paragraph edits sequentially from the earliest changed paragraph, reuse the earlier page checkpoints, and report the union of changed pages. Adds a multi-page regression.
This commit is contained in:
1 parent
506904bbdc
commit
ceb31c4612
3 files changed
+74
-22
No files matched your search
@@ -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
|
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. 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
|
document and returns the same page list plus `changedPages` and
|
||||||
`recomputedPages`, exposing the checkpoint/page-hash diff from the core.
|
`recomputedPages`, exposing the checkpoint/page-hash diff from the core.
|
||||||
+5
-4
@@ -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.
|
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
|
### 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`;
|
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
|
`route_d::project_preview_json` strips the preamble/body wrapper and keeps a
|
||||||
project-scoped `(paragraphs, Engine)` session. An unchanged source returns
|
project-scoped `(paragraphs, Engine)` session. An unchanged source returns
|
||||||
`changedPages: []` and `recomputedPages: 0`; a single-paragraph edit goes
|
`changedPages: []` and `recomputedPages: 0`; one or more same-count paragraph
|
||||||
through `Engine::edit` and reports the changed/recomputed pages, while
|
edits are applied sequentially through `Engine::edit` from the earliest
|
||||||
structural edits fall back to a full layout. The v1 semantic pipeline remains
|
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
|
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.
|
`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
|
- `\middle` does not scale with its enclosing delimiter; math delimiters are
|
||||||
|
|||||||
+68
-17
@@ -5,7 +5,7 @@
|
|||||||
//! core. The v1 preview remains the default and the fallback.
|
//! core. The v1 preview remains the default and the fallback.
|
||||||
|
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::collections::HashMap;
|
use std::collections::{BTreeSet, HashMap};
|
||||||
use std::fmt::Write as _;
|
use std::fmt::Write as _;
|
||||||
use std::sync::{Mutex, OnceLock};
|
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
|
/// Project-scoped route-D preview. Reuses the previous engine when the
|
||||||
/// paragraph list is unchanged and applies exactly one paragraph edit through
|
/// paragraph list is unchanged and applies each changed paragraph through
|
||||||
/// `Engine::edit`; multi-paragraph or structural edits fall back to a full
|
/// `Engine::edit`, starting from the earliest changed paragraph. Structural
|
||||||
/// layout. The v1 project preview remains the default.
|
/// 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<String, String> {
|
pub fn project_preview_json(project_id: &str, source: &str) -> Result<String, String> {
|
||||||
let paragraphs = paragraphs(latex_body(source))?;
|
let paragraphs = paragraphs(latex_body(source))?;
|
||||||
let texts: Vec<String> = paragraphs.iter().map(|entry| entry.text.clone()).collect();
|
let texts: Vec<String> = paragraphs.iter().map(|entry| entry.text.clone()).collect();
|
||||||
@@ -66,26 +66,32 @@ pub fn project_preview_json(project_id: &str, source: &str) -> Result<String, St
|
|||||||
}
|
}
|
||||||
Some(mut session)
|
Some(mut session)
|
||||||
if session.paragraphs.len() == texts.len()
|
if session.paragraphs.len() == texts.len()
|
||||||
&& session
|
&& session.paragraphs.iter().zip(&texts).any(|(a, b)| a != b) =>
|
||||||
.paragraphs
|
|
||||||
.iter()
|
|
||||||
.zip(&texts)
|
|
||||||
.filter(|(a, b)| a != b)
|
|
||||||
.count()
|
|
||||||
== 1 =>
|
|
||||||
{
|
{
|
||||||
let index = session
|
let changed_indices: Vec<usize> = session
|
||||||
.paragraphs
|
.paragraphs
|
||||||
.iter()
|
.iter()
|
||||||
.zip(&texts)
|
.zip(&texts)
|
||||||
.position(|(a, b)| a != b)
|
.enumerate()
|
||||||
.expect("one changed paragraph");
|
.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
|
session
|
||||||
.engine
|
.engine
|
||||||
.edit(&document, index as u64, texts[index].clone());
|
.edit(&document, index as u64, texts[index].clone());
|
||||||
let changed = session.engine.changed_pages().to_vec();
|
if edit == 0 {
|
||||||
let recomputed = session.engine.recomputed_pages();
|
recomputed = session.engine.recomputed_pages();
|
||||||
(session.engine, changed, recomputed)
|
}
|
||||||
|
changed.extend(session.engine.changed_pages().iter().copied());
|
||||||
|
}
|
||||||
|
(
|
||||||
|
session.engine,
|
||||||
|
changed.into_iter().collect::<Vec<_>>(),
|
||||||
|
recomputed,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
let engine = Engine::layout(&document);
|
let engine = Engine::layout(&document);
|
||||||
@@ -412,3 +418,48 @@ fn escape_json(value: &str) -> String {
|
|||||||
}
|
}
|
||||||
escaped
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user