fa-iced 0.5.6

A Font Awesome library written in Rust for the iced UI framework.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//!
//! Rust-Iced-FA
//!
//! Wrapper for Font Awesome icons.
//!

use iced::advanced::graphics::text::font_system;
use iced::font::{Family, Weight};
use iced::widget::text;
use iced::{Element, Font};
use std::borrow::Cow;
use std::fmt;

//
// TODO: FUTURE API
//
// pub enum FaStyle {
//     Regular,
//     Solid,
//     Brands,
// }
// pub const FONT_FA_REGULAR: Font = Font {
//     family: Family::Name("Font Awesome 6 Free"),
//     ..Font::DEFAULT
// };
//
// pub const FONT_FA_SOLID: Font = Font {
//     family: Family::Name("Font Awesome 6 Free Solid"),
//     ..Font::DEFAULT
// };
//
// pub const FONT_FA_BRANDS: Font = Font {
//     family: Family::Name("Font Awesome 6 Brands"),
//     ..Font::DEFAULT
// };
//
// pub fn fa_font(style: FaStyle) -> Font {
//     match style {
//         FaStyle::Regular => FONT_FA_REGULAR,
//         FaStyle::Solid => FONT_FA_SOLID,
//         FaStyle::Brands => FONT_FA_BRANDS,
//     }
// }
//
// pub fn iced_fa_icon<'a, Message>(code: &str, style: FaStyle) -> Element<'a, Message> {
//     let code_u32 = u32::from_str_radix(code, 16).unwrap();
//     let unicode_char = char::from_u32(code_u32).unwrap();
//
//     text(unicode_char)
//         .font(fa_font(style))
//         .size(32)
//         .into()
// }

///
/// Load Font Awesome TTF files. Should only be called once.
///
pub fn load_font_fontawesome_ttf() {
    let mut font_system = font_system().write().unwrap();
    font_system.load_font(Cow::from(FONT_DATA_FA_REGULAR_TTF));
    font_system.load_font(Cow::from(FONT_DATA_FA_BRANDS_TTF));
    font_system.load_font(Cow::from(FONT_DATA_FA_SOLID_TTF));
}

///
/// Load Font Awesome OTF files. Should only be called once.
///
pub fn load_font_fontawesome_otf() {
    let mut font_system = font_system().write().unwrap();
    font_system.load_font(Cow::from(FONT_DATA_FA_REGULAR_TTF));
    font_system.load_font(Cow::from(FONT_DATA_FA_BRANDS_TTF));
    font_system.load_font(Cow::from(FONT_DATA_FA_SOLID_TTF));
}

///
/// Create an iced `text` element containing the specified Font Awesome icon.
///
/// Uses `FONT_FA_REGULAR`.
///
pub fn iced_text_icon_regular<'a, Message>(code: &str, size: u16) -> Element<'a, Message> {
    text(fa_unicode_char(&code))
        .font(FONT_FA_REGULAR)
        .size(size)
        .into()
}

///
/// Create an iced `text` element containing the specified Font Awesome icon.
///
/// Uses `FONT_FA_SOLID`.
///
pub fn iced_text_icon_solid<'a, Message>(code: &str, size: u16) -> Element<'a, Message> {
    text(fa_unicode_char(&code))
        .font(FONT_FA_SOLID)
        .size(size)
        .into()
}

///
/// Convert the Font Awesome `code` into the corresponding Unicode char.
///
pub fn fa_unicode_char(code: &str) -> char {
    let code_u32 = u32::from_str_radix(&code, 16).unwrap();
    let unicode_char = char::from_u32(code_u32).unwrap();
    unicode_char
}

///
/// The "Regular" version of Font Awesome version 6 Free.
///
pub const FONT_FA_REGULAR: Font = Font {
    family: Family::Name("Font Awesome 6 Free"),
    ..Font::DEFAULT
};

///
/// The "Solid" version of Font Awesome version 6 Free.
///
pub const FONT_FA_SOLID: Font = Font {
    family: Family::Name("Font Awesome 6 Free"),
    weight: Weight::Black, // Solid weights are bold
    ..Font::DEFAULT
};

//
// Font data
//

