From 42aef898220408d4f099e2823f6852c2f96ee8b6 Mon Sep 17 00:00:00 2001 From: galister <22305755+galister@users.noreply.github.com> Date: Sun, 14 Apr 2024 18:16:26 +0900 Subject: [PATCH] feat: configurable primary_font --- src/config.rs | 7 +++++++ src/gui/font.rs | 9 +++++---- src/state.rs | 6 ++++-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/config.rs b/src/config.rs index 24dbf04..1fe76e9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -138,6 +138,10 @@ fn def_toast_topics() -> IdMap { IdMap::new() } +fn def_font() -> Arc { + "LiberationSans:style=Bold".into() +} + #[derive(Deserialize, Serialize)] pub struct GeneralConfig { #[serde(default = "def_watch_pos")] @@ -223,6 +227,9 @@ pub struct GeneralConfig { #[serde(default = "def_false")] pub focus_follows_mouse_mode: bool, + + #[serde(default = "def_font")] + pub primary_font: Arc, } impl GeneralConfig { diff --git a/src/gui/font.rs b/src/gui/font.rs index 425620c..53ee8f4 100644 --- a/src/gui/font.rs +++ b/src/gui/font.rs @@ -7,9 +7,8 @@ use vulkano::{command_buffer::CommandBufferUsage, format::Format, image::Image}; use crate::graphics::WlxGraphics; -const PRIMARY_FONT: &str = "LiberationSans"; - pub struct FontCache { + primary_font: Arc, fc: FontConfig, ft: Library, collections: IdMap, @@ -35,11 +34,12 @@ pub struct Glyph { } impl FontCache { - pub fn new() -> anyhow::Result { + pub fn new(primary_font: Arc) -> anyhow::Result { let ft = Library::init()?; let fc = FontConfig::default(); Ok(FontCache { + primary_font, fc, ft, collections: IdMap::new(), @@ -105,7 +105,8 @@ impl FontCache { return *font; } - let pattern_str = format!("{PRIMARY_FONT}-{size}:style=bold:charset={cp:04x}"); + let primary_font = self.primary_font.clone(); + let pattern_str = format!("{primary_font}:size={size}:charset={cp:04x}"); let mut pattern = OwnedPattern::from_str(&pattern_str).unwrap(); // safe because PRIMARY_FONT is const self.fc .substitute(&mut pattern, fontconfig::MatchKind::Pattern); diff --git a/src/state.rs b/src/state.rs index 52b2026..ba6c907 100644 --- a/src/state.rs +++ b/src/state.rs @@ -56,9 +56,11 @@ impl AppState { shaders.insert("frag_srgb", shader); } + let session = AppSession::load(); + Ok(AppState { - fc: FontCache::new()?, - session: AppSession::load(), + fc: FontCache::new(session.config.primary_font.clone())?, + session, tasks: TaskContainer::new(), graphics, input_state: InputState::new(),