bevy_cobweb_ui 0.22.2

UI framework for the bevy game engine
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
use std::collections::{HashMap, HashSet};
use std::str::FromStr;

use bevy::asset::AssetLoadFailedEvent;
use bevy::prelude::*;
use bevy_cobweb::prelude::*;
use fluent_langneg::LanguageIdentifier;
use smol_str::SmolStr;

use crate::prelude::*;

//-------------------------------------------------------------------------------------------------------------------

struct SelectedLocalizedFontFallback
{
    lang: SmolStr,
    font: String,
}

//-------------------------------------------------------------------------------------------------------------------

/// Gets font variants of a specific font family.
///
/// If the attributes list is empty, then all variants are returned. Otherwise only variants equal to requested
/// attributes are returned.
fn get_loaded_font_variants<'a>(
    families: &'a HashMap<SmolStr, Vec<FontVariant>>,
    family: &'a SmolStr,
    attrs: &'a [FontAttributes],
) -> impl Iterator<Item = &'a FontVariant> + 'a
{
    families
        .get(family)
        .or_else(|| {
            tracing::error!("ignoring loaded font ({family:?}, {attrs:?}) with unknown family; this is a bug");
            None
        })
        .into_iter()
        .flat_map(|v| v.iter())
        .filter_map(|v| {
            if attrs.len() > 0 {
                let v_attrs = v.attributes();
                if !attrs.iter().any(|attrs| *attrs == v_attrs) {
                    return None;
                }
            }
            Some(v)
        })
}

//-------------------------------------------------------------------------------------------------------------------

/// Gets localized font fallbacks for a given font variant from the main font.
fn get_dependent_font_variants(
    families: &HashMap<SmolStr, Vec<FontVariant>>,
    reference_font_family: &SmolStr,
    reference_font: FontAttributes,
    loaded_fallbacks: &Vec<LocalizedFontFallback>,
) -> Vec<SelectedLocalizedFontFallback>
{
    let mut fallbacks = Vec::<SelectedLocalizedFontFallback>::default();
    for LocalizedFontFallback { lang, family, attributes } in loaded_fallbacks {
        let Some(variants) = families.get(family) else {
            tracing::error!("failed getting fallback fonts for font family {reference_font_family:?} for language \
                {lang:?}; fallback font family {family:?} is unknown (fallback attrs: {attributes:?}); \
                this is a bug");
            continue;
        };

        let attributes = if attributes.len() == 0 {
            reference_font.negotiate_eligible_fonts(|| variants.iter().map(|v| v.attributes()))
        } else {
            reference_font.negotiate_eligible_fonts(|| attributes.iter().cloned())
        };

        let Some(attributes) = attributes else {
            tracing::warn!("failed getting fallback fonts for font family {reference_font_family:?} with \
                {reference_font:?} for language {lang:?}; unable to select eligible fallback font for fallback \
                font family {family:?} (fallback attrs: {attributes:?}, registered variants: {variants:?})");
            continue;
        };

        let Some(variant) = variants.iter().find(|v| v.attributes() == attributes) else {
            tracing::warn!("failed getting fallback fonts for font family {reference_font_family:?} with \
                {reference_font:?} for language {lang:?}; selected font attributes don't match any registered \
                font variants for fallback font family {family:?} (selected: {attributes:?}, fallback attrs: \
                {attributes:?}, registered variants: {variants:?})");
            continue;
        };

        fallbacks.push(SelectedLocalizedFontFallback { lang: lang.clone(), font: variant.path.clone() })
    }

    fallbacks
}

//-------------------------------------------------------------------------------------------------------------------

