use std::collections::HashSet;
use std::sync::{Arc, Mutex};
use crate::Rgba;
use native_theme_derive::ThemeFields;
use serde::{Deserialize, Serialize};
static FONT_FAMILY_CACHE: std::sync::LazyLock<Mutex<HashSet<Arc<str>>>> =
std::sync::LazyLock::new(|| Mutex::new(HashSet::new()));
pub fn intern_font_family(family: &str) -> Arc<str> {
if let Ok(mut cache) = FONT_FAMILY_CACHE.lock() {
if let Some(existing) = cache.get(family) {
return Arc::clone(existing);
}
let arc: Arc<str> = Arc::from(family);
cache.insert(Arc::clone(&arc));
arc
} else {
Arc::from(family)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum FontStyle {
#[default]
Normal,
Italic,
Oblique,
}
#[derive(Clone, Copy, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum FontSize {
Pt(f32),
Px(f32),
}
impl FontSize {
pub fn to_logical_px(self, dpi: f32) -> f32 {
match self {
Self::Pt(v) => v * dpi / 72.0,
Self::Px(v) => v,
}
}
pub fn raw(self) -> f32 {
match self {
Self::Pt(v) | Self::Px(v) => v,
}
}
pub fn is_pt(self) -> bool {
matches!(self, Self::Pt(_))
}
}
impl Default for FontSize {
fn default() -> Self {
Self::Px(0.0)
}
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, ThemeFields)]
#[serde(try_from = "FontSpecRaw", into = "FontSpecRaw")]
#[theme_layer(fields = "family, size_pt, size_px, weight, style, color")]
pub struct FontSpec {
pub family: Option<Arc<str>>,
pub size: Option<FontSize>,
pub weight: Option<u16>,
pub style: Option<FontStyle>,
pub color: Option<Rgba>,
}
#[serde_with::skip_serializing_none]
#[derive(Default, Serialize, Deserialize)]
#[serde(default)]
struct FontSpecRaw {
family: Option<Arc<str>>,
size_pt: Option<f32>,
size_px: Option<f32>,
weight: Option<u16>,
style: Option<FontStyle>,
color: Option<Rgba>,
}
impl TryFrom<FontSpecRaw> for FontSpec {
type Error = String;
fn try_from(raw: FontSpecRaw) -> Result<Self, Self::Error> {
let size = match (raw.size_pt, raw.size_px) {
(Some(v), None) => Some(FontSize::Pt(v)),
(None, Some(v)) => Some(FontSize::Px(v)),
(None, None) => None,
(Some(_), Some(_)) => return Err("font: set `size_pt` or `size_px`, not both".into()),
};
Ok(FontSpec {
family: raw.family,
size,
weight: raw.weight,
style: raw.style,
color: raw.color,
})
}
}
impl From<FontSpec> for FontSpecRaw {
fn from(fs: FontSpec) -> Self {
let (size_pt, size_px) = match fs.size {
Some(FontSize::Pt(v)) => (Some(v), None),
Some(FontSize::Px(v)) => (None, Some(v)),
None => (None, None),
};
FontSpecRaw {
family: fs.family,
size_pt,
size_px,
weight: fs.weight,
style: fs.style,
color: fs.color,
}
}
}
impl_merge!(FontSpec {
option { family, size, weight, style, color }
});
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ResolvedFontSpec {
pub family: Arc<str>,
pub size: f32,
#[serde(default)]
pub defined_size: Option<FontSize>,
pub weight: u16,
pub style: FontStyle,
pub color: Rgba,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, ThemeFields)]
#[serde(try_from = "TextScaleEntryRaw", into = "TextScaleEntryRaw")]
#[theme_layer(fields = "size_pt, size_px, weight, line_height_pt, line_height_px")]
pub struct TextScaleEntry {
pub size: Option<FontSize>,
pub weight: Option<u16>,
pub line_height: Option<FontSize>,
}
#[serde_with::skip_serializing_none]
#[derive(Default, Serialize, Deserialize)]
#[serde(default)]
struct TextScaleEntryRaw {
size_pt: Option<f32>,
size_px: Option<f32>,
weight: Option<u16>,
line_height_pt: Option<f32>,
line_height_px: Option<f32>,
}
impl TryFrom<TextScaleEntryRaw> for TextScaleEntry {
type Error = String;
fn try_from(raw: TextScaleEntryRaw) -> Result<Self, Self::Error> {
let size = match (raw.size_pt, raw.size_px) {
(Some(v), None) => Some(FontSize::Pt(v)),
(None, Some(v)) => Some(FontSize::Px(v)),
(None, None) => None,
(Some(_), Some(_)) => {
return Err("text_scale: set `size_pt` or `size_px`, not both".into());
}
};
let line_height = match (raw.line_height_pt, raw.line_height_px) {
(Some(v), None) => Some(FontSize::Pt(v)),
(None, Some(v)) => Some(FontSize::Px(v)),
(None, None) => None,
(Some(_), Some(_)) => {
return Err(
"text_scale: set `line_height_pt` or `line_height_px`, not both".into(),
);
}
};
if let (Some(s), Some(lh)) = (&size, &line_height)
&& s.is_pt() != lh.is_pt()
{
return Err(
"text_scale: size and line_height must use the same unit suffix (_pt or _px)"
.into(),
);
}
Ok(TextScaleEntry {
size,
weight: raw.weight,
line_height,
})
}
}
impl From<TextScaleEntry> for TextScaleEntryRaw {
fn from(e: TextScaleEntry) -> Self {
let (size_pt, size_px) = match e.size {
Some(FontSize::Pt(v)) => (Some(v), None),
Some(FontSize::Px(v)) => (None, Some(v)),
None => (None, None),
};
let (line_height_pt, line_height_px) = match e.line_height {
Some(FontSize::Pt(v)) => (Some(v), None),
Some(FontSize::Px(v)) => (None, Some(v)),
None => (None, None),
};
TextScaleEntryRaw {
size_pt,
size_px,
weight: e.weight,
line_height_pt,
line_height_px,
}
}
}
impl_merge!(TextScaleEntry {
option { size, weight, line_height }
});
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, ThemeFields)]
#[serde(default)]
pub struct TextScale {
pub caption: Option<TextScaleEntry>,
pub section_heading: Option<TextScaleEntry>,
pub dialog_title: Option<TextScaleEntry>,
pub display: Option<TextScaleEntry>,
}
impl_merge!(TextScale {
optional_nested { caption, section_heading, dialog_title, display }
});
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::FontSize;
use super::*;
#[test]
fn font_spec_default_is_empty() {
assert!(FontSpec::default().is_empty());
}
#[test]
fn font_spec_not_empty_when_family_set() {
let fs = FontSpec {
family: Some("Inter".into()),
..Default::default()
};
assert!(!fs.is_empty());
}
#[test]
fn font_spec_not_empty_when_size_set() {
let fs = FontSpec {
size: Some(FontSize::Px(14.0)),
..Default::default()
};
assert!(!fs.is_empty());
}
#[test]
fn font_spec_not_empty_when_weight_set() {
let fs = FontSpec {
weight: Some(700),
..Default::default()
};
assert!(!fs.is_empty());
}
#[test]
fn font_spec_toml_round_trip() {
let fs = FontSpec {
family: Some("Inter".into()),
size: Some(FontSize::Px(14.0)),
weight: Some(400),
..Default::default()
};
let toml_str = toml::to_string(&fs).unwrap();
let deserialized: FontSpec = toml::from_str(&toml_str).unwrap();
assert_eq!(deserialized, fs);
}
#[test]
fn font_spec_toml_round_trip_partial() {
let fs = FontSpec {
family: Some("Inter".into()),
size: None,
weight: None,
..Default::default()
};
let toml_str = toml::to_string(&fs).unwrap();
let deserialized: FontSpec = toml::from_str(&toml_str).unwrap();
assert_eq!(deserialized, fs);
assert!(deserialized.size.is_none());
assert!(deserialized.weight.is_none());
}
#[test]
fn font_spec_merge_overlay_family_replaces_base() {
let mut base = FontSpec {
family: Some("Noto Sans".into()),
size: Some(FontSize::Px(12.0)),
weight: None,
..Default::default()
};
let overlay = FontSpec {
family: Some("Inter".into()),
size: None,
weight: None,
..Default::default()
};
base.merge(&overlay);
assert_eq!(base.family.as_deref(), Some("Inter"));
assert_eq!(base.size, Some(FontSize::Px(12.0)));
}
#[test]
fn font_spec_merge_none_preserves_base() {
let mut base = FontSpec {
family: Some("Noto Sans".into()),
size: Some(FontSize::Px(12.0)),
weight: Some(400),
..Default::default()
};
let overlay = FontSpec::default();
base.merge(&overlay);
assert_eq!(base.family.as_deref(), Some("Noto Sans"));
assert_eq!(base.size, Some(FontSize::Px(12.0)));
assert_eq!(base.weight, Some(400));
}
#[test]
fn text_scale_entry_default_is_empty() {
assert!(TextScaleEntry::default().is_empty());
}
#[test]
fn text_scale_entry_toml_round_trip() {
let entry = TextScaleEntry {
size: Some(FontSize::Px(12.0)),
weight: Some(400),
line_height: Some(FontSize::Px(1.4)),
};
let toml_str = toml::to_string(&entry).unwrap();
let deserialized: TextScaleEntry = toml::from_str(&toml_str).unwrap();
assert_eq!(deserialized, entry);
}
#[test]
fn text_scale_entry_merge_overlay_wins() {
let mut base = TextScaleEntry {
size: Some(FontSize::Px(12.0)),
weight: Some(400),
line_height: None,
};
let overlay = TextScaleEntry {
size: None,
weight: Some(700),
line_height: Some(FontSize::Px(1.5)),
};
base.merge(&overlay);
assert_eq!(base.size, Some(FontSize::Px(12.0))); assert_eq!(base.weight, Some(700)); assert_eq!(base.line_height, Some(FontSize::Px(1.5))); }
#[test]
fn text_scale_default_is_empty() {
assert!(TextScale::default().is_empty());
}
#[test]
fn text_scale_not_empty_when_entry_set() {
let ts = TextScale {
caption: Some(TextScaleEntry {
size: Some(FontSize::Px(11.0)),
..Default::default()
}),
..Default::default()
};
assert!(!ts.is_empty());
}
#[test]
fn text_scale_toml_round_trip() {
let ts = TextScale {
caption: Some(TextScaleEntry {
size: Some(FontSize::Px(11.0)),
weight: Some(400),
line_height: Some(FontSize::Px(1.3)),
}),
section_heading: Some(TextScaleEntry {
size: Some(FontSize::Px(14.0)),
weight: Some(600),
line_height: Some(FontSize::Px(1.4)),
}),
dialog_title: Some(TextScaleEntry {
size: Some(FontSize::Px(16.0)),
weight: Some(700),
line_height: Some(FontSize::Px(1.2)),
}),
display: Some(TextScaleEntry {
size: Some(FontSize::Px(24.0)),
weight: Some(300),
line_height: Some(FontSize::Px(1.1)),
}),
};
let toml_str = toml::to_string(&ts).unwrap();
let deserialized: TextScale = toml::from_str(&toml_str).unwrap();
assert_eq!(deserialized, ts);
}
#[test]
fn text_scale_merge_some_plus_some_merges_inner() {
let mut base = TextScale {
caption: Some(TextScaleEntry {
size: Some(FontSize::Px(11.0)),
weight: Some(400),
line_height: None,
}),
..Default::default()
};
let overlay = TextScale {
caption: Some(TextScaleEntry {
size: None,
weight: Some(600),
line_height: Some(FontSize::Px(1.3)),
}),
..Default::default()
};
base.merge(&overlay);
let cap = base.caption.as_ref().unwrap();
assert_eq!(cap.size, Some(FontSize::Px(11.0))); assert_eq!(cap.weight, Some(600)); assert_eq!(cap.line_height, Some(FontSize::Px(1.3))); }
#[test]
fn text_scale_merge_none_plus_some_clones_overlay() {
let mut base = TextScale::default();
let overlay = TextScale {
section_heading: Some(TextScaleEntry {
size: Some(FontSize::Px(14.0)),
..Default::default()
}),
..Default::default()
};
base.merge(&overlay);
assert!(base.section_heading.is_some());
assert_eq!(base.section_heading.unwrap().size, Some(FontSize::Px(14.0)));
}
#[test]
fn text_scale_merge_none_preserves_base_entry() {
let mut base = TextScale {
display: Some(TextScaleEntry {
size: Some(FontSize::Px(24.0)),
..Default::default()
}),
..Default::default()
};
let overlay = TextScale::default();
base.merge(&overlay);
assert!(base.display.is_some());
assert_eq!(base.display.unwrap().size, Some(FontSize::Px(24.0)));
}
#[test]
fn resolved_font_clone_shares_family_arc() {
let font = ResolvedFontSpec {
family: Arc::from("Inter"),
size: 14.0,
defined_size: Some(FontSize::Px(14.0)),
weight: 400,
style: FontStyle::Normal,
color: crate::Rgba::rgb(0, 0, 0),
};
let cloned = font.clone();
assert!(std::ptr::eq(
font.family.as_ref() as *const str,
cloned.family.as_ref() as *const str,
));
}
#[test]
fn font_style_default_is_normal() {
assert_eq!(FontStyle::default(), FontStyle::Normal);
}
#[test]
fn font_style_serde_round_trip() {
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct Wrapper {
style: FontStyle,
}
for (variant, expected_str) in [
(FontStyle::Normal, "normal"),
(FontStyle::Italic, "italic"),
(FontStyle::Oblique, "oblique"),
] {
let original = Wrapper { style: variant };
let serialized = toml::to_string(&original).unwrap();
assert!(serialized.contains(expected_str), "got: {serialized}");
let deserialized: Wrapper = toml::from_str(&serialized).unwrap();
assert_eq!(deserialized, original);
}
}
#[test]
fn font_spec_with_style_and_color_round_trip() {
let fs = FontSpec {
family: Some("Inter".into()),
size: Some(FontSize::Px(14.0)),
weight: Some(400),
style: Some(FontStyle::Italic),
color: Some(crate::Rgba::rgb(255, 0, 0)),
};
let toml_str = toml::to_string(&fs).unwrap();
let deserialized: FontSpec = toml::from_str(&toml_str).unwrap();
assert_eq!(deserialized, fs);
}
#[test]
fn font_spec_style_none_preserved() {
let fs = FontSpec {
family: Some("Inter".into()),
style: None,
..Default::default()
};
let toml_str = toml::to_string(&fs).unwrap();
let deserialized: FontSpec = toml::from_str(&toml_str).unwrap();
assert!(deserialized.style.is_none());
}
#[test]
fn font_spec_merge_includes_style_and_color() {
let mut base = FontSpec {
family: Some("Noto Sans".into()),
style: Some(FontStyle::Normal),
color: Some(crate::Rgba::rgb(0, 0, 0)),
..Default::default()
};
let overlay = FontSpec {
style: Some(FontStyle::Italic),
..Default::default()
};
base.merge(&overlay);
assert_eq!(base.style, Some(FontStyle::Italic)); assert_eq!(base.color, Some(crate::Rgba::rgb(0, 0, 0))); assert_eq!(base.family.as_deref(), Some("Noto Sans")); }
#[test]
fn pt_to_logical_px_at_96_dpi() {
assert_eq!(FontSize::Pt(10.0).to_logical_px(96.0), 10.0 * 96.0 / 72.0);
}
#[test]
fn px_ignores_dpi() {
assert_eq!(FontSize::Px(14.0).to_logical_px(96.0), 14.0);
assert_eq!(FontSize::Px(14.0).to_logical_px(144.0), 14.0);
}
#[test]
fn pt_to_logical_px_at_72_dpi_is_identity() {
assert_eq!(FontSize::Pt(10.0).to_logical_px(72.0), 10.0);
}
#[test]
fn raw_extracts_value() {
assert_eq!(FontSize::Pt(10.0).raw(), 10.0);
assert_eq!(FontSize::Px(14.0).raw(), 14.0);
}
#[test]
fn font_size_default_is_px_zero() {
assert_eq!(FontSize::default(), FontSize::Px(0.0));
}
#[test]
fn fontspec_toml_round_trip_size_pt() {
let fs = FontSpec {
family: Some("Inter".into()),
size: Some(FontSize::Pt(10.0)),
weight: Some(400),
..Default::default()
};
let toml_str = toml::to_string(&fs).expect("serialize");
assert!(
toml_str.contains("size_pt"),
"should contain size_pt: {toml_str}"
);
assert!(
!toml_str.contains("size_px"),
"should not contain size_px: {toml_str}"
);
let deserialized: FontSpec = toml::from_str(&toml_str).expect("deserialize");
assert_eq!(deserialized, fs);
}
#[test]
fn fontspec_toml_round_trip_size_px() {
let fs = FontSpec {
size: Some(FontSize::Px(14.0)),
..Default::default()
};
let toml_str = toml::to_string(&fs).expect("serialize");
assert!(
toml_str.contains("size_px"),
"should contain size_px: {toml_str}"
);
assert!(
!toml_str.contains("size_pt"),
"should not contain size_pt: {toml_str}"
);
let deserialized: FontSpec = toml::from_str(&toml_str).expect("deserialize");
assert_eq!(deserialized, fs);
}
#[test]
fn fontspec_toml_rejects_both_pt_and_px() {
let toml_str = "size_pt = 10.0\nsize_px = 14.0\n";
assert!(toml::from_str::<FontSpec>(toml_str).is_err());
}
#[test]
fn fontspec_toml_rejects_bare_size() {
let toml_str = "size = 10.0\n";
let result: FontSpec = toml::from_str(toml_str).expect("deserialize");
assert!(
result.size.is_none(),
"bare 'size' should not set FontSpec.size"
);
}
#[test]
fn fontspec_toml_no_size_is_valid() {
let fs: FontSpec = toml::from_str(r#"family = "Inter""#).expect("deserialize");
assert!(fs.size.is_none());
}
#[test]
fn text_scale_entry_toml_round_trip_size_pt() {
let entry = TextScaleEntry {
size: Some(FontSize::Pt(9.0)),
weight: Some(400),
line_height: Some(FontSize::Pt(12.6)),
};
let toml_str = toml::to_string(&entry).expect("serialize");
assert!(toml_str.contains("size_pt"));
let deserialized: TextScaleEntry = toml::from_str(&toml_str).expect("deserialize");
assert_eq!(deserialized, entry);
}
#[test]
fn text_scale_entry_toml_round_trip_size_px() {
let entry = TextScaleEntry {
size: Some(FontSize::Px(14.0)),
weight: Some(400),
line_height: Some(FontSize::Px(18.0)),
};
let toml_str = toml::to_string(&entry).expect("serialize");
assert!(toml_str.contains("size_px"));
let deserialized: TextScaleEntry = toml::from_str(&toml_str).expect("deserialize");
assert_eq!(deserialized, entry);
}
}