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 text(fa_unicode_char(&code))
82 .font(FONT_FA_REGULAR)
83 .size(size)
84 .into()
85}
86
87///
88/// Create an iced `text` element containing the specified Font Awesome icon.
89///
90/// Uses `FONT_FA_SOLID`.
91///
92pub fn iced_text_icon_solid<'a, Message>(code: &str, size: u16) -> Element<'a, Message> {
93 text(fa_unicode_char(&code))
94 .font(FONT_FA_SOLID)
95 .size(size)
96 .into()
97}
98
99///
100/// Convert the Font Awesome `code` into the corresponding Unicode char.
101///
102pub fn fa_unicode_char(code: &str) -> char {
103 let code_u32 = u32::from_str_radix(&code, 16).unwrap();
104 let unicode_char = char::from_u32(code_u32).unwrap();
105 unicode_char
106}
107
108///
109/// The "Regular" version of Font Awesome version 6 Free.
110///
111pub const FONT_FA_REGULAR: Font = Font {
112 family: Family::Name("Font Awesome 6 Free"),
113 ..Font::DEFAULT
114};
115
116///
117/// The "Solid" version of Font Awesome version 6 Free.
118///
119pub const FONT_FA_SOLID: Font = Font {
120 family: Family::Name("Font Awesome 6 Free"),
121 weight: Weight::Black, // Solid weights are bold
122 ..Font::DEFAULT
123};
124
125//
126// Font data
127//
128
129///
130/// Font data for Font Awesome Regular OTF. Loaded using `include_bytes!()`.
131///
132pub const FONT_DATA_FA_REGULAR_OTF: &[u8] =
133 include_bytes!("../fonts/font-awesome-6-free-regular-400.otf");
134
135///
136/// Font data for Font Awesome Brands OTF. Loaded using `include_bytes!()`.
137///
138pub const FONT_DATA_FA_BRANDS_OTF: &[u8] =
139 include_bytes!("../fonts/font-awesome-6-brands-regular-400.otf");
140
141///
142/// Font data for Font Awesome Solid OTF. Loaded using `include_bytes!()`.
143///
144pub const FONT_DATA_FA_SOLID_OTF: &[u8] =
145 include_bytes!("../fonts/font-awesome-6-free-solid-900.otf");
146
147///
148/// Font data for Font Awesome Regular TTF. Loaded using `include_bytes!()`.
149///
150pub const FONT_DATA_FA_REGULAR_TTF: &[u8] = include_bytes!("../fonts/fa-regular-400.ttf");
151
152///
153/// Font data for Font Awesome Brands TTF. Loaded using `include_bytes!()`.
154///
155pub const FONT_DATA_FA_BRANDS_TTF: &[u8] = include_bytes!("../fonts/fa-brands-400.ttf");
156
157///
158/// Font data for Font Awesome Solid TTF. Loaded using `include_bytes!()`.
159///
160pub const FONT_DATA_FA_SOLID_TTF: &[u8] = include_bytes!("../fonts/fa-solid-900.ttf");
161
162//
163// File operations
164//
165
166/// Font Awesome Unicode string for `https://fontawesome.com/icons/file`.
167pub const FA_ICON_NEW: &str = "f15b";
168
169/// Font Awesome Unicode string for `https://fontawesome.com/icons/folder-open`.
170pub const FA_ICON_OPEN: &str = "f07c";
171
172/// Font Awesome Unicode string for `https://fontawesome.com/icons/floppy-disk`.
173pub const FA_ICON_SAVE: &str = "f0c7";
174
175//
176// Numbers
177//
178
179/// Font Awesome Unicode string for `https://fontawesome.com/icons/0`.
180pub const FA_ICON_0: &str = "30";
181
182/// Font Awesome Unicode string for `https://fontawesome.com/icons/1`.
183pub const FA_ICON_1: &str = "31";
184
185/// Font Awesome Unicode string for `https://fontawesome.com/icons/2`.
186pub const FA_ICON_2: &str = "32";
187
188/// Font Awesome Unicode string for `https://fontawesome.com/icons/3`.
189pub const FA_ICON_3: &str = "33";
190
191/// Font Awesome Unicode string for `https://fontawesome.com/icons/4`.
192pub const FA_ICON_4: &str = "34";
193
194/// Font Awesome Unicode string for `https://fontawesome.com/icons/5`.
195pub const FA_ICON_5: &str = "35";
196
197/// Font Awesome Unicode string for `https://fontawesome.com/icons/6`.
198pub const FA_ICON_6: &str = "36";
199
200/// Font Awesome Unicode string for `https://fontawesome.com/icons/7`.
201pub const FA_ICON_7: &str = "37";
202
203/// Font Awesome Unicode string for `https://fontawesome.com/icons/8`.
204pub const FA_ICON_8: &str = "38";
205
206/// Font Awesome Unicode string for `https://fontawesome.com/icons/9`.
207pub const FA_ICON_9: &str = "39";
208
209//
210// Circle
211//
212
213/// Font Awesome Unicode string for `https://fontawesome.com/icons/circle-check`
214pub const FA_ICON_CIRCLE_CHECK: &str = "f058";
215
216/// Font Awesome Unicode string for `https://fontawesome.com/icons/circle-xmark`
217pub const FA_ICON_CIRCLE_XMARK: &str = "f057";
218
219//
220// Settings/Options/Utility
221//
222
223/// Font Awesome Unicode string for `https://fontawesome.com/icons/bars`
224pub const FA_ICON_BARS: &str = "f0c9";
225
226/// Font Awesome Unicode string for `https://fontawesome.com/icons/gear`
227///
228/// Only available in SOLID variant.
229pub const FA_ICON_GEAR: &str = "f013";
230
231/// Font Awesome Unicode string for `https://fontawesome.com/icons/screwdriver-wrench`
232///
233/// Only available in SOLID variant.
234pub const FA_ICON_SCREWDRIVER_WRENCH: &str = "f7d9";
235
236//
237// ID cards and badges
238//
239
240/// Font Awesome Unicode string for `https://fontawesome.com/icons/id-card`
241pub const FA_ICON_ID_CARD: &str = "f2c2";
242
243/// Font Awesome Unicode string for `https://fontawesome.com/icons/id-card-clip`
244pub const FA_ICON_ID_CARD_CLIP: &str = "f47f";
245
246/// Font Awesome Unicode string for `https://fontawesome.com/icons/id-badge`
247pub const FA_ICON_ID_BADGE: &str = "f2c1";
248
249//
250// Users
251//
252
253/// Font Awesome Unicode string for `https://fontawesome.com/icons/user`.
254pub const FA_ICON_USER: &str = "f007";
255
256/// Font Awesome Unicode string for `https://fontawesome.com/icons/users`.
257pub const FA_ICON_USERS: &str = "f0c0";
258
259/// Font Awesome Unicode string for `https://fontawesome.com/icons/users-between-lines`.
260pub const FA_ICON_USERS_BETWEEN_LINES: &str = "e591";
261
262/// Font Awesome Unicode string for `https://fontawesome.com/icons/users-line`.
263pub const FA_ICON_USERS_LINE: &str = "e592";
264
265/// Font Awesome Unicode string for `https://fontawesome.com/icons/users-rectangle`.
266pub const FA_ICON_USERS_RECTANGLE: &str = "e594";
267
268/// Font Awesome Unicode string for `https://fontawesome.com/icons/user_minus`
269pub const FA_ICON_USER_MINUS: &str = "f503";
270
271/// Font Awesome Unicode string for `https://fontawesome.com/icons/user_plus`
272pub const FA_ICON_USER_PLUS: &str = "f234";
273
274/// Font Awesome Unicode string for `https://fontawesome.com/icons/user_xmark`
275pub const FA_ICON_USER_XMARK: &str = "f235";
276
277//
278// Edit
279//
280
281/// Font Awesome Unicode string for `https://fontawesome.com/icons/pen-to-square`
282pub const FA_ICON_EDIT: &str = "f044";
283
284/// Font Awesome Unicode string for `https://fontawesome.com/icons/minus`
285pub const FA_ICON_MINUS: &str = "f068";
286
287/// Font Awesome Unicode string for `https://fontawesome.com/icons/square-minus`
288pub const FA_ICON_SQUARE_MINUS: &str = "f146";
289
290/// Font Awesome Unicode string for `https://fontawesome.com/icons/circle-minus`
291pub const FA_ICON_CIRCLE_MINUS: &str = "f056";
292
293/// Font Awesome Unicode string for `https://fontawesome.com/icons/shield-minus`
294pub const FA_ICON_SHIELD_MINUS: &str = "e249";
295
296/// Font Awesome Unicode string for `https://fontawesome.com/icons/plus`
297pub const FA_ICON_PLUS: &str = "2b";
298
299/// Font Awesome Unicode string for `https://fontawesome.com/icons/square-plus`
300pub const FA_ICON_SQUARE_PLUS: &str = "f0fe";
301
302/// Font Awesome Unicode string for `https://fontawesome.com/icons/circle-plus`
303pub const FA_ICON_CIRCLE_PLUS: &str = "f055";
304
305/// Font Awesome Unicode string for `https://fontawesome.com/icons/shield-plus`
306pub const FA_ICON_SHIELD_PLUS: &str = "e24a";
307
308//
309// List Icons
310//
311
312/// Font Awesome Unicode string for `https://fontawesome.com/icons/list`
313pub const FA_ICON_LIST: &str = "f03a";
314
315/// Font Awesome Unicode string for `https://fontawesome.com/icons/list-ul`
316pub const FA_ICON_LIST_UL: &str = "f0ca";
317
318/// Font Awesome Unicode string for `https://fontawesome.com/icons/list-ol`
319pub const FA_ICON_LIST_OL: &str = "f0cb";
320
321/// Font Awesome Unicode string for `https://fontawesome.com/icons/list-tree`
322pub const FA_ICON_LIST_TREE: &str = "e1d2";
323
324/// Font Awesome Unicode string for `https://fontawesome.com/icons/list-timeline`
325pub const FA_ICON_LIST_TIMELINE: &str = "e1d1";
326
327/// Font Awesome Unicode string for `https://fontawesome.com/icons/list-radio`
328pub const FA_ICON_LIST_RADIO: &str = "e1d0";
329
330/// Font Awesome Unicode string for `https://fontawesome.com/icons/table-list`
331pub const FA_ICON_TABLE_LIST: &str = "f00b";
332
333/// Font Awesome Unicode string for `https://fontawesome.com/icons/square-list`
334pub const FA_ICON_SQUARE_LIST: &str = "e489";
335
336/// Font Awesome Unicode string for `https://fontawesome.com/icons/rectangle-list`
337pub const FA_ICON_RECTANGLE_LIST: &str = "f022";
338
339//
340// Info
341//
342
343/// Font Awesome Unicode string for `https://fontawesome.com/icons/info`
344pub const FA_ICON_INFO: &str = "f129";
345
346/// Font Awesome Unicode string for `https://fontawesome.com/icons/circle-info`
347pub const FA_ICON_CIRCLE_INFO: &str = "f05a";
348
349/// Font Awesome Unicode string for `https://fontawesome.com/icons/square-info`
350pub const FA_ICON_SQUARE_INFO: &str = "f30f";
351
352//
353// Question
354//
355
356/// Font Awesome Unicode string for `https://fontawesome.com/icons/question`
357pub const FA_ICON_QUESTION: &str = "3f";
358
359/// Font Awesome Unicode string for `https://fontawesome.com/icons/circle-question`
360pub const FA_ICON_CIRCLE_QUESTION: &str = "f059";
361
362/// Font Awesome Unicode string for `https://fontawesome.com/icons/square-question`
363pub const FA_ICON_SQUARE_QUESTION: &str = "f2fd";
364
365//
366// FaIcon Enum
367//
368
369///
370/// Implementation of Font Awesome icons as enum.
371///
372#[derive(Debug, Clone, Copy, PartialEq, Eq)]
373pub enum FaIcon {
374 User,
375 CircleCheck,
376 CircleXmark,
377 ScrewdriverWrench,
378 Gear,
379 Bars,
380 New,
381 Open,
382 Save,
383 Number0,
384 Number1,
385 Number2,
386 Number3,
387 Number4,
388 Number5,
389 Number6,
390 Number7,
391 Number8,
392 Number9,
393 IdCard,
394 IdCardClip,
395 IdBadge,
396}
397
398impl fmt::Display for FaIcon {
399 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
400 f.write_str(self.as_str())
401 }
402}
403
404impl FaIcon {
405 pub fn as_str(&self) -> &str {
406 match self {
407 FaIcon::User => "f007",
408 FaIcon::CircleCheck => "f058",
409 FaIcon::CircleXmark => "f057",
410 FaIcon::ScrewdriverWrench => "f7d9",
411 FaIcon::Gear => "f013",
412 FaIcon::Bars => "f0c9",
413 FaIcon::New => "f15b",
414 FaIcon::Open => "f07c",
415 FaIcon::Save => "f0c7",
416 FaIcon::Number0 => "30",
417 FaIcon::Number1 => "31",
418 FaIcon::Number2 => "32",
419 FaIcon::Number3 => "33",
420 FaIcon::Number4 => "34",
421 FaIcon::Number5 => "35",
422 FaIcon::Number6 => "36",
423 FaIcon::Number7 => "37",
424 FaIcon::Number8 => "38",
425 FaIcon::Number9 => "39",
426 FaIcon::IdCard => "f2c2",
427 FaIcon::IdCardClip => "f47f",
428 FaIcon::IdBadge => "f2c1",
429 }
430 }
431}