fn get_eligible_font<'a>(
    families: &'a HashMap<SmolStr, Vec<FontVariant>>,
    font: &FontRequest,
) -> Option<&'a FontVariant>
{
    let variants = families.get(&**font.family)?;
    let attributes = font
        .attributes()
        .negotiate_eligible_fonts(|| variants.iter().map(|v| v.attributes()))?;

    // Get the variant that matches what we found.
    let Some(variant) = variants.iter().find(|v| v.attributes() == attributes) else {
        tracing::error!("failed negotiating eligible fonts for request {font:?} and variants {variants:?} \
            even though a variant was presumably found ({attributes:?}); this is a bug");
        return None;
    };
    Some(variant)
}

//-------------------------------------------------------------------------------------------------------------------

fn load_localized_fonts(
    In(loaded): In<Vec<LocalizedFont>>,
    mut c: Commands,
    asset_server: Res<AssetServer>,
    mut fonts: ResMut<FontMap>,
    manifest: Res<LocalizationManifest>,
)
{
    fonts.insert_localized(loaded, &asset_server, &manifest, &mut c);
}

//-------------------------------------------------------------------------------------------------------------------

fn load_fonts(In(families): In<Vec<SmolStr>>, asset_server: Res<AssetServer>, mut fonts: ResMut<FontMap>)
{
    for family in families {
        fonts.load(&FontFamily(family), &asset_server);
    }
}

//-------------------------------------------------------------------------------------------------------------------

fn register_font_families(
    In(registrations): In<Vec<RegisterFontFamily>>,
    mut c: Commands,
    asset_server: Res<AssetServer>,
    mut fonts: ResMut<FontMap>,
    manifest: Res<LocalizationManifest>,
)
{
    for registration in registrations {
        fonts.register(registration, &asset_server, &manifest, &mut c);
    }
}

//-------------------------------------------------------------------------------------------------------------------

fn handle_new_lang_list(
    asset_server: Res<AssetServer>,
    manifest: Res<LocalizationManifest>,
    mut fonts: ResMut<FontMap>,
)
{
    fonts.negotiate_languages(&manifest, &asset_server);
}

//-------------------------------------------------------------------------------------------------------------------

fn check_loaded_fonts(
    mut c: Commands,
    mut events: MessageReader<AssetEvent<Font>>,
    mut errors: MessageReader<AssetLoadFailedEvent<Font>>,
    mut fonts: ResMut<FontMap>,
)
{
    for event in events.read() {
        let AssetEvent::Added { id } = event else { continue };
        fonts.remove_pending(id);
    }

    for error in errors.read() {
        let AssetLoadFailedEvent { id, .. } = error;
        fonts.remove_pending(id);
    }

    fonts.try_emit_load_event(&mut c);
}

//-------------------------------------------------------------------------------------------------------------------

/// Reactive event broadcasted when [`FontMap`] has updated and become fully loaded *after* a [`LoadFonts`]
/// instance was applied.
///
/// This event is *not* emitted when fonts are reloaded due to language renegotiation. Listen for the
/// [`RelocalizeApp`] event instead.
pub struct FontMapLoaded;

//-------------------------------------------------------------------------------------------------------------------

/// Resource that stores handles to loaded fonts and manages font localization.
///
/// Fonts must be pre-registered with [`RegisterFontFamilies`]. Registered fonts are *not* automatically loaded.
/// Use [`LoadFonts`], [`LoadLocalizedFonts`], or [`FontMap::get_or_load`] to trigger asset loads.
///
/// Localization font fallbacks are supported. If you use the [`LocalizedText`] component then fallbacks will be
/// applied automatically. You only need to insert 'main fonts' to entity text via [`Self::get`] (when initially
/// constructing text sections) or [`TextEditor::set_font`] (when editing existing text).
///
/// Fonts are automatically loaded and unloaded when languages are changed, so that only fonts that might be
/// needed are kept in memory. Note that the text backend may cache a copy of all fonts indefinitely. This map
/// only manages Bevy font assets.
#[derive(Resource, Default)]
pub struct FontMap
{
    /// Indicates the current pending fonts came from `LoadFonts`/`LoadLocalizedFonts` entries, rather than from
    /// negotiating languages.
    ///
    /// This is used to emit `FontMapLoaded` events accurately.
    waiting_for_load: bool,
    /// All font assets that are currently loaded.
    pending: HashSet<AssetId<Font>>,

