From a2aec889c87844029e1e70c40f5e26fca8d5f46b Mon Sep 17 00:00:00 2001 From: Greg V Date: Sat, 30 Oct 2021 18:33:24 +0300 Subject: [PATCH] fonts: save used fontconfig match string in the origin for better visibility --- wezterm-font/src/ftwrap.rs | 2 +- wezterm-font/src/locator/font_config.rs | 13 +++++++++---- wezterm-font/src/locator/mod.rs | 11 ++++++++++- wezterm-font/src/parser.rs | 6 +++--- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/wezterm-font/src/ftwrap.rs b/wezterm-font/src/ftwrap.rs index 1760aa66e..fe47c3a45 100644 --- a/wezterm-font/src/ftwrap.rs +++ b/wezterm-font/src/ftwrap.rs @@ -167,7 +167,7 @@ impl Face { source: self.source.source.clone(), index: self.source.index, variation: i, - origin: self.source.origin, + origin: self.source.origin.clone(), coverage: self.source.coverage.clone(), }; res.push(ParsedFont::from_face(&self, source)?); diff --git a/wezterm-font/src/locator/font_config.rs b/wezterm-font/src/locator/font_config.rs index 21ec051fd..b2bf95677 100644 --- a/wezterm-font/src/locator/font_config.rs +++ b/wezterm-font/src/locator/font_config.rs @@ -65,7 +65,10 @@ impl FontLocator for FontConfigFontLocator { Ok(matches) } - fn to_handle(pat: FontPattern) -> anyhow::Result { + fn to_handle( + pat: FontPattern, + match_name: Option, + ) -> anyhow::Result { let file = pat.get_file()?; let index = pat.get_integer("index")? as u32; let variation = index >> 16; @@ -74,7 +77,9 @@ impl FontLocator for FontConfigFontLocator { source: FontDataSource::OnDisk(file.into()), index, variation, - origin: FontOrigin::FontConfig, + origin: match_name + .map(FontOrigin::FontConfigMatch) + .unwrap_or(FontOrigin::FontConfig), coverage: pat.get_charset().ok().map(|c| c.to_range_set()), }) } @@ -87,7 +92,7 @@ impl FontLocator for FontConfigFontLocator { match resolver(attr) { Ok(matches) => { for pat in matches { - let handle = to_handle(pat)?; + let handle = to_handle(pat, None)?; // fontconfig will give us a boatload of random fallbacks. // so we need to parse the returned font @@ -130,7 +135,7 @@ impl FontLocator for FontConfigFontLocator { // For the fallback, be very careful, only select known monospace if let Ok(spacing) = best_match.get_integer("spacing") { if spacing == FC_MONO || spacing == FC_DUAL { - let handle = to_handle(best_match)?; + let handle = to_handle(best_match, Some(attr.family.clone()))?; if let Ok(parsed) = crate::parser::ParsedFont::from_locator(&handle) { log::trace!( "found font-config fallback match for {:?}", diff --git a/wezterm-font/src/locator/mod.rs b/wezterm-font/src/locator/mod.rs index 4e75151e5..c1db3e4b2 100644 --- a/wezterm-font/src/locator/mod.rs +++ b/wezterm-font/src/locator/mod.rs @@ -13,9 +13,10 @@ pub mod core_text; pub mod font_config; pub mod gdi; -#[derive(Clone, Copy, Debug, PartialEq, Eq, Ord, PartialOrd, Display)] +#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd)] pub enum FontOrigin { FontConfig, + FontConfigMatch(String), CoreText, DirectWrite, Gdi, @@ -23,6 +24,14 @@ pub enum FontOrigin { BuiltIn, } +// derived impl would just use the inner string instead of +// 'FontConfigMatch("..")', so use Debug +impl Display for FontOrigin { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{:?}", self) + } +} + #[derive(Clone)] pub enum FontDataSource { OnDisk(PathBuf), diff --git a/wezterm-font/src/parser.rs b/wezterm-font/src/parser.rs index 294d5cd21..23ca63e02 100644 --- a/wezterm-font/src/parser.rs +++ b/wezterm-font/src/parser.rs @@ -535,13 +535,13 @@ pub(crate) fn parse_and_collect_font_info( source: &FontDataSource, index: u32, font_info: &mut Vec, - origin: FontOrigin, + origin: &FontOrigin, ) -> anyhow::Result<()> { let locator = FontDataHandle { source: source.clone(), index, variation: 0, - origin, + origin: origin.clone(), coverage: None, }; @@ -558,7 +558,7 @@ pub(crate) fn parse_and_collect_font_info( } for index in 0..num_faces { - if let Err(err) = load_one(&lib, &source, index, font_info, origin) { + if let Err(err) = load_one(&lib, &source, index, font_info, &origin) { log::trace!("error while parsing {:?} index {}: {}", source, index, err); } }