From 6b92cf7539626ff62b8cb2f03a2d7f7d28a91d51 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Mon, 22 Mar 2021 15:21:52 -0700 Subject: [PATCH] fonts: show config error window when glyphs cannot be resolved Keep track of the glyphs we've already advised about (until the config is reloaded) so that we don't keep spamming the user. refs: https://github.com/wez/wezterm/issues/559 --- wezterm-font/src/lib.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/wezterm-font/src/lib.rs b/wezterm-font/src/lib.rs index b12fd37eb..5dd253a78 100644 --- a/wezterm-font/src/lib.rs +++ b/wezterm-font/src/lib.rs @@ -126,14 +126,14 @@ impl LoadedFont { } if extra_handles.is_empty() { - log::warn!("No fonts have glyphs for {}", fallback_str.escape_debug()); + font_config.advise_no_glyphs(&fallback_str); } else { let loaded = self.insert_fallback_handles(extra_handles)?; if loaded { log::trace!("handles is now: {:#?}", self.handles); return self.shape(text); } else { - log::warn!("No fonts have glyphs for {}", fallback_str.escape_debug()) + font_config.advise_no_glyphs(&fallback_str); } } } @@ -180,6 +180,7 @@ struct FontConfigInner { locator: Box, font_dirs: RefCell, built_in: RefCell, + no_glyphs: RefCell>, } /// Matches and loads fonts for a given input style @@ -201,6 +202,7 @@ impl FontConfigInner { config: RefCell::new(config.clone()), font_dirs: RefCell::new(FontDatabase::with_font_dirs(&config)?), built_in: RefCell::new(FontDatabase::with_built_in()?), + no_glyphs: RefCell::new(HashSet::new()), }) } @@ -210,10 +212,30 @@ impl FontConfigInner { // Config was reloaded, invalidate our caches fonts.clear(); self.metrics.borrow_mut().take(); + self.no_glyphs.borrow_mut().clear(); *self.font_dirs.borrow_mut() = FontDatabase::with_font_dirs(config)?; Ok(()) } + fn advise_no_glyphs(&self, fallback_str: &str) { + let mut no_glyphs = self.no_glyphs.borrow_mut(); + let mut notif = String::new(); + for c in fallback_str.chars() { + if !no_glyphs.contains(&c) { + notif.push(c); + no_glyphs.insert(c); + } + } + if !notif.is_empty() { + mux::connui::show_configuration_error_message(&format!( + "Unable to resolve a font containing glyphs for {}. \ + A glyph from the built-in \"last resort\" font is being \ + used instead.", + notif.escape_unicode() + )); + } + } + /// Given a text style, load (with caching) the font that best /// matches according to the fontconfig pattern. fn resolve_font(&self, myself: &Rc, style: &TextStyle) -> anyhow::Result> {