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