From 7016780c6de78ee4739ec0cc4db0278314ce6d60 Mon Sep 17 00:00:00 2001 From: Piotr Siuszko Date: Wed, 8 Jan 2025 22:25:32 +0100 Subject: [PATCH] Better fonts --- .github/workflows/rust.yml | 28 +++++++------- Cargo.lock | 1 + crates/bevy_rpack/src/lib.rs | 6 ++- crates/rpack/Cargo.toml | 1 + crates/rpack/src/app.rs | 40 +------------------ crates/rpack/src/fonts.rs | 74 ++++++++++++++++++++++++++++++++++++ crates/rpack/src/lib.rs | 1 + 7 files changed, 96 insertions(+), 55 deletions(-) create mode 100644 crates/rpack/src/fonts.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 5a2c968..0f22db7 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -10,20 +10,20 @@ env: RUSTDOCFLAGS: -D warnings jobs: - check: - name: Check - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: actions-rs/toolchain@v1 - with: - profile: minimal - toolchain: stable - override: true - - uses: actions-rs/cargo@v1 - with: - command: check - args: --all-features --lib + # check: + # name: Check + # runs-on: ubuntu-latest + # steps: + # - uses: actions/checkout@v4 + # - uses: actions-rs/toolchain@v1 + # with: + # profile: minimal + # toolchain: stable + # override: true + # - uses: actions-rs/cargo@v1 + # with: + # command: check + # args: --all-features --lib check_wasm: name: Check wasm32 diff --git a/Cargo.lock b/Cargo.lock index e261d68..1f8b297 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4404,6 +4404,7 @@ checksum = "3cd14fd5e3b777a7422cca79358c57a8f6e3a703d9ac187448d0daf220c2407f" name = "rpack" version = "0.1.0" dependencies = [ + "anyhow", "eframe", "egui", "egui_extras", diff --git a/crates/bevy_rpack/src/lib.rs b/crates/bevy_rpack/src/lib.rs index c0213f6..c72d56b 100644 --- a/crates/bevy_rpack/src/lib.rs +++ b/crates/bevy_rpack/src/lib.rs @@ -2,9 +2,11 @@ mod bevy; pub mod prelude { - pub use super::{AtlasAsset,SerializableRect, AtlasFrame}; #[cfg(feature = "bevy")] - pub use super::bevy::{RpackAssetPlugin, RpackAtlasAsset, RpackAtlasAssetError, RpackAtlasAssetLoader}; + pub use super::bevy::{ + RpackAssetPlugin, RpackAtlasAsset, RpackAtlasAssetError, RpackAtlasAssetLoader, + }; + pub use super::{AtlasAsset, AtlasFrame, SerializableRect}; } /// Defines a rectangle in pixels with the origin at the top-left of the texture atlas. diff --git a/crates/rpack/Cargo.toml b/crates/rpack/Cargo.toml index 120efc9..3b3613a 100644 --- a/crates/rpack/Cargo.toml +++ b/crates/rpack/Cargo.toml @@ -27,6 +27,7 @@ image = { version = "0.25", features = ["jpeg", "png"] } egui_extras = { version = "*", features = ["all_loaders"] } rfd = { version = "0.15", features = [] } wasm-bindgen-futures = "0.4" +anyhow = "1" # native: [target.'cfg(not(target_arch = "wasm32"))'.dependencies] diff --git a/crates/rpack/src/app.rs b/crates/rpack/src/app.rs index e69b54f..bbae0e0 100644 --- a/crates/rpack/src/app.rs +++ b/crates/rpack/src/app.rs @@ -89,7 +89,7 @@ impl TemplateApp { } /// Called once before the first frame. pub fn new(cc: &eframe::CreationContext<'_>) -> Self { - setup_custom_fonts(&cc.egui_ctx); + crate::fonts::setup_custom_fonts(&cc.egui_ctx); // This is also where you can customize the look and feel of egui using // `cc.egui_ctx.set_visuals` and `cc.egui_ctx.set_fonts`. egui_extras::install_image_loaders(&cc.egui_ctx); @@ -232,44 +232,6 @@ impl TemplateApp { } } -fn setup_custom_fonts(ctx: &egui::Context) { - // Start with the default fonts (we will be adding to them rather than replacing them). - let mut fonts = egui::FontDefinitions::default(); - - // Install my own font (maybe supporting non-latin characters). - // .ttf and .otf files supported. - fonts.font_data.insert( - "regular".to_owned(), - egui::FontData::from_static(include_bytes!("../static/JetBrainsMonoNL-Regular.ttf")).into(), - ); - fonts.font_data.insert( - "semibold".to_owned(), - egui::FontData::from_static(include_bytes!("../static/JetBrainsMono-SemiBold.ttf")).into(), - ); - - // Put my font first (highest priority) for proportional text: - fonts - .families - .entry(egui::FontFamily::Proportional) - .or_default() - .insert(0, "regular".to_owned()); - fonts - .families - .entry(egui::FontFamily::Name("semibold".into())) - .or_default() - .insert(0, "semibold".to_owned()); - - // Put my font as last fallback for monospace: - fonts - .families - .entry(egui::FontFamily::Monospace) - .or_default() - .push("regular".to_owned()); - - // Tell egui to use these fonts: - ctx.set_fonts(fonts); -} - impl eframe::App for TemplateApp { /// Called by the frame work to save state before shutdown. fn save(&mut self, storage: &mut dyn eframe::Storage) { diff --git a/crates/rpack/src/fonts.rs b/crates/rpack/src/fonts.rs new file mode 100644 index 0000000..d791ed6 --- /dev/null +++ b/crates/rpack/src/fonts.rs @@ -0,0 +1,74 @@ +pub fn setup_custom_fonts(ctx: &egui::Context) { + // Start with the default fonts (we will be adding to them rather than replacing them). + let mut fonts = egui::FontDefinitions::default(); + let Ok((regular, semibold)) = get_fonts() else { + return; + }; + fonts.font_data.insert( + "regular".to_owned(), + egui::FontData::from_owned(regular).into(), + ); + fonts.font_data.insert( + "semibold".to_owned(), + egui::FontData::from_owned(semibold).into(), + ); + + // Put my font first (highest priority) for proportional text: + fonts + .families + .entry(egui::FontFamily::Proportional) + .or_default() + .insert(0, "regular".to_owned()); + fonts + .families + .entry(egui::FontFamily::Name("semibold".into())) + .or_default() + .insert(0, "semibold".to_owned()); + + // Put my font as last fallback for monospace: + fonts + .families + .entry(egui::FontFamily::Monospace) + .or_default() + .push("regular".to_owned()); + + // Tell egui to use these fonts: + ctx.set_fonts(fonts); + + ctx.style_mut(|style| { + for font_id in style.text_styles.values_mut() { + font_id.size *= 1.4; + } + }); +} + +#[cfg(all(not(target_os = "macos"), not(windows)))] +fn get_fonts() -> anyhow::Result<(Vec, Vec)> { + let regular = include_bytes!("../static/JetBrainsMonoNL-Regular.ttf").to_vec(); + let semibold = include_bytes!("../static/JetBrainsMono-SemiBold.ttf").to_vec(); + + Ok((regular, semibold)) +} + +#[cfg(target_os = "macos")] +fn get_fonts() -> anyhow::Result<(Vec, Vec)> { + let font_path = std::path::Path::new("/System/Library/Fonts"); + + let regular = fs::read(font_path.join("SFNSRounded.ttf"))?; + let semibold = fs::read(font_path.join("SFCompact.ttf"))?; + + Ok((regular, semibold)) +} + +#[cfg(windows)] +fn get_fonts() -> anyhow::Result<(Vec, Vec)> { + use std::fs; + + let app_data = std::env::var("APPDATA")?; + let font_path = std::path::Path::new(&app_data); + + let regular = fs::read(font_path.join("../Local/Microsoft/Windows/Fonts/aptos.ttf"))?; + let semibold = fs::read(font_path.join("../Local/Microsoft/Windows/Fonts/aptos-semibold.ttf"))?; + + Ok((regular, semibold)) +} diff --git a/crates/rpack/src/lib.rs b/crates/rpack/src/lib.rs index fbae77a..f47af5d 100644 --- a/crates/rpack/src/lib.rs +++ b/crates/rpack/src/lib.rs @@ -1,4 +1,5 @@ #![warn(clippy::all, rust_2018_idioms)] mod app; +mod fonts; pub use app::TemplateApp;