kui-core 0.1.0-alpha.44

kui contract: flat per-frame tree, clay-style flex layout, text stack, events as data, quad display list
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
//! The flex solver: clay-style layout over the flat tree, run by
//! [`compute`] once per frame.
//!
//! Each node declares a [`Sizing`] per axis (`Fit`,
//! `Grow`, `Fixed`, `Percent` or a size expression), optional clamps, a
//! direction, padding, gap and alignment; see
//! [`LayoutSpec`](crate::spec::LayoutSpec). The solver resolves them in
//! five passes over the preorder tree:
//!
//! 1. fit widths (reverse: children before parents)
//! 2. grow widths (forward: parents before children)
//! 3. fit heights (reverse; text wraps at its final width here)
//! 4. grow heights (forward)
//! 5. positions (forward)
//!
//! A wrapping row breaks its children into lines in pass 2 and every later
//! pass reads that grouping, which is why wrapping is rows-only: a row's
//! width is final before its height is measured, where a column would need
//! its height first. A sixth pass runs only on a frame with a float
//! anchored to a node by key (`FloatAnchor::Node`), since such a float's
//! anchor may come later in preorder; its subtree is laid out again from
//! the anchor's final rect.
//!
//! A table (`LayoutSpec::table`) is a column whose rows' cells line up:
//! pass 1 reaches the table after its rows and cells and sets every fit
//! cell to its column's widest and every row to the columns' width, so the
//! table's own fit width is the aligned columns; pass 2 reaches it before
//! its rows, resolves the columns against the widest row once and writes
//! each column's width into its cells, and a row of a table then leaves
//! its children alone. A column, a table or a leaf straight under the
//! table has no cells.
//!
//! Text measurement goes through [`TextMeasure`], so the solver is
//! testable with a deterministic stub and never depends on system fonts.

use crate::geom::{Rect, Size, Vec2};
use crate::scroll::ScrollStore;
use crate::spec::{Align, Dir, FloatAnchor, Min, Sizing};
use crate::tree::{NIL, NodeContent, Tree};

/// Gated on the tree-level flag first: on a frame with no floats this is
/// one predicted branch rather than a read through every child's spec.
#[inline]
fn is_float(tree: &Tree, i: u32) -> bool {
    tree.any_float && tree.specs[i as usize].layout.float.is_some()
}

/// Where in the free space one thing sits: 0 at the start, 1 at the end.
/// The spreads and `Baseline` mean nothing for a single placement — a
/// cross axis, a float's attach point — and land where a spread puts a
/// lone child (`diag::ALIGN_IGNORED` says so where one is declared).
fn align_factor(a: Align) -> f32 {
    match a {
        Align::Start | Align::SpaceBetween | Align::Baseline => 0.0,
        Align::Center | Align::SpaceAround | Align::SpaceEvenly => 0.5,
        Align::End => 1.0,
    }
}

/// How the main axis's free space is dealt out to `n` in-flow children:
/// what goes before the first, and what goes between each two on top of
/// the gap. Nothing is dealt when nothing is free, so a
/// run that overflows or holds a grow child is laid out by its gaps
/// alone, whatever the alignment.
fn main_spread(a: Align, free: f32, n: u32) -> (f32, f32) {
    let n = n as f32;
    match a {
        Align::SpaceBetween if n > 1.0 => (0.0, free / (n - 1.0)),
        Align::SpaceAround if n > 0.0 => (free / (2.0 * n), free / n),
        Align::SpaceEvenly => (free / (n + 1.0), free / (n + 1.0)),
        _ => (align_factor(a) * free, 0.0),
    }
}

fn mirror(a: Align) -> Align {
    match a {
        Align::Start => Align::End,
        Align::End => Align::Start,
        other => other,
    }
}

/// Whether the in-flow children of `i` line up by their baselines: a row
/// that says so. A column's cross axis is horizontal, where a baseline is
/// not a line, so it lays out as `Start` there (as CSS does).
#[inline]
fn baseline_row(tree: &Tree, i: u32) -> bool {
    let l = &tree.specs[i as usize].layout;
    l.cross_align == Align::Baseline && l.dir == Dir::Row
}

/// Whether child `c` of a baseline row takes part in the alignment: a
/// child whose height is `Grow` or `Percent` is sized against the line
/// and fills it, so it sits at the line's top and its height does not
/// count toward the line's (as in `line_extents`).
#[inline]
fn aligns_by_baseline(tree: &Tree, c: u32) -> bool {
    !matches!(
        child_sizing(tree, c, AxisSel::Height),
        Sizing::Grow(_) | Sizing::Percent(_) | Sizing::Calc(_)
    )
}

/// The first baseline of node `i`, logical px below its top edge, or
/// `None` when nothing inside it is text. A text node's
/// and an editor's are measured (`TextMeasure::baseline`, stored by
/// `fit_heights` on a frame that has a baseline row); a container's is
/// its first in-flow child's, carried down through where that child
/// sits in it — which is why this reads sizes only, and can answer in
/// the fit pass as well as in `positions`. Scrolling is ignored: a
/// scrolled list's baseline is its unscrolled first row's.
fn first_baseline(tree: &Tree, i: u32) -> Option<f32> {
    match tree.content[i as usize] {
        NodeContent::Text(_) | NodeContent::Edit(_) => tree
            .baseline
            .get(i as usize)
            .copied()
            .filter(|b| b.is_finite()),
        NodeContent::Container => {
            let f = first_in_flow(tree, i);
            if f == NIL {
                return None;
            }
            let spec = &tree.specs[i as usize].layout;
            let size = tree.size[i as usize];
            let fs = tree.size[f as usize];
            match spec.dir {
                Dir::Column => {
                    let fb = first_baseline(tree, f)?;
                    let content = (size.h - spec.padding.y()).max(0.0);
                    let (mut used, mut n) = (0.0f32, 0u32);
                    for c in tree.children(i) {
                        if !is_float(tree, c) {
                            used += tree.size[c as usize].h;
                            n += 1;
                        }
                    }
                    if n > 1 {
                        used += spec.gap * (n - 1) as f32;
                    }
                    let (lead, _) = main_spread(spec.main_align, (content - used).max(0.0), n);
                    Some(spec.padding.t + lead + fb)
                }
                Dir::Row => {
                    let end = if wraps(tree, i) {
                        line_end(tree, f)
                    } else {
                        NIL
                    };
                    if spec.cross_align == Align::Baseline {
                        // The row's own shared baseline, when anything in
                        // its first line has one.
                        let (above, _, any) = line_baseline(tree, f, end);
                        return any.then_some(spec.padding.t + above);
                    }
                    let fb = first_baseline(tree, f)?;
                    let extent = if end == NIL {
                        (size.h - spec.padding.y()).max(0.0)
                    } else {
                        line_extents(tree, f, end, spec.gap).1
                    };
                    let off = align_factor(spec.cross_align) * (extent - fs.h).max(0.0);
                    Some(spec.padding.t + off + fb)
                }
            }
        }
        _ => None,
    }
}

/// A baseline line `[c, end)`'s shared baseline: the most any taking
/// part reaches above it, the most any hangs below it, and whether any
/// child had a baseline of its own. A child with none aligns its bottom
/// edge (CSS's synthesized baseline), so all of it is above.
fn line_baseline(tree: &Tree, c: u32, end: u32) -> (f32, f32, bool) {
    let (mut above, mut below, mut any) = (0.0f32, 0.0f32, false);
    let mut k = c;
    while k != end && k != NIL {
        if !is_float(tree, k) && aligns_by_baseline(tree, k) {
            let h = tree.size[k as usize].h;
            let b = match first_baseline(tree, k) {
                Some(b) => {
                    any = true;
                    b
                }
                None => h,
            };
            above = above.max(b);
            below = below.max(h - b);
        }
        k = tree.next_sibling[k as usize];
    }
    (above, below, any)
}

/// One axis of float attachment: anchor point minus self point, plus offset.
fn attach(
    anchor_pos: f32,
    anchor_len: f32,
    self_len: f32,
    anchor_pt: Align,
    self_pt: Align,
    off: f32,
) -> f32 {
    anchor_pos + align_factor(anchor_pt) * anchor_len - align_factor(self_pt) * self_len + off
}

/// How far `[pos, pos+len]` sticks out of `[0, limit]`.
fn overflow(pos: f32, len: f32, limit: f32) -> f32 {
    (-pos).max(0.0) + (pos + len - limit).max(0.0)
}

/// Whether `i` breaks its children into wrap lines.
///
/// Rows only, and never on a main axis that scrolls. Breaking needs a
/// definite main size to break against, and the pass order hands a row one
/// — its width is final in pass 2, before its height is measured in pass 3,
/// so a line's cross extent is known by the time anything needs it. A
/// column is the mirror image and does not work: its main size is not final
/// until pass 4, two passes after the cross-axis fit that would have to sum
/// the lines. `scroll_x` says the same thing a different way — an axis that
/// scrolls is unbounded, and an unbounded axis has nothing to break
/// against. `diag::WRAP_IGNORED` reports both. Always inlined: asked of
/// every node in three passes, as a call it cost the 10k grid 2% (C48).
#[inline(always)]
fn wraps(tree: &Tree, i: u32) -> bool {
    if !tree.any_wrap {
        return false;
    }
    let s = &tree.specs[i as usize].layout;
    s.wrap && s.dir == Dir::Row && !s.scroll_x && !is_table_row(tree, i)
}

/// Whether `i` is a row of a table: an in-flow `Row` container child of
/// a table. Anything else straight under the table — a heading text
/// beside the rows, a `column` section wrapping a heading over a row, a
/// nested table — has no cells and keeps its own width, and its children
/// are its own. Gated on the tree-level flag first, as
/// [`is_float`] is. The rules (`emit_rules`) ask it too, so a table's
/// grid is drawn over the rows its columns were laid across.
#[inline]
pub(crate) fn is_table_row(tree: &Tree, i: u32) -> bool {
    if !tree.any_table {
        return false;
    }
    let p = tree.parent[i as usize];
    p != NIL
        && tree.specs[p as usize].layout.is_table()
        && row_shaped(
            &tree.specs[i as usize].layout,
            matches!(tree.content[i as usize], NodeContent::Container),
        )
}

/// The half of [`is_table_row`] a node answers alone: an in-flow `Row`
/// container (`container` false for a text, an image, any leaf). A
/// departing table's ghost has no tree to ask, and asks this of the
/// children it copied.
#[inline]
pub(crate) fn row_shaped(spec: &crate::spec::LayoutSpec, container: bool) -> bool {
    spec.dir == Dir::Row && container && spec.float.is_none()
}

/// Whether `i` is a cell of a table: an in-flow child of a table row.
#[inline]
fn is_table_cell(tree: &Tree, i: u32) -> bool {
    if !tree.any_table {
        return false;
    }
    let p = tree.parent[i as usize];
    p != NIL && is_table_row(tree, p) && !is_float(tree, i)
}

/// One column of a table, as its cells declared it.
#[derive(Clone, Copy, Debug, Default)]
struct Col {
    /// The widest cell's fitted width: a `Fixed` cell's px, a `Fit`
    /// cell's content, a `Grow` or `Percent` cell's floor.
    fit: f32,
    /// The largest `Grow` factor among the cells, 0 for none: the column
    /// grows with the table when any cell asked to.
    grow: f32,
    /// The largest `Percent` among the cells, 0 for none; read only when
    /// nothing grows.
    pct: f32,
    /// The largest size expression among the cells, in px
    /// of the room the columns are laid across: the column is the larger
    /// of it and `pct` of that room. Each cell's is resolved and the
    /// largest kept, as `pct` keeps the largest percentage — two
    /// expressions cannot be compared until they are numbers, and the
    /// first row's once stood for the column. In the fit pass, before
    /// there is a room, only whether there is one (`Some(0.0)`).
    calc: Option<f32>,
    /// Whether any cell is `Fixed`: a fixed column is never shrunk.
    fixed: bool,
    /// The strictest clamps its cells declared: the largest floor and
    /// the smallest ceiling.
    min: f32,
    max: f32,
    /// The width resolved for it.
    w: f32,
}

impl Col {
    fn clamp(&self, w: f32) -> f32 {
        w.clamp(self.min, self.max.max(self.min))
    }
}

/// The columns of table `i`, read off its cells' current widths and
/// specs: the nth in-flow child of each in-flow row is a cell of column
/// n, and a row with fewer cells fills the first columns. A text cell
/// has no spec sizing and reads as `Fit`. `room` is what a calc cell
/// resolves against — `None` in the fit pass, which reads no calc.
fn table_columns(tree: &Tree, i: u32, room: Option<f32>) -> Vec<Col> {
    let mut cols: Vec<Col> = Vec::new();
    for row in tree.children(i) {
        if !is_table_row(tree, row) {
            continue;
        }
        let mut j = 0usize;
        for cell in tree.children(row) {
            if is_float(tree, cell) {
                continue;
            }
            if j == cols.len() {
                cols.push(Col {
                    max: f32::INFINITY,
                    ..Col::default()
                });
            }
            let col = &mut cols[j];
            let spec = tree.specs[cell as usize].layout;
            col.fit = col.fit.max(tree.size[cell as usize].w);
            match child_sizing(tree, cell, AxisSel::Width) {
                Sizing::Grow(f) => col.grow = col.grow.max(f.max(0.0)),
                Sizing::Percent(p) => col.pct = col.pct.max(p),
                Sizing::Calc(c) => {
                    let px = room.map_or(0.0, |r| c.resolve(r));
                    col.calc = Some(col.calc.map_or(px, |w| w.max(px)));
                }
                Sizing::Fixed(_) => col.fixed = true,
                Sizing::Fit => {}
            }
            if !matches!(tree.content[cell as usize], NodeContent::Text(_)) {
                col.min = col.min.max(spec.min_w.resolved());
                col.max = col.max.min(spec.max_w_px());
            }
            j += 1;
        }
    }
    cols
}

/// The width row `row` needs for `cols` — its cells at the columns'
/// widths, the gaps between them and its padding — which is what a
/// `Fit` row of a table is.
fn table_row_fit(tree: &Tree, row: u32, cols: &[Col]) -> f32 {
    let spec = tree.specs[row as usize].layout;
    let mut w = 0.0f32;
    let mut n = 0u32;
    for cell in tree.children(row) {
        if is_float(tree, cell) {
            continue;
        }
        w += cols.get(n as usize).map_or(0.0, |c| c.w);
        n += 1;
    }
    if n > 1 {
        w += spec.gap * (n - 1) as f32;
    }
    w + spec.padding.x()
}

