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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
//! Special characters with private unicode points.
use super::{legacy::SGA_LEGACY, utf16::{FontUtf16, Utf16Fonts}};
use std::fmt;

/// A constant `[FontUtf16; 26]`, for special SGA fonts (`U+E543` - `U+E55A`).
///
/// ## `SGA_UTF16[0]`: `0xE541` `"\u{e541}"`
///
/// ```text
/// ░░░░░░░░
/// ░░░░░░░░
/// ░░░███░░
/// ░██░░██░
/// ░██░░░░░
/// ░██░░░░░
/// ███░░░░░
/// ░░░░░░░░
/// ```
///
/// ## `SGA_UTF16[1]`: `0xE542` `"\u{e542}"`
///
/// ```text
/// ░░░░░░░░
/// ░░░░░░░░
/// ░░██░░░░
/// ░░██░░░░
/// ░░░██░░░
/// ░░░░██░░
/// ███████░
/// ░░░░░░░░
/// ```
///
/// ## `SGA_UTF16[2]`: `0xE543` `"\u{e543}"`
///
/// ```text
/// ░░░░░░░░
/// ░░░░░░░░
/// ░░██░░░░
/// ░░░░░░░░
/// ░░██░░░░
/// ░░░░██░░
/// ░░░░██░░
/// ░░░░░░░░
/// ```
///
/// ## `SGA_UTF16[3]`: `0xE544` `"\u{e544}"`
///
/// ```text
/// ░░░░░░░░
/// ░░░░░░░░
/// ███████░
/// ░░░░░░░░
/// ██░░░░░░
/// ░░███░░░
/// ░░░░░██░
/// ░░░░░░░░
/// ```
///
/// ## `SGA_UTF16[4]`: `0xE545` `"\u{e545}"`
///
/// ```text
/// ░░░░░░░░
/// ░░░░░░░░
/// ██░░░██░
/// ██░░░░░░
/// ██░░░░░░
/// ██░░░░░░
/// ███████░
/// ░░░░░░░░
/// ```
///
/// ## `SGA_UTF16[5]`: `0xE546` `"\u{e546}"`
///
/// ```text
/// ░░░░░░░░
/// ░░░░░░░░
/// ░░░░░░░░
/// ████████
/// ░░░░░░░░
/// ██░██░██
/// ░░░░░░░░
/// ░░░░░░░░
/// ```
///
/// ## `SGA_UTF16[6]`: `0xE547` `"\u{e547}"`
///
/// ```text
/// ░░░░░░░░
/// ░░░░░░░░
/// ░░░░██░░
/// ░░░░██░░
/// ░█████░░
/// ░░░░██░░
/// ░░░░██░░
/// ░░░░░░░░
/// ```
///
/// ## `SGA_UTF16[7]`: `0xE548` `"\u{e548}"`
///
/// ```text
/// ░░░░░░░░
/// ░░░░░░░░
/// ░██████░
/// ░░░░░░░░
/// ░██████░
/// ░░░██░░░
/// ░░░██░░░
/// ░░░░░░░░
/// ```
///
/// ## `SGA_UTF16[8]`: `0xE549` `"\u{e549}"`
///
/// ```text
/// ░░░░░░░░
/// ░░░░░░░░
/// ░░░██░░░
/// ░░░██░░░
/// ░░░░░░░░
/// ░░░██░░░
/// ░░░██░░░
/// ░░░░░░░░
/// ```
///
/// ## `SGA_UTF16[9]`: `0xE54A` `"\u{e54a}"`
///
/// ```text
/// ░░░░░░░░
/// ░░░░░░░░
/// ░░░██░░░
/// ░░░░░░░░
/// ░░░██░░░
/// ░░░░░░░░
/// ░░░██░░░
/// ░░░░░░░░
/// ```
///
/// ## `SGA_UTF16[10]`: `0xE54B` `"\u{e54b}"`
///
/// ```text
/// ░░░░░░░░
/// ░░░░░░░░
/// ░░░██░░░
/// ░░░██░░░
/// ░█░██░█░
/// ░░░██░░░
/// ░░░██░░░
/// ░░░░░░░░
/// ```
///
/// ## `SGA_UTF16[11]`: `0xE54C` `"\u{e54c}"`
///
/// ```text
/// ░░░░░░░░
/// ░░░░░░░░
/// ██░░░░░░
/// ██░░██░░
/// ██░░░░░░
/// ██░░██░░
/// ██░░░░░░
/// ░░░░░░░░
/// ```
///
/// ## `SGA_UTF16[12]`: `0xE54D` `"\u{e54d}"`
///
/// ```text
/// ░░░░░░░░
/// ░░░░░░░░
/// ██░░░██░
/// ░░░░░██░
/// ░░░░░██░
/// ░░░░░██░
/// ███████░
/// ░░░░░░░░
/// ```
///
/// ## `SGA_UTF16[13]`: `0xE54E` `"\u{e54e}"`
///
/// ```text
/// ░░░░░░░░
/// ░░░░░░░░
/// ░██░░██░
/// ░░░░░██░
/// ░░░░██░░
/// ░░░██░░░
/// ░░██░░░░
/// ░░░░░░░░
/// ```
///
/// ## `SGA_UTF16[14]`: `0xE54F` `"\u{e54f}"`
///
/// ```text
/// ░░░░░░░░
/// ░░░░░░░░
/// ░░████░░
/// ░░░░░██░
/// ░░░░██░░
/// ░░░██░░░
/// ░░██░░░░
/// ░░░░░░░░
/// ```
///
/// ## `SGA_UTF16[15]`: `0xE550` `"\u{e550}"`
///
/// ```text
/// ░░░░░░░░
/// ░░░░░░░░
/// ░██░░██░
/// ░░░░░██░
/// ░██░░██░
/// ░██░░░░░
/// ░██░░██░
/// ░░░░░░░░
/// ```
///
/// ## `SGA_UTF16[16]`: `0xE551` `"\u{e551}"`
///
/// ```text
/// ░░░░░░░░
/// ░░░░░░░░
/// ░░░██░░░
/// ░░░░░░░░
/// ░██████░
/// ░░░░░██░
/// ░██████░
/// ░░░░░░░░
/// ```
///
/// ## `SGA_UTF16[17]`: `0xE552` `"\u{e552}"`
///
/// ```text
/// ░░░░░░░░
/// ░░░░░░░░
/// ░░░░░░░░
/// ░██░░██░
/// ░░░░░░░░
/// ░██░░██░
/// ░░░░░░░░
/// ░░░░░░░░
/// ```
///
/// ## `SGA_UTF16[18]`: `0xE553` `"\u{e553}"`
///
/// ```text
/// ░░░░░░░░
/// ░░░░░░░░
/// ░░██░░░░
/// ░░██░░░░
/// ░░████░░
/// ░░░░██░░
/// ░░░░██░░
/// ░░░░░░░░
/// ```
///
/// ## `SGA_UTF16[19]`: `0xE554` `"\u{e554}"`
///
/// ```text
/// ░░░░░░░░
/// ░░░░░░░░
/// ░░████░░
/// ░░░░██░░
/// ░░░░██░░
/// ░░░░░░░░
/// ░░░░██░░
/// ░░░░░░░░
/// ```
///
/// ## `SGA_UTF16[20]`: `0xE555` `"\u{e555}"`
///
/// ```text
/// ░░░░░░░░
/// ░░░░░░░░
/// ░░░░░░░░
/// ░██░██░░
/// ░░░░░░░░
/// ███████░
/// ░░░░░░░░
/// ░░░░░░░░
/// ```
///
/// ## `SGA_UTF16[21]`: `0xE556` `"\u{e556}"`
///
/// ```text
/// ░░░░░░░░
/// ░░░░░░░░
/// ░░░██░░░
/// ░░░██░░░
/// ░██████░
/// ░░░░░░░░
/// ░██████░
/// ░░░░░░░░
/// ```
///
/// ## `SGA_UTF16[22]`: `0xE557` `"\u{e557}"`
///
/// ```text
/// ░░░░░░░░
/// ░░░░░░░░
/// ░░░░░░░░
/// ░░░██░░░
/// ░░░░░░░░
/// ░██░░██░
/// ░░░░░░░░
/// ░░░░░░░░
/// ```
///
/// ## `SGA_UTF16[23]`: `0xE558` `"\u{e558}"`
///
/// ```text
/// ░░░░░░░░
/// ░░░░░░░░
/// ░██░░██░
/// ░░░░██░░
/// ░░░██░░░
/// ░░██░░░░
/// ░██░░░░░
/// ░░░░░░░░
/// ```
///
/// ## `SGA_UTF16[24]`: `0xE559` `"\u{e559}"`
///
/// ```text
/// ░░░░░░░░
/// ░░░░░░░░
/// ░██░██░░
/// ░██░██░░
/// ░██░██░░
/// ░██░██░░
/// ░██░██░░
/// ░░░░░░░░
/// ```
///
/// ## `SGA_UTF16[25]`: `0xE55A` `"\u{e55a}"`
///
/// ```markdown
/// ░░░░░░░░
/// ░░░░░░░░
/// ░░░██░░░
/// ░░████░░
/// ░██░░██░
/// ░██░░██░
/// ░██░░██░
/// ░░░░░░░░
/// ```
pub const SGA_UTF16: [FontUtf16; 26] = [
    FontUtf16(0xE541 as u16, SGA_LEGACY[0]),
    FontUtf16(0xE542 as u16, SGA_LEGACY[1]),
    FontUtf16(0xE543 as u16, SGA_LEGACY[2]),
    FontUtf16(0xE544 as u16, SGA_LEGACY[3]),
    FontUtf16(0xE545 as u16, SGA_LEGACY[4]),
    FontUtf16(0xE546 as u16, SGA_LEGACY[5]),
    FontUtf16(0xE547 as u16, SGA_LEGACY[6]),
    FontUtf16(0xE548 as u16, SGA_LEGACY[7]),
    FontUtf16(0xE549 as u16, SGA_LEGACY[8]),
    FontUtf16(0xE54A as u16, SGA_LEGACY[9]),
    FontUtf16(0xE54B as u16, SGA_LEGACY[10]),
    FontUtf16(0xE54C as u16, SGA_LEGACY[11]),
    FontUtf16(0xE54D as u16, SGA_LEGACY[12]),
    FontUtf16(0xE54E as u16, SGA_LEGACY[13]),
    FontUtf16(0xE54F as u16, SGA_LEGACY[14]),
    FontUtf16(0xE550 as u16, SGA_LEGACY[15]),
    FontUtf16(0xE551 as u16, SGA_LEGACY[16]),
    FontUtf16(0xE552 as u16, SGA_LEGACY[17]),
    FontUtf16(0xE553 as u16, SGA_LEGACY[18]),
    FontUtf16(0xE554 as u16, SGA_LEGACY[19]),
    FontUtf16(0xE555 as u16, SGA_LEGACY[20]),
    FontUtf16(0xE556 as u16, SGA_LEGACY[21]),
    FontUtf16(0xE557 as u16, SGA_LEGACY[22]),
    FontUtf16(0xE558 as u16, SGA_LEGACY[23]),
    FontUtf16(0xE559 as u16, SGA_LEGACY[24]),
    FontUtf16(0xE55A as u16, SGA_LEGACY[25]),
];