///
/// Font data for Font Awesome Regular OTF. Loaded using `include_bytes!()`.
///
pub const FONT_DATA_FA_REGULAR_OTF: &[u8] =
    include_bytes!("../fonts/font-awesome-6-free-regular-400.otf");

///
/// Font data for Font Awesome Brands OTF. Loaded using `include_bytes!()`.
///
pub const FONT_DATA_FA_BRANDS_OTF: &[u8] =
    include_bytes!("../fonts/font-awesome-6-brands-regular-400.otf");

///
/// Font data for Font Awesome Solid OTF. Loaded using `include_bytes!()`.
///
pub const FONT_DATA_FA_SOLID_OTF: &[u8] =
    include_bytes!("../fonts/font-awesome-6-free-solid-900.otf");

///
/// Font data for Font Awesome Regular TTF. Loaded using `include_bytes!()`.
///
pub const FONT_DATA_FA_REGULAR_TTF: &[u8] = include_bytes!("../fonts/fa-regular-400.ttf");

///
/// Font data for Font Awesome Brands TTF. Loaded using `include_bytes!()`.
///
pub const FONT_DATA_FA_BRANDS_TTF: &[u8] = include_bytes!("../fonts/fa-brands-400.ttf");

///
/// Font data for Font Awesome Solid TTF. Loaded using `include_bytes!()`.
///
pub const FONT_DATA_FA_SOLID_TTF: &[u8] = include_bytes!("../fonts/fa-solid-900.ttf");

//
// File operations
//

/// Font Awesome Unicode string for `https://fontawesome.com/icons/file`.
pub const FA_ICON_NEW: &str = "f15b";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/folder-open`.
pub const FA_ICON_OPEN: &str = "f07c";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/floppy-disk`.
pub const FA_ICON_SAVE: &str = "f0c7";

//
// Numbers
//

/// Font Awesome Unicode string for `https://fontawesome.com/icons/0`.
pub const FA_ICON_0: &str = "30";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/1`.
pub const FA_ICON_1: &str = "31";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/2`.
pub const FA_ICON_2: &str = "32";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/3`.
pub const FA_ICON_3: &str = "33";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/4`.
pub const FA_ICON_4: &str = "34";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/5`.
pub const FA_ICON_5: &str = "35";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/6`.
pub const FA_ICON_6: &str = "36";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/7`.
pub const FA_ICON_7: &str = "37";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/8`.
pub const FA_ICON_8: &str = "38";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/9`.
pub const FA_ICON_9: &str = "39";

//
// Circle
//

/// Font Awesome Unicode string for `https://fontawesome.com/icons/circle-check`
pub const FA_ICON_CIRCLE_CHECK: &str = "f058";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/circle-xmark`
pub const FA_ICON_CIRCLE_XMARK: &str = "f057";

//
// Settings/Options/Utility
//

/// Font Awesome Unicode string for `https://fontawesome.com/icons/bars`
pub const FA_ICON_BARS: &str = "f0c9";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/gear`
///
/// Only available in SOLID variant.
pub const FA_ICON_GEAR: &str = "f013";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/screwdriver-wrench`
///
/// Only available in SOLID variant.
pub const FA_ICON_SCREWDRIVER_WRENCH: &str = "f7d9";

//
// ID cards and badges
//

/// Font Awesome Unicode string for `https://fontawesome.com/icons/id-card`
pub const FA_ICON_ID_CARD: &str = "f2c2";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/id-card-clip`
pub const FA_ICON_ID_CARD_CLIP: &str = "f47f";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/id-badge`
pub const FA_ICON_ID_BADGE: &str = "f2c1";

//
// Users
//

