rust-fontconfig 5.0.0

Pure-Rust alternative to font-loader and fontconfig w. minimal dependencies
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
//! OS-specific font configuration.
//!
//! Contains default font directories, generic CSS families, and fallback configuration.
//! Hardcoded data is returned as `&'static` references to avoid allocation.
use crate::FcFontCache;
use crate::OperatingSystem;
use crate::UnicodeRange;
use alloc::collections::BTreeMap;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use std::path::{Path, PathBuf};

/// Generic CSS font family keywords (CSS Fonts Level 4).

/// Style tokens to filter out when guessing family names from filenames.
/// These are the weight/style/width suffixes commonly appended to font filenames
/// (e.g. "ArialBold.ttf", "NotoSans-SemiBold.otf"). Used by the scout thread
/// to extract the base family name from a filename.
pub const FONT_STYLE_TOKENS: &[&str] = &[
    "Regular",
    "Bold",
    "Italic",
    "Light",
    "Medium",
    "Thin",
    "Black",
    "ExtraLight",
    "ExtraBold",
    "SemiBold",
    "DemiBold",
    "Heavy",
    "Oblique",
    "Condensed",
    "Expanded",
    // The tokenizer splits compound styles (e.g. "SemiBold" → "Semi" + "Bold"),
    // so we need the modifier prefixes as standalone style tokens too.
    "Extra",
    "Semi",
    "Demi",
];

/// Check whether `family` is a generic CSS font family (case-insensitive).
pub fn is_generic_family(family: &str) -> bool {
    GenericFamily::from_css(family).is_some()
}

/// A CSS generic font family.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum GenericFamily {
    Serif,
    SansSerif,
    Monospace,
    Cursive,
    Fantasy,
    SystemUi,
    UiSerif,
    UiSansSerif,
    UiMonospace,
    UiRounded,
    Emoji,
    Math,
    Fangsong,
}

impl GenericFamily {
    /// Every generic, in [`GENERIC_FAMILIES`] order.
    pub const ALL: &'static [GenericFamily] = &[
        GenericFamily::Serif,
        GenericFamily::SansSerif,
        GenericFamily::Monospace,
        GenericFamily::Cursive,
        GenericFamily::Fantasy,
        GenericFamily::SystemUi,
        GenericFamily::UiSerif,
        GenericFamily::UiSansSerif,
        GenericFamily::UiMonospace,
        GenericFamily::UiRounded,
        GenericFamily::Emoji,
        GenericFamily::Math,
        GenericFamily::Fangsong,
    ];

    /// Parse a CSS keyword (case-insensitive, separators ignored).
    pub fn from_css(name: &str) -> Option<Self> {
        let key: String = name
            .chars()
            .filter(|c| c.is_ascii_alphanumeric())
            .map(|c| c.to_ascii_lowercase())
            .collect();
        Some(match key.as_str() {
            "serif" => GenericFamily::Serif,
            "sansserif" => GenericFamily::SansSerif,
            "monospace" => GenericFamily::Monospace,
            "cursive" => GenericFamily::Cursive,
            "fantasy" => GenericFamily::Fantasy,
            "systemui" => GenericFamily::SystemUi,
            "uiserif" => GenericFamily::UiSerif,
            "uisansserif" => GenericFamily::UiSansSerif,
            "uimonospace" => GenericFamily::UiMonospace,
            "uirounded" => GenericFamily::UiRounded,
            "emoji" => GenericFamily::Emoji,
            "math" => GenericFamily::Math,
            "fangsong" => GenericFamily::Fangsong,
            _ => return None,
        })
    }

    /// The CSS keyword.
    pub fn as_css(self) -> &'static str {
        match self {
            GenericFamily::Serif => "serif",
            GenericFamily::SansSerif => "sans-serif",
            GenericFamily::Monospace => "monospace",
            GenericFamily::Cursive => "cursive",
            GenericFamily::Fantasy => "fantasy",
            GenericFamily::SystemUi => "system-ui",
            GenericFamily::UiSerif => "ui-serif",
            GenericFamily::UiSansSerif => "ui-sans-serif",
            GenericFamily::UiMonospace => "ui-monospace",
            GenericFamily::UiRounded => "ui-rounded",
            GenericFamily::Emoji => "emoji",
            GenericFamily::Math => "math",
            GenericFamily::Fangsong => "fangsong",
        }
    }

    /// The generic whose configuration stands in when this one has none.
    pub fn parent(self) -> Option<Self> {
        match self {
            GenericFamily::Serif | GenericFamily::SansSerif | GenericFamily::Monospace => None,
            GenericFamily::UiSerif | GenericFamily::Fangsong => Some(GenericFamily::Serif),
            GenericFamily::UiMonospace => Some(GenericFamily::Monospace),
            GenericFamily::Cursive
            | GenericFamily::Fantasy
            | GenericFamily::SystemUi
            | GenericFamily::UiSansSerif
            | GenericFamily::UiRounded
            | GenericFamily::Emoji
            | GenericFamily::Math => Some(GenericFamily::SansSerif),
        }
    }

    /// `self`, then its parents, root last.
    fn lineage(self) -> impl Iterator<Item = GenericFamily> {
        let mut next = Some(self);
        core::iter::from_fn(move || {
            let current = next?;
            next = current.parent();
            Some(current)
        })
    }
}

