spindrift 0.1.0

A HTML/CSS to PDF renderer, targeting spec compliance and efficiency
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
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
use std::rc::Rc;

use super::*;
use crate::layout::block::{
    FloatContour, FlowExclusionKind, InitialLetterLayout, LogicalFloatPlacement,
};
use crate::layout::inline_layout::{InlineLineKind, InlineLineTermination};

/// Whether a graph's source context can reuse cached advances for a selected
/// line.
///
/// Source shaping is retained across a selected boundary, but CSS line
/// construction can still replace an edge, trim text, or attach a marker.
/// This context gate is deliberately shared by normal wrapping, balancing,
/// clamping, and float-constrained fitting so those selection modes cannot
/// disagree on when exact materialization is required.
/// <https://www.w3.org/TR/css-text-3/#line-breaking>
/// <https://www.w3.org/TR/css-text-3/#boundary-shaping>
fn source_measurement_context_matches_selected_line(
    graph: &InlineOpportunityGraph,
    block_style: &ComputedStyle,
    line_index: usize,
) -> bool {
    (line_index != 0 || block_style.first_line_style.is_none())
        && !block_style.hanging_punctuation.first
        && !block_style.hanging_punctuation.last
        && !block_style.hanging_punctuation.force_end
        && !block_style.hanging_punctuation.allow_end
        && graph.supports_monotonic_source_measurement()
}

/// Whether a selected graph boundary leaves the cached source advance as the
/// selected line's fitting advance.
fn source_measurement_boundary_matches_selected_line(
    break_opportunity: Option<InlineBreakOpportunity>,
) -> bool {
    break_opportunity.is_none_or(|opportunity| {
        matches!(
            opportunity.kind,
            InlineBreakKind::SoftWrap
                | InlineBreakKind::ExplicitVirtual
                | InlineBreakKind::PreservedSpace
                | InlineBreakKind::Forced
        ) && !opportunity.hangs_from_fitting_measure()
            && !opportunity.is_discretionary()
            && opportunity.discretionary.is_none()
            && !opportunity.availability.is_fallback()
    })
}

fn source_measurement_matches_selected_line(
    graph: &InlineOpportunityGraph,
    block_style: &ComputedStyle,
    line_index: usize,
    break_opportunity: Option<InlineBreakOpportunity>,
) -> bool {
    source_measurement_context_matches_selected_line(graph, block_style, line_index)
        && source_measurement_boundary_matches_selected_line(break_opportunity)
}

/// Establish the per-line used font-size for CSS Text fitting without
/// modifying the cascaded computed style. `normal` and unitless line heights
/// follow the used font size; an explicit length, including a percentage that
/// already computed to a length, remains fixed.
/// <https://drafts.csswg.org/css-text-5/#text-fit-property>
pub(in crate::layout) fn scale_text_fit_fragment_style(style: &mut ComputedStyle, scale: f32) {
    debug_assert!(scale.is_finite() && scale >= 0.0);
    style.font_size *= scale;
    if matches!(
        style.line_height_value,
        css::ComputedLineHeight::Normal | css::ComputedLineHeight::Number(_)
    ) {
        // Re-project from the fitted font size. The cached layout scalar can
        // originate before a descendant font shorthand or pseudo-style is
        // materialized, whereas the computed `normal`/unitless value is what
        // CSS Text fitting makes depend on the used font size.
        style.line_height = style.line_height_value.clone().projected(style.font_size).0;
    }
}

/// A validated non-negative scale selected for one `text-fit` block.
///
/// This is a used value, intentionally distinct from the computed CSS
/// percentage limit. It keeps invalid numeric results from escaping the
/// fitting analysis into shaping and line-box construction.
/// <https://drafts.csswg.org/css-text-5/#text-fit-property>
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub(in crate::layout) struct TextFitScale(f32);

impl TextFitScale {
    const ONE: Self = Self(1.0);

    fn new(value: f32) -> Option<Self> {
        (value.is_finite() && value >= 0.0).then_some(Self(value))
    }

    pub(in crate::layout) fn factor(self) -> f32 {
        self.0
    }

    fn min(self, other: Self) -> Self {
        if self.0 <= other.0 { self } else { other }
    }
}

/// The first and last formatted records affected by CSS Text fitting.
/// `text-box` trims those two logical edges independently in per-line modes.
#[derive(Debug, Clone, Copy)]
pub(in crate::layout) struct TextFitAppliedRecords {
    pub(in crate::layout) first_formatted: usize,
    pub(in crate::layout) last_formatted: usize,
}

/// The parent block's text-fitting used values for one formatted line.
///
/// CSS Text fitting changes the used text font size without changing the
/// computed style. Keeping the resolved parent style with the selected line
/// prevents its strut, fragment metrics, trimming, and paint geometry from
/// observing different `line-height: normal` values.
/// <https://drafts.csswg.org/css-text-5/#text-fit-property>
#[derive(Debug, Clone)]
pub(in crate::layout) struct TextFitUsedLineStyle {
    scale: TextFitScale,
    block_style: Rc<ComputedStyle>,
    line_stack_strut: f32,
}

impl TextFitUsedLineStyle {
    pub(in crate::layout) fn scale(&self) -> TextFitScale {
        self.scale
    }

    pub(in crate::layout) fn block_style(&self) -> &ComputedStyle {
        &self.block_style
    }

    /// The scaled containing-block strut used to stack line records.
    ///
    /// This remains distinct from a selected font's `normal` metrics: those
    /// metrics position text within the line, while the strut determines the
    /// inline block's logical block-size.
    pub(in crate::layout) fn line_stack_strut(&self) -> f32 {
        self.line_stack_strut
    }
}

fn text_fit_applies_first_line_style(
    block_style: &ComputedStyle,
    is_first_formatted_line: bool,
) -> bool {
    is_first_formatted_line
        && block_style.first_line_style.is_some()
        && !block_style
            .first_letter_style
            .as_deref()
            .is_some_and(|style| !style.initial_letter.is_normal())
}

/// Select a valid text-fit used scale from two measurements of the same
/// selected line. The measurements respectively use factors one and two, so
/// their difference is the scalable advance while `2 * one - two` is the
/// fixed advance.
/// <https://drafts.csswg.org/css-text-5/#text-fit-property>
fn text_fit_scale_from_measurements(
    available_width: f32,
    one: f32,
    two: f32,
    direction: css::TextFitDirection,
    limit: Option<f32>,
) -> TextFitScale {
    if one <= f32::EPSILON || !one.is_finite() || !two.is_finite() {
        return TextFitScale::ONE;
    }
    let scalable = (two - one).max(0.0);
    if scalable <= f32::EPSILON || !scalable.is_finite() {
        return TextFitScale::ONE;
    }
    let fixed = (2.0 * one - two).max(0.0);
    let mut factor = ((available_width.max(0.0) - fixed).max(0.0) / scalable).max(0.0);
    factor = match direction {
        css::TextFitDirection::Grow => factor.max(1.0),
        css::TextFitDirection::Shrink => factor.min(1.0),
    };
    if let Some(limit) = limit {
        factor = match direction {
            css::TextFitDirection::Grow if limit >= 1.0 => factor.min(limit),
            css::TextFitDirection::Shrink if (0.0..=1.0).contains(&limit) => factor.max(limit),
            css::TextFitDirection::Grow | css::TextFitDirection::Shrink => factor,
        };
    }
    TextFitScale::new(factor).unwrap_or(TextFitScale::ONE)
}

fn text_fit_strategy_scales_record(
    strategy: css::TextFitStrategy,
    record_index: usize,
    last_formatted_record: usize,
    termination: InlineLineTermination,
) -> bool {
    match strategy {
        css::TextFitStrategy::Consistent | css::TextFitStrategy::PerLineAll => true,
        css::TextFitStrategy::PerLine => {
            record_index != last_formatted_record
                && termination != InlineLineTermination::ForcedBreak
        }
    }
}

/// Whether the selected final line must reserve a block ellipsis for source
/// that lies outside the current inline graph.
///
/// A `LineLimitTraversal` carries the fact through a descendant style, while a
/// collected inline sequence supplies it directly for a later preserved-break
/// paragraph. Keeping both sources here prevents graph-local suffix checks
/// from losing a block-flow continuation.
fn line_clamp_has_later_in_flow_content(context: InlineParagraphContext<'_>) -> bool {
    context.clamp_continuation == css::ClampContinuation::LaterInFlowContent
        || context.line_clamp.is_some_and(|line_clamp| {
            line_clamp.continuation() == css::ClampContinuation::LaterInFlowContent
        })
}

/// Classify the first source-order float in a candidate line range without
/// treating its zero-advance graph marker as a CSS Text break opportunity.
fn inline_float_affected_range(
    graph: &InlineOpportunityGraph,
    range: InlineGraphRange,
) -> InlineFloatAffectedRange {
    let Some(marker) = graph.first_float_position_in_range(range) else {
        return InlineFloatAffectedRange::None;
    };
    inline_float_marker_range(graph, marker)
}

fn inline_float_marker_range(
    graph: &InlineOpportunityGraph,
    marker: InlineGraphPosition,
) -> InlineFloatAffectedRange {
    let Some(float) = graph.float_at_position(marker) else {
        debug_assert!(false, "float graph position must own an inline float");
        return InlineFloatAffectedRange::None;
    };
    if float.style().allows_soft_wrap() {
        InlineFloatAffectedRange::Wrappable { marker }
    } else {
        InlineFloatAffectedRange::UnbreakableContinuation { marker }
    }
}

/// One normal or balanced selection in an inline graph.
///
/// CSS Text 4 balancing must use the same legal graph opportunities as normal
/// wrapping. Recording the start position makes the plan safe to apply only
/// while the real selector follows the same source stream.
/// <https://drafts.csswg.org/css-text-4/#text-wrap-style>
#[derive(Debug, Clone, Copy)]
struct BalancedLinePlanEntry {
    start: InlineGraphPosition,
    end: SelectedInlineLineEnd,
    line_index: usize,
    /// The clamp marker occupies this line without retaining any source.
    /// Keeping this in the plan prevents final clamp reconciliation from
    /// treating a deliberate marker-only endpoint as an ordinary no-fit line.
    clamp_marker_replaces_source: bool,
}

/// A source-order float that was provisionally placed only to expose the
/// remaining candidate stream. Once balancing has selected that stream, its
/// placement is replayed from the state immediately before the float.
struct PendingBalancedFloatReplay {
    position: InlineGraphPosition,
    source_line_index: usize,
    snapshot: LayoutSnapshot,
}

/// One complete candidate for a CSS Text Level 4 balanced line group.
///
/// The candidate retains the actual unused inline space of every line.  A
/// single shared target width is insufficient when floats, indents, or other
/// line-local geometry make the available measure differ between lines.
/// <https://drafts.csswg.org/css-text-4/#text-wrap-style>
#[derive(Debug, Clone)]
struct BalancedLineCandidate {
    entries: Vec<BalancedLinePlanEntry>,
    remaining_inline_space: Vec<f32>,
}

/// Immutable inputs shared by one bounded balance-candidate search.
#[derive(Clone, Copy)]
struct BalancedLineSearch<'a, 'b> {
    graph: &'a InlineOpportunityGraph,
    context: InlineParagraphContext<'b>,
    group_end: InlineGraphPosition,
    available_widths: &'a [f32],
    ellipsis_width: f32,
    allow_emergency_breaks: bool,
}

/// Mutable result state for one balance-candidate search.
#[derive(Default)]
struct BalancedLineSearchState {
    examined: usize,
    exhausted_budget: bool,
    best: Option<BalancedLineCandidate>,
}

/// The physical block slab occupied by one selected source line while it
/// queries float exclusions.
///
/// The source line identity stays separate from its page-local placement: a
/// float may move the same selected source line below several nominal strut
/// rows. Keeping those facts together avoids using a retry count as geometry.
/// The offset is relative to the normal physical block position and the size
/// is the inherited strut or the refined used line block-size.
/// <https://www.w3.org/TR/css-inline-3/#line-boxes>
/// <https://www.w3.org/TR/css-shapes-1/#shape-outside-property>
#[derive(Clone, Copy)]
struct PhysicalLineSlab {
    line_index: usize,
    block_offset: f32,
    used_block_size: f32,
}

/// The result of resolving a normal-flow horizontal line against established
/// float exclusions after its complete used block slab is known.
///
/// The source range remains unchanged on retry. Only its physical placement
/// moves, so a float cannot manufacture an empty CSS Text line or become a
/// source break opportunity.
/// <https://www.w3.org/TR/CSS22/visuren.html#floats>
enum HorizontalFloatLinePlacement {
    Accepted,
    RetryAtLaterSlab { block_advance: f32 },
}

/// A selected unbreakable line and the physical block distance skipped before
/// its source content can be placed beside CSS float exclusions.
///
/// The source line remains one CSS line box even when a float moves its used
/// physical slab below the nominal inherited-strut row. Keeping the skipped
/// distance with the selected fragment prevents the durable line sequence
/// from manufacturing an empty source line for that placement retry.
/// <https://www.w3.org/TR/CSS22/visuren.html#floats>
struct UnbreakableInlineFloatSelection {
    fragment: InlineLineFragment,
    block_before: f32,
}

/// Return the total containing-block measure a float-clearance retry needs.
///
/// An unbreakable line can legally overflow even its full containing block,
/// but it must not overflow a narrower float-shortened line when the float
/// can be cleared. Capping the used measure at the full measure therefore
/// finds the first later slab that removes enough exclusion without requiring
/// impossible room for the overflowing word itself.
/// <https://www.w3.org/TR/CSS22/visuren.html#floats>
fn float_clearance_required_width(
    used_line_width: f32,
    full_line_indent: f32,
    containing_width: f32,
) -> f32 {
    (used_line_width + full_line_indent)
        .min(containing_width)
        .max(0.0)
}

impl PhysicalLineSlab {
    fn new(line_index: usize, block_offset: f32, used_block_size: f32) -> Self {
        debug_assert!(block_offset >= 0.0);
        debug_assert!(used_block_size >= 0.0);
        Self {
            line_index,
            block_offset,
            used_block_size,
        }
    }

    fn inherited_strut(line_index: usize, block_offset: f32, line_height: f32) -> Self {
        Self::new(line_index, block_offset, line_height)
    }

    fn with_used_block_size(self, used_block_size: f32) -> Self {
        debug_assert!(used_block_size >= 0.0);
        Self {
            used_block_size,
            ..self
        }
    }
}

// Each candidate materializes a graph range so line-edge effects, tabs, and
// punctuation use the normal line model.  Keep the work comfortably below a
// single layout timeout; a larger search deliberately falls back to ordinary
// wrapping instead of selecting from an incomplete candidate set.
const MAX_BALANCED_LINE_CANDIDATES: usize = 2_048;

/// Return the variance of the unused inline measures in a candidate plan.
///
/// CSS Text Level 4 balances the remaining line-box inline space, not source
/// text advances.  Keeping this pure makes the comparison deterministic and
/// preserves the ordinary greedy selection on exact ties.
/// <https://drafts.csswg.org/css-text-4/#text-wrap-style>
fn balanced_remaining_space_variance(remaining_inline_space: &[f32]) -> f32 {
    if remaining_inline_space.len() < 2 {
        return 0.0;
    }
    let mean = remaining_inline_space.iter().sum::<f32>() / remaining_inline_space.len() as f32;
    remaining_inline_space
        .iter()
        .map(|space| (space - mean).powi(2))
        .sum::<f32>()
        / remaining_inline_space.len() as f32
}

pub(in crate::layout) struct SelectedInlineLines {
    pub(in crate::layout) fragments: Vec<SelectedInlineLine>,
    pub(in crate::layout) next_line_index: usize,
    /// Extra physical block progression accumulated while selecting the last
    /// source line. A collected sequence carries this into the next paragraph
    /// so explicit breaks do not make later lines query the old float slab.
    pub(in crate::layout) next_physical_block_offset: f32,
    pub(in crate::layout) has_float_side_effects: bool,
    /// A source-order float committed by a graph that produced no in-flow
    /// fragment. The collected-line cursor carries it across an explicit
    /// break so the next graph reselects source against the same exclusion.
    pub(in crate::layout) trailing_inline_float_replay: Option<CommittedInlineFloatReplay>,
}

/// The inline formatter's handle for replaying a committed float transaction.
///
/// The floating subtree is already owned by `CommittedInlineFloat`; this
/// record contains only source selection and physical-row state.  In
/// particular, a float marker is not promoted to a CSS Text break while a
/// following graph is reselected.
/// <https://www.w3.org/TR/CSS22/visuren.html#floats>
#[derive(Debug, Clone, Copy)]
pub(in crate::layout) struct CommittedInlineFloatReplay {
    marker: (usize, usize),
    source_range_start: (usize, usize),
    source_range_end: (usize, usize),
    selected_row: usize,
    physical_row: usize,
    physical_block_offset: f32,
    used_block_advance: f32,
}

impl CommittedInlineFloatReplay {
    /// Whether the next source graph must initially select around this float.
    fn applies_before_source_row(self, line_index: usize) -> bool {
        debug_assert!(self.source_range_start <= self.marker);
        debug_assert!(self.marker <= self.source_range_end);
        debug_assert_eq!(self.physical_row, self.selected_row);
        debug_assert!(self.physical_block_offset >= -INLINE_FLOAT_EPSILON);
        debug_assert!(self.used_block_advance >= -INLINE_FLOAT_EPSILON);
        self.selected_row < line_index
    }
}

/// A selected inline fragment and its physical line index.
///
/// Float exclusions can make a line temporarily too narrow for content that
/// otherwise fits the containing block. Retaining the selected index lets the
/// durable sequence materialize intervening empty exclusion lines rather than
/// painting the content as overflow beside the float:
/// <https://www.w3.org/TR/CSS22/visuren.html#floats>.
pub(in crate::layout) struct SelectedInlineLine {
    pub(in crate::layout) fragment: InlineLineFragment,
    pub(in crate::layout) line_index: usize,
    /// Extra physical block advance before this line.
    ///
    /// Float contours can make a line first fit between normal line-height
    /// rows. This stores that CSS Shapes retry distance until the durable line
    /// sequence advances the page-local block cursor.
    pub(in crate::layout) block_before: f32,
}

/// The physical layout state of one provisional inline line.
///
/// Inline line selection happens before final line metrics are known, so it
/// advances by the computed line height just as the horizontal selector has
/// always done.  The block axis is physical y for horizontal writing and
/// physical x for vertical writing:
/// <https://www.w3.org/TR/css-inline-3/#line-layout> and
/// <https://www.w3.org/TR/css-writing-modes-4/#abstract-box>.
#[derive(Debug, Clone, Copy)]
struct InlineLinePhysicalPosition {
    content_left: f32,
    content_right: f32,
    cursor_y: f32,
}

/// Project one vertical line's logical block slab onto physical page X.
///
/// Vertical-lr lines begin at the containing box's physical left edge, while
/// vertical-rl and sideways-rl lines begin at its physical right edge. Keeping
/// this projection in one adapter prevents float-exclusion queries from always
/// sampling the leftmost column.
/// <https://www.w3.org/TR/css-writing-modes-4/#logical-to-physical>
/// <https://www.w3.org/TR/css-writing-modes-4/#line-mappings>
fn vertical_line_block_slab(
    position: InlineLinePhysicalPosition,
    block_style: &ComputedStyle,
    physical_left_inset: f32,
    block_extent: f32,
) -> PageInlineSpan {
    debug_assert!(block_style.writing_mode.has_vertical_lines());
    match FlowAxes::for_style(block_style).block_start_side() {
        PhysicalSide::Left => {
            PageInlineSpan::new(position.content_left + physical_left_inset, block_extent)
        }
        PhysicalSide::Right => {
            let right = position.content_right - block_style.padding.right;
            PageInlineSpan::from_edges(right - block_extent, right)
        }
        PhysicalSide::Top | PhysicalSide::Bottom => {
            unreachable!("a vertical line's block axis is physical X")
        }
    }
}

/// The selected source line's identity, independent of its physical row.
///
/// Float exclusions can create empty physical rows before inline source is
/// formatted. CSS Text keys first-line indentation and `each-line` behavior to
/// the formatted line and forced-break boundary, not to those rows:
/// <https://www.w3.org/TR/css-text-3/#text-indent-property>.
#[derive(Debug, Clone, Copy)]
pub(in crate::layout) struct SelectedLineIdentity {
    is_first_formatted_line: bool,
    starts_after_forced_break: bool,
}

/// The logical source line and its physical block placement.
///
/// A shaped float can move a selected line between normal line-height rows
/// without changing its CSS Text identity.
#[derive(Debug, Clone, Copy)]
pub(in crate::layout) struct InlineLinePhysicalRow {
    line_index: usize,
    identity: SelectedLineIdentity,
    block_offset: f32,
}

/// Immutable state for committing one source-order inline float.
///
/// A float marker is not a CSS Text line end.  Retaining the selected source
/// range and its physical row together means that a retry cannot accidentally
/// reinterpret the marker as a new line or reconstruct its y coordinate from
/// a later mutable block cursor:
/// <https://www.w3.org/TR/CSS22/visuren.html#float-position> and
/// <https://www.w3.org/TR/css-text-3/#line-breaking>.
struct InlineFloatTransactionCheckpoint {
    source_range: InlineGraphRange,
    selected_end: SelectedInlineLineEnd,
    marker: InlineGraphPosition,
    row: InlineLinePhysicalRow,
    /// CSS 2.2 constrains a source-order float by preceding line boxes, not
    /// by the row that happened to select its marker. The float context may
    /// move it later from this floor, but never needs an invented text row.
    placement_floor: InlineLinePhysicalRow,
    snapshot: LayoutSnapshot,
}

/// Return the boundary after a raised or sunken initial letter that must own
/// its originating line by itself.
///
/// CSS Inline lays following ordinary inline content after the leading line
/// slots exposed above a letter when its requested size exceeds its sink.
/// Keeping that as a graph boundary (rather than moving paint with a baseline
/// offset) lets the normal record builder materialize intervening phantom
/// lines and keeps exclusion queries in source order.
/// <https://drafts.csswg.org/css-inline-3/#initial-letter-position>
fn initial_letter_source_handoff_end(
    graph: &InlineOpportunityGraph,
    start: InlineGraphPosition,
) -> Option<InlineGraphPosition> {
    if start.byte_offset != 0 {
        return None;
    }
    let run = graph.runs.get(start.run_index)?;
    let InlineLineItem::Fragment(fragment) = &run.item else {
        return None;
    };
    let (size, sink) = fragment.style().initial_letter.specified()?;
    // Raised and sunken initials expose leading line slots before their
    // following source. A drop initial remains in its originating source
    // line in every writing mode; its exclusion, rather than an artificial
    // graph boundary, selects adjacent vertical columns.
    // <https://drafts.csswg.org/css-inline-3/#initial-letter-position>
    (size > sink as f32 + INLINE_FLOAT_EPSILON)
        .then_some(InlineGraphPosition::at_run_start(start.run_index + 1))
}

/// Number of *additional* ordinary line slots between a raised/sunken
/// initial's originating line and its following source content.
///
/// The initial letter itself does not enlarge the originating line box. These
/// slots are represented by missing records, which the durable line sequence
/// turns into phantom struts while painting and fragmenting the block.
/// <https://drafts.csswg.org/css-inline-3/#initial-letter-position>
fn raised_initial_letter_leading_line_slots(fragment: &InlineLineFragment) -> usize {
    fragment
        .items()
        .iter()
        .find_map(|item| {
            let InlineLineItem::Fragment(fragment) = &item.item else {
                return None;
            };
            let (size, sink) = fragment.style().initial_letter.specified()?;
            (size > sink as f32 + INLINE_FLOAT_EPSILON)
                .then_some((size.ceil() as u32).saturating_sub(sink).saturating_sub(1) as usize)
        })
        .unwrap_or(0)
}

/// Resolve the physical exclusion edge for an initial letter.
///
/// Horizontal initials wrap from their logical inline-start edge. In a
/// vertical or sideways flow, an initial letter instead occupies the logical
/// block-start column, which is the right edge in `*-rl` modes and the left
/// edge in `*-lr` modes.
/// <https://drafts.csswg.org/css-inline-3/#initial-letter-position>
/// <https://drafts.csswg.org/css-writing-modes-4/#logical-to-physical>
fn initial_letter_exclusion_side(
    writing_mode: WritingMode,
    horizontal_inline_start: UsedFloatSide,
) -> UsedFloatSide {
    match writing_mode {
        WritingMode::HorizontalTb => horizontal_inline_start,
        WritingMode::VerticalRl | WritingMode::SidewaysRl => UsedFloatSide::Right,
        WritingMode::VerticalLr | WritingMode::SidewaysLr => UsedFloatSide::Left,
    }
}

impl std::ops::Deref for SelectedInlineLine {
    type Target = InlineLineFragment;

    fn deref(&self) -> &Self::Target {
        &self.fragment
    }
}

impl<'a> LayoutBuilder<'a> {
    /// Apply CSS Text fitting to one used inline style while keeping the
    /// computed-style representation immutable.
    pub(in crate::layout) fn scale_text_fit_used_style(
        &mut self,
        style: &mut ComputedStyle,
        scale: f32,
    ) {
        scale_text_fit_fragment_style(style, scale);
    }

    /// Resolve one fitted line's parent text style exactly once.
    ///
    /// The selected font owns the used `normal` line-height. This must happen
    /// for the parent strut as well as for each fitted descendant fragment;
    /// otherwise right-to-left vertical block progression can position glyphs
    /// from an inflated fallback line height.
    fn text_fit_used_line_style(
        &mut self,
        block_style: &ComputedStyle,
        scale: TextFitScale,
    ) -> TextFitUsedLineStyle {
        let mut fitted_block_style = block_style.clone();
        self.scale_text_fit_used_style(&mut fitted_block_style, scale.factor());
        let line_stack_strut = fitted_block_style.line_height;
        if fitted_block_style.line_height_is_normal() {
            fitted_block_style.line_height = self
                .font_system
                .used_line_height(&fitted_block_style)
                .points();
        }
        TextFitUsedLineStyle {
            scale,
            block_style: Rc::new(fitted_block_style),
            line_stack_strut,
        }
    }

