Insert noalign glue between alignment rows

This commit is contained in:
gloamxun committed 2026-10-24 01:10:49 +08:00
1 parent 3681fd95db
commit 8c3e410ffc
5 files changed
+105 -13

No files matched your search

+98 -6
View File
@@ -84,6 +84,9 @@ pub struct Halign {
/// Painted rules (booktabs heavy/light rules and partial `\cmidrule`s).
#[serde(default)]
pub rules: Vec<HalignRule>,
/// Extra vertical glue after each row (TeX `\noalign{\vskip ...}`).
#[serde(default)]
pub row_gaps: Vec<i64>,
pub width: i64,
pub height: i64,
pub line_height: i64,
@@ -127,7 +130,7 @@ impl Halign {
let mut start = 0_usize;
let mut height = 0_i64;
for index in 0..self.rows.len() {
let row_height = self.row_height(index);
let row_height = self.row_height(index) + self.row_gap(index);
if index > start && height + row_height > max_height {
chunks.push(Self {
rows: self.rows[start..index].to_vec(),
@@ -135,6 +138,11 @@ impl Halign {
column_wrap: self.column_wrap.clone(),
column_skips: self.column_skips.clone(),
rules: self.rules_for_chunk(start, index),
row_gaps: self
.row_gaps
.get(start..index)
.map(<[_]>::to_vec)
.unwrap_or_default(),
width: self.width,
height,
line_height: self.line_height,
@@ -150,6 +158,11 @@ impl Halign {
column_wrap: self.column_wrap.clone(),
column_skips: self.column_skips.clone(),
rules: self.rules_for_chunk(start, self.rows.len()),
row_gaps: self
.row_gaps
.get(start..)
.map(<[_]>::to_vec)
.unwrap_or_default(),
width: self.width,
height,
line_height: self.line_height,
@@ -229,6 +242,11 @@ impl Halign {
}
/// Height of one row, counting wrapped cell lines.
/// Extra vertical glue after `row_index` (TeX `\noalign{\vskip ...}`).
pub fn row_gap(&self, index: usize) -> i64 {
self.row_gaps.get(index).copied().unwrap_or(0)
}
pub fn row_height(&self, row_index: usize) -> i64 {
let row = match self.rows.get(row_index) {
Some(row) => row,
@@ -384,6 +402,20 @@ fn parse_tex_dimension(spec: &str) -> Option<i64> {
Some((value * points_per_unit * 65_536.0).round() as i64)
}
/// Vertical glue inside `\noalign{...}`.
fn noalign_gap(material: &str) -> Option<i64> {
let material = material.trim();
let mut rest = material
.strip_prefix(r"\vskip")
.or_else(|| material.strip_prefix(r"\vspace"))?
.trim_start();
if let Some(inner) = rest.strip_prefix('{') {
rest = inner.split_once('}')?.0.trim();
}
let spec = rest.split_whitespace().next()?;
parse_tex_dimension(spec)
}
/// Strip booktabs rule commands from a row, recording their spans.
fn take_booktabs_rules(raw: &str, row: usize, columns: usize) -> (String, Vec<HalignRule>) {
let mut rules = Vec::new();
@@ -774,12 +806,13 @@ impl Unit {
column_wrap: wraps,
column_skips,
rules: Vec::new(),
row_gaps: Vec::new(),
width,
height: 0,
line_height,
};
halign.height = (0..halign.rows.len())
.map(|index| halign.row_height(index))
.map(|index| halign.row_height(index) + halign.row_gap(index))
.sum();
Self {
id,
@@ -832,6 +865,8 @@ impl Unit {
let at_glue = parse_at_glues(template);
let mut rows = Vec::new();
let mut rules: Vec<HalignRule> = Vec::new();
let mut row_gaps: Vec<i64> = Vec::new();
let mut pending_gap = 0_i64;
for raw_row in source.split(r"\\") {
if raw_row.trim().is_empty() {
continue;
@@ -843,7 +878,10 @@ impl Unit {
if let Some(rest) = raw_row.strip_prefix(r"\noalign") {
let rest = rest.trim_start();
if let Some(body) = rest.strip_prefix('{') {
if let Some((_, tail)) = body.split_once('}') {
if let Some((material, tail)) = body.split_once('}') {
if let Some(gap) = noalign_gap(material) {
pending_gap = gap;
}
raw_row = tail.trim();
}
}
@@ -892,6 +930,11 @@ impl Unit {
.unwrap_or(CellAlignment::Left),
});
}
if !rows.is_empty() {
row_gaps.resize(rows.len(), 0);
row_gaps[rows.len() - 1] = pending_gap;
}
pending_gap = 0;
rows.push(HalignRow { cells, rule: false });
}
let mut column_skips = tabskips;
@@ -910,9 +953,13 @@ impl Unit {
}
let mut unit =
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;
if let Some(VerticalItem::Halign(halign)) = unit.items.first_mut() {
halign.rules = rules;
if !row_gaps.is_empty() {
halign.row_gaps = row_gaps;
halign.height = (0..halign.rows.len())
.map(|index| halign.row_height(index) + halign.row_gap(index))
.sum();
}
}
unit
@@ -6987,6 +7034,51 @@ b & 2 \\
);
}
#[test]
fn noalign_vertical_glue_spaces_rows() {
let source = "a & b \\\\\n\\noalign{\\vskip 6pt}\nc & d \\\\\n";
let unit = Unit::halign_from_source_with_template(1, "ll", source, 0);
let VerticalItem::Halign(halign) = &unit.items[0] else {
panic!("expected halign");
};
assert_eq!(halign.rows.len(), 2);
assert_eq!(halign.row_gaps, vec![6 * 65_536]);
assert_eq!(
halign.height,
halign.row_height(0) + halign.row_height(1) + 6 * 65_536
);
let list = halign.display_list();
let mut a_y = None;
let mut c_y = None;
for item in &list.items {
if let DisplayItem::Text { y, text, .. } = item {
if text == "a" {
a_y = Some(*y);
}
if text == "c" {
c_y = Some(*y);
}
}
}
let (a_y, c_y) = (a_y.unwrap(), c_y.unwrap());
assert_eq!(c_y - a_y, halign.row_height(0) + 6 * 65_536);
}
#[test]
fn noalign_glue_stays_with_its_row_chunk() {
let source = "a & b \\\\\n\\noalign{\\vskip 6pt}\nc & d \\\\\n";
let unit = Unit::halign_from_source_with_template(1, "ll", source, 0);
let VerticalItem::Halign(halign) = &unit.items[0] else {
panic!("expected halign");
};
let chunks = halign.split_for_height(halign.row_height(0) + halign.row_gap(0));
assert_eq!(chunks.len(), 2);
assert_eq!(chunks[0].height, halign.row_height(0) + 6 * 65_536);
assert_eq!(chunks[0].row_gaps, vec![6 * 65_536]);
assert_eq!(chunks[1].rows.len(), 1);
assert_eq!(chunks[1].height, halign.row_height(1));
}
#[test]
fn checkpoint_round_trips_through_serde() {
let engine = Engine::layout(&document());
+2 -2
View File
@@ -290,7 +290,7 @@ impl Halign {
width: self.width,
height,
});
y += height;
y += height + self.row_gap(row_index);
continue;
}
let row_height = self.row_height(row_index);
@@ -326,7 +326,7 @@ impl Halign {
});
}
}
y += row_height;
y += row_height + self.row_gap(row_index);
}
for rule in self.rules.iter().filter(|rule| rule.row == self.rows.len()) {
let (x, width) = self.rule_rect(rule);