sgl-2d 0.7.0

Caller-driven 2D rendering, assets, text, lighting, and immediate UI for SGL games
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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
//! Glyph-atlas text rendering (AR-6): TTF glyphs rasterized on demand with
//! **fontdue** (antialiased, at the exact target pixel size — matching Godot's
//! antialiased font rendering at 1:1 scale) into shelf-packed CPU atlas
//! pages, then drawn through the ordinary sprite pipeline as
//! [`SpriteInstance`] quads on either [`DrawList`] channel.
//!
//! Flow per frame:
//! 1. [`TextRenderer::draw`] / [`draw_in_rect`](TextRenderer::draw_in_rect)
//!    lay out a line, rasterizing any glyph not yet in a page, and queue
//!    positioned quads (shadow → outline → fill push order; z ties resolve
//!    by push order, PR-2).
//! 2. [`TextRenderer::end_frame`] republishes each dirty page into its one
//!    [`Texture`] asset, in place under a handle that stays stable for the
//!    page's lifetime, flushes the queued quads into the `DrawList`, and
//!    returns the changed pages' handles, which the app passes to
//!    `Renderer::upload_texture` or `SpritePass::upload` before rendering
//!    (an already-uploaded page has its GPU pixels replaced in place). A
//!    page costs one asset and one texture however often it changes;
//!    unchanged pages and other textures are not uploaded again.
//!
//! **Outline** (Godot `outline_size`) is done by re-drawing the glyph quads
//! offset along concentric rings of the outline radius, in the outline
//! color, before the fill — no font metrics involved. **Shadow** (Godot
//! `shadow_offset`/`shadow_size`) is a copy drawn first at the shadow
//! offset, with its own ring spread.

use std::collections::HashMap;
use std::path::{Path, PathBuf};

use crate::assets::{Assets, Handle, Texture};
use crate::canvas::atlas::{PADDING, ShelfPacker};
use crate::canvas::draw::{DrawList, Rect, SpriteInstance};
use sgl_core::math::Vec2;

/// Pixel size of one (square) glyph atlas page. Comfortably holds the 84 px
/// title alphabet; more pages open as needed. A glyph too large for it (a
/// large size at a high pixel scale) gets a square page of its own, sized to
/// it and holding nothing else, which uploads like any texture (a device
/// with lower limits refuses one wider than its `max_texture_dimension_2d`
/// with `TextureError::TooLarge`). A glyph whose page would exceed
/// [`MAX_GLYPH_PAGE_SIZE`] is not drawn (its advance still applies).
pub const GLYPH_PAGE_SIZE: u32 = 512;

/// Largest glyph page side, in pixels: 8192, the `max_texture_dimension_2d`
/// of the default limits [`Gpu`](crate::canvas::gpu::Gpu) requests (and
/// WebGPU's default), past which a page could never upload. A larger glyph
/// is neither rasterized nor packed.
pub const MAX_GLYPH_PAGE_SIZE: u32 = 8192;

/// Which [`DrawList`] channel text lands in.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TextChannel {
    /// World-space (drawn through the world camera) — damage numbers etc.
    /// Text is laid out in y-down pixels (glyph advances, ascent, outline
    /// and shadow offsets are pixel offsets from the anchor), so it is
    /// correct under the default pixel camera. Under a world-unit camera
    /// (`Camera::with_units`) the sprite pass sizes each glyph quad to
    /// `px / pixels_per_unit` units, but the pixel layout offsets are still
    /// added to the world-unit anchor unconverted — the world channel is
    /// not converted for units cameras.
    World,
    /// Screen-space over the logical 960×540 view — UI/HUD (the usual case).
    Screen,
}

/// Horizontal alignment inside a rect (Godot label `horizontal_alignment`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HAlign {
    Left,
    Center,
    Right,
}

/// Vertical alignment inside a rect (Godot label `vertical_alignment`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VAlign {
    Top,
    Center,
    Bottom,
}

/// Outline pass parameters (Godot `outline_size` + `font_outline_color`).
#[derive(Debug, Clone, Copy)]
pub struct Outline {
    /// Outline radius in pixels (Godot `outline_size`; PR-9: 10 for the
    /// title, 1 elsewhere). Capped at [`MAX_RING_WIDTH`]; a non-finite
    /// width draws no outline.
    pub width: f32,
    pub color: [f32; 4],
}

/// Drop-shadow pass parameters (Godot `shadow_offset` + `shadow_size`).
#[derive(Debug, Clone, Copy)]
pub struct Shadow {
    /// Pixel offset of the shadow copy (PR-9 title: `(5, 5)`).
    pub offset: Vec2,
    /// Ring spread of the shadow, like an outline width (Godot
    /// `shadow_size`; PR-9 title: 10). `0.0` = a plain offset copy. Capped
    /// at [`MAX_RING_WIDTH`]; a non-finite spread draws a plain copy.
    pub spread: f32,
    pub color: [f32; 4],
}

/// How a piece of text is drawn: size, fill color, optional outline and
/// shadow. Sizes are pixels of em (Godot font "pt" at default settings).
#[derive(Debug, Clone, Copy)]
pub struct TextStyle {
    pub px: f32,
    /// Fill color (straight-alpha sRGB, like sprite modulate).
    pub color: [f32; 4],
    pub outline: Option<Outline>,
    pub shadow: Option<Shadow>,
}

impl TextStyle {
    /// Plain text of the given pixel size and fill color.
    pub fn new(px: f32, color: [f32; 4]) -> Self {
        Self {
            px,
            color,
            outline: None,
            shadow: None,
        }
    }

    /// Add a black outline of `width` pixels (the Godot default outline
    /// color is black).
    #[must_use]
    pub fn with_outline(mut self, width: f32) -> Self {
        self.outline = Some(Outline {
            width,
            color: [0.0, 0.0, 0.0, 1.0],
        });
        self
    }

    /// Add a black drop shadow at `offset` with ring `spread` (Godot
    /// `shadow_offset` / `shadow_size`).
    #[must_use]
    pub fn with_shadow(mut self, offset: Vec2, spread: f32) -> Self {
        self.shadow = Some(Shadow {
            offset,
            spread,
            color: [0.0, 0.0, 0.0, 1.0],
        });
        self
    }
}

/// Text-layer failures (font file loading/parsing).
#[derive(Debug)]
pub enum TextError {
    /// Reading the font file failed.
    Io {
        path: PathBuf,
        source: std::io::Error,
    },
    /// fontdue could not parse the font data.
    Font(&'static str),
}

impl std::fmt::Display for TextError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Io { path, source } => write!(f, "font read {}: {source}", path.display()),
            Self::Font(msg) => write!(f, "font parse: {msg}"),
        }
    }
}

impl std::error::Error for TextError {}

