Skip to content

Commit

Permalink
feat: Try to fetch system font using font-kit
Browse files Browse the repository at this point in the history
  • Loading branch information
VisualEhrmanntraut committed Jan 8, 2025
1 parent c2e9fa7 commit e665368
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 29 deletions.
9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ strip = true
[dependencies]
winit = { version = "0.30.8", default-features = true }
eframe = { version = "0.30.0", default-features = false, features = [
"default_fonts",
"accesskit",
"persistence",
"wgpu",
"wayland",
"x11",
] }
egui = { version = "0.30.0", default-features = false, features = [
"persistence",
] }
egui = { version = "0.30.0" }
egui_extras = "0.30.0"
hex = "0.4.3"
plist = "1.7.0"
rfd = "0.15.2"
serde = { version = "1.0.217", features = ["derive"] }
font-kit = "0.14.2"

[target.'cfg(target_os = "macos")'.dependencies]
objc2 = "0.5.2"
Expand All @@ -46,3 +46,6 @@ objc2-app-kit = { version = "0.2.2", features = [
"NSMenuItem",
] }
objc2-foundation = { version = "0.2.2", features = ["NSString", "NSThread"] }

[patch.crates-io]
pathfinder_simd = { git = "https://github.com/theoparis/pathfinder", rev = "05f19143e21a96027de7cf2d60fa47ecd8899d21" }
Binary file removed assets/fonts/Apple Symbols.ttf
Binary file not shown.
Binary file removed assets/fonts/Helvetica.ttf
Binary file not shown.
Binary file removed assets/fonts/JetBrainsMonoNerdFont.ttf
Binary file not shown.
Binary file removed assets/fonts/Symbol.ttf
Binary file not shown.
72 changes: 46 additions & 26 deletions src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,56 @@
//! See LICENSE for details.
use egui::{FontData, FontDefinitions, FontFamily};
use font_kit::{
family_name::FamilyName, handle::Handle, properties::Properties, source::SystemSource,
};

fn get_system_font(family: FamilyName) -> Option<Vec<u8>> {
match SystemSource::new()
.select_best_match(&[family], &Properties::new())
.ok()?
{
Handle::Path {
path,
font_index: _,
} => std::fs::read(path).ok(),
Handle::Memory {
bytes,
font_index: _,
} => Some(bytes.to_vec()),
}
}

pub fn get_fonts() -> FontDefinitions {
let mut fonts = FontDefinitions::default();
fonts.font_data.insert(
"Helvetica".into(),
FontData::from_static(include_bytes!("../assets/fonts/Helvetica.ttf")).into(),
);
fonts.font_data.insert(
"JetBrainsMonoNerdFont".into(),
FontData::from_static(include_bytes!("../assets/fonts/JetBrainsMonoNerdFont.ttf")).into(),
);
fonts.font_data.insert(
"Symbol".into(),
FontData::from_static(include_bytes!("../assets/fonts/Symbol.ttf")).into(),
);
fonts.font_data.insert(
"Apple Symbols".into(),
FontData::from_static(include_bytes!("../assets/fonts/Apple Symbols.ttf")).into(),
);

let ent = fonts.families.entry(FontFamily::Proportional).or_default();
ent.insert(0, "Helvetica".into());
ent.insert(1, "Symbol".into());
ent.insert(2, "Apple Symbols".into());

let ent = fonts.families.entry(FontFamily::Monospace).or_default();
ent.insert(0, "JetBrainsMonoNerdFont".into());
ent.insert(1, "Symbol".into());
ent.insert(2, "Apple Symbols".into());
ent.push("Helvetica".into());
if let Some(sans_serif) = get_system_font(FamilyName::SansSerif) {
fonts.font_data.insert(
"System Sans Serif".into(),
FontData::from_owned(sans_serif).into(),
);
fonts
.families
.entry(FontFamily::Proportional)
.or_default()
.insert(0, "System Sans Serif".into());
fonts
.families
.entry(FontFamily::Monospace)
.or_default()
.insert(0, "System Sans Serif".into());
}
if let Some(monospace) = get_system_font(FamilyName::Monospace) {
fonts.font_data.insert(
"System Monospace".into(),
FontData::from_owned(monospace).into(),
);
fonts
.families
.entry(FontFamily::Monospace)
.or_default()
.insert(0, "System Monospace".into());
}

fonts
}

0 comments on commit e665368

Please sign in to comment.