/// Preferred families for a Unicode script block.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FcScriptFallback {
    /// Characters this preference applies to.
    pub range: UnicodeRange,
    /// Generic family this preference applies to, if any.
    pub generic: Option<GenericFamily>,
    /// Family names, best first.
    pub families: Vec<String>,
}

/// Fallback configuration for font resolution.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FcFallbackConfig {
    /// Base candidates per generic family, best first.
    pub generic_families: BTreeMap<GenericFamily, Vec<String>>,
    /// Substitutions for named families that are not installed.
    pub substitutions: BTreeMap<String, Vec<String>>,
    /// Per-script preferences, in priority order.
    pub script_fallbacks: Vec<FcScriptFallback>,
    /// Last resort families used when no other font provides coverage.
    pub last_resort: Vec<String>,
    /// Default generic family when a stack doesn't specify one.
    pub default_generic: GenericFamily,
}

impl Default for FcFallbackConfig {
    fn default() -> Self {
        Self::empty()
    }
}

/// Unicode blocks the built-in tables refer to.
pub mod blocks {
    use crate::UnicodeRange;

    pub const ARABIC: UnicodeRange = UnicodeRange {
        start: 0x0600,
        end: 0x06FF,
    };
    pub const HEBREW: UnicodeRange = UnicodeRange {
        start: 0x0590,
        end: 0x05FF,
    };
    pub const THAI: UnicodeRange = UnicodeRange {
        start: 0x0E00,
        end: 0x0E7F,
    };
    pub const CJK_SYMBOLS_AND_PUNCTUATION: UnicodeRange = UnicodeRange {
        start: 0x3000,
        end: 0x303F,
    };
    pub const HIRAGANA: UnicodeRange = UnicodeRange {
        start: 0x3040,
        end: 0x309F,
    };
    pub const KATAKANA: UnicodeRange = UnicodeRange {
        start: 0x30A0,
        end: 0x30FF,
    };
    pub const CJK_UNIFIED_IDEOGRAPHS: UnicodeRange = UnicodeRange {
        start: 0x4E00,
        end: 0x9FFF,
    };
    pub const HANGUL_SYLLABLES: UnicodeRange = UnicodeRange {
        start: 0xAC00,
        end: 0xD7A3,
    };
    pub const HALFWIDTH_AND_FULLWIDTH_FORMS: UnicodeRange = UnicodeRange {
        start: 0xFF00,
        end: 0xFFEF,
    };
}

fn names(list: &[&str]) -> Vec<String> {
    list.iter().map(|s| s.to_string()).collect()
}

fn push_unique(out: &mut Vec<String>, name: &str) {
    if !out.iter().any(|e| e.eq_ignore_ascii_case(name)) {
        out.push(name.to_string());
    }
}

