fa_iced/
lib.rs

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