sim-engine 0.4.0

A validated high-performance visualization engine for simulations
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
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
use super::*;

/// Opaque host-provided glyph identity within one [`GlyphAtlas2d`].
///
/// The value may be a font glyph index, a Unicode scalar value, or a host
/// registry key. Sim;Engine only compares it for exact equality and does not
/// perform shaping or assign text semantics to it.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct GlyphId(u32);

impl GlyphId {
    /// Wraps one host-selected glyph identity.
    pub const fn new(value: u32) -> Self {
        Self(value)
    }

    /// Returns the original host-selected identity.
    pub const fn value(self) -> u32 {
        self.0
    }
}

/// Maps one glyph identity to a non-empty rectangle in an atlas image.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GlyphAtlasEntry {
    glyph: GlyphId,
    source: ImageTexelRect,
}

impl GlyphAtlasEntry {
    /// Associates a glyph with one physical-texel atlas rectangle.
    pub const fn new(glyph: GlyphId, source: ImageTexelRect) -> Self {
        Self { glyph, source }
    }

    /// Returns the opaque glyph identity.
    pub const fn glyph(self) -> GlyphId {
        self.glyph
    }

    /// Returns the physical atlas source rectangle.
    pub const fn source(self) -> ImageTexelRect {
        self.source
    }
}

/// Image and metadata limits retained with a [`GlyphAtlas2d`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GlyphAtlasBudget {
    image: ImageBudget,
    max_entries: usize,
    max_metadata_bytes: usize,
}

impl GlyphAtlasBudget {
    /// Creates non-zero image, entry-count, and metadata-byte limits.
    pub fn new(
        image: ImageBudget,
        max_entries: usize,
        max_metadata_bytes: usize,
    ) -> Result<Self, GlyphError> {
        if max_entries == 0 || max_metadata_bytes < std::mem::size_of::<GlyphAtlasEntry>() {
            return Err(GlyphError::InvalidBudget);
        }
        Ok(Self {
            image,
            max_entries,
            max_metadata_bytes,
        })
    }

    /// Returns the atlas image limits.
    pub const fn image(self) -> ImageBudget {
        self.image
    }

    /// Returns the maximum registered glyph count.
    pub const fn max_entries(self) -> usize {
        self.max_entries
    }

    /// Returns the maximum retained glyph-metadata bytes.
    pub const fn max_metadata_bytes(self) -> usize {
        self.max_metadata_bytes
    }
}

impl Default for GlyphAtlasBudget {
    fn default() -> Self {
        Self {
            image: ImageBudget::default(),
            max_entries: 65_536,
            max_metadata_bytes: 4 * 1024 * 1024,
        }
    }
}

/// Retained glyph-run count and byte limits.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GlyphRunBudget {
    max_glyphs: usize,
    max_retained_bytes: usize,
}

impl GlyphRunBudget {
    /// CPU bytes per glyph capacity slot, including sprite and GPU conversion
    /// staging. Atlas storage and allocator metadata are excluded.
    pub const RETAINED_BYTES_PER_GLYPH: usize =
        std::mem::size_of::<PositionedGlyph2d>() + ImageBatchBudget::RETAINED_BYTES_PER_SPRITE;

    /// Creates non-zero limits for positioned glyphs and recovery metadata.
    pub fn new(max_glyphs: usize, max_retained_bytes: usize) -> Result<Self, GlyphError> {
        if max_glyphs == 0 || max_retained_bytes < glyph_retained_bytes(1).unwrap_or(usize::MAX) {
            return Err(GlyphError::InvalidBudget);
        }
        Ok(Self {
            max_glyphs,
            max_retained_bytes,
        })
    }

    /// Returns the maximum number of submitted positioned glyphs.
    pub const fn max_glyphs(self) -> usize {
        self.max_glyphs
    }

    /// Returns the maximum retained glyph, sprite and conversion-staging bytes.
    pub const fn max_retained_bytes(self) -> usize {
        self.max_retained_bytes
    }
}

impl Default for GlyphRunBudget {
    fn default() -> Self {
        Self {
            max_glyphs: 100_000,
            max_retained_bytes: 32 * 1024 * 1024,
        }
    }
}

/// Failure while constructing, updating, or restoring glyph resources.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GlyphError {
    /// Image creation, update, ownership, or capacity validation failed.
    Image(ImageError),
    /// A glyph budget must fit at least one entry or positioned glyph.
    InvalidBudget,
    /// Atlas or run metadata exceeded its configured entry limit.
    EntryBudgetExceeded {
        /// Configured count ceiling.
        limit: usize,
        /// Requested entry count.
        actual: usize,
    },
    /// Atlas or run recovery metadata exceeded its configured byte limit.
    MetadataBudgetExceeded {
        /// Configured byte ceiling.
        limit: usize,
        /// Required retained bytes.
        actual: usize,
    },
    /// Two atlas entries use the same glyph identity.
    DuplicateGlyph {
        /// Repeated identity.
        glyph: GlyphId,
    },
    /// Two distinct glyph identities claim overlapping atlas texels.
    OverlappingGlyph {
        /// Identity whose rectangle was rejected.
        glyph: GlyphId,
        /// Existing identity whose rectangle overlaps it.
        conflicting_glyph: GlyphId,
    },
    /// A positioned run references a glyph absent from its atlas.
    MissingGlyph {
        /// Missing identity.
        glyph: GlyphId,
        /// Zero-based positioned-glyph index in the submitted run.
        index: usize,
    },
    /// A glyph destination, tint, or atlas rectangle is invalid.
    InvalidGlyph,
    /// CPU storage for retained metadata or temporary validation/conversion
    /// scratch could not be reserved.
    AllocationFailed {
        /// Minimum bytes requested by the failed reservation.
        requested_bytes: usize,
    },
}

