use std::{collections::HashMap, sync::Arc};
use super::role_name::RoleName;
use crate::layout::text_style::{FontWeight, TextCase, TextStyle};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum TypographyStyle {
Heading,
Title,
Body,
Description,
Caption,
Label,
FieldLabel,
Button,
Link,
Mono,
Heading1,
Heading2,
Heading3,
Heading4,
Heading5,
Heading6,
Prose,
Article,
Extra(RoleName),
}
impl TypographyStyle {
pub fn extra(name: impl AsRef<str>) -> Self {
Self::Extra(RoleName::new(name))
}
pub fn as_str(self) -> &'static str {
match self {
Self::Heading => "heading",
Self::Title => "title",
Self::Body => "body",
Self::Description => "description",
Self::Caption => "caption",
Self::Label => "label",
Self::FieldLabel => "field_label",
Self::Button => "button",
Self::Link => "link",
Self::Mono => "mono",
Self::Heading1 => "heading1",
Self::Heading2 => "heading2",
Self::Heading3 => "heading3",
Self::Heading4 => "heading4",
Self::Heading5 => "heading5",
Self::Heading6 => "heading6",
Self::Prose => "prose",
Self::Article => "article",
Self::Extra(name) => name.as_str(),
}
}
}
impl From<&str> for TypographyStyle {
fn from(name: &str) -> Self {
Self::extra(name)
}
}
impl From<Arc<str>> for TypographyStyle {
fn from(name: Arc<str>) -> Self {
Self::extra(name)
}
}
#[derive(Clone, PartialEq)]
pub struct ThemeTypography {
pub sans: Arc<str>,
pub mono_family: Arc<str>,
pub heading: TextStyle,
pub title: TextStyle,
pub body: TextStyle,
pub description: TextStyle,
pub caption: TextStyle,
pub label: TextStyle,
pub field_label: TextStyle,
pub button: TextStyle,
pub link: TextStyle,
pub mono: TextStyle,
pub heading1: TextStyle,
pub heading2: TextStyle,
pub heading3: TextStyle,
pub heading4: TextStyle,
pub heading5: TextStyle,
pub heading6: TextStyle,
pub prose: TextStyle,
pub article: TextStyle,
pub extra: HashMap<Arc<str>, TextStyle>,
}
impl ThemeTypography {
pub fn new() -> Self {
Self::default()
}
pub fn get(&self, style: impl Into<TypographyStyle>) -> TextStyle {
let style = style.into();
self
.try_get(style)
.unwrap_or_else(|| panic!("typography style not found: {}", style.as_str()))
}
pub fn try_get(&self, style: impl Into<TypographyStyle>) -> Option<TextStyle> {
let style = match style.into() {
TypographyStyle::Heading => &self.heading,
TypographyStyle::Title => &self.title,
TypographyStyle::Body => &self.body,
TypographyStyle::Description => &self.description,
TypographyStyle::Caption => &self.caption,
TypographyStyle::Label => &self.label,
TypographyStyle::FieldLabel => &self.field_label,
TypographyStyle::Button => &self.button,
TypographyStyle::Link => &self.link,
TypographyStyle::Mono => &self.mono,
TypographyStyle::Heading1 => &self.heading1,
TypographyStyle::Heading2 => &self.heading2,
TypographyStyle::Heading3 => &self.heading3,
TypographyStyle::Heading4 => &self.heading4,
TypographyStyle::Heading5 => &self.heading5,
TypographyStyle::Heading6 => &self.heading6,
TypographyStyle::Prose => &self.prose,
TypographyStyle::Article => &self.article,
TypographyStyle::Extra(name) => self.extra.get(name.as_str())?,
};
Some(style.clone())
}
pub fn set(&mut self, style: impl Into<TypographyStyle>, value: TextStyle) {
match style.into() {
TypographyStyle::Heading => self.heading = value,
TypographyStyle::Title => self.title = value,
TypographyStyle::Body => self.body = value,
TypographyStyle::Description => self.description = value,
TypographyStyle::Caption => self.caption = value,
TypographyStyle::Label => self.label = value,
TypographyStyle::FieldLabel => self.field_label = value,
TypographyStyle::Button => self.button = value,
TypographyStyle::Link => self.link = value,
TypographyStyle::Mono => self.mono = value,
TypographyStyle::Heading1 => self.heading1 = value,
TypographyStyle::Heading2 => self.heading2 = value,
TypographyStyle::Heading3 => self.heading3 = value,
TypographyStyle::Heading4 => self.heading4 = value,
TypographyStyle::Heading5 => self.heading5 = value,
TypographyStyle::Heading6 => self.heading6 = value,
TypographyStyle::Prose => self.prose = value,
TypographyStyle::Article => self.article = value,
TypographyStyle::Extra(name) => {
self.extra.insert(Arc::from(name.as_str()), value);
}
}
}
pub fn default_style(&self) -> &TextStyle {
&self.body
}
pub fn set_sans(&mut self, family: impl Into<Arc<str>>) {
let (old, new) = (self.sans.clone(), family.into());
self.replace_family(&old, &new);
self.sans = new;
}
pub fn set_mono(&mut self, family: impl Into<Arc<str>>) {
let (old, new) = (self.mono_family.clone(), family.into());
self.replace_family(&old, &new);
self.mono_family = new;
}
fn replace_family(&mut self, old: &Arc<str>, new: &Arc<str>) {
let styles = [
&mut self.heading,
&mut self.title,
&mut self.body,
&mut self.description,
&mut self.caption,
&mut self.label,
&mut self.field_label,
&mut self.button,
&mut self.link,
&mut self.mono,
&mut self.heading1,
&mut self.heading2,
&mut self.heading3,
&mut self.heading4,
&mut self.heading5,
&mut self.heading6,
&mut self.prose,
&mut self.article,
];
for style in styles.into_iter().chain(self.extra.values_mut()) {
if style.font_family == *old {
style.font_family = new.clone();
}
}
}
pub fn set_default_style(&mut self, style: TextStyle) {
self.body = style;
}
pub fn resolve(&self, style: impl Into<TypographyStyle>) -> TextStyle {
self.get(style)
}
pub fn try_resolve(&self, style: impl Into<TypographyStyle>) -> Option<TextStyle> {
self.try_get(style)
}
pub(crate) fn resolve_or_default(&self, style: TypographyStyle) -> TextStyle {
self.try_get(style).unwrap_or_else(|| self.default_style().clone())
}
}
impl ThemeTypography {
pub fn scaled(&self, scale: f32) -> Self {
let adjust = |style: &TextStyle| {
let mut style = style.clone();
style.font_size *= scale;
style.letter_spacing *= scale;
style
};
Self {
sans: self.sans.clone(),
mono_family: self.mono_family.clone(),
heading: adjust(&self.heading),
title: adjust(&self.title),
body: adjust(&self.body),
description: adjust(&self.description),
caption: adjust(&self.caption),
label: adjust(&self.label),
field_label: adjust(&self.field_label),
button: adjust(&self.button),
link: adjust(&self.link),
mono: adjust(&self.mono),
heading1: adjust(&self.heading1),
heading2: adjust(&self.heading2),
heading3: adjust(&self.heading3),
heading4: adjust(&self.heading4),
heading5: adjust(&self.heading5),
heading6: adjust(&self.heading6),
prose: adjust(&self.prose),
article: adjust(&self.article),
extra: self
.extra
.iter()
.map(|(name, style)| (name.clone(), adjust(style)))
.collect(),
}
}
}
impl Default for ThemeTypography {
fn default() -> Self {
let body = TextStyle {
font_size: 14.0,
..TextStyle::default()
};
let mono_family: Arc<str> = Arc::from("monospace");
Self {
sans: body.font_family.clone(),
mono_family: mono_family.clone(),
heading: TextStyle {
font_size: 24.0,
weight: FontWeight::SemiBold,
letter_spacing: -0.5,
..body.clone()
},
title: TextStyle {
font_size: 20.0,
weight: FontWeight::SemiBold,
letter_spacing: -0.3,
..body.clone()
},
body: body.clone(),
description: body.clone(),
caption: TextStyle {
font_size: 12.0,
..body.clone()
},
label: TextStyle {
font_size: 13.0,
weight: FontWeight::Medium,
..body.clone()
},
field_label: TextStyle {
font_size: 13.0,
weight: FontWeight::Medium,
..body.clone()
},
button: TextStyle {
font_size: 13.0,
weight: FontWeight::Medium,
..body.clone()
},
link: body.clone(),
mono: TextStyle {
font_family: mono_family,
font_size: 13.0,
line_height: 1.3,
..body.clone()
},
..Self::document_roles(&body)
}
}
}
impl ThemeTypography {
fn document_roles(body: &TextStyle) -> Self {
let heading = |size: f32, weight: FontWeight, letter_spacing: f32| TextStyle {
font_size: size,
weight,
letter_spacing,
line_height: 1.2,
..body.clone()
};
Self {
sans: body.font_family.clone(),
mono_family: Arc::from("monospace"),
heading: body.clone(),
title: body.clone(),
body: body.clone(),
description: body.clone(),
caption: body.clone(),
label: body.clone(),
field_label: body.clone(),
button: body.clone(),
link: body.clone(),
mono: body.clone(),
heading1: heading(30.0, FontWeight::Bold, -0.6),
heading2: heading(24.0, FontWeight::SemiBold, -0.4),
heading3: heading(20.0, FontWeight::SemiBold, 0.0),
heading4: heading(16.0, FontWeight::SemiBold, 0.0),
heading5: heading(14.0, FontWeight::SemiBold, 0.0),
heading6: TextStyle {
case: TextCase::Upper,
..heading(12.0, FontWeight::SemiBold, 0.8)
},
prose: TextStyle {
font_size: 14.0,
line_height: 1.6,
..body.clone()
},
article: TextStyle {
font_size: 15.0,
line_height: 1.65,
..body.clone()
},
extra: HashMap::new(),
}
}
}
impl ThemeTypography {
pub fn legacy() -> Self {
let body = TextStyle::default();
let mono_family: Arc<str> = Arc::from("monospace");
Self {
sans: body.font_family.clone(),
mono_family: mono_family.clone(),
heading: TextStyle {
font_size: 24.0,
weight: FontWeight::Bold,
..body.clone()
},
title: TextStyle {
font_size: 20.0,
weight: FontWeight::Bold,
..body.clone()
},
body: body.clone(),
description: TextStyle {
font_size: 14.0,
..body.clone()
},
caption: TextStyle {
font_size: 12.0,
..body.clone()
},
label: TextStyle {
font_size: 13.0,
weight: FontWeight::Medium,
..body.clone()
},
field_label: TextStyle {
font_size: 13.0,
weight: FontWeight::Medium,
..body.clone()
},
button: TextStyle {
font_size: 13.0,
weight: FontWeight::Medium,
..body.clone()
},
link: body.clone(),
mono: TextStyle {
font_family: mono_family,
..body.clone()
},
..Self::document_roles(&body)
}
}
}
#[derive(Clone)]
pub struct ThemeFonts {
pub body: TextStyle,
pub heading: TextStyle,
pub mono: TextStyle,
}
impl From<ThemeTypography> for ThemeFonts {
fn from(typography: ThemeTypography) -> Self {
Self {
body: typography.body,
heading: typography.heading,
mono: typography.mono,
}
}
}
impl From<ThemeFonts> for ThemeTypography {
fn from(fonts: ThemeFonts) -> Self {
Self {
body: fonts.body,
heading: fonts.heading,
mono: fonts.mono,
..Self::default()
}
}
}
impl Default for ThemeFonts {
fn default() -> Self {
ThemeTypography::default().into()
}
}