    /// Loaded fonts waiting for font families to be registered.
    loaded_awaiting_families: Vec<FontFamily>,
    localized_awaiting_families: Vec<LocalizedFont>,

    /// Registered font families.
    families: HashMap<SmolStr, Vec<FontVariant>>,

    /// Fonts that are permanently cached, including main fonts.
    ///
    /// We keep all main fonts loaded because to spawn a new text entity, the user needs to get a
    /// `Handle<Font>` for their main font. Font localization happens separate from the 'insert Text component'
    /// step because when inserting a Text component you don't always know what language each text section
    /// requires.
    ///
    /// When localization *does* occur for a newly-inserted Text component, we use existing font handles on
    /// the text sections to look up font fallbacks.
    ///
    /// This can contain stale 'main fonts' if you hot reload `LoadFonts` and the reloaded version removes some
    /// main fonts. It should not affect users in any way.
    /// [ font path : font handle ]
    cached_fonts: HashMap<String, Handle<Font>>,
    /// Map between main fonts and language-specific fallbacks.
    ///
    /// This is updated whenever a new font list is inserted.
    /// [ main font : [ lang : fallback font ] ]
    localization_map: HashMap<AssetId<Font>, HashMap<LanguageIdentifier, String>>,
    /// Map between font paths and font handles for all currently-active localization fallbacks.
    ///
    /// This is reconstructed whenever languages are renegotiated.
    /// [ font path : font handle ]
    localization_fonts: HashMap<String, Handle<Font>>,
}

impl FontMap
{
    /// Checks if the map has any fonts waiting to load.
    pub fn is_loading(&self) -> bool
    {
        !self.pending.is_empty()
    }

    fn try_add_pending(handle: &Handle<Font>, asset_server: &AssetServer, pending: &mut HashSet<AssetId<Font>>)
    {
        match asset_server.load_state(handle) {
            bevy::asset::LoadState::Loaded => (),
            _ => {
                pending.insert(handle.id());
            }
        }
    }

    fn try_emit_load_event(&mut self, c: &mut Commands)
    {
        if self.is_loading() {
            return;
        }
        if !self.waiting_for_load {
            return;
        }

        self.waiting_for_load = false;
        c.react().broadcast(FontMapLoaded);
    }

    /// Returns `false` if no localized assets were loaded.
    fn negotiate_languages(&mut self, manifest: &LocalizationManifest, asset_server: &AssetServer) -> bool
    {
        // Skip negotiation of there are no negotiated languages yet.
        // - This avoids spuriously loading assets that will be replaced once the language list is known.
        let negotiated = manifest.negotiated();
        if negotiated.len() == 0 {
            return false;
        }

        let prev_localization_fonts = std::mem::take(&mut self.localization_fonts);
        self.localization_fonts
            .reserve(prev_localization_fonts.len());

        self.localization_map.iter().for_each(|(_main, fallbacks)| {
            // Save fallbacks for currently-negotiated languages.
            for (_lang, font) in fallbacks
                .iter()
                .filter(|(lang, _)| negotiated.iter().any(|n| n == *lang))
            {
                let handle = prev_localization_fonts
                    .get(font)
                    .or_else(|| self.cached_fonts.get(font))
                    .cloned()
                    .unwrap_or_else(|| {
                        let new_handle = asset_server.load(String::from(font.as_str()));
                        Self::try_add_pending(&new_handle, asset_server, &mut self.pending);
                        new_handle
                    });
                self.localization_fonts.insert(font.clone(), handle);
            }
        });

        true
    }

    fn remove_pending(&mut self, id: &AssetId<Font>) -> bool
    {
        self.pending.remove(id)
    }

