From 472f778e7547de63a79eb5721296d4d595c250ba Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 22:12:39 +0800 Subject: [PATCH] Center route-D paragraphs and preview center blocks --- crates/texrra-tex-core/src/paragraph.rs | 8 ++++++++ crates/texrra-tex-core/src/render.rs | 15 ++++++++++++++- docs/TEX_CORE.md | 4 ++-- src/route_d.rs | 8 ++++++++ tests/server.rs | 24 ++++++++++++++++++++++++ 5 files changed, 56 insertions(+), 3 deletions(-) diff --git a/crates/texrra-tex-core/src/paragraph.rs b/crates/texrra-tex-core/src/paragraph.rs index 961c86a..f10c92f 100755 --- a/crates/texrra-tex-core/src/paragraph.rs +++ b/crates/texrra-tex-core/src/paragraph.rs @@ -42,6 +42,9 @@ pub struct ParagraphStyle { pub left_skip: i64, /// Right skip reserved at the end of every line (`\rightskip`). pub right_skip: i64, + /// Center each line in the measure instead of justifying it. + #[serde(default)] + pub centered: bool, pub par_indent: i64, pub line_height: i64, pub char_width: i64, @@ -56,6 +59,7 @@ impl Default for ParagraphStyle { line_width: 400, left_skip: 0, right_skip: 0, + centered: false, par_indent: 0, line_height: 12, char_width: 10, @@ -91,6 +95,8 @@ pub struct Paragraph { pub font: ParagraphFont, #[serde(default)] pub left_skip: i64, + #[serde(default)] + pub centered: bool, } impl Paragraph { @@ -150,6 +156,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra width: 0, font: ParagraphFont::Roman, left_skip: style.left_skip, + centered: style.centered, }; } let mut breakpoints = vec![0_usize]; @@ -326,6 +333,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra lines, font: ParagraphFont::Roman, left_skip: style.left_skip, + centered: style.centered, } } diff --git a/crates/texrra-tex-core/src/render.rs b/crates/texrra-tex-core/src/render.rs index bcf0abb..517fd6f 100755 --- a/crates/texrra-tex-core/src/render.rs +++ b/crates/texrra-tex-core/src/render.rs @@ -378,7 +378,20 @@ impl Page { let size = ((line_height as f64) * 0.7) as i64; for line in ¶graph.lines { let baseline = y + ((line_height as f64) * 0.8) as i64; - let mut x = paragraph.left_skip; + let natural: i64 = line + .iter() + .map(|item| match item { + HorizontalItem::Box(box_) => box_.width, + HorizontalItem::Glue { width, .. } => *width, + HorizontalItem::Penalty { .. } + | HorizontalItem::Discretionary { .. } => 0, + }) + .sum(); + let mut x = if paragraph.centered { + paragraph.left_skip + ((paragraph.width - natural) / 2).max(0) + } else { + paragraph.left_skip + }; for item in line { match item { HorizontalItem::Box(box_) => { diff --git a/docs/TEX_CORE.md b/docs/TEX_CORE.md index 3b480cf..34f4515 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -234,7 +234,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 section/emphasis/display-math/itemize/enumerate/description/quote mapping; editor integration and inline math pending | +| M7 incremental integration | partial | route-D preview/PDF/edit endpoints with content-based page hashes and section/emphasis/display-math/itemize/enumerate/description/quote/center mapping; editor integration and inline math pending | v1 remains the production renderer; the route-D core is additive and tested independently. @@ -264,7 +264,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 231 tests (138 route-D unit, 1 route-D integration, 71 v1 integration, 16 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 232 tests (138 route-D unit, 1 route-D integration, 71 v1 integration, 17 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 f9c6700..9abd35e 100755 --- a/src/route_d.rs +++ b/src/route_d.rs @@ -78,6 +78,7 @@ pub fn edit_json(source: &str, unit: usize) -> Result { #[derive(Clone)] struct SourceParagraph { text: String, + centered: bool, font: ParagraphFont, line_height: i64, math: bool, @@ -130,6 +131,7 @@ fn parse_blocks(raw: &str) -> Vec { line_height: 12, math: false, left_skip: 12, + centered: false, } }) .collect(); @@ -149,6 +151,7 @@ fn parse_blocks(raw: &str) -> Vec { line_height: 12, math: false, left_skip: 12, + centered: false, }]; } } @@ -177,6 +180,7 @@ fn parse_blocks(raw: &str) -> Vec { line_height: 12, math: false, left_skip: 12, + centered: false, } }) .collect(); @@ -205,6 +209,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph { line_height: 12, math: true, left_skip: 0, + centered: false, }; } let candidates = [ @@ -220,6 +225,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph { line_height, math: false, left_skip: 0, + centered: false, }; } } @@ -229,6 +235,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph { line_height: 12, math: false, left_skip: 0, + centered: false, } } @@ -247,6 +254,7 @@ fn build_document(paragraphs: &[SourceParagraph]) -> Document { let style = ParagraphStyle { line_width: 480 * SCALE, left_skip: paragraph.left_skip * SCALE, + centered: paragraph.centered, right_skip: 0, par_indent: 0, line_height: paragraph.line_height * SCALE, diff --git a/tests/server.rs b/tests/server.rs index dd33b31..4fd5363 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -530,6 +530,30 @@ fn edits_route_d_list_units() { assert!(changed.contains(&0), "{changed:?}"); } +#[test] +fn route_d_renders_center_blocks() { + let server = TestServer::start("route-d-center"); + let response = server.request( + "POST", + "/api/route-d/preview", + b"\\begin{center}Centered\\end{center}", + "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"][0]["svg"].as_str().unwrap(); + let index = svg.find(">C").expect("centered C"); + let prefix = &svg[..index]; + let start = prefix.rfind(" 50.0, "{x}"); +} + #[test] fn edits_route_d_preview_pages() { let server = TestServer::start("route-d-edit");