1use crate::{
2 LayoutPolicy,
3 generated::{BASELINE_TYPE_STYLES, EMPHASIZED_TYPE_STYLES},
4};
5
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8pub enum Typeface {
9 Brand,
10 Plain,
11}
12
13#[derive(Clone, Debug, PartialEq, Eq)]
15#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
16pub struct FontFamilies {
17 pub primary: String,
18 pub fallback: Vec<String>,
19}
20
21impl Default for FontFamilies {
22 fn default() -> Self {
23 Self {
24 primary: "Roboto".into(),
25 fallback: ["Segoe UI", "Microsoft YaHei UI", "Noto Sans CJK SC"]
26 .map(str::to_owned)
27 .into(),
28 }
29 }
30}
31
32#[derive(Clone, Copy, Debug, PartialEq, Eq)]
34pub enum TypeRole {
35 BodyLarge,
36 BodyMedium,
37 BodySmall,
38 DisplayLarge,
39 DisplayMedium,
40 DisplaySmall,
41 HeadlineLarge,
42 HeadlineMedium,
43 HeadlineSmall,
44 LabelLarge,
45 LabelMedium,
46 LabelSmall,
47 TitleLarge,
48 TitleMedium,
49 TitleSmall,
50}
51
52impl TypeRole {
53 pub const ALL: [Self; 15] = [
54 Self::BodyLarge,
55 Self::BodyMedium,
56 Self::BodySmall,
57 Self::DisplayLarge,
58 Self::DisplayMedium,
59 Self::DisplaySmall,
60 Self::HeadlineLarge,
61 Self::HeadlineMedium,
62 Self::HeadlineSmall,
63 Self::LabelLarge,
64 Self::LabelMedium,
65 Self::LabelSmall,
66 Self::TitleLarge,
67 Self::TitleMedium,
68 Self::TitleSmall,
69 ];
70
71 pub const fn style(self, emphasis: TypographyEmphasis) -> TypeStyle {
72 match emphasis {
73 TypographyEmphasis::Baseline => BASELINE_TYPE_STYLES[self as usize],
74 TypographyEmphasis::Emphasized => EMPHASIZED_TYPE_STYLES[self as usize],
75 }
76 }
77}
78
79#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
80pub enum TypographyEmphasis {
81 #[default]
82 Baseline,
83 Emphasized,
84}
85
86#[derive(Clone, Copy, Debug, PartialEq)]
88pub struct TypeStyle {
89 pub typeface: Typeface,
90 pub size: f32,
91 pub line_height: f32,
92 pub tracking: f32,
93 pub weight: u16,
94}
95
96#[derive(Clone, Debug, PartialEq)]
99#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
100pub struct Typography {
101 pub brand: FontFamilies,
102 pub plain: FontFamilies,
103 #[cfg_attr(
104 feature = "serde",
105 serde(
106 default = "default_text_scale",
107 deserialize_with = "deserialize_text_scale"
108 )
109 )]
110 text_scale: f32,
111}
112
113impl Default for Typography {
114 fn default() -> Self {
115 Self {
116 brand: FontFamilies::default(),
117 plain: FontFamilies::default(),
118 text_scale: default_text_scale(),
119 }
120 }
121}
122
123impl Typography {
124 pub fn with_text_scale(mut self, scale: f32) -> Self {
126 self.text_scale = normalize_text_scale(scale);
127 self
128 }
129
130 pub fn text_scale(&self) -> f32 {
131 self.text_scale
132 }
133
134 pub fn style(&self, role: TypeRole, emphasis: TypographyEmphasis) -> TypeStyle {
136 self.scale_style(role.style(emphasis))
137 }
138
139 pub fn scale_style(&self, style: TypeStyle) -> TypeStyle {
141 LayoutPolicy::scale_text(style, self.text_scale)
142 }
143
144 pub fn families(&self, typeface: Typeface) -> &FontFamilies {
145 match typeface {
146 Typeface::Brand => &self.brand,
147 Typeface::Plain => &self.plain,
148 }
149 }
150}
151
152fn default_text_scale() -> f32 {
153 1.0
154}
155
156fn normalize_text_scale(scale: f32) -> f32 {
157 if scale.is_nan() {
158 default_text_scale()
159 } else {
160 scale.clamp(1.0, 2.0)
161 }
162}
163
164#[cfg(feature = "serde")]
165fn deserialize_text_scale<'de, D: serde::Deserializer<'de>>(
166 deserializer: D,
167) -> Result<f32, D::Error> {
168 <f32 as serde::Deserialize>::deserialize(deserializer).map(normalize_text_scale)
169}