/// Font Awesome Unicode string for `https://fontawesome.com/icons/user`.
pub const FA_ICON_USER: &str = "f007";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/users`.
pub const FA_ICON_USERS: &str = "f0c0";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/users-between-lines`.
pub const FA_ICON_USERS_BETWEEN_LINES: &str = "e591";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/users-line`.
pub const FA_ICON_USERS_LINE: &str = "e592";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/users-rectangle`.
pub const FA_ICON_USERS_RECTANGLE: &str = "e594";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/user_minus`
pub const FA_ICON_USER_MINUS: &str = "f503";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/user_plus`
pub const FA_ICON_USER_PLUS: &str = "f234";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/user_xmark`
pub const FA_ICON_USER_XMARK: &str = "f235";

//
// Edit
//

/// Font Awesome Unicode string for `https://fontawesome.com/icons/pen-to-square`
pub const FA_ICON_EDIT: &str = "f044";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/minus`
pub const FA_ICON_MINUS: &str = "f068";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/square-minus`
pub const FA_ICON_SQUARE_MINUS: &str = "f146";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/circle-minus`
pub const FA_ICON_CIRCLE_MINUS: &str = "f056";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/shield-minus`
pub const FA_ICON_SHIELD_MINUS: &str = "e249";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/plus`
pub const FA_ICON_PLUS: &str = "2b";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/square-plus`
pub const FA_ICON_SQUARE_PLUS: &str = "f0fe";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/circle-plus`
pub const FA_ICON_CIRCLE_PLUS: &str = "f055";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/shield-plus`
pub const FA_ICON_SHIELD_PLUS: &str = "e24a";

//
// List Icons
//

/// Font Awesome Unicode string for `https://fontawesome.com/icons/list`
pub const FA_ICON_LIST: &str = "f03a";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/list-ul`
pub const FA_ICON_LIST_UL: &str = "f0ca";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/list-ol`
pub const FA_ICON_LIST_OL: &str = "f0cb";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/list-tree`
pub const FA_ICON_LIST_TREE: &str = "e1d2";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/list-timeline`
pub const FA_ICON_LIST_TIMELINE: &str = "e1d1";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/list-radio`
pub const FA_ICON_LIST_RADIO: &str = "e1d0";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/table-list`
pub const FA_ICON_TABLE_LIST: &str = "f00b";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/square-list`
pub const FA_ICON_SQUARE_LIST: &str = "e489";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/rectangle-list`
pub const FA_ICON_RECTANGLE_LIST: &str = "f022";

//
// Info
//

/// Font Awesome Unicode string for `https://fontawesome.com/icons/info`
pub const FA_ICON_INFO: &str = "f129";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/circle-info`
pub const FA_ICON_CIRCLE_INFO: &str = "f05a";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/square-info`
pub const FA_ICON_SQUARE_INFO: &str = "f30f";

//
// Question
//

/// Font Awesome Unicode string for `https://fontawesome.com/icons/question`
pub const FA_ICON_QUESTION: &str = "3f";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/circle-question`
pub const FA_ICON_CIRCLE_QUESTION: &str = "f059";

/// Font Awesome Unicode string for `https://fontawesome.com/icons/square-question`
pub const FA_ICON_SQUARE_QUESTION: &str = "f2fd";

//
// FaIcon Enum
//

///
/// Implementation of Font Awesome icons as enum.
///
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FaIcon {
    User,
    CircleCheck,
    CircleXmark,
    ScrewdriverWrench,
    Gear,
    Bars,
    New,
    Open,
    Save,
    Number0,
    Number1,
    Number2,
    Number3,
    Number4,
    Number5,
    Number6,
    Number7,
    Number8,
    Number9,
    IdCard,
    IdCardClip,
    IdBadge,
}

impl fmt::Display for FaIcon {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl FaIcon {
    pub fn as_str(&self) -> &str {
        match self {
            FaIcon::User => "f007",
            FaIcon::CircleCheck => "f058",
            FaIcon::CircleXmark => "f057",
            FaIcon::ScrewdriverWrench => "f7d9",
            FaIcon::Gear => "f013",
            FaIcon::Bars => "f0c9",
            FaIcon::New => "f15b",
            FaIcon::Open => "f07c",
            FaIcon::Save => "f0c7",
            FaIcon::Number0 => "30",
            FaIcon::Number1 => "31",
            FaIcon::Number2 => "32",
            FaIcon::Number3 => "33",
            FaIcon::Number4 => "34",
            FaIcon::Number5 => "35",
            FaIcon::Number6 => "36",
            FaIcon::Number7 => "37",
            FaIcon::Number8 => "38",
            FaIcon::Number9 => "39",
            FaIcon::IdCard => "f2c2",
            FaIcon::IdCardClip => "f47f",
            FaIcon::IdBadge => "f2c1",
        }
    }
}