Skip to content

Commit

Permalink
[system_fonts] Add Helvetica/.SFUIText to load list.
Browse files Browse the repository at this point in the history
Should fix OS X (or is it called macOS now?)

Related to #220
  • Loading branch information
ctrlcctrlv committed Dec 16, 2021
1 parent 084a25a commit 371547c
Showing 1 changed file with 61 additions and 21 deletions.
82 changes: 61 additions & 21 deletions src/system_fonts.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use lazy_static::lazy_static;

use crate::constants::CONSOLE_FONTS;
//use crate::constants::CONSOLE_FONTS;

use font_kit::{
family_name::FamilyName as FKFamilyName, handle::Handle as FKHandle, properties::Properties,
source::SystemSource,
error::SelectionError::NotFound as FKNotFoundError, family_name::FamilyName as FKFamilyName,
handle::Handle as FKHandle, properties::Properties, source::SystemSource,
};

use std::fs;
Expand All @@ -18,28 +18,68 @@ pub struct Font {

fn load_font(family: &[FKFamilyName]) -> Font {
log::debug!("Looking for a UI font to satisfy request for {:?}", family);
let font = match SystemSource::new().select_best_match(family, &Properties::new()) {
Ok(FKHandle::Path { path, .. }) => Font {
path: Some(path.clone()),
data: fs::read(path).expect("Failed to open font path system specified"),
},
Ok(FKHandle::Memory { bytes, .. }) => Font {
path: None,
data: Arc::try_unwrap(bytes).expect("Failed to load in-memory font"),
},
Err(e) => panic!(
"Failed to select font for {:?} ! Error from fontkit {:?}",
family, e
),
};
log::debug!("OK: Found {:?} (len {})", &font.path, font.data.len());
font
let mut font = None;
let source = SystemSource::new();
let props = Properties::new();
for fkfamname in family {
let best_match = source.select_best_match(&[fkfamname.clone()], &props);
if let Err(FKNotFoundError) = best_match {
log::debug!("Skipped {:?}", fkfamname);
}
font = match best_match {
Ok(FKHandle::Path { path, .. }) => Some(Font {
path: Some(path.clone()),
data: fs::read(path).expect("Failed to open font path system specified"),
}),
Ok(FKHandle::Memory { bytes, .. }) => Some(Font {
path: None,
data: Arc::try_unwrap(bytes).expect("Failed to load in-memory font"),
}),
// try next font…
Err(FKNotFoundError) => continue,
Err(e) => panic!(
"Failed to select font for {:?} ! Error from fontkit {:?}",
family, e
),
};
if let Some(ref font) = font {
log::debug!(
"OK: Found {:?} (matched @ {:?}, len {})",
&font.path,
fkfamname,
font.data.len()
);
break;
}
}
match font {
Some(font) => font,
None => {
panic!(
"In request for {:?}, no matches were made; cannot render UI!",
family
);
}
}
}

lazy_static! {
/// Windows 10 comes first because if we allow Windows to match on `sans-serif`, it will give
/// us Verdana, which looks incongruent on modern Windows OS. So, we specifically ask for Segoe
/// UI first. Meanwhile, on macOS…the situation is very confusing and complex due to Apple's
/// difficulty in deciding on a default font. I believe it works to try "Helvetica" and then
/// ".SFUI-Text" (San Francisco UI Text); Apple does _NOT_ resolve `sans-serif` to anything,
/// resulting in crash which became issue №220.
pub static ref SYSTEMSANS: Font = load_font(&[
// Windows 10
FKFamilyName::Title("Segoe UI".to_string()),
FKFamilyName::SansSerif
// Linux (fontconfig)
FKFamilyName::SansSerif,
// macOS ??
FKFamilyName::Title("Helvetica".to_string()),
// macOS ??
FKFamilyName::Title(".SFUIText".to_string()),
]);
pub static ref SYSTEMMONO: Font = load_font(CONSOLE_FONTS.as_slice());
//TODO: Replace console.
//pub static ref SYSTEMMONO: Font = load_font(CONSOLE_FONTS.as_slice());
}

0 comments on commit 371547c

Please sign in to comment.