From dc83c6de2a035b670f92644fad7ed3adc30bf8c2 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 19 Dec 2020 09:12:41 -0800 Subject: [PATCH] fix locating Hasklig The root cause of this was that I'd added a fontformat=TrueType constraint to the fontconfig pattern and since fontconfig has fontformat=CFF for Hasklig, it wasn't the primary font candidate. When I cut out redundant fontconfig checks in ee1d84829a15bba560d333d8ec8e6c948ad5d00c it meant that we'd never "see" the hasklig result that turns up ~20 or so fonts into the fallback list. This commit removes the TrueType constraint so that the results are ranked correctly again. I've also switched the main font lookup path to using an alternative font config API that returns only the best match as that more closely aligns our intent in this function; originally, fallback was intended to be handled in this code path, but these days it has its own separate method. closes: https://github.com/wez/wezterm/issues/383 refs: https://github.com/wez/wezterm/issues/379 --- wezterm-font/src/fcwrap.rs | 12 ++++++ wezterm-font/src/locator/font_config.rs | 55 +++++++++---------------- 2 files changed, 32 insertions(+), 35 deletions(-) diff --git a/wezterm-font/src/fcwrap.rs b/wezterm-font/src/fcwrap.rs index f39b47b29..9c109885b 100644 --- a/wezterm-font/src/fcwrap.rs +++ b/wezterm-font/src/fcwrap.rs @@ -321,6 +321,18 @@ impl Pattern { } } + pub fn get_best_match(&self) -> Result { + unsafe { + let mut res = FcResultWrap(0); + let best = FcFontMatch(ptr::null_mut(), self.pat, &mut res.0 as *mut _); + + if !best.is_null() { + add_object(); + } + Ok(Pattern { pat: best }) + } + } + pub fn sort(&self, trim: bool) -> Result { unsafe { let mut res = FcResultWrap(0); diff --git a/wezterm-font/src/locator/font_config.rs b/wezterm-font/src/locator/font_config.rs index 098345e09..2f375c75c 100644 --- a/wezterm-font/src/locator/font_config.rs +++ b/wezterm-font/src/locator/font_config.rs @@ -20,51 +20,37 @@ impl FontLocator for FontConfigFontLocator { for attr in fonts_selection { let mut pattern = FontPattern::new()?; + let start = std::time::Instant::now(); pattern.family(&attr.family)?; pattern.add_integer("weight", if attr.bold { 200 } else { 80 })?; pattern.add_integer("slant", if attr.italic { 100 } else { 0 })?; - pattern.add_string("fontformat", "TrueType")?; - /* - pattern.add_double("size", config.font_size * font_scale)?; - pattern.add_double("dpi", config.dpi)?; - */ pattern.monospace()?; + log::trace!("fc pattern before config subst: {:?}", pattern); pattern.config_substitute(fcwrap::MatchKind::Pattern)?; pattern.default_substitute(); - // and obtain the selection with the best preference - // at index 0. - let start = std::time::Instant::now(); - let font_list = pattern.sort(true)?; + + let best = pattern.get_best_match()?; log::trace!( - "query font-config for {:?} took {:?}", - pattern, - start.elapsed() + "best match took {:?} to compute and is {:?}", + start.elapsed(), + best ); - for pat in font_list.iter() { - pattern.render_prepare(&pat)?; - let file = pat.get_file()?; + let file = best.get_file()?; + let handle = FontDataHandle::OnDisk { + path: file.into(), + index: best.get_integer("index")?.try_into()?, + }; - let handle = FontDataHandle::OnDisk { - path: file.into(), - index: pat.get_integer("index")?.try_into()?, - }; - - // fontconfig will give us a boatload of random fallbacks. - // so we need to parse the returned font - // here to see if we got what we asked for. - if let Ok(parsed) = crate::parser::ParsedFont::from_locator(&handle) { - if crate::parser::font_info_matches(attr, parsed.names()) { - fonts.push(handle); - loaded.insert(attr.clone()); - log::trace!("found font-config match for {:?}", parsed.names()); - } + // fontconfig will give us a boatload of random fallbacks. + // so we need to parse the returned font + // here to see if we got what we asked for. + if let Ok(parsed) = crate::parser::ParsedFont::from_locator(&handle) { + if crate::parser::font_info_matches(attr, parsed.names()) { + fonts.push(handle); + loaded.insert(attr.clone()); + log::trace!("found font-config match for {:?}", parsed.names()); } - - // If the first result from font-config isn't a match, then stop looking. - // font-config can return many fallback fonts. We don't want any of those - // at this stage. - break; } } @@ -82,7 +68,6 @@ impl FontLocator for FontConfigFontLocator { let mut pattern = FontPattern::new()?; pattern.add_charset(&charset)?; - pattern.add_string("fontformat", "TrueType")?; pattern.add_integer("weight", 80)?; pattern.add_integer("slant", 0)?;