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.
This commit is contained in:
Wez Furlong committed 2021-04-11 16:53:12 -07:00
1 parent 16701f61b4
commit e6602dde7d
3 files changed
+23 -20

No files matched your search

+7 -7
View File
@@ -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::<Vec<_>>();
names.sort();
for name in names {
log::debug!("available font: wezterm.font(\"{}\") ", name);
let mut fonts = self.by_full_name.values().collect::<Vec<_>>();
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
+1 -3
View File
@@ -69,9 +69,7 @@ fn handles_from_descriptor(descriptor: &CTFontDescriptor) -> Vec<ParsedFont> {
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);
}
}
+15 -10
View File
@@ -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<String>,
pub family: String,
pub sub_family: Option<String>,
pub postscript_name: Option<String>,
}
@@ -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<Self> {
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)
}