/// Writes each column's width into its cells and sizes the rows to them.
/// A text cell takes the width too — `fit_heights` keeps it.
///
/// Which rows: in pass 1 (`fitting`) every row but a `Fixed` one — the
/// `grow` and percent rows included, whose own pass-1 width is 0 — so
/// the table's fit width, read next, is its columns' and a `Fit` table
/// of `grow` rows is the aligned list and not nothing. Pass 2 sizes them
/// for good, and the fit is only the number the table reads.
/// In pass 2 the `Fit` rows, and — when the table scrolls x — every row
/// widened to its columns if they overflow it, since a `grow` row is the
/// table's own width and `positions` measures a scroll container's
/// content from its children's boxes: without this the overflow the
/// table kept was clipped and `scroll_max.x` was 0. A
/// table that does not scroll leaves its rows' boxes alone, as any row
/// is left when fixed children overflow it.
fn table_apply(tree: &mut Tree, i: u32, cols: &[Col], fitting: bool) {
    let scrolls = tree.specs[i as usize].layout.scroll_x;
    let mut row = tree.first_child[i as usize];
    while row != NIL {
        if is_table_row(tree, row) {
            let mut j = 0usize;
            let mut cell = tree.first_child[row as usize];
            while cell != NIL {
                if !is_float(tree, cell) {
                    tree.size[cell as usize].w = cols[j].w;
                    j += 1;
                }
                cell = tree.next_sibling[cell as usize];
            }
            let spec = tree.specs[row as usize].layout;
            match spec.width {
                Sizing::Fit => {
                    let fit = table_row_fit(tree, row, cols);
                    tree.size[row as usize].w = spec.clamp_w(fit);
                }
                Sizing::Fixed(_) => {}
                Sizing::Grow(_) | Sizing::Percent(_) | Sizing::Calc(_) if fitting => {
                    let fit = table_row_fit(tree, row, cols);
                    tree.size[row as usize].w = spec.clamp_w(fit);
                }
                Sizing::Grow(_) | Sizing::Percent(_) | Sizing::Calc(_) => {}
            }
            if scrolls && !fitting {
                let fit = table_row_fit(tree, row, cols);
                let w = &mut tree.size[row as usize].w;
                *w = w.max(fit);
            }
        }
        row = tree.next_sibling[row as usize];
    }
}

/// Pass 1's table step: every column at its fit — the widest cell — and
/// the rows fitted to that, so the table's own fit width (read next, by
/// the caller) is the aligned one. A growing column sits at its floor
/// here, as a grow child of any row does.
fn table_fit(tree: &mut Tree, i: u32) {
    let mut cols = table_columns(tree, i, None);
    for col in &mut cols {
        col.w = col.clamp(col.fit);
    }
    table_apply(tree, i, &cols, true);
}

/// Pass 2's table step, at the table node, before its rows: the columns
/// resolved once against the widest row's content — `Percent` columns
/// take their cut, `Fixed` and `Fit` ones sit at their fit, `Grow`
/// columns split what is left in the freeze loop `distribute_run` runs
/// (a clamped column is frozen and the rest re-share) — and, when the
/// fits alone overflow the row and the table does not scroll x, the
/// `Fit` columns compressed toward their floors largest first, as
/// `shrink_axis` compresses a row's children. Then written into every
/// cell, so the rows have nothing left to distribute.
fn table_resolve(tree: &mut Tree, i: u32) {
    // How many columns: the most cells any row has, as `table_columns`
    // counts them — known before the columns are read, since they read
    // the clamps resolved just below.
    let n = tree
        .children(i)
        .filter(|&row| is_table_row(tree, row))
        .map(|row| tree.children(row).filter(|&c| !is_float(tree, c)).count())
        .max()
        .unwrap_or(0);
    if n == 0 {
        return;
    }
    // The widest row's content: what the columns are laid across. Rows
    // are usually `grow`, and then this is the table's content box less
    // the row's own padding and gaps.
    let mut avail = 0.0f32;
    for row in tree.children(i) {
        if !is_table_row(tree, row) {
            continue;
        }
        let spec = tree.specs[row as usize].layout;
        let chrome = spec.padding.x() + spec.gap * (n as f32 - 1.0);
        avail = avail.max(tree.size[row as usize].w - chrome);
    }
    let avail = avail.max(0.0);
    // A cell's size-expression clamps, against the room its column's
    // width takes its cut of: a `Percent` or calc width resolves against
    // `avail` below, and in a plain row a child's width and its clamps
    // share one room, so a cell's do too — `maxWidth "50%"` and `width
    // "50%"` beside it are one number. They once took the row's content
    // with the gaps in, and the clamp came out wider than the width. The
    // rows are wide by now, and the columns read the clamps next. A float
    // in a row is no cell: its clamps are its anchor's, in `distribute_axis`.
    if tree.any_calc_bound {
        for row in tree.children(i).collect::<Vec<_>>() {
            if !is_table_row(tree, row) {
                continue;
            }
            for cell in tree.children(row).collect::<Vec<_>>() {
                if !is_float(tree, cell) {
                    resolve_bounds(tree, cell, AxisSel::Width, avail);
                }
            }
        }
    }
    let mut cols = table_columns(tree, i, Some(avail));
    debug_assert_eq!(cols.len(), n);
    let mut used = 0.0f32;
    let mut grow_total = 0.0f32;
    for col in &mut cols {
        if col.grow > 0.0 {
            grow_total += col.grow;
            col.w = col.clamp(0.0);
        } else if col.pct > 0.0 || col.calc.is_some() {
            let calc = col.calc.unwrap_or(0.0);
            col.w = col.clamp((avail * col.pct).max(calc));
            used += col.w;
        } else {
            col.w = col.clamp(col.fit);
            used += col.w;
        }
    }
    if grow_total > 0.0 {
        let mut frozen = vec![false; cols.len()];
        loop {
            let remain = (avail - used).max(0.0);
            let mut froze = false;
            for (j, col) in cols.iter_mut().enumerate() {
                if col.grow <= 0.0 || frozen[j] {
                    continue;
                }
                let share = remain * col.grow / grow_total;
                col.w = col.clamp(share);
                if (col.w - share).abs() > 0.01 {
                    frozen[j] = true;
                    used += col.w;
                    grow_total -= col.grow;
                    froze = true;
                }
            }
            if !froze || grow_total <= 0.0 {
                break;
            }
        }
    }
    // The shrink: the fit columns pay the overflow, largest first, down
    // to their floors — never a fixed, a percent or a growing one.
    let total: f32 = cols.iter().map(|c| c.w).sum();
    let mut deficit = total - avail;
    if deficit > 0.5 && !tree.specs[i as usize].layout.scroll_x {
        let shrinkable = |c: &Col| !c.fixed && c.grow <= 0.0 && c.pct <= 0.0 && c.calc.is_none();
        let mut guard = 0;
        while deficit > 0.5 && guard < 128 {
            guard += 1;
            let mut largest = f32::NEG_INFINITY;
            let mut second = 0.0f32;
            let mut count = 0u32;
            for c in cols.iter().filter(|c| shrinkable(c) && c.w > c.min + 0.01) {
                if c.w > largest + 0.01 {
                    second = if largest.is_finite() {
                        largest.max(second)
                    } else {
                        second
                    };
                    largest = c.w;
                    count = 1;
                } else if c.w > largest - 0.01 {
                    count += 1;
                } else if c.w > second {
                    second = c.w;
                }
            }
            if count == 0 {
                break;
            }
            let target = (largest - deficit / count as f32).max(second).max(0.0);
            let mut shrunk_any = false;
            for c in cols.iter_mut().filter(|c| shrinkable(c)) {
                if c.w > largest - 0.01 {
                    let new = target.max(c.min);
                    if new < c.w {
                        deficit -= c.w - new;
                        c.w = new;
                        shrunk_any = true;
                    }
                }
            }
            if !shrunk_any {
                break;
            }
        }
    }
    table_apply(tree, i, &cols, false);
}

/// The first in-flow child of `i` (`NIL` when it has none).
fn first_in_flow(tree: &Tree, i: u32) -> u32 {
    let mut c = tree.first_child[i as usize];
    while c != NIL && is_float(tree, c) {
        c = tree.next_sibling[c as usize];
    }
    c
}

/// The first in-flow child after `c`'s wrap line (`NIL` at the end). Line
/// numbers only ever go up in child order, so one line's in-flow children
/// are a contiguous sibling run and `[c, line_end(c))` is the whole line.
fn line_end(tree: &Tree, c: u32) -> u32 {
    let l = tree.line[c as usize];
    let mut n = tree.next_sibling[c as usize];
    while n != NIL && (is_float(tree, n) || tree.line[n as usize] == l) {
        n = tree.next_sibling[n as usize];
    }
    n
}

/// Extents of the line `[c, end)`, in one walk: `(main, cross)`.
///
/// Main is its children plus the gaps between them. Cross is its tallest
/// child that has a cross size of its own — Grow and Percent children are
/// skipped by *sizing*, not by their current number, because they are
/// sized against the extent this returns and reading them back would make
/// a line's height depend on whether pass 4 had run yet. Skipping them
/// measures the same thing in every pass, and matches an unwrapped row,
/// where a grow child contributes nothing to a fit height either.
/// Wrapping is rows-only, so main is width and cross is height.
fn line_extents(tree: &Tree, c: u32, end: u32, gap: f32) -> (f32, f32) {
    let baseline = tree.any_baseline && c != NIL && baseline_row(tree, tree.parent[c as usize]);
    let mut main = 0.0f32;
    let mut cross = 0.0f32;
    let mut n = 0u32;
    let mut k = c;
    while k != end && k != NIL {
        if !is_float(tree, k) {
            let size = tree.size[k as usize];
            main += size.w;
            if !matches!(
                child_sizing(tree, k, AxisSel::Height),
                Sizing::Grow(_) | Sizing::Percent(_) | Sizing::Calc(_)
            ) {
                cross = cross.max(size.h);
            }
            n += 1;
        }
        k = tree.next_sibling[k as usize];
    }
    if n > 1 {
        main += gap * (n - 1) as f32;
    }
    if baseline {
        // Aligned on one line, the children reach from the highest top
        // to the lowest bottom, which is more than the tallest of them.
        let (above, below, _) = line_baseline(tree, c, end);
        cross = cross.max(above + below);
    }
    (main, cross)
}

/// A wrapping row's lines in one walk: how many, their stacked cross extent
/// with the cross gaps between them, and the widest line's main extent.
fn wrap_measure(tree: &Tree, i: u32) -> (u32, f32, f32) {
    let spec = tree.specs[i as usize].layout;
    let mut lines = 0u32;
    let mut stacked = 0.0f32;
    let mut widest = 0.0f32;
    let mut c = first_in_flow(tree, i);
    while c != NIL {
        let end = line_end(tree, c);
        let (main, cross) = line_extents(tree, c, end, spec.gap);
        stacked += cross;
        widest = widest.max(main);
        lines += 1;
        c = end;
    }
    if lines > 1 {
        stacked += spec.cross_gap * (lines - 1) as f32;
    }
    (lines, stacked, widest)
}

/// Cross space every line gains beyond its content extent: the container's
/// leftover, shared equally — CSS's `align-content: stretch`, and the
/// reason a wrapping row that happens to fit on one line lays out exactly
/// like an unwrapped one. Zero for a Fit height, whose lines already fill
/// it by construction, and zero when the lines overflow.
fn line_stretch(lines: u32, stacked: f32, cross_content: f32) -> f32 {
    if lines == 0 {
        0.0
    } else {
        (cross_content - stacked).max(0.0) / lines as f32
    }
}

/// Greedy main-axis line breaking, in child order: a child that no longer
/// fits the content box starts the next line, and a child too wide to fit
/// on its own gets a line to itself (and is then the shrink pass's
/// problem). Grow children break on whatever pass 1 left them — zero, or
/// their `min_w` — since a grow child has no size of its own until a line
/// is chosen for it; it then fills what is left of the line it landed on.
fn break_lines(tree: &mut Tree, i: u32, content: f32, gap: f32) {
    let mut line = 0u32;
    let mut used = 0.0f32;
    let mut n = 0u32;
    let mut c = tree.first_child[i as usize];
    while c != NIL {
        if !is_float(tree, c) {
            let base = tree.size[c as usize].w;
            let needed = if n > 0 { gap + base } else { base };
            if n > 0 && used + needed > content + 0.01 {
                line += 1;
                used = base;
                n = 1;
            } else {
                used += needed;
                n += 1;
            }
            tree.line[c as usize] = line;
        }
        c = tree.next_sibling[c as usize];
    }
}

/// What the solver asks about text, editors, images and cell grids: the
/// sizes it cannot compute from specs alone. The core implements it over
/// its text system; a test may implement it with fixed numbers.
pub trait TextMeasure {
    /// Unwrapped preferred size.
    fn intrinsic(&mut self, id: crate::tree::TextId) -> Size;
    /// Size when wrapped to `max_w` logical pixels.
    fn wrapped(&mut self, id: crate::tree::TextId, max_w: f32) -> Size;
    /// The widest stretch of the text no break falls inside, logical px
    /// — CSS's min-content: the longest word, a whole unwrapped line.
    /// Asked only on a frame with a share of the room; 0, the default, is
    /// a text that rewraps to any width.
    fn min_content(&mut self, _id: crate::tree::TextId) -> f32 {
        0.0
    }
    /// Unwrapped content size of an editable text node.
    fn edit_intrinsic(&mut self, _key: crate::key::Key) -> Size {
        Size::ZERO
    }
    /// Content size of an editable text node wrapped to `max_w`.
    fn edit_wrapped(&mut self, _key: crate::key::Key, _max_w: f32) -> Size {
        Size::ZERO
    }
    /// The first line's baseline of a text last wrapped by `wrapped`,
    /// logical px below its top: what `crossAlign: baseline` lines up.
    /// Asked only on a frame with a baseline row. `NaN` = not known, and
    /// the text aligns by its bottom edge.
    fn baseline(&mut self, _id: crate::tree::TextId) -> f32 {
        f32::NAN
    }
    /// An editor's first baseline, below the top of its text (its box's
    /// padding is added by the caller). `NaN` = not known.
    fn edit_baseline(&mut self, _key: crate::key::Key) -> f32 {
        f32::NAN
    }
    /// Pixel dimensions of a registered image (ZERO when unknown).
    fn image_size(&mut self, _id: crate::resources::ImageId) -> Size {
        Size::ZERO
    }
    /// The laid-out size of a cell grid (`rows × cols` cells).
    fn cells_size(&mut self, _id: crate::cells::CellsId) -> Size {
        Size::ZERO
    }
}