    /// Checks and warns if font requests were unresolved on entering `LoadState::Done`.
    fn check_unresolved_font_requests(map: Res<FontMap>)
    {
        for loaded in &map.loaded_awaiting_families {
            tracing::warn!("failed loading requested font {loaded:?} before LoadState::Done; use RegisterFontFamilies");
        }
        for loaded in &map.localized_awaiting_families {
            tracing::warn!("failed loading localized font {loaded:?} or one of its fallbacks before LoadState::Done; \
                use RegisterFontFamilies");
        }
    }

    /// Registers a font family so its members can be loaded on request.
    pub fn register(
        &mut self,
        family: RegisterFontFamily,
        asset_server: &AssetServer,
        manifest: &LocalizationManifest,
        c: &mut Commands,
    )
    {
        if family.fonts.len() == 0 {
            tracing::warn!("ignoring font registration {family:?} with no variants");
            return;
        }

        if let Some(mut prev) = self
            .families
            .insert(family.family.deref().clone(), family.fonts)
        {
            tracing::warn!("overwriting font family {:?}; old font variants: {prev:?} (\"manual::\" fonts are not \
                discarded)", family.family);

            // Preserve manually-inserted variants.
            let mut manual: Vec<_> = prev
                .drain(..)
                .filter(|v| v.path.starts_with("manual::"))
                .collect();
            if manual.len() > 0 {
                self.families
                    .get_mut(&*family.family)
                    .unwrap()
                    .extend(manual.drain(..));
            }
        }

        // Insert loaded requests that can be completed now.
        //todo: use extract_if when stabilized
        let mut to_insert = Vec::<FontFamily>::default();
        self.loaded_awaiting_families.retain(|l| {
            if family.family == *l {
                to_insert.push(l.clone());
                false;
            }
            true
        });
        to_insert.drain(..).for_each(|l| {
            self.load(&l, asset_server);
        });

        // Insert loaded localized requests that can be completed now.
        //todo: use extract_if when stabilized
        let mut to_insert = Vec::<LocalizedFont>::default();
        self.localized_awaiting_families.retain(|l| {
            if self.families.contains_key(&l.family)
                && !l
                    .fallbacks
                    .iter()
                    .any(|fallback| !self.families.contains_key(&fallback.family))
            {
                to_insert.push(l.clone());
                false;
            }
            true
        });
        self.insert_localized(to_insert, asset_server, manifest, c);
    }

    /// Adds a font family that should be loaded.
    ///
    /// Returns `false` if there are no eligible fonts. See [`RegisterFontFamilies`]. The requested font
    /// will be cached in case it was loaded out-of-order with `RegisterFontFamilies` insertion.
    ///
    /// Note that if this is called in state [`LoadState::Loading`], then [`LoadState::Done`] will wait
    /// for the font to be loaded.
    pub fn load(&mut self, family: &FontFamily, asset_server: &AssetServer) -> bool
    {
        // Save font requests that can't be loaded yet.
        let Some(variants) = self.families.get(&**family) else {
            self.loaded_awaiting_families.push(family.clone());
            return false;
        };

        variants.iter().for_each(|v| {
            Self::get_or_load_impl(
                &mut self.pending,
                &mut self.cached_fonts,
                &self.localization_fonts,
                v,
                asset_server,
            );
        });

        true
    }

