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;
12use std::fmt;
13use std::str::FromStr;
14
15///
16/// Load Font Awesome files. Should only be called once.
17///
18pub fn load_font_fontawesome() {
19    let mut font_system = font_system().write().unwrap();
20    font_system.load_font(Cow::from(FONT_DATA_FA_REGULAR));
21    font_system.load_font(Cow::from(FONT_DATA_FA_BRANDS));
22    font_system.load_font(Cow::from(FONT_DATA_FA_SOLID));
23}
24
25///
26/// Create an iced `text` element containing the specified Font Awesome icon.
27///
28/// Uses `FONT_FA_REGULAR`.
29///
30pub fn iced_text_icon<'a, Message>(code: &str) -> Element<'a, Message> {
31    let code_u32 = u32::from_str_radix(&code, 16).unwrap();
32    let unicode_char = char::from_u32(code_u32).unwrap();
33    text(unicode_char).font(FONT_FA_REGULAR).into()
34}
35
36///
37/// The "Regular" version of Font Awesome version 6.
38///
39pub const FONT_FA_REGULAR: Font = Font {
40    family: Family::Name("Font Awesome 6 Free"),
41    ..Font::DEFAULT
42};
43
44//
45// Font data
46//
47
48const FONT_DATA_FA_REGULAR: &[u8] =
49    include_bytes!("../fonts/font-awesome-6-free-regular-400.otf");
50
51const FONT_DATA_FA_BRANDS: &[u8] =
52    include_bytes!("../fonts/font-awesome-6-brands-regular-400.otf");
53
54const FONT_DATA_FA_SOLID: &[u8] =
55    include_bytes!("../fonts/font-awesome-6-free-solid-900.otf");
56
57//
58// File operations
59//
60
61/// Font Awesome Unicode string for `https://fontawesome.com/icons/user`.
62pub const FA_ICON_USER: &str = "f007";
63
64/// Font Awesome Unicode string for `https://fontawesome.com/icons/file`.
65pub const FA_ICON_NEW: &str = "f15b";
66
67/// Font Awesome Unicode string for `https://fontawesome.com/icons/folder-open`.
68pub const FA_ICON_OPEN: &str = "f07c";
69
70/// Font Awesome Unicode string for `https://fontawesome.com/icons/floppy-disk`.
71pub const FA_ICON_SAVE: &str = "f0c7";
72
73//
74// Numbers
75//
76
77/// Font Awesome Unicode string for `https://fontawesome.com/icons/0`.
78pub const FA_ICON_0: &str = "30";
79
80/// Font Awesome Unicode string for `https://fontawesome.com/icons/1`.
81pub const FA_ICON_1: &str = "31";
82
83/// Font Awesome Unicode string for `https://fontawesome.com/icons/2`.
84pub const FA_ICON_2: &str = "32";
85
86/// Font Awesome Unicode string for `https://fontawesome.com/icons/3`.
87pub const FA_ICON_3: &str = "33";
88
89/// Font Awesome Unicode string for `https://fontawesome.com/icons/4`.
90pub const FA_ICON_4: &str = "34";
91
92/// Font Awesome Unicode string for `https://fontawesome.com/icons/5`.
93pub const FA_ICON_5: &str = "35";
94
95/// Font Awesome Unicode string for `https://fontawesome.com/icons/6`.
96pub const FA_ICON_6: &str = "36";
97
98/// Font Awesome Unicode string for `https://fontawesome.com/icons/7`.
99pub const FA_ICON_7: &str = "37";
100
101/// Font Awesome Unicode string for `https://fontawesome.com/icons/8`.
102pub const FA_ICON_8: &str = "38";
103
104/// Font Awesome Unicode string for `https://fontawesome.com/icons/9`.
105pub const FA_ICON_9: &str = "39";
106
107//
108// Circle
109//
110
111/// Font Awesome Unicode string for https://fontawesome.com/icons/circle-check
112pub const FA_ICON_CIRCLE_CHECK: &str = "f058";
113
114/// Font Awesome Unicode string for https://fontawesome.com/icons/circle-xmark
115pub const FA_ICON_CIRCLE_XMARK: &str = "f057";
116
117//
118// Settings/Options/Utility
119//
120
121/// Font Awesome Unicode string for https://fontawesome.com/icons/bars
122pub const FA_ICON_BARS: &str = "f0c9";
123
124/// Font Awesome Unicode string for https://fontawesome.com/icons/gear
125pub const FA_ICON_GEAR: &str = "f013";
126
127/// Font Awesome Unicode string for https://fontawesome.com/icons/screwdriver-wrench
128pub const FA_ICON_SCREWDRIVER_WRENCH: &str = "f7d9";
129
130
131///
132/// Implementation of Font Awesome icons as enum.
133///
134#[derive(Debug, Clone, Copy, PartialEq, Eq)]
135pub enum FaIcon {
136    User,
137    CircleCheck,
138    CircleXmark,
139    ScrewdriverWrench,
140    Gear,
141    Bars,
142    New,
143    Open,
144    Save,
145    Number0,
146    Number1,
147    Number2,
148    Number3,
149    Number4,
150    Number5,
151    Number6,
152    Number7,
153    Number8,
154    Number9,
155}
156
157impl fmt::Display for FaIcon {
158    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
159        f.write_str(self.as_str())
160    }
161}
162
163impl FaIcon {
164    pub fn as_str(&self) -> &str {
165        match self {
166            FaIcon::User => "f007",
167            FaIcon::CircleCheck => "f058",
168            FaIcon::CircleXmark => "f057",
169            FaIcon::ScrewdriverWrench => "f7d9",
170            FaIcon::Gear => "f013",
171            FaIcon::Bars => "f0c9",
172            FaIcon::New => "f15b",
173            FaIcon::Open => "f07c",
174            FaIcon::Save => "f0c7",
175            FaIcon::Number0 => "30",
176            FaIcon::Number1 => "31",
177            FaIcon::Number2 => "32",
178            FaIcon::Number3 => "33",
179            FaIcon::Number4 => "34",
180            FaIcon::Number5 => "35",
181            FaIcon::Number6 => "36",
182            FaIcon::Number7 => "37",
183            FaIcon::Number8 => "38",
184            FaIcon::Number9 => "39",
185        }
186    }
187}