    fn inline_line_physical_position_with_block_offset(
        &self,
        line_index: usize,
        block_style: &ComputedStyle,
        block_offset: f32,
    ) -> InlineLinePhysicalPosition {
        let block_advance = block_style.line_height * line_index as f32 + block_offset;
        match block_style.writing_mode {
            WritingMode::HorizontalTb => InlineLinePhysicalPosition {
                content_left: self.content_left,
                content_right: self.content_right,
                cursor_y: self.cursor_y - block_advance,
            },
            WritingMode::VerticalRl | WritingMode::SidewaysRl => InlineLinePhysicalPosition {
                content_left: self.content_left - block_advance,
                content_right: self.content_right - block_advance,
                cursor_y: self.cursor_y,
            },
            WritingMode::VerticalLr | WritingMode::SidewaysLr => InlineLinePhysicalPosition {
                content_left: self.content_left + block_advance,
                content_right: self.content_right + block_advance,
                cursor_y: self.cursor_y,
            },
        }
    }

    pub(in crate::layout) fn inline_float_band_for_line(
        &self,
        line_index: usize,
        block_style: &ComputedStyle,
        available_width: f32,
        padding_left: f32,
    ) -> InlineFloatBand {
        self.inline_float_band_for_line_with_block_offset(
            line_index,
            block_style,
            available_width,
            padding_left,
            0.0,
        )
    }

    fn inline_float_band_for_line_with_block_offset(
        &self,
        line_index: usize,
        block_style: &ComputedStyle,
        available_width: f32,
        padding_left: f32,
        block_offset: f32,
    ) -> InlineFloatBand {
        self.inline_float_band_for_physical_slab(
            block_style,
            available_width,
            padding_left,
            PhysicalLineSlab::inherited_strut(line_index, block_offset, block_style.line_height),
        )
    }

    /// Return the float-reduced inline band for one physical line slab.
    ///
    /// The initial selector normally knows only the inherited strut. Atomic
    /// inline boxes can enlarge that strut, including from zero, so callers
    /// that have a provisional materialized line pass its used block-size to
    /// refine the band against the full painted slab.
    /// <https://drafts.csswg.org/css-inline-3/#line-boxes>
    /// <https://drafts.csswg.org/TR/css-shapes-1/#shape-outside-property>
    fn inline_float_band_for_physical_slab(
        &self,
        block_style: &ComputedStyle,
        available_width: f32,
        padding_left: f32,
        slab: PhysicalLineSlab,
    ) -> InlineFloatBand {
        let position = self.inline_line_physical_position_with_block_offset(
            slab.line_index,
            block_style,
            slab.block_offset,
        );
        if block_style.writing_mode != WritingMode::HorizontalTb {
            let band = self.current_logical_float_band(
                block_style.writing_mode,
                block_style.used_direction(),
                FloatBandQuery {
                    horizontal_slab: vertical_line_block_slab(
                        position,
                        block_style,
                        padding_left,
                        slab.used_block_size,
                    ),
                    vertical_slab: vertical_physical_inline_span(
                        block_style.writing_mode,
                        block_style.used_direction(),
                        PageTopBlockPosition::new(position.cursor_y),
                        layout_pt(available_width),
                    ),
                },
            );
            return InlineFloatBand::new(band.inline_span.start(), band.inline_span.size());
        }
        let band =
            self.current_float_band(PageBlockSpan::new(position.cursor_y, slab.used_block_size));
        let left_offset = (band.left() - position.content_left - padding_left).max(0.0);
        let right_offset = (position.content_right - band.right()).max(0.0);
        InlineFloatBand::new(left_offset, available_width - left_offset - right_offset)
    }

    /// Return the band created by CSS floats only for initial-letter
    /// placement.
    ///
    /// An initial letter joins the shared content-wrap query after it is
    /// anchored. Reading the normal content band while anchoring would let a
    /// provisional initial-letter exclusion displace the very glyph that
    /// created it. CSS floats remain valid earlier placement geometry.
    /// <https://drafts.csswg.org/css-inline-3/#initial-letter-floats>
    fn css_float_band_for_physical_slab(
        &self,
        block_style: &ComputedStyle,
        available_width: f32,
        padding_left: f32,
        slab: PhysicalLineSlab,
    ) -> InlineFloatBand {
        let position = self.inline_line_physical_position_with_block_offset(
            slab.line_index,
            block_style,
            slab.block_offset,
        );
        let float_context = self
            .float_contexts
            .last()
            .expect("root float context exists");
        let page_index = self.current_float_page_index();
        if block_style.writing_mode != WritingMode::HorizontalTb {
            let band = float_context.logical_band(
                block_style.writing_mode,
                block_style.used_direction(),
                page_index,
                FloatBandQuery {
                    horizontal_slab: vertical_line_block_slab(
                        position,
                        block_style,
                        padding_left,
                        slab.used_block_size,
                    ),
                    vertical_slab: vertical_physical_inline_span(
                        block_style.writing_mode,
                        block_style.used_direction(),
                        PageTopBlockPosition::new(position.cursor_y),
                        layout_pt(available_width),
                    ),
                },
            );
            return InlineFloatBand::new(band.inline_span.start(), band.inline_span.size());
        }
        let band = float_context.band(
            page_index,
            PageBlockSpan::new(position.cursor_y, slab.used_block_size),
            PageInlineSpan::from_edges(
                position.content_left + padding_left,
                position.content_right,
            ),
        );
        let left_offset = (band.left() - position.content_left - padding_left).max(0.0);
        let right_offset = (position.content_right - band.right()).max(0.0);
        InlineFloatBand::new(left_offset, available_width - left_offset - right_offset)
    }

    pub(in crate::layout) fn layout_mixed_inline_paragraph(
        &mut self,
        items: &[InlineItem],
        context: InlineParagraphContext<'_>,
        mut line_index: usize,
        starts_after_forced_break: bool,
        preceding_plaintext_line_direction: &mut Option<Direction>,
    ) -> InlineLayoutOutcome {
        let block_style = context.block_style;
        if context.initial_first_formatted_line
            && line_index == 0
            && block_style
                .first_letter_style
                .as_deref()
                .is_some_and(|first_letter| !first_letter.initial_letter.is_normal())
        {
            self.discard_provisional_initial_letter_exclusions(block_style);
        }
        if block_style.writing_mode == WritingMode::HorizontalTb
            && context.initial_first_formatted_line
            && line_index == 0
            && block_style
                .first_letter_style
                .as_deref()
                .is_some_and(|first_letter| !first_letter.initial_letter.is_normal())
        {
            self.clear_initial_letter_exclusions_for_new_initial(block_style);
        }
        let paragraph_start_line_index = line_index;
        let graph = self.build_inline_opportunity_graph(items, block_style);
        let graph = if context.initial_first_formatted_line && line_index == 0 {
            self.graph_with_first_letter_pseudo(&graph, block_style)
        } else {
            graph
        };
        // An initial letter affects selection of its own companion source,
        // including source separated by an explicit break.  Register a
        // graph-measured provisional exclusion before selecting any line;
        // the final selected fragment replaces it below once its used
        // margins, box edges, and contour are known.
        // <https://drafts.csswg.org/css-inline-3/#initial-letter-position>
        if context.initial_first_formatted_line && line_index == 0 {
            self.register_provisional_initial_letter_exclusion(
                &graph,
                context,
                line_index,
                starts_after_forced_break,
                false,
            );
        }
        let selected_lines = self.select_inline_lines_from_graph(
            &graph,
            context,
            line_index,
            starts_after_forced_break,
        );
        let mut line_boxes = selected_lines.fragments;
        if !selected_lines.has_float_side_effects {
            self.apply_text_fit(&mut line_boxes, block_style);
        }
        let next_line_index = selected_lines.next_line_index;
        line_index = next_line_index;
        let line_count = line_boxes.len();
        let paragraph_last_hanging_width = line_boxes
            .last()
            .map(|line_box| {
                last_hanging_punctuation_width_for_line_items(
                    &mut self.font_system,
                    &line_box.fragment.items,
                    block_style,
                )
            })
            .map(SemanticLengthExt::points)
            .unwrap_or(0.0);
        let mut records = Vec::new();
        let mut next_record_line_index = paragraph_start_line_index;
        for (offset, selected_line) in line_boxes.into_iter().enumerate() {
            while next_record_line_index < selected_line.line_index {
                records.push(InlineLineRecord {
                    paragraph_index: 0,
                    block_line_index: next_record_line_index,
                    paragraph_line_index: records.len(),
                    fragment: None,
                    kind: InlineLineKind::ForcedEmpty,
                    is_first_formatted_line: context.initial_first_formatted_line
                        && next_record_line_index == 0,
                    is_last_line_in_paragraph: false,
                    termination: InlineLineTermination::SoftWrap,
                    used_bidi_base_direction: None,
                    starts_after_preserved_segment_break: false,
                    clear_after: Clear::None,
                    block_before: 0.0,
                    block_start_trim: 0.0,
                    block_end_trim: 0.0,
                    paragraph_last_hanging_width,
                    used_indent: 0.0,
                    available_width: context.available_width,
                    line_height: block_style.line_height,
                    text_fit_used_style: None,
                    decoration_origin_fragments: Default::default(),
                });
                next_record_line_index += 1;
            }
            let line_box = selected_line.fragment;
            let block_line_index = selected_line.line_index;
            // Atomic inline boxes contribute their margin-box block-size to
            // the containing line, including when the inherited strut is
            // zero. Text fragments are already represented by the selected
            // line metrics; adding their raw `line-height` here would undo
            // text-edge trimming.
            // <https://drafts.csswg.org/css-inline-3/#line-boxes>
            let item_line_height = line_box
                .items()
                .iter()
                // Initial letters span later lines through their exclusion
                // participant, rather than increasing this ordinary line
                // record's block advance.
                // <https://drafts.csswg.org/css-inline-3/#initial-letter-position>
                .filter(|item| !LayoutBuilder::inline_line_item_is_initial_letter(&item.item))
                .filter_map(|item| {
                    crate::layout::inline_layout::inline_line_item_additional_block_extent(
                        &item.item,
                        block_style,
                    )
                })
                .fold(0.0_f32, f32::max);
            let line_height = line_box
                .metrics
                .height
                .max(block_style.line_height)
                .max(item_line_height);
            let used_indent = line_box.indent;
            let available_width = line_box.available_width;
            let kind = InlineLineKind::for_fragment(&line_box, false);
            records.push(InlineLineRecord {
                paragraph_index: 0,
                block_line_index,
                paragraph_line_index: records.len(),
                fragment: Some(line_box),
                kind,
                // Edge-only phantom records retain positioned-inline source
                // geometry, but are not formatted lines.
                // <https://drafts.csswg.org/css-inline-3/#phantom-line-boxes>
                is_first_formatted_line: context.initial_first_formatted_line
                    && block_line_index == 0
                    && !kind.is_phantom(),
                is_last_line_in_paragraph: offset + 1 == line_count,
                termination: if offset + 1 == line_count {
                    InlineLineTermination::BlockEnd
                } else {
                    InlineLineTermination::SoftWrap
                },
                used_bidi_base_direction: None,
                starts_after_preserved_segment_break: false,
                clear_after: Clear::None,
                block_before: selected_line.block_before,
                block_start_trim: 0.0,
                block_end_trim: 0.0,
                paragraph_last_hanging_width,
                used_indent,
                available_width,
                line_height,
                text_fit_used_style: None,
                decoration_origin_fragments: Default::default(),
            });
            next_record_line_index = block_line_index + 1;
        }
        let mut sequence = InlineLineSequence {
            records,
            available_width: context.available_width,
            padding_left: context.padding_left,
            hanging_indent: context.hanging_indent,
            hanging_punctuation_reserve: context.hanging_punctuation_reserve,
            fragment_text_box_trim: TextBoxLineTrim::default(),
            has_flow_side_effects: selected_lines.has_float_side_effects,
            replay_float_scope: ReplayFloatScope::InheritContainingBlock,
            has_local_continuation_cutoff: matches!(
                context.line_clamp,
                Some(css::InlineLineClamp::Automatic(_))
            ),
        };
        sequence.resolve_bidi_base_directions(block_style, preceding_plaintext_line_direction);
        self.paint_inline_line_sequence(&sequence, block_style);
        InlineLayoutOutcome {
            next_line_index: line_index,
            clamp_line_slots: sequence.records.len(),
            clamp_block_advance: sequence.layout_outcome().clamp_block_advance,
            has_non_phantom_line: sequence.has_non_phantom_line(),
            has_flow_effects: selected_lines.has_float_side_effects || sequence.has_flow_effects(),
            has_local_continuation_cutoff: sequence.has_local_continuation_cutoff,
        }
    }

    /// Apply CSS Text Level 5 fitting to already selected lines. Line breaking
    /// deliberately precedes this pass: fitting changes used font sizes but
    /// must not select a different source break.
    ///
    /// <https://drafts.csswg.org/css-text-5/#text-fit-property>
    fn apply_text_fit(&mut self, lines: &mut [SelectedInlineLine], block_style: &ComputedStyle) {
        let css::TextFit::Fit {
            direction,
            strategy,
            limit,
        } = block_style.text_fit
        else {
            return;
        };
        if lines.iter().any(|line| {
            line.fragment
                .items()
                .iter()
                .any(|item| Self::inline_line_item_is_initial_letter(&item.item))
        }) {
            // Scaling can change the line block size. An initial letter's
            // exclusion makes a later line's available inline size depend on
            // that block position, for which CSS Text disables fitting.
            return;
        }

        let last_formatted = lines
            .iter()
            .rposition(|line| !inline_line_fragment_is_phantom(&line.fragment));
        let mut scales = vec![TextFitScale::ONE; lines.len()];
        match strategy {
            css::TextFitStrategy::Consistent => {
                let Some(scale) = lines
                    .iter()
                    .filter(|line| !inline_line_fragment_is_phantom(&line.fragment))
                    .map(|line| {
                        self.text_fit_scale_for_line(
                            &line.fragment,
                            direction,
                            limit,
                            line.line_index == 0,
                            block_style,
                        )
                    })
                    .reduce(TextFitScale::min)
                else {
                    return;
                };
                scales.fill(scale);
            }
            css::TextFitStrategy::PerLine | css::TextFitStrategy::PerLineAll => {
                for (index, line) in lines.iter().enumerate() {
                    if inline_line_fragment_is_phantom(&line.fragment)
                        || (matches!(strategy, css::TextFitStrategy::PerLine)
                            && Some(index) == last_formatted)
                    {
                        continue;
                    }
                    scales[index] = self.text_fit_scale_for_line(
                        &line.fragment,
                        direction,
                        limit,
                        line.line_index == 0,
                        block_style,
                    );
                }
            }
        }
        for (line, scale) in lines.iter_mut().zip(scales) {
            if (scale.factor() - 1.0).abs() > f32::EPSILON {
                let used_style = self.text_fit_used_line_style(block_style, scale);
                self.apply_text_fit_scale_to_line(
                    &mut line.fragment,
                    block_style,
                    &used_style,
                    line.line_index == 0,
                );
            }
        }
    }

    /// Apply CSS Text Level 5 fitting to a durable collected sequence.
    /// Collection can split one source block at forced breaks or page scopes,
    /// so strategy selection happens only after every record is available.
    pub(in crate::layout) fn apply_text_fit_to_records(
        &mut self,
        records: &mut [InlineLineRecord],
        block_style: &ComputedStyle,
    ) -> Option<TextFitAppliedRecords> {
        let css::TextFit::Fit {
            direction,
            strategy,
            limit,
        } = block_style.text_fit
        else {
            return None;
        };
        if records
            .iter()
            .filter_map(|record| record.fragment.as_ref())
            .any(|line| {
                line.items()
                    .iter()
                    .any(|item| Self::inline_line_item_is_initial_letter(&item.item))
            })
        {
            return None;
        }
        let formatted_records = records
            .iter()
            .enumerate()
            .filter(|(_, record)| record.fragment.is_some() && !record.kind.is_phantom())
            .map(|(index, _)| index)
            .collect::<Vec<_>>();
        let first_formatted = *formatted_records.first()?;
        let last_formatted = *formatted_records.last()?;
        let has_multiple_formatted_records = formatted_records.len() > 1;
        let mut scales = vec![TextFitScale::ONE; records.len()];
        match strategy {
            css::TextFitStrategy::Consistent => {
                let scale = formatted_records
                    .iter()
                    .map(|&index| {
                        let record = &records[index];
                        self.text_fit_scale_for_line(
                            record
                                .fragment
                                .as_ref()
                                .expect("formatted record has fragment"),
                            direction,
                            limit,
                            record.is_first_formatted_line,
                            block_style,
                        )
                    })
                    .reduce(TextFitScale::min)
                    .expect("formatted records are non-empty");
                for &index in &formatted_records {
                    scales[index] = scale;
                }
            }
            css::TextFitStrategy::PerLine | css::TextFitStrategy::PerLineAll => {
                for &index in &formatted_records {
                    let record = &records[index];
                    if !text_fit_strategy_scales_record(
                        strategy,
                        index,
                        last_formatted,
                        record.termination,
                    ) {
                        continue;
                    }
                    scales[index] = self.text_fit_scale_for_line(
                        record
                            .fragment
                            .as_ref()
                            .expect("formatted record has fragment"),
                        direction,
                        limit,
                        record.is_first_formatted_line,
                        block_style,
                    );
                }
            }
        }
        for &index in &formatted_records {
            let scale = scales[index];
            if (scale.factor() - 1.0).abs() <= f32::EPSILON {
                continue;
            }
            let used_style = self.text_fit_used_line_style(block_style, scale);
            let record = &mut records[index];
            let line = record
                .fragment
                .as_mut()
                .expect("formatted record has fragment");
            self.apply_text_fit_scale_to_line(
                line,
                block_style,
                &used_style,
                record.is_first_formatted_line,
            );
            record.line_height = if (matches!(strategy, css::TextFitStrategy::PerLine)
                && scale.factor() > 1.0)
                || (matches!(strategy, css::TextFitStrategy::PerLineAll)
                    && has_multiple_formatted_records)
            {
                // `per-line` leaves the block strut at its ordinary used
                // value; only its scalable inline content establishes the
                // enlarged line box.
                line.metrics.height
            } else {
                // The fitted style supplies font-resolved metrics to the
                // text fragments and paint context. The shared used style's
                // containing-block strut determines line-stack geometry.
                line.metrics.height.max(used_style.line_stack_strut())
            };
            record.text_fit_used_style = Some(used_style);
        }
        Some(TextFitAppliedRecords {
            first_formatted,
            last_formatted,
        })
    }

    /// Calculate one line's affine text-fit scale. Measuring at factors one
    /// and two separates scalable text from fixed inline contributions (such
    /// as atomic inlines and absolute tracking) without trying to reconstruct
    /// shaping internals from glyph advances.
    fn text_fit_scale_for_line(
        &mut self,
        line: &InlineLineFragment,
        direction: css::TextFitDirection,
        limit: Option<f32>,
        is_first_formatted_line: bool,
        block_style: &ComputedStyle,
    ) -> TextFitScale {
        let (_, one, _) = self.text_fit_items_at_scale(
            line,
            TextFitScale::ONE,
            is_first_formatted_line,
            block_style,
        );
        let (_, two, _) = self.text_fit_items_at_scale(
            line,
            TextFitScale::new(2.0).expect("two is a valid text-fit measurement scale"),
            is_first_formatted_line,
            block_style,
        );
        text_fit_scale_from_measurements(line.available_width, one, two, direction, limit)
    }

    fn apply_text_fit_scale_to_line(
        &mut self,
        line: &mut InlineLineFragment,
        block_style: &ComputedStyle,
        used_style: &TextFitUsedLineStyle,
        is_first_formatted_line: bool,
    ) {
        let materialize_first_line_style =
            text_fit_applies_first_line_style(block_style, is_first_formatted_line);
        let (items, width, edge_effects) = self.text_fit_items_at_scale(
            line,
            used_style.scale(),
            is_first_formatted_line,
            block_style,
        );
        line.metrics = self.mixed_inline_line_metrics(&items, used_style.block_style(), width);
        line.items = Rc::from(items.into_boxed_slice());
        line.edge_effects = edge_effects;
        line.text = Rc::from(text_for_measured_items(line.items()));
        if materialize_first_line_style {
            line.mark_first_line_style_materialized();
        }
    }

    fn text_fit_items_at_scale(
        &mut self,
        line: &InlineLineFragment,
        scale: TextFitScale,
        is_first_formatted_line: bool,
        block_style: &ComputedStyle,
    ) -> (Vec<MeasuredInlineItem>, f32, InlineLineEdgeEffects) {
        let mut items = line.items().to_vec();
        if text_fit_applies_first_line_style(block_style, is_first_formatted_line)
            && !line.first_line_style_materialized
        {
            let mut source_items = measured_inline_items(&items);
            apply_first_line_pseudos_to_line_items(
                &mut source_items,
                block_style,
                false,
                &mut self.font_system,
            );
            for (measured, source) in items.iter_mut().zip(source_items) {
                measured.item = source;
            }
        }
        for item in &mut items {
            let InlineLineItem::Fragment(fragment) = &mut item.item else {
                continue;
            };
            let fitted_tracking_scope = fragment
                .tracking_scope()
                .map(|scope| scope.scaled_for_text_fit(scale.factor()));
            self.scale_text_fit_used_style(fragment.style_mut(), scale.factor());
            if fragment.style().line_height_is_normal() {
                fragment.style_mut().line_height =
                    self.font_system.used_line_height(fragment.style()).points();
            }
            if let Some(scope) = fitted_tracking_scope {
                *fragment = fragment.clone().with_tracking_scope(scope);
            }
            fragment.clear_cached_shaping_for_used_style_change();
            remeasure_materialized_item(item, &mut self.font_system);
        }
        apply_visual_tracking_boundaries(&mut items);
        let widths = inline_content_width_for_line_items(&items, &mut self.font_system, |item| {
            item.used_advance().points()
        });
        let mut edge_effects = line.edge_effects.clone();
        edge_effects.collapsed_end_trim_width = self.text_fit_edge_effect_width(
            &items,
            &edge_effects,
            InlineLineEdgeEffectKind::CollapsedEndTrim,
        );
        edge_effects.pre_wrap_hanging_width = self.text_fit_edge_effect_width(
            &items,
            &edge_effects,
            InlineLineEdgeEffectKind::PreWrapHang,
        );
        edge_effects.hanging_space_separator_width = widths.trailing_space_width;
        let width = (widths.content_width
            - edge_effects.collapsed_end_trim_width
            - edge_effects.pre_wrap_hanging_width)
            .max(0.0);
        (items, width, edge_effects)
    }

    /// Re-measure a source-owned selected line-edge range in the fitted
    /// style. The range remains an edge effect rather than a scalable text
    /// contribution: its newly measured advance is deducted from the line's
    /// used inline measure.
    fn text_fit_edge_effect_width(
        &mut self,
        items: &[MeasuredInlineItem],
        edge_effects: &InlineLineEdgeEffects,
        kind: InlineLineEdgeEffectKind,
    ) -> f32 {
        edge_effects
            .source_effects
            .iter()
            .filter(|effect| effect.kind == kind)
            .filter_map(|effect| {
                let InlineLineItem::Fragment(fragment) = &items.get(effect.item_index)?.item else {
                    return None;
                };
                fragment
                    .text()
                    .get(effect.source_range.clone())
                    .map(|text| self.font_system.measure_text(text, fragment.style()))
            })
            .sum()
    }

    /// Select CSS inline line fragments from an opportunity graph.
    ///
    /// CSS Inline's line construction is greedy, but every soft-wrap fallback
    /// must choose a CSS Text break opportunity. This selector walks graph
    /// boundaries instead of appending raw items and later searching strings,
    /// keeping wrapping, intrinsic sizing, and future fragmentation attached
    /// to the same reusable line-fragment model:
    /// <https://www.w3.org/TR/css-inline-3/#line-layout>,
    /// <https://www.w3.org/TR/css-text-3/#line-breaking>, and
    /// <https://www.w3.org/TR/css-break-3/#widows-orphans>.
    pub(in crate::layout) fn select_inline_lines_from_graph(
        &mut self,
        graph: &InlineOpportunityGraph,
        context: InlineParagraphContext<'_>,
        line_index: usize,
        starts_after_forced_break: bool,
    ) -> SelectedInlineLines {
        self.select_inline_lines_from_graph_with_block_offset(
            graph,
            context,
            line_index,
            starts_after_forced_break,
            0.0,
        )
    }

    /// Select source lines beginning at an already-resolved physical block
    /// displacement.
    ///
    /// A durable collected sequence can contain explicit source breaks. Those
    /// breaks start a new graph but not a new physical block coordinate, so a
    /// preceding contour retry must remain part of the next graph's float
    /// query.
    pub(in crate::layout) fn select_inline_lines_from_graph_with_block_offset(
        &mut self,
        graph: &InlineOpportunityGraph,
        context: InlineParagraphContext<'_>,
        line_index: usize,
        starts_after_forced_break: bool,
        initial_physical_block_offset: f32,
    ) -> SelectedInlineLines {
        self.select_inline_lines_from_graph_with_block_offset_and_replay(
            graph,
            context,
            line_index,
            starts_after_forced_break,
            initial_physical_block_offset,
            None,
        )
    }

