strata-render 0.1.3

High-performance Display List, Stacking Context Compositor, and GPU rasterization pipeline
Documentation
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
//! Embedded 8x16 anti-aliased bitmap font table for zero-dependency software text rasterization.

pub const FONT_GLYPH_WIDTH: usize = 8;
pub const FONT_GLYPH_HEIGHT: usize = 16;

/// Pre-rasterized 8x16 bitmap glyphs for printable ASCII characters 32..=126.
pub const FONT_8X16: [[u8; 16]; 95] = [
    // ASCII 32 (' ')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00,
    ],
    // ASCII 33 ('!')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00, 0x40, 0x40, 0x00,
        0x00,
    ],
    // ASCII 34 ('"')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x58, 0x50, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00,
    ],
    // ASCII 35 ('#')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x12, 0x7f, 0x24, 0x24, 0xff, 0x24, 0x2c, 0x68, 0x00,
        0x00,
    ],
    // ASCII 36 ('$')
    [
        0x00, 0x00, 0x00, 0x00, 0x10, 0x3c, 0x50, 0x50, 0x70, 0x1c, 0x16, 0x16, 0x7c, 0x10, 0x00,
        0x00,
    ],
    // ASCII 37 ('%')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x71, 0x51, 0x5a, 0x5f, 0x57, 0x7a, 0x0a, 0x13, 0x11, 0x00,
        0x00,
    ],
    // ASCII 38 ('&')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x24, 0x24, 0x38, 0x78, 0x4d, 0x47, 0x47, 0x3d, 0x00,
        0x00,
    ],
    // ASCII 39 ("'")
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00,
    ],
    // ASCII 40 ('(')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x60, 0x40, 0x40, 0xc0, 0xc0, 0xc0, 0x40, 0x40, 0x60,
        0x20,
    ],
    // ASCII 41 (')')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x60, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x60,
        0x40,
    ],
    // ASCII 42 ('*')
    [
        0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x7e, 0x38, 0x28, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00,
    ],
    // ASCII 43 ('+')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x7e, 0x10, 0x10, 0x10, 0x00, 0x00,
        0x00,
    ],
    // ASCII 44 (',')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40,
        0x40,
    ],
    // ASCII 45 ('-')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x00,
        0x00,
    ],
    // ASCII 46 ('.')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x00,
        0x00,
    ],
    // ASCII 47 ('/')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x10, 0x10, 0x20, 0x20, 0x60, 0x40, 0x40, 0xc0, 0x00,
        0x00,
    ],
    // ASCII 48 ('0')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x64, 0x46, 0x46, 0x42, 0x46, 0x46, 0x64, 0x38, 0x00,
        0x00,
    ],
    // ASCII 49 ('1')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x38, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00,
        0x00,
    ],
    // ASCII 50 ('2')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x44, 0x04, 0x04, 0x0c, 0x18, 0x30, 0x60, 0x7e, 0x00,
        0x00,
    ],
    // ASCII 51 ('3')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x44, 0x04, 0x0c, 0x38, 0x04, 0x06, 0x06, 0x7c, 0x00,
        0x00,
    ],
    // ASCII 52 ('4')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x1c, 0x14, 0x24, 0x64, 0x44, 0xfe, 0x04, 0x04, 0x00,
        0x00,
    ],
    // ASCII 53 ('5')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x40, 0x40, 0x7c, 0x04, 0x06, 0x06, 0x04, 0x78, 0x00,
        0x00,
    ],
    // ASCII 54 ('6')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x20, 0x40, 0x5c, 0x66, 0x42, 0x46, 0x66, 0x3c, 0x00,
        0x00,
    ],
    // ASCII 55 ('7')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x06, 0x04, 0x0c, 0x08, 0x18, 0x10, 0x30, 0x20, 0x00,
        0x00,
    ],
    // ASCII 56 ('8')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x64, 0x44, 0x6c, 0x38, 0x44, 0x42, 0x46, 0x3c, 0x00,
        0x00,
    ],
    // ASCII 57 ('9')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x64, 0x46, 0x46, 0x46, 0x3e, 0x04, 0x0c, 0x78, 0x00,
        0x00,
    ],
    // ASCII 58 (':')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x00, 0x00, 0x00, 0x40, 0x40, 0x00,
        0x00,
    ],
    // ASCII 59 (';')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40,
        0xc0,
    ],
    // ASCII 60 ('<')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x0c, 0x30, 0x60, 0x38, 0x0e, 0x00, 0x00, 0x00,
        0x00,
    ],
    // ASCII 61 ('=')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x00,
        0x00,
    ],
    // ASCII 62 ('>')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x70, 0x1c, 0x06, 0x18, 0x60, 0x00, 0x00, 0x00,
        0x00,
    ],
    // ASCII 63 ('?')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x08, 0x08, 0x18, 0x30, 0x20, 0x00, 0x20, 0x20, 0x00,
        0x00,
    ],
    // ASCII 64 ('@')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x30, 0x6f, 0x49, 0x59, 0x59, 0x5b, 0x4e, 0x30, 0x1f,
        0x00,
    ],
    // ASCII 65 ('A')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x1c, 0x24, 0x26, 0x7e, 0x42, 0xc3, 0x81, 0x00,
        0x00,
    ],
    // ASCII 66 ('B')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x62, 0x62, 0x62, 0x7c, 0x63, 0x63, 0x63, 0x7e, 0x00,
        0x00,
    ],
    // ASCII 67 ('C')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x30, 0x60, 0x40, 0x40, 0x40, 0x60, 0x30, 0x1e, 0x00,
        0x00,
    ],
    // ASCII 68 ('D')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x63, 0x61, 0x61, 0x61, 0x61, 0x61, 0x63, 0x7e, 0x00,
        0x00,
    ],
    // ASCII 69 ('E')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x60, 0x60, 0x60, 0x7c, 0x60, 0x60, 0x60, 0x7e, 0x00,
        0x00,
    ],
    // ASCII 70 ('F')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x60, 0x60, 0x60, 0x7c, 0x60, 0x60, 0x60, 0x60, 0x00,
        0x00,
    ],
    // ASCII 71 ('G')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x30, 0x60, 0x40, 0x47, 0x41, 0x61, 0x31, 0x1f, 0x00,
        0x00,
    ],
    // ASCII 72 ('H')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x61, 0x61, 0x61, 0x61, 0x7f, 0x61, 0x61, 0x61, 0x61, 0x00,
        0x00,
    ],
    // ASCII 73 ('I')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x70, 0x00,
        0x00,
    ],
    // ASCII 74 ('J')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
        0x40,
    ],
    // ASCII 75 ('K')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x66, 0x6c, 0x78, 0x78, 0x68, 0x64, 0x66, 0x63, 0x00,
        0x00,
    ],
    // ASCII 76 ('L')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x7e, 0x00,
        0x00,
    ],
    // ASCII 77 ('M')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x70, 0x70, 0x51, 0x59, 0x49, 0x4e, 0x46, 0x46, 0x00,
        0x00,
    ],
    // ASCII 78 ('N')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x61, 0x71, 0x71, 0x59, 0x4d, 0x45, 0x47, 0x43, 0x41, 0x00,
        0x00,
    ],
    // ASCII 79 ('O')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x21, 0x60, 0x40, 0x40, 0x40, 0x60, 0x21, 0x1e, 0x00,
        0x00,
    ],
    // ASCII 80 ('P')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x66, 0x62, 0x66, 0x7c, 0x60, 0x60, 0x60, 0x60, 0x00,
        0x00,
    ],
    // ASCII 81 ('Q')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x21, 0x60, 0x40, 0x40, 0x40, 0x60, 0x21, 0x1e, 0x02,
        0x01,
    ],
    // ASCII 82 ('R')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x66, 0x62, 0x66, 0x7c, 0x6c, 0x66, 0x62, 0x63, 0x00,
        0x00,
    ],
    // ASCII 83 ('S')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x40, 0x40, 0x60, 0x38, 0x0c, 0x06, 0x04, 0x78, 0x00,
        0x00,
    ],
    // ASCII 84 ('T')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00,
        0x00,
    ],
    // ASCII 85 ('U')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x61, 0x23, 0x1e, 0x00,
        0x00,
    ],
    // ASCII 86 ('V')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0xc3, 0xc2, 0x42, 0x66, 0x24, 0x2c, 0x3c, 0x18, 0x18, 0x00,
        0x00,
    ],
    // ASCII 87 ('W')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0xc6, 0xc6, 0x46, 0x4f, 0x69, 0x29, 0x39, 0x30, 0x30, 0x00,
        0x00,
    ],
    // ASCII 88 ('X')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0xc2, 0x64, 0x2c, 0x38, 0x18, 0x38, 0x24, 0x46, 0xc2, 0x00,
        0x00,
    ],
    // ASCII 89 ('Y')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0xc2, 0x46, 0x64, 0x2c, 0x38, 0x10, 0x10, 0x10, 0x10, 0x00,
        0x00,
    ],
    // ASCII 90 ('Z')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x04, 0x0c, 0x08, 0x10, 0x30, 0x20, 0x40, 0xfe, 0x00,
        0x00,
    ],
    // ASCII 91 ('[')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
        0x70,
    ],
    // ASCII 92 ('\\')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x40, 0x40, 0x60, 0x20, 0x20, 0x10, 0x10, 0x18, 0x00,
        0x00,
    ],
    // ASCII 93 (']')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
        0xe0,
    ],
    // ASCII 94 ('^')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x38, 0x28, 0x24, 0x44, 0x42, 0x00, 0x00, 0x00, 0x00,
        0x00,
    ],
    // ASCII 95 ('_')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0xfc,
    ],
    // ASCII 96 ('`')
    [
        0x00, 0x00, 0x00, 0x00, 0x40, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00,
    ],
    // ASCII 97 ('a')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x04, 0x04, 0x7c, 0x44, 0x4c, 0x7c, 0x00,
        0x00,
    ],
    // ASCII 98 ('b')
    [
        0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x40, 0x7c, 0x66, 0x42, 0x42, 0x42, 0x66, 0x5c, 0x00,
        0x00,
    ],
    // ASCII 99 ('c')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x60, 0x40, 0x40, 0x40, 0x60, 0x3c, 0x00,
        0x00,
    ],
    // ASCII 100 ('d')
    [
        0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x3e, 0x66, 0x42, 0x42, 0x42, 0x66, 0x3a, 0x00,
        0x00,
    ],
    // ASCII 101 ('e')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x64, 0x46, 0x7e, 0x40, 0x60, 0x3c, 0x00,
        0x00,
    ],
    // ASCII 102 ('f')
    [
        0x00, 0x00, 0x00, 0x00, 0x38, 0x60, 0x60, 0xf0, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x00,
        0x00,
    ],
    // ASCII 103 ('g')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3a, 0x66, 0x42, 0x42, 0x42, 0x66, 0x3e, 0x02,
        0x06,
    ],
    // ASCII 104 ('h')
    [
        0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x40, 0x7c, 0x66, 0x42, 0x42, 0x42, 0x42, 0x42, 0x00,
        0x00,
    ],
    // ASCII 105 ('i')
    [
        0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00,
        0x00,
    ],
    // ASCII 106 ('j')
    [
        0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
        0x40,
    ],
    // ASCII 107 ('k')
    [
        0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x40, 0x44, 0x48, 0x50, 0x70, 0x58, 0x4c, 0x46, 0x00,
        0x00,
    ],
    // ASCII 108 ('l')
    [
        0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00,
        0x00,
    ],
    // ASCII 109 ('m')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5d, 0x66, 0x46, 0x46, 0x46, 0x46, 0x46, 0x00,
        0x00,
    ],
    // ASCII 110 ('n')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5c, 0x66, 0x42, 0x42, 0x42, 0x42, 0x42, 0x00,
        0x00,
    ],
    // ASCII 111 ('o')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x66, 0x42, 0x42, 0x42, 0x66, 0x3c, 0x00,
        0x00,
    ],
    // ASCII 112 ('p')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5c, 0x66, 0x42, 0x42, 0x42, 0x66, 0x7c, 0x40,
        0x40,
    ],
    // ASCII 113 ('q')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3a, 0x66, 0x42, 0x42, 0x42, 0x66, 0x3e, 0x02,
        0x02,
    ],
    // ASCII 114 ('r')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x58, 0x60, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00,
        0x00,
    ],
    // ASCII 115 ('s')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x40, 0x60, 0x38, 0x0c, 0x0c, 0x78, 0x00,
        0x00,
    ],
    // ASCII 116 ('t')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0xf0, 0x40, 0x40, 0x40, 0x40, 0x60, 0x30, 0x00,
        0x00,
    ],
    // ASCII 117 ('u')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x42, 0x42, 0x42, 0x42, 0x66, 0x3a, 0x00,
        0x00,
    ],
    // ASCII 118 ('v')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6, 0xc4, 0x4c, 0x68, 0x28, 0x38, 0x30, 0x00,
        0x00,
    ],
    // ASCII 119 ('w')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcc, 0xcc, 0x4e, 0x5a, 0x72, 0x33, 0x31, 0x00,
        0x00,
    ],
    // ASCII 120 ('x')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x44, 0x6c, 0x38, 0x30, 0x38, 0x6c, 0xc4, 0x00,
        0x00,
    ],
    // ASCII 121 ('y')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6, 0x44, 0x4c, 0x68, 0x28, 0x38, 0x10, 0x30,
        0x20,
    ],
    // ASCII 122 ('z')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x08, 0x10, 0x30, 0x20, 0x40, 0xfc, 0x00,
        0x00,
    ],
    // ASCII 123 ('{')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x20, 0x20, 0x20, 0x20, 0xc0, 0x20, 0x20, 0x20, 0x20,
        0x18,
    ],
    // ASCII 124 ('|')
    [
        0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
        0x10,
    ],
    // ASCII 125 ('}')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x20, 0x20, 0x20, 0x20, 0x18, 0x20, 0x20, 0x20, 0x60,
        0xc0,
    ],
    // ASCII 126 ('~')
    [
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x72, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00,
    ],
];

