1// font-kit/src/family.rs
2//
3// Copyright © 2018 The Pathfinder Project Developers.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
11//! Defines a set of faces that vary in weight, width or slope.
12
13use crate::error::FontLoadingError;
14use crate::family_handle::FamilyHandle;
15use crate::font::Font;
16use crate::handle::Handle;
17use crate::loader::Loader;
18
19/// Defines a set of faces that vary in weight, width or slope.
20#[derive(Debug)]
21pub struct Family<F = Font>
22where
23 F: Loader,
24{
25 fonts: Vec<F>,
26}
27
28impl<F> Family<F>
29where
30 F: Loader,
31{
32 pub(crate) fn from_font_handles<'a, I>(font_handles: I) -> Result<Family<F>, FontLoadingError>
33 where
34 I: Iterator<Item = &'a Handle>,
35 {
36 let mut fonts = vec![];
37 for font_handle in font_handles {
38 fonts.push(F::from_handle(font_handle)?)
39 }
40 Ok(Family { fonts })
41 }
42
43 #[inline]
44 pub(crate) fn from_handle(family_handle: &FamilyHandle) -> Result<Family<F>, FontLoadingError> {
45 Family::from_font_handles(family_handle.fonts.iter())
46 }
47
48 /// Returns the individual fonts in this family.
49 #[inline]
50 pub fn fonts(&self) -> &[F] {
51 &self.fonts
52 }
53
54 /// Returns true if and only if this family is empty.
55 #[inline]
56 pub fn is_empty(&self) -> bool {
57 self.fonts.is_empty()
58 }
59}
60