impl FcFallbackConfig {
    /// Returns an empty fallback configuration.
    pub fn empty() -> Self {
        Self {
            generic_families: BTreeMap::new(),
            substitutions: BTreeMap::new(),
            script_fallbacks: Vec::new(),
            last_resort: Vec::new(),
            default_generic: GenericFamily::SansSerif,
        }
    }

    /// Returns the default OS-specific fallback configuration.
    pub fn os_defaults(os: OperatingSystem) -> Self {
        use blocks::*;
        use GenericFamily::{Monospace, SansSerif, Serif, SystemUi};
        let mut config = Self::empty();
        let mut generic = |g: GenericFamily, list: &[&str]| {
            config.generic_families.insert(g, names(list));
        };
        match os {
            OperatingSystem::Windows => {
                generic(Serif, &["Times New Roman"]);
                generic(
                    SansSerif,
                    &[
                        "Segoe UI",
                        "Tahoma",
                        "Microsoft Sans Serif",
                        "MS Sans Serif",
                        "Helv",
                    ],
                );
                generic(
                    Monospace,
                    &[
                        "Segoe UI Mono",
                        "Courier New",
                        "Cascadia Code",
                        "Cascadia Mono",
                        "Consolas",
                    ],
                );
            }
            OperatingSystem::Linux => {
                generic(
                    Serif,
                    &[
                        "Times",
                        "Times New Roman",
                        "DejaVu Serif",
                        "Free Serif",
                        "Noto Serif",
                        "Bitstream Vera Serif",
                        "Roman",
                        "Regular",
                    ],
                );
                generic(
                    SansSerif,
                    &[
                        "Ubuntu",
                        "Arial",
                        "DejaVu Sans",
                        "Noto Sans",
                        "Liberation Sans",
                    ],
                );
                generic(
                    Monospace,
                    &[
                        "Source Code Pro",
                        "Cantarell",
                        "DejaVu Sans Mono",
                        "Roboto Mono",
                        "Ubuntu Monospace",
                        "Droid Sans Mono",
                    ],
                );
            }
            OperatingSystem::MacOS | OperatingSystem::IOS => {
                generic(
                    SystemUi,
                    &[
                        "San Francisco",
                        "SFNS",
                        "SFNSDisplay",
                        "SFNSText",
                        "SFUI",
                        ".AppleSystemUIFont",
                        ".SFUIText",
                        ".SFUI-Regular",
                        "System Font",
                    ],
                );
                generic(Serif, &["Times New Roman", "Times", "New York", "Palatino"]);
                generic(SansSerif, &["Helvetica Neue", "Helvetica", "Lucida Grande"]);
                generic(
                    Monospace,
                    &[
                        "SF Mono",
                        "Menlo",
                        "Monaco",
                        "Courier",
                        "Oxygen Mono",
                        "Source Code Pro",
                        "Fira Mono",
                    ],
                );
            }
            OperatingSystem::Android => {
                generic(Serif, &["Noto Serif", "Roboto Serif", "Droid Serif"]);
                generic(
                    SansSerif,
                    &["Roboto", "Roboto-Regular", "Noto Sans", "Droid Sans"],
                );
                generic(
                    Monospace,
                    &[
                        "Roboto Mono",
                        "Droid Sans Mono",
                        "Noto Sans Mono",
                        "DejaVu Sans Mono",
                    ],
                );
            }
            OperatingSystem::Wasm => {}
        }

        let mut script = |g: GenericFamily, range: UnicodeRange, list: &[&str]| {
            config.script_fallbacks.push(FcScriptFallback {
                range,
                generic: Some(g),
                families: names(list),
            });
        };

        // CJK: ideographs are shared by Chinese, Japanese and Korean, so
        // their list keeps the historical order; kana are Japanese and
        // Hangul is Korean, so those blocks put the matching font first.
        let mut cjk = |g: GenericFamily, ideographs: &[&str], kana: &[&str], hangul: &[&str]| {
            for block in [
                CJK_SYMBOLS_AND_PUNCTUATION,
                CJK_UNIFIED_IDEOGRAPHS,
                HALFWIDTH_AND_FULLWIDTH_FORMS,
            ] {
                script(g, block, ideographs);
            }
            for block in [HIRAGANA, KATAKANA] {
                script(g, block, kana);
            }
            script(g, HANGUL_SYLLABLES, hangul);
        };
        match os {
            OperatingSystem::Windows => {
                cjk(
                    Serif,
                    &["MS Mincho", "SimSun", "MingLiU"],
                    &["MS Mincho", "SimSun", "MingLiU"],
                    &["SimSun", "MS Mincho", "MingLiU"],
                );
                cjk(
                    SansSerif,
                    &["Microsoft YaHei", "MS Gothic", "Malgun Gothic", "SimHei"],
                    &["MS Gothic", "Microsoft YaHei", "Malgun Gothic", "SimHei"],
                    &["Malgun Gothic", "Microsoft YaHei", "MS Gothic", "SimHei"],
                );
                cjk(
                    Monospace,
                    &["MS Gothic", "SimHei"],
                    &["MS Gothic", "SimHei"],
                    &["MS Gothic", "SimHei"],
                );
                script(Serif, ARABIC, &["Traditional Arabic"]);
                script(SansSerif, ARABIC, &["Segoe UI Arabic"]);
                script(SansSerif, HEBREW, &["Segoe UI Hebrew"]);
                script(SansSerif, THAI, &["Leelawadee UI"]);
            }
            OperatingSystem::Linux => {
                cjk(
                    Serif,
                    &[
                        "Noto Serif CJK SC",
                        "Noto Serif CJK JP",
                        "Noto Serif CJK KR",
                    ],
                    &[
                        "Noto Serif CJK JP",
                        "Noto Serif CJK SC",
                        "Noto Serif CJK KR",
                    ],
                    &[
                        "Noto Serif CJK KR",
                        "Noto Serif CJK SC",
                        "Noto Serif CJK JP",
                    ],
                );
                cjk(
                    SansSerif,
                    &[
                        "Noto Sans CJK SC",
                        "Noto Sans CJK JP",
                        "Noto Sans CJK KR",
                        "WenQuanYi Micro Hei",
                        "Droid Sans Fallback",
                    ],
                    &[
                        "Noto Sans CJK JP",
                        "Noto Sans CJK SC",
                        "Noto Sans CJK KR",
                        "WenQuanYi Micro Hei",
                        "Droid Sans Fallback",
                    ],
                    &[
                        "Noto Sans CJK KR",
                        "Noto Sans CJK SC",
                        "Noto Sans CJK JP",
                        "WenQuanYi Micro Hei",
                        "Droid Sans Fallback",
                    ],
                );
                cjk(
                    Monospace,
                    &[
                        "Noto Sans Mono CJK SC",
                        "Noto Sans Mono CJK JP",
                        "WenQuanYi Zen Hei Mono",
                    ],
                    &[
                        "Noto Sans Mono CJK JP",
                        "Noto Sans Mono CJK SC",
                        "WenQuanYi Zen Hei Mono",
                    ],
                    &[
                        "Noto Sans Mono CJK SC",
                        "Noto Sans Mono CJK JP",
                        "WenQuanYi Zen Hei Mono",
                    ],
                );
                script(Serif, ARABIC, &["Noto Serif Arabic"]);
                script(SansSerif, ARABIC, &["Noto Sans Arabic"]);
                script(SansSerif, HEBREW, &["Noto Sans Hebrew"]);
                script(SansSerif, THAI, &["Noto Sans Thai"]);
            }
            OperatingSystem::MacOS | OperatingSystem::IOS => {
                cjk(
                    Serif,
                    &["Hiragino Mincho ProN", "STSong", "AppleMyungjo"],
                    &["Hiragino Mincho ProN", "STSong", "AppleMyungjo"],
                    &["AppleMyungjo", "Hiragino Mincho ProN", "STSong"],
                );
                cjk(
                    SansSerif,
                    &[
                        "Hiragino Sans",
                        "Hiragino Kaku Gothic ProN",
                        "PingFang SC",
                        "PingFang TC",
                        "Apple SD Gothic Neo",
                    ],
                    &[
                        "Hiragino Sans",
                        "Hiragino Kaku Gothic ProN",
                        "PingFang SC",
                        "PingFang TC",
                        "Apple SD Gothic Neo",
                    ],
                    &[
                        "Apple SD Gothic Neo",
                        "Hiragino Sans",
                        "Hiragino Kaku Gothic ProN",
                        "PingFang SC",
                        "PingFang TC",
                    ],
                );
                cjk(
                    Monospace,
                    &["Hiragino Sans", "PingFang SC"],
                    &["Hiragino Sans", "PingFang SC"],
                    &["Hiragino Sans", "PingFang SC"],
                );
                script(Serif, ARABIC, &["Geeza Pro"]);
                script(SansSerif, ARABIC, &["Geeza Pro"]);
                script(SansSerif, HEBREW, &["Arial Hebrew"]);
                script(SansSerif, THAI, &["Thonburi"]);
            }
            OperatingSystem::Android => {
                cjk(
                    Serif,
                    &[
                        "Noto Serif CJK SC",
                        "Noto Serif CJK JP",
                        "Noto Serif CJK KR",
                    ],
                    &[
                        "Noto Serif CJK JP",
                        "Noto Serif CJK SC",
                        "Noto Serif CJK KR",
                    ],
                    &[
                        "Noto Serif CJK KR",
                        "Noto Serif CJK SC",
                        "Noto Serif CJK JP",
                    ],
                );
                cjk(
                    SansSerif,
                    &[
                        "Noto Sans CJK SC",
                        "Noto Sans CJK JP",
                        "Noto Sans CJK KR",
                        "Droid Sans Fallback",
                    ],
                    &[
                        "Noto Sans CJK JP",
                        "Noto Sans CJK SC",
                        "Noto Sans CJK KR",
                        "Droid Sans Fallback",
                    ],
                    &[
                        "Noto Sans CJK KR",
                        "Noto Sans CJK SC",
                        "Noto Sans CJK JP",
                        "Droid Sans Fallback",
                    ],
                );
                cjk(
                    Monospace,
                    &["Noto Sans Mono CJK SC", "Noto Sans Mono CJK JP"],
                    &["Noto Sans Mono CJK JP", "Noto Sans Mono CJK SC"],
                    &["Noto Sans Mono CJK SC", "Noto Sans Mono CJK JP"],
                );
                script(Serif, ARABIC, &["Noto Naskh Arabic"]);
                script(SansSerif, ARABIC, &["Noto Sans Arabic"]);
                script(SansSerif, HEBREW, &["Noto Sans Hebrew"]);
                script(SansSerif, THAI, &["Noto Sans Thai"]);
            }
            OperatingSystem::Wasm => {}
        }

        config
    }

