operad 6.1.0

A cross-platform GUI library for Rust.
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
//! Backend-neutral font lifecycle and cache accounting.
//!
//! This module tracks font faces without owning backend font objects. Backends
//! can use it to resolve fallback stacks, reject stale load completions, report
//! missing or failed faces, account for loaded bytes, and plan LRU evictions.

use std::collections::HashMap;

use crate::{FontStretch, FontStyle, FontWeight};

#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct FontFamilyId(String);

impl FontFamilyId {
    pub fn new(id: impl Into<String>) -> Self {
        Self(id.into())
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl From<&str> for FontFamilyId {
    fn from(value: &str) -> Self {
        Self::new(value)
    }
}

impl From<String> for FontFamilyId {
    fn from(value: String) -> Self {
        Self::new(value)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct FontFaceId {
    pub family: FontFamilyId,
    pub weight: FontWeight,
    pub style: FontStyle,
    pub stretch: FontStretch,
}

impl FontFaceId {
    pub fn new(
        family: impl Into<FontFamilyId>,
        weight: FontWeight,
        style: FontStyle,
        stretch: FontStretch,
    ) -> Self {
        Self {
            family: family.into(),
            weight,
            style,
            stretch,
        }
    }

    pub fn regular(family: impl Into<FontFamilyId>) -> Self {
        Self::new(
            family,
            FontWeight::NORMAL,
            FontStyle::Normal,
            FontStretch::Normal,
        )
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FontFallbackStack {
    pub families: Vec<FontFamilyId>,
}

impl FontFallbackStack {
    pub fn new(families: impl IntoIterator<Item = impl Into<FontFamilyId>>) -> Self {
        Self {
            families: families.into_iter().map(Into::into).collect(),
        }
    }

    pub fn is_empty(&self) -> bool {
        self.families.is_empty()
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum FontSourceDescriptor {
    BuiltIn { key: String },
    System { family_name: String },
    File { path: String },
    Memory { key: String },
}

impl FontSourceDescriptor {
    pub fn built_in(key: impl Into<String>) -> Self {
        Self::BuiltIn { key: key.into() }
    }

    pub fn system(family_name: impl Into<String>) -> Self {
        Self::System {
            family_name: family_name.into(),
        }
    }

    pub fn file(path: impl Into<String>) -> Self {
        Self::File { path: path.into() }
    }

    pub fn memory(key: impl Into<String>) -> Self {
        Self::Memory { key: key.into() }
    }
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct FontGeneration(pub u64);

impl FontGeneration {
    pub const ZERO: Self = Self(0);

    pub const fn next(self) -> Self {
        Self(self.0.wrapping_add(1))
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FontFaceDescriptor {
    pub id: FontFaceId,
    pub source: FontSourceDescriptor,
    pub generation: FontGeneration,
}

impl FontFaceDescriptor {
    pub fn new(id: FontFaceId, source: FontSourceDescriptor, generation: FontGeneration) -> Self {
        Self {
            id,
            source,
            generation,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FontLoadStatus {
    Pending,
    Loaded,
    Missing,
    Failed { reason: String },
}

impl FontLoadStatus {
    pub const fn is_loaded(&self) -> bool {
        matches!(self, Self::Loaded)
    }

    pub const fn is_terminal_failure(&self) -> bool {
        matches!(self, Self::Missing | Self::Failed { .. })
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FontLifecycleOutcome {
    Registered,
    Loaded,
    Missing,
    Failed,
    Retained,
    Stale,
    Evicted,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FontLifecycleIssue {
    MissingFace,
    InvalidLoadedByteLength,
    StaleGeneration {
        current_generation: FontGeneration,
        update_generation: FontGeneration,
    },
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CachedFontFace {
    pub descriptor: FontFaceDescriptor,
    pub status: FontLoadStatus,
    pub bytes: usize,
    pub created_frame: u64,
    pub last_used_frame: u64,
    pub last_update_frame: u64,
}

impl CachedFontFace {
    fn new(descriptor: FontFaceDescriptor, frame: u64) -> Self {
        Self {
            descriptor,
            status: FontLoadStatus::Pending,
            bytes: 0,
            created_frame: frame,
            last_used_frame: frame,
            last_update_frame: frame,
        }
    }

    fn apply_status(&mut self, status: FontLoadStatus, bytes: usize, frame: u64) -> usize {
        let previous = self.bytes;
        self.status = status;
        self.bytes = bytes;
        self.last_used_frame = frame;
        self.last_update_frame = frame;
        previous
    }

    fn retain(&mut self, frame: u64) {
        self.last_used_frame = frame;
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FontLifecycleReport {
    pub face_id: FontFaceId,
    pub outcome: FontLifecycleOutcome,
    pub issue: Option<FontLifecycleIssue>,
    pub generation: FontGeneration,
    pub current_generation: Option<FontGeneration>,
    pub frame: u64,
    pub bytes_before: usize,
    pub bytes_after: usize,
    pub total_bytes_before: usize,
    pub total_bytes_after: usize,
    pub created_frame: Option<u64>,
    pub last_used_frame: Option<u64>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FontFallbackAttempt {
    pub face_id: FontFaceId,
    pub status: Option<FontLoadStatus>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FontFallbackResolution {
    pub resolved: Option<FontFaceId>,
    pub attempts: Vec<FontFallbackAttempt>,
    pub frame: u64,
}

impl FontFallbackResolution {
    pub fn missing_faces(&self) -> Vec<&FontFaceId> {
        self.attempts
            .iter()
            .filter(|attempt| matches!(attempt.status, Some(FontLoadStatus::Missing) | None))
            .map(|attempt| &attempt.face_id)
            .collect()
    }

    pub fn failed_faces(&self) -> Vec<&FontFaceId> {
        self.attempts
            .iter()
            .filter(|attempt| matches!(attempt.status, Some(FontLoadStatus::Failed { .. })))
            .map(|attempt| &attempt.face_id)
            .collect()
    }
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct FontCachePolicy {
    pub max_bytes: Option<usize>,
}

impl FontCachePolicy {
    pub const UNBOUNDED: Self = Self { max_bytes: None };

    pub const fn max_bytes(max_bytes: usize) -> Self {
        Self {
            max_bytes: Some(max_bytes),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FontEvictionCandidate {
    pub face_id: FontFaceId,
    pub bytes: usize,
    pub created_frame: u64,
    pub last_used_frame: u64,
    pub age_frames: u64,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FontEvictionPlan {
    pub policy: FontCachePolicy,
    pub frame: u64,
    pub total_bytes_before: usize,
    pub bytes_to_free: usize,
    pub total_bytes_after: usize,
    pub candidates: Vec<FontEvictionCandidate>,
}

impl FontEvictionPlan {
    pub fn is_empty(&self) -> bool {
        self.candidates.is_empty()
    }

    pub fn evicted_count(&self) -> usize {
        self.candidates.len()
    }
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct FontRegistry {
    faces: HashMap<FontFaceId, CachedFontFace>,
    generation: FontGeneration,
    frame: u64,
    total_bytes: usize,
}

impl FontRegistry {
    pub fn new() -> Self {
        Self::default()
    }

    pub const fn generation(&self) -> FontGeneration {
        self.generation
    }

    pub fn advance_generation(&mut self) -> FontGeneration {
        self.generation = self.generation.next();
        self.generation
    }

    pub const fn frame(&self) -> u64 {
        self.frame
    }

    pub fn advance_frame(&mut self) -> u64 {
        self.frame = self.frame.wrapping_add(1);
        self.frame
    }

    pub const fn total_bytes(&self) -> usize {
        self.total_bytes
    }

    pub fn len(&self) -> usize {
        self.faces.len()
    }

    pub fn is_empty(&self) -> bool {
        self.faces.is_empty()
    }

    pub fn face(&self, face_id: &FontFaceId) -> Option<&CachedFontFace> {
        self.faces.get(face_id)
    }

    pub fn register_face(&mut self, descriptor: FontFaceDescriptor) -> FontLifecycleReport {
        let before = self.total_bytes;
        self.generation = self.generation.max(descriptor.generation);
        let face_id = descriptor.id.clone();

        if let Some(face) = self.faces.get_mut(&face_id) {
            if descriptor.generation < face.descriptor.generation {
                return stale_report(
                    face_id,
                    descriptor.generation,
                    face.descriptor.generation,
                    self.frame,
                    before,
                    self.total_bytes,
                );
            }
            face.descriptor = descriptor;
            face.status = FontLoadStatus::Pending;
            let previous = face.bytes;
            face.bytes = 0;
            face.last_update_frame = self.frame;
            face.last_used_frame = self.frame;
            self.total_bytes = self.total_bytes.saturating_sub(previous);
            return report_from_face(
                face,
                FontLifecycleOutcome::Registered,
                None,
                self.frame,
                before,
                self.total_bytes,
            );
        }

        let face = CachedFontFace::new(descriptor, self.frame);
        let report = report_from_face(
            &face,
            FontLifecycleOutcome::Registered,
            None,
            self.frame,
            before,
            self.total_bytes,
        );
        self.faces.insert(face_id, face);
        report
    }

    pub fn mark_loaded(
        &mut self,
        face_id: &FontFaceId,
        generation: FontGeneration,
        bytes: usize,
    ) -> FontLifecycleReport {
        if bytes == 0 {
            return self.apply_status(
                face_id,
                generation,
                FontLoadStatus::Failed {
                    reason: "loaded font face reported zero bytes".to_string(),
                },
                0,
                Some(FontLifecycleIssue::InvalidLoadedByteLength),
            );
        }
        self.apply_status(face_id, generation, FontLoadStatus::Loaded, bytes, None)
    }

    pub fn mark_missing(
        &mut self,
        face_id: &FontFaceId,
        generation: FontGeneration,
    ) -> FontLifecycleReport {
        self.apply_status(face_id, generation, FontLoadStatus::Missing, 0, None)
    }

    pub fn mark_failed(
        &mut self,
        face_id: &FontFaceId,
        generation: FontGeneration,
        reason: impl Into<String>,
    ) -> FontLifecycleReport {
        self.apply_status(
            face_id,
            generation,
            FontLoadStatus::Failed {
                reason: reason.into(),
            },
            0,
            None,
        )
    }

    pub fn retain(&mut self, face_id: &FontFaceId) -> FontLifecycleReport {
        let before = self.total_bytes;
        match self.faces.get_mut(face_id) {
            Some(face) => {
                face.retain(self.frame);
                report_from_face(
                    face,
                    FontLifecycleOutcome::Retained,
                    None,
                    self.frame,
                    before,
                    self.total_bytes,
                )
            }
            None => FontLifecycleReport {
                face_id: face_id.clone(),
                outcome: FontLifecycleOutcome::Failed,
                issue: Some(FontLifecycleIssue::MissingFace),
                generation: self.generation,
                current_generation: None,
                frame: self.frame,
                bytes_before: 0,
                bytes_after: 0,
                total_bytes_before: before,
                total_bytes_after: self.total_bytes,
                created_frame: None,
                last_used_frame: None,
            },
        }
    }

    pub fn resolve_fallback_stack(
        &mut self,
        stack: &FontFallbackStack,
        weight: FontWeight,
        style: FontStyle,
        stretch: FontStretch,
    ) -> FontFallbackResolution {
        let mut attempts = Vec::new();
        let mut resolved = None;

        for family in &stack.families {
            let face_id = FontFaceId::new(family.clone(), weight, style, stretch);
            let status = self.faces.get(&face_id).map(|face| face.status.clone());
            attempts.push(FontFallbackAttempt {
                face_id: face_id.clone(),
                status: status.clone(),
            });
            if matches!(status, Some(FontLoadStatus::Loaded)) {
                self.retain(&face_id);
                resolved = Some(face_id);
                break;
            }
        }

        FontFallbackResolution {
            resolved,
            attempts,
            frame: self.frame,
        }
    }

    pub fn missing_faces(&self) -> Vec<&CachedFontFace> {
        let mut faces = self
            .faces
            .values()
            .filter(|face| matches!(face.status, FontLoadStatus::Missing))
            .collect::<Vec<_>>();
        faces.sort_by(|left, right| {
            face_id_order(&left.descriptor.id).cmp(&face_id_order(&right.descriptor.id))
        });
        faces
    }

    pub fn failed_faces(&self) -> Vec<&CachedFontFace> {
        let mut faces = self
            .faces
            .values()
            .filter(|face| matches!(face.status, FontLoadStatus::Failed { .. }))
            .collect::<Vec<_>>();
        faces.sort_by(|left, right| {
            face_id_order(&left.descriptor.id).cmp(&face_id_order(&right.descriptor.id))
        });
        faces
    }

    pub fn plan_eviction_by_byte_budget(&self, max_bytes: usize) -> FontEvictionPlan {
        self.plan_eviction(FontCachePolicy::max_bytes(max_bytes))
    }

    pub fn plan_eviction(&self, policy: FontCachePolicy) -> FontEvictionPlan {
        let mut candidates = Vec::new();
        if let Some(max_bytes) = policy.max_bytes {
            let mut projected_bytes = self.total_bytes;
            if projected_bytes > max_bytes {
                for face in sorted_loaded_faces(&self.faces) {
                    projected_bytes = projected_bytes.saturating_sub(face.bytes);
                    candidates.push(FontEvictionCandidate {
                        face_id: face.descriptor.id.clone(),
                        bytes: face.bytes,
                        created_frame: face.created_frame,
                        last_used_frame: face.last_used_frame,
                        age_frames: self.frame.saturating_sub(face.last_used_frame),
                    });
                    if projected_bytes <= max_bytes {
                        break;
                    }
                }
            }
        }

        let bytes_to_free = candidates
            .iter()
            .map(|candidate| candidate.bytes)
            .sum::<usize>();
        FontEvictionPlan {
            policy,
            frame: self.frame,
            total_bytes_before: self.total_bytes,
            bytes_to_free,
            total_bytes_after: self.total_bytes.saturating_sub(bytes_to_free),
            candidates,
        }
    }

    pub fn evict_planned(&mut self, plan: &FontEvictionPlan) -> Vec<FontLifecycleReport> {
        let mut reports = Vec::new();
        for candidate in &plan.candidates {
            let before = self.total_bytes;
            let Some(mut face) = self.faces.remove(&candidate.face_id) else {
                continue;
            };
            let previous = face.apply_status(FontLoadStatus::Pending, 0, self.frame);
            self.total_bytes = self.total_bytes.saturating_sub(previous);
            reports.push(report_from_face(
                &face,
                FontLifecycleOutcome::Evicted,
                None,
                self.frame,
                before,
                self.total_bytes,
            ));
        }
        reports
    }

    fn apply_status(
        &mut self,
        face_id: &FontFaceId,
        generation: FontGeneration,
        status: FontLoadStatus,
        bytes: usize,
        issue: Option<FontLifecycleIssue>,
    ) -> FontLifecycleReport {
        let before = self.total_bytes;
        let Some(face) = self.faces.get_mut(face_id) else {
            return FontLifecycleReport {
                face_id: face_id.clone(),
                outcome: FontLifecycleOutcome::Failed,
                issue: Some(FontLifecycleIssue::MissingFace),
                generation,
                current_generation: None,
                frame: self.frame,
                bytes_before: 0,
                bytes_after: 0,
                total_bytes_before: before,
                total_bytes_after: self.total_bytes,
                created_frame: None,
                last_used_frame: None,
            };
        };

        if generation < face.descriptor.generation {
            return stale_report(
                face_id.clone(),
                generation,
                face.descriptor.generation,
                self.frame,
                before,
                self.total_bytes,
            );
        }

        face.descriptor.generation = generation;
        self.generation = self.generation.max(generation);
        let previous = face.apply_status(status, bytes, self.frame);
        self.total_bytes = self
            .total_bytes
            .saturating_sub(previous)
            .saturating_add(bytes);
        let outcome = match &face.status {
            FontLoadStatus::Pending => FontLifecycleOutcome::Registered,
            FontLoadStatus::Loaded => FontLifecycleOutcome::Loaded,
            FontLoadStatus::Missing => FontLifecycleOutcome::Missing,
            FontLoadStatus::Failed { .. } => FontLifecycleOutcome::Failed,
        };
        report_from_face(face, outcome, issue, self.frame, before, self.total_bytes)
    }
}

fn stale_report(
    face_id: FontFaceId,
    generation: FontGeneration,
    current_generation: FontGeneration,
    frame: u64,
    total_bytes_before: usize,
    total_bytes_after: usize,
) -> FontLifecycleReport {
    FontLifecycleReport {
        face_id,
        outcome: FontLifecycleOutcome::Stale,
        issue: Some(FontLifecycleIssue::StaleGeneration {
            current_generation,
            update_generation: generation,
        }),
        generation,
        current_generation: Some(current_generation),
        frame,
        bytes_before: 0,
        bytes_after: 0,
        total_bytes_before,
        total_bytes_after,
        created_frame: None,
        last_used_frame: None,
    }
}

fn report_from_face(
    face: &CachedFontFace,
    outcome: FontLifecycleOutcome,
    issue: Option<FontLifecycleIssue>,
    frame: u64,
    total_bytes_before: usize,
    total_bytes_after: usize,
) -> FontLifecycleReport {
    FontLifecycleReport {
        face_id: face.descriptor.id.clone(),
        outcome,
        issue,
        generation: face.descriptor.generation,
        current_generation: Some(face.descriptor.generation),
        frame,
        bytes_before: face.bytes,
        bytes_after: face.bytes,
        total_bytes_before,
        total_bytes_after,
        created_frame: Some(face.created_frame),
        last_used_frame: Some(face.last_used_frame),
    }
}

fn sorted_loaded_faces(faces: &HashMap<FontFaceId, CachedFontFace>) -> Vec<&CachedFontFace> {
    let mut faces = faces
        .values()
        .filter(|face| face.status.is_loaded() && face.bytes > 0)
        .collect::<Vec<_>>();
    faces.sort_by(|left, right| {
        (left.last_used_frame, left.created_frame)
            .cmp(&(right.last_used_frame, right.created_frame))
            .then_with(|| {
                face_id_order(&left.descriptor.id).cmp(&face_id_order(&right.descriptor.id))
            })
    });
    faces
}

fn face_id_order(face_id: &FontFaceId) -> (&str, u16, u8, u8) {
    (
        face_id.family.as_str(),
        face_id.weight.0,
        font_style_order(face_id.style),
        font_stretch_order(face_id.stretch),
    )
}

fn font_style_order(style: FontStyle) -> u8 {
    match style {
        FontStyle::Normal => 0,
        FontStyle::Italic => 1,
        FontStyle::Oblique => 2,
    }
}

fn font_stretch_order(stretch: FontStretch) -> u8 {
    match stretch {
        FontStretch::Condensed => 0,
        FontStretch::Normal => 1,
        FontStretch::Expanded => 2,
    }
}

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

    fn face(family: &str) -> FontFaceId {
        FontFaceId::regular(family)
    }

    fn descriptor(family: &str, generation: u64) -> FontFaceDescriptor {
        FontFaceDescriptor::new(
            face(family),
            FontSourceDescriptor::memory(format!("{family}-regular")),
            FontGeneration(generation),
        )
    }

    fn register_loaded(
        registry: &mut FontRegistry,
        family: &str,
        generation: u64,
        bytes: usize,
    ) -> FontFaceId {
        let id = face(family);
        registry.register_face(descriptor(family, generation));
        registry.mark_loaded(&id, FontGeneration(generation), bytes);
        id
    }

    #[test]
    fn fallback_stack_resolves_first_loaded_face_and_records_attempts() {
        let mut registry = FontRegistry::new();
        let missing = face("Missing Sans");
        let loaded = register_loaded(&mut registry, "App Sans", 1, 128);
        registry.register_face(descriptor("Missing Sans", 1));
        registry.mark_missing(&missing, FontGeneration(1));

        let resolution = registry.resolve_fallback_stack(
            &FontFallbackStack::new(["Missing Sans", "App Sans", "System Sans"]),
            FontWeight::NORMAL,
            FontStyle::Normal,
            FontStretch::Normal,
        );

        assert_eq!(resolution.resolved, Some(loaded));
        assert_eq!(resolution.attempts.len(), 2);
        assert_eq!(resolution.missing_faces(), vec![&missing]);
    }

    #[test]
    fn missing_and_failed_faces_are_reported() {
        let mut registry = FontRegistry::new();
        let missing = face("Missing Sans");
        let failed = face("Broken Serif");
        registry.register_face(descriptor("Missing Sans", 1));
        registry.register_face(descriptor("Broken Serif", 1));

        let missing_report = registry.mark_missing(&missing, FontGeneration(1));
        let failed_report = registry.mark_failed(&failed, FontGeneration(1), "parse error");

        assert_eq!(missing_report.outcome, FontLifecycleOutcome::Missing);
        assert_eq!(failed_report.outcome, FontLifecycleOutcome::Failed);
        assert_eq!(
            registry
                .missing_faces()
                .iter()
                .map(|face| &face.descriptor.id)
                .collect::<Vec<_>>(),
            vec![&missing]
        );
        assert_eq!(
            registry
                .failed_faces()
                .iter()
                .map(|face| &face.descriptor.id)
                .collect::<Vec<_>>(),
            vec![&failed]
        );
    }

    #[test]
    fn stale_generation_load_results_are_rejected() {
        let mut registry = FontRegistry::new();
        let id = face("App Sans");
        registry.register_face(descriptor("App Sans", 2));

        let report = registry.mark_loaded(&id, FontGeneration(1), 128);

        assert_eq!(report.outcome, FontLifecycleOutcome::Stale);
        assert_eq!(
            report.issue,
            Some(FontLifecycleIssue::StaleGeneration {
                current_generation: FontGeneration(2),
                update_generation: FontGeneration(1),
            })
        );
        assert_eq!(registry.total_bytes(), 0);
        assert_eq!(
            registry.face(&id).map(|face| &face.status),
            Some(&FontLoadStatus::Pending)
        );
    }

    #[test]
    fn byte_accounting_tracks_loaded_missing_failed_and_replaced_faces() {
        let mut registry = FontRegistry::new();
        let loaded = face("App Sans");
        let missing = face("Missing Sans");
        let failed = face("Broken Serif");
        registry.register_face(descriptor("App Sans", 1));
        registry.register_face(descriptor("Missing Sans", 1));
        registry.register_face(descriptor("Broken Serif", 1));

        registry.mark_loaded(&loaded, FontGeneration(1), 200);
        registry.mark_missing(&missing, FontGeneration(1));
        registry.mark_failed(&failed, FontGeneration(1), "parse error");
        assert_eq!(registry.total_bytes(), 200);

        registry.mark_loaded(&loaded, FontGeneration(2), 80);

        assert_eq!(registry.total_bytes(), 80);
        assert_eq!(registry.face(&loaded).map(|face| face.bytes), Some(80));
        assert_eq!(registry.face(&missing).map(|face| face.bytes), Some(0));
        assert_eq!(registry.face(&failed).map(|face| face.bytes), Some(0));
    }

    #[test]
    fn fallback_resolution_updates_last_used_frame() {
        let mut registry = FontRegistry::new();
        let id = register_loaded(&mut registry, "App Sans", 1, 128);
        registry.advance_frame();
        registry.advance_frame();

        let resolution = registry.resolve_fallback_stack(
            &FontFallbackStack::new(["App Sans"]),
            FontWeight::NORMAL,
            FontStyle::Normal,
            FontStretch::Normal,
        );

        assert_eq!(resolution.resolved, Some(id.clone()));
        assert_eq!(registry.face(&id).map(|face| face.last_used_frame), Some(2));
    }

    #[test]
    fn eviction_planning_selects_least_recently_used_loaded_faces_under_budget() {
        let mut registry = FontRegistry::new();
        let old = register_loaded(&mut registry, "Old Sans", 1, 100);
        registry.advance_frame();
        let middle = register_loaded(&mut registry, "Middle Sans", 1, 90);
        registry.advance_frame();
        let recent = register_loaded(&mut registry, "Recent Sans", 1, 80);
        registry.advance_frame();
        registry.retain(&middle);

        let plan = registry.plan_eviction_by_byte_budget(120);

        assert_eq!(plan.total_bytes_before, 270);
        assert_eq!(plan.bytes_to_free, 180);
        assert_eq!(plan.total_bytes_after, 90);
        assert_eq!(
            plan.candidates
                .iter()
                .map(|candidate| &candidate.face_id)
                .collect::<Vec<_>>(),
            vec![&old, &recent]
        );
        assert_eq!(
            registry.face(&middle).map(|face| face.last_used_frame),
            Some(3)
        );
    }
}