    /// Select source lines while consuming a float transaction committed by
    /// the immediately preceding collected graph.
    ///
    /// Preserved forced breaks split source graphs but do not end the CSS 2.2
    /// float's exclusion. The replay handle supplies that missing source
    /// ordering fact without re-running the float child traversal.
    pub(in crate::layout) fn select_inline_lines_from_graph_with_block_offset_and_replay(
        &mut self,
        graph: &InlineOpportunityGraph,
        context: InlineParagraphContext<'_>,
        mut line_index: usize,
        starts_after_forced_break: bool,
        initial_physical_block_offset: f32,
        carried_inline_float_replay: Option<CommittedInlineFloatReplay>,
    ) -> SelectedInlineLines {
        if graph.is_empty() {
            return SelectedInlineLines {
                fragments: Vec::new(),
                next_line_index: line_index,
                next_physical_block_offset: initial_physical_block_offset,
                has_float_side_effects: false,
                trailing_inline_float_replay: None,
            };
        }
        // Once an inline-source float is positioned, it remains in the graph
        // as a zero-advance source-order marker while its exclusion changes
        // the available bands.  Keeping its position separate lets a retry
        // select the whole affected line against that new band instead of
        // treating preceding inline content as an in-flow prefix of the
        // float.
        let mut placed_inline_float_positions = Vec::new();
        // This token applies only until the first in-flow source row has been
        // selected. Subsequent rows are already ordered after that source and
        // query the committed exclusion normally.
        let mut carried_inline_float_replay = carried_inline_float_replay;
        let mut pending_balanced_float_replays = Vec::<PendingBalancedFloatReplay>::new();
        let mut replayed_float_block_boundaries = Vec::<InlineGraphPosition>::new();
        let mut balanced_plan = self.balanced_line_plan(
            graph,
            context,
            graph.start_position(),
            line_index,
            starts_after_forced_break,
            &placed_inline_float_positions,
        );
        let mut balanced_plan_index = 0usize;
        // `white-space: normal` permits soft wrapping, but does not itself
        // create a CSS Text break inside an unbreakable word. Float placement
        // must therefore ask the shared opportunity graph for an actual
        // non-forced boundary rather than using an item's wrap-capable style
        // as a proxy. The unbreakable-float path below owns out-of-flow
        // positioning without inventing a text break:
        // <https://drafts.csswg.org/css-text-3/#line-break-details> and
        // <https://www.w3.org/TR/CSS22/visuren.html#float-position>.
        let paragraph_start_line_index = line_index;
        let mut fragments = Vec::new();
        let mut has_float_side_effects = false;
        // Physical block advance beyond the nominal `line-height`. Logical
        // line indices remain source-line bookkeeping, but float-band
        // selection must follow the used line boxes. This is observable for
        // atomic inline boxes when their containing block has `line-height:
        // 0`: subsequent boxes still start after each atom's used block-size.
        // CSS Inline Layout defines a line box from its inline-level
        // participants rather than treating the inherited strut as a cap:
        // <https://drafts.csswg.org/css-inline-3/#line-boxes>.
        let mut physical_line_block_offset = initial_physical_block_offset;
        let mut pending_block_before = 0.0;
        let mut start = graph.start_position();
        let graph_end = context
            .line_clamp
            .and_then(|clamp| clamp.inline_source_end())
            .map(|endpoint| InlineGraphPosition {
                run_index: endpoint.run_index(),
                byte_offset: endpoint.byte_offset(),
            })
            .map_or_else(
                || graph.end_position(),
                |endpoint| endpoint.min(graph.end_position()),
            );
        // Keep the remaining-source query incremental. Re-scanning every
        // graph break on every selected line turns a long, float-heavy
        // paragraph into quadratic retry work.
        let soft_wrap_positions = graph
            .break_opportunities_after(graph.start_position())
            .filter(|opportunity| {
                opportunity.position < graph_end && opportunity_is_soft_wrap(*opportunity)
            })
            .map(|opportunity| opportunity.position)
            .collect::<Vec<_>>();
        let mut next_soft_wrap_position = 0usize;
        while start < graph_end {
            if context
                .line_clamp
                .as_ref()
                .is_some_and(|line_clamp| line_clamp.excludes_line(line_index))
            {
                break;
            }
            while start.byte_offset == 0
                && start.run_index < graph.runs.len()
                && inline_line_item_is_collapsible_space(&graph.runs[start.run_index].item)
            {
                start.run_index += 1;
            }
            if start >= graph_end {
                break;
            }
            if let Some(boundary_index) = replayed_float_block_boundaries
                .iter()
                .position(|boundary| *boundary < start)
            {
                // A source-order float is an out-of-flow participant, but
                // its marker still occupies the formatted-row transition
                // between the preceding selected source and following source.
                // Preserve that transition after replaying the float so the
                // next source line queries the slab below the float marker.
                replayed_float_block_boundaries.remove(boundary_index);
                physical_line_block_offset += context.block_style.line_height;
                pending_block_before += context.block_style.line_height;
            }
            while next_soft_wrap_position < soft_wrap_positions.len()
                && soft_wrap_positions[next_soft_wrap_position] <= start
            {
                next_soft_wrap_position += 1;
            }
            if let Some(float) = graph.float_at_position(start).cloned() {
                // A float marker at the beginning of the source row is the
                // zero-prefix case of ordinary source-order placement. Keep
                // the same transactional path used by a marker after text:
                // the float is committed once, then later source is selected
                // against its exclusion. `white-space: nowrap` does not turn
                // this placement into a CSS Text break opportunity.
                // <https://www.w3.org/TR/CSS22/visuren.html#float-position>
                // <https://www.w3.org/TR/CSS22/text.html#white-space-prop>
                if self
                    .try_place_inline_float_in_line_band(
                        graph,
                        start,
                        context,
                        InlineLinePhysicalRow {
                            line_index,
                            identity: SelectedLineIdentity {
                                starts_after_forced_break: starts_after_forced_break
                                    && line_index == paragraph_start_line_index,
                                is_first_formatted_line: context.initial_first_formatted_line
                                    && paragraph_start_line_index == 0
                                    && fragments.is_empty(),
                            },
                            block_offset: physical_line_block_offset,
                        },
                    )
                    .is_placed()
                {
                    placed_inline_float_positions.push(start);
                    has_float_side_effects = true;
                    balanced_plan = None;
                    balanced_plan_index = 0;
                    start.run_index += 1;
                    start.byte_offset = 0;
                    continue;
                }
                // A marker with no source prefix is already at its earliest
                // legal float row.  The ordinary floated-child placement path
                // is allowed to move it farther down around preceding
                // exclusions, but the marker itself must be consumed even
                // when the provisional line band rejects it.  Otherwise the
                // greedy selector retries the same zero-advance graph node
                // indefinitely and effectively promotes the marker to a
                // line-break opportunity.
                self.place_inline_waiting_float_on_row(
                    &float,
                    start,
                    context,
                    InlineLinePhysicalRow {
                        line_index,
                        identity: SelectedLineIdentity {
                            starts_after_forced_break: starts_after_forced_break
                                && line_index == paragraph_start_line_index,
                            is_first_formatted_line: context.initial_first_formatted_line
                                && paragraph_start_line_index == 0
                                && fragments.is_empty(),
                        },
                        block_offset: physical_line_block_offset,
                    },
                );
                placed_inline_float_positions.push(start);
                has_float_side_effects = true;
                balanced_plan = None;
                balanced_plan_index = 0;
                start.run_index += 1;
                start.byte_offset = 0;
                continue;
            }
            let line_identity = SelectedLineIdentity {
                starts_after_forced_break: starts_after_forced_break
                    && line_index == paragraph_start_line_index,
                is_first_formatted_line: context.initial_first_formatted_line
                    && paragraph_start_line_index == 0
                    && fragments.is_empty(),
            };
            let committed_preceding_float =
                graph.runs[..start.run_index]
                    .iter()
                    .enumerate()
                    .any(|(run_index, run)| {
                        matches!(run.item, InlineLineItem::Float(_))
                            && placed_inline_float_positions
                                .contains(&InlineGraphPosition::at_run_start(run_index))
                    });
            let immediately_preceding_unbreakable_float = start
                .run_index
                .checked_sub(1)
                .and_then(|run_index| {
                    graph.float_at_position(InlineGraphPosition::at_run_start(run_index))
                })
                .is_some_and(|float| !float.style().allows_soft_wrap());
            let starts_initial_letter = matches!(
                graph.runs.get(start.run_index).map(|run| &run.item),
                Some(InlineLineItem::Fragment(fragment))
                    if !fragment.style().initial_letter.is_normal()
            );
            let has_provisional_initial_for_line =
                self.float_contexts.last().is_some_and(|floats| {
                    floats.shapes.iter().any(|shape| {
                        shape.kind == FlowExclusionKind::InitialLetter
                            && shape.page_index == self.current_float_page_index()
                            && shape.initial_letter.as_ref().is_some_and(|layout| {
                                layout.provisional && layout.impacted_line_range.start == line_index
                            })
                    })
                });
            if starts_initial_letter
                && committed_preceding_float
                && !has_provisional_initial_for_line
            {
                self.register_provisional_initial_letter_exclusion(
                    graph,
                    context,
                    line_index,
                    line_identity.starts_after_forced_break,
                    true,
                );
            }
            // A preceding ordinary soft break does not make the remaining
            // source wrappable. Once that break has committed, a following
            // `nowrap` (or otherwise unbreakable) segment containing a float
            // must retain its whole source range and overflow as one line.
            // A float marker is a placement boundary rather than a CSS Text
            // wrapping opportunity. Other atomic boundaries are CSS Text
            // opportunities between adjacent atomic inline-level boxes and
            // keep the remaining source wrappable.
            // <https://www.w3.org/TR/css-text-3/#white-space-property>
            let remaining_source_breakability =
                if next_soft_wrap_position < soft_wrap_positions.len() {
                    InlineSourceBreakability::HasLegalSoftWrap
                } else {
                    InlineSourceBreakability::Unbreakable
                };
            // Float-excluded rows consume physical block-size but do not make
            // a formatted line. Preserve this identity independently of the
            // physical line index for `text-indent` and hanging indents.
            if balanced_plan.is_none()
                && matches!(
                    context.block_style.text_wrap_style,
                    css::TextWrapStyle::Balance
                )
                && graph.runs[start.run_index..]
                    .iter()
                    .enumerate()
                    .all(|(offset, run)| {
                        !matches!(run.item, InlineLineItem::Float(_))
                            || placed_inline_float_positions.contains(
                                &InlineGraphPosition::at_run_start(start.run_index + offset),
                            )
                    })
            {
                balanced_plan = self.balanced_line_plan(
                    graph,
                    context,
                    start,
                    line_index,
                    line_identity.starts_after_forced_break,
                    &placed_inline_float_positions,
                );
            }
            if let Some(plan) = balanced_plan.as_ref()
                && let Some(replay_index) = pending_balanced_float_replays
                    .iter()
                    .position(|replay| plan.iter().any(|entry| entry.start < replay.position))
            {
                let replayed_plan = plan.clone();
                let replay = pending_balanced_float_replays.remove(replay_index);
                // The float's source follows every selected entry whose start
                // precedes its marker. Replaying from the captured snapshot
                // places it after those final source lines instead of beside
                // the greedy prefix that first discovered the marker.
                let source_lines_before_float = plan
                    .iter()
                    .filter(|entry| entry.start < replay.position)
                    .count();
                let target_line_index = replay.source_line_index + source_lines_before_float;
                self.restore(replay.snapshot);
                self.adjoining_float_origin_y = None;
                if let Some(float) = graph.float_at_position(replay.position).cloned() {
                    self.place_inline_waiting_float(
                        &float,
                        replay.position,
                        context,
                        target_line_index,
                    );
                }
                replayed_float_block_boundaries.push(replay.position);
                // The replay changes paint/exclusion geometry, not the
                // source candidate that won the balance search. Retain its
                // endpoints rather than starting over with a greedy normal
                // sequence against the newly placed float.
                balanced_plan = Some(replayed_plan);
                balanced_plan_index = 0;
                continue;
            }
            // A balance plan is a completed source selection, not a hint to
            // the ordinary greedy selector.  In particular, a source-order
            // float makes the selected line's exclusion band meaningful to
            // the balance score.  Remember when this endpoint came from that
            // plan so the later float and clamp reconciliation stages can
            // validate it without silently replacing it with a greedy edge.
            // <https://drafts.csswg.org/css-text-4/#text-wrap-style>
            let selected_from_balanced_plan = balanced_plan
                .as_ref()
                .and_then(|plan| plan.get(balanced_plan_index))
                .filter(|plan| plan.start == start)
                .copied();
            let mut selected_end = selected_from_balanced_plan
                .map(|plan| plan.end)
                .unwrap_or_else(|| {
                    self.select_inline_line_end_with_block_offset(
                        graph,
                        start,
                        context,
                        line_index,
                        line_identity,
                        physical_line_block_offset,
                    )
                });
            // The inherited strut is only a provisional float-query slab.
            // Refine an ordinary greedy candidate against its complete used
            // line slab before committing a CSS Shapes band. Atomic boxes can
            // enlarge a zero-height strut, but ordinary text and markers can
            // also change the band at a shaped float contour. A balanced
            // endpoint has already been selected against its line-local band;
            // replacing it here would discard the candidate that was scored
            // with that band and turn balanced float paragraphs back into
            // greedy paragraphs.
            //
            // Each iteration selects a legal graph boundary for one physical
            // slab. The cap is a diagnostic guard for a pathological contour
            // alternation; normal state transitions either stabilize or move
            // the candidate to a later physical slab below.
            // <https://drafts.csswg.org/css-inline-3/#line-boxes>
            // <https://drafts.csswg.org/TR/css-shapes-1/#shape-outside-property>
            if selected_from_balanced_plan.is_none()
                && selected_end.position > start
                && self
                    .float_contexts
                    .last()
                    .is_some_and(|floats| !floats.shapes.is_empty())
            {
                let mut converged = false;
                for _ in 0..4 {
                    let provisional = graph.materialize_line(
                        InlineGraphRange {
                            start,
                            end: selected_end.position,
                        },
                        selected_end.break_opportunity,
                        &mut self.font_system,
                        context.block_style,
                    );
                    let provisional_metrics = self.mixed_inline_line_metrics(
                        &provisional.items,
                        context.block_style,
                        provisional.content_width,
                    );
                    let block_size = used_inline_line_block_size_from_items(
                        &provisional.items,
                        provisional_metrics.height,
                        context.block_style,
                    );
                    if block_size <= INLINE_FLOAT_EPSILON {
                        converged = true;
                        break;
                    }
                    let refined = self.select_inline_line_end_with_block_span(
                        graph,
                        start,
                        context,
                        line_index,
                        line_identity,
                        PhysicalLineSlab::inherited_strut(
                            line_index,
                            physical_line_block_offset,
                            context.block_style.line_height,
                        )
                        .with_used_block_size(block_size),
                    );
                    if refined.position == selected_end.position
                        && refined.break_opportunity == selected_end.break_opportunity
                    {
                        converged = true;
                        break;
                    }
                    selected_end = refined;
                    if selected_end.position <= start {
                        converged = true;
                        break;
                    }
                }
                debug_assert!(
                    converged,
                    "selected inline candidate did not converge with its physical float slab"
                );
            }
            let initial_source_handoff_end = initial_letter_source_handoff_end(graph, start);
            if let Some(initial_end) = initial_source_handoff_end {
                selected_end = SelectedInlineLineEnd {
                    position: initial_end,
                    break_opportunity: graph.break_opportunity_at(initial_end).filter(
                        |opportunity| !matches!(opportunity.kind, InlineBreakKind::FloatPlacement),
                    ),
                };
                // A balance plan selected against the unsplit source is no
                // longer applicable after the initial-letter-specific first
                // line boundary.
                balanced_plan = None;
                balanced_plan_index = 0;
            }
            if selected_end.position > graph_end {
                selected_end = SelectedInlineLineEnd {
                    position: graph_end,
                    break_opportunity: graph.break_opportunity_at(graph_end),
                };
            }
            // Reserve the truncation marker while selecting the final clamped
            // line.  Removing materialized items afterward loses the graph
            // source ranges that own CSS Text Phase II effects, and can also
            // select a different break from the one that actually fits with
            // the marker.
            // <https://drafts.csswg.org/css-overflow-3/#line-clamp>
            let is_final_clamped_line = context
                .line_clamp
                .as_ref()
                .is_some_and(|line_clamp| line_clamp.is_terminal_line(line_index));
            let clamp_continues_after_line = line_clamp_has_later_in_flow_content(context);
            let mut final_clamp_marker_replaces_source =
                selected_from_balanced_plan.is_some_and(|plan| plan.clamp_marker_replaces_source);
            if is_final_clamped_line
                && selected_from_balanced_plan.is_none()
                && !final_clamp_marker_replaces_source
                // A graph can end at a preserved forced break while the
                // clamp container still has later in-flow source. That is a
                // legal clamp point whose marker must replace this final
                // graph's unbreakable line when no soft wrap fits beside it;
                // only a true terminal graph with no continuation skips the
                // marker-fitting pass.
                && (selected_end.position < graph_end || clamp_continues_after_line)
                && (!graph_remaining_after_position_is_trimmable(graph, selected_end.position)
                    || clamp_continues_after_line)
            {
                let ellipsis_width =
                    self.line_clamp_marker_width(context.line_clamp, context.block_style);
                if ellipsis_width > 0.0 {
                    let band = self.inline_float_band_for_line(
                        line_index,
                        context.block_style,
                        context.available_width,
                        context.padding_left,
                    );
                    let line_indent = used_line_indent_for_formatted_line(
                        line_identity.is_first_formatted_line,
                        line_identity.starts_after_forced_break,
                        context.hanging_indent,
                        context.block_style,
                        band.width(),
                    );
                    let marker_available_width =
                        (band.width() - line_indent - ellipsis_width).max(0.0);
                    let marker_selected = self.select_inline_line_end_for_block_ellipsis(
                        graph,
                        start,
                        context.block_style,
                        marker_available_width,
                        line_index,
                    );
                    let marker_selection_fits = marker_selected.position > start
                        && self.balanced_line_fit_width(
                            graph,
                            start,
                            marker_selected,
                            context.block_style,
                            line_index,
                            Some(marker_available_width),
                        ) <= marker_available_width + INLINE_FLOAT_EPSILON;
                    if marker_selection_fits {
                        selected_end = marker_selected;
                    } else {
                        // An unbreakable source segment may not fit beside
                        // the block ellipsis at all. In that case the marker
                        // replaces the final line's source rather than
                        // allowing the ordinary no-fit fallback to overflow
                        // underneath it.
                        // <https://drafts.csswg.org/css-overflow-4/#line-clamp>
                        selected_end = SelectedInlineLineEnd {
                            position: start,
                            break_opportunity: None,
                        };
                        final_clamp_marker_replaces_source = true;
                    }
                }
            }
            // A paragraph without soft-wrap opportunities can still contain
            // forced breaks. Those breaks delimit real line boxes and must be
            // honored by line clamping rather than collapsing the remaining
            // paragraph into one unbounded line.
            let selected_forced_break = selected_end
                .break_opportunity
                .is_some_and(|opportunity| matches!(opportunity.kind, InlineBreakKind::Forced));
            let mut end = if final_clamp_marker_replaces_source {
                start
            } else if (!remaining_source_breakability.has_legal_soft_wrap()
                && !selected_forced_break
                && initial_source_handoff_end.is_none())
                || selected_end.position <= start
            {
                graph_end
            } else {
                selected_end.position.min(graph_end)
            };
            end = line_end_extended_over_adjacent_inline_float_markers(graph, end).min(graph_end);
            // A candidate soft wrap before a visible `nowrap` continuation
            // with a float must remain the preceding line's end. In
            // particular, the marker-only lookahead below must not absorb the
            // continuation's prefix. The following source selection takes the
            // whole continuation, and its ordinary transaction defers the
            // float below the overflowing line when needed.
            // <https://www.w3.org/TR/css-text-3/#white-space-property>
            // <https://www.w3.org/TR/CSS22/visuren.html#float-position>
            let preserve_unbreakable_continuation = graph
                .unbreakable_inline_float_continuation_after(InlineGraphRange { start, end })
                .is_some_and(|continuation| {
                    continuation.source_range.start == end
                        && continuation.marker < continuation.source_range.end
                });
            // Outside a visible `nowrap` continuation, a marker may extend a
            // fitting ordinary source prefix without becoming a CSS Text wrap
            // opportunity. The continuation query above deliberately blocks
            // that marker-only extension for `nowrap` source.
            // <https://www.w3.org/TR/css-text-3/#white-space-property>
            // <https://www.w3.org/TR/CSS22/visuren.html#float-position>
            if !preserve_unbreakable_continuation
                && let Some(float_position) =
                    graph.first_float_position_in_range(InlineGraphRange {
                        start,
                        end: graph_end,
                    })
                && end < float_position
                && !graph
                    .break_opportunities_after(end)
                    .take_while(|opportunity| opportunity.position <= float_position)
                    .any(opportunity_is_soft_wrap)
            {
                let prefix = graph.materialize_line(
                    InlineGraphRange {
                        start,
                        end: float_position,
                    },
                    None,
                    &mut self.font_system,
                    context.block_style,
                );
                let full_indent = used_line_indent_for_formatted_line(
                    line_identity.is_first_formatted_line,
                    line_identity.starts_after_forced_break,
                    context.hanging_indent,
                    context.block_style,
                    context.available_width,
                );
                if prefix.fitting_width <= context.available_width - full_indent + 0.5 {
                    end = float_position;
                }
            }
            // A wrappable selected range may fit the unexcluded containing
            // block while exceeding this line's temporary float band. CSS 2.2
            // then moves the line below the float instead of painting inline
            // overflow next to the exclusion. The decision is local to this
            // range: a preceding normal run must not make later `nowrap`
            // content defer instead of overflowing beside the float. Preserve
            // the skipped line so paint and subsequent float-band queries
            // advance together.
            // <https://www.w3.org/TR/CSS22/visuren.html#floats>
            // A zero inherited strut does not make an atomic inline's line
            // zero-height. Query the retry band with the selected range's
            // actual used block slab, otherwise an inline-block can be
            // selected beside a float at `line-height: 0` and only discover
            // its collision after it has already been committed.
            // <https://drafts.csswg.org/css-inline-3/#line-boxes>
            let band = self.inline_float_band_for_line_with_block_offset(
                line_index,
                context.block_style,
                context.available_width,
                context.padding_left,
                physical_line_block_offset,
            );
            let line_indent = used_line_indent_for_formatted_line(
                line_identity.is_first_formatted_line,
                line_identity.starts_after_forced_break,
                context.hanging_indent,
                context.block_style,
                band.width(),
            );
            let containing_indent = used_line_indent_for_formatted_line(
                line_identity.is_first_formatted_line,
                line_identity.starts_after_forced_break,
                context.hanging_indent,
                context.block_style,
                context.available_width,
            );
            let measures = InlineSelectionMeasures::new(context.available_width, band);
            let current_available_width = measures.band_after_indent(line_indent);
            let full_available_width = measures.containing_after_indent(containing_indent);
            // A selected range that fits the containing block but not this
            // float-reduced band must retry from the next physical slab.
            // CSS 2.2's float retry is line-box placement, not a synthetic
            // break inside a word or atomic inline participant.
            let replays_committed_preceding_float = carried_inline_float_replay
                .is_some_and(|replay| replay.applies_before_source_row(line_index));
            if context.block_style.allows_soft_wrap()
                && (remaining_source_breakability.has_legal_soft_wrap()
                    || (committed_preceding_float && !immediately_preceding_unbreakable_float)
                    || replays_committed_preceding_float)
                && end > start
                && current_available_width + INLINE_FLOAT_EPSILON < full_available_width
                && let materialized = graph.materialize_line(
                    InlineGraphRange { start, end },
                    (end < graph_end)
                        .then_some(selected_end.break_opportunity)
                        .flatten(),
                    &mut self.font_system,
                    context.block_style,
                )
            {
                // A provisional initial-letter exclusion selects the source
                // beside the initial.  Its own isolated source advance is
                // not companion content competing for that narrowed band:
                // testing it there would make a wide margin box defer its
                // originating line below itself.
                // <https://drafts.csswg.org/css-inline-3/#initial-letter-floats>
                let initial_source_advance = (line_identity.is_first_formatted_line
                    && start.byte_offset == 0)
                    .then(|| {
                        let initial_index = materialized.items.iter().position(|item| {
                            matches!(
                                item.as_ref(),
                                InlineLineItem::Fragment(fragment)
                                    if !fragment.style().initial_letter.is_normal()
                            )
                        })?;
                        let leading_pseudo_width = materialized.items[..initial_index]
                            .iter()
                            .rev()
                            .take_while(|item| {
                                matches!(
                                    item.as_ref(),
                                    InlineLineItem::Fragment(fragment)
                                        if fragment.first_letter_pseudo_role()
                                            == FirstLetterPseudoFragmentRole::LeadingPreservedWhitespace
                                )
                            })
                            .map(|item| item.used_advance().points())
                            .sum::<f32>();
                        Some(
                            leading_pseudo_width
                                + materialized.items[initial_index].used_advance().points(),
                        )
                    })
                    .flatten()
                    .unwrap_or(0.0);
                let companion_fitting_width =
                    (materialized.fitting_width - initial_source_advance).max(0.0);
                let requires_full_float_band = (committed_preceding_float
                    || replays_committed_preceding_float)
                    && companion_fitting_width > current_available_width + INLINE_FLOAT_EPSILON;
                let required_float_clearance_width = if requires_full_float_band {
                    float_clearance_required_width(
                        companion_fitting_width,
                        containing_indent,
                        context.available_width,
                    )
                } else {
                    companion_fitting_width + containing_indent
                };
                let needs_band_retry = companion_fitting_width
                    > current_available_width + INLINE_FLOAT_EPSILON
                    && required_float_clearance_width
                        <= full_available_width + INLINE_FLOAT_EPSILON;
                // In vertical writing, a dropped initial owns the originating
                // logical block column even when its companion happens to fit
                // in the remaining vertical measure. The companion is placed
                // in the adjacent block column; treating this solely as a
                // width retry leaves it painted on top of the initial's own
                // column whenever the remaining height is generous.
                // <https://drafts.csswg.org/css-inline-3/#initial-letter-position>
                let needs_vertical_initial_handoff = matches!(
                    context.block_style.writing_mode,
                    WritingMode::VerticalRl | WritingMode::SidewaysRl
                ) && line_identity.is_first_formatted_line
                    && initial_source_advance > INLINE_FLOAT_EPSILON;
                // The used block slab of a horizontal line is not known until
                // its atomic participants have been materialized. The
                // committed-fragment retry below is therefore the only place
                // allowed to advance this source row. Retrying here and then
                // again after materialization accumulates clearance for one
                // line.
                // <https://www.w3.org/TR/CSS22/visuren.html#floats>
                if context.block_style.writing_mode != WritingMode::HorizontalTb
                    && (needs_band_retry || needs_vertical_initial_handoff)
                {
                    let metrics = self.mixed_inline_line_metrics(
                        &materialized.items,
                        context.block_style,
                        materialized.content_width,
                    );
                    let slab_width = used_inline_line_block_size_from_items(
                        &materialized.items,
                        metrics.height,
                        context.block_style,
                    );
                    let position = self.inline_line_physical_position_with_block_offset(
                        line_index,
                        context.block_style,
                        physical_line_block_offset,
                    );
                    let starting_slab = vertical_line_block_slab(
                        position,
                        context.block_style,
                        context.padding_left,
                        slab_width,
                    );
                    let vertical_inline_span = vertical_physical_inline_span(
                        context.block_style.writing_mode,
                        context.block_style.used_direction(),
                        PageTopBlockPosition::new(position.cursor_y),
                        layout_pt(context.available_width),
                    );
                    let required_width = companion_fitting_width + containing_indent;
                    let next_left = if needs_vertical_initial_handoff {
                        // The initial's wrapping box occupies its own
                        // block-start column. Move the companion past the
                        // physical block-end edge of that box instead of
                        // asking a width query which can legitimately fit
                        // beside it in the same column.
                        self.float_contexts.last().and_then(|float_context| {
                            float_context
                                .shapes
                                .iter()
                                .filter(|shape| {
                                    shape.kind == FlowExclusionKind::InitialLetter
                                        && shape.page_index == self.current_float_page_index()
                                        && shape.rect.x() + shape.rect.width()
                                            > starting_slab.left_x() + INLINE_FLOAT_EPSILON
                                        && shape.rect.x()
                                            < starting_slab.right_x() - INLINE_FLOAT_EPSILON
                                })
                                .map(|shape| match context.block_style.writing_mode {
                                    WritingMode::VerticalRl | WritingMode::SidewaysRl => {
                                        PageInlinePosition::new(
                                            shape.rect.x() - starting_slab.width(),
                                        )
                                    }
                                    WritingMode::VerticalLr | WritingMode::SidewaysLr => {
                                        PageInlinePosition::new(shape.rect.x() + shape.rect.width())
                                    }
                                    WritingMode::HorizontalTb => unreachable!(),
                                })
                                .find(|candidate| match context.block_style.writing_mode {
                                    WritingMode::VerticalRl | WritingMode::SidewaysRl => {
                                        candidate.points()
                                            < starting_slab.left_x() - INLINE_FLOAT_EPSILON
                                    }
                                    WritingMode::VerticalLr | WritingMode::SidewaysLr => {
                                        candidate.points()
                                            > starting_slab.left_x() + INLINE_FLOAT_EPSILON
                                    }
                                    WritingMode::HorizontalTb => false,
                                })
                        })
                    } else {
                        self.float_contexts.last().and_then(|float_context| {
                            float_context.next_vertical_content_slab_with_width(
                                context.block_style.writing_mode,
                                context.block_style.used_direction(),
                                self.current_float_page_index(),
                                starting_slab,
                                vertical_inline_span,
                                required_width,
                            )
                        })
                    };
                    if let Some(next_left) = next_left {
                        let block_advance = match context.block_style.writing_mode {
                            WritingMode::VerticalLr | WritingMode::SidewaysLr => {
                                next_left.points() - starting_slab.left_x()
                            }
                            WritingMode::VerticalRl | WritingMode::SidewaysRl => {
                                starting_slab.left_x() - next_left.points()
                            }
                            WritingMode::HorizontalTb => unreachable!(),
                        };
                        if block_advance > INLINE_FLOAT_EPSILON {
                            physical_line_block_offset += block_advance;
                            pending_block_before += block_advance;
                            balanced_plan = None;
                            continue;
                        }
                    }
                    line_index += 1;
                    balanced_plan = None;
                    continue;
                }
            }
            let selected_range = InlineGraphRange { start, end };
            if !remaining_source_breakability.has_legal_soft_wrap()
                && let Some(selection) = self.try_select_unbreakable_line_with_inline_floats(
                    graph,
                    InlineGraphRange {
                        start,
                        end: graph_end,
                    },
                    SelectedInlineLineEnd {
                        position: graph_end,
                        break_opportunity: graph.break_opportunity_at(graph_end),
                    },
                    context,
                    InlineLinePhysicalRow {
                        line_index,
                        identity: line_identity,
                        block_offset: physical_line_block_offset,
                    },
                )
            {
                physical_line_block_offset += selection.block_before
                    + used_inline_line_block_advance(&selection.fragment, context.block_style);
                fragments.push(SelectedInlineLine {
                    fragment: selection.fragment,
                    line_index,
                    block_before: std::mem::take(&mut pending_block_before)
                        + selection.block_before,
                });
                has_float_side_effects = true;
                line_index += 1;
                balanced_plan_index += 1;
                start = end;
                continue;
            }
            let affected_float_range = inline_float_affected_range(graph, selected_range);
            if matches!(
                affected_float_range,
                InlineFloatAffectedRange::UnbreakableContinuation { .. }
            ) && let Some(selection) = self.try_select_unbreakable_line_with_inline_floats(
                graph,
                selected_range,
                SelectedInlineLineEnd {
                    position: end,
                    break_opportunity: graph.break_opportunity_at(end),
                },
                context,
                InlineLinePhysicalRow {
                    line_index,
                    identity: line_identity,
                    block_offset: physical_line_block_offset,
                },
            ) {
                // The transaction has committed every float in source order
                // and re-materialized the selected source range against its
                // accepted band. Keep that geometry frozen for paint/replay.
                physical_line_block_offset += selection.block_before
                    + used_inline_line_block_advance(&selection.fragment, context.block_style);
                fragments.push(SelectedInlineLine {
                    fragment: selection.fragment,
                    line_index,
                    block_before: std::mem::take(&mut pending_block_before)
                        + selection.block_before,
                });
                has_float_side_effects = true;
                line_index += 1;
                balanced_plan_index += 1;
                start = end;
                continue;
            }
            if let Some(float_position) = affected_float_range.marker()
                && !placed_inline_float_positions.contains(&float_position)
            {
                if matches!(
                    context.block_style.text_wrap_style,
                    css::TextWrapStyle::Balance
                ) && placed_inline_float_positions.is_empty()
                    && let Some(float) = graph.float_at_position(float_position).cloned()
                {
                    // Balancing must choose the whole affected line against
                    // the float's exclusion band. Position the float, record
                    // its zero-advance graph marker, and retry from the same
                    // line start instead of committing the preceding source
                    // as an in-flow prefix.
                    // <https://drafts.csswg.org/css-text-4/#text-wrap-style>
                    self.place_inline_waiting_float(&float, float_position, context, line_index);
                    placed_inline_float_positions.push(float_position);
                    has_float_side_effects = true;
                    balanced_plan = None;
                    balanced_plan_index = 0;
                    continue;
                }
                if float_position <= start {
                    if let Some(float) = graph.float_at_position(float_position).cloned() {
                        self.place_inline_waiting_float(
                            &float,
                            float_position,
                            context,
                            line_index,
                        );
                        placed_inline_float_positions.push(float_position);
                        has_float_side_effects = true;
                    }
                    start = InlineGraphPosition::at_run_start(float_position.run_index + 1);
                    continue;
                }
                // A float following in-flow text is positioned after that
                // text when it fits the remaining current-line band.  Placing
                // it against the whole band first would incorrectly let its
                // exclusion shift preceding source content.  If it does not
                // fit, the prefix is committed and the marker is retried at
                // the next line top.
                // <https://www.w3.org/TR/CSS22/visuren.html#float-position>
                let mut prefix = self.materialize_inline_line_fragment(
                    graph,
                    InlineGraphRange {
                        start,
                        end: float_position,
                    },
                    context,
                    InlineLinePhysicalRow {
                        line_index,
                        identity: line_identity,
                        block_offset: physical_line_block_offset,
                    },
                    None,
                );
                let placement_snapshot = self.snapshot();
                if matches!(
                    context.block_style.text_wrap_style,
                    css::TextWrapStyle::Balance
                ) && let Some(float) = graph.float_at_position(float_position).cloned()
                {
                    // This is an unresolved source-order float, so the
                    // greedy prefix that discovered it cannot establish its
                    // physical row. Put the float below that provisional row,
                    // then restart selection from the same source position.
                    // The following balance pass sees the actual float band
                    // and selects the preceding line against it. Placing it
                    // beside the greedy prefix first would freeze the float
                    // one row too early when balancing moves source back to
                    // the preceding line.
                    // <https://drafts.csswg.org/css-text-4/#text-wrap-style>
                    // This float follows in-flow source. It is therefore not
                    // adjoining the earlier float run, even though its
                    // placement is deferred while the balance candidate is
                    // replayed. Leaving the old origin active lets float
                    // placement pull it back to the preceding float's top.
                    self.adjoining_float_origin_y = None;
                    self.place_inline_waiting_float(
                        &float,
                        float_position,
                        context,
                        line_index + 1,
                    );
                    placed_inline_float_positions.push(float_position);
                    pending_balanced_float_replays.push(PendingBalancedFloatReplay {
                        position: float_position,
                        source_line_index: line_index,
                        snapshot: placement_snapshot,
                    });
                    has_float_side_effects = true;
                    balanced_plan = None;
                    balanced_plan_index = 0;
                    continue;
                }
                if let Some(placement) = self.try_place_inline_float_on_current_line(
                    graph,
                    float_position,
                    prefix.metrics.width,
                    context,
                    InlineLinePhysicalRow {
                        line_index,
                        identity: line_identity,
                        block_offset: physical_line_block_offset,
                    },
                ) {
                    placed_inline_float_positions.push(float_position);
                    has_float_side_effects = true;
                    if matches!(
                        context.block_style.text_wrap_style,
                        css::TextWrapStyle::Balance
                    ) {
                        // The source-order placement above establishes this
                        // float's physical row. Re-select the whole balance
                        // group with that resolved exclusion rather than
                        // committing the greedy prefix that was only needed
                        // to place the float. This is what lets a later float
                        // move earlier source to a more balanced preceding
                        // line without moving the float ahead of its source.
                        // <https://drafts.csswg.org/css-text-4/#text-wrap-style>
                        balanced_plan = None;
                        balanced_plan_index = 0;
                        continue;
                    }
                    let suffix_start =
                        InlineGraphPosition::at_run_start(float_position.run_index + 1);
                    let suffix_is_empty =
                        graph_remaining_after_position_is_trimmable(graph, suffix_start);
                    prefix.freeze_float_band();
                    if let Some(combined) = self.try_select_inline_float_same_line_suffix(
                        graph,
                        prefix.clone(),
                        float_position,
                        suffix_start,
                        placement,
                        context,
                        line_index,
                    ) {
                        let end = combined.end;
                        physical_line_block_offset +=
                            used_inline_line_block_advance(&combined.fragment, context.block_style);
                        fragments.push(SelectedInlineLine {
                            fragment: combined.fragment,
                            line_index,
                            block_before: std::mem::take(&mut pending_block_before),
                        });
                        line_index += 1;
                        balanced_plan_index += 1;
                        start = end;
                        continue;
                    }
                    if !suffix_is_empty && !placement.fits_remaining_band() {
                        self.restore(placement_snapshot);
                        prefix.requery_float_band();
                        physical_line_block_offset +=
                            used_inline_line_block_advance(&prefix, context.block_style);
                        fragments.push(SelectedInlineLine {
                            fragment: prefix,
                            line_index,
                            block_before: std::mem::take(&mut pending_block_before),
                        });
                        line_index += 1;
                        balanced_plan_index += 1;
                        start = float_position;
                        continue;
                    }
                    physical_line_block_offset +=
                        used_inline_line_block_advance(&prefix, context.block_style);
                    fragments.push(SelectedInlineLine {
                        fragment: prefix,
                        line_index,
                        block_before: std::mem::take(&mut pending_block_before),
                    });
                    line_index += 1;
                    balanced_plan_index += 1;
                    start = suffix_start;
                    continue;
                }
                if matches!(
                    context.block_style.text_wrap_style,
                    css::TextWrapStyle::Balance
                ) && let Some(float) = graph.float_at_position(float_position).cloned()
                {
                    // The float cannot share the provisional greedy prefix,
                    // so it moves to the next formatted row. Keep the prefix
                    // provisional too: once the float's real exclusion is
                    // known, the balance search may move part of it onto a
                    // different source line.
                    // <https://drafts.csswg.org/css-text-4/#text-wrap-style>
                    self.place_inline_waiting_float(
                        &float,
                        float_position,
                        context,
                        line_index + 1,
                    );
                    placed_inline_float_positions.push(float_position);
                    has_float_side_effects = true;
                    balanced_plan = None;
                    balanced_plan_index = 0;
                    continue;
                }
                let mut fragment = self.materialize_inline_line_fragment(
                    graph,
                    InlineGraphRange {
                        start,
                        end: float_position,
                    },
                    context,
                    InlineLinePhysicalRow {
                        line_index,
                        identity: line_identity,
                        block_offset: physical_line_block_offset,
                    },
                    None,
                );
                self.register_initial_letter_exclusion_for_line(
                    &mut fragment,
                    context,
                    line_index,
                    physical_line_block_offset,
                    false,
                );
                physical_line_block_offset +=
                    used_inline_line_block_advance(&fragment, context.block_style);
                fragments.push(SelectedInlineLine {
                    fragment,
                    line_index,
                    block_before: std::mem::take(&mut pending_block_before),
                });
                line_index += 1;
                balanced_plan_index += 1;
                start = float_position;
                continue;
            }
            let break_opportunity = selected_end
                .break_opportunity
                .filter(|opportunity| opportunity.position == end && end < graph_end);
            let mut fragment = self.materialize_inline_line_fragment(
                graph,
                selected_range,
                context,
                InlineLinePhysicalRow {
                    line_index,
                    identity: line_identity,
                    block_offset: physical_line_block_offset,
                },
                break_opportunity,
            );
            // CSS 2.2 requires a line that cannot contain its content beside
            // a float to move down until a later float slab can contain it.
            // This must happen after materialization: atomic inlines can make
            // the line taller than the selection strut, changing its actual
            // float-exclusion band.
            // <https://www.w3.org/TR/CSS22/visuren.html#floats>
            // Inline-source float transactions own their own source-order
            // retry rules. This CSS 2.2 line-clearance path is for ordinary
            // established exclusions, not a suffix after an inline marker.
            let selected_range_contains_placed_inline_float =
                placed_inline_float_positions.iter().any(|position| {
                    *position >= selected_range.start && *position < selected_range.end
                });
            // A `nowrap` suffix following an inline-source float is not an
            // ordinary line that may move below an established exclusion.
            // The float's source-order transaction already selected its row;
            // the unbreakable suffix instead overflows in that row's
            // remaining band, where paint applies the float's inline-start
            // offset.  Retrying below the float loses that source-order
            // relationship and turns the suffix into a full-width line.
            // <https://www.w3.org/TR/CSS22/visuren.html#floats>
            // <https://www.w3.org/TR/css-text-3/#white-space-property>
            let follows_unbreakable_placed_inline_float =
                committed_preceding_float && immediately_preceding_unbreakable_float;
            if !selected_range_contains_placed_inline_float
                && !follows_unbreakable_placed_inline_float
                && let HorizontalFloatLinePlacement::RetryAtLaterSlab { block_advance } = self
                    .resolve_horizontal_float_line_placement(
                        &fragment,
                        context,
                        InlineLinePhysicalRow {
                            line_index,
                            identity: line_identity,
                            block_offset: physical_line_block_offset,
                        },
                    )
            {
                physical_line_block_offset += block_advance;
                pending_block_before += block_advance;
                balanced_plan = None;
                continue;
            }
            // A retry after placing an inline-source float already selected
            // this fragment against that float's exclusion band. Preserve the
            // stored band at paint time instead of applying the live band a
            // second time; that distinction is essential when a left float
            // shifts an RTL line's physical left edge.
            if placed_inline_float_positions
                .iter()
                .any(|position| *position >= selected_range.start && *position < selected_range.end)
            {
                fragment.freeze_float_band();
            }
            self.register_initial_letter_exclusion_for_line(
                &mut fragment,
                context,
                line_index,
                physical_line_block_offset,
                false,
            );
            let leading_initial_letter_slots = raised_initial_letter_leading_line_slots(&fragment);
            physical_line_block_offset +=
                used_inline_line_block_advance(&fragment, context.block_style);
            fragments.push(SelectedInlineLine {
                fragment,
                line_index,
                block_before: std::mem::take(&mut pending_block_before),
            });
            carried_inline_float_replay = None;
            // The current line owns the initial letter. The following normal
            // source begins after the exposed leading slots; leaving the
            // indices sparse causes durable record construction to emit the
            // corresponding phantom struts.
            line_index += 1 + leading_initial_letter_slots;
            balanced_plan_index += 1;
            start = end;
        }
        if context
            .line_clamp
            .as_ref()
            .is_some_and(|line_clamp| line_clamp.reached_after_line_count(line_index))
            && (!graph_remaining_after_position_is_trimmable(graph, start)
                || line_clamp_has_later_in_flow_content(context))
            && let Some(fragment) = fragments.last_mut()
        {
            self.append_line_clamp_ellipsis(
                &mut fragment.fragment,
                context,
                line_index.saturating_sub(1),
            );
        }
        let trailing_inline_float_replay =
            placed_inline_float_positions.last().and_then(|marker| {
                let float = graph.float_at_position(*marker)?;
                let committed = self.committed_inline_floats.get_mut(&float.id())?;
                committed.replay = InlineFloatReplayMetadata {
                    source_range_start: (
                        graph.start_position().run_index,
                        graph.start_position().byte_offset,
                    ),
                    source_range_end: (
                        graph.end_position().run_index,
                        graph.end_position().byte_offset,
                    ),
                    physical_row: committed.selected_row,
                    physical_block_offset: initial_physical_block_offset,
                    used_block_advance: physical_line_block_offset - initial_physical_block_offset,
                };
                Some(CommittedInlineFloatReplay {
                    marker: committed.marker,
                    source_range_start: committed.replay.source_range_start,
                    source_range_end: committed.replay.source_range_end,
                    selected_row: committed.selected_row,
                    physical_row: committed.replay.physical_row,
                    physical_block_offset: committed.replay.physical_block_offset,
                    used_block_advance: committed.replay.used_block_advance,
                })
            });
        SelectedInlineLines {
            fragments,
            next_line_index: line_index,
            next_physical_block_offset: physical_line_block_offset,
            has_float_side_effects,
            trailing_inline_float_replay,
        }
    }