    /// Base candidates for `generic`, borrowing from its
    /// [`parent`](GenericFamily::parent) when it has no entry of its own.
    pub fn generic_candidates(&self, generic: GenericFamily) -> &[String] {
        generic
            .lineage()
            .find_map(|g| self.generic_families.get(&g))
            .map(Vec::as_slice)
            .unwrap_or(&[])
    }

    /// Substitutions for the named `family` (normalized lookup).
    pub fn substitutions_for(&self, family: &str) -> &[String] {
        self.substitutions
            .get(&crate::utils::normalize_family_name(family))
            .map(Vec::as_slice)
            .unwrap_or(&[])
    }

    /// Returns the preferred fallback families for a given unicode range.
    /// Checks the specified generic family first (if any), followed by
    /// generic-agnostic fallbacks. Results are deduplicated and keep their order.
    pub fn script_candidates(
        &self,
        generic: Option<GenericFamily>,
        block: &UnicodeRange,
    ) -> Vec<String> {
        let mut out = Vec::new();
        if let Some(generic) = generic {
            for g in generic.lineage() {
                for entry in &self.script_fallbacks {
                    if entry.generic == Some(g) && entry.range.overlaps(block) {
                        entry.families.iter().for_each(|f| push_unique(&mut out, f));
                    }
                }
            }
        }
        for entry in &self.script_fallbacks {
            if entry.generic.is_none() && entry.range.overlaps(block) {
                entry.families.iter().for_each(|f| push_unique(&mut out, f));
            }
        }
        out
    }

