fontsource_downloader 0.3.1

A library to download (and cache) fonts with Fontsource REST API
Documentation
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
use std::collections::{HashSet, hash_set::Iter};

#[cfg(feature = "pyo3")]
use pyo3::prelude::*;
use serde::{Deserialize, Serialize};

/// A struct describing the desired font(s) to query.
///
#[cfg_attr(
    feature = "pyo3",
    pyclass(module = "fontsource_downloader", frozen, get_all, from_py_object)
)]
#[cfg_attr(
    not(feature = "pyo3"),
    doc = "Use [`QueryBuilder`] to construct a [`FontQuery`]."
)]
#[derive(Debug, Clone)]
pub struct FontQuery {
    family: String,
    style: HashSet<String>,
    weight: HashSet<Weight>,
    subset: HashSet<String>,
    pub(crate) file_type: HashSet<FontFileType>,
}

impl Default for FontQuery {
    /// Create a default [`FontQuery`] with the following default values:
    ///
    /// - `family` = `"Roboto"`
    /// - `style` = `"normal"`
    /// - `weight` = [`Weight::Normal`]
    /// - `subset` = `"latin"`
    /// - `file_type` = [`FontFileType::Ttf`]
    fn default() -> Self {
        Self {
            family: String::from("Roboto"),
            style: HashSet::from_iter([String::from("normal")]),
            weight: HashSet::from_iter([Weight::default()]),
            subset: HashSet::from_iter([String::from("latin")]),
            file_type: HashSet::from_iter([FontFileType::default()]),
        }
    }
}

/// A builder for constructing a [`FontQuery`].
///
/// # Example
/// ```rust
/// use fontsource_downloader::{QueryBuilder, FontFileType, Weight};
/// let query = QueryBuilder::new("Roboto")
///     .with_weight(Weight::from(400))
///     .with_weight(Weight::from(700))
///     .with_file_type(FontFileType::Woff2)
///     .with_file_type(FontFileType::Ttf)
///     .build();
/// assert_eq!(query.family(), "Roboto");
/// assert!(query.weights().any(|w| *w == Weight::Normal));
/// assert!(query.weights().any(|w| *w == Weight::Bold));
/// assert!(query.file_types().any(|t| *t == FontFileType::Woff2));
/// assert!(query.file_types().any(|t| *t == FontFileType::Ttf));
/// ```
#[derive(Debug, Default)]
pub struct QueryBuilder {
    family: String,
    style: HashSet<String>,
    weight: HashSet<Weight>,
    subset: HashSet<String>,
    file_type: HashSet<FontFileType>,
}

impl From<FontQuery> for QueryBuilder {
    fn from(value: FontQuery) -> Self {
        Self::from(&value)
    }
}

impl From<&FontQuery> for QueryBuilder {
    fn from(value: &FontQuery) -> Self {
        Self {
            family: value.family.clone(),
            style: value.style.clone(),
            weight: value.weight.clone(),
            subset: value.subset.clone(),
            file_type: value.file_type.clone(),
        }
    }
}

impl QueryBuilder {
    /// Create a new [`QueryBuilder`] for the given font family (display name).
    pub fn new(family: &str) -> Self {
        Self {
            family: family.trim().to_string(),
            ..Default::default()
        }
    }

    /// Add a style to this query.
    ///
    /// The style will be normalized to "normal", "italic", or "oblique"
    /// (case-insensitive, with leading/trailing whitespace stripped).
    ///
    /// If not specified, the default style is "normal".
    pub fn with_style(self, style: &str) -> Self {
        let mut styles = self.style;
        styles.insert(Self::normalized_style(style).to_string());
        Self {
            style: styles,
            ..self
        }
    }

