From 691883f99c65772ed3d25a6247f3e71b28c0f184 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 11 Oct 2026 20:56:16 +0200 Subject: [PATCH 01/76] Functions: VLOOKUP/HLOOKUP/LOOKUP/XLOOKUP keep an empty target blank (#374) A looked-up empty cell was turned into the number 0 inside the function, so ISBLANK saw FALSE and & / CONCATENATE inserted "0". The value now stays blank, like INDEX; a bare lookup still displays 0 as a formula's final result. Co-Authored-By: Claude Opus 5.5 --- crates/calc/src/tests.rs | 23 +++++++++++++++++++++++ crates/functions/src/lookup.rs | 22 ++++++++++------------ 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index 513340983..ac58b3d31 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -968,3 +968,26 @@ fn trim_references() { t.set("K20", "=SUM(Q1:.Q9)"); assert_eq!(t.get("K20"), Value::Error(CellError::Calc)); } + +#[test] +fn lookup_of_an_empty_cell_stays_blank() { + // #374: B1 (the value for key "k") is empty. + let mut t = T::new(); + t.set("A1", "k"); + t.set("A2", "j"); + t.set("B2", "v"); + for (at, f, want) in [ + ("D1", "=ISBLANK(VLOOKUP(\"k\",A1:B2,2,0))", Value::Bool(true)), + ("D2", "=\"x\"&VLOOKUP(\"k\",A1:B2,2,0)&\"y\"", Value::from("xy")), + ("D3", "=CONCATENATE(\"x\",VLOOKUP(\"k\",A1:B2,2,0),\"y\")", Value::from("xy")), + ("D4", "=ISBLANK(XLOOKUP(\"k\",A1:A2,B1:B2))", Value::Bool(true)), + ("D5", "=\"x\"&XLOOKUP(\"k\",A1:A2,B1:B2)&\"y\"", Value::from("xy")), + ("D6", "=ISBLANK(LOOKUP(\"k\",A1:A1,B1:B1))", Value::Bool(true)), + ("D7", "=ISBLANK(HLOOKUP(\"j\",A2:B3,2,0))", Value::Bool(true)), + // A bare lookup still shows 0, like a reference to an empty cell. + ("D8", "=VLOOKUP(\"k\",A1:B2,2,0)", Value::Number(0.0)), + ] { + t.set(at, f); + assert_eq!(t.get(at), want, "{f}"); + } +} diff --git a/crates/functions/src/lookup.rs b/crates/functions/src/lookup.rs index 56739c933..a04ce9960 100644 --- a/crates/functions/src/lookup.rs +++ b/crates/functions/src/lookup.rs @@ -119,11 +119,6 @@ fn row(a: &Array, r: usize) -> Vec { (0..a.cols).map(|c| a.get(r, c).cloned().unwrap_or(Value::Empty)).collect() } -/// A looked-up cell value: blanks read as 0 like a reference to an empty cell. -fn cell_result(v: Value) -> Value { - if matches!(v, Value::Empty) { Value::Number(0.0) } else { v } -} - // --------------------------------------------------------------------------------------------- // VLOOKUP / HLOOKUP / LOOKUP / MATCH @@ -146,7 +141,9 @@ fn vh_lookup(args: &[Arg], vertical: bool) -> R { let pos = if approx { bsearch_last(&x, &keys, false) } else { find_exact(&x, &keys, true, false) }; let pos = pos.ok_or(CellError::NA)?; let v = if vertical { table.get(pos, idx) } else { table.get(idx, pos) }; - Ok(cell_result(v.cloned().unwrap_or(Value::Empty))) + // An empty target stays blank, as in Excel: ISBLANK sees it as blank and `&` / + // CONCATENATE treat it as ""; only a formula's final result displays 0 (#374). + Ok(v.cloned().unwrap_or(Value::Empty)) } fn vlookup(a: &[Arg], _c: &mut dyn Ctx) -> R { @@ -179,18 +176,18 @@ fn lookup(args: &[Arg], _c: &mut dyn Ctx) -> R { } else { None }; - return v.map(cell_result).ok_or(CellError::NA); + return v.ok_or(CellError::NA); } // Array form: search the first row of a wide array, else the first column; return the // matching entry from the last row / column. if la.cols > la.rows { let keys = row(&la, 0); let pos = bsearch_last(&x, &keys, false).ok_or(CellError::NA)?; - Ok(cell_result(la.get(la.rows - 1, pos).cloned().unwrap_or(Value::Empty))) + Ok(la.get(la.rows - 1, pos).cloned().unwrap_or(Value::Empty)) } else { let keys = column(&la, 0); let pos = bsearch_last(&x, &keys, false).ok_or(CellError::NA)?; - Ok(cell_result(la.get(pos, la.cols - 1).cloned().unwrap_or(Value::Empty))) + Ok(la.get(pos, la.cols - 1).cloned().unwrap_or(Value::Empty)) } } @@ -311,13 +308,13 @@ fn xlookup(args: &[Arg], _c: &mut dyn Ctx) -> R { if vertical { let r = row(&ra, p); if r.len() == 1 { - return Ok(cell_result(r.into_iter().next().unwrap_or(Value::Empty))); + return Ok(r.into_iter().next().unwrap_or(Value::Empty)); } array_val(1, r.len(), r) } else { let c = column(&ra, p); if c.len() == 1 { - return Ok(cell_result(c.into_iter().next().unwrap_or(Value::Empty))); + return Ok(c.into_iter().next().unwrap_or(Value::Empty)); } array_val(c.len(), 1, c) } @@ -581,7 +578,8 @@ mod tests { is_err(ev("VLOOKUP", vec![n(4.0), rf(table()), n(2.0), b(false)]), CellError::NA); is_err(ev("VLOOKUP", vec![n(4.0), rf(table()), n(2.0), empty()]), CellError::NA); close(ev("VLOOKUP", vec![n(3.0), rf(table()), n(3.0), b(false)]), 30.0); - close(ev("VLOOKUP", vec![n(5.0), rf(table()), n(3.0), b(false)]), 0.0); + // An empty target stays blank (the cell displays 0 only as a final result) (#374). + assert_eq!(ev("VLOOKUP", vec![n(5.0), rf(table()), n(3.0), b(false)]), Value::Empty); is_err(ev("VLOOKUP", vec![n(3.0), rf(table()), n(4.0)]), CellError::Ref); is_err(ev("VLOOKUP", vec![n(3.0), rf(table()), n(0.0)]), CellError::Value); close(ev("VLOOKUP", vec![t("b*"), rf(names()), n(2.0), b(false)]), 2.0); From d2b9affbc519c54d438e3b6b74f930a0c3de6c8f Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 11 Oct 2026 20:59:09 +0200 Subject: [PATCH 02/76] Functions: test that arithmetic reads a blank lookup result as 0 (#374) Co-Authored-By: Claude Opus 5.5 --- crates/calc/src/tests.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index ac58b3d31..fe55dc3e1 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -986,6 +986,8 @@ fn lookup_of_an_empty_cell_stays_blank() { ("D7", "=ISBLANK(HLOOKUP(\"j\",A2:B3,2,0))", Value::Bool(true)), // A bare lookup still shows 0, like a reference to an empty cell. ("D8", "=VLOOKUP(\"k\",A1:B2,2,0)", Value::Number(0.0)), + // Arithmetic still reads the blank as 0. + ("D9", "=VLOOKUP(\"k\",A1:B2,2,0)+1", Value::Number(1.0)), ] { t.set(at, f); assert_eq!(t.get(at), want, "{f}"); From 423810750d8d43f732163d57267905a0110afe83 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Mon, 12 Oct 2026 19:18:01 +0200 Subject: [PATCH 03/76] Functions: don't assert ISBLANK(VLOOKUP/HLOOKUP/LOOKUP), unverified against Excel (#374) Co-Authored-By: Claude Sonnet 5.5 --- crates/calc/src/tests.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index fe55dc3e1..cfce701d1 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -977,13 +977,10 @@ fn lookup_of_an_empty_cell_stays_blank() { t.set("A2", "j"); t.set("B2", "v"); for (at, f, want) in [ - ("D1", "=ISBLANK(VLOOKUP(\"k\",A1:B2,2,0))", Value::Bool(true)), ("D2", "=\"x\"&VLOOKUP(\"k\",A1:B2,2,0)&\"y\"", Value::from("xy")), ("D3", "=CONCATENATE(\"x\",VLOOKUP(\"k\",A1:B2,2,0),\"y\")", Value::from("xy")), ("D4", "=ISBLANK(XLOOKUP(\"k\",A1:A2,B1:B2))", Value::Bool(true)), ("D5", "=\"x\"&XLOOKUP(\"k\",A1:A2,B1:B2)&\"y\"", Value::from("xy")), - ("D6", "=ISBLANK(LOOKUP(\"k\",A1:A1,B1:B1))", Value::Bool(true)), - ("D7", "=ISBLANK(HLOOKUP(\"j\",A2:B3,2,0))", Value::Bool(true)), // A bare lookup still shows 0, like a reference to an empty cell. ("D8", "=VLOOKUP(\"k\",A1:B2,2,0)", Value::Number(0.0)), // Arithmetic still reads the blank as 0. From 9977d850a4db34ebe2028ebe0320e027e43eb3af Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 17 Oct 2026 15:23:42 +0200 Subject: [PATCH 04/76] test(calc): Excel-verified blank results for VLOOKUP, HLOOKUP and LOOKUP (#374) Excel 365 for Windows (2609, build 16.0.20430.20032) keeps a lookup of an empty cell blank inside ISBLANK and concatenation for VLOOKUP, HLOOKUP and LOOKUP. The cases were checked in Excel by scottcampbelldata in vangel76/gridcraft#1. Co-Authored-By: scottcampbelldata <251750033+scottcampbelldata@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 --- crates/calc/src/tests.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index 4827b1bcc..357249f56 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -1193,6 +1193,24 @@ fn lookup_of_an_empty_cell_stays_blank() { } } +#[test] +fn blank_lookup_values_match_excel_windows_observation() { + // Excel 365 Windows 2609, build 16.0.20430.20032, black-box check 2026-10-13: + // A1="k", B1 and A2 genuinely empty. + // All three value lookups preserve blankness inside ISBLANK/concatenation. + let mut t = T::new(); + t.set("A1", "k"); + for (at, f, want) in [ + ("D1", "=ISBLANK(VLOOKUP(\"k\",A1:B1,2,FALSE))", Value::Bool(true)), + ("D2", "=ISBLANK(HLOOKUP(\"k\",A1:A2,2,FALSE))", Value::Bool(true)), + ("D3", "=ISBLANK(LOOKUP(\"k\",A1:A1,B1:B1))", Value::Bool(true)), + ("D4", "=\"x\"&HLOOKUP(\"k\",A1:A2,2,FALSE)&\"y\"", Value::from("xy")), + ] { + t.set(at, f); + assert_eq!(t.get(at), want, "{f}"); + } +} + #[test] fn cell_width_is_the_column_width_dialog_value_rounded() { // #735: Excel 365 values. Widths in px as `home.columnWidth {"chars"}` sets them From 4ab3edaf3b5a3ee540a76301039a80a79e063bb0 Mon Sep 17 00:00:00 2001 From: Higgs32584 Date: Wed, 21 Oct 2026 20:06:33 -0400 Subject: [PATCH 05/76] xlsx: escape shape font typeface attribute (#2170) Co-Authored-By: Claude Opus 5.5 --- crates/xlsx/src/drawing.rs | 2 +- crates/xlsx/src/tests/shape_text_rtl.rs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/crates/xlsx/src/drawing.rs b/crates/xlsx/src/drawing.rs index a6892f0b8..6fd2c22f3 100644 --- a/crates/xlsx/src/drawing.rs +++ b/crates/xlsx/src/drawing.rs @@ -471,7 +471,7 @@ fn run_props(f: &ShapeFormat, theme: &Theme) -> (String, String) { let _ = write!(body, "{c}"); } if let Some(font) = &f.font { - let _ = write!(body, "", esc(font)); + let _ = write!(body, "", esc_attr(font)); } (attrs, body) } diff --git a/crates/xlsx/src/tests/shape_text_rtl.rs b/crates/xlsx/src/tests/shape_text_rtl.rs index 36bdb861d..2ece04d31 100644 --- a/crates/xlsx/src/tests/shape_text_rtl.rs +++ b/crates/xlsx/src/tests/shape_text_rtl.rs @@ -84,3 +84,19 @@ fn shape_text_without_rtl_writes_unchanged_ppr() { let xml = written_drawing(&text_box("plain", ShapeFormat::default())); assert!(!xml.contains(" Date: Mon, 26 Oct 2026 22:22:22 +0100 Subject: [PATCH 06/76] perf(calc): index the pass's spill areas so reads don't scan every spill (#2683) A cell read and a range read looked at every spill computed so far in the pass, and cells_changed looked at every spill range of the sheet for each changed cell. A spatial index (16 x 16 cell blocks) now finds the areas covering a cell or meeting a range. Where areas overlap, the one that counted before (first/last in the pass's spill map order) still counts. Fixes #2683 Co-Authored-By: Claude Sonnet 5.5 --- crates/calc/src/lib.rs | 1 + crates/calc/src/recalc.rs | 152 +++++++++++++++++++--------- crates/calc/src/spill_index.rs | 175 +++++++++++++++++++++++++++++++++ crates/calc/src/tests.rs | 29 ++++++ 4 files changed, 310 insertions(+), 47 deletions(-) create mode 100644 crates/calc/src/spill_index.rs diff --git a/crates/calc/src/lib.rs b/crates/calc/src/lib.rs index 27c9e1961..c71d8b7b1 100644 --- a/crates/calc/src/lib.rs +++ b/crates/calc/src/lib.rs @@ -5,6 +5,7 @@ pub mod eval; pub mod recalc; +mod spill_index; mod trimrange; pub use eval::{Area, SPECIAL_FUNCTIONS, is_known_function}; diff --git a/crates/calc/src/recalc.rs b/crates/calc/src/recalc.rs index 17a0e6649..d86e73fa5 100644 --- a/crates/calc/src/recalc.rs +++ b/crates/calc/src/recalc.rs @@ -45,6 +45,7 @@ use gridcraft_formula::Expr; use gridcraft_model::{CalcMode, Workbook}; use crate::eval::{Area, Evaluator, Host, precedents, stack_position}; +use crate::spill_index::{Cover, SpillIndex}; pub type Key = (usize, CellRef); @@ -393,6 +394,8 @@ struct PassHost<'a> { in_progress: HashSet, /// New spills found during the pass: anchor → array. spills: HashMap>, + /// Where the areas in `spills` are, so a read finds the ones it meets without scanning them all. + spill_index: SpillIndex, /// Used range per sheet: the workbook doesn't change during a pass, and working it out walks /// every row, so once per sheet rather than once per range read. used: HashMap>, @@ -447,6 +450,7 @@ impl<'a> PassHost<'a> { pending: exprs.keys().copied().collect(), in_progress: HashSet::default(), spills: HashMap::default(), + spill_index: SpillIndex::default(), used: HashMap::default(), rng, now, @@ -472,6 +476,33 @@ impl<'a> PassHost<'a> { } } +/// Most spill areas a range read meets before they are checked for overlapping one another by +/// walking all of the pass's spills instead of comparing pairs. +/// Changed cells (in one `cells_changed`) from which the sheet's spill areas are indexed. +const INDEX_SPILLS_FROM: usize = 16; + +const MAX_UNORDERED_HITS: usize = 16; + +/// Whether an array at `anchor` covers `c`. +fn spill_covers(anchor: CellRef, arr: &Array, c: CellRef) -> bool { + c.row >= anchor.row && c.col >= anchor.col && ((c.row - anchor.row) as usize) < arr.rows && ((c.col - anchor.col) as usize) < arr.cols +} + +/// Whether two of the spill areas anchored at `hits` share a cell inside `range`. +fn spill_hits_overlap(index: &SpillIndex, sheet: usize, hits: &[CellRef], range: RangeRef) -> bool { + let rects: Vec = hits.iter().filter_map(|a| index.area(sheet, *a)).filter(|(_, empty)| !empty).map(|(r, _)| r).collect(); + rects.iter().enumerate().any(|(i, a)| { + rects.iter().skip(i + 1).any(|b| { + a.intersects(b) + && RangeRef::new( + CellRef::new(a.start.row.max(b.start.row), a.start.col.max(b.start.col)), + CellRef::new(a.end.row.min(b.end.row), a.end.col.min(b.end.col)), + ) + .intersects(&range) + }) + }) +} + /// Whether an array result at `k` can't spill: it is in a table, or its area runs off the sheet, /// overlaps merged cells, holds other cells, or another formula's spill (unless that formula is /// recalculated in this pass, and so may spill elsewhere now). @@ -659,6 +690,7 @@ impl PassHost<'_> { // value -- a FILTER matching one row, a SORT or UNIQUE over one item -- so `C1#` // is the 1 x 1 range and `ROWS(C1#)` is 1, as in Excel (#1491). A formula that // returns a scalar never reaches this arm, so it does not become an anchor. + self.spill_index.insert(k, a.rows, a.cols); self.spills.insert(k, a.clone()); a.data.first().cloned().unwrap_or_default() } @@ -669,6 +701,7 @@ impl PassHost<'_> { let v = match self.spills.get(&k) { Some(a) if spill_blocked(self, k, a) => { self.spills.remove(&k); + self.spill_index.remove(k); Value::Error(CellError::Spill) } _ => v, @@ -715,7 +748,11 @@ impl PassHost<'_> { while passes < max_iterations.min(max_passes) && !self.converged(max_change) { self.prev = again.iter().filter_map(|k| self.results.remove(k).map(|v| (*k, v))).collect(); self.pending = set.clone(); - self.spills.retain(|a, _| !set.contains(a)); + let gone: Vec = self.spills.keys().filter(|a| set.contains(a)).copied().collect(); + for a in gone { + self.spills.remove(&a); + self.spill_index.remove(a); + } for k in &again { self.calculate(*k); } @@ -799,17 +836,19 @@ impl Host for PassHost<'_> { if self.pending.contains(&k) || self.results.contains_key(&k) { return self.compute(k); } - // A cell covered by a spill computed in this pass. - for (anchor, arr) in &self.spills { - if anchor.0 == sheet && c.row >= anchor.1.row && c.col >= anchor.1.col { - let (dr, dc) = ((c.row - anchor.1.row) as usize, (c.col - anchor.1.col) as usize); - if dr < arr.rows && dc < arr.cols { - if self.iterate && self.tainted.contains(anchor) { - self.taint = true; - } - return arr.get(dr, dc).cloned().unwrap_or_default(); - } + // A cell covered by a spill computed in this pass. Where several overlap, the first in + // `spills`' iteration order counts, as it always has. + let covering = match self.spill_index.covering(sheet, c) { + Cover::None => None, + Cover::One(a) => self.spills.get_key_value(&(sheet, a)), + Cover::Many => self.spills.iter().find(|(a, arr)| a.0 == sheet && spill_covers(a.1, arr, c)), + }; + if let Some((anchor, arr)) = covering { + let (dr, dc) = ((c.row - anchor.1.row) as usize, (c.col - anchor.1.col) as usize); + if self.iterate && self.tainted.contains(anchor) { + self.taint = true; } + return arr.get(dr, dc).cloned().unwrap_or_default(); } self.wb.sheet(sheet).map(|s| s.value(c)).unwrap_or_default() } @@ -845,41 +884,44 @@ impl Host for PassHost<'_> { } } if !self.spills.is_empty() { - let new: Vec<(usize, Value)> = self - .spills - .iter() - .filter(|(a, _)| a.0 == sheet) - .flat_map(|(a, arr)| { - let mut v = Vec::new(); - for r in 0..arr.rows { - for cc in 0..arr.cols { - let c = CellRef::new(a.1.row.saturating_add(r as u32), a.1.col.saturating_add(cc as u32)); - if c != a.1 && range.contains(c) { - v.push(( - (c.row - range.start.row) as usize * w + (c.col - range.start.col) as usize, - arr.get(r, cc).cloned().unwrap_or_default(), - )); - } - } - } - v - }) - .collect(); + let mut hits = Vec::new(); + self.spill_index.meeting(sheet, range, &mut hits); // Iterating: an array spilled by a formula that read a previous value. - if self.iterate - && self.spills.iter().any(|(a, arr)| { - let end = CellRef::new( - a.1.row.saturating_add((arr.rows as u32).saturating_sub(1)), - a.1.col.saturating_add((arr.cols as u32).saturating_sub(1)), - ); - a.0 == sheet && self.tainted.contains(a) && RangeRef::new(a.1, end).intersects(&range) - }) - { + if self.iterate && hits.iter().any(|a| self.tainted.contains(&(sheet, *a))) { self.taint = true; } - for (i, v) in new { - if let Some(slot) = out.get_mut(i) { - *slot = v; + // Where areas overlap inside the range, the last in `spills`' iteration order wins, + // as it always has; otherwise the order doesn't matter. + let ordered = hits.len() > MAX_UNORDERED_HITS || spill_hits_overlap(&self.spill_index, sheet, &hits, range); + let mut put = |anchor: CellRef, arr: &Array| { + if arr.rows == 0 || arr.cols == 0 { + return; + } + let (r0, c0) = (range.start.row.max(anchor.row), range.start.col.max(anchor.col)); + let r1 = range.end.row.min(anchor.row.saturating_add((arr.rows as u32).saturating_sub(1))); + let c1 = range.end.col.min(anchor.col.saturating_add((arr.cols as u32).saturating_sub(1))); + for r in r0..=r1 { + for cc in c0..=c1 { + if (r, cc) != (anchor.row, anchor.col) + && let Some(slot) = out.get_mut((r - range.start.row) as usize * w + (cc - range.start.col) as usize) + { + *slot = arr.get((r - anchor.row) as usize, (cc - anchor.col) as usize).cloned().unwrap_or_default(); + } + } + } + }; + if ordered { + let wanted: HashSet = hits.iter().map(|a| (sheet, *a)).collect(); + for (a, arr) in &self.spills { + if wanted.contains(a) { + put(a.1, arr); + } + } + } else { + for a in &hits { + if let Some(arr) = self.spills.get(&(sheet, *a)) { + put(*a, arr); + } } } } @@ -898,7 +940,7 @@ impl Host for PassHost<'_> { } // Spilled values sit between the stored cells (and the formulas just calculated may have // produced some): leave such blocks to `range_values`. - if self.spills.keys().any(|a| a.0 == sheet) || sh.spill.range(range.start..=range.end).any(|(c, _)| range.contains(*c)) { + if self.spill_index.has_sheet(sheet) || sh.spill.range(range.start..=range.end).any(|(c, _)| range.contains(*c)) { return false; } for (c, cell) in sh.cells.iter_range(range) { @@ -1124,11 +1166,27 @@ impl Calc { let mut emptied: Vec = Vec::new(); // Typing into (or clearing) a cell a dynamic array spills over (or would spill over) // re-evaluates the anchor, which may now be blocked or unblocked. + // With many changed cells (a paste) the sheet's spill areas are indexed once instead of + // being walked for each one. + let mut areas = SpillIndex::default(); + let mut indexed: HashSet = HashSet::default(); + let mut at = Vec::new(); for &k in changed { if let Some(sh) = wb.sheet(k.0) { - for (anchor, r) in &sh.spill_ranges { - if *anchor != k.1 && r.contains(k.1) { - seeds.push((k.0, *anchor)); + if changed.len() > INDEX_SPILLS_FROM && !sh.spill_ranges.is_empty() { + if indexed.insert(k.0) { + for (anchor, r) in &sh.spill_ranges { + areas.insert_rect((k.0, *anchor), *r, false); + } + } + // Anchor order, as walking `spill_ranges` gives. + areas.meeting(k.0, RangeRef::new(k.1, k.1), &mut at); + seeds.extend(at.iter().filter(|a| **a != k.1).map(|a| (k.0, *a))); + } else { + for (anchor, r) in &sh.spill_ranges { + if *anchor != k.1 && r.contains(k.1) { + seeds.push((k.0, *anchor)); + } } } for (c, cell) in sh.cells.iter_range(RangeRef::new(CellRef::new(k.1.row.saturating_sub(64), k.1.col.saturating_sub(64)), k.1)) { diff --git a/crates/calc/src/spill_index.rs b/crates/calc/src/spill_index.rs new file mode 100644 index 000000000..9a50996a3 --- /dev/null +++ b/crates/calc/src/spill_index.rs @@ -0,0 +1,175 @@ +//! A spatial index over spill areas: which areas cover a cell, and which meet a range, without +//! looking at every area. Areas are registered in 16 x 16 cell blocks, so a lookup touches one +//! block (a cell) or the blocks a range covers (a range) rather than every spill on the sheet. + +use crate::recalc::{FxHasher, Key}; +use gridcraft_core::{CellRef, RangeRef}; +use std::hash::BuildHasherDefault; + +type HashMap = std::collections::HashMap>; + +/// Block edge in cells (rows and columns). +const BLOCK: u32 = 16; + +/// One indexed area: its rectangle, and whether it covers no cells (an array with no rows or no +/// columns still meets the ranges its anchor is in, as a 1 x 1 area does for taint tracking). +#[derive(Clone, Copy)] +struct Entry { + rect: RangeRef, + empty: bool, +} + +#[derive(Default)] +struct SheetAreas { + entries: HashMap, + blocks: HashMap<(u32, u32), Vec>, +} + +/// What covers a cell. +pub(crate) enum Cover { + None, + One(CellRef), + /// Several areas overlap here: the caller decides which one counts. + Many, +} + +#[derive(Default)] +pub(crate) struct SpillIndex { + sheets: HashMap, +} + +fn block_span(rect: &RangeRef) -> (u32, u32, u32, u32) { + (rect.start.row / BLOCK, rect.end.row / BLOCK, rect.start.col / BLOCK, rect.end.col / BLOCK) +} + +impl SpillIndex { + /// Whether any area is indexed on `sheet`. + pub(crate) fn has_sheet(&self, sheet: usize) -> bool { + self.sheets.get(&sheet).is_some_and(|s| !s.entries.is_empty()) + } + + /// Indexes the area an array of `rows` x `cols` anchored at `anchor` covers (replacing the + /// one already indexed for that anchor). + pub(crate) fn insert(&mut self, (sheet, anchor): Key, rows: usize, cols: usize) { + let end = + CellRef::new(anchor.row.saturating_add((rows as u32).saturating_sub(1)), anchor.col.saturating_add((cols as u32).saturating_sub(1))); + self.insert_rect((sheet, anchor), RangeRef::new(anchor, end), rows == 0 || cols == 0); + } + + /// Indexes an area given as a range. + pub(crate) fn insert_rect(&mut self, (sheet, anchor): Key, rect: RangeRef, empty: bool) { + self.remove((sheet, anchor)); + let areas = self.sheets.entry(sheet).or_default(); + let (r0, r1, c0, c1) = block_span(&rect); + for br in r0..=r1 { + for bc in c0..=c1 { + areas.blocks.entry((br, bc)).or_default().push(anchor); + } + } + areas.entries.insert(anchor, Entry { rect, empty }); + } + + pub(crate) fn remove(&mut self, (sheet, anchor): Key) { + let Some(areas) = self.sheets.get_mut(&sheet) else { return }; + let Some(old) = areas.entries.remove(&anchor) else { return }; + let (r0, r1, c0, c1) = block_span(&old.rect); + for br in r0..=r1 { + for bc in c0..=c1 { + if let Some(list) = areas.blocks.get_mut(&(br, bc)) { + list.retain(|a| *a != anchor); + if list.is_empty() { + areas.blocks.remove(&(br, bc)); + } + } + } + } + } + + /// The areas that cover `c`. + pub(crate) fn covering(&self, sheet: usize, c: CellRef) -> Cover { + let Some(areas) = self.sheets.get(&sheet) else { return Cover::None }; + let Some(list) = areas.blocks.get(&(c.row / BLOCK, c.col / BLOCK)) else { return Cover::None }; + let mut found = Cover::None; + for a in list { + if let Some(e) = areas.entries.get(a) + && !e.empty + && e.rect.contains(c) + { + found = match found { + Cover::None => Cover::One(*a), + _ => return Cover::Many, + }; + } + } + found + } + + /// The anchors of the areas whose rectangle meets `range` (an empty area counts through its + /// anchor cell), sorted by anchor. + pub(crate) fn meeting(&self, sheet: usize, range: RangeRef, out: &mut Vec) { + out.clear(); + let Some(areas) = self.sheets.get(&sheet) else { return }; + let (r0, r1, c0, c1) = block_span(&range); + let touched = u64::from(r1 - r0 + 1) * u64::from(c1 - c0 + 1); + if touched > 4 * areas.entries.len() as u64 + 16 { + // A range wider than the areas are many: look at each area once. + out.extend(areas.entries.iter().filter(|(_, e)| e.rect.intersects(&range)).map(|(a, _)| *a)); + } else { + for br in r0..=r1 { + for bc in c0..=c1 { + if let Some(list) = areas.blocks.get(&(br, bc)) { + out.extend(list.iter().copied().filter(|a| areas.entries.get(a).is_some_and(|e| e.rect.intersects(&range)))); + } + } + } + } + out.sort_unstable(); + out.dedup(); + } + + /// The rectangle indexed for `anchor` and whether it covers no cells. + pub(crate) fn area(&self, sheet: usize, anchor: CellRef) -> Option<(RangeRef, bool)> { + self.sheets.get(&sheet)?.entries.get(&anchor).map(|e| (e.rect, e.empty)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn at(r: u32, c: u32) -> CellRef { + CellRef::new(r, c) + } + + #[test] + fn finds_the_areas_covering_a_cell_and_meeting_a_range() { + let mut ix = SpillIndex::default(); + ix.insert((0, at(0, 0)), 40, 3); + ix.insert((0, at(30, 1)), 1, 40); + ix.insert((1, at(0, 0)), 5, 5); + assert!(matches!(ix.covering(0, at(39, 2)), Cover::One(a) if a == at(0, 0))); + assert!(matches!(ix.covering(0, at(30, 2)), Cover::Many)); + assert!(matches!(ix.covering(0, at(40, 0)), Cover::None)); + assert!(matches!(ix.covering(2, at(0, 0)), Cover::None)); + let mut hits = Vec::new(); + ix.meeting(0, RangeRef::new(at(30, 30), at(50, 60)), &mut hits); + assert_eq!(hits, vec![at(30, 1)]); + ix.meeting(0, RangeRef::new(at(0, 0), at(1_000_000, 16_000)), &mut hits); + assert_eq!(hits, vec![at(0, 0), at(30, 1)]); + ix.remove((0, at(30, 1))); + assert!(matches!(ix.covering(0, at(30, 2)), Cover::One(_))); + assert!(ix.has_sheet(1) && !ix.has_sheet(2)); + ix.insert((0, at(0, 0)), 1, 1); + assert!(matches!(ix.covering(0, at(39, 2)), Cover::None)); + } + + #[test] + fn an_area_without_cells_covers_none_but_is_met_at_its_anchor() { + let mut ix = SpillIndex::default(); + ix.insert((0, at(3, 3)), 0, 4); + assert!(matches!(ix.covering(0, at(3, 3)), Cover::None)); + let mut hits = Vec::new(); + ix.meeting(0, RangeRef::new(at(3, 3), at(3, 3)), &mut hits); + assert_eq!(hits, vec![at(3, 3)]); + } +} diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index 5b9e90085..a90a997ec 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -2274,3 +2274,32 @@ fn loan_schedule_residue_keeps_excels_sign() { } assert_eq!(t.get("P7"), Value::Text("".into())); } + +#[test] +fn many_spills_are_read_and_blocked_by_a_paste() { + let mut t = T::new(); + // 40 arrays of 3 rows side by side in rows 1-3, and a reader of a range over two of them. + for i in 0..40u32 { + let anchor = CellRef::new(0, 2 * i); + t.wb.sheet_mut(0).unwrap().cells.set(anchor, Cell::formula(Formula::new(&format!("=SEQUENCE(3,1,{})", 10 * i)))); + } + t.set("A10", "=SUM(C1:F3)"); + t.set("A12", "=COUNT(C1:F3)"); + t.set("A11", "=C2*1"); + t.calc.recalc_all(&mut t.wb); + assert_eq!(t.num("A10"), (10 + 11 + 12 + 20 + 21 + 22) as f64); + assert_eq!(t.num("A11"), 11.0); + assert_eq!(t.num("A12"), 6.0); + // Pasting 20 cells (more than one spill's width worth, so the changed set is indexed) over + // the second row of every other array blocks those arrays; the rest are unchanged. + let changed: Vec<(usize, CellRef)> = (0..20u32).map(|i| (0, CellRef::new(1, 4 * i))).collect(); + for k in &changed { + t.wb.sheet_mut(0).unwrap().cells.set(k.1, Cell::value(Value::Number(1.0))); + } + t.calc.cells_changed(&mut t.wb, &changed); + assert_eq!(t.get("E1"), Value::Error(CellError::Spill)); + assert_eq!(t.get("C1"), Value::Number(10.0)); + assert_eq!(t.get("G2"), Value::Number(31.0)); + assert_eq!(t.get("A10"), Value::Error(CellError::Spill)); + assert_eq!(t.num("A12"), 4.0); +} From 72236e4aab3cdf94123023d00307e08a23a9c0aa Mon Sep 17 00:00:00 2001 From: vangel76 Date: Mon, 26 Oct 2026 22:26:15 +0100 Subject: [PATCH 07/76] perf(calc): find the formulas a cleared cell may unblock from a set of #SPILL! cells (#2684) Clearing a cell walked every formula in the graph, and each changed cell scanned a 64 x 64 window of stored cells, to find formulas showing #SPILL!. Calc now keeps the set of cells whose value is #SPILL! (filled in rebuild, kept up to date by cells_changed and the write-back of a run) and takes those candidates from it. The same set replaces the walk over the graph in run for areas a formula stopped spilling over. Each entry is checked against the workbook before it is used. Fixes #2684 Co-Authored-By: Claude Sonnet 5.5 --- crates/calc/src/recalc.rs | 80 ++++++++++++++++++++++++++++----------- crates/calc/src/tests.rs | 28 ++++++++++++++ 2 files changed, 85 insertions(+), 23 deletions(-) diff --git a/crates/calc/src/recalc.rs b/crates/calc/src/recalc.rs index 17a0e6649..4a90a6775 100644 --- a/crates/calc/src/recalc.rs +++ b/crates/calc/src/recalc.rs @@ -377,11 +377,23 @@ pub struct Calc { /// Cells whose formulas have errors from cycles. pub circular: Vec, pub last_recalc_cells: usize, + /// The cells whose value is `#SPILL!`, kept so that finding the formulas an emptied cell or a + /// freed spill area may unblock doesn't walk every formula or window of cells. Filled in + /// `rebuild`, kept up to date by `cells_changed` and by the write-back of a run; an entry is + /// always checked against the workbook before it is used. + spill_errors: BTreeSet, } impl Default for Calc { fn default() -> Self { - Calc { graph: Graph::default(), rng: 0x9E37_79B9_7F4A_7C15, fixed_now: None, circular: vec![], last_recalc_cells: 0 } + Calc { + graph: Graph::default(), + rng: 0x9E37_79B9_7F4A_7C15, + fixed_now: None, + circular: vec![], + last_recalc_cells: 0, + spill_errors: BTreeSet::new(), + } } } @@ -1007,8 +1019,12 @@ impl Calc { pub fn rebuild(&mut self, wb: &Workbook) { self.graph = Graph::default(); let mut circ = Vec::new(); + self.spill_errors.clear(); for (si, sheet) in wb.sheets.iter().enumerate() { for (c, cell) in sheet.cells.iter() { + if cell.value == Value::Error(CellError::Spill) { + self.spill_errors.insert((si, c)); + } if let Some(f) = &cell.formula && let Some(e) = f.expr() { @@ -1066,6 +1082,25 @@ impl Calc { out } + /// The cells of `sheet` in `rows` x `cols` (inclusive, `cols` a lower and an upper column) that + /// show `#SPILL!`, in row-major order. + fn spill_errors_in(&self, wb: &Workbook, sheet: usize, rows: (u32, u32), cols: (u32, u32)) -> Vec { + let Some(sh) = wb.sheet(sheet) else { return Vec::new() }; + self.spill_errors + .range((sheet, CellRef::new(rows.0, 0))..=(sheet, CellRef::new(rows.1, u32::MAX))) + .filter(|(_, c)| c.col >= cols.0 && c.col <= cols.1) + .filter(|(_, c)| sh.cells.get(*c).is_some_and(|x| x.value == Value::Error(CellError::Spill))) + .map(|(_, c)| *c) + .collect() + } + + /// The formulas showing `#SPILL!` on `sheet` at or above and to the left of `end`: what a + /// cell emptied or a spill area freed there may unblock. + fn unblock_candidates(&self, wb: &Workbook, sheet: usize, end: CellRef) -> Vec { + let cells = self.spill_errors_in(wb, sheet, (0, end.row), (0, end.col)); + cells.into_iter().filter(|c| self.graph.nodes.contains_key(&(sheet, *c))).map(|c| (sheet, c)).collect() + } + /// Recalculates every formula (F9 / Ctrl+Alt+F9, and after loading). pub fn recalc_all(&mut self, wb: &mut Workbook) { self.rebuild(wb); @@ -1087,6 +1122,11 @@ impl Calc { pub fn cells_changed(&mut self, wb: &mut Workbook, changed: &[Key]) { let mut cleared: Vec<(usize, RangeRef)> = Vec::new(); for &k in changed { + if wb.sheet(k.0).and_then(|s| s.cell(k.1)).is_some_and(|c| c.value == Value::Error(CellError::Spill)) { + self.spill_errors.insert(k); + } else { + self.spill_errors.remove(&k); + } let formula = wb.sheet(k.0).and_then(|s| s.cell(k.1)).and_then(|c| c.formula.clone()); match formula.and_then(|f| f.expr()) { Some(e) => { @@ -1109,6 +1149,7 @@ impl Calc { && cell.formula.is_some() { cell.value = Value::Error(CellError::Name); + self.spill_errors.remove(&k); } } } @@ -1131,10 +1172,9 @@ impl Calc { seeds.push((k.0, *anchor)); } } - for (c, cell) in sh.cells.iter_range(RangeRef::new(CellRef::new(k.1.row.saturating_sub(64), k.1.col.saturating_sub(64)), k.1)) { - if c != k.1 && cell.value == Value::Error(CellError::Spill) { - seeds.push((k.0, c)); - } + if !self.spill_errors.is_empty() { + let (r0, c0) = (k.1.row.saturating_sub(64), k.1.col.saturating_sub(64)); + seeds.extend(self.spill_errors_in(wb, k.0, (r0, k.1.row), (c0, k.1.col)).into_iter().filter(|c| *c != k.1).map(|c| (k.0, c))); } if !sh.cells.has(k.1) { match emptied.iter_mut().find(|x| x.0 == k.0) { @@ -1146,11 +1186,7 @@ impl Calc { } // An emptied cell can unblock an anchor any distance above or to the left of it. for (si, end) in emptied { - for &(s, c) in self.graph.nodes.keys() { - if s == si && c.row <= end.row && c.col <= end.col && wb.sheet(s).is_some_and(|sh| sh.value(c) == Value::Error(CellError::Spill)) { - seeds.push((s, c)); - } - } + seeds.extend(self.unblock_candidates(wb, si, end)); } // Spill areas of changed anchors also change. for &k in changed { @@ -1254,10 +1290,15 @@ impl Calc { let mut freed: Vec<(usize, RangeRef)> = Vec::new(); for (k, v) in results { let Some(sheet) = wb.sheet_mut(k.0) else { continue }; - if let Some(cell) = sheet.cells.get_mut(k.1) - && cell.value != v - { - cell.value = v; + if let Some(cell) = sheet.cells.get_mut(k.1) { + if v == Value::Error(CellError::Spill) { + self.spill_errors.insert(k); + } else { + self.spill_errors.remove(&k); + } + if cell.value != v { + cell.value = v; + } } } for k in &dirty { @@ -1293,6 +1334,7 @@ impl Calc { if blocked { if let Some(cell) = sheet.cells.get_mut(k.1) { cell.value = Value::Error(CellError::Spill); + self.spill_errors.insert(*k); } // Not blocked while the pass ran (it would have been #SPILL! there), so // blocked by another array of this pass: the formulas that read this one, @@ -1338,15 +1380,7 @@ impl Calc { } // An area a formula no longer spills over may unblock another formula's array. for (si, o) in freed { - for &(s, c) in self.graph.nodes.keys() { - if s == si - && c.row <= o.end.row - && c.col <= o.end.col - && wb.sheet(s).is_some_and(|sh| sh.value(c) == Value::Error(CellError::Spill)) - { - spill_changes.push((s, c)); - } - } + spill_changes.extend(self.unblock_candidates(wb, si, o.end)); } if full { // Everything was already evaluated once; spill dependents need another pass. diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index 5b9e90085..fa8aab3ca 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -2274,3 +2274,31 @@ fn loan_schedule_residue_keeps_excels_sign() { } assert_eq!(t.get("P7"), Value::Text("".into())); } + +#[test] +fn clearing_a_cell_unblocks_the_arrays_above_and_left_of_it() { + let mut t = T::new(); + for r in 1..=50 { + t.set(&format!("D{r}"), &format!("={r}*2")); + } + t.set("B2", "7"); + t.set("B1", "=SEQUENCE(3)"); + t.set("C1", "=SEQUENCE(1,3)"); + t.set("A20", "=B1+1"); + assert_eq!(t.get("B1"), Value::Error(CellError::Spill)); + assert_eq!(t.get("A20"), Value::Error(CellError::Spill)); + // Emptied: the array above it spills. + t.wb.sheet_mut(0).unwrap().cells.remove(c("B2")); + t.calc.cells_changed(&mut t.wb, &[(0, c("B2"))]); + assert_eq!(t.num("B3"), 3.0); + assert_eq!(t.num("A20"), 2.0); + // Typing over the area blocks it again; far from it, nothing is disturbed. + t.set("B3", "9"); + assert_eq!(t.get("B1"), Value::Error(CellError::Spill)); + t.set("D50", "1"); + assert_eq!(t.get("B1"), Value::Error(CellError::Spill)); + assert_eq!(t.num("D49"), 98.0); + // Clearing it again frees the array. + t.set("B3", ""); + assert_eq!(t.num("B3"), 3.0); +} From 5f3fcc8140670454146044ddce955cac7f18539c Mon Sep 17 00:00:00 2001 From: vangel76 Date: Tue, 27 Oct 2026 19:45:28 +0100 Subject: [PATCH 08/76] perf(calc): look up #SPILL! cells by row and column range, not by row band The spill-error set was ranged by rows only and filtered by column afterwards, so an edit in column A walked every #SPILL! cell to its right in the same rows. Each row now keeps its own column set and a window lookup takes one column range per row that has an entry. Results are unchanged. Also covers an array spilling right over the cleared cell. Co-Authored-By: Claude Sonnet 5.5 --- crates/calc/src/recalc.rs | 51 ++++++++++++++++++++++++++++++++------- crates/calc/src/tests.rs | 48 ++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 9 deletions(-) diff --git a/crates/calc/src/recalc.rs b/crates/calc/src/recalc.rs index d139486c1..ce01c2984 100644 --- a/crates/calc/src/recalc.rs +++ b/crates/calc/src/recalc.rs @@ -10,7 +10,7 @@ //! and the formulas that read one are evaluated again until the loop settles //! (`PassHost::iterate_loops`). -use std::collections::{BTreeSet, VecDeque}; +use std::collections::{BTreeMap, BTreeSet, VecDeque}; use std::hash::{BuildHasherDefault, Hasher}; use gridcraft_core::{Array, CellError, CellRef, RangeRef, Value}; @@ -42,7 +42,45 @@ pub struct Calc { /// freed spill area may unblock doesn't walk every formula or window of cells. Filled in /// `rebuild`, kept up to date by `cells_changed` and by the write-back of a run; an entry is /// always checked against the workbook before it is used. - spill_errors: BTreeSet, + spill_errors: SpillErrors, +} + +/// A set of cells by row, each row's columns in their own set: a window of rows and columns is +/// found by one column range per row that has an entry, so the cost follows the cells in the +/// window, not those to the side of it in the same rows. +#[derive(Clone, Debug, Default)] +struct SpillErrors { + rows: BTreeMap<(usize, u32), BTreeSet>, +} + +impl SpillErrors { + fn is_empty(&self) -> bool { + self.rows.is_empty() + } + + fn clear(&mut self) { + self.rows.clear(); + } + + fn insert(&mut self, (sheet, c): Key) { + self.rows.entry((sheet, c.row)).or_default().insert(c.col); + } + + fn remove(&mut self, (sheet, c): &Key) { + if let Some(cols) = self.rows.get_mut(&(*sheet, c.row)) { + cols.remove(&c.col); + if cols.is_empty() { + self.rows.remove(&(*sheet, c.row)); + } + } + } + + /// The cells of `sheet` in `rows` x `cols` (both inclusive), in row-major order. + fn range(&self, sheet: usize, rows: (u32, u32), cols: (u32, u32)) -> impl Iterator + '_ { + self.rows + .range((sheet, rows.0)..=(sheet, rows.1)) + .flat_map(move |(&(_, row), set)| set.range(cols.0..=cols.1).map(move |&col| CellRef::new(row, col))) + } } impl Default for Calc { @@ -53,7 +91,7 @@ impl Default for Calc { fixed_now: None, circular: vec![], last_recalc_cells: 0, - spill_errors: BTreeSet::new(), + spill_errors: SpillErrors::default(), } } } @@ -205,12 +243,7 @@ impl Calc { /// show `#SPILL!`, in row-major order. fn spill_errors_in(&self, wb: &Workbook, sheet: usize, rows: (u32, u32), cols: (u32, u32)) -> Vec { let Some(sh) = wb.sheet(sheet) else { return Vec::new() }; - self.spill_errors - .range((sheet, CellRef::new(rows.0, 0))..=(sheet, CellRef::new(rows.1, u32::MAX))) - .filter(|(_, c)| c.col >= cols.0 && c.col <= cols.1) - .filter(|(_, c)| sh.cells.get(*c).is_some_and(|x| x.value == Value::Error(CellError::Spill))) - .map(|(_, c)| *c) - .collect() + self.spill_errors.range(sheet, rows, cols).filter(|c| sh.cells.get(*c).is_some_and(|x| x.value == Value::Error(CellError::Spill))).collect() } /// The formulas showing `#SPILL!` on `sheet` at or above and to the left of `end`: what a diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index fa8aab3ca..9de680e2f 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -2302,3 +2302,51 @@ fn clearing_a_cell_unblocks_the_arrays_above_and_left_of_it() { t.set("B3", ""); assert_eq!(t.num("B3"), 3.0); } + +/// A sheet with `#SPILL!` cells in rows 0..64, columns 100..16384 (as blocked arrays leave), none +/// near column A. +fn spill_error_field() -> T { + let mut t = T::new(); + let sh = t.wb.sheet_mut(0).unwrap(); + for r in 0..64 { + for col in 100..16_384 { + sh.cells.set(CellRef::new(r, col), Cell::value(Value::Error(CellError::Spill))); + } + } + t.calc.recalc_all(&mut t.wb); + t +} + +#[test] +fn an_edit_far_left_of_many_spill_errors_does_not_disturb_them() { + let mut t = spill_error_field(); + t.set("A65", "5"); + assert_eq!(t.num("A65"), 5.0); + assert_eq!(t.get("CW1"), Value::Error(CellError::Spill)); + assert_eq!(t.get("XFD64"), Value::Error(CellError::Spill)); + // A blocked formula inside the window is still found when a cell to its right is cleared. + t.set("DA66", "7"); + t.set("CZ66", "=SEQUENCE(1,3)"); + assert_eq!(t.get("CZ66"), Value::Error(CellError::Spill)); + t.wb.sheet_mut(0).unwrap().cells.remove(c("DA66")); + t.calc.cells_changed(&mut t.wb, &[(0, c("DA66"))]); + assert_eq!(t.num("DA66"), 2.0); +} + +/// Probe: single-cell edits in column A below a block of `#SPILL!` cells to its right. +/// `cargo test -p gridcraft-calc --release -- --ignored --nocapture spill_error_field_probe` +#[test] +#[ignore] +#[allow(clippy::disallowed_methods)] +fn spill_error_field_probe() { + let mut t = spill_error_field(); + let mut best = std::time::Duration::MAX; + for i in 0..9 { + let start = std::time::Instant::now(); + for _ in 0..100 { + t.set("A65", &format!("{i}")); + } + best = best.min(start.elapsed()); + } + eprintln!("100 edits of A65 beside 1,040,000 spill errors: best of 9 = {best:?}"); +} From 6c912d2eb130130f93d9281baca435a37e5d30e7 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Thu, 29 Oct 2026 23:48:11 +0100 Subject: [PATCH 09/76] perf(calc): keep tall and wide spill areas out of the block index A full-height column spill took 65,536 block registrations, on every pass that produced it. Areas over 1024 blocks are now tested directly by each lookup instead. Adds a randomized comparison against a scan of all areas. Co-Authored-By: Claude Sonnet 5.5 --- crates/calc/src/spill_index.rs | 121 +++++++++++++++++++++++++++++---- crates/calc/src/tests.rs | 21 ++++++ 2 files changed, 127 insertions(+), 15 deletions(-) diff --git a/crates/calc/src/spill_index.rs b/crates/calc/src/spill_index.rs index 7cf2d44fe..1bbf0f9c0 100644 --- a/crates/calc/src/spill_index.rs +++ b/crates/calc/src/spill_index.rs @@ -1,6 +1,9 @@ //! A spatial index over spill areas: which areas cover a cell, and which meet a range, without //! looking at every area. Areas are registered in 16 x 16 cell blocks, so a lookup touches one //! block (a cell) or the blocks a range covers (a range) rather than every spill on the sheet. +//! An area that would take more than `BIG_AREA_BLOCKS` blocks (a full-height column spill takes +//! 65,536) is not registered in blocks but kept in a short list that every lookup tests directly, +//! so registering costs what the area's size warrants only for small areas. use crate::recalc::{FxHasher, Key}; use gridcraft_core::{CellRef, RangeRef}; @@ -11,6 +14,9 @@ type HashMap = std::collections::HashMap, blocks: HashMap<(u32, u32), Vec>, + /// Anchors of the areas too big to register in blocks. + big: Vec, } /// What covers a cell. @@ -42,16 +50,24 @@ fn block_span(rect: &RangeRef) -> (u32, u32, u32, u32) { (rect.start.row / BLOCK, rect.end.row / BLOCK, rect.start.col / BLOCK, rect.end.col / BLOCK) } +fn block_count(rect: &RangeRef) -> u64 { + let (r0, r1, c0, c1) = block_span(rect); + u64::from(r1 - r0 + 1) * u64::from(c1 - c0 + 1) +} + /// Whether registering `areas` in blocks costs less than checking each of them against each of /// `lookups` cells would: a few block entries cost about one containment check each. A tall or -/// wide area (a full-height column spill takes 65,536 blocks) is cheaper to test directly. +/// wide area is not registered at all (see `BIG_AREA_BLOCKS`) and so costs nothing here. pub(crate) fn indexing_pays<'a>(areas: impl ExactSizeIterator, lookups: usize) -> bool { const BLOCK_COST: u64 = 16; let budget = (lookups as u64).saturating_mul(areas.len() as u64) / BLOCK_COST; let mut blocks = 0u64; for r in areas { - let (r0, r1, c0, c1) = block_span(r); - blocks = blocks.saturating_add(u64::from(r1 - r0 + 1) * u64::from(c1 - c0 + 1)); + let n = block_count(r); + if n > BIG_AREA_BLOCKS { + continue; + } + blocks = blocks.saturating_add(n); if blocks > budget { return false; } @@ -77,10 +93,14 @@ impl SpillIndex { pub(crate) fn insert_rect(&mut self, (sheet, anchor): Key, rect: RangeRef, empty: bool) { self.remove((sheet, anchor)); let areas = self.sheets.entry(sheet).or_default(); - let (r0, r1, c0, c1) = block_span(&rect); - for br in r0..=r1 { - for bc in c0..=c1 { - areas.blocks.entry((br, bc)).or_default().push(anchor); + if block_count(&rect) > BIG_AREA_BLOCKS { + areas.big.push(anchor); + } else { + let (r0, r1, c0, c1) = block_span(&rect); + for br in r0..=r1 { + for bc in c0..=c1 { + areas.blocks.entry((br, bc)).or_default().push(anchor); + } } } areas.entries.insert(anchor, Entry { rect, empty }); @@ -89,6 +109,10 @@ impl SpillIndex { pub(crate) fn remove(&mut self, (sheet, anchor): Key) { let Some(areas) = self.sheets.get_mut(&sheet) else { return }; let Some(old) = areas.entries.remove(&anchor) else { return }; + if block_count(&old.rect) > BIG_AREA_BLOCKS { + areas.big.retain(|a| *a != anchor); + return; + } let (r0, r1, c0, c1) = block_span(&old.rect); for br in r0..=r1 { for bc in c0..=c1 { @@ -105,9 +129,9 @@ impl SpillIndex { /// The areas that cover `c`. pub(crate) fn covering(&self, sheet: usize, c: CellRef) -> Cover { let Some(areas) = self.sheets.get(&sheet) else { return Cover::None }; - let Some(list) = areas.blocks.get(&(c.row / BLOCK, c.col / BLOCK)) else { return Cover::None }; + let in_block = areas.blocks.get(&(c.row / BLOCK, c.col / BLOCK)).map_or(&[][..], |l| l.as_slice()); let mut found = Cover::None; - for a in list { + for a in in_block.iter().chain(&areas.big) { if let Some(e) = areas.entries.get(a) && !e.empty && e.rect.contains(c) @@ -139,6 +163,7 @@ impl SpillIndex { } } } + out.extend(areas.big.iter().copied().filter(|a| areas.entries.get(a).is_some_and(|e| e.rect.intersects(&range)))); } out.sort_unstable(); out.dedup(); @@ -181,15 +206,81 @@ mod tests { } #[test] - fn tall_or_wide_areas_are_not_worth_indexing_for_a_small_paste() { + fn tall_or_wide_areas_are_not_registered_in_blocks() { let column = RangeRef::new(at(0, 0), at(1_048_575, 0)); - assert!(!indexing_pays([column].iter(), 17)); - assert!(!indexing_pays([column].iter(), 1_000)); - // Many small areas pay back for a paste of a hundred cells. + assert!(block_count(&column) > BIG_AREA_BLOCKS); + let mut ix = SpillIndex::default(); + ix.insert_rect((0, at(0, 0)), column, false); + assert!(ix.sheets[&0].blocks.is_empty()); + assert!(matches!(ix.covering(0, at(900_000, 0)), Cover::One(a) if a == at(0, 0))); + assert!(matches!(ix.covering(0, at(900_000, 1)), Cover::None)); + let mut hits = Vec::new(); + ix.meeting(0, RangeRef::new(at(5, 0), at(6, 3)), &mut hits); + assert_eq!(hits, vec![at(0, 0)]); + ix.remove((0, at(0, 0))); + assert!(matches!(ix.covering(0, at(900_000, 0)), Cover::None) && ix.sheets[&0].big.is_empty()); + // Registering a big area costs nothing (lookups test it directly), so a small paste still indexes next to many small areas. + assert!(indexing_pays([column].iter(), 17)); + // Many small areas pay back for a paste of a hundred cells, but not for a handful. let small: Vec = (0..1_000).map(|i| RangeRef::new(at(i * 3, 0), at(i * 3 + 2, 0))).collect(); assert!(indexing_pays(small.iter(), 100)); - // A big area among many areas is paid for by a big paste. - assert!(indexing_pays([column].iter(), 2_000_000)); + assert!(!indexing_pays(small.iter(), 17)); + } + + /// The index against the old way: test every area for every lookup. Mixed shapes: small, + /// thin and tall, wide, big (kept out of the blocks), empty, on two sheets, replaced and removed. + #[test] + fn matches_a_scan_of_every_area_on_mixed_shapes() { + let mut seed = 0x2545_F491_4F6C_DD1Du64; + let mut next = |n: u32| { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + (seed >> 11) as u32 % n + }; + let mut ix = SpillIndex::default(); + let mut model: std::collections::BTreeMap<(usize, CellRef), (RangeRef, bool)> = Default::default(); + for step in 0..3_000 { + let sheet = next(2) as usize; + let anchor = at(next(400) * 5, next(40) * 3); + if next(4) == 0 { + ix.remove((sheet, anchor)); + model.remove(&(sheet, anchor)); + } else { + let (rows, cols) = match next(8) { + 0 => (0, 1 + next(5) as usize), + 1 => (1 + next(5) as usize, 0), + 2 => (5_000 + next(100_000) as usize, 1), + 3 => (1, 1 + next(40_000) as usize), + 4 => (200 + next(300) as usize, 40 + next(40) as usize), + _ => (1 + next(60) as usize, 1 + next(40) as usize), + }; + ix.insert((sheet, anchor), rows, cols); + let end = at(anchor.row + (rows as u32).saturating_sub(1), anchor.col + (cols as u32).saturating_sub(1)); + model.insert((sheet, anchor), (RangeRef::new(anchor, end), rows == 0 || cols == 0)); + } + if step % 7 != 0 { + continue; + } + let sheet = next(3) as usize; + for _ in 0..20 { + let cell = at(next(2_100), next(130)); + let want: Vec = + model.iter().filter(|((s, _), (r, e))| *s == sheet && !e && r.contains(cell)).map(|((_, a), _)| *a).collect(); + match ix.covering(sheet, cell) { + Cover::None => assert!(want.is_empty(), "{cell:?}"), + Cover::One(a) => assert_eq!(want, vec![a], "{cell:?}"), + Cover::Many => assert!(want.len() > 1, "{cell:?}"), + } + let (r0, c0) = (next(2_100), next(130)); + let (tall, wide) = (if next(5) == 0 { 3_000 } else { 40 }, if next(5) == 0 { 130 } else { 20 }); + let range = RangeRef::new(at(r0, c0), at(r0 + next(tall), c0 + next(wide))); + let want: Vec = model.iter().filter(|((s, _), (r, _))| *s == sheet && r.intersects(&range)).map(|((_, a), _)| *a).collect(); + let mut got = Vec::new(); + ix.meeting(sheet, range, &mut got); + assert_eq!(got, want, "{range:?}"); + } + } } #[test] diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index 9dd1fd925..ac254239a 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -2350,3 +2350,24 @@ fn many_spills_are_read_and_blocked_by_a_paste() { assert_eq!(t.get("A10"), Value::Error(CellError::Spill)); assert_eq!(t.num("A12"), 4.0); } + +#[test] +fn a_paste_over_small_and_tall_spills_blocks_those_it_touches() { + let mut t = T::new(); + for i in 0..40u32 { + t.wb.sheet_mut(0).unwrap().cells.set(CellRef::new(0, 2 * i), Cell::formula(Formula::new("=SEQUENCE(3)"))); + } + t.wb.sheet_mut(0).unwrap().cells.set(CellRef::new(0, 200), Cell::formula(Formula::new("=SEQUENCE(1048576)"))); + t.calc.recalc_all(&mut t.wb); + // 20 cells: the middle row of the small arrays in columns 0, 8, 16 ... 72 (columns 80 and up hit + // nothing), and one deep inside the tall one. + let mut cells: Vec<(u32, u32)> = (0..19u32).map(|i| (1, 8 * i)).collect(); + cells.push((700_000, 200)); + paste_into(&mut t, &cells); + assert_eq!(t.get("A1"), Value::Error(CellError::Spill)); + assert_eq!(t.get("GS1"), Value::Error(CellError::Spill)); + assert_eq!(t.num("C1"), 1.0); + assert_eq!(t.num("C3"), 3.0); + assert_eq!(t.get("I1"), Value::Error(CellError::Spill)); + assert_eq!(t.num("E3"), 3.0); +} From bdbd29e623c7ddf4df27331a7f7834295b5f770c Mon Sep 17 00:00:00 2001 From: vangel76 Date: Fri, 30 Oct 2026 00:04:25 +0100 Subject: [PATCH 10/76] perf(calc): size the spill-index decision by the sheet's own changed cells A batch spread over several sheets used its total length for every sheet, so a one-cell sheet with many spills built the block index as if it had the whole batch's lookups. Co-Authored-By: Claude Sonnet 5.5 --- crates/calc/src/recalc.rs | 44 +++++++++++++++++++++++++++++---------- crates/calc/src/tests.rs | 22 ++++++++++++++++++++ 2 files changed, 55 insertions(+), 11 deletions(-) diff --git a/crates/calc/src/recalc.rs b/crates/calc/src/recalc.rs index aae212dd3..7c3a6f361 100644 --- a/crates/calc/src/recalc.rs +++ b/crates/calc/src/recalc.rs @@ -22,6 +22,13 @@ use crate::spill_index::{SpillIndex, indexing_pays}; /// Changed cells (in one `cells_changed`) from which a sheet's spill areas may be indexed. const INDEX_SPILLS_FROM: usize = 16; +/// Whether a sheet's spill areas are indexed for a batch of `batch` changed cells of which +/// `on_sheet` are on that sheet: the batch must be a paste, and registering the areas must cost +/// less than walking them once for each of the sheet's own changed cells. +fn index_spills<'a>(batch: usize, on_sheet: usize, areas: impl ExactSizeIterator) -> bool { + batch > INDEX_SPILLS_FROM && areas.len() > 0 && indexing_pays(areas, on_sheet) +} + mod graph; mod pass_host; @@ -250,20 +257,22 @@ impl Calc { // walked for each one, when registering them costs less than walking them would. let mut areas = SpillIndex::default(); let mut strategy: HashMap = HashMap::default(); + let mut per_sheet: HashMap = HashMap::default(); + for k in changed { + *per_sheet.entry(k.0).or_insert(0) += 1; + } let mut at = Vec::new(); for &k in changed { if let Some(sh) = wb.sheet(k.0) { - let indexed = changed.len() > INDEX_SPILLS_FROM - && !sh.spill_ranges.is_empty() - && *strategy.entry(k.0).or_insert_with(|| { - let indexed = indexing_pays(sh.spill_ranges.values(), changed.len()); - if indexed { - for (anchor, r) in &sh.spill_ranges { - areas.insert_rect((k.0, *anchor), *r, false); - } + let indexed = *strategy.entry(k.0).or_insert_with(|| { + let indexed = index_spills(changed.len(), per_sheet[&k.0], sh.spill_ranges.values()); + if indexed { + for (anchor, r) in &sh.spill_ranges { + areas.insert_rect((k.0, *anchor), *r, false); } - indexed - }); + } + indexed + }); if indexed { // Anchor order, as walking `spill_ranges` gives. areas.meeting(k.0, RangeRef::new(k.1, k.1), &mut at); @@ -556,7 +565,7 @@ pub(crate) mod tests { use gridcraft_core::{CellRef, RangeRef, Value}; use gridcraft_model::{Cell, Formula, Workbook}; - use super::{Calc, HashMap, Host, Intervals, Key, PassHost}; + use super::{Calc, HashMap, Host, Intervals, Key, PassHost, index_spills}; /// xorshift64 for repeatable pseudo-random cases. fn rng(state: &mut u64) -> u64 { @@ -573,6 +582,19 @@ pub(crate) mod tests { v } + /// One changed cell on a sheet with many small spills does not index them just because the + /// batch (mostly on another sheet) is large; the sheet's own share of the batch decides. + #[test] + fn the_spill_index_is_chosen_from_the_sheets_own_changed_cells() { + let small: Vec = (0..1_000).map(|i| RangeRef::new(CellRef::new(i * 3, 0), CellRef::new(i * 3 + 2, 0))).collect(); + // Same areas, same batch of 5000: indexed when 5000 of them are on the sheet, walked when 1 is. + assert!(index_spills(5_000, 5_000, small.iter())); + assert!(!index_spills(5_000, 1, small.iter())); + // Not a paste, or nothing to index: never. + assert!(!index_spills(16, 16, small.iter())); + assert!(!index_spills(5_000, 5_000, [].iter())); + } + #[test] fn intervals_match_a_linear_scan() { let mut iv: Intervals = Intervals::default(); diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index ac254239a..a3e7f810d 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -2371,3 +2371,25 @@ fn a_paste_over_small_and_tall_spills_blocks_those_it_touches() { assert_eq!(t.get("I1"), Value::Error(CellError::Spill)); assert_eq!(t.num("E3"), 3.0); } + +#[test] +fn a_big_paste_on_one_sheet_and_one_cell_over_many_spills_on_another() { + let mut t = T::new(); + t.wb.sheets.push(std::sync::Arc::new(gridcraft_model::Sheet::new("Data"))); + for i in 0..1_500u32 { + t.wb.sheet_mut(1).unwrap().cells.set(CellRef::new(4 * i, 0), Cell::formula(Formula::new(&format!("=SEQUENCE(3,1,{})", 10 * i)))); + } + t.calc.recalc_all(&mut t.wb); + assert_eq!(t.wb.sheet(1).unwrap().value(CellRef::new(4 * 700 + 2, 0)), Value::Number(7002.0)); + // 5000 cells pasted on the first sheet, one over the middle of the 701st array on the second. + let mut changed: Vec<(usize, CellRef)> = (0..5_000u32).map(|i| (0, CellRef::new(i / 50, i % 50))).collect(); + changed.push((1, CellRef::new(4 * 700 + 1, 0))); + for k in &changed { + t.wb.sheet_mut(k.0).unwrap().cells.set(k.1, Cell::value(Value::Number(1.0))); + } + t.calc.cells_changed(&mut t.wb, &changed); + let data = t.wb.sheet(1).unwrap(); + assert_eq!(data.value(CellRef::new(4 * 700, 0)), Value::Error(CellError::Spill)); + assert_eq!(data.value(CellRef::new(4 * 699 + 2, 0)), Value::Number(6992.0)); + assert_eq!(data.value(CellRef::new(4 * 701 + 2, 0)), Value::Number(7012.0)); +} From e7cda6337b5ec39dacee91ec67075f69077c80b6 Mon Sep 17 00:00:00 2001 From: nihilist-narwhal <240798405+nihilist-narwhal@users.noreply.github.com> Date: Thu, 22 Oct 2026 22:19:05 -0500 Subject: [PATCH 11/76] MCP: the stdio server keeps at most 16 MB of a message (#2164) `serve` read a whole line before checking it against `MAX_LINE`, so a client sending a newline-free payload grew the buffer without limit. The line is now read through `take`, as the control server does; the rest of an over-long line is read and dropped, the client gets the "message too large" error, and the next message is served (stdio has one client, so the stream stays open rather than closing as the control server's connections do). Co-Authored-By: Claude Opus 5.5 --- crates/mcp/src/server.rs | 56 ++++++++++++++++++++++++++++------ crates/mcp/src/tests.rs | 65 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 9 deletions(-) diff --git a/crates/mcp/src/server.rs b/crates/mcp/src/server.rs index 75b82d8d8..0d262dbe7 100644 --- a/crates/mcp/src/server.rs +++ b/crates/mcp/src/server.rs @@ -1,6 +1,6 @@ //! JSON-RPC 2.0 framing and the MCP lifecycle / tools / resources / prompts methods. -use std::io::{BufRead, Write}; +use std::io::{BufRead, Read, Write}; use serde_json::{Value, json}; @@ -45,6 +45,45 @@ fn response(id: Value, result: Value) -> Value { json!({"jsonrpc": "2.0", "id": id, "result": result}) } +/// Reads the next newline-delimited message into `buf`, keeping at most `MAX_LINE` bytes of it: +/// a client sending no newline can't make the server allocate more (#2164). `None` at the end of +/// `input`; `Some(false)` for a line over the limit, whose rest is read and dropped so the next +/// message is read whole. +pub(crate) fn read_message(input: &mut impl BufRead, buf: &mut Vec) -> std::io::Result> { + buf.clear(); + // One byte past the limit tells an over-long line from one of exactly `MAX_LINE` bytes. + let limit = u64::try_from(MAX_LINE).unwrap_or(u64::MAX).saturating_add(1); + if input.take(limit).read_until(b'\n', buf)? == 0 { + return Ok(None); + } + if buf.len() > MAX_LINE && buf.last() != Some(&b'\n') { + skip_line(input)?; + buf.clear(); + return Ok(Some(false)); + } + Ok(Some(true)) +} + +/// Reads up to and including the next newline (or to the end of `input`) without keeping it. +fn skip_line(input: &mut impl BufRead) -> std::io::Result<()> { + loop { + let chunk = input.fill_buf()?; + if chunk.is_empty() { + return Ok(()); + } + match chunk.iter().position(|&b| b == b'\n') { + Some(i) => { + input.consume(i + 1); + return Ok(()); + } + None => { + let n = chunk.len(); + input.consume(n); + } + } + } +} + fn error(id: Value, code: i64, message: impl Into) -> Value { json!({"jsonrpc": "2.0", "id": id, "error": {"code": code, "message": message.into()}}) } @@ -68,14 +107,13 @@ impl Server { pub fn serve(&mut self, mut input: impl BufRead, mut output: impl Write) -> std::io::Result<()> { let mut buf = Vec::new(); loop { - buf.clear(); - let n = input.read_until(b'\n', &mut buf)?; - if n == 0 { - return Ok(()); - } - let reply = match std::str::from_utf8(&buf) { - Ok(line) => self.handle_line(line), - Err(_) => Some(error(Value::Null, PARSE_ERROR, "message is not valid UTF-8").to_string()), + let reply = match read_message(&mut input, &mut buf)? { + None => return Ok(()), + Some(false) => Some(error(Value::Null, INVALID_REQUEST, "message too large (16 MB max)").to_string()), + Some(true) => match std::str::from_utf8(&buf) { + Ok(line) => self.handle_line(line), + Err(_) => Some(error(Value::Null, PARSE_ERROR, "message is not valid UTF-8").to_string()), + }, }; if let Some(reply) = reply { output.write_all(reply.as_bytes())?; diff --git a/crates/mcp/src/tests.rs b/crates/mcp/src/tests.rs index 71631b5e8..5a5297246 100644 --- a/crates/mcp/src/tests.rs +++ b/crates/mcp/src/tests.rs @@ -359,6 +359,71 @@ fn serve_loop_over_buffers() { assert!(lines[2]["result"]["content"][0]["text"].as_str().unwrap().contains("42")); } +/// Streams `len` bytes of `a` from one reused buffer, then `tail`: a huge line costs no memory to +/// produce, so only the server can make it expensive. +struct HugeLine { + left: u64, + tail: &'static [u8], + chunk: Vec, +} + +impl std::io::Read for HugeLine { + fn read(&mut self, out: &mut [u8]) -> std::io::Result { + let buf = std::io::BufRead::fill_buf(self)?; + let n = buf.len().min(out.len()); + out[..n].copy_from_slice(&buf[..n]); + std::io::BufRead::consume(self, n); + Ok(n) + } +} + +impl std::io::BufRead for HugeLine { + fn fill_buf(&mut self) -> std::io::Result<&[u8]> { + if self.left > 0 { + let n = self.chunk.len().min(usize::try_from(self.left).unwrap_or(usize::MAX)); + return Ok(&self.chunk[..n]); + } + Ok(self.tail) + } + fn consume(&mut self, n: usize) { + if self.left > 0 { + self.left -= n as u64; + } else { + self.tail = &self.tail[n..]; + } + } +} + +#[test] +fn a_line_without_newline_keeps_at_most_the_limit_in_memory() { + // #2164: the whole line was buffered before its size was checked (4 GiB here; any size a + // client cares to send). + let mut input = + HugeLine { left: 4 << 30, tail: concat!("\n", r#"{"jsonrpc":"2.0","id":7,"method":"ping"}"#, "\n").as_bytes(), chunk: vec![b'a'; 1 << 20] }; + let mut buf = Vec::new(); + assert_eq!(crate::server::read_message(&mut input, &mut buf).unwrap(), Some(false)); + assert!(buf.capacity() <= 2 * (16 << 20) + 1, "kept {} bytes", buf.capacity()); + assert_eq!(crate::server::read_message(&mut input, &mut buf).unwrap(), Some(true)); + assert!(buf.starts_with(br#"{"jsonrpc""#)); + assert_eq!(crate::server::read_message(&mut input, &mut buf).unwrap(), None); +} + +#[test] +fn serve_loop_bounds_a_line_without_newline() { + // #2164: a 4 GiB line was read whole before its size was checked. Now the server keeps at + // most 16 MB of it, answers that it is too large, and reads the next message normally. + let input = + HugeLine { left: 4 << 30, tail: concat!("\n", r#"{"jsonrpc":"2.0","id":7,"method":"ping"}"#, "\n").as_bytes(), chunk: vec![b'a'; 1 << 20] }; + let mut out = Vec::new(); + server().serve(input, &mut out).unwrap(); + let lines: Vec = String::from_utf8(out).unwrap().lines().map(|l| serde_json::from_str(l).unwrap()).collect(); + assert_eq!(lines.len(), 2); + assert_eq!(lines[0]["error"]["code"], -32600); + assert!(lines[0]["error"]["message"].as_str().unwrap().contains("too large")); + assert_eq!(lines[1]["id"], 7); + assert!(lines[1].get("result").is_some(), "{}", lines[1]); +} + #[test] fn cell_set_without_input_is_error() { let mut s = server(); From 96672e8ad5900dd20bd2034a99d4abebaccda297 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Mon, 2 Nov 2026 19:33:53 +0100 Subject: [PATCH 12/76] Drop helpers the merged range reads no longer use Co-Authored-By: Claude Sonnet 5.5 --- crates/calc/src/recalc/pass_host.rs | 7 ------- crates/calc/src/spill_index.rs | 1 + 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/crates/calc/src/recalc/pass_host.rs b/crates/calc/src/recalc/pass_host.rs index 9d00f9784..9a733501b 100644 --- a/crates/calc/src/recalc/pass_host.rs +++ b/crates/calc/src/recalc/pass_host.rs @@ -185,13 +185,6 @@ pub(super) fn spills_as_before(sheet: &gridcraft_model::Sheet, anchor: CellRef, .all(|(i, c)| c == anchor || (!sheet.cells.has(c) && sheet.spill.get(&c) == Some(arr.data.get(i).unwrap_or(&Value::Empty)))) } -/// The area an array anchored at `anchor` covers. -fn array_area(anchor: CellRef, a: &Array) -> RangeRef { - let end = - CellRef::new(anchor.row.saturating_add((a.rows as u32).saturating_sub(1)), anchor.col.saturating_add((a.cols as u32).saturating_sub(1))); - RangeRef::new(anchor, end) -} - /// The spilled values `sheet` stores inside `range`, passed to `f` until it returns false. Values /// outside the range are not walked: with fewer rows in the range than spilled values, each row /// is looked up on its own; otherwise one ordered scan visits each spilled value at most once. diff --git a/crates/calc/src/spill_index.rs b/crates/calc/src/spill_index.rs index 1bbf0f9c0..d642bdee7 100644 --- a/crates/calc/src/spill_index.rs +++ b/crates/calc/src/spill_index.rs @@ -77,6 +77,7 @@ pub(crate) fn indexing_pays<'a>(areas: impl ExactSizeIterator bool { self.sheets.get(&sheet).is_some_and(|s| !s.entries.is_empty()) } From e85b24df5724d224ae69b2d9b7bc5cf7554f4d42 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Mon, 2 Nov 2026 20:31:52 +0100 Subject: [PATCH 13/76] calc: register a Python result's spill in the spill index python_result wrote into spills but not into spill_index, so indexed readers (cell_value, range_values, visit_range) missed a Python array laid out in the same pass. Insert on spill, remove when blocked, as the formula path does. Co-Authored-By: Claude Sonnet 5.5 --- crates/calc/src/recalc.rs | 33 +++++++++++++++++++++++++++++ crates/calc/src/recalc/pass_host.rs | 2 ++ 2 files changed, 35 insertions(+) diff --git a/crates/calc/src/recalc.rs b/crates/calc/src/recalc.rs index 77d5b68dd..286569822 100644 --- a/crates/calc/src/recalc.rs +++ b/crates/calc/src/recalc.rs @@ -663,6 +663,39 @@ pub(crate) mod tests { assert!(!host.visit_range(0, inside, &mut |_| true), "an overlapping read leaves the block to range_values"); } + /// A Python array laid out in this pass is seen by the indexed readers, whether the sheet + /// has no spill from before or an older, smaller one. + #[test] + fn a_python_spill_found_in_the_pass_is_read_through_the_index() { + for stale in [false, true] { + let mut wb = Workbook::new(); + let sh = wb.sheet_mut(0).unwrap(); + let anchor = CellRef::new(0, 0); + let py = gridcraft_model::PyCode::new("grid"); + sh.set_cell(anchor, Cell { value: Value::Number(0.0), py: Some(std::sync::Arc::new(py)), ..Cell::default() }); + if stale { + // The last calculation spilled a 2 x 2 block of 100s. + sh.spill_ranges.insert(anchor, RangeRef::new(anchor, CellRef::new(1, 1))); + for c in RangeRef::new(anchor, CellRef::new(1, 1)).iter().skip(1) { + sh.spill.insert(c, Value::Number(100.0)); + } + } + let data: Vec = (1..=9).map(|i| Value::Number(f64::from(i))).collect(); + let array = gridcraft_core::Array::new(3, 3, data).unwrap(); + let mut python = HashMap::default(); + python.insert((0, anchor), Value::Array(std::sync::Arc::new(array))); + let exprs = HashMap::default(); + let mut rng = 1u64; + let mut host = PassHost::new(&wb, &exprs, &mut rng, 0.0).with_python(python); + assert_eq!(host.cell_value(0, anchor), Value::Number(1.0)); + let all = RangeRef::parse("A1:C3").unwrap(); + let sum: f64 = host.range_values(0, all).iter().map(|v| if let Value::Number(n) = v { *n } else { 0.0 }).sum(); + assert_eq!(sum, 45.0, "stale={stale}: a SUM over the new 3 x 3 spill reads all of it"); + assert_eq!(host.cell_value(0, CellRef::new(2, 2)), Value::Number(9.0), "stale={stale}"); + assert!(!host.visit_range(0, all, &mut |_| true), "stale={stale}: the new spill leaves the block to range_values"); + } + } + #[test] fn intervals_match_a_linear_scan() { let mut iv: Intervals = Intervals::default(); diff --git a/crates/calc/src/recalc/pass_host.rs b/crates/calc/src/recalc/pass_host.rs index 9a733501b..1ef24bc42 100644 --- a/crates/calc/src/recalc/pass_host.rs +++ b/crates/calc/src/recalc/pass_host.rs @@ -400,6 +400,7 @@ impl PassHost<'_> { && a.cols >= 1 && a.rows.checked_mul(a.cols).is_some_and(|n| n == a.data.len() && n <= gridcraft_model::python::MAX_RESULT_CELLS); if sane { + self.spill_index.insert(k, a.rows, a.cols); self.spills.insert(k, a.clone()); a.data.first().cloned().unwrap_or_default() } else { @@ -411,6 +412,7 @@ impl PassHost<'_> { let v = match self.spills.get(&k) { Some(a) if spill_blocked(self, k, a) => { self.spills.remove(&k); + self.spill_index.remove(k); Value::Error(CellError::Spill) } _ => v, From 472132b1585bef6bec6a296128f2ac72166034ca Mon Sep 17 00:00:00 2001 From: vangel76 Date: Tue, 3 Nov 2026 19:44:55 +0100 Subject: [PATCH 14/76] math: refuse macro definitions and cap matrix cells RaTeX counts macro expansions (1,000) but not how large they grow, so a definition whose argument doubles on every call keeps the expander busy without bound. User macros have no use in an equation, so check_caps now refuses the definers (def, gdef, edef, xdef, let, futurelet, global, long, newcommand, renewcommand, providecommand) before RaTeX sees the source. The scan reads control words, verbatim text and comments the way RaTeX's lexer does. A matrix is laid out and written as rows times the widest row, so one long row in a tall matrix multiplied the output. check_caps now counts the padded cells of every environment from the source and refuses more than 2,500 in total; to_omml checks the parsed grid as well. Co-Authored-By: Claude Sonnet 5.5 --- crates/math/src/lib.rs | 232 ++++++++++++++++++++++++++++++++++++++-- crates/math/src/omml.rs | 3 + docs/decisions.md | 5 +- 3 files changed, 232 insertions(+), 8 deletions(-) diff --git a/crates/math/src/lib.rs b/crates/math/src/lib.rs index 5e1e99a41..e1f783dca 100644 --- a/crates/math/src/lib.rs +++ b/crates/math/src/lib.rs @@ -3,7 +3,7 @@ //! without calling RaTeX again. //! //! RaTeX is young (0.1.x), so it only ever runs inside this crate's entry points ([`layout`], -//! [`to_omml`], [`check`]). Each one caps its input (4 KB, nesting 32) and runs under +//! [`to_omml`], [`check`]). Each one caps its input (4 KB, nesting 32, no macro definitions, at most [`MAX_CELLS`] matrix cells) and runs under //! `catch_unwind`, so a stray panic becomes a [`MathError`]. See the RaTeX gate in //! `docs/decisions.md` and `tests/gate.rs`. //! @@ -33,6 +33,9 @@ pub const MAX_DEPTH: usize = 32; pub const MAX_EM: f32 = 10_000.0; /// Most items one equation may produce. pub const MAX_ITEMS: usize = 50_000; +/// Most matrix cells one equation may have, counting the empty cells that pad a short row to the +/// width of the widest one. A ragged matrix costs rows times the widest row, not what is typed. +pub const MAX_CELLS: usize = 2_500; /// The error for a source over [`MAX_SOURCE_BYTES`]. It is one shared value so that a cache can /// answer for such a source by its length alone, without reading it. @@ -223,20 +226,61 @@ fn parse(latex: &str) -> Result, MathError> { }) } -/// The size and nesting caps, checked before RaTeX sees the source. +/// Control words that define or rebind macros. User macros have no use in an equation, and +/// RaTeX caps only how many expansions run, not how large they grow, so a self-referencing +/// definition whose argument doubles on every call keeps the expander busy without bound. They are +/// refused before RaTeX sees the source. +const MACRO_DEFINERS: &[&str] = + &["def", "gdef", "edef", "xdef", "let", "futurelet", "global", "long", "newcommand", "renewcommand", "providecommand"]; + +/// One open `\begin…\end` environment, counted from the source text. +struct Grid { + /// Rows ended so far (by `\\` or `\cr`). + rows: usize, + /// Cells in the current row. + cells: usize, + /// Cells in the widest row so far. + widest: usize, +} + +impl Grid { + fn new() -> Grid { + Grid { rows: 0, cells: 1, widest: 0 } + } + fn end_row(&mut self) { + self.widest = self.widest.max(self.cells); + self.rows += 1; + self.cells = 1; + } + /// The padded grid RaTeX lays out and `to_omml` writes: every row as wide as the widest. + fn padded_cells(&self) -> usize { + (self.rows + 1).saturating_mul(self.widest.max(self.cells)) + } +} + +/// The size, nesting, macro and matrix caps, checked before RaTeX sees the source. fn check_caps(latex: &str) -> Result<(), MathError> { if latex.len() > MAX_SOURCE_BYTES { return Err(SOURCE_TOO_LONG.clone()); } + // `%` starts a comment, except inside the address of `\href` and `\url`, where RaTeX reads it + // as a character. Which one it is decides what the rest of the line means, so the two are not + // allowed together. + if latex.contains('%') && (latex.contains("\\href") || latex.contains("\\url")) { + return Err(MathError::new("A % can't be combined with a link in an equation.", latex.find('%').map(|i| i..i + 1))); + } let mut depth = 0usize; + let mut grids: Vec = Vec::new(); + let mut cells_total = 0usize; let mut chars = latex.char_indices().peekable(); while let Some((i, c)) = chars.next() { let open = match c { '\\' => { - // A control word, or an escaped character (`\{` is not a group). + // A control word (letters and `@`, as RaTeX's lexer reads it), or an escaped + // character (`\{` is not a group). let mut word = String::new(); while let Some(&(_, n)) = chars.peek() { - if n.is_ascii_alphabetic() { + if n.is_ascii_alphabetic() || n == '@' { word.push(n); chars.next(); } else { @@ -244,15 +288,69 @@ fn check_caps(latex: &str) -> Result<(), MathError> { } } if word.is_empty() { - chars.next(); + if let Some((_, n)) = chars.next() + && n == '\\' + && let Some(g) = grids.last_mut() + { + g.end_row(); + } continue; } + if MACRO_DEFINERS.contains(&word.as_str()) { + return Err(MathError::new("Macro definitions are not supported in equations.", Some(i..i + 1 + word.len()))); + } match word.as_str() { - "left" | "begin" => Some(true), - "right" | "end" => Some(false), + "verb" => { + // Verbatim text runs to the next copy of its delimiter, whatever it + // holds: no group, comment, row or definition starts inside it. + if chars.peek().is_some_and(|&(_, n)| n == '*') { + chars.next(); + } + if let Some((_, delim)) = chars.next() { + for (_, n) in chars.by_ref() { + if n == delim { + break; + } + } + } + None + } + "cr" => { + if let Some(g) = grids.last_mut() { + g.end_row(); + } + None + } + "begin" => { + if let Some(n) = alignat_columns(latex.get(i + 6..).unwrap_or_default()) + && n > MAX_ALIGNAT_PAIRS + { + return Err(too_many_cells(i..i + 6)); + } + grids.push(Grid::new()); + Some(true) + } + "end" => { + if let Some(g) = grids.pop() { + cells_total = cells_total.saturating_add(g.padded_cells()); + if cells_total > MAX_CELLS { + return Err(too_many_cells(i..i + 1 + word.len())); + } + } + Some(false) + } + "left" => Some(true), + "right" => Some(false), _ => None, } } + '&' | '@' => { + // `@` makes an arrow cell and an empty cell beside it in a `CD` diagram. + if let Some(g) = grids.last_mut() { + g.cells += if c == '@' { 2 } else { 1 }; + } + None + } '%' => { // A comment runs to the end of the line. for (_, n) in chars.by_ref() { @@ -277,9 +375,39 @@ fn check_caps(latex: &str) -> Result<(), MathError> { None => {} } } + // An environment left open is a syntax error for the parser to report, but it still counts. + for g in grids { + cells_total = cells_total.saturating_add(g.padded_cells()); + } + if cells_total > MAX_CELLS { + return Err(too_many_cells(latex.len()..latex.len())); + } Ok(()) } +/// Most column pairs `\begin{alignedat}{N}` may ask for. RaTeX allocates 2N column specs before +/// it lays anything out. +const MAX_ALIGNAT_PAIRS: usize = 50; + +/// N of `{alignat}{N}`, `{alignat*}{N}` or `{alignedat}{N}` at the start of `rest` (just after +/// `\begin`); `usize::MAX` when the count is present but too long to read. +fn alignat_columns(rest: &str) -> Option { + let rest = rest.trim_start().strip_prefix('{')?; + let (name, after) = rest.split_once('}')?; + if !matches!(name, "alignat" | "alignat*" | "alignedat") { + return None; + } + let digits = after.trim_start().strip_prefix('{')?.split('}').next()?.trim(); + if digits.is_empty() || !digits.bytes().all(|b| b.is_ascii_digit()) { + return None; + } + Some(digits.parse().unwrap_or(usize::MAX)) +} + +fn too_many_cells(span: Range) -> MathError { + MathError::new(format!("An equation can have at most {MAX_CELLS} matrix cells (rows times the widest row)."), Some(span)) +} + fn color(c: &Color) -> Option<[u8; 3]> { let q = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as u8; let rgb = [q(c.r), q(c.g), q(c.b)]; @@ -509,6 +637,96 @@ mod tests { } } + /// Runs `f` on its own thread and fails if it has not returned within `secs` seconds. + fn within(secs: u64, f: impl FnOnce() -> T + Send + 'static) -> T { + let (tx, rx) = std::sync::mpsc::channel(); + std::thread::Builder::new() + .stack_size(2 << 20) + .spawn(move || { + let _ = tx.send(f()); + }) + .unwrap(); + match rx.recv_timeout(std::time::Duration::from_secs(secs)) { + Ok(v) => v, + Err(std::sync::mpsc::RecvTimeoutError::Timeout) => panic!("did not return within {secs} s"), + Err(e) => panic!("the worker thread died: {e}"), + } + } + + #[test] + fn macro_definitions_are_refused_before_the_expander_runs() { + let defs = [ + "\\def\\a#1{\\a{#1#1}}\\a{x}", + "\\gdef\\a#1{\\a{#1#1#1#1}}\\a{x}", + "\\edef\\a#1{\\a{#1#1}}\\a{x}", + "\\xdef\\a#1{{#1}{#1}}\\a{x}", + "\\newcommand{\\a}[1]{\\a{#1#1}}\\a{x}", + "\\renewcommand{\\a}[1]{\\a{#1#1}}\\a{x}", + "\\providecommand{\\a}[1]{\\a{#1#1}}\\a{x}", + "\\let\\a\\frac \\a{1}{2}", + "\\futurelet\\a\\frac \\a{1}{2}", + "\\global\\def\\a#1{\\a{#1#1}}\\a{x}", + "\\long\\def\\a#1{\\a{#1#1}}\\a{x}", + "{\\def\\a#1{\\a{#1#1}}}\\a{x}", + ]; + for src in defs { + // `check_caps` alone, so this cannot hang even if the refusal regresses. + let e = check_caps(src).unwrap_err(); + assert!(e.message.contains("Macro definitions"), "{src}: {e}"); + assert_eq!(e.span.as_ref().map(|r| &src[r.clone()]).map(|w| w.starts_with('\\')), Some(true)); + let s = src.to_string(); + let (c, l, o) = within(10, move || (check(&s).is_err(), layout(&s).is_err(), to_omml(&s, 11.0, None).is_err())); + assert!(c && l && o, "{src}"); + } + // Names that merely start with a definer, escaped backslashes and comments are not macros. + for ok in ["\\default", "\\letter", "\\\\def", "x % \\def\\a\n+1", "\\longrightarrow"] { + assert!(check_caps(ok).is_ok(), "{ok}"); + } + assert!(layout("\\longrightarrow x").is_ok()); + // Text the lexer reads differently from a plain scan must not hide a definition: a + // verbatim `%` is no comment, a `%` in a link address is no comment, `@` joins a word. + for src in [ + "\\verb|%|\\def\\a#1{\\a{#1#1}}\\a{x}", + "\\verb\\x\\\\def\\a#1{\\a{#1#1}}\\a{x}", + "%\\verb|\n\\def\\a#1{\\a{#1#1}}\\a{x}", + "\\href{a%b}{x}\\def\\a#1{\\a{#1#1}}\\a{x}", + "\\url{a%}\\def\\a#1{\\a{#1#1}}\\a{x}", + ] { + assert!(check_caps(src).is_err(), "{src}"); + } + assert!(check_caps("\\verb|\\def| x").is_ok(), "verbatim text is not a definition"); + assert!(check_caps("\\def@x").is_ok(), "`\\def@x` is one unknown word to the lexer"); + } + + #[test] + fn ragged_matrices_are_capped_by_their_padded_size() { + let ragged = format!("\\begin{{matrix}}{}{}\\end{{matrix}}", "a\\\\".repeat(650), "a&".repeat(999) + "a"); + assert!(ragged.len() < MAX_SOURCE_BYTES); + let e = check_caps(&ragged).unwrap_err(); + assert!(e.message.contains("matrix cells"), "{e}"); + let r = ragged.clone(); + let (l, o) = within(10, move || (layout(&r).unwrap_err(), to_omml(&r, 11.0, None).unwrap_err())); + assert!(l.message.contains("matrix cells") && o.message.contains("matrix cells")); + // Without the environment words there is no grid to count; the parser rejects it. + // Ordinary and wide-but-regular matrices still work, and so does a trailing `\\`. + let wide = format!("\\begin{{pmatrix}}{}\\end{{pmatrix}}", vec!["a&".repeat(39) + "a"; 40].join("\\\\")); + assert!(wide.len() < MAX_SOURCE_BYTES); + assert!(layout(&wide).is_ok()); + assert!(to_omml(&wide, 11.0, None).is_ok()); + assert!(layout("\\begin{pmatrix} a & b \\\\ c & d \\\\ \\end{pmatrix}").is_ok()); + // Diagram arrows and `alignedat` column counts size a grid without any `&`. + let cd = format!("\\begin{{CD}}{}{}\\end{{CD}}", "A\\\\".repeat(900), "@>>>".repeat(300)); + assert!(cd.len() < MAX_SOURCE_BYTES); + assert!(check_caps(&cd).is_err_and(|e| e.message.contains("matrix cells"))); + let r = "\\begin{alignedat}{99999999}a\\end{alignedat}"; + assert!(check_caps(r).is_err_and(|e| e.message.contains("matrix cells"))); + assert!(check_caps("\\begin{alignedat}{2}a&b&c&d\\end{alignedat}").is_ok()); + assert!(layout("\\begin{CD}A @>f>> B \\\\ @VVV @VVV \\\\ C @>>> D\\end{CD}").is_ok()); + assert!(check_caps("\\operatorname{curl} 50\\%").is_ok()); + let two = format!("{0}{0}", "\\begin{matrix}a&b\\\\c&d\\end{matrix}"); + assert!(check_caps(&two).is_ok()); + } + #[test] fn an_injected_panic_is_an_error_not_a_crash() { INJECT_PANIC.with(|p| p.set(true)); diff --git a/crates/math/src/omml.rs b/crates/math/src/omml.rs index dc0141aed..7d5c66835 100644 --- a/crates/math/src/omml.rs +++ b/crates/math/src/omml.rs @@ -406,6 +406,9 @@ impl Writer { } ParseNode::Array { body, is_cd: None | Some(false), .. } => { let cols = body.iter().map(Vec::len).max().unwrap_or(0).max(1); + if body.len().saturating_mul(cols) > crate::MAX_CELLS { + return Err(MathError::new(format!("An equation can have at most {} matrix cells.", crate::MAX_CELLS), None)); + } self.out.push_str(&format!( "" )); diff --git a/docs/decisions.md b/docs/decisions.md index 09bc9b5cc..7ccc445c8 100644 --- a/docs/decisions.md +++ b/docs/decisions.md @@ -95,7 +95,10 @@ LaTeX subset: access. Indexing is not counted; the fuzz run covers it. - **Fuzzing.** 70,000 random token strings and 50,000 mutations of valid equations: no panic, no input slower than 5 ms. Self-referencing and exponential macros stop at the parser's - 1,000-expansion limit. Nesting deeper than 32 is refused ("Recursion limit exceeded"), and + 1,000-expansion limit, which counts expansions and not their size: a definition whose + argument doubles on every call never reaches it. The wrapper therefore refuses macro + definitions (`\def`, `\newcommand`, `\let` and the like) before RaTeX sees the source, and + caps the padded size of matrices at 2,500 cells. Nesting deeper than 32 is refused ("Recursion limit exceeded"), and the deepest accepted input needs under 256 KB of stack in a release build, well inside the smallest main-thread stack we run on (1 MB on Windows). Absurd sizes (`\rule{1e308em}{1em}`) lay out with huge dimensions, so the wrapper caps them. From d8ba397bb721ff16124b207ff3cedbb8bb5cc7d2 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Wed, 4 Nov 2026 00:04:32 +0100 Subject: [PATCH 15/76] math: refuse an alignedat count hidden by a comment or spaces (review point: cap bypass) CodeRabbit found that \begin{alignedat}%{99999999} and {9 9} made the count parser return None, which skipped the 50-pair cap. The lookahead now drops comments and spaces as RaTeX's lexer does, and a matched alignat name with a count that is not a plain digit string is refused. Co-Authored-By: Claude Sonnet 5.5 --- crates/math/src/lib.rs | 45 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/crates/math/src/lib.rs b/crates/math/src/lib.rs index e1f783dca..62033f2b8 100644 --- a/crates/math/src/lib.rs +++ b/crates/math/src/lib.rs @@ -390,16 +390,39 @@ fn check_caps(latex: &str) -> Result<(), MathError> { const MAX_ALIGNAT_PAIRS: usize = 50; /// N of `{alignat}{N}`, `{alignat*}{N}` or `{alignedat}{N}` at the start of `rest` (just after -/// `\begin`); `usize::MAX` when the count is present but too long to read. +/// `\begin`). `usize::MAX` when the count is present but is not a plain digit string RaTeX reads +/// the same way (too long, or hidden by a comment, inner spaces or a macro): refused, not skipped. +/// +/// RaTeX reads the environment name and the count through its lexer, which drops comments and +/// spaces, so they are dropped here too before the two groups are compared. fn alignat_columns(rest: &str) -> Option { - let rest = rest.trim_start().strip_prefix('{')?; - let (name, after) = rest.split_once('}')?; + // Enough for the longest name and any count that fits `usize`; a longer count is refused. + const WINDOW: usize = 64; + let mut kept = String::new(); + let mut chars = rest.chars(); + while kept.len() < WINDOW { + match chars.next() { + None => break, + Some('%') => { + for n in chars.by_ref() { + if n == '\n' { + break; + } + } + } + Some(c) if c.is_whitespace() => {} + Some(c) => kept.push(c), + } + } + let (name, after) = kept.strip_prefix('{')?.split_once('}')?; if !matches!(name, "alignat" | "alignat*" | "alignedat") { return None; } - let digits = after.trim_start().strip_prefix('{')?.split('}').next()?.trim(); + let Some(digits) = after.strip_prefix('{').and_then(|a| a.split_once('}')).map(|(d, _)| d) else { + return Some(usize::MAX); + }; if digits.is_empty() || !digits.bytes().all(|b| b.is_ascii_digit()) { - return None; + return Some(usize::MAX); } Some(digits.parse().unwrap_or(usize::MAX)) } @@ -721,6 +744,18 @@ mod tests { let r = "\\begin{alignedat}{99999999}a\\end{alignedat}"; assert!(check_caps(r).is_err_and(|e| e.message.contains("matrix cells"))); assert!(check_caps("\\begin{alignedat}{2}a&b&c&d\\end{alignedat}").is_ok()); + // A comment, inner spaces or a macro in the count do not hide it from the cap. + for hidden in [ + "\\begin{alignedat}%\n{99999999}a\\end{alignedat}", + "\\begin %c\n {alignedat}{99999999}a\\end{alignedat}", + "\\begin{alignedat}{9 9}a\\end{alignedat}", + "\\begin{alignedat}{9%x\n9}a\\end{alignedat}", + "\\begin{align%x\nedat}{99}a\\end{alignedat}", + "\\begin{alignedat}{9\\relax9}a\\end{alignedat}", + ] { + assert!(check_caps(hidden).is_err_and(|e| e.message.contains("matrix cells")), "{hidden}"); + } + assert!(check_caps("\\begin{alignedat} {2} % two pairs\na&b&c&d\\end{alignedat}").is_ok()); assert!(layout("\\begin{CD}A @>f>> B \\\\ @VVV @VVV \\\\ C @>>> D\\end{CD}").is_ok()); assert!(check_caps("\\operatorname{curl} 50\\%").is_ok()); let two = format!("{0}{0}", "\\begin{matrix}a&b\\\\c&d\\end{matrix}"); From 86c5c8dd813e05b2f9912bd3dcc4a1e9e042e5a5 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Wed, 4 Nov 2026 01:31:04 +0100 Subject: [PATCH 16/76] Math: count \newline as a row break in the matrix cell cap (#3327) RaTeX reads \newline as \\, but the source scan only counted \\ and \cr, so a matrix built with \newline skipped the 2,500 cell cap. Co-Authored-By: Claude Sonnet 5.5 --- crates/math/src/lib.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/crates/math/src/lib.rs b/crates/math/src/lib.rs index 62033f2b8..3a5b310cf 100644 --- a/crates/math/src/lib.rs +++ b/crates/math/src/lib.rs @@ -235,7 +235,7 @@ const MACRO_DEFINERS: &[&str] = /// One open `\begin…\end` environment, counted from the source text. struct Grid { - /// Rows ended so far (by `\\` or `\cr`). + /// Rows ended so far (by `\\`, `\cr` or `\newline`). rows: usize, /// Cells in the current row. cells: usize, @@ -315,7 +315,8 @@ fn check_caps(latex: &str) -> Result<(), MathError> { } None } - "cr" => { + // RaTeX reads `\newline` as `\\`, so it ends a row like `\cr` does. + "cr" | "newline" => { if let Some(g) = grids.last_mut() { g.end_row(); } @@ -783,4 +784,20 @@ mod tests { let l = layout("").unwrap(); assert!(l.items.is_empty()); } + + #[test] + fn newline_rows_count_toward_the_matrix_cap() { + // RaTeX reads `\newline` as `\\`, so it ends a row like `\cr` does. + for brk in ["\\newline ", "\\cr ", "\\\\"] { + let src = format!("\\begin{{matrix}}{}{}\\end{{matrix}}", format!("a{brk}").repeat(250), "a&".repeat(700) + "a"); + assert!(src.len() < MAX_SOURCE_BYTES); + let e = check_caps(&src).unwrap_err(); + assert!(e.message.contains("matrix cells"), "{brk}: {e}"); + let s = src.clone(); + let (l, o) = within(10, move || (layout(&s).unwrap_err(), to_omml(&s, 11.0, None).unwrap_err())); + assert!(l.message.contains("matrix cells") && o.message.contains("matrix cells"), "{brk}"); + } + assert!(check_caps("\\begin{matrix}a&b\\newline c&d\\end{matrix}").is_ok()); + assert!(layout("\\begin{matrix}a&b\\newline c&d\\end{matrix}").is_ok()); + } } From 8c1c7cfd27a22b366ce2ddb94120165b5978147e Mon Sep 17 00:00:00 2001 From: vangel76 Date: Thu, 5 Nov 2026 00:47:47 +0100 Subject: [PATCH 17/76] Manage Rules: scroll the rule list and lay out only the visible rules (#3321) The dialog laid out one row per rule with no height limit and ran home.manageRules (copying and serialising every rule, and journaling a step) on every frame. A sheet with thousands of rules made the window as tall as the whole list and the application window go blank. Co-Authored-By: Claude Sonnet 5.5 --- .../ui-egui/src/dialogs/cond_format_rules.rs | 54 ++++++++++------ crates/ui-egui/tests/it/cf_rules_manager.rs | 64 +++++++++++++++++++ crates/ui-egui/tests/it/main.rs | 1 + 3 files changed, 98 insertions(+), 21 deletions(-) create mode 100644 crates/ui-egui/tests/it/cf_rules_manager.rs diff --git a/crates/ui-egui/src/dialogs/cond_format_rules.rs b/crates/ui-egui/src/dialogs/cond_format_rules.rs index 4d8a22375..23af88aae 100644 --- a/crates/ui-egui/src/dialogs/cond_format_rules.rs +++ b/crates/ui-egui/src/dialogs/cond_format_rules.rs @@ -1,28 +1,40 @@ use super::{Json, SheetApp, json}; +/// The list scrolls inside the dialog: a sheet read from a file can carry thousands of rules, and +/// a list laid out in full made the window as tall as all of them (issue #3321). +const LIST_HEIGHT: f32 = 360.0; + pub(super) fn manage_rules(app: &mut SheetApp, ui: &mut egui::Ui) { - let list = app.session.run("home.manageRules", json!({})).unwrap_or(Json::Null); - let rules = list["rules"].as_array().cloned().unwrap_or_default(); - if rules.is_empty() { + // Read the rules in place: running `home.manageRules` for them each frame copied and + // serialised every rule and journaled a step 60 times a second. + let count = app.session.active().and_then(|d| d.wb.active()).map_or(0, |sh| sh.cond_formats.len()); + if count == 0 { ui.label("There are no conditional formatting rules on this sheet."); + return; } - for (i, r) in rules.iter().enumerate() { - ui.horizontal(|ui| { - let kind = r["rule"].as_object().and_then(|o| o.keys().next().cloned()).unwrap_or_default(); - ui.label(format!( - "{}. {kind} applies to {}", - i + 1, - r["ranges"].as_array().map(|a| a.iter().filter_map(|x| x.as_str()).collect::>().join(",")).unwrap_or_default() - )); - if ui.small_button("▲").clicked() { - app.run_or_alert("home.manageRules", json!({"moveUp": i})); - } - if ui.small_button("▼").clicked() { - app.run_or_alert("home.manageRules", json!({"moveDown": i})); - } - if ui.small_button("Delete").clicked() { - app.run_or_alert("home.manageRules", json!({"delete": i})); - } - }); + let mut action: Option = None; + let row_height = ui.spacing().interact_size.y; + egui::ScrollArea::vertical().max_height(LIST_HEIGHT).auto_shrink([false, true]).show_rows(ui, row_height, count, |ui, rows| { + let Some(sh) = app.session.active().and_then(|d| d.wb.active()) else { return }; + for i in rows { + let Some(cf) = sh.cond_formats.get(i) else { break }; + let kind = serde_json::to_value(&cf.rule).ok().and_then(|r| r.as_object().and_then(|o| o.keys().next().cloned())).unwrap_or_default(); + let ranges = cf.ranges.iter().map(|r| r.a1()).collect::>().join(","); + ui.horizontal(|ui| { + ui.label(format!("{}. {kind} applies to {ranges}", i + 1)); + if ui.small_button("▲").clicked() { + action = Some(json!({"moveUp": i})); + } + if ui.small_button("▼").clicked() { + action = Some(json!({"moveDown": i})); + } + if ui.small_button("Delete").clicked() { + action = Some(json!({"delete": i})); + } + }); + } + }); + if let Some(params) = action { + app.run_or_alert("home.manageRules", params); } } diff --git a/crates/ui-egui/tests/it/cf_rules_manager.rs b/crates/ui-egui/tests/it/cf_rules_manager.rs new file mode 100644 index 000000000..540b19403 --- /dev/null +++ b/crates/ui-egui/tests/it/cf_rules_manager.rs @@ -0,0 +1,64 @@ +//! Manage Rules on a sheet with thousands of conditional formats (issue #3321): the list scrolls +//! inside the dialog, and only the visible rules are laid out, instead of one row per rule +//! making the window as tall as the whole list and the application window go blank. + +use egui::vec2; +use egui_kittest::kittest::Queryable; +use gridcraft_engine::Session; +use gridcraft_ui_egui::SheetApp; +use serde_json::json; + +type Harness = egui_kittest::Harness<'static, SheetApp>; + +const RULES: usize = 3000; +const SCREEN_H: f32 = 800.0; + +fn harness_with_rules(n: usize) -> Harness { + let mut session = Session::new(); + session.new_workbook(); + for i in 0..n { + let range = format!("A{}", i + 1); + session + .execute( + "home.conditionalFormat", + json!({"range": range, "rule": {"type": "cellIs", "operator": "greaterThan", "value": "5", "preset": "yellowFill"}}), + ) + .unwrap(); + } + let mut h = crate::common::builder().with_size(vec2(1200.0, SCREEN_H)).build_ui_state( + |ui, app: &mut SheetApp| { + app.logic(ui.ctx()); + app.ui(ui); + }, + crate::common::app(session), + ); + h.run_steps(4); + h.state_mut().open_dialog("manageRules", json!({})); + h.run_steps(6); + h +} + +#[test] +fn many_rules_scroll_inside_a_dialog_that_fits_the_window() { + let mut h = harness_with_rules(RULES); + let deletes: Vec<_> = h.query_all_by_label("Delete").collect(); + assert!(!deletes.is_empty(), "the first rules are listed"); + // One more Delete sits in the ribbon. + assert!(deletes.len() < 100, "only the visible rules are laid out, got {}", deletes.len()); + let lowest = deletes.iter().map(|d| d.rect().bottom()).fold(0.0, f32::max); + assert!(lowest <= SCREEN_H, "the list stays inside the window, lowest button at {lowest}"); + drop(deletes); + // Deleting through the dialog removes the first rule and the dialog stays usable. + // (The ribbon has a Delete button of its own, outside the dialog's window.) + let window = h.get_by_label("Conditional Formatting Rules Manager").rect(); + h.query_all_by_label("Delete").find(|d| window.contains_rect(d.rect())).unwrap().click(); + h.run_steps(4); + let left = h.state().session.active().unwrap().wb.active().unwrap().cond_formats.len(); + assert_eq!(left, RULES - 1); +} + +#[test] +fn an_empty_sheet_says_so() { + let h = harness_with_rules(0); + h.get_by_label("There are no conditional formatting rules on this sheet."); +} diff --git a/crates/ui-egui/tests/it/main.rs b/crates/ui-egui/tests/it/main.rs index efc4fdc23..9cb395e5d 100644 --- a/crates/ui-egui/tests/it/main.rs +++ b/crates/ui-egui/tests/it/main.rs @@ -15,6 +15,7 @@ mod borders; mod cell_editor_caret; mod cell_pictures; mod cf_rule_pick; +mod cf_rules_manager; mod chart_data_table; mod chart_resize; mod chart_source_layering; From 51e242e1fa1ad79764115cb7f3427d1e539930ec Mon Sep 17 00:00:00 2001 From: vangel76 Date: Thu, 5 Nov 2026 00:52:15 +0100 Subject: [PATCH 18/76] Manage Rules test: check the dialog rect and which rule was deleted Co-Authored-By: Claude Sonnet 5.5 --- crates/ui-egui/tests/it/cf_rules_manager.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/tests/it/cf_rules_manager.rs b/crates/ui-egui/tests/it/cf_rules_manager.rs index 540b19403..d6f19a715 100644 --- a/crates/ui-egui/tests/it/cf_rules_manager.rs +++ b/crates/ui-egui/tests/it/cf_rules_manager.rs @@ -48,13 +48,17 @@ fn many_rules_scroll_inside_a_dialog_that_fits_the_window() { let lowest = deletes.iter().map(|d| d.rect().bottom()).fold(0.0, f32::max); assert!(lowest <= SCREEN_H, "the list stays inside the window, lowest button at {lowest}"); drop(deletes); + let window = h.get_by_label("Conditional Formatting Rules Manager").rect(); + assert!(window.bottom() <= SCREEN_H, "the dialog fits the window, bottom at {}", window.bottom()); // Deleting through the dialog removes the first rule and the dialog stays usable. // (The ribbon has a Delete button of its own, outside the dialog's window.) - let window = h.get_by_label("Conditional Formatting Rules Manager").rect(); h.query_all_by_label("Delete").find(|d| window.contains_rect(d.rect())).unwrap().click(); h.run_steps(4); let left = h.state().session.active().unwrap().wb.active().unwrap().cond_formats.len(); assert_eq!(left, RULES - 1); + let first = h.state().session.active().unwrap().wb.active().unwrap().cond_formats[0].ranges[0].a1(); + // New rules go first, so the one deleted was A3000. + assert_eq!(first, format!("A{}", RULES - 1), "the first rule was the one removed"); } #[test] From d7c0158f0a6de45660750d1387969b36369025ac Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 17:08:44 +0100 Subject: [PATCH 19/76] Manage Rules: let the rule list scroll sideways A long range list pushed the move and Delete buttons out of the vertical-only scroll area, where they could not be clicked. Co-Authored-By: Claude Sonnet 5.5 --- crates/ui-egui/src/dialogs/cond_format_rules.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ui-egui/src/dialogs/cond_format_rules.rs b/crates/ui-egui/src/dialogs/cond_format_rules.rs index 23af88aae..79df9897f 100644 --- a/crates/ui-egui/src/dialogs/cond_format_rules.rs +++ b/crates/ui-egui/src/dialogs/cond_format_rules.rs @@ -14,7 +14,7 @@ pub(super) fn manage_rules(app: &mut SheetApp, ui: &mut egui::Ui) { } let mut action: Option = None; let row_height = ui.spacing().interact_size.y; - egui::ScrollArea::vertical().max_height(LIST_HEIGHT).auto_shrink([false, true]).show_rows(ui, row_height, count, |ui, rows| { + egui::ScrollArea::both().max_height(LIST_HEIGHT).auto_shrink([false, true]).show_rows(ui, row_height, count, |ui, rows| { let Some(sh) = app.session.active().and_then(|d| d.wb.active()) else { return }; for i in rows { let Some(cf) = sh.cond_formats.get(i) else { break }; From 225496e5445eda6f0bd2cd7edf4cefc688b62f02 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 17:19:20 +0100 Subject: [PATCH 20/76] Math: refuse computed environment names in the alignat pair cap A name such as align\@char{97}t reads as alignat to the parser but split at the first } looked like another environment, so the pair limit was skipped. Names that contain a backslash or brace, or do not close in the window, are now refused like an unreadable count. Co-Authored-By: Claude Sonnet 5.5 --- crates/math/src/lib.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/crates/math/src/lib.rs b/crates/math/src/lib.rs index 62033f2b8..fb01b2374 100644 --- a/crates/math/src/lib.rs +++ b/crates/math/src/lib.rs @@ -392,6 +392,7 @@ const MAX_ALIGNAT_PAIRS: usize = 50; /// N of `{alignat}{N}`, `{alignat*}{N}` or `{alignedat}{N}` at the start of `rest` (just after /// `\begin`). `usize::MAX` when the count is present but is not a plain digit string RaTeX reads /// the same way (too long, or hidden by a comment, inner spaces or a macro): refused, not skipped. +/// An environment name that is computed (contains `\` or `{`) is refused the same way. /// /// RaTeX reads the environment name and the count through its lexer, which drops comments and /// spaces, so they are dropped here too before the two groups are compared. @@ -414,7 +415,16 @@ fn alignat_columns(rest: &str) -> Option { Some(c) => kept.push(c), } } - let (name, after) = kept.strip_prefix('{')?.split_once('}')?; + let group = kept.strip_prefix('{')?; + // A name that does not close inside the window cannot be read, so it is refused. + let Some((name, after)) = group.split_once('}') else { + return Some(usize::MAX); + }; + // A computed name (`align\@char{97}t` reads as `alignat`) cannot be resolved here: refused + // too. Every real environment name is letters, optionally with a trailing `*`. + if name.contains(['\\', '{']) { + return Some(usize::MAX); + } if !matches!(name, "alignat" | "alignat*" | "alignedat") { return None; } @@ -755,6 +765,14 @@ mod tests { ] { assert!(check_caps(hidden).is_err_and(|e| e.message.contains("matrix cells")), "{hidden}"); } + // A computed environment name reads as `alignat` to the parser, so it is capped too. + for name in ["align\\@char{97}t", "alignedat\\relax", "al\\@char{105}gnat*"] { + let over = format!("\\begin{{{name}}}{{51}}a\\end{{alignat}}"); + assert!(check_caps(&over).is_err_and(|e| e.message.contains("matrix cells")), "{over}"); + } + let long = format!("\\begin{{{}}}{{51}}a\\end{{alignat}}", "a".repeat(80)); + assert!(check_caps(&long).is_err_and(|e| e.message.contains("matrix cells"))); + assert!(check_caps("\\begin{alignat}{50}a\\end{alignat}").is_ok()); assert!(check_caps("\\begin{alignedat} {2} % two pairs\na&b&c&d\\end{alignedat}").is_ok()); assert!(layout("\\begin{CD}A @>f>> B \\\\ @VVV @VVV \\\\ C @>>> D\\end{CD}").is_ok()); assert!(check_caps("\\operatorname{curl} 50\\%").is_ok()); From 684f051f0da03c797ee39f89bda3708bf81f026b Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 8 Nov 2026 12:51:27 +0100 Subject: [PATCH 21/76] Manage Rules: test reaching rules by scrolling down and sideways Scroll the 3,000-rule list to a row that was off screen and move and delete that rule through its own buttons; give one rule a label of 200 ranges whose buttons start outside the dialog, scroll sideways, then move and delete it. Each test checks the intended rule changed. Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/tests/it/cf_rules_manager.rs | 117 +++++++++++++++++++- 1 file changed, 114 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/tests/it/cf_rules_manager.rs b/crates/ui-egui/tests/it/cf_rules_manager.rs index d6f19a715..013a0b28f 100644 --- a/crates/ui-egui/tests/it/cf_rules_manager.rs +++ b/crates/ui-egui/tests/it/cf_rules_manager.rs @@ -2,7 +2,7 @@ //! inside the dialog, and only the visible rules are laid out, instead of one row per rule //! making the window as tall as the whole list and the application window go blank. -use egui::vec2; +use egui::{Event, Modifiers, MouseWheelSource, MouseWheelUnit, PointerButton, Pos2, Rect, TouchPhase, Vec2, pos2, vec2}; use egui_kittest::kittest::Queryable; use gridcraft_engine::Session; use gridcraft_ui_egui::SheetApp; @@ -14,10 +14,15 @@ const RULES: usize = 3000; const SCREEN_H: f32 = 800.0; fn harness_with_rules(n: usize) -> Harness { + let ranges: Vec = (0..n).map(|i| format!("A{}", i + 1)).collect(); + harness_with_ranges(&ranges) +} + +/// One rule per entry of `ranges`, added in order (so the last one is listed first). +fn harness_with_ranges(ranges: &[String]) -> Harness { let mut session = Session::new(); session.new_workbook(); - for i in 0..n { - let range = format!("A{}", i + 1); + for range in ranges { session .execute( "home.conditionalFormat", @@ -38,6 +43,67 @@ fn harness_with_rules(n: usize) -> Harness { h } +fn dialog(h: &Harness) -> Rect { + h.get_by_label("Conditional Formatting Rules Manager").rect() +} + +/// The rows of the list shown inside the dialog: (rule number, row rectangle). +fn visible_rows(h: &Harness) -> Vec<(usize, Rect)> { + let window = dialog(h); + let mut rows: Vec<(usize, Rect)> = h + .query_all_by_label_contains("applies to") + .filter(|n| window.contains(n.rect().left_center())) + .filter_map(|n| Some((n.value()?.split('.').next()?.parse().ok()?, n.rect()))) + .collect(); + rows.sort_by(|a, b| a.1.top().total_cmp(&b.1.top())); + rows +} + +/// The row of rule `n`, wherever it is laid out (its label may reach past the dialog's edges). +fn row_of(h: &Harness, n: usize) -> Rect { + let prefix = format!("{n}. "); + h.query_all_by_label_contains("applies to") + .find(|r| r.value().is_some_and(|l| l.starts_with(&prefix))) + .expect("the rule's row is laid out") + .rect() +} + +/// The `label` button on the row at `row`, if it is inside the dialog. +fn row_button(h: &Harness, row: Rect, label: &str) -> Option { + let window = dialog(h); + h.query_all_by_label(label).map(|n| n.rect()).find(|r| window.contains_rect(*r) && (r.center().y - row.center().y).abs() < row.height() / 2.0) +} + +fn wheel(h: &mut Harness, at: Pos2, delta: Vec2) { + h.input_mut().events.push(Event::PointerMoved(at)); + h.run_steps(2); + h.input_mut().events.push(Event::MouseWheel { + unit: MouseWheelUnit::Point, + delta, + phase: TouchPhase::Move, + source: MouseWheelSource::Unknown, + modifiers: Modifiers::NONE, + }); + h.run_steps(30); +} + +fn click(h: &mut Harness, r: Rect) { + let pos = r.center(); + for pressed in [true, false] { + h.input_mut() + .events + .extend([Event::PointerMoved(pos), Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE }]); + h.step(); + } + h.run_steps(4); +} + +/// The ranges of every rule on the sheet, in list order. +fn rule_ranges(h: &Harness) -> Vec { + let sh = h.state().session.active().unwrap().wb.active().unwrap(); + sh.cond_formats.iter().map(|cf| cf.ranges.iter().map(|r| r.a1()).collect::>().join(",")).collect() +} + #[test] fn many_rules_scroll_inside_a_dialog_that_fits_the_window() { let mut h = harness_with_rules(RULES); @@ -66,3 +132,48 @@ fn an_empty_sheet_says_so() { let h = harness_with_rules(0); h.get_by_label("There are no conditional formatting rules on this sheet."); } + +#[test] +fn a_rule_scrolled_into_view_is_moved_and_deleted() { + let mut h = harness_with_rules(RULES); + let before = visible_rows(&h); + let last_shown = before.iter().map(|r| r.0).max().expect("the first rules are listed"); + // Scroll the list down, then act on a row in the middle of what is shown now. + let at = pos2(dialog(&h).left() + 40.0, before[before.len() / 2].1.center().y); + wheel(&mut h, at, vec2(0.0, -2000.0)); + let rows = visible_rows(&h); + let (n, row) = rows[rows.len() / 2]; + assert!(n > last_shown, "rule {n} was not on screen before scrolling (rules 1 to {last_shown} were)"); + // Rule n is the n-th listed; new rules go first, so it applies to A{RULES + 1 - n}. + let target = format!("A{}", RULES + 1 - n); + assert_eq!(rule_ranges(&h)[n - 1], target); + let down = row_button(&h, row, "▼").expect("the row's move-down button is in the dialog"); + click(&mut h, down); + let ranges = rule_ranges(&h); + assert_eq!(ranges[n], target, "rule {n} moved down one place"); + assert_eq!(ranges[n - 1], format!("A{}", RULES - n), "the rule below it moved up"); + // The moved rule is now rule n + 1, on the next row. + let delete = row_button(&h, row_of(&h, n + 1), "Delete").expect("the row's Delete button is in the dialog"); + click(&mut h, delete); + let ranges = rule_ranges(&h); + assert_eq!(ranges.len(), RULES - 1); + assert!(!ranges.contains(&target), "the rule scrolled to was deleted"); +} + +#[test] +fn a_rule_with_a_long_label_is_reached_by_scrolling_sideways() { + let long: Vec = (0..200).map(|i| format!("C{}", 2 * i + 1)).collect(); + let long = long.join(","); + let mut h = harness_with_ranges(&["A1".into(), long.clone(), "B1".into()]); + assert_eq!(rule_ranges(&h), ["B1", long.as_str(), "A1"]); + let row = row_of(&h, 2); + assert!(row_button(&h, row, "▼").is_none(), "the long rule's buttons start out of view"); + let window = dialog(&h); + wheel(&mut h, pos2(window.left() + 40.0, row.center().y), vec2(-20000.0, 0.0)); + let down = row_button(&h, row_of(&h, 2), "▼").expect("scrolling sideways brings the move-down button into the dialog"); + click(&mut h, down); + assert_eq!(rule_ranges(&h), ["B1", "A1", long.as_str()], "the long rule moved down"); + let delete = row_button(&h, row_of(&h, 3), "Delete").expect("the long rule's Delete button is in the dialog on its new row"); + click(&mut h, delete); + assert_eq!(rule_ranges(&h), ["B1", "A1"], "the long rule was deleted"); +} From 63150810b35539a05492c1269bb438ff4a2f7766 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 8 Nov 2026 13:21:44 +0100 Subject: [PATCH 22/76] fix(functions): a blank lookup given as an argument is not an omitted one FILTER(1,FALSE,VLOOKUP(...)) with a blank lookup result returned #CALC! because util::has read the blank value as a left-out argument. Arguments written in the formula that compute a blank are now marked as given. Co-Authored-By: Claude Opus 5.5 --- crates/calc/src/eval/call.rs | 10 ++++++---- crates/calc/src/eval/reference_fns.rs | 7 ++++--- crates/calc/src/tests.rs | 3 +++ crates/functions/src/info.rs | 2 +- crates/functions/src/lib.rs | 7 +++++-- crates/functions/src/lift.rs | 7 +------ crates/functions/src/util.rs | 2 +- 7 files changed, 21 insertions(+), 17 deletions(-) diff --git a/crates/calc/src/eval/call.rs b/crates/calc/src/eval/call.rs index 8ac58c8d1..848762ab9 100644 --- a/crates/calc/src/eval/call.rs +++ b/crates/calc/src/eval/call.rs @@ -203,9 +203,9 @@ impl<'h> Evaluator<'h> { let index = self.value(&args[0]); if let Value::Array(_) = index { let Some(spec) = gridcraft_functions::lookup("CHOOSE") else { return err(CellError::Name) }; - let mut evaluated = vec![Arg { value: index, from_ref: false }]; + let mut evaluated = vec![Arg::val(index)]; for argument in &args[1..] { - evaluated.push(Arg { value: self.value(argument), from_ref: false }); + evaluated.push(Arg::val(self.value(argument))); } let mut context = FnCtx { ev: self, criteria: None, lambda: None }; return gridcraft_functions::call(spec, &evaluated, &mut context).pipe(Ev::V); @@ -351,7 +351,9 @@ impl<'h> Evaluator<'h> { } ev => self.deref(ev), }; - evaluated.push(Arg { value, from_ref }); + // Only an argument left out (`f(a,,b)`) is omitted; one that computes a blank is given. + let blank_value = !from_ref && value == Value::Empty && lambda_at != Some(i) && !matches!(a, Expr::Missing); + evaluated.push(Arg { value, from_ref, blank_value }); } let mut ctx = FnCtx { ev: self, criteria, lambda }; gridcraft_functions::call(spec, &evaluated, &mut ctx).pipe(Ev::V) @@ -414,7 +416,7 @@ impl<'h> Evaluator<'h> { let (Some(sum), Some(count)) = (gridcraft_functions::lookup("SUM"), gridcraft_functions::lookup("COUNT")) else { return Err(err(CellError::Name)); }; - let args = [Arg { value, from_ref }]; + let args = [Arg { value, from_ref, blank_value: false }]; if kind != Agg::Count { match gridcraft_functions::call(sum, &args, &mut FnCtx { ev: self, criteria: None, lambda: None }) { Value::Number(n) => acc.sum += n, diff --git a/crates/calc/src/eval/reference_fns.rs b/crates/calc/src/eval/reference_fns.rs index 4f5154c2d..6bef99732 100644 --- a/crates/calc/src/eval/reference_fns.rs +++ b/crates/calc/src/eval/reference_fns.rs @@ -203,7 +203,7 @@ impl<'h> Evaluator<'h> { let horizontal = l.range.height() == 1 && l.range.width() > 1; let same_len = if vertical { r.range.height() == l.range.height() } else { r.range.width() == l.range.width() }; if !matches!(key, Value::Array(_)) && (vertical || horizontal) && same_len { - let mut xargs = vec![Arg { value: key, from_ref: false }, Arg { value: self.area_value(&l), from_ref: true }]; + let mut xargs = vec![Arg::val(key), Arg::reference(self.area_value(&l))]; // An empty match_mode before a given search_mode is the default (0), not the end. let given = |i: usize| args.get(i).is_some_and(|e| !matches!(e, Expr::Missing)); let last = if given(5) { @@ -215,10 +215,11 @@ impl<'h> Evaluator<'h> { }; for i in 4..=last { match args.get(i) { - Some(Expr::Missing) | None => xargs.push(Arg { value: Value::Number(0.0), from_ref: false }), + Some(Expr::Missing) | None => xargs.push(Arg::val(Value::Number(0.0))), Some(e) => { let value = self.value(e); - xargs.push(Arg { value, from_ref: false }); + let blank_value = value == Value::Empty; + xargs.push(Arg { value, from_ref: false, blank_value }); } } } diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index fffbc59a1..3c1330dbe 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -1842,6 +1842,9 @@ fn lookup_of_an_empty_cell_stays_blank() { ("D8", "=VLOOKUP(\"k\",A1:B2,2,0)", Value::Number(0.0)), // Arithmetic still reads the blank as 0. ("D9", "=VLOOKUP(\"k\",A1:B2,2,0)+1", Value::Number(1.0)), + // A blank lookup given as an argument is still given, not omitted. + ("D10", "=FILTER(1,FALSE,VLOOKUP(\"k\",A1:B2,2,FALSE))", Value::Number(0.0)), + ("D11", "=XLOOKUP(\"z\",A1:A2,B1:B2,VLOOKUP(\"k\",A1:B2,2,0))", Value::Number(0.0)), ] { t.set(at, f); assert_eq!(t.get(at), want, "{f}"); diff --git a/crates/functions/src/info.rs b/crates/functions/src/info.rs index 309f0323e..82eda5828 100644 --- a/crates/functions/src/info.rs +++ b/crates/functions/src/info.rs @@ -91,7 +91,7 @@ fn na(_a: &[Arg], _c: &mut dyn Ctx) -> R { fn n_fn(a: &[Arg], _c: &mut dyn Ctx) -> R { // N(range) reads the range's first cell, but a computed array (N(A1:A3>0), N({...})) is // converted element by element, so N can stand in for `--` inside SUMPRODUCT. - if let Some(Arg { value: Value::Array(arr), from_ref: false }) = a.first() { + if let Some(Arg { value: Value::Array(arr), from_ref: false, .. }) = a.first() { let data = arr.data.iter().map(|v| n_of(v.clone()).unwrap_or_else(Value::Error)).collect(); return Array::new(arr.rows, arr.cols, data).map(Value::from).ok_or(CellError::Calc); } diff --git a/crates/functions/src/lib.rs b/crates/functions/src/lib.rs index ccc61b026..6cb035b28 100644 --- a/crates/functions/src/lib.rs +++ b/crates/functions/src/lib.rs @@ -87,16 +87,19 @@ pub use stat::{aggregate_values, aggregate_values_k}; pub struct Arg { pub value: Value, pub from_ref: bool, + /// A written argument whose value came out blank (a lookup of an empty cell): given, not + /// omitted, although its value is `Value::Empty`. + pub blank_value: bool, } impl Arg { /// A literal (non-reference) argument. pub fn val(value: impl Into) -> Arg { - Arg { value: value.into(), from_ref: false } + Arg { value: value.into(), from_ref: false, blank_value: false } } /// An argument that came from a cell or range reference. pub fn reference(value: impl Into) -> Arg { - Arg { value: value.into(), from_ref: true } + Arg { value: value.into(), from_ref: true, blank_value: false } } } diff --git a/crates/functions/src/lift.rs b/crates/functions/src/lift.rs index ea7f07fd9..6d2e3fbc4 100644 --- a/crates/functions/src/lift.rs +++ b/crates/functions/src/lift.rs @@ -50,12 +50,7 @@ fn reject_multi_cell_refs(spec: &FnSpec, args: &[Arg]) -> Option> { if !args.iter().enumerate().any(|(i, a)| multi(i, a)) { return None; } - Some( - args.iter() - .enumerate() - .map(|(i, a)| if multi(i, a) { Arg { value: Value::Error(CellError::Value), from_ref: false } } else { a.clone() }) - .collect(), - ) + Some(args.iter().enumerate().map(|(i, a)| if multi(i, a) { Arg::val(Value::Error(CellError::Value)) } else { a.clone() }).collect()) } /// Lookups whose every call searches a lookup vector from its start, so a call lifted over `n` diff --git a/crates/functions/src/util.rs b/crates/functions/src/util.rs index 14c627937..4e7853d2c 100644 --- a/crates/functions/src/util.rs +++ b/crates/functions/src/util.rs @@ -51,7 +51,7 @@ pub(crate) fn arg(args: &[Arg], i: usize) -> R<&Arg> { /// Whether argument `i` was supplied (present and not an empty literal like `F(1,)`). pub(crate) fn has(args: &[Arg], i: usize) -> bool { - matches!(args.get(i), Some(a) if !matches!(a.value, Value::Empty) || a.from_ref) + matches!(args.get(i), Some(a) if !matches!(a.value, Value::Empty) || a.from_ref || a.blank_value) } /// Number coercion for a scalar parameter: empty = 0, TRUE = 1, numeric text parses. From 642fa97fa5360ff54a4cff82d5d415dbadcc2089 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 8 Nov 2026 14:19:05 +0100 Subject: [PATCH 23/76] fix(functions): a blank element of a lifted argument is given, not omitted CEILING.MATH(6.7,VLOOKUP({"k","k"},A1:B1,2,FALSE)) with B1 empty now gives 0 per element, as the scalar call does, instead of treating the significance as left out. Co-Authored-By: Claude Opus 5.5 --- crates/calc/src/tests.rs | 5 +++++ crates/functions/src/lift.rs | 2 ++ 2 files changed, 7 insertions(+) diff --git a/crates/calc/src/tests.rs b/crates/calc/src/tests.rs index 3c1330dbe..d29dc9653 100644 --- a/crates/calc/src/tests.rs +++ b/crates/calc/src/tests.rs @@ -1845,6 +1845,11 @@ fn lookup_of_an_empty_cell_stays_blank() { // A blank lookup given as an argument is still given, not omitted. ("D10", "=FILTER(1,FALSE,VLOOKUP(\"k\",A1:B2,2,FALSE))", Value::Number(0.0)), ("D11", "=XLOOKUP(\"z\",A1:A2,B1:B2,VLOOKUP(\"k\",A1:B2,2,0))", Value::Number(0.0)), + // The same holds per element when the call is lifted over an array of blank lookups. + ("D12", "=CEILING.MATH(6.7,VLOOKUP(\"k\",A1:B2,2,0))", Value::Number(0.0)), + ("D13", "=SUM(CEILING.MATH(6.7,VLOOKUP({\"k\",\"k\"},A1:B2,2,0)))", Value::Number(0.0)), + ("D14", "=FLOOR.MATH(6.7,VLOOKUP(\"k\",A1:B2,2,0))", Value::Number(0.0)), + ("D15", "=SUM(FLOOR.MATH(6.7,VLOOKUP({\"k\",\"k\"},A1:B2,2,0)))", Value::Number(0.0)), ] { t.set(at, f); assert_eq!(t.get(at), want, "{f}"); diff --git a/crates/functions/src/lift.rs b/crates/functions/src/lift.rs index 6d2e3fbc4..d474bf12d 100644 --- a/crates/functions/src/lift.rs +++ b/crates/functions/src/lift.rs @@ -115,6 +115,8 @@ pub(crate) fn call_lifted(spec: &FnSpec, args: &[Arg], ctx: &mut dyn Ctx) -> Val { let v = arr.get_broadcast(r, c); slot.value = if let Value::Array(inner) = &v { inner.data.first().cloned().unwrap_or(Value::Empty) } else { v }; + // A blank element of a written array argument is given, as a scalar blank is. + slot.blank_value = !slot.from_ref && matches!(slot.value, Value::Empty); } } let out = (spec.imp)(&cell_args, ctx); From 99bbd237fcfdd851dd56f26cff0efc6758151c89 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 8 Nov 2026 11:53:25 -0800 Subject: [PATCH 24/76] Updates: the updater reports installing, ready, installed and failed, and installs at exit craft-update, craft-window, craft-print and craft-ui move from craft-libs c5a1cbfb to c7c84230, a branch with storytold/craft-libs#687 and #696 merged into craft-libs main. - The check result window shows whenever the updater's status is not idle, so the first start after an update reports that it installed or why it failed. Restart to Update asks the app to quit once nothing is unsaved. - An update's close request quits the app (Quit asks about each unsaved workbook), so every workbook window closes. - on_exit starts the install helper of a ready update before the macOS process exit. --- Cargo.lock | 17 ++++----- Cargo.toml | 6 +-- apps/gridcraft/Cargo.toml | 2 +- apps/gridcraft/src/main.rs | 3 ++ crates/ui-egui/Cargo.toml | 2 +- crates/ui-egui/src/app/prefs.rs | 8 ++++ crates/ui-egui/src/updates_ui.rs | 63 +++++++++++++++++++++++--------- 7 files changed, 69 insertions(+), 32 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a39620f96..5d78dbed4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -910,12 +910,12 @@ dependencies = [ [[package]] name = "craft-fonts-build" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=c5a1cbfb5f9049f9270955caffb5bee5997aebf7#c5a1cbfb5f9049f9270955caffb5bee5997aebf7" +source = "git+https://github.com/dotCooCoo/craft-libs?rev=c7c84230631cbc01ee928f0a0e0d838b1ed612ae#c7c84230631cbc01ee928f0a0e0d838b1ed612ae" [[package]] name = "craft-i18n" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=c5a1cbfb5f9049f9270955caffb5bee5997aebf7#c5a1cbfb5f9049f9270955caffb5bee5997aebf7" +source = "git+https://github.com/dotCooCoo/craft-libs?rev=c7c84230631cbc01ee928f0a0e0d838b1ed612ae#c7c84230631cbc01ee928f0a0e0d838b1ed612ae" dependencies = [ "fixed_decimal", "icu_decimal", @@ -938,7 +938,7 @@ dependencies = [ [[package]] name = "craft-print" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=c5a1cbfb5f9049f9270955caffb5bee5997aebf7#c5a1cbfb5f9049f9270955caffb5bee5997aebf7" +source = "git+https://github.com/dotCooCoo/craft-libs?rev=c7c84230631cbc01ee928f0a0e0d838b1ed612ae#c7c84230631cbc01ee928f0a0e0d838b1ed612ae" dependencies = [ "craft-winprint-gdi", "log", @@ -952,7 +952,7 @@ dependencies = [ [[package]] name = "craft-ui" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=c5a1cbfb5f9049f9270955caffb5bee5997aebf7#c5a1cbfb5f9049f9270955caffb5bee5997aebf7" +source = "git+https://github.com/dotCooCoo/craft-libs?rev=c7c84230631cbc01ee928f0a0e0d838b1ed612ae#c7c84230631cbc01ee928f0a0e0d838b1ed612ae" dependencies = [ "craft-fonts-build", "craft-i18n", @@ -972,7 +972,7 @@ dependencies = [ [[package]] name = "craft-update" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=c5a1cbfb5f9049f9270955caffb5bee5997aebf7#c5a1cbfb5f9049f9270955caffb5bee5997aebf7" +source = "git+https://github.com/dotCooCoo/craft-libs?rev=c7c84230631cbc01ee928f0a0e0d838b1ed612ae#c7c84230631cbc01ee928f0a0e0d838b1ed612ae" dependencies = [ "chrono", "dunce", @@ -1018,7 +1018,7 @@ dependencies = [ [[package]] name = "craft-window" version = "0.1.0" -source = "git+https://github.com/storytold/craft-libs?rev=c5a1cbfb5f9049f9270955caffb5bee5997aebf7#c5a1cbfb5f9049f9270955caffb5bee5997aebf7" +source = "git+https://github.com/dotCooCoo/craft-libs?rev=c7c84230631cbc01ee928f0a0e0d838b1ed612ae#c7c84230631cbc01ee928f0a0e0d838b1ed612ae" dependencies = [ "egui", "objc2 0.6.5", @@ -1029,7 +1029,7 @@ dependencies = [ [[package]] name = "craft-winprint-gdi" version = "0.1.0" -source = "git+https://github.com/storytold/winprint.rs?rev=417ac3c4d14d3da5485a99c7d5ce8f05664ec312#417ac3c4d14d3da5485a99c7d5ce8f05664ec312" +source = "git+https://github.com/storytold/winprint.rs?rev=64e7afd4d1483512385012c340ecaaaf0f3beb7f#64e7afd4d1483512385012c340ecaaaf0f3beb7f" dependencies = [ "windows-sys 0.61.2", ] @@ -6485,7 +6485,7 @@ dependencies = [ [[package]] name = "winprint" version = "0.2.1" -source = "git+https://github.com/storytold/winprint.rs?rev=5f80b9baf6b58937c401656634e42d33f00ddf49#5f80b9baf6b58937c401656634e42d33f00ddf49" +source = "git+https://github.com/storytold/winprint.rs?rev=64e7afd4d1483512385012c340ecaaaf0f3beb7f#64e7afd4d1483512385012c340ecaaaf0f3beb7f" dependencies = [ "flate2", "fmt-derive", @@ -6935,7 +6935,6 @@ dependencies = [ "nix", "typed-path", "windows-sys 0.61.2", - "zopfli", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 594b29273..bc69f4847 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,9 +26,9 @@ gridcraft-mcp = { path = "crates/mcp" } gridcraft-pdf = { path = "crates/pdf" } gridcraft-math = { path = "crates/math" } # Every craft-libs crate (craft-update and craft-window here, craft-ui in ui-egui, craft-print in -# the app) comes from one storytold/craft-libs revision. +# the app) comes from one craft-libs revision. # craft-update: app-specific pretend versions apply to startup and manual checks. -craft-update = { git = "https://github.com/storytold/craft-libs", rev = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7", version = "=0.1.0", features = ["egui"] } +craft-update = { git = "https://github.com/dotCooCoo/craft-libs", rev = "c7c84230631cbc01ee928f0a0e0d838b1ed612ae", version = "=0.1.0", features = ["egui"] } # foundation thiserror = "2" @@ -71,7 +71,7 @@ skrifa = { version = "0.44", default-features = false, features = ["std"] } epaint_default_fonts = "0.37" # One native window per workbook: the window list, Window menu titles and the immediate-viewport # driver. Same craft-libs revision as craft-update above. -craft-window = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7", default-features = false, features = ["egui"] } +craft-window = { version = "0.1.0", git = "https://github.com/dotCooCoo/craft-libs", rev = "c7c84230631cbc01ee928f0a0e0d838b1ed612ae", default-features = false, features = ["egui"] } [workspace.lints.rust] unsafe_code = "forbid" diff --git a/apps/gridcraft/Cargo.toml b/apps/gridcraft/Cargo.toml index a7472821f..e706279ce 100644 --- a/apps/gridcraft/Cargo.toml +++ b/apps/gridcraft/Cargo.toml @@ -34,7 +34,7 @@ winit = { version = "0.30", default-features = false, features = ["x11", "waylan [target.'cfg(not(target_arch = "wasm32"))'.dependencies] # Same craft-libs revision as the workspace's craft-update and craft-window (see the root # Cargo.toml). -craft-print = { git = "https://github.com/storytold/craft-libs", rev = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7" } +craft-print = { git = "https://github.com/dotCooCoo/craft-libs", rev = "c7c84230631cbc01ee928f0a0e0d838b1ed612ae" } craft-update = { workspace = true } rfd = { workspace = true } arboard = { workspace = true, features = ["image-data"] } diff --git a/apps/gridcraft/src/main.rs b/apps/gridcraft/src/main.rs index 2aa98b6eb..cc67e241a 100644 --- a/apps/gridcraft/src/main.rs +++ b/apps/gridcraft/src/main.rs @@ -64,6 +64,9 @@ impl eframe::App for App { fn on_exit(&mut self) { print::finish_pending_jobs(); save_prefs(&self.0); + // A ready update installs once the process has exited. The macOS exit below skips + // dropping the app, so the helper is started here. + self.0.start_pending_update(); // macOS (#117): end the process here, before eframe tears the window down. AccessKit's // macOS adapter subclasses the window's content view and restores its class on teardown; // AppKit's Touch Bar support observes that view (KVO), so in the next display cycle diff --git a/crates/ui-egui/Cargo.toml b/crates/ui-egui/Cargo.toml index fd18f7041..7dccfd584 100644 --- a/crates/ui-egui/Cargo.toml +++ b/crates/ui-egui/Cargo.toml @@ -11,7 +11,7 @@ workspace = true [dependencies] # The shared capture slot (`craft_ui::capture`) comes from the one craft-libs revision every # craft-libs dependency uses (see the root Cargo.toml). -craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7" } +craft-ui = { git = "https://github.com/dotCooCoo/craft-libs", rev = "c7c84230631cbc01ee928f0a0e0d838b1ed612ae" } gridcraft-engine = { workspace = true } gridcraft-chart = { workspace = true } # `monochrome_emoji_fonts` restores the Noto Emoji and emoji-icon faces that 0.37 moved behind diff --git a/crates/ui-egui/src/app/prefs.rs b/crates/ui-egui/src/app/prefs.rs index 216e4a568..151374e6e 100644 --- a/crates/ui-egui/src/app/prefs.rs +++ b/crates/ui-egui/src/app/prefs.rs @@ -64,4 +64,12 @@ impl SheetApp { self.message = Some(("GridCraft".into(), "The web version is always the latest release.".into())); } } + + /// Starts the install helper of an update that is ready, so it installs once the process has + /// exited (a no-op on the web, and when no update is ready). Call it in `on_exit` when the + /// process ends there, since the app is then never dropped. + pub fn start_pending_update(&mut self) { + #[cfg(not(target_arch = "wasm32"))] + updates_ui::start_pending_update(self); + } } diff --git a/crates/ui-egui/src/updates_ui.rs b/crates/ui-egui/src/updates_ui.rs index 2553fbbc7..cded96c85 100644 --- a/crates/ui-egui/src/updates_ui.rs +++ b/crates/ui-egui/src/updates_ui.rs @@ -15,13 +15,11 @@ pub(crate) struct Runtime { startup: bool, /// Help ▸ Check for Updates… was chosen; the next `tick` starts it. manual: bool, - /// The result dialog of a requested check is showing. - result_open: bool, } impl Default for Runtime { fn default() -> Self { - Self { updater: Updater::new(&crate::UPDATE_APP, VERSION), startup: false, manual: false, result_open: false } + Self { updater: Updater::new(&crate::UPDATE_APP, VERSION), startup: false, manual: false } } } @@ -38,19 +36,26 @@ pub(crate) fn tick(app: &mut SheetApp, ctx: &egui::Context) { let rt = &mut app.updates_rt; if std::mem::take(&mut rt.manual) { rt.updater.check_now(ctx); - rt.result_open = true; } else if rt.startup { rt.updater.check_at_start(ctx); } rt.updater.tick(ctx); - // A requested check that found a release leaves the status idle and the prompt opens. - if matches!(rt.updater.status(), Status::Idle) { - rt.result_open = false; - } +} + +/// Starts the helper of a ready update; it installs once the process has exited. +pub(crate) fn start_pending_update(app: &mut SheetApp) { + app.updates_rt.updater.start_pending_update(); +} + +/// Whether the check result window shows: for every status but idle (a requested check, and the +/// first start after an update, which reports that it installed or why it failed), and never +/// over the startup prompt. +fn result_window_shows(status: &Status, prompt_open: bool) -> bool { + !prompt_open && !matches!(status, Status::Idle) } pub(crate) fn show(app: &mut SheetApp, ctx: &egui::Context) { - // The restart helper is scheduled before the app closes, so no update starts over unsaved work. + // The close for a ready update waits until no workbook holds unsaved work. let clean = !app.session.documents().iter().any(|d| d.is_dirty()); match app.updates_rt.updater.show_if(ctx, clean) { Action::OpenUrl(url) => { @@ -58,20 +63,42 @@ pub(crate) fn show(app: &mut SheetApp, ctx: &egui::Context) { open(&url); } } - // The guarded close path asks about unsaved work; the restart helper relaunches once the - // process has exited. - Action::Close => ctx.send_viewport_cmd(egui::ViewportCommand::Close), + // Quit closes every workbook window. The install helper starts once the app exits. + Action::Close => app.request_quit(ctx), Action::None => {} } - if app.updates_rt.result_open { - let rt = &mut app.updates_rt; + let rt = &mut app.updates_rt; + if result_window_shows(rt.updater.status(), rt.updater.prompt_open()) { match craft_update::CheckResultDialog::new(&crate::UPDATE_APP, VERSION).show(ctx, rt.updater.status()) { - CheckResultResponse::Close => { - rt.updater.dismiss_status(); - rt.result_open = false; - } + CheckResultResponse::Close => rt.updater.dismiss_status(), CheckResultResponse::CheckAgain => rt.updater.check_now(ctx), + // The next `show_if` asks the window to close once nothing is unsaved. + CheckResultResponse::RestartToUpdate => rt.updater.restart_to_update(), CheckResultResponse::None => {} } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn result_window_shows_every_status_but_idle() { + let shown = [ + Status::Checking, + Status::UpToDate, + Status::Failed("no network".into()), + Status::Installing, + Status::Ready, + Status::Managed, + Status::Updated("0.2.0".into()), + Status::InstallFailed("disk full".into()), + ]; + for status in &shown { + assert!(result_window_shows(status, false), "{status:?}"); + assert!(!result_window_shows(status, true), "{status:?} over the prompt"); + } + assert!(!result_window_shows(&Status::Idle, false)); + } +} From 3004a05e463c3aac87b8901152d81a99b63672ac Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 8 Nov 2026 19:45:34 -0800 Subject: [PATCH 25/76] Updates: take craft-update from craft-libs main at 1b34eba41 --- Cargo.lock | 14 +++++++------- Cargo.toml | 4 ++-- apps/gridcraft/Cargo.toml | 2 +- crates/ui-egui/Cargo.toml | 2 +- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5d78dbed4..cb783e546 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -910,12 +910,12 @@ dependencies = [ [[package]] name = "craft-fonts-build" version = "0.1.0" -source = "git+https://github.com/dotCooCoo/craft-libs?rev=c7c84230631cbc01ee928f0a0e0d838b1ed612ae#c7c84230631cbc01ee928f0a0e0d838b1ed612ae" +source = "git+https://github.com/storytold/craft-libs?rev=1b34eba41e62138fc2438d2734523b302840b45d#1b34eba41e62138fc2438d2734523b302840b45d" [[package]] name = "craft-i18n" version = "0.1.0" -source = "git+https://github.com/dotCooCoo/craft-libs?rev=c7c84230631cbc01ee928f0a0e0d838b1ed612ae#c7c84230631cbc01ee928f0a0e0d838b1ed612ae" +source = "git+https://github.com/storytold/craft-libs?rev=1b34eba41e62138fc2438d2734523b302840b45d#1b34eba41e62138fc2438d2734523b302840b45d" dependencies = [ "fixed_decimal", "icu_decimal", @@ -938,7 +938,7 @@ dependencies = [ [[package]] name = "craft-print" version = "0.1.0" -source = "git+https://github.com/dotCooCoo/craft-libs?rev=c7c84230631cbc01ee928f0a0e0d838b1ed612ae#c7c84230631cbc01ee928f0a0e0d838b1ed612ae" +source = "git+https://github.com/storytold/craft-libs?rev=1b34eba41e62138fc2438d2734523b302840b45d#1b34eba41e62138fc2438d2734523b302840b45d" dependencies = [ "craft-winprint-gdi", "log", @@ -952,7 +952,7 @@ dependencies = [ [[package]] name = "craft-ui" version = "0.1.0" -source = "git+https://github.com/dotCooCoo/craft-libs?rev=c7c84230631cbc01ee928f0a0e0d838b1ed612ae#c7c84230631cbc01ee928f0a0e0d838b1ed612ae" +source = "git+https://github.com/storytold/craft-libs?rev=1b34eba41e62138fc2438d2734523b302840b45d#1b34eba41e62138fc2438d2734523b302840b45d" dependencies = [ "craft-fonts-build", "craft-i18n", @@ -972,7 +972,7 @@ dependencies = [ [[package]] name = "craft-update" version = "0.1.0" -source = "git+https://github.com/dotCooCoo/craft-libs?rev=c7c84230631cbc01ee928f0a0e0d838b1ed612ae#c7c84230631cbc01ee928f0a0e0d838b1ed612ae" +source = "git+https://github.com/storytold/craft-libs?rev=1b34eba41e62138fc2438d2734523b302840b45d#1b34eba41e62138fc2438d2734523b302840b45d" dependencies = [ "chrono", "dunce", @@ -1018,7 +1018,7 @@ dependencies = [ [[package]] name = "craft-window" version = "0.1.0" -source = "git+https://github.com/dotCooCoo/craft-libs?rev=c7c84230631cbc01ee928f0a0e0d838b1ed612ae#c7c84230631cbc01ee928f0a0e0d838b1ed612ae" +source = "git+https://github.com/storytold/craft-libs?rev=1b34eba41e62138fc2438d2734523b302840b45d#1b34eba41e62138fc2438d2734523b302840b45d" dependencies = [ "egui", "objc2 0.6.5", @@ -6485,7 +6485,7 @@ dependencies = [ [[package]] name = "winprint" version = "0.2.1" -source = "git+https://github.com/storytold/winprint.rs?rev=64e7afd4d1483512385012c340ecaaaf0f3beb7f#64e7afd4d1483512385012c340ecaaaf0f3beb7f" +source = "git+https://github.com/storytold/winprint.rs?rev=51934db382f225454cf152a8657eb03789be63c4#51934db382f225454cf152a8657eb03789be63c4" dependencies = [ "flate2", "fmt-derive", diff --git a/Cargo.toml b/Cargo.toml index bc69f4847..1941c9d1e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,7 +28,7 @@ gridcraft-math = { path = "crates/math" } # Every craft-libs crate (craft-update and craft-window here, craft-ui in ui-egui, craft-print in # the app) comes from one craft-libs revision. # craft-update: app-specific pretend versions apply to startup and manual checks. -craft-update = { git = "https://github.com/dotCooCoo/craft-libs", rev = "c7c84230631cbc01ee928f0a0e0d838b1ed612ae", version = "=0.1.0", features = ["egui"] } +craft-update = { git = "https://github.com/storytold/craft-libs", rev = "1b34eba41e62138fc2438d2734523b302840b45d", version = "=0.1.0", features = ["egui"] } # foundation thiserror = "2" @@ -71,7 +71,7 @@ skrifa = { version = "0.44", default-features = false, features = ["std"] } epaint_default_fonts = "0.37" # One native window per workbook: the window list, Window menu titles and the immediate-viewport # driver. Same craft-libs revision as craft-update above. -craft-window = { version = "0.1.0", git = "https://github.com/dotCooCoo/craft-libs", rev = "c7c84230631cbc01ee928f0a0e0d838b1ed612ae", default-features = false, features = ["egui"] } +craft-window = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "1b34eba41e62138fc2438d2734523b302840b45d", default-features = false, features = ["egui"] } [workspace.lints.rust] unsafe_code = "forbid" diff --git a/apps/gridcraft/Cargo.toml b/apps/gridcraft/Cargo.toml index e706279ce..75acee3ff 100644 --- a/apps/gridcraft/Cargo.toml +++ b/apps/gridcraft/Cargo.toml @@ -34,7 +34,7 @@ winit = { version = "0.30", default-features = false, features = ["x11", "waylan [target.'cfg(not(target_arch = "wasm32"))'.dependencies] # Same craft-libs revision as the workspace's craft-update and craft-window (see the root # Cargo.toml). -craft-print = { git = "https://github.com/dotCooCoo/craft-libs", rev = "c7c84230631cbc01ee928f0a0e0d838b1ed612ae" } +craft-print = { git = "https://github.com/storytold/craft-libs", rev = "1b34eba41e62138fc2438d2734523b302840b45d" } craft-update = { workspace = true } rfd = { workspace = true } arboard = { workspace = true, features = ["image-data"] } diff --git a/crates/ui-egui/Cargo.toml b/crates/ui-egui/Cargo.toml index 7dccfd584..b4c049afa 100644 --- a/crates/ui-egui/Cargo.toml +++ b/crates/ui-egui/Cargo.toml @@ -11,7 +11,7 @@ workspace = true [dependencies] # The shared capture slot (`craft_ui::capture`) comes from the one craft-libs revision every # craft-libs dependency uses (see the root Cargo.toml). -craft-ui = { git = "https://github.com/dotCooCoo/craft-libs", rev = "c7c84230631cbc01ee928f0a0e0d838b1ed612ae" } +craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "1b34eba41e62138fc2438d2734523b302840b45d" } gridcraft-engine = { workspace = true } gridcraft-chart = { workspace = true } # `monochrome_emoji_fonts` restores the Noto Emoji and emoji-icon faces that 0.37 moved behind From 7102429a9a2fd9f6fba8e06238a1cce5807fb1b0 Mon Sep 17 00:00:00 2001 From: Valentin Klein Date: Mon, 9 Nov 2026 08:46:53 +0100 Subject: [PATCH 26/76] perf(grid): skip hidden row runs and keep sheet geometry and CF statistics between frames (#3580) (#3629) A sheet whose filter hides a million rows walked every hidden row several times per frame (cells, gridlines, headers), rebuilt the row index, the outline and the used range every frame, and a conditional format over a whole column rescanned it every frame for its statistics. - LineIndex::next_visible / visible step over a run of hidden lines in one O(log n) search; the grid's row and column loops use them. - The grid keeps the active sheet's line index, outlines, used range and CfCache between frames, tied to the sheet by a Weak: any change goes through Arc::make_mut, which moves or copies a sheet that has a weak reference, so the cache can't outlive a change. - Scroll offsets for the scrollbars and the scroll limit come from the line index instead of walking every custom row. Co-authored-by: Claude Opus 5.5 --- crates/model/src/sheet.rs | 99 ++++++++++++++++++++++++++++- crates/ui-egui/src/grid.rs | 8 +++ crates/ui-egui/src/grid/geo.rs | 67 ++++++++++++++++--- crates/ui-egui/src/grid/quadrant.rs | 24 ++----- crates/ui-egui/src/grid/show.rs | 50 ++++++++++++--- crates/ui-egui/src/grid/tests.rs | 83 ++++++++++++++++++++++++ 6 files changed, 294 insertions(+), 37 deletions(-) diff --git a/crates/model/src/sheet.rs b/crates/model/src/sheet.rs index 440647d42..409e93023 100644 --- a/crates/model/src/sheet.rs +++ b/crates/model/src/sheet.rs @@ -566,7 +566,9 @@ impl LineIndex { let mut prev = 0u32; for (&line, info) in custom { pos += (line - prev) as f64 * default; - let size = if info.hidden { 0.0 } else { info.size.map_or(default, |s| s as f64) }; + // A negative size (only a malformed file has one) counts as zero, so positions only + // grow and the searches below stay valid. + let size = if info.hidden { 0.0 } else { info.size.map_or(default, |s| s as f64).max(0.0) }; lines.push((line, pos, size)); pos += size; prev = line + 1; @@ -584,6 +586,44 @@ impl LineIndex { }, } } + /// The first line at or after `line` (and before the axis end) with a size above zero, or + /// `None` when every line from there on is hidden or zero-sized. O(log n): a run of hidden + /// lines all start where the next shown line starts, so one search steps over the whole run. + pub fn next_visible(&self, line: u32) -> Option { + if line >= self.max { + return None; + } + let i = self.lines.partition_point(|e| e.0 < line); + let custom = self.lines.get(i).is_some_and(|e| e.0 == line); + if !custom && self.default > 0.0 { + return Some(line); + } + // Every line from `line` up to the answer is zero-sized, so they all start at `p`; the + // last custom line starting at `p` either has a size (the answer) or is the end of the run. + let p = self.start(line); + let k = self.lines.partition_point(|e| e.1 <= p); + let &(last, _, size) = self.lines.get(k.checked_sub(1)?)?; + if last < line { + return None; + } + if size > 0.0 { + return Some(last).filter(|l| *l < self.max); + } + // The line after the run has no custom line (a custom one would start at `p` too and be + // part of the run): it is shown when the default size is. + let next = last.checked_add(1).filter(|n| *n < self.max)?; + (self.default > 0.0).then_some(next) + } + /// The lines from `first` to `last` (inclusive) with a size above zero, in order. Runs of + /// hidden lines cost one O(log n) step each, not one per line. + pub fn visible(&self, first: u32, last: u32) -> impl Iterator + '_ { + let mut next = self.next_visible(first); + std::iter::from_fn(move || { + let line = next.filter(|l| *l <= last)?; + next = line.checked_add(1).and_then(|n| self.next_visible(n)); + Some(line) + }) + } /// The line containing `pos`. pub fn at(&self, pos: f64) -> u32 { if pos <= 0.0 { @@ -644,6 +684,63 @@ mod tests { use super::*; use crate::cell::Formula; + /// `LineIndex::visible` lists exactly the lines a line-by-line size check shows, for hidden + /// runs at the start, in the middle and up to the end of the axis, custom sizes (zero and + /// negative included), lines with only a style, and a zero default size. + #[test] + fn visible_lines_match_a_line_by_line_check() { + let max = 400u32; + let mut seed = 0x2545_f491_4f6c_dd1du64; + let mut rnd = |n: u64| { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + seed % n + }; + for case in 0..300 { + let mut custom = BTreeMap::new(); + let runs = rnd(6); + for _ in 0..runs { + let start = rnd(u64::from(max)) as u32; + let len = rnd(if case % 3 == 0 { 400 } else { 30 }) as u32; + for l in start..(start + len).min(max) { + custom.insert(l, LineInfo { hidden: true, ..Default::default() }); + } + } + // Runs that start at the first line or end at the last one. + if case % 5 == 0 { + for l in 0..rnd(20) as u32 { + custom.insert(l, LineInfo { hidden: true, ..Default::default() }); + } + } + if case % 5 == 1 { + for l in max - rnd(20) as u32 - 1..max { + custom.insert(l, LineInfo { hidden: true, ..Default::default() }); + } + } + for _ in 0..rnd(8) { + let size = [Some(0.0), Some(3.0), Some(20.0), Some(-5.0), None][rnd(5) as usize]; + custom.insert(rnd(u64::from(max)) as u32, LineInfo { size, ..Default::default() }); + } + let default = if case % 7 == 0 { 0.0 } else { 15.0 }; + let size = |l: u32| match custom.get(&l) { + Some(i) if i.hidden => 0.0, + Some(LineInfo { size: Some(s), .. }) => s.max(0.0), + _ => default, + }; + let index = LineIndex::new(&custom, default, max); + for (first, last) in [(0, max - 1), (rnd(u64::from(max)) as u32, max - 1), (5, 5), (max - 1, max - 1)] { + let want: Vec = (first..=last).filter(|&l| size(l) > 0.0).collect(); + assert_eq!(index.visible(first, last).collect::>(), want, "case {case}, {first}..={last}"); + } + assert_eq!(index.next_visible(max), None); + } + // The row after a million hidden rows comes straight after the last shown one. + let custom: BTreeMap = (47..1_048_422).map(|r| (r, LineInfo { hidden: true, ..Default::default() })).collect(); + let index = LineIndex::new(&custom, 15.0, 1_048_576); + assert_eq!(index.visible(40, 1_048_424).collect::>(), [40, 41, 42, 43, 44, 45, 46, 1_048_422, 1_048_423, 1_048_424]); + } + #[test] fn a_sheet_keeps_its_identity_through_clones_and_a_new_sheet_gets_its_own() { let a = Sheet::new("A"); diff --git a/crates/ui-egui/src/grid.rs b/crates/ui-egui/src/grid.rs index f0ae6270e..1a5d71621 100644 --- a/crates/ui-egui/src/grid.rs +++ b/crates/ui-egui/src/grid.rs @@ -235,6 +235,14 @@ impl Trace { #[derive(Default)] pub struct GridState { + /// The active sheet's line positions and outlines, kept with a weak reference to the sheet + /// they were built from: while it still upgrades to the very sheet shown, the sheet hasn't + /// changed (every change goes through `Arc::make_mut`, which moves or copies a sheet that + /// has a weak reference to it), so they still hold. + pub(crate) lines: Option<(std::sync::Weak, geo::SheetLines)>, + /// The conditional formatting statistics (minimum, maximum, percentiles, duplicates) of the + /// active sheet's rules, kept the same way: they are read from that sheet's cells alone. + pub(crate) cf: Option<(std::sync::Weak, CfCache)>, pub drag: Drag, pub ensure_visible: bool, /// With `ensure_visible`: scroll to the moving end of the selection instead of the active cell diff --git a/crates/ui-egui/src/grid/geo.rs b/crates/ui-egui/src/grid/geo.rs index a1005b32f..7786f4048 100644 --- a/crates/ui-egui/src/grid/geo.rs +++ b/crates/ui-egui/src/grid/geo.rs @@ -24,28 +24,60 @@ pub struct Geo { /// vertical centre line on screen: painted shapes by `mirror_painted`, widgets by /// `mirror_rect`/`mirror_pos`, and pointer input back by `mirror_pos` (an involution). pub rtl: bool, - /// Prefix sums over custom row heights / column widths, built once per frame. + /// Prefix sums over custom row heights / column widths, from [`SheetLines`]. pub(super) ri: std::sync::Arc, pub(super) ci: std::sync::Arc, + /// The sheet's used range, from [`SheetLines`]. + pub(super) used: Option, /// Page Layout view: cells sit on pages (`scroll` is then in page-layout units). pub paper: Option>, } +/// The parts of the grid's geometry that depend on the sheet alone: where each row and column +/// starts, and the row and column outlines. Building them walks every row and column that has a +/// size, a style or an outline level (a million rows in a file whose filter hides most of them), +/// so the grid keeps them while the sheet stays the same (`show::sheet_lines`). +#[derive(Clone)] +pub struct SheetLines { + pub(super) ri: Arc, + pub(super) ci: Arc, + pub(super) row_outline: Arc, + pub(super) col_outline: Arc, + /// The sheet's used range (it walks every stored row): how far the scrollbars reach. + pub(super) used: Option, +} + +impl SheetLines { + pub fn of(sh: &Sheet) -> SheetLines { + SheetLines { + ri: Arc::new(LineIndex::new(&sh.rows, sh.default_row_height, MAX_ROWS)), + ci: Arc::new(LineIndex::new(&sh.cols, sh.default_col_width, MAX_COLS)), + row_outline: Arc::new(crate::outline::Axis::of(sh, true)), + col_outline: Arc::new(crate::outline::Axis::of(sh, false)), + used: sh.used_range(), + } + } +} + impl Geo { pub fn new(sh: &Sheet, rect: Rect, scroll: egui::Vec2) -> Geo { + Geo::with_lines(sh, rect, scroll, &SheetLines::of(sh)) + } + /// [`Geo::new`] with the sheet's `lines` already built (`SheetLines::of(sh)`). + pub fn with_lines(sh: &Sheet, rect: Rect, scroll: egui::Vec2, lines: &SheetLines) -> Geo { let zoom = (sh.zoom as f32 / 100.0).clamp(0.1, 4.0); let z = zoom * DISPLAY_SCALE; let header_h = if sh.show_headings { (COL_HEADER_H * zoom.max(0.75)).round() } else { 0.0 }; - let ri = std::sync::Arc::new(LineIndex::new(&sh.rows, sh.default_row_height, MAX_ROWS)); - let ci = std::sync::Arc::new(LineIndex::new(&sh.cols, sh.default_col_width, MAX_COLS)); + let ri = lines.ri.clone(); + let ci = lines.ci.clone(); let last_row = ri.at((scroll.y + rect.height() / z) as f64 + 200.0) + 1; let digits = (last_row as f32).log10().floor() as i32 + 1; let header_w = if sh.show_headings { ((digits.max(2) as f32 * 7.0 + 14.0) * zoom.max(0.75)).round() } else { 0.0 }; let (fr, fc) = sh.freeze.unwrap_or((0, 0)); let frozen_w = (ci.start(fc) as f32) * z; let frozen_h = (ri.start(fr) as f32) * z; - let row_outline = Arc::new(crate::outline::Axis::of(sh, true)); - let col_outline = Arc::new(crate::outline::Axis::of(sh, false)); + let row_outline = lines.row_outline.clone(); + let col_outline = lines.col_outline.clone(); // Show Outline Symbols off: no bands (the outline's hidden lines stay hidden). let symbols = sh.show_outline_symbols; let mut band_w = if symbols { crate::outline::band(row_outline.levels, zoom) } else { 0.0 }; @@ -76,6 +108,7 @@ impl Geo { rtl: sh.right_to_left, ri, ci, + used: lines.used, paper: None, } } @@ -90,7 +123,11 @@ impl Geo { /// Page Layout view geometry: `rect` excludes the rulers; cells are drawn at the print scale /// on pages laid out by `grid`; frozen panes don't apply (as in Excel). pub fn paged(sh: &Sheet, rect: Rect, scroll: egui::Vec2, grid: Arc) -> Geo { - let mut g = Geo::new(sh, rect, scroll); + Geo::paged_with_lines(sh, rect, scroll, grid, &SheetLines::of(sh)) + } + /// [`Geo::paged`] with the sheet's `lines` already built. + pub fn paged_with_lines(sh: &Sheet, rect: Rect, scroll: egui::Vec2, grid: Arc, lines: &SheetLines) -> Geo { + let mut g = Geo::with_lines(sh, rect, scroll, lines); let zoom = (sh.zoom as f32 / 100.0).clamp(0.1, 4.0); g.z = zoom * DISPLAY_SCALE * grid.scale.clamp(0.1, 4.0); (g.fr, g.fc, g.frozen_w, g.frozen_h) = (0, 0, 0.0, 0.0); @@ -111,17 +148,17 @@ impl Geo { } } /// Scroll offset (in `scroll` units) at which `row` reaches the top of the scrolled area. - pub fn scroll_top_of(&self, sh: &Sheet, row: u32) -> f32 { + pub fn scroll_top_of(&self, _sh: &Sheet, row: u32) -> f32 { match &self.paper { Some(pg) => pg.row_y(row) as f32, - None => (sh.row_top(row) - sh.row_top(self.fr)) as f32, + None => (self.ri.start(row) - self.ri.start(self.fr)) as f32, } } /// Scroll offset (in `scroll` units) at which `col` reaches the left of the scrolled area. - pub fn scroll_left_of(&self, sh: &Sheet, col: u32) -> f32 { + pub fn scroll_left_of(&self, _sh: &Sheet, col: u32) -> f32 { match &self.paper { Some(pg) => pg.col_x(col) as f32, - None => (sh.col_left(col) - sh.col_left(self.fc)) as f32, + None => (self.ci.start(col) - self.ci.start(self.fc)) as f32, } } /// Screen x for a logical x (and back): identity unless the sheet is right-to-left. @@ -204,6 +241,16 @@ impl Geo { pub fn cell_at(&self, sh: &Sheet, p: Pos2) -> CellRef { CellRef::new(self.row_at(sh, p.y), self.col_at(sh, p.x)) } + /// The rows from `r0` to `r1` (inclusive) with a height above zero, in order. A run of + /// hidden rows costs one O(log n) step, so a filter that hides a million rows doesn't make + /// every frame walk them. + pub(super) fn shown_rows(&self, r0: u32, r1: u32) -> impl Iterator + '_ { + self.ri.visible(r0, r1) + } + /// The columns from `c0` to `c1` (inclusive) with a width above zero (see `shown_rows`). + pub(super) fn shown_cols(&self, c0: u32, c1: u32) -> impl Iterator + '_ { + self.ci.visible(c0, c1) + } /// Visible scrolled rows (from the first visible row after the frozen ones). pub(super) fn scroll_rows(&self, _sh: &Sheet) -> (u32, u32) { let top = self.ri.start(self.fr) + self.scroll.y as f64; diff --git a/crates/ui-egui/src/grid/quadrant.rs b/crates/ui-egui/src/grid/quadrant.rs index 7461c2204..e209e4f4a 100644 --- a/crates/ui-egui/src/grid/quadrant.rs +++ b/crates/ui-egui/src/grid/quadrant.rs @@ -150,12 +150,12 @@ fn paint_gridlines(q: &QuadrantPaint<'_>) { let bottom = geo.y_end(sh, r1); let left = geo.x(sh, c0); let right = geo.x_end(sh, c1); - let xs = (c0..=c1).filter(|&c| sh.col_width(c) > 0.0).map(|c| geo.x(sh, c)).chain([right]); + let xs = geo.shown_cols(c0, c1).map(|c| geo.x(sh, c)).chain([right]); for x in xs { let x = x.round() - 0.5; p.line_segment([pos2(x, top), pos2(x, bottom)], stroke); } - let ys = (r0..=r1).filter(|&r| sh.row_height(r) > 0.0).map(|r| geo.y(sh, r)).chain([bottom]); + let ys = geo.shown_rows(r0, r1).map(|r| geo.y(sh, r)).chain([bottom]); for y in ys { let y = y.round() - 0.5; p.line_segment([pos2(left, y), pos2(right, y)], stroke); @@ -164,21 +164,14 @@ fn paint_gridlines(q: &QuadrantPaint<'_>) { } fn visible_cells(q: &QuadrantPaint<'_>) -> Vec { - let sh = q.sheet; + let geo = q.geo; let (r0, r1) = q.rows; let (c0, c1) = q.cols; // Merged areas: blank them (no inner gridlines) and paint as one cell. + let cols: Vec = geo.shown_cols(c0, c1).collect(); let mut cells: Vec = Vec::new(); - for r in r0..=r1 { - if sh.row_height(r) == 0.0 { - continue; - } - for c in c0..=c1 { - if sh.col_width(c) == 0.0 { - continue; - } - cells.push(CellRef::new(r, c)); - } + for r in geo.shown_rows(r0, r1) { + cells.extend(cols.iter().map(|&c| CellRef::new(r, c))); } cells } @@ -225,10 +218,7 @@ fn outside_text_cells(q: &mut QuadrantPaint<'_>, merges: &[RangeRef]) -> Vec<(Ce // Text cells just outside the quadrant whose text may overflow into it: the nearest non-empty // cell left of `c0` and right of `c1` in each row (within the same frozen / scrolled pane). let mut outside: Vec<(CellRef, Rect, Look, Option)> = Vec::new(); - for r in r0..=r1 { - if sh.row_height(r) == 0.0 { - continue; - } + for r in geo.shown_rows(r0, r1) { let left = (c0.saturating_sub(20)..c0).rev().map(|col| CellRef::new(r, col)).find(|&n| !empty_ok(n)); let right = (c1.saturating_add(1)..=c1.saturating_add(20).min(MAX_COLS - 1)).map(|col| CellRef::new(r, col)).find(|&n| !empty_ok(n)); for n in [left, right].into_iter().flatten() { diff --git a/crates/ui-egui/src/grid/show.rs b/crates/ui-egui/src/grid/show.rs index c1c9011e7..34b76c982 100644 --- a/crates/ui-egui/src/grid/show.rs +++ b/crates/ui-egui/src/grid/show.rs @@ -15,6 +15,33 @@ use super::{ #[cfg(test)] use super::{CellRef, Pos2}; +/// The line positions and outlines of sheet `si`: the ones built for it before while it is the +/// same sheet, else built now and kept (see `GridState::lines`). +fn sheet_lines(app: &mut SheetApp, wb: &Workbook, si: usize) -> Option { + let sheet = wb.sheets.get(si)?; + if let Some((built_from, lines)) = &app.grid.lines + && is_sheet(built_from, wb, si) + { + return Some(lines.clone()); + } + let lines = super::geo::SheetLines::of(sheet); + app.grid.lines = Some((Arc::downgrade(sheet), lines.clone())); + Some(lines) +} + +/// Whether `built_from` still is sheet `si` of `wb`, unchanged (see `GridState::lines`). +fn is_sheet(built_from: &std::sync::Weak, wb: &Workbook, si: usize) -> bool { + wb.sheets.get(si).is_some_and(|s| built_from.upgrade().is_some_and(|b| Arc::ptr_eq(&b, s))) +} + +/// The conditional formatting cache kept for sheet `si` while it is unchanged, else a new one. +fn kept_cf(app: &mut SheetApp, wb: &Workbook, si: usize) -> CfCache { + match app.grid.cf.take() { + Some((built_from, cf)) if is_sheet(&built_from, wb, si) => cf, + _ => CfCache::new(), + } +} + pub fn show(app: &mut SheetApp, ui: &mut egui::Ui) { let t = Tokens::get(ui.ctx()); let available = ui.available_rect_before_wrap(); @@ -44,15 +71,16 @@ pub fn show(app: &mut SheetApp, ui: &mut egui::Ui) { } else { Rect::from_min_max(pos2((rect.left() + ruler).min(rect.right()), (rect.top() + ruler).min(rect.bottom())), rect.max) }; + let Some(lines) = sheet_lines(app, &wb, si) else { return }; let make_geo = |app: &mut SheetApp, scroll: egui::Vec2| -> Geo { if paper { - let probe = Geo::new(sh, grid_rect, scroll); + let probe = Geo::with_lines(sh, grid_rect, scroll, &lines); if let Some(g) = crate::paper_view::grid_for(app, &wb, si, sh, probe.cells.size(), scroll) { let g = crate::paper_view::with_dragged_margin(app, &wb, si, g); - return Geo::paged(sh, grid_rect, scroll, g); + return Geo::paged_with_lines(sh, grid_rect, scroll, g, &lines); } } - Geo::new(sh, rect, scroll) + Geo::with_lines(sh, rect, scroll, &lines) }; // Scrolling. let (scroll_delta, zoom_delta, modifiers, now) = ui.input(|i| (i.smooth_scroll_delta, i.zoom_delta(), i.modifiers, i.time)); @@ -115,7 +143,7 @@ pub fn show(app: &mut SheetApp, ui: &mut egui::Ui) { } update_zoom_glide(app, ui, place, sh.zoom, now); view.scroll = view.scroll.max(egui::Vec2::ZERO); - let (mut max_x, mut max_y) = (sh.col_left(MAX_COLS - 20) as f32, sh.row_top(MAX_ROWS - 40) as f32); + let (mut max_x, mut max_y) = (lines.ci.start(MAX_COLS - 20) as f32, lines.ri.start(MAX_ROWS - 40) as f32); if paper { // Page layout units run past sheet units (margins and gaps); the end is known once the // pages reach the last row / column. @@ -146,7 +174,10 @@ pub fn show(app: &mut SheetApp, ui: &mut egui::Ui) { painter.rect_filled(rect, 0.0, t.grid_bg); // Everything up to the headers is painted in logical (left-to-right) coordinates. let mirror_from = painter.add(egui::Shape::Noop); - let mut cf = CfCache::new(); + // A rule over a whole column scans it for its statistics: done once per change of the sheet, + // not every frame. + let cf_sheet = wb.sheets.get(si).map(Arc::downgrade); + let mut cf = kept_cf(app, &wb, si); let quads = if geo.paper.is_some() { crate::paper_view::paint_pages( crate::paper_view::PagePaintInput { @@ -168,6 +199,7 @@ pub fn show(app: &mut SheetApp, ui: &mut egui::Ui) { &mut cf, ) }; + app.grid.cf = cf_sheet.map(|s| (s, cf)); // Objects above cells. A hidden object can't stay selected: Delete or the Format pane would act // on something the user can't see. if crate::chartview::objects_hidden(&wb) { @@ -448,7 +480,7 @@ fn vertical_scrollbar(app: &mut SheetApp, ui: &mut egui::Ui, sh: &Sheet, geo: &G } let visible = track.height() / geo.z; let scroll = app.view().scroll.y; - let last = sh.used_range().map(|r| r.end.row.saturating_add(10)).unwrap_or(100).clamp(100, MAX_ROWS - 1); + let last = geo.used.map(|r| r.end.row.saturating_add(10)).unwrap_or(100).clamp(100, MAX_ROWS - 1); let content = geo.scroll_top_of(sh, last); let extent = (content - visible).max(scroll).max(0.0).min(limit); let id = ui.scope_id().with("vscroll"); @@ -501,7 +533,7 @@ pub(crate) fn horizontal_scrollbar(app: &mut SheetApp, ui: &mut egui::Ui, track: let Some(sh) = wb.active() else { return }; let visible = ((geo.cells.width() - geo.frozen_w) / geo.z).max(0.0); let scroll = app.view().scroll.x; - let last = sh.used_range().map(|r| r.end.col.saturating_add(10)).unwrap_or(26).clamp(26, MAX_COLS - 1); + let last = geo.used.map(|r| r.end.col.saturating_add(10)).unwrap_or(26).clamp(26, MAX_COLS - 1); let content = geo.scroll_left_of(sh, last); let extent = (content - visible).max(scroll).max(0.0); let id = ui.scope_id().with("hscroll"); @@ -574,7 +606,7 @@ pub(crate) fn paint_headers( for (rows, cols, clip) in quads { let pc = p.with_clip_rect(Rect::from_min_max(pos2(clip.left(), top.top()), pos2(clip.right(), top.bottom()))); if cols.0 <= cols.1 { - for c in cols.0..=cols.1 { + for c in geo.shown_cols(cols.0, cols.1) { if !done_cols.insert((c, clip.left() as i32)) { continue; } @@ -615,7 +647,7 @@ pub(crate) fn paint_headers( } let pr = p.with_clip_rect(Rect::from_min_max(pos2(left.left(), clip.top()), pos2(left.right(), clip.bottom()))); if rows.0 <= rows.1 { - for rr in rows.0..=rows.1 { + for rr in geo.shown_rows(rows.0, rows.1) { if !done_rows.insert((rr, clip.top() as i32)) { continue; } diff --git a/crates/ui-egui/src/grid/tests.rs b/crates/ui-egui/src/grid/tests.rs index 427c67629..be3ca6eb5 100644 --- a/crates/ui-egui/src/grid/tests.rs +++ b/crates/ui-egui/src/grid/tests.rs @@ -350,3 +350,86 @@ fn upward_text_with_middle_align_is_centred_in_a_merged_block() { assert!((bb.center().y - rect.center().y).abs() < 0.01, "{bb:?} not centred in {rect:?}"); assert!((bb.center().x - rect.center().x).abs() < 0.01, "{bb:?} not centred in {rect:?}"); } + +/// One grid frame of `app` in a headless context. +fn grid_frame(app: &mut crate::SheetApp) { + let ctx = egui::Context::default(); + let screen = egui::Rect::from_min_size(egui::pos2(0.0, 0.0), egui::vec2(1200.0, 800.0)); + let mut out = ctx.run_ui(egui::RawInput { screen_rect: Some(screen), ..Default::default() }, |ui| super::show(app, ui)); + out.textures_delta.clear(); // nothing consumes the uploads in a headless test +} + +/// The line positions, used range and conditional formatting statistics the grid keeps between +/// frames are rebuilt after every change of the sheet, whichever way it is changed, and match +/// what a fresh build gives. +#[test] +fn kept_sheet_geometry_and_cf_statistics_follow_every_change() { + use gridcraft_engine::cf::CfCache; + use gridcraft_engine::core::CellRef; + let mut session = gridcraft_engine::Session::new(); + session.new_workbook(); + let mut app = crate::SheetApp::new(session, Default::default()); + app.run("range.setValues", json!({"range": "A1:A3", "values": [[1], [2], [3]]})).unwrap(); + app.run("home.conditionalFormat", json!({"range": "A:A", "rule": {"type": "colorScale", "colors": ["#FFFFFF", "#FF0000"]}})).unwrap(); + let kept = |app: &crate::SheetApp| app.grid.lines.as_ref().map(|(_, l)| (Arc::clone(&l.ri), l.used)).unwrap(); + // The kept statistics give the look a fresh cache gives. + let check_cf = |app: &mut crate::SheetApp| { + let wb = app.session.active().unwrap().wb.clone(); + for row in 0..4 { + let c = CellRef::new(row, 0); + let kept = app.grid.cf.as_mut().map(|(_, cf)| cf.look(&wb, 0, c)); + assert_eq!(kept, Some(CfCache::new().look(&wb, 0, c)), "A{}", row + 1); + } + }; + grid_frame(&mut app); + let (ri, used) = kept(&app); + assert_eq!(used.map(|r| r.a1()).as_deref(), Some("A1:A3")); + check_cf(&mut app); + grid_frame(&mut app); + assert!(Arc::ptr_eq(&kept(&app).0, &ri), "an unchanged sheet keeps its line positions"); + + // A command: the new value moves the colour scale's maximum and the used range. + app.run("cell.set", json!({"cell": "A5", "input": "30"})).unwrap(); + grid_frame(&mut app); + assert_eq!(kept(&app).1.map(|r| r.a1()).as_deref(), Some("A1:A5")); + check_cf(&mut app); + // Hiding a row moves the rows under it. + app.run("selection.set", json!({"range": "A2"})).unwrap(); + app.run("home.hideRows", json!({})).unwrap(); + grid_frame(&mut app); + let geo = app.grid.geo.clone().unwrap(); + let sh = app.session.active().unwrap().wb.sheet(0).unwrap().clone(); + assert_eq!(geo.y(&sh, 2), geo.y(&sh, 1), "row 2 is hidden"); + assert!(!Arc::ptr_eq(&kept(&app).0, &ri)); + // Undo brings the row back. + app.run("edit.undo", json!({})).unwrap(); + grid_frame(&mut app); + let geo = app.grid.geo.clone().unwrap(); + let sh = app.session.active().unwrap().wb.sheet(0).unwrap().clone(); + assert!(geo.y(&sh, 2) > geo.y(&sh, 1), "row 2 is shown again"); + let fresh = super::geo::SheetLines::of(&sh); + for row in [0, 1, 2, 3, 100, 1_048_575] { + assert_eq!(kept(&app).0.start(row), fresh.ri.start(row), "row {row}"); + } + assert_eq!(kept(&app).1, fresh.used); + + // A change with nothing else holding the sheet (no undo step, no saved copy): the grid's weak + // reference makes `Arc::make_mut` move the sheet to a new allocation, and the grid notices. + let ri = kept(&app).0; + { + let d = app.session.active_mut().unwrap(); + d.undo.clear(); + d.redo.clear(); + d.saved = Arc::new(gridcraft_engine::model::Workbook::default()); + let sheet = Arc::make_mut(&mut d.wb).sheet_mut(0).unwrap(); + sheet.rows.entry(0).or_default().hidden = true; + sheet.rows.entry(1).or_default().hidden = true; + sheet.set_value(CellRef::new(2, 0), gridcraft_engine::core::Value::Number(300.0)); + } + grid_frame(&mut app); + let geo = app.grid.geo.clone().unwrap(); + let sh = app.session.active().unwrap().wb.sheet(0).unwrap().clone(); + assert_eq!(geo.y(&sh, 2), geo.y(&sh, 0), "rows 1 and 2 are hidden"); + assert!(!Arc::ptr_eq(&kept(&app).0, &ri)); + check_cf(&mut app); +} From 517a4b92567bda33fa66cb0f7bd74e6f17328399 Mon Sep 17 00:00:00 2001 From: Mouhand Kaddo Date: Mon, 9 Nov 2026 18:49:26 +0400 Subject: [PATCH 27/76] Sort: order Arabic text by its letters, not its vowel marks or digit script (#1940) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * M12.AR7: sort Arabic text by its letters, not its vowel marks or digit script * M12.AR7: order marked Arabic text the way Excel does Checked compare_text against Excel 16.0 build 20527 (en-US) via COM: Range.Sort, =SORT() and the < / = operators over ~300 strings. Fixes the mismatches inside this change's scope: - The tie-break compared raw code points, so an RLM-prefixed word sorted after every vowelled form of it, "‏12" after "١٢", and a tatweel form after a shadda form. Ignorable characters are now skipped at the tie-break too. - Marks are compared letter by letter (an unmarked letter first), so a mark on an earlier letter outranks the digit or letter after it, as in Excel ("ب١" < "بَ1", "بپ" < "بَپ"). - Marks rank in Excel's measured order (sukun before shadda), and the Quranic marks Excel skips (U+0610–061A, U+06D8–06ED except 06DD/06DE/ 06DF/06E0/06E5/06E6/06E9) are skipped by letter order. - Excel also ignores bidi embeddings/overrides/isolates, BOM, soft hyphen, CGJ and word joiner; they now are ignored too (ZWSP is not, as in Excel). Equality is unchanged. Regression tests use the orders Excel produced. Co-Authored-By: Claude Opus 5.5 * M12.AR7: compare the mark level of Arabic text without allocating --------- Co-authored-by: Claude Opus 5.5 Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com> --- crates/core/src/value.rs | 268 ++++++++++++++++++++++++++- crates/engine/src/cmd/data/filter.rs | 6 +- crates/engine/src/cmd/data/tests.rs | 11 ++ crates/engine/src/pivot/items.rs | 2 +- crates/engine/src/tests.rs | 22 +++ docs/localization-parity.md | 3 +- 6 files changed, 299 insertions(+), 13 deletions(-) diff --git a/crates/core/src/value.rs b/crates/core/src/value.rs index 0000028e9..a0b4fcad6 100644 --- a/crates/core/src/value.rs +++ b/crates/core/src/value.rs @@ -346,19 +346,91 @@ pub fn compare_numbers(a: f64, b: f64) -> Ordering { round15(a).partial_cmp(&round15(b)).unwrap_or(Ordering::Equal) } -/// Case-insensitive text order (Unicode lowercase). +/// Case-insensitive text order in three levels, as Excel's collation sorts: first by letters and +/// digits alone (Arabic marks, tatweel and invisible format characters skipped, Arabic-Indic digits +/// read as digits); then, letter by letter, by the marks on each letter (none first, then in +/// Excel's mark order) and the digit script (ASCII, Arabic-Indic, Extended Arabic-Indic); then, to +/// break a remaining tie, by every character (Unicode lowercase). Only text that is equal at the +/// last level compares equal. pub fn compare_text(a: &str, b: &str) -> Ordering { - let mut ai = a.chars().flat_map(char::to_lowercase); - let mut bi = b.chars().flat_map(char::to_lowercase); - loop { - match (ai.next(), bi.next()) { - (None, None) => return Ordering::Equal, - (None, Some(_)) => return Ordering::Less, - (Some(_), None) => return Ordering::Greater, - (Some(x), Some(y)) if x != y => return x.cmp(&y), - _ => {} + if !(a.is_ascii() && b.is_ascii()) { + let primary = primary_chars(a).cmp(primary_chars(b)); + if primary != Ordering::Equal { + return primary; + } + let secondary = secondary_weights(a).cmp(secondary_weights(b)); + if secondary != Ordering::Equal { + return secondary; } } + lowercase_chars(a).cmp(lowercase_chars(b)) +} + +fn lowercase_chars(s: &str) -> impl Iterator + '_ { + s.chars().flat_map(char::to_lowercase) +} + +/// Characters Excel's text order skips entirely: tatweel and invisible format characters (soft +/// hyphen, combining grapheme joiner, zero-width (non-)joiner, bidi marks, embeddings, overrides +/// and isolates, word joiner, byte order mark). The zero-width space is not one. +fn ignorable(c: char) -> bool { + matches!( + c, + '\u{00AD}' + | '\u{034F}' + | '\u{061C}' + | '\u{0640}' + | '\u{200C}'..='\u{200F}' + | '\u{202A}'..='\u{202E}' + | '\u{2060}' + | '\u{2066}'..='\u{2069}' + | '\u{FEFF}' + ) +} + +/// Arabic combining marks in the order Excel sorts a letter carrying them; an unmarked letter +/// sorts before all of them. Measured in Excel 16.0 (build 20527, en-US) with one mark on a letter. +/// U+06D6 and U+06D7 sort as letters in Excel, so they are not listed. +const ARABIC_MARK_ORDER: [char; 48] = [ + '\u{06EC}', '\u{0611}', '\u{0612}', '\u{0613}', '\u{0614}', '\u{06DB}', '\u{0656}', '\u{0657}', '\u{0610}', '\u{06EB}', '\u{0615}', '\u{06D9}', + '\u{06E4}', '\u{06E1}', '\u{06DA}', '\u{06DC}', '\u{06EA}', '\u{06E3}', '\u{06D8}', '\u{0655}', '\u{065F}', '\u{0654}', '\u{06E7}', '\u{0653}', + '\u{06E8}', '\u{06ED}', '\u{06E2}', '\u{0658}', '\u{064B}', '\u{064C}', '\u{064D}', '\u{064E}', '\u{064F}', '\u{0650}', '\u{0652}', '\u{0651}', + '\u{0670}', '\u{0659}', '\u{065A}', '\u{065B}', '\u{065C}', '\u{065D}', '\u{065E}', '\u{0616}', '\u{0617}', '\u{0618}', '\u{0619}', '\u{061A}', +]; + +/// Rank of an Arabic mark that letter order skips (its index in `ARABIC_MARK_ORDER`). +fn arabic_mark_rank(c: char) -> Option { + ARABIC_MARK_ORDER.iter().position(|&m| m == c).and_then(|i| u16::try_from(i).ok()) +} + +/// Digit script of `c` (0 ASCII, 1 Arabic-Indic, 2 Extended Arabic-Indic) and its ASCII digit. +fn digit(c: char) -> Option<(u16, char)> { + let (script, zero) = match c { + '0'..='9' => return Some((0, c)), + '\u{0660}'..='\u{0669}' => (1, 0x0660), + '\u{06F0}'..='\u{06F9}' => (2, 0x06F0), + _ => return None, + }; + char::from_u32(u32::from(c) - zero + u32::from('0')).map(|d| (script, d)) +} + +/// The characters that decide text order before any tie-break. +fn primary_chars(s: &str) -> impl Iterator + '_ { + lowercase_chars(s).filter_map(|c| if ignorable(c) || arabic_mark_rank(c).is_some() { None } else { Some(digit(c).map_or(c, |(_, d)| d)) }) +} + +/// The second level, for text whose primary characters are equal: per letter, its digit script and +/// then its marks, the letters' lists closed by 0 so that an unmarked letter sorts first. Marks +/// before the first letter form a list of their own. +fn secondary_weights(s: &str) -> impl Iterator + '_ { + lowercase_chars(s) + .filter(|&c| !ignorable(c)) + .flat_map(|c| match arabic_mark_rank(c) { + Some(rank) => [Some(rank.saturating_add(1)), None], + None => [Some(0), Some(digit(c).map_or(0, |(script, _)| script))], + }) + .flatten() + .chain(std::iter::once(0)) } /// Sorting order used by Sort (ascending): numbers, text, booleans, errors, then blanks last. @@ -457,6 +529,182 @@ mod tests { assert_eq!(sort_compare(&Value::Empty, &Value::Number(1.0)), Ordering::Greater); } + #[test] + fn arabic_diacritics_sort_with_their_base_letter() { + assert_eq!(compare_text("بَاب", "بيت"), Ordering::Less); + assert_eq!(compare_text("بيت", "بَاب"), Ordering::Greater); + assert_eq!(compare_text("كِتاب", "كتب"), Ordering::Less); + assert_eq!(compare_text("رحمٰن", "رحمي"), Ordering::Less); + assert_eq!(compare(&Value::from("بَاب"), &Value::from("بيت")), Ordering::Less); + } + + #[test] + fn arabic_indic_digits_sort_with_digits() { + assert_eq!(compare_text("١٢", "ب"), Ordering::Less); + assert_eq!(compare_text("۱۲", "ب"), Ordering::Less); + assert_eq!(compare_text("١", "a"), Ordering::Less); + assert_eq!(compare_text("١٢", "13"), Ordering::Less); + assert_eq!(compare_text("۱۴", "13"), Ordering::Greater); + } + + #[test] + fn tatweel_and_bidi_marks_do_not_move_sort_order() { + assert_eq!(compare_text("بـاب", "بت"), Ordering::Less); + assert_eq!(compare_text("\u{200F}apple", "banana"), Ordering::Less); + assert_eq!(compare_text("\u{200E}zebra", "apple"), Ordering::Greater); + assert_eq!(compare_text("\u{061C}a", "b"), Ordering::Less); + assert_eq!(compare_text("a\u{200C}b", "ac"), Ordering::Less); + assert_eq!(compare_text("a\u{200D}b", "ac"), Ordering::Less); + } + + #[test] + fn primary_ties_keep_equality_unchanged() { + assert_eq!(compare_text("ABC", "abc"), Ordering::Equal); + assert_eq!(compare_text("باب", "بَاب"), Ordering::Less); + assert_eq!(compare_text("12", "١٢"), Ordering::Less); + assert_eq!(compare_text("a", "\u{200F}a"), Ordering::Less); + let samples = [ + "", + "abc", + "ABC", + "abd", + "ab", + "باب", + "بَاب", + "بِاب", + "بـاب", + "بيت", + "١٢", + "۱۲", + "12", + "\u{200F}12", + "\u{200F}a", + "a", + "A\u{200C}", + "a\u{200F}", + "b", + "Straße", + "STRASSE", + "ΣΑΣ", + "σας", + "İ", + "i̇", + ]; + let lowered = |s: &str| s.chars().flat_map(char::to_lowercase).collect::(); + for a in samples { + for b in samples { + let order = compare_text(a, b); + assert_eq!(order.is_eq(), lowered(a) == lowered(b), "{a:?} vs {b:?}"); + assert_eq!(order, compare_text(b, a).reverse(), "{a:?} vs {b:?}"); + for c in samples { + if order.is_le() && compare_text(b, c).is_le() { + assert!(compare_text(a, c).is_le(), "{a:?} <= {b:?} <= {c:?}"); + } + } + } + } + } + + /// Sorts `expected` from reversed and rotated starts and checks each comes back as `expected`. + #[track_caller] + fn assert_sorts_to(expected: &[&str]) { + let mut reversed: Vec<&str> = expected.iter().rev().copied().collect(); + reversed.sort_by(|a, b| compare_text(a, b)); + assert_eq!(reversed, expected); + let mut rotated: Vec<&str> = expected.to_vec(); + rotated.rotate_left(expected.len() / 2); + rotated.sort_by(|a, b| compare_text(a, b)); + assert_eq!(rotated, expected); + } + + // The expected orders below are Excel's, observed with Range.Sort, =SORT() and the `<` + // operator in Excel 16.0 (build 20527, en-US). Strings Excel ranks equal are left out. + + #[test] + fn excel_orders_a_letter_by_its_mark() { + assert_sorts_to(&[ + "باب", + "ب\u{0611}اب", + "ب\u{0610}اب", + "ب\u{0654}اب", + "ب\u{064B}اب", + "ب\u{064C}اب", + "ب\u{064D}اب", + "ب\u{064E}اب", + "ب\u{064F}اب", + "ب\u{0650}اب", + "ب\u{0652}اب", + "ب\u{0651}اب", + "ب\u{0670}اب", + "ب\u{065E}اب", + "ب\u{061A}اب", + ]); + // Quranic marks are skipped by letter order like harakat (hamza sorts before alef). + assert_eq!(compare_text("ب\u{0610}ء", "باب"), Ordering::Less); + assert_eq!(compare_text("ب\u{06E1}ء", "باب"), Ordering::Less); + } + + #[test] + fn excel_compares_marks_letter_by_letter() { + assert_sorts_to(&[ + "بت", + "بت\u{064E}", + "بت\u{064F}", + "بت\u{0650}", + "ب\u{064E}ت", + "ب\u{064E}ت\u{064E}", + "ب\u{064E}ت\u{064F}", + "ب\u{064F}ت", + "ب\u{064F}ت\u{064E}", + "ب\u{0650}ت\u{0650}", + ]); + assert_sorts_to(&["مـحـمـد", "محم\u{0651}د", "م\u{064E}حمد", "م\u{064F}ح\u{064E}م\u{064E}\u{0651}د", "محمدي"]); + } + + #[test] + fn excel_weighs_marks_and_digit_scripts_by_position() { + // A mark on an earlier letter outranks the digit script or letter after it, whatever the + // code point of that next character. + assert_sorts_to(&[ + "ب", + "ب\u{064E}", + "ب1", + "ب١", + "ب۱", + "ب\u{064E}1", + "ب\u{064E}١", + "ب\u{064E}۱", + "بa", + "ب\u{064E}a", + "با", + "ب\u{064E}ا", + "ب\u{067E}", + "ب\u{064E}\u{067E}", + "ب\u{06CC}", + "ب\u{064E}\u{06CC}", + ]); + // And an earlier digit script outranks a later mark. + assert_sorts_to(&["12", "1٢", "١2", "١٢", "1ب", "1ب\u{064E}", "1ب\u{0650}", "١ب", "١ب\u{064E}", "١ب\u{0650}", "۱ب", "۱ب\u{064E}"]); + assert_sorts_to(&["ب1ب", "ب1ب\u{064E}", "ب١ب", "ب\u{064E}ب", "بب1", "بب١", "ب\u{064E}ب1"]); + } + + #[test] + fn excel_skips_invisible_format_characters() { + for c in ['\u{00AD}', '\u{034F}', '\u{061C}', '\u{200C}', '\u{200D}', '\u{200E}', '\u{200F}', '\u{202A}', '\u{202B}'] + .into_iter() + .chain(['\u{202C}', '\u{202D}', '\u{202E}', '\u{2060}', '\u{2066}', '\u{2067}', '\u{2068}', '\u{2069}', '\u{FEFF}']) + { + let s = format!("{c}b"); + assert_eq!(compare_text(&s, "a"), Ordering::Greater, "{s:?}"); + assert_eq!(compare_text(&s, "ba"), Ordering::Less, "{s:?}"); + assert_eq!(compare_text(&format!("ب{c}\u{064E}اب"), "باب\u{064B}"), Ordering::Greater, "{c:?}"); + } + // Not even as a tie-break: a leading mark doesn't push text past marked or other-script forms. + assert_sorts_to(&["\u{200F}باب", "ب\u{064B}اب", "ب\u{064E}اب", "ب\u{0670}اب"]); + assert_sorts_to(&["\u{200F}12", "١٢", "۱۲"]); + assert_sorts_to(&["ب\u{200F}ب\u{064E}", "\u{200F}ب\u{064E}ب"]); + } + #[test] fn errors() { for e in CellError::ALL { diff --git a/crates/engine/src/cmd/data/filter.rs b/crates/engine/src/cmd/data/filter.rs index 437bd7887..cda2160bf 100644 --- a/crates/engine/src/cmd/data/filter.rs +++ b/crates/engine/src/cmd/data/filter.rs @@ -317,5 +317,9 @@ pub fn filter_values(wb: &Workbook, sheet: usize, col: u32) -> Vec<(String, bool let key = if blank { sort_key(&Value::Empty) } else { sort_key(&v) }; seen.entry((key, t)).or_insert(on); } - seen.into_iter().map(|((_, t), on)| (t, on)).collect() + let mut values: Vec<_> = seen.into_iter().collect(); + values.sort_by(|((a, ta), _), ((b, tb), _)| { + a.0.cmp(&b.0).then(a.1.cmp(&b.1)).then_with(|| gridcraft_core::compare_text(&a.2, &b.2)).then_with(|| ta.cmp(tb)) + }); + values.into_iter().map(|((_, t), on)| (t, on)).collect() } diff --git a/crates/engine/src/cmd/data/tests.rs b/crates/engine/src/cmd/data/tests.rs index 3c2782444..e073e23a5 100644 --- a/crates/engine/src/cmd/data/tests.rs +++ b/crates/engine/src/cmd/data/tests.rs @@ -53,6 +53,17 @@ fn subtotal_puts_summary_rows_below_their_detail() { assert_eq!(sh.value(CellRef::new(3, 0)).display(), "West"); } +#[test] +fn filter_list_orders_arabic_like_sort() { + let mut s = Session::new(); + s.new_workbook(); + s.execute("range.setValues", json!({"range": "A1", "values": [["H"], ["بج"], ["بُت"], ["\u{200F}بب"]]})).unwrap(); + s.execute("data.filter", json!({"range": "A1:A4"})).unwrap(); + let doc = s.doc().unwrap(); + let list: Vec = filter_values(&doc.wb, 0, 0).into_iter().map(|(t, _)| t).collect(); + assert_eq!(list, ["\u{200F}بب", "بُت", "بج"]); +} + #[test] fn filter_by_huge_numeric_column_is_rejected_not_overflowed() { let mut s = Session::new(); diff --git a/crates/engine/src/pivot/items.rs b/crates/engine/src/pivot/items.rs index 79b74da1a..6dee1dc5a 100644 --- a/crates/engine/src/pivot/items.rs +++ b/crates/engine/src/pivot/items.rs @@ -21,7 +21,7 @@ pub(super) struct FieldItems { } fn cmp_items(a: &Item, b: &Item) -> Ordering { - a.class.cmp(&b.class).then(a.num.total_cmp(&b.num)).then_with(|| a.text.cmp(&b.text)) + a.class.cmp(&b.class).then(a.num.total_cmp(&b.num)).then_with(|| gridcraft_core::compare_text(&a.text, &b.text)) } /// (identity key, item) for a source value under a field's date grouping. diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index f97e1bc10..bf36bd15b 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -1459,6 +1459,28 @@ fn case_sensitive_sort_puts_lowercase_first() { assert_eq!(col, ["a", "A", "aB", "Ab", "b", "B"].map(Value::from)); } +#[test] +fn data_sort_orders_vowelled_arabic_by_letters() { + let mut s = s(); + s.execute("range.setValues", json!({"range": "A1", "values": [["بيت"], ["بَاب"], ["تمر"], ["\u{200F}بحر"]]})).unwrap(); + s.execute("data.sort", json!({"range": "A1:A4", "header": false, "keys": [{"column": "A"}]})).unwrap(); + let col: Vec = (1..=4).map(|r| v(&s, &format!("A{r}"))).collect(); + assert_eq!(col, ["بَاب", "\u{200F}بحر", "بيت", "تمر"].map(Value::from)); +} + +#[test] +fn data_sort_orders_marks_and_digit_scripts_like_excel() { + // Excel 16.0 (build 20527, en-US) sorts this column, as text, into exactly this order. + let want = ["١٢", "۱۲", "ب\u{064E}1", "ب\u{064E}١", "بa", "\u{200F}باب", "ب\u{064B}اب", "ب\u{064E}اب", "ب\u{0652}اب", "ب\u{0651}اب"]; + let mut s = s(); + s.execute("home.numberFormat", json!({"range": "A1:A10", "code": "@"})).unwrap(); + let input: Vec> = want.iter().rev().map(|t| vec![*t]).collect(); + s.execute("range.setValues", json!({"range": "A1", "values": input})).unwrap(); + s.execute("data.sort", json!({"range": "A1:A10", "header": false, "keys": [{"column": "A"}]})).unwrap(); + let col: Vec = (1..=10).map(|r| v(&s, &format!("A{r}"))).collect(); + assert_eq!(col, want.map(Value::from)); +} + #[test] fn sort_with_a_sheet_prefix_sorts_the_named_sheet() { let col = |s: &Session, sheet: &str| -> Vec { diff --git a/docs/localization-parity.md b/docs/localization-parity.md index 39a9b3f91..7b5dda2c0 100644 --- a/docs/localization-parity.md +++ b/docs/localization-parity.md @@ -1,6 +1,6 @@ # Localization parity -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-23 · **Change:** minor (missing translations are listed by `cargo xtask i18n-report` and never fail a check) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-23 · **Change:** minor (missing translations are listed by `cargo xtask i18n-report` and never fail a check; Arabic text sorts by its letters, not its vowel marks) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Per-language status against Excel, which ships its UI in dozens of languages (Windows language packs cover 100+), with localized function names, argument separators, number and date formats, @@ -117,6 +117,7 @@ language (none so far) and of localized function names. | 2026-10-20 | minor | Dutch (nl) interface catalog (`i18n/nl.rs`, 518 labels, ~24%): the shared key set every language must cover (ribbon, context menus, File view, dialog titles, Options page), matching the German, French and Czech catalogs; `nl-NL`, `nl-BE` locale tags resolve to Dutch | | 2026-10-20 | minor | XLSX: a theme's per-script typefaces (``, Hebr, Jpan…) are kept through open + save, so Arabic Excel's theme fonts for shapes, charts and theme-font cells survive a GridCraft save | | 2026-10-20 | trivial | XLSX: a workbook theme's major/minor East Asian (``) and complex-script (``) typefaces are kept through open + save, so a Japanese workbook keeps its theme's East Asian font; rendering does not resolve them yet (follow-up) | +| 2026-10-19 | minor | Text order (Sort, SORT/SORTBY, `<`/`>`, approximate MATCH/XLOOKUP, `" Date: Mon, 9 Nov 2026 18:52:55 +0400 Subject: [PATCH 28/76] AutoFit: estimate Arabic text width from measured glyph widths (#1933) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * M12.AR12: estimate Arabic text width from measured glyph widths in AutoFit * M12.AR12: treat U+0897–089F as marks and U+08E2 as visible; size the honorific ligatures (review) * M12.AR12: size the remaining honorific ligatures like their siblings (review) U+FD4C-FD4F (Unicode 14) and U+FDC8-FDCF (FDCF Unicode 14, FDC8-FDCE Unicode 17) are honorific word ligatures, not letters, but fell into the 0.48 em letter arm. Give them the 1.6 em of the measured U+FD40-FD4B honorifics and assert each range's endpoints. Co-Authored-By: Claude Opus 5.5 * M12.AR12: size Arabic-Indic digits like ASCII digits and the remaining honorific ligatures (review) Arabic-Indic (U+0660-0669) and Extended Arabic-Indic (U+06F0-06F9) digits measure 0.53-0.57 em in Noto Sans Arabic, Arial, Tahoma and Times New Roman, the same as each font's ASCII digits, so they get the ASCII digit estimate (0.56) instead of the letter estimate. Arabic punctuation measures 0.17-0.50 em, so it keeps 0.48 rather than falling back to 1 em. U+FBC3-FBD2 and U+FD90-FD91 are honorific word ligatures like U+FD40-FD4F and get the same 1.6 em. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com> Co-authored-by: FredApps --- crates/engine/src/cmd/format.rs | 46 +++++++++++++++++++++++++++ crates/engine/src/cmd/format/sizes.rs | 45 +++++++++++++++++++++++++- docs/localization-parity.md | 3 +- 3 files changed, 92 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/format.rs b/crates/engine/src/cmd/format.rs index 1066f48e2..d5eac5b5d 100644 --- a/crates/engine/src/cmd/format.rs +++ b/crates/engine/src/cmd/format.rs @@ -870,6 +870,52 @@ mod tests { assert_eq!(s.execute("cell.get", json!({"cell": "A1"})).unwrap()["style"]["num_fmt"], "0.00"); } + #[test] + fn arabic_vowel_and_direction_marks_add_no_width() { + let plain = approx_text_width("محمد يدرس في المدرسة", 11.0, false); + assert_eq!(approx_text_width("مُحَمَّد يَدْرُسُ فِي الْمَدْرَسَةِ", 11.0, false), plain); + assert_eq!(approx_text_width("\u{200F}محمد يدرس في المدرسة\u{200C}", 11.0, false), plain); + assert_eq!(approx_text_width("ب\u{0897}\u{0898}\u{089F}", 11.0, false), approx_text_width("ب", 11.0, false)); + assert!(approx_text_width("\u{08E2}", 11.0, false) > 0.0, "the disputed end of ayah is a visible sign"); + } + + #[test] + fn arabic_text_fits_a_column_about_as_wide_as_latin_text_of_the_same_length() { + let mut s = crate::Session::new(); + s.new_workbook(); + s.execute("range.setValues", json!({"range": "A1", "values": [["مُحَمَّد يَدْرُسُ فِي الْمَدْرَسَةِ", "Hello there in school"]]})).unwrap(); + s.execute("home.autofitColumnWidth", json!({"cols": "A:B"})).unwrap(); + let doc = s.doc().unwrap(); + let width = |c: u32| doc.wb.active().and_then(|sh| sh.cols.get(&c)).and_then(|e| e.size).unwrap(); + let ratio = width(0) / width(1); + assert!((0.75..=1.15).contains(&ratio), "Arabic {} vs Latin {}", width(0), width(1)); + } + + #[test] + fn arabic_word_ligatures_are_wider_than_letters() { + let em = |t: &str| approx_text_width(t, 72.0, false) / 96.0; + assert!(em("\u{FDFD}") > 8.0); + assert!(em("\u{FDFA}") > 1.0); + let honorifics = ['\u{FD40}', '\u{FD4B}', '\u{FD4C}', '\u{FD4F}', '\u{FDC8}', '\u{FDCF}', '\u{FDFE}', '\u{FDFF}']; + for honorific in honorifics.into_iter().chain(['\u{FBC3}', '\u{FBD2}', '\u{FD90}', '\u{FD91}']) { + assert!(em(&honorific.to_string()) > 1.0, "U+{:04X}", u32::from(honorific)); + } + assert!(em("ب") < 0.6); + } + + #[test] + fn arabic_digits_are_as_wide_as_ascii_digits_and_punctuation_as_a_letter() { + let width = |t: &str| approx_text_width(t, 11.0, false); + assert_eq!(width("١٢٣"), width("123")); + assert_eq!(width("۴۵۶"), width("456")); + assert_eq!(width("،؟"), width("بب")); + } + + #[test] + fn cjk_text_keeps_a_full_em_per_character() { + assert_eq!(approx_text_width("中文", 12.0, false), 2.0 * 12.0 * 96.0 / 72.0); + } + #[test] fn width_units() { assert_eq!(chars_to_points(8.43), 64.0); diff --git a/crates/engine/src/cmd/format/sizes.rs b/crates/engine/src/cmd/format/sizes.rs index c79327678..8b304c889 100644 --- a/crates/engine/src/cmd/format/sizes.rs +++ b/crates/engine/src/cmd/format/sizes.rs @@ -136,12 +136,55 @@ pub fn approx_text_width(text: &str, size: f32, bold: bool) -> f32 { c if c.is_ascii_uppercase() => 0.65, c if c.is_ascii_digit() => 0.56, c if c.is_ascii() => 0.52, - _ => 1.0, + c if is_zero_width(c) => 0.0, + c => arabic_em(c).unwrap_or(1.0), }; } w * size * if bold { 1.07 } else { 1.0 } * 96.0 / 72.0 } +/// Arabic vowel and Quranic marks, which sit on the previous letter, and invisible format +/// characters such as direction marks and zero-width joiners. +fn is_zero_width(c: char) -> bool { + matches!( + c, + '\u{0610}'..='\u{061A}' + | '\u{061C}' + | '\u{064B}'..='\u{065F}' + | '\u{0670}' + | '\u{06D6}'..='\u{06DC}' + | '\u{06DF}'..='\u{06E4}' + | '\u{06E7}'..='\u{06E8}' + | '\u{06EA}'..='\u{06ED}' + | '\u{0897}'..='\u{089F}' + | '\u{08CA}'..='\u{08E1}' + | '\u{08E3}'..='\u{08FF}' + | '\u{200B}'..='\u{200F}' + | '\u{202A}'..='\u{202E}' + | '\u{2060}'..='\u{2069}' + | '\u{FE20}'..='\u{FE2F}' + | '\u{FEFF}' + ) +} + +/// Width in ems of an Arabic-script character, measured from shaped Noto Sans Arabic, Noto Naskh +/// Arabic and DejaVu Sans: joined letters average about half an em, word ligatures such as ﷺ and the +/// rial sign about 1.2 em, the honorific ligatures (U+FD40–FD4B) about 1.6 em, and the bismillah +/// ligature about 11 em. The later honorific ligatures (U+FBC3–FBD2, U+FD4C–FD4F, U+FD90–FD91, +/// U+FDC8–FDCF) are not measured and get the 1.6 em of their siblings rather than the letter width. +/// Arabic-Indic digits are as wide as ASCII digits (0.53–0.57 em in Noto Sans Arabic, Arial, Tahoma +/// and Times New Roman); Arabic punctuation (0.2–0.5 em in the same fonts) keeps the letter width. +fn arabic_em(c: char) -> Option { + match c { + '\u{FDFD}' => Some(11.0), + '\u{FBC3}'..='\u{FBD2}' | '\u{FD40}'..='\u{FD4F}' | '\u{FD90}'..='\u{FD91}' | '\u{FDC8}'..='\u{FDCF}' => Some(1.6), + '\u{FDF0}'..='\u{FDFC}' | '\u{FDFE}'..='\u{FDFF}' => Some(1.2), + '\u{0660}'..='\u{0669}' | '\u{06F0}'..='\u{06F9}' => Some(0.56), + '\u{0600}'..='\u{06FF}' | '\u{0750}'..='\u{077F}' | '\u{0870}'..='\u{08FF}' | '\u{FB50}'..='\u{FDFF}' | '\u{FE70}'..='\u{FEFC}' => Some(0.48), + _ => None, + } +} + /// Distinct texts AutoFit measures exactly; past this many, `Session::text_width` is asked for a /// quick measure so a huge column doesn't lay out every value. const EXACT_TEXT_WIDTHS: usize = 2_000; diff --git a/docs/localization-parity.md b/docs/localization-parity.md index 7b5dda2c0..3f1dacb96 100644 --- a/docs/localization-parity.md +++ b/docs/localization-parity.md @@ -1,6 +1,6 @@ # Localization parity -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-23 · **Change:** minor (missing translations are listed by `cargo xtask i18n-report` and never fail a check; Arabic text sorts by its letters, not its vowel marks) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-27 · **Change:** minor (AutoFit estimates Arabic text width from measured glyph widths); previously minor (missing translations are listed by `cargo xtask i18n-report` and never fail a check; Arabic text sorts by its letters, not its vowel marks) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Per-language status against Excel, which ships its UI in dozens of languages (Windows language packs cover 100+), with localized function names, argument separators, number and date formats, @@ -110,6 +110,7 @@ language (none so far) and of localized function names. | Date | Change | Summary | |---|---|---| +| 2026-10-27 | minor | AutoFit column width, wrapped-row height and PivotTable column fit estimate Arabic letters at ~0.48 em (measured 0.36–0.53 em shaped, Noto Sans/Naskh Arabic and DejaVu Sans) instead of 1 em, Arabic-Indic digits as wide as ASCII digits, and give vowel marks and direction marks no width; vowelled Arabic no longer fits ~3.5× too wide | | 2026-10-23 | minor | Missing translations are reported, never a failure: `cargo xtask i18n-report` prints, per language, the strings other catalogs translate and it lacks (plus Spanish function help rows keyed by English the function catalog no longer has) and always passes; `cargo xtask ci` runs it first. Coverage and per-language parity tests and tests of one language's exact wording are gone; UI tests take the expected text from the catalog. Duplicate-key, placeholder, context-prefix and line-shape checks stay | | 2026-10-21 | minor | Romanian interface (`ro`, Română): 970 labels covering every key of the other catalogs (`crates/ui-egui/src/i18n/ro.rs`), `ro-*` system locales, the View-tab switcher and `app.language.set`; context keys show no prefix (File › Options test). English formulas and formats; AI-assisted draft pending native-speaker review | | 2026-10-20 | minor | Japanese: furigana (`rPh` phonetic runs and `` in `xl/sharedStrings.xml`) is no longer deleted by an open + save; it is kept on the workbook and written back (a `` without runs is kept too, while duplicate texts with different readings keep only the first, reported on open). Display of the readings is still missing (`crates/xlsx/src/tests/phonetic_tests.rs`) | From c35234e5f555201cf39669c7164a1fc9f5267cf8 Mon Sep 17 00:00:00 2001 From: Mouhand Kaddo Date: Mon, 9 Nov 2026 18:54:05 +0400 Subject: [PATCH 29/76] Number formats: honour the B1/B2 calendar prefixes (Hijri dates for B2) (#1918) * M12.AR6: honour the B1/B2 calendar prefixes in number formats (Hijri for B2) * M12.AR6: match Excel's B1/B2 behaviour measured in review Changes from a reviewer's Excel run (TEXT and Range.NumberFormat): - A [$-...] locale tag before B2 keeps the date Gregorian (prefix still hidden); B2[$-401] stays Hijri. - Any other unquoted B and digit (B3dd, dd/mm/yyyyB2, dd B2 mm), and B2 before a number format (B20.00), make the code invalid: TEXT gives #VALUE!, a cell shows General, as for a code over 255 characters. - Under B2, ddd and dddd show the Arabic weekday name (full name for both). - Serial 60 has no Hijri date: TEXT gives #VALUE!, a cell shows ####. - Serial 0 shows as serial 1 (29/08/1317). Also fixes a break from merging main: days_from_civil now returns 0 for day 0 and 29 February 1900, which sent serials 0 and 60 to 1970 (23/10/1389) under B2. The Hijri day count no longer passes those dates. Docs: localization-parity states the behaviour, including the one measured case not matched (mmmm next to dddd prints the weekday in Excel). Co-Authored-By: Claude Opus 5.5 * M12.AR6: Hijri leap year in cycle year 15, not 16, as Excel shows A reviewer compared Excel's B2yyyy-mm-dd on 43,512 serials with this formula: it disagreed by one day on 1,372, all in cycle years 15 and 16, and moving the leap year from 16 to 15 left no disagreement. The leap years are now 2, 5, 7, 10, 13, 15, 18, 21, 24, 26 and 29 (still 10,631 days a cycle). The count of leap years before a year becomes (4 + 11y) div 30; it differs from the old (3 + 11y) div 30 only when y = 16 (mod 30), which is exactly the move from 16 to 15. The inverse's year estimate can now be one year off at a boundary and is corrected by one step. Tests: Excel values for 2 March 2005 (1426-01-21) and 29 January 2006 (1426-12-29) plus the 1425/1426 year boundaries they imply, the reviewer's other anchors, and exhaustive checks: year, month and 30-year cycle lengths for Hijri years 1 to 10,000; day -> Hijri -> day round trip and day-by-day succession for every day from 1 Muharram 1 AH to one cycle past 31 December 9999; every 1900-system serial 1 to 2,958,465 (except 60). Co-Authored-By: Claude Opus 5.5 * M12.AR6: match B1/B2 details checked in Excel 16.0 build 20527 Compared GridCraft with Excel 16.0 (build 20527, en-US) through COM: TEXT with B2dd/mm/yyyy on every serial from 1 to 2,958,465 (no difference), and about 1,750 format/value pairs through both a cell's NumberFormat and TEXT. Fixed what differed and is in scope: - Two-word Hijri month names use a no-break space (U+00A0), as Excel does. - B1 before a number format is rejected like B2 (B10.00, B1#,##0). - Prefix runs: a repeated prefix is accepted (B2B2, B2b2, [Red]B2B2, B1B1); a mixed one (B1B2, B2B1), more digits (B12, B21, B02), a prefix after a tag or space (B2[Red]B2, B2 B2) and an escaped \B2 are rejected. - Serial 60 under B2 is #VALUE! for any date or clock part (hh:mm, ss, AM/PM), not only date parts; [h] and literal-only sections still show. Regression tests use the Excel values (all 12 month and 7 weekday names, the accepted and rejected codes, serial 60). Not changed, listed in the docs: Excel's weekday/month-name mix-ups when ddd(d) and mmm(m) share a B2 section, weekday-only B2 sections (real weekday for serials 0-59, #VALUE! for the last days of 9999), and Buddhist-year b/bb/bbbb tokens. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com> Co-authored-by: FredApps --- crates/numfmt/src/calendar.rs | 206 ++++++++++++++++++++++++++++++ crates/numfmt/src/lib.rs | 22 +++- crates/numfmt/src/parse.rs | 69 +++++++++- crates/numfmt/src/render.rs | 34 +++-- crates/numfmt/src/tests.rs | 234 ++++++++++++++++++++++++++++++++++ docs/localization-parity.md | 19 ++- 6 files changed, 563 insertions(+), 21 deletions(-) create mode 100644 crates/numfmt/src/calendar.rs diff --git a/crates/numfmt/src/calendar.rs b/crates/numfmt/src/calendar.rs new file mode 100644 index 000000000..ab5bd4bbd --- /dev/null +++ b/crates/numfmt/src/calendar.rs @@ -0,0 +1,206 @@ +//! Calendars a format section can show dates in: the `B1` (Gregorian) and `B2` (Hijri) prefixes. + +use gridcraft_core::date::{DateTime, MONTHS, WEEKDAYS, days_from_civil, days_in_month}; + +/// The calendar a section's year, month and day parts count in. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)] +pub(crate) enum Calendar { + #[default] + Gregorian, + /// The tabular (arithmetic) Islamic calendar. + Hijri, +} + +/// Arabic names of the Hijri months, Muharram first. Two-word names are joined by a no-break +/// space (U+00A0), as Excel writes them. +const HIJRI_MONTHS: [&str; 12] = [ + "محرم", + "صفر", + "ربيع\u{a0}الأول", + "ربيع\u{a0}الثاني", + "جمادى\u{a0}الأولى", + "جمادى\u{a0}الثانية", + "رجب", + "شعبان", + "رمضان", + "شوال", + "ذو\u{a0}القعدة", + "ذو\u{a0}الحجة", +]; + +/// Arabic weekday names, Sunday first, shown for `ddd` and `dddd` under `B2`. +const ARABIC_WEEKDAYS: [&str; 7] = ["الأحد", "الإثنين", "الثلاثاء", "الأربعاء", "الخميس", "الجمعة", "السبت"]; + +/// Year, month and day of a date in a section's calendar, with the month and weekday names. +pub(crate) struct CalendarDate { + pub year: i64, + pub month: u32, + pub day: u32, + /// `mmmm`. + pub month_name: &'static str, + /// `mmm`. + pub month_short: &'static str, + /// `dddd`. + pub day_name: &'static str, + /// `ddd`. + pub day_short: &'static str, +} + +impl Calendar { + /// The date of a day in this calendar, `None` when it has none: under `B2`, the 1900 system's + /// fictitious 29 February 1900 (serial 60, which Excel shows as `#VALUE!`) and days before + /// 1 Muharram 1 AH. + pub(crate) fn date(self, dt: &DateTime) -> Option { + match self { + Calendar::Hijri => hijri_date(dt), + Calendar::Gregorian => Some(gregorian_date(dt)), + } + } +} + +fn gregorian_date(dt: &DateTime) -> CalendarDate { + let month_name = MONTHS.get((dt.month as usize).wrapping_sub(1)).copied().unwrap_or(""); + let day_name = WEEKDAYS.get(dt.weekday as usize).copied().unwrap_or(""); + CalendarDate { + year: dt.year.into(), + month: dt.month, + day: dt.day, + month_name, + month_short: month_name.get(..3).unwrap_or(month_name), + day_name, + day_short: day_name.get(..3).unwrap_or(day_name), + } +} + +/// Days from 1 Muharram 1 AH to the first of a Hijri month. Leap years (30 days in the last +/// month) are years 2, 5, 7, 10, 13, 15, 18, 21, 24, 26 and 29 of each 30-year cycle (10,631 +/// days), which is what Excel shows; odd months have 30 days and even months 29. +fn days_before_month(year: i64, month: i64) -> i64 { + // `(4 + 11 * year) / 30` is the number of leap years before `year`. + (year - 1) * 354 + (4 + 11 * year).div_euclid(30) + 29 * (month - 1) + month / 2 +} + +/// Hijri year, month and day of the day `days` after 1 Muharram 1 AH (`days >= 0`). +fn hijri_from_days(days: i64) -> Option<(i64, i64, i64)> { + if days < 0 { + return None; + } + // The estimate is the mean year length rounded; it can be one year off at a year boundary. + // Estimate and `days_before_month` both move by 30 years per 10,631 days, so one step is + // enough everywhere if it is within one cycle (`every_day_round_trips` checks many). + let mut year = (30 * days + 10646) / 10631; + if days < days_before_month(year, 1) { + year -= 1; + } else if days >= days_before_month(year + 1, 1) { + year += 1; + } + let month = ((11 * (days - days_before_month(year, 1)) + 330) / 325).clamp(1, 12); + let day = days - days_before_month(year, month) + 1; + Some((year, month, day)) +} + +/// 1 Muharram 1 AH is 18 July 622 (proleptic Gregorian), so that 1 January 2001 is 6 Shawwal 1421. +fn hijri_date(dt: &DateTime) -> Option { + // 29 February 1900 (serial 60) is no real day and has no Hijri date. The 1900 system's day 0 + // (`1900-01-00`, serial 0) shows as 1 January 1900, as Excel shows it. + if dt.day > days_in_month(dt.year, dt.month) { + return None; + } + let (year, month, day) = hijri_from_days(days_from_civil(dt.year, dt.month, dt.day.max(1)) - days_from_civil(622, 7, 18))?; + let month_name = HIJRI_MONTHS.get(usize::try_from(month - 1).ok()?).copied()?; + // Excel shows the full Arabic name for `ddd` as well as `dddd`, as it does for `mmm`. + let day_name = ARABIC_WEEKDAYS.get(dt.weekday as usize).copied()?; + Some(CalendarDate { + year, + month: u32::try_from(month).ok()?, + day: u32::try_from(day).ok()?, + month_name, + month_short: month_name, + day_name, + day_short: day_name, + }) +} + +#[cfg(test)] +mod tests { + use super::*; + use gridcraft_core::DateSystem; + use gridcraft_core::date::{datetime_from_serial, max_serial}; + + /// Leap years of the 30-year cycle (Excel's set, per the reviewer's 43,512-serial comparison). + const LEAP: [i64; 11] = [2, 5, 7, 10, 13, 15, 18, 21, 24, 26, 29]; + + fn is_leap(year: i64) -> bool { + LEAP.contains(&((year - 1).rem_euclid(30) + 1)) + } + + fn month_len(year: i64, month: i64) -> i64 { + if month == 12 { 29 + i64::from(is_leap(year)) } else { 30 - (month + 1) % 2 } + } + + /// Years 1 to 10,000 (every cycle up to and past 31 December 9999, which is 9666 AH): year, + /// month and cycle lengths follow the leap-year set. + #[test] + fn year_and_month_lengths_follow_the_leap_set() { + assert_eq!(days_before_month(1, 1), 0); + for year in 1..=10_000 { + let start = days_before_month(year, 1); + let len = days_before_month(year + 1, 1) - start; + assert_eq!(len, if is_leap(year) { 355 } else { 354 }, "year {year}"); + for month in 1..=12 { + let next = if month == 12 { days_before_month(year + 1, 1) } else { days_before_month(year, month + 1) }; + assert_eq!(next - days_before_month(year, month), month_len(year, month), "{year}-{month}"); + } + assert_eq!(days_before_month(year + 30, 1) - start, 10_631, "cycle from {year}"); + } + // One cycle: exactly the eleven leap years, 10,631 days. + let cycle: Vec = (1..=30).filter(|&y| days_before_month(y + 1, 1) - days_before_month(y, 1) == 355).collect(); + assert_eq!(cycle, LEAP); + } + + /// Every day from 1 Muharram 1 AH to one cycle past 31 December 9999: day -> Hijri -> day + /// round-trips, the day is within its month, and consecutive days follow each other. + #[test] + fn every_day_round_trips() { + let last = days_from_civil(9999, 12, 31) - days_from_civil(622, 7, 18) + 10_631; + let mut prev: Option<(i64, i64, i64)> = None; + for days in 0..=last { + let (y, m, d) = hijri_from_days(days).unwrap(); + assert!((1..=12).contains(&m) && (1..=month_len(y, m)).contains(&d), "{days}: {y}-{m}-{d}"); + assert_eq!(days_before_month(y, m) + d - 1, days, "{y}-{m}-{d}"); + if let Some((py, pm, pd)) = prev { + let expected = if pd < month_len(py, pm) { + (py, pm, pd + 1) + } else if pm < 12 { + (py, pm + 1, 1) + } else { + (py + 1, 1, 1) + }; + assert_eq!((y, m, d), expected, "after {py}-{pm}-{pd}"); + } + prev = Some((y, m, d)); + } + assert_eq!(hijri_from_days(-1), None); + } + + /// Every serial of the 1900 system: the Hijri date names the serial's day, one day apart. + #[test] + fn every_serial_has_the_right_hijri_day() { + let epoch = days_from_civil(622, 7, 18); + let mut prev = None; + for serial in 1..=max_serial(DateSystem::D1900) { + if serial == 60 { + continue; + } + let dt = datetime_from_serial(DateSystem::D1900, serial as f64).unwrap(); + let h = Calendar::Hijri.date(&dt).unwrap(); + assert!((1..=month_len(h.year, i64::from(h.month))).contains(&i64::from(h.day)), "serial {serial}"); + let days = days_before_month(h.year, i64::from(h.month)) + i64::from(h.day) - 1; + assert_eq!(days, days_from_civil(dt.year, dt.month, dt.day) - epoch, "serial {serial}"); + if let Some(p) = prev { + assert_eq!(days, p + 1, "serial {serial}"); + } + prev = Some(days); + } + } +} diff --git a/crates/numfmt/src/lib.rs b/crates/numfmt/src/lib.rs index cbb11bc31..57384cc89 100644 --- a/crates/numfmt/src/lib.rs +++ b/crates/numfmt/src/lib.rs @@ -4,6 +4,7 @@ #![forbid(unsafe_code)] mod builtin; +mod calendar; mod decimal; mod parse; mod render; @@ -96,6 +97,9 @@ struct Inner { num_sections: usize, /// Index of the section that applies to text, if any. text_section: Option, + /// A code Excel rejects (a misplaced `B1`/`B2` calendar prefix): shown as General, and + /// `TEXT` returns `#VALUE!`, as for a code that is too long. + invalid: bool, } /// A parsed number format code. Cheap to clone. @@ -130,7 +134,8 @@ fn too_long(code: &str) -> bool { impl NumberFormat { /// Parses a format code. Never fails: unknown or malformed parts degrade to literal text, and - /// a code longer than [`MAX_FORMAT_CODE_CHARS`] characters degrades to General. + /// a code longer than [`MAX_FORMAT_CODE_CHARS`] characters, or one Excel rejects for a + /// misplaced `B1`/`B2` calendar prefix, degrades to General. pub fn parse(code: &str) -> NumberFormat { Self::parse_inner(code, false) } @@ -147,12 +152,16 @@ impl NumberFormat { let trimmed = code.trim(); // `checkbox` marks a checkbox cell (Insert › Checkbox): TRUE/FALSE draw as a box, any other // value displays as General. + let general = |invalid: bool| NumberFormat { + inner: Arc::new(Inner { code: code.to_string(), general: true, sections: Vec::new(), num_sections: 0, text_section: None, invalid }), + }; if trimmed.is_empty() || trimmed.eq_ignore_ascii_case("general") || trimmed == "checkbox" || (!uncapped && too_long(code)) { - return NumberFormat { - inner: Arc::new(Inner { code: code.to_string(), general: true, sections: Vec::new(), num_sections: 0, text_section: None }), - }; + return general(false); } let sections = parse::parse_sections(code); + if sections.iter().any(|s| s.invalid) { + return general(true); + } let len = sections.len(); let last_is_text = sections.last().is_some_and(|s| s.kind == SecKind::Text); let text_section = if len >= 4 { @@ -169,7 +178,7 @@ impl NumberFormat { } else { len }; - NumberFormat { inner: Arc::new(Inner { code: code.to_string(), general: false, sections, num_sections, text_section }) } + NumberFormat { inner: Arc::new(Inner { code: code.to_string(), general: false, sections, num_sections, text_section, invalid: false }) } } pub fn general() -> NumberFormat { @@ -487,6 +496,9 @@ pub fn text_function(v: &Value, code: &str, sys: DateSystem) -> Result return Err(e), Value::Empty => 0.0, diff --git a/crates/numfmt/src/parse.rs b/crates/numfmt/src/parse.rs index ac905d36b..c2b121940 100644 --- a/crates/numfmt/src/parse.rs +++ b/crates/numfmt/src/parse.rs @@ -1,6 +1,7 @@ //! Format code parsing: sections, bracket tags and tokens. use crate::FormatColor; +use crate::calendar::Calendar; /// At most this many sections are honoured (`pos;neg;zero;text`); extra ones are ignored. const MAX_SECTIONS: usize = 4; @@ -24,6 +25,11 @@ pub(crate) enum DatePart { } impl DatePart { + /// `[h]`, `[m]` or `[s]`. + pub(crate) fn is_elapsed(self) -> bool { + matches!(self, DatePart::ElapsedH(_) | DatePart::ElapsedM(_) | DatePart::ElapsedS(_)) + } + pub(crate) fn is_calendar(self) -> bool { matches!(self, DatePart::Year(_) | DatePart::Month(_) | DatePart::Day(_)) } @@ -122,6 +128,10 @@ pub(crate) struct Section { pub fraction: bool, pub currency: bool, pub fill: bool, + pub calendar: Calendar, + /// Excel rejects the whole code because of this section: a `B` and digit that is not a + /// calendar prefix, or a prefix before a number format (`B20.00`). + pub invalid: bool, } /// Splits a code into sections at unquoted, unescaped, unbracketed semicolons. @@ -251,17 +261,60 @@ struct Raw { color: Option, cond: Option, currency: bool, + calendar: Calendar, + /// The section's calendar prefix as written, `'1'` or `'2'` (whether or not a locale tag + /// cancels it). + prefix: Option, + /// An unquoted `B` and digit that is not a calendar prefix: Excel rejects the code. + invalid: bool, +} + +/// Where a section's body starts: after its leading `[...]` tags, read as `tokenize` reads them. +/// Also whether one of those tags is a `[$...]` locale tag. +fn after_leading_brackets(cs: &[char]) -> (usize, bool) { + let mut i = 0usize; + let mut locale = false; + while cs.get(i) == Some(&'[') { + match cs.get(i + 1..).and_then(|r| r.iter().position(|&d| d == ']')) { + Some(p) => { + locale |= cs.get(i + 1) == Some(&'$'); + i += p + 2; + } + None => break, + } + } + (i, locale) } fn tokenize(src: &str) -> Raw { let cs: Vec = src.chars().collect(); - let mut raw = Raw { toks: Vec::new(), color: None, cond: None, currency: false }; + let mut raw = Raw { toks: Vec::new(), color: None, cond: None, currency: false, calendar: Calendar::Gregorian, prefix: None, invalid: false }; let toks = &mut raw.toks; // With no `]` at or after a `[`, that `[` is a literal; checking this once keeps the scan linear. let last_close = cs.iter().rposition(|&c| c == ']'); + let (body_start, locale_first) = after_leading_brackets(&cs); + // Where the next calendar prefix may stand: the body's start, or right after another prefix. + let mut prefix_at = body_start; let mut i = 0usize; while let Some(&c) = cs.get(i) { match c { + // A calendar prefix, never shown: `B1` or `B2`, any case, which Excel accepts repeated + // (`B2B2`) but not mixed (`B2B1`) or with more digits (`B12`, `B02`). + 'B' | 'b' if i == prefix_at && cs.get(i + 1).is_some_and(char::is_ascii_digit) => { + let d = cs.get(i + 1).copied(); + let ok = matches!(d, Some('1' | '2')) && !cs.get(i + 2).is_some_and(char::is_ascii_digit) && raw.prefix.is_none_or(|p| Some(p) == d); + raw.invalid |= !ok; + raw.prefix = d; + i += 2; + prefix_at = i; + continue; + } + // Any other `B` and digit (`B3dd`, `dd/mm/yyyyB2`, `dd B2 mm`, `B2[Red]B2`) makes Excel + // reject the code. + 'B' | 'b' if cs.get(i + 1).is_some_and(char::is_ascii_digit) => { + raw.invalid = true; + push_lit(toks, c.encode_utf8(&mut [0; 4])); + } '"' => { let mut s = String::new(); i += 1; @@ -283,6 +336,8 @@ fn tokenize(src: &str) -> Raw { if is_currency_char(d) { raw.currency = true; } + // Excel rejects an escaped `\B2` as well. + raw.invalid |= matches!(d, 'B' | 'b') && cs.get(i + 2).is_some_and(char::is_ascii_digit); push_lit(toks, d.encode_utf8(&mut [0; 4])); } i += 2; @@ -383,6 +438,11 @@ fn tokenize(src: &str) -> Raw { } i += 1; } + // Excel keeps the date Gregorian when a `[$-401]`-style locale or currency tag comes before + // `B2` (`B2[$-401]` is still Hijri). + if raw.prefix == Some('2') && !locale_first { + raw.calendar = Calendar::Hijri; + } raw } @@ -438,7 +498,7 @@ fn date_part(t: &Tok) -> Option { } fn analyze(raw: Raw) -> Section { - let Raw { toks, color, cond, currency } = raw; + let Raw { toks, color, cond, currency, calendar, prefix, invalid } = raw; let is_date = toks.iter().any(|t| matches!(t, Tok::Date(_))); let has_general = toks.iter().any(|t| matches!(t, Tok::General)); let mut sec = Section { @@ -455,6 +515,8 @@ fn analyze(raw: Raw) -> Section { fraction: false, currency, fill: toks.iter().any(|t| matches!(t, Tok::Fill(_))), + calendar, + invalid, }; if is_date { sec.kind = SecKind::Date; @@ -469,6 +531,9 @@ fn analyze(raw: Raw) -> Section { sec.kind = SecKind::Text; return analyze_text(toks, sec); } + // Number section. Excel rejects a calendar prefix before digits (`B20.00`, `B1#,##0`); + // `B2General`, `B2@` and `B2"x"` show as without the prefix. + sec.invalid |= prefix.is_some() && sec.has_digits; analyze_number(toks, sec) } diff --git a/crates/numfmt/src/render.rs b/crates/numfmt/src/render.rs index bb7fd1514..bfd31e56c 100644 --- a/crates/numfmt/src/render.rs +++ b/crates/numfmt/src/render.rs @@ -1,8 +1,9 @@ //! Rendering a value with a parsed section. use gridcraft_core::DateSystem; -use gridcraft_core::date::{MONTHS, WEEKDAYS, datetime_from_serial, max_serial}; +use gridcraft_core::date::{datetime_from_serial, max_serial}; +use crate::calendar::Calendar; use crate::decimal::{decimal_digits, fixed, fixed_from_digits, format_general_fit}; use crate::parse::{DatePart, Section, Tok}; @@ -72,6 +73,15 @@ pub(crate) fn render_date(sec: &Section, v: f64, sys: DateSystem) -> Option day += 1; } let total_ms = day.saturating_mul(86_400_000).saturating_add(ms); + // Under `B2`, Excel shows the 1900 system's fictitious 29 February (serial 60) as `#VALUE!` + // whenever a clock or calendar part is shown, even for `hh:mm` (`[h]` alone still works). + if sec.calendar == Calendar::Hijri + && sys == DateSystem::D1900 + && day == 60 + && sec.toks.iter().any(|t| matches!(t, Tok::Date(d) if !d.is_elapsed())) + { + return None; + } let needs_date = sec.toks.iter().any(|t| matches!(t, Tok::Date(d) if d.is_calendar())); let dt = if needs_date { datetime_from_serial(sys, day as f64)? } else { datetime_from_serial(sys, 0.0).unwrap_or_default() }; let ampm = sec.toks.iter().any(|t| matches!(t, Tok::Date(DatePart::AmPm | DatePart::AP(..)))); @@ -80,8 +90,8 @@ pub(crate) fn render_date(sec: &Section, v: f64, sys: DateSystem) -> Option let second = (ms / 1000) % 60; let milli = ms % 1000; let pad = |n: i64, w: u8| if w >= 2 { format!("{:0w$}", n, w = w.min(20) as usize) } else { n.to_string() }; - let month_name = MONTHS.get((dt.month as usize).wrapping_sub(1)).copied().unwrap_or(""); - let day_name = WEEKDAYS.get(dt.weekday as usize).copied().unwrap_or(""); + // No date in the section's calendar (serial 60 under `B2`): shown like an out-of-range date. + let date = sec.calendar.date(&dt)?; let mut out = Out::default(); for t in &sec.toks { match t { @@ -89,21 +99,21 @@ pub(crate) fn render_date(sec: &Section, v: f64, sys: DateSystem) -> Option let s = match *d { DatePart::Year(n) => { if n <= 2 { - format!("{:02}", dt.year.rem_euclid(100)) + format!("{:02}", date.year.rem_euclid(100)) } else { - format!("{:04}", dt.year) + format!("{:04}", date.year) } } DatePart::Month(n) => match n { - 1 | 2 => pad(dt.month as i64, n), - 3 => month_name.chars().take(3).collect(), - 5 => month_name.chars().take(1).collect(), - _ => month_name.to_string(), + 1 | 2 => pad(date.month as i64, n), + 3 => date.month_short.to_string(), + 5 => date.month_name.chars().take(1).collect(), + _ => date.month_name.to_string(), }, DatePart::Day(n) => match n { - 1 | 2 => pad(dt.day as i64, n), - 3 => day_name.chars().take(3).collect(), - _ => day_name.to_string(), + 1 | 2 => pad(date.day as i64, n), + 3 => date.day_short.to_string(), + _ => date.day_name.to_string(), }, DatePart::Hour(n) => { let h = if ampm { diff --git a/crates/numfmt/src/tests.rs b/crates/numfmt/src/tests.rs index b2709b320..0e9a169a5 100644 --- a/crates/numfmt/src/tests.rs +++ b/crates/numfmt/src/tests.rs @@ -902,6 +902,240 @@ fn closed_brackets_still_hold_their_semicolons() { assert_eq!(f("\"[\"0;0", 5.0), "[5"); } +const JAN_1_2001: f64 = 36892.0; + +#[test] +fn b1_prefix_is_hidden_and_gregorian() { + assert_eq!(f("B1dd/mm/yyyy", 46314.0), "19/10/2026"); + assert_eq!(f("b1dddd, mmmm d, yyyy", 46314.0), "Monday, October 19, 2026"); + assert_eq!(f("B1yyyy-mm-dd hh:mm", 46314.5), "2026-10-19 12:00"); +} + +#[test] +fn b2_prefix_renders_hijri_date() { + assert_eq!(f("B2dd/mm/yyyy", JAN_1_2001), "06/10/1421"); + assert_eq!(f("b2d/m/yy", JAN_1_2001), "6/10/21"); + assert_eq!(f("B2dddd dd/mm/yyyy hh:mm", JAN_1_2001 + 0.75), "الإثنين 06/10/1421 18:00"); + assert_eq!(text_function(&Value::Number(46314.0), "B2dd/mm/yyyy", S).as_deref(), Ok("08/05/1448")); + assert_eq!(f("B2e", 46314.0), "1448"); + assert_eq!(f("B2ee", 46314.0), "1448"); + assert_eq!(f("B2d/m/yyyy;@", 46314.0), "8/5/1448"); +} + +/// Under `B2`, Excel shows Arabic weekday names, the full name for `ddd` as well as `dddd`. +#[test] +fn b2_prefix_names_weekdays_in_arabic() { + // Monday 19 and Tuesday 20 October 2026. + assert_eq!(f("B2dddd", 46314.0), "الإثنين"); + assert_eq!(f("B2ddd", 46314.0), "الإثنين"); + assert_eq!(f("B2dddd", 46315.0), "الثلاثاء"); + assert_eq!(f("B1dddd", 46314.0), "Monday"); + assert_eq!(f("B1ddd", 46315.0), "Tue"); + // Every weekday has a name. + for v in 46314..46321 { + assert!(!f("B2dddd", v as f64).is_empty()); + } +} + +/// `B2` on a General or text section changes nothing: Excel shows the plain serial. +#[test] +fn b2_prefix_on_general_and_text_sections() { + assert_eq!(f("B2General", 46314.0), "46314"); + assert_eq!(f("B2@", 46314.0), "46314"); +} + +#[test] +fn b2_prefix_names_hijri_months() { + assert_eq!(f("B2d mmmm yyyy", JAN_1_2001), "6 شوال 1421"); + assert_eq!(f("B2mmm", 37034.0), "ربيع\u{a0}الأول"); + assert_eq!(f("B2mmmm", JAN_1_2001 + 23.0), "شوال"); + assert_eq!(f("B2mmmm", JAN_1_2001 + 24.0), "ذو\u{a0}القعدة"); + assert_eq!(f("B2mmmmm", JAN_1_2001 + 24.0), "ذ"); +} + +/// All twelve month names and seven weekday names as Excel 16.0 (build 20527, en-US) shows them, +/// read with TEXT and a cell format: two-word month names are joined by a no-break space. +#[test] +fn b2_names_match_excel() { + let months = [ + (46219.0, "صفر"), + (46248.0, "ربيع\u{a0}الأول"), + (46278.0, "ربيع\u{a0}الثاني"), + (46307.0, "جمادى\u{a0}الأولى"), + (46337.0, "جمادى\u{a0}الثانية"), + (46366.0, "رجب"), + (46396.0, "شعبان"), + (46425.0, "رمضان"), + (46455.0, "شوال"), + (46484.0, "ذو\u{a0}القعدة"), + (46514.0, "ذو\u{a0}الحجة"), + (46543.0, "محرم"), + ]; + for (serial, name) in months { + assert_eq!(f("B2dd", serial), "01", "{serial}"); + assert_eq!(f("B2mmmm", serial), name, "{serial}"); + assert_eq!(f("B2mmm", serial), name, "{serial}"); + assert_eq!(f("B2mmmmmm", serial), name, "{serial}"); + } + // Sunday 18 to Saturday 24 October 2026. + let days = ["الأحد", "الإثنين", "الثلاثاء", "الأربعاء", "الخميس", "الجمعة", "السبت"]; + for (k, name) in days.iter().enumerate() { + let serial = 46313.0 + k as f64; + assert_eq!(f("B2dddd", serial), *name, "{serial}"); + assert_eq!(f("B2ddd", serial), *name, "{serial}"); + } + assert_eq!(f("B2yy", 46314.0), "48"); + assert_eq!(f("B2y", 46314.0), "48"); + assert_eq!(f("B2yyy", 46314.0), "1448"); + assert_eq!(f("B2yyyyy", 46314.0), "1448"); +} + +#[test] +fn b2_month_and_year_rollover() { + assert_eq!(f("B2dd/mm/yyyy", 36915.0), "29/10/1421"); + assert_eq!(f("B2dd/mm/yyyy", 36916.0), "01/11/1421"); + assert_eq!(f("B2dd/mm/yyyy", 36974.0), "29/12/1421"); + assert_eq!(f("B2dd/mm/yyyy", 36975.0), "01/01/1422"); + assert_eq!(f("B2dd/mm/yyyy", 37683.0), "30/12/1423"); + assert_eq!(f("B2dd/mm/yyyy", 37684.0), "01/01/1424"); +} + +/// Cycle years 15 (1425, leap) and 16 (1426, not leap): Excel's leap year is 15, not 16. The +/// 2 March 2005 and 29 January 2006 values were read from Excel by a reviewer; the year +/// boundaries follow from them and the 354/355-day years. The same comparison covered every +/// serial from 36,000 to 49,000 with no disagreement. +#[test] +fn b2_leap_year_is_cycle_year_15() { + assert_eq!(f("B2yyyy-mm-dd", 38413.0), "1426-01-21"); // 2005-03-02, from Excel + assert_eq!(f("B2yyyy-mm-dd", 38746.0), "1426-12-29"); // 2006-01-29, from Excel + assert_eq!(f("B2dd/mm/yyyy", 38037.0), "29/12/1424"); // 2004-02-20 + assert_eq!(f("B2dd/mm/yyyy", 38038.0), "01/01/1425"); // 2004-02-21 + assert_eq!(f("B2dd/mm/yyyy", 38392.0), "30/12/1425"); // 2005-02-09 + assert_eq!(f("B2dd/mm/yyyy", 38393.0), "01/01/1426"); // 2005-02-10 + assert_eq!(f("B2dd/mm/yyyy", 38747.0), "01/01/1427"); // 2006-01-30 + // Anchors the reviewer read from Excel. + assert_eq!(f("B2dd/mm/yyyy", 46314.0), "08/05/1448"); // 2026-10-19 + assert_eq!(f("B2dd/mm/yyyy", 46313.0), "07/05/1448"); // 2026-10-18 + assert_eq!(f("B2dd/mm/yyyy", JAN_1_2001), "06/10/1421"); + assert_eq!(f("B2dd/mm/yyyy", 61.0), "29/10/1317"); // 1900-03-01 + assert_eq!(f("B2dd/mm/yyyy", 64970.0), "01/01/1501"); // 2077-11-16 + assert_eq!(f("B2dd/mm/yyyy", 64971.0), "02/01/1501"); // 2077-11-17 + assert_eq!(f("B2dd/mm/yyyy", 0.0), "29/08/1317"); + assert_eq!(f("B2dd/mm/yyyy", 2958465.0), "03/04/9666"); // 9999-12-31 +} + +/// Excel honours `B2` after a colour tag, but a `[$-...]` locale tag before it keeps the date +/// Gregorian (the prefix is still hidden); `B2[$-401]` is Hijri. +#[test] +fn calendar_prefix_and_bracket_tags() { + assert_eq!(f("[Red]B2dd/mm/yyyy", 46314.0), "08/05/1448"); + assert_eq!(f("[<0]0;B2dd/mm/yyyy", JAN_1_2001), "06/10/1421"); + assert_eq!(f("B2[$-401]dd/mm/yyyy", 46314.0), "08/05/1448"); + for code in ["[$-401]B2dd/mm/yyyy", "[$-409]B2dd/mm/yyyy", "[$-1010401]B2dd/mm/yyyy", "[$-ar-SA]B2dd/mm/yyyy"] { + assert_eq!(f(code, 46314.0), "19/10/2026", "{code}"); + assert_eq!(text_function(&Value::Number(46314.0), code, S).as_deref(), Ok("19/10/2026"), "{code}"); + } + assert_eq!(f("[Red][$-401]B1dd/mm/yyyy", JAN_1_2001), "01/01/2001"); + // Checked in Excel 16.0 (build 20527, en-US): a currency tag cancels `B2` like a locale tag, + // a tag after the prefix does not, and a repeated prefix is accepted. + assert_eq!(f("[$€-407]B2dd/mm/yyyy", 46314.0), "€19/10/2026"); + assert_eq!(f("[$$-409]B2dd/mm/yyyy", 46314.0), "$19/10/2026"); + assert_eq!(f("[$-401][Red]B2dd/mm/yyyy", 46314.0), "19/10/2026"); + assert_eq!(f("B2[Red]dd/mm/yyyy", 46314.0), "08/05/1448"); + for code in ["B2B2dd/mm/yyyy", "B2B2B2dd/mm/yyyy", "B2b2dd/mm/yyyy", "[Red]B2B2dd/mm/yyyy"] { + assert_eq!(f(code, 46314.0), "08/05/1448", "{code}"); + } + assert_eq!(f("B1B1dd/mm/yyyy", 46314.0), "19/10/2026"); + assert_eq!(f("B2 dd/mm/yyyy", 46314.0), " 08/05/1448"); + assert_eq!(f("B2\"x\"dd", 46314.0), "x08"); + for code in ["B1\"x\"", "B2\"x\""] { + assert_eq!(f(code, 46314.0), "x", "{code}"); + } + for code in ["B1General", "B1@", "0;0;0;B2@", "0;B2dd/mm/yyyy"] { + assert_eq!(f(code, 46314.0), "46314", "{code}"); + } +} + +/// A quoted `B2` is literal text; any other `B` and digit makes Excel reject the code (`TEXT` is +/// `#VALUE!`), as does a prefix before a number format. A rejected code shows as General. All +/// checked in Excel 16.0 (build 20527, en-US), with TEXT and with a cell format. +#[test] +fn misplaced_calendar_prefix_is_rejected() { + assert_eq!(f("\"B2\"dd/mm/yyyy", 46314.0), "B219/10/2026"); + assert_eq!(f("dd/\"B2\"/yyyy", 46314.0), "19/B2/2026"); + for code in [ + "B0dd/mm/yyyy", + "B3dd/mm/yyyy", + "B4dd/mm/yyyy", + "B9dd/mm/yyyy", + "B02dd", + "B12dd", + "B21dd", + "B1B2dd/mm/yyyy", + "B2B1dd/mm/yyyy", + "B2[Red]B2dd/mm/yyyy", + "B2 B2dd", + "dd/mm/yyyyB2", + "dd/mm/yyyyB1", + "dd B2 mm", + "dd b2 mm", + "dd \\B2 yyyy", + "B2\\B2dd", + "B20.00", + "B2#,##0", + "B10.00", + "B1#,##0", + "0;B3dd", + ] { + assert_eq!(text_function(&Value::Number(46314.0), code, S), Err(CellError::Value), "{code}"); + assert_eq!(f(code, 46314.0), "46314", "{code}"); + assert!(NumberFormat::parse(code).is_general(), "{code}"); + } +} + +#[test] +fn b2_edges_of_the_date_range() { + // Serial 60 is the 1900 system's fictitious 29 February: Excel has no Hijri date for it. + assert_eq!(text_function(&Value::Number(60.0), "B2dd/mm/yyyy", S), Err(CellError::Value)); + assert_eq!(f("B2dd/mm/yyyy", 60.0), "########"); + assert_eq!(f("B1dd/mm/yyyy", 60.0), "29/02/1900"); + assert_eq!(f("B2dd/mm/yyyy", 59.0), "28/10/1317"); + assert_eq!(f("B2dd/mm/yyyy", 61.0), "29/10/1317"); + // So does any clock part (Excel 16.0, build 20527, en-US), but not an elapsed `[h]`, a literal, + // or a locale tag that cancels `B2`. + for code in ["B2hh:mm", "B2hh", "B2ss", "B2am/pm", "B2hh:mm AM/PM", "B2[$-401]hh:mm", "B2dd/mm/yyyy;\"x\""] { + assert_eq!(text_function(&Value::Number(60.0), code, S), Err(CellError::Value), "{code}"); + assert_eq!(text_function(&Value::Number(60.5), code, S), Err(CellError::Value), "{code}"); + } + assert_eq!(f("B2[h]", 60.0), "1440"); + assert_eq!(f("B2\"x\"", 60.0), "x"); + assert_eq!(f("[$-401]B2hh:mm", 60.0), "00:00"); + assert_eq!(f("B1hh:mm", 60.0), "00:00"); + assert_eq!(f("B2hh:mm", 61.0), "00:00"); + // Serial 0 shows as serial 1. + assert_eq!(f("B2dd/mm/yyyy", 0.0), "29/08/1317"); + assert_eq!(f("B2dd/mm/yyyy", 1.0), "29/08/1317"); + // No upper limit short of 31 December 9999: 16 and 17 November 2077, 31 December 9999. + assert_eq!(f("B2dd/mm/yyyy", 64970.0), "01/01/1501"); + assert_eq!(f("B2dd/mm/yyyy", 64971.0), "02/01/1501"); + assert_eq!(f("B2dd/mm/yyyy", 2958465.0), "03/04/9666"); + // In the 1904 system, serial 60 is a real day. + assert_ne!(format_value(&Value::Number(60.0), &NumberFormat::parse("B2dd/mm/yyyy"), DateSystem::D1904).text, "########"); +} + +#[test] +fn b2_out_of_range_does_not_panic() { + for v in [-1.0, 0.0, 60.0, 60.5, 2958465.0, 2958466.0, 1e300, f64::NAN] { + let _ = f("B2dd/mm/yyyy mmmm dddd", v); + let _ = text_function(&Value::Number(v), "B2dd/mm/yyyy mmmm dddd", S); + } + assert_eq!(f("B2dd/mm/yyyy", -1.0), f("dd/mm/yyyy", -1.0)); + assert_eq!(f("B2dd/mm/yyyy", 2958466.0), f("dd/mm/yyyy", 2958466.0)); + // Before 1 Muharram 1 AH there is no Hijri date (no serial reaches it). + let before_epoch = gridcraft_core::date::DateTime { year: 600, month: 3, day: 9, ..Default::default() }; + assert!(calendar::Calendar::Hijri.date(&before_epoch).is_none()); +} + /// "Set precision as displayed": the value each format stores. Expected values observed in Excel /// (COM, `Workbook.PrecisionAsDisplayed = True`) with synthetic values. #[test] diff --git a/docs/localization-parity.md b/docs/localization-parity.md index 3f1dacb96..d504c19ea 100644 --- a/docs/localization-parity.md +++ b/docs/localization-parity.md @@ -1,6 +1,6 @@ # Localization parity -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-27 · **Change:** minor (AutoFit estimates Arabic text width from measured glyph widths); previously minor (missing translations are listed by `cargo xtask i18n-report` and never fail a check; Arabic text sorts by its letters, not its vowel marks) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-27 · **Change:** minor (number formats honour the `B1`/`B2` calendar prefixes, checked against Excel; earlier: missing translations are listed by `cargo xtask i18n-report` and never fail a check; Arabic text sorts by its letters, not its vowel marks); previously minor (AutoFit estimates Arabic text width from measured glyph widths); previously minor (missing translations are listed by `cargo xtask i18n-report` and never fail a check; Arabic text sorts by its letters, not its vowel marks) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Per-language status against Excel, which ships its UI in dozens of languages (Windows language packs cover 100+), with localized function names, argument separators, number and date formats, @@ -40,7 +40,21 @@ right-to-left sheets and full complex-script support. Part of - **en-US only formulas and formats.** Function names, `,` argument separator, `.` decimal separator, en-US number and date formats (`crates/numfmt` says "en-US only"). Users ask for localized function names and `;` separators (#45). German with localized numbers and formulas - is in PR #163 (open). + is in PR #163 (open). The `B1` (Gregorian) and `B2` (Hijri) calendar prefixes that Arabic + Excel writes at the start of a format section are hidden; under `B2` the year, month number, + day, month names and weekday names (Arabic; `mmm` and `ddd` show the full name) count in the + tabular Hijri calendar (leap years 2, 5, 7, 10, 13, 15, 18, 21, 24, 26 and 29 of each 30-year + cycle). Checked against Excel 16.0 (build 20527, en-US) on every serial from 1 to 2,958,465 + and about 1,700 format/value pairs: a `[$...]` locale or currency tag before `B2` keeps the + date Gregorian; a repeated prefix (`B2B2`) is accepted; any other `B` and digit (`B3`, + `B12`, `B1B2`, `dd B2`, `\B2`), or a prefix before a number format (`B10.00`), is + rejected (`TEXT` gives `#VALUE!`, a cell shows General); serial 60 shows `#VALUE!` under `B2` + for any date or clock part; two-word month names use a no-break space. Differs from Excel: + in a `B2` section with a weekday and a month name, Excel prints the weekday for + `mmmm`/`mmmmm` after `ddd`/`dddd`, and the weekday numbered by the month (Saturday from + month 7) for `ddd`/`dddd` after `mmm`/`mmmm`; a `B2` section showing only the weekday uses + the real weekday for serials 0–59 and gives `#VALUE!` for the last days of 9999. Excel's + Buddhist-year `b`/`bb`/`bbbb` tokens are not supported. - **Script support.** System-font fallback for CJK, Thai and Hebrew landed in #38 (closes #32; #142 is still open pending confirmation). Native CJK fallback also probes Flatpak's read-only `/run/host/fonts`, `/run/host/local-fonts` and `/run/host/user-fonts` layouts, so installed @@ -110,6 +124,7 @@ language (none so far) and of localized function names. | Date | Change | Summary | |---|---|---| +| 2026-10-27 | minor | Number formats honour the `B1`/`B2` calendar prefixes: never shown; `B2` renders year, month, day and Arabic month and weekday names in the tabular Hijri calendar (1 Jan 2001 = 6 Shawwal 1421; leap years 2, 5, 7, 10, 13, 15, 18, 21, 24, 26 and 29 of the 30-year cycle, which is what Excel shows). Checked against Excel 16.0 (build 20527, en-US) on every serial and about 1,700 format/value pairs: a locale or currency tag before `B2` keeps the date Gregorian, a misplaced `B` and digit or a prefix before a number format is rejected, serial 60 has no Hijri date, serial 0 shows as serial 1, two-word month names use a no-break space. Not matched: Excel's weekday/month-name mix-ups in one `B2` section and its weekday-only quirks | | 2026-10-27 | minor | AutoFit column width, wrapped-row height and PivotTable column fit estimate Arabic letters at ~0.48 em (measured 0.36–0.53 em shaped, Noto Sans/Naskh Arabic and DejaVu Sans) instead of 1 em, Arabic-Indic digits as wide as ASCII digits, and give vowel marks and direction marks no width; vowelled Arabic no longer fits ~3.5× too wide | | 2026-10-23 | minor | Missing translations are reported, never a failure: `cargo xtask i18n-report` prints, per language, the strings other catalogs translate and it lacks (plus Spanish function help rows keyed by English the function catalog no longer has) and always passes; `cargo xtask ci` runs it first. Coverage and per-language parity tests and tests of one language's exact wording are gone; UI tests take the expected text from the catalog. Duplicate-key, placeholder, context-prefix and line-shape checks stay | | 2026-10-21 | minor | Romanian interface (`ro`, Română): 970 labels covering every key of the other catalogs (`crates/ui-egui/src/i18n/ro.rs`), `ro-*` system locales, the View-tab switcher and `app.language.set`; context keys show no prefix (File › Options test). English formulas and formats; AI-assisted draft pending native-speaker review | From cda767b49b1b4fdf691cb4cc4b95bdd40c9262d0 Mon Sep 17 00:00:00 2001 From: Mouhand Kaddo Date: Mon, 9 Nov 2026 18:54:55 +0400 Subject: [PATCH 30/76] Arabic font fallback in cells, the formula bar and the web (#931) * M12.AR2: Arabic font fallback in cells and the interface (#229) * M12.AR2: reserve the preferred Arabic file only once it has loaded load_fallback treated fontconfig's Arabic file as already loaded even when load_preferred_arabic could not load it, so a system Arabic face from the same file was skipped and the chains could end up with no Arabic system face. The file is now reserved only when the preferred face is in the font set; the candidate loop moves to load_faces so a test can drive it with a fixture file. Co-Authored-By: Claude Opus 5.5 * M12.AR2: say where fontconfig's Arabic choice applies fontconfig's Arabic face leads the Arabic fallbacks, so it draws Arabic where the UI or cell font has none. A UI font with Arabic of its own, such as DejaVu Sans in the Flatpak, still draws interface Arabic (#229), which is left for a follow-up. The docs and the test's doc comment no longer say the choice comes first in every chain. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com> Co-authored-by: FredApps --- ROADMAP.md | 2 + crates/ui-egui/src/craft_fonts_policy.rs | 3 + crates/ui-egui/src/fonts.rs | 29 +++-- crates/ui-egui/src/theme.rs | 9 +- crates/ui-egui/src/theme/font_fallback.rs | 95 ++++++++++++++- crates/ui-egui/src/theme/font_setup.rs | 37 ++++-- crates/ui-egui/src/theme/tests.rs | 141 +++++++++++++++++++++- docs/gaps.md | 5 +- docs/localization-parity.md | 7 +- 9 files changed, 296 insertions(+), 32 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 340bb2d06..f12cd112a 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -149,6 +149,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | 2026-10-31 | CSV and text open detects each column's date order: a value that reads only day first (`31/05/2026`) makes the column day first, a value that reads only month first (`12/31/2026`) makes it month first, an all-ambiguous column keeps month first with a warning, and a column with both kinds stays text with a warning (#2765) | | 2026-10-31 | Recently Used lists the functions most recently entered in a formula from any source (ribbon, control channel, MCP, scripts), kept with the preferences and returned by `formulas.recentlyUsed`; the Insert Function dialog takes Excel's title and a working category list (Most Recently Used, All, each category) | | 2026-10-31 | Formulas › Function Library as a big Insert Function button, the AutoSum split button and three columns of labelled small buttons, each category book in its own colour; category menus end with Insert Function… on that category; More Functions cascades into the remaining categories; Home › Editing shares the AutoSum menu; the MR key tip opens Recently Used | +| 2026-10-26 | Arabic text shows in cells and the interface through a system, fontconfig or craft-fonts Arabic face; workbook fonts keep their script fallbacks | | 2026-10-25 | AutoSum over a selection does what Excel does: a selected row gets one total to its right (in its last cell if that is empty), not a one-cell total under each cell; a block whose last column is empty gets each row's total there, one whose last row is empty each column's, and with both the corner gets the total of the block; otherwise totals go under each column as before | | 2026-10-24 | Macro interface ([macros.md](docs/macros.md#in-the-window)): an Automate › Macros group (Record Macro, which turns into Stop Recording, Use Relative References, Run Macro with recent macros); "Recording · N steps" with Stop in the status bar; a Record Macro dialog (checked name, free Ctrl+Shift shortcut, description); the Macros dialog lists macros beside Automate scripts and a VBA project, with Run, Step Into, Edit, Create, Delete and Options; Ctrl+Shift+letter macro shortcuts; questions that offer Run, Overwrite, Shift Down or Shift Right; a step editor with a breakpoint gutter, drag to reorder, a command picker, reference boxes that switch between relative tokens and absolute references, Inputs and Output tabs, a debug bar (Check, Run, Step, Continue, Run to Here, Stop with keep or undo) and grid overlays for inputs, writes, conflicts and the current step | | 2026-10-24 | Go To lists the named ranges of every sheet (`Sheet2!Name` for a name scoped to another sheet; hidden names, constants and formulas left out) and the references entered before, a double-click goes to an entry, and the Reference box takes several names or references separated by commas (`Price, Type`, `B14:C22,F19:G30`) | @@ -276,6 +277,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | 2026-10-31 | minor | File-page text fields keep the view open when Esc cancels an edit (#3035) | | 2026-10-31 | minor | Progress log: Recently Used and Insert Function categories | | 2026-10-31 | minor | Progress log: Function Library | +| 2026-10-26 | minor | Progress log: Arabic font fallback | | 2026-10-25 | minor | Progress log: AutoSum over a selected row, and a selection's empty last row and column | | 2026-10-24 | minor | Macro interface on the Automate tab; progress log row | | 2026-10-24 | minor | Macro engine and `macro.*` commands; catalog 275 / 291 → 276 / 292 (View › Macros › Record Macro); progress log row | diff --git a/crates/ui-egui/src/craft_fonts_policy.rs b/crates/ui-egui/src/craft_fonts_policy.rs index d88fa6a75..1a9109406 100644 --- a/crates/ui-egui/src/craft_fonts_policy.rs +++ b/crates/ui-egui/src/craft_fonts_policy.rs @@ -24,6 +24,7 @@ pub const REQUIRED_FACES: &[(&str, &str, &[&str])] = &[ ("NanumGothic", "Regular", &["Hang", "Kore"]), ("Noto Sans Symbols", "Regular", &["Zsym"]), ("Noto Sans Symbols 2", "Regular", &["Zsym"]), + ("Noto Sans Arabic", "Regular", &["Arab"]), ]; /// The native-only Simplified Chinese face from craft-fonts' separate @@ -41,6 +42,7 @@ pub const WEB_ALLOWED: &[(&str, &str)] = &[ ("NanumGothic", "Regular"), ("Noto Sans Symbols", "Regular"), ("Noto Sans Symbols 2", "Regular"), + ("Noto Sans Arabic", "Regular"), ]; /// One validated `fonts/manifest.txt` row. Only the fields GridCraft consumes are kept. @@ -172,6 +174,7 @@ mod tests { ("NanumGothic", "Regular"), ("Noto Sans Symbols", "Regular"), ("Noto Sans Symbols 2", "Regular"), + ("Noto Sans Arabic", "Regular"), ] ); for (family, _) in WEB_ALLOWED { diff --git a/crates/ui-egui/src/fonts.rs b/crates/ui-egui/src/fonts.rs index 699806eba..da7dd57ec 100644 --- a/crates/ui-egui/src/fonts.rs +++ b/crates/ui-egui/src/fonts.rs @@ -19,6 +19,24 @@ use std::sync::OnceLock; #[cfg(not(target_arch = "wasm32"))] const MAX_FONT_FILE: u64 = 64 << 20; +/// Reads a font file of at most [`MAX_FONT_FILE`] bytes, even when it grows after it was found. +#[cfg(not(target_arch = "wasm32"))] +pub(crate) fn read_font(path: &std::path::Path) -> Option> { + use std::io::Read as _; + let file = std::fs::File::open(path).ok()?; + if file.metadata().ok()?.len() > MAX_FONT_FILE { + return None; + } + let mut bytes = Vec::new(); + file.take(MAX_FONT_FILE + 1).read_to_end(&mut bytes).ok()?; + (bytes.len() as u64 <= MAX_FONT_FILE).then_some(bytes) +} + +#[cfg(target_arch = "wasm32")] +pub(crate) fn read_font(_path: &std::path::Path) -> Option> { + None +} + /// A face keeps at most this many family names, so a hostile `name` table can't grow the catalog. #[cfg(not(target_arch = "wasm32"))] const MAX_FAMILY_NAMES: usize = 16; @@ -172,7 +190,7 @@ pub fn scan_dirs(dirs: &[PathBuf]) -> Vec { if !is_font || std::fs::metadata(&path).map(|m| m.len() > MAX_FONT_FILE).unwrap_or(true) { continue; } - let Ok(data) = std::fs::read(&path) else { continue }; + let Some(data) = read_font(&path) else { continue }; for (index, family, names, style) in file_faces(&data) { out.push(Face { family, names, path: path.clone(), index, weight: style_weight(&style), italic: style_italic(&style) }); } @@ -335,7 +353,7 @@ pub fn bind_faces(fonts: &mut egui::FontDefinitions, family: &str, faces: &[Face continue; } if !fonts.font_data.contains_key(&key) { - let Ok(bytes) = std::fs::read(&face.path) else { continue }; + let Some(bytes) = read_font(&face.path) else { continue }; let mut fd = egui::FontData::from_owned(bytes); fd.index = face.index; fonts.font_data.insert(key.clone(), std::sync::Arc::new(fd)); @@ -353,11 +371,6 @@ pub fn bind_faces(fonts: &mut egui::FontDefinitions, family: &str, faces: &[Face bound } -/// Bind every installed face of `family`, found by any of its names (see [`bind_faces`]). -pub fn bind_family(fonts: &mut egui::FontDefinitions, family: &str) -> Vec { - bind_faces(fonts, family, &faces_of(family)) -} - /// The role families currently bound to font data. `cell_family` consults this, so it never names a /// family egui hasn't been given. static BOUND: OnceLock>> = OnceLock::new(); @@ -472,6 +485,8 @@ mod tests { } // A family with no faces binds nothing and never touches the map. assert!(bind_faces(&mut fonts, "No Such Font", &[]).is_empty()); + std::fs::File::create(&face.path).expect("sparse file").set_len(MAX_FONT_FILE + 1).expect("oversized file"); + assert!(read_font(&face.path).is_none(), "an oversized font is not read"); let _ = std::fs::remove_dir_all(&dir); } diff --git a/crates/ui-egui/src/theme.rs b/crates/ui-egui/src/theme.rs index 343fd79f4..9ebf19944 100644 --- a/crates/ui-egui/src/theme.rs +++ b/crates/ui-egui/src/theme.rs @@ -25,7 +25,12 @@ mod font_setup; pub use font_fallback::symbol_face_installed; #[cfg(test)] -use font_fallback::{cjk_han_japanese, cjk_han_simplified, cjk_hangul, fallback_faces, hebrew, lao, symbols_more, thai}; +use font_fallback::{ + PREFERRED_ARABIC, arabic, cjk_han_japanese, cjk_han_simplified, cjk_hangul, fallback_faces, hebrew, lao, load_faces, preferred_file, + symbols_more, thai, +}; +#[cfg(all(test, unix, not(target_os = "macos")))] +use font_fallback::{covers_arabic, fontconfig_arabic, parse_fc_match}; use font_setup::pick; pub use font_setup::{ CELL, CELL_BOLD, CELL_BOLD_ITALIC, CELL_ITALIC, CraftFont, HanOrder, MATH, MATH_BOLD, MATH_ITALIC, MONO, SERIF, UI, UI_BOLD, available_text, @@ -33,7 +38,7 @@ pub use font_setup::{ font_definitions, font_definitions_for_language, font_definitions_with, pinned_font_definitions, symbol_fallback, ui_bold, ui_font, }; #[cfg(test)] -use font_setup::{candidates, craft_font_faces, try_load}; +use font_setup::{candidates, craft_font_faces, font_definitions_impl, try_load}; #[cfg(test)] pub(crate) use font_setup::{drop_unparseable_fonts, font_parses}; diff --git a/crates/ui-egui/src/theme/font_fallback.rs b/crates/ui-egui/src/theme/font_fallback.rs index 1f5d70150..65479d017 100644 --- a/crates/ui-egui/src/theme/font_fallback.rs +++ b/crates/ui-egui/src/theme/font_fallback.rs @@ -12,12 +12,25 @@ use super::font_setup::{in_dirs, try_load}; /// is bundled, and a machine without the face keeps the old behaviour. #[cfg(not(target_arch = "wasm32"))] pub(super) fn load_fallback(fonts: &mut FontDefinitions, han: HanOrder) -> Vec { + let loaded = preferred_file(fonts, fontconfig_arabic()); + load_faces(fonts, fallback_faces(han), loaded) +} + +/// The preferred Arabic file, once [`load_preferred_arabic`] has loaded it: a file that failed to +/// load must not keep the system Arabic faces out of the chains. +#[cfg(not(target_arch = "wasm32"))] +pub(super) fn preferred_file(fonts: &FontDefinitions, preferred: Option<(String, u32)>) -> Vec { + preferred.filter(|_| fonts.font_data.contains_key(PREFERRED_ARABIC)).map(|(path, _)| path).into_iter().collect() +} + +/// Loads the first candidate of each fallback face that loads and returns their keys. `loaded` +/// holds the files already loaded as a fallback face. A file serves at most one key: the +/// region-merged Noto Sans CJK .ttc found for Han also carries Hangul (and Tahoma carries both Thai +/// and Hebrew), so loading it again for a later script would only duplicate tens of MB in memory. +#[cfg(not(target_arch = "wasm32"))] +pub(super) fn load_faces(fonts: &mut FontDefinitions, faces: Vec<(&'static str, Vec<(String, u32)>)>, mut loaded: Vec) -> Vec { let mut keys = Vec::new(); - // Files already loaded as a fallback face. A file serves at most one key: the region-merged - // Noto Sans CJK .ttc found for Han also carries Hangul (and Tahoma carries both Thai and - // Hebrew), so loading it again for a later script would only duplicate tens of MB in memory. - let mut loaded: Vec = Vec::new(); - for (key, cands) in fallback_faces(han) { + for (key, cands) in faces { for (path, index) in &cands { if loaded.contains(path) { break; // a face loaded for an earlier script already covers this one @@ -38,6 +51,60 @@ pub(super) fn load_fallback(_fonts: &mut FontDefinitions, _han: HanOrder) -> Vec Vec::new() } +/// The face fontconfig prefers for Arabic leads the script faces (#229), ahead of the system and +/// bundled Arabic faces. +#[cfg(not(target_arch = "wasm32"))] +pub(super) fn load_preferred_arabic(fonts: &mut FontDefinitions) -> Vec { + let Some((path, index)) = fontconfig_arabic() else { return Vec::new() }; + let Some(fd) = try_load(&[(path.as_str(), index)]) else { return Vec::new() }; + fonts.font_data.insert(PREFERRED_ARABIC.into(), Arc::new(fd)); + vec![PREFERRED_ARABIC.into()] +} + +#[cfg(target_arch = "wasm32")] +pub(super) fn load_preferred_arabic(_fonts: &mut FontDefinitions) -> Vec { + Vec::new() +} + +#[cfg(not(target_arch = "wasm32"))] +pub(super) const PREFERRED_ARABIC: &str = "sys-arabic-preferred"; + +/// The face fontconfig prefers for Arabic, such as a Vazirmatn the user set as their Arabic font +/// (#229), kept only when it has Arabic letters. Asked once per process. +#[cfg(all(unix, not(target_os = "macos")))] +pub(super) fn fontconfig_arabic() -> Option<(String, u32)> { + static PREFERRED: std::sync::OnceLock> = std::sync::OnceLock::new(); + PREFERRED + .get_or_init(|| { + let out = std::process::Command::new("fc-match").args(["--format=%{file}\n%{index}\n", ":lang=ar"]).output().ok()?; + let (file, index) = parse_fc_match(&String::from_utf8_lossy(&out.stdout)).filter(|_| out.status.success())?; + let bytes = crate::fonts::read_font(std::path::Path::new(&file))?; + covers_arabic(&bytes, index).then_some((file, index)) + }) + .clone() +} + +#[cfg(all(not(target_arch = "wasm32"), not(all(unix, not(target_os = "macos")))))] +pub(super) fn fontconfig_arabic() -> Option<(String, u32)> { + None +} + +/// The file and face index from `fc-match --format='%{file}\n%{index}\n'`. fontconfig packs a +/// variable font's named instance into the upper 16 bits of the index; only the face index is kept. +#[cfg(all(unix, not(target_os = "macos")))] +pub(super) fn parse_fc_match(out: &str) -> Option<(String, u32)> { + let mut lines = out.lines().map(str::trim); + let file = lines.next().filter(|file| !file.is_empty())?; + let index = lines.next().and_then(|index| index.parse::().ok()).map_or(0, |packed| packed & 0xFFFF); + Some((file.to_owned(), index)) +} + +#[cfg(all(unix, not(target_os = "macos")))] +pub(super) fn covers_arabic(bytes: &[u8], index: u32) -> bool { + use skrifa::MetadataProvider; + skrifa::FontRef::from_index(bytes, index).is_ok_and(|font| "العربية".chars().all(|ch| font.charmap().map(ch).is_some())) +} + /// Every fallback face we look for, in chain order: `(key, candidate files)`. One file per script /// family is enough — epaint picks the first face with the glyph, and a script's glyphs live in one /// file. Order only matters between faces that both cover a glyph (Japanese vs Chinese kanji), so @@ -57,6 +124,7 @@ pub(super) fn fallback_faces(han: HanOrder) -> Vec<(&'static str, Vec<(String, u ("sys-thai", thai()), ("sys-lao", lao()), ("sys-hebrew", hebrew()), + ("sys-arabic", arabic()), ("sys-symbols", symbols()), ("sys-symbols-2", symbols_more()), ] @@ -243,6 +311,23 @@ pub(super) fn hebrew() -> Vec<(String, u32)> { ) } +/// Arabic faces. Windows has Tahoma, Arial and Segoe UI with Arabic; macOS has Geeza Pro; Linux +/// usually has Noto Sans Arabic or Noto Naskh Arabic. Installed Noto and Amiri faces found by the +/// font scan come last, for layouts the paths miss. +#[cfg(not(target_arch = "wasm32"))] +pub(super) fn arabic() -> Vec<(String, u32)> { + let mut v = script_fonts( + &["tahoma.ttf", "arial.ttf", "segoeui.ttf"], + &["/System/Library/Fonts/GeezaPro.ttc", "/System/Library/Fonts/Supplemental/GeezaPro.ttc"], + &["NotoSansArabic-Regular.ttf", "NotoNaskhArabic-Regular.ttf", "noto/NotoSansArabic-Regular.ttf", "noto/NotoNaskhArabic-Regular.ttf"], + "/usr/share/fonts/google-noto-vf/NotoSansArabic[wght].ttf", + ); + for family in ["Noto Sans Arabic", "Noto Naskh Arabic", "Amiri"] { + v.extend(crate::fonts::faces_of(family).into_iter().map(|face| (face.path.to_string_lossy().into_owned(), face.index))); + } + v +} + /// Symbol faces behind the UI font. Interface fonts such as Noto Sans and Cantarell (and egui's /// built-in Ubuntu) lack arrows, check marks, geometric shapes and keyboard symbols (▾ ✓ ✕ ▲ ↳ ⇧), /// which then render as empty boxes (#197, #54). DejaVu Sans covers most of them; the paths cover diff --git a/crates/ui-egui/src/theme/font_setup.rs b/crates/ui-egui/src/theme/font_setup.rs index 3489b1443..da995c489 100644 --- a/crates/ui-egui/src/theme/font_setup.rs +++ b/crates/ui-egui/src/theme/font_setup.rs @@ -1,6 +1,6 @@ #[cfg(not(target_arch = "wasm32"))] use super::Blob; -use super::font_fallback::load_fallback; +use super::font_fallback::{load_fallback, load_preferred_arabic}; use super::{Arc, Color32, FontData, FontDefinitions, FontFamily, FontId}; /// A font from the optional craft-fonts build input (`CRAFT_FONTS_DIR` at build time; see @@ -152,7 +152,7 @@ fn font_file(path: &str, index: u32) -> Option { let bytes: &[u8] = (**blob).as_ref(); return font_parses(bytes, index).then(|| std::sync::Arc::clone(blob)); } - let bytes = std::fs::read(path).ok()?; + let bytes = crate::fonts::read_font(std::path::Path::new(path))?; if !font_parses(&bytes, index) { log::warn!("skipping font {path} (face {index}): not a font file egui can read"); return None; @@ -466,37 +466,45 @@ pub fn font_definitions_for_language(language: crate::i18n::Language) -> FontDef /// machine see the same font set. Craft-fonts remains a pinned build input rather than a repository /// asset; without it only egui's bundled faces are available, which is the documented no-font mode. pub fn pinned_font_definitions() -> FontDefinitions { - font_definitions_impl(crate::i18n::Language::En, &[], false) + font_definitions_impl(crate::i18n::Language::En, Vec::new(), false) } /// [`font_definitions_for_language`] that also binds the workbook fonts in `used` that are /// installed, so cells set in them render with them; the rest fall back to the roles. The bound /// families are recorded in [`crate::fonts`] so [`cell_family`] only names a family egui has. pub fn font_definitions_with(language: crate::i18n::Language, used: &[String]) -> FontDefinitions { - font_definitions_impl(language, used, true) + let installed = crate::fonts::installed_among(used).into_iter().map(|family| { + let faces = crate::fonts::faces_of(&family); + (family, faces) + }); + font_definitions_impl(language, installed.collect(), true) } /// Builds the role font chains, script fallbacks, installed workbook fonts and optional craft-font faces, drops unparseable data and records which families were bound. -fn font_definitions_impl(language: crate::i18n::Language, used: &[String], system: bool) -> FontDefinitions { +pub(super) fn font_definitions_impl( + language: crate::i18n::Language, + installed: Vec<(String, Vec)>, + system: bool, +) -> FontDefinitions { let mut fonts = FontDefinitions::default(); let base_prop: Vec = fonts.families.get(&FontFamily::Proportional).cloned().unwrap_or_default(); let base_mono: Vec = fonts.families.get(&FontFamily::Monospace).cloned().unwrap_or_default(); let mut bound: Vec = Vec::new(); // The non-Latin fallback faces have to sit at the end of each chain: a Latin font owns Latin, // and epaint picks the first face that has the glyph, so they only catch their own scripts. - let fallback = if system { load_fallback(&mut fonts, HanOrder::of(language)) } else { Vec::new() }; + let fallback = if system { + let mut fallback = load_preferred_arabic(&mut fonts); + fallback.extend(load_fallback(&mut fonts, HanOrder::of(language))); + fallback + } else { + Vec::new() + }; let hack = fonts.font_data.contains_key(HACK).then_some(HACK); for role in [UI, UI_BOLD, CELL, CELL_BOLD, CELL_ITALIC, CELL_BOLD_ITALIC, SERIF, MONO, MATH, MATH_ITALIC, MATH_BOLD] { let chain = build_role_chain(&mut fonts, role, &base_prop, &base_mono, &fallback, hack, system); fonts.families.insert(FontFamily::Name(role.into()), chain); bound.push(role.to_string()); } - // Installed workbook fonts, so a cell's chosen font actually renders in it. - if system { - for family in crate::fonts::installed_among(used) { - bound.extend(crate::fonts::bind_family(&mut fonts, &family)); - } - } // Interface text (formula bar, name box, dialogs) falls back to the cell font before the // script faces: whatever a cell can draw, the formula bar can draw too, even where no script // fallback face was found (#349: macOS UI font without Hebrew, cells in Arial with it). @@ -505,6 +513,11 @@ fn font_definitions_impl(language: crate::i18n::Language, used: &[String], syste if let Some(ui) = fonts.families.get(&FontFamily::Name(UI.into())).cloned() { fonts.families.insert(FontFamily::Proportional, ui); } + // Installed workbook fonts, so a cell's chosen font actually renders in it. They are bound + // after the proportional chain has the script faces, which their chains end with. + for (family, faces) in installed { + bound.extend(crate::fonts::bind_faces(&mut fonts, &family, &faces)); + } // The optional craft-fonts faces go at the very end of every family: a system or bundled face // that has the script keeps its glyph shapes, while the permitted OFL faces cover scripts and // symbols the host image lacks (Japanese, Korean, Thai, Arabic, Georgian and the UI symbols). diff --git a/crates/ui-egui/src/theme/tests.rs b/crates/ui-egui/src/theme/tests.rs index 31dc653ca..359336ee8 100644 --- a/crates/ui-egui/src/theme/tests.rs +++ b/crates/ui-egui/src/theme/tests.rs @@ -76,6 +76,7 @@ fn the_required_craft_faces_keep_their_exact_scripts() { ("NanumGothic", "Regular", ["Hang", "Kore"].as_slice()), ("Noto Sans Symbols", "Regular", ["Zsym"].as_slice()), ("Noto Sans Symbols 2", "Regular", ["Zsym"].as_slice()), + ("Noto Sans Arabic", "Regular", ["Arab"].as_slice()), ] { assert!( faces.iter().any(|(f, s, declared)| *f == family && *s == style && *declared == scripts), @@ -121,11 +122,11 @@ fn language_reorders_only_the_han_faces() { let keys = |han| fallback_faces(han).into_iter().map(|(k, _)| k).collect::>(); assert_eq!( keys(HanOrder::Japanese), - ["sys-cjk-han-ja", "sys-cjk-han-zh", "sys-cjk-hangul", "sys-thai", "sys-lao", "sys-hebrew", "sys-symbols", "sys-symbols-2"] + ["sys-cjk-han-ja", "sys-cjk-han-zh", "sys-cjk-hangul", "sys-thai", "sys-lao", "sys-hebrew", "sys-arabic", "sys-symbols", "sys-symbols-2"] ); assert_eq!( keys(HanOrder::Chinese), - ["sys-cjk-han-zh", "sys-cjk-han-ja", "sys-cjk-hangul", "sys-thai", "sys-lao", "sys-hebrew", "sys-symbols", "sys-symbols-2"] + ["sys-cjk-han-zh", "sys-cjk-han-ja", "sys-cjk-hangul", "sys-thai", "sys-lao", "sys-hebrew", "sys-arabic", "sys-symbols", "sys-symbols-2"] ); assert_eq!(HanOrder::of(Language::Ja), HanOrder::Japanese); for l in [Language::En, Language::Zh, Language::Ko, Language::Ru, Language::PtBr, Language::De, Language::Cs, Language::Fr, Language::Tr] { @@ -184,7 +185,7 @@ fn fallback_covers_its_script() { // face or the optional craft-fonts input provides one; the printed skip is a recorded // gate, and a present face that is not wired in fails. type Case = (&'static str, Vec<(String, u32)>, &'static str); - let cases: [Case; 10] = [ + let cases: [Case; 11] = [ ("sys-cjk-han-ja", cjk_han_japanese(), "カテゴリ"), // Chinese uses a system Han face or the native-only permitted LXGW craft face; the // barred Noto CJK collection never supplies it. @@ -193,6 +194,7 @@ fn fallback_covers_its_script() { ("sys-thai", thai(), "ทดสอบ"), ("sys-lao", lao(), "ພາສາລາວ"), ("sys-hebrew", hebrew(), "עברית"), + ("sys-arabic", arabic(), "العربية١٢٣"), ("craft-noto-sans-arabic", Vec::new(), "مرحبا"), ("craft-noto-sans-georgian", Vec::new(), "ქართული"), ("ui", candidates(UI), "Русский"), @@ -221,6 +223,138 @@ fn fallback_covers_its_script() { } } +#[test] +/// A preferred Arabic file keeps the system Arabic faces from loading it again only once it is +/// loaded: one that failed to load leaves them free to load it. +fn an_unloaded_preferred_arabic_file_does_not_block_the_system_face() { + let dir = std::env::temp_dir().join(format!("gridcraft-theme-preferred-arabic-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).expect("temp dir"); + let path = dir.join("Arabic.ttf"); + std::fs::write(&path, epaint_default_fonts::UBUNTU_LIGHT).expect("font fixture"); + let path = path.to_string_lossy().into_owned(); + let preferred = || Some((path.clone(), 0)); + let faces = || vec![("sys-arabic", vec![(path.clone(), 0)])]; + + let mut fonts = FontDefinitions::default(); + let loaded = preferred_file(&fonts, preferred()); + assert!(loaded.is_empty(), "a preferred face that did not load reserves no file"); + assert_eq!(load_faces(&mut fonts, faces(), loaded), ["sys-arabic"]); + + let mut fonts = FontDefinitions::default(); + fonts.font_data.insert(PREFERRED_ARABIC.into(), Arc::new(FontData::from_static(epaint_default_fonts::UBUNTU_LIGHT))); + let loaded = preferred_file(&fonts, preferred()); + assert_eq!(loaded, std::slice::from_ref(&path)); + assert!(load_faces(&mut fonts, faces(), loaded).is_empty(), "a loaded preferred file is not loaded a second time"); + assert!(!fonts.font_data.contains_key("sys-arabic")); + let _ = std::fs::remove_dir_all(&dir); +} + +fn with_installed_latin_font() -> std::io::Result { + let dir = std::env::temp_dir().join(format!("gridcraft-theme-latin-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir)?; + let path = dir.join("Ubuntu-Light.ttf"); + std::fs::write(&path, epaint_default_fonts::UBUNTU_LIGHT)?; + let face = crate::fonts::Face { family: "Ubuntu".into(), names: vec!["Ubuntu".into()], path, index: 0, weight: 300.0, italic: false }; + let fonts = font_definitions_impl(Language::En, vec![("Ubuntu".into(), vec![face])], true); + std::fs::remove_dir_all(&dir)?; + Ok(fonts) +} + +fn installed_latin_chains(fonts: &FontDefinitions) -> Vec<(FontFamily, Option<&Vec>)> { + [(false, false), (true, false), (false, true), (true, true)] + .into_iter() + .map(|(bold, italic)| { + let family = FontFamily::Name(crate::fonts::role_key("Ubuntu", bold, italic).into()); + let chain = fonts.families.get(&family); + (family, chain) + }) + .collect() +} + +fn chain_covers(fonts: &FontDefinitions, chain: &[String], ch: char) -> bool { + use skrifa::MetadataProvider; + chain + .iter() + .filter_map(|key| fonts.font_data.get(key)) + .any(|data| skrifa::FontRef::from_index((*data.font).as_ref(), data.index).is_ok_and(|font| font.charmap().map(ch).is_some())) +} + +#[test] +/// A workbook font with no Arabic or Hangul still shows them through the system script faces. +fn installed_latin_fonts_keep_system_script_fallbacks() { + let _g = crate::fonts::TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + let fonts = with_installed_latin_font().expect("Latin-only fixture"); + let loaded: Vec<&str> = + fallback_faces(HanOrder::Chinese).into_iter().map(|(key, _)| key).filter(|key| fonts.font_data.contains_key(*key)).collect(); + for (family, chain) in installed_latin_chains(&fonts) { + let chain = chain.expect("bound fixture family"); + for key in &loaded { + assert!(chain.iter().any(|k| k == key), "{family:?} lost the {key} fallback"); + } + if fonts.font_data.contains_key("sys-arabic") { + assert!(chain_covers(&fonts, chain, 'ع'), "{family:?} has no glyph for ع"); + } + } +} + +#[test] +/// The craft Arabic face reaches workbook fonts and the pinned web set, without help from system faces. +fn the_craft_arabic_face_reaches_every_chain() { + assert!(crate::craft_fonts_policy::web_allowed("Noto Sans Arabic", "Regular")); + if !craft_fonts_cover("العربية١٢٣") { + assert!(!craft_fonts_required(), "CRAFT_FONTS_REQUIRED=1 but no craft-fonts face covers Arabic"); + return; + } + let _g = crate::fonts::TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + let mut fonts = with_installed_latin_font().expect("Latin-only fixture"); + fonts.font_data.retain(|key, _| !key.starts_with("sys-")); + for (family, chain) in installed_latin_chains(&fonts) { + let chain = chain.expect("bound fixture family"); + assert!("العربية١٢٣".chars().all(|ch| chain_covers(&fonts, chain, ch)), "{family:?} has no Arabic glyphs"); + } + let pinned = pinned_font_definitions(); + for (family, chain) in &pinned.families { + assert!("العربية".chars().all(|ch| chain_covers(&pinned, chain, ch)), "pinned {family:?} has no Arabic glyphs"); + } +} + +#[cfg(all(unix, not(target_os = "macos")))] +#[test] +/// fontconfig's Arabic face, when it has Arabic letters, comes first among the Arabic fallbacks in every chain (#229). A +/// UI or cell face with Arabic of its own sits ahead of every fallback and still draws Arabic first. +fn the_fontconfig_arabic_choice_comes_first() { + assert_eq!( + parse_fc_match("/home/u/.local/share/fonts/Vazirmatn-Regular.ttf\n0\n"), + Some(("/home/u/.local/share/fonts/Vazirmatn-Regular.ttf".into(), 0)) + ); + assert_eq!( + parse_fc_match("/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc\n2\n"), + Some(("/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc".into(), 2)) + ); + assert_eq!(parse_fc_match("/usr/share/fonts/a.ttf"), Some(("/usr/share/fonts/a.ttf".into(), 0))); + assert_eq!( + parse_fc_match("/usr/share/fonts/vazirmatn/Vazirmatn[wght].ttf\n262145\n"), + Some(("/usr/share/fonts/vazirmatn/Vazirmatn[wght].ttf".into(), 1)), + "a named instance's bits are not part of the face index" + ); + assert_eq!(parse_fc_match(""), None); + assert!(!covers_arabic(epaint_default_fonts::UBUNTU_LIGHT, 0), "a Latin-only face is not an Arabic choice"); + if fontconfig_arabic().is_some() { + let fonts = font_definitions(); + for (family, chain) in fonts.families.iter().filter(|(_, chain)| chain.iter().any(|key| key.starts_with("sys-"))) { + let preferred = chain.iter().position(|key| key == PREFERRED_ARABIC); + let other_arabic = chain.iter().position(|key| key == "sys-arabic" || key.starts_with("craft-")); + assert!(preferred.is_some(), "{family:?} lacks the preferred Arabic face"); + assert!(other_arabic.is_none_or(|other| preferred < Some(other)), "{family:?} puts another Arabic face first"); + } + if let (Some(preferred), Some(system)) = (fonts.font_data.get(PREFERRED_ARABIC), fonts.font_data.get("sys-arabic")) { + assert!((*preferred.font).as_ref() != (*system.font).as_ref(), "the system Arabic fallback is a different file from the preferred face"); + } + } +} + #[test] fn chromeos_font_layout_is_probed() { // #1762: Crostini mounts the host's fonts under /usr/share/fonts/chromeos, one folder per @@ -229,6 +363,7 @@ fn chromeos_font_layout_is_probed() { let cros = "/usr/share/fonts/chromeos"; assert!(probes(thai(), &format!("{cros}/noto/NotoSansThai-Regular.ttf"))); assert!(probes(hebrew(), &format!("{cros}/noto/NotoSansHebrew-Regular.ttf"))); + assert!(probes(arabic(), &format!("{cros}/noto/NotoSansArabic-Regular.ttf"))); assert!(probes(cjk_han_japanese(), &format!("{cros}/notocjk/NotoSansCJK-Regular.ttc"))); assert!(probes(cjk_han_simplified(), &format!("{cros}/notocjk/NotoSansCJK-Regular.ttc"))); assert!(probes(cjk_hangul(), &format!("{cros}/notocjk/NotoSansCJK-Regular.ttc"))); diff --git a/docs/gaps.md b/docs/gaps.md index 8879e967d..3fda49d2b 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -55,7 +55,9 @@ No alpha blockers: every core workflow passes the [alpha gate](roadmap.md#alpha- ### 5. Text in non-Latin scripts doesn't render or export (L) - **Missing:** CJK font fallback, complex shaping (Arabic, Devanagari, Thai), bidi, non-Latin PDF. -- **Evidence:** system-font fallback for CJK, Thai and Hebrew landed in #38 (closed #32); #142 +- **Evidence:** system-font fallback for CJK, Thai and Hebrew landed in #38 (closed #32), and for + Arabic (system, fontconfig and craft-fonts faces); a UI font with Arabic of its own (DejaVu Sans + in the Flatpak) still draws interface Arabic ahead of fontconfig's choice (#229); #142 (Chinese text) awaits confirmation; #54 (symbols as boxes on Fedora); no shaping for Arabic or Devanagari; PDF writer uses 14 standard fonts only. - **Impact:** blocks most of the world's users even with an English UI. @@ -193,6 +195,7 @@ No alpha blockers: every core workflow passes the [alpha gate](roadmap.md#alpha- | 2026-11-01 | minor | §4: chart data tables implemented for column, bar, line, area and compatible combo charts, with legend keys and border settings, live source values, XLSX round-trip and print/PDF; other chart gaps remain. | | 2026-10-31 | minor | §13: Solver Options (Max Evaluations, Integer Optimality and the Evolutionary options), saved with the model | | 2026-10-31 | minor | §13: Solver Load/Save Model (`data.solverSaveModel`, `data.solverLoadModel`) and Reset All (`data.solverReset`), with buttons in the Solver Parameters dialog | +| 2026-10-26 | minor | §5: Arabic text renders in cells and the interface: a system Arabic face (Tahoma, Arial or Segoe UI on Windows, Geeza Pro on macOS, Noto Sans or Naskh Arabic on Linux) joins the script fallbacks, fontconfig's Arabic choice, when it has Arabic letters, leads the Arabic fallbacks and draws Arabic wherever the UI or cell font has none, such as the default cell fonts (part of #229; a UI font with Arabic of its own, such as DejaVu Sans in the Flatpak, still draws interface Arabic, left for a follow-up), and craft-fonts' Noto Sans Arabic is a required face allowed on the web. Workbook fonts without Arabic keep every script fallback. Shaping and bidirectional layout remain separate work | | 2026-10-24 | minor | §8: PivotTable Enable show details: `pivot.options` `enableShowDetails` turns Show Details off for a PivotTable, read and written in XLSX as `enableDrill` | | 2026-10-23 | minor | §8: PivotTable Show Details: double-clicking a value cell lists its source records on a new sheet (#2364) | | 2026-10-22 | minor | §20: a native window per workbook window, New Window on the same workbook and Arrange All on the desktop; left: Arrange All across monitors, the web's one window, pen pressure (estimate 10–14 h → 5–8 h) | diff --git a/docs/localization-parity.md b/docs/localization-parity.md index d504c19ea..6bc96187c 100644 --- a/docs/localization-parity.md +++ b/docs/localization-parity.md @@ -1,6 +1,6 @@ # Localization parity -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-27 · **Change:** minor (number formats honour the `B1`/`B2` calendar prefixes, checked against Excel; earlier: missing translations are listed by `cargo xtask i18n-report` and never fail a check; Arabic text sorts by its letters, not its vowel marks); previously minor (AutoFit estimates Arabic text width from measured glyph widths); previously minor (missing translations are listed by `cargo xtask i18n-report` and never fail a check; Arabic text sorts by its letters, not its vowel marks) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-27 · **Change:** minor (Arabic font fallback; missing translations are listed by `cargo xtask i18n-report` and never fail a check); previously minor (number formats honour the `B1`/`B2` calendar prefixes, checked against Excel; earlier: missing translations are listed by `cargo xtask i18n-report` and never fail a check; Arabic text sorts by its letters, not its vowel marks); previously minor (AutoFit estimates Arabic text width from measured glyph widths); previously minor (missing translations are listed by `cargo xtask i18n-report` and never fail a check; Arabic text sorts by its letters, not its vowel marks) · **Target:** Microsoft Excel (Microsoft 365), Excel for Mac 16.113.4 Per-language status against Excel, which ships its UI in dozens of languages (Windows language packs cover 100+), with localized function names, argument separators, number and date formats, @@ -60,7 +60,9 @@ right-to-left sheets and full complex-script support. Part of `/run/host/fonts`, `/run/host/local-fonts` and `/run/host/user-fonts` layouts, so installed Noto CJK and WenQuanYi faces can render Chinese without a broad filesystem grant. This does not change the installed-font catalogue or add bundled fonts. Lao falls back to Lao Sangam MN / Lao MN (macOS), - Leelawadee UI / DokChampa (Windows) or Noto Sans Lao / Phetsarath OT (Linux). There is no complex-text shaping: Arabic and + Leelawadee UI / DokChampa (Windows) or Noto Sans Lao / Phetsarath OT (Linux). Arabic falls back to a system Arabic face, fontconfig's + Arabic choice or craft-fonts' Noto Sans Arabic; fontconfig's choice leads these fallbacks, but a UI font with Arabic of its + own (DejaVu Sans in the Flatpak) still draws interface Arabic (#229). There is no complex-text shaping: Arabic and Devanagari don't join or reorder, and there is no bidirectional layout. **Sheet Right-to-Left** (`view.rightToLeft`, Page Layout › Sheet Options, XLSX `rightToLeft`, `.xls` WINDOW2 fRightToLeft) mirrors the grid (#146): column A and the row headers on the right, the vertical scrollbar on the left; hit-testing, @@ -124,6 +126,7 @@ language (none so far) and of localized function names. | Date | Change | Summary | |---|---|---| +| 2026-10-26 | minor | Arabic text renders in cells and the interface: a system Arabic face (Tahoma, Arial or Segoe UI on Windows, Geeza Pro on macOS, Noto Sans or Naskh Arabic on Linux) joins the script fallbacks, fontconfig's Arabic choice, when it has Arabic letters, leads the Arabic fallbacks and draws Arabic wherever the UI or cell font has none, such as the default cell fonts (part of #229; a UI font with Arabic of its own, such as DejaVu Sans in the Flatpak, still draws interface Arabic, left for a follow-up), and craft-fonts' Noto Sans Arabic is a required face allowed on the web. Workbook fonts without Arabic keep every script fallback. Shaping and bidirectional layout remain separate work | | 2026-10-27 | minor | Number formats honour the `B1`/`B2` calendar prefixes: never shown; `B2` renders year, month, day and Arabic month and weekday names in the tabular Hijri calendar (1 Jan 2001 = 6 Shawwal 1421; leap years 2, 5, 7, 10, 13, 15, 18, 21, 24, 26 and 29 of the 30-year cycle, which is what Excel shows). Checked against Excel 16.0 (build 20527, en-US) on every serial and about 1,700 format/value pairs: a locale or currency tag before `B2` keeps the date Gregorian, a misplaced `B` and digit or a prefix before a number format is rejected, serial 60 has no Hijri date, serial 0 shows as serial 1, two-word month names use a no-break space. Not matched: Excel's weekday/month-name mix-ups in one `B2` section and its weekday-only quirks | | 2026-10-27 | minor | AutoFit column width, wrapped-row height and PivotTable column fit estimate Arabic letters at ~0.48 em (measured 0.36–0.53 em shaped, Noto Sans/Naskh Arabic and DejaVu Sans) instead of 1 em, Arabic-Indic digits as wide as ASCII digits, and give vowel marks and direction marks no width; vowelled Arabic no longer fits ~3.5× too wide | | 2026-10-23 | minor | Missing translations are reported, never a failure: `cargo xtask i18n-report` prints, per language, the strings other catalogs translate and it lacks (plus Spanish function help rows keyed by English the function catalog no longer has) and always passes; `cargo xtask ci` runs it first. Coverage and per-language parity tests and tests of one language's exact wording are gone; UI tests take the expected text from the catalog. Duplicate-key, placeholder, context-prefix and line-shape checks stay | From e9addf52fe68847e6c1310fcfa5d42b7aa8b9650 Mon Sep 17 00:00:00 2001 From: Mouhand Kaddo Date: Mon, 9 Nov 2026 18:55:43 +0400 Subject: [PATCH 31/76] Arabic bidi layout and cell reading direction in the grid and editors (#947) * M12.AR4: preserve Arabic logical text in bidi display and editing * M12.AR4: drop the stale "no bidi" note from the RTL sheet status * M12.AR4: open the Arabic editors and place the caret through key and pointer events in tests * M12.AR4: skip the conditional-format lookup in the formula bar unless a rule sets reading order * M12.AR4: test that the caret after typed Arabic is drawn at the text's left end (#1070) * M12.AR4: pass the theme body font in the reading-order style test * M12.AR4: adapt the Arabic UI tests to egui 0.37 (row direction, editor ids, Ctrl+End to the logical end) * M12.AR4: read the active sheet through Workbook::active_sheet() in the formula bar main made `Workbook::active_sheet` an `ActiveSheet` (shared between windows on one workbook); the formula bar's reading-order lookup takes the index from the accessor. Co-Authored-By: Claude Opus 5.5 * M12.AR4: keep explicit Left on wrapped right-to-left cells and trailing spaces in the editor Wrapped cell text lays out every row from the left when the painted alignment is Left, instead of aligning right-to-left rows to their start. The cell and formula bar editor keep trailing whitespace in their layout so a right-to-left entry ending in a space keeps the caret inside the text. * M12.AR4: keep Right and General on right-to-left sheets aligned right in wrapped cells * M12.AR4: keep right-aligned Arabic with a marked first letter inside its cell --------- Co-authored-by: Claude Opus 5.5 Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com> Co-authored-by: FredApps --- Cargo.lock | 1 + ROADMAP.md | 7 +- crates/engine/src/cf.rs | 25 +++ crates/engine/src/cmd/format.rs | 2 +- crates/engine/src/tests.rs | 38 ++++ crates/model/src/features.rs | 24 +++ crates/model/src/lib.rs | 4 +- crates/model/src/style.rs | 30 +++ crates/ui-egui/Cargo.toml | 1 + crates/ui-egui/src/dialogs/format_cells.rs | 13 +- crates/ui-egui/src/formula_bar.rs | 33 ++- crates/ui-egui/src/grid/cell_paint.rs | 3 + crates/ui-egui/src/grid/quadrant.rs | 29 ++- crates/ui-egui/src/lib.rs | 1 + crates/ui-egui/src/text_direction.rs | 32 +++ crates/ui-egui/tests/arabic_bidi.rs | 228 +++++++++++++++++++++ crates/ui-egui/tests/arabic_editing.rs | 131 ++++++++++++ crates/xlsx/src/styles.rs | 62 +++++- crates/xlsx/src/tests/roundtrip.rs | 10 +- crates/xlsx/src/xls/styles.rs | 1 + docs/file-format-parity.md | 3 + docs/gaps.md | 6 + docs/localization-parity.md | 13 +- 23 files changed, 673 insertions(+), 24 deletions(-) create mode 100644 crates/ui-egui/src/text_direction.rs create mode 100644 crates/ui-egui/tests/arabic_bidi.rs create mode 100644 crates/ui-egui/tests/arabic_editing.rs diff --git a/Cargo.lock b/Cargo.lock index cb783e546..543cddfe1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2197,6 +2197,7 @@ dependencies = [ "skrifa", "sys-locale", "toml", + "unicode-bidi", "web-sys", "web-time", "zbus", diff --git a/ROADMAP.md b/ROADMAP.md index f12cd112a..5906fda99 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -43,8 +43,7 @@ long `.xls` track, so about half parallelizes (≈30–50 wall-clock hours with Why alpha: all six core workflows in the [alpha gate](docs/roadmap.md#alpha-gate) pass end to end on macOS with save and reopen, and the core workflow works end to end (open an XLSX, enter formulas and data, format, sort and filter, chart, pivot, print to PDF, save XLSX), but users still hit blocking gaps on real -files: 100 MB workbooks don't open (#175), there's no `.xls` (#121), Arabic and Devanagari text -isn't shaped (Chinese text still reported in #142), charts lose their type and options on round trip, and +files: 100 MB workbooks don't open (#175), there's no `.xls` (#121), Devanagari text needs shaping verification (Chinese text still reported in #142), charts lose their type and options on round trip, and nobody has yet confirmed that Excel opens our output without a repair prompt across a real corpus. Beta needs those closed (see [beta gates](docs/roadmap.md#beta-gates)). @@ -105,7 +104,7 @@ Localized German formulas and numbers are in PR #163. Detail: [localization-pari | Simplified Chinese | zh-Hans | menus only | ~12% | | Spanish | es | none | 0% | | Hindi | hi | none | 0% | -| Arabic | ar | none (no RTL layout, no shaping) | 0% | +| Arabic | ar | catalog pending; shared shaping/bidi works | 0% | | French | fr | none | 0% | | Portuguese | pt-BR | menus, File view, dialogs | ~43% | | Indonesian | id | none | 0% | @@ -149,6 +148,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | 2026-10-31 | CSV and text open detects each column's date order: a value that reads only day first (`31/05/2026`) makes the column day first, a value that reads only month first (`12/31/2026`) makes it month first, an all-ambiguous column keeps month first with a warning, and a column with both kinds stays text with a warning (#2765) | | 2026-10-31 | Recently Used lists the functions most recently entered in a formula from any source (ribbon, control channel, MCP, scripts), kept with the preferences and returned by `formulas.recentlyUsed`; the Insert Function dialog takes Excel's title and a working category list (Most Recently Used, All, each category) | | 2026-10-31 | Formulas › Function Library as a big Insert Function button, the AutoSum split button and three columns of labelled small buttons, each category book in its own colour; category menus end with Insert Function… on that category; More Functions cascades into the remaining categories; Home › Editing shares the AutoSum menu; the MR key tip opens Recently Used | +| 2026-10-26 | Arabic bidi display/editing preserve logical source text; per-cell reading direction is editable and survives undo, XLSX and conditional-format priority | | 2026-10-26 | Arabic text shows in cells and the interface through a system, fontconfig or craft-fonts Arabic face; workbook fonts keep their script fallbacks | | 2026-10-25 | AutoSum over a selection does what Excel does: a selected row gets one total to its right (in its last cell if that is empty), not a one-cell total under each cell; a block whose last column is empty gets each row's total there, one whose last row is empty each column's, and with both the corner gets the total of the block; otherwise totals go under each column as before | | 2026-10-24 | Macro interface ([macros.md](docs/macros.md#in-the-window)): an Automate › Macros group (Record Macro, which turns into Stop Recording, Use Relative References, Run Macro with recent macros); "Recording · N steps" with Stop in the status bar; a Record Macro dialog (checked name, free Ctrl+Shift shortcut, description); the Macros dialog lists macros beside Automate scripts and a VBA project, with Run, Step Into, Edit, Create, Delete and Options; Ctrl+Shift+letter macro shortcuts; questions that offer Run, Overwrite, Shift Down or Shift Right; a step editor with a breakpoint gutter, drag to reorder, a command picker, reference boxes that switch between relative tokens and absolute references, Inputs and Output tabs, a debug bar (Check, Run, Step, Continue, Run to Here, Stop with keep or undo) and grid overlays for inputs, writes, conflicts and the current step | @@ -277,6 +277,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in | 2026-10-31 | minor | File-page text fields keep the view open when Esc cancels an edit (#3035) | | 2026-10-31 | minor | Progress log: Recently Used and Insert Function categories | | 2026-10-31 | minor | Progress log: Function Library | +| 2026-10-26 | minor | Progress log: Arabic bidi display and editing | | 2026-10-26 | minor | Progress log: Arabic font fallback | | 2026-10-25 | minor | Progress log: AutoSum over a selected row, and a selection's empty last row and column | | 2026-10-24 | minor | Macro interface on the Automate tab; progress log row | diff --git a/crates/engine/src/cf.rs b/crates/engine/src/cf.rs index 38f5bb1fe..0af7a9df5 100644 --- a/crates/engine/src/cf.rs +++ b/crates/engine/src/cf.rs @@ -368,6 +368,9 @@ impl CfCache { /// Adds the parts `lower` sets that `style` leaves unset (differential styles: default parts /// are unset). fn layer_under(style: &mut Style, lower: &Style) { + if style.align.reading_order.is_none() { + style.align.reading_order = lower.align.reading_order; + } if style.fill.pattern == PatternType::None { style.fill = lower.fill; } @@ -417,6 +420,28 @@ mod tests { use super::*; + #[test] + fn reading_order_respects_conditional_rule_priority_and_explicit_context() { + for (higher, expected) in [ + (json!({"font": {"bold": true}}), "RightToLeft"), + (json!({"align": {"reading_order": "Context"}}), "Context"), + (json!({"align": {"reading_order": "LeftToRight"}}), "LeftToRight"), + ] { + let mut s = crate::Session::new(); + s.new_workbook(); + s.execute("cell.set", json!({"cell": "A1", "input": "مرحبا 123 ABC"})).unwrap(); + for style in [json!({"align": {"reading_order": "RightToLeft"}}), higher] { + s.execute("home.conditionalFormat", json!({"range": "A1", "rule": {"type": "noBlanks", "style": style}})).unwrap(); + } + let wb = &s.doc().unwrap().wb; + let (reopened, _) = gridcraft_xlsx::read_xlsx(&gridcraft_xlsx::write_xlsx(wb).unwrap()).unwrap(); + for wb in [wb, &reopened] { + let style = CfCache::new().look(wb, 0, CellRef::new(0, 0)).unwrap().style.unwrap(); + assert_eq!(serde_json::to_value(style.align).unwrap()["reading_order"], expected); + } + } + } + #[test] fn every_matching_rule_applies() { let mut s = crate::Session::new(); diff --git a/crates/engine/src/cmd/format.rs b/crates/engine/src/cmd/format.rs index d5eac5b5d..aa2638801 100644 --- a/crates/engine/src/cmd/format.rs +++ b/crates/engine/src/cmd/format.rs @@ -85,7 +85,7 @@ pub fn specs() -> Vec { "Format Cells…", ["Home", "Cells", "Format"], Some("Cmd+1"), - "{range?, style: {font?, fill?, borders?, align?, numFmt?, protection?}} (partial Style JSON merged in)", + "{range?, style: {font?, fill?, borders?, align?: {reading_order?: Context|LeftToRight|RightToLeft|null, ...}, numFmt?, protection?}} (partial Style JSON merged in; null clears reading_order)", has_doc, format_cells ), diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index bf36bd15b..fcaf19c36 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -18,6 +18,44 @@ fn v(s: &Session, a: &str) -> Value { s.doc().unwrap().wb.active().unwrap().value(CellRef::parse(a).unwrap()) } +#[test] +fn format_cells_reading_order_survives_undo_and_xlsx() { + let mut s = s(); + s.execute("cell.set", json!({"cell": "A1", "input": "مرحبا 123 ABC"})).unwrap(); + s.execute("home.bold", json!({"range": "A1"})).unwrap(); + s.execute("home.formatCells", json!({"range": "A1", "style": {"align": {"reading_order": "RightToLeft"}}})).unwrap(); + let alignment = |s: &Session| { + let d = s.doc().unwrap(); + let style = d.wb.styles.get(d.wb.active().unwrap().style_id(CellRef::new(0, 0))); + assert!(style.font.bold); + serde_json::to_value(style.align).unwrap() + }; + assert_eq!(alignment(&s)["reading_order"], "RightToLeft"); + s.execute("edit.undo", json!({})).unwrap(); + assert!(alignment(&s)["reading_order"].is_null()); + s.execute("edit.redo", json!({})).unwrap(); + assert_eq!(alignment(&s)["reading_order"], "RightToLeft"); + for order in [json!("invalid"), json!(2)] { + assert!(s.execute("home.formatCells", json!({"range": "A1", "style": {"align": {"reading_order": order}}})).is_err()); + assert_eq!(alignment(&s)["reading_order"], "RightToLeft"); + } + let wb = &s.doc().unwrap().wb; + let (reopened, _) = gridcraft_xlsx::read_xlsx(&gridcraft_xlsx::write_xlsx(wb).unwrap()).unwrap(); + let cell = CellRef::new(0, 0); + assert_eq!(reopened.styles.get(reopened.active().unwrap().style_id(cell)), wb.styles.get(wb.active().unwrap().style_id(cell))); + assert_eq!(reopened.active().unwrap().value(cell), Value::text("مرحبا 123 ABC")); + for order in ["LeftToRight", "Context"] { + s.execute("home.formatCells", json!({"range": "A1", "style": {"align": {"reading_order": order}}})).unwrap(); + assert_eq!(alignment(&s)["reading_order"], order); + } + s.execute("home.formatCells", json!({"range": "A1", "style": {"align": {"reading_order": null}}})).unwrap(); + assert!(alignment(&s)["reading_order"].is_null()); + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!(alignment(&s)["reading_order"], "Context"); + s.execute("edit.redo", json!({})).unwrap(); + assert!(alignment(&s)["reading_order"].is_null()); +} + #[test] fn entering_empty_cells_preserves_whole_sheet_formatting() { let mut s = s(); diff --git a/crates/model/src/features.rs b/crates/model/src/features.rs index 06e68839f..8ab74606a 100644 --- a/crates/model/src/features.rs +++ b/crates/model/src/features.rs @@ -230,6 +230,30 @@ pub enum CfRule { }, } +impl CfRule { + /// The differential style a matching rule applies; colour scales, data bars and icon sets have none. + pub fn style(&self) -> Option<&Style> { + match self { + CfRule::CellIs { style, .. } + | CfRule::Expression { style, .. } + | CfRule::ContainsText { style, .. } + | CfRule::NotContainsText { style, .. } + | CfRule::BeginsWith { style, .. } + | CfRule::EndsWith { style, .. } + | CfRule::Blanks { style } + | CfRule::NoBlanks { style } + | CfRule::Errors { style } + | CfRule::NoErrors { style } + | CfRule::Duplicate { style } + | CfRule::Unique { style } + | CfRule::Top10 { style, .. } + | CfRule::AboveAverage { style, .. } + | CfRule::TimePeriod { style, .. } => Some(style), + CfRule::ColorScale { .. } | CfRule::DataBar { .. } | CfRule::IconSet { .. } => None, + } + } +} + /// Number of icons in an icon set (`3Arrows` → 3, `5Quarters` → 5), always 3 to 5. pub fn icon_count(set: &str) -> usize { set.chars().next().and_then(|c| c.to_digit(10)).unwrap_or(3).clamp(3, 5) as usize diff --git a/crates/model/src/lib.rs b/crates/model/src/lib.rs index 35cfa315f..b1f875b3c 100644 --- a/crates/model/src/lib.rs +++ b/crates/model/src/lib.rs @@ -26,8 +26,8 @@ pub use python::{PyCode, PyOutput}; pub use sheet::{DEFAULT_COL_WIDTH, DEFAULT_ROW_HEIGHT, LineIndex, Sheet, SheetUid, SheetViewMode, Visibility, points_to_chars}; pub use store::CellStore; pub use style::{ - Alignment, BorderLine, BorderStyle, Borders, Color, Fill, Font, HAlign, NumFmt, PatternType, Style, StyleId, StyleTable, Theme, Underline, - VAlign, VertAlign, + Alignment, BorderLine, BorderStyle, Borders, Color, Fill, Font, HAlign, NumFmt, PatternType, ReadingOrder, Style, StyleId, StyleTable, Theme, + Underline, VAlign, VertAlign, }; pub use workbook::{ CalcMode, CalcSettings, DefinedName, DocProps, EQUATION_BUDGET_MESSAGE, MAX_EQUATION_LAYOUT_BYTES, PhoneticProps, PhoneticRun, PhoneticString, diff --git a/crates/model/src/style.rs b/crates/model/src/style.rs index d2f10780e..9ad61abec 100644 --- a/crates/model/src/style.rs +++ b/crates/model/src/style.rs @@ -350,6 +350,14 @@ pub enum VAlign { Distributed, } +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub enum ReadingOrder { + #[default] + Context, + LeftToRight, + RightToLeft, +} + #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)] pub struct Alignment { pub h: HAlign, @@ -359,6 +367,9 @@ pub struct Alignment { pub indent: u8, /// Degrees -90..=90, or 255 for vertical stacked text. pub rotation: i16, + /// Unspecified inherits in differential styles and uses context for ordinary cells. + #[serde(default)] + pub reading_order: Option, } #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] @@ -488,6 +499,25 @@ static DEFAULT_STYLE: std::sync::LazyLock