    /// Every family name a generic may resolve to for text in `ranges`:
    /// script preferences for the overlapping blocks first, then the base
    /// candidates.
    pub fn expand_generic(&self, generic: GenericFamily, ranges: &[UnicodeRange]) -> Vec<String> {
        let mut out = Vec::new();
        for block in ranges {
            self.script_candidates(Some(generic), block)
                .iter()
                .for_each(|f| push_unique(&mut out, f));
        }
        self.generic_candidates(generic)
            .iter()
            .for_each(|f| push_unique(&mut out, f));
        out
    }

    /// [`expand_generic`](Self::expand_generic) for a generic keyword; a
    /// named family expands to itself followed by its substitutions.
    pub fn expand_family(&self, family: &str, ranges: &[UnicodeRange]) -> Vec<String> {
        match GenericFamily::from_css(family) {
            Some(generic) => self.expand_generic(generic, ranges),
            None => {
                let mut out = Vec::new();
                push_unique(&mut out, family);
                self.substitutions_for(family)
                    .iter()
                    .for_each(|f| push_unique(&mut out, f));
                out
            }
        }
    }

    /// Expands a CSS font stack and unicode ranges into a complete, ordered
    /// list of candidate font families to search for.
    pub fn candidate_families(&self, stack: &[String], ranges: &[UnicodeRange]) -> Vec<String> {
        let mut out = Vec::new();
        let mut any_generic = false;
        for family in stack {
            any_generic |= GenericFamily::from_css(family).is_some();
            self.expand_family(family, ranges)
                .iter()
                .for_each(|f| push_unique(&mut out, f));
        }
        for block in ranges {
            let generic = if any_generic {
                None
            } else {
                Some(self.default_generic)
            };
            self.script_candidates(generic, block)
                .iter()
                .for_each(|f| push_unique(&mut out, f));
        }
        self.last_resort
            .iter()
            .for_each(|f| push_unique(&mut out, f));
        out
    }

