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
use enums::{FontStretch, FontStyle, FontWeight};
use font::Font;
use font_list::FontList;
use localized_strings::LocalizedStrings;

use std::ptr;

use com_wrapper::ComWrapper;
use winapi::shared::winerror::SUCCEEDED;
use winapi::um::dwrite::IDWriteFontFamily;
use wio::com::ComPtr;

#[repr(transparent)]
#[derive(Clone, ComWrapper, PartialEq)]
#[com(send, sync, debug)]
/// Represents a family of related fonts.
pub struct FontFamily {
    ptr: ComPtr<IDWriteFontFamily>,
}

impl FontFamily {
    /// Creates a localized strings object that contains the family names for the font family,
    /// indexed by locale name.
    pub fn family_name(&self) -> Option<LocalizedStrings> {
        unsafe {
            let mut ptr = ptr::null_mut();
            let hr = self.ptr.GetFamilyNames(&mut ptr);
            if SUCCEEDED(hr) {
                Some(LocalizedStrings::from_raw(ptr))
            } else {
                None
            }
        }
    }

    /// Gets the font that best matches the specified properties.
    pub fn first_matching_font(
        &self,
        weight: FontWeight,
        stretch: FontStretch,
        style: FontStyle,
    ) -> Option<Font> {
        unsafe {
            let mut font_ptr = ptr::null_mut();
            let hr = self.ptr.GetFirstMatchingFont(
                weight.0,
                stretch as u32,
                style as u32,
                &mut font_ptr,
            );
            if SUCCEEDED(hr) {
                Some(Font::from_raw(font_ptr))
            } else {
                None
            }
        }
    }

    /// Gets a list of fonts in the font family ranked in order of how well they match the
    /// specified properties.
    pub fn matching_fonts(
        &self,
        weight: FontWeight,
        stretch: FontStretch,
        style: FontStyle,
    ) -> Option<FontList> {
        unsafe {
            let mut list = ptr::null_mut();
            let hr = self
                .ptr
                .GetMatchingFonts(weight.0, stretch as u32, style as u32, &mut list);
            if SUCCEEDED(hr) {
                Some(FontList::from_raw(list))
            } else {
                None
            }
        }
    }
}