From 2c4d968659253bd90fba506f8465fd0b7f19a0b0 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 21:45:34 +0800 Subject: [PATCH] Map sections and emphasis in the route-D preview --- README.md | 2 +- docs/TEX_CORE.md | 4 +-- src/route_d.rs | 79 +++++++++++++++++++++++++++++++++++++----------- tests/server.rs | 26 ++++++++++++++++ 4 files changed, 91 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index b8f3169..2d72d6d 100644 --- a/README.md +++ b/README.md @@ -564,7 +564,7 @@ paragraph builder, CMR10 metrics extracted from `cmr10.tfm`, and a matching pdfT 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`, with PDF export at `POST /api/route-d/pdf`. -It accepts blank-line-separated plain text, lays it out with the route-D page +It accepts blank-line-separated source with a small `\section`/`\textbf`/`\emph` subset, 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 a99391e..0cb696f 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/PDF/edit endpoints with page hashes; editor integration pending | +| M7 incremental integration | partial | route-D preview/PDF/edit endpoints with page hashes and basic section/emphasis mapping; 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 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. +The crate currently has 137 unit tests plus one route-D integration test. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 222 tests (137 route-D unit, 1 route-D integration, 71 v1 integration, 8 server, 4 v1 unit, 1 doctest). v1 remains the live server path. ### M0 incremental demo diff --git a/src/route_d.rs b/src/route_d.rs index 60740b2..96e9090 100755 --- a/src/route_d.rs +++ b/src/route_d.rs @@ -8,7 +8,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}; +use texrra_tex_core::{Document, Engine, ParagraphFont, ParagraphStyle, Unit, VerticalItem}; const SCALE: i64 = 65_536; const PAGE_HEIGHT_PT: i64 = 792; @@ -64,7 +64,7 @@ pub fn edit_json(source: &str, unit: usize) -> Result { (next.clone(), engine) } }; - engine.edit(&next, unit as u64, paragraphs[unit].to_string()); + engine.edit(&next, unit as u64, paragraphs[unit].text.clone()); let changed = engine.changed_pages().to_vec(); let recomputed = engine.recomputed_pages(); let json = render_json(&engine, Some((&changed, recomputed))); @@ -73,7 +73,14 @@ pub fn edit_json(source: &str, unit: usize) -> Result { }) } -fn paragraphs(source: &str) -> Result, String> { +#[derive(Clone)] +struct SourceParagraph { + text: String, + font: ParagraphFont, + line_height: i64, +} + +fn paragraphs(source: &str) -> Result, String> { let paragraphs: Vec<&str> = source .split("\n\n") .map(str::trim) @@ -82,26 +89,64 @@ fn paragraphs(source: &str) -> Result, String> { if paragraphs.is_empty() { return Err("route-D preview needs at least one paragraph".to_string()); } - Ok(paragraphs) + Ok(paragraphs + .into_iter() + .map(|paragraph| parse_paragraph(paragraph)) + .collect()) } -fn build_document(paragraphs: &[&str]) -> Document { - let style = ParagraphStyle { - line_width: 480 * SCALE, - left_skip: 0, - right_skip: 0, - par_indent: 0, - line_height: 12 * SCALE, - char_width: 10 * SCALE, - space_width: 4 * SCALE, - space_stretch: 6 * SCALE, - space_shrink: 2 * SCALE, - }; +fn command_body<'a>(text: &'a str, command: &str) -> Option<&'a str> { + let rest = text.strip_prefix(command)?.trim_start(); + let rest = rest.strip_prefix('{')?.strip_suffix('}')?; + Some(rest.trim()) +} + +/// Map a small, general LaTeX subset onto route-D paragraph faces. This is not +/// a full LaTeX reader; it exists so the live endpoint can show headings and +/// emphasis while the semantic layer is built out. +fn parse_paragraph(raw: &str) -> SourceParagraph { + let candidates = [ + ("\\section", ParagraphFont::Bold, 18), + ("\\textbf", ParagraphFont::Bold, 12), + ("\\emph", ParagraphFont::Italic, 12), + ]; + for (command, font, line_height) in candidates { + if let Some(text) = command_body(raw, command) { + return SourceParagraph { + text: text.to_string(), + font, + line_height, + }; + } + } + SourceParagraph { + text: raw.to_string(), + font: ParagraphFont::Roman, + line_height: 12, + } +} + +fn build_document(paragraphs: &[SourceParagraph]) -> Document { let units = paragraphs .iter() .enumerate() .map(|(index, paragraph)| { - Unit::paragraph_with_style(index as u64, (*paragraph).to_string(), style.clone()) + let style = ParagraphStyle { + line_width: 480 * SCALE, + left_skip: 0, + right_skip: 0, + par_indent: 0, + line_height: paragraph.line_height * SCALE, + char_width: 10 * SCALE, + space_width: 4 * SCALE, + space_stretch: 6 * SCALE, + space_shrink: 2 * SCALE, + }; + let mut unit = Unit::paragraph_with_style(index as u64, paragraph.text.clone(), style); + if let Some(VerticalItem::Paragraph(paragraph_box)) = unit.items.first_mut() { + paragraph_box.font = paragraph.font; + } + unit }) .collect(); Document::new(700 * SCALE, units) diff --git a/tests/server.rs b/tests/server.rs index 69ef985..0b07c9c 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -326,6 +326,32 @@ Second paragraph", assert!(text.contains("%%EOF")); } +#[test] +fn route_d_maps_sections_and_emphasis() { + let server = TestServer::start("route-d-markup"); + let response = server.request( + "POST", + "/api/route-d/preview", + b"\\section{Heading} + +\\textbf{Bold paragraph} + +\\emph{Italic paragraph}", + "text/plain", + ); + assert_eq!(response.status, 200, "{}", response.text()); + let value: Value = serde_json::from_slice(&response.body).expect("route-D JSON"); + let svg = value["pages"] + .as_array() + .unwrap() + .iter() + .map(|page| page["svg"].as_str().unwrap()) + .collect::(); + assert!(svg.contains("font-weight=\"bold\"")); + assert!(svg.contains("font-style=\"italic\"")); + assert!(svg.contains(">H")); +} + #[test] fn edits_route_d_preview_pages() { let server = TestServer::start("route-d-edit");