ori_core/text/
attributes.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
use std::hash::{Hash, Hasher};

use smol_str::SmolStr;

use crate::canvas::Color;

/// A font family, by default [`FontFamily::SansSerif`].
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub enum FontFamily {
    /// A font family by name, e.g. "Arial".
    Name(SmolStr),

    /// A serif font family.
    Serif,

    /// A sans-serif font family.
    #[default]
    SansSerif,

    /// A monospace font family.
    Monospace,

    /// A cursive font family.
    Cursive,

    /// A fantasy font family.
    Fantasy,
}

impl From<&str> for FontFamily {
    fn from(name: &str) -> Self {
        Self::Name(SmolStr::new(name))
    }
}

impl From<String> for FontFamily {
    fn from(name: String) -> Self {
        Self::Name(SmolStr::new(name))
    }
}

impl From<SmolStr> for FontFamily {
    fn from(name: SmolStr) -> Self {
        Self::Name(name)
    }
}

/// A font weight.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct FontWeight(pub u16);

impl FontWeight {
    /// Thin font weight (100), the thinnest possible.
    pub const THIN: Self = Self(100);
    /// Extra-light font weight (200).
    pub const EXTRA_LIGHT: Self = Self(200);
    /// Light font weight (300).
    pub const LIGHT: Self = Self(300);
    /// Normal font weight (400), the default.
    pub const NORMAL: Self = Self(400);
    /// Medium font weight (500).
    pub const MEDIUM: Self = Self(500);
    /// Semi-bold font weight (600).
    pub const SEMI_BOLD: Self = Self(600);
    /// Bold font weight (700).
    pub const BOLD: Self = Self(700);
    /// Extra-bold font weight (800).
    pub const EXTRA_BOLD: Self = Self(800);
    /// Black font weight (900), the boldest possible.
    pub const BLACK: Self = Self(900);
}

impl Default for FontWeight {
    fn default() -> Self {
        Self::NORMAL
    }
}

impl From<u16> for FontWeight {
    fn from(weight: u16) -> Self {
        Self(weight)
    }
}

impl From<&str> for FontWeight {
    fn from(weight: &str) -> Self {
        match weight {
            "thin" => Self::THIN,
            "extra-light" => Self::EXTRA_LIGHT,
            "light" => Self::LIGHT,
            "normal" => Self::NORMAL,
            "medium" => Self::MEDIUM,
            "semi-bold" => Self::SEMI_BOLD,
            "bold" => Self::BOLD,
            "extra-bold" => Self::EXTRA_BOLD,
            "black" => Self::BLACK,
            _ => Self::NORMAL,
        }
    }
}

impl From<String> for FontWeight {
    fn from(weight: String) -> Self {
        Self::from(weight.as_str())
    }
}

/// A font stretch.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum FontStretch {
    /// Ultra-condensed font stretch.
    UltraCondensed,

    /// Extra-condensed font stretch.
    ExtraCondensed,

    /// Condensed font stretch.
    Condensed,

    /// Semi-condensed font stretch.
    SemiCondensed,

    /// Normal font stretch, the default.
    #[default]
    Normal,

    /// Semi-expanded font stretch.
    SemiExpanded,

    /// Expanded font stretch.
    Expanded,

    /// Extra-expanded font stretch.
    ExtraExpanded,

    /// Ultra-expanded font stretch.
    UltraExpanded,
}

impl From<&str> for FontStretch {
    fn from(stretch: &str) -> Self {
        match stretch {
            "ultra-condensed" => Self::UltraCondensed,
            "extra-condensed" => Self::ExtraCondensed,
            "condensed" => Self::Condensed,
            "semi-condensed" => Self::SemiCondensed,
            "normal" => Self::Normal,
            "semi-expanded" => Self::SemiExpanded,
            "expanded" => Self::Expanded,
            "extra-expanded" => Self::ExtraExpanded,
            "ultra-expanded" => Self::UltraExpanded,
            _ => Self::Normal,
        }
    }
}

impl From<String> for FontStretch {
    fn from(stretch: String) -> Self {
        Self::from(stretch.as_str())
    }
}

/// A font style.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum FontStyle {
    /// Normal font style, the default.
    #[default]
    Normal,

    /// Italic font style.
    Italic,

    /// Oblique font style.
    Oblique,
}

impl From<&str> for FontStyle {
    fn from(style: &str) -> Self {
        match style {
            "normal" => Self::Normal,
            "italic" => Self::Italic,
            "oblique" => Self::Oblique,
            _ => Self::Normal,
        }
    }
}

impl From<String> for FontStyle {
    fn from(style: String) -> Self {
        Self::from(style.as_str())
    }
}

/// Alignment of a section of text.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum TextAlign {
    /// Align text at the start.
    #[default]
    Start,

    /// Align text in the center.
    Center,

    /// Align text at the end.
    End,
}

#[allow(non_upper_case_globals, missing_docs)]
impl TextAlign {
    pub const Left: Self = Self::Start;
    pub const Top: Self = Self::Start;
    pub const Middle: Self = Self::Center;
    pub const Right: Self = Self::End;
    pub const Bottom: Self = Self::End;
}

impl From<&str> for TextAlign {
    fn from(align: &str) -> Self {
        match align {
            "start" => Self::Start,
            "center" => Self::Center,
            "end" => Self::End,
            "left" => Self::Start,
            "top" => Self::Start,
            "middle" => Self::Center,
            "right" => Self::End,
            "bottom" => Self::End,
            _ => Self::Start,
        }
    }
}

impl From<String> for TextAlign {
    fn from(align: String) -> Self {
        Self::from(align.as_str())
    }
}

/// Wrapping of a section of text.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum TextWrap {
    /// Do not wrap text.
    None,

    /// Wrap text at the word boundary.
    #[default]
    Word,
}

impl From<&str> for TextWrap {
    fn from(wrap: &str) -> Self {
        match wrap {
            "none" => Self::None,
            "word" => Self::Word,
            _ => Self::Word,
        }
    }
}

impl From<String> for TextWrap {
    fn from(wrap: String) -> Self {
        Self::from(wrap.as_str())
    }
}

/// Attributes of a section of text.
#[derive(Clone, Debug, PartialEq)]
pub struct FontAttributes {
    /// The font size of the font.
    pub size: f32,

    /// The font family of the font.
    pub family: FontFamily,

    /// The font stretch of the font.
    pub stretch: FontStretch,

    /// The font weight of the font.
    pub weight: FontWeight,

    /// The font style of the font.
    pub style: FontStyle,

    /// Whether to use ligatures.
    pub ligatures: bool,

    /// The color of the font.
    pub color: Color,
}

impl Default for FontAttributes {
    fn default() -> Self {
        Self {
            size: 16.0,
            family: FontFamily::SansSerif,
            stretch: FontStretch::Normal,
            weight: FontWeight::NORMAL,
            style: FontStyle::Normal,
            ligatures: true,
            color: Color::BLACK,
        }
    }
}

impl Eq for FontAttributes {}

impl Hash for FontAttributes {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.size.to_bits().hash(state);
        self.family.hash(state);
        self.stretch.hash(state);
        self.weight.hash(state);
        self.style.hash(state);
        self.ligatures.hash(state);
        self.color.hash(state);
    }
}