1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-23 13:21:38 +03:00

wezterm-font: move FontDatabase into FontConfiguration

Previously, we'd enumerate the font dirs on every font resolve for
every bit of styled text.

This moves the new FontDatabase instances to be single instanced
in the FontConfiguration.  The font-dirs will be scanned once
on a config reload, but the built-in in-memory fonts will only
every be enumerated once per FontConfiguration instance.
This commit is contained in:
Wez Furlong 2020-11-25 19:21:51 -08:00
parent b0bee3b036
commit d934a0ae88
2 changed files with 20 additions and 13 deletions

View File

@ -141,6 +141,8 @@ struct FontConfigInner {
font_scale: RefCell<f64>,
config_generation: RefCell<usize>,
locator: Box<dyn FontLocator>,
font_dirs: RefCell<FontDatabase>,
built_in: RefCell<FontDatabase>,
}
/// Matches and loads fonts for a given input style
@ -150,16 +152,19 @@ pub struct FontConfiguration {
impl FontConfigInner {
/// Create a new empty configuration
pub fn new() -> Self {
pub fn new() -> anyhow::Result<Self> {
let locator = new_locator(FontLocatorSelection::get_default());
Self {
let config = configuration();
Ok(Self {
fonts: RefCell::new(HashMap::new()),
locator,
metrics: RefCell::new(None),
font_scale: RefCell::new(1.0),
dpi_scale: RefCell::new(1.0),
config_generation: RefCell::new(configuration().generation()),
}
config_generation: RefCell::new(config.generation()),
font_dirs: RefCell::new(FontDatabase::with_font_dirs(&config)?),
built_in: RefCell::new(FontDatabase::with_built_in()?),
})
}
/// Given a text style, load (with caching) the font that best
@ -173,6 +178,7 @@ impl FontConfigInner {
// Config was reloaded, invalidate our caches
fonts.clear();
self.metrics.borrow_mut().take();
*self.font_dirs.borrow_mut() = FontDatabase::with_font_dirs(&config)?;
*self.config_generation.borrow_mut() = current_generation;
}
@ -193,14 +199,15 @@ impl FontConfigInner {
.collect::<Vec<_>>();
let mut loaded = HashSet::new();
let font_dirs = FontDatabase::with_font_dirs(&config)?;
let built_in = FontDatabase::with_built_in()?;
let mut handles = vec![];
for attrs in &[&preferred_attributes, &fallback_attributes] {
font_dirs.resolve_multiple(attrs, &mut handles, &mut loaded);
self.font_dirs
.borrow()
.resolve_multiple(attrs, &mut handles, &mut loaded);
handles.append(&mut self.locator.load_fonts(attrs, &mut loaded)?);
built_in.resolve_multiple(attrs, &mut handles, &mut loaded);
self.built_in
.borrow()
.resolve_multiple(attrs, &mut handles, &mut loaded);
}
for attr in &attributes {
@ -345,9 +352,9 @@ impl FontConfigInner {
impl FontConfiguration {
/// Create a new empty configuration
pub fn new() -> Self {
let inner = Rc::new(FontConfigInner::new());
Self { inner }
pub fn new() -> anyhow::Result<Self> {
let inner = Rc::new(FontConfigInner::new()?);
Ok(Self { inner })
}
/// Given a text style, load (with caching) the font that best

View File

@ -845,7 +845,7 @@ impl TermWindow {
let window_background = load_background_image(&config);
let fontconfig = Rc::new(FontConfiguration::new());
let fontconfig = Rc::new(FontConfiguration::new()?);
let mux = Mux::get().expect("to be main thread with mux running");
let size = match mux.get_active_tab_for_window(mux_window_id) {
Some(tab) => tab.get_size(),