rust-fontconfig 4.2.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
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
//! # rust-fontconfig
//!
//! Pure-Rust rewrite of the Linux fontconfig library (no system dependencies) - using allsorts as a font parser to support `.woff`, `.woff2`, `.ttc`, `.otf` and `.ttf`
//!
//! **NOTE**: Also works on Windows, macOS and WASM - without external dependencies!
//!
//! ## Usage
//!
//! ### Basic Font Query
//!
//! ```rust,no_run
//! use rust_fontconfig::{FcFontCache, FcPattern};
//!
//! fn main() {
//!     // Build the font cache
//!     let cache = FcFontCache::build();
//!
//!     // Query a font by name
//!     let results = cache.query(
//!         &FcPattern {
//!             name: Some(String::from("Arial")),
//!             ..Default::default()
//!         },
//!         &mut Vec::new() // Trace messages container
//!     );
//!
//!     if let Some(font_match) = results {
//!         println!("Font match ID: {:?}", font_match.id);
//!         println!("Font unicode ranges: {:?}", font_match.unicode_ranges);
//!     } else {
//!         println!("No matching font found");
//!     }
//! }
//! ```
//!
//! ### Resolve Font Chain and Query for Text
//!
//! ```rust,no_run
//! use rust_fontconfig::{FcFontCache, FcWeight, PatternMatch};
//!
//! fn main() {
//!     # #[cfg(feature = "std")]
//!     # {
//!     let cache = FcFontCache::build();
//!
//!     // Build font fallback chain (without text parameter)
//!     let font_chain = cache.resolve_font_chain(
//!         &["Arial".to_string(), "sans-serif".to_string()],
//!         FcWeight::Normal,
//!         PatternMatch::DontCare,
//!         PatternMatch::DontCare,
//!         &mut Vec::new(),
//!     );
//!
//!     // Query which fonts to use for specific text
//!     let text = "Hello 你好 Здравствуйте";
//!     let font_runs = font_chain.query_for_text(&cache, text);
//!
//!     println!("Text split into {} font runs:", font_runs.len());
//!     for run in font_runs {
//!         println!("  '{}' -> font {:?}", run.text, run.font_id);
//!     }
//!     # }
//! }
//! ```

#![allow(non_snake_case)]

// As of v4.1 this crate is std-only. The v4.0 `no_std` path is gone —
// it never supported the registry / multi-thread parsing anyway, and
// the shared-state `FcFontCache` refactor depends on `std::sync::RwLock`
// which is unavailable without std. Keeping the `alloc::` import paths
// means the existing call sites in this file and submodules keep
// compiling — in std builds `alloc` is just `core::alloc`'s companion
// crate already linked by the standard library.
extern crate alloc;

use alloc::collections::btree_map::BTreeMap;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
#[cfg(all(feature = "std", feature = "parsing"))]
use allsorts::binary::read::ReadScope;
#[cfg(all(feature = "std", feature = "parsing"))]
use allsorts::get_name::fontcode_get_name;
#[cfg(all(feature = "std", feature = "parsing"))]
use allsorts::tables::os2::Os2;
#[cfg(all(feature = "std", feature = "parsing"))]
use allsorts::tables::{FontTableProvider, HheaTable, HmtxTable, MaxpTable};
#[cfg(all(feature = "std", feature = "parsing"))]
use allsorts::tag;
#[cfg(feature = "std")]
use std::path::PathBuf;

pub mod utils;
#[cfg(feature = "std")]
pub mod config;

#[cfg(feature = "ffi")]
pub mod ffi;

#[cfg(feature = "async-registry")]
pub mod scoring;
#[cfg(feature = "async-registry")]
pub mod registry;
#[cfg(feature = "async-registry")]
pub mod multithread;
#[cfg(feature = "cache")]
pub mod disk_cache;

/// Operating system type for generic font family resolution
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum OperatingSystem {
    Windows,
    Linux,
    MacOS,
    Wasm,
}

impl OperatingSystem {
    /// Detect the current operating system at compile time
    pub fn current() -> Self {
        #[cfg(target_os = "windows")]
        return OperatingSystem::Windows;
        
        #[cfg(target_os = "linux")]
        return OperatingSystem::Linux;
        
        #[cfg(target_os = "macos")]
        return OperatingSystem::MacOS;
        
        #[cfg(target_family = "wasm")]
        return OperatingSystem::Wasm;
        
        #[cfg(not(any(target_os = "windows", target_os = "linux", target_os = "macos", target_family = "wasm")))]
        return OperatingSystem::Linux; // Default fallback
    }
    
    /// Get system-specific fonts for the "serif" generic family
    /// Prioritizes fonts based on Unicode range coverage
    pub fn get_serif_fonts(&self, unicode_ranges: &[UnicodeRange]) -> Vec<String> {
        let has_cjk = has_cjk_ranges(unicode_ranges);
        let has_arabic = has_arabic_ranges(unicode_ranges);
        let _has_cyrillic = has_cyrillic_ranges(unicode_ranges);
        
        match self {
            OperatingSystem::Windows => {
                let mut fonts = Vec::new();
                if has_cjk {
                    fonts.extend_from_slice(&["MS Mincho", "SimSun", "MingLiU"]);
                }
                if has_arabic {
                    fonts.push("Traditional Arabic");
                }
                fonts.push("Times New Roman");
                fonts.iter().map(|s| s.to_string()).collect()
            }
            OperatingSystem::Linux => {
                let mut fonts = Vec::new();
                if has_cjk {
                    fonts.extend_from_slice(&["Noto Serif CJK SC", "Noto Serif CJK JP", "Noto Serif CJK KR"]);
                }
                if has_arabic {
                    fonts.push("Noto Serif Arabic");
                }
                fonts.extend_from_slice(&[
                    "Times", "Times New Roman", "DejaVu Serif", "Free Serif", 
                    "Noto Serif", "Bitstream Vera Serif", "Roman", "Regular"
                ]);
                fonts.iter().map(|s| s.to_string()).collect()
            }
            OperatingSystem::MacOS => {
                let mut fonts = Vec::new();
                if has_cjk {
                    fonts.extend_from_slice(&["Hiragino Mincho ProN", "STSong", "AppleMyungjo"]);
                }
                if has_arabic {
                    fonts.push("Geeza Pro");
                }
                fonts.extend_from_slice(&["Times", "New York", "Palatino"]);
                fonts.iter().map(|s| s.to_string()).collect()
            }
            OperatingSystem::Wasm => Vec::new(),
        }
    }
    
    /// Get system-specific fonts for the "sans-serif" generic family
    /// Prioritizes fonts based on Unicode range coverage
    pub fn get_sans_serif_fonts(&self, unicode_ranges: &[UnicodeRange]) -> Vec<String> {
        let has_cjk = has_cjk_ranges(unicode_ranges);
        let has_arabic = has_arabic_ranges(unicode_ranges);
        let _has_cyrillic = has_cyrillic_ranges(unicode_ranges);
        let has_hebrew = has_hebrew_ranges(unicode_ranges);
        let has_thai = has_thai_ranges(unicode_ranges);
        
        match self {
            OperatingSystem::Windows => {
                let mut fonts = Vec::new();
                if has_cjk {
                    fonts.extend_from_slice(&["Microsoft YaHei", "MS Gothic", "Malgun Gothic", "SimHei"]);
                }
                if has_arabic {
                    fonts.push("Segoe UI Arabic");
                }
                if has_hebrew {
                    fonts.push("Segoe UI Hebrew");
                }
                if has_thai {
                    fonts.push("Leelawadee UI");
                }
                fonts.extend_from_slice(&["Segoe UI", "Tahoma", "Microsoft Sans Serif", "MS Sans Serif", "Helv"]);
                fonts.iter().map(|s| s.to_string()).collect()
            }
            OperatingSystem::Linux => {
                let mut fonts = Vec::new();
                if has_cjk {
                    fonts.extend_from_slice(&[
                        "Noto Sans CJK SC", "Noto Sans CJK JP", "Noto Sans CJK KR",
                        "WenQuanYi Micro Hei", "Droid Sans Fallback"
                    ]);
                }
                if has_arabic {
                    fonts.push("Noto Sans Arabic");
                }
                if has_hebrew {
                    fonts.push("Noto Sans Hebrew");
                }
                if has_thai {
                    fonts.push("Noto Sans Thai");
                }
                fonts.extend_from_slice(&["Ubuntu", "Arial", "DejaVu Sans", "Noto Sans", "Liberation Sans"]);
                fonts.iter().map(|s| s.to_string()).collect()
            }
            OperatingSystem::MacOS => {
                let mut fonts = Vec::new();
                if has_cjk {
                    fonts.extend_from_slice(&[
                        "Hiragino Sans", "Hiragino Kaku Gothic ProN", 
                        "PingFang SC", "PingFang TC", "Apple SD Gothic Neo"
                    ]);
                }
                if has_arabic {
                    fonts.push("Geeza Pro");
                }
                if has_hebrew {
                    fonts.push("Arial Hebrew");
                }
                if has_thai {
                    fonts.push("Thonburi");
                }
                fonts.extend_from_slice(&["San Francisco", "Helvetica Neue", "Lucida Grande"]);
                fonts.iter().map(|s| s.to_string()).collect()
            }
            OperatingSystem::Wasm => Vec::new(),
        }
    }
    
    /// Get system-specific fonts for the "monospace" generic family
    /// Prioritizes fonts based on Unicode range coverage
    pub fn get_monospace_fonts(&self, unicode_ranges: &[UnicodeRange]) -> Vec<String> {
        let has_cjk = has_cjk_ranges(unicode_ranges);
        
        match self {
            OperatingSystem::Windows => {
                let mut fonts = Vec::new();
                if has_cjk {
                    fonts.extend_from_slice(&["MS Gothic", "SimHei"]);
                }
                fonts.extend_from_slice(&["Segoe UI Mono", "Courier New", "Cascadia Code", "Cascadia Mono", "Consolas"]);
                fonts.iter().map(|s| s.to_string()).collect()
            }
            OperatingSystem::Linux => {
                let mut fonts = Vec::new();
                if has_cjk {
                    fonts.extend_from_slice(&["Noto Sans Mono CJK SC", "Noto Sans Mono CJK JP", "WenQuanYi Zen Hei Mono"]);
                }
                fonts.extend_from_slice(&[
                    "Source Code Pro", "Cantarell", "DejaVu Sans Mono", 
                    "Roboto Mono", "Ubuntu Monospace", "Droid Sans Mono"
                ]);
                fonts.iter().map(|s| s.to_string()).collect()
            }
            OperatingSystem::MacOS => {
                let mut fonts = Vec::new();
                if has_cjk {
                    fonts.extend_from_slice(&["Hiragino Sans", "PingFang SC"]);
                }
                fonts.extend_from_slice(&["SF Mono", "Menlo", "Monaco", "Courier", "Oxygen Mono", "Source Code Pro", "Fira Mono"]);
                fonts.iter().map(|s| s.to_string()).collect()
            }
            OperatingSystem::Wasm => Vec::new(),
        }
    }
    
    /// Expand a generic CSS font family to system-specific font names
    /// Returns the original name if not a generic family
    /// Prioritizes fonts based on Unicode range coverage
    pub fn expand_generic_family(&self, family: &str, unicode_ranges: &[UnicodeRange]) -> Vec<String> {
        match family.to_lowercase().as_str() {
            "serif" => self.get_serif_fonts(unicode_ranges),
            "sans-serif" => self.get_sans_serif_fonts(unicode_ranges),
            "monospace" => self.get_monospace_fonts(unicode_ranges),
            "cursive" | "fantasy" | "system-ui" => {
                // Use sans-serif as fallback for these
                self.get_sans_serif_fonts(unicode_ranges)
            }
            _ => vec![family.to_string()],
        }
    }
}

/// Expand a CSS font-family stack with generic families resolved to OS-specific fonts
/// Prioritizes fonts based on Unicode range coverage
/// Example: ["Arial", "sans-serif"] on macOS with CJK ranges -> ["Arial", "PingFang SC", "Hiragino Sans", ...]
pub fn expand_font_families(families: &[String], os: OperatingSystem, unicode_ranges: &[UnicodeRange]) -> Vec<String> {
    let mut expanded = Vec::new();
    
    for family in families {
        expanded.extend(os.expand_generic_family(family, unicode_ranges));
    }
    
    expanded
}

/// UUID to identify a font (collections are broken up into separate fonts)
#[derive(Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "cache", derive(serde::Serialize, serde::Deserialize))]
pub struct FontId(pub u128);

impl core::fmt::Debug for FontId {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        core::fmt::Display::fmt(self, f)
    }
}

impl core::fmt::Display for FontId {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let id = self.0;
        write!(
            f,
            "{:08x}-{:04x}-{:04x}-{:04x}-{:012x}",
            (id >> 96) & 0xFFFFFFFF,
            (id >> 80) & 0xFFFF,
            (id >> 64) & 0xFFFF,
            (id >> 48) & 0xFFFF,
            id & 0xFFFFFFFFFFFF
        )
    }
}

impl FontId {
    /// Generate a new unique FontId using an atomic counter
    pub fn new() -> Self {
        use core::sync::atomic::{AtomicU64, Ordering};
        static COUNTER: AtomicU64 = AtomicU64::new(1);
        let id = COUNTER.fetch_add(1, Ordering::Relaxed) as u128;
        FontId(id)
    }
}

/// Whether a field is required to match (yes / no / don't care)
#[derive(Debug, Default, Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "cache", derive(serde::Serialize, serde::Deserialize))]
#[repr(C)]
pub enum PatternMatch {
    /// Default: don't particularly care whether the requirement matches
    #[default]
    DontCare,
    /// Requirement has to be true for the selected font
    True,
    /// Requirement has to be false for the selected font
    False,
}

impl PatternMatch {
    fn needs_to_match(&self) -> bool {
        matches!(self, PatternMatch::True | PatternMatch::False)
    }

    fn matches(&self, other: &PatternMatch) -> bool {
        match (self, other) {
            (PatternMatch::DontCare, _) => true,
            (_, PatternMatch::DontCare) => true,
            (a, b) => a == b,
        }
    }
}

/// Font weight values as defined in CSS specification
#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "cache", derive(serde::Serialize, serde::Deserialize))]
#[repr(C)]
pub enum FcWeight {
    Thin = 100,
    ExtraLight = 200,
    Light = 300,
    Normal = 400,
    Medium = 500,
    SemiBold = 600,
    Bold = 700,
    ExtraBold = 800,
    Black = 900,
}

impl FcWeight {
    pub fn from_u16(weight: u16) -> Self {
        match weight {
            0..=149 => FcWeight::Thin,
            150..=249 => FcWeight::ExtraLight,
            250..=349 => FcWeight::Light,
            350..=449 => FcWeight::Normal,
            450..=549 => FcWeight::Medium,
            550..=649 => FcWeight::SemiBold,
            650..=749 => FcWeight::Bold,
            750..=849 => FcWeight::ExtraBold,
            _ => FcWeight::Black,
        }
    }

    pub fn find_best_match(&self, available: &[FcWeight]) -> Option<FcWeight> {
        if available.is_empty() {
            return None;
        }

        // Exact match
        if available.contains(self) {
            return Some(*self);
        }

        // Get numeric value
        let self_value = *self as u16;

        match *self {
            FcWeight::Normal => {
                // For Normal (400), try Medium (500) first
                if available.contains(&FcWeight::Medium) {
                    return Some(FcWeight::Medium);
                }
                // Then try lighter weights
                for weight in &[FcWeight::Light, FcWeight::ExtraLight, FcWeight::Thin] {
                    if available.contains(weight) {
                        return Some(*weight);
                    }
                }
                // Last, try heavier weights
                for weight in &[
                    FcWeight::SemiBold,
                    FcWeight::Bold,
                    FcWeight::ExtraBold,
                    FcWeight::Black,
                ] {
                    if available.contains(weight) {
                        return Some(*weight);
                    }
                }
            }
            FcWeight::Medium => {
                // For Medium (500), try Normal (400) first
                if available.contains(&FcWeight::Normal) {
                    return Some(FcWeight::Normal);
                }
                // Then try lighter weights
                for weight in &[FcWeight::Light, FcWeight::ExtraLight, FcWeight::Thin] {
                    if available.contains(weight) {
                        return Some(*weight);
                    }
                }
                // Last, try heavier weights
                for weight in &[
                    FcWeight::SemiBold,
                    FcWeight::Bold,
                    FcWeight::ExtraBold,
                    FcWeight::Black,
                ] {
                    if available.contains(weight) {
                        return Some(*weight);
                    }
                }
            }
            FcWeight::Thin | FcWeight::ExtraLight | FcWeight::Light => {
                // For lightweight fonts (<400), first try lighter or equal weights
                let mut best_match = None;
                let mut smallest_diff = u16::MAX;

                // Find the closest lighter weight
                for weight in available {
                    let weight_value = *weight as u16;
                    // Only consider weights <= self (per test expectation)
                    if weight_value <= self_value {
                        let diff = self_value - weight_value;
                        if diff < smallest_diff {
                            smallest_diff = diff;
                            best_match = Some(*weight);
                        }
                    }
                }

                if best_match.is_some() {
                    return best_match;
                }

                // If no lighter weight, find the closest heavier weight
                best_match = None;
                smallest_diff = u16::MAX;

                for weight in available {
                    let weight_value = *weight as u16;
                    if weight_value > self_value {
                        let diff = weight_value - self_value;
                        if diff < smallest_diff {
                            smallest_diff = diff;
                            best_match = Some(*weight);
                        }
                    }
                }

                return best_match;
            }
            FcWeight::SemiBold | FcWeight::Bold | FcWeight::ExtraBold | FcWeight::Black => {
                // For heavyweight fonts (>500), first try heavier or equal weights
                let mut best_match = None;
                let mut smallest_diff = u16::MAX;

                // Find the closest heavier weight
                for weight in available {
                    let weight_value = *weight as u16;
                    // Only consider weights >= self
                    if weight_value >= self_value {
                        let diff = weight_value - self_value;
                        if diff < smallest_diff {
                            smallest_diff = diff;
                            best_match = Some(*weight);
                        }
                    }
                }

                if best_match.is_some() {
                    return best_match;
                }

                // If no heavier weight, find the closest lighter weight
                best_match = None;
                smallest_diff = u16::MAX;

                for weight in available {
                    let weight_value = *weight as u16;
                    if weight_value < self_value {
                        let diff = self_value - weight_value;
                        if diff < smallest_diff {
                            smallest_diff = diff;
                            best_match = Some(*weight);
                        }
                    }
                }

                return best_match;
            }
        }

        // If nothing matches by now, return the first available weight
        Some(available[0])
    }
}

impl Default for FcWeight {
    fn default() -> Self {
        FcWeight::Normal
    }
}

/// CSS font-stretch values
#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "cache", derive(serde::Serialize, serde::Deserialize))]
#[repr(C)]
pub enum FcStretch {
    UltraCondensed = 1,
    ExtraCondensed = 2,
    Condensed = 3,
    SemiCondensed = 4,
    Normal = 5,
    SemiExpanded = 6,
    Expanded = 7,
    ExtraExpanded = 8,
    UltraExpanded = 9,
}

impl FcStretch {
    pub fn is_condensed(&self) -> bool {
        use self::FcStretch::*;
        match self {
            UltraCondensed => true,
            ExtraCondensed => true,
            Condensed => true,
            SemiCondensed => true,
            Normal => false,
            SemiExpanded => false,
            Expanded => false,
            ExtraExpanded => false,
            UltraExpanded => false,
        }
    }
    pub fn from_u16(width_class: u16) -> Self {
        match width_class {
            1 => FcStretch::UltraCondensed,
            2 => FcStretch::ExtraCondensed,
            3 => FcStretch::Condensed,
            4 => FcStretch::SemiCondensed,
            5 => FcStretch::Normal,
            6 => FcStretch::SemiExpanded,
            7 => FcStretch::Expanded,
            8 => FcStretch::ExtraExpanded,
            9 => FcStretch::UltraExpanded,
            _ => FcStretch::Normal,
        }
    }

    /// Follows CSS spec for stretch matching
    pub fn find_best_match(&self, available: &[FcStretch]) -> Option<FcStretch> {
        if available.is_empty() {
            return None;
        }

        if available.contains(self) {
            return Some(*self);
        }

        // For 'normal' or condensed values, narrower widths are checked first, then wider values
        if *self <= FcStretch::Normal {
            // Find narrower values first
            let mut closest_narrower = None;
            for stretch in available.iter() {
                if *stretch < *self
                    && (closest_narrower.is_none() || *stretch > closest_narrower.unwrap())
                {
                    closest_narrower = Some(*stretch);
                }
            }

            if closest_narrower.is_some() {
                return closest_narrower;
            }

            // Otherwise, find wider values
            let mut closest_wider = None;
            for stretch in available.iter() {
                if *stretch > *self
                    && (closest_wider.is_none() || *stretch < closest_wider.unwrap())
                {
                    closest_wider = Some(*stretch);
                }
            }

            return closest_wider;
        } else {
            // For expanded values, wider values are checked first, then narrower values
            let mut closest_wider = None;
            for stretch in available.iter() {
                if *stretch > *self
                    && (closest_wider.is_none() || *stretch < closest_wider.unwrap())
                {
                    closest_wider = Some(*stretch);
                }
            }

            if closest_wider.is_some() {
                return closest_wider;
            }

            // Otherwise, find narrower values
            let mut closest_narrower = None;
            for stretch in available.iter() {
                if *stretch < *self
                    && (closest_narrower.is_none() || *stretch > closest_narrower.unwrap())
                {
                    closest_narrower = Some(*stretch);
                }
            }

            return closest_narrower;
        }
    }
}