impl fmt::Display for GlyphError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Image(error) => write!(formatter, "glyph image error: {error}"),
            Self::InvalidBudget => write!(formatter, "glyph budget must fit one entry"),
            Self::EntryBudgetExceeded { limit, actual } => {
                write!(formatter, "glyph count {actual} exceeds limit {limit}")
            }
            Self::MetadataBudgetExceeded { limit, actual } => write!(
                formatter,
                "glyph recovery metadata requires {actual} bytes, over limit {limit}"
            ),
            Self::DuplicateGlyph { glyph } => {
                write!(formatter, "glyph identity {} is duplicated", glyph.value())
            }
            Self::OverlappingGlyph {
                glyph,
                conflicting_glyph,
            } => write!(
                formatter,
                "glyph identity {} overlaps atlas texels owned by glyph {}",
                glyph.value(),
                conflicting_glyph.value()
            ),
            Self::MissingGlyph { glyph, index } => write!(
                formatter,
                "glyph identity {} at run index {index} is absent from the atlas",
                glyph.value()
            ),
            Self::InvalidGlyph => write!(formatter, "glyph placement or atlas region is invalid"),
            Self::AllocationFailed { requested_bytes } => write!(
                formatter,
                "could not reserve {requested_bytes} bytes for glyph metadata or scratch storage"
            ),
        }
    }
}

impl Error for GlyphError {}

impl From<ImageError> for GlyphError {
    fn from(error: ImageError) -> Self {
        Self::Image(error)
    }
}

/// Result of uploading one glyph image into an existing atlas.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GlyphUploadReport {
    uploaded_bytes: usize,
    inserted_entry: bool,
}

impl GlyphUploadReport {
    /// Returns the exact number of RGBA bytes submitted to the GPU queue.
    pub const fn uploaded_bytes(self) -> usize {
        self.uploaded_bytes
    }

    /// Returns whether a new glyph identity was registered.
    pub const fn inserted_entry(self) -> bool {
        self.inserted_entry
    }
}

/// Renderer-owned sRGB RGBA glyph atlas with exact pixels and sorted metadata.
///
/// Sim;Engine does not evict entries implicitly. Hosts can therefore treat a
/// successful upload as stable until they explicitly replace or restore the
/// atlas. Straight-alpha pixels follow the same color contract as [`Image2d`].
pub struct GlyphAtlas2d {
    pub(super) image: Image2d,
    entries: Vec<GlyphAtlasEntry>,
    budget: GlyphAtlasBudget,
}

impl GlyphAtlas2d {
    /// Returns atlas dimensions in physical source texels.
    pub const fn size(&self) -> (u32, u32) {
        self.image.size()
    }

    /// Returns registered entries sorted by glyph identity.
    pub fn entries(&self) -> &[GlyphAtlasEntry] {
        &self.entries
    }

    /// Returns one atlas rectangle, or `None` for an unregistered identity.
    pub fn source_for(&self, glyph: GlyphId) -> Option<ImageTexelRect> {
        find_entry(&self.entries, glyph).map(GlyphAtlasEntry::source)
    }

    /// Returns the retained atlas limits.
    pub const fn budget(&self) -> GlyphAtlasBudget {
        self.budget
    }

    /// Returns exact CPU bytes retained for image and glyph recovery.
    pub fn recovery_memory_bytes(&self) -> usize {
        self.image.recovery_memory_bytes().saturating_add(
            self.entries
                .capacity()
                .saturating_mul(std::mem::size_of::<GlyphAtlasEntry>()),
        )
    }

    /// Returns nominal single-level RGBA8 texel-storage bytes, excluding
    /// opaque backend alignment, tiling, allocation pages, and metadata.
    pub fn gpu_allocation_bytes(&self) -> usize {
        self.image.gpu_allocation_bytes()
    }
}

/// One host-shaped glyph quad in local logical-screen coordinates.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PositionedGlyph2d {
    glyph: GlyphId,
    destination: LogicalViewportRegion,
    tint: Color,
}

impl PositionedGlyph2d {
    /// Creates a finite logical-pixel quad with normalized straight-linear tint.
    pub fn new(
        glyph: GlyphId,
        destination: LogicalViewportRegion,
        tint: Color,
    ) -> Result<Self, GlyphError> {
        if !tint.is_normalized() || !logical_region_arithmetic_is_finite(destination) {
            return Err(GlyphError::InvalidGlyph);
        }
        Ok(Self {
            glyph,
            destination,
            tint,
        })
    }

    /// Returns the atlas-local glyph identity.
    pub const fn glyph(self) -> GlyphId {
        self.glyph
    }

    /// Returns the exact logical-pixel destination supplied by the host shaper.
    pub const fn destination(self) -> LogicalViewportRegion {
        self.destination
    }

    /// Returns normalized straight-linear tint and opacity.
    pub const fn tint(self) -> Color {
        self.tint
    }
}

/// Deterministic logical-pixel bounds of a submitted positioned glyph run.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GlyphRunBounds {
    region: Option<LogicalViewportRegion>,
}

impl GlyphRunBounds {
    /// Returns `None` for an empty run or the exact union of its glyph quads.
    pub const fn region(self) -> Option<LogicalViewportRegion> {
        self.region
    }

    /// Returns whether the submitted run contains no glyph quads.
    pub const fn is_empty(self) -> bool {
        self.region.is_none()
    }
}

/// Bounded retained glyph-run statistics independent of frame timing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GlyphRunStatistics {
    submitted_glyphs: usize,
    rendered_quads: usize,
    atlas_misses: usize,
    retained_bytes: usize,
}

impl GlyphRunStatistics {
    /// Returns positioned glyphs accepted from the host.
    pub const fn submitted_glyphs(self) -> usize {
        self.submitted_glyphs
    }

    /// Returns retained drawable quads. Successful runs have one per glyph.
    pub const fn rendered_quads(self) -> usize {
        self.rendered_quads
    }

    /// Returns atlas misses. Successful retained runs always report zero.
    pub const fn atlas_misses(self) -> usize {
        self.atlas_misses
    }

    /// Returns retained glyph, sprite and conversion-staging capacity bytes.
    pub const fn retained_bytes(self) -> usize {
        self.retained_bytes
    }
}

/// Renderer-owned positioned glyph run drawn as at most one instanced atlas batch.
///
/// A run references exactly one atlas. Mixed fallback fonts remain explicit:
/// build one run per atlas and submit them at the same frame order. The host is
/// responsible for shaping, baseline selection, fallback, and line breaking.
/// An empty run is valid and emits no draw call.
pub struct GlyphRun2d {
    pub(super) batch: ImageBatch2d,
    glyphs: Vec<PositionedGlyph2d>,
    bounds: GlyphRunBounds,
    budget: GlyphRunBudget,
}

/// Outcome of an atomic capacity-aware glyph-run update.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GlyphRunUploadReport {
    batch: ImageBatchUploadReport,
    capacity: usize,
    retained_bytes: usize,
    peak_retained_bytes: usize,
}

