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    [universal, alkhemi, cyri]
44}
45
46pub struct IcedFont {
47    metadata: i18n::Font,
48    pub id: crate::ui::ice::FontId,
49}
50
51impl IcedFont {
52    fn new(font: &i18n::Font, ui: &mut crate::ui::ice::IcedUi) -> Result<Self, assets::Error> {
53        let raw_font = RawFont::load(&font.asset_key)?.cloned();
54
55        Ok(Self {
56            metadata: font.clone(),
57            id: ui.add_font(raw_font),
58        })
59    }
60
61    /// Scale input size to final UI size
62    /// TODO: change metadata to use u16
63    pub fn scale(&self, value: u16) -> u16 { self.metadata.scale(value as u32) as u16 }
64}
65
66macro_rules! iced_fonts {
67    ($([ $( $name:ident$(,)? )* ])*) => {
68        $(
69            pub struct IcedFonts {
70                $(pub $name: IcedFont,)*
71            }
72
73            impl IcedFonts {
74                pub fn load(fonts: &i18n::Fonts, ui: &mut crate::ui::ice::IcedUi) -> Result<Self, assets::Error> {
75                    Ok(Self {
76                        $( $name: IcedFont::new(fonts.get(stringify!($name)).unwrap(), ui)?, )*
77                    })
78                }
79            }
80        )*
81    };
82}
83
84iced_fonts! {
85    [universal, alkhemi, cyri]
86}
87
88#[cfg(test)]
89mod tests {
90    use super::*;
91    use conrod_core::text::Font as ConrodFont;
92    use glyph_brush::ab_glyph::FontArc as GlyphFont;
93
94    #[test]
95    fn test_font_manifests() {
96        let lang_list = i18n::list_localizations();
97        for meta in lang_list {
98            let lang = i18n::LocalizationHandle::load_expect(&meta.language_identifier);
99            let lang = lang.read();
100            let fonts = lang.fonts();
101
102            // conrod check
103            for font in fonts.values() {
104                let raw_font = RawFont::load(&font.asset_key).unwrap().cloned();
105                let _ = ConrodFont::from_bytes(raw_font.0).unwrap();
106            }
107
108            // iced check
109            for font in fonts.values() {
110                let raw_font = RawFont::load(&font.asset_key).unwrap().cloned();
111                let _ = GlyphFont::try_from_vec(raw_font.0).unwrap();
112            }
113        }
114    }
115}