impl Default for FcStretch {
    fn default() -> Self {
        FcStretch::Normal
    }
}

/// Unicode range representation for font matching
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "cache", derive(serde::Serialize, serde::Deserialize))]
pub struct UnicodeRange {
    pub start: u32,
    pub end: u32,
}

/// The default set of Unicode-block fallback scripts that
/// [`FcFontCache::resolve_font_chain`] pulls in when no explicit
/// `scripts_hint` is supplied.
///
/// Keeping this exposed lets callers that *do* want the default
/// behaviour build the set explicitly — typically by union-ing it
/// with a detected-from-document set before calling
/// [`FcFontCache::resolve_font_chain_with_scripts`].
pub const DEFAULT_UNICODE_FALLBACK_SCRIPTS: &[UnicodeRange] = &[
    UnicodeRange { start: 0x0400, end: 0x04FF }, // Cyrillic
    UnicodeRange { start: 0x0600, end: 0x06FF }, // Arabic
    UnicodeRange { start: 0x0900, end: 0x097F }, // Devanagari
    UnicodeRange { start: 0x3040, end: 0x309F }, // Hiragana
    UnicodeRange { start: 0x30A0, end: 0x30FF }, // Katakana
    UnicodeRange { start: 0x4E00, end: 0x9FFF }, // CJK Unified Ideographs
    UnicodeRange { start: 0xAC00, end: 0xD7A3 }, // Hangul Syllables
];

impl UnicodeRange {
    pub fn contains(&self, c: char) -> bool {
        let c = c as u32;
        c >= self.start && c <= self.end
    }

    pub fn overlaps(&self, other: &UnicodeRange) -> bool {
        self.start <= other.end && other.start <= self.end
    }

    pub fn is_subset_of(&self, other: &UnicodeRange) -> bool {
        self.start >= other.start && self.end <= other.end
    }
}

/// Check if any range covers CJK Unified Ideographs, Hiragana, Katakana, or Hangul
pub fn has_cjk_ranges(ranges: &[UnicodeRange]) -> bool {
    ranges.iter().any(|r| {
        (r.start >= 0x4E00 && r.start <= 0x9FFF) ||
        (r.start >= 0x3040 && r.start <= 0x309F) ||
        (r.start >= 0x30A0 && r.start <= 0x30FF) ||
        (r.start >= 0xAC00 && r.start <= 0xD7AF)
    })
}

/// Check if any range covers the Arabic block
pub fn has_arabic_ranges(ranges: &[UnicodeRange]) -> bool {
    ranges.iter().any(|r| r.start >= 0x0600 && r.start <= 0x06FF)
}

/// Check if any range covers the Cyrillic block
pub fn has_cyrillic_ranges(ranges: &[UnicodeRange]) -> bool {
    ranges.iter().any(|r| r.start >= 0x0400 && r.start <= 0x04FF)
}

/// Check if any range covers the Hebrew block
pub fn has_hebrew_ranges(ranges: &[UnicodeRange]) -> bool {
    ranges.iter().any(|r| r.start >= 0x0590 && r.start <= 0x05FF)
}

/// Check if any range covers the Thai block
pub fn has_thai_ranges(ranges: &[UnicodeRange]) -> bool {
    ranges.iter().any(|r| r.start >= 0x0E00 && r.start <= 0x0E7F)
}

/// Log levels for trace messages
#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub enum TraceLevel {
    Debug,
    Info,
    Warning,
    Error,
}

/// Reason for font matching failure or success
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum MatchReason {
    NameMismatch {
        requested: Option<String>,
        found: Option<String>,
    },
    FamilyMismatch {
        requested: Option<String>,
        found: Option<String>,
    },
    StyleMismatch {
        property: &'static str,
        requested: String,
        found: String,
    },
    WeightMismatch {
        requested: FcWeight,
        found: FcWeight,
    },
    StretchMismatch {
        requested: FcStretch,
        found: FcStretch,
    },
    UnicodeRangeMismatch {
        character: char,
        ranges: Vec<UnicodeRange>,
    },
    Success,
}

/// Trace message for debugging font matching
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TraceMsg {
    pub level: TraceLevel,
    pub path: String,
    pub reason: MatchReason,
}

/// Hinting style for font rendering.
#[repr(C)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "cache", derive(serde::Serialize, serde::Deserialize))]
pub enum FcHintStyle {
    #[default]
    None = 0,
    Slight = 1,
    Medium = 2,
    Full = 3,
}

/// Subpixel rendering order.
#[repr(C)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "cache", derive(serde::Serialize, serde::Deserialize))]
pub enum FcRgba {
    #[default]
    Unknown = 0,
    Rgb = 1,
    Bgr = 2,
    Vrgb = 3,
    Vbgr = 4,
    None = 5,
}

/// LCD filter mode for subpixel rendering.
#[repr(C)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "cache", derive(serde::Serialize, serde::Deserialize))]
pub enum FcLcdFilter {
    #[default]
    None = 0,
    Default = 1,
    Light = 2,
    Legacy = 3,
}

/// Per-font rendering configuration from system font config (Linux fonts.conf).
///
/// All fields are `Option<T>` -- `None` means "use system default".
/// On non-Linux platforms, this is always all-None (no per-font overrides).
#[derive(Debug, Default, Clone, PartialEq, PartialOrd)]
#[cfg_attr(feature = "cache", derive(serde::Serialize, serde::Deserialize))]
pub struct FcFontRenderConfig {
    pub antialias: Option<bool>,
    pub hinting: Option<bool>,
    pub hintstyle: Option<FcHintStyle>,
    pub autohint: Option<bool>,
    pub rgba: Option<FcRgba>,
    pub lcdfilter: Option<FcLcdFilter>,
    pub embeddedbitmap: Option<bool>,
    pub embolden: Option<bool>,
    pub dpi: Option<f64>,
    pub scale: Option<f64>,
    pub minspace: Option<bool>,
}

/// Helper newtype to provide Eq/Ord for Option<f64> via total-order bit comparison.
/// This allows FcFontRenderConfig to be used inside FcPattern which derives Eq + Ord.
impl Eq for FcFontRenderConfig {}

impl Ord for FcFontRenderConfig {
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        // Compare all non-f64 fields first
        let ord = self.antialias.cmp(&other.antialias)
            .then_with(|| self.hinting.cmp(&other.hinting))
            .then_with(|| self.hintstyle.cmp(&other.hintstyle))
            .then_with(|| self.autohint.cmp(&other.autohint))
            .then_with(|| self.rgba.cmp(&other.rgba))
            .then_with(|| self.lcdfilter.cmp(&other.lcdfilter))
            .then_with(|| self.embeddedbitmap.cmp(&other.embeddedbitmap))
            .then_with(|| self.embolden.cmp(&other.embolden))
            .then_with(|| self.minspace.cmp(&other.minspace));

        // For f64 fields, use to_bits() for total ordering
        let ord = ord.then_with(|| {
            let a = self.dpi.map(|v| v.to_bits());
            let b = other.dpi.map(|v| v.to_bits());
            a.cmp(&b)
        });
        ord.then_with(|| {
            let a = self.scale.map(|v| v.to_bits());
            let b = other.scale.map(|v| v.to_bits());
            a.cmp(&b)
        })
    }
}

/// Font pattern for matching
#[derive(Default, Clone, PartialOrd, Ord, PartialEq, Eq)]
#[cfg_attr(feature = "cache", derive(serde::Serialize, serde::Deserialize))]
#[repr(C)]
pub struct FcPattern {
    // font name
    pub name: Option<String>,
    // family name
    pub family: Option<String>,
    // "italic" property
    pub italic: PatternMatch,
    // "oblique" property
    pub oblique: PatternMatch,
    // "bold" property
    pub bold: PatternMatch,
    // "monospace" property
    pub monospace: PatternMatch,
    // "condensed" property
    pub condensed: PatternMatch,
    // font weight
    pub weight: FcWeight,
    // font stretch
    pub stretch: FcStretch,
    // unicode ranges to match
    pub unicode_ranges: Vec<UnicodeRange>,
    // extended font metadata
    pub metadata: FcFontMetadata,
    // per-font rendering configuration (from system fonts.conf on Linux)
    pub render_config: FcFontRenderConfig,
}

impl core::fmt::Debug for FcPattern {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let mut d = f.debug_struct("FcPattern");

        if let Some(name) = &self.name {
            d.field("name", name);
        }

        if let Some(family) = &self.family {
            d.field("family", family);
        }

        if self.italic != PatternMatch::DontCare {
            d.field("italic", &self.italic);
        }

        if self.oblique != PatternMatch::DontCare {
            d.field("oblique", &self.oblique);
        }

        if self.bold != PatternMatch::DontCare {
            d.field("bold", &self.bold);
        }

        if self.monospace != PatternMatch::DontCare {
            d.field("monospace", &self.monospace);
        }

        if self.condensed != PatternMatch::DontCare {
            d.field("condensed", &self.condensed);
        }

        if self.weight != FcWeight::Normal {
            d.field("weight", &self.weight);
        }

        if self.stretch != FcStretch::Normal {
            d.field("stretch", &self.stretch);
        }

        if !self.unicode_ranges.is_empty() {
            d.field("unicode_ranges", &self.unicode_ranges);
        }

        // Only show non-empty metadata fields
        let empty_metadata = FcFontMetadata::default();
        if self.metadata != empty_metadata {
            d.field("metadata", &self.metadata);
        }

        // Only show render_config when it differs from default
        let empty_render_config = FcFontRenderConfig::default();
        if self.render_config != empty_render_config {
            d.field("render_config", &self.render_config);
        }

        d.finish()
    }
}

/// Font metadata from the OS/2 table
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "cache", derive(serde::Serialize, serde::Deserialize))]
pub struct FcFontMetadata {
    pub copyright: Option<String>,
    pub designer: Option<String>,
    pub designer_url: Option<String>,
    pub font_family: Option<String>,
    pub font_subfamily: Option<String>,
    pub full_name: Option<String>,
    pub id_description: Option<String>,
    pub license: Option<String>,
    pub license_url: Option<String>,
    pub manufacturer: Option<String>,
    pub manufacturer_url: Option<String>,
    pub postscript_name: Option<String>,
    pub preferred_family: Option<String>,
    pub preferred_subfamily: Option<String>,
    pub trademark: Option<String>,
    pub unique_id: Option<String>,
    pub version: Option<String>,
}

impl FcPattern {
    /// Check if this pattern would match the given character
    pub fn contains_char(&self, c: char) -> bool {
        if self.unicode_ranges.is_empty() {
            return true; // No ranges specified means match all characters
        }

        for range in &self.unicode_ranges {
            if range.contains(c) {
                return true;
            }
        }

        false
    }
}

/// Font match result with UUID
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FontMatch {
    pub id: FontId,
    pub unicode_ranges: Vec<UnicodeRange>,
    pub fallbacks: Vec<FontMatchNoFallback>,
}

/// Font match result with UUID (without fallback)
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FontMatchNoFallback {
    pub id: FontId,
    pub unicode_ranges: Vec<UnicodeRange>,
}

/// A run of text that uses the same font
/// Returned by FontFallbackChain::query_for_text()
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResolvedFontRun {
    /// The text content of this run
    pub text: String,
    /// Start byte index in the original text
    pub start_byte: usize,
    /// End byte index in the original text (exclusive)
    pub end_byte: usize,
    /// The font to use for this run (None if no font found)
    pub font_id: Option<FontId>,
    /// Which CSS font-family this came from
    pub css_source: String,
}

/// Resolved font fallback chain for a CSS font-family stack
/// This represents the complete chain of fonts to use for rendering text
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FontFallbackChain {
    /// CSS-based fallbacks: Each CSS font expanded to its system fallbacks
    /// Example: ["NotoSansJP" -> [Hiragino Sans, PingFang SC], "sans-serif" -> [Helvetica]]
    pub css_fallbacks: Vec<CssFallbackGroup>,
    
    /// Unicode-based fallbacks: Fonts added to cover missing Unicode ranges
    /// Only populated if css_fallbacks don't cover all requested characters
    pub unicode_fallbacks: Vec<FontMatch>,
    
    /// The original CSS font-family stack that was requested
    pub original_stack: Vec<String>,
}

impl FontFallbackChain {
    /// Resolve which font should be used for a specific character
    /// Returns (FontId, css_source_name) where css_source_name indicates which CSS font matched
    /// Returns None if no font in the chain can render this character
    pub fn resolve_char(&self, cache: &FcFontCache, ch: char) -> Option<(FontId, String)> {
        let codepoint = ch as u32;

        // Check CSS fallbacks in order
        for group in &self.css_fallbacks {
            for font in &group.fonts {
                let Some(meta) = cache.get_metadata_by_id(&font.id) else { continue };
                if meta.unicode_ranges.is_empty() {
                    continue; // No range info — don't assume it covers everything
                }
                if meta.unicode_ranges.iter().any(|r| codepoint >= r.start && codepoint <= r.end) {
                    return Some((font.id, group.css_name.clone()));
                }
            }
        }

        // Check Unicode fallbacks
        for font in &self.unicode_fallbacks {
            let Some(meta) = cache.get_metadata_by_id(&font.id) else { continue };
            if meta.unicode_ranges.iter().any(|r| codepoint >= r.start && codepoint <= r.end) {
                return Some((font.id, "(unicode-fallback)".to_string()));
            }
        }

        None
    }
    
    /// Resolve all characters in a text string to their fonts
    /// Returns a vector of (character, FontId, css_source) tuples
    pub fn resolve_text(&self, cache: &FcFontCache, text: &str) -> Vec<(char, Option<(FontId, String)>)> {
        text.chars()
            .map(|ch| (ch, self.resolve_char(cache, ch)))
            .collect()
    }
    
    /// Query which fonts should be used for a text string, grouped by font
    /// Returns runs of consecutive characters that use the same font
    /// This is the main API for text shaping - call this to get font runs, then shape each run
    pub fn query_for_text(&self, cache: &FcFontCache, text: &str) -> Vec<ResolvedFontRun> {
        if text.is_empty() {
            return Vec::new();
        }
        
        let mut runs: Vec<ResolvedFontRun> = Vec::new();
        let mut current_font: Option<FontId> = None;
        let mut current_css_source: Option<String> = None;
        let mut current_start_byte: usize = 0;
        
        for (byte_idx, ch) in text.char_indices() {
            let resolved = self.resolve_char(cache, ch);
            let (font_id, css_source) = match &resolved {
                Some((id, source)) => (Some(*id), Some(source.clone())),
                None => (None, None),
            };
            
            // Check if we need to start a new run
            let font_changed = font_id != current_font;
            
            if font_changed && byte_idx > 0 {
                // Finalize the current run
                let run_text = &text[current_start_byte..byte_idx];
                runs.push(ResolvedFontRun {
                    text: run_text.to_string(),
                    start_byte: current_start_byte,
                    end_byte: byte_idx,
                    font_id: current_font,
                    css_source: current_css_source.clone().unwrap_or_default(),
                });
                current_start_byte = byte_idx;
            }
            
            current_font = font_id;
            current_css_source = css_source;
        }
        
        // Finalize the last run
        if current_start_byte < text.len() {
            let run_text = &text[current_start_byte..];
            runs.push(ResolvedFontRun {
                text: run_text.to_string(),
                start_byte: current_start_byte,
                end_byte: text.len(),
                font_id: current_font,
                css_source: current_css_source.unwrap_or_default(),
            });
        }
        
        runs
    }
}

/// A group of fonts that are fallbacks for a single CSS font-family name
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CssFallbackGroup {
    /// The CSS font name (e.g., "NotoSansJP", "sans-serif")
    pub css_name: String,
    
    /// System fonts that match this CSS name
    /// First font in list is the best match
    pub fonts: Vec<FontMatch>,
}

/// Cache key for font fallback chain queries
///
/// IMPORTANT: This key intentionally does NOT include per-text unicode
/// ranges — fallback chains are cached by CSS properties only. Different
/// texts with the same CSS font-stack share the same chain.
///
/// `scripts_hint_hash` distinguishes *which set of Unicode-fallback
/// scripts* the caller asked for. `None` means "the default set of 7
/// major scripts" (Cyrillic/Arabic/Devanagari/Hiragana/Katakana/CJK/Hangul,
/// back-compat behaviour of `resolve_font_chain`). `Some(h)` is a
/// stable hash of a caller-supplied script list so an ASCII-only
/// query doesn't collide with a CJK-aware one.
#[cfg(feature = "std")]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) struct FontChainCacheKey {
    /// CSS font stack (expanded to OS-specific fonts)
    pub(crate) font_families: Vec<String>,
    /// Font weight
    pub(crate) weight: FcWeight,
    /// Font style flags
    pub(crate) italic: PatternMatch,
    pub(crate) oblique: PatternMatch,
    /// Hash of the caller-supplied script hint (or `None` for the default set).
    pub(crate) scripts_hint_hash: Option<u64>,
}

/// Hash a `scripts_hint` slice into a stable u64 for use as a
/// [`FontChainCacheKey`] component. Order-insensitive: we sort a
/// local copy before hashing so `[CJK, Arabic]` and `[Arabic, CJK]`
/// key into the same cache slot.
#[cfg(feature = "std")]
fn hash_scripts_hint(ranges: &[UnicodeRange]) -> u64 {
    let mut sorted: Vec<UnicodeRange> = ranges.to_vec();
    sorted.sort();
    let mut buf = Vec::with_capacity(sorted.len() * 8);
    for r in &sorted {
        buf.extend_from_slice(&r.start.to_le_bytes());
        buf.extend_from_slice(&r.end.to_le_bytes());
    }
    crate::utils::content_hash_u64(&buf)
}

/// Path to a font file
///
/// `bytes_hash` is a deterministic 64-bit hash of the file's full
/// byte contents (see [`crate::utils::content_hash_u64`]). All faces
/// of a given `.ttc` file share the same `bytes_hash`, and two
/// different paths pointing at the same file contents also do —
/// so the cache can share a single `Arc<[u8]>` across them via
/// [`FcFontCache::get_font_bytes`]. A value of `0` means "hash
/// not computed" (e.g. built from a filename-only scan, or loaded
/// from a legacy v1 disk cache); callers must treat `0` as opaque
/// and fall back to unshared reads.
#[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq)]
#[cfg_attr(feature = "cache", derive(serde::Serialize, serde::Deserialize))]
#[repr(C)]
pub struct FcFontPath {
    pub path: String,
    pub font_index: usize,
    /// 64-bit content hash of the file's bytes. 0 = not computed.
    #[cfg_attr(feature = "cache", serde(default))]
    pub bytes_hash: u64,
}

/// In-memory font data
#[derive(Debug, Clone, PartialEq, Eq)]
#[repr(C)]
pub struct FcFont {
    pub bytes: Vec<u8>,
    pub font_index: usize,
    pub id: String, // For identification in tests
}

/// Owned font-source descriptor, returned by
/// [`FcFontCache::get_font_by_id`].
///
/// In v4.0 this was a borrowed enum (`FontSource<'a>` with refs into
/// the pattern map). With v4.1's shared-state cache, the map lives
/// behind an `RwLock`, so returning a reference would require the
/// caller to hold a read guard for the full lifetime of the result —
/// which bleeds the locking strategy into every call site. The owned
/// variant clones the small `FcFont` / `FcFontPath` struct and
/// releases the lock immediately. Bytes/mmap are not cloned — those
/// go through `get_font_bytes` which hands out `Arc<FontBytes>`.
#[derive(Debug, Clone)]
pub enum OwnedFontSource {
    /// Font loaded from memory (small metadata + owned `Vec<u8>`).
    Memory(FcFont),
    /// Font loaded from disk.
    Disk(FcFontPath),
}

/// A handle to font bytes returned by [`FcFontCache::get_font_bytes`].
///
/// On disk, an `Mmap` is used so untouched pages don't count toward
/// process RSS. In-memory fonts (`FcFont`) come back as `Owned` since
/// they're already on the heap.
///
/// `FontBytes` derefs to `[u8]` and implements `AsRef<[u8]>`, so any
/// existing API that wants `&[u8]` (allsorts, ttf-parser, …) can
/// accept it without code changes.
///
/// Both variants are `Send + Sync` (mmaps and `Arc<[u8]>` are both
/// safe to share across threads).
#[cfg(feature = "std")]
pub enum FontBytes {
    /// Heap-owned bytes. Used for `FontSource::Memory` and as a
    /// fallback when mmap is unavailable.
    Owned(std::sync::Arc<[u8]>),
    /// File-backed mmap. Read-only; pages are demand-loaded by the
    /// kernel.
    Mmapped(mmapio::Mmap),
}

#[cfg(feature = "std")]
impl FontBytes {
    /// Borrow the underlying byte slice.
    #[inline]
    pub fn as_slice(&self) -> &[u8] {
        match self {
            FontBytes::Owned(arc) => arc,
            FontBytes::Mmapped(m) => &m[..],
        }
    }
}

#[cfg(feature = "std")]
impl core::ops::Deref for FontBytes {
    type Target = [u8];
    #[inline]
    fn deref(&self) -> &[u8] {
        self.as_slice()
    }
}