    fn normalized_style(style: &str) -> &'static str {
        let style = style.trim();
        if style.eq_ignore_ascii_case("italic") {
            return "italic";
        } else if style.eq_ignore_ascii_case("oblique") {
            return "oblique";
        }
        "normal"
    }

    /// Add a weight to this query.
    ///
    /// If not specified, the default weight is [`Weight::Normal`]
    /// (or `Weight::from(400)`).
    pub fn with_weight(self, weight: Weight) -> Self {
        let mut weights = self.weight;
        weights.insert(weight);
        Self {
            weight: weights,
            ..self
        }
    }

    /// Add a subset to this query.
    ///
    /// The subset will be normalized by trimming surrounding whitespace.
    /// If the resulting string is empty (or never specified),
    /// it will default to "latin".
    pub fn with_subset(self, subset: &str) -> Self {
        let mut subsets = self.subset;
        subsets.insert(Self::normalized_subset(subset).to_string());
        Self {
            subset: subsets,
            ..self
        }
    }

    fn normalized_subset(subset: &str) -> &str {
        let result = subset.trim();
        if !result.is_empty() { result } else { "latin" }
    }

    /// Add a file type to this query.
    ///
    /// If not specified, the default file type is [`FontFileType::Ttf`].
    pub fn with_file_type(self, file_type: FontFileType) -> Self {
        let mut file_types = self.file_type;
        file_types.insert(file_type);
        Self {
            file_type: file_types,
            ..self
        }
    }

    /// Build the [`FontQuery`] from this builder.
    ///
    /// This applies default values for any fields that were not set.
    /// See [`FontQuery::default()`] for the default values.
    pub fn build(self) -> FontQuery {
        // Ensure that at least one style, weight, subset, and file type is specified
        let style = if self.style.is_empty() {
            HashSet::from_iter(["normal".to_string()])
        } else {
            self.style
        };
        let weight = if self.weight.is_empty() {
            HashSet::from_iter([Weight::Normal])
        } else {
            self.weight
        };
        let subset = if self.subset.is_empty() {
            HashSet::from_iter(["latin".to_string()])
        } else {
            self.subset
        };
        let file_type = if self.file_type.is_empty() {
            HashSet::from_iter([FontFileType::Ttf])
        } else {
            self.file_type
        };
        FontQuery {
            family: self.family,
            style,
            weight,
            subset,
            file_type,
        }
    }
}

impl FontQuery {
    /// The font family's display name.
    pub fn family(&self) -> &str {
        &self.family
    }

    /// Return an iterator over the styles in this query.
    pub fn styles(&self) -> Iter<'_, String> {
        self.style.iter()
    }

    /// An iterator over the weights in this query.
    pub fn weights(&self) -> Iter<'_, Weight> {
        self.weight.iter()
    }

    /// An iterator over the subsets in this query.
    pub fn subsets(&self) -> Iter<'_, String> {
        self.subset.iter()
    }

    /// An iterator over the file types in this query.
    pub fn file_types(&'_ self) -> Iter<'_, FontFileType> {
        self.file_type.iter()
    }

    pub(crate) fn filter_subsets<'a>(&'a self, available: &[String]) -> Vec<&'a String> {
        self.subsets().filter(|v| available.contains(v)).collect()
    }

    pub(crate) fn filter_styles<'a>(&'a self, available: &[String]) -> Vec<&'a String> {
        self.styles().filter(|v| available.contains(v)).collect()
    }

    pub(crate) fn filter_weights(&self, available: &[u16]) -> Vec<u16> {
        self.weights()
            .filter_map(|v| {
                let int_weight: u16 = (*v).into();
                if available.contains(&int_weight) {
                    Some(int_weight)
                } else {
                    None
                }
            })
            .collect()
    }
}