impl GlyphRunUploadReport {
    /// Returns the accepted positioned-glyph count.
    pub const fn glyph_count(self) -> usize {
        self.batch.sprite_count()
    }
    /// Returns glyph/layout capacity reusable without growing CPU arrays.
    pub const fn capacity(self) -> usize {
        self.capacity
    }
    /// Returns the instance upload and GPU capacity/growth outcome.
    pub const fn instances(self) -> ImageBatchUploadReport {
        self.batch
    }
    /// Returns all retained CPU glyph, sprite and conversion-staging bytes.
    pub const fn retained_bytes(self) -> usize {
        self.retained_bytes
    }
    /// Returns the conservative old/new CPU peak, excluding caller and backend storage.
    pub const fn peak_retained_bytes(self) -> usize {
        self.peak_retained_bytes
    }
}

impl GlyphRun2d {
    /// Returns the exact host-positioned glyph descriptions.
    pub fn glyphs(&self) -> &[PositionedGlyph2d] {
        &self.glyphs
    }

    /// Returns the retained glyph count.
    pub fn glyph_count(&self) -> usize {
        self.glyphs.len()
    }

    /// Returns CPU capacity reusable without growing glyph or sprite arrays.
    pub fn capacity(&self) -> usize {
        self.glyphs.capacity().min(self.batch.capacity())
    }

    /// Returns deterministic logical-pixel bounds without font interpretation.
    pub const fn bounds(&self) -> GlyphRunBounds {
        self.bounds
    }

    /// Returns creation and restoration limits.
    pub const fn budget(&self) -> GlyphRunBudget {
        self.budget
    }

    /// Returns retained work counters for this run.
    pub fn statistics(&self) -> GlyphRunStatistics {
        GlyphRunStatistics {
            submitted_glyphs: self.glyphs.len(),
            rendered_quads: self.glyphs.len(),
            atlas_misses: 0,
            retained_bytes: self.recovery_memory_bytes(),
        }
    }

    /// Returns CPU glyph, sprite and conversion-staging capacity bytes.
    pub fn recovery_memory_bytes(&self) -> usize {
        self.glyphs
            .capacity()
            .saturating_mul(std::mem::size_of::<PositionedGlyph2d>())
            .saturating_add(self.batch.recovery_memory_bytes())
    }

    /// Returns GPU instance-buffer bytes actively addressable by the run.
    pub fn gpu_allocation_bytes(&self) -> usize {
        self.batch.gpu_allocation_bytes()
    }
}

impl WgpuRenderer {
    /// Creates a bounded glyph atlas from exact RGBA pixels and initial entries.
    pub fn create_glyph_atlas(
        &self,
        width: u32,
        height: u32,
        pixels: Vec<u8>,
        entries: Vec<GlyphAtlasEntry>,
        budget: GlyphAtlasBudget,
    ) -> Result<GlyphAtlas2d, GlyphError> {
        create_glyph_atlas_resources(
            &self.device,
            &self.queue,
            Arc::clone(&self.renderer_identity),
            width,
            height,
            pixels,
            entries,
            budget,
        )
    }

    /// Uploads or replaces one glyph rectangle after complete metadata preflight.
    ///
    /// Reusing an identity requires the same rectangle. Changing atlas packing
    /// is an explicit new-atlas operation so existing retained runs cannot
    /// silently sample unrelated texels.
    pub fn upload_glyph(
        &self,
        atlas: &mut GlyphAtlas2d,
        entry: GlyphAtlasEntry,
        pixels: &[u8],
    ) -> Result<GlyphUploadReport, GlyphError> {
        self.validate_image(&atlas.image)?;
        if !entry.source.fits(atlas.image.width(), atlas.image.height()) {
            return Err(GlyphError::InvalidGlyph);
        }
        image::validate_image_region_upload(&atlas.image, entry.source, pixels.len())?;
        let insertion = validate_glyph_insertion(&atlas.entries, entry)?;
        if insertion.is_some() {
            validate_metadata_capacity(atlas.entries.len().saturating_add(1), atlas.budget)?;
        }
        let replacement_entries = if let Some(index) = insertion {
            let new_len = atlas.entries.len() + 1;
            let requested_bytes = new_len
                .checked_mul(std::mem::size_of::<GlyphAtlasEntry>())
                .ok_or(GlyphError::InvalidGlyph)?;
            let mut replacement = Vec::new();
            replacement
                .try_reserve_exact(new_len)
                .map_err(|_| GlyphError::AllocationFailed { requested_bytes })?;
            replacement.extend_from_slice(&atlas.entries[..index]);
            replacement.push(entry);
            replacement.extend_from_slice(&atlas.entries[index..]);
            let actual = replacement
                .capacity()
                .saturating_mul(std::mem::size_of::<GlyphAtlasEntry>());
            if actual > atlas.budget.max_metadata_bytes {
                return Err(GlyphError::MetadataBudgetExceeded {
                    limit: atlas.budget.max_metadata_bytes,
                    actual,
                });
            }
            Some(replacement)
        } else {
            None
        };
        let report = self.update_image_region(&mut atlas.image, entry.source, pixels)?;
        if let Some(replacement) = replacement_entries {
            atlas.entries = replacement;
        }
        Ok(GlyphUploadReport {
            uploaded_bytes: report.uploaded_bytes(),
            inserted_entry: insertion.is_some(),
        })
    }

    /// Creates one retained instanced run from host-positioned glyph quads.
    pub fn create_glyph_run(
        &self,
        atlas: &GlyphAtlas2d,
        glyphs: Vec<PositionedGlyph2d>,
        budget: GlyphRunBudget,
    ) -> Result<GlyphRun2d, GlyphError> {
        self.validate_image(&atlas.image)?;
        create_glyph_run_resources(
            &self.device,
            &self.queue,
            Arc::clone(&self.renderer_identity),
            atlas,
            glyphs,
            budget,
        )
    }