/// Cache key: character + the exact raster size's bits. A glyph's bitmap,
/// bearings and advance all depend on the size, so sizes never share a slot:
/// a shared slot would lay out text by whichever size filled it first.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct GlyphKey {
    c: char,
    px_bits: u32,
}

/// A rasterized glyph's page placement + the metrics needed to position it.
#[derive(Debug, Clone, Copy)]
struct GlyphBitmap {
    page: usize,
    /// Placement in page pixels.
    x: u32,
    y: u32,
    w: u32,
    h: u32,
    /// fontdue metrics: left side bearing…
    xmin: f32,
    /// …and the y-up offset of the bitmap *bottom* from the baseline.
    ymin: f32,
}

/// A glyph's layout: advance always; bitmap only when it has ink (space etc.
/// advance without a quad). Only slots with a bitmap are cached, so page
/// recycling bounds the cache whatever sizes a game animates through.
#[derive(Debug, Clone, Copy)]
struct GlyphSlot {
    bitmap: Option<GlyphBitmap>,
    advance: f32,
}

/// One CPU-side atlas page: pixels + packer + the publish state.
struct GlyphPage {
    /// Width and height in pixels: [`GLYPH_PAGE_SIZE`], or an oversized
    /// glyph's larger side plus padding.
    side: u32,
    /// Holds one oversized glyph and nothing else: never packed into, so
    /// ordinary glyphs never depend on a page that may not upload.
    dedicated: bool,
    packer: ShelfPacker,
    /// RGBA8: white RGB, glyph coverage in alpha (the shader multiplies the
    /// instance color in, so one page serves every text color).
    pixels: Vec<u8>,
    dirty: bool,
    /// The page's asset handle (`None` until first publish), stable from
    /// then on: republishing replaces the asset in place.
    handle: Option<Handle<Texture>>,
    /// The last frame a glyph on this page was drawn or placed.
    last_used: u64,
}

impl GlyphPage {
    /// A page of `side` pixels square; `side` is at most
    /// [`MAX_GLYPH_PAGE_SIZE`].
    fn new(side: u32, dedicated: bool, frame: u64) -> Self {
        Self {
            side,
            dedicated,
            packer: ShelfPacker::new(side, side),
            pixels: vec![0; page_bytes(side)],
            dirty: false,
            handle: None,
            last_used: frame,
        }
    }

    /// Empty the page for reuse at `side` pixels square, keeping its kind
    /// and handle (republishing replaces the texture, resized if need be).
    fn reset(&mut self, side: u32, frame: u64) {
        if side == self.side {
            self.pixels.fill(0);
        } else {
            self.pixels = vec![0; page_bytes(side)];
            self.side = side;
        }
        self.packer = ShelfPacker::new(side, side);
        self.last_used = frame;
    }
}

/// RGBA bytes of a page `side` pixels square; `side` is at most
/// [`MAX_GLYPH_PAGE_SIZE`].
fn page_bytes(side: u32) -> usize {
    usize::try_from(u64::from(side) * u64::from(side) * 4)
        .expect("a page within MAX_GLYPH_PAGE_SIZE is addressable")
}

/// A queued glyph quad, waiting for [`TextRenderer::end_frame`] to resolve
/// its page handle.
struct Quad {
    page: usize,
    src: Rect,
    /// Quad center in logical pixels (`SpriteInstance` convention).
    center: Vec2,
    /// Instance scale: `1 / pixel_scale` at queue time, so a glyph
    /// rasterized at `s ×` draws at layout size (`1.0` on the game path —
    /// bit-identical to the pre-scale default).
    scale: f32,
    color: [f32; 4],
    z: f32,
    screen: bool,
    /// Clip rect captured from [`TextRenderer::set_clip`] at queue time
    /// (quads flush at `end_frame`, after the clip state has moved on).
    clip: Option<Rect>,
}

/// On-demand glyph rasterizer + atlas + line layout over one TTF font.
pub struct TextRenderer {
    font: fontdue::Font,
    /// Unique per-renderer id namespacing the published glyph-page asset
    /// paths — two renderers (game Risque + editor sans, D-26) must never
    /// collide on `sgl://glyph-page/...` (path collisions alias handles
    /// and scramble glyphs).
    instance: u64,
    pages: Vec<GlyphPage>,
    glyphs: HashMap<GlyphKey, GlyphSlot>,
    queued: Vec<Quad>,
    /// Current clip rect applied to newly queued quads (scrollable UI).
    clip: Option<Rect>,
    /// Physical pixels per layout unit (see [`set_pixel_scale`]).
    ///
    /// [`set_pixel_scale`]: Self::set_pixel_scale
    pixel_scale: f32,
    /// Frames completed by [`end_frame`](Self::end_frame); pages record the
    /// last one that used them.
    frame: u64,
}

impl TextRenderer {
    /// Parse a TTF from raw bytes.
    pub fn new(font_bytes: &[u8]) -> Result<Self, TextError> {
        let settings = fontdue::FontSettings {
            // Optimize glyph geometry for the largest size in use (the 84 px
            // title, PR-9); smaller sizes only get more accurate.
            scale: 84.0,
            ..fontdue::FontSettings::default()
        };
        let font = fontdue::Font::from_bytes(font_bytes, settings).map_err(TextError::Font)?;
        // A process-wide counter: usize atomics exist on every target
        // (wasm32 lacks 64-bit atomics without the threads proposal).
        static NEXT_INSTANCE: std::sync::atomic::AtomicUsize =
            std::sync::atomic::AtomicUsize::new(0);
        let instance = NEXT_INSTANCE.fetch_add(1, std::sync::atomic::Ordering::Relaxed) as u64;
        Ok(Self {
            font,
            instance,
            pages: Vec::new(),
            glyphs: HashMap::new(),
            queued: Vec::new(),
            clip: None,
            pixel_scale: 1.0,
            frame: 0,
        })
    }

    /// Set the physical-pixels-per-layout-unit factor. At scale `s`, glyphs
    /// rasterize at `s ×` the requested size and their quads shrink back to
    /// layout units, snapped to the `1/s` grid — so a render target `s ×`
    /// the layout space samples them 1:1 and text stays crisp at native
    /// resolution (the editor scene). The default `1.0` is bit-identical to
    /// the pre-scale behavior; game scenes never change it (parity, D-11).
    pub fn set_pixel_scale(&mut self, scale: f32) {
        self.pixel_scale = scale.max(0.25);
    }

    /// Set the clip rect applied to text queued from now on (logical view
    /// pixels; `None` = unclipped). Captured per quad — safe to change
    /// between draws within a frame (scrollable panels set it around their
    /// content and reset it after).
    pub fn set_clip(&mut self, clip: Option<Rect>) {
        self.clip = clip;
    }

    /// Read and parse a TTF file (e.g. `assets/ui/fonts/Risque-Regular.ttf`).
    pub fn load(path: &Path) -> Result<Self, TextError> {
        let bytes = std::fs::read(path).map_err(|source| TextError::Io {
            path: path.to_owned(),
            source,
        })?;
        Self::new(&bytes)
    }

