Render display math in the route-D preview

This commit is contained in:
gloamxun committed 2026-10-20 21:57:12 +08:00
1 parent 15880c884d
commit 63593e7ff3
4 files changed
+48 -3

No files matched your search

+1 -1
View File
@@ -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 source with a small `\section`/`\textbf`/`\emph` subset, lays it out with the route-D page
It accepts blank-line-separated source with a small `\section`/`\textbf`/`\emph`/display-math 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.
+2 -2
View File
@@ -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 preview/PDF/edit endpoints with content-based page hashes and basic section/emphasis mapping; editor integration pending |
| M7 incremental integration | partial | route-D preview/PDF/edit endpoints with content-based page hashes and basic section/emphasis/display-math 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 138 unit tests plus one route-D integration test. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 224 tests (138 route-D unit, 1 route-D integration, 71 v1 integration, 9 server, 4 v1 unit, 1 doctest). v1 remains the live server path.
The crate currently has 138 unit tests plus one route-D integration test. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 225 tests (138 route-D unit, 1 route-D integration, 71 v1 integration, 10 server, 4 v1 unit, 1 doctest). v1 remains the live server path.
### M0 incremental demo
+27
View File
@@ -7,6 +7,8 @@
use std::cell::RefCell;
use std::fmt::Write as _;
use texrra_tex_core::cmr10::Cmr10Metrics;
use texrra_tex_core::math::parse_math;
use texrra_tex_core::pdf::display_lists_to_pdf;
use texrra_tex_core::{Document, Engine, ParagraphFont, ParagraphStyle, Unit, VerticalItem};
@@ -78,6 +80,7 @@ struct SourceParagraph {
text: String,
font: ParagraphFont,
line_height: i64,
math: bool,
}
fn paragraphs(source: &str) -> Result<Vec<SourceParagraph>, String> {
@@ -105,6 +108,21 @@ fn command_body<'a>(text: &'a str, command: &str) -> Option<&'a str> {
/// 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 {
if let Some(math) = raw
.strip_prefix("$$")
.and_then(|text| text.strip_suffix("$$"))
.or_else(|| {
raw.strip_prefix("\\[")
.and_then(|text| text.strip_suffix("\\]"))
})
{
return SourceParagraph {
text: math.trim().to_string(),
font: ParagraphFont::Roman,
line_height: 12,
math: true,
};
}
let candidates = [
("\\section", ParagraphFont::Bold, 18),
("\\textbf", ParagraphFont::Bold, 12),
@@ -116,6 +134,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
text: text.to_string(),
font,
line_height,
math: false,
};
}
}
@@ -123,6 +142,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
text: raw.to_string(),
font: ParagraphFont::Roman,
line_height: 12,
math: false,
}
}
@@ -131,6 +151,13 @@ fn build_document(paragraphs: &[SourceParagraph]) -> Document {
.iter()
.enumerate()
.map(|(index, paragraph)| {
if paragraph.math {
return Unit::math(
index as u64,
parse_math(&paragraph.text),
&Cmr10Metrics::default(),
);
}
let style = ParagraphStyle {
line_width: 480 * SCALE,
left_skip: 0,
+18
View File
@@ -406,6 +406,24 @@ fn route_d_edit_keeps_unaffected_page_hashes() {
assert_ne!(hashes[1], edited[1]);
}
#[test]
fn route_d_renders_display_math() {
let server = TestServer::start("route-d-math");
let response = server.request(
"POST",
"/api/route-d/preview",
b"$$x^2 + \\frac{a}{b}$$",
"text/plain",
);
assert_eq!(response.status, 200, "{}", response.text());
let value: Value = serde_json::from_slice(&response.body).expect("route-D JSON");
assert_eq!(value["status"], "ok");
let svg = value["pages"][0]["svg"].as_str().unwrap();
assert!(svg.contains(">x</text>"));
assert!(svg.contains(">a</text>"));
assert!(svg.contains(">b</text>"));
}
#[test]
fn edits_route_d_preview_pages() {
let server = TestServer::start("route-d-edit");