#[cfg(feature = "std")]
impl AsRef<[u8]> for FontBytes {
    #[inline]
    fn as_ref(&self) -> &[u8] {
        self.as_slice()
    }
}

#[cfg(feature = "std")]
impl core::fmt::Debug for FontBytes {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let kind = match self {
            FontBytes::Owned(_) => "Owned",
            FontBytes::Mmapped(_) => "Mmapped",
        };
        write!(f, "FontBytes::{}({} bytes)", kind, self.as_slice().len())
    }
}

/// Open a font file as an mmap-backed [`FontBytes`]. Falls back to a
/// heap read if mmap fails (e.g. the file is on a network share that
/// doesn't support mmap, or we're on a target without `std`-mmap).
#[cfg(feature = "std")]
fn open_font_bytes_mmap(path: &str) -> Option<std::sync::Arc<FontBytes>> {
    use std::fs::File;
    use std::sync::Arc;

    #[cfg(not(target_family = "wasm"))]
    {
        if let Ok(file) = File::open(path) {
            // Safety: `Mmap::map` requires that the file is not
            // mutated while mapped. For system fonts that's the
            // overwhelming common case; if a user replaces the file
            // we accept reading the snapshot we mapped earlier.
            if let Ok(mmap) = unsafe { mmapio::MmapOptions::new().map(&file) } {
                return Some(Arc::new(FontBytes::Mmapped(mmap)));
            }
        }
    }
    let bytes = std::fs::read(path).ok()?;
    Some(Arc::new(FontBytes::Owned(Arc::from(bytes))))
}

/// A named font to be added to the font cache from memory.
/// This is the primary way to supply custom fonts to the application.
#[derive(Debug, Clone)]
pub struct NamedFont {
    /// Human-readable name for this font (e.g., "My Custom Font")
    pub name: String,
    /// The raw font file bytes (TTF, OTF, WOFF, WOFF2, TTC)
    pub bytes: Vec<u8>,
}

impl NamedFont {
    /// Create a new named font from bytes
    pub fn new(name: impl Into<String>, bytes: Vec<u8>) -> Self {
        Self {
            name: name.into(),
            bytes,
        }
    }
}

/// Font cache, initialized at startup.
///
/// Thread-safe, shared font cache.
///
/// As of v4.1 the cache internally owns its state via
/// `Arc<RwLock<FcFontCacheInner>>`: cloning an `FcFontCache` returns
/// a handle that shares the same underlying data. Writes by one holder
/// (typically the background builder inside `FcFontRegistry`) become
/// immediately visible to every other holder (layout engines,
/// shape-time resolvers, etc.).
///
/// Before 4.1 the clone deep-copied every map, so external holders
/// were frozen at the moment they took the snapshot — the mismatch
/// between "live registry cache" and "frozen font manager cache"
/// was the root of the silent-text regression when lazy scout mode
/// was enabled. The shared-state design eliminates that entire class
/// of staleness bugs by construction.
pub struct FcFontCache {
    pub(crate) shared: std::sync::Arc<FcFontCacheShared>,
}

/// Shared interior of `FcFontCache`. Always accessed through an
/// `Arc` — never referenced directly by external callers.
pub(crate) struct FcFontCacheShared {
    /// Main pattern/metadata state, guarded by a reader-writer lock.
    /// Builder threads take the write lock to insert a parsed font;
    /// all query paths take the read lock.
    pub(crate) state: std::sync::RwLock<FcFontCacheInner>,
    /// Font fallback chain cache. Not part of the RwLock-guarded
    /// state because cache insertions happen under `&self` on read
    /// paths (they're a memoisation, not observable state).
    pub(crate) chain_cache: std::sync::Mutex<std::collections::HashMap<FontChainCacheKey, FontFallbackChain>>,
    /// Shared file-bytes cache: content-hash → weak [`FontBytes`].
    ///
    /// [`FcFontCache::get_font_bytes`] populates this so that multiple
    /// FontIds backed by the same file (e.g. every face of a `.ttc`)
    /// return the same `Arc<FontBytes>` — and therefore the same mmap
    /// — instead of each allocating their own buffer. We hold `Weak`
    /// references so the mmap unmap as soon as no parsed font holds
    /// it alive.
    pub(crate) shared_bytes: std::sync::Mutex<std::collections::HashMap<u64, std::sync::Weak<FontBytes>>>,
}

/// The actual font-pattern state, held behind the RwLock in
/// `FcFontCacheShared`. Private — all access goes through
/// `FcFontCache` methods which lock transparently.
#[derive(Default, Debug)]
pub(crate) struct FcFontCacheInner {
    /// Pattern to FontId mapping (query index)
    pub(crate) patterns: BTreeMap<FcPattern, FontId>,
    /// On-disk font paths
    pub(crate) disk_fonts: BTreeMap<FontId, FcFontPath>,
    /// In-memory fonts
    pub(crate) memory_fonts: BTreeMap<FontId, FcFont>,
    /// Metadata cache (patterns stored by ID for quick lookup)
    pub(crate) metadata: BTreeMap<FontId, FcPattern>,
    /// Token index: maps lowercase tokens ("noto", "sans", "jp") to sets of FontIds.
    /// Enables fast fuzzy search by intersecting token sets.
    pub(crate) token_index: BTreeMap<String, alloc::collections::BTreeSet<FontId>>,
    /// Pre-tokenized font names (lowercase): FontId -> Vec<lowercase tokens>.
    /// Avoids re-tokenization during fuzzy search.
    pub(crate) font_tokens: BTreeMap<FontId, Vec<String>>,
}

impl FcFontCacheInner {
    /// Add a font pattern to the token index. Called under the
    /// write lock by insertion paths.
    pub(crate) fn index_pattern_tokens(&mut self, pattern: &FcPattern, id: FontId) {
        // Extract tokens from both name and family
        let mut all_tokens = Vec::new();

        if let Some(name) = &pattern.name {
            all_tokens.extend(FcFontCache::extract_font_name_tokens(name));
        }

        if let Some(family) = &pattern.family {
            all_tokens.extend(FcFontCache::extract_font_name_tokens(family));
        }

        // Convert tokens to lowercase and store them
        let tokens_lower: Vec<String> =
            all_tokens.iter().map(|t| t.to_lowercase()).collect();

        // Add each token (lowercase) to the index
        for token_lower in &tokens_lower {
            self.token_index
                .entry(token_lower.clone())
                .or_insert_with(alloc::collections::BTreeSet::new)
                .insert(id);
        }

        // Store pre-tokenized font name for fast lookup (no re-tokenization needed)
        self.font_tokens.insert(id, tokens_lower);
    }
}

impl Clone for FcFontCache {
    /// Shallow clone — the returned handle shares the same underlying
    /// state as `self`. Writes through either are visible to both.
    /// This is the whole point of the v4.1 redesign; callers that need
    /// an isolated frozen copy must explicitly request one (e.g. via
    /// `snapshot_state`, which is intentionally not provided because
    /// we no longer have a use case for it).
    fn clone(&self) -> Self {
        Self {
            shared: std::sync::Arc::clone(&self.shared),
        }
    }
}

impl core::fmt::Debug for FcFontCache {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let state = self.state_read();
        f.debug_struct("FcFontCache")
            .field("patterns_len", &state.patterns.len())
            .field("metadata_len", &state.metadata.len())
            .field("disk_fonts_len", &state.disk_fonts.len())
            .field("memory_fonts_len", &state.memory_fonts.len())
            .finish()
    }
}

impl Default for FcFontCache {
    fn default() -> Self {
        Self {
            shared: std::sync::Arc::new(FcFontCacheShared {
                state: std::sync::RwLock::new(FcFontCacheInner::default()),
                chain_cache: std::sync::Mutex::new(std::collections::HashMap::new()),
                shared_bytes: std::sync::Mutex::new(std::collections::HashMap::new()),
            }),
        }
    }
}

