#[derive(Debug, Clone)]
pub(crate) struct RowColumnGeometry {
positions: Vec<Vec<f64>>,
widths: Vec<Vec<f64>>,
spacing: f64,
cell_widths: Vec<Vec<(usize, usize, f64)>>,
}
impl RowColumnGeometry {
pub(super) fn new(positions: Vec<Vec<f64>>, widths: Vec<Vec<f64>>, spacing: f64) -> Self {
Self {
positions,
widths,
spacing,
cell_widths: Vec::new(),
}
}
pub(crate) fn cell_width(
&self,
row: usize,
start: usize,
end: usize,
cell_spacing: f64,
) -> f64 {
if let Some(width) = self.cell_widths.get(row).and_then(|cells| {
cells
.iter()
.find_map(|&(left, right, width)| (left == start && right == end).then_some(width))
}) {
return width;
}
self.widths[row][start..end].iter().sum::<f64>()
+ (self.spacing * (end - start) as f64 - cell_spacing)
}
pub(crate) fn scale(&mut self, factor: f64) {
for row in &mut self.positions {
for position in row {
*position *= factor;
}
}
for row in &mut self.widths {
for width in row {
*width *= factor;
}
}
self.spacing *= factor;
for row in &mut self.cell_widths {
for (_, _, width) in row {
*width *= factor;
}
}
}
pub(super) fn apply_closing_rows(
&mut self,
table: &crate::model::table::Table,
dpi: f64,
width_scale: f64,
) {
self.cell_widths = vec![Vec::new(); self.positions.len()];
let scale = dpi / 7200.0 * width_scale;
let spacing = f64::from(table.cell_spacing) * scale;
for (row, frames) in table.closing_row_frames().into_iter().enumerate() {
let Some(frames) = frames else {
continue;
};
for frame in frames {
let cell = &table.cells[frame.cell_index];
let start = usize::from(cell.col);
let end = start + usize::from(cell.col_span);
let left = frame.start_hu as f64 * scale;
let width = frame.width_hu as f64 * scale;
let track = (width + spacing) / f64::from(cell.col_span);
for col in start..end {
self.positions[row][col] = left + track * (col - start) as f64;
self.widths[row][col] = track - spacing;
}
self.positions[row][end] = left + width + spacing;
self.cell_widths[row].push((start, end, width));
}
}
}
}
impl std::ops::Deref for RowColumnGeometry {
type Target = [Vec<f64>];
fn deref(&self) -> &Self::Target {
&self.positions
}
}