    /// Number of atlas pages opened so far (diagnostics/tests). It grows only
    /// when every page is full and was used in the current frame: otherwise
    /// the least recently used page is emptied and reused, so text whose
    /// size changes every frame keeps a bounded number of pages.
    pub fn page_count(&self) -> usize {
        self.pages.len()
    }

    /// Single-line size of `text` at `px`: width = advances + kerning,
    /// height = [`line_height`](Self::line_height). Pure metrics — no
    /// rasterization. A size whose raster size (`px × pixel_scale`) is not
    /// positive and finite measures zero, matching [`draw`](Self::draw),
    /// which draws nothing at it.
    ///
    /// **Coupling with [`draw`](Self::draw)**: `draw` advances the pen with
    /// metrics fetched at `px × pixel_scale` and divided back by the scale,
    /// while this measures at `px` directly. fontdue's advances are linear
    /// in the requested size, so the two agree to floating-point rounding
    /// (sub-ULP today — layout code may treat this as `draw`'s width). If
    /// `draw`'s advance math ever stops being a pure rescale of the `px`
    /// metrics, route this through the same `raster_px / s` computation.
    pub fn measure(&self, text: &str, px: f32) -> Vec2 {
        if !drawable_px(px * self.pixel_scale) {
            return Vec2::ZERO;
        }
        let mut width = 0.0;
        let mut prev: Option<char> = None;
        for c in text.chars() {
            if let Some(p) = prev
                && let Some(kern) = self.font.horizontal_kern(p, c, px)
            {
                width += kern;
            }
            width += self.font.metrics(c, px).advance_width;
            prev = Some(c);
        }
        Vec2::new(width, self.line_height(px))
    }

    /// Line height at `px`: ascent − descent (descent is negative), the box
    /// a single line occupies for vertical alignment (Godot label sizing).
    pub fn line_height(&self, px: f32) -> f32 {
        self.font
            .horizontal_line_metrics(px)
            .map_or(px, |m| m.ascent - m.descent)
    }

    /// Baseline offset from the top of the line box at `px`.
    pub fn ascent(&self, px: f32) -> f32 {
        self.font
            .horizontal_line_metrics(px)
            .map_or(px * 0.8, |m| m.ascent)
    }

    /// Queue one line of text with its top-left at `top_left` (logical
    /// pixels). Pass order per glyph run: shadow, outline rings, fill — all
    /// at `z`, relying on the `DrawList`'s stable sort (PR-2 push order).
    /// Quads reach the list at [`end_frame`](Self::end_frame). A size that
    /// is not positive and finite draws nothing.
    pub fn draw(
        &mut self,
        text: &str,
        top_left: Vec2,
        style: &TextStyle,
        z: f32,
        channel: TextChannel,
    ) {
        // Layout pass: absolute, pixel-snapped fill positions (glyphs are
        // pre-antialiased at the target size; placement snaps to the
        // physical-pixel grid — the `1/pixel_scale` layout grid — so they
        // stay crisp under the nearest-sampled 1:1 target mapping). At
        // `pixel_scale = 1` every operation below is bit-identical to the
        // pre-scale layout (multiplying/dividing by 1.0 is exact).
        let s = self.pixel_scale;
        let raster_px = style.px * s;
        if !drawable_px(raster_px) {
            return;
        }
        let snap = |v: f32| (v * s).round() / s;
        let baseline = top_left.y + self.ascent(raster_px) / s;
        let mut placed: Vec<(GlyphBitmap, Vec2)> = Vec::new();
        let mut pen = top_left.x;
        let mut prev: Option<char> = None;
        for c in text.chars() {
            if let Some(p) = prev
                && let Some(kern) = self.font.horizontal_kern(p, c, raster_px)
            {
                pen += kern / s;
            }
            let slot = self.glyph(c, raster_px);
            if let Some(b) = slot.bitmap {
                // In use this frame: never recycled before its quads flush.
                self.pages[b.page].last_used = self.frame;
                let gx = snap(pen + b.xmin / s);
                let gy = snap(baseline - b.ymin / s - b.h as f32 / s);
                placed.push((b, Vec2::new(gx, gy)));
            }
            pen += slot.advance / s;
            prev = Some(c);
        }

        // Emission passes: each queues every placed glyph at an extra offset.
        let screen = channel == TextChannel::Screen;
        let clip = self.clip;
        let queued = &mut self.queued;
        let mut emit = |offset: Vec2, color: [f32; 4]| {
            for (b, pos) in &placed {
                let size = Vec2::new(b.w as f32, b.h as f32) / s;
                queued.push(Quad {
                    page: b.page,
                    src: Rect::new(b.x as f32, b.y as f32, b.w as f32, b.h as f32),
                    center: *pos + offset + size * 0.5,
                    scale: 1.0 / s,
                    color,
                    z,
                    screen,
                    clip,
                });
            }
        };

        if let Some(shadow) = style.shadow {
            for o in ring_offsets(shadow.spread) {
                emit(shadow.offset + o, shadow.color);
            }
            emit(shadow.offset, shadow.color);
        }
        if let Some(outline) = style.outline {
            for o in ring_offsets(outline.width) {
                emit(o, outline.color);
            }
        }
        emit(Vec2::ZERO, style.color);
    }

    /// Queue one line aligned inside `rect` — how Godot labels position text
    /// (rect + h/v alignment, PR-9).
    #[allow(clippy::too_many_arguments)]
    pub fn draw_in_rect(
        &mut self,
        text: &str,
        rect: Rect,
        h_align: HAlign,
        v_align: VAlign,
        style: &TextStyle,
        z: f32,
        channel: TextChannel,
    ) {
        let size = self.measure(text, style.px);
        let space = rect.size();
        let x = match h_align {
            HAlign::Left => rect.min.x,
            HAlign::Center => rect.min.x + (space.x - size.x) * 0.5,
            HAlign::Right => rect.max.x - size.x,
        };
        let y = match v_align {
            VAlign::Top => rect.min.y,
            VAlign::Center => rect.min.y + (space.y - size.y) * 0.5,
            VAlign::Bottom => rect.max.y - size.y,
        };
        self.draw(text, Vec2::new(x, y), style, z, channel);
    }

