From b3fbbc8dda246d42a0defdc2e5ab8d9b1d218530 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 27 Oct 2026 01:34:07 +0800 Subject: [PATCH] Render inline math in sized paragraphs Inline math returned None whenever the paragraph base size was not 10pt, so a 12pt or \large paragraph printed its dollar delimiters literally. Keep the math segment at every size: the box width still scales with the paragraph, while the math glyphs are pinned to the 10pt metric baseline by setting the box's size field, so paint positions and glyph sizes stay consistent (slightly smaller than the surrounding text, which is documented). The old sized-paragraph test now expects a math box and no literal dollars; a new 12pt preview test checks italic math glyphs and no "$" with no warnings. --- crates/texrra-tex-core/src/paragraph.rs | 2 +- docs/ROUTE_D_PLAN.md | 2 +- docs/TEX_CORE.md | 9 +++--- src/route_d.rs | 37 ++++++++++++++++++------- 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/crates/texrra-tex-core/src/paragraph.rs b/crates/texrra-tex-core/src/paragraph.rs index 572803d..5ce607b 100755 --- a/crates/texrra-tex-core/src/paragraph.rs +++ b/crates/texrra-tex-core/src/paragraph.rs @@ -2055,7 +2055,7 @@ pub fn layout_paragraph_segments(segments: &[InlineSegment], style: &ParagraphSt italic: 0, font: None, math: Some(Box::new(atom.clone())), - size: None, + size: Some(655_360), raise: 0, color: None, background: None, diff --git a/docs/ROUTE_D_PLAN.md b/docs/ROUTE_D_PLAN.md index 0219df8..9896445 100644 --- a/docs/ROUTE_D_PLAN.md +++ b/docs/ROUTE_D_PLAN.md @@ -49,7 +49,7 @@ floats (`\floatsep`, `\textfloatsep`, `\intextsep`, `\topfraction`, absolute limits, splitting), marks, Rust output hook, multi-page PDF, and the route-D service endpoints (`/api/route-d/preview|pdf|edit`, project preview and PDF opt-in with `?engine=route-d`; flat `/preview` and project previews now -prefer route-D with a v1 fallback). 1011 workspace tests are green and no oracle +prefer route-D with a v1 fallback). 1012 workspace tests are green and no oracle fixture is ignored. Preview JSON also carries a `warnings` array naming environments route-D cannot lay out and control words that survived layout, so unsupported source is reported instead of being silently typeset. diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index 75263fc..c0640ab 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -370,7 +370,7 @@ Math coverage includes fractions/roots, scripts, `\left...\right` with body-scal Paragraph units carry a `ParagraphFont`; `Unit::paragraph_with_face` and `Unit::paragraph_with_style_and_face` select Roman, bold, italic, mono, or sans. The corresponding Computer Modern TFM (`cmr10`, `cmbx10`, `cmti10`, `cmbxti10`, `cmtt10`, `cmss10`, `cmssbx10`, `cmssi10`, `cmcsc10`) supplies advances, kerns, ligatures, and italic correction for layout, the display list selects the matching face, and the PDF writes Computer Modern Type1 outlines for each built-in face (`cmr10`/`cmbx10`/`cmti10`/`cmbxti10`/`cmtt10`/`cmss10`/`cmssbx10`/`cmssi10`/`cmcsc10`, plus `cmmi10` for mathematical italic letters, `cmsy10` for math symbols and `cmex10` for big operators), falling back to Liberation subsets only for characters the Type1 encodings cannot address; those fallback glyphs keep their Unicode text under PDF extraction, so accented and Greek words round-trip; Type1 faces carry a ToUnicode CMap built from the requested characters, so glyphs whose names are ambiguous in the Adobe Glyph List (capital Omega, which would otherwise extract as U+2126) keep their intended code points. `Engine::edit` preserves the face when reflowing an edited paragraph. -The crate currently has 437 unit tests plus ten route-D integration tests and forty-two oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 1011 tests (437 route-D unit, 384 engine binary unit, 10 route-D integration, 42 oracle, 71 v1 integration, 62 server, 4 engine unit, 1 core unit), with none ignored. The flat `/preview` endpoint and the project preview/PDF endpoints now try route-D first and fall back to v1 on error (`?engine=v1` or `TEXRRA_ENGINE=v1` forces v1; the flat response carries `X-Preview-Engine`, naming the engine used). A geometry-less project now defaults to Letter, matching pdfLaTeX, where the old v1 path used A4. +The crate currently has 437 unit tests plus ten route-D integration tests and forty-two oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 1012 tests (437 route-D unit, 385 engine binary unit, 10 route-D integration, 42 oracle, 71 v1 integration, 62 server, 4 engine unit, 1 core unit), with none ignored. The flat `/preview` endpoint and the project preview/PDF endpoints now try route-D first and fall back to v1 on error (`?engine=v1` or `TEXRRA_ENGINE=v1` forces v1; the flat response carries `X-Preview-Engine`, naming the engine used). A geometry-less project now defaults to Letter, matching pdfLaTeX, where the old v1 path used A4. ### M0 incremental demo @@ -520,7 +520,7 @@ deliberately open rather than approximated: ## Limits and remaining gaps -The suite currently stands at 1011 passing tests, with the workspace clean. +The suite currently stands at 1012 passing tests, with the workspace clean. Route-D serves both flat `/preview` and project preview/PDF requests by default (automatic v1 fallback; `?engine=v1` or `TEXRRA_ENGINE=v1` opts out). The objective's areas are implemented to the limits below; complex-script shaping is @@ -603,5 +603,6 @@ are: Devanagari, Thai and emoji have no shaping fallback and render as missing glyphs; arbitrary external font families remain open. - **Inline runs**: colour/link/sized runs split at any base size; inline math - still falls back to literal delimiters when the paragraph's base size is not - 10pt, because math metrics are measured at 10pt. + also renders at any base size, with its glyphs pinned to the 10pt metric size + inside a larger paragraph (the box width scales with the paragraph), so 12pt + documents no longer print literal `$` delimiters. diff --git a/src/route_d.rs b/src/route_d.rs index c64f3fa..f41b0b0 100755 --- a/src/route_d.rs +++ b/src/route_d.rs @@ -10168,13 +10168,8 @@ fn inline_math_segments( } } } - // Math metrics are laid out at 10pt, so a paragraph that mixes math with - // another base size keeps its literal delimiters instead of painting the - // formula at the wrong size. Colour, link and sized runs are safe at any - // base size. - if found_math && style.font_size != 655_360 { - return None; - } + // Math metrics are laid out at 10pt; its glyphs stay at that size inside a + // larger paragraph (the box width still scales with the paragraph). (found_math || found_link || found_size || found_color).then_some(segments) } @@ -16768,6 +16763,22 @@ left & right \\ ); } + #[test] + fn inline_math_renders_at_twelve_point() { + let source = "\\documentclass[12pt]{article}\n\\begin{document}\n\nInline $x^2$ math.\n\n\\end{document}\n"; + let json = preview_json(source).expect("preview"); + let value: serde_json::Value = serde_json::from_str(&json).expect("JSON"); + assert_eq!( + value["warnings"].as_array().map(Vec::len), + Some(0), + "{}", + value["warnings"] + ); + let svg = value["pages"][0]["svg"].as_str().expect("svg"); + assert!(svg.contains("font-style=\"italic\""), "{svg}"); + assert!(!svg.contains('$'), "{svg}"); + } + #[test] fn inline_colours_and_links_work_at_twelve_point() { let source = "\\documentclass[12pt]{article}\n\\begin{document}\n\nA \\textcolor{red}{red word} and a \\href{https://example.com}{link} and x\\textsuperscript{2}.\n\n\\end{document}\n"; @@ -17781,7 +17792,7 @@ left & right \\ } #[test] - fn inline_math_stays_literal_in_sized_paragraphs() { + fn inline_math_renders_in_sized_paragraphs() { let blocks = paragraphs("\\large Value $x$ tail.").expect("paragraphs"); let document = build_document(&blocks, &PageGeometry::default()); let paragraph = match &document.units[0].items[0] { @@ -17797,8 +17808,14 @@ left & right \\ _ => None, }) .collect(); - assert!(boxes.iter().all(|box_| box_.math.is_none())); - assert!(boxes.iter().any(|box_| box_.character == Some('$'))); + assert!( + boxes.iter().any(|box_| box_.math.is_some()), + "math box missing" + ); + assert!( + !boxes.iter().any(|box_| box_.character == Some('$')), + "literal dollar left" + ); } #[test]