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
42//
43// Font data
44//
45
46const FONT_DATA_FA_REGULAR: &[u8] =
47    include_bytes!("../fonts/font-awesome-6-free-regular-400.otf");
48
49const FONT_DATA_FA_BRANDS: &[u8] =
50    include_bytes!("../fonts/font-awesome-6-brands-regular-400.otf");
51
52const FONT_DATA_FA_SOLID: &[u8] =
53    include_bytes!("../fonts/font-awesome-6-free-solid-900.otf");
54
55//
56// File operations
57//
58
59/// Font Awesome Unicode string for `https://fontawesome.com/icons/user`.
60pub const FA_ICON_USER: &str = "f007";
61
62/// Font Awesome Unicode string for `https://fontawesome.com/icons/file`.
63pub const FA_ICON_NEW: &str = "f15b";
64
65/// Font Awesome Unicode string for `https://fontawesome.com/icons/folder-open`.
66pub const FA_ICON_OPEN: &str = "f07c";
67
68/// Font Awesome Unicode string for `https://fontawesome.com/icons/floppy-disk`.
69pub const FA_ICON_SAVE: &str = "f0c7";
70
71//
72// Numbers
73//
74
75/// Font Awesome Unicode string for `https://fontawesome.com/icons/0`.
76pub const FA_ICON_0: &str = "30";
77
78/// Font Awesome Unicode string for `https://fontawesome.com/icons/1`.
79pub const FA_ICON_1: &str = "31";
80
81/// Font Awesome Unicode string for `https://fontawesome.com/icons/2`.
82pub const FA_ICON_2: &str = "32";
83
84/// Font Awesome Unicode string for `https://fontawesome.com/icons/3`.
85pub const FA_ICON_3: &str = "33";
86
87/// Font Awesome Unicode string for `https://fontawesome.com/icons/4`.
88pub const FA_ICON_4: &str = "34";
89
90/// Font Awesome Unicode string for `https://fontawesome.com/icons/5`.
91pub const FA_ICON_5: &str = "35";
92
93/// Font Awesome Unicode string for `https://fontawesome.com/icons/6`.
94pub const FA_ICON_6: &str = "36";
95
96/// Font Awesome Unicode string for `https://fontawesome.com/icons/7`.
97pub const FA_ICON_7: &str = "37";
98
99/// Font Awesome Unicode string for `https://fontawesome.com/icons/8`.
100pub const FA_ICON_8: &str = "38";
101
102/// Font Awesome Unicode string for `https://fontawesome.com/icons/9`.
103pub const FA_ICON_9: &str = "39";
104
105//
106// Circle
107//
108
109/// Font Awesome Unicode string for https://fontawesome.com/icons/circle-check
110pub const FA_ICON_CIRCLE_CHECK: &str = "f058";
111
112/// Font Awesome Unicode string for https://fontawesome.com/icons/circle-xmark
113pub const FA_ICON_CIRCLE_XMARK: &str = "f057";
114
115//
116// Settings/Options/Utility
117//
118
119/// Font Awesome Unicode string for https://fontawesome.com/icons/screwdriver-wrench
120pub const FA_ICON_CIRCLE_BARS: &str = "f0c9";
121
122/// Font Awesome Unicode string for https://fontawesome.com/icons/gear
123pub const FA_ICON_CIRCLE_GEAR: &str = "f013";
124
125/// Font Awesome Unicode string for https://fontawesome.com/icons/screwdriver-wrench
126pub const FA_ICON_CIRCLE_SCREWDRIVER_WRENCH: &str = "f7d9";