    /// Publish dirty pages into their texture assets and flush the queued
    /// quads into `list`. Each page keeps one asset, replaced in place, so
    /// its handle never changes. Returns the handles of the pages that
    /// changed this frame — the app must upload each
    /// (`Renderer::upload_texture` or `SpritePass::upload`, which replace an
    /// already-uploaded page's pixels under its handle) before rendering the
    /// frame. Call once per presented frame after all `draw` calls. The glyph
    /// instances it emits are valid for that frame only: pages are reused, so
    /// a retained `DrawList`, or a second `end_frame` before the first is
    /// submitted, can sample a reused page's new pixels. An upload can fail
    /// (`TextureError::TooLarge` for an oversized glyph's dedicated page on a
    /// device with a smaller texture limit): log it and carry on rather than
    /// unwrapping. A dedicated page holds one glyph and is sized to it, so
    /// only that glyph goes undrawn.
    pub fn end_frame(
        &mut self,
        assets: &mut Assets<Texture>,
        list: &mut DrawList,
    ) -> Vec<Handle<Texture>> {
        let mut changed = Vec::new();
        for (i, page) in self.pages.iter_mut().enumerate() {
            if !page.dirty {
                continue;
            }
            let path = PathBuf::from(format!("sgl://glyph-page/{}/{i}", self.instance));
            let handle = assets.insert(
                path,
                Texture {
                    width: page.side,
                    height: page.side,
                    rgba: page.pixels.clone(),
                },
            );
            page.handle = Some(handle);
            page.dirty = false;
            changed.push(handle);
        }

        for quad in self.queued.drain(..) {
            let Some(handle) = self.pages[quad.page].handle else {
                continue; // unreachable: a quad's page was dirtied at least once
            };
            let instance = SpriteInstance {
                src: Some(quad.src),
                scale: Vec2::splat(quad.scale),
                color: quad.color,
                z: quad.z,
                clip: quad.clip,
                ..SpriteInstance::new(handle, quad.center)
            };
            if quad.screen {
                list.push_screen(instance);
            } else {
                list.push(instance);
            }
        }
        self.frame += 1;
        changed
    }

    /// Fetch (or rasterize + pack) the glyph for `c` at `px`.
    fn glyph(&mut self, c: char, px: f32) -> GlyphSlot {
        let key = GlyphKey {
            c,
            px_bits: px.to_bits(),
        };
        if let Some(slot) = self.glyphs.get(&key) {
            return *slot;
        }
        let metrics = self.font.metrics(c, px);
        // A size fontdue reports past `u32` (a negative size wraps its
        // dimensions near `usize::MAX`) is not drawn, like one past the cap.
        let dims = u32::try_from(metrics.width)
            .ok()
            .zip(u32::try_from(metrics.height).ok())
            .filter(|&(w, h)| {
                w != 0
                    && h != 0
                    && u64::from(w.max(h)) + u64::from(PADDING) <= u64::from(MAX_GLYPH_PAGE_SIZE)
            });
        let bitmap = if dims.is_none() {
            None
        } else {
            let (metrics, coverage) = self.font.rasterize(c, px);
            let (w, h) = (metrics.width as u32, metrics.height as u32);
            let (page, x, y) = self.place(w, h);
            let stride = self.pages[page].side as usize;
            let pixels = &mut self.pages[page].pixels;
            for row in 0..metrics.height {
                for col in 0..metrics.width {
                    let a = coverage[row * metrics.width + col];
                    let at = ((y as usize + row) * stride + x as usize + col) * 4;
                    // White ink, coverage in alpha (straight alpha — the
                    // sprite shader premultiplies).
                    pixels[at..at + 4].copy_from_slice(&[255, 255, 255, a]);
                }
            }
            self.pages[page].dirty = true;
            Some(GlyphBitmap {
                page,
                x,
                y,
                w,
                h,
                xmin: metrics.xmin as f32,
                ymin: metrics.ymin as f32,
            })
        };
        let slot = GlyphSlot {
            bitmap,
            advance: metrics.advance_width,
        };
        // A bitmap-less slot is cheap to recompute and nothing would evict it.
        if slot.bitmap.is_some() {
            self.glyphs.insert(key, slot);
        }
        slot
    }

    /// Find room for `w × h`, returning `(page index, x, y)` and marking the
    /// page used this frame. A glyph that fits a [`GLYPH_PAGE_SIZE`] page
    /// takes a standard page with space; else the least recently used
    /// standard page not drawn from this frame, emptied (its cached glyphs
    /// re-rasterize on next use); else a new standard page. A larger glyph
    /// takes a dedicated page of its own, never packed into again, exactly its
    /// larger side plus the packer's padding: the least recently used stale
    /// dedicated page, rebuilt at that size under its handle, or else a new
    /// one. So dedicated pages never outnumber the oversized glyphs one frame
    /// draws.
    fn place(&mut self, w: u32, h: u32) -> (usize, u32, u32) {
        let frame = self.frame;
        let need = w.max(h) + PADDING;
        let dedicated = need > GLYPH_PAGE_SIZE;
        let found = if dedicated {
            None
        } else {
            self.pages
                .iter_mut()
                .enumerate()
                .filter(|(_, page)| !page.dedicated)
                .find_map(|(i, page)| page.packer.insert(w, h).map(|(x, y)| (i, x, y)))
        };
        let (i, x, y) = found.unwrap_or_else(|| {
            let stale = self
                .pages
                .iter()
                .enumerate()
                .filter(|(_, page)| page.last_used < frame && page.dedicated == dedicated)
                .min_by_key(|(_, page)| page.last_used)
                .map(|(i, _)| i);
            let side = need.max(GLYPH_PAGE_SIZE);
            let i = if let Some(i) = stale {
                self.glyphs
                    .retain(|_, slot| slot.bitmap.is_none_or(|b| b.page != i));
                self.pages[i].reset(side, frame);
                i
            } else {
                self.pages.push(GlyphPage::new(side, dedicated, frame));
                self.pages.len() - 1
            };
            let (x, y) = self.pages[i]
                .packer
                .insert(w, h)
                .expect("an empty page one padding wider than the glyph fits it");
            (i, x, y)
        });
        self.pages[i].last_used = frame;
        (i, x, y)
    }
}

/// Widest outline or shadow spread drawn, in pixels. Each ring copies every
/// glyph quad, so the quad count grows with the square of the width: 64 px is
/// several times the widest authored outline (the 10 px title) and still
/// bounds a glyph to about 3,300 copies.
pub const MAX_RING_WIDTH: f32 = 64.0;

/// Offsets approximating a solid stroke of radius `width`: concentric rings
/// sampled every ~2 px, from `width` inward. Glyph coverage overlap fills
/// the gaps; a 1 px outline is the classic 8-direction ring. A width above
/// [`MAX_RING_WIDTH`] draws at that width; a non-finite width draws no ring.
fn ring_offsets(width: f32) -> Vec<Vec2> {
    let mut out = Vec::new();
    if !width.is_finite() {
        return out;
    }
    let mut r = width.min(MAX_RING_WIDTH);
    while r > 0.25 {
        let samples = ((std::f32::consts::TAU * r) / 2.0).ceil().max(8.0) as usize;
        for i in 0..samples {
            let a = std::f32::consts::TAU * i as f32 / samples as f32;
            out.push(Vec2::new(a.cos(), a.sin()) * r);
        }
        r -= 2.0;
    }
    out
}