    /// Adds a new set of [`LocalizedFonts`](`LocalizedFont`).
    ///
    /// Will automatically renegotiate languages and emit [`FontMapLoaded`] if appropriate.
    ///
    /// Note that if this is called in state [`LoadState::Loading`], then [`LoadState::Done`] will wait
    /// for new fonts to be loaded.
    pub fn insert_localized(
        &mut self,
        mut loaded: Vec<LocalizedFont>,
        asset_server: &AssetServer,
        manifest: &LocalizationManifest,
        c: &mut Commands,
    )
    {
        // Extract font requests that can't be loaded yet.
        //todo: use extract_if when stabilized
        let mut loaded2 = Vec::with_capacity(loaded.len());
        for font in loaded.drain(..) {
            if !self.families.contains_key(&font.family)
                || font
                    .fallbacks
                    .iter()
                    .any(|fallback| !self.families.contains_key(&fallback.family))
            {
                self.localized_awaiting_families.push(font);
            } else {
                loaded2.push(font);
            }
        }

        // Load the requested fonts.
        for (font_variant, mut loaded_fallbacks) in loaded2.iter().flat_map(|loaded| {
            // A `LocalizedFont` is a set of fonts that should be localized to other sets of fonts
            // per-language. We first get the set of fonts to localize, then for each of those fonts,
            // we get the font that each language should fall back to.
            get_loaded_font_variants(&self.families, &loaded.family, &loaded.attributes).map(|font_variant| {
                let fallbacks = get_dependent_font_variants(
                    &self.families,
                    &loaded.family,
                    font_variant.attributes(),
                    &loaded.fallbacks,
                );
                (font_variant, fallbacks)
            })
        }) {
            // Add main font.
            let loaded_font = &font_variant.path;
            let main_handle = self
                .cached_fonts
                .get(loaded_font)
                .cloned()
                .or_else(|| {
                    self.localization_fonts
                        .get(loaded_font)
                        .inspect(|handle| {
                            // Main fonts need to be cached.
                            self.cached_fonts
                                .insert(loaded_font.clone(), (*handle).clone());
                        })
                        .cloned()
                })
                .unwrap_or_else(|| {
                    let new_handle = asset_server.load(String::from(loaded_font.as_str()));
                    self.cached_fonts
                        .insert(loaded_font.clone(), new_handle.clone());
                    Self::try_add_pending(&new_handle, asset_server, &mut self.pending);
                    new_handle
                });

            // Add fallbacks.
            let fallbacks = self.localization_map.entry(main_handle.id()).or_default();

            #[cfg(not(feature = "hot_reload"))]
            if fallbacks.len() > 0 {
                // This is feature-gated by hot_reload to avoid spam when hot reloading large lists.
                tracing::warn!("overwritting font fallbacks for main font {:?}; main fonts should only appear in one \
                    LoadFonts command per app", loaded_font);
            }

            fallbacks.clear();
            fallbacks.reserve(loaded_fallbacks.len());

            for SelectedLocalizedFontFallback { lang, font } in loaded_fallbacks.drain(..) {
                let lang_id = match LanguageIdentifier::from_str(lang.as_str()) {
                    Ok(lang_id) => lang_id,
                    Err(err) => {
                        tracing::error!("failed parsing target language id for font fallback {:?} for main font \
                            {:?}: {:?}", font, loaded_font, err);
                        continue;
                    }
                };

                if let Some(prev) = fallbacks.insert(lang_id, font) {
                    tracing::warn!("overwriting font fallback {:?} for font {:?} for lang {:?}",
                        prev, loaded_font, lang);
                }
            }
        }

        // Load fallback fonts as needed.
        if self.negotiate_languages(manifest, asset_server) {
            self.waiting_for_load = true;
            self.try_emit_load_event(c);
        }
    }

    /// Gets the nearest eligible font using CSS font eligibility rules.
    ///
    /// Returns `None` if the requested font's family was not registered. See [`RegisterFontFamilies`].
    pub fn get_eligible_font(&self, font: &FontRequest) -> Option<&FontVariant>
    {
        get_eligible_font(&self.families, font)
    }

    /// Gets a font handle for the requested font.
    ///
    /// The returned handle will *not* be localized. Use [`Self::get_localized`] or
    /// [`Self::get_or_load_localized`] instead.
    ///
    /// Returns a default handle if the font was not successfully pre-inserted via [`Self::load`].
    pub fn get(&self, font: &FontRequest) -> Handle<Font>
    {
        // Get the best eligible font.
        let Some(eligible_font) = self.get_eligible_font(font) else {
            tracing::error!("failed font request {:?} that has no eligible fonts; use RegisterFontFamilies command", font);
            return Default::default();
        };

        // Look in cached map only.
        // - We assume localization fonts are 'invisible' to the user.
        let Some(entry) = self.cached_fonts.get(&eligible_font.path) else {
            tracing::error!("failed getting font {:?} that was not loaded; use LoadFonts command or \
                FontMap::insert", font);
            return Default::default();
        };
        entry.clone()
    }

