Honour @ column expressions in alignment templates

This commit is contained in:
gloamxun committed 2026-10-24 01:07:48 +08:00
1 parent c3e75bd244
commit 3681fd95db
4 files changed
+160 -8

No files matched your search

+155 -3
View File
@@ -457,6 +457,78 @@ fn take_booktabs_rules(raw: &str, row: usize, columns: usize) -> (String, Vec<Ha
(remaining.to_owned(), rules)
}
/// Glue overrides introduced by `@{}` / `@{\hspace{...}}` in a column template.
fn parse_at_glues(template: &str) -> Vec<Option<i64>> {
let mut columns = 0_usize;
let mut overrides: Vec<(usize, Option<i64>)> = Vec::new();
let mut characters = template.chars().peekable();
while let Some(character) = characters.next() {
match character {
'l' | 'L' | 'c' | 'C' | 'r' | 'R' => columns += 1,
'p' | 'P' => {
columns += 1;
if characters.peek() == Some(&'{') {
characters.next();
for inner in characters.by_ref() {
if inner == '}' {
break;
}
}
}
}
'@' => {
let mut value = Some(0_i64);
if characters.peek() == Some(&'{') {
characters.next();
let mut spec = String::new();
let mut depth = 0_usize;
for inner in characters.by_ref() {
match inner {
'{' => {
depth += 1;
spec.push('{');
}
'}' => {
if depth == 0 {
break;
}
depth -= 1;
spec.push('}');
}
other => spec.push(other),
}
}
let spec = spec.trim();
if !spec.is_empty() {
value = parse_at_glue(spec);
}
}
overrides.push((columns, value));
}
_ => {}
}
}
let mut glue = vec![None; columns + 1];
for (boundary, value) in overrides {
if let Some(slot) = glue.get_mut(boundary) {
*slot = value;
}
}
glue
}
/// Parse the body of an `@{\hspace{...}}` column expression.
fn parse_at_glue(spec: &str) -> Option<i64> {
let spec = spec.trim();
let rest = spec
.strip_prefix(r"\hspace")
.or_else(|| spec.strip_prefix(r"\hskip"))?;
let rest = rest.trim_start();
let rest = rest.strip_prefix('*').unwrap_or(rest).trim_start();
let body = rest.strip_prefix('{')?.split_once('}')?.0;
parse_tex_dimension(body)
}
/// Parse an alignment template into per-column alignment and wrap widths.
fn parse_column_template(template: &str) -> (Vec<CellAlignment>, Vec<Option<i64>>) {
let mut alignments = Vec::new();
@@ -492,7 +564,25 @@ fn parse_column_template(template: &str) -> (Vec<CellAlignment>, Vec<Option<i64>
alignments.push(CellAlignment::Left);
wraps.push(wrap);
}
'|' | '@' | ' ' => {}
'|' | ' ' => {}
'@' => {
if characters.peek() == Some(&'{') {
characters.next();
let mut depth = 0_usize;
for inner in characters.by_ref() {
match inner {
'{' => depth += 1,
'}' => {
if depth == 0 {
break;
}
depth -= 1;
}
_ => {}
}
}
}
}
_ => {
alignments.push(CellAlignment::Left);
wraps.push(None);
@@ -635,12 +725,24 @@ impl Unit {
column += span;
}
}
let span_gaps = |column: usize, span: usize| -> i64 {
if column_skips.is_empty() {
intercolumn * (span.saturating_sub(1) as i64)
} else {
column_skips
.iter()
.skip(column)
.take(span.saturating_sub(1))
.copied()
.sum()
}
};
for row in &rows {
let mut column = 0_usize;
for cell in &row.cells {
let span = cell.span.max(1).min(column_count.saturating_sub(column));
if span > 1 {
let gaps = intercolumn * (span.saturating_sub(1) as i64);
let gaps = span_gaps(column, span);
let available: i64 = column_widths[column..column + span]
.iter()
.copied()
@@ -727,6 +829,7 @@ impl Unit {
) -> Self {
let (alignments, wraps) = parse_column_template(template);
let column_count = alignments.len().max(1);
let at_glue = parse_at_glues(template);
let mut rows = Vec::new();
let mut rules: Vec<HalignRule> = Vec::new();
for raw_row in source.split(r"\\") {
@@ -791,8 +894,22 @@ impl Unit {
}
rows.push(HalignRow { cells, rule: false });
}
let mut column_skips = tabskips;
if at_glue.iter().any(|value| value.is_some()) {
let default_gap = intercolumn.saturating_mul(65_536);
let mut merged = vec![default_gap; column_count.saturating_sub(1)];
for (slot, width) in merged.iter_mut().zip(column_skips.iter()) {
*slot = *width;
}
for boundary in 1..column_count {
if let Some(Some(width)) = at_glue.get(boundary).copied() {
merged[boundary - 1] = width;
}
}
column_skips = merged;
}
let mut unit =
Self::halign_with_layout(id, rows, column_count, intercolumn, wraps, tabskips);
Self::halign_with_layout(id, rows, column_count, intercolumn, wraps, column_skips);
if !rules.is_empty() {
if let Some(VerticalItem::Halign(halign)) = unit.items.first_mut() {
halign.rules = rules;
@@ -6835,6 +6952,41 @@ b & 2 \\
);
}
#[test]
fn at_expressions_override_column_gaps() {
let unit = Unit::halign_from_source_with_template(1, "l@{}c", "aa & bb \\\\", 20);
let VerticalItem::Halign(halign) = &unit.items[0] else {
panic!("expected halign");
};
assert_eq!(halign.column_skips, vec![0]);
assert_eq!(halign.width, halign.column_widths.iter().sum::<i64>());
assert_eq!(halign.column_offset(1), halign.column_widths[0]);
let unit =
Unit::halign_from_source_with_template(1, r"l@{\hspace{5pt}}c", "aa & bb \\\\", 20);
let VerticalItem::Halign(halign) = &unit.items[0] else {
panic!("expected halign");
};
assert_eq!(halign.column_skips, vec![5 * 65_536]);
assert_eq!(
halign.column_offset(1),
halign.column_widths[0] + 5 * 65_536
);
}
#[test]
fn plain_templates_keep_uniform_gaps() {
let unit = Unit::halign_from_source_with_template(1, "lc", "aa & bb \\\\", 20);
let VerticalItem::Halign(halign) = &unit.items[0] else {
panic!("expected halign");
};
assert!(halign.column_skips.is_empty());
assert_eq!(
halign.column_offset(1),
halign.column_widths[0] + 20 * 65_536
);
}
#[test]
fn checkpoint_round_trips_through_serde() {
let engine = Engine::layout(&document());