wezterm-font: cache metrics lookup in harfbuzz shaper

98f289f511 causes more metrics retrieval
than in earlier versions; each unchached glyph render would trigger
a metrics recompute for the relevant font.

Add a simple cache for this.

refs: #353
This commit is contained in:
Wez Furlong committed 2020-11-24 18:19:49 -08:00
1 parent 827d94a9a8
commit 5fbb6a4400
3 files changed
+25

No files matched your search

Generated
+1
View File
@@ -4297,6 +4297,7 @@ dependencies = [
"log",
"metrics",
"mux",
"ordered-float",
"termwiz",
"thiserror",
"tinyvec 1.1.0",
+1
View File
@@ -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
+23
View File
@@ -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<f64>,
dpi: u32,
}
pub struct HarfbuzzShaper {
handles: Vec<FontDataHandle>,
fonts: Vec<RefCell<Option<FontPair>>>,
lib: ftwrap::Library,
metrics: RefCell<HashMap<MetricsKey, FontMetrics>>,
}
#[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)