/// A convenient constant for special SGA fonts (`U+E541` - `U+E55A`), that implements the [`Utf16Fonts`](./utf16/trait.Utf16Fonts.html) trait.
pub const SGA_FONTS: SgaFonts = SgaFonts(SGA_UTF16);

/// Strong-typed collection wrapper for [SGA_UTF16](./constant.SGA_UTF16.html).
pub struct SgaFonts([FontUtf16; 26]);

impl SgaFonts {
    pub fn new() -> Self {
        SgaFonts(SGA_UTF16)
    }
}

impl fmt::Debug for SgaFonts {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", stringify!(SGA_UTF16))
    }
}

impl PartialEq for SgaFonts {
    fn eq(&self, other: &SgaFonts) -> bool {
        self.0
            .iter()
            .zip(other.0.iter())
            .fold(true, |eq, (a, b)| eq && a == b)
    }
}

impl Default for SgaFonts {
    fn default() -> Self {
        SgaFonts::new()
    }
}

impl Utf16Fonts for SgaFonts {
    fn get(&self, key: u16) -> Option<[u8; 8]> {
        match self.get_font(key) {
            Some(font) => Some(font.into()),
            None => None,
        }
    }

    fn get_font(&self, key: u16) -> Option<FontUtf16> {
        match self.0.binary_search_by_key(&key, |&f| f.utf16()) {
            Ok(idx) => Some(self.0[idx]),
            _ => None,
        }
    }

