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![