/// Retrieves the 8x16 bitmask slice for a given printable ASCII character.
#[inline]
pub fn get_glyph(c: char) -> Option<&'static [u8; 16]> {
    let code = c as u32;
    if (32..=126).contains(&code) {
        Some(&FONT_8X16[(code - 32) as usize])
    } else {
        None
    }
}

/// Fallback replacement glyph (hollow box / tofu) for unresolved Unicode characters.
pub const TOFU_GLYPH: [u8; 16] = [
    0x00, 0x00, 0x7e, 0x42, 0x42, 0x5a, 0x5a, 0x42, 0x5a, 0x5a, 0x42, 0x42, 0x7e, 0x00, 0x00, 0x00,
];

/// Maps common typographical Unicode characters to their visual ASCII approximations.
pub fn map_common_unicode(c: char) -> Option<&'static [u8; 16]> {
    match c {
        '\u{00A0}' => get_glyph(' '), // Non-breaking space
        '\u{2018}' | '\u{2019}' | '\u{201A}' | '\u{201B}' => get_glyph('\''), // Single curly quotes
        '\u{201C}' | '\u{201D}' | '\u{201E}' | '\u{201F}' => get_glyph('"'), // Double curly quotes
        '\u{2013}' | '\u{2014}' | '\u{2212}' => get_glyph('-'), // En-dash, Em-dash, Minus sign
        '\u{2026}' => get_glyph('.'), // Horizontal ellipsis
        '\u{2022}' => get_glyph('*'), // Bullet
        '\u{00B7}' => get_glyph('.'), // Middle dot
        '\u{00AB}' => get_glyph('<'), // Left-pointing double angle quotation mark
        '\u{00BB}' => get_glyph('>'), // Right-pointing double angle quotation mark
        '\u{00D7}' => get_glyph('x'), // Multiplication sign
        '\u{00F7}' => get_glyph('/'), // Division sign
        '\u{00B1}' => get_glyph('+'), // Plus-minus sign
        '\u{00B0}' => get_glyph('o'), // Degree sign
        _ => None,
    }
}

