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
use futures::channel::oneshot::Sender;
use wasm_bindgen::JsValue;
#[derive(Debug)]
pub struct Font {
pub(crate) name: String
}
impl Font {
pub async fn load(url: &str) -> Result<Font, JsValue> {
use web_sys::FontFace;
use web_sys::window;
use wasm_bindgen_futures::JsFuture;
let window = window().unwrap();
let crypto = window.crypto()?;
let mut randoms = [0; 25];
crypto.get_random_values_with_u8_array(&mut randoms)?;
let mut family_name = String::new();
for random in randoms.iter() {
family_name.push(match random % 26 {
0 => 'a',
1 => 'b',
2 => 'c',
3 => 'd',
4 => 'e',
5 => 'f',
6 => 'g',
7 => 'h',
8 => 'i',
9 => 'j',
10 => 'k',
11 => 'l',
12 => 'm',
13 => 'n',
14 => 'o',
15 => 'p',
16 => 'q',
17 => 'r',
18 => 's',
19 => 't',
20 => 'u',
21 => 'v',
22 => 'w',
23 => 'x',
24 => 'y',
_ => 'z'
});
}
let font = FontFace::new_with_str(&family_name, &format!("url({})", url))?;
JsFuture::from(font.load()?).await?;
window.document().unwrap().fonts().add(&font)?;
Ok(Font {
name: family_name
})
}
pub async fn load_and_send(url: &str, sender: Sender<Result<Font, JsValue>>) {
let font = Font::load(url).await;
sender.send(font).expect("can't send the loaded font trought the oneshot shannel");
}
pub fn sherif() -> Font {
Font {
name: String::from("sherif"),
}
}
pub fn arial() -> Font {
Font {
name: String::from("Arial"),
}
}
pub fn times_new_roman() -> Font {
Font {
name: String::from("Times New Roman"),
}
}
pub fn times() -> Font {
Font {
name: String::from("Times"),
}
}
pub fn courier_new() -> Font {
Font {
name: String::from("Courier New"),
}
}
pub fn courier() -> Font {
Font {
name: String::from("Courier"),
}
}
pub fn verdana() -> Font {
Font {
name: String::from("Verdana"),
}
}
pub fn georgia() -> Font {
Font {
name: String::from("Georgia"),
}
}
pub fn palatino() -> Font {
Font {
name: String::from("Palatino"),
}
}
pub fn garamond() -> Font {
Font {
name: String::from("Garamond"),
}
}
pub fn bookman() -> Font {
Font {
name: String::from("Bookman"),
}
}
pub fn comic_sans_ms() -> Font {
Font {
name: String::from("Comic Sans MS"),
}
}
pub fn candara() -> Font {
Font {
name: String::from("Candara"),
}
}
pub fn helvetica() -> Font {
Font {
name: String::from("Helvetica"),
}
}
pub fn arial_black() -> Font {
Font {
name: String::from("Arial Black"),
}
}
pub fn impact() -> Font {
Font {
name: String::from("Impact"),
}
}
}