Configurable font stretch, font weight, and bold font weight

* Store a font name => font faces map

 * Only list font names that have `NORMAL` and `BOLD` faces with
   `Normal` stretch. This will give us defaults and fall-backs that are
   guaranteed to always exist.

 * Filter by stretch first, with `Normal` chosen as the always existing
   default.

 * Then only list font weights supported by the font name stretch
   selected, for the normal and bold cases.

 * When changing the font name selected, the stretch/weight options
   will stay the same if supported by the new font name, or revert to
   the always existing fall-backs.

Signed-off-by: Mohammad AlSaleh <CE.Mohammad.AlSaleh@gmail.com>
This commit is contained in:
Mohammad AlSaleh 2024-01-10 01:19:04 +03:00 committed by Jeremy Soller
parent c74d5d6f56
commit 5eba6eb4d6
6 changed files with 251 additions and 12 deletions

View file

@ -4,9 +4,12 @@ use cosmic::{
cosmic_config::{self, cosmic_config_derive::CosmicConfigEntry, CosmicConfigEntry},
theme,
};
use cosmic_text::Metrics;
use cosmic_text::{Metrics, Weight, Stretch};
use serde::{Deserialize, Serialize};
use std::sync::OnceLock;
use std::collections::BTreeMap;
pub const CONFIG_VERSION: u64 = 1;
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
@ -31,6 +34,9 @@ pub struct Config {
pub app_theme: AppTheme,
pub font_name: String,
pub font_size: u16,
pub font_weight: u16,
pub bold_font_weight: u16,
pub font_stretch: u16,
pub font_size_zoom_step_mul_100: u16,
pub show_headerbar: bool,
pub syntax_theme_dark: String,
@ -43,6 +49,9 @@ impl Default for Config {
app_theme: AppTheme::System,
font_name: "Fira Mono".to_string(),
font_size: 14,
font_weight: Weight::NORMAL.0,
bold_font_weight: Weight::BOLD.0,
font_stretch: Stretch::Normal.to_number(),
font_size_zoom_step_mul_100: 100,
show_headerbar: true,
syntax_theme_dark: "COSMIC Dark".to_string(),
@ -75,4 +84,23 @@ impl Config {
&self.syntax_theme_light
}
}
pub fn typed_font_stretch(&self) -> Stretch {
macro_rules! populate_num_typed_map {
($($stretch:ident,)+) => {
let mut map = BTreeMap::new();
$(map.insert(Stretch::$stretch.to_number(), Stretch::$stretch);)+
map
};
}
static NUM_TO_TYPED_MAP: OnceLock<BTreeMap<u16, Stretch>> = OnceLock::new();
NUM_TO_TYPED_MAP.get_or_init(|| {
populate_num_typed_map!{
UltraCondensed, ExtraCondensed, Condensed, SemiCondensed,
Normal, SemiExpanded, Expanded, ExtraExpanded, UltraExpanded,
}
})[&self.font_stretch]
}
}