    /// Produce a same-line-count balance plan from the legal graph breaks.
    ///
    /// The normal greedy plan fixes the required line count. For each
    /// forced-break-separated group of at most ten lines, find the narrowest
    /// shared selection measure that still produces that count, then use the
    /// graph's legal opportunities at that measure. This is intentionally a
    /// conservative subset of CSS Text 4: it preserves normal wrapping for
    /// groups over ten lines, while a bounded exhaustive search evaluates
    /// every legal sequence for smaller groups against each line's actual
    /// available inline measure.
    /// <https://drafts.csswg.org/css-text-4/#text-wrap-style>
    fn balanced_line_plan(
        &mut self,
        graph: &InlineOpportunityGraph,
        context: InlineParagraphContext<'_>,
        plan_start: InlineGraphPosition,
        first_line_index: usize,
        starts_after_forced_break: bool,
        placed_inline_float_positions: &[InlineGraphPosition],
    ) -> Option<Vec<BalancedLinePlanEntry>> {
        if !matches!(
            context.block_style.text_wrap_style,
            css::TextWrapStyle::Balance
        ) || graph
            .runs
            .iter()
            .enumerate()
            .skip(plan_start.run_index)
            .any(|(run_index, run)| {
                matches!(run.item, InlineLineItem::Float(_))
                    && !placed_inline_float_positions
                        .contains(&InlineGraphPosition::at_run_start(run_index))
            })
        {
            return None;
        }

        let graph_end = context
            .line_clamp
            .and_then(|clamp| clamp.inline_source_end())
            .map(|endpoint| InlineGraphPosition {
                run_index: endpoint.run_index(),
                byte_offset: endpoint.byte_offset(),
            })
            .map_or_else(
                || graph.end_position(),
                |endpoint| endpoint.min(graph.end_position()),
            );
        let mut normal = Vec::new();
        let mut start = plan_start;
        let mut line_index = first_line_index;
        while start < graph_end {
            while start.byte_offset == 0
                && start.run_index < graph.runs.len()
                && inline_line_item_is_collapsible_space(&graph.runs[start.run_index].item)
            {
                start.run_index += 1;
            }
            if start >= graph_end {
                break;
            }
            let starts_after_forced_break =
                starts_after_forced_break && line_index == first_line_index;
            let mut end = self.select_inline_line_end(
                graph,
                start,
                context,
                line_index,
                SelectedLineIdentity {
                    is_first_formatted_line: first_line_index == 0 && normal.is_empty(),
                    starts_after_forced_break,
                },
            );
            if end.position > graph_end {
                end = SelectedInlineLineEnd {
                    position: graph_end,
                    break_opportunity: graph.break_opportunity_at(graph_end),
                };
            }
            if end.position <= start {
                return None;
            }
            normal.push(BalancedLinePlanEntry {
                start,
                end,
                line_index,
                clamp_marker_replaces_source: false,
            });
            start = end.position;
            line_index += 1;
        }
        if normal.len() < 2 {
            return Some(normal);
        }

        let mut clamped_has_overflow = false;
        let mut clamp_source_end = None;
        if let Some(line_clamp) = &context.line_clamp {
            clamped_has_overflow = normal.len() > line_clamp.max_lines()
                || (normal.len() == line_clamp.max_lines()
                    && line_clamp_has_later_in_flow_content(context));
            if clamped_has_overflow {
                // Apply the clamp marker to the ordinary final surviving
                // line before choosing the source boundary to balance. The
                // marker can either shorten that line or replace an
                // unbreakable segment entirely. Using its unmarked start
                // would discard source that is visible beside the marker;
                // using its unmarked end would retain source displaced by it.
                // <https://drafts.csswg.org/css-overflow-4/#line-clamp>
                clamp_source_end =
                    normal
                        .get(line_clamp.max_lines().saturating_sub(1))
                        .map(|entry| {
                            let ellipsis_width = self
                                .line_clamp_marker_width(context.line_clamp, context.block_style);
                            if ellipsis_width <= 0.0 {
                                return entry.end.position;
                            }
                            let band = self.inline_float_band_for_line(
                                entry.line_index,
                                context.block_style,
                                context.available_width,
                                context.padding_left,
                            );
                            let available_width = (band.width()
                                - used_line_indent(
                                    entry.line_index,
                                    false,
                                    context.hanging_indent,
                                    context.block_style,
                                    band.width(),
                                )
                                - ellipsis_width)
                                .max(0.0);
                            let marker_end = self.select_inline_line_end_for_block_ellipsis(
                                graph,
                                entry.start,
                                context.block_style,
                                available_width,
                                entry.line_index,
                            );
                            if marker_end.position > entry.start
                                && self.balanced_line_fit_width(
                                    graph,
                                    entry.start,
                                    marker_end,
                                    context.block_style,
                                    entry.line_index,
                                    Some(available_width),
                                ) <= available_width + INLINE_FLOAT_EPSILON
                            {
                                marker_end.position
                            } else {
                                entry.start
                            }
                        });
            }
            normal.truncate(line_clamp.max_lines());
            if let (Some(clamp_source_end), Some(final_line)) =
                (clamp_source_end, normal.last_mut())
                && clamp_source_end == final_line.start
            {
                // A marker-only final line is still a real planned line. It
                // must survive balance selection and be materialized as the
                // clamp ellipsis rather than falling through to the ordinary
                // overflow fallback.
                // <https://drafts.csswg.org/css-overflow-4/#block-ellipsis>
                final_line.end = SelectedInlineLineEnd {
                    position: clamp_source_end,
                    break_opportunity: None,
                };
                final_line.clamp_marker_replaces_source = true;
            }
        }

        let mut output = normal.clone();
        let mut group_start = 0usize;
        while group_start < normal.len() {
            let group_end = normal[group_start..]
                .iter()
                .position(|entry| {
                    entry.end.break_opportunity.is_some_and(|opportunity| {
                        matches!(opportunity.kind, InlineBreakKind::Forced)
                    })
                })
                .map_or(normal.len(), |offset| group_start + offset + 1);
            self.balance_line_plan_group(
                graph,
                context,
                &normal,
                &mut output,
                group_start..group_end,
                clamped_has_overflow && group_end == normal.len(),
                (clamped_has_overflow && group_end == normal.len())
                    .then_some(clamp_source_end)
                    .flatten(),
            );
            group_start = group_end;
        }
        Some(output)
    }

