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.
This commit is contained in:
D. Fender committed 2026-10-22 19:09:38 -07:00
1 parent 74f09bc672
commit c2457bdb8c
5 files changed
+48 -18

No files matched your search

+8 -1
View File
@@ -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<usize, RuleStats>,
table_edges: HashMap<RangeRef, crate::tables::ShownEdges>,
}
#[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<CfLook> {
let sh = wb.sheet(sheet)?;
+5 -5
View File
@@ -634,12 +634,12 @@ struct Look {
cf: Option<crate::cf::CfLook>,
}
fn cell_look(wb: &Workbook, si: usize, sh: &Sheet, c: CellRef, cf: &mut Option<crate::cf::CfCache>) -> 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<crate::cf::CfCache>) {
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() {
+29 -9
View File
@@ -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;
}
+5 -2
View File
@@ -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<Vec<St
let mut t = wb.sheet(0).unwrap().tables[0].clone();
t.style = format!("TableStyle{style}");
edit(&mut t);
(0..ROWS).map(|r| (0..COLS).map(|c| table_cell_shown_style(wb, 0, &t, CellRef::new(r, c))).collect()).collect()
let e = shown_edges(wb.sheet(0).unwrap(), &t);
(0..ROWS).map(|r| (0..COLS).map(|c| table_cell_shown_style(wb, 0, &t, e, CellRef::new(r, c))).collect()).collect()
}
#[test]
@@ -210,6 +211,8 @@ fn frame_follows_the_last_visible_row_and_column() {
let mut s = session();
s.execute("home.hideRows", json!({"rows": "5:6"})).unwrap();
s.execute("home.hideColumns", json!({"cols": "E:E"})).unwrap();
let t = &s.doc().unwrap().wb.sheet(0).unwrap().tables[0];
assert_eq!(shown_edges(s.doc().unwrap().wb.sheet(0).unwrap(), t), ShownEdges { top: 0, bottom: 3, left: 0, right: 3 });
let g = shown_with(&s, "Medium2", |_| {});
for (c, cell) in g[3].iter().take(4).enumerate() {
assert_eq!(cell.borders.bottom, frame, "bottom frame on row 4, col {c}");
+1 -1
View File
@@ -450,7 +450,7 @@ fn center_across_end(start: u32, limit: u32, continues: impl Fn(u32) -> bool) ->
fn look(wb: &Workbook, si: usize, sh: &Sheet, c: CellRef, cf: &mut CfCache) -> (Look, Option<CfLook>) {
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