ls-fonts: add --list-system flag to list system fonts

heads up @bew!

This is implemented on windows and font-config systems;
needs to be fleshed out for macos.

refs: https://github.com/wez/wezterm/issues/347
This commit is contained in:
Wez Furlong committed 2021-06-17 09:11:54 -07:00
1 parent 8ca0024bfb
commit e0b62d07ca
7 files changed
+119 -12

No files matched your search

+5 -10
View File
@@ -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::<Vec<_>>();
fonts.sort();
for font in fonts {
log::debug!("available font: {}", font.lua_name());
}
pub fn list_available(&self) -> Vec<ParsedFont> {
self.by_full_name
.values()
.map(|p| p.clone())
.collect::<Vec<_>>()
}
pub fn with_built_in() -> anyhow::Result<Self> {
@@ -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)
}
+13
View File
@@ -588,6 +588,19 @@ impl FontConfiguration {
self.inner.default_font_metrics(&self.inner)
}
pub fn list_fonts_in_font_dirs(&self) -> Vec<ParsedFont> {
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<Vec<ParsedFont>> {
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.
+29
View File
@@ -166,4 +166,33 @@ impl FontLocator for FontConfigFontLocator {
Ok(fonts)
}
fn enumerate_all_fonts(&self) -> anyhow::Result<Vec<ParsedFont>> {
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)
}
}
+33
View File
@@ -305,4 +305,37 @@ impl FontLocator for GdiFontLocator {
Ok(handles)
}
fn enumerate_all_fonts(&self) -> anyhow::Result<Vec<ParsedFont>> {
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)
}
}
+8
View File
@@ -156,6 +156,10 @@ pub trait FontLocator {
loaded: &mut HashSet<FontAttributes>,
) -> anyhow::Result<Vec<ParsedFont>>;
fn enumerate_all_fonts(&self) -> anyhow::Result<Vec<ParsedFont>> {
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<Vec<ParsedFont>> {
Ok(vec![])
}
fn locate_fallback_for_codepoints(
&self,
_codepoints: &[char],
+5 -1
View File
@@ -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,
}
+26 -1
View File
@@ -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(())
}