    /// Merges missing configuration values from `defaults` into this config.
    /// Does not overwrite or reorder existing entries.
    pub fn merge_defaults(&mut self, defaults: &FcFallbackConfig) {
        for (generic, families) in &defaults.generic_families {
            self.generic_families
                .entry(*generic)
                .or_insert_with(|| families.clone());
        }
        for (family, replacements) in &defaults.substitutions {
            self.substitutions
                .entry(family.clone())
                .or_insert_with(|| replacements.clone());
        }
        for entry in &defaults.script_fallbacks {
            let already = self
                .script_fallbacks
                .iter()
                .any(|e| e.generic == entry.generic && e.range.overlaps(&entry.range));
            if !already {
                self.script_fallbacks.push(entry.clone());
            }
        }
        if self.last_resort.is_empty() {
            self.last_resort = defaults.last_resort.clone();
        }
    }

    /// Take over parsed platform aliases (`fonts.conf` `<alias><prefer>`):
    /// a generic keyword becomes that generic's base candidates, anything
    /// else a named-family substitution. Keys are normalized family names.

    /// Extract all unique font families listed in this fallback configuration.
    pub fn extract_all_families(&self) -> Vec<String> {
        let mut out = Vec::new();
        for families in self.generic_families.values() {
            families.iter().for_each(|f| push_unique(&mut out, f));
        }
        for replacements in self.substitutions.values() {
            replacements.iter().for_each(|f| push_unique(&mut out, f));
        }
        for entry in &self.script_fallbacks {
            entry.families.iter().for_each(|f| push_unique(&mut out, f));
        }
        self.last_resort
            .iter()
            .for_each(|f| push_unique(&mut out, f));
        out
    }

    pub fn absorb_system_aliases(&mut self, aliases: BTreeMap<String, Vec<String>>) {
        for (key, prefs) in aliases {
            match GenericFamily::from_css(&key) {
                Some(generic) => {
                    self.generic_families.insert(generic, prefs);
                }
                None => {
                    self.substitutions.insert(key, prefs);
                }
            }
        }
    }
}

