Skip to content

Commit 417d495

Browse files
fontique: Improve docs
1 parent 9af2072 commit 417d495

File tree

7 files changed

+134
-14
lines changed

7 files changed

+134
-14
lines changed

fontique/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ rust-version.workspace = true
99
license.workspace = true
1010
repository.workspace = true
1111

12+
[package.metadata.docs.rs]
13+
all-features = true
14+
1215
[lints]
1316
workspace = true
1417

fontique/src/attributes.rs

+89-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use core_maths::*;
88

99
use core::fmt;
1010

11-
/// Primary attributes for font matching: stretch, style and weight.
11+
/// Primary attributes for font matching: [`Stretch`], [`Style`] and [`Weight`].
1212
#[derive(Copy, Clone, PartialEq, Default, Debug)]
1313
pub struct Attributes {
1414
pub stretch: Stretch,
@@ -38,7 +38,9 @@ impl fmt::Display for Attributes {
3838
}
3939

4040
/// 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`.
4244
///
4345
/// In variable fonts, this can be controlled with the `wdth` axis.
4446
///
@@ -59,7 +61,7 @@ impl Stretch {
5961
/// Width that is 87.5% of normal.
6062
pub const SEMI_CONDENSED: Self = Self(0.875);
6163

62-
/// Width that is 100% of normal.
64+
/// Width that is 100% of normal. This is the default value.
6365
pub const NORMAL: Self = Self(1.0);
6466

6567
/// Width that is 112.5% of normal.
@@ -77,45 +79,112 @@ impl Stretch {
7779

7880
impl Stretch {
7981
/// 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()`]
8094
pub fn from_ratio(ratio: f32) -> Self {
8195
Self(ratio)
8296
}
8397

8498
/// 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()`]
85111
pub fn from_percentage(percentage: f32) -> Self {
86112
Self(percentage / 100.0)
87113
}
88114

89115
/// Returns the stretch attribute as a ratio.
90116
///
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()`]
92130
pub fn ratio(self) -> f32 {
93131
self.0
94132
}
95133

96134
/// Returns the stretch attribute as a percentage value.
97135
///
98136
/// This is generally the value associated with the `wdth` axis.
137+
///
138+
/// # See also
139+
///
140+
/// * [`Stretch::from_percentage()`]
141+
/// * [`Stretch::ratio()`]
99142
pub fn percentage(self) -> f32 {
100143
self.0 * 100.0
101144
}
102145

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
104154
pub fn is_normal(self) -> bool {
105155
self == Self::NORMAL
106156
}
107157

108158
/// Returns true if the stretch is condensed (less than normal).
159+
///
160+
/// # See also
161+
///
162+
/// * [`Stretch::is_expanded()`]
163+
/// * [`Stretch::is_normal()`]
109164
pub fn is_condensed(self) -> bool {
110165
self < Self::NORMAL
111166
}
112167

113168
/// Returns true if the stretch is expanded (greater than normal).
169+
///
170+
/// # See also
171+
///
172+
/// * [`Stretch::is_condensed()`]
173+
/// * [`Stretch::is_normal()`]
114174
pub fn is_expanded(self) -> bool {
115175
self > Self::NORMAL
116176
}
117177

118178
/// 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+
/// ```
119188
pub fn parse(s: &str) -> Option<Self> {
120189
let s = s.trim();
121190
Some(match s {
@@ -171,6 +240,8 @@ impl Default for Stretch {
171240

172241
/// Visual weight class of a font, typically on a scale from 1.0 to 1000.0.
173242
///
243+
/// The default value is [`Weight::NORMAL`] or `400.0`.
244+
///
174245
/// In variable fonts, this can be controlled with the `wght` axis.
175246
///
176247
/// See <https://fonts.google.com/knowledge/glossary/weight>
@@ -190,7 +261,7 @@ impl Weight {
190261
/// Weight value of 350.
191262
pub const SEMI_LIGHT: Self = Self(350.0);
192263

193-
/// Weight value of 400.
264+
/// Weight value of 400. This is the default value.
194265
pub const NORMAL: Self = Self(400.0);
195266

196267
/// Weight value of 500.
@@ -224,6 +295,16 @@ impl Weight {
224295
}
225296

226297
/// 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+
/// ```
227308
pub fn parse(s: &str) -> Option<Self> {
228309
let s = s.trim();
229310
Some(match s {
@@ -265,6 +346,8 @@ impl fmt::Display for Weight {
265346

266347
/// Visual style or 'slope' of a font.
267348
///
349+
/// The default value is [`Style::Normal`].
350+
///
268351
/// In variable fonts, this can be controlled with the `ital`
269352
/// and `slnt` axes for italic and oblique styles, respectively.
270353
///

fontique/src/collection/mod.rs

+26-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use std::sync::{atomic::Ordering, Mutex};
2828

2929
type FamilyMap = HashMap<FamilyId, Option<FamilyInfo>>;
3030

31-
/// Options for a font collection.
31+
/// Options for a [font collection](Collection).
3232
#[derive(Copy, Clone, Debug)]
3333
pub struct CollectionOptions {
3434
/// If true, the font collection will use a secondary shared store
@@ -38,13 +38,13 @@ pub struct CollectionOptions {
3838
/// If the font collection will be used by a single thread, this is
3939
/// pure overhead and should be disabled.
4040
///
41-
/// The default value is false.
41+
/// The default value is `false`.
4242
pub shared: bool,
4343

4444
/// If true, the font collection will provide access to system fonts
4545
/// using platform specific APIs.
4646
///
47-
/// The default value is true.
47+
/// The default value is `true`.
4848
pub system_fonts: bool,
4949
}
5050

@@ -66,6 +66,14 @@ pub struct Collection {
6666

6767
impl Collection {
6868
/// Creates a new collection with the given options.
69+
///
70+
/// If `fontique` was compiled with the `"system"` feature and
71+
/// [`CollectionOptions::system_fonts`] was set to `true` when
72+
/// creating this collection, then it will register the fonts
73+
/// available on the system.
74+
///
75+
/// Additional fonts can be registered via [`Collection::register_fonts()`]
76+
/// and providing it with the data for those fonts.
6977
pub fn new(options: CollectionOptions) -> Self {
7078
Self {
7179
inner: Inner::new(options),
@@ -75,12 +83,18 @@ impl Collection {
7583

7684
/// Returns an iterator over all available family names in the collection.
7785
///
78-
/// This includes both system and registered fonts.
86+
/// If `fontique` was compiled with the `"system"` feature, then it will
87+
/// include system fonts after the registered fonts.
7988
pub fn family_names(&mut self) -> impl Iterator<Item = &str> + '_ + Clone {
8089
self.inner.family_names()
8190
}
8291

8392
/// Returns the family identifier for the given family name.
93+
///
94+
/// # See also
95+
///
96+
/// * [`Collection::family_name()`]
97+
/// * [`Collection::family_names()`]
8498
pub fn family_id(&mut self, name: &str) -> Option<FamilyId> {
8599
self.inner.family_id(name)
86100
}
@@ -91,11 +105,19 @@ impl Collection {
91105
}
92106

93107
/// Returns the family object for the given family identifier.
108+
///
109+
/// # See also
110+
///
111+
/// * [`Collection::family_by_name()`]
94112
pub fn family(&mut self, id: FamilyId) -> Option<FamilyInfo> {
95113
self.inner.family(id)
96114
}
97115

98116
/// Returns the family object for the given name.
117+
///
118+
/// # See also
119+
///
120+
/// * [`Collection::family()`]
99121
pub fn family_by_name(&mut self, name: &str) -> Option<FamilyInfo> {
100122
self.inner.family_by_name(name)
101123
}

fontique/src/font.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl FontInfo {
6767
}
6868

6969
/// Returns the visual width of the font-- a relative change from the normal
70-
/// aspect ratio, typically in the range 0.5 to 2.0.
70+
/// aspect ratio, typically in the range `0.5` to `2.0`.
7171
pub fn stretch(&self) -> Stretch {
7272
self.stretch
7373
}
@@ -78,7 +78,7 @@ impl FontInfo {
7878
}
7979

8080
/// Returns the visual weight class of the font, typically on a scale
81-
/// from 1.0 to 1000.0.
81+
/// from `1.0` to `1000.0`.
8282
pub fn weight(&self) -> Weight {
8383
self.weight
8484
}
@@ -230,6 +230,17 @@ const ITALIC_AXIS: u8 = 0x08;
230230
const OPTICAL_SIZE_AXIS: u8 = 0x10;
231231

232232
/// An axis of variation for a variable font.
233+
///
234+
/// Instances of this can be obtained from [`FontInfo::axes()`].
235+
///
236+
/// # See also
237+
///
238+
/// * [`FontInfo::axes()`]
239+
/// * [`FontInfo::has_italic_axis()`]
240+
/// * [`FontInfo::has_optical_size_axis()`]
241+
/// * [`FontInfo::has_slant_axis()`]
242+
/// * [`FontInfo::has_weight_axis()`]
243+
/// * [`FontInfo::has_width_axis()`]
233244
#[derive(Copy, Clone, Default, Debug)]
234245
pub struct AxisInfo {
235246
/// The tag that identifies the axis.

fontique/src/generic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type FamilyVec = SmallVec<[FamilyId; 2]>;
1414
#[repr(u8)]
1515
pub enum GenericFamily {
1616
/// Glyphs have finishing strokes, flared or tapering ends, or have actual
17-
/// serifed endings.
17+
/// serifed endings.
1818
Serif = 0,
1919
/// Glyphs have stroke endings that are plain.
2020
SansSerif = 1,

fontique/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
//! Font enumeration and fallback.
55
6+
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
67
// TODO: Remove this dead code allowance and hide the offending code behind the std feature gate.
78
#![cfg_attr(not(feature = "std"), allow(dead_code))]
89
#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]

fontique/src/source_cache.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub struct SourceCacheOptions {
2020
/// This is useful for ensuring that only one copy of font data is
2121
/// loaded into memory in multi-threaded scenarios.
2222
///
23-
/// The default value is false.
23+
/// The default value is `false`.
2424
pub shared: bool,
2525
}
2626

0 commit comments

Comments
 (0)