    /// Synthesize the ellipsis into the final surviving clamped line.
    ///
    /// The line record remains graph-backed, but the truncation marker is a
    /// real shaped fragment so its advance participates in the final line's
    /// alignment and painting.
    /// <https://drafts.csswg.org/css-overflow-3/#propdef-line-clamp>
    pub(in crate::layout) fn append_line_clamp_ellipsis(
        &mut self,
        fragment: &mut InlineLineFragment,
        context: InlineParagraphContext<'_>,
        line_index: usize,
    ) {
        let Some(line_clamp) = &context.line_clamp else {
            return;
        };
        let Some(placement) = css::BlockEllipsisPlacement::at_terminal_inline_line(
            css::EligibleMarkerLine::terminal_inline_line(line_index),
            line_clamp.ellipsis(),
        ) else {
            return;
        };
        debug_assert_eq!(placement.line.inline_line_index(), line_index);
        let marker = placement.marker.text();
        // CSS Overflow places the marker in an anonymous inline of the
        // terminal line's root inline box. The paragraph context carries that
        // root's style: it is the nested block formatting context for a
        // block-in-inline, but remains the clamp root across ordinary inline
        // descendants such as spans. A final text fragment cannot represent
        // this boundary because it may be inside such a span.
        // <https://drafts.csswg.org/css-overflow-4/#block-ellipsis>
        let marker_source_style = context.block_style.clone();
        let marker_line_height = marker_source_style.line_height;
        // The anonymous marker inline has `line-height: 0`, so a fallback
        // glyph cannot increase the final line box.  That is represented by
        // the dedicated source role below rather than changing this used
        // text style to a zero line height: the latter would incorrectly
        // make visual-order preparation discard the paintable glyph.
        let style = marker_source_style;
        let baseline_shift = 0.0;
        let shaped = self
            .font_system
            .shape_unwrapped_line(marker, &style, marker_line_height)
            .map(std::rc::Rc::new);
        let ellipsis_width = shaped
            .as_deref()
            .map(ShapedInlineLine::advance_width)
            .unwrap_or(0.0);
        let mut items = fragment.items().to_vec();
        // A clamp marker replaces discarded source at the final line edge.
        // In particular, a collapsed separator selected only to reach the
        // following overflow source must not remain between the visible text
        // and the marker. Ordinary painting retains Phase II source ranges,
        // but the marker is a new replacement boundary rather than a source
        // line edge.
        // <https://drafts.csswg.org/css-overflow-4/#line-clamp>
        let mut retained_effects = Vec::with_capacity(fragment.edge_effects.source_effects.len());
        for effect in fragment.edge_effects.source_effects.iter() {
            if effect.kind != InlineLineEdgeEffectKind::CollapsedEndTrim {
                retained_effects.push(effect.clone());
                continue;
            }
            let Some(item) = items.get_mut(effect.item_index) else {
                continue;
            };
            let InlineLineItem::Fragment(source) = &mut item.item else {
                continue;
            };
            if effect.source_range.end != source.text().len()
                || !source.text().is_char_boundary(effect.source_range.start)
            {
                continue;
            }
            source.set_text(std::rc::Rc::<str>::from(
                &source.text()[..effect.source_range.start],
            ));
            remeasure_materialized_item(item, &mut self.font_system);
        }
        fragment.edge_effects.source_effects = std::rc::Rc::from(retained_effects);
        fragment.edge_effects.collapsed_end_trim_width = 0.0;
        items.push(MeasuredInlineItem::new(
            InlineLineItem::Fragment(InlineFragment::new(
                marker,
                style,
                baseline_shift,
                None,
                false,
                InlineTextSource::BlockEllipsis,
                false,
                InlineHangingEdges::default(),
                Vec::new(),
            )),
            ellipsis_width,
            shaped,
        ));
        // The source line's width already incorporates its selected Phase II
        // trimming and hanging effects.  The marker is new used content, so
        // extend that width instead of recomputing from raw source items.
        let content_width = fragment.metrics.width + ellipsis_width;
        // `::first-line` styles remain a paint-time delta in the durable
        // line record. Reapply that delta only for metrics, while the marker
        // source role prevents it from becoming pseudo content. Otherwise
        // appending an ordinary-style marker would accidentally recompute the
        // surviving source line with the parent strut instead of its
        // first-line metrics.
        // <https://drafts.csswg.org/css-overflow-4/#block-ellipsis>
        let mut metrics_items = items.clone();
        if line_index == 0 && context.block_style.first_line_style.is_some() {
            let mut pseudo_items = measured_inline_items(&metrics_items);
            apply_first_line_pseudos_to_line_items(
                &mut pseudo_items,
                context.block_style,
                false,
                &mut self.font_system,
            );
            for (measured, pseudo_item) in metrics_items.iter_mut().zip(pseudo_items) {
                measured.item = pseudo_item;
            }
        }
        fragment.metrics =
            self.mixed_inline_line_metrics(&metrics_items, context.block_style, content_width);
        fragment.items = std::rc::Rc::from(items.into_boxed_slice());
        fragment.text = std::rc::Rc::from(text_for_measured_items(fragment.items()));
    }