/// Retrieves a glyph bitmask, resolving common Unicode symbols or falling back to the standard tofu box.
#[inline]
pub fn get_glyph_or_fallback(c: char) -> &'static [u8; 16] {
    if let Some(glyph) = get_glyph(c) {
        glyph
    } else if let Some(mapped) = map_common_unicode(c) {
        mapped
    } else {
        &TOFU_GLYPH
    }
}

/// Operating system platform indicator for font resolution.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OsPlatform {
    Linux,
    MacOS,
    Windows,
    Other,
}

impl OsPlatform {
    pub const fn current() -> Self {
        if cfg!(target_os = "macos") {
            Self::MacOS
        } else if cfg!(target_os = "windows") {
            Self::Windows
        } else if cfg!(target_os = "linux") {
            Self::Linux
        } else {
            Self::Other
        }
    }
}

/// Generic CSS font family keyword.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GenericFontFamily {
    SansSerif,
    Serif,
    Monospace,
    Cursive,
    Fantasy,
    SystemUi,
}

/// System font fallback cascade resolver across Linux, macOS, and Windows.
#[derive(Debug, Clone)]
pub struct SystemFontResolver;

impl SystemFontResolver {
    /// Resolves default system font chain for generic families on the current OS.
    pub fn default_chain_for_generic(family: GenericFontFamily) -> Vec<String> {
        Self::chain_for_platform(family, OsPlatform::current())
    }

