Better fonts

This commit is contained in:
Piotr Siuszko 2025-01-08 22:25:32 +01:00
parent 35ae42d2bb
commit 7016780c6d
7 changed files with 96 additions and 55 deletions

View File

@ -10,20 +10,20 @@ env:
RUSTDOCFLAGS: -D warnings RUSTDOCFLAGS: -D warnings
jobs: jobs:
check: # check:
name: Check # name: Check
runs-on: ubuntu-latest # runs-on: ubuntu-latest
steps: # steps:
- uses: actions/checkout@v4 # - uses: actions/checkout@v4
- uses: actions-rs/toolchain@v1 # - uses: actions-rs/toolchain@v1
with: # with:
profile: minimal # profile: minimal
toolchain: stable # toolchain: stable
override: true # override: true
- uses: actions-rs/cargo@v1 # - uses: actions-rs/cargo@v1
with: # with:
command: check # command: check
args: --all-features --lib # args: --all-features --lib
check_wasm: check_wasm:
name: Check wasm32 name: Check wasm32

1
Cargo.lock generated
View File

@ -4404,6 +4404,7 @@ checksum = "3cd14fd5e3b777a7422cca79358c57a8f6e3a703d9ac187448d0daf220c2407f"
name = "rpack" name = "rpack"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow",
"eframe", "eframe",
"egui", "egui",
"egui_extras", "egui_extras",

View File

@ -2,9 +2,11 @@
mod bevy; mod bevy;
pub mod prelude { pub mod prelude {
pub use super::{AtlasAsset,SerializableRect, AtlasFrame};
#[cfg(feature = "bevy")] #[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. /// Defines a rectangle in pixels with the origin at the top-left of the texture atlas.

View File

@ -27,6 +27,7 @@ image = { version = "0.25", features = ["jpeg", "png"] }
egui_extras = { version = "*", features = ["all_loaders"] } egui_extras = { version = "*", features = ["all_loaders"] }
rfd = { version = "0.15", features = [] } rfd = { version = "0.15", features = [] }
wasm-bindgen-futures = "0.4" wasm-bindgen-futures = "0.4"
anyhow = "1"
# native: # native:
[target.'cfg(not(target_arch = "wasm32"))'.dependencies] [target.'cfg(not(target_arch = "wasm32"))'.dependencies]

View File

@ -89,7 +89,7 @@ impl TemplateApp {
} }
/// Called once before the first frame. /// Called once before the first frame.
pub fn new(cc: &eframe::CreationContext<'_>) -> Self { 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 // 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`. // `cc.egui_ctx.set_visuals` and `cc.egui_ctx.set_fonts`.
egui_extras::install_image_loaders(&cc.egui_ctx); 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 { impl eframe::App for TemplateApp {
/// Called by the frame work to save state before shutdown. /// Called by the frame work to save state before shutdown.
fn save(&mut self, storage: &mut dyn eframe::Storage) { fn save(&mut self, storage: &mut dyn eframe::Storage) {

74
crates/rpack/src/fonts.rs Normal file
View File

@ -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<u8>, Vec<u8>)> {
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<u8>, Vec<u8>)> {
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<u8>, Vec<u8>)> {
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))
}

View File

@ -1,4 +1,5 @@
#![warn(clippy::all, rust_2018_idioms)] #![warn(clippy::all, rust_2018_idioms)]
mod app; mod app;
mod fonts;
pub use app::TemplateApp; pub use app::TemplateApp;