    /// Updates host-shaped layout while reusing CPU and GPU capacity when possible.
    ///
    /// Exact unchanged input uploads nothing; an empty slice clears the drawable
    /// run but retains capacity. The atlas must be the run's original active-device
    /// atlas. All glyph sources, bounds and budget checks complete before changing
    /// drawable state. Synchronous rejection preserves the previous revision;
    /// device failure cannot roll back work already submitted to the queue.
    pub fn update_glyph_run(
        &self,
        atlas: &GlyphAtlas2d,
        run: &mut GlyphRun2d,
        glyphs: &[PositionedGlyph2d],
    ) -> Result<GlyphRunUploadReport, GlyphError> {
        self.validate_glyph_run(atlas, run)?;
        update_glyph_run_resources(&self.device, &self.queue, atlas, run, glyphs)
    }

    /// Restores exact atlas pixels and metadata for this renderer generation.
    pub fn restore_glyph_atlas(&self, source: &GlyphAtlas2d) -> Result<GlyphAtlas2d, GlyphError> {
        image::validate_image_shape(
            source.image.width(),
            source.image.height(),
            source.image.pixels().len(),
            source.budget.image,
            self.device.limits().max_texture_dimension_2d,
        )?;
        let mut entries = Vec::new();
        entries
            .try_reserve_exact(source.entries.len())
            .map_err(|_| GlyphError::AllocationFailed {
                requested_bytes: std::mem::size_of_val(source.entries.as_slice()),
            })?;
        entries.extend_from_slice(&source.entries);
        validate_atlas_entries(
            &mut entries,
            source.image.width(),
            source.image.height(),
            source.budget,
        )?;
        entries = compact_glyph_vec(entries, source.budget.max_metadata_bytes)?;
        let actual = entries
            .capacity()
            .saturating_mul(std::mem::size_of::<GlyphAtlasEntry>());
        if actual > source.budget.max_metadata_bytes {
            return Err(GlyphError::MetadataBudgetExceeded {
                limit: source.budget.max_metadata_bytes,
                actual,
            });
        }
        let image = self.restore_image(&source.image)?;
        Ok(GlyphAtlas2d {
            image,
            entries,
            budget: source.budget,
        })
    }

    /// Restores a run against the restored copy of its original atlas.
    pub fn restore_glyph_run(
        &self,
        atlas: &GlyphAtlas2d,
        source: &GlyphRun2d,
    ) -> Result<GlyphRun2d, GlyphError> {
        self.validate_image(&atlas.image)?;
        if !Arc::ptr_eq(
            &atlas.image.recovery_identity,
            &source.batch.image_recovery_identity,
        ) {
            return Err(ImageError::RecoverySourceMismatch.into());
        }
        validate_glyph_run_sources(&atlas.entries, &source.glyphs, source.batch.sprites())?;
        validate_glyph_run_budget(source.glyphs.len(), source.budget)?;
        validate_glyph_membership(&atlas.entries, &source.glyphs)?;
        measure_glyphs(&source.glyphs)?;
        let batch_budget =
            ImageBatchBudget::new(source.budget.max_glyphs, source.budget.max_retained_bytes)?;
        image::preflight_image_batch_capacity(&self.device, source.glyphs.len(), batch_budget)?;
        let retained_bytes = source
            .glyphs
            .len()
            .checked_mul(std::mem::size_of::<PositionedGlyph2d>())
            .ok_or(GlyphError::InvalidGlyph)?;
        let mut glyphs = Vec::new();
        glyphs.try_reserve_exact(source.glyphs.len()).map_err(|_| {
            GlyphError::AllocationFailed {
                requested_bytes: retained_bytes,
            }
        })?;
        glyphs.extend_from_slice(&source.glyphs);
        self.create_glyph_run(atlas, glyphs, source.budget)
    }

    pub(super) fn validate_glyph_run(
        &self,
        atlas: &GlyphAtlas2d,
        run: &GlyphRun2d,
    ) -> Result<(), GlyphError> {
        self.validate_image_batch(&atlas.image, &run.batch)?;
        Ok(())
    }
}

#[allow(clippy::too_many_arguments)]
pub(super) fn create_glyph_atlas_resources(
    device: &wgpu::Device,
    queue: &wgpu::Queue,
    renderer_identity: Arc<()>,
    width: u32,
    height: u32,
    pixels: Vec<u8>,
    mut entries: Vec<GlyphAtlasEntry>,
    budget: GlyphAtlasBudget,
) -> Result<GlyphAtlas2d, GlyphError> {
    image::validate_image_shape(
        width,
        height,
        pixels.len(),
        budget.image,
        device.limits().max_texture_dimension_2d,
    )?;
    validate_atlas_entries(&mut entries, width, height, budget)?;
    entries = compact_glyph_vec(entries, budget.max_metadata_bytes)?;
    let actual_metadata_bytes = entries
        .capacity()
        .saturating_mul(std::mem::size_of::<GlyphAtlasEntry>());
    if actual_metadata_bytes > budget.max_metadata_bytes {
        return Err(GlyphError::MetadataBudgetExceeded {
            limit: budget.max_metadata_bytes,
            actual: actual_metadata_bytes,
        });
    }
    let image = image::create_image_resources(
        device,
        queue,
        renderer_identity,
        width,
        height,
        pixels,
        budget.image,
    )?;
    Ok(GlyphAtlas2d {
        image,
        entries,
        budget,
    })
}

pub(super) fn create_glyph_run_resources(
    device: &wgpu::Device,
    queue: &wgpu::Queue,
    renderer_identity: Arc<()>,
    atlas: &GlyphAtlas2d,
    glyphs: Vec<PositionedGlyph2d>,
    budget: GlyphRunBudget,
) -> Result<GlyphRun2d, GlyphError> {
    validate_glyph_run_budget(glyphs.len(), budget)?;
    validate_glyph_membership(&atlas.entries, &glyphs)?;
    let bounds = measure_glyphs(&glyphs)?;
    let batch_budget = ImageBatchBudget::new(budget.max_glyphs, budget.max_retained_bytes)?;
    image::preflight_image_batch_capacity(device, glyphs.len(), batch_budget)?;
    let glyph_byte_limit = budget
        .max_retained_bytes
        .saturating_sub(image::batch_retained_bytes(glyphs.len().max(1)));
    let glyphs = compact_glyph_vec(glyphs, glyph_byte_limit)?;
    let sprites = resolve_glyph_sprites(&atlas.entries, &glyphs)?;
    let actual_retained_bytes = glyphs
        .capacity()
        .saturating_mul(std::mem::size_of::<PositionedGlyph2d>())
        .saturating_add(
            sprites
                .capacity()
                .saturating_mul(std::mem::size_of::<ImageSprite2d>()),
        );
    if actual_retained_bytes > budget.max_retained_bytes {
        return Err(GlyphError::MetadataBudgetExceeded {
            limit: budget.max_retained_bytes,
            actual: actual_retained_bytes,
        });
    }
    let batch_budget = ImageBatchBudget::new(
        budget.max_glyphs,
        budget.max_retained_bytes.saturating_sub(
            glyphs
                .capacity()
                .saturating_mul(std::mem::size_of::<PositionedGlyph2d>()),
        ),
    )?;
    let batch = image::create_image_batch_resources(
        device,
        queue,
        renderer_identity,
        &atlas.image,
        sprites,
        batch_budget,
    )?;
    Ok(GlyphRun2d {
        batch,
        glyphs,
        bounds,
        budget,
    })
}

