Paint vertical rules from table column separators

This commit is contained in:
gloamxun committed 2026-10-25 02:29:23 +08:00
1 parent 8bd725e9ff
commit 6d0794ef3a
6 files changed
+74 -11

No files matched your search

+48 -4
View File
@@ -157,6 +157,9 @@ pub struct Halign {
/// Extra vertical glue after each row (TeX `\noalign{\vskip ...}`).
#[serde(default)]
pub row_gaps: Vec<i64>,
/// Column boundaries (`|` in the template) that paint a vertical rule.
#[serde(default)]
pub vertical_rules: Vec<usize>,
/// Leading rows repeated on the first page only (longtable `\endfirsthead`).
#[serde(default)]
pub first_head_len: usize,
@@ -321,6 +324,7 @@ impl Halign {
.iter()
.map(|&index| self.row_gap(index))
.collect(),
vertical_rules: self.vertical_rules.clone(),
first_head_len: 0,
head_len: 0,
foot_len: 0,
@@ -358,6 +362,7 @@ impl Halign {
.get(start..index)
.map(<[_]>::to_vec)
.unwrap_or_default(),
vertical_rules: self.vertical_rules.clone(),
first_head_len: 0,
head_len: 0,
foot_len: 0,
@@ -381,6 +386,7 @@ impl Halign {
.get(start..)
.map(<[_]>::to_vec)
.unwrap_or_default(),
vertical_rules: self.vertical_rules.clone(),
first_head_len: 0,
head_len: 0,
foot_len: 0,
@@ -810,9 +816,10 @@ fn parse_at_glue(spec: &str) -> Option<i64> {
}
/// Parse an alignment template into per-column alignment and wrap widths.
fn parse_column_template(template: &str) -> (Vec<CellAlignment>, Vec<Option<i64>>) {
fn parse_column_template(template: &str) -> (Vec<CellAlignment>, Vec<Option<i64>>, Vec<usize>) {
let mut alignments = Vec::new();
let mut wraps = Vec::new();
let mut vertical_rules = Vec::new();
let mut characters = template.chars().peekable();
while let Some(character) = characters.next() {
match character {
@@ -844,7 +851,13 @@ fn parse_column_template(template: &str) -> (Vec<CellAlignment>, Vec<Option<i64>
alignments.push(CellAlignment::Left);
wraps.push(wrap);
}
'|' | ' ' => {}
'|' => {
let boundary = alignments.len();
if vertical_rules.last() != Some(&boundary) {
vertical_rules.push(boundary);
}
}
' ' => {}
'@' => {
if characters.peek() == Some(&'{') {
characters.next();
@@ -873,7 +886,7 @@ fn parse_column_template(template: &str) -> (Vec<CellAlignment>, Vec<Option<i64>
alignments.push(CellAlignment::Left);
wraps.push(None);
}
(alignments, wraps)
(alignments, wraps, vertical_rules)
}
/// Vertical material reserved at the bottom of a page (footnotes etc.).
@@ -1064,6 +1077,7 @@ impl Unit {
column_skips,
rules: Vec::new(),
row_gaps: Vec::new(),
vertical_rules: Vec::new(),
first_head_len: 0,
head_len: 0,
foot_len: 0,
@@ -1123,7 +1137,7 @@ impl Unit {
intercolumn: i64,
tabskips: Vec<i64>,
) -> Self {
let (alignments, wraps) = parse_column_template(template);
let (alignments, wraps, vertical_rules) = parse_column_template(template);
let column_count = alignments.len().max(1);
let at_glue = parse_at_glues(template);
let mut rows = Vec::new();
@@ -1285,6 +1299,7 @@ impl Unit {
Self::halign_with_layout(id, rows, column_count, intercolumn, wraps, column_skips);
if let Some(VerticalItem::Halign(halign)) = unit.items.first_mut() {
halign.rules = rules;
halign.vertical_rules = vertical_rules;
halign.first_head_len = first_len.min(halign.rows.len());
halign.head_len = head_len.min(halign.rows.len() - halign.first_head_len);
halign.foot_len =
@@ -8006,6 +8021,35 @@ b & 2 \\
);
}
#[test]
fn vertical_rules_paint_at_column_boundaries() {
let unit = Unit::halign_from_source_with_template(1, "|l|c|", "a & b", 6);
let VerticalItem::Halign(halign) = &unit.items[0] else {
panic!("expected halign");
};
assert_eq!(halign.vertical_rules, vec![0, 1, 2]);
let list = halign.display_list();
let rules: Vec<(i64, i64, i64)> = list
.items
.iter()
.filter_map(|item| match item {
DisplayItem::Rule {
x, width, height, ..
} => Some((*x, *width, *height)),
_ => None,
})
.collect();
assert_eq!(rules.len(), 3);
assert_eq!(rules[0].0, 0);
assert_eq!(rules[2].0, halign.width - 26_214);
assert!(rules[1].0 > rules[0].0 && rules[1].0 < rules[2].0);
assert!(
rules
.iter()
.all(|(_, width, height)| *width == 26_214 && *height == halign.height)
);
}
#[test]
fn row_separator_optional_gap_spaces_rows() {
let source = "a & b \\\\[6pt]\nc & d";