From ba096771ef2d8563730714147fa056eb5ff6981c Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 11 Oct 2020 16:51:55 -0700 Subject: [PATCH] wezterm: show config error window when fonts fail to load This commit teaches the config layer to distinguish between explicitly configured fonts and automatic fallback fonts. Font loading now maintains the set of loaded fonts. If after loading from all configured sources a non-fallback font is not present in the loaded set, wezterm will now pop up the configuration error window to explain what is happening. closes: #263 --- config/src/font.rs | 31 ++++++++++++++++++------- config/src/lua.rs | 4 +++- wezterm/src/connui.rs | 2 +- wezterm/src/font/locator/font_config.rs | 3 +++ wezterm/src/font/locator/font_kit.rs | 5 +++- wezterm/src/font/locator/font_loader.rs | 3 +++ wezterm/src/font/locator/mod.rs | 9 +++++-- wezterm/src/font/mod.rs | 25 ++++++++++++++++---- wezterm/src/font/parser.rs | 17 +++++++------- 9 files changed, 71 insertions(+), 28 deletions(-) diff --git a/config/src/font.rs b/config/src/font.rs index c6b18f007..0f706aa04 100644 --- a/config/src/font.rs +++ b/config/src/font.rs @@ -47,6 +47,7 @@ pub struct FontAttributes { /// Whether the font should be an italic variant #[serde(default)] pub italic: bool, + pub is_fallback: bool, } impl_lua_conversion!(FontAttributes); @@ -56,6 +57,16 @@ impl FontAttributes { family: family.into(), bold: false, italic: false, + is_fallback: false, + } + } + + pub fn new_fallback(family: &str) -> Self { + Self { + family: family.into(), + bold: false, + italic: false, + is_fallback: true, } } } @@ -66,6 +77,7 @@ impl Default for FontAttributes { family: "JetBrains Mono".into(), bold: false, italic: false, + is_fallback: false, } } } @@ -130,34 +142,35 @@ impl TextStyle { pub fn font_with_fallback(&self) -> Vec { let mut font = self.font.clone(); - let default_font = FontAttributes::default(); + let mut default_font = FontAttributes::default(); // Insert our bundled default JetBrainsMono as a fallback // in case their preference doesn't match anything. // But don't add it if it is already their preference. if font.iter().position(|f| *f == default_font).is_none() { + default_font.is_fallback = true; font.push(default_font); } #[cfg(target_os = "macos")] - font.push(FontAttributes::new("Apple Color Emoji")); + font.push(FontAttributes::new_fallback("Apple Color Emoji")); #[cfg(target_os = "macos")] - font.push(FontAttributes::new("Apple Symbols")); + font.push(FontAttributes::new_fallback("Apple Symbols")); #[cfg(target_os = "macos")] - font.push(FontAttributes::new("Zapf Dingbats")); + font.push(FontAttributes::new_fallback("Zapf Dingbats")); #[cfg(target_os = "macos")] - font.push(FontAttributes::new("Apple LiGothic")); + font.push(FontAttributes::new_fallback("Apple LiGothic")); // Fallback font that has unicode replacement character #[cfg(windows)] - font.push(FontAttributes::new("Segoe UI")); + font.push(FontAttributes::new_fallback("Segoe UI")); #[cfg(windows)] - font.push(FontAttributes::new("Segoe UI Emoji")); + font.push(FontAttributes::new_fallback("Segoe UI Emoji")); #[cfg(windows)] - font.push(FontAttributes::new("Segoe UI Symbol")); + font.push(FontAttributes::new_fallback("Segoe UI Symbol")); // We bundle this emoji font as an in-memory fallback - font.push(FontAttributes::new("Noto Color Emoji")); + font.push(FontAttributes::new_fallback("Noto Color Emoji")); font } diff --git a/config/src/lua.rs b/config/src/lua.rs index 12cc7515b..088437ae4 100644 --- a/config/src/lua.rs +++ b/config/src/lua.rs @@ -179,6 +179,7 @@ fn font<'lua>( family, bold: attrs.bold, italic: attrs.italic, + is_fallback: false, }); text_style.foreground = attrs.foreground; @@ -200,11 +201,12 @@ fn font_with_fallback<'lua>( let mut text_style = TextStyle::default(); text_style.font.clear(); - for family in fallback { + for (idx, family) in fallback.into_iter().enumerate() { text_style.font.push(FontAttributes { family, bold: attrs.bold, italic: attrs.italic, + is_fallback: idx != 0, }); } text_style.foreground = attrs.foreground; diff --git a/wezterm/src/connui.rs b/wezterm/src/connui.rs index 922c7872a..7bac7c9c3 100644 --- a/wezterm/src/connui.rs +++ b/wezterm/src/connui.rs @@ -428,7 +428,7 @@ fn get_error_window() -> ConnectionUI { /// message framed as a configuration error. /// If there is no GUI front end, generates a toast notification instead. pub fn show_configuration_error_message(err: &str) { - log::error!("While (re)loading configuration: {}", err); + log::error!("Configuration Error: {}", err); let ui = get_error_window(); let mut wrapped = textwrap::fill(&err, 78); diff --git a/wezterm/src/font/locator/font_config.rs b/wezterm/src/font/locator/font_config.rs index eb1a169e4..52ffb53b7 100644 --- a/wezterm/src/font/locator/font_config.rs +++ b/wezterm/src/font/locator/font_config.rs @@ -2,6 +2,7 @@ use crate::font::fcwrap; use crate::font::locator::{FontDataHandle, FontLocator}; use config::FontAttributes; use fcwrap::Pattern as FontPattern; +use std::collections::HashSet; use std::convert::TryInto; /// A FontLocator implemented using the system font loading @@ -12,6 +13,7 @@ impl FontLocator for FontConfigFontLocator { fn load_fonts( &self, fonts_selection: &[FontAttributes], + loaded: &mut HashSet, ) -> anyhow::Result> { let mut fonts = vec![]; let mut fallback = vec![]; @@ -47,6 +49,7 @@ impl FontLocator for FontConfigFontLocator { // suggested by fontconfig and are lower precedence if idx == 0 { fonts.push(handle); + loaded.insert(attr.clone()); } else { fallback.push(handle); } diff --git a/wezterm/src/font/locator/font_kit.rs b/wezterm/src/font/locator/font_kit.rs index 2199f46aa..e98923da5 100644 --- a/wezterm/src/font/locator/font_kit.rs +++ b/wezterm/src/font/locator/font_kit.rs @@ -4,6 +4,7 @@ use ::font_kit::handle::Handle; use ::font_kit::properties::Properties; use ::font_kit::source::Source; use config::FontAttributes; +use std::collections::HashSet; /// A FontLocator implemented using the font loading /// functions provided by Source's from font-kit crate. @@ -14,6 +15,7 @@ where fn load_fonts( &self, fonts_selection: &[FontAttributes], + loaded: &mut HashSet, ) -> anyhow::Result> { let mut handles = vec![]; @@ -35,8 +37,9 @@ where data: bytes.to_vec(), index: font_index, }), - Err(_) => {} + Err(_) => continue, } + loaded.insert(font.clone()); } Ok(handles) diff --git a/wezterm/src/font/locator/font_loader.rs b/wezterm/src/font/locator/font_loader.rs index 963db2a55..b29932626 100644 --- a/wezterm/src/font/locator/font_loader.rs +++ b/wezterm/src/font/locator/font_loader.rs @@ -1,6 +1,7 @@ use crate::font::locator::{FontDataHandle, FontLocator}; use ::font_loader::system_fonts; use config::FontAttributes; +use std::collections::HashSet; /// A FontLocator implemented using the system font loading /// functions provided by the font-loader crate. @@ -10,6 +11,7 @@ impl FontLocator for FontLoaderFontLocator { fn load_fonts( &self, fonts_selection: &[FontAttributes], + loaded: &mut HashSet, ) -> anyhow::Result> { let mut fonts = Vec::new(); for font_attr in fonts_selection { @@ -43,6 +45,7 @@ impl FontLocator for FontLoaderFontLocator { index: index as u32, }; fonts.push(handle); + loaded.insert(font_attr.clone()); } } Ok(fonts) diff --git a/wezterm/src/font/locator/mod.rs b/wezterm/src/font/locator/mod.rs index a4dee432c..3afb92337 100644 --- a/wezterm/src/font/locator/mod.rs +++ b/wezterm/src/font/locator/mod.rs @@ -1,4 +1,5 @@ use config::FontAttributes; +use std::collections::HashSet; use std::path::PathBuf; #[cfg(all(unix, not(target_os = "macos")))] @@ -47,8 +48,11 @@ impl std::fmt::Debug for FontDataHandle { pub trait FontLocator { /// Given a font selection, return the list of successfully loadable /// FontDataHandle's that correspond to it - fn load_fonts(&self, fonts_selection: &[FontAttributes]) - -> anyhow::Result>; + fn load_fonts( + &self, + fonts_selection: &[FontAttributes], + loaded: &mut HashSet, + ) -> anyhow::Result>; } pub fn new_locator(locator: FontLocatorSelection) -> Box { @@ -83,6 +87,7 @@ impl FontLocator for NopSystemSource { fn load_fonts( &self, _fonts_selection: &[FontAttributes], + _loaded: &mut HashSet, ) -> anyhow::Result> { Ok(vec![]) } diff --git a/wezterm/src/font/mod.rs b/wezterm/src/font/mod.rs index 584e373d3..224a7c246 100644 --- a/wezterm/src/font/mod.rs +++ b/wezterm/src/font/mod.rs @@ -2,7 +2,7 @@ use anyhow::{anyhow, Error}; mod hbwrap; use std::cell::RefCell; -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use std::rc::Rc; pub mod ftwrap; @@ -110,10 +110,25 @@ impl FontConfiguration { } let attributes = style.font_with_fallback(); - let mut handles = parser::ParsedFont::load_fonts(&config, &attributes)?; - handles.append(&mut self.locator.load_fonts(&attributes)?); - handles.append(&mut parser::ParsedFont::load_built_in_fonts(&attributes)?); - log::debug!("handles: {:#?}", handles); + let mut loaded = HashSet::new(); + let mut handles = parser::ParsedFont::load_fonts(&config, &attributes, &mut loaded)?; + handles.append(&mut self.locator.load_fonts(&attributes, &mut loaded)?); + handles.append(&mut parser::ParsedFont::load_built_in_fonts( + &attributes, + &mut loaded, + )?); + + for attr in &attributes { + if !attr.is_fallback && !loaded.contains(attr) { + crate::connui::show_configuration_error_message(&format!( + "Unable to load a font matching {:?}. Fallback(s) + are being used instead, and the terminal may not + render as intended", + attr + )); + } + } + let mut rasterizers = vec![]; for _ in &handles { rasterizers.push(RefCell::new(None)); diff --git a/wezterm/src/font/parser.rs b/wezterm/src/font/parser.rs index 2559d7222..52aade3ea 100644 --- a/wezterm/src/font/parser.rs +++ b/wezterm/src/font/parser.rs @@ -2,7 +2,7 @@ //! At this time it is used only to extract name information, //! but in the future I'd like to use its shaping functionality #![allow(dead_code)] -use crate::font::locator::{FontDataHandle, FontLocatorSelection}; +use crate::font::locator::FontDataHandle; use crate::font::shaper::{FallbackIdx, FontMetrics, GlyphInfo}; use crate::font::units::*; use allsorts::binary::read::{ReadScope, ReadScopeOwned}; @@ -18,6 +18,7 @@ use allsorts::tables::{ use allsorts::tag; use anyhow::anyhow; use config::{Config, FontAttributes}; +use std::collections::HashSet; use std::convert::TryInto; use std::path::{Path, PathBuf}; use termwiz::cell::unicode_column_width; @@ -78,6 +79,7 @@ impl ParsedFont { pub fn load_fonts( config: &Config, fonts_selection: &[FontAttributes], + loaded: &mut HashSet, ) -> anyhow::Result> { // First discover the available fonts let mut font_info = vec![]; @@ -93,20 +95,22 @@ impl ParsedFont { } } - Self::match_font_info(fonts_selection, font_info) + Self::match_font_info(fonts_selection, font_info, loaded) } pub fn load_built_in_fonts( fonts_selection: &[FontAttributes], + loaded: &mut HashSet, ) -> anyhow::Result> { let mut font_info = vec![]; load_built_in_fonts(&mut font_info).ok(); - Self::match_font_info(fonts_selection, font_info) + Self::match_font_info(fonts_selection, font_info, loaded) } fn match_font_info( fonts_selection: &[FontAttributes], mut font_info: Vec<(Names, std::path::PathBuf, FontDataHandle)>, + loaded: &mut HashSet, ) -> anyhow::Result> { font_info.sort_by_key(|(names, _, _)| names.full_name.clone()); for (names, _, _) in &font_info { @@ -119,19 +123,14 @@ impl ParsedFont { // fonts_selection is strictly ordered let mut handles = vec![]; for attr in fonts_selection { - let mut found = false; for (names, path, handle) in &font_info { if font_info_matches(attr, &names) { log::warn!("Using {} from {}", names.full_name, path.display(),); handles.push(handle.clone()); - found = true; + loaded.insert(attr.clone()); break; } } - if !found && FontLocatorSelection::get_default() == FontLocatorSelection::ConfigDirsOnly - { - log::error!("Did not locate a font match for {:?}", attr); - } } Ok(handles) }