use std::sync::{Arc, LazyLock};
use crate::{
LayoutPolicy,
generated::{BASELINE_TYPE_STYLES, EMPHASIZED_TYPE_STYLES},
};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Typeface {
Brand,
Plain,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FontFamilies {
pub primary: Arc<str>,
pub fallback: Arc<[Arc<str>]>,
}
impl FontFamilies {
pub fn new(
primary: impl Into<Arc<str>>,
fallback: impl IntoIterator<Item = impl Into<Arc<str>>>,
) -> Self {
Self {
primary: primary.into(),
fallback: fallback.into_iter().map(Into::into).collect(),
}
}
pub fn with_primary(mut self, primary: impl Into<Arc<str>>) -> Self {
self.primary = primary.into();
self
}
pub fn with_fallback(
mut self,
fallback: impl IntoIterator<Item = impl Into<Arc<str>>>,
) -> Self {
self.fallback = fallback.into_iter().map(Into::into).collect();
self
}
}
impl Default for FontFamilies {
fn default() -> Self {
static DEFAULT: LazyLock<FontFamilies> = LazyLock::new(|| {
FontFamilies::new(
"Roboto",
[
"SF Pro Text",
"Segoe UI",
"Noto Sans",
"PingFang SC",
"Microsoft YaHei UI",
"Noto Sans CJK SC",
],
)
});
DEFAULT.clone()
}
}
#[cfg(feature = "serde")]
impl serde::Serialize for FontFamilies {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
#[derive(serde::Serialize)]
#[serde(rename = "FontFamilies")]
struct SerializedFontFamilies<'a> {
primary: &'a str,
fallback: Vec<&'a str>,
}
SerializedFontFamilies {
primary: self.primary.as_ref(),
fallback: self.fallback.iter().map(Arc::as_ref).collect(),
}
.serialize(serializer)
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for FontFamilies {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
#[derive(serde::Deserialize)]
#[serde(rename = "FontFamilies")]
struct SerializedFontFamilies {
primary: String,
fallback: Vec<String>,
}
let raw = SerializedFontFamilies::deserialize(deserializer)?;
Ok(Self::new(raw.primary, raw.fallback))
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TypeRole {
BodyLarge,
BodyMedium,
BodySmall,
DisplayLarge,
DisplayMedium,
DisplaySmall,
HeadlineLarge,
HeadlineMedium,
HeadlineSmall,
LabelLarge,
LabelMedium,
LabelSmall,
TitleLarge,
TitleMedium,
TitleSmall,
}
impl TypeRole {
pub const ALL: [Self; 15] = [
Self::BodyLarge,
Self::BodyMedium,
Self::BodySmall,
Self::DisplayLarge,
Self::DisplayMedium,
Self::DisplaySmall,
Self::HeadlineLarge,
Self::HeadlineMedium,
Self::HeadlineSmall,
Self::LabelLarge,
Self::LabelMedium,
Self::LabelSmall,
Self::TitleLarge,
Self::TitleMedium,
Self::TitleSmall,
];
pub const fn style(self, emphasis: TypographyEmphasis) -> TypeStyle {
match emphasis {
TypographyEmphasis::Baseline => BASELINE_TYPE_STYLES[self as usize],
TypographyEmphasis::Emphasized => EMPHASIZED_TYPE_STYLES[self as usize],
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TypographyEmphasis {
#[default]
Baseline,
Emphasized,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct TypeStyle {
pub typeface: Typeface,
pub size: f32,
pub line_height: f32,
pub tracking: f32,
pub weight: u16,
}
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Typography {
pub brand: FontFamilies,
pub plain: FontFamilies,
#[cfg_attr(
feature = "serde",
serde(
default = "default_text_scale",
deserialize_with = "deserialize_text_scale"
)
)]
text_scale: f32,
}
impl Default for Typography {
fn default() -> Self {
Self {
brand: FontFamilies::default(),
plain: FontFamilies::default(),
text_scale: default_text_scale(),
}
}
}
impl Typography {
pub fn with_text_scale(mut self, scale: f32) -> Self {
self.text_scale = normalize_text_scale(scale);
self
}
pub fn text_scale(&self) -> f32 {
self.text_scale
}
pub fn style(&self, role: TypeRole, emphasis: TypographyEmphasis) -> TypeStyle {
self.scale_style(role.style(emphasis))
}
pub fn scale_style(&self, style: TypeStyle) -> TypeStyle {
LayoutPolicy::scale_text(style, self.text_scale)
}
pub fn families(&self, typeface: Typeface) -> &FontFamilies {
match typeface {
Typeface::Brand => &self.brand,
Typeface::Plain => &self.plain,
}
}
}
fn default_text_scale() -> f32 {
1.0
}
fn normalize_text_scale(scale: f32) -> f32 {
if scale.is_nan() {
default_text_scale()
} else {
scale.clamp(1.0, 2.0)
}
}
#[cfg(feature = "serde")]
fn deserialize_text_scale<'de, D: serde::Deserializer<'de>>(
deserializer: D,
) -> Result<f32, D::Error> {
<f32 as serde::Deserialize>::deserialize(deserializer).map(normalize_text_scale)
}