/// Static system font directories per OS.
/// All font directories (system + user-specific).
pub fn font_directories(os: OperatingSystem) -> Vec<PathBuf> {
    let mut dirs = Vec::new();
    match os {
        OperatingSystem::MacOS => {
            dirs.push(PathBuf::from("/System/Library/Fonts"));
            dirs.push(PathBuf::from("/Library/Fonts"));
            dirs.push(PathBuf::from("/System/Library/AssetsV2"));
            if let Ok(home) = std::env::var("HOME") {
                dirs.push(PathBuf::from(format!("{}/Library/Fonts", home)));
            }
        }
        OperatingSystem::Linux => {
            dirs.push(PathBuf::from("/usr/share/fonts"));
            dirs.push(PathBuf::from("/usr/local/share/fonts"));
            if let Ok(home) = std::env::var("HOME") {
                dirs.push(PathBuf::from(format!("{}/.fonts", home)));
                dirs.push(PathBuf::from(format!("{}/.local/share/fonts", home)));
            }
        }
        OperatingSystem::Windows => {
            let system_root = std::env::var("SystemRoot")
                .or_else(|_| std::env::var("WINDIR"))
                .unwrap_or_else(|_| "C:\\Windows".to_string());
            let user_profile =
                std::env::var("USERPROFILE").unwrap_or_else(|_| "C:\\Users\\Default".to_string());
            dirs.push(PathBuf::from(format!("{}\\Fonts", system_root)));
            dirs.push(PathBuf::from(format!(
                "{}\\AppData\\Local\\Microsoft\\Windows\\Fonts",
                user_profile
            )));
        }
        OperatingSystem::Android => {
            dirs.push(PathBuf::from("/system/fonts"));
            dirs.push(PathBuf::from("/product/fonts"));
            dirs.push(PathBuf::from("/system_ext/fonts"));
            dirs.push(PathBuf::from("/data/fonts"));
        }
        OperatingSystem::IOS | OperatingSystem::Wasm => {}
    }

    dirs
}

/// Common font families for priority boosting, as human-readable names.
/// These families will be parsed first so likely-needed fonts are available sooner.
pub fn common_font_families(os: OperatingSystem) -> &'static [&'static str] {
    match os {
        OperatingSystem::MacOS => &[
            // System UI fonts (actual filenames use SFNS prefix)
            "San Francisco",
            "SFNS",
            "System Font",
            // Sans-serif
            "Helvetica Neue",
            "Helvetica",
            "Arial",
            "Lucida Grande",
            // Serif
            "Times New Roman",
            "Georgia",
            // Monospace
            "Menlo",
            "SF Mono",
            "Courier",
        ],
        OperatingSystem::Linux => &[
            // Sans-serif
            "DejaVu Sans",
            "Ubuntu",
            "Roboto",
            "Noto Sans",
            "Liberation Sans",
            "Droid Sans",
            "Arial",
            // Serif
            "DejaVu Serif",
            "Noto Serif",
            // Monospace
            "DejaVu Sans Mono",
        ],
        OperatingSystem::Windows => &[
            // Sans-serif
            "Segoe UI",
            "Arial",
            "Tahoma",
            "Verdana",
            // Serif
            "Times New Roman",
            "Calibri",
            // Monospace
            "Consolas",
            "Courier New",
        ],
        OperatingSystem::IOS => &[
            // System UI fonts (filenames use SFNS/SFUI prefix)
            "San Francisco",
            "SFNS",
            "SFNSDisplay",
            "SFNSText",
            "SFUI",
            ".AppleSystemUIFont",
            "System Font",
            // Sans-serif
            "Helvetica Neue",
            "Helvetica",
            "Avenir",
            "Avenir Next",
            // Serif
            "Times New Roman",
            "Georgia",
            // Monospace
            "Menlo",
            "SF Mono",
            "Courier",
        ],
        OperatingSystem::Android => &[
            // System UI fonts
            "Roboto",
            "Roboto Flex",
            "Roboto Condensed",
            // Sans-serif
            "Noto Sans",
            "Droid Sans",
            // Serif
            "Noto Serif",
            "Roboto Serif",
            "Droid Serif",
            // Monospace
            "Roboto Mono",
            "Droid Sans Mono",
            "Noto Sans Mono",
        ],
        OperatingSystem::Wasm => &[],
    }
}

