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
+112
-9
@@ -229,6 +229,8 @@ struct PageGeometry {
|
||||
head_rule_width: Option<i64>,
|
||||
/// Frozen `\sbox`/`\savebox` metrics: name -> (width, height, depth), sp.
|
||||
savebox_dimensions: Vec<(String, (i64, i64, i64))>,
|
||||
/// Nested `tabular` sources referenced by `\texrranested{index}` tokens.
|
||||
nested_tables: Vec<String>,
|
||||
head_sep: Option<i64>,
|
||||
head_height: Option<i64>,
|
||||
foot_skip: Option<i64>,
|
||||
@@ -2351,7 +2353,7 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
||||
}
|
||||
}
|
||||
let (template, text, _, target, _) = tabular.take().unwrap();
|
||||
let text = flatten_nested_tables(&text);
|
||||
let text = protect_nested_tables(&text, &mut geometry.nested_tables);
|
||||
geometry
|
||||
.tabular
|
||||
.push((template, expand_multirow(&text), target));
|
||||
@@ -3038,7 +3040,7 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
||||
}
|
||||
}
|
||||
if let Some((template, text, _, target, _)) = tabular {
|
||||
let text = flatten_nested_tables(&text);
|
||||
let text = protect_nested_tables(&text, &mut geometry.nested_tables);
|
||||
geometry
|
||||
.tabular
|
||||
.push((template, expand_multirow(&text), target));
|
||||
@@ -3063,9 +3065,52 @@ fn braced(text: &str) -> Option<(&str, &str)> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Replace nested `tabular` environments with a flattened text summary so the
|
||||
/// outer table keeps its cell structure. True nested-table layout is still
|
||||
/// open; this keeps the inner commands from breaking the outer parse.
|
||||
/// Replace nested `tabular` blocks with `\texrranested{index}` tokens and
|
||||
/// store their sources on the geometry for `tabular_unit` to resolve.
|
||||
fn protect_nested_tables(source: &str, nested_tables: &mut Vec<String>) -> String {
|
||||
let mut out = String::new();
|
||||
let mut rest = source;
|
||||
while let Some(position) = rest.find("\\begin{tabular") {
|
||||
out.push_str(&rest[..position]);
|
||||
let after = &rest[position..];
|
||||
let Some(end) = nested_table_end(after) else {
|
||||
out.push_str(after);
|
||||
return out;
|
||||
};
|
||||
let block = &after[..end];
|
||||
let index = nested_tables.len();
|
||||
nested_tables.push(block.to_string());
|
||||
out.push_str(&format!("\\texrranested{{{index}}}"));
|
||||
rest = &after[end..];
|
||||
}
|
||||
out.push_str(rest);
|
||||
out
|
||||
}
|
||||
|
||||
/// Split a full `\begin{tabular}...\end{tabular}` block into its column spec
|
||||
/// and body rows.
|
||||
fn nested_table_parts(block: &str) -> Option<(&str, &str)> {
|
||||
let open_end = block.find('}')?;
|
||||
let environment = block.get("\\begin{".len()..open_end)?;
|
||||
let mut rest = block[open_end + 1..].trim_start();
|
||||
if let Some(stripped) = rest.strip_prefix('[')
|
||||
&& let Some((_, tail)) = stripped.split_once(']')
|
||||
{
|
||||
rest = tail.trim_start();
|
||||
}
|
||||
if (environment == "tabularx" || environment == "tabular*")
|
||||
&& let Some((_, used)) = braced_group(rest)
|
||||
{
|
||||
rest = rest[used..].trim_start();
|
||||
}
|
||||
let (spec, used) = braced_group(rest)?;
|
||||
let body = &rest[used..];
|
||||
let body_end = body.rfind("\\end{tabular")?;
|
||||
Some((spec, body[..body_end].trim()))
|
||||
}
|
||||
|
||||
/// Flatten nested `tabular` environments to a text summary (fallback).
|
||||
#[allow(dead_code)]
|
||||
fn flatten_nested_tables(source: &str) -> String {
|
||||
let mut out = String::new();
|
||||
let mut rest = source;
|
||||
@@ -3472,9 +3517,51 @@ fn tabular_unit(
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(VerticalItem::Halign(halign)) = unit.items.first_mut() {
|
||||
resolve_nested_tables(halign, geometry, id);
|
||||
}
|
||||
unit
|
||||
}
|
||||
|
||||
fn resolve_nested_tables(halign: &mut texrra_tex_core::Halign, geometry: &PageGeometry, id: u64) {
|
||||
let mut placements = Vec::new();
|
||||
for (row_index, row) in halign.rows.iter().enumerate() {
|
||||
for (cell_index, cell) in row.cells.iter().enumerate() {
|
||||
if let Some(rest) = cell.text.trim().strip_prefix("\\texrranested{")
|
||||
&& let Some((index, _)) = rest.split_once('}')
|
||||
&& let Ok(index) = index.trim().parse::<usize>()
|
||||
{
|
||||
placements.push((row_index, cell_index, index));
|
||||
}
|
||||
}
|
||||
}
|
||||
for (row, cell, index) in placements {
|
||||
let Some(source) = geometry.nested_tables.get(index).cloned() else {
|
||||
continue;
|
||||
};
|
||||
let Some((template, body)) = nested_table_parts(&source) else {
|
||||
continue;
|
||||
};
|
||||
let template = template.to_string();
|
||||
let body = body.to_string();
|
||||
let inner = tabular_unit(geometry, id, &template, &body, None);
|
||||
let Some(VerticalItem::Halign(table)) = inner.items.first().cloned() else {
|
||||
continue;
|
||||
};
|
||||
if let Some(cell) = halign
|
||||
.rows
|
||||
.get_mut(row)
|
||||
.and_then(|line| line.cells.get_mut(cell))
|
||||
{
|
||||
cell.text.clear();
|
||||
}
|
||||
halign.nested.push((row, cell, table));
|
||||
}
|
||||
if !halign.nested.is_empty() {
|
||||
halign.refresh_measurements();
|
||||
}
|
||||
}
|
||||
|
||||
fn engine_template(spec: &str) -> String {
|
||||
let mut out = String::new();
|
||||
let mut characters = spec.chars().peekable();
|
||||
@@ -15280,8 +15367,10 @@ left & right \\
|
||||
\end{tabular}";
|
||||
let (body, geometry) = geometry_and_body(source);
|
||||
assert_eq!(geometry.tabular.len(), 1, "outer table captured once");
|
||||
assert_eq!(geometry.nested_tables.len(), 1, "nested table stored once");
|
||||
let (template, text, _) = &geometry.tabular[0];
|
||||
assert_eq!(template, "ll");
|
||||
assert!(text.contains("\\texrranested{0}"), "{text}");
|
||||
assert!(!text.contains("begin{tabular}"), "{text}");
|
||||
let blocks = paragraphs(&body).expect("paragraphs");
|
||||
let document = build_document(&blocks, &geometry);
|
||||
@@ -15297,11 +15386,25 @@ left & right \\
|
||||
.expect("halign");
|
||||
assert_eq!(halign.column_widths.len(), 2);
|
||||
assert_eq!(halign.rows.len(), 2);
|
||||
assert!(
|
||||
halign.rows[0].cells[1].text.contains("a b ; c d"),
|
||||
"nested content flattened as {:?}",
|
||||
halign.rows[0].cells[1].text
|
||||
assert_eq!(halign.nested.len(), 1, "nested table attached");
|
||||
let (row, cell, table) = &halign.nested[0];
|
||||
assert_eq!((*row, *cell), (0, 1));
|
||||
assert_eq!(table.rows.len(), 2);
|
||||
assert_eq!(table.column_widths.len(), 2);
|
||||
assert!(halign.rows[0].cells[1].text.is_empty());
|
||||
let cells: String = table
|
||||
.rows
|
||||
.iter()
|
||||
.flat_map(|row| row.cells.iter().map(|cell| cell.text.clone()))
|
||||
.collect();
|
||||
assert!(cells.contains('a') && cells.contains('d'), "{cells}");
|
||||
let document_source = format!(
|
||||
"\\documentclass{{article}}\n\\begin{{document}}\n\n{source}\n\nAfter.\n\n\\end{{document}}\n"
|
||||
);
|
||||
let json = preview_json(&document_source).expect("preview");
|
||||
let value: serde_json::Value = serde_json::from_str(&json).expect("JSON");
|
||||
let svg = value["pages"][0]["svg"].as_str().expect("svg");
|
||||
assert!(svg.contains("a") && svg.contains("d"), "{svg}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user