pub(super) fn update_glyph_run_resources(
    device: &wgpu::Device,
    queue: &wgpu::Queue,
    atlas: &GlyphAtlas2d,
    run: &mut GlyphRun2d,
    glyphs: &[PositionedGlyph2d],
) -> Result<GlyphRunUploadReport, GlyphError> {
    validate_glyph_run_budget(glyphs.len(), run.budget)?;
    image::preflight_image_batch_capacity(
        device,
        glyphs.len(),
        ImageBatchBudget::new(run.budget.max_glyphs, run.budget.max_retained_bytes)?,
    )?;
    validate_glyph_membership(&atlas.entries, glyphs)?;
    if glyphs.iter().any(|glyph| !glyph.tint.is_normalized()) {
        return Err(GlyphError::InvalidGlyph);
    }
    let bounds = measure_glyphs(glyphs)?;
    let old_glyph_bytes = run
        .glyphs
        .capacity()
        .saturating_mul(std::mem::size_of::<PositionedGlyph2d>());
    let mut replacement = if glyphs.len() > run.glyphs.capacity() {
        let mut values = Vec::new();
        values
            .try_reserve_exact(glyphs.len())
            .map_err(|_| GlyphError::AllocationFailed {
                requested_bytes: std::mem::size_of_val(glyphs),
            })?;
        Some(values)
    } else {
        None
    };
    let glyph_capacity = replacement
        .as_ref()
        .map_or(run.glyphs.capacity(), Vec::capacity);
    let glyph_bytes = glyph_capacity.saturating_mul(std::mem::size_of::<PositionedGlyph2d>());
    let batch_budget = ImageBatchBudget::new(
        run.budget.max_glyphs,
        run.budget.max_retained_bytes.saturating_sub(glyph_bytes),
    )?;
    // Membership, tint and destination validation above prove one sprite per
    // glyph. The common update also checks that exact count before publication.
    let sprites = glyphs.iter().filter_map(|glyph| {
        let source = find_entry(&atlas.entries, glyph.glyph)?.source;
        ImageSprite2d::new(source, glyph.destination, glyph.tint).ok()
    });
    let batch = image::update_image_batch_resources(
        device,
        queue,
        &atlas.image,
        &mut run.batch,
        sprites,
        glyphs.len(),
        batch_budget,
    )?;
    let peak_retained_bytes = batch
        .peak_retained_bytes()
        .saturating_add(old_glyph_bytes)
        .saturating_add(if replacement.is_some() {
            glyph_bytes
        } else {
            0
        });
    if let Some(values) = replacement.take() {
        run.glyphs = values;
    }
    run.glyphs.clear();
    run.glyphs.extend_from_slice(glyphs);
    run.bounds = bounds;
    Ok(GlyphRunUploadReport {
        batch,
        capacity: run.capacity(),
        retained_bytes: run.recovery_memory_bytes(),
        peak_retained_bytes,
    })
}

fn compact_glyph_vec<T: Copy>(values: Vec<T>, maximum_bytes: usize) -> Result<Vec<T>, GlyphError> {
    if values.capacity().saturating_mul(std::mem::size_of::<T>()) <= maximum_bytes {
        return Ok(values);
    }
    let requested_bytes = values
        .len()
        .checked_mul(std::mem::size_of::<T>())
        .ok_or(GlyphError::InvalidGlyph)?;
    let mut compact = Vec::new();
    compact
        .try_reserve_exact(values.len())
        .map_err(|_| GlyphError::AllocationFailed { requested_bytes })?;
    compact.extend_from_slice(&values);
    Ok(compact)
}

fn validate_atlas_entries(
    entries: &mut [GlyphAtlasEntry],
    width: u32,
    height: u32,
    budget: GlyphAtlasBudget,
) -> Result<(), GlyphError> {
    validate_metadata_capacity(entries.len(), budget)?;
    entries.sort_unstable_by_key(|entry| entry.glyph);
    for (index, entry) in entries.iter().enumerate() {
        if !entry.source.fits(width, height) {
            return Err(GlyphError::InvalidGlyph);
        }
        if index > 0 && entries[index - 1].glyph == entry.glyph {
            return Err(GlyphError::DuplicateGlyph { glyph: entry.glyph });
        }
    }
    validate_non_overlapping_glyph_entries(entries)
}

#[derive(Clone, Copy)]
struct GlyphSweepEvent {
    x: u32,
    starts: bool,
    entry_index: usize,
    minimum_y: u32,
    maximum_y: u32,
}