/// Lays `tree` out into `viewport` logical px: sizes and positions every
/// node, breaks wrapping rows into lines, resolves tables and floats, and
/// clamps the scroll offsets in `scroll`. `Core::finish_frame` calls it
/// once per frame; it is public for a custom runner or a test that drives
/// the solver directly.
pub fn compute(
    tree: &mut Tree,
    text: &mut dyn TextMeasure,
    scroll: &mut ScrollStore,
    viewport: Size,
    // Physical pixels per logical one: `positions` snaps a scroll offset to them.
    scale: f32,
) {
    if tree.is_empty() {
        return;
    }
    // A `Min::FIT` floor is written back into the spec as the number it
    // resolved to (`fit_widths`, `fit_heights`), and a size-expression
    // clamp as the px it came to against its room (`resolve_bounds`);
    // a node-anchored float is laid out again in the sixth pass, and
    // would read what the first run left as declared numbers. For a fit
    // floor that is one measured before the float had a width — a
    // paragraph folded into a column of one word. For a calc clamp it is
    // one resolved against a room of 0: the first run meets such a float
    // before its anchor is placed and gives it no room, so a `maxWidth
    // "50%"` on it — or a `maxWidth "100%"` on a child of it, resolved
    // against the float's own 0 — came out 0 and the sixth pass, finding
    // no expression left to resolve, kept it. Every clamp
    // in those subtrees that layout writes over is remembered here, as
    // declared, and put back before the re-run.
    let declared = if tree.any_node_float {
        declared_clamps(tree)
    } else {
        Vec::new()
    };
    fit_widths(tree, text, 0..tree.len());
    grow_widths(tree, text, viewport);
    fit_heights(tree, text, 0..tree.len());
    grow_heights(tree, text, viewport);
    positions(tree, scroll, viewport, scale, 0..tree.len());
    if tree.any_node_float {
        anchored(tree, text, scroll, viewport, scale, &declared);
    }
}

/// The four clamps of a node as its spec declared them, for one layout
/// writes over (a fit floor, a size expression): `(node, min_w, max_w,
/// min_h, max_h)`.
type Declared = (usize, Min, f32, Min, f32);

/// The clamps of every node inside a node-anchored float's subtree that
/// layout resolves in place — the ones the sixth pass must see again as
/// declared. Read off the raw numbers, a negative
/// being what both forms are, so no node pays a table lookup.
fn declared_clamps(tree: &Tree) -> Vec<Declared> {
    let mut out = Vec::new();
    for (c, end, _) in node_floats(tree) {
        for i in c..end {
            let l = &tree.specs[i].layout;
            if l.min_w.deferred() || l.min_h.deferred() || l.max_w < 0.0 || l.max_h < 0.0 {
                out.push((i, l.min_w, l.max_w, l.min_h, l.max_h));
            }
        }
    }
    out
}

/// The sixth pass: every float anchored to a node by key, laid out again
/// against that node's final rect. A subtree is a contiguous index range
/// in preorder — every descendant's parent index is at or after the
/// root's — so the five passes run over the range alone: the float's own
/// size is resolved against the anchor the way a `Parent` float's is
/// against its parent, and the rest is what the passes always do. An
/// anchor the frame does not have leaves the float at zero size, which
/// paints nothing and takes no input; a caller that built the content
/// without its body has nothing to show it in.
fn anchored(
    tree: &mut Tree,
    text: &mut dyn TextMeasure,
    scroll: &mut ScrollStore,
    viewport: Size,
    scale: f32,
    declared: &[Declared],
) {
    for (c, end, key) in node_floats(tree) {
        let Some(a) = tree.index_of(key) else {
            tree.size[c] = Size::default();
            continue;
        };
        let anchor = Rect::from_pos_size(tree.pos[a], tree.size[a]);
        // The fit floors and size-expression clamps declared in this
        // subtree, as declared again.
        for &(i, min_w, max_w, min_h, max_h) in
            declared.iter().filter(|(i, ..)| (c..end).contains(i))
        {
            let l = &mut tree.specs[i].layout;
            (l.min_w, l.max_w, l.min_h, l.max_h) = (min_w, max_w, min_h, max_h);
        }
        // The root's spec is read *after* each fit pass, which is where
        // a `Min::FIT` floor of its own — just declared again above —
        // resolves to its number; a copy taken before it clamped with a
        // floor of 0 and the float lost its own floor.
        fit_widths(tree, text, c..end);
        if tree.any_calc_bound {
            resolve_bounds(tree, c as u32, AxisSel::Width, anchor.w);
        }
        let spec = tree.specs[c].layout;
        tree.size[c].w = spec.clamp_w(match spec.width {
            Sizing::Grow(_) => anchor.w,
            s => of_room(s, anchor.w).unwrap_or(tree.size[c].w),
        });
        for i in c..end {
            distribute_axis(tree, text, i as u32, AxisSel::Width, viewport);
        }
        fit_heights(tree, text, c..end);
        if tree.any_calc_bound {
            resolve_bounds(tree, c as u32, AxisSel::Height, anchor.h);
        }
        let spec = tree.specs[c].layout;
        tree.size[c].h = spec.clamp_h(match spec.height {
            Sizing::Grow(_) => anchor.h,
            s => of_room(s, anchor.h).unwrap_or(tree.size[c].h),
        });
        for i in c..end {
            distribute_axis(tree, text, i as u32, AxisSel::Height, viewport);
        }
        place_anchored(tree, c, anchor, viewport);
        positions(tree, scroll, viewport, scale, c..end);
    }
}

/// `(root, end, anchor key)` of every node-anchored float, in tree
/// order, each subtree the index range `root..end`.
fn node_floats(tree: &Tree) -> Vec<(usize, usize, crate::key::Key)> {
    let mut out = Vec::new();
    let mut c = 0usize;
    while c < tree.len() {
        if let Some(crate::spec::FloatConfig {
            anchor: FloatAnchor::Node(key),
            ..
        }) = tree.specs[c].layout.float
        {
            let mut end = c + 1;
            while end < tree.len() && (tree.parent[end] as usize) >= c {
                end += 1;
            }
            out.push((c, end, key));
            c = end;
        } else {
            c += 1;
        }
    }
    out
}

/// Attaches the sized float `c` to `anchor`, its config's points — kept
/// on screen as a parent-anchored float is when it declares `fit`, which
/// a leaf's tooltip anchored to the leaf does (backlog RG113).
fn place_anchored(tree: &mut Tree, c: usize, anchor: Rect, viewport: Size) {
    let Some(cfg) = tree.specs[c].layout.float else {
        return;
    };
    let vp = float_viewport(tree, c as u32, viewport);
    tree.pos[c] = attach_fitted(&cfg, anchor, tree.size[c], vp, true);
}

/// Pass 5 again, with the sizes kept: what a scroll that moved a node
/// re-runs. The node-anchored floats follow their anchors.
pub(crate) fn reposition(tree: &mut Tree, scroll: &mut ScrollStore, viewport: Size, scale: f32) {
    positions(tree, scroll, viewport, scale, 0..tree.len());
    if tree.any_node_float {
        for (c, end, key) in node_floats(tree) {
            if let Some(a) = tree.index_of(key) {
                let anchor = Rect::from_pos_size(tree.pos[a], tree.size[a]);
                place_anchored(tree, c, anchor, viewport);
                positions(tree, scroll, viewport, scale, c..end);
            }
        }
    }
}

/// The fit width of `i`, a non-text node: what its content wants on its
/// own. Read for a `Fit` width, and for a `Min::FIT` floor under any other
/// sizing — a `Grow` tab that must never be narrower than its label.
#[inline(always)]
fn fit_width(tree: &Tree, i: usize, text: &mut dyn TextMeasure) -> f32 {
    let spec = &tree.specs[i].layout;
    match tree.content[i] {
        NodeContent::Edit(key) => text.edit_intrinsic(key).w + spec.padding.x(),
        // Image pixels as logical px (1:1 at scale 1).
        NodeContent::Image(id, _) => text.image_size(id).w,
        NodeContent::Cells(id) => text.cells_size(id).w + spec.padding.x(),
        _ => {
            let mut w = 0.0f32;
            let mut n = 0u32;
            for c in tree.children(i as u32) {
                if is_float(tree, c) {
                    continue;
                }
                let cw = tree.size[c as usize].w;
                if spec.dir == Dir::Row {
                    w += cw;
                } else {
                    w = w.max(cw);
                }
                n += 1;
            }
            if spec.dir == Dir::Row && n > 1 {
                w += spec.gap * (n - 1) as f32;
            }
            w + spec.padding.x()
        }
    }
}

fn fit_widths(tree: &mut Tree, text: &mut dyn TextMeasure, range: std::ops::Range<usize>) {
    for i in range.rev() {
        if let NodeContent::Text(tid) = tree.content[i] {
            tree.size[i].w = text.intrinsic(tid).w;
            continue;
        }
        // A table's rows and cells are fitted by now (children first):
        // align them, so the fit read next is the aligned one.
        if tree.any_table && tree.specs[i].layout.is_table() {
            table_fit(tree, i as u32);
        }
        let width = tree.specs[i].layout.width;
        let min_fit = tree.specs[i].layout.min_w.is_fit();
        // Measured once for both uses: the Fit sizing, and the Fit floor.
        let fit = if min_fit || width == Sizing::Fit {
            fit_width(tree, i, text)
        } else {
            0.0
        };
        // A `Min::FIT` floor resolves here, once, to the number every later
        // clamp on this axis reads — written back into the spec so
        // `set_axis_clamped` in the grow pass and `break_lines` need no
        // second form.
        if min_fit {
            tree.specs[i].layout.min_w = Min::px(fit);
        }
        let spec = &tree.specs[i].layout;
        tree.size[i].w = spec.clamp_w(match width {
            Sizing::Fixed(px) => px,
            // Resolved against the parent later; contributes nothing to fit
            // beyond its own floor.
            Sizing::Grow(_) | Sizing::Percent(_) | Sizing::Calc(_) => 0.0,
            Sizing::Fit => spec.aspect_width().unwrap_or(fit),
        });
    }
}

/// Node `i`'s min-content on `axis` — CSS's: the widest (or tallest)
/// thing in it that cannot wrap, its padding on top — asked by a shrink
/// that gives CSS's way, for the children of a run that overflowed and
/// no other node, so a frame whose runs all fit measures nothing.
/// Across, a text is its longest word
/// (`TextMeasure::min_content`); down, it is its lines at the width it
/// has, which the fit pass of that axis measured, as an image's and a
/// grid's size is theirs. A box scrolling that axis, or clipping, needs
/// nothing of what it holds — CSS's automatic minimum of a box whose
/// overflow is not visible. Asked too by the shrink of a column of fit
/// children, down, for its children alone.
fn min_content(tree: &Tree, text: &mut dyn TextMeasure, i: usize, axis: AxisSel) -> f32 {
    let spec = &tree.specs[i].layout;
    let (pad, scrolls) = match axis {
        AxisSel::Width => (spec.padding.x(), spec.scroll_x),
        AxisSel::Height => (spec.padding.y(), spec.scroll_y),
    };
    let own = match (tree.content[i], axis) {
        (NodeContent::Text(tid), AxisSel::Width) => return text.min_content(tid),
        (
            NodeContent::Text(_) | NodeContent::Image(..) | NodeContent::Cells(_),
            AxisSel::Height,
        ) => {
            return tree.size[i].h;
        }
        (NodeContent::Edit(_), AxisSel::Width) => 0.0,
        (NodeContent::Edit(key), AxisSel::Height) => {
            text.edit_wrapped(key, (tree.size[i].w - spec.padding.x()).max(0.0))
                .h
        }
        (NodeContent::Image(id, _), AxisSel::Width) => text.image_size(id).w,
        (NodeContent::Cells(id), AxisSel::Width) => text.cells_size(id).w,
        _ if scrolls || spec.clip => 0.0,
        _ => children_min_content(tree, text, i, axis),
    };
    own + pad
}

/// What node `i`'s in-flow children need on `axis` at the least — CSS's
/// min-content of a box, less its padding: each child's
/// contribution is its fixed size or its own min-content, held to its
/// clamps, and the contributions add up with the gaps along a main axis
/// that does not wrap, where they cannot be put side by side any other
/// way, and are the widest across it or when each may take a line of its
/// own. Down a wrapping row, whose lines the width pass has broken, they
/// are each line's tallest stacked with the cross gaps, as the row's fit
/// height is.
fn children_min_content(tree: &Tree, text: &mut dyn TextMeasure, i: usize, axis: AxisSel) -> f32 {
    let spec = &tree.specs[i].layout;
    let main = (spec.dir == Dir::Row) == (axis == AxisSel::Width);
    let adds = main && !(spec.wrap && axis == AxisSel::Width);
    let lines = axis == AxisSel::Height && wraps(tree, i as u32);
    let (mut total, mut n) = (0.0f32, 0u32);
    // The line being read down a wrapping row, and its tallest so far.
    let (mut line, mut tallest, mut stacked) = (None, 0.0f32, 0u32);
    let mut c = tree.first_child[i];
    while c != NIL {
        if !is_float(tree, c) {
            let l = &tree.specs[c as usize].layout;
            let (sizing, min, max) = match axis {
                AxisSel::Width => (l.width, l.min_w, l.max_w_px()),
                AxisSel::Height => (l.height, l.min_h, l.max_h_px()),
            };
            let own = match sizing {
                Sizing::Fixed(px) => px,
                _ => min_content(tree, text, c as usize, axis),
            };
            let v = own.min(max).max(min.resolved());
            if lines {
                let l = tree.line[c as usize];
                if line != Some(l) {
                    total += tallest;
                    (line, tallest) = (Some(l), 0.0);
                    stacked += 1;
                }
                tallest = tallest.max(v);
            } else if adds {
                total += v;
            } else {
                total = total.max(v);
            }
            n += 1;
        }
        c = tree.next_sibling[c as usize];
    }
    if lines {
        total += tallest + spec.cross_gap * stacked.saturating_sub(1) as f32;
    } else if adds && n > 1 {
        total += spec.gap * (n - 1) as f32;
    }
    total
}

/// The fit height of `i`, a non-text node, against its final width:
/// `fit_width`'s mirror, read for a `Fit` height and for a `Min::FIT`
/// floor. An editor's is its wrapped extent, which the caller has already
/// measured (emission and input must share one line layout, whatever the
/// sizing) and passes in as `edit`.
#[inline(always)]
fn fit_height(tree: &Tree, i: usize, text: &mut dyn TextMeasure, edit: Size) -> f32 {
    let spec = &tree.specs[i].layout;
    match tree.content[i] {
        NodeContent::Edit(_) => edit.h + spec.padding.y(),
        NodeContent::Cells(id) => text.cells_size(id).h + spec.padding.y(),
        // Width is final by now: a Fit height preserves the aspect. A
        // cell's width is its column's, which the image did not ask for:
        // its height is its aspect at the width its own sizing gave it —
        // a 16 px icon in a 200 px column is a 200 x 16 box, not a 200 x
        // 200 one — and how the pixels meet the wider box
        // is the image's `fit` row.
        NodeContent::Image(id, _) => {
            let intrinsic = text.image_size(id);
            if intrinsic.w <= 0.0 {
                return 0.0;
            }
            let w = if is_table_cell(tree, i as u32) {
                match spec.width {
                    Sizing::Fixed(px) => spec.clamp_w(px),
                    Sizing::Fit => spec.clamp_w(intrinsic.w),
                    Sizing::Grow(_) | Sizing::Percent(_) | Sizing::Calc(_) => tree.size[i].w,
                }
            } else {
                tree.size[i].w
            };
            intrinsic.h * w / intrinsic.w
        }
        // A wrapping row is as tall as its lines stacked: the lines were
        // chosen in pass 2, against a width that is already final.
        _ if wraps(tree, i as u32) => wrap_measure(tree, i as u32).1 + spec.padding.y(),
        // One line, as tall as its children reach once their baselines
        // line up: `line_extents` measures exactly that.
        _ if tree.any_baseline && baseline_row(tree, i as u32) => {
            let f = first_in_flow(tree, i as u32);
            line_extents(tree, f, NIL, spec.gap).1 + spec.padding.y()
        }
        _ => {
            let mut h = 0.0f32;
            let mut n = 0u32;
            for c in tree.children(i as u32) {
                if is_float(tree, c) {
                    continue;
                }
                let ch = tree.size[c as usize].h;
                if spec.dir == Dir::Column {
                    h += ch;
                } else {
                    h = h.max(ch);
                }
                n += 1;
            }
            if spec.dir == Dir::Column && n > 1 {
                h += spec.gap * (n - 1) as f32;
            }
            h + spec.padding.y()
        }
    }
}