impl FcFontCache {
    /// Acquire a read guard on the cache's state. Panics if the lock
    /// was poisoned by a panic inside the write guard — same
    /// contract as `RwLock::read().expect(..)`.
    #[inline]
    pub(crate) fn state_read(
        &self,
    ) -> std::sync::RwLockReadGuard<'_, FcFontCacheInner> {
        self.shared
            .state
            .read()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
    }

    /// Acquire a write guard on the cache's state. Panics on
    /// poisoning, same as `state_read`.
    #[inline]
    pub(crate) fn state_write(
        &self,
    ) -> std::sync::RwLockWriteGuard<'_, FcFontCacheInner> {
        self.shared
            .state
            .write()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
    }

    /// Adds in-memory font files.
    ///
    /// Note: takes `&self` — the shared cache handles interior
    /// mutability via the RwLock.
    pub fn with_memory_fonts(&self, fonts: Vec<(FcPattern, FcFont)>) -> &Self {
        let mut state = self.state_write();
        for (pattern, font) in fonts {
            let id = FontId::new();
            state.patterns.insert(pattern.clone(), id);
            state.metadata.insert(id, pattern.clone());
            state.memory_fonts.insert(id, font);
            state.index_pattern_tokens(&pattern, id);
        }
        self
    }

    /// Adds a memory font with a specific ID (for testing).
    pub fn with_memory_font_with_id(
        &self,
        id: FontId,
        pattern: FcPattern,
        font: FcFont,
    ) -> &Self {
        let mut state = self.state_write();
        state.patterns.insert(pattern.clone(), id);
        state.metadata.insert(id, pattern.clone());
        state.memory_fonts.insert(id, font);
        state.index_pattern_tokens(&pattern, id);
        self
    }

    /// Register a newly-parsed on-disk font. Called by the builder
    /// thread inside `FcFontRegistry`. Allocates a fresh `FontId`,
    /// inserts the pattern + path + metadata in one write lock, and
    /// invalidates the chain cache so subsequent resolutions pick
    /// up the new font.
    pub fn insert_builder_font(&self, pattern: FcPattern, path: FcFontPath) {
        let id = FontId::new();
        {
            let mut state = self.state_write();
            state.index_pattern_tokens(&pattern, id);
            state.patterns.insert(pattern.clone(), id);
            state.disk_fonts.insert(id, path);
            state.metadata.insert(id, pattern);
        }
        // Invalidate chain cache so callers see the new font on the
        // next resolve. Scoped after the state write to keep lock
        // nesting shallow.
        if let Ok(mut cc) = self.shared.chain_cache.lock() {
            cc.clear();
        }
    }

    /// Insert a *fast-probed* pattern into the cache and return its
    /// fresh `FontId`. Used by [`FcFontRegistry::request_fonts_fast`]
    /// when a cmap probe discovers a font that covers some subset of
    /// the requested codepoints. Unlike [`insert_builder_font`] this
    /// does **not** populate the token index (we don't have NAME
    /// table data), so fuzzy-name lookups on fast-probed fonts fall
    /// through to the filename-guess in `known_paths`.
    pub fn insert_fast_pattern(&self, pattern: FcPattern, path: FcFontPath) -> FontId {
        let id = FontId::new();
        let mut state = self.state_write();
        state.patterns.insert(pattern.clone(), id);
        state.disk_fonts.insert(id, path);
        state.metadata.insert(id, pattern);
        id
    }

    /// Look up all `FontId`s whose `FcFontPath` matches `path`.
    /// Cheap way for `request_fonts_fast` to reuse fast-probed
    /// entries across layout passes without re-reading the cmap.
    ///
    /// O(n) over the disk_fonts map; fine for the typical case of
    /// <100 parsed fonts, and we skip the scan entirely when a
    /// stack's first candidate covers.
    pub fn lookup_paths_cached(&self, path: &str) -> Option<Vec<FontId>> {
        let state = self.state_read();
        let mut out = Vec::new();
        for (id, font_path) in &state.disk_fonts {
            if font_path.path == path {
                out.push(*id);
            }
        }
        if out.is_empty() { None } else { Some(out) }
    }

    /// Get font data for a given font ID.
    ///
    /// Returns owned values (not references) because the underlying
    /// maps live behind an RwLock — a reference could not outlive
    /// the read guard. In-memory fonts come back as cloned `FcFont`
    /// instances; disk fonts return their `FcFontPath`.
    pub fn get_font_by_id(&self, id: &FontId) -> Option<OwnedFontSource> {
        let state = self.state_read();
        if let Some(font) = state.memory_fonts.get(id) {
            return Some(OwnedFontSource::Memory(font.clone()));
        }
        if let Some(path) = state.disk_fonts.get(id) {
            return Some(OwnedFontSource::Disk(path.clone()));
        }
        None
    }

    /// Get metadata for a font ID. Returns an owned `FcPattern`
    /// (cloned out of the shared map) because we can't return a
    /// reference across the RwLock boundary.
    pub fn get_metadata_by_id(&self, id: &FontId) -> Option<FcPattern> {
        self.state_read().metadata.get(id).cloned()
    }

    /// Get the font bytes for `id` as a shared [`FontBytes`].
    ///
    /// On disk the returned `Arc<FontBytes>` wraps an mmap of the file
    /// (`FontBytes::Mmapped`). Untouched pages of the file never count
    /// toward the process's RSS — for a font where layout shapes only
    /// a handful of glyphs, this is the difference between paying for
    /// the whole 4 MiB `.ttc` and paying for the cmap + a few glyf
    /// pages.
    ///
    /// In-memory fonts (`FontSource::Memory`) come back as
    /// `FontBytes::Owned`, since the bytes are already on the heap.
    ///
    /// Multiple `FontId`s backed by the same file content (every face
    /// of a `.ttc`, or two paths with identical bytes) return the
    /// *same* `Arc<FontBytes>` thanks to a content-hash → `Weak`
    /// cache. Bytes get unmapped automatically when the last consumer
    /// drops the Arc.
    ///
    /// `FontBytes` derefs to `[u8]`, so callers that only need
    /// `&[u8]` (allsorts, ttf-parser, …) can pass it through without
    /// thinking about the backing.
    ///
    /// Failure modes: returns `None` if the path is unknown, or the
    /// file no longer exists / cannot be opened, or the mmap call
    /// fails. Callers may retry with a fresh `get_font_bytes` if they
    /// suspect the file was replaced underneath them; the next call
    /// re-opens cleanly.
    #[cfg(feature = "std")]
    pub fn get_font_bytes(&self, id: &FontId) -> Option<std::sync::Arc<FontBytes>> {
        use std::sync::Arc;
        match self.get_font_by_id(id)? {
            OwnedFontSource::Memory(font) => Some(Arc::new(FontBytes::Owned(
                Arc::from(font.bytes.as_slice()),
            ))),
            OwnedFontSource::Disk(path) => {
                let hash = path.bytes_hash;
                if hash != 0 {
                    if let Ok(guard) = self.shared.shared_bytes.lock() {
                        if let Some(weak) = guard.get(&hash) {
                            if let Some(arc) = weak.upgrade() {
                                return Some(arc);
                            }
                        }
                    }
                }

                let arc = open_font_bytes_mmap(&path.path)?;
                if hash != 0 {
                    if let Ok(mut guard) = self.shared.shared_bytes.lock() {
                        // Overwrite any stale weak ref that failed to upgrade.
                        guard.insert(hash, Arc::downgrade(&arc));
                    }
                }
                Some(arc)
            }
        }
    }

    /// Returns an empty font cache (no_std / no filesystem).
    #[cfg(not(feature = "std"))]
    pub fn build() -> Self { Self::default() }

    /// Scans system font directories using filename heuristics (no allsorts).
    #[cfg(all(feature = "std", not(feature = "parsing")))]
    pub fn build() -> Self { Self::build_from_filenames() }

    /// Scans and parses all system fonts via allsorts for full metadata.
    #[cfg(all(feature = "std", feature = "parsing"))]
    pub fn build() -> Self { Self::build_inner(None) }

    /// Filename-only scan: discovers fonts on disk, guesses metadata from
    /// the filename using [`config::tokenize_font_stem`].
    #[cfg(all(feature = "std", not(feature = "parsing")))]
    fn build_from_filenames() -> Self {
        let cache = Self::default();
        {
            let mut state = cache.state_write();
            for dir in crate::config::font_directories(OperatingSystem::current()) {
                for path in FcCollectFontFilesRecursive(dir) {
                    let pattern = match pattern_from_filename(&path) {
                        Some(p) => p,
                        None => continue,
                    };
                    let id = FontId::new();
                    state.disk_fonts.insert(id, FcFontPath {
                        path: path.to_string_lossy().to_string(),
                        font_index: 0,
                        // Filename-only scan — we never read the bytes,
                        // so there's no dedup key. Leave as 0.
                        bytes_hash: 0,
                    });
                    state.index_pattern_tokens(&pattern, id);
                    state.metadata.insert(id, pattern.clone());
                    state.patterns.insert(pattern, id);
                }
            }
        }
        cache
    }
    
    /// Builds a font cache with only specific font families (and their fallbacks).
    /// 
    /// This is a performance optimization for applications that know ahead of time
    /// which fonts they need. Instead of scanning all system fonts (which can be slow
    /// on systems with many fonts), only fonts matching the specified families are loaded.
    /// 
    /// Generic family names like "sans-serif", "serif", "monospace" are expanded
    /// to OS-specific font names (e.g., "sans-serif" on macOS becomes "Helvetica Neue", 
    /// "San Francisco", etc.).
    /// 
    /// **Note**: This will NOT automatically load fallback fonts for scripts not covered
    /// by the requested families. If you need Arabic, CJK, or emoji support, either:
    /// - Add those families explicitly to the filter
    /// - Use `with_memory_fonts()` to add bundled fonts
    /// - Use `build()` to load all system fonts
    /// 
    /// # Arguments
    /// * `families` - Font family names to load (e.g., ["Arial", "sans-serif"])
    /// 
    /// # Example
    /// ```ignore
    /// // Only load Arial and sans-serif fallback fonts
    /// let cache = FcFontCache::build_with_families(&["Arial", "sans-serif"]);
    /// ```
    #[cfg(all(feature = "std", feature = "parsing"))]
    pub fn build_with_families(families: &[impl AsRef<str>]) -> Self {
        // Expand generic families to OS-specific names
        let os = OperatingSystem::current();
        let mut target_families: Vec<String> = Vec::new();
        
        for family in families {
            let family_str = family.as_ref();
            let expanded = os.expand_generic_family(family_str, &[]);
            if expanded.is_empty() || (expanded.len() == 1 && expanded[0] == family_str) {
                target_families.push(family_str.to_string());
            } else {
                target_families.extend(expanded);
            }
        }
        
        Self::build_inner(Some(&target_families))
    }
    
    /// Inner build function that handles both filtered and unfiltered font loading.
    /// 
    /// # Arguments
    /// * `family_filter` - If Some, only load fonts matching these family names.
    ///                     If None, load all fonts.
    #[cfg(all(feature = "std", feature = "parsing"))]
    fn build_inner(family_filter: Option<&[String]>) -> Self {
        let cache = FcFontCache::default();

        // Normalize filter families for matching
        let filter_normalized: Option<Vec<String>> = family_filter.map(|families| {
            families
                .iter()
                .map(|f| crate::utils::normalize_family_name(f))
                .collect()
        });

        // Helper closure to check if a pattern matches the filter
        let matches_filter = |pattern: &FcPattern| -> bool {
            match &filter_normalized {
                None => true, // No filter = accept all
                Some(targets) => {
                    pattern.name.as_ref().map_or(false, |name| {
                        let name_norm = crate::utils::normalize_family_name(name);
                        targets.iter().any(|target| name_norm.contains(target))
                    }) || pattern.family.as_ref().map_or(false, |family| {
                        let family_norm = crate::utils::normalize_family_name(family);
                        targets.iter().any(|target| family_norm.contains(target))
                    })
                }
            }
        };

        let mut state = cache.state_write();

        #[cfg(target_os = "linux")]
        {
            if let Some((font_entries, render_configs)) = FcScanDirectories() {
                for (mut pattern, path) in font_entries {
                    if matches_filter(&pattern) {
                        // Apply per-font render config if a matching family rule exists
                        if let Some(family) = pattern.name.as_ref().or(pattern.family.as_ref()) {
                            if let Some(rc) = render_configs.get(family) {
                                pattern.render_config = rc.clone();
                            }
                        }
                        let id = FontId::new();
                        state.patterns.insert(pattern.clone(), id);
                        state.metadata.insert(id, pattern.clone());
                        state.disk_fonts.insert(id, path);
                        state.index_pattern_tokens(&pattern, id);
                    }
                }
            }
        }

        #[cfg(target_os = "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());

            let font_dirs = vec![
                (None, format!("{}\\Fonts\\", system_root)),
                (None, format!("{}\\AppData\\Local\\Microsoft\\Windows\\Fonts\\", user_profile)),
            ];

            let font_entries = FcScanDirectoriesInner(&font_dirs);
            for (pattern, path) in font_entries {
                if matches_filter(&pattern) {
                    let id = FontId::new();
                    state.patterns.insert(pattern.clone(), id);
                    state.metadata.insert(id, pattern.clone());
                    state.disk_fonts.insert(id, path);
                    state.index_pattern_tokens(&pattern, id);
                }
            }
        }

        #[cfg(target_os = "macos")]
        {
            let font_dirs = vec![
                (None, "~/Library/Fonts".to_owned()),
                (None, "/System/Library/Fonts".to_owned()),
                (None, "/Library/Fonts".to_owned()),
                (None, "/System/Library/AssetsV2".to_owned()),
            ];

            let font_entries = FcScanDirectoriesInner(&font_dirs);
            for (pattern, path) in font_entries {
                if matches_filter(&pattern) {
                    let id = FontId::new();
                    state.patterns.insert(pattern.clone(), id);
                    state.metadata.insert(id, pattern.clone());
                    state.disk_fonts.insert(id, path);
                    state.index_pattern_tokens(&pattern, id);
                }
            }
        }

        drop(state);
        cache
    }
    
    /// Check if a font ID is a memory font (preferred over disk fonts)
    pub fn is_memory_font(&self, id: &FontId) -> bool {
        self.state_read().memory_fonts.contains_key(id)
    }

    /// Returns the list of fonts and font patterns.
    ///
    /// Returns owned `FcPattern` values (cloned out of the shared
    /// state) — this is the v4.1 API change described on
    /// [`FcFontCache`]. Callers that need to iterate without
    /// cloning should use [`FcFontCache::for_each_pattern`].
    pub fn list(&self) -> Vec<(FcPattern, FontId)> {
        self.state_read()
            .patterns
            .iter()
            .map(|(pattern, id)| (pattern.clone(), *id))
            .collect()
    }

    /// Iterate over every `(pattern, id)` pair under a single read
    /// guard. `f` is called once per entry — avoids the per-entry
    /// clone that [`list`] incurs.
    pub fn for_each_pattern<F: FnMut(&FcPattern, &FontId)>(&self, mut f: F) {
        let state = self.state_read();
        for (pattern, id) in &state.patterns {
            f(pattern, id);
        }
    }

    /// Returns true if the cache contains no font patterns
    pub fn is_empty(&self) -> bool {
        self.state_read().patterns.is_empty()
    }

    /// Returns the number of font patterns in the cache
    pub fn len(&self) -> usize {
        self.state_read().patterns.len()
    }

    /// Queries a font from the in-memory cache, returns the first found font (early return)
    /// Memory fonts are always preferred over disk fonts with the same match quality.
    pub fn query(&self, pattern: &FcPattern, trace: &mut Vec<TraceMsg>) -> Option<FontMatch> {
        let state = self.state_read();
        let mut matches = Vec::new();

        for (stored_pattern, id) in &state.patterns {
            if Self::query_matches_internal(stored_pattern, pattern, trace) {
                let metadata = state.metadata.get(id).unwrap_or(stored_pattern);

                // Calculate Unicode compatibility score
                let unicode_compatibility = if pattern.unicode_ranges.is_empty() {
                    // No specific Unicode requirements, use general coverage
                    Self::calculate_unicode_coverage(&metadata.unicode_ranges) as i32
                } else {
                    // Calculate how well this font covers the requested Unicode ranges
                    Self::calculate_unicode_compatibility(&pattern.unicode_ranges, &metadata.unicode_ranges)
                };

                let style_score = Self::calculate_style_score(pattern, metadata);

                // Memory fonts get a bonus to prefer them over disk fonts
                let is_memory = state.memory_fonts.contains_key(id);

                matches.push((*id, unicode_compatibility, style_score, metadata.clone(), is_memory));
            }
        }

        // Sort by: 1. Memory font (preferred), 2. Unicode compatibility, 3. Style score
        matches.sort_by(|a, b| {
            // Memory fonts first
            b.4.cmp(&a.4)
                .then_with(|| b.1.cmp(&a.1)) // Unicode compatibility (higher is better)
                .then_with(|| a.2.cmp(&b.2)) // Style score (lower is better)
        });

        matches.first().map(|(id, _, _, metadata, _)| {
            FontMatch {
                id: *id,
                unicode_ranges: metadata.unicode_ranges.clone(),
                fallbacks: Vec::new(), // Fallbacks computed lazily via compute_fallbacks()
            }
        })
    }

    /// Queries all fonts matching a pattern (internal use only).
    ///
    /// Note: This function is now private. Use resolve_font_chain() to build a font fallback chain,
    /// then call FontFallbackChain::query_for_text() to resolve fonts for specific text.
    fn query_internal(&self, pattern: &FcPattern, trace: &mut Vec<TraceMsg>) -> Vec<FontMatch> {
        let state = self.state_read();
        self.query_internal_locked(&state, pattern, trace)
    }

    /// Internal variant used when the caller already holds a read
    /// guard on the state. Avoids re-locking.
    fn query_internal_locked(
        &self,
        state: &FcFontCacheInner,
        pattern: &FcPattern,
        trace: &mut Vec<TraceMsg>,
    ) -> Vec<FontMatch> {
        let mut matches = Vec::new();

        for (stored_pattern, id) in &state.patterns {
            if Self::query_matches_internal(stored_pattern, pattern, trace) {
                let metadata = state.metadata.get(id).unwrap_or(stored_pattern);

                // Calculate Unicode compatibility score
                let unicode_compatibility = if pattern.unicode_ranges.is_empty() {
                    Self::calculate_unicode_coverage(&metadata.unicode_ranges) as i32
                } else {
                    Self::calculate_unicode_compatibility(&pattern.unicode_ranges, &metadata.unicode_ranges)
                };

                let style_score = Self::calculate_style_score(pattern, metadata);
                matches.push((*id, unicode_compatibility, style_score, metadata.clone()));
            }
        }

        // Sort by style score (lowest first), THEN by Unicode compatibility (highest first)
        // Style matching (weight, italic, etc.) is now the primary criterion
        // Deterministic tiebreaker: prefer non-italic, then alphabetical by name
        matches.sort_by(|a, b| {
            a.2.cmp(&b.2) // Style score (lower is better)
                .then_with(|| b.1.cmp(&a.1)) // Unicode compatibility (higher is better)
                .then_with(|| a.3.italic.cmp(&b.3.italic)) // Prefer non-italic
                .then_with(|| a.3.name.cmp(&b.3.name)) // Alphabetical tiebreaker
        });

        matches
            .into_iter()
            .map(|(id, _, _, metadata)| {
                FontMatch {
                    id,
                    unicode_ranges: metadata.unicode_ranges.clone(),
                    fallbacks: Vec::new(), // Fallbacks computed lazily via compute_fallbacks()
                }
            })
            .collect()
    }

    /// Compute fallback fonts for a given font
    /// This is a lazy operation that can be expensive - only call when actually needed
    /// (e.g., for FFI or debugging, not needed for resolve_char)
    pub fn compute_fallbacks(
        &self,
        font_id: &FontId,
        trace: &mut Vec<TraceMsg>,
    ) -> Vec<FontMatchNoFallback> {
        let state = self.state_read();
        let pattern = match state.metadata.get(font_id) {
            Some(p) => p.clone(),
            None => return Vec::new(),
        };
        drop(state);

        self.compute_fallbacks_for_pattern(&pattern, Some(font_id), trace)
    }

    fn compute_fallbacks_for_pattern(
        &self,
        pattern: &FcPattern,
        exclude_id: Option<&FontId>,
        _trace: &mut Vec<TraceMsg>,
    ) -> Vec<FontMatchNoFallback> {
        let state = self.state_read();
        let mut candidates = Vec::new();

        // Collect all potential fallbacks (excluding original pattern)
        for (stored_pattern, id) in &state.patterns {
            // Skip if this is the original font
            if exclude_id.is_some() && exclude_id.unwrap() == id {
                continue;
            }

            // Check if this font supports any of the unicode ranges
            if !stored_pattern.unicode_ranges.is_empty() && !pattern.unicode_ranges.is_empty() {
                // Calculate Unicode compatibility
                let unicode_compatibility = Self::calculate_unicode_compatibility(
                    &pattern.unicode_ranges,
                    &stored_pattern.unicode_ranges
                );

                // Only include if there's actual overlap
                if unicode_compatibility > 0 {
                    let style_score = Self::calculate_style_score(pattern, stored_pattern);
                    candidates.push((
                        FontMatchNoFallback {
                            id: *id,
                            unicode_ranges: stored_pattern.unicode_ranges.clone(),
                        },
                        unicode_compatibility,
                        style_score,
                        stored_pattern.clone(),
                    ));
                }
            } else if pattern.unicode_ranges.is_empty() && !stored_pattern.unicode_ranges.is_empty() {
                // No specific Unicode requirements, use general coverage
                let coverage = Self::calculate_unicode_coverage(&stored_pattern.unicode_ranges) as i32;
                let style_score = Self::calculate_style_score(pattern, stored_pattern);
                candidates.push((
                    FontMatchNoFallback {
                        id: *id,
                        unicode_ranges: stored_pattern.unicode_ranges.clone(),
                    },
                    coverage,
                    style_score,
                    stored_pattern.clone(),
                ));
            }
        }

        drop(state);

        // Sort by Unicode compatibility (highest first), THEN by style score (lowest first)
        candidates.sort_by(|a, b| {
            b.1.cmp(&a.1)
                .then_with(|| a.2.cmp(&b.2))
        });

        // Deduplicate by keeping only the best match per unique unicode range
        let mut seen_ranges = Vec::new();
        let mut deduplicated = Vec::new();

        for (id, _, _, pattern) in candidates {
            let mut is_new_range = false;

            for range in &pattern.unicode_ranges {
                if !seen_ranges.iter().any(|r: &UnicodeRange| r.overlaps(range)) {
                    seen_ranges.push(*range);
                    is_new_range = true;
                }
            }

            if is_new_range {
                deduplicated.push(id);
            }
        }

        deduplicated
    }

    /// Get in-memory font data (cloned out of the shared state).
    pub fn get_memory_font(&self, id: &FontId) -> Option<FcFont> {
        self.state_read().memory_fonts.get(id).cloned()
    }

    /// Check if a pattern matches the query, with detailed tracing
    fn trace_path(k: &FcPattern) -> String {
        k.name.as_ref().cloned().unwrap_or_else(|| "<unknown>".to_string())
    }

    pub fn query_matches_internal(
        k: &FcPattern,
        pattern: &FcPattern,
        trace: &mut Vec<TraceMsg>,
    ) -> bool {
        // Check name - substring match
        if let Some(ref name) = pattern.name {
            if !k.name.as_ref().map_or(false, |kn| kn.contains(name)) {
                trace.push(TraceMsg {
                    level: TraceLevel::Info,
                    path: Self::trace_path(k),
                    reason: MatchReason::NameMismatch {
                        requested: pattern.name.clone(),
                        found: k.name.clone(),
                    },
                });
                return false;
            }
        }

        // Check family - substring match
        if let Some(ref family) = pattern.family {
            if !k.family.as_ref().map_or(false, |kf| kf.contains(family)) {
                trace.push(TraceMsg {
                    level: TraceLevel::Info,
                    path: Self::trace_path(k),
                    reason: MatchReason::FamilyMismatch {
                        requested: pattern.family.clone(),
                        found: k.family.clone(),
                    },
                });
                return false;
            }
        }

        // Check style properties
        let style_properties = [
            (
                "italic",
                pattern.italic.needs_to_match(),
                pattern.italic.matches(&k.italic),
            ),
            (
                "oblique",
                pattern.oblique.needs_to_match(),
                pattern.oblique.matches(&k.oblique),
            ),
            (
                "bold",
                pattern.bold.needs_to_match(),
                pattern.bold.matches(&k.bold),
            ),
            (
                "monospace",
                pattern.monospace.needs_to_match(),
                pattern.monospace.matches(&k.monospace),
            ),
            (
                "condensed",
                pattern.condensed.needs_to_match(),
                pattern.condensed.matches(&k.condensed),
            ),
        ];

        for (property_name, needs_to_match, matches) in style_properties {
            if needs_to_match && !matches {
                let (requested, found) = match property_name {
                    "italic" => (format!("{:?}", pattern.italic), format!("{:?}", k.italic)),
                    "oblique" => (format!("{:?}", pattern.oblique), format!("{:?}", k.oblique)),
                    "bold" => (format!("{:?}", pattern.bold), format!("{:?}", k.bold)),
                    "monospace" => (
                        format!("{:?}", pattern.monospace),
                        format!("{:?}", k.monospace),
                    ),
                    "condensed" => (
                        format!("{:?}", pattern.condensed),
                        format!("{:?}", k.condensed),
                    ),
                    _ => (String::new(), String::new()),
                };

                trace.push(TraceMsg {
                    level: TraceLevel::Info,
                    path: Self::trace_path(k),
                    reason: MatchReason::StyleMismatch {
                        property: property_name,
                        requested,
                        found,
                    },
                });
                return false;
            }
        }

        // Check weight - hard filter if non-normal weight is requested
        if pattern.weight != FcWeight::Normal && pattern.weight != k.weight {
            trace.push(TraceMsg {
                level: TraceLevel::Info,
                path: Self::trace_path(k),
                reason: MatchReason::WeightMismatch {
                    requested: pattern.weight,
                    found: k.weight,
                },
            });
            return false;
        }

        // Check stretch - hard filter if non-normal stretch is requested
        if pattern.stretch != FcStretch::Normal && pattern.stretch != k.stretch {
            trace.push(TraceMsg {
                level: TraceLevel::Info,
                path: Self::trace_path(k),
                reason: MatchReason::StretchMismatch {
                    requested: pattern.stretch,
                    found: k.stretch,
                },
            });
            return false;
        }

        // Check unicode ranges if specified
        if !pattern.unicode_ranges.is_empty() {
            let mut has_overlap = false;

            for p_range in &pattern.unicode_ranges {
                for k_range in &k.unicode_ranges {
                    if p_range.overlaps(k_range) {
                        has_overlap = true;
                        break;
                    }
                }
                if has_overlap {
                    break;
                }
            }

            if !has_overlap {
                trace.push(TraceMsg {
                    level: TraceLevel::Info,
                    path: Self::trace_path(k),
                    reason: MatchReason::UnicodeRangeMismatch {
                        character: '\0', // No specific character to report
                        ranges: k.unicode_ranges.clone(),
                    },
                });
                return false;
            }
        }

        true
    }
    
    /// Resolve a complete font fallback chain for a CSS font-family stack
    /// This is the main entry point for font resolution with caching
    /// Automatically expands generic CSS families (serif, sans-serif, monospace) to OS-specific fonts
    /// 
    /// # Arguments
    /// * `font_families` - CSS font-family stack (e.g., ["Arial", "sans-serif"])
    /// * `text` - The text to render (used to extract Unicode ranges)
    /// * `weight` - Font weight
    /// * `italic` - Italic style requirement
    /// * `oblique` - Oblique style requirement
    /// * `trace` - Debug trace messages
    /// 
    /// # Returns
    /// A complete font fallback chain with CSS fallbacks and Unicode fallbacks
    /// 
    /// # Example
    /// ```no_run
    /// # use rust_fontconfig::{FcFontCache, FcWeight, PatternMatch};
    /// let cache = FcFontCache::build();
    /// let families = vec!["Arial".to_string(), "sans-serif".to_string()];
    /// let chain = cache.resolve_font_chain(&families, FcWeight::Normal, 
    ///                                       PatternMatch::DontCare, PatternMatch::DontCare, 
    ///                                       &mut Vec::new());
    /// // On macOS: families expanded to ["Arial", "San Francisco", "Helvetica Neue", "Lucida Grande"]
    /// ```
    #[cfg(feature = "std")]
    pub fn resolve_font_chain(
        &self,
        font_families: &[String],
        weight: FcWeight,
        italic: PatternMatch,
        oblique: PatternMatch,
        trace: &mut Vec<TraceMsg>,
    ) -> FontFallbackChain {
        self.resolve_font_chain_with_os(font_families, weight, italic, oblique, trace, OperatingSystem::current())
    }
    
    /// Resolve font chain with explicit OS specification (useful for testing)
    #[cfg(feature = "std")]
    pub fn resolve_font_chain_with_os(
        &self,
        font_families: &[String],
        weight: FcWeight,
        italic: PatternMatch,
        oblique: PatternMatch,
        trace: &mut Vec<TraceMsg>,
        os: OperatingSystem,
    ) -> FontFallbackChain {
        self.resolve_font_chain_impl(font_families, weight, italic, oblique, None, trace, os)
    }

    /// Resolve a font fallback chain, restricting Unicode fallbacks to the
    /// caller-supplied set of scripts (usually derived from the actual
    /// text content of the document).
    ///
    /// - `scripts_hint: None` → back-compat behaviour, equivalent to
    ///   [`FcFontCache::resolve_font_chain`]: pulls in fallback fonts for
    ///   the full [`DEFAULT_UNICODE_FALLBACK_SCRIPTS`] set.
    /// - `scripts_hint: Some(&[])` → no Unicode fallbacks attached. For
    ///   an ASCII-only page this avoids pulling Arial Unicode MS,
    ///   CJK fonts, etc. into memory when they're not needed.
    /// - `scripts_hint: Some(&[CJK])` → only CJK fallback attached.
    ///
    /// The chain cache is keyed so an ASCII-only resolution cannot be
    /// served from a slot populated by a default/all-scripts resolution.
    #[cfg(feature = "std")]
    pub fn resolve_font_chain_with_scripts(
        &self,
        font_families: &[String],
        weight: FcWeight,
        italic: PatternMatch,
        oblique: PatternMatch,
        scripts_hint: Option<&[UnicodeRange]>,
        trace: &mut Vec<TraceMsg>,
    ) -> FontFallbackChain {
        self.resolve_font_chain_impl(
            font_families, weight, italic, oblique, scripts_hint,
            trace, OperatingSystem::current(),
        )
    }

    /// Shared entry used by [`resolve_font_chain_with_os`] and
    /// [`resolve_font_chain_with_scripts`]. Handles the cache lookup,
    /// generic-family expansion, and delegation to the uncached builder.
    #[cfg(feature = "std")]
    fn resolve_font_chain_impl(
        &self,
        font_families: &[String],
        weight: FcWeight,
        italic: PatternMatch,
        oblique: PatternMatch,
        scripts_hint: Option<&[UnicodeRange]>,
        trace: &mut Vec<TraceMsg>,
        os: OperatingSystem,
    ) -> FontFallbackChain {
        // Check cache FIRST - key uses original (unexpanded) families
        // plus a hash over the scripts_hint so ASCII-only callers don't
        // consume a slot filled by a default-scripts caller.
        let scripts_hint_hash = scripts_hint.map(hash_scripts_hint);
        let cache_key = FontChainCacheKey {
            font_families: font_families.to_vec(),
            weight,
            italic,
            oblique,
            scripts_hint_hash,
        };

        if let Some(cached) = self
            .shared
            .chain_cache
            .lock()
            .ok()
            .and_then(|c| c.get(&cache_key).cloned())
        {
            return cached;
        }

        // Expand generic CSS families to OS-specific fonts
        let expanded_families = expand_font_families(font_families, os, &[]);

        // Build the chain
        let chain = self.resolve_font_chain_uncached(
            &expanded_families,
            weight,
            italic,
            oblique,
            scripts_hint,
            trace,
        );

        // Cache the result
        if let Ok(mut cache) = self.shared.chain_cache.lock() {
            cache.insert(cache_key, chain.clone());
        }

        chain
    }
    
    /// Internal implementation without caching.
    ///
    /// `scripts_hint`:
    /// - `None` pulls in the full [`DEFAULT_UNICODE_FALLBACK_SCRIPTS`]
    ///   set (the original, back-compat behaviour).
    /// - `Some(&[])` attaches no Unicode fallbacks.
    /// - `Some(ranges)` attaches fallbacks only for those ranges.
    #[cfg(feature = "std")]
    fn resolve_font_chain_uncached(
        &self,
        font_families: &[String],
        weight: FcWeight,
        italic: PatternMatch,
        oblique: PatternMatch,
        scripts_hint: Option<&[UnicodeRange]>,
        trace: &mut Vec<TraceMsg>,
    ) -> FontFallbackChain {
        let mut css_fallbacks = Vec::new();
        
        // Resolve each CSS font-family to its system fallbacks
        for (_i, family) in font_families.iter().enumerate() {
            // Check if this is a generic font family
            let (pattern, is_generic) = if config::is_generic_family(family) {
                let monospace = if family.eq_ignore_ascii_case("monospace") {
                    PatternMatch::True
                } else {
                    PatternMatch::False
                };
                let pattern = FcPattern {
                    name: None,
                    weight,
                    italic,
                    oblique,
                    monospace,
                    unicode_ranges: Vec::new(),
                    ..Default::default()
                };
                (pattern, true)
            } else {
                // Specific font family name
                let pattern = FcPattern {
                    name: Some(family.clone()),
                    weight,
                    italic,
                    oblique,
                    unicode_ranges: Vec::new(),
                    ..Default::default()
                };
                (pattern, false)
            };
            
            // Use fuzzy matching for specific fonts (fast token-based lookup)
            // For generic families, use query (slower but necessary for property matching)
            let mut matches = if is_generic {
                // Generic families need full pattern matching
                self.query_internal(&pattern, trace)
            } else {
                // Specific font names: use fast token-based fuzzy matching
                self.fuzzy_query_by_name(family, weight, italic, oblique, &[], trace)
            };
            
            // For generic families, limit to top 5 fonts to avoid too many matches
            if is_generic && matches.len() > 5 {
                matches.truncate(5);
            }
            
            // Always add the CSS fallback group to preserve CSS ordering
            // even if no fonts were found for this family
            css_fallbacks.push(CssFallbackGroup {
                css_name: family.clone(),
                fonts: matches,
            });
        }
        
        // Populate unicode_fallbacks. CSS fallback fonts may falsely claim
        // coverage of a script via the OS/2 unicode-range bits without
        // actually having glyphs, so we supplement the CSS chain with an
        // explicit lookup for each requested script block. resolve_char()
        // prefers CSS fallbacks first (earlier in the chain wins).
        //
        // The set of script blocks to cover is caller-controlled via
        // `scripts_hint`: `None` keeps the back-compat DEFAULT_UNICODE_FALLBACK_SCRIPTS
        // behaviour (7 scripts) so existing `resolve_font_chain` consumers
        // stay unchanged; `Some(&[])` opts into "no unicode fallbacks at all"
        // for ASCII-only documents, eliminating the big CJK / Arabic fonts
        // from the resolved chain (and therefore from eager downstream parses).
        let important_ranges: &[UnicodeRange] =
            scripts_hint.unwrap_or(DEFAULT_UNICODE_FALLBACK_SCRIPTS);
        let unicode_fallbacks = if important_ranges.is_empty() {
            Vec::new()
        } else {
            let all_uncovered = vec![false; important_ranges.len()];
            self.find_unicode_fallbacks(
                important_ranges,
                &all_uncovered,
                &css_fallbacks,
                weight,
                italic,
                oblique,
                trace,
            )
        };

        FontFallbackChain {
            css_fallbacks,
            unicode_fallbacks,
            original_stack: font_families.to_vec(),
        }
    }
    
    /// Extract Unicode ranges from text
    #[allow(dead_code)]
    fn extract_unicode_ranges(text: &str) -> Vec<UnicodeRange> {
        let mut chars: Vec<char> = text.chars().collect();
        chars.sort_unstable();
        chars.dedup();
        
        if chars.is_empty() {
            return Vec::new();
        }
        
        let mut ranges = Vec::new();
        let mut range_start = chars[0] as u32;
        let mut range_end = range_start;
        
        for &c in &chars[1..] {
            let codepoint = c as u32;
            if codepoint == range_end + 1 {
                range_end = codepoint;
            } else {
                ranges.push(UnicodeRange { start: range_start, end: range_end });
                range_start = codepoint;
                range_end = codepoint;
            }
        }
        
        ranges.push(UnicodeRange { start: range_start, end: range_end });
        ranges
    }
    
    /// Fuzzy query for fonts by name when exact match fails
    /// Uses intelligent token-based matching with inverted index for speed:
    /// 1. Break name into tokens (e.g., "NotoSansJP" -> ["noto", "sans", "jp"])
    /// 2. Use token_index to find candidate fonts via BTreeSet intersection
    /// 3. Score only the candidate fonts (instead of all 800+ patterns)
    /// 4. Prioritize fonts matching more tokens + Unicode coverage
    #[cfg(feature = "std")]
    fn fuzzy_query_by_name(
        &self,
        requested_name: &str,
        weight: FcWeight,
        italic: PatternMatch,
        oblique: PatternMatch,
        unicode_ranges: &[UnicodeRange],
        _trace: &mut Vec<TraceMsg>,
    ) -> Vec<FontMatch> {
        // Extract tokens from the requested name (e.g., "NotoSansJP" -> ["noto", "sans", "jp"])
        let tokens = Self::extract_font_name_tokens(requested_name);
        
        if tokens.is_empty() {
            return Vec::new();
        }
        
        // Convert tokens to lowercase for case-insensitive lookup
        let tokens_lower: Vec<String> = tokens.iter().map(|t| t.to_lowercase()).collect();
        
        // Progressive token matching strategy:
        // Start with first token, then progressively narrow down with each additional token
        // If adding a token results in 0 matches, use the previous (broader) set
        // Example: ["Noto"] -> 10 fonts, ["Noto","Sans"] -> 2 fonts, ["Noto","Sans","JP"] -> 0 fonts => use 2 fonts
        
        let state = self.state_read();

        // Start with the first token
        let first_token = &tokens_lower[0];
        let mut candidate_ids = match state.token_index.get(first_token) {
            Some(ids) if !ids.is_empty() => ids.clone(),
            _ => {
                // First token not found - no fonts match, quit immediately
                return Vec::new();
            }
        };

        // Progressively narrow down with each additional token
        for token in &tokens_lower[1..] {
            if let Some(token_ids) = state.token_index.get(token) {
                // Calculate intersection
                let intersection: alloc::collections::BTreeSet<FontId> =
                    candidate_ids.intersection(token_ids).copied().collect();

                if intersection.is_empty() {
                    // Adding this token results in 0 matches - keep previous set and stop
                    break;
                } else {
                    // Successfully narrowed down - use intersection
                    candidate_ids = intersection;
                }
            } else {
                // Token not in index - keep current set and stop
                break;
            }
        }

        // Now score only the candidate fonts (HUGE speedup!)
        let mut candidates = Vec::new();

        for id in candidate_ids {
            let pattern = match state.metadata.get(&id) {
                Some(p) => p,
                None => continue,
            };
            
            // Get pre-tokenized font name (already lowercase)
            let font_tokens_lower = match state.font_tokens.get(&id) {
                Some(tokens) => tokens,
                None => continue,
            };
            
            if font_tokens_lower.is_empty() {
                continue;
            }
            
            // Calculate token match score (how many requested tokens appear in font name)
            // Both tokens_lower and font_tokens_lower are already lowercase, so direct comparison
            let token_matches = tokens_lower.iter()
                .filter(|req_token| {
                    font_tokens_lower.iter().any(|font_token| {
                        // Both already lowercase — exact token match (index guarantees candidates)
                        font_token == *req_token
                    })
                })
                .count();
            
            // Skip if no tokens match (shouldn't happen due to index, but safety check)
            if token_matches == 0 {
                continue;
            }
            
            // Calculate token similarity score (0-100)
            let token_similarity = (token_matches * 100 / tokens.len()) as i32;
            
            // Calculate Unicode range similarity
            let unicode_similarity = if !unicode_ranges.is_empty() && !pattern.unicode_ranges.is_empty() {
                Self::calculate_unicode_compatibility(unicode_ranges, &pattern.unicode_ranges)
            } else {
                0
            };
            
            // CRITICAL: If we have Unicode requirements, ONLY accept fonts that cover them
            // A font with great name match but no Unicode coverage is useless
            if !unicode_ranges.is_empty() && unicode_similarity == 0 {
                continue;
            }
            
            let style_score = Self::calculate_style_score(&FcPattern {
                weight,
                italic,
                oblique,
                ..Default::default()
            }, pattern);
            
            candidates.push((
                id,
                token_similarity,
                unicode_similarity,
                style_score,
                pattern.clone(),
            ));
        }
        
        // Sort by:
        // 1. Token matches (more matches = better)
        // 2. Unicode compatibility (if ranges provided)
        // 3. Style score (lower is better)
        // 4. Deterministic tiebreaker: prefer non-italic, then by font name
        candidates.sort_by(|a, b| {
            if !unicode_ranges.is_empty() {
                // When we have Unicode requirements, prioritize coverage
                b.1.cmp(&a.1) // Token similarity (higher is better) - PRIMARY
                    .then_with(|| b.2.cmp(&a.2)) // Unicode similarity (higher is better) - SECONDARY
                    .then_with(|| a.3.cmp(&b.3)) // Style score (lower is better) - TERTIARY
                    .then_with(|| a.4.italic.cmp(&b.4.italic)) // Prefer non-italic (False < True)
                    .then_with(|| a.4.name.cmp(&b.4.name)) // Alphabetical by name
            } else {
                // No Unicode requirements, token similarity is primary
                b.1.cmp(&a.1) // Token similarity (higher is better)
                    .then_with(|| a.3.cmp(&b.3)) // Style score (lower is better)
                    .then_with(|| a.4.italic.cmp(&b.4.italic)) // Prefer non-italic (False < True)
                    .then_with(|| a.4.name.cmp(&b.4.name)) // Alphabetical by name
            }
        });
        
        // Take top 5 matches
        candidates.truncate(5);
        
        // Convert to FontMatch
        candidates
            .into_iter()
            .map(|(id, _token_sim, _unicode_sim, _style, pattern)| {
                FontMatch {
                    id,
                    unicode_ranges: pattern.unicode_ranges.clone(),
                    fallbacks: Vec::new(), // Fallbacks computed lazily via compute_fallbacks()
                }
            })
            .collect()
    }
    
    /// Extract tokens from a font name
    /// E.g., "NotoSansJP" -> ["Noto", "Sans", "JP"]
    /// E.g., "Noto Sans CJK JP" -> ["Noto", "Sans", "CJK", "JP"]
    pub fn extract_font_name_tokens(name: &str) -> Vec<String> {
        let mut tokens = Vec::new();
        let mut current_token = String::new();
        let mut last_was_lower = false;
        
        for c in name.chars() {
            if c.is_whitespace() || c == '-' || c == '_' {
                // Word separator
                if !current_token.is_empty() {
                    tokens.push(current_token.clone());
                    current_token.clear();
                }
                last_was_lower = false;
            } else if c.is_uppercase() && last_was_lower && !current_token.is_empty() {
                // CamelCase boundary (e.g., "Noto" | "Sans")
                tokens.push(current_token.clone());
                current_token.clear();
                current_token.push(c);
                last_was_lower = false;
            } else {
                current_token.push(c);
                last_was_lower = c.is_lowercase();
            }
        }
        
        if !current_token.is_empty() {
            tokens.push(current_token);
        }
        
        tokens
    }
    
    /// Find fonts to cover missing Unicode ranges
    /// Uses intelligent matching: prefers fonts with similar names to existing ones
    /// Early quits once all Unicode ranges are covered for performance
    fn find_unicode_fallbacks(
        &self,
        unicode_ranges: &[UnicodeRange],
        covered_chars: &[bool],
        existing_groups: &[CssFallbackGroup],
        _weight: FcWeight,
        _italic: PatternMatch,
        _oblique: PatternMatch,
        trace: &mut Vec<TraceMsg>,
    ) -> Vec<FontMatch> {
        // Extract uncovered ranges
        let mut uncovered_ranges = Vec::new();
        for (i, &covered) in covered_chars.iter().enumerate() {
            if !covered && i < unicode_ranges.len() {
                uncovered_ranges.push(unicode_ranges[i].clone());
            }
        }
        
        if uncovered_ranges.is_empty() {
            return Vec::new();
        }

        // Query for fonts that cover these ranges.
        // Use DontCare for weight/italic/oblique — we want ANY font that covers
        // the missing characters, regardless of style. The similarity sort below
        // will prefer fonts matching the existing chain's style anyway.
        let pattern = FcPattern {
            name: None,
            weight: FcWeight::Normal, // Normal weight is not filtered by query_matches_internal (line 1836)
            italic: PatternMatch::DontCare,
            oblique: PatternMatch::DontCare,
            unicode_ranges: uncovered_ranges.clone(),
            ..Default::default()
        };
        
        let mut candidates = self.query_internal(&pattern, trace);

        // Intelligent sorting: prefer fonts with similar names to existing ones
        // Extract font family prefixes from existing fonts (e.g., "Noto Sans" from "Noto Sans JP")
        let existing_prefixes: Vec<String> = existing_groups
            .iter()
            .flat_map(|group| {
                group.fonts.iter().filter_map(|font| {
                    self.get_metadata_by_id(&font.id)
                        .and_then(|meta| meta.family.clone())
                        .and_then(|family| {
                            // Extract prefix (e.g., "Noto Sans" from "Noto Sans JP")
                            family.split_whitespace()
                                .take(2)
                                .collect::<Vec<_>>()
                                .join(" ")
                                .into()
                        })
                })
            })
            .collect();
        
        // Sort candidates by:
        // 1. Name similarity to existing fonts (highest priority)
        // 2. Unicode coverage (secondary)
        candidates.sort_by(|a, b| {
            let a_meta = self.get_metadata_by_id(&a.id);
            let b_meta = self.get_metadata_by_id(&b.id);

            let a_score = Self::calculate_font_similarity_score(a_meta.as_ref(), &existing_prefixes);
            let b_score = Self::calculate_font_similarity_score(b_meta.as_ref(), &existing_prefixes);
            
            b_score.cmp(&a_score) // Higher score = better match
                .then_with(|| {
                    let a_coverage = Self::calculate_unicode_compatibility(&uncovered_ranges, &a.unicode_ranges);
                    let b_coverage = Self::calculate_unicode_compatibility(&uncovered_ranges, &b.unicode_ranges);
                    b_coverage.cmp(&a_coverage)
                })
        });
        
        // Early quit optimization: only take fonts until all ranges are covered
        let mut result = Vec::new();
        let mut remaining_uncovered: Vec<bool> = vec![true; uncovered_ranges.len()];
        
        for candidate in candidates {
            // Check which ranges this font covers
            let mut covers_new_range = false;
            
            for (i, range) in uncovered_ranges.iter().enumerate() {
                if remaining_uncovered[i] {
                    // Check if this font covers this range
                    for font_range in &candidate.unicode_ranges {
                        if font_range.overlaps(range) {
                            remaining_uncovered[i] = false;
                            covers_new_range = true;
                            break;
                        }
                    }
                }
            }
            
            // Only add fonts that cover at least one new range
            if covers_new_range {
                result.push(candidate);
                
                // Early quit: if all ranges are covered, stop
                if remaining_uncovered.iter().all(|&uncovered| !uncovered) {
                    break;
                }
            }
        }
        
        result
    }
    
    /// Calculate similarity score between a font and existing font prefixes
    /// Higher score = more similar
    fn calculate_font_similarity_score(
        font_meta: Option<&FcPattern>,
        existing_prefixes: &[String],
    ) -> i32 {
        let Some(meta) = font_meta else { return 0; };
        let Some(family) = &meta.family else { return 0; };
        
        // Check if this font's family matches any existing prefix
        for prefix in existing_prefixes {
            if family.starts_with(prefix) {
                return 100; // Strong match
            }
            if family.contains(prefix) {
                return 50; // Partial match
            }
        }
        
        0 // No match
    }
    
    /// Find fallback fonts for a given pattern
    // Helper to calculate total unicode coverage
    pub fn calculate_unicode_coverage(ranges: &[UnicodeRange]) -> u64 {
        ranges
            .iter()
            .map(|range| (range.end - range.start + 1) as u64)
            .sum()
    }

    /// Calculate how well a font's Unicode ranges cover the requested ranges
    /// Returns a compatibility score (higher is better, 0 means no overlap)
    pub fn calculate_unicode_compatibility(
        requested: &[UnicodeRange],
        available: &[UnicodeRange],
    ) -> i32 {
        if requested.is_empty() {
            // No specific requirements, return total coverage
            return Self::calculate_unicode_coverage(available) as i32;
        }
        
        let mut total_coverage = 0u32;
        
        for req_range in requested {
            for avail_range in available {
                // Calculate overlap between requested and available ranges
                let overlap_start = req_range.start.max(avail_range.start);
                let overlap_end = req_range.end.min(avail_range.end);
                
                if overlap_start <= overlap_end {
                    // There is overlap
                    let overlap_size = overlap_end - overlap_start + 1;
                    total_coverage += overlap_size;
                }
            }
        }
        
        total_coverage as i32
    }

    pub fn calculate_style_score(original: &FcPattern, candidate: &FcPattern) -> i32 {

        let mut score = 0_i32;

        // Weight calculation with special handling for bold property
        if (original.bold == PatternMatch::True && candidate.weight == FcWeight::Bold)
            || (original.bold == PatternMatch::False && candidate.weight != FcWeight::Bold)
        {
            // No weight penalty when bold is requested and font has Bold weight
            // No weight penalty when non-bold is requested and font has non-Bold weight
        } else {
            // Apply normal weight difference penalty
            let weight_diff = (original.weight as i32 - candidate.weight as i32).abs();
            score += weight_diff as i32;
        }

        // Exact weight match bonus: reward fonts whose weight matches the request exactly,
        // with an extra bonus when both are Normal (the most common case for body text)
        if original.weight == candidate.weight {
            score -= 15;
            if original.weight == FcWeight::Normal {
                score -= 10; // Extra bonus for Normal-Normal match
            }
        }

        // Stretch calculation with special handling for condensed property
        if (original.condensed == PatternMatch::True && candidate.stretch.is_condensed())
            || (original.condensed == PatternMatch::False && !candidate.stretch.is_condensed())
        {
            // No stretch penalty when condensed is requested and font has condensed stretch
            // No stretch penalty when non-condensed is requested and font has non-condensed stretch
        } else {
            // Apply normal stretch difference penalty
            let stretch_diff = (original.stretch as i32 - candidate.stretch as i32).abs();
            score += (stretch_diff * 100) as i32;
        }

        // Handle style properties with standard penalties and bonuses
        let style_props = [
            (original.italic, candidate.italic, 300, 150),
            (original.oblique, candidate.oblique, 200, 100),
            (original.bold, candidate.bold, 300, 150),
            (original.monospace, candidate.monospace, 100, 50),
            (original.condensed, candidate.condensed, 100, 50),
        ];

        for (orig, cand, mismatch_penalty, dontcare_penalty) in style_props {
            if orig.needs_to_match() {
                if orig == PatternMatch::False && cand == PatternMatch::DontCare {
                    // Requesting non-italic but font doesn't declare: small penalty
                    // (less than a full mismatch but more than a perfect match)
                    score += dontcare_penalty / 2;
                } else if !orig.matches(&cand) {
                    if cand == PatternMatch::DontCare {
                        score += dontcare_penalty;
                    } else {
                        score += mismatch_penalty;
                    }
                } else if orig == PatternMatch::True && cand == PatternMatch::True {
                    // Give bonus for exact True match
                    score -= 20;
                } else if orig == PatternMatch::False && cand == PatternMatch::False {
                    // Give bonus for exact False match (prefer explicitly non-italic
                    // over fonts with unknown/DontCare italic status)
                    score -= 20;
                }
            } else {
                // orig == DontCare: prefer "normal" fonts over styled ones.
                // When the caller doesn't specify italic/bold/etc., a font
                // that IS italic/bold should score slightly worse than one
                // that isn't, so Regular is chosen over Italic by default.
                if cand == PatternMatch::True {
                    score += dontcare_penalty / 3;
                }
            }
        }

        // ── Name-based "base font" detection ──
        // The shorter the font name relative to its family, the more "basic" the
        // variant.  E.g. "System Font" (the base) should score better than
        // "System Font Regular Italic" (a variant) when the user hasn't
        // explicitly requested italic.
        if let (Some(name), Some(family)) = (&candidate.name, &candidate.family) {
            let name_lower = name.to_lowercase();
            let family_lower = family.to_lowercase();

            // Strip the family prefix from the name to get the "extra" part
            let extra = if name_lower.starts_with(&family_lower) {
                name_lower[family_lower.len()..].to_string()
            } else {
                String::new()
            };

            // Strip common neutral descriptors that don't indicate a style variant
            let stripped = extra
                .replace("regular", "")
                .replace("normal", "")
                .replace("book", "")
                .replace("roman", "");
            let stripped = stripped.trim();

            if stripped.is_empty() {
                // This is a "base font" – name is just the family (± "Regular")
                score -= 50;
            } else {
                // Name has extra style descriptors – add a penalty per extra word
                let extra_words = stripped.split_whitespace().count();
                score += (extra_words as i32) * 25;
            }
        }

        // ── Subfamily "Regular" bonus ──
        // Fonts whose OpenType subfamily is exactly "Regular" are the canonical
        // base variant and should be strongly preferred.
        if let Some(ref subfamily) = candidate.metadata.font_subfamily {
            let sf_lower = subfamily.to_lowercase();
            if sf_lower == "regular" {
                score -= 30;
            }
        }

        score
    }
}

