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