fn fit_heights(tree: &mut Tree, text: &mut dyn TextMeasure, range: std::ops::Range<usize>) {
    if tree.any_baseline {
        // One per node, `NaN` where no text measured one; kept only on a
        // frame that has a baseline row.
        let n = tree.len();
        tree.baseline.resize(n, f32::NAN);
    }
    for i in range.rev() {
        if let NodeContent::Text(tid) = tree.content[i] {
            // Width is final by now: wrap to it.
            let wrapped = text.wrapped(tid, tree.size[i].w.max(0.0));
            // The wrapped measurement is authoritative for both axes (a long
            // unbroken word may still exceed the clamp; report it truthfully)
            // — except that a text which is a table's cell keeps the column
            // width pass 2 gave it, or the cells after it would close up.
            if is_table_cell(tree, i as u32) {
                tree.size[i].h = wrapped.h;
                tree.size[i].w = tree.size[i].w.max(wrapped.w);
            } else {
                tree.size[i] = wrapped;
            }
            if tree.any_baseline {
                tree.baseline[i] = text.baseline(tid);
            }
            continue;
        }
        // Always wrap an editor to the final content width so emission and
        // input hit the same line layout, whatever the height sizing is.
        let edit = if let NodeContent::Edit(key) = tree.content[i] {
            let inner = (tree.size[i].w - tree.specs[i].layout.padding.x()).max(0.0);
            let wrapped = text.edit_wrapped(key, inner);
            if tree.any_baseline {
                tree.baseline[i] = tree.specs[i].layout.padding.t + text.edit_baseline(key);
            }
            wrapped
        } else {
            Size::default()
        };
        let height = tree.specs[i].layout.height;
        let min_fit = tree.specs[i].layout.min_h.is_fit();
        // Same resolution as `fit_widths`: measured once, the floor written
        // back as a number.
        let fit = if min_fit || height == Sizing::Fit {
            fit_height(tree, i, text, edit)
        } else {
            0.0
        };
        if min_fit {
            tree.specs[i].layout.min_h = Min::px(fit);
        }
        let spec = &tree.specs[i].layout;
        tree.size[i].h = spec.clamp_h(match height {
            Sizing::Fixed(px) => px,
            Sizing::Grow(_) | Sizing::Percent(_) | Sizing::Calc(_) => 0.0,
            // Width is final by now: a declared ratio reads it, as an
            // image's pixels do below it.
            Sizing::Fit if spec.aspect_height() => tree.size[i].w / spec.aspect,
            Sizing::Fit => fit,
        });
    }
}

fn grow_widths(tree: &mut Tree, text: &mut dyn TextMeasure, viewport: Size) {
    for i in 0..tree.len() {
        if tree.parent[i] == NIL {
            if tree.any_calc_bound {
                resolve_bounds(tree, i as u32, AxisSel::Width, viewport.w);
            }
            let spec = tree.specs[i].layout;
            tree.size[i].w = spec.clamp_w(resolve_root(spec.width, tree.size[i].w, viewport.w));
        }
        distribute_axis(tree, text, i as u32, AxisSel::Width, viewport);
    }
}

fn grow_heights(tree: &mut Tree, text: &mut dyn TextMeasure, viewport: Size) {
    for i in 0..tree.len() {
        if tree.parent[i] == NIL {
            if tree.any_calc_bound {
                resolve_bounds(tree, i as u32, AxisSel::Height, viewport.h);
            }
            let spec = tree.specs[i].layout;
            tree.size[i].h = spec.clamp_h(resolve_root(spec.height, tree.size[i].h, viewport.h));
        }
        distribute_axis(tree, text, i as u32, AxisSel::Height, viewport);
    }
}

fn resolve_root(sizing: Sizing, fitted: f32, viewport: f32) -> f32 {
    match sizing {
        Sizing::Grow(_) => viewport,
        Sizing::Percent(p) => viewport * p,
        Sizing::Calc(c) => c.resolve(viewport),
        Sizing::Fixed(px) => px,
        Sizing::Fit => fitted,
    }
}

#[derive(Clone, Copy, PartialEq)]
enum AxisSel {
    Width,
    Height,
}

/// Resolves Grow/Percent children of `i` along the given axis, assuming `i`'s
/// own size on that axis is final.
fn distribute_axis(
    tree: &mut Tree,
    text: &mut dyn TextMeasure,
    i: u32,
    axis: AxisSel,
    viewport: Size,
) {
    let spec = tree.specs[i as usize].layout;
    let (own, pad) = match axis {
        AxisSel::Width => (tree.size[i as usize].w, spec.padding.x()),
        AxisSel::Height => (tree.size[i as usize].h, spec.padding.y()),
    };
    let content = (own - pad).max(0.0);
    let is_main = (spec.dir == Dir::Row) == (axis == AxisSel::Width);

    // Size-expression clamps against the content box, the
    // room a percentage takes its cut of, before anything below is sized
    // by them; a child whose size is its own — fixed, fit — is clamped
    // again here, since its fit pass ran with no such clamp. A float's
    // room is its anchor, below.
    if tree.any_calc_bound {
        let mut c = tree.first_child[i as usize];
        while c != NIL {
            if !is_float(tree, c) && resolve_bounds(tree, c, axis, content) {
                let now = get_axis(tree, c, axis);
                set_axis_clamped(tree, c, axis, now);
            }
            c = tree.next_sibling[c as usize];
        }
    }

    if is_main && axis == AxisSel::Width && is_table_row(tree, i) {
        // The cells were sized by the table (`table_resolve`), the same
        // in every row: nothing to grow, cut or shrink here.
    } else if is_main {
        // Percent takes its cut of the content box first: a wrap line
        // breaks on sizes that are already resolved against the container,
        // not against the line it is about to land on.
        // Noting on the way whether any child is such a share, the one
        // thing the shrink below needs to know before it picks a rule
        // — here, where every child is read anyway: a scan
        // of its own cost a column of a thousand fixed rows 2%.
        let mut share = false;
        let mut c = tree.first_child[i as usize];
        while c != NIL {
            if !is_float(tree, c)
                && let Some(px) = of_room(child_sizing(tree, c, axis), content)
            {
                set_axis_clamped(tree, c, axis, px);
                share = true;
            }
            c = tree.next_sibling[c as usize];
        }
        let scrolls = match axis {
            AxisSel::Width => spec.scroll_x,
            AxisSel::Height => spec.scroll_y,
        };
        if wraps(tree, i) {
            break_lines(tree, i, content, spec.gap);
            let mut c = first_in_flow(tree, i);
            while c != NIL {
                let end = line_end(tree, c);
                let line = tree.line[c as usize];
                // Each line is its own main-axis box: Grow splits what is
                // left of *its* line.
                let total = distribute_run(tree, c, end, axis, content, spec.gap);
                // Wrapping and shrinking answer the same overflow, and
                // wrapping answers it first: greedy breaking never puts a
                // second child on a line that is already full, so the only
                // line that can still overflow is one holding a single
                // child too wide for the box. Nothing can be broken off
                // that, which is exactly when shrinking is the remaining
                // answer — applied to that line alone, so a wide chip
                // compresses without dragging its neighbours on other
                // lines down with it.
                let deficit = total - content;
                if deficit > 0.5 {
                    shrink_axis(tree, text, i, axis, deficit, Some(line), share);
                }
                c = end;
            }
        } else {
            // Fixed/Fit keep their size, Percent has taken its cut, Grow
            // splits the rest.
            let total = distribute_run(
                tree,
                tree.first_child[i as usize],
                NIL,
                axis,
                content,
                spec.gap,
            );
            let deficit = total - content;
            if deficit > 0.5 && !scrolls {
                shrink_axis(tree, text, i, axis, deficit, None, share);
            }
        }
    } else if wraps(tree, i) {
        // Cross axis of a wrapping row: a Grow child fills *its line*, not
        // the container. The lines share the container's leftover equally
        // (see `line_stretch`), so with one line this is the branch below
        // exactly, and a row that happens not to wrap keeps its old layout.
        let (lines, stacked, _) = wrap_measure(tree, i);
        let stretch = line_stretch(lines, stacked, content);
        let mut c = first_in_flow(tree, i);
        while c != NIL {
            let end = line_end(tree, c);
            let extent = line_extents(tree, c, end, spec.gap).1 + stretch;
            let mut k = c;
            while k != end && k != NIL {
                if !is_float(tree, k) {
                    match child_sizing(tree, k, axis) {
                        Sizing::Grow(_) => set_axis_clamped(tree, k, axis, extent),
                        s => {
                            if let Some(px) = of_room(s, extent) {
                                set_axis_clamped(tree, k, axis, px);
                            }
                        }
                    }
                }
                k = tree.next_sibling[k as usize];
            }
            c = end;
        }
    } else {
        // Cross axis: Grow/Percent resolve against the content box directly.
        // A Fit box across a column is its content's width but no wider
        // than the box — CSS's `fit-content` — down to its declared min,
        // none undeclared, as a row's fit children give.
        // Pass 1 summed it bottom-up with no room in sight; held here,
        // before its own children are distributed, its run gives and its
        // text wraps. Not across a column that scrolls x, whose overflow
        // is the point, nor a width a ratio derives from the height; a
        // text has its own clamp, below; and a grid or an image is its
        // content's size, CSS's replaced element: a held box would not
        // take a column off the grid the app laid out, and would stretch
        // an image under a fixed height.
        let fits = axis == AxisSel::Width && !spec.scroll_x;
        let mut c = tree.first_child[i as usize];
        while c != NIL {
            if !is_float(tree, c) {
                match child_sizing(tree, c, axis) {
                    Sizing::Grow(_) => set_axis_clamped(tree, c, axis, content),
                    Sizing::Fit
                        if fits
                            && tree.size[c as usize].w > content
                            && !matches!(
                                tree.content[c as usize],
                                NodeContent::Text(_)
                                    | NodeContent::Cells(_)
                                    | NodeContent::Image(..)
                            )
                            && tree.specs[c as usize].layout.aspect_width().is_none() =>
                    {
                        set_axis_clamped(tree, c, axis, content)
                    }
                    s => {
                        if let Some(px) = of_room(s, content) {
                            set_axis_clamped(tree, c, axis, px);
                        }
                    }
                }
            }
            c = tree.next_sibling[c as usize];
        }
        // A table's rows are wide by now: lay its columns across them.
        if axis == AxisSel::Width && tree.any_table && spec.is_table() {
            table_resolve(tree, i);
        }
    }

    // Floating children size Grow/Percent against their anchor. A frame
    // with no floats skips the walk: it would read every child's spec to
    // find none.
    let mut c = if tree.any_float {
        tree.first_child[i as usize]
    } else {
        NIL
    };
    while c != NIL {
        if let Some(cfg) = tree.specs[c as usize].layout.float {
            let vp = float_viewport(tree, c, viewport);
            let anchor_dim = match (cfg.anchor, axis) {
                (FloatAnchor::Parent, AxisSel::Width) => tree.size[i as usize].w,
                (FloatAnchor::Parent, AxisSel::Height) => tree.size[i as usize].h,
                (FloatAnchor::Viewport, AxisSel::Width) => vp.w,
                (FloatAnchor::Viewport, AxisSel::Height) => vp.h,
                // Sized in the sixth pass, once the anchor is placed.
                (FloatAnchor::Node(_), _) => 0.0,
            };
            if tree.any_calc_bound && resolve_bounds(tree, c, axis, anchor_dim) {
                let now = get_axis(tree, c, axis);
                set_axis_clamped(tree, c, axis, now);
            }
            match child_sizing(tree, c, axis) {
                Sizing::Grow(_) => set_axis_clamped(tree, c, axis, anchor_dim),
                s => {
                    if let Some(px) = of_room(s, anchor_dim) {
                        set_axis_clamped(tree, c, axis, px);
                    }
                }
            }
        }
        c = tree.next_sibling[c as usize];
    }

    // Text children have no spec sizing; clamp their width to the content box
    // so fit_heights wraps them. Same walk, same gate: no text, no clamp.
    if axis == AxisSel::Width && tree.any_text {
        let mut c = tree.first_child[i as usize];
        while c != NIL {
            if matches!(tree.content[c as usize], NodeContent::Text(_))
                && tree.size[c as usize].w > content
            {
                tree.size[c as usize].w = content;
            }
            c = tree.next_sibling[c as usize];
        }
    }
}

