diff --git a/wezterm-font/src/db.rs b/wezterm-font/src/db.rs index 70423113a..ed7f25823 100644 --- a/wezterm-font/src/db.rs +++ b/wezterm-font/src/db.rs @@ -49,17 +49,14 @@ 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 fonts = self.by_full_name.values().collect::>(); - fonts.sort(); - for font in fonts { - log::debug!("available font: {}", font.lua_name()); - } + pub fn list_available(&self) -> Vec { + self.by_full_name + .values() + .map(|p| p.clone()) + .collect::>() } pub fn with_built_in() -> anyhow::Result { @@ -67,8 +64,6 @@ 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) } diff --git a/wezterm-font/src/lib.rs b/wezterm-font/src/lib.rs index 4f6050aaf..590449f56 100644 --- a/wezterm-font/src/lib.rs +++ b/wezterm-font/src/lib.rs @@ -588,6 +588,19 @@ impl FontConfiguration { self.inner.default_font_metrics(&self.inner) } + pub fn list_fonts_in_font_dirs(&self) -> Vec { + let mut font_dirs = self.inner.font_dirs.borrow().list_available(); + let mut built_in = self.inner.built_in.borrow().list_available(); + + font_dirs.append(&mut built_in); + font_dirs.sort(); + font_dirs + } + + pub fn list_system_fonts(&self) -> anyhow::Result> { + self.inner.locator.enumerate_all_fonts() + } + /// Apply the defined font_rules from the user configuration to /// produce the text style that best matches the supplied input /// cell attributes. diff --git a/wezterm-font/src/locator/font_config.rs b/wezterm-font/src/locator/font_config.rs index 6631b74af..9ef0f67c3 100644 --- a/wezterm-font/src/locator/font_config.rs +++ b/wezterm-font/src/locator/font_config.rs @@ -166,4 +166,33 @@ impl FontLocator for FontConfigFontLocator { Ok(fonts) } + + fn enumerate_all_fonts(&self) -> anyhow::Result> { + let mut fonts = vec![]; + let pattern = FontPattern::new()?; + let mut files = HashSet::new(); + for pat in pattern + .list() + .context("listing fonts from font-config")? + .iter() + { + let file = pat.get_file().context("pat.get_file")?; + if files.contains(&file) { + continue; + } + files.insert(file.clone()); + + let source = FontDataSource::OnDisk(file.into()); + if let Err(err) = crate::parser::parse_and_collect_font_info( + &source, + &mut fonts, + FontOrigin::FontConfig, + ) { + log::warn!("While parsing: {:?}: {:#}", source, err); + } + } + fonts.sort(); + + Ok(fonts) + } } diff --git a/wezterm-font/src/locator/gdi.rs b/wezterm-font/src/locator/gdi.rs index ddd941788..1b3433260 100644 --- a/wezterm-font/src/locator/gdi.rs +++ b/wezterm-font/src/locator/gdi.rs @@ -305,4 +305,37 @@ impl FontLocator for GdiFontLocator { Ok(handles) } + + fn enumerate_all_fonts(&self) -> anyhow::Result> { + let collection = dwrote::FontCollection::system(); + let mut fonts = vec![]; + let mut files = HashSet::new(); + for family in collection.families_iter() { + let count = family.get_font_count(); + for idx in 0..count { + let font = family.get_font(idx); + let face = font.create_font_face(); + for file in face.get_files() { + if let Some(path) = file.get_font_file_path() { + if files.contains(&path) { + continue; + } + files.insert(path.clone()); + + let source = FontDataSource::OnDisk(path); + if let Err(err) = parse_and_collect_font_info( + &source, + &mut fonts, + FontOrigin::DirectWrite, + ) { + log::warn!("While parsing: {:?}: {:#}", source, err); + } + } + } + } + } + fonts.sort(); + + Ok(fonts) + } } diff --git a/wezterm-font/src/locator/mod.rs b/wezterm-font/src/locator/mod.rs index 5288f2b1a..3fb80bf09 100644 --- a/wezterm-font/src/locator/mod.rs +++ b/wezterm-font/src/locator/mod.rs @@ -156,6 +156,10 @@ pub trait FontLocator { loaded: &mut HashSet, ) -> anyhow::Result>; + fn enumerate_all_fonts(&self) -> anyhow::Result> { + Ok(vec![]) + } + fn locate_fallback_for_codepoints( &self, codepoints: &[char], @@ -199,6 +203,10 @@ impl FontLocator for NopSystemSource { Ok(vec![]) } + fn enumerate_all_fonts(&self) -> anyhow::Result> { + Ok(vec![]) + } + fn locate_fallback_for_codepoints( &self, _codepoints: &[char], diff --git a/wezterm-gui-subcommands/src/lib.rs b/wezterm-gui-subcommands/src/lib.rs index e508fa9cf..36d2c4925 100644 --- a/wezterm-gui-subcommands/src/lib.rs +++ b/wezterm-gui-subcommands/src/lib.rs @@ -110,4 +110,8 @@ pub struct ConnectCommand { } #[derive(Debug, StructOpt, Clone)] -pub struct LsFontsCommand {} +pub struct LsFontsCommand { + /// Whether to list all fonts available to the system + #[structopt(long = "list-system")] + pub list_system: bool, +} diff --git a/wezterm-gui/src/main.rs b/wezterm-gui/src/main.rs index f891b2c78..edca412de 100644 --- a/wezterm-gui/src/main.rs +++ b/wezterm-gui/src/main.rs @@ -410,7 +410,7 @@ fn maybe_show_configuration_error_window() { } } -pub fn run_ls_fonts(config: config::ConfigHandle, _cmd: &LsFontsCommand) -> anyhow::Result<()> { +pub fn run_ls_fonts(config: config::ConfigHandle, cmd: &LsFontsCommand) -> anyhow::Result<()> { use wezterm_font::parser::ParsedFont; // Disable the normal config error UI window, as we don't have @@ -459,6 +459,31 @@ pub fn run_ls_fonts(config: config::ConfigHandle, _cmd: &LsFontsCommand) -> anyh println!(); } + if cmd.list_system { + let font_dirs = font_config.list_fonts_in_font_dirs(); + println!( + "{} fonts found in your font_dirs + built-in fonts:", + font_dirs.len() + ); + for font in font_dirs { + println!("{} -- {}", font.lua_name(), font.handle.diagnostic_string()); + } + + match font_config.list_system_fonts() { + Ok(sys_fonts) => { + println!( + "{} system fonts found using {:?}:", + sys_fonts.len(), + config.font_locator + ); + for font in sys_fonts { + println!("{} -- {}", font.lua_name(), font.handle.diagnostic_string()); + } + } + Err(err) => log::error!("Unable to list system fonts: {}", err), + } + } + Ok(()) }