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, Weight};
11use iced::widget::text;
12use std::fmt;
13
14
15//
16// TODO: FUTURE API
17//
18// pub enum FaStyle {
19//     Regular,
20//     Solid,
21//     Brands,
22// }
23// pub const FONT_FA_REGULAR: Font = Font {
24//     family: Family::Name("Font Awesome 6 Free"),
25//     ..Font::DEFAULT
26// };
27//
28// pub const FONT_FA_SOLID: Font = Font {
29//     family: Family::Name("Font Awesome 6 Free Solid"),
30//     ..Font::DEFAULT
31// };
32//
33// pub const FONT_FA_BRANDS: Font = Font {
34//     family: Family::Name("Font Awesome 6 Brands"),
35//     ..Font::DEFAULT
36// };
37// 
38// pub fn fa_font(style: FaStyle) -> Font {
39//     match style {
40//         FaStyle::Regular => FONT_FA_REGULAR,
41//         FaStyle::Solid => FONT_FA_SOLID,
42//         FaStyle::Brands => FONT_FA_BRANDS,
43//     }
44// }
45// 
46// pub fn iced_fa_icon<'a, Message>(code: &str, style: FaStyle) -> Element<'a, Message> {
47//     let code_u32 = u32::from_str_radix(code, 16).unwrap();
48//     let unicode_char = char::from_u32(code_u32).unwrap();
49// 
50//     text(unicode_char)
51//         .font(fa_font(style))
52//         .size(32)
53//         .into()
54// }
55
56
57
58///
59/// Load Font Awesome files. Should only be called once.
60///
61pub fn load_font_fontawesome() {
62    let mut font_system = font_system().write().unwrap();
63    font_system.load_font(Cow::from(FONT_DATA_FA_REGULAR_TTF));
64    font_system.load_font(Cow::from(FONT_DATA_FA_BRANDS_TTF));
65    font_system.load_font(Cow::from(FONT_DATA_FA_SOLID_TTF));
66}
67
68///
69/// Create an iced `text` element containing the specified Font Awesome icon.
70///
71/// Uses `FONT_FA_REGULAR`.
72///
73pub fn iced_text_icon_regular<'a, Message>(code: &str) -> Element<'a, Message> {
74    let code_u32 = u32::from_str_radix(&code, 16).unwrap();
75    let unicode_char = char::from_u32(code_u32).unwrap();
76    text(unicode_char).font(FONT_FA_REGULAR).into()
77}
78
79///
80/// Create an iced `text` element containing the specified Font Awesome icon.
81///
82/// Uses `FONT_FA_SOLID`.
83///
84pub fn iced_text_icon_solid<'a, Message>(code: &str) -> Element<'a, Message> {
85    let code_u32 = u32::from_str_radix(&code, 16).unwrap();
86    let unicode_char = char::from_u32(code_u32).unwrap();
87    text(unicode_char).font(FONT_FA_SOLID).into()
88}
89
90///
91/// The "Regular" version of Font Awesome version 6.
92///
93pub const FONT_FA_REGULAR: Font = Font {
94    family: Family::Name("Font Awesome 6 Free"),
95    ..Font::DEFAULT
96};
97
98///
99/// The "Regular" version of Font Awesome version 6.
100///
101// pub const FONT_FA_SOLID: Font = Font {
102//     family: Family::Name("Font Awesome 6 Free"),
103//     ..Font::DEFAULT
104// };
105
106pub const FONT_FA_SOLID: Font = Font {
107    family: Family::Name("Font Awesome 6 Free"),
108    weight: Weight::Bold, // Solid weights are bold
109    ..Font::DEFAULT
110};
111
112//
113// Font data
114//
115
116pub const FONT_DATA_FA_REGULAR_OTF: &[u8] =
117    include_bytes!("../fonts/font-awesome-6-free-regular-400.otf");
118
119pub const FONT_DATA_FA_BRANDS_OTF: &[u8] =
120    include_bytes!("../fonts/font-awesome-6-brands-regular-400.otf");
121
122pub const FONT_DATA_FA_SOLID_OTF: &[u8] =
123    include_bytes!("../fonts/font-awesome-6-free-solid-900.otf");
124
125pub const FONT_DATA_FA_REGULAR_TTF: &[u8] =
126    include_bytes!("../fonts/fa-regular-400.ttf");
127
128pub const FONT_DATA_FA_BRANDS_TTF: &[u8] =
129    include_bytes!("../fonts/fa-brands-400.ttf");
130
131pub const FONT_DATA_FA_SOLID_TTF: &[u8] =
132    include_bytes!("../fonts/fa-solid-900.ttf");
133
134//
135// File operations
136//
137
138/// Font Awesome Unicode string for `https://fontawesome.com/icons/user`.
139pub const FA_ICON_USER: &str = "f007";
140
141/// Font Awesome Unicode string for `https://fontawesome.com/icons/file`.
142pub const FA_ICON_NEW: &str = "f15b";
143
144/// Font Awesome Unicode string for `https://fontawesome.com/icons/folder-open`.
145pub const FA_ICON_OPEN: &str = "f07c";
146
147/// Font Awesome Unicode string for `https://fontawesome.com/icons/floppy-disk`.
148pub const FA_ICON_SAVE: &str = "f0c7";
149
150//
151// Numbers
152//
153
154/// Font Awesome Unicode string for `https://fontawesome.com/icons/0`.
155pub const FA_ICON_0: &str = "30";
156
157/// Font Awesome Unicode string for `https://fontawesome.com/icons/1`.
158pub const FA_ICON_1: &str = "31";
159
160/// Font Awesome Unicode string for `https://fontawesome.com/icons/2`.
161pub const FA_ICON_2: &str = "32";
162
163/// Font Awesome Unicode string for `https://fontawesome.com/icons/3`.
164pub const FA_ICON_3: &str = "33";
165
166/// Font Awesome Unicode string for `https://fontawesome.com/icons/4`.
167pub const FA_ICON_4: &str = "34";
168
169/// Font Awesome Unicode string for `https://fontawesome.com/icons/5`.
170pub const FA_ICON_5: &str = "35";
171
172/// Font Awesome Unicode string for `https://fontawesome.com/icons/6`.
173pub const FA_ICON_6: &str = "36";
174
175/// Font Awesome Unicode string for `https://fontawesome.com/icons/7`.
176pub const FA_ICON_7: &str = "37";
177
178/// Font Awesome Unicode string for `https://fontawesome.com/icons/8`.
179pub const FA_ICON_8: &str = "38";
180
181/// Font Awesome Unicode string for `https://fontawesome.com/icons/9`.
182pub const FA_ICON_9: &str = "39";
183
184//
185// Circle
186//
187
188/// Font Awesome Unicode string for https://fontawesome.com/icons/circle-check
189pub const FA_ICON_CIRCLE_CHECK: &str = "f058";
190
191/// Font Awesome Unicode string for https://fontawesome.com/icons/circle-xmark
192pub const FA_ICON_CIRCLE_XMARK: &str = "f057";
193
194//
195// Settings/Options/Utility
196//
197
198/// Font Awesome Unicode string for https://fontawesome.com/icons/bars
199pub const FA_ICON_BARS: &str = "f0c9";
200
201/// Font Awesome Unicode string for https://fontawesome.com/icons/gear
202///
203/// Only available in SOLID variant.
204pub const FA_ICON_GEAR: &str = "f013";
205
206/// Font Awesome Unicode string for https://fontawesome.com/icons/screwdriver-wrench
207///
208/// Only available in SOLID variant.
209pub const FA_ICON_SCREWDRIVER_WRENCH: &str = "f7d9";
210
211
212///
213/// Implementation of Font Awesome icons as enum.
214///
215#[derive(Debug, Clone, Copy, PartialEq, Eq)]
216pub enum FaIcon {
217    User,
218    CircleCheck,
219    CircleXmark,
220    ScrewdriverWrench,
221    Gear,
222    Bars,
223    New,
224    Open,
225    Save,
226    Number0,
227    Number1,
228    Number2,
229    Number3,
230    Number4,
231    Number5,
232    Number6,
233    Number7,
234    Number8,
235    Number9,
236}
237
238impl fmt::Display for FaIcon {
239    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
240        f.write_str(self.as_str())
241    }
242}
243
244impl FaIcon {
245    pub fn as_str(&self) -> &str {
246        match self {
247            FaIcon::User => "f007",
248            FaIcon::CircleCheck => "f058",
249            FaIcon::CircleXmark => "f057",
250            FaIcon::ScrewdriverWrench => "f7d9",
251            FaIcon::Gear => "f013",
252            FaIcon::Bars => "f0c9",
253            FaIcon::New => "f15b",
254            FaIcon::Open => "f07c",
255            FaIcon::Save => "f0c7",
256            FaIcon::Number0 => "30",
257            FaIcon::Number1 => "31",
258            FaIcon::Number2 => "32",
259            FaIcon::Number3 => "33",
260            FaIcon::Number4 => "34",
261            FaIcon::Number5 => "35",
262            FaIcon::Number6 => "36",
263            FaIcon::Number7 => "37",
264            FaIcon::Number8 => "38",
265            FaIcon::Number9 => "39",
266        }
267    }
268}