    /// Gets a font handle for the requested font, or loads and caches the font if it's unloaded.
    ///
    /// Returns a default handle if there are no eligible fonts. See [`RegisterFontFamilies`].
    ///
    /// Note that if this is called in state [`LoadState::Loading`], then [`LoadState::Done`] will wait
    /// for the font to be loaded.
    pub fn get_or_load(&mut self, font: &FontRequest, asset_server: &AssetServer) -> Handle<Font>
    {
        // Get the best eligible font.
        let Some(eligible_font) = get_eligible_font(&self.families, font) else {
            tracing::error!("failed font request {:?} that has no eligible fonts; use RegisterFontFamilies command", font);
            return Default::default();
        };

        Self::get_or_load_impl(
            &mut self.pending,
            &mut self.cached_fonts,
            &self.localization_fonts,
            eligible_font,
            asset_server,
        )
    }

    fn get_or_load_impl(
        pending: &mut HashSet<AssetId<Font>>,
        cached_fonts: &mut HashMap<String, Handle<Font>>,
        localization_fonts: &HashMap<String, Handle<Font>>,
        eligible_font: &FontVariant,
        asset_server: &AssetServer,
    ) -> Handle<Font>
    {
        let path = &eligible_font.path;
        cached_fonts
            .get(path)
            .cloned()
            .or_else(|| {
                localization_fonts
                    .get(path)
                    .inspect(|handle| {
                        // Cache the font because `get_or_load` access implies the font should be permanently
                        // stored.
                        cached_fonts.insert(path.clone(), (*handle).clone());
                    })
                    .cloned()
            })
            .unwrap_or_else(|| {
                let new_handle = asset_server.load(String::from(path.as_str()));
                cached_fonts.insert(path.clone(), new_handle.clone());
                Self::try_add_pending(&new_handle, asset_server, pending);
                new_handle
            })
    }

    /// Inserts a font handle directly, with the assumption it never needs to be loaded or unloaded.
    pub fn manual_insert(&mut self, font: Handle<Font>, req: FontRequest)
    {
        let path = format!("manual::{}::{:?}::{:?}::{:?}", req.family.as_str(), req.width, req.style, req.weight);
        let variant = FontVariant {
            path: path.clone(),
            width: req.width,
            style: req.style,
            weight: req.weight,
        };

        if let Some(_) = self.cached_fonts.insert(path, font) {
            tracing::warn!("overwriting handle for manually inserted font {:?}", req);
        }

        let Some(existing) = self.families.get_mut(&*req.family) else {
            self.families.insert(req.family.0, vec![variant]);
            return;
        };

        // Note: this can cause duplicate variants to be inserted. Doing so should be harmless.
        existing.push(variant);
    }

    /// Gets the font localized to `lang_id` for the given `main_font`.
    ///
    /// Returns `None` if there is no font fallback for `lang_id`.
    pub fn get_localized(&self, lang_id: &LanguageIdentifier, main_font: AssetId<Font>) -> Option<Handle<Font>>
    {
        self.localization_map
            .get(&main_font)
            .and_then(|fallbacks| fallbacks.get(lang_id))
            .and_then(|lang_font| {
                self.localization_fonts.get(lang_font).or_else(|| {
                    tracing::error!("font fallback {:?} is missing from loaded fonts, the requested language {:?} \
                        is probably not in the negotiated languages list of LocalizationManifest", lang_font, lang_id);
                    None
                })
            })
            .cloned()
    }

