AutoFit Column Width measures text in the font the grid draws it with (fixes #1316)

This commit is contained in:
Marco Arnulfo
2026-10-14 18:01:11 +02:00
parent d9d09f0baa
commit f92b4d30cb
5 changed files with 56 additions and 4 deletions
+7 -4
View File
@@ -1008,7 +1008,7 @@ pub fn chars_to_points(c: f64) -> f64 {
pub use gridcraft_model::points_to_chars;
/// Approximate text width in points for autofit (no font metrics in the engine: average glyph
/// widths for the default font; the UI refines with real measurement when it autofits).
/// widths for the default font; a front end with fonts measures instead, see `Session::text_width`).
pub fn approx_text_width(text: &str, size: f32, bold: bool) -> f32 {
let mut w = 0.0;
for c in text.chars() {
@@ -1027,6 +1027,7 @@ pub fn approx_text_width(text: &str, size: f32, bold: bool) -> f32 {
fn autofit_cols(s: &mut Session, p: &Json) -> Result<Json> {
let lines = line_targets(s, p, false)?;
let measure = s.text_width.clone();
edit(s, |cx| {
let si = cx.sheet_index();
let Some(sh) = cx.wb.sheet(si) else { return Ok(Json::Null) };
@@ -1044,9 +1045,11 @@ fn autofit_cols(s: &mut Session, p: &Json) -> Result<Json> {
let st = cx.wb.styles.get(sh.style_id(cell));
let text = crate::display::cell_text(&cx.wb, sh, cell);
for line in text.lines() {
max = max.max(
approx_text_width(line, st.font.size, st.font.bold) + 6.0 + st.align.indent as f32 * 9.0 + sh.filter_button_space(cell),
);
let w = match &measure {
Some(m) => m(line, &st.font),
None => approx_text_width(line, st.font.size, st.font.bold),
};
max = max.max(w + 6.0 + st.align.indent as f32 * 9.0 + sh.filter_button_space(cell));
}
}
if max > 0.0 {