From e6602dde7d2fc8caad103f916eda1a146e60f049 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 11 Apr 2021 16:53:12 -0700 Subject: [PATCH] fonts: refine font sorting and listing Sort the available fonts list by family name (rather than full name), then the styling attributes. Display the font name in the preferred form for inclusion in the wezterm config. --- wezterm-font/src/db.rs | 14 +++++++------- wezterm-font/src/locator/core_text.rs | 4 +--- wezterm-font/src/parser.rs | 25 +++++++++++++++---------- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/wezterm-font/src/db.rs b/wezterm-font/src/db.rs index ef4c6eb5d..e14e4c23b 100644 --- a/wezterm-font/src/db.rs +++ b/wezterm-font/src/db.rs @@ -49,15 +49,16 @@ impl FontDatabase { let mut db = Self::new(); db.load_font_info(font_info); + log::debug!("Available fonts from font_dirs:"); db.print_available(); Ok(db) } pub fn print_available(&self) { - let mut names = self.by_full_name.keys().collect::>(); - names.sort(); - for name in names { - log::debug!("available font: wezterm.font(\"{}\") ", name); + let mut fonts = self.by_full_name.values().collect::>(); + fonts.sort(); + for font in fonts { + log::debug!("available font: {}", font.lua_name()); } } @@ -66,6 +67,7 @@ impl FontDatabase { load_built_in_fonts(&mut font_info)?; let mut db = Self::new(); db.load_font_info(font_info); + log::debug!("Available built-in fonts:"); db.print_available(); Ok(db) } @@ -100,9 +102,7 @@ impl FontDatabase { let mut matches = vec![]; for parsed in self.by_full_name.values() { - if parsed.names().family.as_ref().map(|s| s.as_str()) - == Some("Last Resort High-Efficiency") - { + if parsed.names().family == "Last Resort High-Efficiency" { continue; } let covered = parsed diff --git a/wezterm-font/src/locator/core_text.rs b/wezterm-font/src/locator/core_text.rs index 1bb827d5b..da34240d1 100644 --- a/wezterm-font/src/locator/core_text.rs +++ b/wezterm-font/src/locator/core_text.rs @@ -69,9 +69,7 @@ fn handles_from_descriptor(descriptor: &CTFontDescriptor) -> Vec { let _ = crate::parser::parse_and_collect_font_info(&source, &mut font_info); for parsed in font_info { - if parsed.names().full_name == family_name - || parsed.names().family.as_ref() == Some(&family_name) - { + if parsed.names().full_name == family_name || parsed.names().family == family_name { result.push(parsed); } } diff --git a/wezterm-font/src/parser.rs b/wezterm-font/src/parser.rs index d0349437e..6776d2d58 100644 --- a/wezterm-font/src/parser.rs +++ b/wezterm-font/src/parser.rs @@ -60,13 +60,13 @@ impl PartialEq for ParsedFont { impl Ord for ParsedFont { fn cmp(&self, rhs: &Self) -> Ordering { - match self.stretch.cmp(&rhs.stretch) { + match self.names.family.cmp(&rhs.names.family) { o @ Ordering::Less | o @ Ordering::Greater => o, - Ordering::Equal => match self.weight.cmp(&rhs.weight) { + Ordering::Equal => match self.stretch.cmp(&rhs.stretch) { o @ Ordering::Less | o @ Ordering::Greater => o, - Ordering::Equal => match self.italic.cmp(&rhs.italic) { + Ordering::Equal => match self.weight.cmp(&rhs.weight) { o @ Ordering::Less | o @ Ordering::Greater => o, - Ordering::Equal => self.names.cmp(&rhs.names), + Ordering::Equal => self.italic.cmp(&rhs.italic), }, }, } @@ -82,7 +82,7 @@ impl PartialOrd for ParsedFont { #[derive(Debug, PartialEq, Eq, Clone, PartialOrd, Ord)] pub struct Names { pub full_name: String, - pub family: Option, + pub family: String, pub sub_family: Option, pub postscript_name: Option, } @@ -101,7 +101,7 @@ impl Names { Names { full_name, - family: Some(family), + family, sub_family: Some(sub_family), postscript_name: Some(postscript_name), } @@ -115,6 +115,13 @@ impl ParsedFont { Self::from_face(&face, handle.clone()) } + pub fn lua_name(&self) -> String { + format!( + "wezterm.font(\"{}\", weight=\"{}\", stretch=\"{}\", italic={})", + self.names.family, self.weight, self.stretch, self.italic + ) + } + pub fn from_face(face: &crate::ftwrap::Face, handle: FontDataHandle) -> anyhow::Result { let italic = face.italic(); let (weight, width) = face.weight_and_width(); @@ -170,10 +177,8 @@ impl ParsedFont { } pub fn matches_name(&self, attr: &FontAttributes) -> bool { - if let Some(fam) = self.names.family.as_ref() { - if attr.family == *fam { - return true; - } + if attr.family == self.names.family { + return true; } self.matches_full_or_ps_name(attr) }