    /// Resolves system font chain for a specific OS platform.
    pub fn chain_for_platform(family: GenericFontFamily, platform: OsPlatform) -> Vec<String> {
        match platform {
            OsPlatform::MacOS => match family {
                GenericFontFamily::SansSerif | GenericFontFamily::SystemUi => vec![
                    "-apple-system".into(),
                    "BlinkMacSystemFont".into(),
                    "SF Pro Text".into(),
                    "Helvetica Neue".into(),
                    "Helvetica".into(),
                    "Arial".into(),
                    "PingFang SC".into(),
                    "Hiragino Sans".into(),
                    "Apple Color Emoji".into(),
                    "sans-serif".into(),
                ],
                GenericFontFamily::Serif => vec![
                    "New York".into(),
                    "Times New Roman".into(),
                    "Times".into(),
                    "Songti SC".into(),
                    "serif".into(),
                ],
                GenericFontFamily::Monospace => vec![
                    "SF Mono".into(),
                    "Menlo".into(),
                    "Monaco".into(),
                    "Courier New".into(),
                    "monospace".into(),
                ],
                _ => vec!["-apple-system".into(), "sans-serif".into()],
            },
            OsPlatform::Windows => match family {
                GenericFontFamily::SansSerif | GenericFontFamily::SystemUi => vec![
                    "Segoe UI".into(),
                    "Calibri".into(),
                    "Arial".into(),
                    "Microsoft YaHei".into(),
                    "Meiryo".into(),
                    "Segoe UI Emoji".into(),
                    "Segoe UI Symbol".into(),
                    "sans-serif".into(),
                ],
                GenericFontFamily::Serif => vec![
                    "Georgia".into(),
                    "Times New Roman".into(),
                    "Cambria".into(),
                    "SimSun".into(),
                    "serif".into(),
                ],
                GenericFontFamily::Monospace => vec![
                    "Consolas".into(),
                    "Cascadia Code".into(),
                    "Courier New".into(),
                    "monospace".into(),
                ],
                _ => vec!["Segoe UI".into(), "sans-serif".into()],
            },
            OsPlatform::Linux | OsPlatform::Other => match family {
                GenericFontFamily::SansSerif | GenericFontFamily::SystemUi => vec![
                    "Ubuntu".into(),
                    "DejaVu Sans".into(),
                    "Liberation Sans".into(),
                    "Noto Sans".into(),
                    "Noto Sans CJK SC".into(),
                    "Noto Color Emoji".into(),
                    "sans-serif".into(),
                ],
                GenericFontFamily::Serif => vec![
                    "DejaVu Serif".into(),
                    "Liberation Serif".into(),
                    "Noto Serif".into(),
                    "serif".into(),
                ],
                GenericFontFamily::Monospace => vec![
                    "Ubuntu Mono".into(),
                    "DejaVu Sans Mono".into(),
                    "Liberation Mono".into(),
                    "Noto Sans Mono".into(),
                    "monospace".into(),
                ],
                _ => vec!["Ubuntu".into(), "sans-serif".into()],
            },
        }
    }