/// Resolves the Grow children of one main-axis run — a whole child list
/// (`end == NIL`) or one wrap line — into `content`, and returns what the
/// run ends up occupying, gaps included.
fn distribute_run(
    tree: &mut Tree,
    start: u32,
    end: u32,
    axis: AxisSel,
    content: f32,
    gap: f32,
) -> f32 {
    let mut used = 0.0f32;
    let mut grow_total = 0.0f32;
    let mut n = 0u32;
    let mut c = start;
    while c != end && c != NIL {
        if !is_float(tree, c) {
            match child_sizing(tree, c, axis) {
                Sizing::Grow(f) => grow_total += f.max(0.0),
                _ => used += get_axis(tree, c, axis),
            }
            n += 1;
        }
        c = tree.next_sibling[c as usize];
    }
    if n > 1 {
        used += gap * (n - 1) as f32;
    }
    let mut total = used;
    if grow_total > 0.0 {
        // Flexbox's freeze loop (CSS Flexible Box §9.7, step 6). A grow
        // child's share is `remain` split by factor; one whose own min or
        // max holds it off that share is a violator, min or max by the
        // sign of the difference. A pass sums its violations and freezes
        // only the violators of the dominant sign — the min ones when the
        // sum is positive, the max ones when negative — each at its
        // clamp, its size moved into `used`; the rest, the other sign's
        // violators included, share what is left in the next pass, until
        // a pass's violations sum to nothing. A max on one child is room
        // for its siblings, not a hole at the end of the run (a devtools
        // inspector capped at 300 left the node list above it short of
        // the panel by the same 300); and a min on one is not the cue to
        // freeze a capped sibling at its cap while a plain one gets
        // nothing (RG5: 600 over A max 100, B min 500, C — 50 / 500 / 50,
        // not 100 / 500 / 0). At most one pass per grow child, since each
        // pass past the first froze one. The per-child state is a byte in
        // `grow_scratch`, indexed by the child's place in the run, so a
        // pass over n children costs n whichever way the earlier ones
        // went.
        const FROZEN: u8 = 1;
        const MIN_VIOLATOR: u8 = 2;
        const MAX_VIOLATOR: u8 = 4;
        let mut scratch = std::mem::take(&mut tree.grow_scratch);
        scratch.clear();
        scratch.resize(n as usize, 0);
        loop {
            let remain = (content - used).max(0.0);
            let mut violation = 0.0f32;
            let mut unfrozen = 0.0f32;
            let mut k = 0usize;
            let mut c = start;
            while c != end && c != NIL {
                if !is_float(tree, c) {
                    if scratch[k] & FROZEN == 0
                        && let Sizing::Grow(f) = child_sizing(tree, c, axis)
                    {
                        let share = remain * f.max(0.0) / grow_total;
                        set_axis_clamped(tree, c, axis, share);
                        let got = get_axis(tree, c, axis);
                        unfrozen += got;
                        let off = got - share;
                        scratch[k] = if off > 0.01 {
                            violation += off;
                            MIN_VIOLATOR
                        } else if off < -0.01 {
                            violation += off;
                            MAX_VIOLATOR
                        } else {
                            0
                        };
                    }
                    k += 1;
                }
                c = tree.next_sibling[c as usize];
            }
            if violation.abs() <= 0.01 {
                // Nothing to freeze: every unfrozen child is at its
                // share, or the clamps cancel and the run adds up.
                total = used + unfrozen;
                break;
            }
            let freeze = if violation > 0.0 {
                MIN_VIOLATOR
            } else {
                MAX_VIOLATOR
            };
            let mut k = 0usize;
            let mut c = start;
            while c != end && c != NIL {
                if !is_float(tree, c) {
                    if scratch[k] & freeze != 0
                        && let Sizing::Grow(f) = child_sizing(tree, c, axis)
                    {
                        scratch[k] = FROZEN;
                        used += get_axis(tree, c, axis);
                        grow_total -= f.max(0.0);
                    }
                    k += 1;
                }
                c = tree.next_sibling[c as usize];
            }
            if grow_total <= 0.0 {
                total = used;
                break;
            }
        }
        tree.grow_scratch = scratch;
    }
    total
}

/// The shrink pass: pays off `deficit` (how far in-flow children overflow
/// the parent's main-axis content box) by compressing Fit-sized children
/// and the shares of the room — `Percent` and a size expression. A share
/// was cut from the content box before the gaps between the
/// children took theirs, so two `"50%"` children and a gap overflow until
/// this gives. Two rules, by what the run holds:
///
/// - With a share in it, CSS's ([`shrink_as_css`]): every
///   shrinkable child gives in proportion to its size, down to its floor —
///   its declared min, or where none was declared ([`Min::AUTO`]) its
///   min-content ([`min_content`], measured here and only here), none for
///   a child that scrolls that axis — `min-width: auto`.
/// - Fit children alone, clay's: largest first, so equal children end up
///   equal, each toward its declared min — 0 across when none was
///   declared, and down its min-content, none for a child
///   that scrolls or clips that axis: `min-height: auto`, so a row is
///   never squeezed below the text in it.
///
/// Fixed keeps its declared size; Grow never overflows. Text shrinks in
/// width (it rewraps at the new width in fit_heights) but never in height.
/// Scroll axes skip this entirely — overflow is the point of a scroll
/// container.
///
/// `only_line` restricts it to one wrap line. A wrapping container reaches
/// here only for a line it could not break any further (a single child
/// wider than the box), so the compression stays on that line instead of
/// squeezing children that are already comfortable on other ones.
fn shrink_axis(
    tree: &mut Tree,
    text: &mut dyn TextMeasure,
    i: u32,
    axis: AxisSel,
    mut deficit: f32,
    only_line: Option<u32>,
    share: bool,
) {
    let shrinkable = |tree: &Tree, c: u32| -> Option<f32> {
        if is_float(tree, c)
            || !matches!(
                child_sizing(tree, c, axis),
                Sizing::Fit | Sizing::Percent(_) | Sizing::Calc(_)
            )
        {
            return None;
        }
        if only_line.is_some_and(|l| tree.line[c as usize] != l) {
            return None;
        }
        // Squashing text/editors vertically would clip lines, and images
        // would distort; width shrink rewraps (and re-aspects) instead.
        if axis == AxisSel::Height
            && matches!(
                tree.content[c as usize],
                NodeContent::Text(_) | NodeContent::Edit(_) | NodeContent::Image(..)
            )
        {
            return None;
        }
        // Resolved to a number by the fit pass of this axis, which ran.
        let spec = tree.specs[c as usize].layout;
        // An axis a declared ratio set would come out of shrinking at some
        // other ratio; like an image, it keeps its size.
        let derived = match axis {
            AxisSel::Width => spec.aspect_width().is_some(),
            AxisSel::Height => spec.aspect_height(),
        };
        if derived {
            return None;
        }
        Some(match axis {
            AxisSel::Width => spec.min_w.resolved(),
            AxisSel::Height => spec.min_h.resolved(),
        })
    };

    // A run holding a share of the room gives as CSS's flex items do,
    // every shrinkable child of it with the share; a run
    // of fit children alone keeps clay's rule below. `share` is what the
    // caller saw resolving the shares, so a run without one — a column
    // of a thousand fixed rows overflowing its box, the common one here —
    // walks its children once, not once more to look (6% of such a frame
    // when it did).
    if share {
        let mut c = tree.first_child[i as usize];
        let mut items = std::mem::take(&mut tree.shrink_scratch);
        items.clear();
        let mut share = false;
        while c != NIL {
            if let Some(declared) = shrinkable(tree, c) {
                share |= matches!(
                    child_sizing(tree, c, axis),
                    Sizing::Percent(_) | Sizing::Calc(_)
                );
                let l = &tree.specs[c as usize].layout;
                let (min, scrolls) = match axis {
                    AxisSel::Width => (l.min_w, l.scroll_x),
                    AxisSel::Height => (l.min_h, l.scroll_y),
                };
                let base = get_axis(tree, c, axis);
                // `min-width: auto`: the content's, where nothing was
                // declared — no more than the child's own size, and none
                // for a child that scrolls its overflow, as CSS has it.
                let floor = match () {
                    _ if !min.is_auto() => declared,
                    _ if scrolls => 0.0,
                    _ => min_content(tree, text, c as usize, axis),
                };
                items.push(Give {
                    c,
                    base,
                    floor: floor.min(base),
                    size: base,
                    held: false,
                });
            }
            c = tree.next_sibling[c as usize];
        }
        if share {
            shrink_as_css(tree, axis, deficit, &mut items);
        }
        tree.shrink_scratch = items;
        if share {
            return;
        }
    }

    // Largest-first, like grow in reverse: pull the biggest children down
    // to the second-biggest, repeat until the deficit is paid or every
    // shrinkable child sits at its floor. Across, the floor is the
    // declared min (0 undeclared), so a row of labels squeezes them into
    // their ellipses. Down, an undeclared one is the child's min-content
    // — CSS's `min-height: auto` — since nothing in a box
    // gives vertically but a scroller or a clip: a row squeezed below
    // its text painted the text over the rows under it. One that scrolls
    // or clips that axis goes to 0.
    let mut items = std::mem::take(&mut tree.shrink_scratch);
    items.clear();
    let mut c = tree.first_child[i as usize];
    while c != NIL {
        if let Some(declared) = shrinkable(tree, c) {
            let base = get_axis(tree, c, axis);
            let l = &tree.specs[c as usize].layout;
            let floor = match axis {
                AxisSel::Height if l.min_h.is_auto() => {
                    if l.scroll_y || l.clip {
                        0.0
                    } else {
                        min_content(tree, text, c as usize, axis)
                    }
                }
                _ => declared,
            };
            items.push(Give {
                c,
                base,
                floor: floor.min(base),
                size: base,
                held: false,
            });
        }
        c = tree.next_sibling[c as usize];
    }
    let mut guard = 0;
    while deficit > 0.5 && guard < 128 {
        guard += 1;
        let mut largest = f32::NEG_INFINITY;
        let mut second = 0.0f32;
        let mut count = 0u32;
        for g in items.iter().filter(|g| g.size > g.floor + 0.01) {
            let s = g.size;
            if s > largest + 0.01 {
                second = if largest.is_finite() {
                    largest.max(second)
                } else {
                    second
                };
                largest = s;
                count = 1;
            } else if s > largest - 0.01 {
                count += 1;
            } else if s > second {
                second = s;
            }
        }
        if count == 0 {
            break;
        }
        let target = (largest - deficit / count as f32).max(second).max(0.0);
        let mut shrunk_any = false;
        for g in items.iter_mut().filter(|g| g.size > largest - 0.01) {
            let new = target.max(g.floor);
            if new < g.size {
                deficit -= g.size - new;
                g.size = new;
                shrunk_any = true;
            }
        }
        if !shrunk_any {
            break;
        }
    }
    for g in items.iter() {
        if g.size < g.base {
            set_axis(tree, g.c, axis, g.size);
        }
    }
    tree.shrink_scratch = items;
}

/// CSS's shrink of flex items (css-flexbox §9.7, `flex-shrink: 1`),
/// over `items`: each gives in proportion to its
/// size — its scaled shrink factor is its base — and one that would go
/// under its floor is held there and the rest share what it could not
/// pay, until nothing is under its floor or every child is held. What is
/// left unpaid overflows, as it does in CSS.
#[inline(never)]
fn shrink_as_css(tree: &mut Tree, axis: AxisSel, deficit: f32, items: &mut [Give]) {
    // Each round holds at least one more child or ends, so `n + 1` rounds
    // settle it.
    for _ in 0..=items.len() {
        let (mut paid, mut weight) = (0.0f32, 0.0f32);
        for g in items.iter() {
            if g.held {
                paid += g.base - g.size;
            } else {
                weight += g.base;
            }
        }
        let left = deficit - paid;
        if left <= 0.0 || weight <= 0.0 {
            break;
        }
        let mut under = false;
        for g in items.iter_mut().filter(|g| !g.held) {
            let want = g.base - left * g.base / weight;
            if want < g.floor {
                (g.size, g.held, under) = (g.floor, true, true);
            } else {
                g.size = want;
            }
        }
        if !under {
            break;
        }
    }
    for g in items.iter() {
        if g.size < g.base {
            set_axis(tree, g.c, axis, g.size);
        }
    }
}

/// One child of a run giving CSS's way ([`shrink_as_css`]): its size
/// before, its floor, the size it is given, and whether it is held there.
#[derive(Clone, Copy, Debug)]
pub(crate) struct Give {
    c: u32,
    base: f32,
    floor: f32,
    size: f32,
    held: bool,
}

/// A sizing that takes its size from the room once the parent's is
/// known — a `Percent`, or a size expression — in px of
/// `room`; `None` for the others.
#[inline]
fn of_room(sizing: Sizing, room: f32) -> Option<f32> {
    match sizing {
        Sizing::Percent(p) => Some(room * p),
        Sizing::Calc(c) => Some(c.resolve(room)),
        _ => None,
    }
}

/// Writes node `c`'s size-expression clamps on `axis` as px of `room`
/// into the spec's, so every later clamp — and every
/// reader of `min_w` / `max_w` — reads a number, as a `Min::FIT` floor
/// is written back once its fit pass ran. Before this a calc clamp is
/// none, as a percentage clamp is in CSS's intrinsic sizing. `true` when
/// the node had one to write.
fn resolve_bounds(tree: &mut Tree, c: u32, axis: AxisSel, room: f32) -> bool {
    let l = &mut tree.specs[c as usize].layout;
    let (min, max) = match axis {
        AxisSel::Width => (&mut l.min_w, &mut l.max_w),
        AxisSel::Height => (&mut l.min_h, &mut l.max_h),
    };
    let mut any = false;
    if let Some(k) = min.as_calc() {
        *min = Min::px(k.resolve(room));
        any = true;
    }
    if let Some(k) = crate::spec::max_calc(*max) {
        *max = k.resolve(room);
        any = true;
    }
    any
}

fn child_sizing(tree: &Tree, c: u32, axis: AxisSel) -> Sizing {
    if matches!(tree.content[c as usize], NodeContent::Text(_)) {
        return Sizing::Fit;
    }
    match axis {
        AxisSel::Width => tree.specs[c as usize].layout.width,
        AxisSel::Height => tree.specs[c as usize].layout.height,
    }
}

fn get_axis(tree: &Tree, c: u32, axis: AxisSel) -> f32 {
    match axis {
        AxisSel::Width => tree.size[c as usize].w,
        AxisSel::Height => tree.size[c as usize].h,
    }
}

fn set_axis(tree: &mut Tree, c: u32, axis: AxisSel, v: f32) {
    match axis {
        AxisSel::Width => tree.size[c as usize].w = v,
        AxisSel::Height => tree.size[c as usize].h = v,
    }
}

/// set_axis clamped by the child's own min/max on that axis. Inlined,
/// and borrowing the spec rather than copying it: a call per child that
/// copied the whole layout spec cost the 10k grid 2% (C48).
#[inline]
fn set_axis_clamped(tree: &mut Tree, c: u32, axis: AxisSel, v: f32) {
    let spec = &tree.specs[c as usize].layout;
    let v = match axis {
        AxisSel::Width => spec.clamp_w(v),
        AxisSel::Height => spec.clamp_h(v),
    };
    set_axis(tree, c, axis, v);
}

/// The "viewport" a float of `c` means: the window for the devtools' own
/// nodes and for the core's menu (a transient the platform's own would
/// not confine either — and one the panel's select opens *in* the dock,
/// where the host area would push it into the app), and for everyone
/// else the host area — the window less the devtools' dock — when one
/// is set (`Tree::host_area`).
fn float_viewport(tree: &Tree, c: u32, viewport: Size) -> Rect {
    use crate::tree::OriginId;
    let window = Rect::new(0.0, 0.0, viewport.w, viewport.h);
    let origin = tree.origins[c as usize];
    if tree.host_area.w <= 0.0 || origin == OriginId::DEVTOOLS || origin == OriginId::MENU {
        window
    } else {
        tree.host_area
    }
}

/// Places `c`, which is out of flow, against its anchor.
fn place_float(
    tree: &mut Tree,
    cfg: crate::spec::FloatConfig,
    c: u32,
    parent: Rect,
    viewport: Size,
) {
    let vp = float_viewport(tree, c, viewport);
    let anchor = match cfg.anchor {
        FloatAnchor::Parent => parent,
        FloatAnchor::Viewport => vp,
        // Placed in the sixth pass, once the anchor is.
        FloatAnchor::Node(_) => return,
    };
    let cs = tree.size[c as usize];
    // Mirroring across the viewport itself would teleport a
    // cursor-anchored float to the opposite side of the window,
    // so viewport floats only clamp.
    let mirrors = cfg.anchor == FloatAnchor::Parent;
    tree.pos[c as usize] = attach_fitted(&cfg, anchor, cs, vp, mirrors);
}

