use super::table::Cell;
use super::Table;
impl Table {
pub(crate) fn physical_cell_width_hu(&self, cell: &Cell) -> u32 {
if cell.width == 0 {
return 0;
}
const MIN_CONTENT_WIDTH_HU: i32 = 283;
let padding = cell.effective_padding(&self.padding);
let minimum = (i32::from(padding.left) + i32::from(padding.right) + MIN_CONTENT_WIDTH_HU)
.max(0) as u32;
cell.width.max(minimum)
}
pub(crate) fn closed_inline_grid_width_hu(&self) -> Option<u32> {
match self.closed_inline_grid_width_hu_with_storage(&crate::storage::Unbounded) {
Ok(value) => value,
Err(never) => match never {},
}
}
pub(crate) fn closed_inline_grid_width_hu_with_storage<S: crate::storage::Storage>(
&self,
storage: &S,
) -> Result<Option<u32>, S::Error> {
if !self.common.treat_as_char
|| self.row_count == 0
|| self.col_count == 0
|| self.cell_spacing < 0
|| self.cells.iter().any(|cell| {
cell.row >= self.row_count
|| cell.row_span != 1
|| cell.col_span == 0
|| cell.width == 0
})
{
return Ok(None);
}
let mut rows = storage.vec(usize::from(self.row_count))?;
rows.resize_with(usize::from(self.row_count), Vec::new);
for cell in &self.cells {
storage.push(&mut rows[usize::from(cell.row)], cell)?;
}
Ok((|| {
let mut consensus = None;
for cells in &mut rows {
cells.sort_unstable_by_key(|cell| cell.col);
let mut next_col = 0u16;
let mut width = 0u64;
for cell in cells.iter() {
if cell.col != next_col {
return None;
}
next_col = next_col.checked_add(cell.col_span)?;
if next_col > self.col_count {
return None;
}
width = width.checked_add(u64::from(self.physical_cell_width_hu(cell)))?;
}
if next_col != self.col_count {
return None;
}
let spacing = i64::from(self.outer_cell_spacing_hu())
+ self.inner_cell_spacing_hu(cells.len());
width = width.checked_add(u64::try_from(spacing).ok()?)?;
let width = u32::try_from(width).ok()?;
if width > i32::MAX as u32 || consensus.is_some_and(|prior| prior != width) {
return None;
}
consensus = Some(width);
}
consensus
})())
}
}