1#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9pub struct FontWeight(pub u16);
10
11impl FontWeight {
12 pub const THIN: Self = Self(100);
13 pub const EXTRA_LIGHT: Self = Self(200);
14 pub const LIGHT: Self = Self(300);
15 pub const NORMAL: Self = Self(400);
16 pub const MEDIUM: Self = Self(500);
17 pub const SEMIBOLD: Self = Self(600);
18 pub const BOLD: Self = Self(700);
19 pub const EXTRA_BOLD: Self = Self(800);
20 pub const BLACK: Self = Self(900);
21
22 pub const fn new(value: u16) -> Self {
23 Self(value)
24 }
25
26 pub const fn value(self) -> u16 {
27 self.0
28 }
29}
30
31impl Default for FontWeight {
32 fn default() -> Self {
33 Self::NORMAL
34 }
35}
36
37#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
39pub enum FontStyle {
40 #[default]
41 Normal,
42 Italic,
43 Oblique,
44}
45
46#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
48pub enum FontStretch {
49 UltraCondensed,
50 ExtraCondensed,
51 Condensed,
52 SemiCondensed,
53 #[default]
54 Normal,
55 SemiExpanded,
56 Expanded,
57 ExtraExpanded,
58 UltraExpanded,
59}
60
61#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
64pub struct FontFeature {
65 pub tag: [u8; 4],
66 pub value: u32,
67}
68
69impl FontFeature {
70 pub const KERNING: [u8; 4] = *b"kern";
71 pub const STANDARD_LIGATURES: [u8; 4] = *b"liga";
72 pub const CONTEXTUAL_LIGATURES: [u8; 4] = *b"clig";
73 pub const DISCRETIONARY_LIGATURES: [u8; 4] = *b"dlig";
74 pub const SMALL_CAPS: [u8; 4] = *b"smcp";
75 pub const CAPITALS_TO_SMALL_CAPS: [u8; 4] = *b"c2sc";
76 pub const TABULAR_FIGURES: [u8; 4] = *b"tnum";
77 pub const OLDSTYLE_FIGURES: [u8; 4] = *b"onum";
78 pub const FRACTIONS: [u8; 4] = *b"frac";
79 pub const SLASHED_ZERO: [u8; 4] = *b"zero";
80
81 pub const fn stylistic_set(n: u8) -> [u8; 4] {
84 let n = if n < 1 {
85 1
86 } else if n > 20 {
87 20
88 } else {
89 n
90 };
91 let tens = b'0' + (n / 10);
92 let ones = b'0' + (n % 10);
93 [b's', b's', tens, ones]
94 }
95
96 pub const fn on(tag: [u8; 4]) -> Self {
97 Self { tag, value: 1 }
98 }
99
100 pub const fn off(tag: [u8; 4]) -> Self {
101 Self { tag, value: 0 }
102 }
103
104 pub const fn set(tag: [u8; 4], value: u32) -> Self {
105 Self { tag, value }
106 }
107}
108
109#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
112pub struct FontFeatures {
113 features: Vec<FontFeature>,
114}
115
116impl FontFeatures {
117 pub const fn new() -> Self {
118 Self {
119 features: Vec::new(),
120 }
121 }
122
123 pub fn enable(&mut self, tag: [u8; 4]) -> &mut Self {
125 self.features.push(FontFeature::on(tag));
126 self
127 }
128
129 pub fn disable(&mut self, tag: [u8; 4]) -> &mut Self {
131 self.features.push(FontFeature::off(tag));
132 self
133 }
134
135 pub fn set(&mut self, tag: [u8; 4], value: u32) -> &mut Self {
137 self.features.push(FontFeature::set(tag, value));
138 self
139 }
140
141 pub fn is_empty(&self) -> bool {
142 self.features.is_empty()
143 }
144
145 pub fn len(&self) -> usize {
146 self.features.len()
147 }
148
149 pub fn iter(&self) -> impl Iterator<Item = &FontFeature> {
150 self.features.iter()
151 }
152}
153
154impl FromIterator<FontFeature> for FontFeatures {
155 fn from_iter<I: IntoIterator<Item = FontFeature>>(iter: I) -> Self {
156 Self {
157 features: iter.into_iter().collect(),
158 }
159 }
160}
161
162#[cfg(test)]
163mod tests {
164 use super::*;
165
166 #[test]
167 fn weight_named_values_and_default() {
168 assert_eq!(FontWeight::NORMAL.value(), 400);
169 assert_eq!(FontWeight::BOLD.value(), 700);
170 assert_eq!(FontWeight::THIN.value(), 100);
171 assert_eq!(FontWeight::BLACK.value(), 900);
172 assert_eq!(FontWeight::new(550).value(), 550);
173 assert_eq!(FontWeight::default(), FontWeight::NORMAL);
174 }
175
176 #[test]
177 fn style_and_stretch_defaults_are_normal() {
178 assert_eq!(FontStyle::default(), FontStyle::Normal);
179 assert_eq!(FontStretch::default(), FontStretch::Normal);
180 }
181
182 #[test]
183 fn feature_tag_constants_and_constructors() {
184 assert_eq!(FontFeature::STANDARD_LIGATURES, *b"liga");
185 assert_eq!(FontFeature::SMALL_CAPS, *b"smcp");
186 assert_eq!(FontFeature::stylistic_set(1), *b"ss01");
187 assert_eq!(FontFeature::stylistic_set(12), *b"ss12");
188 assert_eq!(
189 FontFeature::on(*b"liga"),
190 FontFeature {
191 tag: *b"liga",
192 value: 1
193 }
194 );
195 assert_eq!(
196 FontFeature::off(*b"liga"),
197 FontFeature {
198 tag: *b"liga",
199 value: 0
200 }
201 );
202 assert_eq!(FontFeature::set(*b"aalt", 3).value, 3);
203 }
204
205 #[test]
206 fn feature_collection_records_settings_in_order() {
207 let mut features = FontFeatures::new();
208 assert!(features.is_empty());
209 features
210 .disable(FontFeature::STANDARD_LIGATURES)
211 .enable(FontFeature::SMALL_CAPS)
212 .set(FontFeature::TABULAR_FIGURES, 1);
213
214 let collected: Vec<_> = features.iter().copied().collect();
215 assert_eq!(
216 collected,
217 vec![
218 FontFeature::off(*b"liga"),
219 FontFeature::on(*b"smcp"),
220 FontFeature::set(*b"tnum", 1),
221 ]
222 );
223 assert_eq!(features.len(), 3);
224 assert!(!features.is_empty());
225 }
226
227 #[test]
228 fn features_equality_is_order_sensitive() {
229 let a: FontFeatures = [FontFeature::on(*b"liga"), FontFeature::on(*b"smcp")]
230 .into_iter()
231 .collect();
232 let b: FontFeatures = [FontFeature::on(*b"smcp"), FontFeature::on(*b"liga")]
233 .into_iter()
234 .collect();
235 assert_ne!(a, b);
236
237 let c: FontFeatures = [FontFeature::on(*b"liga"), FontFeature::on(*b"smcp")]
238 .into_iter()
239 .collect();
240 assert_eq!(a, c);
241 }
242}