/// Where a float of size `cs` sits against `anchor` by its config's
/// attach points and offset — and, when it declares `fit`, kept in `vp`:
/// mirrored across the anchor per axis where that side is less off-screen
/// (if `mirrors`), then clamped. A parent-anchored float and a
/// node-anchored one both mirror; a viewport one only clamps.
fn attach_fitted(
    cfg: &crate::spec::FloatConfig,
    anchor: Rect,
    cs: Size,
    vp: Rect,
    mirrors: bool,
) -> Vec2 {
    let mut x = attach(
        anchor.x,
        anchor.w,
        cs.w,
        cfg.anchor_point.0,
        cfg.self_point.0,
        cfg.offset.x,
    );
    let mut y = attach(
        anchor.y,
        anchor.h,
        cs.h,
        cfg.anchor_point.1,
        cfg.self_point.1,
        cfg.offset.y,
    );
    if cfg.fit && mirrors {
        // Mirror the attachment across the anchor per axis when
        // the mirrored side is less off-screen (ties keep the
        // declared side), then clamp the rest. Clamp order pins
        // the top/left edge on screen when nothing fits.
        let fx = attach(
            anchor.x,
            anchor.w,
            cs.w,
            mirror(cfg.anchor_point.0),
            mirror(cfg.self_point.0),
            -cfg.offset.x,
        );
        if overflow(x - vp.x, cs.w, vp.w) > overflow(fx - vp.x, cs.w, vp.w) {
            x = fx;
        }
        let fy = attach(
            anchor.y,
            anchor.h,
            cs.h,
            mirror(cfg.anchor_point.1),
            mirror(cfg.self_point.1),
            -cfg.offset.y,
        );
        if overflow(y - vp.y, cs.h, vp.h) > overflow(fy - vp.y, cs.h, vp.h) {
            y = fy;
        }
    }
    if cfg.fit {
        x = x.min(vp.x + vp.w - cs.w).max(vp.x);
        y = y.min(vp.y + vp.h - cs.h).max(vp.y);
    }
    Vec2::new(x, y)
}