    fn print_set(&self) {
        println!();
        println!("# `{:?}`", self);
        for (idx, font) in self.0.iter().enumerate() {
            print_set(idx, &font);
        }
    }
}
fn print_set(idx: usize, font: &FontUtf16) {
    if font.is_whitespace() {
        println!("## {:3?}: 0x{:04X} \" \"", idx, font.utf16());
        return;
    }
    println!(
        "## `[{:?}]`: `0x{:04X}` `{:?}`",
        idx,
        font.utf16(),
        font.to_string()
    );
    println!();
    println!("```text");
    for x in &font.byte_array() {
        for bit in 0..8 {
            match *x & 1 << bit {
                0 => print!("░"),
                _ => print!("█"),
            }
        }
        println!();
    }
    println!("```");
    println!();
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn sga_set_implements_default_trait_with_method_new() {
        let sga_set: SgaFonts = Default::default();
        assert_eq!(sga_set, SgaFonts::new());
    }

    #[test]
    fn sga_fonts_constant_is_equal_to_a_new_instance() {
        assert_eq!(SGA_FONTS, SgaFonts::new());
    }

    #[test]
    fn sga_fonts_constant_wraps_basic_utf16_constant() {
        let sga = SgaFonts::new();
        assert!(sga.0.len() == SGA_UTF16.len());
        for (idx, font) in sga.0.iter().enumerate() {
            assert_eq!(font, &SGA_UTF16[idx]);
        }
    }
}