    /// Gets the font localized to `lang_id` for the given `main_font`.
    ///
    /// Will cache the font if it's not already cached, and will load it if it's not loaded.
    ///
    /// This can be used if you need to localize the font of text that should be displayed in another
    /// language (e.g. usernames).
    ///
    /// Will return the requested main font if no language-specific fallback is found.
    pub fn get_or_load_localized(
        &mut self,
        lang_id: &LanguageIdentifier,
        main_font: &FontRequest,
        asset_server: &AssetServer,
    ) -> Handle<Font>
    {
        // Get the best eligible font.
        let Some(eligible_font) = get_eligible_font(&self.families, main_font) else {
            tracing::error!("failed get-or-loaded-localized font request {:?} that has no eligible fonts; use \
                RegisterFontFamilies command", main_font);
            return Default::default();
        };

        let main_font = &eligible_font.path;
        let mut to_cache: Option<(&String, Handle<Font>)> = None;
        let req_handle = self.cached_fonts
            .get(main_font)
            .map(|main| {
                // Look up the fallback and clone it into the `cached_fonts` map.
                // - If the fallback exists but isn't loaded, then load it.
                // - If there is no fallback, just use the `main` font.
                self.localization_map
                    .get(&main.id())
                    .and_then(|fallbacks| fallbacks.get(lang_id))
                    .map(|lang_font| {
                        self.cached_fonts.get(lang_font)
                            .or_else(|| {
                                self.localization_fonts.get(lang_font).inspect(|handle| {
                                    to_cache = Some((lang_font, (*handle).clone()));
                                })
                            })
                            .cloned()
                            .unwrap_or_else(|| {
                                let new_handle = asset_server.load(String::from(lang_font.as_str()));
                                to_cache = Some((lang_font, new_handle.clone()));
                                Self::try_add_pending(&new_handle, asset_server, &mut self.pending);
                                new_handle
                            })
                    })
                    .unwrap_or_else(|| {
                        tracing::debug!("failed get-or-load-localized font, requested main font does not have a fallback \
                            for language {:?}; returning main font {:?} instead", lang_id, main_font);
                        main.clone()
                    })
            })
            .unwrap_or_else(|| {
                tracing::debug!("failed get-or-load-localized font, requested main font {:?} is not registered, loading it \
                    directy instead for language {:?}", main_font, lang_id);

                let new_handle = asset_server.load(String::from(main_font.as_str()));
                to_cache = Some((main_font, new_handle.clone()));
                Self::try_add_pending(&new_handle, asset_server, &mut self.pending);
                new_handle
            });

        if let Some((path, handle)) = to_cache {
            self.cached_fonts.insert(path.clone(), handle);
        }

        req_handle
    }
}

impl AssetLoadProgress for FontMap
{
    fn pending_assets(&self) -> usize
    {
        self.pending.len()
    }

    // This may not be totally accurate if localization fonts and cached fonts overlap.
    fn total_assets(&self) -> usize
    {
        self.localization_fonts.len() + self.cached_fonts.len()
    }
}

//-------------------------------------------------------------------------------------------------------------------

/// Contains information for a font fallback.
///
/// See [`LocalizedFont`].
#[derive(Reflect, Default, Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct LocalizedFontFallback
{
    /// The language id for the fallback.
    pub lang: SmolStr,
    /// The font family for the fallback.
    pub family: SmolStr,
    /// The [`FontAttributes`] allowed for the fallback.
    ///
    /// A font variant will be selected from the attributes set that best matches the source font(s). If no
    /// attributes are specified, then the best variant from the font family will be selected.
    #[reflect(default)]
    pub attributes: Vec<FontAttributes>,
}

//-------------------------------------------------------------------------------------------------------------------