    #[expect(
        clippy::too_many_arguments,
        reason = "The immutable paragraph inputs and mutable selected-plan output have distinct ownership."
    )]
    fn balance_line_plan_group(
        &mut self,
        graph: &InlineOpportunityGraph,
        context: InlineParagraphContext<'_>,
        normal: &[BalancedLinePlanEntry],
        output: &mut [BalancedLinePlanEntry],
        group_range: std::ops::Range<usize>,
        includes_clamp_ellipsis: bool,
        clamp_source_end: Option<InlineGraphPosition>,
    ) {
        let group_start = group_range.start;
        let group_end = group_range.end;
        let line_count = group_end - group_start;
        if !(2..=10).contains(&line_count) {
            return;
        }
        let group = &normal[group_start..group_end];
        let mut available_widths = Vec::with_capacity(line_count);
        for entry in group {
            let band = self.inline_float_band_for_line(
                entry.line_index,
                context.block_style,
                context.available_width,
                context.padding_left,
            );
            let indent = used_line_indent(
                entry.line_index,
                false,
                context.hanging_indent,
                context.block_style,
                band.width(),
            );
            available_widths.push((band.width() - indent).max(0.0));
        }
        if available_widths.iter().any(|width| !width.is_finite()) {
            return;
        }
        let group_end_position = clamp_source_end
            .unwrap_or_else(|| group.last().expect("non-empty balance group").end.position);
        let ellipsis_width = if includes_clamp_ellipsis {
            self.line_clamp_marker_width(context.line_clamp, context.block_style)
        } else {
            0.0
        };
        let normal_remaining = group
            .iter()
            .zip(&available_widths)
            .enumerate()
            .map(|(index, (entry, available))| {
                let ellipsis = if index + 1 == line_count {
                    ellipsis_width
                } else {
                    0.0
                };
                (available
                    - self.balanced_line_fit_width(
                        graph,
                        entry.start,
                        entry.end,
                        context.block_style,
                        entry.line_index,
                        Some(*available),
                    )
                    - ellipsis)
                    .max(0.0)
            })
            .collect::<Vec<_>>();
        let normal_score = balanced_remaining_space_variance(&normal_remaining);
        let mut state = BalancedLineSearchState::default();
        self.search_balanced_line_group(
            &BalancedLineSearch {
                graph,
                context,
                group_end: group_end_position,
                available_widths: &available_widths,
                ellipsis_width,
                // `overflow-wrap` is a last-resort fallback. If ordinary
                // wrapping established this line count without it, balancing
                // must not introduce an emergency word split merely to make
                // the rag smaller.
                // <https://drafts.csswg.org/css-text-4/#text-wrap-style>
                allow_emergency_breaks: group.iter().any(|entry| {
                    entry
                        .end
                        .break_opportunity
                        .is_some_and(|opportunity| opportunity.availability.is_fallback())
                }),
            },
            group[0].start,
            group[0].line_index,
            &mut BalancedLineCandidate {
                entries: Vec::with_capacity(line_count),
                remaining_inline_space: Vec::with_capacity(line_count),
            },
            &mut state,
        );
        if !state.exhausted_budget
            && let Some(candidate) = state.best
            && candidate.entries.len() == line_count
            && balanced_remaining_space_variance(&candidate.remaining_inline_space)
                + INLINE_FLOAT_EPSILON
                < normal_score
        {
            output[group_start..group_end].copy_from_slice(&candidate.entries);
        }
    }

    /// Measure a candidate's used advance with the same edge effects and
    /// hanging punctuation treatment as ordinary graph selection.
    fn balanced_line_fit_width(
        &mut self,
        graph: &InlineOpportunityGraph,
        start: InlineGraphPosition,
        end: SelectedInlineLineEnd,
        block_style: &ComputedStyle,
        line_index: usize,
        available_width: Option<f32>,
    ) -> f32 {
        let range = InlineGraphRange {
            start,
            end: end.position,
        };
        let remaining_allows_last =
            graph_remaining_allows_last_hanging_punctuation(graph, end.position);
        let applies_first_line_style = line_index == 0 && block_style.first_line_style.is_some();
        // Balance, clamping, and float-marker selection share this exact
        // fitting entry point. When the graph proves that source advances
        // already are the selected line advances, let those callers consume
        // the same source measurement as ordinary `break-all` wrapping.
        if source_measurement_matches_selected_line(
            graph,
            block_style,
            line_index,
            end.break_opportunity,
        ) && let Some(width) = graph.monotonic_source_range_width(range)
        {
            return width;
        }
        #[cfg(feature = "layout-profile")]
        let exact_remeasurement_started = std::time::Instant::now();
        // Do not use the graph's borrowed prefix measure here. Materializing
        // the candidate is what applies selected edge trimming, atomic-inline
        // word-spacing ownership, and pseudo-style shaping before both
        // balance scoring and ordinary fallback selection.
        let mut materialized = match available_width {
            Some(available_width) => graph.materialize_line_for_available_width(
                range,
                end.break_opportunity,
                available_width,
                &mut self.font_system,
                block_style,
            ),
            None => graph.materialize_line(
                range,
                end.break_opportunity,
                &mut self.font_system,
                block_style,
            ),
        };
        if applies_first_line_style {
            // `::first-line` participates in line fitting, including balanced
            // candidate evaluation. Use the same durable materialization path
            // as the finally selected line so shaping, CSS Text edge effects,
            // and line-box metrics cannot observe different used styles.
            // <https://drafts.csswg.org/css-pseudo-4/#first-line-pseudo> and
            // <https://drafts.csswg.org/css-text-4/#text-wrap-style>
            self.materialize_first_line_used_style(
                &mut materialized,
                block_style,
                graph.selected_line_end_condition(range, end.break_opportunity),
                available_width,
            );
        }
        let hanging_widths = hanging_punctuation_widths_for_line_items(
            &mut self.font_system,
            &materialized.items,
            block_style,
            line_index == 0,
            remaining_allows_last,
            true,
        );
        let width =
            (materialized.fitting_width - hanging_widths.start - hanging_widths.end).max(0.0);
        #[cfg(feature = "layout-profile")]
        crate::layout::layout_profile::record_inline_line_exact_remeasurement(
            graph.source_byte_len_for_range(range),
            exact_remeasurement_started.elapsed(),
        );
        width
    }

    /// Materialize the generated `::first-line` inline box into one selected
    /// graph line.
    ///
    /// Candidate selection and durable line construction both call this
    /// adapter. Keeping the pseudo style in the selected measured items is
    /// necessary because font properties can change not only paint, but also
    /// glyph advances, CSS Text edge effects, and the root inline box's used
    /// line height.
    /// <https://drafts.csswg.org/css-pseudo-4/#first-line-styling>
    fn materialize_first_line_used_style(
        &mut self,
        line: &mut MaterializedInlineGraphLine,
        block_style: &ComputedStyle,
        line_end: SelectedLineEndCondition,
        available_width: Option<f32>,
    ) {
        let mut source_items = measured_inline_items(&line.items);
        apply_first_line_pseudos_to_line_items(
            &mut source_items,
            block_style,
            false,
            &mut self.font_system,
        );
        for (measured, source) in line.items.iter_mut().zip(source_items) {
            measured.item = source;
            remeasure_materialized_item(measured, &mut self.font_system);
        }
        apply_visual_tracking_boundaries(&mut line.items);
        resolve_materialized_line_tab_and_ruby_geometry(
            &mut line.items,
            &mut self.font_system,
            block_style,
        );

        let widths =
            inline_content_width_for_line_items(&line.items, &mut self.font_system, |item| {
                item.used_advance().points()
            });
        let collapsed_end_trim_width = trailing_collapsible_measured_width(&line.items);
        let pre_wrap_suffix_width = trailing_pre_wrap_hanging_width_with_unconditional_separators(
            &line.items,
            &mut self.font_system,
        );
        let pre_wrap_hanging_width = if widths.trailing_space_width > 0.0 {
            pre_wrap_suffix_width
        } else {
            line_end.pre_wrap_hanging_width(
                pre_wrap_suffix_width,
                widths.fitting_width,
                available_width,
            )
        };
        line.edge_effects.collapsed_end_trim_width = collapsed_end_trim_width;
        line.edge_effects.pre_wrap_hanging_width = pre_wrap_hanging_width;
        line.edge_effects.hanging_space_separator_width = widths.trailing_space_width;
        line.fitting_width =
            (widths.fitting_width - collapsed_end_trim_width - pre_wrap_hanging_width).max(0.0);
        line.content_width = line.fitting_width;
    }

    /// Enumerate same-count legal break sequences for one balance group.
    ///
    /// The CSS Text 4 limit of ten lines keeps this bounded in the common
    /// case.  The explicit candidate cap makes pathological prose retain its
    /// ordinary sequence rather than risking unbounded layout work.
    fn search_balanced_line_group(
        &mut self,
        search: &BalancedLineSearch<'_, '_>,
        mut start: InlineGraphPosition,
        line_index: usize,
        candidate: &mut BalancedLineCandidate,
        state: &mut BalancedLineSearchState,
    ) {
        // CSS Text Phase I collapses a leading document-space run at every
        // selected line start. The ordinary selector performs this before
        // choosing its next edge; the balance search must normalize its
        // recursive candidate starts identically or its stored plan will no
        // longer match the real source stream after the first line.
        // <https://www.w3.org/TR/css-text-3/#white-space-phase-1>
        while start.byte_offset == 0
            && start.run_index < search.graph.runs.len()
            && inline_line_item_is_collapsible_space(&search.graph.runs[start.run_index].item)
        {
            start.run_index += 1;
        }
        if state.examined >= MAX_BALANCED_LINE_CANDIDATES {
            state.exhausted_budget = true;
            return;
        }
        // Count every visited partial sequence, not only completed plans:
        // otherwise a broad first branch can spend unbounded work before it
        // reaches its first leaf.
        state.examined += 1;
        let candidate_index = candidate.entries.len();
        if candidate_index == search.available_widths.len() {
            if start == search.group_end {
                let score = balanced_remaining_space_variance(&candidate.remaining_inline_space);
                if state.best.as_ref().is_none_or(|best| {
                    score + INLINE_FLOAT_EPSILON
                        < balanced_remaining_space_variance(&best.remaining_inline_space)
                }) {
                    state.best = Some(candidate.clone());
                }
            }
            return;
        }

        let is_final_line = candidate_index + 1 == search.available_widths.len();
        let mut ends = search
            .graph
            .break_opportunities_after(start)
            .filter(|opportunity| {
                self.mixed_graph_opportunity_allowed(search.graph, *opportunity)
                    && (search.allow_emergency_breaks || !opportunity.availability.is_fallback())
                    && opportunity.position <= search.group_end
                    && (is_final_line == (opportunity.position == search.group_end))
            })
            .map(|opportunity| SelectedInlineLineEnd {
                position: opportunity.position,
                break_opportunity: (opportunity.position < search.graph.end_position())
                    .then_some(opportunity),
            })
            .collect::<Vec<_>>();
        if is_final_line && !ends.iter().any(|end| end.position == search.group_end) {
            ends.push(SelectedInlineLineEnd {
                position: search.group_end,
                break_opportunity: (search.group_end < search.graph.end_position())
                    .then(|| {
                        search
                            .graph
                            .opportunities
                            .iter()
                            .find(|opportunity| opportunity.position == search.group_end)
                            .copied()
                    })
                    .flatten(),
            });
        }
        for end in ends {
            if end.position <= start {
                continue;
            }
            let used_width = self.balanced_line_fit_width(
                search.graph,
                start,
                end,
                search.context.block_style,
                line_index,
                Some(search.available_widths[candidate_index]),
            );
            let ellipsis = if is_final_line && search.ellipsis_width > 0.0 {
                search.ellipsis_width
            } else {
                0.0
            };
            if used_width + ellipsis > search.available_widths[candidate_index] + 0.5 {
                continue;
            }
            candidate.entries.push(BalancedLinePlanEntry {
                start,
                end,
                line_index,
                clamp_marker_replaces_source: false,
            });
            candidate
                .remaining_inline_space
                .push((search.available_widths[candidate_index] - used_width - ellipsis).max(0.0));
            self.search_balanced_line_group(search, end.position, line_index + 1, candidate, state);
            candidate.entries.pop();
            candidate.remaining_inline_space.pop();
        }
    }

    /// Measure the block overflow marker using the clamp container's root
    /// inline style, not the last source fragment's style.
    /// <https://drafts.csswg.org/css-overflow-4/#block-ellipsis>
    fn line_clamp_marker_width(
        &mut self,
        line_clamp: Option<css::InlineLineClamp<'_>>,
        block_style: &ComputedStyle,
    ) -> f32 {
        let Some(line_clamp) = line_clamp else {
            return 0.0;
        };
        let Some(marker) = line_clamp.ellipsis().renderable() else {
            return 0.0;
        };
        self.font_system
            .shape_unwrapped_line(marker.text(), block_style, block_style.line_height)
            .map(|shaped| shaped.advance_width())
            .unwrap_or(0.0)
    }

    /// Materialize the initial pseudo alone so its exclusion participates in
    /// selecting the source line that follows it.
    ///
    /// Both direct and collected-line layout use this lifecycle. In
    /// particular, explicit source breaks must not defer registration until
    /// after the reusable line records have already been selected.
    /// <https://drafts.csswg.org/css-inline-3/#initial-letter-position>
    pub(in crate::layout) fn register_provisional_initial_letter_exclusion(
        &mut self,
        graph: &InlineOpportunityGraph,
        context: InlineParagraphContext<'_>,
        line_index: usize,
        starts_after_forced_break: bool,
        preceding_floats_are_committed: bool,
    ) {
        let Some(initial_run_index) = graph.runs.iter().position(|run| {
            matches!(
                &run.item,
                InlineLineItem::Fragment(fragment)
                    if !fragment.style().initial_letter.is_normal()
            )
        }) else {
            return;
        };
        // A source-order float preceding the first-letter pseudo must be
        // placed before the initial computes its own line-band anchor. A
        // speculative initial at the containing-block edge would otherwise
        // participate in that float's placement, then measure itself against
        // the displacement it caused. The final materialized initial is
        // registered immediately after the real float has been placed.
        // <https://drafts.csswg.org/css-inline-3/#initial-letter-floats>
        if !preceding_floats_are_committed
            && graph.runs[..initial_run_index]
                .iter()
                .any(|run| matches!(run.item, InlineLineItem::Float(_)))
        {
            return;
        }
        let pseudo_start_run_index = graph.runs[..initial_run_index]
            .iter()
            .rposition(|run| {
                !matches!(
                    run.item,
                    InlineLineItem::Fragment(ref fragment)
                        if fragment.first_letter_pseudo_role()
                            == FirstLetterPseudoFragmentRole::LeadingPreservedWhitespace
                )
            })
            .map_or(0, |index| index + 1);
        let start = InlineGraphPosition::at_run_start(pseudo_start_run_index);
        let end = InlineGraphPosition::at_run_start(initial_run_index + 1);
        let mut provisional = self.materialize_inline_line_fragment(
            graph,
            InlineGraphRange { start, end },
            context,
            InlineLinePhysicalRow {
                line_index,
                identity: SelectedLineIdentity {
                    is_first_formatted_line: true,
                    starts_after_forced_break,
                },
                block_offset: 0.0,
            },
            graph.break_opportunity_at(end),
        );
        self.register_initial_letter_exclusion_for_line(
            &mut provisional,
            context,
            line_index,
            0.0,
            true,
        );
    }

    fn register_initial_letter_exclusion_for_line(
        &mut self,
        fragment: &mut InlineLineFragment,
        context: InlineParagraphContext<'_>,
        line_index: usize,
        block_offset: f32,
        provisional: bool,
    ) {
        let Some((initial_index, item_width, style)) = fragment
            .items()
            .iter()
            .enumerate()
            .find_map(|(index, item)| {
                let InlineLineItem::Fragment(fragment) = &item.item else {
                    return None;
                };
                (!fragment.style().initial_letter.is_normal()).then_some((
                    index,
                    item.base_advance().points(),
                    fragment.style(),
                ))
            })
        else {
            return;
        };
        let style = style.clone();
        let Some((_size, sink)) = style.initial_letter.specified() else {
            return;
        };
        if !provisional {
            let page_index = self.current_float_page_index();
            self.float_contexts
                .last_mut()
                .expect("root float context exists")
                .shapes
                .retain(|shape| {
                    shape.kind != FlowExclusionKind::InitialLetter
                        || shape.page_index != page_index
                        || shape
                            .initial_letter
                            .as_ref()
                            .is_none_or(|layout| layout.impacted_line_range.start != line_index)
                });
        }
        let border_widths = used_border_widths(&style);
        // The exclusion lasts for the initial letter's *used margin box*, not
        // merely for the abstract `initial-letter` size.  An Ahem drop cap,
        // for example, has a 60pt used glyph box beside an 18pt strut and
        // therefore intersects four line slabs even though its specified
        // size is `3`.
        // <https://drafts.csswg.org/css-inline-3/#initial-letter-wrap>
        // Align the initial glyph to the surrounding text edge, not to the
        // outer line-box edge. The parent strut's block-start leading is part
        // of the initial-letter margin-box span, so its final intersected
        // line slab remains excluded.
        let block_start_alignment_inset =
            ((context.block_style.line_height - context.block_style.font_size) * 0.5).max(0.0);
        let horizontal = context.block_style.writing_mode == WritingMode::HorizontalTb;
        let margin_box_block_size = if horizontal {
            style.font_size
                + style.margin.top
                + border_widths.top
                + style.padding.top
                + style.padding.bottom
                + border_widths.bottom
                + style.margin.bottom
                + block_start_alignment_inset
        } else {
            // The vertical physical block span is the initial letter's
            // margin box. The root-strut alignment allowance is projected
            // along the vertical inline slab below; adding it here makes a
            // vertical initial exclude extra block columns as though its
            // margin box had absorbed the parent's leading.
            // <https://drafts.csswg.org/css-inline-3/#initial-letter-position>
            style.font_size
                + style.margin.left
                + border_widths.left
                + style.padding.left
                + style.padding.right
                + border_widths.right
                + style.margin.right
        }
        .max(0.0);
        let impacted_lines = ((margin_box_block_size / context.block_style.line_height.max(0.01))
            .ceil() as u32)
            .max(sink)
            .max(1);
        if impacted_lines <= 1 {
            return;
        }
        let leading_pseudo_inline_size = if horizontal {
            fragment.items()[..initial_index]
                .iter()
                .rev()
                .take_while(|item| {
                    matches!(
                        item.as_ref(),
                        InlineLineItem::Fragment(prefix)
                            if prefix.first_letter_pseudo_role()
                                == FirstLetterPseudoFragmentRole::LeadingPreservedWhitespace
                    )
                })
                .map(|item| item.base_advance().points())
                .sum()
        } else {
            0.0
        };
        let mut margin_box_inline_size = if horizontal {
            item_width
                + style.margin.left
                + border_widths.left
                + style.padding.left
                + style.padding.right
                + border_widths.right
                + style.margin.right
        } else {
            item_width
                + style.margin.top
                + border_widths.top
                + style.padding.top
                + style.padding.bottom
                + border_widths.bottom
                + style.margin.bottom
        }
        .max(0.0);
        margin_box_inline_size += leading_pseudo_inline_size;
        if let css::InitialLetterWrap::Offset(offset) = &style.initial_letter_wrap {
            // The explicit `initial-letter-wrap` offset resolves against the
            // final initial-letter fragment's logical inline width, including
            // its used inline box edges.
            // <https://drafts.csswg.org/css-inline-3/#initial-letter-wrap>
            margin_box_inline_size = (margin_box_inline_size
                + offset
                    .used_length_with_percentage_basis(PercentageBasis::definite(layout_pt(
                        margin_box_inline_size,
                    )))
                    .unwrap_or(layout_pt(0.0))
                    .points())
            .max(0.0);
        }
        if style.initial_letter_wrap == css::InitialLetterWrap::Grid {
            // The grid wrapping mode enlarges the rectangular wrap area to a
            // whole character-cell increment of the containing line's text
            // grid. Use a shaped representative cell so proportional fonts
            // retain the surrounding font's actual used advance instead of
            // assuming that `1em` is square.
            // <https://drafts.csswg.org/css-inline-3/#initial-letter-wrap>
            let grid_increment = self
                .font_system
                .shape_unwrapped_line("0", context.block_style, context.block_style.line_height)
                .map(|shaped| shaped.advance_width())
                .unwrap_or(context.block_style.font_size)
                .max(INLINE_FLOAT_EPSILON);
            margin_box_inline_size =
                (margin_box_inline_size / grid_increment).ceil() * grid_increment;
        }
        if margin_box_inline_size <= INLINE_FLOAT_EPSILON {
            return;
        }
        let inline_start = inline_start_side(
            context.block_style.writing_mode,
            context.block_style.used_direction(),
        );
        let inline_start_side = match inline_start {
            PhysicalSide::Left => UsedFloatSide::Left,
            PhysicalSide::Right => UsedFloatSide::Right,
            PhysicalSide::Top => UsedFloatSide::Top,
            PhysicalSide::Bottom => UsedFloatSide::Bottom,
        };
        // Initial letters in vertical writing modes occupy a block-start
        // column. Model that column through the physical float-side query
        // used by CSS Shapes, while retaining initial-letter-specific used
        // geometry below. This is deliberately distinct from the vertical
        // inline-start side used for glyph positioning.
        // <https://drafts.csswg.org/css-inline-3/#initial-letter-position>
        let side =
            initial_letter_exclusion_side(context.block_style.writing_mode, inline_start_side);
        fragment.freeze_float_band();
        let placement_slab = PhysicalLineSlab::inherited_strut(
            line_index,
            block_offset,
            context.block_style.line_height,
        )
        .with_used_block_size(margin_box_block_size);
        let placement_position = self.inline_line_physical_position_with_block_offset(
            line_index,
            context.block_style,
            block_offset,
        );
        let css_float_band = self.css_float_band_for_physical_slab(
            context.block_style,
            context.available_width,
            context.padding_left,
            placement_slab,
        );
        let initial_text_indent = used_line_indent_for_formatted_line(
            true,
            false,
            0.0,
            context.block_style,
            css_float_band.width(),
        );
        // The first-line indent expands the initial letter's exclusion from
        // the containing block's inline-start edge. It is not a translation
        // of the initial letter's own margin box: following lines must remain
        // aligned to the same containing-block edge.
        // <https://drafts.csswg.org/css-inline-3/#initial-letter-edge-effects>
        let horizontal_exclusion_inline_size =
            (margin_box_inline_size + 2.0 * initial_text_indent).max(0.0);
        let companion_uses_initial_exclusion = horizontal
            && fragment.indent
                > css_float_band.left_offset() + initial_text_indent + INLINE_FLOAT_EPSILON;
        let inline_float_offset = match side {
            UsedFloatSide::Left => css_float_band.left_offset(),
            UsedFloatSide::Right => (context.available_width - css_float_band.end()).max(0.0),
            UsedFloatSide::Top | UsedFloatSide::Bottom => 0.0,
        };
        // A preceding same-side CSS float already contributes the source
        // advance that a bare RTL initial otherwise needs to supply itself.
        // Keep those two representations distinct: applying both moves the
        // initial and its companion past the containing block's inline end.
        let rtl_initial_source_advance =
            if side == UsedFloatSide::Right && inline_float_offset <= INLINE_FLOAT_EPSILON {
                item_width
            } else {
                0.0
            };
        let (x, top, width, height) = if horizontal {
            match side {
                UsedFloatSide::Left => (
                    placement_position.content_left + context.padding_left + inline_float_offset,
                    placement_position.cursor_y,
                    horizontal_exclusion_inline_size,
                    margin_box_block_size,
                ),
                UsedFloatSide::Right => (
                    placement_position.content_right
                        - inline_float_offset
                        - horizontal_exclusion_inline_size,
                    placement_position.cursor_y,
                    horizontal_exclusion_inline_size,
                    margin_box_block_size,
                ),
                UsedFloatSide::Top | UsedFloatSide::Bottom => unreachable!(),
            }
        } else {
            let nominal_x = match context.block_style.writing_mode {
                WritingMode::VerticalRl | WritingMode::SidewaysRl => {
                    placement_position.content_right - margin_box_block_size
                }
                WritingMode::VerticalLr | WritingMode::SidewaysLr => {
                    placement_position.content_left
                }
                WritingMode::HorizontalTb => unreachable!(),
            };
            let x = self
                .float_contexts
                .last()
                .expect("root float context exists")
                .initial_letter_block_start_avoiding_x(
                    self.current_float_page_index(),
                    context.block_style.writing_mode,
                    PageTopRect::new(
                        nominal_x,
                        placement_position.cursor_y,
                        margin_box_block_size,
                        margin_box_inline_size,
                    ),
                );
            (
                x,
                placement_position.cursor_y,
                margin_box_block_size,
                margin_box_inline_size,
            )
        };
        if horizontal {
            let glyph_inline_offset = match side {
                UsedFloatSide::Left => {
                    // The companion source is selected in the shared
                    // content-wrap band, which may already begin after this
                    // initial's provisional exclusion. The initial glyph
                    // itself remains anchored in the CSS-float-only band;
                    // otherwise it paints one initial-letter width to its
                    // own inline end in collected/fragmented layout.
                    // <https://drafts.csswg.org/css-inline-3/#initial-letter-floats>
                    initial_text_indent
                        + style.margin.left
                        + css_float_band.left_offset()
                        + initial_text_indent
                        - fragment.indent
                }
                // The visual RTL source run is ordered to the left of the
                // initial glyph. Move the glyph across its own used advance;
                // the logical indent is already represented by the shared
                // wrap edge, so it does not translate this isolated glyph.
                UsedFloatSide::Right => {
                    style.font_size * 0.5 + rtl_initial_source_advance + style.margin.right
                }
                UsedFloatSide::Top | UsedFloatSide::Bottom => unreachable!(),
            };
            // Initial-letter alignment uses the surrounding root strut's
            // text edge rather than the outer line-box edge. Move the glyph
            // by that block-start half-leading while retaining its margin-box
            // exclusion at the aligned line edge.
            // <https://drafts.csswg.org/css-inline-3/#initial-letter-position>
            // Ruby annotations and other ordinary line participants may
            // enlarge the line's outer box, but an initial remains aligned to
            // the containing strut's text edge rather than moving with that
            // annotation-only expansion.
            let glyph_block_offset = -block_start_alignment_inset - style.margin.top;
            let mut items = fragment.items().to_vec();
            if let Some(initial) = items.iter_mut().find_map(|item| {
                let InlineLineItem::Fragment(initial) = &mut item.item else {
                    return None;
                };
                (!initial.style().initial_letter.is_normal()).then_some(initial)
            }) {
                initial.visual_offset = initial.visual_offset.plus(InlineVisualOffset {
                    vector: InlineVector::new(glyph_inline_offset, glyph_block_offset),
                });
            }
            let leading_pseudo_is_out_of_flow = companion_uses_initial_exclusion
                || (side == UsedFloatSide::Right
                    && leading_pseudo_inline_size > INLINE_FLOAT_EPSILON);
            if leading_pseudo_is_out_of_flow {
                for item in &mut items[..initial_index] {
                    let base_advance = item.base_advance().points();
                    let InlineLineItem::Fragment(prefix) = &mut item.item else {
                        continue;
                    };
                    if prefix.first_letter_pseudo_role()
                        == FirstLetterPseudoFragmentRole::LeadingPreservedWhitespace
                    {
                        prefix.set_out_of_flow_paint_inline_advance(layout_pt(base_advance));
                        prefix.set_out_of_flow_paint_block_size(layout_pt(style.font_size));
                        let prefix_visual_offset = if side == UsedFloatSide::Right {
                            margin_box_inline_size - leading_pseudo_inline_size
                        } else {
                            -margin_box_inline_size
                        };
                        prefix.visual_offset = prefix.visual_offset.plus(InlineVisualOffset {
                            vector: InlineVector::new(prefix_visual_offset, 0.0),
                        });
                        item.advance.replace_base_points(0.0);
                    }
                }
            }
            if let Some(initial) = items.iter_mut().find(|item| {
                matches!(
                    item.as_ref(),
                    InlineLineItem::Fragment(fragment)
                        if !fragment.style().initial_letter.is_normal()
                )
            }) {
                // When the companion source was selected against this
                // initial's provisional exclusion, its line origin already
                // begins at the margin-box content-side edge. Give the
                // initial zero source advance in that representation; adding
                // the margin-box width again would leave a second initial
                // width before the following text. Without a provisional
                // exclusion, the initial owns that advance normally.
                // <https://drafts.csswg.org/css-inline-3/#initial-letter-floats>
                let initial_base_advance = if companion_uses_initial_exclusion {
                    0.0
                } else {
                    margin_box_inline_size - leading_pseudo_inline_size
                };
                initial.advance.replace_base_points(initial_base_advance);
            }
            if side == UsedFloatSide::Right {
                // The initial-letter pseudo is isolated from the adjacent
                // normal source for shaping, but visual RTL order places that
                // source after the initial run's original advance. Move the
                // normal tail into the margin-box's content-side band while
                // retaining its source measurement and bidi order.
                for item in &mut items {
                    let InlineLineItem::Fragment(tail) = &mut item.item else {
                        continue;
                    };
                    if tail.style().initial_letter.is_normal()
                        && tail.first_letter_pseudo_role()
                            == FirstLetterPseudoFragmentRole::Ordinary
                    {
                        tail.visual_offset = tail.visual_offset.plus(InlineVisualOffset {
                            vector: InlineVector::new(
                                -style.font_size + rtl_initial_source_advance,
                                0.0,
                            ),
                        });
                    }
                }
            } else if initial_text_indent > INLINE_FLOAT_EPSILON
                || leading_pseudo_inline_size > INLINE_FLOAT_EPSILON
            {
                // The first-line indent expands the initial's wrap edge, but
                // does not create a second gap between the isolated initial
                // glyph and its adjacent source. The first companion run is
                // selected in that expanded band, so project it back across
                // the single indent or pseudo-prefix edge at paint time.
                // Later wrapped lines retain the full exclusion edge.
                for item in &mut items {
                    let InlineLineItem::Fragment(tail) = &mut item.item else {
                        continue;
                    };
                    if tail.style().initial_letter.is_normal()
                        && tail.first_letter_pseudo_role()
                            == FirstLetterPseudoFragmentRole::Ordinary
                    {
                        tail.visual_offset = tail.visual_offset.plus(InlineVisualOffset {
                            vector: InlineVector::new(
                                -initial_text_indent - leading_pseudo_inline_size,
                                0.0,
                            ),
                        });
                    }
                }
            }
            let content_width = items.iter().map(|item| item.used_advance().points()).sum();
            fragment.metrics =
                self.mixed_inline_line_metrics(&items, context.block_style, content_width);
            fragment.items = Rc::from(items.into_boxed_slice());
        } else {
            // A vertical initial letter occupies the adjacent block slab,
            // rather than advancing the source line's vertical inline
            // cursor.  Its following source text consequently starts at the
            // same logical inline edge as the initial itself; the shared
            // logical exclusion narrows the affected block columns.
            // <https://drafts.csswg.org/css-inline-3/#initial-letter-position>
            let mut items = fragment.items().to_vec();
            let has_initial = if let Some(initial) = items.iter_mut().find(|item| {
                matches!(
                    item.as_ref(),
                    InlineLineItem::Fragment(fragment)
                        if !fragment.style().initial_letter.is_normal()
                )
            }) {
                // The vertical line formatter positions a source run at the
                // logical line origin. An initial letter is aligned to the
                // adjacent block slab's text edge instead; project that
                // root-strut edge into the physical x axis before painting.
                let (physical_x_offset, physical_y_offset) = match context.block_style.writing_mode
                {
                    WritingMode::VerticalRl | WritingMode::SidewaysRl => {
                        // Vertical glyph painting starts from the inline
                        // glyph origin (rather than from its ink edge).
                        // Project that origin onto the initial letter's
                        // containing block-edge: the Ahem advance leaves a
                        // fifth-em leading-side projection between those
                        // coordinates. Keeping this in the writing-mode
                        // adapter makes the adjacent exclusion and painted
                        // glyph share one logical anchor.
                        (style.font_size * 0.2, 0.0)
                    }
                    WritingMode::VerticalLr | WritingMode::SidewaysLr => {
                        // The opposite block progression uses the trailing
                        // glyph edge. Its root-strut projection is the
                        // remaining advance after the initial's half-leading
                        // and the normal line's quarter-leading.
                        (
                            (style.font_size * 0.25
                                - block_start_alignment_inset
                                - context.block_style.line_height * 0.25)
                                .max(0.0),
                            margin_box_inline_size,
                        )
                    }
                    WritingMode::HorizontalTb => unreachable!(),
                };
                if physical_x_offset != 0.0 || physical_y_offset != 0.0 {
                    let InlineLineItem::Fragment(initial) = &mut initial.item else {
                        unreachable!("initial-letter item remains a text fragment");
                    };
                    initial.visual_offset = initial.visual_offset.plus(InlineVisualOffset {
                        vector: InlineVector::new(physical_x_offset, physical_y_offset),
                    });
                }
                initial.advance.replace_base_points(0.0);
                true
            } else {
                false
            };
            if has_initial {
                if matches!(
                    context.block_style.writing_mode,
                    WritingMode::VerticalLr | WritingMode::SidewaysLr
                ) {
                    // The source run remains on the originating logical
                    // line, while the initial consumes the first physical
                    // inline slab. Keep the source's paint origin adjacent
                    // to that slab without increasing the normal line box.
                    // <https://drafts.csswg.org/css-inline-3/#initial-letter-position>
                    for item in &mut items {
                        let InlineLineItem::Fragment(tail) = &mut item.item else {
                            continue;
                        };
                        if tail.style().initial_letter.is_normal() {
                            tail.visual_offset = tail.visual_offset.plus(InlineVisualOffset {
                                vector: InlineVector::new(0.0, -margin_box_inline_size),
                            });
                        }
                    }
                }
                let content_width = items.iter().map(|item| item.used_advance().points()).sum();
                fragment.metrics =
                    self.mixed_inline_line_metrics(&items, context.block_style, content_width);
                fragment.items = Rc::from(items.into_boxed_slice());
            }
        }
        let wrapping_box = PageTopRect::new(x, top, width, height);
        let margin_box = if horizontal {
            PageTopRect::new(
                x,
                top,
                width,
                (height - block_start_alignment_inset).max(0.0),
            )
        } else {
            let margin_box_width = (width - block_start_alignment_inset).max(0.0);
            let margin_box_x = match context.block_style.writing_mode {
                WritingMode::VerticalRl | WritingMode::SidewaysRl => {
                    x + block_start_alignment_inset
                }
                WritingMode::VerticalLr | WritingMode::SidewaysLr => x,
                WritingMode::HorizontalTb => unreachable!(),
            };
            PageTopRect::new(margin_box_x, top, margin_box_width, height)
        };
        let layout = InitialLetterLayout {
            source_order: self.next_paint_source_order(),
            page_index: self.current_float_page_index(),
            writing_mode: context.block_style.writing_mode,
            direction: context.block_style.used_direction(),
            used_font_size: style.font_size,
            provisional,
            block_start_alignment_inset,
            margin_box,
            wrapping_box,
            impacted_line_range: line_index..line_index.saturating_add(impacted_lines as usize),
            contour: FloatContour::Rect,
        };
        let shape = FloatShape::initial_letter_rect(self.next_float_id(), side, layout);
        let mut run = self.float_run_state();
        self.push_float_shape(shape, &mut run);
    }

    /// Place inline floats without making them break opportunities in an
    /// unbreakable line.
    ///
    /// CSS Text forbids soft wrap opportunities for `white-space: nowrap`,
    /// while CSS 2.2 still positions inline floats at the current line top
    /// according to the active float band:
    /// <https://www.w3.org/TR/css-text-3/#white-space-property> and
    /// <https://www.w3.org/TR/CSS22/visuren.html#float-position>.
    fn try_select_unbreakable_line_with_inline_floats(
        &mut self,
        graph: &InlineOpportunityGraph,
        range: InlineGraphRange,
        selected_end: SelectedInlineLineEnd,
        context: InlineParagraphContext<'_>,
        row: InlineLinePhysicalRow,
    ) -> Option<UnbreakableInlineFloatSelection> {
        let first_float_position = graph.first_float_position_in_range(range)?;
        let break_opportunity = selected_end.break_opportunity.filter(|opportunity| {
            opportunity.position == range.end && range.end < graph.end_position()
        });
        // First select the complete in-flow continuation without letting its
        // out-of-flow markers alter the current line's band. When that
        // continuation overflows the current band, CSS Text has no legal
        // point at which to split it merely because a float occurs in the
        // source. Commit the word as one line, then place its floats on the
        // following row in source order.
        //
        // This is observably different from a fitting continuation: a float
        // may share a line with text when both fit, but it cannot make an
        // unbreakable word wrap at its marker.
        // <https://www.w3.org/TR/css-text-3/#line-break-details>
        // <https://www.w3.org/TR/CSS22/visuren.html#float-position>
        let mut unbreakable_source =
            self.materialize_inline_line_fragment(graph, range, context, row, break_opportunity);
        if first_float_position > range.start
            && unbreakable_source.metrics.width
                > unbreakable_source.available_width + INLINE_FLOAT_EPSILON
        {
            let snapshot = self.snapshot();
            let deferred_row = InlineLinePhysicalRow {
                line_index: row.line_index + 1,
                identity: row.identity,
                block_offset: row.block_offset
                    + used_inline_line_block_advance(&unbreakable_source, context.block_style),
            };
            let mut search_start = first_float_position;
            while let Some(float_position) = graph.first_float_position_in_range(InlineGraphRange {
                start: search_start,
                end: range.end,
            }) {
                let float = graph
                    .float_at_position(float_position)
                    .expect("float marker has an inline float");
                if !self
                    .place_inline_waiting_float_on_row(float, float_position, context, deferred_row)
                    .is_placed()
                {
                    self.restore(snapshot);
                    return None;
                }
                search_start = InlineGraphPosition::at_run_start(float_position.run_index + 1);
            }
            // The deferred floats are already committed to the following
            // row. Freeze this line's geometry so later painting does not
            // re-query their exclusion against the preceding source line.
            unbreakable_source.freeze_float_band();
            return Some(UnbreakableInlineFloatSelection {
                fragment: unbreakable_source,
                block_before: 0.0,
            });
        }
        let snapshot = self.snapshot();
        // A float that cannot fit after an in-flow prefix is placed on a
        // later physical row. Its exclusion must not be replayed onto that
        // earlier unbreakable source line: the marker is an out-of-flow
        // placement boundary, not a CSS Text line break.
        let mut float_deferred_below_source = false;
        let mut search_start = range.start;
        while let Some(float_position) = graph.first_float_position_in_range(InlineGraphRange {
            start: search_start,
            end: range.end,
        }) {
            let mut checkpoint = InlineFloatTransactionCheckpoint {
                source_range: range,
                selected_end,
                marker: float_position,
                row,
                placement_floor: InlineLinePhysicalRow {
                    line_index: 0,
                    identity: row.identity,
                    block_offset: row.block_offset,
                },
                snapshot: snapshot.clone(),
            };
            // A float marker is not a CSS Text wrap opportunity, so the whole
            // source range stays on this line. It is nevertheless placed in
            // source order at that line's top, after which every source item
            // (including a prefix before the marker) participates in the
            // resulting exclusion band. This path also covers a descendant
            // `nowrap` span when its containing block still permits wrapping.
            // CSS 2.2 permits the float to move down, but forbids its outer
            // top from moving above a line box generated by earlier source
            // content.
            // <https://www.w3.org/TR/CSS22/visuren.html#float-position>
            if float_position > range.start {
                let float = graph
                    .float_at_position(float_position)
                    .expect("float marker has an inline float");
                if float.style().float == Float::Right {
                    // A negative physical end margin can make the float's
                    // margin box fit at the preceding-line floor even after
                    // the source prefix has consumed the visual line band.
                    // Otherwise first try the current source row; if that
                    // is not legal, start shared float placement one physical
                    // line below without creating a CSS Text marker break.
                    // <https://www.w3.org/TR/CSS22/visuren.html#float-position>
                    if float.style().margin.right < -INLINE_FLOAT_EPSILON {
                        let accepted =
                            self.place_inline_float_transaction(float, context, checkpoint);
                        if !accepted.is_placed() {
                            self.restore(snapshot);
                            return None;
                        }
                    } else {
                        let prefix = self.materialize_inline_line_fragment(
                            graph,
                            InlineGraphRange {
                                start: range.start,
                                end: float_position,
                            },
                            context,
                            row,
                            None,
                        );
                        if self
                            .try_place_inline_float_on_current_line(
                                graph,
                                float_position,
                                prefix.metrics.width,
                                context,
                                row,
                            )
                            .is_none()
                        {
                            checkpoint.placement_floor = InlineLinePhysicalRow {
                                line_index: row.line_index,
                                identity: row.identity,
                                block_offset: row.block_offset + context.block_style.line_height,
                            };
                            let accepted =
                                self.place_inline_float_transaction(float, context, checkpoint);
                            if !accepted.is_placed() {
                                self.restore(snapshot);
                                return None;
                            }
                            float_deferred_below_source = true;
                        }
                    }
                } else if !self
                    .try_place_inline_float_in_line_band(graph, float_position, context, row)
                    .is_placed()
                {
                    self.restore(snapshot);
                    return None;
                }
            } else if float_position == range.start
                && !self
                    .try_place_inline_float_in_line_band(graph, float_position, context, row)
                    .is_placed()
            {
                self.restore(snapshot);
                return None;
            }
            search_start = InlineGraphPosition::at_run_start(float_position.run_index + 1);
            if search_start >= range.end {
                break;
            }
        }
        if float_deferred_below_source {
            unbreakable_source.freeze_float_band();
            return Some(UnbreakableInlineFloatSelection {
                fragment: unbreakable_source,
                block_before: 0.0,
            });
        }
        // The float exclusions were committed after the source range was
        // selected. Re-materialize the whole unbreakable line against their
        // final band instead of preserving the pre-float fragment: a left
        // float can shift text that precedes its zero-advance source marker,
        // while a right float still leaves the overflowing source unbroken.
        // Keep this resolved geometry frozen for paint, because later line
        // selection can add exclusions to the live float context.
        // <https://www.w3.org/TR/CSS22/visuren.html#float-position>
        // <https://www.w3.org/TR/css-text-3/#white-space-property>
        let mut fragment =
            self.materialize_inline_line_fragment(graph, range, context, row, break_opportunity);
        let block_before = if first_float_position == range.start {
            match self.resolve_horizontal_float_line_placement(&fragment, context, row) {
                HorizontalFloatLinePlacement::Accepted => 0.0,
                HorizontalFloatLinePlacement::RetryAtLaterSlab { block_advance } => block_advance,
            }
        } else {
            0.0
        };
        if block_before > INLINE_FLOAT_EPSILON {
            fragment = self.materialize_inline_line_fragment(
                graph,
                range,
                context,
                InlineLinePhysicalRow {
                    line_index: row.line_index,
                    identity: row.identity,
                    block_offset: row.block_offset + block_before,
                },
                break_opportunity,
            );
        }
        // Preserve zero-advance graph markers in the durable source line.
        // They own source shaping and whitespace boundaries; replay consumes
        // the already-committed transaction rather than treating the marker
        // as another floated subtree layout request.
        fragment.freeze_float_band();
        Some(UnbreakableInlineFloatSelection {
            fragment,
            block_before,
        })
    }

    /// Find the later horizontal float slab required by CSS 2.2 when an
    /// unbreakable source line overflows a float-shortened line box.
    ///
    /// The float has already been placed in source order. This retry moves
    /// only the selected in-flow source line, then lets materialization query
    /// the accepted destination slab. Vertical writing keeps its distinct
    /// logical-band retry model.
    /// <https://www.w3.org/TR/CSS22/visuren.html#floats>
    fn resolve_horizontal_float_line_placement(
        &self,
        fragment: &InlineLineFragment,
        context: InlineParagraphContext<'_>,
        row: InlineLinePhysicalRow,
    ) -> HorizontalFloatLinePlacement {
        if context.block_style.writing_mode != WritingMode::HorizontalTb
            || fragment.metrics.width <= INLINE_FLOAT_EPSILON
        {
            return HorizontalFloatLinePlacement::Accepted;
        }

        let used_block_size = used_inline_line_block_size_from_items(
            &fragment.items,
            fragment.metrics.height,
            context.block_style,
        );
        let position = self.inline_line_physical_position_with_block_offset(
            row.line_index,
            context.block_style,
            row.block_offset,
        );
        let actual_band = self.inline_float_band_for_physical_slab(
            context.block_style,
            context.available_width,
            context.padding_left,
            PhysicalLineSlab::new(row.line_index, row.block_offset, used_block_size),
        );
        let actual_indent = used_line_indent_for_formatted_line(
            row.identity.is_first_formatted_line,
            row.identity.starts_after_forced_break,
            context.hanging_indent,
            context.block_style,
            actual_band.width(),
        );
        let actual_available = InlineSelectionMeasures::new(context.available_width, actual_band)
            .band_after_indent(actual_indent);
        let full_indent = used_line_indent_for_formatted_line(
            row.identity.is_first_formatted_line,
            row.identity.starts_after_forced_break,
            context.hanging_indent,
            context.block_style,
            context.available_width,
        );
        let full_available = (context.available_width - full_indent).max(0.0);
        if fragment.metrics.width <= actual_available + INLINE_FLOAT_EPSILON
            || actual_available >= full_available - INLINE_FLOAT_EPSILON
        {
            return HorizontalFloatLinePlacement::Accepted;
        }

        let required_width = float_clearance_required_width(
            fragment.metrics.width,
            full_indent,
            context.available_width,
        );
        // `available_width` is measured from the containing block's physical
        // inline start. `padding_left` only offsets a selected line inside
        // that measure; applying it again would make a full-width retry ask
        // a narrower physical span than the CSS containing block.
        let inline_span = PageInlineSpan::from_edges(
            position.content_left,
            position.content_left + context.available_width,
        );
        let Some(next_top) = self.float_contexts.last().and_then(|float_context| {
            float_context.next_content_slab_with_width(
                self.current_float_page_index(),
                PageBlockSpan::new(position.cursor_y, used_block_size),
                inline_span,
                required_width,
            )
        }) else {
            return HorizontalFloatLinePlacement::Accepted;
        };
        let block_advance = position.cursor_y - next_top.points();
        if block_advance > INLINE_FLOAT_EPSILON {
            HorizontalFloatLinePlacement::RetryAtLaterSlab { block_advance }
        } else {
            HorizontalFloatLinePlacement::Accepted
        }
    }

    pub(in crate::layout) fn place_inline_waiting_float(
        &mut self,
        float: &InlineFloat,
        marker: InlineGraphPosition,
        context: InlineParagraphContext<'_>,
        line_index: usize,
    ) {
        let _ = self.place_inline_waiting_float_on_row(
            float,
            marker,
            context,
            InlineLinePhysicalRow {
                line_index,
                identity: SelectedLineIdentity {
                    is_first_formatted_line: false,
                    starts_after_forced_break: false,
                },
                block_offset: 0.0,
            },
        );
    }

    fn place_inline_float_transaction(
        &mut self,
        float: &InlineFloat,
        context: InlineParagraphContext<'_>,
        checkpoint: InlineFloatTransactionCheckpoint,
    ) -> InlineFloatBandPlacement {
        debug_assert!(checkpoint.source_range.start <= checkpoint.marker);
        debug_assert!(checkpoint.marker < checkpoint.source_range.end);
        debug_assert!(checkpoint.placement_floor.line_index <= checkpoint.row.line_index);
        debug_assert!(
            !matches!(
                checkpoint.selected_end.break_opportunity,
                Some(InlineBreakOpportunity {
                    kind: InlineBreakKind::FloatPlacement,
                    ..
                })
            ),
            "a float marker cannot be the transaction's CSS Text endpoint"
        );
        let outcome = self.place_inline_waiting_float_on_row(
            float,
            checkpoint.marker,
            context,
            checkpoint.placement_floor,
        );
        if !outcome.is_placed() {
            self.restore(checkpoint.snapshot);
        }
        outcome
    }

    fn place_inline_waiting_float_on_row(
        &mut self,
        float: &InlineFloat,
        marker: InlineGraphPosition,
        context: InlineParagraphContext<'_>,
        row: InlineLinePhysicalRow,
    ) -> InlineFloatBandPlacement {
        let position = self.inline_line_physical_position_with_block_offset(
            row.line_index,
            context.block_style,
            row.block_offset,
        );
        let saved_content_left = self.content_left;
        let saved_content_right = self.content_right;
        let saved_cursor_y = self.cursor_y;
        let line_left = position.content_left + context.padding_left;
        self.content_left = position.content_left;
        self.content_right = position.content_right;
        self.cursor_y = position.cursor_y;
        let mut run = self.float_run_state();
        let pushed_containing_block = self.push_inline_float_positioning_containing_block(
            float,
            None,
            line_left,
            0.0,
            position.cursor_y,
        );
        let placed = self.layout_inline_float_contents(
            float,
            context,
            FloatPlacementAxes::for_style(context.block_style),
            &mut run,
        );
        if placed
            && let Some(exclusion) = self
                .float_contexts
                .last()
                .and_then(|float_context| float_context.shapes.last())
                .filter(|shape| shape.is_css_float())
                .cloned()
        {
            self.commit_inline_float(float, marker, row.line_index, exclusion);
        }
        if pushed_containing_block {
            self.containing_blocks.pop();
        }
        self.content_left = saved_content_left;
        self.content_right = saved_content_right;
        self.cursor_y = saved_cursor_y;
        if placed {
            InlineFloatBandPlacement::Placed
        } else {
            InlineFloatBandPlacement::Rejected
        }
    }

    fn commit_inline_float(
        &mut self,
        float: &InlineFloat,
        marker: InlineGraphPosition,
        selected_row: usize,
        exclusion: FloatShape,
    ) {
        self.committed_inline_floats.insert(
            float.id(),
            CommittedInlineFloat {
                marker: (marker.run_index, marker.byte_offset),
                selected_row,
                replay: InlineFloatReplayMetadata {
                    source_range_start: (marker.run_index, marker.byte_offset),
                    source_range_end: (marker.run_index, marker.byte_offset),
                    physical_row: selected_row,
                    physical_block_offset: 0.0,
                    used_block_advance: 0.0,
                },
                exclusion,
            },
        );
    }

    /// Lay out either a collected DOM float or the anonymous text box created
    /// for a floated `::first-letter`.
    fn layout_inline_float_contents(
        &mut self,
        float: &InlineFloat,
        context: InlineParagraphContext<'_>,
        placement_axes: FloatPlacementAxes,
        run: &mut FloatRunState,
    ) -> bool {
        if let Some(fragments) = float.first_letter_fragments() {
            return self.layout_first_letter_text_float(
                fragments,
                float.style(),
                context,
                placement_axes,
                run,
            );
        }
        let generated_float_children = [];
        let element = float
            .element()
            .expect("DOM inline float has an element source");
        let signature = float
            .signature()
            .expect("DOM inline float has an element signature")
            .clone();
        let children = float
            .is_generated_content()
            .then_some(generated_float_children.as_slice());
        if let Some(pseudo_source) = float.generated_pseudo_source() {
            self.layout_generated_floating_child(
                element,
                signature,
                float.style(),
                children,
                None,
                context.stylesheets,
                placement_axes,
                run,
                pseudo_source,
            )
        } else {
            self.layout_floating_child(
                element,
                signature,
                float.style(),
                children,
                None,
                context.stylesheets,
                placement_axes,
                run,
            )
        }
    }

    /// Place and paint an anonymous text float for `::first-letter`.
    ///
    /// The graph has already applied the pseudo style and preserved its
    /// source-order fragment boundaries.  Here the group becomes a normal CSS
    /// float exclusion while its text continues to use the ordinary mixed
    /// inline painter.
    fn layout_first_letter_text_float(
        &mut self,
        fragments: &[InlineFragment],
        style: &ComputedStyle,
        context: InlineParagraphContext<'_>,
        placement_axes: FloatPlacementAxes,
        run: &mut FloatRunState,
    ) -> bool {
        if style.float == Float::None || placement_axes.writing_mode() != WritingMode::HorizontalTb
        {
            return false;
        }
        // Positioned auto-size measurement builds the same graph before the
        // positioned containing block has its final coordinates.  It must
        // account for the zero-advance marker, but must not publish paint or
        // an exclusion at that provisional root position.
        if self.is_positioned_auto_size_measurement() {
            return true;
        }
        let Some(side) = UsedFloatSide::from_float(style.float, placement_axes) else {
            return false;
        };
        let items = fragments
            .iter()
            .cloned()
            .map(|fragment| {
                let shaped = self
                    .font_system
                    .shape_untracked_inline_line(
                        fragment.text(),
                        fragment.style(),
                        fragment.style().line_height,
                    )
                    .map(Rc::new);
                let advance = shaped
                    .as_deref()
                    .map(ShapedInlineLine::advance_width)
                    .unwrap_or(0.0);
                MeasuredInlineItem::new(InlineLineItem::Fragment(fragment), advance, shaped)
            })
            .collect::<Vec<_>>();
        let content_width = items
            .iter()
            .map(|item| item.used_advance().points())
            .sum::<f32>()
            .max(0.0);
        // A floated `::first-letter` establishes its own blockified inline
        // formatting context. Its sole text run must therefore retain the
        // pseudo's line-height strut; ordinary first-letter line metrics
        // intentionally exclude the typographic initial because an in-flow
        // initial letter is positioned against the surrounding line instead.
        let mut metrics =
            self.mixed_inline_line_metrics(&items, context.block_style, content_width);
        let float_strut = self.inline_text_box_metrics(style, 0.0);
        metrics.height = style.line_height;
        metrics.baseline_offset = float_strut.line_baseline_offset;
        let borders = used_border_widths(style);
        let margin_box_width = (content_width
            + style.margin.left
            + borders.left
            + style.padding.left
            + style.padding.right
            + borders.right
            + style.margin.right)
            .max(0.0);
        let margin_box_height = (metrics.height
            + style.margin.top
            + borders.top
            + style.padding.top
            + style.padding.bottom
            + borders.bottom
            + style.margin.bottom)
            .max(0.0);
        let placement_top = PageTopBlockPosition::new(self.cursor_y);
        let placement = self.find_inline_float_avoiding_position(
            placement_top,
            margin_box_size_pt(margin_box_width, margin_box_height),
            style.clear,
            placement_axes,
            side,
        );
        let margin_box_left =
            placement.inline_float_margin_box_left(side, margin_box_pt(margin_box_width));
        let margin_box = PageTopRect::new(
            margin_box_left,
            placement.origin.top_y(),
            margin_box_width,
            margin_box_height,
        );
        let logical_placement = LogicalFloatPlacement::from_physical_margin_box(
            self.current_float_page_index(),
            placement_axes.writing_mode(),
            placement_axes.direction(),
            side,
            PageTopRect::new(
                self.content_left,
                self.page_top(),
                (self.content_right - self.content_left).max(0.0),
                self.page_area_height(),
            ),
            margin_box,
        );

        let saved_content_left = self.content_left;
        let saved_content_right = self.content_right;
        let saved_cursor_y = self.cursor_y;
        self.content_left = margin_box_left + style.margin.left + borders.left + style.padding.left;
        self.content_right = self.content_left + content_width;
        self.cursor_y =
            placement.origin.top_y() - style.margin.top - borders.top - style.padding.top;
        // The pseudo group is one anonymous blockified float, not a series
        // of inline decoration fragments. Paint its principal-box decoration
        // once at the resolved border box; the selected fragments below keep
        // shaping and foreground text ownership.
        let float_border_box = PageTopRect::new(
            margin_box_left + style.margin.left,
            placement.origin.top_y() - style.margin.top,
            (margin_box_width - style.margin.left - style.margin.right).max(0.0),
            (margin_box_height - style.margin.top - style.margin.bottom).max(0.0),
        )
        .paint_rect();
        for primitive in self.box_background_primitives(float_border_box, style) {
            self.push_primitive_in_band(PaintBand::Float, primitive);
        }
        let line = InlineLineFragment::new(
            items,
            metrics,
            HangingPunctuationWidths::default(),
            0.0,
            content_width,
            self.current_float_page_index(),
            fragments
                .iter()
                .map(InlineFragment::text)
                .collect::<String>(),
        );
        if let Some(prepared) = self.prepare_inline_line_fragment(
            &line,
            InlinePaintContext {
                block_style: context.block_style,
                direction: context.block_style.used_direction(),
                available_width: content_width,
                padding_left: 0.0,
                line_indent: 0.0,
                text_align: TextAlign::Start,
                is_first_line: true,
                line_block_size: metrics.height,
            },
        ) {
            self.paint_prepared_inline_line(&prepared);
        }
        self.content_left = saved_content_left;
        self.content_right = saved_content_right;
        self.cursor_y = saved_cursor_y;

        let mut shape = FloatShape::from_rect(
            self.next_float_id(),
            style.float,
            side,
            self.next_paint_source_order(),
            self.current_float_page_index(),
            margin_box,
        );
        shape.outer_inline_extent = margin_box_pt(margin_box_width);
        shape.placement = Some(logical_placement);
        self.push_float_shape(shape, run);
        true
    }

    /// Position an inline-source float against the whole current line band.
    ///
    /// The source-order marker stays in the graph with zero advance; callers
    /// retry selection after this succeeds so both the prefix and suffix flow
    /// around the new exclusion.
    /// <https://www.w3.org/TR/CSS22/visuren.html#float-position>
    pub(in crate::layout) fn try_place_inline_float_in_line_band(
        &mut self,
        graph: &InlineOpportunityGraph,
        float_position: InlineGraphPosition,
        context: InlineParagraphContext<'_>,
        row: InlineLinePhysicalRow,
    ) -> InlineFloatBandPlacement {
        if context.block_style.writing_mode != WritingMode::HorizontalTb {
            // A source-order float still establishes a physical CSS float in
            // vertical and sideways text. Its later line-wrap effect is
            // queried through `content_logical_band`, but placement itself
            // uses the existing floated-child path and the line's projected
            // physical containing block.
            // <https://drafts.csswg.org/css-writing-modes-4/#logical-to-physical>
            // <https://drafts.csswg.org/css-inline-3/#initial-letter-floats>
            let Some(float) = graph.float_at_position(float_position).cloned() else {
                return InlineFloatBandPlacement::Rejected;
            };
            let snapshot = self.snapshot();
            let saved_content_left = self.content_left;
            let saved_content_right = self.content_right;
            let saved_cursor_y = self.cursor_y;
            let saved_direction = self.containing_block_direction;
            let position = self.inline_line_physical_position_with_block_offset(
                row.line_index,
                context.block_style,
                row.block_offset,
            );
            self.content_left = position.content_left;
            self.content_right = position.content_right;
            self.cursor_y = position.cursor_y;
            self.containing_block_direction = context.block_style.used_direction();
            let shape_count_before = self
                .float_contexts
                .last()
                .map_or(0, |float_context| float_context.shapes.len());
            let mut run = self.float_run_state();
            let pushed_containing_block = self.push_inline_float_positioning_containing_block(
                &float,
                Some((graph, float_position)),
                position.content_left + context.padding_left,
                0.0,
                position.cursor_y,
            );
            let placed = self.layout_inline_float_contents(
                &float,
                context,
                FloatPlacementAxes::for_style(context.block_style),
                &mut run,
            );
            if pushed_containing_block {
                self.containing_blocks.pop();
            }
            let exclusion = self
                .float_contexts
                .last()
                .and_then(|float_context| float_context.shapes.last())
                .cloned();
            let accepted = placed
                && self.pages.len() == snapshot.page_count()
                && match self.float_contexts.last() {
                    Some(float_context) if float_context.shapes.len() > shape_count_before => {
                        float_context.shapes.last().is_some_and(|shape| {
                            shape.is_css_float()
                                && shape.page_index == self.current_float_page_index()
                        })
                    }
                    // An empty inline float has no physical exclusion, but
                    // its zero-advance marker was successfully consumed.
                    // It must remain distinct from a failed placement so it
                    // cannot manufacture a CSS Text source split.
                    _ => true,
                };
            if accepted {
                if let Some(exclusion) = exclusion {
                    self.commit_inline_float(&float, float_position, row.line_index, exclusion);
                }
                self.content_left = saved_content_left;
                self.content_right = saved_content_right;
                self.cursor_y = saved_cursor_y;
                self.containing_block_direction = saved_direction;
            } else {
                self.restore(snapshot);
            }
            return if accepted {
                InlineFloatBandPlacement::Placed
            } else {
                InlineFloatBandPlacement::Rejected
            };
        }
        let Some(float) = graph.float_at_position(float_position).cloned() else {
            return InlineFloatBandPlacement::Rejected;
        };
        let block_style = context.block_style;
        let band = self.inline_float_band_for_line_with_block_offset(
            row.line_index,
            block_style,
            context.available_width,
            context.padding_left,
            row.block_offset,
        );
        let line_indent = used_line_indent_for_formatted_line(
            row.identity.is_first_formatted_line,
            row.identity.starts_after_forced_break,
            context.hanging_indent,
            block_style,
            band.width(),
        );
        let line_left = self.content_left + context.padding_left + band.left_offset() + line_indent;
        let line_right =
            self.content_left + context.padding_left + band.left_offset() + band.width();
        let snapshot = self.snapshot();
        let saved_content_left = self.content_left;
        let saved_content_right = self.content_right;
        let saved_cursor_y = self.cursor_y;
        let saved_direction = self.containing_block_direction;
        let target_top = self
            .inline_line_physical_position_with_block_offset(
                row.line_index,
                block_style,
                row.block_offset,
            )
            .cursor_y;

        self.content_left = line_left;
        self.content_right = line_right;
        self.cursor_y = target_top;
        self.containing_block_direction = block_style.used_direction();
        let mut run = self.float_run_state();
        let pushed_containing_block = self.push_inline_float_positioning_containing_block(
            &float,
            Some((graph, float_position)),
            line_left,
            0.0,
            target_top,
        );
        let placed = self.layout_inline_float_contents(
            &float,
            context,
            FloatPlacementAxes::for_style(block_style),
            &mut run,
        );
        if pushed_containing_block {
            self.containing_blocks.pop();
        }
        let exclusion = self
            .float_contexts
            .last()
            .and_then(|context| context.shapes.last())
            .cloned();
        let accepted = if placed && self.pages.len() == snapshot.page_count() {
            exclusion.as_ref().is_none_or(|shape| {
                let float_block_span = shape.margin_box_block_span();
                let outer_edges = shape.outer_inline_edges();
                let line_band = PageInlineSpan::from_edges(line_left, line_right);
                let fits_line_band = outer_edges.fits_at_used_side_in_band(
                    shape.side,
                    line_band,
                    INLINE_FLOAT_EPSILON,
                );
                shape.page_index == self.pages.len()
                    && (float_block_span.top_y() - target_top).abs() <= INLINE_FLOAT_EPSILON
                    && (fits_line_band
                        || outer_edges.signed_extent().points()
                            > line_band.width() + INLINE_FLOAT_EPSILON)
            })
        } else {
            false
        };
        if accepted {
            if let Some(exclusion) = &exclusion {
                self.commit_inline_float(&float, float_position, row.line_index, exclusion.clone());
            }
            self.content_left = saved_content_left;
            self.content_right = saved_content_right;
            self.cursor_y = saved_cursor_y;
            self.containing_block_direction = saved_direction;
            if exclusion.is_none() || line_right - line_left <= INLINE_FLOAT_EPSILON {
                InlineFloatBandPlacement::PlacedInZeroWidthBand
            } else {
                InlineFloatBandPlacement::Placed
            }
        } else {
            self.restore(snapshot);
            InlineFloatBandPlacement::Rejected
        }
    }

    pub(in crate::layout) fn try_place_inline_float_on_current_line(
        &mut self,
        graph: &InlineOpportunityGraph,
        float_position: InlineGraphPosition,
        prefix_width: f32,
        context: InlineParagraphContext<'_>,
        row: InlineLinePhysicalRow,
    ) -> Option<InlineFloatPlacement> {
        if context.block_style.writing_mode != WritingMode::HorizontalTb {
            return None;
        }
        let float = graph.float_at_position(float_position).cloned()?;
        let block_style = context.block_style;
        let band = self.inline_float_band_for_line_with_block_offset(
            row.line_index,
            block_style,
            context.available_width,
            context.padding_left,
            row.block_offset,
        );
        let line_indent = used_line_indent_for_formatted_line(
            row.identity.is_first_formatted_line,
            row.identity.starts_after_forced_break,
            context.hanging_indent,
            block_style,
            band.width(),
        );
        let line_left = self.content_left + context.padding_left + band.left_offset() + line_indent;
        let line_right =
            self.content_left + context.padding_left + band.left_offset() + band.width();
        let (remaining_left, remaining_right) = match block_style.used_direction() {
            Direction::Ltr => ((line_left + prefix_width).min(line_right), line_right),
            Direction::Rtl => (line_left, (line_right - prefix_width).max(line_left)),
        };
        let snapshot = self.snapshot();
        let saved_content_left = self.content_left;
        let saved_content_right = self.content_right;
        let saved_cursor_y = self.cursor_y;
        let saved_direction = self.containing_block_direction;
        let target_top = self
            .inline_line_physical_position_with_block_offset(
                row.line_index,
                block_style,
                row.block_offset,
            )
            .cursor_y;
        self.content_left = remaining_left;
        self.content_right = remaining_right;
        self.cursor_y = target_top;
        self.containing_block_direction = block_style.used_direction();
        let mut run = self.float_run_state();
        let pushed_containing_block = self.push_inline_float_positioning_containing_block(
            &float,
            Some((graph, float_position)),
            line_left,
            prefix_width,
            target_top,
        );
        let placed = self.layout_inline_float_contents(
            &float,
            context,
            FloatPlacementAxes::for_style(block_style),
            &mut run,
        );
        if pushed_containing_block {
            self.containing_blocks.pop();
        }
        let accepted_shape = if placed && self.pages.len() == snapshot.page_count() {
            self.float_contexts.last().and_then(|context| {
                context.shapes.last().and_then(|shape| {
                    let float_block_span = shape.margin_box_block_span();
                    let outer_edges = shape.outer_inline_edges();
                    let remaining_band =
                        PageInlineSpan::from_edges(remaining_left, remaining_right);
                    let fits_remaining_band = outer_edges.fits_at_used_side_in_band(
                        shape.side,
                        remaining_band,
                        INLINE_FLOAT_EPSILON,
                    );
                    let accepted = shape.page_index == self.pages.len()
                        && (float_block_span.top_y() - target_top).abs() <= INLINE_FLOAT_EPSILON
                        && (fits_remaining_band
                            || (prefix_width <= INLINE_FLOAT_EPSILON
                                && outer_edges.signed_extent().points()
                                    > remaining_band.width() + INLINE_FLOAT_EPSILON));
                    accepted.then_some((fits_remaining_band, shape.clone()))
                })
            })
        } else {
            None
        };
        if let Some((fits_remaining_band, exclusion)) = accepted_shape {
            self.commit_inline_float(&float, float_position, row.line_index, exclusion);
            self.content_left = saved_content_left;
            self.content_right = saved_content_right;
            self.cursor_y = saved_cursor_y;
            self.containing_block_direction = saved_direction;
            let post_float_band = self.inline_float_band_for_line_with_block_offset(
                row.line_index,
                block_style,
                context.available_width,
                context.padding_left,
                row.block_offset,
            );
            let post_float_left =
                self.content_left + context.padding_left + post_float_band.left_offset();
            let post_float_right = self.content_left + context.padding_left + post_float_band.end();
            Some(InlineFloatPlacement::new(
                line_left,
                line_right,
                prefix_width,
                PageInlineSpan::from_edges(
                    post_float_left.max(line_left),
                    post_float_right.min(line_right),
                ),
                fits_remaining_band,
            ))
        } else {
            self.restore(snapshot);
            None
        }
    }

    fn push_inline_float_positioning_containing_block(
        &mut self,
        float: &InlineFloat,
        graph_position: Option<(&InlineOpportunityGraph, InlineGraphPosition)>,
        line_left: f32,
        prefix_width: f32,
        line_top: f32,
    ) -> bool {
        let Some(source) = float.positioning_containing_block() else {
            return false;
        };
        let style = &source.style;
        let border_widths = used_border_widths(style);
        let horizontal_non_padding =
            style.margin.left + border_widths.left + border_widths.right + style.margin.right;
        let padding_box_width =
            (prefix_width - horizontal_non_padding).max(style.padding.left + style.padding.right);
        let source_margin_edge_left = graph_position
            .and_then(|(graph, position)| {
                inline_positioning_source_margin_edge_left(graph, position, source.id, line_left)
            })
            .unwrap_or(line_left);
        let containing_block = ContainingBlock::from_page_top_rect(PageTopRect::new(
            source_margin_edge_left + style.margin.left + border_widths.left,
            line_top - border_widths.top,
            padding_box_width,
            style.line_height + style.padding.top + style.padding.bottom,
        ));
        let containing_block = self.positioned_containing_block_context(containing_block);
        self.containing_blocks.push(containing_block);
        true
    }

    #[allow(clippy::too_many_arguments)]
    pub(in crate::layout) fn try_select_inline_float_same_line_suffix(
        &mut self,
        graph: &InlineOpportunityGraph,
        prefix: InlineLineFragment,
        float_position: InlineGraphPosition,
        suffix_start: InlineGraphPosition,
        placement: InlineFloatPlacement,
        context: InlineParagraphContext<'_>,
        line_index: usize,
    ) -> Option<CombinedInlineFloatLine> {
        if context.block_style.used_direction() != Direction::Ltr
            || suffix_start >= graph.end_position()
        {
            return None;
        }
        // The float's signed outer margin geometry chose its placement, but
        // only the resulting non-negative exclusion band controls the
        // following in-flow suffix. In particular, a negative end margin may
        // leave that band unchanged while painting the float past its edge.
        // <https://www.w3.org/TR/CSS22/visuren.html#float-position>
        let float_gap = placement.same_line_suffix_gap();
        let suffix_available_width = placement.same_line_suffix_available_width();
        if suffix_available_width <= INLINE_FLOAT_EPSILON {
            return None;
        }
        let selected_end = self.select_inline_line_end_for_width(
            graph,
            suffix_start,
            context.block_style,
            suffix_available_width,
            line_index,
        );
        let end = selected_end.position.min(graph.end_position());
        if end <= suffix_start {
            return None;
        }
        // This helper combines one already-placed float with ordinary inline
        // suffix content. A later cleared float is not ordinary zero-advance
        // content: it must re-enter the main source-order loop so clearance
        // can select a later fragmentainer and register its own exclusion.
        // Otherwise the line fragment retains its marker but never lays the
        // float out, dropping it from both paint and fragmentation.
        // <https://www.w3.org/TR/CSS22/visuren.html#flow-control>
        if graph
            .first_float_position_in_range(InlineGraphRange {
                start: suffix_start,
                end,
            })
            .and_then(|position| graph.float_at_position(position))
            .is_some_and(|float| float.style().clear != Clear::None)
        {
            return None;
        }
        let suffix = graph.materialize_line(
            InlineGraphRange {
                start: suffix_start,
                end,
            },
            selected_end
                .break_opportunity
                .filter(|opportunity| opportunity.position == end && end < graph.end_position()),
            &mut self.font_system,
            context.block_style,
        );
        if suffix.items.is_empty() {
            return None;
        }

        let float = graph.float_at_position(float_position).cloned()?;
        let mut combined_items = prefix.items().to_vec();
        combined_items.push(MeasuredInlineItem::new(
            InlineLineItem::Float(float),
            float_gap,
            None,
        ));
        let suffix_text = suffix.used_text();
        combined_items.extend(suffix.items);
        let width = combined_items
            .iter()
            .map(|item| item.used_advance().points())
            .sum::<f32>();
        let metrics = self.mixed_inline_line_metrics(&combined_items, context.block_style, width);
        if (metrics.height - prefix.metrics.height).abs() > INLINE_FLOAT_EPSILON {
            return None;
        }
        let mut text = prefix.text().to_string();
        text.push_str(&suffix_text);
        Some(CombinedInlineFloatLine {
            end,
            fragment: InlineLineFragment::new(
                combined_items,
                metrics,
                prefix.hanging_widths,
                prefix.indent,
                prefix.available_width,
                prefix.float_replay.selected_float_page_index(),
                text,
            )
            .with_float_replay(prefix.float_replay.freeze_selected_band()),
        })
    }

    pub(in crate::layout) fn select_inline_line_end(
        &mut self,
        graph: &InlineOpportunityGraph,
        start: InlineGraphPosition,
        context: InlineParagraphContext<'_>,
        line_index: usize,
        line_identity: SelectedLineIdentity,
    ) -> SelectedInlineLineEnd {
        self.select_inline_line_end_with_block_offset(
            graph,
            start,
            context,
            line_index,
            line_identity,
            0.0,
        )
    }

    fn select_inline_line_end_with_block_offset(
        &mut self,
        graph: &InlineOpportunityGraph,
        start: InlineGraphPosition,
        context: InlineParagraphContext<'_>,
        line_index: usize,
        line_identity: SelectedLineIdentity,
        block_offset: f32,
    ) -> SelectedInlineLineEnd {
        self.select_inline_line_end_with_block_span(
            graph,
            start,
            context,
            line_index,
            line_identity,
            PhysicalLineSlab::inherited_strut(
                line_index,
                block_offset,
                context.block_style.line_height,
            ),
        )
    }

    fn select_inline_line_end_with_block_span(
        &mut self,
        graph: &InlineOpportunityGraph,
        start: InlineGraphPosition,
        context: InlineParagraphContext<'_>,
        line_index: usize,
        line_identity: SelectedLineIdentity,
        slab: PhysicalLineSlab,
    ) -> SelectedInlineLineEnd {
        let block_style = context.block_style;
        debug_assert_eq!(slab.line_index, line_index);
        let band = self.inline_float_band_for_physical_slab(
            block_style,
            context.available_width,
            context.padding_left,
            slab,
        );
        let line_indent = used_line_indent_for_formatted_line(
            line_identity.is_first_formatted_line,
            line_identity.starts_after_forced_break,
            context.hanging_indent,
            block_style,
            band.width(),
        );
        let line_available_width = (band.width() - line_indent).max(0.0);
        self.select_inline_line_end_for_width(
            graph,
            start,
            block_style,
            line_available_width,
            line_index,
        )
    }

    /// Select a source endpoint that may precede a block overflow marker.
    ///
    /// This is intentionally stricter than ordinary line fitting:
    /// `overflow-wrap` emergency boundaries can make the source fit a normal
    /// line, but do not become soft-wrap opportunities at which CSS Overflow
    /// may insert the marker. Likewise, `white-space: pre` retains forced
    /// line boundaries without permitting its internal soft candidates to
    /// host the marker.
    /// <https://drafts.csswg.org/css-overflow-4/#block-ellipsis>
    fn select_inline_line_end_for_block_ellipsis(
        &mut self,
        graph: &InlineOpportunityGraph,
        start: InlineGraphPosition,
        block_style: &ComputedStyle,
        line_available_width: f32,
        line_index: usize,
    ) -> SelectedInlineLineEnd {
        let mut last_fitting = None;
        for opportunity in graph.break_opportunities_after(start) {
            if !self.mixed_graph_opportunity_allowed(graph, opportunity)
                || opportunity.availability.is_fallback()
                || (!block_style.allows_soft_wrap() && opportunity_is_soft_wrap(opportunity))
            {
                continue;
            }
            let selected = SelectedInlineLineEnd {
                position: opportunity.position,
                break_opportunity: (opportunity.position < graph.end_position())
                    .then_some(opportunity),
            };
            let fitting_tolerance = if matches!(opportunity.kind, InlineBreakKind::BreakSpaces) {
                0.0
            } else {
                INLINE_FLOAT_EPSILON
            };
            if self.balanced_line_fit_width(
                graph,
                start,
                selected,
                block_style,
                line_index,
                Some(line_available_width),
            ) <= line_available_width + fitting_tolerance
            {
                last_fitting = Some(selected);
                if matches!(opportunity.kind, InlineBreakKind::Forced) {
                    break;
                }
            }
        }
        last_fitting.unwrap_or(SelectedInlineLineEnd {
            position: start,
            break_opportunity: None,
        })
    }

    pub(in crate::layout) fn select_inline_line_end_for_width(
        &mut self,
        graph: &InlineOpportunityGraph,
        start: InlineGraphPosition,
        block_style: &ComputedStyle,
        line_available_width: f32,
        line_index: usize,
    ) -> SelectedInlineLineEnd {
        // `wrap-inside: avoid` ranks otherwise-legal break opportunities by
        // lexical containment. The smallest depth is least avoided; ties keep
        // the ordinary last-fitting-line policy.
        // <https://drafts.csswg.org/css-text-4/#wrap-inside-property>
        let mut regular_fit = None::<(u16, SelectedInlineLineEnd)>;
        // CSS Text 4 relaxes candidates in ordered stages. Keeping one best
        // candidate per stage makes it impossible for an overflow fallback to
        // outrank a deferred `keep-all` or `auto-phrase` boundary merely
        // because it appears later in source order.
        let mut fallback_fits = [None::<(u16, SelectedInlineLineEnd)>; 3];
        // A zero-width line can have no fitting candidate at all. Preserve
        // the first candidate in each deferred stage so CSS Text can still
        // make progress with its required overflow relaxation (notably an
        // authored `&shy;` under `auto-phrase`).
        let mut fallback_overflows = [None::<SelectedInlineLineEnd>; 3];
        // A `break-spaces` boundary is an ordinary, source-order boundary
        // *after* its preserved space. Unlike the other ordinary boundaries,
        // its selected line is allowed to overflow: CSS Text retains the
        // space's advance rather than hanging or trimming it. Keep that
        // deliberate overflow separate from fitting candidates. Any fitting
        // candidate, including an `overflow-wrap` emergency boundary before
        // the preserved space, must win because CSS Text requires line
        // wrapping to minimize overflow.
        // <https://drafts.csswg.org/css-text-3/#valdef-white-space-break-spaces>
        let mut overflowing_break_spaces = None::<SelectedInlineLineEnd>;
        let opportunities = graph.break_opportunity_slice_after(start);
        // `::first-line` can change shaping advances (notably
        // `word-spacing`), so its candidate measurements are not the graph's
        // monotonic source measurements. Keep it on the shared materialized
        // measurement path used by balancing and final paint.
        // <https://drafts.csswg.org/css-pseudo-4/#first-line-pseudo>
        if let Some(selected) = self.select_monotonic_regular_line_end(
            graph,
            start,
            block_style,
            line_index,
            line_available_width,
            opportunities,
        ) {
            return selected;
        }
        for &opportunity in opportunities {
            // A source-order float marker participates in float placement,
            // never in CSS Text line breaking. In particular, selecting it
            // here would split a `white-space: pre`/`nowrap` continuation and
            // could make an earlier hyphenation opportunity look like a
            // legitimate rewind target.
            // <https://www.w3.org/TR/css-text-3/#line-breaking>
            // <https://www.w3.org/TR/CSS22/visuren.html#float-position>
            if matches!(opportunity.kind, InlineBreakKind::FloatPlacement) {
                continue;
            }
            if !self.mixed_graph_opportunity_allowed(graph, opportunity) {
                continue;
            }
            let selected_break =
                (opportunity.position < graph.end_position()).then_some(opportunity);
            let selected = SelectedInlineLineEnd {
                position: opportunity.position,
                break_opportunity: selected_break,
            };
            let fit_width = self.balanced_line_fit_width(
                graph,
                start,
                selected,
                block_style,
                line_index,
                Some(line_available_width),
            );
            // A fitting `break-spaces` boundary must include the preserved
            // space exactly. The general half-point tolerance is only for
            // shaped glyph rounding; applying it here would incorrectly turn
            // a four-cell preserved space line into a fitting three-cell one.
            let fitting_tolerance = if matches!(opportunity.kind, InlineBreakKind::BreakSpaces) {
                0.0
            } else {
                0.5
            };
            if fit_width <= line_available_width + fitting_tolerance {
                let avoid_depth = graph.wrap_inside_avoid_depth(opportunity.position);
                // An automatic opportunity inside the paragraph's final word
                // is unnecessary when that whole word fits on a fresh line.
                // Keep the preceding ordinary boundary instead of using a
                // discretionary hyphen merely to fill spare space on the
                // current line. This is the CSS Text hyphenation preference
                // exercised by the final-word case; a word that cannot fit
                // in the line measure still uses its legal dictionary break.
                // <https://drafts.csswg.org/css-text-3/#hyphenation>
                if opportunity.is_discretionary()
                    // CSS Text's preference for moving an automatically
                    // hyphenated final word to the next line does not apply
                    // to an authored U+00AD. The author supplied that exact
                    // discretionary boundary, whose marker and advance must
                    // compete with the preceding ordinary space normally.
                    && graph.source_character_before(opportunity.position) != Some('\u{00ad}')
                    && let Some((previous_depth, previous)) = regular_fit
                    && previous_depth == avoid_depth
                    && previous
                        .break_opportunity
                        .is_some_and(|previous| !previous.is_discretionary())
                    && graph
                        .source_character_before(previous.position)
                        .is_some_and(is_css_collapsible_whitespace)
                {
                    let remainder = graph.materialize_line(
                        InlineGraphRange {
                            start: previous.position,
                            end: graph.end_position(),
                        },
                        None,
                        &mut self.font_system,
                        block_style,
                    );
                    if !remainder.used_text_contains_whitespace()
                        && remainder.fitting_width <= line_available_width + 0.5
                    {
                        return previous;
                    }
                    // If the final word needs hyphenation even on a fresh
                    // line, choose the preceding ordinary boundary whenever
                    // that fresh line can use a later dictionary opportunity.
                    // It avoids degrading `1 example` from `1` / `exam-` /
                    // `ple` to `1 ex-` / `ample` simply to fill the first
                    // line's spare inline measure.
                    let fresh = self.select_inline_line_end_for_width(
                        graph,
                        previous.position,
                        block_style,
                        line_available_width,
                        line_index.saturating_add(1),
                    );
                    if fresh.position > selected.position {
                        return previous;
                    }
                }
                let stage = usize::from(opportunity.availability.fitting_stage());
                if stage != 0 {
                    let fallback_fit = &mut fallback_fits[stage - 1];
                    if fallback_fit.is_none_or(|(fit_depth, fit)| {
                        avoid_depth < fit_depth
                            || (avoid_depth == fit_depth && selected.position > fit.position)
                    }) {
                        *fallback_fit = Some((avoid_depth, selected));
                    }
                } else if regular_fit.is_none_or(|(fit_depth, fit)| {
                    avoid_depth < fit_depth
                        || (avoid_depth == fit_depth && selected.position > fit.position)
                }) && !regular_fit.is_some_and(|(fit_depth, fit)| {
                    fit_depth == avoid_depth
                        && graph.soft_hyphen_precedes_literal_hyphen(
                            fit.break_opportunity.expect("fitting graph boundary"),
                            opportunity,
                        )
                }) {
                    regular_fit = Some((avoid_depth, selected));
                }
                if matches!(opportunity.kind, InlineBreakKind::Forced) {
                    return selected;
                }
            } else if matches!(opportunity.kind, InlineBreakKind::BreakSpaces)
                && regular_fit.is_none()
            {
                // This is not a hanging edge effect: it is the retained
                // document space itself. Continue in source order so a later
                // fitting opportunity can still win. It remains a progress
                // fallback only after every candidate that fits has failed.
                overflowing_break_spaces.get_or_insert(selected);
            } else if opportunity.availability.is_fallback() {
                // A later source candidate can still be available in an
                // earlier relaxation stage. Continue until every typed stage
                // has been considered, then choose the first populated one.
                let stage = usize::from(opportunity.availability.fitting_stage());
                fallback_overflows[stage - 1].get_or_insert(selected);
                continue;
            } else if let Some(position) = regular_fit
                .map(|(_, selected)| selected)
                .or_else(|| {
                    fallback_fits
                        .iter()
                        .flatten()
                        .next()
                        .map(|(_, selected)| *selected)
                })
                .or(overflowing_break_spaces)
                .or_else(|| fallback_overflows.iter().flatten().next().copied())
            {
                return position;
            } else {
                return SelectedInlineLineEnd {
                    position: opportunity.position,
                    break_opportunity: (opportunity.position < graph.end_position())
                        .then_some(opportunity),
                };
            }
        }
        regular_fit
            .map(|(_, selected)| selected)
            .or_else(|| {
                fallback_fits
                    .iter()
                    .flatten()
                    .next()
                    .map(|(_, selected)| *selected)
            })
            .or(overflowing_break_spaces)
            .or_else(|| fallback_overflows.iter().flatten().next().copied())
            .unwrap_or_else(|| SelectedInlineLineEnd {
                position: graph.end_position(),
                break_opportunity: None,
            })
    }

    /// Select a line end by binary search when every candidate has a monotonic
    /// used advance.
    ///
    /// Re-materializing every candidate prefix makes a long paragraph
    /// quadratic, especially for `word-break: break-all`. Restrict this
    /// shortcut to source text without discretionary, hanging, spacing-trim,
    /// or atomic effects; source provenance, rather than ASCII membership,
    /// establishes whether a particular Unicode range can use this
    /// measurement.
    /// <https://drafts.csswg.org/css-text-3/#line-breaking>
    /// <https://drafts.csswg.org/css-text-3/#word-break-property>
    fn select_monotonic_regular_line_end(
        &mut self,
        graph: &InlineOpportunityGraph,
        start: InlineGraphPosition,
        block_style: &ComputedStyle,
        line_index: usize,
        line_available_width: f32,
        opportunities: &[InlineBreakOpportunity],
    ) -> Option<SelectedInlineLineEnd> {
        if opportunities.is_empty()
            || !source_measurement_context_matches_selected_line(graph, block_style, line_index)
        {
            return None;
        }
        let cursor = graph.monotonic_source_measure_cursor_after(start)?;
        let opportunity = cursor
            .last_fitting(line_available_width)
            .or_else(|| cursor.first())?;
        Some(SelectedInlineLineEnd {
            position: opportunity.position,
            break_opportunity: (opportunity.position < graph.end_position()).then_some(opportunity),
        })
    }

    pub(in crate::layout) fn mixed_graph_opportunity_allowed(
        &mut self,
        graph: &InlineOpportunityGraph,
        opportunity: InlineBreakOpportunity,
    ) -> bool {
        // Float-placement checkpoints are consumed by the source-order float
        // handler after a candidate line range is selected. They are not CSS
        // Text line ends: treating one as a regular fitting candidate splits
        // a `nowrap` run immediately before its float marker.
        if matches!(opportunity.kind, InlineBreakKind::FloatPlacement) {
            return false;
        }
        if opportunity.position.byte_offset > 0
            || opportunity.availability.is_fallback()
            || matches!(
                opportunity.kind,
                InlineBreakKind::Forced
                    | InlineBreakKind::PreservedSpace
                    | InlineBreakKind::BreakSpaces
                    | InlineBreakKind::Hyphenation
            )
        {
            return true;
        }
        let Some(item) = graph
            .runs
            .iter()
            .skip(opportunity.position.run_index)
            .find(|run| {
                !matches!(
                    &run.item,
                    InlineLineItem::Atom(atom) if atom.content().is_box_edge()
                )
            })
            .map(|run| &run.item)
        else {
            return true;
        };
        !mixed_inline_item_starts_with_suppressed_line_start_punctuation(item)
    }

    pub(in crate::layout) fn materialize_inline_line_fragment(
        &mut self,
        graph: &InlineOpportunityGraph,
        range: InlineGraphRange,
        context: InlineParagraphContext<'_>,
        row: InlineLinePhysicalRow,
        break_opportunity: Option<InlineBreakOpportunity>,
    ) -> InlineLineFragment {
        let block_style = context.block_style;
        let band = self.inline_float_band_for_line_with_block_offset(
            row.line_index,
            block_style,
            context.available_width,
            context.padding_left,
            row.block_offset,
        );
        let line_indent = used_line_indent_for_formatted_line(
            row.identity.is_first_formatted_line,
            row.identity.starts_after_forced_break,
            context.hanging_indent,
            block_style,
            band.width(),
        );
        let line_available_width = (band.width() - line_indent).max(0.0);
        let line_end = graph.selected_line_end_condition(range, break_opportunity);
        let mut materialized = graph.materialize_line_for_selected_end_for_available_width(
            range,
            break_opportunity,
            line_end,
            line_available_width,
            &mut self.font_system,
            block_style,
        );
        let materializes_first_line_style =
            row.identity.is_first_formatted_line && block_style.first_line_style.is_some();
        if materializes_first_line_style {
            self.materialize_first_line_used_style(
                &mut materialized,
                block_style,
                line_end,
                Some(line_available_width),
            );
        }
        resolve_materialized_line_leaders(
            &mut materialized,
            &mut self.font_system,
            line_available_width,
        );
        let content_width = materialized.content_width;
        let metrics =
            self.mixed_inline_line_metrics(&materialized.items, block_style, content_width);
        let bidi_scope_continuations = graph.bidi_scope_continuations_for_range(range);
        let used_text = materialized.used_text();
        let edge_effects = materialized.edge_effects.clone();
        let mut fragment = InlineLineFragment::new(
            materialized.items,
            metrics,
            HangingPunctuationWidths::default(),
            band.left_offset() + line_indent,
            band.end(),
            self.current_float_page_index(),
            used_text,
        )
        .with_edge_effects(edge_effects)
        .with_bidi_scope_continuations(bidi_scope_continuations)
        .with_source_end(range.end);
        if materializes_first_line_style {
            fragment.mark_first_line_style_materialized();
        }
        fragment
    }
}

