@@ -8,7 +8,7 @@ use core_maths::*;
8
8
9
9
use core:: fmt;
10
10
11
- /// Primary attributes for font matching: stretch, style and weight .
11
+ /// Primary attributes for font matching: [`Stretch`], [`Style`] and [`Weight`] .
12
12
#[ derive( Copy , Clone , PartialEq , Default , Debug ) ]
13
13
pub struct Attributes {
14
14
pub stretch : Stretch ,
@@ -38,7 +38,9 @@ impl fmt::Display for Attributes {
38
38
}
39
39
40
40
/// Visual width of a font-- a relative change from the normal aspect
41
- /// ratio, typically in the range 0.5 to 2.0.
41
+ /// ratio, typically in the range `0.5` to `2.0`.
42
+ ///
43
+ /// The default value is [`Stretch::NORMAL`] or `1.0`.
42
44
///
43
45
/// In variable fonts, this can be controlled with the `wdth` axis.
44
46
///
@@ -59,7 +61,7 @@ impl Stretch {
59
61
/// Width that is 87.5% of normal.
60
62
pub const SEMI_CONDENSED : Self = Self ( 0.875 ) ;
61
63
62
- /// Width that is 100% of normal.
64
+ /// Width that is 100% of normal. This is the default value.
63
65
pub const NORMAL : Self = Self ( 1.0 ) ;
64
66
65
67
/// Width that is 112.5% of normal.
@@ -77,45 +79,112 @@ impl Stretch {
77
79
78
80
impl Stretch {
79
81
/// Creates a new stretch attribute with the given ratio.
82
+ ///
83
+ /// # Example
84
+ ///
85
+ /// ```
86
+ /// # use fontique::Stretch;
87
+ /// assert_eq!(Stretch::from_ratio(1.5), Stretch::EXTRA_EXPANDED);
88
+ /// ```
89
+ ///
90
+ /// # See also
91
+ ///
92
+ /// * [`Stretch::from_percentage()`]
93
+ /// * [`Stretch::ratio()`]
80
94
pub fn from_ratio ( ratio : f32 ) -> Self {
81
95
Self ( ratio)
82
96
}
83
97
84
98
/// Creates a stretch attribute from a percentage.
99
+ ///
100
+ /// # Example
101
+ ///
102
+ /// ```
103
+ /// # use fontique::Stretch;
104
+ /// assert_eq!(Stretch::from_percentage(87.5), Stretch::SEMI_CONDENSED);
105
+ /// ```
106
+ ///
107
+ /// # See also
108
+ ///
109
+ /// * [`Stretch::from_ratio()`]
110
+ /// * [`Stretch::percentage()`]
85
111
pub fn from_percentage ( percentage : f32 ) -> Self {
86
112
Self ( percentage / 100.0 )
87
113
}
88
114
89
115
/// Returns the stretch attribute as a ratio.
90
116
///
91
- /// This is a linear scaling factor with 1.0 being "normal" width.
117
+ /// This is a linear scaling factor with `1.0` being "normal" width.
118
+ ///
119
+ /// # Example
120
+ ///
121
+ /// ```
122
+ /// # use fontique::Stretch;
123
+ /// assert_eq!(Stretch::NORMAL.ratio(), 1.0);
124
+ /// ```
125
+ ///
126
+ /// # See also
127
+ ///
128
+ /// * [`Stretch::from_ratio()`]
129
+ /// * [`Stretch::percentage()`]
92
130
pub fn ratio ( self ) -> f32 {
93
131
self . 0
94
132
}
95
133
96
134
/// Returns the stretch attribute as a percentage value.
97
135
///
98
136
/// This is generally the value associated with the `wdth` axis.
137
+ ///
138
+ /// # See also
139
+ ///
140
+ /// * [`Stretch::from_percentage()`]
141
+ /// * [`Stretch::ratio()`]
99
142
pub fn percentage ( self ) -> f32 {
100
143
self . 0 * 100.0
101
144
}
102
145
103
- /// Returns true if the stretch is normal.
146
+ /// Returns true if the stretch is [normal].
147
+ ///
148
+ /// # See also
149
+ ///
150
+ /// * [`Stretch::is_condensed()`]
151
+ /// * [`Stretch::is_expanded()`]
152
+ ///
153
+ /// [normal]: Stretch::NORMAL
104
154
pub fn is_normal ( self ) -> bool {
105
155
self == Self :: NORMAL
106
156
}
107
157
108
158
/// Returns true if the stretch is condensed (less than normal).
159
+ ///
160
+ /// # See also
161
+ ///
162
+ /// * [`Stretch::is_expanded()`]
163
+ /// * [`Stretch::is_normal()`]
109
164
pub fn is_condensed ( self ) -> bool {
110
165
self < Self :: NORMAL
111
166
}
112
167
113
168
/// Returns true if the stretch is expanded (greater than normal).
169
+ ///
170
+ /// # See also
171
+ ///
172
+ /// * [`Stretch::is_condensed()`]
173
+ /// * [`Stretch::is_normal()`]
114
174
pub fn is_expanded ( self ) -> bool {
115
175
self > Self :: NORMAL
116
176
}
117
177
118
178
/// Parses the stretch from a CSS style keyword or a percentage value.
179
+ ///
180
+ /// # Examples
181
+ ///
182
+ /// ```
183
+ /// # use fontique::Stretch;
184
+ /// assert_eq!(Stretch::parse("semi-condensed"), Some(Stretch::SEMI_CONDENSED));
185
+ /// assert_eq!(Stretch::parse("80%"), Some(Stretch::from_percentage(80.0)));
186
+ /// assert_eq!(Stretch::parse("wideload"), None);
187
+ /// ```
119
188
pub fn parse ( s : & str ) -> Option < Self > {
120
189
let s = s. trim ( ) ;
121
190
Some ( match s {
@@ -171,6 +240,8 @@ impl Default for Stretch {
171
240
172
241
/// Visual weight class of a font, typically on a scale from 1.0 to 1000.0.
173
242
///
243
+ /// The default value is [`Weight::NORMAL`] or `400.0`.
244
+ ///
174
245
/// In variable fonts, this can be controlled with the `wght` axis.
175
246
///
176
247
/// See <https://fonts.google.com/knowledge/glossary/weight>
@@ -190,7 +261,7 @@ impl Weight {
190
261
/// Weight value of 350.
191
262
pub const SEMI_LIGHT : Self = Self ( 350.0 ) ;
192
263
193
- /// Weight value of 400.
264
+ /// Weight value of 400. This is the default value.
194
265
pub const NORMAL : Self = Self ( 400.0 ) ;
195
266
196
267
/// Weight value of 500.
@@ -224,6 +295,16 @@ impl Weight {
224
295
}
225
296
226
297
/// Parses a CSS style font weight attribute.
298
+ ///
299
+ /// # Examples
300
+ ///
301
+ /// ```
302
+ /// # use fontique::Weight;
303
+ /// assert_eq!(Weight::parse("normal"), Some(Weight::NORMAL));
304
+ /// assert_eq!(Weight::parse("bold"), Some(Weight::BOLD));
305
+ /// assert_eq!(Weight::parse("850"), Some(Weight::new(850.0)));
306
+ /// assert_eq!(Weight::parse("invalid"), None);
307
+ /// ```
227
308
pub fn parse ( s : & str ) -> Option < Self > {
228
309
let s = s. trim ( ) ;
229
310
Some ( match s {
@@ -265,6 +346,8 @@ impl fmt::Display for Weight {
265
346
266
347
/// Visual style or 'slope' of a font.
267
348
///
349
+ /// The default value is [`Style::Normal`].
350
+ ///
268
351
/// In variable fonts, this can be controlled with the `ital`
269
352
/// and `slnt` axes for italic and oblique styles, respectively.
270
353
///
0 commit comments