#[cfg(all(feature = "std", feature = "parsing", target_os = "linux"))]
fn FcScanDirectories() -> Option<(Vec<(FcPattern, FcFontPath)>, BTreeMap<String, FcFontRenderConfig>)> {
    use std::fs;
    use std::path::Path;

    const BASE_FONTCONFIG_PATH: &str = "/etc/fonts/fonts.conf";

    if !Path::new(BASE_FONTCONFIG_PATH).exists() {
        return None;
    }

    let mut font_paths = Vec::with_capacity(32);
    let mut paths_to_visit = vec![(None, PathBuf::from(BASE_FONTCONFIG_PATH))];
    let mut render_configs: BTreeMap<String, FcFontRenderConfig> = BTreeMap::new();

    while let Some((prefix, path_to_visit)) = paths_to_visit.pop() {
        let path = match process_path(&prefix, path_to_visit, true) {
            Some(path) => path,
            None => continue,
        };

        let metadata = match fs::metadata(&path) {
            Ok(metadata) => metadata,
            Err(_) => continue,
        };

        if metadata.is_file() {
            let xml_utf8 = match fs::read_to_string(&path) {
                Ok(xml_utf8) => xml_utf8,
                Err(_) => continue,
            };

            if ParseFontsConf(&xml_utf8, &mut paths_to_visit, &mut font_paths).is_none() {
                continue;
            }

            // Also parse render config blocks from this file
            ParseFontsConfRenderConfig(&xml_utf8, &mut render_configs);
        } else if metadata.is_dir() {
            let dir_entries = match fs::read_dir(&path) {
                Ok(dir_entries) => dir_entries,
                Err(_) => continue,
            };

            for entry_result in dir_entries {
                let entry = match entry_result {
                    Ok(entry) => entry,
                    Err(_) => continue,
                };

                let entry_path = entry.path();

                // `fs::metadata` traverses symbolic links
                let entry_metadata = match fs::metadata(&entry_path) {
                    Ok(metadata) => metadata,
                    Err(_) => continue,
                };

                if !entry_metadata.is_file() {
                    continue;
                }

                let file_name = match entry_path.file_name() {
                    Some(name) => name,
                    None => continue,
                };

                let file_name_str = file_name.to_string_lossy();
                if file_name_str.starts_with(|c: char| c.is_ascii_digit())
                    && file_name_str.ends_with(".conf")
                {
                    paths_to_visit.push((None, entry_path));
                }
            }
        }
    }

    if font_paths.is_empty() {
        return None;
    }

    Some((FcScanDirectoriesInner(&font_paths), render_configs))
}

