Honour explicit tabskip glue in alignments
This commit is contained in:
1 parent
d1b93ac9be
commit
d0ffeea94b
3 files changed
+138
-14
No files matched your search
@@ -58,6 +58,10 @@ pub struct Halign {
|
||||
/// Per-column `p{width}` declarations; `Some` makes the column wrap.
|
||||
#[serde(default)]
|
||||
pub column_wrap: Vec<Option<i64>>,
|
||||
/// Glue before each column boundary (TeX `\tabskip`); empty falls back to
|
||||
/// the uniform intercolumn spacing derived from `width`.
|
||||
#[serde(default)]
|
||||
pub column_skips: Vec<i64>,
|
||||
pub width: i64,
|
||||
pub height: i64,
|
||||
pub line_height: i64,
|
||||
@@ -107,6 +111,7 @@ impl Halign {
|
||||
rows: self.rows[start..index].to_vec(),
|
||||
column_widths: self.column_widths.clone(),
|
||||
column_wrap: self.column_wrap.clone(),
|
||||
column_skips: self.column_skips.clone(),
|
||||
width: self.width,
|
||||
height,
|
||||
line_height: self.line_height,
|
||||
@@ -120,6 +125,7 @@ impl Halign {
|
||||
rows: self.rows[start..].to_vec(),
|
||||
column_widths: self.column_widths.clone(),
|
||||
column_wrap: self.column_wrap.clone(),
|
||||
column_skips: self.column_skips.clone(),
|
||||
width: self.width,
|
||||
height,
|
||||
line_height: self.line_height,
|
||||
@@ -164,18 +170,17 @@ impl Halign {
|
||||
if span == 0 {
|
||||
return None;
|
||||
}
|
||||
let intercolumn = if self.column_widths.len() > 1 {
|
||||
(self.width - self.column_widths.iter().copied().sum::<i64>())
|
||||
/ (self.column_widths.len() as i64 - 1)
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let gaps = intercolumn * (span.saturating_sub(1) as i64);
|
||||
let gaps: i64 = (column..column + span - 1)
|
||||
.map(|boundary| self.gap_width(boundary))
|
||||
.sum();
|
||||
let width = self.column_widths[column..column + span]
|
||||
.iter()
|
||||
.sum::<i64>()
|
||||
+ gaps;
|
||||
let x: i64 = self.column_widths[..column].iter().sum::<i64>() + intercolumn * column as i64;
|
||||
let x: i64 = self.column_widths[..column].iter().sum::<i64>()
|
||||
+ (0..column)
|
||||
.map(|boundary| self.gap_width(boundary))
|
||||
.sum::<i64>();
|
||||
Some((x, width))
|
||||
}
|
||||
|
||||
@@ -215,6 +220,25 @@ impl Halign {
|
||||
.max(1);
|
||||
self.line_height * lines as i64
|
||||
}
|
||||
|
||||
/// Uniform intercolumn spacing derived from the table width.
|
||||
fn uniform_intercolumn(&self) -> i64 {
|
||||
if self.column_widths.len() > 1 {
|
||||
(self.width - self.column_widths.iter().copied().sum::<i64>())
|
||||
/ (self.column_widths.len() as i64 - 1)
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
/// Width of the glue before `column` (boundary `column - 1`).
|
||||
fn gap_width(&self, boundary: usize) -> i64 {
|
||||
if self.column_skips.is_empty() {
|
||||
self.uniform_intercolumn()
|
||||
} else {
|
||||
self.column_skips.get(boundary).copied().unwrap_or(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Natural width of a run of text at the illustrative 10pt metrics.
|
||||
@@ -448,6 +472,18 @@ impl Unit {
|
||||
column_count: usize,
|
||||
intercolumn: i64,
|
||||
wraps: Vec<Option<i64>>,
|
||||
) -> Self {
|
||||
Self::halign_with_layout(id, rows, column_count, intercolumn, wraps, Vec::new())
|
||||
}
|
||||
|
||||
/// Build a `\halign` table with explicit inter-column glue (`\tabskip`).
|
||||
pub fn halign_with_layout(
|
||||
id: UnitId,
|
||||
rows: Vec<HalignRow>,
|
||||
column_count: usize,
|
||||
intercolumn: i64,
|
||||
wraps: Vec<Option<i64>>,
|
||||
column_skips: Vec<i64>,
|
||||
) -> Self {
|
||||
let intercolumn = intercolumn.saturating_mul(65_536);
|
||||
let mut column_widths = vec![0_i64; column_count];
|
||||
@@ -488,13 +524,18 @@ impl Unit {
|
||||
*slot = (*width).max(10 * 65_536);
|
||||
}
|
||||
}
|
||||
let gaps = intercolumn * column_count.saturating_sub(1) as i64;
|
||||
let gaps: i64 = if column_skips.is_empty() {
|
||||
intercolumn * column_count.saturating_sub(1) as i64
|
||||
} else {
|
||||
column_skips.iter().copied().sum()
|
||||
};
|
||||
let width = column_widths.iter().sum::<i64>() + gaps;
|
||||
let line_height = 12 * 65_536;
|
||||
let mut halign = Halign {
|
||||
rows,
|
||||
column_widths,
|
||||
column_wrap: wraps,
|
||||
column_skips,
|
||||
width,
|
||||
height: 0,
|
||||
line_height,
|
||||
@@ -530,6 +571,23 @@ impl Unit {
|
||||
template: &str,
|
||||
source: &str,
|
||||
intercolumn: i64,
|
||||
) -> Self {
|
||||
Self::halign_from_source_with_template_and_tabskips(
|
||||
id,
|
||||
template,
|
||||
source,
|
||||
intercolumn,
|
||||
Vec::new(),
|
||||
)
|
||||
}
|
||||
|
||||
/// Parse a column template and accept explicit `\tabskip` glue widths.
|
||||
pub fn halign_from_source_with_template_and_tabskips(
|
||||
id: UnitId,
|
||||
template: &str,
|
||||
source: &str,
|
||||
intercolumn: i64,
|
||||
tabskips: Vec<i64>,
|
||||
) -> Self {
|
||||
let (alignments, wraps) = parse_column_template(template);
|
||||
let column_count = alignments.len().max(1);
|
||||
@@ -593,7 +651,7 @@ impl Unit {
|
||||
}
|
||||
rows.push(HalignRow { cells, rule: false });
|
||||
}
|
||||
Self::halign_with_wraps(id, rows, column_count, intercolumn, wraps)
|
||||
Self::halign_with_layout(id, rows, column_count, intercolumn, wraps, tabskips)
|
||||
}
|
||||
|
||||
/// A running mark item, recorded on the page where it is shipped out.
|
||||
@@ -6469,6 +6527,72 @@ second row text & y\n",
|
||||
assert_eq!(chunks[0].column_widths, chunks[1].column_widths);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn explicit_tabskips_set_the_column_gaps() {
|
||||
let unit = Unit::halign_from_source_with_template_and_tabskips(
|
||||
1,
|
||||
"ll",
|
||||
"a & b \\\\
|
||||
cc & d\n",
|
||||
0,
|
||||
vec![30 * 65_536],
|
||||
);
|
||||
let VerticalItem::Halign(halign) = &unit.items[0] else {
|
||||
panic!("expected halign");
|
||||
};
|
||||
assert_eq!(halign.column_skips, vec![30 * 65_536]);
|
||||
let first_width = halign.column_widths[0];
|
||||
assert_eq!(
|
||||
halign.cell_span(0, 1),
|
||||
Some((first_width + 30 * 65_536, halign.column_widths[1]))
|
||||
);
|
||||
assert_eq!(
|
||||
halign.width,
|
||||
halign.column_widths.iter().sum::<i64>() + 30 * 65_536
|
||||
);
|
||||
let list = halign.display_list();
|
||||
let second_x = list
|
||||
.items
|
||||
.iter()
|
||||
.find_map(|item| match item {
|
||||
DisplayItem::Text { text, x, .. } if text == "b" => Some(*x),
|
||||
_ => None,
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(second_x, first_width + 30 * 65_536);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tabskips_survive_page_splitting() {
|
||||
let mut source = String::new();
|
||||
for index in 0..20 {
|
||||
source.push_str(&format!(
|
||||
"row{index} & value{index} \\\\
|
||||
"
|
||||
));
|
||||
}
|
||||
let unit = Unit::halign_from_source_with_template_and_tabskips(
|
||||
1,
|
||||
"ll",
|
||||
&source,
|
||||
0,
|
||||
vec![15 * 65_536],
|
||||
);
|
||||
let VerticalItem::Halign(halign) = &unit.items[0] else {
|
||||
panic!("expected halign");
|
||||
};
|
||||
let chunks = halign.split_for_height(halign.line_height * 3);
|
||||
assert!(chunks.len() >= 2);
|
||||
for chunk in &chunks {
|
||||
assert_eq!(chunk.column_skips, vec![15 * 65_536]);
|
||||
assert_eq!(chunk.column_widths, halign.column_widths);
|
||||
}
|
||||
assert_eq!(
|
||||
chunks.iter().map(|chunk| chunk.rows.len()).sum::<usize>(),
|
||||
20
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn checkpoint_round_trips_through_serde() {
|
||||
let engine = Engine::layout(&document());
|
||||
|
||||
Reference in new issue
Block a user