fa_iced/
lib.rs

1//!
2//! Rust-Iced-FA
3//!
4//! Wrapper for Font Awesome icons.
5//!
6
7use std::borrow::Cow;
8use iced::advanced::graphics::text::font_system;
9use iced::{Element, Font};
10use iced::font::Family;
11use iced::widget::text;
12
13///
14/// Load Font Awesome files. Should only be called once.
15///
16pub fn load_font_fontawesome() {
17    let mut font_system = font_system().write().unwrap();
18    font_system.load_font(Cow::from(FONT_DATA_FA_REGULAR));
19    font_system.load_font(Cow::from(FONT_DATA_FA_BRANDS));
20    font_system.load_font(Cow::from(FONT_DATA_FA_SOLID));
21}
22
23///
24/// Create an iced `text` element containing the specified Font Awesome icon.
25///
26/// Uses `FONT_FA_REGULAR`.
27///
28pub fn iced_text_icon<'a, Message>(code: &str) -> Element<'a, Message> {
29    let code_u32 = u32::from_str_radix(&code, 16).unwrap();
30    let unicode_char = char::from_u32(code_u32).unwrap();
31    text(unicode_char).font(FONT_FA_REGULAR).into()
32}
33
34///
35/// The "Regular" version of Font Awesome version 6.
36///
37pub const FONT_FA_REGULAR: Font = Font {
38    family: Family::Name("Font Awesome 6 Free"),
39    ..Font::DEFAULT
40};
41
42const FONT_DATA_FA_REGULAR: &[u8] =
43    include_bytes!("../fonts/font-awesome-6-free-regular-400.otf");
44
45const FONT_DATA_FA_BRANDS: &[u8] =
46    include_bytes!("../fonts/font-awesome-6-brands-regular-400.otf");
47
48const FONT_DATA_FA_SOLID: &[u8] =
49    include_bytes!("../fonts/font-awesome-6-free-solid-900.otf");
50
51///
52/// Font Awesome Unicode string for `https://fontawesome.com/icons/user`.
53///
54pub const FA_ICON_USER: &str = "f007";
55
56///
57/// Font Awesome Unicode string for `https://fontawesome.com/icons/file`.
58///
59pub const FA_ICON_NEW: &str = "f15b";
60
61///
62/// Font Awesome Unicode string for `https://fontawesome.com/icons/folder-open`.
63///
64pub const FA_ICON_OPEN: &str = "f07c";
65
66///
67/// Font Awesome Unicode string for `https://fontawesome.com/icons/floppy-disk`.
68///
69pub const FA_ICON_SAVE: &str = "f0c7";
70
71///
72/// Font Awesome Unicode string for `https://fontawesome.com/icons/1`.
73///
74pub const FA_ICON_0: &str = "30";
75pub const FA_ICON_1: &str = "31";
76pub const FA_ICON_2: &str = "32";
77pub const FA_ICON_3: &str = "33";
78pub const FA_ICON_4: &str = "34";
79pub const FA_ICON_5: &str = "35";
80pub const FA_ICON_6: &str = "36";
81pub const FA_ICON_7: &str = "37";
82pub const FA_ICON_8: &str = "38";
83pub const FA_ICON_9: &str = "39";
84