#[cfg(feature = "pyo3")]
#[pymethods]
impl FontQuery {
    /// Create a new `FontQuery` with the given parameters.
    ///
    /// Required parameter `family` is the display name of the font family to query.
    ///
    /// Optional parameter defaults are as follows:
    ///
    /// - ``styles=["normal"]``
    /// - ``weights=[Weight(400)]``
    /// - ``subsets=["latin"]``
    /// - ``file_types=[FontFileType.Ttf]``
    #[new]
    #[pyo3(
        signature = (family, styles=None, weights=None, subsets=None, file_types=None),
        text_signature = "(family: str, styles: str | None = None, weights: list[Weight] | None = None, subsets: list[str] | None = None, file_types: list[FontFileType] | None = None) -> FontQuery"
    )]
    pub fn new_py(
        family: String,
        styles: Option<Vec<String>>,
        weights: Option<Vec<Weight>>,
        subsets: Option<Vec<String>>,
        file_types: Option<Vec<FontFileType>>,
    ) -> Self {
        let mut result = QueryBuilder::new(&family);
        if let Some(styles) = styles {
            for style in styles {
                result = result.with_style(&style);
            }
        }
        if let Some(weight) = weights {
            for weight in weight {
                result = result.with_weight(weight);
            }
        }
        if let Some(subsets) = subsets {
            for subset in subsets {
                result = result.with_subset(&subset);
            }
        }
        if let Some(file_types) = file_types {
            for file_type in file_types {
                result = result.with_file_type(file_type);
            }
        }
        result.build()
    }

    /// The font family's display name.
    #[getter]
    pub fn get_family(&self) -> &str {
        &self.family
    }

    /// A ``list`` of the styles in this query.
    #[getter]
    pub fn get_styles(&self) -> Vec<String> {
        self.styles().cloned().collect()
    }

    /// A ``list`` of the weights in this query.
    #[getter]
    pub fn get_weights(&self) -> Vec<Weight> {
        self.weights().cloned().collect()
    }

    /// A ``list`` of the subsets in this query.
    #[getter]
    pub fn get_subsets(&self) -> Vec<String> {
        self.subsets().cloned().collect()
    }

    /// A ``list`` of the file types in this query.
    #[getter]
    pub fn get_file_types(&self) -> Vec<FontFileType> {
        self.file_types().cloned().collect()
    }
}

/// An enum representing supported downloadable font file types.
#[cfg_attr(
    feature = "pyo3",
    pyclass(module = "fontsource_downloader", eq, from_py_object)
)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[allow(missing_docs)]
pub enum FontFileType {
    Woff2,
    Woff,
    #[default]
    Ttf,
}

impl FontFileType {
    pub(crate) fn extension(&self) -> &'static str {
        match self {
            FontFileType::Woff2 => "woff2",
            FontFileType::Woff => "woff",
            FontFileType::Ttf => "ttf",
        }
    }
}

#[cfg(feature = "pyo3")]
#[cfg_attr(feature = "pyo3", pymethods)]
impl FontFileType {
    /// Return the file-extension representation of this ``FontFileType``.
    pub fn __str__(&self) -> &'static str {
        self.extension()
    }
}

/// An enum representing the standard font weights.
#[cfg_attr(
    feature = "pyo3",
    pyclass(module = "fontsource_downloader", eq_int, eq, from_py_object)
)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Hash)]
#[allow(missing_docs)]
pub enum Weight {
    Thin = 100,
    ExtraLight = 200,
    Light = 300,
    #[default]
    Normal = 400,
    Medium = 500,
    SemiBold = 600,
    Bold = 700,
    ExtraBold = 800,
    Black = 900,
}

impl From<Weight> for u16 {
    fn from(value: Weight) -> Self {
        value as u16
    }
}

impl From<&Weight> for u16 {
    fn from(value: &Weight) -> Self {
        (*value).into()
    }
}

impl From<u16> for Weight {
    fn from(value: u16) -> Self {
        // Round to nearest hundred
        let value = (value.clamp(100, 900) / 100) * 100;
        match value {
            100 => Weight::Thin,
            200 => Weight::ExtraLight,
            300 => Weight::Light,
            500 => Weight::Medium,
            600 => Weight::SemiBold,
            700 => Weight::Bold,
            800 => Weight::ExtraBold,
            900 => Weight::Black,
            _ => Weight::Normal, // Default to Normal for non-standard weights
        }
    }
}

