diff --git a/Cargo.lock b/Cargo.lock index ea316ea9b..a7b538eea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4297,6 +4297,7 @@ dependencies = [ "log", "metrics", "mux", + "ordered-float", "termwiz", "thiserror", "tinyvec 1.1.0", diff --git a/wezterm-font/Cargo.toml b/wezterm-font/Cargo.toml index 66ab4036f..d9e4113d9 100644 --- a/wezterm-font/Cargo.toml +++ b/wezterm-font/Cargo.toml @@ -17,6 +17,7 @@ lazy_static = "1.4" log = "0.4" metrics = { version="0.12", features=["std"]} mux = { path = "../mux" } +ordered-float = "1.0" termwiz = { path = "../termwiz" } thiserror = "1.0" tinyvec = "1.1" # Note: constrained by the allsorts crate diff --git a/wezterm-font/src/shaper/harfbuzz.rs b/wezterm-font/src/shaper/harfbuzz.rs index 8f567c0a6..d7d465d11 100644 --- a/wezterm-font/src/shaper/harfbuzz.rs +++ b/wezterm-font/src/shaper/harfbuzz.rs @@ -6,7 +6,9 @@ use crate::units::*; use anyhow::anyhow; use config::configuration; use log::error; +use ordered_float::NotNan; use std::cell::{RefCell, RefMut}; +use std::collections::HashMap; use termwiz::cell::unicode_column_width; use thiserror::Error; use unicode_segmentation::UnicodeSegmentation; @@ -54,10 +56,18 @@ struct FontPair { cell_height: f64, } +#[derive(Debug, Hash, PartialEq, Eq, Clone)] +struct MetricsKey { + font_idx: usize, + size: NotNan, + dpi: u32, +} + pub struct HarfbuzzShaper { handles: Vec, fonts: Vec>>, lib: ftwrap::Library, + metrics: RefCell>, } #[derive(Error, Debug)] @@ -105,6 +115,7 @@ impl HarfbuzzShaper { fonts, handles, lib, + metrics: RefCell::new(HashMap::new()), }) } @@ -359,6 +370,16 @@ impl FontShaper for HarfbuzzShaper { let mut pair = self .load_fallback(font_idx)? .ok_or_else(|| anyhow!("unable to load font idx {}!?", font_idx))?; + + let key = MetricsKey { + font_idx, + size: NotNan::new(size).unwrap(), + dpi, + }; + if let Some(metrics) = self.metrics.borrow().get(&key) { + return Ok(metrics.clone()); + } + let (cell_width, cell_height) = pair.face.set_font_size(size, dpi)?; let y_scale = unsafe { (*(*pair.face.face).size).metrics.y_scale as f64 / 65536.0 }; let metrics = FontMetrics { @@ -377,6 +398,8 @@ impl FontShaper for HarfbuzzShaper { ), }; + self.metrics.borrow_mut().insert(key, metrics.clone()); + log::trace!("metrics: {:?}", metrics); Ok(metrics)