From c2457bdb8c250fc9c704ac66a2a98c681965ad62 Mon Sep 17 00:00:00 2001 From: "D. Fender" <26173007+postfen@users.noreply.github.com> Date: Thu, 22 Oct 2026 19:09:38 -0700 Subject: [PATCH] Tables: Find a table's visible edges once per paint The grid and print checked, for every painted table cell, whether all rows and columns between it and the table's edge were hidden. On a big filtered table that rescans the hidden rows for every cell. shown_edges() now finds a table's first and last visible row and column, and the per-paint CfCache keeps it for each table. Print now always keeps a CfCache. --- crates/engine/src/cf.rs | 9 +++++- crates/engine/src/cmd/print.rs | 10 +++---- crates/engine/src/tables.rs | 38 ++++++++++++++++++------ crates/engine/tests/table_style_looks.rs | 7 +++-- crates/ui-egui/src/grid.rs | 2 +- 5 files changed, 48 insertions(+), 18 deletions(-) diff --git a/crates/engine/src/cf.rs b/crates/engine/src/cf.rs index 425e5264e..331953288 100644 --- a/crates/engine/src/cf.rs +++ b/crates/engine/src/cf.rs @@ -20,10 +20,12 @@ pub struct CfLook { pub hide_value: bool, } -/// Precomputed statistics per rule (min/max/percentiles/top-N thresholds/duplicates). +/// Precomputed statistics per rule (min/max/percentiles/top-N thresholds/duplicates), and each +/// table's visible edges. The grid and print keep one per paint. #[derive(Default)] pub struct CfCache { stats: HashMap, + table_edges: HashMap, } #[derive(Default, Clone)] @@ -141,6 +143,11 @@ impl CfCache { Self::default() } + /// [`crate::tables::shown_edges`] of `t`, found once per cache. + pub fn table_edges(&mut self, sh: &Sheet, t: &Table) -> crate::tables::ShownEdges { + *self.table_edges.entry(t.range).or_insert_with(|| crate::tables::shown_edges(sh, t)) + } + /// Conditional look of `c`, or `None` when no rule applies. pub fn look(&mut self, wb: &Workbook, sheet: usize, c: CellRef) -> Option { let sh = wb.sheet(sheet)?; diff --git a/crates/engine/src/cmd/print.rs b/crates/engine/src/cmd/print.rs index ba115e63e..3a7dc3fa8 100644 --- a/crates/engine/src/cmd/print.rs +++ b/crates/engine/src/cmd/print.rs @@ -634,12 +634,12 @@ struct Look { cf: Option, } -fn cell_look(wb: &Workbook, si: usize, sh: &Sheet, c: CellRef, cf: &mut Option) -> Look { +fn cell_look(wb: &Workbook, si: usize, sh: &Sheet, c: CellRef, cf: &mut crate::cf::CfCache) -> Look { let mut style = match sh.table_at(c) { - Some(t) => crate::tables::table_cell_shown_style(wb, si, t, c), + Some(t) => crate::tables::table_cell_shown_style(wb, si, t, cf.table_edges(sh, t), c), None => wb.styles.get(sh.style_id(c)).clone(), }; - let cfl = cf.as_mut().and_then(|cache| cache.look(wb, si, c)); + let cfl = cf.look(wb, si, c); if let Some(st) = cfl.as_ref().and_then(|l| l.style.as_ref()) { if st.fill.pattern != PatternType::None { style.fill = st.fill; @@ -882,7 +882,7 @@ fn font_for(style: &Style) -> Font { } /// Draws the cells of a block: fills, gridlines, data bars, borders and text. -fn draw_block(pg: &mut Page, b: &Block, gridlines: bool, cf: &mut Option) { +fn draw_block(pg: &mut Page, b: &Block, gridlines: bool, cf: &mut crate::cf::CfCache) { let (bx, by) = (b.xs.first().copied().unwrap_or(0.0), b.ys.first().copied().unwrap_or(0.0)); let (bw, bh) = (b.xs.last().copied().unwrap_or(bx) - bx, b.ys.last().copied().unwrap_or(by) - by); if bw <= 0.0 || bh <= 0.0 { @@ -1530,7 +1530,7 @@ pub fn render_pdf(wb: &Workbook, layouts: &[SheetLayout], file: &str, path: &str let mut printed_blocks = Vec::new(); for lay in layouts { let Some(sh) = wb.sheet(lay.sheet) else { continue }; - let mut cf = if sh.cond_formats.is_empty() { None } else { Some(crate::cf::CfCache::new()) }; + let mut cf = crate::cf::CfCache::new(); let k = lay.k(); let (aw, ah) = lay.avail(); for (pi, (ri, ci)) in lay.pages.iter().enumerate() { diff --git a/crates/engine/src/tables.rs b/crates/engine/src/tables.rs index b2afdd350..42da29e54 100644 --- a/crates/engine/src/tables.rs +++ b/crates/engine/src/tables.rs @@ -243,23 +243,43 @@ pub fn table_cell_style(wb: &Workbook, sheet: usize, t: &Table, c: CellRef) -> S s } +/// A table's first and last visible row and column (hidden or filtered lines skipped). +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct ShownEdges { + pub top: u32, + pub bottom: u32, + pub left: u32, + pub right: u32, +} + +/// Finds [`ShownEdges`]. It scans the hidden lines at each edge, so the grid and print call it +/// once per table per paint (through [`crate::cf::CfCache::table_edges`]), not once per cell. +pub fn shown_edges(sh: &Sheet, t: &Table) -> ShownEdges { + let (start, end) = (t.range.start, t.range.end); + let row_shown = |r: &u32| sh.row_height(*r) > 0.0; + let col_shown = |k: &u32| sh.col_width(*k) > 0.0; + // With every row (or column) hidden, each cell takes both edges' lines; nothing is drawn. + ShownEdges { + top: (start.row..=end.row).find(row_shown).unwrap_or(end.row), + bottom: (start.row..=end.row).rev().find(row_shown).unwrap_or(start.row), + left: (start.col..=end.col).find(col_shown).unwrap_or(end.col), + right: (start.col..=end.col).rev().find(col_shown).unwrap_or(start.col), + } +} + /// The style the grid and print draw for a table cell. Like [`table_cell_style`], but the /// table's outer frame follows hidden rows and columns: the last visible row takes the table's /// bottom line, the last visible column its right side, and the first visible row and column its /// top line and left side. (Each frame line is owned by the edge cell, which is never drawn while /// it is hidden or filtered out.) Convert to Range uses [`table_cell_style`], the table as it is. -pub fn table_cell_shown_style(wb: &Workbook, sheet: usize, t: &Table, c: CellRef) -> Style { +pub fn table_cell_shown_style(wb: &Workbook, sheet: usize, t: &Table, e: ShownEdges, c: CellRef) -> Style { let mut s = table_cell_style(wb, sheet, t, c); let Some(sh) = wb.sheet(sheet) else { return s }; let (start, end) = (t.range.start, t.range.end); - let row_gone = |r: u32| sh.row_height(r) <= 0.0; - let col_gone = |k: u32| sh.col_width(k) <= 0.0; - // Each check walks outward from the cell and stops at the first visible line, so the gaps - // scanned by different cells don't overlap. - let below = c.row < end.row && (c.row + 1..=end.row).all(row_gone); - let above = c.row > start.row && (start.row..c.row).rev().all(row_gone); - let right = c.col < end.col && (c.col + 1..=end.col).all(col_gone); - let left = c.col > start.col && (start.col..c.col).rev().all(col_gone); + let below = c.row < end.row && c.row >= e.bottom; + let above = c.row > start.row && c.row <= e.top; + let right = c.col < end.col && c.col >= e.right; + let left = c.col > start.col && c.col <= e.left; if !(below || above || right || left) { return s; } diff --git a/crates/engine/tests/table_style_looks.rs b/crates/engine/tests/table_style_looks.rs index 366a89c67..64bfd2d55 100644 --- a/crates/engine/tests/table_style_looks.rs +++ b/crates/engine/tests/table_style_looks.rs @@ -4,7 +4,7 @@ use gridcraft_core::CellRef; use gridcraft_engine::Session; use gridcraft_engine::model::*; -use gridcraft_engine::tables::{table_cell_shown_style, table_cell_style}; +use gridcraft_engine::tables::{ShownEdges, shown_edges, table_cell_shown_style, table_cell_style}; use serde_json::json; const ROWS: u32 = 6; // header + 5 data rows @@ -197,7 +197,8 @@ fn shown_with(s: &Session, style: &str, edit: impl Fn(&mut Table)) -> Vec bool) -> fn look(wb: &Workbook, si: usize, sh: &Sheet, c: CellRef, cf: &mut CfCache) -> (Look, Option) { let mut style = wb.styles.get(sh.style_id(c)).clone(); if let Some(t) = sh.table_at(c) { - style = gridcraft_engine::tables::table_cell_shown_style(wb, si, t, c); + style = gridcraft_engine::tables::table_cell_shown_style(wb, si, t, cf.table_edges(sh, t), c); } let cfl = cf.look(wb, si, c); if let Some(l) = &cfl