use std::collections::HashSet;
use crate::model::document::DocInfo;
use crate::model::paragraph::Paragraph;
use crate::model::shape::Caption;
use crate::model::table::Table;
use crate::doclang::ir::{Block, Inline, Table as IrTable, TableCell as IrTableCell};
use crate::doclang::loss::report::{LossEntry, LossKind, LossReport};
use super::inline::extract_inlines;
pub(crate) fn convert_table(
table: &Table,
doc_info: &DocInfo,
location: &str,
convert_paragraphs: &dyn Fn(&[Paragraph]) -> Vec<Block>,
loss: &mut LossReport,
) -> IrTable {
let rows = table.row_count;
let cols = table.col_count;
let mut cells: Vec<IrTableCell> = Vec::with_capacity(table.cells.len());
let mut occupied: HashSet<(u16, u16)> = HashSet::with_capacity(table.cells.len());
for cell in &table.cells {
let mut col = cell.col;
let mut row = cell.row;
if cols == 0 || row >= rows || col >= cols {
let new_col = if cols == 0 { 0 } else { col.min(cols - 1) };
let new_row = if rows == 0 { 0 } else { row.min(rows - 1) };
loss.push(LossEntry {
kind: LossKind::Other("table-cell-out-of-bounds".to_string()),
location: location.to_string(),
detail: format!(
"cell ({},{}) outside {}x{} grid, clamped to ({},{})",
row, col, rows, cols, new_row, new_col
),
});
col = new_col;
row = new_row;
}
if !occupied.insert((row, col)) {
loss.push(LossEntry {
kind: LossKind::Other("table-cell-collision".to_string()),
location: location.to_string(),
detail: format!(
"cell anchor ({},{}) collides with an earlier cell",
row, col
),
});
}
let col_span = cell.col_span.max(1);
let row_span = cell.row_span.max(1);
cells.push(IrTableCell {
col,
row,
col_span,
row_span,
is_header: cell.is_header,
content: convert_paragraphs(&cell.paragraphs),
});
}
let caption = table
.caption
.as_ref()
.and_then(|cap| convert_caption(cap, doc_info, location, loss));
IrTable {
rows,
cols,
cells,
caption,
prov: None,
}
}
fn convert_caption(
caption: &Caption,
doc_info: &DocInfo,
location: &str,
loss: &mut LossReport,
) -> Option<Vec<Inline>> {
let caption_loc = format!("{}/caption", location);
let mut inlines: Vec<Inline> = Vec::new();
for para in &caption.paragraphs {
inlines.extend(extract_inlines(para, doc_info, &caption_loc, loss));
}
loss.push(LossEntry {
kind: LossKind::Caption,
location: caption_loc,
detail: format!(
"caption direction/width/spacing dropped (direction={:?})",
caption.direction
),
});
if inlines.is_empty() {
None
} else {
Some(inlines)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::document::DocInfo;
use crate::model::paragraph::Paragraph;
use crate::model::shape::Caption;
use crate::model::table::{Cell, Table};
fn stub_convert(paras: &[Paragraph]) -> Vec<Block> {
paras
.iter()
.map(|_| Block::Paragraph {
content: Vec::new(),
lost: None,
prov: None,
})
.collect()
}
fn anchor(col: u16, row: u16, col_span: u16, row_span: u16) -> Cell {
Cell {
col,
row,
col_span,
row_span,
paragraphs: vec![Paragraph::default()],
..Default::default()
}
}
#[test]
fn simple_2x2_maps_all_cells() {
let table = Table {
row_count: 2,
col_count: 2,
cells: vec![
anchor(0, 0, 1, 1),
anchor(1, 0, 1, 1),
anchor(0, 1, 1, 1),
anchor(1, 1, 1, 1),
],
..Default::default()
};
let di = DocInfo::default();
let mut loss = LossReport::new();
let out = convert_table(&table, &di, "s0/tbl", &stub_convert, &mut loss);
assert_eq!(out.rows, 2);
assert_eq!(out.cols, 2);
assert_eq!(out.cells.len(), 4);
assert!(out.cells.iter().all(|c| c.content.len() == 1));
assert!(out.cells.iter().all(|c| c.col_span == 1 && c.row_span == 1));
assert!(out.caption.is_none());
assert!(loss.is_empty());
}
#[test]
fn colspan_and_rowspan_preserved() {
let table = Table {
row_count: 2,
col_count: 2,
cells: vec![anchor(0, 0, 2, 1), anchor(0, 1, 1, 2)],
..Default::default()
};
let di = DocInfo::default();
let mut loss = LossReport::new();
let out = convert_table(&table, &di, "s0/tbl", &stub_convert, &mut loss);
assert_eq!(out.cells.len(), 2);
assert_eq!(out.cells[0].col_span, 2);
assert_eq!(out.cells[0].row_span, 1);
assert_eq!(out.cells[1].col_span, 1);
assert_eq!(out.cells[1].row_span, 2);
assert!(loss.is_empty());
}
#[test]
fn is_header_flag_preserved() {
let mut header = anchor(0, 0, 1, 1);
header.is_header = true;
let table = Table {
row_count: 1,
col_count: 2,
cells: vec![header, anchor(1, 0, 1, 1)],
..Default::default()
};
let di = DocInfo::default();
let mut loss = LossReport::new();
let out = convert_table(&table, &di, "s0/tbl", &stub_convert, &mut loss);
assert!(out.cells[0].is_header);
assert!(!out.cells[1].is_header);
}
#[test]
fn caption_present_extracts_inlines_and_records_loss() {
let text = "Table 1";
let char_offsets: Vec<u32> = (0..text.chars().count() as u32).collect();
let cap_para = Paragraph {
text: text.to_string(),
char_offsets,
..Default::default()
};
let table = Table {
row_count: 1,
col_count: 1,
cells: vec![anchor(0, 0, 1, 1)],
caption: Some(Caption {
paragraphs: vec![cap_para],
..Default::default()
}),
..Default::default()
};
let di = DocInfo::default();
let mut loss = LossReport::new();
let out = convert_table(&table, &di, "s0/tbl", &stub_convert, &mut loss);
assert_eq!(out.caption, Some(vec![Inline::Text("Table 1".to_string())]));
assert_eq!(
loss.iter().filter(|e| e.kind == LossKind::Caption).count(),
1
);
}
#[test]
fn out_of_bounds_cell_clamped_and_recorded() {
let table = Table {
row_count: 2,
col_count: 2,
cells: vec![anchor(0, 0, 1, 1), anchor(5, 9, 1, 1)],
..Default::default()
};
let di = DocInfo::default();
let mut loss = LossReport::new();
let out = convert_table(&table, &di, "s0/tbl", &stub_convert, &mut loss);
assert_eq!(out.cells[1].col, 1);
assert_eq!(out.cells[1].row, 1);
let other_count = loss
.iter()
.filter(|e| matches!(&e.kind, LossKind::Other(s) if s == "table-cell-out-of-bounds"))
.count();
assert_eq!(other_count, 1);
}
#[test]
fn clamped_cells_colliding_on_same_anchor_are_recorded() {
let table = Table {
row_count: 2,
col_count: 2,
cells: vec![anchor(0, 0, 1, 1), anchor(5, 9, 1, 1), anchor(6, 8, 1, 1)],
..Default::default()
};
let di = DocInfo::default();
let mut loss = LossReport::new();
let _ = convert_table(&table, &di, "s0/tbl", &stub_convert, &mut loss);
let collisions = loss
.iter()
.filter(|e| matches!(&e.kind, LossKind::Other(s) if s == "table-cell-collision"))
.count();
assert_eq!(collisions, 1);
}
#[test]
fn well_formed_table_records_no_collision() {
let table = Table {
row_count: 2,
col_count: 2,
cells: vec![
anchor(0, 0, 1, 1),
anchor(1, 0, 1, 1),
anchor(0, 1, 1, 1),
anchor(1, 1, 1, 1),
],
..Default::default()
};
let di = DocInfo::default();
let mut loss = LossReport::new();
let _ = convert_table(&table, &di, "s0/tbl", &stub_convert, &mut loss);
assert!(loss
.iter()
.all(|e| !matches!(&e.kind, LossKind::Other(s) if s == "table-cell-collision")));
}
#[test]
fn empty_caption_paragraphs_yields_none_caption() {
let table = Table {
row_count: 1,
col_count: 1,
cells: vec![anchor(0, 0, 1, 1)],
caption: Some(Caption {
paragraphs: vec![Paragraph::default()], ..Default::default()
}),
..Default::default()
};
let di = DocInfo::default();
let mut loss = LossReport::new();
let out = convert_table(&table, &di, "s0/tbl", &stub_convert, &mut loss);
assert!(out.caption.is_none());
assert_eq!(
loss.iter().filter(|e| e.kind == LossKind::Caption).count(),
1
);
}
}