/// Configuration for the font scanner: directories to search and families to prioritize.
/// Injected by the embedder via `FcFontRegistry::new_with_config`.
#[derive(Debug, Clone, PartialEq)]
pub struct FcScanConfig {
    /// Directories to scan recursively. Empty = scan nothing.
    pub font_dirs: Vec<PathBuf>,
    /// Human-readable family names whose files the scout parses first
    /// (they become token sets via [`tokenize_lowercase`]).
    pub priority_families: Vec<String>,
}

impl FcScanConfig {
    /// Returns the default OS-specific scan configuration.
    pub fn os_defaults(os: OperatingSystem) -> Self {
        Self {
            font_dirs: font_directories(os),
            priority_families: FcFallbackConfig::os_defaults(os).extract_all_families(),
        }
    }
    /// Returns an empty scan configuration.
    pub fn empty() -> Self {
        Self {
            font_dirs: Vec::new(),
            priority_families: Vec::new(),
        }
    }
    /// Pre-tokenizes priority families for faster matching against filenames.
    pub fn priority_token_sets(&self) -> Vec<Vec<String>> {
        self.priority_families
            .iter()
            .map(|family| tokenize_lowercase(family))
            .collect()
    }
}

/// Pre-tokenize common font families for efficient per-file matching.
/// Call this once before iterating over font files, then pass the result
/// to [`matches_common_family_tokens`] for each file.
pub fn tokenize_common_families(os: OperatingSystem) -> Vec<Vec<String>> {
    FcScanConfig::os_defaults(os).priority_token_sets()
}

/// Check if a set of filename tokens matches any pre-tokenized common family.
///
/// Both sides are joined into a single normalized string (tokens concatenated),
/// then checked for substring containment. This handles cases where the tokenizer
/// produces different splits for the same underlying name (e.g. `"SFMono"` stays
/// as one token from a filename, but `"SF Mono"` splits into `["sf", "mono"]`).
pub fn matches_common_family_tokens(
    file_tokens: &[String],
    common_token_sets: &[Vec<String>],
) -> bool {
    let file_joined: String = file_tokens.concat();
    common_token_sets.iter().any(|family_tokens| {
        let family_joined: String = family_tokens.concat();
        file_joined.contains(&family_joined)
    })
}

/// Tokenize a name into lowercase tokens (no style filtering).
///
/// Useful for priority scoring where style tokens like "Bold" are still relevant.
pub fn tokenize_lowercase(name: &str) -> Vec<String> {
    FcFontCache::extract_font_name_tokens(name)
        .into_iter()
        .map(|t| t.to_lowercase())
        .collect()
}

/// Extract non-style tokens from a font filename stem.
///
/// Tokenizes using CamelCase boundaries, hyphens, underscores, and spaces,
/// then filters out style tokens (Bold, Italic, Regular, etc.).
/// Returns lowercased tokens suitable for family name matching.
///
/// # Examples
///
/// - `"ArialBold"` → `["arial"]`
/// - `"NotoSansJP-Regular"` → `["noto", "sans", "jp"]`
/// - `"HelveticaNeue-BoldItalic"` → `["helvetica", "neue"]`
pub fn tokenize_font_stem(stem: &str) -> Vec<String> {
    tokenize_lowercase(stem)
        .into_iter()
        .filter(|t| !FONT_STYLE_TOKENS.iter().any(|s| s.eq_ignore_ascii_case(t)))
        .collect()
}

/// Guess the font family name from a filename, using tokenization.
///
/// Extracts non-style tokens from the filename stem and joins them
/// into a single normalized string (lowercase, no separators).
///
/// # Examples
///
/// - `"ArialBold.ttf"` → `"arial"`
/// - `"NotoSansJP-Regular.otf"` → `"notosansjp"`
/// - `"Helvetica Neue Bold Italic.ttf"` → `"helveticaneue"`
pub fn guess_family_from_filename(path: &Path) -> String {
    let stem = path.file_stem().and_then(|s| s.to_str()).unwrap_or("");
    tokenize_font_stem(stem).join("")
}

#[cfg(test)]
#[path = "config_test.rs"]
mod tests;