// Parses the fonts.conf file
#[cfg(all(feature = "std", feature = "parsing", target_os = "linux"))]
fn ParseFontsConf(
    input: &str,
    paths_to_visit: &mut Vec<(Option<String>, PathBuf)>,
    font_paths: &mut Vec<(Option<String>, String)>,
) -> Option<()> {
    use xmlparser::Token::*;
    use xmlparser::Tokenizer;

    const TAG_INCLUDE: &str = "include";
    const TAG_DIR: &str = "dir";
    const ATTRIBUTE_PREFIX: &str = "prefix";

    let mut current_prefix: Option<&str> = None;
    let mut current_path: Option<&str> = None;
    let mut is_in_include = false;
    let mut is_in_dir = false;

    for token_result in Tokenizer::from(input) {
        let token = match token_result {
            Ok(token) => token,
            Err(_) => return None,
        };

        match token {
            ElementStart { local, .. } => {
                if is_in_include || is_in_dir {
                    return None; /* error: nested tags */
                }

                match local.as_str() {
                    TAG_INCLUDE => {
                        is_in_include = true;
                    }
                    TAG_DIR => {
                        is_in_dir = true;
                    }
                    _ => continue,
                }

                current_path = None;
            }
            Text { text, .. } => {
                let text = text.as_str().trim();
                if text.is_empty() {
                    continue;
                }
                if is_in_include || is_in_dir {
                    current_path = Some(text);
                }
            }
            Attribute { local, value, .. } => {
                if !is_in_include && !is_in_dir {
                    continue;
                }
                // attribute on <include> or <dir> node
                if local.as_str() == ATTRIBUTE_PREFIX {
                    current_prefix = Some(value.as_str());
                }
            }
            ElementEnd { end, .. } => {
                let end_tag = match end {
                    xmlparser::ElementEnd::Close(_, a) => a,
                    _ => continue,
                };

                match end_tag.as_str() {
                    TAG_INCLUDE => {
                        if !is_in_include {
                            continue;
                        }

                        if let Some(current_path) = current_path.as_ref() {
                            paths_to_visit.push((
                                current_prefix.map(ToOwned::to_owned),
                                PathBuf::from(*current_path),
                            ));
                        }
                    }
                    TAG_DIR => {
                        if !is_in_dir {
                            continue;
                        }

                        if let Some(current_path) = current_path.as_ref() {
                            font_paths.push((
                                current_prefix.map(ToOwned::to_owned),
                                (*current_path).to_owned(),
                            ));
                        }
                    }
                    _ => continue,
                }

                is_in_include = false;
                is_in_dir = false;
                current_path = None;
                current_prefix = None;
            }
            _ => {}
        }
    }

    Some(())
}

/// Parses `<match target="font">` blocks from fonts.conf XML and returns
/// a map from family name to per-font rendering configuration.
///
/// Example fonts.conf snippet that this handles:
/// ```xml
/// <match target="font">
///   <test name="family"><string>Inconsolata</string></test>
///   <edit name="antialias" mode="assign"><bool>true</bool></edit>
///   <edit name="hintstyle" mode="assign"><const>hintslight</const></edit>
/// </match>
/// ```
#[cfg(all(feature = "std", feature = "parsing", target_os = "linux"))]
fn ParseFontsConfRenderConfig(
    input: &str,
    configs: &mut BTreeMap<String, FcFontRenderConfig>,
) {
    use xmlparser::Token::*;
    use xmlparser::Tokenizer;

    // Parser state machine
    #[derive(Clone, Copy, PartialEq)]
    enum State {
        /// Outside any relevant block
        Idle,
        /// Inside <match target="font">
        InMatchFont,
        /// Inside <test name="family"> within a match block
        InTestFamily,
        /// Inside <edit name="..."> within a match block
        InEdit,
        /// Inside a value element (<bool>, <double>, <const>, <string>) within <edit> or <test>
        InValue,
    }

    let mut state = State::Idle;
    let mut match_is_font_target = false;
    let mut current_family: Option<String> = None;
    let mut current_edit_name: Option<String> = None;
    let mut current_value: Option<String> = None;
    let mut value_tag: Option<String> = None;
    let mut config = FcFontRenderConfig::default();
    let mut in_test = false;
    let mut test_name: Option<String> = None;

    for token_result in Tokenizer::from(input) {
        let token = match token_result {
            Ok(token) => token,
            Err(_) => continue,
        };

        match token {
            ElementStart { local, .. } => {
                let tag = local.as_str();
                match tag {
                    "match" => {
                        // Reset state for a new match block
                        match_is_font_target = false;
                        current_family = None;
                        config = FcFontRenderConfig::default();
                    }
                    "test" if state == State::InMatchFont => {
                        in_test = true;
                        test_name = None;
                    }
                    "edit" if state == State::InMatchFont => {
                        current_edit_name = None;
                    }
                    "bool" | "double" | "const" | "string" | "int" => {
                        if state == State::InTestFamily || state == State::InEdit {
                            value_tag = Some(tag.to_owned());
                            current_value = None;
                        }
                    }
                    _ => {}
                }
            }
            Attribute { local, value, .. } => {
                let attr_name = local.as_str();
                let attr_value = value.as_str();

                match attr_name {
                    "target" => {
                        if attr_value == "font" {
                            match_is_font_target = true;
                        }
                    }
                    "name" => {
                        if in_test && state == State::InMatchFont {
                            test_name = Some(attr_value.to_owned());
                        } else if state == State::InMatchFont {
                            current_edit_name = Some(attr_value.to_owned());
                        }
                    }
                    _ => {}
                }
            }
            Text { text, .. } => {
                let text = text.as_str().trim();
                if !text.is_empty() && (state == State::InTestFamily || state == State::InEdit) {
                    current_value = Some(text.to_owned());
                }
            }
            ElementEnd { end, .. } => {
                match end {
                    xmlparser::ElementEnd::Open => {
                        // Tag just opened (after attributes processed)
                        if match_is_font_target && state == State::Idle {
                            state = State::InMatchFont;
                            match_is_font_target = false;
                        } else if in_test {
                            if test_name.as_deref() == Some("family") {
                                state = State::InTestFamily;
                            }
                            in_test = false;
                        } else if current_edit_name.is_some() && state == State::InMatchFont {
                            state = State::InEdit;
                        }
                    }
                    xmlparser::ElementEnd::Close(_, local) => {
                        let tag = local.as_str();
                        match tag {
                            "match" => {
                                // End of match block: store config if we have a family
                                if let Some(family) = current_family.take() {
                                    let empty = FcFontRenderConfig::default();
                                    if config != empty {
                                        configs.insert(family, config.clone());
                                    }
                                }
                                state = State::Idle;
                                config = FcFontRenderConfig::default();
                            }
                            "test" => {
                                if state == State::InTestFamily {
                                    // Extract the family name from the value we collected
                                    if let Some(ref val) = current_value {
                                        current_family = Some(val.clone());
                                    }
                                    state = State::InMatchFont;
                                }
                                current_value = None;
                                value_tag = None;
                            }
                            "edit" => {
                                if state == State::InEdit {
                                    // Apply the collected value to the config
                                    if let (Some(ref name), Some(ref val)) = (&current_edit_name, &current_value) {
                                        apply_edit_value(&mut config, name, val, value_tag.as_deref());
                                    }
                                    state = State::InMatchFont;
                                }
                                current_edit_name = None;
                                current_value = None;
                                value_tag = None;
                            }
                            "bool" | "double" | "const" | "string" | "int" => {
                                // value_tag and current_value already set by Text handler
                            }
                            _ => {}
                        }
                    }
                    xmlparser::ElementEnd::Empty => {
                        // Self-closing tags: nothing to do
                    }
                }
            }
            _ => {}
        }
    }
}

/// Apply a parsed edit value to the render config.
#[cfg(all(feature = "std", feature = "parsing", target_os = "linux"))]
fn apply_edit_value(
    config: &mut FcFontRenderConfig,
    edit_name: &str,
    value: &str,
    value_tag: Option<&str>,
) {
    match edit_name {
        "antialias" => {
            config.antialias = parse_bool_value(value);
        }
        "hinting" => {
            config.hinting = parse_bool_value(value);
        }
        "autohint" => {
            config.autohint = parse_bool_value(value);
        }
        "embeddedbitmap" => {
            config.embeddedbitmap = parse_bool_value(value);
        }
        "embolden" => {
            config.embolden = parse_bool_value(value);
        }
        "minspace" => {
            config.minspace = parse_bool_value(value);
        }
        "hintstyle" => {
            config.hintstyle = parse_hintstyle_const(value);
        }
        "rgba" => {
            config.rgba = parse_rgba_const(value);
        }
        "lcdfilter" => {
            config.lcdfilter = parse_lcdfilter_const(value);
        }
        "dpi" => {
            if let Ok(v) = value.parse::<f64>() {
                config.dpi = Some(v);
            }
        }
        "scale" => {
            if let Ok(v) = value.parse::<f64>() {
                config.scale = Some(v);
            }
        }
        _ => {
            // Unknown edit property, ignore
        }
    }
}

#[cfg(all(feature = "std", feature = "parsing", target_os = "linux"))]
fn parse_bool_value(value: &str) -> Option<bool> {
    match value {
        "true" => Some(true),
        "false" => Some(false),
        _ => None,
    }
}

#[cfg(all(feature = "std", feature = "parsing", target_os = "linux"))]
fn parse_hintstyle_const(value: &str) -> Option<FcHintStyle> {
    match value {
        "hintnone" => Some(FcHintStyle::None),
        "hintslight" => Some(FcHintStyle::Slight),
        "hintmedium" => Some(FcHintStyle::Medium),
        "hintfull" => Some(FcHintStyle::Full),
        _ => None,
    }
}

#[cfg(all(feature = "std", feature = "parsing", target_os = "linux"))]
fn parse_rgba_const(value: &str) -> Option<FcRgba> {
    match value {
        "unknown" => Some(FcRgba::Unknown),
        "rgb" => Some(FcRgba::Rgb),
        "bgr" => Some(FcRgba::Bgr),
        "vrgb" => Some(FcRgba::Vrgb),
        "vbgr" => Some(FcRgba::Vbgr),
        "none" => Some(FcRgba::None),
        _ => None,
    }
}

#[cfg(all(feature = "std", feature = "parsing", target_os = "linux"))]
fn parse_lcdfilter_const(value: &str) -> Option<FcLcdFilter> {
    match value {
        "lcdnone" => Some(FcLcdFilter::None),
        "lcddefault" => Some(FcLcdFilter::Default),
        "lcdlight" => Some(FcLcdFilter::Light),
        "lcdlegacy" => Some(FcLcdFilter::Legacy),
        _ => None,
    }
}

// Unicode range bit positions to actual ranges (full table from OpenType spec).
// Based on: https://learn.microsoft.com/en-us/typography/opentype/spec/os2#ur
#[cfg(all(feature = "std", feature = "parsing"))]
const UNICODE_RANGE_MAPPINGS: &[(usize, u32, u32)] = &[
    // ulUnicodeRange1 (bits 0-31)
    (0, 0x0000, 0x007F), // Basic Latin
    (1, 0x0080, 0x00FF), // Latin-1 Supplement
    (2, 0x0100, 0x017F), // Latin Extended-A
    (3, 0x0180, 0x024F), // Latin Extended-B
    (4, 0x0250, 0x02AF), // IPA Extensions
    (5, 0x02B0, 0x02FF), // Spacing Modifier Letters
    (6, 0x0300, 0x036F), // Combining Diacritical Marks
    (7, 0x0370, 0x03FF), // Greek and Coptic
    (8, 0x2C80, 0x2CFF), // Coptic
    (9, 0x0400, 0x04FF), // Cyrillic
    (10, 0x0530, 0x058F), // Armenian
    (11, 0x0590, 0x05FF), // Hebrew
    (12, 0x0600, 0x06FF), // Arabic
    (13, 0x0700, 0x074F), // Syriac
    (14, 0x0780, 0x07BF), // Thaana
    (15, 0x0900, 0x097F), // Devanagari
    (16, 0x0980, 0x09FF), // Bengali
    (17, 0x0A00, 0x0A7F), // Gurmukhi
    (18, 0x0A80, 0x0AFF), // Gujarati
    (19, 0x0B00, 0x0B7F), // Oriya
    (20, 0x0B80, 0x0BFF), // Tamil
    (21, 0x0C00, 0x0C7F), // Telugu
    (22, 0x0C80, 0x0CFF), // Kannada
    (23, 0x0D00, 0x0D7F), // Malayalam
    (24, 0x0E00, 0x0E7F), // Thai
    (25, 0x0E80, 0x0EFF), // Lao
    (26, 0x10A0, 0x10FF), // Georgian
    (27, 0x1B00, 0x1B7F), // Balinese
    (28, 0x1100, 0x11FF), // Hangul Jamo
    (29, 0x1E00, 0x1EFF), // Latin Extended Additional
    (30, 0x1F00, 0x1FFF), // Greek Extended
    (31, 0x2000, 0x206F), // General Punctuation
    // ulUnicodeRange2 (bits 32-63)
    (32, 0x2070, 0x209F), // Superscripts And Subscripts
    (33, 0x20A0, 0x20CF), // Currency Symbols
    (34, 0x20D0, 0x20FF), // Combining Diacritical Marks For Symbols
    (35, 0x2100, 0x214F), // Letterlike Symbols
    (36, 0x2150, 0x218F), // Number Forms
    (37, 0x2190, 0x21FF), // Arrows
    (38, 0x2200, 0x22FF), // Mathematical Operators
    (39, 0x2300, 0x23FF), // Miscellaneous Technical
    (40, 0x2400, 0x243F), // Control Pictures
    (41, 0x2440, 0x245F), // Optical Character Recognition
    (42, 0x2460, 0x24FF), // Enclosed Alphanumerics
    (43, 0x2500, 0x257F), // Box Drawing
    (44, 0x2580, 0x259F), // Block Elements
    (45, 0x25A0, 0x25FF), // Geometric Shapes
    (46, 0x2600, 0x26FF), // Miscellaneous Symbols
    (47, 0x2700, 0x27BF), // Dingbats
    (48, 0x3000, 0x303F), // CJK Symbols And Punctuation
    (49, 0x3040, 0x309F), // Hiragana
    (50, 0x30A0, 0x30FF), // Katakana
    (51, 0x3100, 0x312F), // Bopomofo
    (52, 0x3130, 0x318F), // Hangul Compatibility Jamo
    (53, 0x3190, 0x319F), // Kanbun
    (54, 0x31A0, 0x31BF), // Bopomofo Extended
    (55, 0x31C0, 0x31EF), // CJK Strokes
    (56, 0x31F0, 0x31FF), // Katakana Phonetic Extensions
    (57, 0x3200, 0x32FF), // Enclosed CJK Letters And Months
    (58, 0x3300, 0x33FF), // CJK Compatibility
    (59, 0x4E00, 0x9FFF), // CJK Unified Ideographs
    (60, 0xA000, 0xA48F), // Yi Syllables
    (61, 0xA490, 0xA4CF), // Yi Radicals
    (62, 0xAC00, 0xD7AF), // Hangul Syllables
    (63, 0xD800, 0xDFFF), // Non-Plane 0 (note: surrogates, not directly usable)
    // ulUnicodeRange3 (bits 64-95)
    (64, 0x10000, 0x10FFFF), // Phoenician and other non-BMP (bit 64 indicates non-BMP support)
    (65, 0xF900, 0xFAFF), // CJK Compatibility Ideographs
    (66, 0xFB00, 0xFB4F), // Alphabetic Presentation Forms
    (67, 0xFB50, 0xFDFF), // Arabic Presentation Forms-A
    (68, 0xFE00, 0xFE0F), // Variation Selectors
    (69, 0xFE10, 0xFE1F), // Vertical Forms
    (70, 0xFE20, 0xFE2F), // Combining Half Marks
    (71, 0xFE30, 0xFE4F), // CJK Compatibility Forms
    (72, 0xFE50, 0xFE6F), // Small Form Variants
    (73, 0xFE70, 0xFEFF), // Arabic Presentation Forms-B
    (74, 0xFF00, 0xFFEF), // Halfwidth And Fullwidth Forms
    (75, 0xFFF0, 0xFFFF), // Specials
    (76, 0x0F00, 0x0FFF), // Tibetan
    (77, 0x0700, 0x074F), // Syriac
    (78, 0x0780, 0x07BF), // Thaana
    (79, 0x0D80, 0x0DFF), // Sinhala
    (80, 0x1000, 0x109F), // Myanmar
    (81, 0x1200, 0x137F), // Ethiopic
    (82, 0x13A0, 0x13FF), // Cherokee
    (83, 0x1400, 0x167F), // Unified Canadian Aboriginal Syllabics
    (84, 0x1680, 0x169F), // Ogham
    (85, 0x16A0, 0x16FF), // Runic
    (86, 0x1780, 0x17FF), // Khmer
    (87, 0x1800, 0x18AF), // Mongolian
    (88, 0x2800, 0x28FF), // Braille Patterns
    (89, 0xA000, 0xA48F), // Yi Syllables
    (90, 0x1680, 0x169F), // Ogham
    (91, 0x16A0, 0x16FF), // Runic
    (92, 0x1700, 0x171F), // Tagalog
    (93, 0x1720, 0x173F), // Hanunoo
    (94, 0x1740, 0x175F), // Buhid
    (95, 0x1760, 0x177F), // Tagbanwa
    // ulUnicodeRange4 (bits 96-127)
    (96, 0x1900, 0x194F), // Limbu
    (97, 0x1950, 0x197F), // Tai Le
    (98, 0x1980, 0x19DF), // New Tai Lue
    (99, 0x1A00, 0x1A1F), // Buginese
    (100, 0x2C00, 0x2C5F), // Glagolitic
    (101, 0x2D30, 0x2D7F), // Tifinagh
    (102, 0x4DC0, 0x4DFF), // Yijing Hexagram Symbols
    (103, 0xA800, 0xA82F), // Syloti Nagri
    (104, 0x10000, 0x1007F), // Linear B Syllabary
    (105, 0x10080, 0x100FF), // Linear B Ideograms
    (106, 0x10100, 0x1013F), // Aegean Numbers
    (107, 0x10140, 0x1018F), // Ancient Greek Numbers
    (108, 0x10300, 0x1032F), // Old Italic
    (109, 0x10330, 0x1034F), // Gothic
    (110, 0x10380, 0x1039F), // Ugaritic
    (111, 0x103A0, 0x103DF), // Old Persian
    (112, 0x10400, 0x1044F), // Deseret
    (113, 0x10450, 0x1047F), // Shavian
    (114, 0x10480, 0x104AF), // Osmanya
    (115, 0x10800, 0x1083F), // Cypriot Syllabary
    (116, 0x10A00, 0x10A5F), // Kharoshthi
    (117, 0x1D000, 0x1D0FF), // Byzantine Musical Symbols
    (118, 0x1D100, 0x1D1FF), // Musical Symbols
    (119, 0x1D200, 0x1D24F), // Ancient Greek Musical Notation
    (120, 0x1D300, 0x1D35F), // Tai Xuan Jing Symbols
    (121, 0x1D400, 0x1D7FF), // Mathematical Alphanumeric Symbols
    (122, 0x1F000, 0x1F02F), // Mahjong Tiles
    (123, 0x1F030, 0x1F09F), // Domino Tiles
    (124, 0x1F300, 0x1F9FF), // Miscellaneous Symbols And Pictographs (Emoji)
    (125, 0x1F680, 0x1F6FF), // Transport And Map Symbols
    (126, 0x1F700, 0x1F77F), // Alchemical Symbols
    (127, 0x1F900, 0x1F9FF), // Supplemental Symbols and Pictographs
];

/// Intermediate parsed data from a single font face within a font file.
/// Used to share parsing logic between `FcParseFont` and `FcParseFontBytesInner`.
#[cfg(all(feature = "std", feature = "parsing"))]
struct ParsedFontFace {
    pattern: FcPattern,
    font_index: usize,
}

