Parse array column modifiers and their alignment

This commit is contained in:
gloamxun committed 2026-10-26 20:31:33 +08:00
1 parent 71dc8ca59b
commit f147ccc8c5
4 files changed
+76 -6

No files matched your search

+62 -4
View File
@@ -877,18 +877,19 @@ fn parse_column_template(template: &str) -> (Vec<CellAlignment>, Vec<Option<i64>
let mut wraps = Vec::new();
let mut vertical_rules = Vec::new();
let mut characters = template.chars().peekable();
let mut pending_alignment: Option<CellAlignment> = 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<CellAlignment>, Vec<Option<i64>
}
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![
+1 -1
View File
@@ -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.
+1 -1
View File
@@ -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
+12
View File
@@ -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 =