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
This commit is contained in:
Wez Furlong committed 2020-10-11 16:54:51 -07:00
1 parent bc1d4bba35
commit ba096771ef
9 files changed
+71 -28

No files matched your search

+22 -9
View File
@@ -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<FontAttributes> {
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
}
+3 -1
View File
@@ -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;
+1 -1
View File
@@ -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);
+3
View File
@@ -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<FontAttributes>,
) -> anyhow::Result<Vec<FontDataHandle>> {
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);
}
+4 -1
View File
@@ -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<FontAttributes>,
) -> anyhow::Result<Vec<FontDataHandle>> {
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)
+3
View File
@@ -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<FontAttributes>,
) -> anyhow::Result<Vec<FontDataHandle>> {
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)
+7 -2
View File
@@ -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<Vec<FontDataHandle>>;
fn load_fonts(
&self,
fonts_selection: &[FontAttributes],
loaded: &mut HashSet<FontAttributes>,
) -> anyhow::Result<Vec<FontDataHandle>>;
}
pub fn new_locator(locator: FontLocatorSelection) -> Box<dyn FontLocator> {
@@ -83,6 +87,7 @@ impl FontLocator for NopSystemSource {
fn load_fonts(
&self,
_fonts_selection: &[FontAttributes],
_loaded: &mut HashSet<FontAttributes>,
) -> anyhow::Result<Vec<FontDataHandle>> {
Ok(vec![])
}
+20 -5
View File
@@ -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));
+8 -9
View File
@@ -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<FontAttributes>,
) -> anyhow::Result<Vec<FontDataHandle>> {
// 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<FontAttributes>,
) -> anyhow::Result<Vec<FontDataHandle>> {
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<FontAttributes>,
) -> anyhow::Result<Vec<FontDataHandle>> {
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)
}