/// See [`LoadLocalizedFonts`].
#[derive(Reflect, Default, Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct LocalizedFont
{
    /// Family of the font to localize.
    pub family: SmolStr,
    /// A set of [`FontAttributes`] to target for the fallback.
    ///
    /// If no attributes are specified, then all members of this font family will use the fallbacks specified
    /// here. Note that each 'main font' variant can only be mapped to localization fallbacks *once*. Multiple
    /// mappings will override each other.
    #[reflect(default)]
    pub attributes: Vec<FontAttributes>,
    /// Fallback fonts for specific languages.
    ///
    /// Add fallbacks if `self.font` cannot be used for all languages. Any reference to `self.font` will be
    /// automatically localized to the right fallback on entities with [`LocalizedText`]. Note that
    /// [`LocalizedText`] keeps track of the main font (`self.font`) for every text section in case the user
    /// changes languages and it is necessary to switch to a different fallback.
    #[reflect(default)]
    pub fallbacks: Vec<LocalizedFontFallback>,
}

//-------------------------------------------------------------------------------------------------------------------

/// A member of a font family, with path for loading the asset.
#[derive(Reflect, Default, Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FontVariant
{
    /// Path to the font asset.
    pub path: String,
    #[reflect(default)]
    pub width: FontWidth,
    #[reflect(default)]
    pub style: FontStyle,
    #[reflect(default)]
    pub weight: FontWeight,
}

impl FontVariant
{
    /// Gets a [`FontAttributes`] from the variant.
    pub fn attributes(&self) -> FontAttributes
    {
        FontAttributes { width: self.width, style: self.style, weight: self.weight }
    }
}

//-------------------------------------------------------------------------------------------------------------------

/// A font family with all its font variants.
#[derive(Reflect, Default, Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct RegisterFontFamily
{
    pub family: FontFamily,
    pub fonts: Vec<FontVariant>,
}

//-------------------------------------------------------------------------------------------------------------------

/// Loadable command for registering localized fonts.
///
/// The loaded fonts can be accessed via [`FontMap`].
#[derive(Reflect, Default, Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct LoadLocalizedFonts(pub Vec<LocalizedFont>);

impl Command for LoadLocalizedFonts
{
    fn apply(self, world: &mut World)
    {
        world.syscall(self.0, load_localized_fonts);
    }
}

//-------------------------------------------------------------------------------------------------------------------

/// Loadable command for requesting font families to be loaded.
///
/// The [`RegisterFontFamilies`] command should be used before this command.
///
/// The loaded fonts can be accessed via [`FontMap`].
#[derive(Reflect, Default, Debug, Clone, PartialEq)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize, serde::Deserialize),
    reflect(Serialize, Deserialize)
)]
pub struct LoadFonts(pub Vec<SmolStr>);

impl Command for LoadFonts
{
    fn apply(self, world: &mut World)
    {
        world.syscall(self.0, load_fonts);
    }
}

//-------------------------------------------------------------------------------------------------------------------

/// Loadable command for registering font family asset maps.
///
/// The font families can be accessed via [`FontMap`].
///
/// Font assets added here are *not* automatically loaded. Use [`LoadFonts`], [`LoadLocalizedFonts`], or
/// [`FontMap::get_or_load`] to actually load the fonts you want to use.
#[derive(Reflect, Default, Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct RegisterFontFamilies(pub Vec<RegisterFontFamily>);

impl Command for RegisterFontFamilies
{
    fn apply(self, world: &mut World)
    {
        world.syscall(self.0, register_font_families);
    }
}

//-------------------------------------------------------------------------------------------------------------------

pub(crate) struct FontLoadPlugin;

impl Plugin for FontLoadPlugin
{
    fn build(&self, app: &mut App)
    {
        app.init_resource::<FontMap>()
            .register_asset_tracker::<FontMap>()
            .register_command_type::<LoadFonts>()
            .register_command_type::<LoadLocalizedFonts>()
            .register_command_type::<RegisterFontFamilies>()
            .add_reactor(broadcast::<LanguagesNegotiated>(), handle_new_lang_list)
            .add_systems(OnEnter(LoadState::Done), FontMap::check_unresolved_font_requests)
            .add_systems(PreUpdate, check_loaded_fonts.in_set(LoadProgressSet::Prepare));
    }
}

//-------------------------------------------------------------------------------------------------------------------