/// Return the used block advance not represented by the selector's nominal
/// line-height index.
///
/// The durable inline-line sequence uses the greater of the strut, resolved
/// line metrics, and atomic inline margin-box block-size. Keep float-band
/// selection in the same coordinate system so a later row is queried beside
/// the float at the row it will actually paint on.
/// <https://drafts.csswg.org/css-inline-3/#line-boxes>
fn used_inline_line_block_advance(
    fragment: &InlineLineFragment,
    block_style: &ComputedStyle,
) -> f32 {
    (used_inline_line_block_size_from_items(&fragment.items, fragment.metrics.height, block_style)
        - block_style.line_height)
        .max(0.0)
}

fn used_inline_line_block_size_from_items(
    items: &[MeasuredInlineItem],
    metrics_height: f32,
    block_style: &ComputedStyle,
) -> f32 {
    let atomic_block_size = items
        .iter()
        // An initial letter is an in-flow inline, but CSS Inline explicitly
        // excludes it from the logical height of its originating line box.
        // Its multi-line block extent belongs to its exclusion geometry, not
        // to this line's normal block-stack advance.
        // <https://drafts.csswg.org/css-inline-3/#initial-letter-position>
        .filter(|item| {
            !LayoutBuilder::inline_line_item_is_initial_letter(&item.item)
                && matches!(
                    &item.item,
                    InlineLineItem::Atom(atom)
                        if !matches!(atom.content(), InlineAtomContent::InlineEdge(_))
                )
        })
        // Text fragments already contribute their selected line-box extents
        // through `metrics_height`. Reintroducing their raw `line-height`
        // here would erase `text-box-trim` (and `line-fit-edge`) after the
        // line-metrics pass. Only atomic inline margin boxes need this
        // independent minimum-size contribution.
        // <https://drafts.csswg.org/css-inline-3/#line-box>
        .map(|item| inline_line_item_logical_block_size(&item.item, block_style))
        .fold(0.0_f32, f32::max);
    metrics_height
        .max(atomic_block_size)
        .max(block_style.line_height)
}

