From ec9ce3b76503469844b08ba3a3c7bf2bd4d0c44f Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 21:41:46 +0800 Subject: [PATCH] Export route-D preview pages as PDF --- README.md | 2 +- docs/TEX_CORE.md | 8 ++++---- src/main.rs | 21 +++++++++++++++++++++ src/route_d.rs | 19 +++++++++++++++++++ tests/server.rs | 18 ++++++++++++++++++ 5 files changed, 63 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3b4e3ac..b8f3169 100644 --- a/README.md +++ b/README.md @@ -563,7 +563,7 @@ edit demo. M1 adds the horizontal box/glue/penalty model, an item-stream Knuth-P paragraph builder, CMR10 metrics extracted from `cmr10.tfm`, and a matching pdfTeX line-break fixture. The production v1 engine remains the default until the new core reaches layout parity. See `docs/TEX_CORE.md`. -The first M7 service integration is available as `POST /api/route-d/preview`. +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 plain text, lays it out with the route-D page builder, and returns the v1-compatible preview shape plus per-page hashes and SVG. The v1 `/preview` and project APIs are unchanged and remain the default. diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index a558fbc..a99391e 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -232,7 +232,7 @@ for a later overlaid slash. | M4 math | partial | atoms, scripts, fractions, roots, delimiters, accents, operators, classes, braces, boxed, negated; no math painter/shrink wrapping | | M5 NFSS/TFM | partial | runtime `cmr10.tfm` widths/heights/depths/kern pairs and ff/fi/fl/ffi/ffl ligatures; all five text faces embed Liberation Serif/Mono as Type0 CID fonts with Identity-H, ToUnicode and `/W` glyph widths; no Base-14 dependency; SVG carries family/weight/style | | M6 specials/XObject | partial | raw specials, RGB image XObjects, URI link annotations; class/package execution absent | -| M7 incremental integration | partial | route-D plain-text preview endpoint with page hashes; editor integration pending | +| M7 incremental integration | partial | route-D plain-text preview/PDF/edit endpoints with page hashes; editor integration pending | v1 remains the production renderer; the route-D core is additive and tested independently. @@ -262,7 +262,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 137 unit tests plus one route-D integration test. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 220 tests (137 route-D unit, 1 route-D integration, 71 v1 integration, 6 server, 4 v1 unit, 1 doctest). v1 remains the live server path. +The crate currently has 137 unit tests plus one route-D integration test. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 221 tests (137 route-D unit, 1 route-D integration, 71 v1 integration, 7 server, 4 v1 unit, 1 doctest). v1 remains the live server path. ### M0 incremental demo @@ -292,8 +292,8 @@ deliberately open rather than approximated: - **M5**: `cmr10.tfm` is parsed at runtime with widths, glyph heights/depths and kern pairs. all five text faces embed Liberation Serif/Mono as Type0/Identity-H with `/W` widths; ff/fi/fl/ffi/ffl ligatures come from TFM, and CMR10 italic corrections are applied at line ends; full NFSS family switching is not implemented. - **M6**: `\output`, `\insert`, mark/float output policies, class-file execution and math limit policies are only partially modeled; `OutputRoutine::wants_break` can force a page break between units, and insertion material is stacked by class from the page bottom; `\output` is still Rust code rather than interpreted tokens. -- **M7**: `POST /api/route-d/preview` and `/api/route-d/edit/{unit}` expose - route-D pages and edit diff; the preview path lays out +- **M7**: `POST /api/route-d/preview`, `/api/route-d/pdf` and + `/api/route-d/edit/{unit}` expose route-D pages, PDF export and edit diff; the preview path lays out blank-line-separated plain text and returns route-D pages with SVG and page hashes. It is not yet the editor preview: LaTeX-to-route-D semantic mapping and edit-diff reuse inside the project preview remain open. diff --git a/src/main.rs b/src/main.rs index 6010fbd..5fa3142 100644 --- a/src/main.rs +++ b/src/main.rs @@ -160,6 +160,27 @@ fn handle_request( Err(error) => respond(request, 422, "application/json; charset=utf-8", &error), } } + (&Method::Post, "/api/route-d/pdf") => { + let bytes = match read_body_bytes(&mut request, MAX_SOURCE_BYTES) { + Ok(bytes) => bytes, + Err(error) => return respond(request, 413, "text/plain; charset=utf-8", &error), + }; + let source = match String::from_utf8(bytes) { + Ok(source) => source, + Err(_) => { + return respond( + request, + 400, + "text/plain; charset=utf-8", + "source must be UTF-8", + ); + } + }; + match route_d::pdf_bytes(&source) { + Ok(pdf) => respond_pdf(request, pdf), + Err(error) => respond(request, 422, "application/json; charset=utf-8", &error), + } + } (&Method::Post, "/api/route-d/preview") => { let bytes = match read_body_bytes(&mut request, MAX_SOURCE_BYTES) { Ok(bytes) => bytes, diff --git a/src/route_d.rs b/src/route_d.rs index d983148..60740b2 100755 --- a/src/route_d.rs +++ b/src/route_d.rs @@ -7,6 +7,7 @@ use std::cell::RefCell; use std::fmt::Write as _; +use texrra_tex_core::pdf::display_lists_to_pdf; use texrra_tex_core::{Document, Engine, ParagraphStyle, Unit}; const SCALE: i64 = 65_536; @@ -25,6 +26,24 @@ pub fn preview_json(source: &str) -> Result { Ok(render_json(&engine, None)) } +/// Render the same checkpointed document as a route-D PDF. +pub fn pdf_bytes(source: &str) -> Result, String> { + let paragraphs = paragraphs(source)?; + let document = build_document(¶graphs); + let engine = Engine::layout(&document); + let pages: Vec<_> = engine + .pages() + .iter() + .map(|page| { + let mut list = page.display_list(); + list.width = list.width.max(PAGE_WIDTH_PT * SCALE); + list.height = list.height.max(PAGE_HEIGHT_PT * SCALE); + list + }) + .collect(); + Ok(display_lists_to_pdf(&pages, PAGE_HEIGHT_PT)) +} + /// Apply one paragraph edit to the last route-D document and report page /// checkpoints that were reused versus rebuilt. pub fn edit_json(source: &str, unit: usize) -> Result { diff --git a/tests/server.rs b/tests/server.rs index b243331..69ef985 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -308,6 +308,24 @@ Second paragraph", assert!(value["pages"][0]["hash"].as_str().unwrap().len() == 16); } +#[test] +fn exports_route_d_pdf() { + let server = TestServer::start("route-d-pdf"); + let response = server.request( + "POST", + "/api/route-d/pdf", + b"Hello route D + +Second paragraph", + "text/plain", + ); + assert_eq!(response.status, 200); + let text = String::from_utf8_lossy(&response.body); + assert!(text.starts_with("%PDF-")); + assert!(text.contains("/Count ")); + assert!(text.contains("%%EOF")); +} + #[test] fn edits_route_d_preview_pages() { let server = TestServer::start("route-d-edit");