/// Parse all font table data from a single font face and return the extracted patterns.
///
/// This is the shared core of `FcParseFont` and `FcParseFontBytesInner`:
/// TTC detection, font table parsing, OS/2/head/post reading, unicode range extraction,
/// CMAP verification, monospace detection, metadata extraction, and pattern creation.
#[cfg(all(feature = "std", feature = "parsing"))]
fn parse_font_faces(font_bytes: &[u8]) -> Option<Vec<ParsedFontFace>> {
    use allsorts::{
        binary::read::ReadScope,
        font_data::FontData,
        get_name::fontcode_get_name,
        post::PostTable,
        tables::{
            os2::Os2, HeadTable, NameTable,
        },
        tag,
    };
    use std::collections::BTreeSet;

    const FONT_SPECIFIER_NAME_ID: u16 = 4;
    const FONT_SPECIFIER_FAMILY_ID: u16 = 1;

    let max_fonts = if font_bytes.len() >= 12 && &font_bytes[0..4] == b"ttcf" {
        // Read numFonts from TTC header (offset 8, 4 bytes)
        let num_fonts =
            u32::from_be_bytes([font_bytes[8], font_bytes[9], font_bytes[10], font_bytes[11]]);
        // Cap at a reasonable maximum as a safety measure
        std::cmp::min(num_fonts as usize, 100)
    } else {
        // Not a collection, just one font
        1
    };

    let scope = ReadScope::new(font_bytes);
    let font_file = scope.read::<FontData<'_>>().ok()?;

    // Handle collections properly by iterating through all fonts
    let mut results = Vec::new();

    for font_index in 0..max_fonts {
        let provider = font_file.table_provider(font_index).ok()?;
        let head_data = provider.table_data(tag::HEAD).ok()??.into_owned();
        let head_table = ReadScope::new(&head_data).read::<HeadTable>().ok()?;

        let is_bold = head_table.is_bold();
        let is_italic = head_table.is_italic();
        let mut detected_monospace = None;

        let post_data = provider.table_data(tag::POST).ok()??;
        if let Ok(post_table) = ReadScope::new(&post_data).read::<PostTable>() {
            // isFixedPitch here - https://learn.microsoft.com/en-us/typography/opentype/spec/post#header
            detected_monospace = Some(post_table.header.is_fixed_pitch != 0);
        }

        // Get font properties from OS/2 table
        let os2_data = provider.table_data(tag::OS_2).ok()??;
        let os2_table = ReadScope::new(&os2_data)
            .read_dep::<Os2>(os2_data.len())
            .ok()?;

        // Extract additional style information
        let is_oblique = os2_table
            .fs_selection
            .contains(allsorts::tables::os2::FsSelection::OBLIQUE);
        let weight = FcWeight::from_u16(os2_table.us_weight_class);
        let stretch = FcStretch::from_u16(os2_table.us_width_class);

        // Extract unicode ranges from OS/2 table (fast, but may be inaccurate)
        // These are hints about what the font *should* support
        // For actual glyph coverage verification, query the font file directly
        let mut unicode_ranges = Vec::new();

        // Process the 4 Unicode range bitfields from OS/2 table
        let os2_ranges = [
            os2_table.ul_unicode_range1,
            os2_table.ul_unicode_range2,
            os2_table.ul_unicode_range3,
            os2_table.ul_unicode_range4,
        ];

        for &(bit, start, end) in UNICODE_RANGE_MAPPINGS {
            let range_idx = bit / 32;
            let bit_pos = bit % 32;
            if range_idx < 4 && (os2_ranges[range_idx] & (1 << bit_pos)) != 0 {
                unicode_ranges.push(UnicodeRange { start, end });
            }
        }

        // Verify OS/2 reported ranges against actual CMAP support
        // OS/2 ulUnicodeRange bits can be unreliable - fonts may claim support
        // for ranges they don't actually have glyphs for
        unicode_ranges = verify_unicode_ranges_with_cmap(&provider, unicode_ranges);

        // If still empty (OS/2 had no ranges or all were invalid), do full CMAP analysis
        if unicode_ranges.is_empty() {
            if let Some(cmap_ranges) = analyze_cmap_coverage(&provider) {
                unicode_ranges = cmap_ranges;
            }
        }

        // Use the shared detect_monospace helper for PANOSE + hmtx fallback
        let is_monospace = detect_monospace(&provider, &os2_table, detected_monospace)
            .unwrap_or(false);

        let name_data = provider.table_data(tag::NAME).ok()??.into_owned();
        let name_table = ReadScope::new(&name_data).read::<NameTable>().ok()?;

        // Extract metadata from name table
        let mut metadata = FcFontMetadata::default();

        const NAME_ID_COPYRIGHT: u16 = 0;
        const NAME_ID_FAMILY: u16 = 1;
        const NAME_ID_SUBFAMILY: u16 = 2;
        const NAME_ID_UNIQUE_ID: u16 = 3;
        const NAME_ID_FULL_NAME: u16 = 4;
        const NAME_ID_VERSION: u16 = 5;
        const NAME_ID_POSTSCRIPT_NAME: u16 = 6;
        const NAME_ID_TRADEMARK: u16 = 7;
        const NAME_ID_MANUFACTURER: u16 = 8;
        const NAME_ID_DESIGNER: u16 = 9;
        const NAME_ID_DESCRIPTION: u16 = 10;
        const NAME_ID_VENDOR_URL: u16 = 11;
        const NAME_ID_DESIGNER_URL: u16 = 12;
        const NAME_ID_LICENSE: u16 = 13;
        const NAME_ID_LICENSE_URL: u16 = 14;
        const NAME_ID_PREFERRED_FAMILY: u16 = 16;
        const NAME_ID_PREFERRED_SUBFAMILY: u16 = 17;

        metadata.copyright = get_name_string(&name_data, NAME_ID_COPYRIGHT);
        metadata.font_family = get_name_string(&name_data, NAME_ID_FAMILY);
        metadata.font_subfamily = get_name_string(&name_data, NAME_ID_SUBFAMILY);
        metadata.full_name = get_name_string(&name_data, NAME_ID_FULL_NAME);
        metadata.unique_id = get_name_string(&name_data, NAME_ID_UNIQUE_ID);
        metadata.version = get_name_string(&name_data, NAME_ID_VERSION);
        metadata.postscript_name = get_name_string(&name_data, NAME_ID_POSTSCRIPT_NAME);
        metadata.trademark = get_name_string(&name_data, NAME_ID_TRADEMARK);
        metadata.manufacturer = get_name_string(&name_data, NAME_ID_MANUFACTURER);
        metadata.designer = get_name_string(&name_data, NAME_ID_DESIGNER);
        metadata.id_description = get_name_string(&name_data, NAME_ID_DESCRIPTION);
        metadata.designer_url = get_name_string(&name_data, NAME_ID_DESIGNER_URL);
        metadata.manufacturer_url = get_name_string(&name_data, NAME_ID_VENDOR_URL);
        metadata.license = get_name_string(&name_data, NAME_ID_LICENSE);
        metadata.license_url = get_name_string(&name_data, NAME_ID_LICENSE_URL);
        metadata.preferred_family = get_name_string(&name_data, NAME_ID_PREFERRED_FAMILY);
        metadata.preferred_subfamily = get_name_string(&name_data, NAME_ID_PREFERRED_SUBFAMILY);

        // One font can support multiple patterns
        let mut f_family = None;

        let patterns = name_table
            .name_records
            .iter()
            .filter_map(|name_record| {
                let name_id = name_record.name_id;
                if name_id == FONT_SPECIFIER_FAMILY_ID {
                    if let Ok(Some(family)) =
                        fontcode_get_name(&name_data, FONT_SPECIFIER_FAMILY_ID)
                    {
                        f_family = Some(family);
                    }
                    None
                } else if name_id == FONT_SPECIFIER_NAME_ID {
                    let family = f_family.as_ref()?;
                    let name = fontcode_get_name(&name_data, FONT_SPECIFIER_NAME_ID).ok()??;
                    if name.to_bytes().is_empty() {
                        None
                    } else {
                        let mut name_str =
                            String::from_utf8_lossy(name.to_bytes()).to_string();
                        let mut family_str =
                            String::from_utf8_lossy(family.as_bytes()).to_string();
                        if name_str.starts_with('.') {
                            name_str = name_str[1..].to_string();
                        }
                        if family_str.starts_with('.') {
                            family_str = family_str[1..].to_string();
                        }
                        Some((
                            FcPattern {
                                name: Some(name_str),
                                family: Some(family_str),
                                bold: if is_bold {
                                    PatternMatch::True
                                } else {
                                    PatternMatch::False
                                },
                                italic: if is_italic {
                                    PatternMatch::True
                                } else {
                                    PatternMatch::False
                                },
                                oblique: if is_oblique {
                                    PatternMatch::True
                                } else {
                                    PatternMatch::False
                                },
                                monospace: if is_monospace {
                                    PatternMatch::True
                                } else {
                                    PatternMatch::False
                                },
                                condensed: if stretch <= FcStretch::Condensed {
                                    PatternMatch::True
                                } else {
                                    PatternMatch::False
                                },
                                weight,
                                stretch,
                                unicode_ranges: unicode_ranges.clone(),
                                metadata: metadata.clone(),
                                render_config: FcFontRenderConfig::default(),
                            },
                            font_index,
                        ))
                    }
                } else {
                    None
                }
            })
            .collect::<BTreeSet<_>>();

        results.extend(patterns.into_iter().map(|(pat, idx)| ParsedFontFace {
            pattern: pat,
            font_index: idx,
        }));
    }

    if results.is_empty() {
        None
    } else {
        Some(results)
    }
}

// Remaining implementation for font scanning, parsing, etc.
#[cfg(all(feature = "std", feature = "parsing"))]
pub(crate) fn FcParseFont(filepath: &PathBuf) -> Option<Vec<(FcPattern, FcFontPath)>> {
    #[cfg(all(not(target_family = "wasm"), feature = "std"))]
    use mmapio::MmapOptions;
    use std::fs::File;

    // Try parsing the font file and see if the postscript name matches
    let file = File::open(filepath).ok()?;

    #[cfg(all(not(target_family = "wasm"), feature = "std"))]
    let font_bytes = unsafe { MmapOptions::new().map(&file).ok()? };

    #[cfg(not(all(not(target_family = "wasm"), feature = "std")))]
    let font_bytes = std::fs::read(filepath).ok()?;

    let faces = parse_font_faces(&font_bytes[..])?;
    let path_str = filepath.to_string_lossy().to_string();
    // Hash once per file — every face of a .ttc shares this value,
    // so the shared-bytes cache can return the same Arc<[u8]> for
    // all of them. Use the cheap sampled variant so the scout doesn't
    // page-fault the full file into RSS just to produce a dedup key.
    let bytes_hash = crate::utils::content_dedup_hash_u64(&font_bytes[..]);

    Some(
        faces
            .into_iter()
            .map(|face| {
                (
                    face.pattern,
                    FcFontPath {
                        path: path_str.clone(),
                        font_index: face.font_index,
                        bytes_hash,
                    },
                )
            })
            .collect(),
    )
}

/// Coverage info returned by a fast-probe parse.
///
/// Produced by [`FcParseFontFaceFast`] / [`FcProbeCoverage`] — the
/// v4.2 "cheap cmap-only" entry point. Unlike `parse_font_faces`,
/// this path does **not** read NAME, OS/2, POST, HHEA, HMTX, HEAD's
/// style metadata, or anything else. It only reads the table
/// directory, `head.macStyle` (2 bytes), and the cmap subtable that
/// matches the codepoints we care about. ~1 ms/face on warm FS
/// cache vs ~13 ms for the full parse.
///
/// The `pattern.unicode_ranges` is populated from the *actual* cmap
/// contents (one `UnicodeRange` per covered codepoint in the input
/// set) rather than the OS/2 `ulUnicodeRange` bitfield. That's more
/// precise (OS/2 bits lie on many fonts — they're hints, not ground
/// truth) and means `FontFallbackChain::resolve_char`'s coverage
/// check matches what the shaper can actually render.
#[cfg(all(feature = "std", feature = "parsing"))]
#[derive(Debug, Clone)]
pub struct FastCoverage {
    /// Metadata pattern with `unicode_ranges` populated from the
    /// codepoints this face covered from the request set. `name` /
    /// `family` fields are left empty — callers already have the
    /// filename-guessed family in [`FcFontRegistry.known_paths`];
    /// we avoid the NAME table read entirely.
    pub pattern: FcPattern,
    /// Subset of the input codepoints that this face covers (maps
    /// to a non-zero gid via the best cmap subtable). May be empty
    /// if the face covers none, in which case callers should fall
    /// through to the next candidate path.
    pub covered: alloc::collections::BTreeSet<char>,
    /// `head.macStyle.bold` (bit 0).
    pub is_bold: bool,
    /// `head.macStyle.italic` (bit 1).
    pub is_italic: bool,
}

/// Fast per-face coverage probe.
///
/// Opens the provided font bytes as a `FontData` (detects TTC
/// collections), walks the given face, reads `head.macStyle` for
/// bold/italic flags, picks the best cmap subtable, and records
/// which of the requested codepoints have a non-zero gid.
///
/// Cost: table-dir parse + head (54 bytes) + cmap (5-100 KiB,
/// faulted in from mmap). No heap allocation besides the
/// covered-codepoints set and the returned `FcPattern`.
///
/// Returns `None` only if the font bytes are structurally bad or
/// the face index is out of range — empty coverage returns
/// `Some` with `covered.is_empty()`, so the caller can distinguish
/// "this face doesn't have the char we want" (try next face) from
/// "this file is corrupt" (give up on the whole file).
#[cfg(all(feature = "std", feature = "parsing"))]
#[allow(non_snake_case)]
pub fn FcParseFontFaceFast(
    font_bytes: &[u8],
    font_index: usize,
    codepoints: &alloc::collections::BTreeSet<char>,
) -> Option<FastCoverage> {
    use allsorts::{
        binary::read::ReadScope,
        font_data::FontData,
        tables::{
            cmap::{Cmap, CmapSubtable},
            FontTableProvider, HeadTable,
        },
        tag,
    };

    let scope = ReadScope::new(font_bytes);
    let font_file = scope.read::<FontData<'_>>().ok()?;
    let provider = font_file.table_provider(font_index).ok()?;

    // head — 54 bytes, macStyle at offset 44. Cheap.
    let head_data = provider.table_data(tag::HEAD).ok()??;
    let head_table = ReadScope::new(&head_data).read::<HeadTable>().ok()?;
    let is_bold = head_table.is_bold();
    let is_italic = head_table.is_italic();

    // cmap — find the best Unicode subtable, probe each codepoint.
    // The mmap page-cache only faults in the bytes we touch.
    let cmap_data = provider.table_data(tag::CMAP).ok()??;
    let cmap = ReadScope::new(&cmap_data).read::<Cmap<'_>>().ok()?;
    let encoding_record = find_best_cmap_subtable(&cmap)?;
    let cmap_subtable = ReadScope::new(&cmap_data)
        .offset(encoding_record.offset as usize)
        .read::<CmapSubtable<'_>>()
        .ok()?;

    let mut covered: alloc::collections::BTreeSet<char> =
        alloc::collections::BTreeSet::new();
    let mut covered_ranges: Vec<UnicodeRange> = Vec::new();
    for ch in codepoints {
        let cp = *ch as u32;
        if let Ok(Some(gid)) = cmap_subtable.map_glyph(cp) {
            if gid != 0 {
                covered.insert(*ch);
                // Accumulate into ranges for the FcPattern. Merge
                // adjacent codepoints so `unicode_ranges` stays
                // compact (common case on Western text: one range).
                if let Some(last) = covered_ranges.last_mut() {
                    if cp == last.end + 1 {
                        last.end = cp;
                        continue;
                    }
                }
                covered_ranges.push(UnicodeRange { start: cp, end: cp });
            }
        }
    }

    let weight = if is_bold {
        FcWeight::Bold
    } else {
        FcWeight::Normal
    };
    let italic_match = if is_italic {
        PatternMatch::True
    } else {
        PatternMatch::False
    };

    let pattern = FcPattern {
        name: None,
        family: None,
        weight,
        italic: italic_match,
        oblique: PatternMatch::DontCare,
        monospace: PatternMatch::DontCare,
        unicode_ranges: covered_ranges,
        ..Default::default()
    };

    Some(FastCoverage {
        pattern,
        covered,
        is_bold,
        is_italic,
    })
}

/// Count the number of faces inside a TTC, or `1` for a single-face
/// font file. Used by [`FcFontRegistry::request_fonts_fast`] to
/// iterate every face in a `.ttc` without paying the full-parse
/// cost (the TTC header is 12 bytes).
#[cfg(all(feature = "std", feature = "parsing"))]
#[allow(non_snake_case)]
pub fn FcCountFontFaces(font_bytes: &[u8]) -> usize {
    if font_bytes.len() >= 12 && &font_bytes[0..4] == b"ttcf" {
        let num_fonts = u32::from_be_bytes([
            font_bytes[8], font_bytes[9], font_bytes[10], font_bytes[11],
        ]);
        // Same cap as parse_font_faces, for safety.
        std::cmp::min(num_fonts as usize, 100).max(1)
    } else {
        1
    }
}

/// Parse font bytes and extract font patterns for in-memory fonts.
///
/// This is the public API for parsing in-memory font data to create
/// `(FcPattern, FcFont)` tuples that can be added to an `FcFontCache`
/// via `with_memory_fonts()`.
///
/// # Arguments
/// * `font_bytes` - The raw bytes of a TrueType/OpenType font file
/// * `font_id` - An identifier string for this font (used internally)
///
/// # Returns
/// A vector of `(FcPattern, FcFont)` tuples, one for each font face in the file.
/// Returns `None` if the font could not be parsed.
///
/// # Example
/// ```ignore
/// use rust_fontconfig::{FcFontCache, FcParseFontBytes};
///
/// let font_bytes = include_bytes!("path/to/font.ttf");
/// let mut cache = FcFontCache::default();
///
/// if let Some(fonts) = FcParseFontBytes(font_bytes, "MyFont") {
///     cache.with_memory_fonts(fonts);
/// }
/// ```
#[cfg(all(feature = "std", feature = "parsing"))]
#[allow(non_snake_case)]
pub fn FcParseFontBytes(font_bytes: &[u8], font_id: &str) -> Option<Vec<(FcPattern, FcFont)>> {
    FcParseFontBytesInner(font_bytes, font_id)
}

/// Internal implementation for parsing font bytes.
/// Delegates to `parse_font_faces` for shared parsing logic and wraps results as `FcFont`.
#[cfg(all(feature = "std", feature = "parsing"))]
fn FcParseFontBytesInner(font_bytes: &[u8], font_id: &str) -> Option<Vec<(FcPattern, FcFont)>> {
    let faces = parse_font_faces(font_bytes)?;
    let id = font_id.to_string();
    let bytes = font_bytes.to_vec();

    Some(
        faces
            .into_iter()
            .map(|face| {
                (
                    face.pattern,
                    FcFont {
                        bytes: bytes.clone(),
                        font_index: face.font_index,
                        id: id.clone(),
                    },
                )
            })
            .collect(),
    )
}

#[cfg(all(feature = "std", feature = "parsing"))]
fn FcScanDirectoriesInner(paths: &[(Option<String>, String)]) -> Vec<(FcPattern, FcFontPath)> {
    #[cfg(feature = "multithreading")]
    {
        use rayon::prelude::*;

        // scan directories in parallel
        paths
            .par_iter()
            .filter_map(|(prefix, p)| {
                process_path(prefix, PathBuf::from(p), false).map(FcScanSingleDirectoryRecursive)
            })
            .flatten()
            .collect()
    }
    #[cfg(not(feature = "multithreading"))]
    {
        paths
            .iter()
            .filter_map(|(prefix, p)| {
                process_path(prefix, PathBuf::from(p), false).map(FcScanSingleDirectoryRecursive)
            })
            .flatten()
            .collect()
    }
}

/// Recursively collect all files from a directory (no parsing, no allsorts).
#[cfg(feature = "std")]
fn FcCollectFontFilesRecursive(dir: PathBuf) -> Vec<PathBuf> {
    let mut files = Vec::new();
    let mut dirs_to_parse = vec![dir];

    loop {
        let mut new_dirs = Vec::new();
        for dir in &dirs_to_parse {
            let entries = match std::fs::read_dir(dir) {
                Ok(o) => o,
                Err(_) => continue,
            };
            for entry in entries.flatten() {
                let path = entry.path();
                if path.is_dir() {
                    new_dirs.push(path);
                } else {
                    files.push(path);
                }
            }
        }
        if new_dirs.is_empty() {
            break;
        }
        dirs_to_parse = new_dirs;
    }

    files
}

#[cfg(all(feature = "std", feature = "parsing"))]
fn FcScanSingleDirectoryRecursive(dir: PathBuf) -> Vec<(FcPattern, FcFontPath)> {
    let files = FcCollectFontFilesRecursive(dir);
    FcParseFontFiles(&files)
}

#[cfg(all(feature = "std", feature = "parsing"))]
fn FcParseFontFiles(files_to_parse: &[PathBuf]) -> Vec<(FcPattern, FcFontPath)> {
    let result = {
        #[cfg(feature = "multithreading")]
        {
            use rayon::prelude::*;

            files_to_parse
                .par_iter()
                .filter_map(|file| FcParseFont(file))
                .collect::<Vec<Vec<_>>>()
        }
        #[cfg(not(feature = "multithreading"))]
        {
            files_to_parse
                .iter()
                .filter_map(|file| FcParseFont(file))
                .collect::<Vec<Vec<_>>>()
        }
    };

    result.into_iter().flat_map(|f| f.into_iter()).collect()
}