fn inline_positioning_source_margin_edge_left(
    graph: &InlineOpportunityGraph,
    float_position: InlineGraphPosition,
    source_id: InlinePositioningContainingBlockId,
    line_left: f32,
) -> Option<f32> {
    // CSS 2.2 uses the nearest positioned inline ancestor's padding box, not
    // the float's own inline position, as the containing block for abspos
    // descendants:
    // <https://www.w3.org/TR/CSS22/visudet.html#containing-block-details>.
    for source_run_index in (0..float_position.run_index).rev() {
        let run = &graph.runs[source_run_index];
        if inline_run_is_positioning_source_start_edge(run, source_id) {
            // The start-edge atom carries the source's own padding advance,
            // whereas the containing-block origin is its margin edge. Its
            // coordinate is therefore the accumulated advance *before* that
            // edge. This retains a positioned outer inline's preceding
            // padding for a nested source, without treating the source's own
            // padding (or later text before the float) as an origin offset.
            let source_margin_edge_offset = graph.runs[..source_run_index]
                .iter()
                .map(|preceding| preceding.width)
                .sum::<f32>();
            return Some(line_left + source_margin_edge_offset);
        }
    }
    None
}

fn inline_run_is_positioning_source_start_edge(
    run: &InlineParagraphRun,
    source_id: InlinePositioningContainingBlockId,
) -> bool {
    let InlineLineItem::Atom(atom) = &run.item else {
        return false;
    };
    let InlineAtomContent::InlineEdge(InlineEdgeRole::BoxEdge(edge)) = atom.content() else {
        return false;
    };
    edge.logical_edge == InlineLogicalEdge::Start
        && edge.positioning_containing_block_id == Some(source_id)
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use super::*;
    use crate::css::{self, ComputedLineHeight, FontFamily, Stylesheets};
    use crate::layout::{LayoutBuilderConfig, RenderOptions};
    use crate::resource::ResourceCache;
    use crate::text::FontSystem;

    /// CSS Text fitting changes `font-size` as a used value, while CSS
    /// Inline's `normal` line height comes from the selected font's metrics.
    /// <https://drafts.csswg.org/css-text-5/#text-fit-property>
    /// <https://drafts.csswg.org/css-inline-3/#line-height-property>
    #[tokio::test]
    async fn text_fit_used_line_style_resolves_normal_line_height_from_ahem() {
        let stylesheet = css::parse_stylesheet(
            &css::Css::from_string(
                r#"@font-face {
                    font-family: TextFitAhem;
                    src: url("tests/fixtures/wpt/css/css-fonts/Ahem.ttf");
                }"#,
            )
            .with_base_path(".")
            .expect("current directory should be a valid file URL"),
        );
        let font_system = FontSystem::start_loading()
            .load_stylesheet_fonts(&[stylesheet])
            .finish()
            .await;
        let options = RenderOptions::default();
        let stylesheets = Vec::new();
        let resource_cache = ResourceCache::default();
        let iframe_documents = HashMap::new();
        let mut builder = LayoutBuilder::new(LayoutBuilderConfig {
            options: &options,
            stylesheets: Stylesheets::document_only(&stylesheets),
            base_url: None,
            root_url: None,
            resource_cache: &resource_cache,
            iframe_documents: &iframe_documents,
            iframe_viewport: None,
            page_progression_direction: Direction::Ltr,
            page_counter_initial_values: HashMap::new(),
            target_references: crate::layout::TargetReferenceSnapshot::default(),
            font_system,
        });
        let mut parent = ComputedStyle::initial();
        parent.font_family = FontFamily::Names(vec!["TextFitAhem".to_string()]);
        parent.font_size = 7.5;
        parent.line_height = parent.font_size * 1.2;
        parent.line_height_value = ComputedLineHeight::Normal;

        let used = builder.text_fit_used_line_style(
            &parent,
            TextFitScale::new(2.0).expect("two is a valid text-fit scale"),
        );
        let descendant_metrics = builder.inline_text_box_metrics(used.block_style(), 0.0);
        let font_resolved = builder
            .font_system
            .used_line_height(used.block_style())
            .points();

        assert!((used.block_style().line_height - font_resolved).abs() < 0.001);
        assert!(
            (used.block_style().line_height - descendant_metrics.line_block_size).abs() < 0.001
        );
        assert!(
            (used.block_style().line_height - used.block_style().font_size * 1.2).abs() > 0.001,
            "Ahem's metric line height must not fall back to 1.2em"
        );
    }

    fn test_break_opportunity(kind: InlineBreakKind) -> InlineBreakOpportunity {
        InlineBreakOpportunity {
            position: InlineGraphPosition::at_run_start(1),
            kind,
            availability: BreakAvailability::Ordinary,
            whitespace_edge: SelectedWhitespaceEdge::None,
            discretionary: None,
        }
    }

    #[test]
    fn float_placement_is_not_a_soft_wrap() {
        assert!(opportunity_is_soft_wrap(test_break_opportunity(
            InlineBreakKind::AtomicBoundary,
        )));
        assert!(!opportunity_is_soft_wrap(test_break_opportunity(
            InlineBreakKind::FloatPlacement,
        )));
        assert!(!opportunity_is_soft_wrap(test_break_opportunity(
            InlineBreakKind::Forced,
        )));
    }

    #[test]
    fn balance_scores_remaining_line_space_not_source_width() {
        // A float can make a shorter source line better balanced because its
        // available measure is smaller.  The score must therefore be based on
        // the remaining space after each line's own exclusion and indent.
        let float_aware = balanced_remaining_space_variance(&[7.5, 9.5, 6.5]);
        let source_width_only = balanced_remaining_space_variance(&[2.0, 8.0, 13.0]);
        assert!(float_aware < source_width_only);
        assert_eq!(balanced_remaining_space_variance(&[4.0, 4.0]), 0.0);
    }

    #[test]
    fn float_clearance_caps_an_overwide_line_at_its_containing_measure() {
        assert_eq!(float_clearance_required_width(140.0, 10.0, 100.0), 100.0);
        assert_eq!(float_clearance_required_width(60.0, 10.0, 100.0), 70.0);
    }

    #[test]
    fn text_fit_scale_separates_fixed_and_scalable_advances() {
        // At scale one, this line is 70pt: 20pt fixed plus 50pt scalable.
        // At scale two it is 120pt, confirming the same 20pt fixed advance.
        let scale =
            text_fit_scale_from_measurements(95.0, 70.0, 120.0, css::TextFitDirection::Grow, None);
        assert_eq!(scale.factor(), 1.5);
    }

    #[test]
    fn text_fit_scale_honors_directional_limits_and_fixed_only_lines() {
        assert_eq!(
            text_fit_scale_from_measurements(
                220.0,
                70.0,
                120.0,
                css::TextFitDirection::Grow,
                Some(1.2),
            )
            .factor(),
            1.2
        );
        assert_eq!(
            text_fit_scale_from_measurements(
                30.0,
                70.0,
                120.0,
                css::TextFitDirection::Shrink,
                Some(0.8),
            )
            .factor(),
            0.8
        );
        assert_eq!(
            text_fit_scale_from_measurements(100.0, 40.0, 40.0, css::TextFitDirection::Grow, None,)
                .factor(),
            1.0,
            "a line containing only fixed inline content must not fit"
        );
    }

    #[test]
    fn per_line_excludes_final_and_forced_break_records() {
        assert!(text_fit_strategy_scales_record(
            css::TextFitStrategy::PerLine,
            0,
            2,
            InlineLineTermination::SoftWrap,
        ));
        assert!(!text_fit_strategy_scales_record(
            css::TextFitStrategy::PerLine,
            1,
            2,
            InlineLineTermination::ForcedBreak,
        ));
        assert!(!text_fit_strategy_scales_record(
            css::TextFitStrategy::PerLine,
            2,
            2,
            InlineLineTermination::BlockEnd,
        ));
        assert!(text_fit_strategy_scales_record(
            css::TextFitStrategy::PerLineAll,
            2,
            2,
            InlineLineTermination::ForcedBreak,
        ));
    }

    #[test]
    fn text_fit_scales_only_font_dependent_line_height() {
        let mut unitless = ComputedStyle::initial();
        unitless.font_size = 10.0;
        unitless.line_height = 15.0;
        unitless.line_height_value = css::ComputedLineHeight::Number(1.5);
        scale_text_fit_fragment_style(&mut unitless, 2.0);
        assert_eq!(unitless.font_size, 20.0);
        assert_eq!(unitless.line_height, 30.0);

        let mut fixed = ComputedStyle::initial();
        fixed.font_size = 10.0;
        fixed.line_height = 15.0;
        fixed.line_height_value = css::ComputedLineHeight::from_points(15.0);
        scale_text_fit_fragment_style(&mut fixed, 2.0);
        assert_eq!(fixed.font_size, 20.0);
        assert_eq!(fixed.line_height, 15.0);
    }

    #[test]
    fn initial_letter_uses_the_logical_block_start_column_in_vertical_modes() {
        assert_eq!(
            initial_letter_exclusion_side(WritingMode::VerticalRl, UsedFloatSide::Left),
            UsedFloatSide::Right
        );
        assert_eq!(
            initial_letter_exclusion_side(WritingMode::SidewaysRl, UsedFloatSide::Left),
            UsedFloatSide::Right
        );
        assert_eq!(
            initial_letter_exclusion_side(WritingMode::VerticalLr, UsedFloatSide::Right),
            UsedFloatSide::Left
        );
        assert_eq!(
            initial_letter_exclusion_side(WritingMode::SidewaysLr, UsedFloatSide::Right),
            UsedFloatSide::Left
        );
        assert_eq!(
            initial_letter_exclusion_side(WritingMode::HorizontalTb, UsedFloatSide::Right),
            UsedFloatSide::Right
        );
    }
}