fn validate_non_overlapping_glyph_entries(entries: &[GlyphAtlasEntry]) -> Result<(), GlyphError> {
    if entries.len() < 2 {
        return Ok(());
    }
    let endpoint_count = entries
        .len()
        .checked_mul(2)
        .ok_or(GlyphError::InvalidGlyph)?;
    let mut events = Vec::new();
    events
        .try_reserve_exact(endpoint_count)
        .map_err(|_| GlyphError::AllocationFailed {
            requested_bytes: endpoint_count.saturating_mul(std::mem::size_of::<GlyphSweepEvent>()),
        })?;
    let mut vertical_coordinates = Vec::new();
    vertical_coordinates
        .try_reserve_exact(endpoint_count)
        .map_err(|_| GlyphError::AllocationFailed {
            requested_bytes: endpoint_count.saturating_mul(std::mem::size_of::<u32>()),
        })?;
    for (entry_index, entry) in entries.iter().copied().enumerate() {
        let source = entry.source;
        let maximum_x = source
            .x()
            .checked_add(source.width())
            .ok_or(GlyphError::InvalidGlyph)?;
        let maximum_y = source
            .y()
            .checked_add(source.height())
            .ok_or(GlyphError::InvalidGlyph)?;
        events.push(GlyphSweepEvent {
            x: source.x(),
            starts: true,
            entry_index,
            minimum_y: source.y(),
            maximum_y,
        });
        events.push(GlyphSweepEvent {
            x: maximum_x,
            starts: false,
            entry_index,
            minimum_y: source.y(),
            maximum_y,
        });
        vertical_coordinates.push(source.y());
        vertical_coordinates.push(maximum_y);
    }
    // End events precede start events at the same x so edge-touching atlas
    // rectangles remain valid.
    events.sort_unstable_by_key(|event| (event.x, event.starts));
    vertical_coordinates.sort_unstable();
    vertical_coordinates.dedup();
    let interval_count = vertical_coordinates.len().saturating_sub(1);
    let tree_len = interval_count
        .checked_mul(4)
        .and_then(|value| value.checked_add(4))
        .ok_or(GlyphError::InvalidGlyph)?;
    let mut maximum_coverage = Vec::new();
    maximum_coverage
        .try_reserve_exact(tree_len)
        .map_err(|_| GlyphError::AllocationFailed {
            requested_bytes: tree_len.saturating_mul(std::mem::size_of::<i32>()),
        })?;
    maximum_coverage.resize(tree_len, 0_i32);
    let mut lazy_coverage = Vec::new();
    lazy_coverage
        .try_reserve_exact(tree_len)
        .map_err(|_| GlyphError::AllocationFailed {
            requested_bytes: tree_len.saturating_mul(std::mem::size_of::<i32>()),
        })?;
    lazy_coverage.resize(tree_len, 0_i32);
    let mut active = Vec::new();
    active
        .try_reserve_exact(entries.len())
        .map_err(|_| GlyphError::AllocationFailed {
            requested_bytes: entries.len().saturating_mul(std::mem::size_of::<bool>()),
        })?;
    active.resize(entries.len(), false);

    for event in events {
        let minimum = vertical_coordinates
            .binary_search(&event.minimum_y)
            .expect("glyph sweep endpoints came from the coordinate set");
        let maximum = vertical_coordinates
            .binary_search(&event.maximum_y)
            .expect("glyph sweep endpoints came from the coordinate set");
        if event.starts {
            if glyph_coverage_max(
                &maximum_coverage,
                &lazy_coverage,
                1,
                0,
                interval_count,
                minimum,
                maximum,
            ) > 0
            {
                let entry = entries[event.entry_index];
                let conflict = active
                    .iter()
                    .enumerate()
                    .find(|(index, active)| {
                        **active && glyph_rectangles_overlap(entries[*index].source, entry.source)
                    })
                    .map(|(index, _)| entries[index].glyph)
                    .expect("positive sweep coverage has an active owning glyph");
                return Err(GlyphError::OverlappingGlyph {
                    glyph: entry.glyph,
                    conflicting_glyph: conflict,
                });
            }
            glyph_coverage_add(
                &mut maximum_coverage,
                &mut lazy_coverage,
                1,
                0,
                interval_count,
                minimum,
                maximum,
                1,
            );
            active[event.entry_index] = true;
        } else {
            glyph_coverage_add(
                &mut maximum_coverage,
                &mut lazy_coverage,
                1,
                0,
                interval_count,
                minimum,
                maximum,
                -1,
            );
            active[event.entry_index] = false;
        }
    }
    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn glyph_coverage_add(
    maximum_coverage: &mut [i32],
    lazy_coverage: &mut [i32],
    node: usize,
    left: usize,
    right: usize,
    query_left: usize,
    query_right: usize,
    amount: i32,
) {
    if query_left <= left && right <= query_right {
        maximum_coverage[node] += amount;
        lazy_coverage[node] += amount;
        return;
    }
    let middle = left + (right - left) / 2;
    if query_left < middle {
        glyph_coverage_add(
            maximum_coverage,
            lazy_coverage,
            node * 2,
            left,
            middle,
            query_left,
            query_right,
            amount,
        );
    }
    if query_right > middle {
        glyph_coverage_add(
            maximum_coverage,
            lazy_coverage,
            node * 2 + 1,
            middle,
            right,
            query_left,
            query_right,
            amount,
        );
    }
    maximum_coverage[node] =
        lazy_coverage[node] + maximum_coverage[node * 2].max(maximum_coverage[node * 2 + 1]);
}

#[allow(clippy::too_many_arguments)]
fn glyph_coverage_max(
    maximum_coverage: &[i32],
    lazy_coverage: &[i32],
    node: usize,
    left: usize,
    right: usize,
    query_left: usize,
    query_right: usize,
) -> i32 {
    if query_left <= left && right <= query_right {
        return maximum_coverage[node];
    }
    let middle = left + (right - left) / 2;
    let mut maximum = 0;
    if query_left < middle {
        maximum = maximum.max(glyph_coverage_max(
            maximum_coverage,
            lazy_coverage,
            node * 2,
            left,
            middle,
            query_left,
            query_right,
        ));
    }
    if query_right > middle {
        maximum = maximum.max(glyph_coverage_max(
            maximum_coverage,
            lazy_coverage,
            node * 2 + 1,
            middle,
            right,
            query_left,
            query_right,
        ));
    }
    lazy_coverage[node] + maximum
}

fn validate_glyph_insertion(
    entries: &[GlyphAtlasEntry],
    entry: GlyphAtlasEntry,
) -> Result<Option<usize>, GlyphError> {
    match entries.binary_search_by_key(&entry.glyph, |candidate| candidate.glyph) {
        Ok(index) => {
            if entries[index].source != entry.source {
                return Err(GlyphError::DuplicateGlyph { glyph: entry.glyph });
            }
            Ok(None)
        }
        Err(index) => {
            if let Some(conflict) = entries
                .iter()
                .find(|candidate| glyph_rectangles_overlap(candidate.source, entry.source))
            {
                return Err(GlyphError::OverlappingGlyph {
                    glyph: entry.glyph,
                    conflicting_glyph: conflict.glyph,
                });
            }
            Ok(Some(index))
        }
    }
}

fn glyph_rectangles_overlap(left: ImageTexelRect, right: ImageTexelRect) -> bool {
    let left_max_x = u64::from(left.x()) + u64::from(left.width());
    let left_max_y = u64::from(left.y()) + u64::from(left.height());
    let right_max_x = u64::from(right.x()) + u64::from(right.width());
    let right_max_y = u64::from(right.y()) + u64::from(right.height());
    u64::from(left.x()) < right_max_x
        && u64::from(right.x()) < left_max_x
        && u64::from(left.y()) < right_max_y
        && u64::from(right.y()) < left_max_y
}

fn validate_metadata_capacity(
    entry_count: usize,
    budget: GlyphAtlasBudget,
) -> Result<(), GlyphError> {
    if entry_count > budget.max_entries {
        return Err(GlyphError::EntryBudgetExceeded {
            limit: budget.max_entries,
            actual: entry_count,
        });
    }
    let bytes = entry_count
        .checked_mul(std::mem::size_of::<GlyphAtlasEntry>())
        .ok_or(GlyphError::InvalidGlyph)?;
    if bytes > budget.max_metadata_bytes {
        return Err(GlyphError::MetadataBudgetExceeded {
            limit: budget.max_metadata_bytes,
            actual: bytes,
        });
    }
    Ok(())
}

pub(super) fn validate_glyph_run_budget(
    glyph_count: usize,
    budget: GlyphRunBudget,
) -> Result<(), GlyphError> {
    if glyph_count > budget.max_glyphs {
        return Err(GlyphError::EntryBudgetExceeded {
            limit: budget.max_glyphs,
            actual: glyph_count,
        });
    }
    let retained_bytes = glyph_retained_bytes(glyph_count).ok_or(GlyphError::InvalidGlyph)?;
    if retained_bytes > budget.max_retained_bytes {
        return Err(GlyphError::MetadataBudgetExceeded {
            limit: budget.max_retained_bytes,
            actual: retained_bytes,
        });
    }
    Ok(())
}

fn glyph_retained_bytes(glyph_count: usize) -> Option<usize> {
    glyph_count.checked_mul(GlyphRunBudget::RETAINED_BYTES_PER_GLYPH)
}

fn find_entry(entries: &[GlyphAtlasEntry], glyph: GlyphId) -> Option<GlyphAtlasEntry> {
    entries
        .binary_search_by_key(&glyph, |entry| entry.glyph)
        .ok()
        .map(|index| entries[index])
}

fn resolve_glyph_sprites(
    entries: &[GlyphAtlasEntry],
    glyphs: &[PositionedGlyph2d],
) -> Result<Vec<ImageSprite2d>, GlyphError> {
    validate_glyph_membership(entries, glyphs)?;
    let mut sprites = Vec::new();
    sprites
        .try_reserve_exact(glyphs.len())
        .map_err(|_| GlyphError::AllocationFailed {
            requested_bytes: glyphs
                .len()
                .saturating_mul(std::mem::size_of::<ImageSprite2d>()),
        })?;
    for (index, glyph) in glyphs.iter().copied().enumerate() {
        let source = find_entry(entries, glyph.glyph)
            .map(GlyphAtlasEntry::source)
            // The complete lookup scan above makes this branch defensive.
            .ok_or(GlyphError::MissingGlyph {
                glyph: glyph.glyph,
                index,
            })?;
        sprites.push(ImageSprite2d::new(source, glyph.destination, glyph.tint)?);
    }
    Ok(sprites)
}

fn validate_glyph_membership(
    entries: &[GlyphAtlasEntry],
    glyphs: &[PositionedGlyph2d],
) -> Result<(), GlyphError> {
    if let Some((index, glyph)) = glyphs
        .iter()
        .enumerate()
        .find(|(_, glyph)| find_entry(entries, glyph.glyph).is_none())
    {
        return Err(GlyphError::MissingGlyph {
            glyph: glyph.glyph,
            index,
        });
    }
    Ok(())
}

fn validate_glyph_run_sources(
    entries: &[GlyphAtlasEntry],
    glyphs: &[PositionedGlyph2d],
    sprites: &[ImageSprite2d],
) -> Result<(), GlyphError> {
    if glyphs.len() != sprites.len()
        || glyphs.iter().zip(sprites).any(|(glyph, sprite)| {
            find_entry(entries, glyph.glyph).map(GlyphAtlasEntry::source) != Some(sprite.source())
        })
    {
        return Err(ImageError::RecoverySourceMismatch.into());
    }
    Ok(())
}

fn measure_glyphs(glyphs: &[PositionedGlyph2d]) -> Result<GlyphRunBounds, GlyphError> {
    if glyphs
        .iter()
        .any(|glyph| !image::logical_image_region_is_portable(glyph.destination))
    {
        return Err(GlyphError::InvalidGlyph);
    }
    let Some(first) = glyphs.first().copied() else {
        return Ok(GlyphRunBounds { region: None });
    };
    let first_origin = first.destination.origin().to_vec2();
    let first_size = first.destination.viewport().size();
    let mut minimum = first_origin;
    let mut maximum = first_origin + first_size;
    for glyph in &glyphs[1..] {
        if !logical_region_arithmetic_is_finite(glyph.destination) {
            return Err(GlyphError::InvalidGlyph);
        }
        let origin = glyph.destination.origin().to_vec2();
        let end = origin + glyph.destination.viewport().size();
        minimum.x = minimum.x.min(origin.x);
        minimum.y = minimum.y.min(origin.y);
        maximum.x = maximum.x.max(end.x);
        maximum.y = maximum.y.max(end.y);
    }
    if !minimum.is_finite() || !maximum.is_finite() {
        return Err(GlyphError::InvalidGlyph);
    }
    let viewport = LogicalViewport::new(maximum.x - minimum.x, maximum.y - minimum.y)
        .map_err(|_| GlyphError::InvalidGlyph)?;
    let region = LogicalViewportRegion::new(LogicalScreenPosition::from_vec2(minimum), viewport)
        .map_err(|_| GlyphError::InvalidGlyph)?;
    Ok(GlyphRunBounds {
        region: Some(region),
    })
}

fn logical_region_arithmetic_is_finite(region: LogicalViewportRegion) -> bool {
    let origin = region.origin().to_vec2();
    let end = origin + region.viewport().size();
    origin.is_finite() && end.is_finite()
}

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

    fn rectangle(x: u32) -> ImageTexelRect {
        ImageTexelRect::new(x, 0, 1, 1).unwrap()
    }

    fn positioned(glyph: u32, x: f32) -> PositionedGlyph2d {
        PositionedGlyph2d::new(
            GlyphId::new(glyph),
            LogicalViewportRegion::new(
                LogicalScreenPosition::new(x, 2.0),
                LogicalViewport::new(4.0, 6.0).unwrap(),
            )
            .unwrap(),
            Color::WHITE,
        )
        .unwrap()
    }

    #[test]
    fn atlas_entries_sort_and_reject_duplicate_or_out_of_bounds_glyphs() {
        let budget = GlyphAtlasBudget::default();
        let mut entries = vec![
            GlyphAtlasEntry::new(GlyphId::new(2), rectangle(1)),
            GlyphAtlasEntry::new(GlyphId::new(1), rectangle(0)),
        ];
        assert_eq!(validate_atlas_entries(&mut entries, 2, 1, budget), Ok(()));
        assert_eq!(entries[0].glyph(), GlyphId::new(1));

        let mut duplicate = vec![
            GlyphAtlasEntry::new(GlyphId::new(7), rectangle(0)),
            GlyphAtlasEntry::new(GlyphId::new(7), rectangle(1)),
        ];
        assert_eq!(
            validate_atlas_entries(&mut duplicate, 2, 1, budget),
            Err(GlyphError::DuplicateGlyph {
                glyph: GlyphId::new(7)
            })
        );

        let mut outside = vec![GlyphAtlasEntry::new(GlyphId::new(9), rectangle(2))];
        assert_eq!(
            validate_atlas_entries(&mut outside, 2, 1, budget),
            Err(GlyphError::InvalidGlyph)
        );

        let mut overlapping = vec![
            GlyphAtlasEntry::new(GlyphId::new(10), ImageTexelRect::new(0, 0, 2, 2).unwrap()),
            GlyphAtlasEntry::new(GlyphId::new(11), ImageTexelRect::new(1, 1, 2, 2).unwrap()),
        ];
        assert_eq!(
            validate_atlas_entries(&mut overlapping, 4, 4, budget),
            Err(GlyphError::OverlappingGlyph {
                glyph: GlyphId::new(11),
                conflicting_glyph: GlyphId::new(10),
            })
        );
    }

    #[test]
    fn incremental_glyph_overlap_is_rejected_before_metadata_or_pixel_update() {
        let entries = [GlyphAtlasEntry::new(
            GlyphId::new(1),
            ImageTexelRect::new(0, 0, 4, 4).unwrap(),
        )];
        let overlapping =
            GlyphAtlasEntry::new(GlyphId::new(2), ImageTexelRect::new(3, 0, 4, 4).unwrap());
        assert_eq!(
            validate_glyph_insertion(&entries, overlapping),
            Err(GlyphError::OverlappingGlyph {
                glyph: GlyphId::new(2),
                conflicting_glyph: GlyphId::new(1),
            })
        );
        assert_eq!(entries.len(), 1);

        let adjacent =
            GlyphAtlasEntry::new(GlyphId::new(2), ImageTexelRect::new(4, 0, 4, 4).unwrap());
        assert_eq!(validate_glyph_insertion(&entries, adjacent), Ok(Some(1)));

        let below = GlyphAtlasEntry::new(GlyphId::new(2), ImageTexelRect::new(0, 8, 4, 4).unwrap());
        assert_eq!(validate_glyph_insertion(&entries, below), Ok(Some(1)));

        let entries_below = [GlyphAtlasEntry::new(
            GlyphId::new(1),
            ImageTexelRect::new(0, 8, 4, 4).unwrap(),
        )];
        let above = GlyphAtlasEntry::new(GlyphId::new(2), ImageTexelRect::new(0, 0, 4, 4).unwrap());
        assert_eq!(validate_glyph_insertion(&entries_below, above), Ok(Some(1)));
    }

    #[test]
    fn default_maximum_non_overlapping_atlas_validation_is_subquadratic() {
        let mut entries = Vec::with_capacity(65_536);
        for glyph in 0..65_536_u32 {
            entries.push(GlyphAtlasEntry::new(
                GlyphId::new(glyph),
                ImageTexelRect::new(glyph % 256, glyph / 256, 1, 1).unwrap(),
            ));
        }
        assert_eq!(
            validate_atlas_entries(&mut entries, 256, 256, GlyphAtlasBudget::default()),
            Ok(())
        );
    }

    #[test]
    fn positioned_run_resolves_opaque_scientific_glyphs_and_measures_bounds() {
        let entries = [
            GlyphAtlasEntry::new(GlyphId::new('Δ' as u32), rectangle(1)),
            GlyphAtlasEntry::new(GlyphId::new('μ' as u32), rectangle(0)),
            GlyphAtlasEntry::new(GlyphId::new('∫' as u32), rectangle(2)),
        ];
        let glyphs = [
            positioned('μ' as u32, 1.0),
            positioned('Δ' as u32, 5.0),
            positioned('∫' as u32, 9.0),
        ];
        assert_eq!(resolve_glyph_sprites(&entries, &glyphs).unwrap().len(), 3);
        let bounds = measure_glyphs(&glyphs).unwrap().region().unwrap();
        assert_eq!(bounds.origin(), LogicalScreenPosition::new(1.0, 2.0));
        assert_eq!(bounds.viewport().size(), Vec2::new(12.0, 6.0));

        let missing = [positioned('Σ' as u32, 1.0)];
        assert_eq!(
            resolve_glyph_sprites(&entries, &missing),
            Err(GlyphError::MissingGlyph {
                glyph: GlyphId::new('Σ' as u32),
                index: 0,
            })
        );
    }

    #[test]
    fn glyph_run_recovery_rejects_forked_atlas_mapping() {
        let glyph = positioned(42, 1.0);
        let left = rectangle(0);
        let right = rectangle(1);
        let source_entries = [GlyphAtlasEntry::new(GlyphId::new(42), left)];
        let forked_entries = [GlyphAtlasEntry::new(GlyphId::new(42), right)];
        let source_sprites = [ImageSprite2d::new(left, glyph.destination(), glyph.tint()).unwrap()];

        assert_eq!(
            validate_glyph_run_sources(&source_entries, &[glyph], &source_sprites),
            Ok(())
        );
        assert_eq!(
            validate_glyph_run_sources(&forked_entries, &[glyph], &source_sprites),
            Err(GlyphError::Image(ImageError::RecoverySourceMismatch))
        );
    }
}