font8x8/sga.rs
1//! Special characters with private unicode points.
2use super::{
3 legacy::SGA_LEGACY,
4 unicode::{FontUnicode, UnicodeFonts},
5};
6use core::fmt;
7
8/// A constant `[FontUnicode; 26]`, for special SGA fonts (`U+E543` - `U+E55A`).
9pub const SGA_UNICODE: [FontUnicode; 26] = [
10 FontUnicode('\u{E541}', SGA_LEGACY[0]),
11 FontUnicode('\u{E542}', SGA_LEGACY[1]),
12 FontUnicode('\u{E543}', SGA_LEGACY[2]),
13 FontUnicode('\u{E544}', SGA_LEGACY[3]),
14 FontUnicode('\u{E545}', SGA_LEGACY[4]),
15 FontUnicode('\u{E546}', SGA_LEGACY[5]),
16 FontUnicode('\u{E547}', SGA_LEGACY[6]),
17 FontUnicode('\u{E548}', SGA_LEGACY[7]),
18 FontUnicode('\u{E549}', SGA_LEGACY[8]),
19 FontUnicode('\u{E54A}', SGA_LEGACY[9]),
20 FontUnicode('\u{E54B}', SGA_LEGACY[10]),
21 FontUnicode('\u{E54C}', SGA_LEGACY[11]),
22 FontUnicode('\u{E54D}', SGA_LEGACY[12]),
23 FontUnicode('\u{E54E}', SGA_LEGACY[13]),
24 FontUnicode('\u{E54F}', SGA_LEGACY[14]),
25 FontUnicode('\u{E550}', SGA_LEGACY[15]),
26 FontUnicode('\u{E551}', SGA_LEGACY[16]),
27 FontUnicode('\u{E552}', SGA_LEGACY[17]),
28 FontUnicode('\u{E553}', SGA_LEGACY[18]),
29 FontUnicode('\u{E554}', SGA_LEGACY[19]),
30 FontUnicode('\u{E555}', SGA_LEGACY[20]),
31 FontUnicode('\u{E556}', SGA_LEGACY[21]),
32 FontUnicode('\u{E557}', SGA_LEGACY[22]),
33 FontUnicode('\u{E558}', SGA_LEGACY[23]),
34 FontUnicode('\u{E559}', SGA_LEGACY[24]),
35 FontUnicode('\u{E55A}', SGA_LEGACY[25]),
36];
37
38/// A convenient constant for special SGA fonts (`U+E541` - `U+E55A`), that implements the `UnicodeFonts` trait.
39///
40/// ## `SGA_UNICODE[0]: U+E541`
41/// `"\u{e541}"`
42///
43/// ```text
44/// ░░░░░░░░
45/// ░░░░░░░░
46/// ░░░███░░
47/// ░██░░██░
48/// ░██░░░░░
49/// ░██░░░░░
50/// ███░░░░░
51/// ░░░░░░░░
52/// ```
53///
54/// ## `SGA_UNICODE[1]: U+E542`
55/// `"\u{e542}"`
56///
57/// ```text
58/// ░░░░░░░░
59/// ░░░░░░░░
60/// ░░██░░░░
61/// ░░██░░░░
62/// ░░░██░░░
63/// ░░░░██░░
64/// ███████░
65/// ░░░░░░░░
66/// ```
67///
68/// ## `SGA_UNICODE[2]: U+E543`
69/// `"\u{e543}"`
70///
71/// ```text
72/// ░░░░░░░░
73/// ░░░░░░░░
74/// ░░██░░░░
75/// ░░░░░░░░
76/// ░░██░░░░
77/// ░░░░██░░
78/// ░░░░██░░
79/// ░░░░░░░░
80/// ```
81///
82/// ## `SGA_UNICODE[3]: U+E544`
83/// `"\u{e544}"`
84///
85/// ```text
86/// ░░░░░░░░
87/// ░░░░░░░░
88/// ███████░
89/// ░░░░░░░░
90/// ██░░░░░░
91/// ░░███░░░
92/// ░░░░░██░
93/// ░░░░░░░░
94/// ```
95///
96/// ## `SGA_UNICODE[4]: U+E545`
97/// `"\u{e545}"`
98///
99/// ```text
100/// ░░░░░░░░
101/// ░░░░░░░░
102/// ██░░░██░
103/// ██░░░░░░
104/// ██░░░░░░
105/// ██░░░░░░
106/// ███████░
107/// ░░░░░░░░
108/// ```
109///
110/// ## `SGA_UNICODE[5]: U+E546`
111/// `"\u{e546}"`
112///
113/// ```text
114/// ░░░░░░░░
115/// ░░░░░░░░
116/// ░░░░░░░░
117/// ████████
118/// ░░░░░░░░
119/// ██░██░██
120/// ░░░░░░░░
121/// ░░░░░░░░
122/// ```
123///
124/// ## `SGA_UNICODE[6]: U+E547`
125/// `"\u{e547}"`
126///
127/// ```text
128/// ░░░░░░░░
129/// ░░░░░░░░
130/// ░░░░██░░
131/// ░░░░██░░
132/// ░█████░░
133/// ░░░░██░░
134/// ░░░░██░░
135/// ░░░░░░░░
136/// ```
137///
138/// ## `SGA_UNICODE[7]: U+E548`
139/// `"\u{e548}"`
140///
141/// ```text
142/// ░░░░░░░░
143/// ░░░░░░░░
144/// ░██████░
145/// ░░░░░░░░
146/// ░██████░
147/// ░░░██░░░
148/// ░░░██░░░
149/// ░░░░░░░░
150/// ```
151///
152/// ## `SGA_UNICODE[8]: U+E549`
153/// `"\u{e549}"`
154///
155/// ```text
156/// ░░░░░░░░
157/// ░░░░░░░░
158/// ░░░██░░░
159/// ░░░██░░░
160/// ░░░░░░░░
161/// ░░░██░░░
162/// ░░░██░░░
163/// ░░░░░░░░
164/// ```
165///
166/// ## `SGA_UNICODE[9]: U+E54A`
167/// `"\u{e54a}"`
168///
169/// ```text
170/// ░░░░░░░░
171/// ░░░░░░░░
172/// ░░░██░░░
173/// ░░░░░░░░
174/// ░░░██░░░
175/// ░░░░░░░░
176/// ░░░██░░░
177/// ░░░░░░░░
178/// ```
179///
180/// ## `SGA_UNICODE[10]: U+E54B`
181/// `"\u{e54b}"`
182///
183/// ```text
184/// ░░░░░░░░
185/// ░░░░░░░░
186/// ░░░██░░░
187/// ░░░██░░░
188/// ░█░██░█░
189/// ░░░██░░░
190/// ░░░██░░░
191/// ░░░░░░░░
192/// ```
193///
194/// ## `SGA_UNICODE[11]: U+E54C`
195/// `"\u{e54c}"`
196///
197/// ```text
198/// ░░░░░░░░
199/// ░░░░░░░░
200/// ██░░░░░░
201/// ██░░██░░
202/// ██░░░░░░
203/// ██░░██░░
204/// ██░░░░░░
205/// ░░░░░░░░
206/// ```
207///
208/// ## `SGA_UNICODE[12]: U+E54D`
209/// `"\u{e54d}"`
210///
211/// ```text
212/// ░░░░░░░░
213/// ░░░░░░░░
214/// ██░░░██░
215/// ░░░░░██░
216/// ░░░░░██░
217/// ░░░░░██░
218/// ███████░
219/// ░░░░░░░░
220/// ```
221///
222/// ## `SGA_UNICODE[13]: U+E54E`
223/// `"\u{e54e}"`
224///
225/// ```text
226/// ░░░░░░░░
227/// ░░░░░░░░
228/// ░██░░██░
229/// ░░░░░██░
230/// ░░░░██░░
231/// ░░░██░░░
232/// ░░██░░░░
233/// ░░░░░░░░
234/// ```
235///
236/// ## `SGA_UNICODE[14]: U+E54F`
237/// `"\u{e54f}"`
238///
239/// ```text
240/// ░░░░░░░░
241/// ░░░░░░░░
242/// ░░████░░
243/// ░░░░░██░
244/// ░░░░██░░
245/// ░░░██░░░
246/// ░░██░░░░
247/// ░░░░░░░░
248/// ```
249///
250/// ## `SGA_UNICODE[15]: U+E550`
251/// `"\u{e550}"`
252///
253/// ```text
254/// ░░░░░░░░
255/// ░░░░░░░░
256/// ░██░░██░
257/// ░░░░░██░
258/// ░██░░██░
259/// ░██░░░░░
260/// ░██░░██░
261/// ░░░░░░░░
262/// ```
263///
264/// ## `SGA_UNICODE[16]: U+E551`
265/// `"\u{e551}"`
266///
267/// ```text
268/// ░░░░░░░░
269/// ░░░░░░░░
270/// ░░░██░░░
271/// ░░░░░░░░
272/// ░██████░
273/// ░░░░░██░
274/// ░██████░
275/// ░░░░░░░░
276/// ```
277///
278/// ## `SGA_UNICODE[17]: U+E552`
279/// `"\u{e552}"`
280///
281/// ```text
282/// ░░░░░░░░
283/// ░░░░░░░░
284/// ░░░░░░░░
285/// ░██░░██░
286/// ░░░░░░░░
287/// ░██░░██░
288/// ░░░░░░░░
289/// ░░░░░░░░
290/// ```
291///
292/// ## `SGA_UNICODE[18]: U+E553`
293/// `"\u{e553}"`
294///
295/// ```text
296/// ░░░░░░░░
297/// ░░░░░░░░
298/// ░░██░░░░
299/// ░░██░░░░
300/// ░░████░░
301/// ░░░░██░░
302/// ░░░░██░░
303/// ░░░░░░░░
304/// ```
305///
306/// ## `SGA_UNICODE[19]: U+E554`
307/// `"\u{e554}"`
308///
309/// ```text
310/// ░░░░░░░░
311/// ░░░░░░░░
312/// ░░████░░
313/// ░░░░██░░
314/// ░░░░██░░
315/// ░░░░░░░░
316/// ░░░░██░░
317/// ░░░░░░░░
318/// ```
319///
320/// ## `SGA_UNICODE[20]: U+E555`
321/// `"\u{e555}"`
322///
323/// ```text
324/// ░░░░░░░░
325/// ░░░░░░░░
326/// ░░░░░░░░
327/// ░██░██░░
328/// ░░░░░░░░
329/// ███████░
330/// ░░░░░░░░
331/// ░░░░░░░░
332/// ```
333///
334/// ## `SGA_UNICODE[21]: U+E556`
335/// `"\u{e556}"`
336///
337/// ```text
338/// ░░░░░░░░
339/// ░░░░░░░░
340/// ░░░██░░░
341/// ░░░██░░░
342/// ░██████░
343/// ░░░░░░░░
344/// ░██████░
345/// ░░░░░░░░
346/// ```
347///
348/// ## `SGA_UNICODE[22]: U+E557`
349/// `"\u{e557}"`
350///
351/// ```text
352/// ░░░░░░░░
353/// ░░░░░░░░
354/// ░░░░░░░░
355/// ░░░██░░░
356/// ░░░░░░░░
357/// ░██░░██░
358/// ░░░░░░░░
359/// ░░░░░░░░
360/// ```
361///
362/// ## `SGA_UNICODE[23]: U+E558`
363/// `"\u{e558}"`
364///
365/// ```text
366/// ░░░░░░░░
367/// ░░░░░░░░
368/// ░██░░██░
369/// ░░░░██░░
370/// ░░░██░░░
371/// ░░██░░░░
372/// ░██░░░░░
373/// ░░░░░░░░
374/// ```
375///
376/// ## `SGA_UNICODE[24]: U+E559`
377/// `"\u{e559}"`
378///
379/// ```text
380/// ░░░░░░░░
381/// ░░░░░░░░
382/// ░██░██░░
383/// ░██░██░░
384/// ░██░██░░
385/// ░██░██░░
386/// ░██░██░░
387/// ░░░░░░░░
388/// ```
389///
390/// ## `SGA_UNICODE[25]: U+E55A`
391/// `"\u{e55a}"`
392///
393/// ```text
394/// ░░░░░░░░
395/// ░░░░░░░░
396/// ░░░██░░░
397/// ░░████░░
398/// ░██░░██░
399/// ░██░░██░
400/// ░██░░██░
401/// ░░░░░░░░
402/// ```
403pub const SGA_FONTS: SgaFonts = SgaFonts(SGA_UNICODE);
404
405/// Strong-typed collection wrapper for [SGA_UNICODE](./constant.SGA_UNICODE.html).
406pub struct SgaFonts([FontUnicode; 26]);
407
408impl SgaFonts {
409 pub fn new() -> Self {
410 SgaFonts(SGA_UNICODE)
411 }
412}
413
414impl fmt::Debug for SgaFonts {
415 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
416 write!(f, stringify!(SGA_UNICODE))
417 }
418}
419
420impl PartialEq for SgaFonts {
421 fn eq(&self, other: &SgaFonts) -> bool {
422 self.0
423 .iter()
424 .zip(other.0.iter())
425 .fold(true, |eq, (a, b)| eq && a == b)
426 }
427}
428
429impl Default for SgaFonts {
430 fn default() -> Self {
431 SgaFonts::new()
432 }
433}
434
435impl UnicodeFonts for SgaFonts {
436 fn get(&self, key: char) -> Option<[u8; 8]> {
437 match self.get_font(key) {
438 Some(font) => Some(font.into()),
439 None => None,
440 }
441 }
442
443 fn get_font(&self, key: char) -> Option<FontUnicode> {
444 match self.0.binary_search_by_key(&key, |&f| f.char()) {
445 Ok(idx) => Some(self.0[idx]),
446 _ => None,
447 }
448 }
449
450 fn iter(&self) -> ::core::slice::Iter<FontUnicode> {
451 self.0.iter()
452 }
453
454 #[cfg(feature = "std")]
455 fn print_set(&self) {
456 println!();
457 println!("# `{:?}`", self);
458 for (idx, font) in self.0.iter().enumerate() {
459 print!("## `{:?}[{:?}]: U+{:04X}`", self, idx, font.char() as u32,);
460 if font.is_whitespace() {
461 println!(" `WHITESPACE`");
462 } else {
463 println!();
464 println!("`{:?}`", font.to_string());
465 println!();
466 println!("```text");
467 for x in &font.byte_array() {
468 for bit in 0..8 {
469 match *x & 1 << bit {
470 0 => print!("░"),
471 _ => print!("█"),
472 }
473 }
474 println!();
475 }
476 println!("```");
477 println!();
478 }
479 }
480 }
481
482 #[cfg(feature = "std")]
483 fn to_vec(&self) -> Vec<(char, FontUnicode)> {
484 self.0.iter().fold(Vec::with_capacity(128), |mut v, font| {
485 v.push((font.char(), *font));
486 v
487 })
488 }
489}
490
491#[cfg(test)]
492mod tests {
493 use super::*;
494
495 #[test]
496 fn sga_fonts_inner_array_is_sorted_by_unicode_key() {
497 let mut sorted = SGA_UNICODE;
498 sorted.sort_by_key(|f| f.char());
499 for (idx, key) in sorted.iter().enumerate() {
500 assert_eq!(SGA_UNICODE[idx].char(), key.char());
501 }
502 }
503
504 #[test]
505 fn sga_set_implements_default_trait_with_method_new() {
506 let sga_set: SgaFonts = Default::default();
507 assert_eq!(sga_set, SgaFonts::new());
508 }
509
510 #[test]
511 fn sga_fonts_constant_is_equal_to_a_new_instance() {
512 assert_eq!(SGA_FONTS, SgaFonts::new());
513 }
514
515 #[test]
516 fn sga_fonts_constant_wraps_basic_unicode_constant() {
517 let sga = SgaFonts::new();
518 assert!(sga.0.len() == SGA_UNICODE.len());
519 for (idx, font) in sga.0.iter().enumerate() {
520 assert_eq!(font, &SGA_UNICODE[idx]);
521 }
522 }
523}