diff --git a/crates/engine/src/cmd/mod.rs b/crates/engine/src/cmd/mod.rs index 5365d1403..70f20fce6 100644 --- a/crates/engine/src/cmd/mod.rs +++ b/crates/engine/src/cmd/mod.rs @@ -488,7 +488,9 @@ pub fn auto_row_height(wb: &mut Workbook, si: usize, row: u32) { } let st = wb.styles.get(cell.style); let size = st.font.size; - let line = (size * 1.8).round().max(default * size / gridcraft_model::DEFAULT_FONT_SIZE); + // Scaled from an 11pt row's height, not the sheet's default: a sheet whose default + // already fits a bigger font (21pt rows for 16pt text) would otherwise scale it twice. + let line = (size * 1.8).round().max(gridcraft_model::DEFAULT_ROW_HEIGHT * size / gridcraft_model::DEFAULT_FONT_SIZE); let mut lines = 1.0f32; if !cell.value.is_empty() || cell.formula.is_some() { let text = crate::display::cell_text(wb, sh, CellRef::new(row, c)); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 2da46caed..c49e67540 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -1193,6 +1193,26 @@ fn autofit_skips_merged_cells() { assert!(xml.contains(""), "the merge itself is saved"); } +#[test] +fn open_fits_big_font_rows_from_the_font_not_the_sheet_default() { + // Excel saves a sheet whose rows hold 16pt text with a 21pt (28px) default row height and no + // `ht` on rows of that height. Those rows already fit their font: they open at about 28px, + // not scaled up again by 16/11 to 41px. + let mut wb = gridcraft_model::Workbook::new(); + let big = wb.styles.derive(gridcraft_model::StyleId::DEFAULT, |s| s.font.size = 16.0); + let sh = wb.sheet_mut(0).unwrap(); + sh.default_row_height = 28.0; + sh.set_value(CellRef::new(4, 0), Value::text("SANTANDER")); + sh.set_style(CellRef::new(4, 0), big); + let bytes = gridcraft_xlsx::write_xlsx(&wb).unwrap(); + let mut s = Session::new(); + s.execute("file.open", json!({"name": "tall-default.xlsx", "base64": crate::io::base64_encode(&bytes)})).unwrap(); + let sh = s.doc().unwrap().wb.active().unwrap(); + assert_eq!(sh.default_row_height, 28.0); + let h = sh.row_height(4); + assert!((28.0..30.0).contains(&h), "a 16pt row on a 28px-default sheet opens at about 28px, got {h}"); +} + #[test] fn tables_and_structured_refs() { let mut s = s();