From f147ccc8c5f9192db7ffc35f746d65beb05368b4 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Mon, 26 Oct 2026 20:31:33 +0800 Subject: [PATCH] Parse array column modifiers and their alignment --- crates/texrra-tex-core/src/lib.rs | 66 +++++++++++++++++++++++++++++-- docs/ROUTE_D_PLAN.md | 2 +- docs/TEX_CORE.md | 2 +- src/route_d.rs | 12 ++++++ 4 files changed, 76 insertions(+), 6 deletions(-) diff --git a/crates/texrra-tex-core/src/lib.rs b/crates/texrra-tex-core/src/lib.rs index b5c6201..518aa26 100644 --- a/crates/texrra-tex-core/src/lib.rs +++ b/crates/texrra-tex-core/src/lib.rs @@ -877,18 +877,19 @@ fn parse_column_template(template: &str) -> (Vec, Vec let mut wraps = Vec::new(); let mut vertical_rules = Vec::new(); let mut characters = template.chars().peekable(); + let mut pending_alignment: Option = None; while let Some(character) = characters.next() { match character { 'l' | 'L' => { - alignments.push(CellAlignment::Left); + alignments.push(pending_alignment.take().unwrap_or(CellAlignment::Left)); wraps.push(None); } 'c' | 'C' => { - alignments.push(CellAlignment::Center); + alignments.push(pending_alignment.take().unwrap_or(CellAlignment::Center)); wraps.push(None); } 'r' | 'R' => { - alignments.push(CellAlignment::Right); + alignments.push(pending_alignment.take().unwrap_or(CellAlignment::Right)); wraps.push(None); } 'p' | 'P' => { @@ -904,9 +905,41 @@ fn parse_column_template(template: &str) -> (Vec, Vec } wrap = parse_tex_dimension(&spec); } - alignments.push(CellAlignment::Left); + alignments.push(pending_alignment.take().unwrap_or(CellAlignment::Left)); wraps.push(wrap); } + '>' | '<' | '!' => { + if characters.peek() == Some(&'{') { + characters.next(); + let mut declaration = String::new(); + let mut depth = 0_usize; + for inner in characters.by_ref() { + match inner { + '{' => { + depth += 1; + declaration.push(inner); + } + '}' => { + if depth == 0 { + break; + } + depth -= 1; + declaration.push(inner); + } + _ => declaration.push(inner), + } + } + if character == '>' { + if declaration.contains("raggedleft") { + pending_alignment = Some(CellAlignment::Right); + } else if declaration.contains("centering") { + pending_alignment = Some(CellAlignment::Center); + } else if declaration.contains("raggedright") { + pending_alignment = Some(CellAlignment::Left); + } + } + } + } '|' => { let boundary = alignments.len(); if vertical_rules.last() != Some(&boundary) { @@ -6247,6 +6280,31 @@ mod tests { assert!((both - middle).abs() <= halign.line_height / 2); } + #[test] + fn array_column_modifiers_set_alignment() { + let rows = vec![HalignRow { + cells: vec![HalignCell { + text: "alpha beta".into(), + span: 1, + alignment: CellAlignment::Left, + }], + rule: false, + }]; + let unit = Unit::halign_with_layout(0, rows, 1, 10, vec![Some(40 * 65_536)], Vec::new()); + let VerticalItem::Halign(halign) = &unit.items[0] else { + panic!("expected halign"); + }; + assert_eq!(halign.column_widths.len(), 1); + let parsed = + Unit::halign_from_source_with_template(0, ">{\\centering}p{20mm}", "alpha beta", 10); + let VerticalItem::Halign(parsed) = &parsed.items[0] else { + panic!("expected parsed halign"); + }; + assert_eq!(parsed.column_widths.len(), 1); + assert_eq!(parsed.rows[0].cells[0].alignment, CellAlignment::Center); + assert!(!parsed.cell_lines(0, 0).is_empty()); + } + #[test] fn halign_aligns_cells_inside_their_columns() { let rows = vec![ diff --git a/docs/ROUTE_D_PLAN.md b/docs/ROUTE_D_PLAN.md index 94add29..cd68e84 100644 --- a/docs/ROUTE_D_PLAN.md +++ b/docs/ROUTE_D_PLAN.md @@ -48,7 +48,7 @@ floats (`\floatsep`, `\textfloatsep`, `\intextsep`, `\topfraction`, `\bottomnumber`, `\totalnumber`), insertion classes (`\skip`, fraction and 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`). 932 workspace tests are green and no oracle +PDF opt-in with `?engine=route-d`). 934 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 23d99cf..deaf6fd 100755 --- a/docs/TEX_CORE.md +++ b/docs/TEX_CORE.md @@ -367,7 +367,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 427 unit tests plus ten route-D integration tests and thirty-eight oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 932 tests (429 route-D unit, 318 engine binary unit, 10 route-D integration, 39 oracle, 71 v1 integration, 60 server, 4 engine unit, 1 core unit), with none ignored. v1 remains the live server path. +The crate currently has 427 unit tests plus ten route-D integration tests and thirty-eight oracle fixtures. Workspace verification: `cargo fmt --all -- --check`; `cargo test --workspace --offline` runs 934 tests (430 route-D unit, 319 engine binary unit, 10 route-D integration, 39 oracle, 71 v1 integration, 60 server, 4 engine unit, 1 core unit), with none ignored. v1 remains the live server path. ### M0 incremental demo diff --git a/src/route_d.rs b/src/route_d.rs index 4e28f86..cd00c9b 100755 --- a/src/route_d.rs +++ b/src/route_d.rs @@ -11936,6 +11936,18 @@ See \ref{fig:a} and \ref{tab:b}."; assert!(!json.contains("multirow")); } + #[test] + fn array_column_modifiers_stay_out_of_the_text() { + let source = + "\\begin{tabular}{>{\\centering}p{20mm}l}\nalpha beta & right \\\\\n\\end{tabular}"; + let (body, geometry) = geometry_and_body(source); + assert_eq!(geometry.tabular.len(), 1); + let json = preview_json(source).expect("preview"); + assert!(json.contains("\"warnings\":[]"), "warnings: {json}"); + assert!(!json.contains("centering")); + assert!(!body.contains("centering")); + } + #[test] fn tabularx_fills_its_target_width() { let source =