Lay out nested tables inside their cell
Nested tabulars were flattened to a text summary before the outer halign
parsed them, so the inner rows never existed as a table. Keep them instead:
strip_page_geometry replaces each nested block with a \texrranested{index}
token and stores the source on the geometry, and tabular_unit resolves the
token into a recursively built inner table attached to the cell. Cell text is
cleared, column widths and row height take the inner table's metrics, and page
splitting remaps nested row indices when it chunks rows.
Halign gains a nested list, refresh_measurements, nested-aware row_height and a
painting branch that translates the inner display list to the cell position at
the row's alignment. The old flattening pass stays as a dead-code fallback.
The nested-table test now checks one stored source, the placeholder token, one
attached 2x2 table in cell (0, 1), an empty outer cell and inner "a"/"d"
painted in the preview SVG.
This commit is contained in:
1 parent
371f05baaa
commit
81da180ab7
3 files changed
+205
-23
No files matched your search
@@ -178,6 +178,9 @@ pub struct Halign {
|
||||
pub width: i64,
|
||||
pub height: i64,
|
||||
pub line_height: i64,
|
||||
/// Nested tables painted inside a cell: (row, cell, table).
|
||||
#[serde(default)]
|
||||
pub nested: Vec<(usize, usize, Halign)>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
|
||||
@@ -336,6 +339,7 @@ impl Halign {
|
||||
foot_len: 0,
|
||||
margin: self.margin,
|
||||
multirow: Vec::new(),
|
||||
nested: self.nested_for_rows(&chunk_rows),
|
||||
width: self.width,
|
||||
height,
|
||||
line_height: self.line_height,
|
||||
@@ -376,6 +380,7 @@ impl Halign {
|
||||
foot_len: 0,
|
||||
margin: self.margin,
|
||||
multirow: Vec::new(),
|
||||
nested: self.nested_for_rows(&(start..index).collect::<Vec<_>>()),
|
||||
width: self.width,
|
||||
height,
|
||||
line_height: self.line_height,
|
||||
@@ -402,6 +407,7 @@ impl Halign {
|
||||
foot_len: 0,
|
||||
margin: self.margin,
|
||||
multirow: Vec::new(),
|
||||
nested: self.nested_for_rows(&(start..self.rows.len()).collect::<Vec<_>>()),
|
||||
width: self.width,
|
||||
height,
|
||||
line_height: self.line_height,
|
||||
@@ -409,6 +415,18 @@ impl Halign {
|
||||
chunks
|
||||
}
|
||||
|
||||
fn nested_for_rows(&self, indices: &[usize]) -> Vec<(usize, usize, Halign)> {
|
||||
let mut nested = Vec::new();
|
||||
for (new_row, old_row) in indices.iter().copied().enumerate() {
|
||||
for (row, cell, table) in &self.nested {
|
||||
if *row == old_row {
|
||||
nested.push((new_row, *cell, table.clone()));
|
||||
}
|
||||
}
|
||||
}
|
||||
nested
|
||||
}
|
||||
|
||||
/// Column index of `cell_index` inside `row`.
|
||||
fn cell_column(&self, row: &HalignRow, cell_index: usize) -> usize {
|
||||
row.cells[..cell_index.min(row.cells.len())]
|
||||
@@ -530,18 +548,77 @@ impl Halign {
|
||||
if row.rule {
|
||||
return 26_214;
|
||||
}
|
||||
let lines = (0..row.cells.len())
|
||||
.map(|index| {
|
||||
if self.cell_row_span(row_index, index) > 1 {
|
||||
1
|
||||
} else {
|
||||
self.cell_lines(row_index, index).len()
|
||||
let mut height = self.line_height
|
||||
* (0..row.cells.len())
|
||||
.map(|index| {
|
||||
if self.cell_row_span(row_index, index) > 1 {
|
||||
1
|
||||
} else {
|
||||
self.cell_lines(row_index, index).len()
|
||||
}
|
||||
})
|
||||
.max()
|
||||
.unwrap_or(1)
|
||||
.max(1) as i64;
|
||||
for (row, _, table) in &self.nested {
|
||||
if *row == row_index {
|
||||
height = height.max(table.height);
|
||||
}
|
||||
}
|
||||
height
|
||||
}
|
||||
|
||||
/// Recompute widths and height after nested tables are attached.
|
||||
pub fn refresh_measurements(&mut self) {
|
||||
let column_count = self.column_widths.len();
|
||||
let mut widths = vec![0_i64; column_count];
|
||||
for row in &self.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 width = crate::text_width(&cell.text);
|
||||
if let Some(slot) = widths.get_mut(column) {
|
||||
*slot = (*slot).max(width);
|
||||
}
|
||||
}
|
||||
})
|
||||
.max()
|
||||
.unwrap_or(1)
|
||||
.max(1);
|
||||
self.line_height * lines as i64
|
||||
column += span;
|
||||
}
|
||||
}
|
||||
for (row_index, row) in self.rows.iter().enumerate() {
|
||||
let mut column = 0_usize;
|
||||
for (cell_index, cell) in row.cells.iter().enumerate() {
|
||||
let span = cell.span.max(1).min(column_count.saturating_sub(column));
|
||||
if let Some((_, _, table)) = self
|
||||
.nested
|
||||
.iter()
|
||||
.find(|(row, cell, _)| *row == row_index && *cell == cell_index)
|
||||
{
|
||||
if span == 1 {
|
||||
if let Some(slot) = widths.get_mut(column) {
|
||||
*slot = (*slot).max(table.width);
|
||||
}
|
||||
}
|
||||
}
|
||||
column += span;
|
||||
}
|
||||
}
|
||||
let gaps: i64 = if self.column_skips.is_empty() {
|
||||
if column_count > 1 {
|
||||
let old_gap = (self.width - self.column_widths.iter().sum::<i64>())
|
||||
/ (column_count as i64 - 1);
|
||||
old_gap.max(0) * (column_count as i64 - 1)
|
||||
} else {
|
||||
0
|
||||
}
|
||||
} else {
|
||||
self.column_skips.iter().copied().sum()
|
||||
};
|
||||
self.column_widths = widths;
|
||||
self.width = self.column_widths.iter().sum::<i64>() + gaps;
|
||||
self.height = (0..self.rows.len())
|
||||
.map(|index| self.row_height(index) + self.row_gap(index))
|
||||
.sum();
|
||||
}
|
||||
|
||||
/// Uniform intercolumn spacing derived from the table width.
|
||||
@@ -1172,6 +1249,7 @@ impl Unit {
|
||||
foot_len: 0,
|
||||
margin: 0,
|
||||
multirow: Vec::new(),
|
||||
nested: Vec::new(),
|
||||
width,
|
||||
height: 0,
|
||||
line_height,
|
||||
|
||||
Reference in new issue
Block a user