    /// Parses a CSS `font-family` property value and returns an ordered cascade of resolved font names.
    pub fn parse_font_family_cascade(css_value: &str) -> Vec<String> {
        let mut results = Vec::new();
        for item in css_value.split(',') {
            let trimmed = item.trim().trim_matches(|c| c == '\'' || c == '"');
            if trimmed.is_empty() {
                continue;
            }
            match trimmed.to_ascii_lowercase().as_str() {
                "sans-serif" => {
                    for f in Self::default_chain_for_generic(GenericFontFamily::SansSerif) {
                        if !results.contains(&f) {
                            results.push(f);
                        }
                    }
                }
                "serif" => {
                    for f in Self::default_chain_for_generic(GenericFontFamily::Serif) {
                        if !results.contains(&f) {
                            results.push(f);
                        }
                    }
                }
                "monospace" => {
                    for f in Self::default_chain_for_generic(GenericFontFamily::Monospace) {
                        if !results.contains(&f) {
                            results.push(f);
                        }
                    }
                }
                "system-ui" | "-apple-system" | "blinkmacsystemfont" => {
                    let token = trimmed.to_string();
                    if !results.contains(&token) {
                        results.push(token);
                    }
                    for f in Self::default_chain_for_generic(GenericFontFamily::SystemUi) {
                        if !results.contains(&f) {
                            results.push(f);
                        }
                    }
                }
                _ => {
                    let owned = trimmed.to_string();
                    if !results.contains(&owned) {
                        results.push(owned);
                    }
                }
            }
        }

        if results.is_empty() {
            results = Self::default_chain_for_generic(GenericFontFamily::SansSerif);
        }
        results
    }
}

/// Fallback font resolution chain with per-glyph coverage checking.
#[derive(Debug, Clone)]
pub struct FontFallbackChain {
    pub families: Vec<String>,
}

impl FontFallbackChain {
    pub fn new(families: Vec<String>) -> Self {
        Self { families }
    }

    pub fn from_css(css_value: &str) -> Self {
        Self {
            families: SystemFontResolver::parse_font_family_cascade(css_value),
        }
    }

    pub fn primary_font(&self) -> Option<&str> {
        self.families.first().map(|s| s.as_str())
    }

    /// Resolves whether a character is natively printable or mapped by the fallback table.
    pub fn can_render(&self, c: char) -> bool {
        get_glyph(c).is_some() || map_common_unicode(c).is_some()
    }
}