Reserve outer tabcolsep margins around tables

This commit is contained in:
gloamxun committed 2026-10-26 20:29:51 +08:00
1 parent 20d23ce68d
commit aa072f58d8
5 files changed
+52 -12

No files matched your search

+33 -4
View File
@@ -2659,7 +2659,9 @@ fn tabular_unit(
source: &str,
target: Option<i64>,
) -> Unit {
let template = expand_tabular_template(template, source, target);
let separator = geometry.tab_col_sep.unwrap_or(6 * SCALE);
let inner_target = target.map(|target| (target - 2 * 6 * SCALE).max(10 * 65_536));
let template = expand_tabular_template(template, source, inner_target);
let mut unit = Unit::halign_from_source_with_template_and_tabskips(
id,
&engine_template(&template),
@@ -2667,7 +2669,6 @@ fn tabular_unit(
12,
Vec::new(),
);
let separator = geometry.tab_col_sep.unwrap_or(6 * SCALE);
let stretch = geometry.array_stretch.unwrap_or(1000);
if separator != 6 * SCALE || stretch != 1000 {
if let Some(VerticalItem::Halign(halign)) = unit.items.first_mut() {
@@ -2677,6 +2678,7 @@ fn tabular_unit(
halign.width =
halign.column_widths.iter().sum::<i64>() + separator * 2 * gaps as i64;
}
halign.margin = separator;
if stretch != 1000 {
halign.line_height = halign.line_height.saturating_mul(stretch) / 1000;
halign.height = (0..halign.rows.len())
@@ -2685,6 +2687,10 @@ fn tabular_unit(
}
}
}
if let Some(VerticalItem::Halign(halign)) = unit.items.first_mut() {
halign.margin = separator;
halign.width += 2 * separator;
}
unit
}
@@ -10151,6 +10157,29 @@ mod tests {
assert_eq!(caption, Some("Tab. 1: B"));
}
#[test]
fn tables_reserve_outer_tabcolsep_margins() {
let source = "\\begin{tabular}{ll}\nA & B \\\\\nC & D\n\\end{tabular}";
let (body, geometry) = geometry_and_body(source);
let blocks = paragraphs(&body).expect("paragraphs");
let document = build_document(&blocks, &geometry);
let halign = document
.units
.iter()
.flat_map(|unit| &unit.items)
.find_map(|item| match item {
VerticalItem::Halign(halign) => Some(halign),
_ => None,
})
.expect("tabular halign");
assert_eq!(halign.margin, 6 * SCALE);
assert_eq!(halign.cell_box(0, 0).expect("first cell").0, 6 * SCALE);
assert_eq!(
halign.width,
halign.column_widths.iter().sum::<i64>() + 24 * SCALE
);
}
#[test]
fn tabcolsep_sets_tabular_column_glue() {
let source = "\\tabcolsep=20pt\n\n\\begin{tabular}{ll}\nA & B \\\\\nC & D\n\\end{tabular}";
@@ -10171,7 +10200,7 @@ mod tests {
.expect("tabular halign");
assert_eq!(halign.column_skips, vec![40 * SCALE]);
let columns: i64 = halign.column_widths.iter().sum();
assert_eq!(halign.width, columns + 40 * SCALE);
assert_eq!(halign.width, columns + 80 * SCALE);
let (_, default) = geometry_and_body("\\begin{tabular}{ll}\nA & B\n\\end{tabular}");
assert_eq!(default.tab_col_sep, None);
@@ -10189,7 +10218,7 @@ mod tests {
.expect("tabular halign");
assert!(halign.column_skips.is_empty());
let columns: i64 = halign.column_widths.iter().sum();
assert_eq!(halign.width, columns + 12 * SCALE);
assert_eq!(halign.width, columns + 24 * SCALE);
}
#[test]