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.
99///
100pub const FONT_FA_REGULAR: Font = Font {
101    family: Family::Name("Font Awesome 6 Free"),
102    ..Font::DEFAULT
103};
104
105///
106/// The "Regular" version of Font Awesome version 6.
107///
108// pub const FONT_FA_SOLID: Font = Font {
109//     family: Family::Name("Font Awesome 6 Free"),
110//     ..Font::DEFAULT
111// };
112
113pub const FONT_FA_SOLID: Font = Font {
114    family: Family::Name("Font Awesome 6 Free"),
115    weight: Weight::Black, // Solid weights are bold
116    ..Font::DEFAULT
117};
118
119//
120// Font data
121//
122
123pub const FONT_DATA_FA_REGULAR_OTF: &[u8] =
124    include_bytes!("../fonts/font-awesome-6-free-regular-400.otf");
125
126pub const FONT_DATA_FA_BRANDS_OTF: &[u8] =
127    include_bytes!("../fonts/font-awesome-6-brands-regular-400.otf");
128
129pub const FONT_DATA_FA_SOLID_OTF: &[u8] =
130    include_bytes!("../fonts/font-awesome-6-free-solid-900.otf");
131
132pub const FONT_DATA_FA_REGULAR_TTF: &[u8] = include_bytes!("../fonts/fa-regular-400.ttf");
133
134pub const FONT_DATA_FA_BRANDS_TTF: &[u8] = include_bytes!("../fonts/fa-brands-400.ttf");
135
136pub const FONT_DATA_FA_SOLID_TTF: &[u8] = include_bytes!("../fonts/fa-solid-900.ttf");
137
138//
139// File operations
140//
141
142/// Font Awesome Unicode string for `https://fontawesome.com/icons/user`.
143pub const FA_ICON_USER: &str = "f007";
144
145/// Font Awesome Unicode string for `https://fontawesome.com/icons/file`.
146pub const FA_ICON_NEW: &str = "f15b";
147
148/// Font Awesome Unicode string for `https://fontawesome.com/icons/folder-open`.
149pub const FA_ICON_OPEN: &str = "f07c";
150
151/// Font Awesome Unicode string for `https://fontawesome.com/icons/floppy-disk`.
152pub const FA_ICON_SAVE: &str = "f0c7";
153
154//
155// Numbers
156//
157
158/// Font Awesome Unicode string for `https://fontawesome.com/icons/0`.
159pub const FA_ICON_0: &str = "30";
160
161/// Font Awesome Unicode string for `https://fontawesome.com/icons/1`.
162pub const FA_ICON_1: &str = "31";
163
164/// Font Awesome Unicode string for `https://fontawesome.com/icons/2`.
165pub const FA_ICON_2: &str = "32";
166
167/// Font Awesome Unicode string for `https://fontawesome.com/icons/3`.
168pub const FA_ICON_3: &str = "33";
169
170/// Font Awesome Unicode string for `https://fontawesome.com/icons/4`.
171pub const FA_ICON_4: &str = "34";
172
173/// Font Awesome Unicode string for `https://fontawesome.com/icons/5`.
174pub const FA_ICON_5: &str = "35";
175
176/// Font Awesome Unicode string for `https://fontawesome.com/icons/6`.
177pub const FA_ICON_6: &str = "36";
178
179/// Font Awesome Unicode string for `https://fontawesome.com/icons/7`.
180pub const FA_ICON_7: &str = "37";
181
182/// Font Awesome Unicode string for `https://fontawesome.com/icons/8`.
183pub const FA_ICON_8: &str = "38";
184
185/// Font Awesome Unicode string for `https://fontawesome.com/icons/9`.
186pub const FA_ICON_9: &str = "39";
187
188//
189// Circle
190//
191
192/// Font Awesome Unicode string for https://fontawesome.com/icons/circle-check
193pub const FA_ICON_CIRCLE_CHECK: &str = "f058";
194
195/// Font Awesome Unicode string for https://fontawesome.com/icons/circle-xmark
196pub const FA_ICON_CIRCLE_XMARK: &str = "f057";
197
198//
199// Settings/Options/Utility
200//
201
202/// Font Awesome Unicode string for https://fontawesome.com/icons/bars
203pub const FA_ICON_BARS: &str = "f0c9";
204
205/// Font Awesome Unicode string for https://fontawesome.com/icons/gear
206///
207/// Only available in SOLID variant.
208pub const FA_ICON_GEAR: &str = "f013";
209
210/// Font Awesome Unicode string for https://fontawesome.com/icons/screwdriver-wrench
211///
212/// Only available in SOLID variant.
213pub const FA_ICON_SCREWDRIVER_WRENCH: &str = "f7d9";
214
215//
216// ID cards and badges
217//
218
219/// Font Awesome Unicode string for https://fontawesome.com/icons/id-card
220pub const FA_ICON_ID_CARD: &str = "f2c2";
221
222/// Font Awesome Unicode string for https://fontawesome.com/icons/id-card-clip
223pub const FA_ICON_ID_CARD_CLIP: &str = "f47f";
224
225/// Font Awesome Unicode string for https://fontawesome.com/icons/id-badge
226pub const FA_ICON_ID_BADGE: &str = "f2c1";
227
228//
229// Enum
230//
231
232///
233/// Implementation of Font Awesome icons as enum.
234///
235#[derive(Debug, Clone, Copy, PartialEq, Eq)]
236pub enum FaIcon {
237    User,
238    CircleCheck,
239    CircleXmark,
240    ScrewdriverWrench,
241    Gear,
242    Bars,
243    New,
244    Open,
245    Save,
246    Number0,
247    Number1,
248    Number2,
249    Number3,
250    Number4,
251    Number5,
252    Number6,
253    Number7,
254    Number8,
255    Number9,
256    IdCard,
257    IdCardClip,
258    IdBadge,
259}
260
261impl fmt::Display for FaIcon {
262    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
263        f.write_str(self.as_str())
264    }
265}
266
267impl FaIcon {
268    pub fn as_str(&self) -> &str {
269        match self {
270            FaIcon::User => "f007",
271            FaIcon::CircleCheck => "f058",
272            FaIcon::CircleXmark => "f057",
273            FaIcon::ScrewdriverWrench => "f7d9",
274            FaIcon::Gear => "f013",
275            FaIcon::Bars => "f0c9",
276            FaIcon::New => "f15b",
277            FaIcon::Open => "f07c",
278            FaIcon::Save => "f0c7",
279            FaIcon::Number0 => "30",
280            FaIcon::Number1 => "31",
281            FaIcon::Number2 => "32",
282            FaIcon::Number3 => "33",
283            FaIcon::Number4 => "34",
284            FaIcon::Number5 => "35",
285            FaIcon::Number6 => "36",
286            FaIcon::Number7 => "37",
287            FaIcon::Number8 => "38",
288            FaIcon::Number9 => "39",
289            FaIcon::IdCard => "f2c2",
290            FaIcon::IdCardClip => "f47f",
291            FaIcon::IdBadge => "f2c1",
292        }
293    }
294}