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
//! Load and use fonts.
use std::hash::Hash;
/// A font.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct Font {
/// The [`Family`] of the [`Font`].
pub family: Family,
/// The [`Weight`] of the [`Font`].
pub weight: Weight,
/// The [`Stretch`] of the [`Font`].
pub stretch: Stretch,
/// The [`Style`] of the [`Font`].
pub style: Style,
/// The OpenType [`Features`] the [`Font`] turns on.
pub features: Features,
}
impl Font {
/// A non-monospaced sans-serif font with normal [`Weight`].
pub const DEFAULT: Font = Font {
family: Family::SansSerif,
weight: Weight::Normal,
stretch: Stretch::Normal,
style: Style::Normal,
features: Features::NONE,
};
/// A monospaced font with normal [`Weight`].
pub const MONOSPACE: Font = Font {
family: Family::Monospace,
..Self::DEFAULT
};
/// Creates a non-monospaced [`Font`] with the given [`Family::Name`] and
/// normal [`Weight`].
pub const fn with_name(name: &'static str) -> Self {
Font {
family: Family::Name(name),
..Self::DEFAULT
}
}
/// Returns this [`Font`] with every figure the same width, so numbers
/// that change in place keep their columns (CSS `tabular-nums`).
pub const fn tabular_figures(self) -> Self {
Font {
features: self.features.union(Features::TABULAR_FIGURES),
..self
}
}
}
/// A set of OpenType features a [`Font`] turns on.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct Features(u8);
impl Features {
/// No features beyond the font's defaults.
pub const NONE: Self = Self(0);
/// Tabular figures (`tnum`): every digit takes the same advance.
pub const TABULAR_FIGURES: Self = Self(1);
/// Returns the features in either set.
pub const fn union(self, other: Self) -> Self {
Self(self.0 | other.0)
}
/// Returns whether every feature in `other` is in this set.
pub const fn contains(self, other: Self) -> bool {
self.0 & other.0 == other.0
}
/// Returns whether the set is empty.
pub const fn is_empty(self) -> bool {
self.0 == 0
}
}
/// A font family.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Family {
/// The name of a font family of choice.
Name(&'static str),
/// Serif fonts represent the formal text style for a script.
Serif,
/// Glyphs in sans-serif fonts, as the term is used in CSS, are generally low
/// contrast and have stroke endings that are plain — without any flaring,
/// cross stroke, or other ornamentation.
#[default]
SansSerif,
/// Glyphs in cursive fonts generally use a more informal script style, and
/// the result looks more like handwritten pen or brush writing than printed
/// letterwork.
Cursive,
/// Fantasy fonts are primarily decorative or expressive fonts that contain
/// decorative or expressive representations of characters.
Fantasy,
/// The sole criterion of a monospace font is that all glyphs have the same
/// fixed width.
Monospace,
}
/// The weight of some text.
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Weight {
Thin,
ExtraLight,
Light,
#[default]
Normal,
Medium,
Semibold,
Bold,
ExtraBold,
Black,
}
/// The width of some text.
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Stretch {
UltraCondensed,
ExtraCondensed,
Condensed,
SemiCondensed,
#[default]
Normal,
SemiExpanded,
Expanded,
ExtraExpanded,
UltraExpanded,
}
/// The style of some text.
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum Style {
#[default]
Normal,
Italic,
Oblique,
}