/// Whether text at `px` is drawn: a size that is not positive and finite is
/// not (fontdue's metrics for a negative size are meaningless).
fn drawable_px(px: f32) -> bool {
    px > 0.0 && px.is_finite()
}

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

    /// #255: `draw_in_rect` alignment is exact for every corner and the
    /// center against `measure`, and a 1 px outline ring is the classic
    /// eight directions at radius 1.
    #[wasm_bindgen_test(unsupported = test)]
    fn alignment_and_the_unit_outline_ring_are_exact() {
        let mut r = renderer();
        let style = TextStyle::new(16.0, [1.0; 4]);
        let text = "Hg";
        let size = r.measure(text, 16.0);
        let rect = Rect::new(100.0, 100.0, 200.0, 80.0);
        let mut assets: Assets<Texture> = Assets::new();
        let mut anchors = Vec::new();
        for (h, v, expected) in [
            (HAlign::Left, VAlign::Top, Vec2::new(100.0, 100.0)),
            (
                HAlign::Right,
                VAlign::Bottom,
                Vec2::new(300.0 - size.x, 180.0 - size.y),
            ),
            (
                HAlign::Center,
                VAlign::Center,
                Vec2::new(
                    100.0 + (200.0 - size.x) * 0.5,
                    100.0 + (80.0 - size.y) * 0.5,
                ),
            ),
        ] {
            let mut list = DrawList::new();
            r.draw_in_rect(text, rect, h, v, &style, 0.0, TextChannel::Screen);
            r.end_frame(&mut assets, &mut list);
            let mut direct = DrawList::new();
            r.draw(text, expected, &style, 0.0, TextChannel::Screen);
            r.end_frame(&mut assets, &mut direct);
            let centers = |l: &DrawList| l.screen.iter().map(|q| q.pos).collect::<Vec<_>>();
            assert_eq!(centers(&list), centers(&direct), "{h:?} {v:?}");
            anchors.push(centers(&list));
        }
        assert_ne!(anchors[0], anchors[1]);

        let ring = ring_offsets(1.0);
        assert_eq!(ring.len(), 8);
        for o in &ring {
            assert!((o.length() - 1.0).abs() < 1e-5, "{o}");
        }
        assert!(ring_offsets(0.25).is_empty());
        assert_eq!(ring_offsets(3.0).len(), ring_offsets(1.0).len() + 10);
    }

    fn renderer() -> TextRenderer {
        TextRenderer::new(include_bytes!(
            "../../tests/fixtures/IBMPlexSans-Regular.ttf"
        ))
        .expect("test font should parse")
    }

    /// `measure()` is monotone over prefixes: adding characters never shrinks
    /// the width, and adding an inked character strictly grows it.
    #[wasm_bindgen_test(unsupported = test)]
    fn measure_is_monotone_over_prefixes() {
        let tr = renderer();
        let text = "ELEMENTAL CHAOS";
        let mut last = 0.0;
        for end in 1..=text.len() {
            let w = tr.measure(&text[..end], 48.0).x;
            assert!(w >= last, "width shrank at prefix {end}: {w} < {last}");
            if !text[end - 1..end].trim().is_empty() {
                assert!(w > last, "inked char did not widen at prefix {end}");
            }
            last = w;
        }
        // Height is the size-scaled line box.
        assert!(tr.measure(text, 48.0).y > tr.measure(text, 24.0).y);
    }

    /// Bigger sizes measure wider (the same string scales up).
    #[wasm_bindgen_test(unsupported = test)]
    fn measure_scales_with_size() {
        let tr = renderer();
        let w8 = tr.measure("99", 8.0).x;
        let w24 = tr.measure("99", 24.0).x;
        let w84 = tr.measure("99", 84.0).x;
        assert!(w8 < w24 && w24 < w84);
    }

    /// Rasterizing the game's glyph load fills a page and opens a second one
    /// (dynamic growth), and every glyph stays inside page bounds.
    #[wasm_bindgen_test(unsupported = test)]
    fn glyph_atlas_grows_new_pages() {
        let mut tr = renderer();
        for px in [84.0, 72.0, 48.0] {
            for c in ('A'..='Z').chain('a'..='z').chain('0'..='9') {
                let slot = tr.glyph(c, px);
                if let Some(b) = slot.bitmap {
                    assert!(b.x + b.w <= GLYPH_PAGE_SIZE, "{c}@{px} escapes page x");
                    assert!(b.y + b.h <= GLYPH_PAGE_SIZE, "{c}@{px} escapes page y");
                }
            }
        }
        assert!(
            tr.page_count() >= 2,
            "expected atlas growth past one page, got {}",
            tr.page_count()
        );
        // The cache is stable: re-requesting returns the same placement.
        let a = tr.glyph('A', 84.0).bitmap.unwrap();
        let b = tr.glyph('A', 84.0).bitmap.unwrap();
        assert_eq!((a.page, a.x, a.y), (b.page, b.x, b.y));
    }

    /// Space advances the pen but emits no quad.
    #[wasm_bindgen_test(unsupported = test)]
    fn space_has_advance_but_no_bitmap() {
        let mut tr = renderer();
        let slot = tr.glyph(' ', 24.0);
        assert!(slot.bitmap.is_none());
        assert!(slot.advance > 0.0);
    }

    /// `end_frame` republishes a page only when new glyphs landed, and
    /// flushes quads to the requested channel.
    #[wasm_bindgen_test(unsupported = test)]
    fn end_frame_republishes_dirty_pages_and_flushes_quads() {
        let mut tr = renderer();
        let mut assets: Assets<Texture> = Assets::new();
        let mut list = DrawList::new();

        let style = TextStyle::new(24.0, [1.0; 4]);
        tr.draw("Hello", Vec2::ZERO, &style, 5.0, TextChannel::Screen);
        let first = tr.end_frame(&mut assets, &mut list);
        assert_eq!(first.len(), 1, "one dirty page after first draw");
        assert_eq!(list.screen.len(), 5, "one quad per inked glyph");
        assert!(list.world.is_empty());
        assert!(list.screen.iter().all(|i| i.z == 5.0));
        assert!(assets.get(first[0]).is_some(), "published page resolves");

        // Same glyphs again: nothing to republish, quads still flush.
        list.clear();
        tr.draw("Hello", Vec2::ZERO, &style, 0.0, TextChannel::World);
        let again = tr.end_frame(&mut assets, &mut list);
        assert!(again.is_empty(), "clean page must not republish");
        assert_eq!(list.world.len(), 5, "world channel supported");

        // New glyphs dirty the page again → republished under its handle.
        list.clear();
        tr.draw("xyz!", Vec2::ZERO, &style, 0.0, TextChannel::Screen);
        let third = tr.end_frame(&mut assets, &mut list);
        assert_eq!(third, first, "a page keeps its handle");
    }

    /// Outline and shadow multiply the quad count (ring copies + fill), and
    /// push order is shadow → outline → fill.
    #[wasm_bindgen_test(unsupported = test)]
    fn outline_and_shadow_add_ring_copies() {
        let mut tr = renderer();
        let mut assets: Assets<Texture> = Assets::new();
        let mut list = DrawList::new();

        let plain = TextStyle::new(24.0, [1.0, 1.0, 0.0, 1.0]);
        tr.draw("AB", Vec2::ZERO, &plain, 0.0, TextChannel::Screen);
        tr.end_frame(&mut assets, &mut list);
        let plain_count = list.screen.len();
        assert_eq!(plain_count, 2);

        list.clear();
        let fancy = plain
            .with_outline(1.0)
            .with_shadow(Vec2::new(5.0, 5.0), 0.0);
        tr.draw("AB", Vec2::ZERO, &fancy, 0.0, TextChannel::Screen);
        tr.end_frame(&mut assets, &mut list);
        // 8-dir outline ring + 1 shadow copy + 1 fill = 10 per glyph.
        assert_eq!(list.screen.len(), 2 * (8 + 1 + 1));
        // Push order: shadow (black) first, fill (yellow) last.
        assert_eq!(list.screen.first().unwrap().color, [0.0, 0.0, 0.0, 1.0]);
        assert_eq!(list.screen.last().unwrap().color, [1.0, 1.0, 0.0, 1.0]);
    }

    /// Rect alignment lands the line inside the rect per the PR-9 title
    /// metrics (centered in the top 216 px band).
    #[wasm_bindgen_test(unsupported = test)]
    fn draw_in_rect_centers_within_the_rect() {
        let mut tr = renderer();
        let mut assets: Assets<Texture> = Assets::new();
        let mut list = DrawList::new();

        let style = TextStyle::new(84.0, [1.0, 1.0, 0.0, 1.0]);
        let band = Rect::new(0.0, 0.0, 960.0, 216.0);
        tr.draw_in_rect(
            "ELEMENTAL CHAOS",
            band,
            HAlign::Center,
            VAlign::Center,
            &style,
            0.0,
            TextChannel::Screen,
        );
        tr.end_frame(&mut assets, &mut list);
        assert!(!list.screen.is_empty());

        // The glyph quads' collective bounds sit centered in the band.
        let (mut min_x, mut max_x) = (f32::MAX, f32::MIN);
        let (mut min_y, mut max_y) = (f32::MAX, f32::MIN);
        for i in &list.screen {
            let half = i.src.unwrap().size() * 0.5;
            min_x = min_x.min(i.pos.x - half.x);
            max_x = max_x.max(i.pos.x + half.x);
            min_y = min_y.min(i.pos.y - half.y);
            max_y = max_y.max(i.pos.y + half.y);
        }
        let center_x = (min_x + max_x) * 0.5;
        assert!(
            (center_x - 480.0).abs() < 4.0,
            "title not horizontally centered: {center_x}"
        );
        assert!(min_y > 0.0 && max_y < 216.0, "title escapes the top band");
        // 84 px text is actually large.
        assert!(max_y - min_y > 50.0, "title glyphs suspiciously small");
    }

    /// Pixel scale 1.0 (the game path, D-11) is bit-identical to the
    /// default: same quad positions, sizes, and page sources.
    #[wasm_bindgen_test(unsupported = test)]
    fn pixel_scale_one_is_bit_identical() {
        let mut assets: Assets<Texture> = Assets::new();
        let style = TextStyle::new(24.0, [1.0; 4]);

        let mut base = renderer();
        let mut base_list = DrawList::new();
        base.draw(
            "Zoom 100%",
            Vec2::new(3.7, 9.2),
            &style,
            1.0,
            TextChannel::Screen,
        );
        base.end_frame(&mut assets, &mut base_list);

        let mut scaled = renderer();
        scaled.set_pixel_scale(1.0);
        let mut scaled_list = DrawList::new();
        scaled.draw(
            "Zoom 100%",
            Vec2::new(3.7, 9.2),
            &style,
            1.0,
            TextChannel::Screen,
        );
        scaled.end_frame(&mut assets, &mut scaled_list);

        assert_eq!(base_list.screen.len(), scaled_list.screen.len());
        for (a, b) in base_list.screen.iter().zip(&scaled_list.screen) {
            assert_eq!(a.pos, b.pos, "positions must match bit-for-bit");
            assert_eq!(a.src, b.src);
            assert_eq!(a.scale, b.scale);
        }
    }

    /// Pixel scale 2.0 (the editor at devicePixelRatio 2): glyphs rasterize
    /// at twice the size while quads keep layout-unit dimensions snapped to
    /// the half-point grid — so the 2× render target samples them 1:1.
    #[wasm_bindgen_test(unsupported = test)]
    fn pixel_scale_two_rasterizes_double_and_snaps_half_grid() {
        let mut assets: Assets<Texture> = Assets::new();
        let style = TextStyle::new(24.0, [1.0; 4]);

        let mut one = renderer();
        let mut one_list = DrawList::new();
        one.draw(
            "Hg",
            Vec2::new(10.0, 10.0),
            &style,
            0.0,
            TextChannel::Screen,
        );
        one.end_frame(&mut assets, &mut one_list);

        let mut two = renderer();
        two.set_pixel_scale(2.0);
        let mut two_list = DrawList::new();
        two.draw(
            "Hg",
            Vec2::new(10.0, 10.0),
            &style,
            0.0,
            TextChannel::Screen,
        );
        two.end_frame(&mut assets, &mut two_list);

        assert_eq!(one_list.screen.len(), two_list.screen.len());
        for (a, b) in one_list.screen.iter().zip(&two_list.screen) {
            let (sa, sb) = (a.src.unwrap(), b.src.unwrap());
            // The 2× raster is roughly double the coverage box…
            assert!(
                (sb.size().x - sa.size().x * 2.0).abs() <= 2.0,
                "raster width should double: {} vs {}",
                sa.size().x,
                sb.size().x
            );
            // …but the on-screen quad stays layout-sized (bitmap / scale)…
            let quad = sb.size() * b.scale;
            assert!(
                (quad.x - sa.size().x).abs() <= 1.5 && (quad.y - sa.size().y).abs() <= 1.5,
                "quad size should stay in layout units: {quad:?} vs {sa:?}"
            );
            // …with its corner on the half-point grid (physical pixels).
            let corner = b.pos - quad * 0.5;
            for v in [corner.x, corner.y] {
                let doubled = v * 2.0;
                assert!(
                    (doubled - doubled.round()).abs() < 1e-3,
                    "corner {v} is off the 1/2-point grid"
                );
            }
        }
    }

    /// #291: a glyph larger than a standard page (160 px at pixel scale 4,
    /// so 640 px) gets a page of its own size instead of panicking, and its
    /// quad samples exactly fontdue's coverage for that glyph.
    #[wasm_bindgen_test(unsupported = test)]
    fn an_oversized_high_dpi_glyph_gets_its_own_page() {
        let mut tr = renderer();
        let mut assets: Assets<Texture> = Assets::new();
        let mut list = DrawList::new();
        tr.set_pixel_scale(4.0);
        tr.draw(
            "W",
            Vec2::ZERO,
            &TextStyle::new(160.0, [1.0; 4]),
            0.0,
            TextChannel::Screen,
        );
        tr.end_frame(&mut assets, &mut list);

        let (metrics, coverage) = renderer().font.rasterize('W', 640.0);
        assert!(metrics.width.max(metrics.height) > GLYPH_PAGE_SIZE as usize);
        let [quad] = list.screen.as_slice() else {
            panic!("one quad for one glyph, got {}", list.screen.len());
        };
        let page = assets.get(quad.texture).expect("published page");
        let src = quad.src.expect("glyph quads have a src rect");
        assert_eq!(
            (src.size().x as usize, src.size().y as usize),
            (metrics.width, metrics.height)
        );
        let (x0, y0) = (src.min.x as usize, src.min.y as usize);
        let stride = page.width as usize;
        let alpha: Vec<u8> = (0..metrics.height)
            .flat_map(|row| {
                (0..metrics.width).map(move |col| ((y0 + row) * stride + x0 + col) * 4 + 3)
            })
            .map(|at| page.rgba[at])
            .collect();
        assert_eq!(alpha, coverage);
    }

    /// #291: an oversized glyph's page holds only that glyph: standard glyphs
    /// drawn after it, enough to need a new page, all land on
    /// `GLYPH_PAGE_SIZE` pages.
    #[wasm_bindgen_test(unsupported = test)]
    fn standard_glyphs_never_share_an_oversized_page() {
        let mut tr = renderer();
        let mut assets: Assets<Texture> = Assets::new();
        let mut list = DrawList::new();
        tr.set_pixel_scale(4.0);
        let big = TextStyle::new(160.0, [1.0; 4]);
        tr.draw("W", Vec2::ZERO, &big, 0.0, TextChannel::Screen);
        tr.end_frame(&mut assets, &mut list);
        list.clear();

        tr.set_pixel_scale(1.0);
        let alphabet: String = ('A'..='Z').chain('a'..='z').chain('0'..='9').collect();
        for px in [84.0, 72.0, 48.0] {
            let style = TextStyle::new(px, [1.0; 4]);
            tr.draw(&alphabet, Vec2::ZERO, &style, 0.0, TextChannel::Screen);
        }
        tr.end_frame(&mut assets, &mut list);
        assert!(tr.page_count() >= 3, "standard glyphs needed a second page");
        for quad in &list.screen {
            let page = assets.get(quad.texture).expect("published page");
            assert_eq!(page.width, GLYPH_PAGE_SIZE);
        }
    }

    /// #291/#327: oversized text whose size shrinks or grows every frame
    /// reuses its stale dedicated page, resized to each glyph, so the page
    /// count stays bounded, and standard pages are never handed to it.
    #[wasm_bindgen_test(unsupported = test)]
    fn animated_oversized_text_reuses_dedicated_pages() {
        let mut tr = renderer();
        let mut assets: Assets<Texture> = Assets::new();
        let mut list = DrawList::new();
        tr.set_pixel_scale(4.0);
        // Sizes shrinking from 200 px (800 px raster), then growing back.
        for frame in 0..80u8 {
            let px = 200.0 - f32::from(frame.min(80 - frame));
            let style = TextStyle::new(px, [1.0; 4]);
            tr.draw("W", Vec2::ZERO, &style, 0.0, TextChannel::Screen);
            tr.end_frame(&mut assets, &mut list);
            for quad in &list.screen {
                let page = assets.get(quad.texture).expect("published page");
                let src = quad.src.expect("glyph quads have a src rect");
                assert!(page.width > GLYPH_PAGE_SIZE, "a dedicated page");
                // Sized to the glyph: its larger side plus the padding.
                let glyph_side = src.size().x.max(src.size().y) as u32;
                assert_eq!(page.width, glyph_side + PADDING);
            }
            list.clear();
            assert!(
                tr.page_count() <= 2,
                "{} pages at frame {frame}",
                tr.page_count()
            );
        }
    }

    /// #291: oversized text that grows every frame keeps one dedicated page:
    /// no stale page is ever large enough, so it is rebuilt at each size.
    #[wasm_bindgen_test(unsupported = test)]
    fn growing_oversized_text_keeps_a_bounded_page_count() {
        let mut tr = renderer();
        let mut assets: Assets<Texture> = Assets::new();
        let mut list = DrawList::new();
        tr.set_pixel_scale(4.0);
        for frame in 0..40u8 {
            let style = TextStyle::new(160.0 + f32::from(frame), [1.0; 4]);
            tr.draw("W", Vec2::ZERO, &style, 0.0, TextChannel::Screen);
            tr.end_frame(&mut assets, &mut list);
            list.clear();
            assert!(
                tr.page_count() <= 2,
                "{} pages at frame {frame}",
                tr.page_count()
            );
        }
    }

    /// #291: a glyph whose page would exceed `MAX_GLYPH_PAGE_SIZE` is neither
    /// rasterized nor packed, and still advances the pen.
    #[wasm_bindgen_test(unsupported = test)]
    fn a_glyph_beyond_the_page_cap_is_skipped() {
        let mut tr = renderer();
        let slot = tr.glyph('W', 40_000.0);
        assert!(slot.bitmap.is_none());
        assert!(slot.advance > 0.0);
        assert_eq!(tr.page_count(), 0);
    }

    /// #430: a glyph whose page would exceed the default-limits device's
    /// `max_texture_dimension_2d` (which `Gpu` requests) could never upload,
    /// so it is neither rasterized nor packed.
    #[wasm_bindgen_test(unsupported = test)]
    fn a_glyph_past_the_default_texture_limit_is_skipped() {
        let mut tr = renderer();
        let limit = wgpu::Limits::default().max_texture_dimension_2d;
        let px = 10_400.0;
        let m = tr.font.metrics('W', px);
        assert!(m.width.max(m.height) as u32 + PADDING > limit);
        let slot = tr.glyph('W', px);
        assert!(slot.bitmap.is_none());
        assert!(slot.advance > 0.0);
        assert_eq!(tr.page_count(), 0);
    }

    /// #435: a size sharing a cache bucket with one drawn earlier lays out
    /// exactly as on a cold cache, and its glyphs sit where `measure` puts
    /// them.
    #[wasm_bindgen_test(unsupported = test)]
    fn warm_cache_layout_matches_cold_and_measure() {
        let text = "W".repeat(40);
        let style = TextStyle::new(16.1, [1.0; 4]);
        let centers = |tr: &mut TextRenderer| {
            let mut assets: Assets<Texture> = Assets::new();
            let mut list = DrawList::new();
            tr.draw(&text, Vec2::ZERO, &style, 0.0, TextChannel::Screen);
            tr.end_frame(&mut assets, &mut list);
            list.screen.iter().map(|q| q.pos.x).collect::<Vec<_>>()
        };
        let mut cold = renderer();
        let cold_x = centers(&mut cold);
        let mut warm = renderer();
        let mut assets: Assets<Texture> = Assets::new();
        warm.draw(
            &text,
            Vec2::ZERO,
            &TextStyle::new(16.0, [1.0; 4]),
            0.0,
            TextChannel::Screen,
        );
        warm.end_frame(&mut assets, &mut DrawList::new());
        assert_eq!(centers(&mut warm), cold_x);

        // Every "W" is identical, so the last one's centre is the first's
        // shifted by the measured width of the 39 before it (each centre is
        // snapped to the pixel grid, hence the 1 px tolerance).
        let expected = cold_x[0] + cold.measure(&text[..39], 16.1).x;
        let last = cold_x[39];
        assert!((last - expected).abs() <= 1.0, "{last} vs {expected}");
    }

    /// #429: a negative or non-finite size draws nothing, measures zero and
    /// does not panic.
    #[wasm_bindgen_test(unsupported = test)]
    fn negative_and_non_finite_sizes_draw_nothing() {
        let mut tr = renderer();
        let mut assets: Assets<Texture> = Assets::new();
        for px in [-16.0, f32::NEG_INFINITY, f32::INFINITY, f32::NAN] {
            let mut list = DrawList::new();
            let style = TextStyle::new(px, [1.0; 4]);
            tr.draw("Hello", Vec2::ZERO, &style, 0.0, TextChannel::Screen);
            tr.end_frame(&mut assets, &mut list);
            assert!(list.screen.is_empty(), "{px} drew glyphs");
            assert_eq!(tr.measure("Hello", px), Vec2::ZERO, "{px}");
        }
        // A finite size whose raster size overflows measures zero, as
        // `draw` draws nothing at it.
        tr.set_pixel_scale(2.0);
        assert_eq!(tr.measure("Hello", f32::MAX), Vec2::ZERO);
        assert_eq!(tr.page_count(), 0);
    }

    /// The glyph pixels each queued quad samples, in push order.
    fn sampled_glyphs(assets: &Assets<Texture>, list: &DrawList) -> Vec<Vec<u8>> {
        list.screen
            .iter()
            .map(|quad| {
                let page = assets.get(quad.texture).expect("published page");
                let src = quad.src.expect("glyph quads have a src rect");
                let (x0, y0) = (src.min.x as usize, src.min.y as usize);
                let (x1, y1) = (src.max.x as usize, src.max.y as usize);
                let stride = GLYPH_PAGE_SIZE as usize * 4;
                (y0..y1)
                    .flat_map(|y| page.rgba[y * stride + x0 * 4..y * stride + x1 * 4].to_vec())
                    .collect()
            })
            .collect()
    }

    /// #327: text whose size changes every frame reuses stale pages instead
    /// of opening new ones, and a glyph whose page was reused re-rasterizes:
    /// drawn again, its quads sample the same pixels a fresh renderer gives.
    #[wasm_bindgen_test(unsupported = test)]
    fn animated_text_sizes_keep_a_bounded_page_count() {
        let mut tr = renderer();
        let mut assets: Assets<Texture> = Assets::new();
        let mut list = DrawList::new();
        // 1000 distinct quarter-pixel sizes from 16 px to about 266 px; one
        // frame's two glyphs always fit on one page.
        for frame in 0..1000u16 {
            let style = TextStyle::new(16.0 + f32::from(frame) * 0.25, [1.0; 4]);
            tr.draw("Hi", Vec2::ZERO, &style, 0.0, TextChannel::Screen);
            tr.end_frame(&mut assets, &mut list);
            list.clear();
            assert!(
                tr.page_count() <= 3,
                "{} pages at frame {frame}",
                tr.page_count()
            );
        }

        let style = TextStyle::new(16.0, [1.0; 4]);
        tr.draw("Hi", Vec2::ZERO, &style, 0.0, TextChannel::Screen);
        tr.end_frame(&mut assets, &mut list);
        let mut fresh = renderer();
        let mut fresh_assets: Assets<Texture> = Assets::new();
        let mut fresh_list = DrawList::new();
        fresh.draw("Hi", Vec2::ZERO, &style, 0.0, TextChannel::Screen);
        fresh.end_frame(&mut fresh_assets, &mut fresh_list);
        assert_eq!(
            sampled_glyphs(&assets, &list),
            sampled_glyphs(&fresh_assets, &fresh_list)
        );
    }

    /// #435: text without ink (a space) drawn at a new size every frame does
    /// not grow the glyph cache.
    #[wasm_bindgen_test(unsupported = test)]
    fn animated_inkless_glyphs_keep_a_bounded_cache() {
        let mut tr = renderer();
        let mut assets: Assets<Texture> = Assets::new();
        let mut list = DrawList::new();
        for frame in 0..1000u16 {
            let style = TextStyle::new(16.0 + f32::from(frame) * 0.001, [1.0; 4]);
            tr.draw("  ", Vec2::ZERO, &style, 0.0, TextChannel::Screen);
            tr.end_frame(&mut assets, &mut list);
            list.clear();
        }
        assert!(tr.glyphs.len() <= 1, "{} cached slots", tr.glyphs.len());
    }

    /// #327: an infinite or NaN outline or shadow width draws no ring (and
    /// returns), and an enormous one draws at the cap.
    #[wasm_bindgen_test(unsupported = test)]
    fn non_finite_and_huge_ring_widths_are_bounded() {
        assert!(ring_offsets(f32::INFINITY).is_empty());
        assert!(ring_offsets(f32::NAN).is_empty());
        assert_eq!(ring_offsets(1e9).len(), ring_offsets(MAX_RING_WIDTH).len());
    }

    /// The 1 px outline ring is the classic 8 directions; a 10 px ring has
    /// multiple concentric rings.
    #[wasm_bindgen_test(unsupported = test)]
    fn ring_offsets_scale_with_width() {
        assert_eq!(ring_offsets(1.0).len(), 8);
        assert!(ring_offsets(0.0).is_empty());
        let big = ring_offsets(10.0);
        assert!(big.len() > 40, "10 px ring too sparse: {}", big.len());
        assert!(big.iter().all(|o| o.length() <= 10.001));
        assert!(big.iter().any(|o| o.length() < 5.0), "no inner ring");
    }
}