1use std::sync::{Arc, LazyLock};
2
3use crate::{
4 LayoutPolicy,
5 generated::{BASELINE_TYPE_STYLES, EMPHASIZED_TYPE_STYLES},
6};
7
8#[derive(Clone, Copy, Debug, PartialEq, Eq)]
10pub enum Typeface {
11 Brand,
13 Plain,
15}
16
17#[derive(Clone, Debug, PartialEq, Eq)]
22pub struct FontFamilies {
23 pub primary: Arc<str>,
25 pub fallback: Arc<[Arc<str>]>,
27}
28
29impl FontFamilies {
30 pub fn new(
32 primary: impl Into<Arc<str>>,
33 fallback: impl IntoIterator<Item = impl Into<Arc<str>>>,
34 ) -> Self {
35 Self {
36 primary: primary.into(),
37 fallback: fallback.into_iter().map(Into::into).collect(),
38 }
39 }
40
41 pub fn with_primary(mut self, primary: impl Into<Arc<str>>) -> Self {
43 self.primary = primary.into();
44 self
45 }
46
47 pub fn with_fallback(
49 mut self,
50 fallback: impl IntoIterator<Item = impl Into<Arc<str>>>,
51 ) -> Self {
52 self.fallback = fallback.into_iter().map(Into::into).collect();
53 self
54 }
55}
56
57impl Default for FontFamilies {
58 fn default() -> Self {
59 static DEFAULT: LazyLock<FontFamilies> = LazyLock::new(|| {
60 FontFamilies::new(
61 "Roboto",
62 [
63 "SF Pro Text",
64 "Segoe UI",
65 "Noto Sans",
66 "PingFang SC",
67 "Microsoft YaHei UI",
68 "Noto Sans CJK SC",
69 ],
70 )
71 });
72 DEFAULT.clone()
73 }
74}
75
76#[cfg(feature = "serde")]
77impl serde::Serialize for FontFamilies {
78 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
79 #[derive(serde::Serialize)]
80 #[serde(rename = "FontFamilies")]
81 struct SerializedFontFamilies<'a> {
82 primary: &'a str,
83 fallback: Vec<&'a str>,
84 }
85
86 SerializedFontFamilies {
87 primary: self.primary.as_ref(),
88 fallback: self.fallback.iter().map(Arc::as_ref).collect(),
89 }
90 .serialize(serializer)
91 }
92}
93
94#[cfg(feature = "serde")]
95impl<'de> serde::Deserialize<'de> for FontFamilies {
96 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
97 #[derive(serde::Deserialize)]
98 #[serde(rename = "FontFamilies")]
99 struct SerializedFontFamilies {
100 primary: String,
101 fallback: Vec<String>,
102 }
103
104 let raw = SerializedFontFamilies::deserialize(deserializer)?;
105 Ok(Self::new(raw.primary, raw.fallback))
106 }
107}
108
109#[derive(Clone, Copy, Debug, PartialEq, Eq)]
111pub enum TypeRole {
112 BodyLarge,
114 BodyMedium,
116 BodySmall,
118 DisplayLarge,
120 DisplayMedium,
122 DisplaySmall,
124 HeadlineLarge,
126 HeadlineMedium,
128 HeadlineSmall,
130 LabelLarge,
132 LabelMedium,
134 LabelSmall,
136 TitleLarge,
138 TitleMedium,
140 TitleSmall,
142}
143
144impl TypeRole {
145 pub const ALL: [Self; 15] = [
147 Self::BodyLarge,
148 Self::BodyMedium,
149 Self::BodySmall,
150 Self::DisplayLarge,
151 Self::DisplayMedium,
152 Self::DisplaySmall,
153 Self::HeadlineLarge,
154 Self::HeadlineMedium,
155 Self::HeadlineSmall,
156 Self::LabelLarge,
157 Self::LabelMedium,
158 Self::LabelSmall,
159 Self::TitleLarge,
160 Self::TitleMedium,
161 Self::TitleSmall,
162 ];
163
164 pub const fn style(self, emphasis: TypographyEmphasis) -> TypeStyle {
166 match emphasis {
167 TypographyEmphasis::Baseline => BASELINE_TYPE_STYLES[self as usize],
168 TypographyEmphasis::Emphasized => EMPHASIZED_TYPE_STYLES[self as usize],
169 }
170 }
171}
172
173#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
175pub enum TypographyEmphasis {
176 #[default]
178 Baseline,
179 Emphasized,
181}
182
183#[derive(Clone, Copy, Debug, PartialEq)]
185pub struct TypeStyle {
186 pub typeface: Typeface,
188 pub size: f32,
190 pub line_height: f32,
192 pub tracking: f32,
194 pub weight: u16,
196}
197
198#[derive(Clone, Debug, PartialEq)]
201#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
202pub struct Typography {
203 pub brand: FontFamilies,
205 pub plain: FontFamilies,
207 #[cfg_attr(
208 feature = "serde",
209 serde(
210 default = "default_text_scale",
211 deserialize_with = "deserialize_text_scale"
212 )
213 )]
214 text_scale: f32,
215}
216
217impl Default for Typography {
218 fn default() -> Self {
219 Self {
220 brand: FontFamilies::default(),
221 plain: FontFamilies::default(),
222 text_scale: default_text_scale(),
223 }
224 }
225}
226
227impl Typography {
228 pub fn with_text_scale(mut self, scale: f32) -> Self {
230 self.text_scale = normalize_text_scale(scale);
231 self
232 }
233
234 pub fn text_scale(&self) -> f32 {
236 self.text_scale
237 }
238
239 pub fn style(&self, role: TypeRole, emphasis: TypographyEmphasis) -> TypeStyle {
241 self.scale_style(role.style(emphasis))
242 }
243
244 pub fn scale_style(&self, style: TypeStyle) -> TypeStyle {
246 LayoutPolicy::scale_text(style, self.text_scale)
247 }
248
249 pub fn families(&self, typeface: Typeface) -> &FontFamilies {
251 match typeface {
252 Typeface::Brand => &self.brand,
253 Typeface::Plain => &self.plain,
254 }
255 }
256}
257
258fn default_text_scale() -> f32 {
259 1.0
260}
261
262fn normalize_text_scale(scale: f32) -> f32 {
263 if scale.is_nan() {
264 default_text_scale()
265 } else {
266 scale.clamp(1.0, 2.0)
267 }
268}
269
270#[cfg(feature = "serde")]
271fn deserialize_text_scale<'de, D: serde::Deserializer<'de>>(
272 deserializer: D,
273) -> Result<f32, D::Error> {
274 <f32 as serde::Deserialize>::deserialize(deserializer).map(normalize_text_scale)
275}