#[cfg(feature = "pyo3")]
#[cfg_attr(feature = "pyo3", pymethods)]
impl Weight {
    /// Create a new ``Weight`` from a given integer value.
    ///
    /// The value will be rounded to the nearest standard weight (100, 200, ..., 900).
    #[new]
    #[pyo3(signature = (value), text_signature = "(value: int) -> Weight")]
    pub fn new(value: i32) -> Self {
        (value.clamp(100, 900) as u16).into()
    }

    /// Get the integer value of this ``Weight``.
    pub fn __int__(&self) -> u16 {
        (*self).into()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn weight_conversion() {
        assert_eq!(Weight::from(50), Weight::Thin); // Clamped to 100
        assert_eq!(Weight::from(256), Weight::ExtraLight);
        assert_eq!(Weight::from(322), Weight::Light);
        assert_eq!(Weight::from(404), Weight::Normal);
        assert_eq!(Weight::from(555), Weight::Medium);
        assert_eq!(Weight::from(666), Weight::SemiBold);
        assert_eq!(Weight::from(750), Weight::Bold);
        assert_eq!(Weight::from(888), Weight::ExtraBold);
        assert_eq!(Weight::from(950), Weight::Black); // Clamped to 900

        assert_eq!(u16::from(Weight::Thin), 100);
        assert_eq!(u16::from(Weight::ExtraLight), 200);
        assert_eq!(u16::from(Weight::Light), 300);
        assert_eq!(u16::from(Weight::Normal), 400);
        assert_eq!(u16::from(Weight::Medium), 500);
        assert_eq!(u16::from(Weight::SemiBold), 600);
        assert_eq!(u16::from(Weight::Bold), 700);
        assert_eq!(u16::from(Weight::ExtraBold), 800);
        assert_eq!(u16::from(Weight::Black), 900);

        let weight_ref = &Weight::Medium;
        assert_eq!(u16::from(weight_ref), 500);
    }

    #[test]
    fn font_query_defaults() {
        let mut query = QueryBuilder::new("Roboto")
            .with_subset("") // Override default subset to test normalized_subset method
            .with_style(" Italic  ")
            .build(); // Test trimming and case insensitivity
        assert_eq!(query.family, "Roboto");
        assert_eq!(query.styles().collect::<Vec<&String>>(), vec!["italic"]);
        assert_eq!(
            query.weights().collect::<Vec<&Weight>>(),
            vec![&Weight::Normal]
        );
        assert_eq!(
            query.file_types().collect::<Vec<&FontFileType>>(),
            vec![&FontFileType::Ttf]
        );
        assert_eq!(query.subsets().collect::<Vec<&String>>(), vec!["latin"]);

        query = QueryBuilder::from(query)
            .with_subset(" cyrillic ")
            .with_style("Oblique")
            .build();
        let subsets = query.subsets().map(|v| v.as_str()).collect::<Vec<&str>>();
        assert!(subsets.contains(&"latin"));
        assert!(subsets.contains(&"cyrillic"));
        let styles = query.styles().map(|v| v.as_str()).collect::<Vec<&str>>();
        assert!(styles.contains(&"italic"));
        assert!(styles.contains(&"oblique"));

        let default_query = QueryBuilder::new(&query.family).build();
        assert_eq!(default_query.family(), "Roboto");
        assert_eq!(
            default_query.styles().collect::<Vec<&String>>(),
            vec!["normal"]
        );
        assert_eq!(
            default_query.weights().collect::<Vec<&Weight>>(),
            vec![&Weight::Normal]
        );
        assert_eq!(
            default_query.file_types().collect::<Vec<&FontFileType>>(),
            vec![&FontFileType::Ttf]
        );
        assert_eq!(
            default_query.subsets().collect::<Vec<&String>>(),
            vec!["latin"]
        );
    }
}