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
use crate::ui::ice::RawFont;
use common::assets::{self, AssetExt};

pub struct Font {
    metadata: i18n::Font,
    pub conrod_id: conrod_core::text::font::Id,
}

impl Font {
    fn new(font: &i18n::Font, ui: &mut crate::ui::Ui) -> Result<Self, assets::Error> {
        let raw_font = RawFont::load(&font.asset_key)?.cloned();

        Ok(Self {
            metadata: font.clone(),
            conrod_id: ui.new_font(raw_font),
        })
    }

    /// Scale input size to final UI size
    pub fn scale(&self, value: u32) -> u32 { self.metadata.scale(value) }
}

macro_rules! conrod_fonts {
    ($([ $( $name:ident$(,)? )* ])*) => {
        $(
            pub struct Fonts {
                $(pub $name: Font,)*
            }

            impl Fonts {
                pub fn load(fonts: &i18n::Fonts, ui: &mut crate::ui::Ui) -> Result<Self, assets::Error> {
                    Ok(Self {
                        $( $name: Font::new(fonts.get(stringify!($name)).unwrap(), ui)?, )*
                    })
                }
            }
        )*
    };
}

conrod_fonts! {
    [opensans, metamorph, alkhemi, cyri, wizard]
}

pub struct IcedFont {
    metadata: i18n::Font,
    pub id: crate::ui::ice::FontId,
}

impl IcedFont {
    fn new(font: &i18n::Font, ui: &mut crate::ui::ice::IcedUi) -> Result<Self, assets::Error> {
        let raw_font = RawFont::load(&font.asset_key)?.cloned();

        Ok(Self {
            metadata: font.clone(),
            id: ui.add_font(raw_font),
        })
    }

    /// Scale input size to final UI size
    /// TODO: change metadata to use u16
    pub fn scale(&self, value: u16) -> u16 { self.metadata.scale(value as u32) as u16 }
}

macro_rules! iced_fonts {
    ($([ $( $name:ident$(,)? )* ])*) => {
        $(
            pub struct IcedFonts {
                $(pub $name: IcedFont,)*
            }

            impl IcedFonts {
                pub fn load(fonts: &i18n::Fonts, ui: &mut crate::ui::ice::IcedUi) -> Result<Self, assets::Error> {
                    Ok(Self {
                        $( $name: IcedFont::new(fonts.get(stringify!($name)).unwrap(), ui)?, )*
                    })
                }
            }
        )*
    };
}

iced_fonts! {
    [opensans, metamorph, alkhemi, cyri, wizard]
}