#[cfg(all(feature = "std", feature = "parsing"))]
/// Takes a path & prefix and resolves them to a usable path, or `None` if they're unsupported/unavailable.
///
/// Behaviour is based on: https://www.freedesktop.org/software/fontconfig/fontconfig-user.html
fn process_path(
    prefix: &Option<String>,
    mut path: PathBuf,
    is_include_path: bool,
) -> Option<PathBuf> {
    use std::env::var;

    const HOME_SHORTCUT: &str = "~";
    const CWD_PATH: &str = ".";

    const HOME_ENV_VAR: &str = "HOME";
    const XDG_CONFIG_HOME_ENV_VAR: &str = "XDG_CONFIG_HOME";
    const XDG_CONFIG_HOME_DEFAULT_PATH_SUFFIX: &str = ".config";
    const XDG_DATA_HOME_ENV_VAR: &str = "XDG_DATA_HOME";
    const XDG_DATA_HOME_DEFAULT_PATH_SUFFIX: &str = ".local/share";

    const PREFIX_CWD: &str = "cwd";
    const PREFIX_DEFAULT: &str = "default";
    const PREFIX_XDG: &str = "xdg";

    // These three could, in theory, be cached, but the work required to do so outweighs the minor benefits
    fn get_home_value() -> Option<PathBuf> {
        var(HOME_ENV_VAR).ok().map(PathBuf::from)
    }
    fn get_xdg_config_home_value() -> Option<PathBuf> {
        var(XDG_CONFIG_HOME_ENV_VAR)
            .ok()
            .map(PathBuf::from)
            .or_else(|| {
                get_home_value()
                    .map(|home_path| home_path.join(XDG_CONFIG_HOME_DEFAULT_PATH_SUFFIX))
            })
    }
    fn get_xdg_data_home_value() -> Option<PathBuf> {
        var(XDG_DATA_HOME_ENV_VAR)
            .ok()
            .map(PathBuf::from)
            .or_else(|| {
                get_home_value().map(|home_path| home_path.join(XDG_DATA_HOME_DEFAULT_PATH_SUFFIX))
            })
    }

    // Resolve the tilde character in the path, if present
    if path.starts_with(HOME_SHORTCUT) {
        if let Some(home_path) = get_home_value() {
            path = home_path.join(
                path.strip_prefix(HOME_SHORTCUT)
                    .expect("already checked that it starts with the prefix"),
            );
        } else {
            return None;
        }
    }

    // Resolve prefix values
    match prefix {
        Some(prefix) => match prefix.as_str() {
            PREFIX_CWD | PREFIX_DEFAULT => {
                let mut new_path = PathBuf::from(CWD_PATH);
                new_path.push(path);

                Some(new_path)
            }
            PREFIX_XDG => {
                if is_include_path {
                    get_xdg_config_home_value()
                        .map(|xdg_config_home_path| xdg_config_home_path.join(path))
                } else {
                    get_xdg_data_home_value()
                        .map(|xdg_data_home_path| xdg_data_home_path.join(path))
                }
            }
            _ => None, // Unsupported prefix
        },
        None => Some(path),
    }
}

// Helper function to extract a string from the name table
#[cfg(all(feature = "std", feature = "parsing"))]
fn get_name_string(name_data: &[u8], name_id: u16) -> Option<String> {
    fontcode_get_name(name_data, name_id)
        .ok()
        .flatten()
        .map(|name| String::from_utf8_lossy(name.to_bytes()).to_string())
}

/// Representative test codepoints for each Unicode block.
/// These are carefully chosen to be actual script characters (not punctuation/symbols)
/// that a font claiming to support this script should definitely have.
#[cfg(all(feature = "std", feature = "parsing"))]
fn get_verification_codepoints(start: u32, end: u32) -> Vec<u32> {
    match start {
        // Basic Latin - test uppercase, lowercase, and digits
        0x0000 => vec!['A' as u32, 'M' as u32, 'Z' as u32, 'a' as u32, 'm' as u32, 'z' as u32],
        // Latin-1 Supplement - common accented letters
        0x0080 => vec![0x00C0, 0x00C9, 0x00D1, 0x00E0, 0x00E9, 0x00F1], // À É Ñ à é ñ
        // Latin Extended-A
        0x0100 => vec![0x0100, 0x0110, 0x0141, 0x0152, 0x0160], // Ā Đ Ł Œ Š
        // Latin Extended-B
        0x0180 => vec![0x0180, 0x01A0, 0x01B0, 0x01CD], // ƀ Ơ ư Ǎ
        // IPA Extensions
        0x0250 => vec![0x0250, 0x0259, 0x026A, 0x0279], // ɐ ə ɪ ɹ
        // Greek and Coptic
        0x0370 => vec![0x0391, 0x0392, 0x0393, 0x03B1, 0x03B2, 0x03C9], // Α Β Γ α β ω
        // Cyrillic
        0x0400 => vec![0x0410, 0x0411, 0x0412, 0x0430, 0x0431, 0x042F], // А Б В а б Я
        // Armenian
        0x0530 => vec![0x0531, 0x0532, 0x0533, 0x0561, 0x0562], // Ա Բ Գ ա բ
        // Hebrew
        0x0590 => vec![0x05D0, 0x05D1, 0x05D2, 0x05E9, 0x05EA], // א ב ג ש ת
        // Arabic
        0x0600 => vec![0x0627, 0x0628, 0x062A, 0x062C, 0x0645], // ا ب ت ج م
        // Syriac
        0x0700 => vec![0x0710, 0x0712, 0x0713, 0x0715], // ܐ ܒ ܓ ܕ
        // Devanagari
        0x0900 => vec![0x0905, 0x0906, 0x0915, 0x0916, 0x0939], // अ आ क ख ह
        // Bengali
        0x0980 => vec![0x0985, 0x0986, 0x0995, 0x0996], // অ আ ক খ
        // Gurmukhi
        0x0A00 => vec![0x0A05, 0x0A06, 0x0A15, 0x0A16], // ਅ ਆ ਕ ਖ
        // Gujarati
        0x0A80 => vec![0x0A85, 0x0A86, 0x0A95, 0x0A96], // અ આ ક ખ
        // Oriya
        0x0B00 => vec![0x0B05, 0x0B06, 0x0B15, 0x0B16], // ଅ ଆ କ ଖ
        // Tamil
        0x0B80 => vec![0x0B85, 0x0B86, 0x0B95, 0x0BA4], // அ ஆ க த
        // Telugu
        0x0C00 => vec![0x0C05, 0x0C06, 0x0C15, 0x0C16], // అ ఆ క ఖ
        // Kannada
        0x0C80 => vec![0x0C85, 0x0C86, 0x0C95, 0x0C96], // ಅ ಆ ಕ ಖ
        // Malayalam
        0x0D00 => vec![0x0D05, 0x0D06, 0x0D15, 0x0D16], // അ ആ ക ഖ
        // Thai
        0x0E00 => vec![0x0E01, 0x0E02, 0x0E04, 0x0E07, 0x0E40], // ก ข ค ง เ
        // Lao
        0x0E80 => vec![0x0E81, 0x0E82, 0x0E84, 0x0E87], // ກ ຂ ຄ ງ
        // Myanmar
        0x1000 => vec![0x1000, 0x1001, 0x1002, 0x1010, 0x1019], // က ခ ဂ တ မ
        // Georgian
        0x10A0 => vec![0x10D0, 0x10D1, 0x10D2, 0x10D3], // ა ბ გ დ
        // Hangul Jamo
        0x1100 => vec![0x1100, 0x1102, 0x1103, 0x1161, 0x1162], // ᄀ ᄂ ᄃ ᅡ ᅢ
        // Ethiopic
        0x1200 => vec![0x1200, 0x1208, 0x1210, 0x1218], // ሀ ለ ሐ መ
        // Cherokee
        0x13A0 => vec![0x13A0, 0x13A1, 0x13A2, 0x13A3], // Ꭰ Ꭱ Ꭲ Ꭳ
        // Khmer
        0x1780 => vec![0x1780, 0x1781, 0x1782, 0x1783], // ក ខ គ ឃ
        // Mongolian
        0x1800 => vec![0x1820, 0x1821, 0x1822, 0x1823], // ᠠ ᠡ ᠢ ᠣ
        // Hiragana
        0x3040 => vec![0x3042, 0x3044, 0x3046, 0x304B, 0x304D, 0x3093], // あ い う か き ん
        // Katakana
        0x30A0 => vec![0x30A2, 0x30A4, 0x30A6, 0x30AB, 0x30AD, 0x30F3], // ア イ ウ カ キ ン
        // Bopomofo
        0x3100 => vec![0x3105, 0x3106, 0x3107, 0x3108], // ㄅ ㄆ ㄇ ㄈ
        // CJK Unified Ideographs - common characters
        0x4E00 => vec![0x4E00, 0x4E2D, 0x4EBA, 0x5927, 0x65E5, 0x6708], // 一 中 人 大 日 月
        // Hangul Syllables
        0xAC00 => vec![0xAC00, 0xAC01, 0xAC04, 0xB098, 0xB2E4], // 가 각 간 나 다
        // CJK Compatibility Ideographs
        0xF900 => vec![0xF900, 0xF901, 0xF902], // 豈 更 車
        // Arabic Presentation Forms-A
        0xFB50 => vec![0xFB50, 0xFB51, 0xFB52, 0xFB56], // ﭐ ﭑ ﭒ ﭖ
        // Arabic Presentation Forms-B
        0xFE70 => vec![0xFE70, 0xFE72, 0xFE74, 0xFE76], // ﹰ ﹲ ﹴ ﹶ
        // Halfwidth and Fullwidth Forms
        0xFF00 => vec![0xFF01, 0xFF21, 0xFF41, 0xFF61], // ! A a 。
        // Default: sample at regular intervals
        _ => {
            let range_size = end - start;
            if range_size > 20 {
                vec![
                    start + range_size / 5,
                    start + 2 * range_size / 5,
                    start + 3 * range_size / 5,
                    start + 4 * range_size / 5,
                ]
            } else {
                vec![start, start + range_size / 2]
            }
        }
    }
}

/// Find the best Unicode CMAP subtable from a font provider.
/// Tries multiple platform/encoding combinations in priority order.
#[cfg(all(feature = "std", feature = "parsing"))]
fn find_best_cmap_subtable<'a>(
    cmap: &allsorts::tables::cmap::Cmap<'a>,
) -> Option<allsorts::tables::cmap::EncodingRecord> {
    use allsorts::tables::cmap::{PlatformId, EncodingId};

    cmap.find_subtable(PlatformId::UNICODE, EncodingId(3))
        .or_else(|| cmap.find_subtable(PlatformId::UNICODE, EncodingId(4)))
        .or_else(|| cmap.find_subtable(PlatformId::WINDOWS, EncodingId(1)))
        .or_else(|| cmap.find_subtable(PlatformId::WINDOWS, EncodingId(10)))
        .or_else(|| cmap.find_subtable(PlatformId::UNICODE, EncodingId(0)))
        .or_else(|| cmap.find_subtable(PlatformId::UNICODE, EncodingId(1)))
}

/// Verify OS/2 reported Unicode ranges against actual CMAP support.
/// Returns only ranges that are actually supported by the font's CMAP table.
#[cfg(all(feature = "std", feature = "parsing"))]
fn verify_unicode_ranges_with_cmap(
    provider: &impl FontTableProvider,
    os2_ranges: Vec<UnicodeRange>
) -> Vec<UnicodeRange> {
    use allsorts::tables::cmap::{Cmap, CmapSubtable};

    if os2_ranges.is_empty() {
        return Vec::new();
    }

    // Try to get CMAP subtable
    let cmap_data = match provider.table_data(tag::CMAP) {
        Ok(Some(data)) => data,
        _ => return os2_ranges, // Can't verify, trust OS/2
    };

    let cmap = match ReadScope::new(&cmap_data).read::<Cmap<'_>>() {
        Ok(c) => c,
        Err(_) => return os2_ranges,
    };

    let encoding_record = match find_best_cmap_subtable(&cmap) {
        Some(r) => r,
        None => return os2_ranges, // No suitable subtable, trust OS/2
    };

    let cmap_subtable = match ReadScope::new(&cmap_data)
        .offset(encoding_record.offset as usize)
        .read::<CmapSubtable<'_>>()
    {
        Ok(st) => st,
        Err(_) => return os2_ranges,
    };

    // Verify each range
    let mut verified_ranges = Vec::new();

    for range in os2_ranges {
        let test_codepoints = get_verification_codepoints(range.start, range.end);

        // Require at least 50% of test codepoints to have valid glyphs
        // This is stricter than before to avoid false positives
        let required_hits = (test_codepoints.len() + 1) / 2; // ceil(len/2)
        let mut hits = 0;

        for cp in test_codepoints {
            if cp >= range.start && cp <= range.end {
                if let Ok(Some(gid)) = cmap_subtable.map_glyph(cp) {
                    if gid != 0 {
                        hits += 1;
                        if hits >= required_hits {
                            break;
                        }
                    }
                }
            }
        }

        if hits >= required_hits {
            verified_ranges.push(range);
        }
    }

    verified_ranges
}

/// Analyze CMAP table to discover font coverage when OS/2 provides no info.
/// This is the fallback when OS/2 ulUnicodeRange bits are all zero.
#[cfg(all(feature = "std", feature = "parsing"))]
fn analyze_cmap_coverage(provider: &impl FontTableProvider) -> Option<Vec<UnicodeRange>> {
    use allsorts::tables::cmap::{Cmap, CmapSubtable};

    let cmap_data = provider.table_data(tag::CMAP).ok()??;
    let cmap = ReadScope::new(&cmap_data).read::<Cmap<'_>>().ok()?;

    let encoding_record = find_best_cmap_subtable(&cmap)?;

    let cmap_subtable = ReadScope::new(&cmap_data)
        .offset(encoding_record.offset as usize)
        .read::<CmapSubtable<'_>>()
        .ok()?;

    // Standard Unicode blocks to probe
    let blocks_to_check: &[(u32, u32)] = &[
        (0x0000, 0x007F), // Basic Latin
        (0x0080, 0x00FF), // Latin-1 Supplement
        (0x0100, 0x017F), // Latin Extended-A
        (0x0180, 0x024F), // Latin Extended-B
        (0x0250, 0x02AF), // IPA Extensions
        (0x0300, 0x036F), // Combining Diacritical Marks
        (0x0370, 0x03FF), // Greek and Coptic
        (0x0400, 0x04FF), // Cyrillic
        (0x0500, 0x052F), // Cyrillic Supplement
        (0x0530, 0x058F), // Armenian
        (0x0590, 0x05FF), // Hebrew
        (0x0600, 0x06FF), // Arabic
        (0x0700, 0x074F), // Syriac
        (0x0900, 0x097F), // Devanagari
        (0x0980, 0x09FF), // Bengali
        (0x0A00, 0x0A7F), // Gurmukhi
        (0x0A80, 0x0AFF), // Gujarati
        (0x0B00, 0x0B7F), // Oriya
        (0x0B80, 0x0BFF), // Tamil
        (0x0C00, 0x0C7F), // Telugu
        (0x0C80, 0x0CFF), // Kannada
        (0x0D00, 0x0D7F), // Malayalam
        (0x0E00, 0x0E7F), // Thai
        (0x0E80, 0x0EFF), // Lao
        (0x1000, 0x109F), // Myanmar
        (0x10A0, 0x10FF), // Georgian
        (0x1100, 0x11FF), // Hangul Jamo
        (0x1200, 0x137F), // Ethiopic
        (0x13A0, 0x13FF), // Cherokee
        (0x1780, 0x17FF), // Khmer
        (0x1800, 0x18AF), // Mongolian
        (0x2000, 0x206F), // General Punctuation
        (0x20A0, 0x20CF), // Currency Symbols
        (0x2100, 0x214F), // Letterlike Symbols
        (0x2190, 0x21FF), // Arrows
        (0x2200, 0x22FF), // Mathematical Operators
        (0x2500, 0x257F), // Box Drawing
        (0x25A0, 0x25FF), // Geometric Shapes
        (0x2600, 0x26FF), // Miscellaneous Symbols
        (0x3000, 0x303F), // CJK Symbols and Punctuation
        (0x3040, 0x309F), // Hiragana
        (0x30A0, 0x30FF), // Katakana
        (0x3100, 0x312F), // Bopomofo
        (0x3130, 0x318F), // Hangul Compatibility Jamo
        (0x4E00, 0x9FFF), // CJK Unified Ideographs
        (0xAC00, 0xD7AF), // Hangul Syllables
        (0xF900, 0xFAFF), // CJK Compatibility Ideographs
        (0xFB50, 0xFDFF), // Arabic Presentation Forms-A
        (0xFE70, 0xFEFF), // Arabic Presentation Forms-B
        (0xFF00, 0xFFEF), // Halfwidth and Fullwidth Forms
    ];

    let mut ranges = Vec::new();

    for &(start, end) in blocks_to_check {
        let test_codepoints = get_verification_codepoints(start, end);
        let required_hits = (test_codepoints.len() + 1) / 2;
        let mut hits = 0;

        for cp in test_codepoints {
            if let Ok(Some(gid)) = cmap_subtable.map_glyph(cp) {
                if gid != 0 {
                    hits += 1;
                    if hits >= required_hits {
                        break;
                    }
                }
            }
        }

        if hits >= required_hits {
            ranges.push(UnicodeRange { start, end });
        }
    }

    if ranges.is_empty() {
        None
    } else {
        Some(ranges)
    }
}

// Helper function to extract unicode ranges (unused, kept for reference)
#[cfg(all(feature = "std", feature = "parsing"))]
#[allow(dead_code)]
fn extract_unicode_ranges(os2_table: &Os2) -> Vec<UnicodeRange> {
    let mut unicode_ranges = Vec::new();

    let ranges = [
        os2_table.ul_unicode_range1,
        os2_table.ul_unicode_range2,
        os2_table.ul_unicode_range3,
        os2_table.ul_unicode_range4,
    ];

    for &(bit, start, end) in UNICODE_RANGE_MAPPINGS {
        let range_idx = bit / 32;
        let bit_pos = bit % 32;
        if range_idx < 4 && (ranges[range_idx] & (1 << bit_pos)) != 0 {
            unicode_ranges.push(UnicodeRange { start, end });
        }
    }

    unicode_ranges
}

// Helper function to detect if a font is monospace
#[cfg(all(feature = "std", feature = "parsing"))]
fn detect_monospace(
    provider: &impl FontTableProvider,
    os2_table: &Os2,
    detected_monospace: Option<bool>,
) -> Option<bool> {
    if let Some(is_monospace) = detected_monospace {
        return Some(is_monospace);
    }

    // Try using PANOSE classification
    if os2_table.panose[0] == 2 {
        // 2 = Latin Text
        return Some(os2_table.panose[3] == 9); // 9 = Monospaced
    }

    // Check glyph widths in hmtx table
    let hhea_data = provider.table_data(tag::HHEA).ok()??;
    let hhea_table = ReadScope::new(&hhea_data).read::<HheaTable>().ok()?;
    let maxp_data = provider.table_data(tag::MAXP).ok()??;
    let maxp_table = ReadScope::new(&maxp_data).read::<MaxpTable>().ok()?;
    let hmtx_data = provider.table_data(tag::HMTX).ok()??;
    let hmtx_table = ReadScope::new(&hmtx_data)
        .read_dep::<HmtxTable<'_>>((
            usize::from(maxp_table.num_glyphs),
            usize::from(hhea_table.num_h_metrics),
        ))
        .ok()?;

    let mut monospace = true;
    let mut last_advance = 0;

    // Check if all advance widths are the same
    for i in 0..hhea_table.num_h_metrics as usize {
        let advance = hmtx_table.h_metrics.read_item(i).ok()?.advance_width;
        if i > 0 && advance != last_advance {
            monospace = false;
            break;
        }
        last_advance = advance;
    }

    Some(monospace)
}

/// Guess font metadata from a filename using the existing tokenizer.
///
/// Uses [`config::tokenize_font_stem`] and [`config::FONT_STYLE_TOKENS`]
/// to extract the family name and detect style hints from the filename.
#[cfg(feature = "std")]
fn pattern_from_filename(path: &std::path::Path) -> Option<FcPattern> {
    let ext = path.extension()?.to_str()?.to_lowercase();
    match ext.as_str() {
        "ttf" | "otf" | "ttc" | "woff" | "woff2" => {}
        _ => return None,
    }

    let stem = path.file_stem()?.to_str()?;
    let all_tokens = crate::config::tokenize_lowercase(stem);

    // Style detection: check if any token matches a known style keyword
    let has_token = |kw: &str| all_tokens.iter().any(|t| t == kw);
    let is_bold = has_token("bold") || has_token("heavy");
    let is_italic = has_token("italic");
    let is_oblique = has_token("oblique");
    let is_mono = has_token("mono") || has_token("monospace");
    let is_condensed = has_token("condensed");

    // Family = non-style tokens joined
    let family_tokens = crate::config::tokenize_font_stem(stem);
    if family_tokens.is_empty() { return None; }
    let family = family_tokens.join(" ");

    Some(FcPattern {
        name: Some(stem.to_string()),
        family: Some(family),
        bold: if is_bold { PatternMatch::True } else { PatternMatch::False },
        italic: if is_italic { PatternMatch::True } else { PatternMatch::False },
        oblique: if is_oblique { PatternMatch::True } else { PatternMatch::DontCare },
        monospace: if is_mono { PatternMatch::True } else { PatternMatch::DontCare },
        condensed: if is_condensed { PatternMatch::True } else { PatternMatch::DontCare },
        weight: if is_bold { FcWeight::Bold } else { FcWeight::Normal },
        stretch: if is_condensed { FcStretch::Condensed } else { FcStretch::Normal },
        unicode_ranges: Vec::new(),
        metadata: FcFontMetadata::default(),
        render_config: FcFontRenderConfig::default(),
    })
}