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