veloren_voxygen/ui/
fonts.rs

1use crate::ui::ice::RawFont;
2use common::assets::{self, AssetExt};
3
4pub struct Font {
5    metadata: i18n::Font,
6    pub conrod_id: conrod_core::text::font::Id,
7}
8
9impl Font {
10    fn new(font: &i18n::Font, ui: &mut crate::ui::Ui) -> Result<Self, assets::Error> {
11        let raw_font = RawFont::load(&font.asset_key)?.cloned();
12
13        Ok(Self {
14            metadata: font.clone(),
15            conrod_id: ui.new_font(raw_font),
16        })
17    }
18
19    /// Scale input size to final UI size
20    pub fn scale(&self, value: u32) -> u32 { self.metadata.scale(value) }
21}
22
23macro_rules! conrod_fonts {
24    ($([ $( $name:ident$(,)? )* ])*) => {
25        $(
26            pub struct Fonts {
27                $(pub $name: Font,)*
28            }
29
30            impl Fonts {
31                // TODO: test that no additional fonts are present
32                pub fn load(fonts: &i18n::Fonts, ui: &mut crate::ui::Ui) -> Result<Self, assets::Error> {
33                    Ok(Self {
34                        $( $name: Font::new(fonts.get(stringify!($name)).unwrap(), ui)?, )*
35                    })
36                }
37            }
38        )*
39    };
40}
41
42conrod_fonts! {
43    // TODO: wizard and metamorph aren't used, should we remove them?
44    [opensans, metamorph, alkhemi, cyri, wizard]
45}
46
47pub struct IcedFont {
48    metadata: i18n::Font,
49    pub id: crate::ui::ice::FontId,
50}
51
52impl IcedFont {
53    fn new(font: &i18n::Font, ui: &mut crate::ui::ice::IcedUi) -> Result<Self, assets::Error> {
54        let raw_font = RawFont::load(&font.asset_key)?.cloned();
55
56        Ok(Self {
57            metadata: font.clone(),
58            id: ui.add_font(raw_font),
59        })
60    }
61
62    /// Scale input size to final UI size
63    /// TODO: change metadata to use u16
64    pub fn scale(&self, value: u16) -> u16 { self.metadata.scale(value as u32) as u16 }
65}
66
67macro_rules! iced_fonts {
68    ($([ $( $name:ident$(,)? )* ])*) => {
69        $(
70            pub struct IcedFonts {
71                $(pub $name: IcedFont,)*
72            }
73
74            impl IcedFonts {
75                pub fn load(fonts: &i18n::Fonts, ui: &mut crate::ui::ice::IcedUi) -> Result<Self, assets::Error> {
76                    Ok(Self {
77                        $( $name: IcedFont::new(fonts.get(stringify!($name)).unwrap(), ui)?, )*
78                    })
79                }
80            }
81        )*
82    };
83}
84
85iced_fonts! {
86    [opensans, metamorph, alkhemi, cyri, wizard]
87}