fn positions(
    tree: &mut Tree,
    scroll: &mut ScrollStore,
    viewport: Size,
    scale: f32,
    range: std::ops::Range<usize>,
) {
    for i in range {
        if tree.parent[i] == NIL {
            tree.pos[i] = Vec2::ZERO;
        }
        let spec = tree.specs[i].layout;
        let origin = tree.pos[i];
        let size = tree.size[i];
        let wrap = wraps(tree, i as u32);

        let (main_content, cross_content, main_pad_start, cross_pad_start) = match spec.dir {
            Dir::Row => (
                size.w - spec.padding.x(),
                size.h - spec.padding.y(),
                spec.padding.l,
                spec.padding.t,
            ),
            Dir::Column => (
                size.h - spec.padding.y(),
                size.w - spec.padding.x(),
                spec.padding.t,
                spec.padding.l,
            ),
        };

        // The content box the children occupy, in main/cross terms. For a
        // wrapping row that is the widest line by the longest stack of
        // lines; for everything else the one run of children.
        let mut stretch = 0.0f32;
        let (total_main, max_cross) = if wrap {
            let (lines, stacked, widest) = wrap_measure(tree, i as u32);
            stretch = line_stretch(lines, stacked, cross_content);
            (widest, stacked + stretch * lines as f32)
        } else {
            let mut total_main = 0.0f32;
            let mut max_cross = 0.0f32;
            let mut n = 0u32;
            for c in tree.children(i as u32) {
                if is_float(tree, c) {
                    continue;
                }
                let (c_main, c_cross) = match spec.dir {
                    Dir::Row => (tree.size[c as usize].w, tree.size[c as usize].h),
                    Dir::Column => (tree.size[c as usize].h, tree.size[c as usize].w),
                };
                total_main += c_main;
                max_cross = max_cross.max(c_cross);
                n += 1;
            }
            if n > 1 {
                total_main += spec.gap * (n - 1) as f32;
            }
            (total_main, max_cross)
        };

        // Scroll containers: clamp the retained offset to this frame's
        // overflow and shift children by it.
        let mut offset = Vec2::ZERO;
        // Anchoring: the scroll axis is the main axis,
        // the container is not wrapping, and a previous layout recorded
        // which child was first in view and where its leading edge sat in
        // the content. Where that edge sits *now* is the same walk the
        // placement below makes, minus the offset; the difference is added
        // to the retained offset before it is clamped, so the child stays
        // where it was on screen whatever grew or shrank before it.
        let anchors = spec.anchor
            && !wrap
            && match spec.dir {
                Dir::Row => spec.scroll_x,
                Dir::Column => spec.scroll_y,
            };
        let free_main = (main_content - total_main).max(0.0);
        // The unwrapped run's spread; a wrapping row deals out each line's
        // own below. Counting the children is skipped for the three
        // alignments that need no count.
        let n_main = if matches!(spec.main_align, Align::Start | Align::Center | Align::End) {
            0
        } else {
            tree.children(i as u32)
                .filter(|&c| !is_float(tree, c))
                .count() as u32
        };
        let (lead_main, between_main) = main_spread(spec.main_align, free_main, n_main);
        let content_start = main_pad_start + lead_main;
        if anchors && let Some((anchor, was_at)) = scroll.anchor(tree.keys[i]) {
            let mut at = content_start;
            let mut c = first_in_flow(tree, i as u32);
            while c != NIL {
                if !is_float(tree, c) {
                    if tree.keys[c as usize] == anchor {
                        let delta = at - was_at;
                        if delta != 0.0 {
                            scroll.scroll_by(
                                tree.keys[i],
                                match spec.dir {
                                    Dir::Row => Vec2::new(delta, 0.0),
                                    Dir::Column => Vec2::new(0.0, delta),
                                },
                            );
                        }
                        break;
                    }
                    let c_main = match spec.dir {
                        Dir::Row => tree.size[c as usize].w,
                        Dir::Column => tree.size[c as usize].h,
                    };
                    at += c_main + spec.gap + between_main;
                }
                c = tree.next_sibling[c as usize];
            }
        }
        if spec.scroll_x || spec.scroll_y {
            let (content_w, content_h) = match spec.dir {
                Dir::Row => (total_main + spec.padding.x(), max_cross + spec.padding.y()),
                Dir::Column => (max_cross + spec.padding.x(), total_main + spec.padding.y()),
            };
            let max = Vec2::new(
                if spec.scroll_x {
                    (content_w - size.w).max(0.0)
                } else {
                    0.0
                },
                if spec.scroll_y {
                    (content_h - size.h).max(0.0)
                } else {
                    0.0
                },
            );
            tree.scroll_max[i] = max;
            // The one place the container's resolved box and its content
            // size exist together; the store keeps a copy, since the tree
            // holding them is cleared before the next view reads it.
            // The retained offset stays exact — a wheel notch of 0.3 px is
            // not lost, it accumulates — and what the children are *moved*
            // by is whole physical pixels, so a row's text does not wobble
            // inside the row while the list scrolls (`Vec2::snapped`).
            offset = scroll
                .resolve(
                    tree.keys[i],
                    Rect::from_pos_size(origin, size),
                    Size::new(content_w, content_h),
                    max,
                    tree.specs[i].transition,
                )
                .snapped(scale);
        }
        let (main_scroll, cross_scroll) = match spec.dir {
            Dir::Row => (offset.x, offset.y),
            Dir::Column => (offset.y, offset.x),
        };
        if anchors {
            // The anchor for the next layout: the first in-flow child whose
            // trailing edge is past the offset — the first one in view —
            // and where its leading edge sits in the content.
            let mut next = None;
            let mut at = content_start;
            let mut c = first_in_flow(tree, i as u32);
            while c != NIL {
                if !is_float(tree, c) {
                    let c_main = match spec.dir {
                        Dir::Row => tree.size[c as usize].w,
                        Dir::Column => tree.size[c as usize].h,
                    };
                    if at + c_main > main_scroll {
                        next = Some((tree.keys[c as usize], at));
                        break;
                    }
                    at += c_main + spec.gap + between_main;
                }
                c = tree.next_sibling[c as usize];
            }
            scroll.set_anchor(tree.keys[i], next);
        }

        // Out of flow first, so the in-flow walk is one shape whether or
        // not it goes line by line. A frame with no floats skips the walk.
        let mut c = if tree.any_float {
            tree.first_child[i]
        } else {
            NIL
        };
        while c != NIL {
            if let Some(cfg) = tree.specs[c as usize].layout.float {
                place_float(tree, cfg, c, Rect::from_pos_size(origin, size), viewport);
            }
            c = tree.next_sibling[c as usize];
        }

        // One line for an unwrapped container, N for a wrapping row. Main
        // alignment places each line's children in the content box the way
        // it places the single run's, and cross alignment places a child in
        // its own line; with one line the two compose back into the
        // unwrapped placement exactly.
        let mut cross_cursor = cross_pad_start - cross_scroll;
        let mut line_start = first_in_flow(tree, i as u32);
        while line_start != NIL {
            let (end, extent, run_main) = if wrap {
                let end = line_end(tree, line_start);
                let (main, cross) = line_extents(tree, line_start, end, spec.gap);
                (end, cross + stretch, main)
            } else {
                (NIL, cross_content, total_main)
            };
            let free = (main_content - run_main).max(0.0);
            let (lead, between) = if wrap {
                let mut n = 0u32;
                let mut k = line_start;
                while k != end && k != NIL {
                    n += u32::from(!is_float(tree, k));
                    k = tree.next_sibling[k as usize];
                }
                main_spread(spec.main_align, free, n)
            } else {
                (lead_main, between_main)
            };
            // A baseline line's shared baseline, from the line's top.
            let base_above =
                if tree.any_baseline && spec.cross_align == Align::Baseline && spec.dir == Dir::Row
                {
                    Some(line_baseline(tree, line_start, end).0)
                } else {
                    None
                };
            let mut cursor = main_pad_start + lead - main_scroll;
            let mut c = line_start;
            while c != end && c != NIL {
                if is_float(tree, c) {
                    c = tree.next_sibling[c as usize];
                    continue;
                }
                let cs = tree.size[c as usize];
                let (c_main, c_cross) = match spec.dir {
                    Dir::Row => (cs.w, cs.h),
                    Dir::Column => (cs.h, cs.w),
                };
                let cross_off = match base_above {
                    Some(above) if aligns_by_baseline(tree, c) => {
                        let b = first_baseline(tree, c).unwrap_or(c_cross);
                        cross_cursor + above - b
                    }
                    Some(_) => cross_cursor,
                    None => {
                        cross_cursor + align_factor(spec.cross_align) * (extent - c_cross).max(0.0)
                    }
                };
                tree.pos[c as usize] = match spec.dir {
                    Dir::Row => Vec2::new(origin.x + cursor, origin.y + cross_off),
                    Dir::Column => Vec2::new(origin.x + cross_off, origin.y + cursor),
                };
                cursor += c_main + spec.gap + between;
                c = tree.next_sibling[c as usize];
            }
            cross_cursor += extent + spec.cross_gap;
            line_start = end;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::geom::Edges;
    use crate::key::Key;
    use crate::spec::{FloatConfig, NodeSpec};
    use crate::tree::{NodeContent, OriginId, TextId};

    /// Deterministic measurer: every text is `10px * chars_hint` wide and wraps
    /// into 20px lines. The TextId encodes the char count for test purposes.
    struct StubText;

    impl TextMeasure for StubText {
        fn intrinsic(&mut self, id: TextId) -> Size {
            Size::new(10.0 * id.0 as f32, 20.0)
        }
        fn wrapped(&mut self, id: TextId, max_w: f32) -> Size {
            let full = 10.0 * id.0 as f32;
            if max_w <= 0.0 || full <= max_w {
                return Size::new(full, 20.0);
            }
            let lines = (full / max_w).ceil();
            Size::new(max_w, lines * 20.0)
        }
        /// A 20 px line with 5 px of it below the baseline.
        fn baseline(&mut self, _id: TextId) -> f32 {
            15.0
        }
    }

    struct T {
        tree: Tree,
    }

    impl T {
        fn new(root_spec: NodeSpec) -> Self {
            let mut tree = Tree::new();
            tree.push(
                NIL,
                Key::ROOT,
                OriginId::HOST,
                root_spec,
                NodeContent::Container,
            );
            T { tree }
        }

        fn node(&mut self, parent: u32, spec: NodeSpec) -> u32 {
            let key = Key::ROOT.index(self.tree.len() as u64);
            self.tree
                .push(parent, key, OriginId::HOST, spec, NodeContent::Container)
        }

        fn text(&mut self, parent: u32, chars: u32) -> u32 {
            let key = Key::ROOT.index(self.tree.len() as u64);
            self.tree.push(
                parent,
                key,
                OriginId::HOST,
                NodeSpec::default(),
                NodeContent::Text(TextId(chars)),
            )
        }

        fn run(&mut self, vw: f32, vh: f32) {
            let mut scroll = ScrollStore::default();
            compute(
                &mut self.tree,
                &mut StubText,
                &mut scroll,
                Size::new(vw, vh),
                1.0,
            );
        }

        fn size(&self, i: u32) -> Size {
            self.tree.size[i as usize]
        }

        fn pos(&self, i: u32) -> Vec2 {
            self.tree.pos[i as usize]
        }
    }

    fn px(v: f32) -> Sizing {
        Sizing::Fixed(v)
    }

    /// Two grow children in a 600 column, one capped at 100: the cap is
    /// the other's room, not a hole. A min that holds a child past its
    /// share takes from its siblings the same way, and a run of clamps
    /// resolves in one layout.
    #[test]
    fn a_grow_childs_clamp_is_its_siblings_room() {
        let mut t = T::new(NodeSpec::column().width(px(100.0)).height(px(600.0)));
        let a = t.node(0, NodeSpec::row().grow_height());
        let b = t.node(0, NodeSpec::row().grow_height().max_height(100.0));
        t.run(1000.0, 1000.0);
        assert_eq!(t.size(b).h, 100.0);
        assert_eq!(t.size(a).h, 500.0);

        let mut t = T::new(NodeSpec::row().width(px(300.0)).height(px(50.0)));
        let a = t.node(0, NodeSpec::row().grow_width());
        let b = t.node(0, NodeSpec::row().grow_width().min_width(200.0));
        let c = t.node(0, NodeSpec::row().grow_width().max_width(20.0));
        t.run(1000.0, 1000.0);
        assert_eq!(t.size(b).w, 200.0);
        assert_eq!(t.size(c).w, 20.0);
        assert_eq!(t.size(a).w, 80.0);
        assert_eq!(t.pos(c).x, 280.0);
    }

    /// Flexbox's sign rule (CSS Flexible Box §9.7, step 6): a pass sums
    /// its violations and freezes only the violators of the dominant
    /// sign, then re-shares. A 600 column of three grow rows, A capped
    /// at 100, B held to 500, C plain: the first pass shares 200, A's
    /// −100 and B's +300 sum positive, so only B is frozen and A shares
    /// the remaining 100 with C — 50 / 500 / 50, not A at its cap and C
    /// empty (RG5). Mirrored: A capped at 100, B held to 210, the sum
    /// −90 is negative, so only A is frozen, and B's re-share of 250
    /// clears its min on its own — 100 / 250 / 250, not 100 / 210 / 290.
    #[test]
    fn a_pass_freezes_only_the_violators_of_the_dominant_sign() {
        let mut t = T::new(NodeSpec::column().width(px(100.0)).height(px(600.0)));
        let a = t.node(0, NodeSpec::row().grow_height().max_height(100.0));
        let b = t.node(0, NodeSpec::row().grow_height().min_height(Min::px(500.0)));
        let c = t.node(0, NodeSpec::row().grow_height());
        t.run(1000.0, 1000.0);
        assert_eq!(t.size(a).h, 50.0);
        assert_eq!(t.size(b).h, 500.0);
        assert_eq!(t.size(c).h, 50.0);
        assert_eq!(t.pos(c).y, 550.0);

        let mut t = T::new(NodeSpec::column().width(px(100.0)).height(px(600.0)));
        let a = t.node(0, NodeSpec::row().grow_height().max_height(100.0));
        let b = t.node(0, NodeSpec::row().grow_height().min_height(Min::px(210.0)));
        let c = t.node(0, NodeSpec::row().grow_height());
        t.run(1000.0, 1000.0);
        assert_eq!(t.size(a).h, 100.0);
        assert_eq!(t.size(b).h, 250.0);
        assert_eq!(t.size(c).h, 250.0);
    }

    #[test]
    fn fit_row_sums_children_and_gaps() {
        let mut t = T::new(NodeSpec::row().pad(10.0).gap(5.0));
        let r = 0;
        t.node(r, NodeSpec::column().width(px(30.0)).height(px(40.0)));
        t.node(r, NodeSpec::column().width(px(20.0)).height(px(25.0)));
        t.run(1000.0, 1000.0);
        // 30 + 5 + 20 + 2*10 pad = 75; height = max(40,25) + 20 = 60
        assert_eq!(t.size(r), Size::new(75.0, 60.0));
    }

    #[test]
    fn fit_column_sums_heights() {
        let mut t = T::new(NodeSpec::column().gap(4.0));
        t.node(0, NodeSpec::row().width(px(10.0)).height(px(10.0)));
        t.node(0, NodeSpec::row().width(px(50.0)).height(px(10.0)));
        t.node(0, NodeSpec::row().width(px(30.0)).height(px(10.0)));
        t.run(1000.0, 1000.0);
        assert_eq!(t.size(0), Size::new(50.0, 38.0));
    }

    #[test]
    fn grow_splits_remaining_space_by_factor() {
        let mut t = T::new(NodeSpec::row().width(px(300.0)).height(px(100.0)).gap(10.0));
        let a = t.node(0, NodeSpec::column().width(px(50.0)).height(px(10.0)));
        let b = t.node(0, NodeSpec::column().grow_width().height(px(10.0)));
        let c = t.node(
            0,
            NodeSpec::column().width(Sizing::Grow(2.0)).height(px(10.0)),
        );
        t.run(1000.0, 1000.0);
        // content 300, fixed 50, gaps 20 -> remain 230 split 1:2
        let bw = t.size(b).w;
        let cw = t.size(c).w;
        assert!((bw - 230.0 / 3.0).abs() < 0.01, "b={bw}");
        assert!((cw - 460.0 / 3.0).abs() < 0.01, "c={cw}");
        assert_eq!(t.size(a).w, 50.0);
    }

    #[test]
    fn percent_resolves_against_content_box() {
        let mut t = T::new(NodeSpec::row().width(px(200.0)).height(px(100.0)).pad(10.0));
        let a = t.node(
            0,
            NodeSpec::column()
                .width(Sizing::Percent(0.5))
                .height(Sizing::Percent(1.0)),
        );
        t.run(1000.0, 1000.0);
        assert_eq!(t.size(a), Size::new(90.0, 80.0)); // (200-20)*0.5, (100-20)*1.0
    }

    #[test]
    fn cross_axis_grow_fills_content() {
        let mut t = T::new(
            NodeSpec::column()
                .width(px(120.0))
                .height(px(200.0))
                .pad(8.0),
        );
        let a = t.node(0, NodeSpec::row().grow_width().height(px(30.0)));
        t.run(1000.0, 1000.0);
        assert_eq!(t.size(a).w, 104.0);
    }

    #[test]
    fn nested_fit_propagates_up() {
        let mut t = T::new(NodeSpec::column());
        let mid = t.node(0, NodeSpec::row().pad(5.0).gap(2.0));
        t.node(mid, NodeSpec::column().width(px(10.0)).height(px(10.0)));
        t.node(mid, NodeSpec::column().width(px(10.0)).height(px(10.0)));
        t.run(1000.0, 1000.0);
        assert_eq!(t.size(mid), Size::new(32.0, 20.0));
        assert_eq!(t.size(0), Size::new(32.0, 20.0));
    }

    #[test]
    fn root_grow_takes_viewport() {
        let mut t = T::new(NodeSpec::column().fill());
        t.run(800.0, 600.0);
        assert_eq!(t.size(0), Size::new(800.0, 600.0));
    }

    #[test]
    fn positions_row_with_gap_and_padding() {
        let mut t = T::new(
            NodeSpec::row()
                .width(px(300.0))
                .height(px(100.0))
                .pad(10.0)
                .gap(5.0),
        );
        let a = t.node(0, NodeSpec::column().width(px(40.0)).height(px(20.0)));
        let b = t.node(0, NodeSpec::column().width(px(40.0)).height(px(20.0)));
        t.run(1000.0, 1000.0);
        assert_eq!(t.pos(a), Vec2::new(10.0, 10.0));
        assert_eq!(t.pos(b), Vec2::new(55.0, 10.0));
    }

    #[test]
    fn main_center_alignment_offsets_children() {
        let mut t = T::new(NodeSpec::row().width(px(200.0)).height(px(50.0)).center());
        let a = t.node(0, NodeSpec::column().width(px(60.0)).height(px(20.0)));
        t.run(1000.0, 1000.0);
        assert_eq!(t.pos(a), Vec2::new(70.0, 15.0));
    }

    #[test]
    fn main_end_alignment() {
        let mut t = T::new(
            NodeSpec::column()
                .width(px(100.0))
                .height(px(100.0))
                .main_align(Align::End)
                .gap(10.0),
        );
        let a = t.node(0, NodeSpec::row().width(px(10.0)).height(px(20.0)));
        let b = t.node(0, NodeSpec::row().width(px(10.0)).height(px(20.0)));
        t.run(1000.0, 1000.0);
        assert_eq!(t.pos(a).y, 50.0);
        assert_eq!(t.pos(b).y, 80.0);
    }

    /// Three 10 px children in a 100 px row: 70 px free, dealt out the
    /// way each spread says.
    fn spread(a: Align, gap: f32) -> Vec<f32> {
        let mut t = T::new(
            NodeSpec::row()
                .width(px(100.0))
                .height(px(10.0))
                .gap(gap)
                .main_align(a),
        );
        let kids: Vec<u32> = (0..3)
            .map(|_| t.node(0, NodeSpec::row().width(px(10.0)).height(px(10.0))))
            .collect();
        t.run(1000.0, 1000.0);
        kids.iter().map(|&k| t.pos(k).x).collect()
    }

    #[test]
    fn space_between_puts_the_free_space_between_the_children() {
        assert_eq!(spread(Align::SpaceBetween, 0.0), [0.0, 45.0, 90.0]);
        // The spread is on top of the gap: 50 free, 25 each.
        assert_eq!(spread(Align::SpaceBetween, 10.0), [0.0, 45.0, 90.0]);
    }

    #[test]
    fn space_around_gives_the_ends_half_a_share() {
        // 70 / 3 each, half of it on either side of a child.
        let x = spread(Align::SpaceAround, 0.0);
        let share = 70.0 / 3.0;
        for (i, want) in [share / 2.0, share * 1.5 + 10.0, share * 2.5 + 20.0]
            .into_iter()
            .enumerate()
        {
            assert!((x[i] - want).abs() < 1e-3, "{x:?}");
        }
    }

    #[test]
    fn space_evenly_makes_every_gap_and_both_ends_equal() {
        assert_eq!(spread(Align::SpaceEvenly, 0.0), [17.5, 45.0, 72.5]);
    }

    /// One child: `space-between` has nothing to go between and starts it,
    /// the other two centre it, as CSS does.
    #[test]
    fn a_lone_child_under_a_spread_starts_or_centres() {
        for (a, want) in [
            (Align::SpaceBetween, 0.0),
            (Align::SpaceAround, 45.0),
            (Align::SpaceEvenly, 45.0),
        ] {
            let mut t = T::new(NodeSpec::row().width(px(100.0)).main_align(a));
            let c = t.node(0, NodeSpec::row().width(px(10.0)).height(px(10.0)));
            t.run(1000.0, 1000.0);
            assert_eq!(t.pos(c).x, want, "{a:?}");
        }
    }

    /// Nothing free, nothing dealt: a grow child takes the space, and a run
    /// that overflows keeps its plain gaps.
    #[test]
    fn a_spread_with_nothing_free_is_the_gaps_alone() {
        let mut t = T::new(
            NodeSpec::row()
                .width(px(100.0))
                .gap(5.0)
                .main_align(Align::SpaceBetween),
        );
        let a = t.node(0, NodeSpec::row().width(px(10.0)).height(px(10.0)));
        let b = t.node(0, NodeSpec::row().grow_width().height(px(10.0)));
        let c = t.node(0, NodeSpec::row().width(px(10.0)).height(px(10.0)));
        t.run(1000.0, 1000.0);
        assert_eq!((t.pos(a).x, t.pos(b).x, t.pos(c).x), (0.0, 15.0, 90.0));

        let mut t = T::new(
            NodeSpec::row()
                .width(px(100.0))
                .gap(5.0)
                .scroll_x()
                .main_align(Align::SpaceEvenly),
        );
        let kids: Vec<u32> = (0..3)
            .map(|_| t.node(0, NodeSpec::row().width(px(50.0)).height(px(10.0))))
            .collect();
        t.run(1000.0, 1000.0);
        let x: Vec<f32> = kids.iter().map(|&k| t.pos(k).x).collect();
        assert_eq!(x, [0.0, 55.0, 110.0]);
    }

    /// A column spreads down, and floats take no share.
    #[test]
    fn a_column_spreads_its_height_and_floats_take_no_share() {
        let mut t = T::new(
            NodeSpec::column()
                .width(px(10.0))
                .height(px(100.0))
                .main_align(Align::SpaceBetween),
        );
        let a = t.node(0, NodeSpec::row().width(px(10.0)).height(px(20.0)));
        t.node(
            0,
            NodeSpec::row()
                .width(px(5.0))
                .height(px(5.0))
                .float(FloatConfig::below()),
        );
        let b = t.node(0, NodeSpec::row().width(px(10.0)).height(px(20.0)));
        t.run(1000.0, 1000.0);
        assert_eq!((t.pos(a).y, t.pos(b).y), (0.0, 80.0));
    }

    /// A wrapping row deals out each line's own free space.
    #[test]
    fn a_wrapping_row_spreads_each_line_by_itself() {
        let mut t = T::new(
            NodeSpec::row()
                .width(px(100.0))
                .wrap()
                .main_align(Align::SpaceBetween),
        );
        let kids: Vec<u32> = (0..3)
            .map(|_| t.node(0, NodeSpec::row().width(px(40.0)).height(px(10.0))))
            .collect();
        t.run(1000.0, 1000.0);
        let at: Vec<(f32, f32)> = kids.iter().map(|&k| (t.pos(k).x, t.pos(k).y)).collect();
        // Line one holds two with 20 between; line two one, at the start.
        assert_eq!(at, [(0.0, 0.0), (60.0, 0.0), (0.0, 10.0)]);
    }

    /// A 20 px text (baseline 15) and a 40 px box on one row: the box has
    /// no text, so its bottom edge is its baseline, and the text drops to
    /// meet it. The fit row holds both: 40 above, the text's 5 below.
    #[test]
    fn baseline_lines_up_text_with_a_box_s_bottom_edge() {
        let mut t = T::new(NodeSpec::row().cross_align(Align::Baseline));
        let txt = t.text(0, 3);
        let bx = t.node(0, NodeSpec::row().width(px(10.0)).height(px(40.0)));
        t.run(1000.0, 1000.0);
        assert_eq!(t.pos(bx).y, 0.0);
        assert_eq!(t.pos(txt).y, 25.0);
        assert_eq!(t.size(0).h, 45.0);
    }

    /// A container's baseline is its first child's, carried down through
    /// where that child sits in it: a column padded 10 on top holds its
    /// text 10 lower, so a bare text beside it drops 10 to meet it.
    #[test]
    fn a_container_s_baseline_is_its_first_text_s() {
        let mut t = T::new(NodeSpec::row().cross_align(Align::Baseline));
        let bare = t.text(0, 3);
        let col = t.node(
            0,
            NodeSpec::column().padding(Edges {
                l: 0.0,
                r: 0.0,
                t: 10.0,
                b: 0.0,
            }),
        );
        let inner = t.text(col, 3);
        t.node(col, NodeSpec::row().width(px(10.0)).height(px(30.0)));
        t.run(1000.0, 1000.0);
        assert_eq!(t.pos(col).y, 0.0);
        assert_eq!(t.pos(inner).y, 10.0);
        assert_eq!(t.pos(bare).y, 10.0);
        // The column (60 tall, baseline 25) sets the row's height.
        assert_eq!(t.size(0).h, 60.0);
    }

    /// A grow height fills the line from its top instead of aligning.
    #[test]
    fn a_grow_height_child_of_a_baseline_row_fills_from_the_top() {
        let mut t = T::new(
            NodeSpec::row()
                .height(px(50.0))
                .cross_align(Align::Baseline),
        );
        let txt = t.text(0, 3);
        let g = t.node(0, NodeSpec::row().width(px(10.0)).grow_height());
        t.run(1000.0, 1000.0);
        assert_eq!((t.pos(g).y, t.size(g).h), (0.0, 50.0));
        assert_eq!(t.pos(txt).y, 0.0);
    }

    /// A column's cross axis is horizontal: `baseline` there is `start`.
    #[test]
    fn baseline_on_a_column_is_start() {
        let mut t = T::new(
            NodeSpec::column()
                .width(px(100.0))
                .cross_align(Align::Baseline),
        );
        let c = t.node(0, NodeSpec::row().width(px(10.0)).height(px(10.0)));
        t.run(1000.0, 1000.0);
        assert_eq!(t.pos(c).x, 0.0);
    }

    /// `width: grow` and a ratio keeps its shape: the fit height is the
    /// final width over the ratio, and the children overflow it.
    #[test]
    fn a_ratio_sizes_a_fit_height_from_the_final_width() {
        let mut t = T::new(NodeSpec::column().width(px(320.0)));
        let v = t.node(0, NodeSpec::column().grow_width().aspect_ratio(16.0 / 9.0));
        t.node(v, NodeSpec::row().width(px(10.0)).height(px(500.0)));
        t.run(1000.0, 1000.0);
        assert_eq!(t.size(v), Size::new(320.0, 180.0));
    }

    /// Under a fixed height, a fit width is the height times the ratio.
    #[test]
    fn a_ratio_sizes_a_fit_width_from_a_fixed_height() {
        let mut t = T::new(NodeSpec::row());
        let sq = t.node(0, NodeSpec::row().height(px(24.0)).aspect_ratio(1.0));
        t.run(1000.0, 1000.0);
        assert_eq!(t.size(sq), Size::new(24.0, 24.0));
    }

    /// A derived height is not shrunk: a column too short for a ratio box
    /// shrinks its other fit children and leaves the box its shape.
    #[test]
    fn a_ratio_s_derived_height_is_not_shrunk() {
        let mut t = T::new(NodeSpec::column().width(px(100.0)).height(px(100.0)));
        let r = t.node(0, NodeSpec::column().grow_width().aspect_ratio(1.25));
        let other = t.node(0, NodeSpec::column().height(Sizing::Fit));
        t.node(other, NodeSpec::row().width(px(10.0)).height(px(60.0)));
        t.run(1000.0, 1000.0);
        assert_eq!(t.size(r).h, 80.0);
        // Nor is its sibling below the 60 px it holds: the
        // column overflows, where the sibling was squeezed to 20.
        assert_eq!(t.size(other).h, 60.0);
    }

    /// `minHeight: fit` floors a derived height at the children.
    #[test]
    fn min_fit_floors_a_ratio_height_at_its_children() {
        let mut t = T::new(NodeSpec::column().width(px(100.0)));
        let r = t.node(
            0,
            NodeSpec::column()
                .grow_width()
                .aspect_ratio(4.0)
                .min_height(Min::FIT),
        );
        t.node(r, NodeSpec::row().width(px(10.0)).height(px(40.0)));
        t.run(1000.0, 1000.0);
        assert_eq!(t.size(r).h, 40.0);
    }

    #[test]
    fn text_gets_intrinsic_size_when_it_fits() {
        let mut t = T::new(NodeSpec::column().width(px(500.0)).height(px(500.0)));
        let txt = t.text(0, 8); // 80px wide
        t.run(1000.0, 1000.0);
        assert_eq!(t.size(txt), Size::new(80.0, 20.0));
    }

    #[test]
    fn text_wraps_when_clamped_by_parent() {
        let mut t = T::new(
            NodeSpec::column()
                .width(px(100.0))
                .height(px(500.0))
                .pad(10.0),
        );
        let txt = t.text(0, 20); // 200px intrinsic, clamped to 80 -> 3 lines
        t.run(1000.0, 1000.0);
        assert_eq!(t.size(txt).w, 80.0);
        assert_eq!(t.size(txt).h, 60.0);
    }

    #[test]
    fn text_wrapping_grows_fit_parent_height() {
        let mut t = T::new(NodeSpec::column().width(px(100.0)));
        let txt = t.text(0, 30); // 300px intrinsic -> wraps to 100 -> 3 lines
        t.run(1000.0, 1000.0);
        assert_eq!(t.size(txt).h, 60.0);
        assert_eq!(t.size(0).h, 60.0);
    }

    #[test]
    fn grow_with_no_space_left_gets_zero() {
        let mut t = T::new(NodeSpec::row().width(px(100.0)).height(px(50.0)));
        let a = t.node(0, NodeSpec::column().width(px(120.0)).height(px(10.0)));
        let b = t.node(0, NodeSpec::column().grow_width().height(px(10.0)));
        t.run(1000.0, 1000.0);
        assert_eq!(t.size(a).w, 120.0); // no shrinking in v0
        assert_eq!(t.size(b).w, 0.0);
    }

    #[test]
    fn deep_nesting_positions_accumulate() {
        let mut t = T::new(NodeSpec::column().pad(10.0));
        let l1 = t.node(0, NodeSpec::column().pad(10.0));
        let l2 = t.node(l1, NodeSpec::column().pad(10.0));
        let leaf = t.node(l2, NodeSpec::row().width(px(10.0)).height(px(10.0)));
        t.run(1000.0, 1000.0);
        assert_eq!(t.pos(leaf), Vec2::new(30.0, 30.0));
        assert_eq!(t.size(0), Size::new(70.0, 70.0));
    }

    #[test]
    fn grow_respects_max_width() {
        let mut t = T::new(NodeSpec::row().width(px(800.0)).height(px(100.0)));
        let a = t.node(
            0,
            NodeSpec::column()
                .grow_width()
                .max_width(560.0)
                .height(px(10.0)),
        );
        t.run(1000.0, 1000.0);
        assert_eq!(t.size(a).w, 560.0);
        // And tracks the parent when it's smaller than the cap.
        let mut t = T::new(NodeSpec::row().width(px(400.0)).height(px(100.0)));
        let a = t.node(
            0,
            NodeSpec::column()
                .grow_width()
                .max_width(560.0)
                .height(px(10.0)),
        );
        t.run(1000.0, 1000.0);
        assert_eq!(t.size(a).w, 400.0);
    }

    #[test]
    fn min_width_forces_fit_up() {
        let mut t = T::new(NodeSpec::column());
        let a = t.node(0, NodeSpec::row().min_width(120.0).height(px(10.0)));
        t.node(a, NodeSpec::column().width(px(30.0)).height(px(10.0)));
        t.run(1000.0, 1000.0);
        assert_eq!(t.size(a).w, 120.0);
        // Min propagates into the fit parent.
        assert_eq!(t.size(0).w, 120.0);
    }

    #[test]
    fn percent_respects_max() {
        let mut t = T::new(NodeSpec::column().width(px(1000.0)).height(px(1000.0)));
        let a = t.node(
            0,
            NodeSpec::row()
                .width(Sizing::Percent(0.9))
                .max_width(300.0)
                .height(px(10.0)),
        );
        t.run(1000.0, 1000.0);
        assert_eq!(t.size(a).w, 300.0);
    }

    #[test]
    fn text_rewraps_when_capped_parent_shrinks() {
        // Same tree shape at two parent widths: the narrow one wraps taller.
        let build = |parent_w: f32| {
            let mut t = T::new(NodeSpec::column().width(px(parent_w)));
            let txt = t.text(0, 40); // 400px intrinsic
            t.run(1000.0, 1000.0);
            t.size(txt)
        };
        let wide = build(500.0);
        let narrow = build(100.0);
        assert_eq!(wide.h, 20.0);
        assert_eq!(narrow.h, 80.0); // 400 / 100 -> 4 lines
    }

    #[test]
    fn shrink_compresses_largest_fit_child_first() {
        let mut t = T::new(NodeSpec::row().width(px(100.0)).height(px(50.0)));
        let a = t.node(0, NodeSpec::column());
        t.node(a, NodeSpec::row().width(px(80.0)).height(px(10.0)));
        let b = t.node(0, NodeSpec::column());
        t.node(b, NodeSpec::row().width(px(40.0)).height(px(10.0)));
        t.run(1000.0, 1000.0);
        // 120 into 100: the 80 child pays the whole 20px deficit.
        assert_eq!(t.size(a).w, 60.0);
        assert_eq!(t.size(b).w, 40.0);
    }

    #[test]
    fn shrink_respects_min_and_spills_to_the_next() {
        let mut t = T::new(NodeSpec::row().width(px(100.0)).height(px(50.0)));
        let a = t.node(0, NodeSpec::column().min_width(70.0));
        t.node(a, NodeSpec::row().width(px(80.0)).height(px(10.0)));
        let b = t.node(0, NodeSpec::column());
        t.node(b, NodeSpec::row().width(px(40.0)).height(px(10.0)));
        t.run(1000.0, 1000.0);
        // a stops at its min; b pays the rest.
        assert_eq!(t.size(a).w, 70.0);
        assert_eq!(t.size(b).w, 30.0);
    }

    #[test]
    fn equal_children_shrink_equally() {
        let mut t = T::new(NodeSpec::row().width(px(100.0)).height(px(50.0)));
        let mut kids = Vec::new();
        for _ in 0..3 {
            let c = t.node(0, NodeSpec::column());
            t.node(c, NodeSpec::row().width(px(60.0)).height(px(10.0)));
            kids.push(c);
        }
        t.run(1000.0, 1000.0);
        for c in kids {
            assert!(
                (t.size(c).w - 100.0 / 3.0).abs() < 0.1,
                "got {}",
                t.size(c).w
            );
        }
    }

    #[test]
    fn shrunk_text_rewraps() {
        let mut t = T::new(NodeSpec::row().width(px(200.0)).height(px(500.0)));
        t.node(0, NodeSpec::column().width(px(80.0)).height(px(10.0)));
        let txt = t.text(0, 20); // 200px intrinsic
        t.run(1000.0, 1000.0);
        // 280 into 200: text pays the deficit, then wraps at 120 -> 2 lines.
        assert_eq!(t.size(txt).w, 120.0);
        assert_eq!(t.size(txt).h, 40.0);
    }

    #[test]
    fn text_never_shrinks_vertically() {
        let mut t = T::new(NodeSpec::column().width(px(200.0)).height(px(30.0)));
        let txt = t.text(0, 30); // wraps to 200 -> 2 lines = 40 > 30 parent
        t.run(1000.0, 1000.0);
        assert_eq!(
            t.size(txt).h,
            40.0,
            "text overflows rather than clipping lines"
        );
    }

    #[test]
    fn scroll_axis_skips_shrink() {
        let mut t = T::new(
            NodeSpec::column()
                .width(px(100.0))
                .height(px(100.0))
                .scroll_y(),
        );
        for _ in 0..2 {
            let c = t.node(0, NodeSpec::column());
            t.node(c, NodeSpec::row().width(px(10.0)).height(px(80.0)));
        }
        t.run(1000.0, 1000.0);
        // 160 of content in a 100 box stays 160: it scrolls instead.
        for c in [1u32, 3u32] {
            assert_eq!(t.size(c).h, 80.0);
        }
    }

    /// An i3-style tab bar: every tab `grow` with a `min_width`, on a row
    /// that scrolls x. With room the tabs split the bar evenly; past it
    /// each sits at its min and the bar scrolls by the overflow — the
    /// clamp in `distribute_run` and the shrink pass a scroll axis skips
    /// are what make one declaration cover both regimes.
    #[test]
    fn grow_tabs_split_evenly_then_scroll_at_their_min() {
        let bar = || NodeSpec::row().width(px(600.0)).height(px(30.0)).scroll_x();
        let tab = || {
            NodeSpec::column()
                .grow_width()
                .min_width(80.0)
                .height(px(30.0))
        };
        // Three tabs: 200 each, nothing to scroll.
        let mut t = T::new(bar());
        let tabs: Vec<u32> = (0..3).map(|_| t.node(0, tab())).collect();
        t.run(1000.0, 1000.0);
        for (k, &c) in tabs.iter().enumerate() {
            assert_eq!(t.size(c).w, 200.0);
            assert_eq!(t.pos(c).x, 200.0 * k as f32);
        }
        assert_eq!(t.tree.scroll_max[0].x, 0.0);
        // Ten tabs: 800 of min in a 600 bar, every tab at 80, 200 to scroll.
        let mut t = T::new(bar());
        let tabs: Vec<u32> = (0..10).map(|_| t.node(0, tab())).collect();
        t.run(1000.0, 1000.0);
        for (k, &c) in tabs.iter().enumerate() {
            assert_eq!(t.size(c).w, 80.0);
            assert_eq!(t.pos(c).x, 80.0 * k as f32);
        }
        assert_eq!(t.tree.scroll_max[0].x, 200.0);
        // Same bar without scroll_x: the mins still hold (grow is not
        // shrinkable), so the row overflows and clips instead.
        let mut t = T::new(NodeSpec::row().width(px(600.0)).height(px(30.0)));
        let tabs: Vec<u32> = (0..10).map(|_| t.node(0, tab())).collect();
        t.run(1000.0, 1000.0);
        for &c in &tabs {
            assert_eq!(t.size(c).w, 80.0);
        }
        // The floor as the tab's own content: `Min::FIT` under `Grow`.
        // Ten tabs each around an 80 px label split a 600 bar as the
        // numeric min did — the label is the min — and the fit resolves
        // to a number the spec keeps.
        let mut t = T::new(bar());
        let tabs: Vec<u32> = (0..10)
            .map(|_| {
                let c = t.node(0, tab().min_width(Min::FIT));
                t.text(c, 8);
                c
            })
            .collect();
        t.run(1000.0, 1000.0);
        for (k, &c) in tabs.iter().enumerate() {
            assert_eq!(t.size(c).w, 80.0);
            assert_eq!(t.pos(c).x, 80.0 * k as f32);
            assert_eq!(t.tree.specs[c as usize].layout.min_w, Min::px(80.0));
        }
        assert_eq!(t.tree.scroll_max[0].x, 200.0);
        // And with room, the same tabs split it: 3 × 200, the floor idle.
        let mut t = T::new(bar());
        let tabs: Vec<u32> = (0..3)
            .map(|_| {
                let c = t.node(0, tab().min_width(Min::FIT));
                t.text(c, 8);
                c
            })
            .collect();
        t.run(1000.0, 1000.0);
        for &c in &tabs {
            assert_eq!(t.size(c).w, 200.0);
        }
    }

    /// `Min::FIT` on the cross axis and under a percent: a 50%-tall cell
    /// in a 20-tall row floors at its 16-tall child, and a fit floor with
    /// nothing inside is no floor.
    #[test]
    fn min_fit_floors_a_percent_height_at_its_content() {
        let mut t = T::new(NodeSpec::row().width(px(100.0)).height(px(20.0)));
        let a = t.node(
            0,
            NodeSpec::column()
                .width(px(10.0))
                .height(Sizing::Percent(0.5))
                .min_height(Min::FIT),
        );
        t.node(a, NodeSpec::column().width(px(10.0)).height(px(16.0)));
        let b = t.node(
            0,
            NodeSpec::column()
                .width(px(10.0))
                .height(Sizing::Percent(0.5))
                .min_height(Min::FIT),
        );
        t.run(1000.0, 1000.0);
        assert_eq!(t.size(a).h, 16.0);
        assert_eq!(t.size(b).h, 10.0);
    }

    #[test]
    fn padding_asymmetric() {
        let mut t = T::new(
            NodeSpec::column()
                .width(px(100.0))
                .height(px(100.0))
                .padding(Edges {
                    l: 1.0,
                    r: 2.0,
                    t: 3.0,
                    b: 4.0,
                }),
        );
        let a = t.node(0, NodeSpec::row().fill());
        t.run(1000.0, 1000.0);
        assert_eq!(t.pos(a), Vec2::new(1.0, 3.0));
        assert_eq!(t.size(a), Size::new(97.0, 93.0));
    }
}