rhwp 0.9.1

HWP/HWPX document engine for Rust applications, WebAssembly, and CLI automation
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
//! Materialize geometry-confirmed grids as editable rhwp tables. Uncertain grids stay positional.
mod body;
mod composition;
mod native_borders;
mod wrap_width;
use super::{
    allocation::{message, Allocation},
    lowering::hu,
    recognition::{ParagraphProposal, ParagraphState},
    text_analysis::{reconstruct_line, LineJoin, ParagraphLayout, TextAlignment, TextLine},
    value_macro::request_json,
    ExtractedPage,
};
use crate::model::{
    control::Control,
    document::Document,
    paragraph::{CharShapeRef, LineSeg, Paragraph},
    shape::{CommonObjAttr, HorzRelTo, ShapeObject, TextWrap, VertRelTo},
    style::{
        border_width_index, Alignment, BorderLineType, LineSpacingType, TabDef, TabItem,
        PARA_ATTR2_LINE_WRAP_MASK,
    },
    table::{Cell, Table, TablePageBreak, VerticalAlign},
};
use serde_json::{json, Value};
use std::collections::{BTreeMap, BTreeSet};

/// Source spans refer to document IDs, but glyph fitting only reads this page's
/// source styles. Keep those IDs in the document and resolve a compact view for
/// the common measurer, without repeatedly copying all earlier pages' styles.
struct SourceStyles {
    request: std::sync::Arc<pdf_oxide::import_limits::ImportBudget>,
    ids: BTreeMap<u32, u32>,
    resolved: crate::renderer::style_resolver::ResolvedStyleSet,
    widths: std::cell::RefCell<BTreeMap<GlyphWidthKey, f64>>,
}

type GlyphWidthKey = (u32, usize, char, Option<(u8, i8)>);

type StyleCheckpoint = (usize, usize, usize, usize);
fn checkpoint(doc: &Document, typography: &std::cell::RefCell<Vec<Value>>) -> StyleCheckpoint {
    (
        doc.doc_info.para_shapes.len(),
        doc.doc_info.char_shapes.len(),
        doc.doc_info.tab_defs.len(),
        typography.borrow().len(),
    )
}
fn rollback(doc: &mut Document, typography: &std::cell::RefCell<Vec<Value>>, c: StyleCheckpoint) {
    doc.doc_info.para_shapes.truncate(c.0);
    doc.doc_info.char_shapes.truncate(c.1);
    doc.doc_info.tab_defs.truncate(c.2);
    typography.borrow_mut().truncate(c.3);
}

/// A source style and language slot are immutable for this page. Only the
/// ordinary ratio/spacing fields vary during fitting. Memoize exact calls to
/// the common measurer, including the unfitted source style as a distinct key.
/// No font metric, rounding, search order or candidate tolerance is replaced.
struct GlyphWidths<'a> {
    source: &'a SourceStyles,
    style_id: u32,
    lang: usize,
    character: char,
}
impl GlyphWidths<'_> {
    fn measure(
        &self,
        style: &crate::renderer::TextStyle,
        fit: Option<(u8, i8)>,
    ) -> Result<f64, String> {
        self.source.request.check().map_err(|e| e.to_string())?;
        let key = (self.style_id, self.lang, self.character, fit);
        if let Some(width) = self.source.widths.borrow().get(&key) {
            return Ok(*width);
        }
        let mut utf8 = [0; 4];
        let width = crate::renderer::layout::estimate_text_width_exact(
            self.character.encode_utf8(&mut utf8),
            style,
        );
        // Bound auxiliary memory independently of input glyph count. Reaching
        // the memo limit changes only reuse, never acceptance or measurement.
        let mut widths = self.source.widths.borrow_mut();
        if widths.len() < 16_384 {
            Allocation(&self.source.request)
                .tree_entry::<GlyphWidthKey, f64>()
                .map_err(message)?;
            widths.insert(key, width);
        }
        Ok(width)
    }
}
impl SourceStyles {
    fn new(
        doc: &Document,
        shapes: &[Option<u32>],
        request: &std::sync::Arc<pdf_oxide::import_limits::ImportBudget>,
    ) -> Result<Self, String> {
        let allocation = Allocation(request);
        let mut ids = allocation
            .collect(shapes.iter().flatten().copied())
            .map_err(message)?;
        ids.sort_unstable();
        ids.dedup();
        let mut char_styles = allocation.vec(ids.len()).map_err(message)?;
        for &id in &ids {
            let shape = doc
                .doc_info
                .char_shapes
                .get(id as usize)
                .ok_or("missing PDF source character style")?;
            char_styles.push(
                crate::renderer::style_resolver::try_resolve_single_char_style(
                    shape,
                    &doc.doc_info,
                    96.,
                    &allocation,
                )
                .map_err(message)?,
            );
        }
        // The empty projection owns one Arc node; no input font table or
        // character record is copied just to resolve this page's selected IDs.
        allocation
            .bytes(
                2 * std::mem::size_of::<usize>()
                    + std::mem::size_of::<crate::renderer::native_pua_fonts::NativePuaFonts>(),
            )
            .map_err(message)?;
        let resolved = crate::renderer::style_resolver::ResolvedStyleSet {
            char_styles,
            resolved_dpi: Some(96.),
            ..Default::default()
        };
        let mut mapping = BTreeMap::new();
        for (local, id) in ids.into_iter().enumerate() {
            allocation.tree_entry::<u32, u32>().map_err(message)?;
            mapping.insert(id, local as u32);
        }
        Ok(Self {
            request: std::sync::Arc::clone(request),
            ids: mapping,
            resolved,
            widths: Default::default(),
        })
    }
    fn text_style(&self, id: u32, lang: usize) -> Result<crate::renderer::TextStyle, String> {
        let local = *self
            .ids
            .get(&id)
            .ok_or("missing PDF source style projection")?;
        let allocation = Allocation(&self.request);
        let style = self
            .resolved
            .char_styles
            .get(local as usize)
            .ok_or("missing PDF resolved character style")?;
        allocation
            .bytes(std::mem::size_of::<crate::renderer::TextStyle>())
            .map_err(message)?;
        let reserve_family = |language| -> Result<(), String> {
            allocation
                .bytes(style.font_family_for_lang(language).len())
                .map_err(message)?;
            if let Some(face) = style.metric_face_for_lang(language) {
                allocation.bytes(face.len()).map_err(message)?;
            }
            if let Some(face) = style.original_glyph_metric_face_for_lang(language) {
                allocation
                    .bytes(2 * std::mem::size_of::<usize>() + std::mem::size_of::<String>())
                    .map_err(message)?;
                allocation.bytes(face.len()).map_err(message)?;
            }
            Ok(())
        };
        reserve_family(lang)?;
        if lang != 1 && style.ascii_punct_latin_slot {
            allocation
                .bytes(std::mem::size_of::<crate::renderer::LatinSlotMetrics>())
                .map_err(message)?;
            reserve_family(1)?;
        }
        Ok(crate::renderer::layout::resolved_to_text_style(
            &self.resolved,
            local,
            lang,
        ))
    }
}

fn source_baseline_position(
    page: &ExtractedPage,
    doc: &Document,
    baseline: f32,
    span: usize,
    base_id: u32,
) -> Result<(i8, f64), String> {
    let source_delta = page.media_box.3 - page.spans[span].bbox.y - baseline;
    let size = doc.doc_info.char_shapes[base_id as usize].base_size as f64 / 100.;
    let position = (f64::from(source_delta) / size * 100.).round();
    if !position.is_finite() || !(-100. ..=100.).contains(&position) {
        return Err("source baseline displacement exceeds HWP character position range".into());
    }
    Ok((
        position as i8,
        (size * position / 100. - f64::from(source_delta)).abs(),
    ))
}

/// Source baseline is independent of whether horizontal reconstruction succeeds.
/// Both the legacy tab frame and ordinary paragraphs must retain this property.
fn positioned_span_style(
    page: &ExtractedPage,
    doc: &mut Document,
    line: &TextLine,
    span: usize,
    base_id: u32,
    cache: &mut BTreeMap<(u32, u8, i8, i8), u32>,
) -> Result<u32, String> {
    let allocation = Allocation(&page.import_budget);
    let (position, _) = source_baseline_position(page, doc, line.baseline, span, base_id)?;
    if position == 0 {
        return Ok(base_id);
    }
    let base = &doc.doc_info.char_shapes[base_id as usize];
    let key = (base_id, base.ratios[0], base.spacings[0], position);
    if let Some(id) = cache.get(&key) {
        return Ok(*id);
    }
    if doc.doc_info.char_shapes.len() >= u32::MAX as usize {
        return Err("PDF character style budget exceeded".into());
    }
    let mut style = allocation.clone_record(base).map_err(message)?;
    style.raw_data = None;
    style.char_offsets = [position; 7];
    let id = doc.doc_info.char_shapes.len() as u32;
    allocation
        .push(&mut doc.doc_info.char_shapes, style)
        .map_err(message)?;
    allocation
        .tree_entry::<(u32, u8, i8, i8), u32>()
        .map_err(message)?;
    cache.insert(key, id);
    Ok(id)
}

/// Points a source row's last visible glyph outline gives back. Hancom 2024
/// moved a column-wide title's final word to the next row until its last glyph
/// was about 0.12pt narrower; ratio rounds up to keep at least this guard.
const ROW_END_GUARD_PT: f64 = 0.2;

/// Fit the ordinary spacing field after selecting an ordinary ratio. The
/// common final measurer, including font-space and punctuation rules, remains
/// the authority for the emitted advance.
fn fit_spacing(
    style: &mut crate::renderer::TextStyle,
    widths: &GlyphWidths<'_>,
    ratio: u8,
    desired: f64,
) -> Result<Option<(i8, f64, f64)>, String> {
    style.ratio = f64::from(ratio) / 100.;
    style.letter_spacing = 0.;
    if let Some(latin) = &mut style.ascii_punct_latin {
        latin.ratio = style.ratio;
        latin.letter_spacing = 0.;
    }
    let nominal = widths.measure(style, Some((ratio, 0)))?;
    let guess = (desired / nominal - 1.) * 100.;
    if !nominal.is_finite()
        || nominal <= 0.
        || !guess.is_finite()
        || !(-52. ..=52.).contains(&guess)
    {
        return Ok(None);
    }
    let mut best: Option<(i8, f64, f64)> = None;
    for spacing in (guess.floor() as i32 - 2)..=(guess.ceil() as i32 + 2) {
        if !(-50..=50).contains(&spacing) {
            continue;
        }
        style.letter_spacing = style.font_size * f64::from(spacing) / 100.;
        if let Some(latin) = &mut style.ascii_punct_latin {
            latin.letter_spacing = style.letter_spacing;
        }
        let measured = widths.measure(style, Some((ratio, spacing as i8)))?;
        let error = (measured - desired).abs();
        if best.is_none_or(|(_, previous, _)| error < previous) {
            best = Some((spacing as i8, error, measured));
        }
    }
    Ok(best)
}

/// A blank separator has an observed extent, not an outline whose aspect ratio
/// must be recovered. Reject only when the combined legal ratio/spacing fields
/// cannot represent it, rather than requiring ratio alone to own the full gap.
fn fit_whitespace_advance(
    style: &mut crate::renderer::TextStyle,
    widths: &GlyphWidths<'_>,
    desired: f64,
) -> Result<Option<(u8, i8, f64)>, String> {
    let mut best: Option<(u8, i8, f64, f64)> = None;
    for ratio in 50..=200 {
        let Some((spacing, error, measured)) = fit_spacing(style, widths, ratio, desired)? else {
            continue;
        };
        let preference = (spacing.unsigned_abs(), ratio.abs_diff(100));
        if best.is_none_or(|(r, s, previous, _)| {
            error < previous
                || (error == previous && preference < (s.unsigned_abs(), r.abs_diff(100)))
        }) {
            best = Some((ratio, spacing, error, measured));
        }
    }
    Ok(best.map(|(r, s, _, measured)| (r, s, measured)))
}

/// Translate observed glyph metrics into the ordinary HWP character contract.
/// Source-font loss is explicit: width-compatible fallback does not recover its outline.
struct ObservedLineText {
    text: String,
    runs: Vec<(u32, u32)>,
    tabs: Vec<TabItem>,
    extensions: Vec<[u16; 7]>,
    positioning: Vec<PositioningTab>,
    evidence: Value,
}

struct PositioningTab {
    content_utf16: u32,
    cursor_px: f64,
    stop: u32,
}

fn observed_line_text(
    page: &ExtractedPage,
    doc: &mut Document,
    line: &TextLine,
    shape_ids: &[Option<u32>],
    styles: &SourceStyles,
    cache: &mut BTreeMap<(u32, u8, i8, i8), u32>,
    owner_x: f32,
    paragraph_baseline: f32,
) -> Result<ObservedLineText, String> {
    use crate::renderer::style_resolver::detect_lang_category;
    let allocation = Allocation(&page.import_budget);
    let content = reconstruct_line(&page.spans, &line.indices, allocation)?;
    let mut runs = Vec::new();
    let mut text = String::new();
    let mut tabs = Vec::new();
    let mut extensions = Vec::new();
    let mut positioning = Vec::new();
    let mut positioning_tabs = Vec::new();
    let mut offset = 0;
    let mut maximum_error: f64 = 0.;
    let mut maximum_baseline_error: f64 = 0.;
    let mut maximum_origin_error: f64 = 0.;
    let mut joint_whitespace_fits = 0;
    let mut source_space_movements = Vec::new();
    let origin = content
        .characters
        .first()
        .ok_or("empty source line")?
        .origin;
    let mut emitted_advance = 0.;
    // Nothing on the row follows the last visible glyph. A row that ends at the
    // column edge still has to fit under Hancom's rounding of every advance.
    let last_visible = content
        .characters
        .iter()
        .rposition(|glyph| !glyph.character.is_whitespace());
    for (index, glyph) in content.characters.iter().enumerate() {
        let base_id = shape_ids[glyph.span].ok_or("missing source style")?;
        let source = &page.spans[glyph.span];
        // PDF font size already includes script scaling. Preserve it and encode
        // only the observed displacement in ordinary HWP CHAROFFSET; setting
        // the superscript flag would shrink a small PDF glyph a second time.
        let (position, baseline_error) =
            source_baseline_position(page, doc, paragraph_baseline, glyph.span, base_id)?;
        maximum_baseline_error = maximum_baseline_error.max(baseline_error);
        let lang = detect_lang_category(glyph.character);
        let mut style = styles.text_style(base_id, lang)?;
        let widths = GlyphWidths {
            source: styles,
            style_id: base_id,
            lang,
            character: glyph.character,
        };
        // Glyph reconstruction and actual run placement must use the same
        // fractional width. Rounding each scalar can invent an unsupported
        // ratio (e.g. a 2.5pt TJ separator measured as 7 instead of 6.667px).
        let base_width = widths.measure(&style, None)?;
        if !base_width.is_finite() || base_width <= 0. {
            return Err("glyph advance needs separate shaping".into());
        }
        let fallback = source.font_name.is_empty() || source.font_name == "Unknown";
        // A missing source font needs metric-compatible fallback; declared fonts
        // retain their Tz scaling. Spaces have no outline and own their measured extent.
        let ratio = if glyph.character.is_whitespace() {
            // Whitespace has no outline to preserve. Its full observed advance
            // includes TJ/Tw and displacement across text-show boundaries; using
            // only the source font's nominal space can falsely exceed spacing's
            // range and discard an otherwise representable line.
            (f64::from(glyph.advance) * 96. / 72. / base_width * 100.).round()
        } else if fallback {
            (f64::from(glyph.nominal_advance) * 96. / 72. / base_width * 100.).round()
        } else {
            f64::from(source.horizontal_scaling).round()
        };
        let source_start = (f64::from(glyph.origin) - f64::from(origin)) * 96. / 72.;
        let source_advance = f64::from(glyph.advance) * 96. / 72.;
        let desired = source_start + source_advance - emitted_advance;
        // Hancom fits a row without its last glyph's spacing, so only a
        // narrower outline leaves room; spacing then restores the source end.
        let guarded = if last_visible == Some(index) && style.ratio > 0. {
            let percent = base_width / (style.ratio * 100.);
            ratio - (ROW_END_GUARD_PT * 96. / 72. / percent).ceil()
        } else {
            ratio
        };
        // Preserve the established representable path. Search the combined
        // field contract only when that path cannot encode a blank separator.
        let ordinary = if (50. ..=200.).contains(&ratio) {
            let mut fit = None;
            if guarded != ratio && (50. ..=200.).contains(&guarded) {
                fit = fit_spacing(&mut style, &widths, guarded as u8, desired)?
                    .map(|(s, _, measured)| (guarded as u8, s, measured));
            }
            // The guard is a margin, not a source displacement: drop it
            // rather than the line when spacing cannot restore the end.
            if fit.is_none() {
                fit = fit_spacing(&mut style, &widths, ratio as u8, desired)?
                    .map(|(s, _, measured)| (ratio as u8, s, measured));
            }
            fit
        } else {
            None
        };
        let mut fit = if let Some(fit) = ordinary {
            Some(fit)
        } else if glyph.character.is_whitespace() {
            fit_whitespace_advance(&mut style, &widths, desired)?
        } else {
            None
        };
        // A PDF source space and the displacement following it are distinct.
        // Keep the scalar at the ordinary font size, and infer only the excess
        // forward movement as a standard positioning tab. A small/backtracking
        // advance still needs a representable character metric or is deferred.
        let mut space_movement = false;
        if fit.is_none() && glyph.source_character.is_some() && glyph.character == ' ' {
            let ordinary_space =
                styles.text_style(base_id, detect_lang_category(glyph.character))?;
            let measured = widths.measure(&ordinary_space, None)?;
            if measured.is_finite() && measured > 0. && desired > measured {
                fit = Some((100, 0, measured));
                space_movement = true;
            }
        }
        // A source-less geometric separator is an inference, not an authored
        // space with an out-of-range character style. Preserve actual source
        // whitespace; only the inferred separator may become a positioning tab.
        if fit.is_none() && glyph.character.is_whitespace() && glyph.source_character.is_none() {
            let stop_pt = glyph.origin + glyph.advance - owner_x;
            let stop = u32::try_from(physical_margin(stop_pt)?)
                .map_err(|_| "invalid inferred PDF tab position")?;
            allocation
                .push(
                    &mut tabs,
                    TabItem {
                        position: stop,
                        ..Default::default()
                    },
                )
                .map_err(message)?;
            // The stop is editable; an observed advance is not a fixed-width
            // tab cache. Reuse the standard no-cache marker so insertion before
            // the tab still reaches the same stop in measure, paint and saves.
            allocation
                .push(
                    &mut extensions,
                    crate::model::paragraph::TAB_EXT_PLACEHOLDER,
                )
                .map_err(message)?;
            allocation
                .push(
                    &mut positioning,
                    PositioningTab {
                        content_utf16: offset,
                        cursor_px: f64::from(origin - owner_x) * 96. / 72. + emitted_advance,
                        stop,
                    },
                )
                .map_err(message)?;
            let id = positioned_span_style(page, doc, line, glyph.span, base_id, cache)?;
            if runs.last().is_none_or(|&(_, previous)| previous != id) {
                allocation.push(&mut runs, (offset, id)).map_err(message)?;
            }
            allocation
                .push(
                    &mut positioning_tabs,
                    request_json!(allocation, {"emittedContentUtf16Start":offset,
                "sourceSpan":glyph.span,"sourceCharacter":null,"sourceUtf16":null,
                "inference":"positioning_tab_from_observed_gap","authoredTab":"unknown",
                "observedGapPt":glyph.advance,"stopPt":stop_pt,
                "tabWidthPolicy":"standard_tab_stop_without_cached_advance"})?,
                )
                .map_err(message)?;
            allocation.push_char(&mut text, '\t').map_err(message)?;
            offset += 1;
            let measured = (f64::from(stop) / 200. - f64::from(origin - owner_x)) * 96. / 72.
                - emitted_advance;
            maximum_origin_error = maximum_origin_error.max((measured - desired).abs() * 72. / 96.);
            emitted_advance += measured;
            continue;
        }
        let (ratio, spacing, measured) = if let Some(fit) = fit {
            fit
        } else if glyph.character.is_whitespace() {
            return Err("source whitespace advance exceeds combined HWP character range".into());
        } else if !(50. ..=200.).contains(&ratio) {
            return Err("source glyph width exceeds HWP ratio range".into());
        } else {
            return Err("source displacement exceeds HWP spacing range".into());
        };
        if ordinary.is_none() && glyph.character.is_whitespace() && !space_movement {
            joint_whitespace_fits += 1;
        }
        emitted_advance += measured;
        let key = (base_id, ratio, spacing, position);
        let id = if ratio == 100 && spacing == 0 && position == 0 {
            base_id
        } else if let Some(id) = cache.get(&key) {
            *id
        } else {
            if doc.doc_info.char_shapes.len() >= u32::MAX as usize {
                return Err("PDF character style budget exceeded".into());
            }
            let mut character_style = allocation
                .clone_record(&doc.doc_info.char_shapes[base_id as usize])
                .map_err(message)?;
            character_style.raw_data = None;
            character_style.ratios = [ratio; 7];
            character_style.spacings = [spacing; 7];
            character_style.char_offsets = [position; 7];
            let id = doc.doc_info.char_shapes.len() as u32;
            allocation
                .push(&mut doc.doc_info.char_shapes, character_style)
                .map_err(message)?;
            allocation
                .tree_entry::<(u32, u8, i8, i8), u32>()
                .map_err(message)?;
            cache.insert(key, id);
            id
        };
        if runs.last().is_none_or(|&(_, previous)| previous != id) {
            allocation.push(&mut runs, (offset, id)).map_err(message)?;
        }
        allocation
            .push_char(&mut text, glyph.character)
            .map_err(message)?;
        offset += glyph.character.len_utf16() as u32;
        let mut movement = 0.;
        if space_movement {
            let stop_pt = glyph.origin + glyph.advance - owner_x;
            let stop = u32::try_from(physical_margin(stop_pt)?)
                .map_err(|_| "invalid source-space PDF tab position")?;
            allocation
                .push(
                    &mut tabs,
                    TabItem {
                        position: stop,
                        ..Default::default()
                    },
                )
                .map_err(message)?;
            allocation
                .push(
                    &mut extensions,
                    crate::model::paragraph::TAB_EXT_PLACEHOLDER,
                )
                .map_err(message)?;
            allocation
                .push(
                    &mut positioning,
                    PositioningTab {
                        content_utf16: offset,
                        cursor_px: f64::from(origin - owner_x) * 96. / 72. + emitted_advance,
                        stop,
                    },
                )
                .map_err(message)?;
            allocation.push(&mut source_space_movements, request_json!(allocation, {"sourceSpan":glyph.span,
                "sourceCharacter":glyph.source_character,"sourceUtf16":glyph.source_utf16,
                "emittedSpaceUtf16Start":offset-glyph.character.len_utf16() as u32,
                "emittedTabUtf16Start":offset,"stopPt":stop_pt,
                "inference":"forward_movement_after_preserved_source_space",
                "authoredTab":"unknown","tabWidthPolicy":"standard_tab_stop_without_cached_advance"})?).map_err(message)?;
            allocation.push_char(&mut text, '\t').map_err(message)?;
            offset += 1;
            movement = (f64::from(stop) / 200. - f64::from(origin - owner_x)) * 96. / 72.
                - emitted_advance;
            emitted_advance += movement;
        }
        maximum_origin_error =
            maximum_origin_error.max((measured + movement - desired).abs() * 72. / 96.);
        maximum_error = maximum_error.max((measured + movement - source_advance).abs() * 72. / 96.);
    }
    Ok(ObservedLineText {
        text,
        runs,
        tabs,
        extensions,
        positioning,
        evidence: request_json!(allocation, {
            "characters":content.characters,
            "positioningTabs":positioning_tabs,
            "maximumAdvanceQuantizationErrorPt":maximum_error,
            "jointWhitespaceFits":joint_whitespace_fits,
            "sourceSpaceMovements":source_space_movements,
            "maximumOriginQuantizationErrorPt":maximum_origin_error,
            "maximumBaselineQuantizationErrorPt":maximum_baseline_error,
            "lineBaselinePt":line.baseline,
            "paragraphBaselinePt":paragraph_baseline,
            "baselinePolicy":"observed glyph origins encoded as ordinary character positions on the paragraph grid",
            "status":"observed_origins_with_nominal_widths",
            "hwpQuantization":"integer-percent ratio, spacing and character position; no source-font outline recovery"
        })?,
    })
}

/// Each inferred gap names a source destination, whereas a standard tab visits
/// the next stop in the paragraph-wide definition. Other rows may introduce
/// intervening stops. Traverse them with ordinary tabs, preserving every source
/// scalar and retaining the same standard navigation after editing and saving.
fn materialize_positioning_tabs(
    para: &mut Paragraph,
    tabs: &[TabItem],
    positioning: &[PositioningTab],
    allocation: Allocation<'_>,
) -> Result<Vec<Value>, String> {
    use crate::renderer::{hwpunit_to_px, layout::find_next_tab_stop, TabStop};
    if positioning.is_empty() {
        return Ok(Vec::new());
    }
    let stops: Vec<_> = allocation
        .collect(tabs.iter().map(|tab| TabStop {
            position: hwpunit_to_px(tab.position as i32, 96.) / 2.,
            tab_type: tab.tab_type,
            fill_type: tab.fill_type,
        }))
        .map_err(message)?;
    let mut repeats = BTreeMap::new();
    let mut evidence = Vec::new();
    let mut extra = 0_usize;
    let original_length = para.text.chars().count();
    for tab in positioning {
        // All destinations are explicit and inside their source owner. The
        // final common wrap trial checks its actual available width separately.
        let (next, _, _) = find_next_tab_stop(tab.cursor_px, &stops, 0., false, 0., 0.);
        let first = stops
            .binary_search_by(|stop| stop.position.total_cmp(&next))
            .map_err(|_| "inferred tab has no reachable standard stop")?;
        let target = tabs
            .binary_search_by_key(&tab.stop, |stop| stop.position)
            .map_err(|_| "missing inferred tab destination")?;
        let count = target
            .checked_sub(first)
            .map(|delta| delta + 1)
            .ok_or("inferred tab destination precedes the next standard stop")?;
        extra = extra
            .checked_add(count - 1)
            .ok_or("inferred tab navigation exceeds PDF text budget")?;
        if original_length + extra > super::text_analysis::MAX_GLYPHS {
            return Err("inferred tab navigation exceeds PDF text budget".into());
        }
        allocation.tree_entry::<u32, usize>().map_err(message)?;
        repeats.insert(tab.content_utf16, count);
        allocation
            .push(
                &mut evidence,
                request_json!(allocation, {"paragraphContentUtf16Start":tab.content_utf16,
            "stopHwpUnits":tab.stop,"emittedStandardTabs":count,
            "policy":"ordinary_next_stop_navigation; source_scalars_unchanged"})?,
            )
            .map_err(message)?;
    }
    if extra == 0 {
        return Ok(evidence);
    }
    let mut text = String::new();
    let mut extensions = Vec::new();
    let mut offsets = BTreeMap::new();
    let (mut old, mut new, mut tab_index) = (0, 0, 0);
    for ch in para.text.chars() {
        allocation.tree_entry::<u32, u32>().map_err(message)?;
        offsets.insert(old, new);
        let count = repeats.get(&old).copied().unwrap_or(1);
        if ch == '\t' {
            let extension = *para
                .tab_extended
                .get(tab_index)
                .ok_or("missing PDF tab extension")?;
            if count > 1 && !crate::model::paragraph::tab_ext_is_placeholder(&extension) {
                return Err("only inferred uncached tabs may traverse shared stops".into());
            }
            allocation
                .reserve(&mut extensions, count)
                .map_err(message)?;
            extensions.extend(std::iter::repeat_n(extension, count));
            tab_index += 1;
        }
        for _ in 0..count {
            allocation.push_char(&mut text, ch).map_err(message)?;
        }
        old += ch.len_utf16() as u32;
        new += ch.len_utf16() as u32 * count as u32;
    }
    allocation.tree_entry::<u32, u32>().map_err(message)?;
    offsets.insert(old, new);
    let convert = |position| {
        offsets
            .get(&position)
            .copied()
            .ok_or("PDF positioning metadata is not on a scalar boundary")
    };
    for run in &mut para.char_shapes {
        run.start_pos = convert(run.start_pos)?;
    }
    for line in &mut para.line_segs {
        line.text_start = convert(line.text_start)?;
    }
    para.text = text;
    para.tab_extended = extensions;
    Ok(evidence)
}

fn array(v: &Value) -> Result<&Vec<Value>, String> {
    v.as_array()
        .ok_or_else(|| "invalid recognition array".into())
}
fn number(v: &Value) -> Result<f32, String> {
    v.as_f64()
        .filter(|n| n.is_finite())
        .map(|n| n as f32)
        .ok_or_else(|| "invalid recognition coordinate".into())
}
fn index(v: &Value) -> Result<usize, String> {
    v.as_u64()
        .and_then(|n| usize::try_from(n).ok())
        .ok_or_else(|| "invalid recognition index".into())
}
fn refs(v: &Value, allocation: Allocation<'_>) -> Result<Vec<usize>, String> {
    allocation.try_collect(array(v)?.iter().map(index))
}

fn physical_margin(points: f32) -> Result<i32, String> {
    // Odd IR margin values encode CHAR units, not sub-HWPUNIT PDF precision.
    i32::try_from(hu(points)?)
        .ok()
        .and_then(|value| value.checked_mul(2))
        .ok_or_else(|| "physical paragraph measure exceeds format range".into())
}

// PDF TJ displacements can express word spaces without a U+0020 glyph.
// Positional boxes and reconstructed table paragraphs must recover the same
// text from the same glyph origins, before either path measures the line.
pub(super) fn span_text_with_spaces(
    span: &pdf_oxide::layout::TextSpan,
    allocation: Allocation<'_>,
) -> Result<String, String> {
    let reconstructed = reconstruct_line(std::slice::from_ref(span), &[0], allocation);
    allocation.0.check().map_err(|error| error.to_string())?;
    if let Ok(content) = reconstructed {
        return Ok(content.text);
    }
    // Preserve the pre-existing positional path when complete glyph origins
    // are unavailable. This fallback does not assert recovered word boundaries.
    let mut text = String::new();
    for (j, ch) in span.text.chars().enumerate() {
        if j > 0 {
            if let (Some(x), Some(prev), Some(width)) = (
                span.char_x_offsets.get(j),
                span.char_x_offsets.get(j - 1),
                span.char_widths.get(j - 1),
            ) {
                if x - prev - width > span.font_size * 0.2
                    && !text.ends_with(char::is_whitespace)
                    && !ch.is_whitespace()
                {
                    allocation.push_char(&mut text, ' ').map_err(message)?;
                }
            }
        }
        allocation.push_char(&mut text, ch).map_err(message)?;
    }
    Ok(text)
}

fn text_with_spaces(
    page: &ExtractedPage,
    indices: &[usize],
) -> Result<(String, Vec<(u32, usize)>), String> {
    let allocation = Allocation(&page.import_budget);
    let mut text = String::new();
    let mut runs = Vec::new();
    let mut end = None;
    for &i in indices {
        let span = &page.spans[i];
        if end.is_some_and(|x| span.bbox.x - x > span.font_size * 0.2)
            && !text.ends_with(char::is_whitespace)
            && !span.text.starts_with(char::is_whitespace)
        {
            allocation.push_char(&mut text, ' ').map_err(message)?;
        }
        allocation
            .push(&mut runs, (text.encode_utf16().count() as u32, i))
            .map_err(message)?;
        allocation
            .push_str(&mut text, &span_text_with_spaces(span, allocation)?)
            .map_err(message)?;
        end = Some(span.bbox.x + span.bbox.width);
    }
    Ok((text, runs))
}

type LineText = (String, Vec<(u32, usize)>, Vec<TabItem>, Vec<[u16; 7]>);

fn text_with_tabs(
    page: &ExtractedPage,
    indices: &[usize],
    cell_x: f32,
) -> Result<LineText, String> {
    let allocation = Allocation(&page.import_budget);
    let mut text = String::new();
    let mut runs = Vec::new();
    let mut tabs = Vec::new();
    let mut extensions = Vec::new();
    let mut end = None;
    for &i in indices {
        let span = &page.spans[i];
        if let Some(previous_end) = end.filter(|x| span.bbox.x - x > span.font_size * 0.2) {
            allocation.push_char(&mut text, '\t').map_err(message)?;
            allocation
                .push(
                    &mut tabs,
                    TabItem {
                        position: u32::try_from(physical_margin(span.bbox.x - cell_x)?)
                            .map_err(|_| "invalid PDF tab position")?,
                        ..Default::default()
                    },
                )
                .map_err(message)?;
            let width = u16::try_from(hu(span.bbox.x - previous_end)?)
                .map_err(|_| "PDF span gap exceeds inline tab width")?;
            allocation
                .push(&mut extensions, [width, 0, 0, 0, 0, 0, 9])
                .map_err(message)?;
        }
        allocation
            .push(&mut runs, (text.encode_utf16().count() as u32, i))
            .map_err(message)?;
        allocation
            .push_str(&mut text, &span_text_with_spaces(span, allocation)?)
            .map_err(message)?;
        end = Some(span.bbox.x + span.bbox.width);
    }
    Ok((text, runs, tabs, extensions))
}

/// Recognition uses text-content UTF-16 positions. Publish every IR position
/// on the same HWP paragraph stream axis, where an extended tab owns 8 units.
/// Otherwise save/reopen expands tabs but leaves later styles and line starts
/// pointing into the preceding line. Floating paint slots are attached later.
fn finalize_text_axis(para: &mut Paragraph, allocation: Allocation<'_>) -> Result<(), String> {
    let mut content_to_stream = BTreeMap::new();
    let mut offsets = allocation.vec(para.text.chars().count()).map_err(message)?;
    let (mut content, mut stream) = (0, 0);
    for ch in para.text.chars() {
        allocation.tree_entry::<u32, u32>().map_err(message)?;
        content_to_stream.insert(content, stream);
        offsets.push(stream);
        content += ch.len_utf16() as u32;
        stream += Paragraph::char_stream_len(ch);
    }
    allocation.tree_entry::<u32, u32>().map_err(message)?;
    content_to_stream.insert(content, stream);
    let convert = |pos| {
        content_to_stream
            .get(&pos)
            .copied()
            .ok_or_else(|| "PDF text metadata is not on a scalar boundary".to_string())
    };
    for shape in &mut para.char_shapes {
        shape.start_pos = convert(shape.start_pos)?;
    }
    for line in &mut para.line_segs {
        line.text_start = convert(line.text_start)?;
    }
    para.char_offsets = offsets;
    para.char_count = stream + 1;
    Ok(())
}

struct FlowItem {
    top: f32,
    bottom: f32,
    paragraph: Paragraph,
}

fn paragraph(
    page: &ExtractedPage,
    doc: &mut Document,
    proposal: &ParagraphProposal,
    cell_box: &Value,
    shape_ids: &[Option<u32>],
    fixed_line_height: bool,
    styles: &SourceStyles,
    typography: &std::cell::RefCell<Vec<Value>>,
) -> Result<FlowItem, String> {
    let allocation = Allocation(&page.import_budget);
    let lines = &proposal.lines;
    let first = lines.first().ok_or("paragraph has no lines")?;
    let cell_x = number(&cell_box["x"])?;
    let cell_width = number(&cell_box["width"])?;
    // PDF observations are in points (72 dpi). Publish the same HWP row
    // metrics as edit reflow, and derive the row origin from the observed
    // baseline. A separate 0.95em ascent made the first edit move ink upward.
    let row_metrics = allocation.try_collect(lines.iter().map(|line| {
        let size = f64::from(hu(line.font)?) / 100.;
        Ok::<_, String>(crate::renderer::composer::frame_metrics_for_line(
            size,
            size,
            LineSpacingType::Percent,
            100.,
            72.,
        ))
    }))?;
    let mut row_tops: Vec<_> = allocation
        .collect(
            lines
                .iter()
                .zip(&row_metrics)
                .map(|(line, metrics)| line.baseline - metrics.baseline_distance as f32 / 100.),
        )
        .map_err(message)?;
    let top = row_tops[0];
    // Observed row baselines are not an authored paragraph grid. Infer one
    // ordinary fixed advance, then encode each original glyph's displacement
    // with the existing HWP character-position contract. This preserves PDF
    // glyph origins without manufacturing hard breaks or per-backend offsets.
    let fixed_advance = if fixed_line_height {
        let (distance, weight) = row_tops.iter().enumerate().skip(1).fold(
            (0.0_f64, 0.0_f64),
            |(distance, weight), (index, row_top)| {
                let index = index as f64;
                (
                    distance + index * f64::from(*row_top - top),
                    weight + index * index,
                )
            },
        );
        let advance = if weight > 0.0 {
            (distance / weight) as f32
        } else {
            first.font
        };
        let advance = hu(advance)? as f32 / 100.;
        let mut current = top;
        for (row_top, metrics) in row_tops.iter_mut().zip(&row_metrics) {
            *row_top = current;
            current += advance.max(metrics.line_height as f32 / 100.);
        }
        Some(advance)
    } else {
        None
    };
    let layout = proposal
        .layout
        .as_ref()
        .ok_or("paragraph layout deferred")?;
    let centered = layout.alignment == TextAlignment::Center
        || (!fixed_line_height
            && lines.iter().all(|l| {
                ((l.left + l.right) / 2. - cell_x - cell_width / 2.).abs() <= l.font * 0.75
            }));
    allocation
        .bytes(std::mem::size_of::<LineSeg>())
        .map_err(message)?;
    let mut para = Paragraph::new_empty();
    para.char_count_msb = true;
    para.char_shapes.clear();
    para.line_segs.clear();
    let mut last_end = top;
    let mut utf16 = 0;
    let mut tabs = Vec::new();
    let mut positioning = Vec::new();
    let typography_start = typography.borrow().len();
    let mut glyph_styles = BTreeMap::new();
    for (line_no, line) in lines.iter().enumerate() {
        let mut materialization = None;
        let indices = &line.indices;
        if indices.is_empty() {
            return Err("empty recognized line".into());
        }
        let metrics = &row_metrics[line_no];
        let line_height = metrics.line_height as f32 / 100.;
        let line_top = row_tops[line_no];
        let next_top = row_tops
            .get(line_no + 1)
            .copied()
            .unwrap_or(line_top + line_height);
        let mut line_positioning = Vec::new();
        let (text, runs): (String, Vec<(u32, u32)>) = if fixed_line_height {
            let before_styles = doc.doc_info.char_shapes.len();
            let observed = observed_line_text(
                page,
                doc,
                line,
                shape_ids,
                styles,
                &mut glyph_styles,
                cell_x,
                line_top + metrics.baseline_distance as f32 / 100.,
            );
            page.import_budget
                .check()
                .map_err(|error| error.to_string())?;
            match observed {
                Ok(observed) => {
                    materialization = Some(observed.evidence);
                    allocation
                        .reserve(&mut tabs, observed.tabs.len())
                        .map_err(message)?;
                    tabs.extend(observed.tabs);
                    allocation
                        .reserve(&mut para.tab_extended, observed.extensions.len())
                        .map_err(message)?;
                    para.tab_extended.extend(observed.extensions);
                    line_positioning = observed.positioning;
                    (observed.text, observed.runs)
                }
                Err(reason) => {
                    doc.doc_info.char_shapes.truncate(before_styles);
                    glyph_styles.retain(|_, id| (*id as usize) < before_styles);
                    materialization = Some(
                        request_json!(allocation, {"status":"unverified_legacy_line_frame","reason":reason,
                            "baselinePolicy":"observed_source_position_preserved_independently_of_horizontal_metrics"})?,
                    );
                    let (text, runs, line_tabs, extensions) =
                        text_with_tabs(page, indices, cell_x)?;
                    allocation
                        .reserve(&mut tabs, line_tabs.len())
                        .map_err(message)?;
                    tabs.extend(line_tabs);
                    allocation
                        .reserve(&mut para.tab_extended, extensions.len())
                        .map_err(message)?;
                    para.tab_extended.extend(extensions);
                    (
                        text,
                        allocation.try_collect(runs.into_iter().map(|(offset, i)| {
                            let base = shape_ids[i].ok_or("missing source style")?;
                            positioned_span_style(page, doc, line, i, base, &mut glyph_styles)
                                .map(|id| (offset, id))
                        }))?,
                    )
                }
            }
        } else {
            let (text, runs) = text_with_spaces(page, indices)?;
            (
                text,
                allocation.try_collect(runs.into_iter().map(|(offset, i)| {
                    let base = shape_ids[i].ok_or("missing source style")?;
                    positioned_span_style(page, doc, line, i, base, &mut glyph_styles)
                        .map(|id| (offset, id))
                }))?,
            )
        };
        // A visual wrap does not terminate an editable paragraph or insert a hard break.
        // This inferred word separator is recorded separately from source whitespace.
        if matches!(
            proposal.line_joins.get(line_no),
            Some(LineJoin::InferredWordSpace)
        ) {
            allocation.push_char(&mut para.text, ' ').map_err(message)?;
            utf16 += 1;
        }
        let line_start = utf16;
        allocation
            .reserve(&mut positioning, line_positioning.len())
            .map_err(message)?;
        positioning.extend(line_positioning.into_iter().map(|mut tab| {
            tab.content_utf16 += line_start;
            tab
        }));
        if let Some(mut evidence) = materialization {
            allocation
                .set(
                    &mut evidence,
                    "context",
                    request_json!(allocation, proposal.context)?,
                )
                .map_err(message)?;
            allocation
                .set(&mut evidence, "spans", request_json!(allocation, indices)?)
                .map_err(message)?;
            allocation
                .set(
                    &mut evidence,
                    "lineIndex",
                    request_json!(allocation, line_no)?,
                )
                .map_err(message)?;
            allocation
                .set(
                    &mut evidence,
                    "paragraphUtf16Start",
                    request_json!(allocation, line_start)?,
                )
                .map_err(message)?;
            allocation
                .push(&mut typography.borrow_mut(), evidence)
                .map_err(message)?;
        }
        for (run_offset, style) in runs {
            allocation
                .push(
                    &mut para.char_shapes,
                    CharShapeRef {
                        start_pos: utf16 + run_offset,
                        char_shape_id: style,
                    },
                )
                .map_err(message)?;
        }
        allocation
            .push_str(&mut para.text, &text)
            .map_err(message)?;
        utf16 += text.encode_utf16().count() as u32;
        allocation
            .push(
                &mut para.line_segs,
                LineSeg {
                    text_start: line_start,
                    vertical_pos: hu(line_top - top)? as i32,
                    line_height: metrics.line_height,
                    text_height: metrics.text_height,
                    baseline_distance: metrics.baseline_distance,
                    line_spacing: hu((next_top - line_top - line_height).max(0.))? as i32,
                    column_start: if centered {
                        0
                    } else {
                        hu(line.left - cell_x)? as i32
                    },
                    segment_width: if centered {
                        hu(cell_width)? as i32
                    } else {
                        hu(cell_width - (line.left - cell_x))? as i32
                    },
                    tag: LineSeg::TAG_SINGLE_SEGMENT_LINE,
                },
            )
            .map_err(message)?;
        last_end = line_top + line_height;
    }
    allocation
        .sort_by(&mut tabs, |a, b| a.position.cmp(&b.position))
        .map_err(message)?;
    tabs.dedup_by_key(|tab| tab.position);
    let navigation = materialize_positioning_tabs(&mut para, &tabs, &positioning, allocation)?;
    if !navigation.is_empty() {
        if let Some(first) = typography.borrow_mut().get_mut(typography_start) {
            allocation
                .set(
                    first,
                    "paragraphPositioningTabs",
                    request_json!(allocation, navigation)?,
                )
                .map_err(message)?;
        }
    }
    finalize_text_axis(&mut para, Allocation(&page.import_budget))?;
    para.has_para_text = true;
    let mut style = allocation
        .clone_record(&doc.doc_info.para_shapes[0])
        .map_err(message)?;
    style.raw_data = None;
    if fixed_line_height {
        style.attr1 &= !(1 << 8);
    }
    // Stored LineSegs preserve observed PDF lines. The editable paragraph must
    // still allow normal wrapping: SQUEEZE makes Hancom compress newly typed
    // multiline text onto one line although native reflow creates several.
    style.attr2 &= !PARA_ATTR2_LINE_WRAP_MASK;
    style.alignment = if centered {
        Alignment::Center
    } else {
        Alignment::Left
    };
    style.margin_left = if centered {
        0
    } else {
        physical_margin(layout.margin_left)?
    };
    style.margin_right = 0;
    style.indent = if centered {
        0
    } else {
        let value = physical_margin(layout.indent.abs())?;
        if layout.indent < 0. {
            -value
        } else {
            value
        }
    };
    style.spacing_before = 0;
    style.spacing_after = 0;
    if !tabs.is_empty() {
        style.tab_def_id = u16::try_from(doc.doc_info.tab_defs.len())
            .map_err(|_| "too many PDF line tab definitions")?;
        allocation
            .push(
                &mut doc.doc_info.tab_defs,
                TabDef {
                    tabs,
                    ..Default::default()
                },
            )
            .map_err(message)?;
    }
    style.line_spacing_type = if fixed_line_height {
        LineSpacingType::Fixed
    } else {
        LineSpacingType::Percent
    };
    style.line_spacing = if let Some(advance) = fixed_advance {
        physical_margin(advance)?
    } else if lines.len() > 1 {
        ((lines[1].baseline - first.baseline) / first.font * 100.).round() as i32
    } else {
        100
    };
    // [#8355] 한/글은 고정 줄간격 문단의 마지막 줄도 고정 피치를 차지한다 — 한/글 저장본의
    // 고정 줄간격 문단은 마지막 줄까지 `줄 높이 + 줄간격 = 고정값 / 2` 다. 마지막 줄 줄간격을
    // 0 으로 두고 문단 끝을 줄 바닥으로 재면, 그 뒤 간격에 같은 몫이 한 번 더 들어가 한/글에서
    // 문단마다 그만큼 내려간다(편집 우선 가져오기 저장본 1쪽 오른쪽 단 −37px).
    //
    // [#8361] 저장 줄도 한/글이 짜는 대로 `첫 줄 + 줄 번호 × 고정 피치` 에 둔다. PDF 관측 줄
    // 위치에는 피치가 짧은 줄(줄간격 707HU 등)이 섞여, 한/글이 그 줄마다 1.5px 씩 더 내려가
    // 단 끝 줄이 다음 단으로 넘어갔다(13쪽 → 14쪽).
    let mut bottom = last_end;
    if fixed_line_height {
        let pitch = style.line_spacing / 2;
        if let Some(origin) = para.line_segs.first().map(|seg| seg.vertical_pos) {
            for (row, seg) in para.line_segs.iter_mut().enumerate() {
                seg.vertical_pos = origin + row as i32 * pitch;
                seg.line_spacing = (pitch - seg.line_height).max(0);
            }
        }
        if let Some(last) = para.line_segs.last() {
            bottom = top
                + (last.vertical_pos - para.line_segs[0].vertical_pos
                    + last.line_height
                    + last.line_spacing) as f32
                    / 100.;
        }
    }
    para.para_shape_id = doc.doc_info.para_shapes.len() as u16;
    allocation
        .push(&mut doc.doc_info.para_shapes, style)
        .map_err(message)?;
    Ok(FlowItem {
        top,
        bottom,
        paragraph: para,
    })
}

/// Body and cell owners publish the same ordinary-flow witness. Recorded PDF
/// rows alone do not prove that concatenating them produces a soft paragraph.
fn verified_paragraph(
    page: &ExtractedPage,
    doc: &mut Document,
    proposal: &ParagraphProposal,
    owner: pdf_oxide::geometry::Rect,
    shape_ids: &[Option<u32>],
    styles: &SourceStyles,
    typography: &std::cell::RefCell<Vec<Value>>,
) -> Result<FlowItem, String> {
    let allocation = Allocation(&page.import_budget);
    let start = typography.borrow().len();
    let mut item = paragraph(
        page,
        doc,
        proposal,
        &allocation.value(&owner).map_err(message)?,
        shape_ids,
        true,
        styles,
        typography,
    )?;
    if let Some(reason) = typography.borrow()[start..]
        .iter()
        .find(|r| r["status"] == "unverified_legacy_line_frame")
        .and_then(|r| r["reason"].as_str())
    {
        return Err(allocation
            .display(&format_args!(
                "glyph metrics require positioned source: {reason}"
            ))
            .map_err(message)?);
    }
    let wrap = wrap_width::fit(page, doc, proposal, owner, &mut item)?;
    page.import_budget.check().map_err(|e| e.to_string())?;
    if wrap["status"] == "source_reflow_unverified" {
        return Err(allocation
            .display(&format_args!(
                "source rows require separate owners: {}",
                wrap["reason"]
                    .as_str()
                    .unwrap_or("ordinary flow unverified")
            ))
            .map_err(message)?);
    }
    if let Some(first) = typography.borrow_mut().get_mut(start) {
        first["paragraphReflow"] = wrap;
    }
    Ok(item)
}

fn is_grid_rule(shape: &ShapeObject, grid: &Value) -> Result<bool, String> {
    let (x1, y1, x2, y2) = match shape {
        ShapeObject::Line(line) => (
            (line.common.horizontal_offset as f32 + line.start.x as f32) / 100.,
            (line.common.vertical_offset as f32 + line.start.y as f32) / 100.,
            (line.common.horizontal_offset as f32 + line.end.x as f32) / 100.,
            (line.common.vertical_offset as f32 + line.end.y as f32) / 100.,
        ),
        ShapeObject::Rectangle(r)
            if r.drawing.text_box.is_none() && r.drawing.fill.solid.is_some() =>
        {
            let c = &r.common;
            let (x, y, w, h) = (
                c.horizontal_offset as f32 / 100.,
                c.vertical_offset as f32 / 100.,
                c.width as f32 / 100.,
                c.height as f32 / 100.,
            );
            if w < h {
                (x + w / 2., y, x + w / 2., y + h)
            } else {
                (x, y + h / 2., x + w, y + h / 2.)
            }
        }
        _ => return Ok(false),
    };
    // Only consume axes within the reconstructed grid, never arbitrary interior artwork.
    let b = &grid["bbox"];
    let inside = x1.min(x2) >= number(&b["x"])? - 0.5
        && x1.max(x2) <= number(&b["x"])? + number(&b["width"])? + 0.5
        && y1.min(y2) >= number(&b["y"])? - 0.5
        && y1.max(y2) <= number(&b["y"])? + number(&b["height"])? + 0.5;
    if !inside {
        return Ok(false);
    }
    let on_axis = |values: &Value, at: f32| -> Result<bool, String> {
        Ok(array(values)?
            .iter()
            .any(|v| number(v).is_ok_and(|n| (n - at).abs() <= 0.5)))
    };
    Ok(
        ((y1 - y2).abs() <= 0.5 && on_axis(&grid["yBoundaries"], y1)?)
            || ((x1 - x2).abs() <= 0.5 && on_axis(&grid["xBoundaries"], x1)?),
    )
}

struct TableBuilder<'a> {
    table_id_base: u32,
    page: &'a ExtractedPage,
    controls: &'a [Control],
    grids: &'a [Value],
    recognized: &'a [Value],
    span_controls: &'a [Option<usize>],
    shape_ids: &'a [Option<u32>],
    image_controls: &'a [usize],
    rule_controls: &'a [Option<usize>],
    styles: &'a SourceStyles,
    typography: &'a std::cell::RefCell<Vec<Value>>,
}

fn cell_border(
    doc: &mut Document,
    controls: &[Control],
    bounds: &Value,
    allocation: Allocation<'_>,
) -> Result<u16, String> {
    let (x, y, w, h) = (
        number(&bounds["x"])?,
        number(&bounds["y"])?,
        number(&bounds["width"])?,
        number(&bounds["height"])?,
    );
    let mut border = allocation
        .clone_record(&doc.doc_info.border_fills[1])
        .map_err(message)?;
    border.raw_data = None;
    for (side, (horizontal, at, from, to)) in [
        (false, x, y, y + h),
        (false, x + w, y, y + h),
        (true, y, x, x + w),
        (true, y + h, x, x + w),
    ]
    .into_iter()
    .enumerate()
    {
        let mut segments = Vec::new();
        let mut styles = BTreeSet::new();
        for control in controls {
            let Control::Shape(shape) = control else {
                continue;
            };
            let ShapeObject::Rectangle(r) = shape.as_ref() else {
                continue;
            };
            if r.drawing.text_box.is_some() {
                continue;
            }
            let Some(fill) = &r.drawing.fill.solid else {
                continue;
            };
            let c = &r.common;
            let (xx, yy, ww, hh) = (
                c.horizontal_offset as f32 / 100.,
                c.vertical_offset as f32 / 100.,
                c.width as f32 / 100.,
                c.height as f32 / 100.,
            );
            let (rule_h, rule_at, a, b) = if ww >= hh {
                (true, yy + hh / 2., xx, xx + ww)
            } else {
                (false, xx + ww / 2., yy, yy + hh)
            };
            let (a, b) = (a.max(from), b.min(to));
            if rule_h == horizontal && (rule_at - at).abs() <= 0.5 && b - a > 0.5 {
                allocation.push(&mut segments, (a, b)).map_err(message)?;
                allocation
                    .insert_set(
                        &mut styles,
                        (
                            fill.background_color,
                            border_width_index(ww.min(hh) as f64 * 25.4 / 72.),
                        ),
                    )
                    .map_err(message)?;
            }
        }
        allocation
            .sort_by(&mut segments, |a, b| a.0.total_cmp(&b.0))
            .map_err(message)?;
        let mut end = from;
        for (a, b) in segments {
            if a > end + 0.5 {
                return Err("cell border coverage gap".into());
            }
            end = end.max(b);
        }
        if end < to - 0.5 || styles.len() != 1 {
            return Err("nonuniform or missing cell side style".into());
        }
        let &(color, width) = styles.first().unwrap();
        border.borders[side].line_type = BorderLineType::Solid;
        border.borders[side].color = color;
        border.borders[side].width = width;
    }
    if let Some(i) = doc.doc_info.border_fills.iter().position(|b| b == &border) {
        return u16::try_from(i + 1).map_err(|e| e.to_string());
    }
    allocation
        .push(&mut doc.doc_info.border_fills, border)
        .map_err(message)?;
    u16::try_from(doc.doc_info.border_fills.len()).map_err(|e| e.to_string())
}

impl TableBuilder<'_> {
    fn build(
        &self,
        doc: &mut Document,
        candidate: usize,
        removed: &mut BTreeSet<usize>,
        stack: &mut BTreeSet<usize>,
        family: bool,
        source_paint: bool,
    ) -> Result<Table, String> {
        let allocation = Allocation(&self.page.import_budget);
        if stack.len() >= 8 || !allocation.insert_set(stack, candidate).map_err(message)? {
            return Err("invalid or too-deep nested table composition".into());
        }
        let result = self.build_inner(doc, candidate, removed, stack, family, source_paint);
        stack.remove(&candidate);
        result
    }

    fn build_inner(
        &self,
        doc: &mut Document,
        candidate: usize,
        removed: &mut BTreeSet<usize>,
        stack: &mut BTreeSet<usize>,
        family: bool,
        source_paint: bool,
    ) -> Result<Table, String> {
        let allocation = Allocation(&self.page.import_budget);
        let Self {
            page,
            controls,
            grids,
            recognized,
            span_controls,
            shape_ids,
            image_controls,
            rule_controls,
            ..
        } = self;
        let grid = grids.get(candidate).ok_or("missing recognized grid")?;
        let recognized_table = recognized
            .iter()
            .find(|r| r["candidate"].as_u64() == Some(candidate as u64))
            .ok_or("missing table recognition")?;
        if recognized_table["status"] != "GeometryConfirmed" {
            return Err("recognition_deferred".into());
        }
        let preserve_paint = source_paint;
        // Every PDF table consumes the same observed line metrics. Paint style
        // and whether this is a nested table cannot change source text placement.
        if page
            .recognition
            .cells
            .iter()
            .filter(|((table, _), _)| *table == candidate)
            .flat_map(|(_, region)| &region.paragraphs)
            .flat_map(|p| &p.span_indices)
            .any(|&i| super::text_clip::has_nonrectangular(page.span_clips[i].as_deref()))
        {
            return Err("source text clip requires positional editable owner".into());
        }
        let fixed_lines = true;
        let mut stroke_styles = BTreeSet::new();
        for c in *controls {
            if let Control::Shape(s) = c {
                if is_grid_rule(s, grid)? {
                    match s.as_ref() {
                        ShapeObject::Line(l) => {
                            allocation
                                .insert_set(
                                    &mut stroke_styles,
                                    (
                                        l.drawing.border_line.color,
                                        l.drawing.border_line.width as u32,
                                    ),
                                )
                                .map_err(message)?;
                        }
                        ShapeObject::Rectangle(r) => {
                            allocation
                                .insert_set(
                                    &mut stroke_styles,
                                    (
                                        r.drawing
                                            .fill
                                            .solid
                                            .as_ref()
                                            .ok_or("missing rule fill")?
                                            .background_color,
                                        r.common.width.min(r.common.height),
                                    ),
                                )
                                .map_err(message)?;
                        }
                        _ => {}
                    }
                }
            }
        }
        if !family && !preserve_paint && stroke_styles.len() != 1 {
            return Err("mixed or missing rule styles require further lowering".into());
        }
        let (color, width) = if preserve_paint {
            (0, 0)
        } else {
            *stroke_styles.first().ok_or("missing rule style")?
        };
        let mut border = allocation
            .clone_record(
                doc.doc_info
                    .border_fills
                    .get(1)
                    .ok_or("missing scaffold solid border")?,
            )
            .map_err(message)?;
        border.raw_data = None;
        for side in &mut border.borders {
            if family || preserve_paint {
                side.line_type = BorderLineType::None;
            } else {
                side.color = color;
                side.width = border_width_index(width as f64 / 100. * 25.4 / 72.);
            }
        }
        allocation
            .push(&mut doc.doc_info.border_fills, border)
            .map_err(message)?;
        let border_id =
            u16::try_from(doc.doc_info.border_fills.len()).map_err(|e| e.to_string())?;
        let b = &grid["bbox"];
        let extent = if preserve_paint {
            Some(super::table_paint::dimensions(grid, None)?)
        } else {
            None
        };
        let mut table = Table {
            row_count: u16::try_from(index(&grid["rows"])?).map_err(|e| e.to_string())?,
            col_count: u16::try_from(index(&grid["columns"])?).map_err(|e| e.to_string())?,
            border_fill_id: border_id,
            page_break: TablePageBreak::None,
            common: CommonObjAttr {
                width: extent.map_or_else(|| hu(number(&b["width"])?), |(w, _)| Ok(w))?,
                height: extent.map_or_else(|| hu(number(&b["height"])?), |(_, h)| Ok(h))?,
                horizontal_offset: hu(number(&b["x"])?)?,
                vertical_offset: hu(number(&b["y"])?)?,
                instance_id: self
                    .table_id_base
                    .checked_add(u32::try_from(candidate).map_err(|e| e.to_string())?)
                    .ok_or("table instance ID overflow")?,
                text_wrap: TextWrap::InFrontOfText,
                allow_overlap: true,
                ..Default::default()
            },
            ..Default::default()
        };
        table.row_sizes = allocation
            .filled(table.row_count as usize, table.col_count as i16)
            .map_err(message)?;
        for recognized_cell in array(&recognized_table["cells"])? {
            let cell_idx = index(&recognized_cell["cell"])?;
            let source = array(&grid["cells"])?
                .get(cell_idx)
                .ok_or("missing cell geometry")?;
            let cb = &source["bbox"];
            let cell_border_id = if family && !preserve_paint {
                cell_border(doc, controls, cb, allocation)?
            } else {
                border_id
            };
            let extent = if preserve_paint {
                Some(super::table_paint::dimensions(grid, Some(source))?)
            } else {
                None
            };
            allocation
                .bytes(std::mem::size_of::<Paragraph>() + std::mem::size_of::<LineSeg>())
                .map_err(message)?;
            let mut cell = Cell::new_empty(
                index(&source["column"])? as u16,
                index(&source["row"])? as u16,
                extent.map_or_else(|| hu(number(&cb["width"])?), |(w, _)| Ok(w))?,
                extent.map_or_else(|| hu(number(&cb["height"])?), |(_, h)| Ok(h))?,
                cell_border_id,
            );
            cell.col_span = index(&source["colspan"])? as u16;
            cell.row_span = index(&source["rowspan"])? as u16;
            cell.vertical_align = VerticalAlign::Top;
            cell.apply_inner_margin = true;
            cell.paragraphs[0].char_shapes = allocation
                .collect([CharShapeRef {
                    start_pos: 0,
                    char_shape_id: 0,
                }])
                .map_err(message)?;
            let image_ids = allocation.try_collect(
                refs(&source["imageIndices"], allocation)?
                    .into_iter()
                    .map(|i| {
                        image_controls
                            .get(i)
                            .copied()
                            .ok_or_else(|| "missing cell image owner".to_string())
                    }),
            )?;
            let content = allocation
                .set_from(
                    refs(&source["spanIndices"], allocation)?
                        .into_iter()
                        .filter_map(|i| span_controls[i])
                        .chain(image_ids.iter().copied()),
                )
                .map_err(message)?;
            let mixed = image_ids.iter().any(|image| {
                let a = paint_frame(&controls[*image]).unwrap();
                content.iter().any(|other| {
                    let b = paint_frame(&controls[*other]).unwrap();
                    other != image
                        && a.vertical_offset < b.vertical_offset + b.height
                        && b.vertical_offset < a.vertical_offset + a.height
                })
            });
            if mixed && array(&recognized_cell["childTableIndices"])?.is_empty() {
                // A PDF can interleave text paint and image glyphs within one
                // row. The cell is known; an inline paragraph is not. Keep the
                // original editable owners and paint order locally, rather
                // than rejecting the whole table or rasterizing its text.
                let mut original = allocation.try_collect(
                    content
                        .iter()
                        .map(|&i| allocation.clone_record(&controls[i]).map_err(message)),
                )?;
                allocation
                    .sort_by(&mut original, |a, b| {
                        paint_frame(a)
                            .unwrap()
                            .z_order
                            .cmp(&paint_frame(b).unwrap().z_order)
                    })
                    .map_err(message)?;
                let (x, y) = (hu(number(&cb["x"])?)? as i32, hu(number(&cb["y"])?)? as i32);
                for control in &mut original {
                    let a = match control {
                        Control::Shape(s) => s.common_mut(),
                        Control::Picture(p) => &mut p.common,
                        _ => unreachable!(),
                    };
                    a.horizontal_offset = (a.horizontal_offset as i32 - x) as u32;
                    a.vertical_offset = (a.vertical_offset as i32 - y) as u32;
                    a.horz_rel_to = HorzRelTo::Column;
                    a.vert_rel_to = VertRelTo::Para;
                    a.flow_with_text = true;
                    a.treat_as_char = false;
                    a.text_wrap = TextWrap::InFrontOfText;
                }
                let p = &mut cell.paragraphs[0];
                p.char_count_msb = true;
                p.control_mask |= 0x800;
                p.controls = original;
                p.ctrl_data_records = allocation
                    .collect((0..p.controls.len()).map(|_| None))
                    .map_err(message)?;
                allocation
                    .bytes(std::mem::size_of::<i64>())
                    .map_err(message)?;
                p.rebuild_char_offsets(
                    &allocation
                        .filled(p.controls.len(), 0usize)
                        .map_err(message)?,
                );
                allocation
                    .push(
                        &mut self.typography.borrow_mut(),
                        request_json!(allocation, {"status":"retained_interleaved_cell_content",
                    "candidate":candidate,"cell":cell_idx,"sourceControlCount":content.len(),
                    "paragraphGrouping":"unverified; original editable text owners preserved"})?,
                    )
                    .map_err(message)?;
                allocation.extend_set(removed, content).map_err(message)?;
                allocation.push(&mut table.cells, cell).map_err(message)?;
                continue;
            }
            let mut flow = Vec::new();
            let recognized_text = self
                .page
                .recognition
                .cells
                .get(&(candidate, cell_idx))
                .ok_or("missing typed cell text recognition")?;
            for proposal in &recognized_text.paragraphs {
                if proposal.status == ParagraphState::Confirmed {
                    if !fixed_lines {
                        allocation
                            .push(
                                &mut flow,
                                paragraph(
                                    page,
                                    doc,
                                    proposal,
                                    cb,
                                    shape_ids,
                                    false,
                                    self.styles,
                                    self.typography,
                                )?,
                            )
                            .map_err(message)?;
                        continue;
                    }
                    let before = checkpoint(doc, self.typography);
                    let owner = pdf_oxide::geometry::Rect::new(
                        number(&cb["x"])?,
                        number(&cb["y"])?,
                        number(&cb["width"])?,
                        number(&cb["height"])?,
                    );
                    match verified_paragraph(
                        page,
                        doc,
                        proposal,
                        owner,
                        shape_ids,
                        self.styles,
                        self.typography,
                    ) {
                        Ok(item) => {
                            allocation.push(&mut flow, item).map_err(message)?;
                            continue;
                        }
                        Err(reason) => {
                            rollback(doc, self.typography, before);
                            allocation
                                .push(
                                    &mut self.typography.borrow_mut(),
                                    request_json!(allocation, {
                                        "status":"retained_separate_cell_rows",
                                        "candidate":candidate,"cell":cell_idx,
                                        "spans":proposal.span_indices,"reason":reason,
                                        "authoredParagraphBoundaries":"unknown",
                                    })?,
                                )
                                .map_err(message)?;
                        }
                    }
                }
                if fixed_lines {
                    // Preserve unresolved appearance without claiming recovered paragraph grouping.
                    for line in &proposal.lines {
                        let mut unresolved_line =
                            allocation.clone_record(proposal).map_err(message)?;
                        unresolved_line.lines = allocation
                            .collect([allocation.clone_record(line).map_err(message)?])
                            .map_err(message)?;
                        unresolved_line.line_joins =
                            allocation.collect([LineJoin::Start]).map_err(message)?;
                        unresolved_line.layout = Some(ParagraphLayout {
                            alignment: TextAlignment::Left,
                            margin_left: line.left - number(&cb["x"])?,
                            indent: 0.,
                        });
                        unresolved_line.span_indices =
                            allocation.copy_slice(&line.indices).map_err(message)?;
                        allocation
                            .push(
                                &mut flow,
                                paragraph(
                                    page,
                                    doc,
                                    &unresolved_line,
                                    cb,
                                    shape_ids,
                                    true,
                                    self.styles,
                                    self.typography,
                                )?,
                            )
                            .map_err(message)?;
                    }
                } else {
                    return Err("recognition_deferred".into());
                }
            }
            let children = recognized_cell["childTableIndices"]
                .as_array()
                .map(Vec::as_slice)
                .unwrap_or_default();
            for child_ref in children {
                let child_id = index(&child_ref)?;
                let child_grid = grids.get(child_id).ok_or("missing child grid")?;
                if child_grid["hierarchy"]["parent"]
                    != request_json!(allocation, {"table":candidate,"cell":cell_idx})?
                {
                    return Err("nested cell-slot ownership mismatch".into());
                }
                let mut child = self.build(doc, child_id, removed, stack, true, source_paint)?;
                let child_box = &child_grid["bbox"];
                let top = number(&child_box["y"])?;
                let bottom = top + number(&child_box["height"])?;
                let left = hu(number(&child_box["x"])? - number(&cb["x"])?)?;
                child.common.treat_as_char = true;
                child.common.horz_rel_to = HorzRelTo::Para;
                child.common.vert_rel_to = VertRelTo::Para;
                child.common.horizontal_offset = 0;
                child.common.vertical_offset = 0;
                child.common.text_wrap = TextWrap::TopAndBottom;
                child.common.allow_overlap = false;
                child.common.flow_with_text = true;
                child.outer_margin_left =
                    i16::try_from(left).map_err(|_| "nested inset exceeds format range")?;
                let height = child.common.height;
                allocation
                    .bytes(std::mem::size_of::<LineSeg>())
                    .map_err(message)?;
                let mut host = Paragraph::new_empty();
                host.char_count_msb = true;
                host.char_count = 9;
                host.control_mask = 0x800;
                host.char_shapes = allocation
                    .collect([CharShapeRef {
                        start_pos: 0,
                        char_shape_id: 0,
                    }])
                    .map_err(message)?;
                let mut ps = allocation
                    .clone_record(&doc.doc_info.para_shapes[0])
                    .map_err(message)?;
                ps.raw_data = None;
                ps.attr1 &= !(1 << 8);
                ps.margin_left = 0;
                ps.margin_right = 0;
                ps.indent = 0;
                ps.spacing_before = 0;
                ps.spacing_after = 0;
                ps.line_spacing_type = LineSpacingType::SpaceOnly;
                ps.line_spacing = 0;
                host.para_shape_id =
                    u16::try_from(doc.doc_info.para_shapes.len()).map_err(|e| e.to_string())?;
                allocation
                    .push(&mut doc.doc_info.para_shapes, ps)
                    .map_err(message)?;
                host.line_segs = allocation
                    .collect([LineSeg {
                        line_height: height as i32,
                        text_height: height as i32,
                        baseline_distance: (height as f32 * 0.85).round() as i32,
                        segment_width: cell.width as i32,
                        tag: LineSeg::TAG_SINGLE_SEGMENT_LINE,
                        ..Default::default()
                    }])
                    .map_err(message)?;
                allocation
                    .push(
                        &mut host.controls,
                        Control::Table(allocation.boxed(child).map_err(message)?),
                    )
                    .map_err(message)?;
                allocation
                    .push(
                        &mut flow,
                        FlowItem {
                            top,
                            bottom,
                            paragraph: host,
                        },
                    )
                    .map_err(message)?;
            }
            for image_idx in refs(&source["imageIndices"], allocation)? {
                let control_idx = *image_controls
                    .get(image_idx)
                    .ok_or("image/control map mismatch")?;
                let Control::Picture(original) = &controls[control_idx] else {
                    unreachable!()
                };
                let mut picture = allocation.clone_record(original).map_err(message)?;
                let top = picture.common.vertical_offset as f32 / 100.;
                picture.common.treat_as_char = true;
                picture.common.text_wrap = TextWrap::TopAndBottom;
                picture.common.allow_overlap = false;
                if fixed_lines {
                    picture.common.flow_with_text = true;
                }
                picture.common.vert_rel_to = VertRelTo::Para;
                picture.common.horz_rel_to = HorzRelTo::Para;
                picture.common.horizontal_offset = 0;
                picture.common.vertical_offset = 0;
                allocation
                    .bytes(std::mem::size_of::<LineSeg>())
                    .map_err(message)?;
                let mut para = Paragraph::new_empty();
                para.char_count_msb = true;
                para.char_count = 9;
                para.control_mask = 0x800;
                para.char_shapes = allocation
                    .collect([CharShapeRef {
                        start_pos: 0,
                        char_shape_id: 0,
                    }])
                    .map_err(message)?;
                let mut ps = allocation
                    .clone_record(&doc.doc_info.para_shapes[0])
                    .map_err(message)?;
                ps.raw_data = None;
                ps.margin_left =
                    2 * (original.common.horizontal_offset as i32 - hu(number(&cb["x"])?)? as i32);
                ps.line_spacing = 100;
                if fixed_lines {
                    ps.line_spacing_type = LineSpacingType::Fixed;
                    ps.line_spacing = i32::try_from(picture.common.height)
                        .ok()
                        .and_then(|height| height.checked_mul(2))
                        .ok_or("picture line height exceeds format range")?;
                }
                ps.spacing_before = 0;
                ps.spacing_after = 0;
                para.para_shape_id = doc.doc_info.para_shapes.len() as u16;
                allocation
                    .push(&mut doc.doc_info.para_shapes, ps)
                    .map_err(message)?;
                para.line_segs = allocation
                    .collect([LineSeg {
                        line_height: picture.common.height as i32,
                        text_height: picture.common.height as i32,
                        baseline_distance: picture.common.height as i32,
                        segment_width: cell.width as i32,
                        tag: LineSeg::TAG_SINGLE_SEGMENT_LINE,
                        ..Default::default()
                    }])
                    .map_err(message)?;
                let bottom = top + picture.common.height as f32 / 100.;
                allocation
                    .push(&mut para.controls, Control::Picture(picture))
                    .map_err(message)?;
                allocation
                    .push(
                        &mut flow,
                        FlowItem {
                            top,
                            bottom,
                            paragraph: para,
                        },
                    )
                    .map_err(message)?;
                allocation
                    .insert_set(removed, control_idx)
                    .map_err(message)?;
            }
            allocation
                .sort_by(&mut flow, |a, b| a.top.total_cmp(&b.top))
                .map_err(message)?;
            let mut cursor = number(&cb["y"])?;
            let mut previous_style: Option<u16> = None;
            for (flow_index, item) in flow.iter_mut().enumerate() {
                if item.top < cursor - 0.5 {
                    return Err("overlapping cell flow requires additional lowering".into());
                }
                let gap = (item.top - cursor).max(0.);
                let picture = item.paragraph.controls.first().is_some_and(|c| {
                    matches!(
                        c,
                        Control::Picture(_) | Control::Table(_) | Control::Shape(_)
                    )
                });
                if picture {
                    // Place the inter-item gap before the inline image's flow origin.
                    if let Some(style) = previous_style {
                        doc.doc_info.para_shapes[style as usize].spacing_after =
                            physical_margin(gap)?;
                    } else {
                        cell.padding.top = i16::try_from(hu(gap)?)
                            .map_err(|_| "cell padding exceeds format range")?;
                    }
                } else {
                    doc.doc_info.para_shapes[item.paragraph.para_shape_id as usize]
                        .spacing_before = physical_margin(gap)?;
                }
                if flow_index == 0 && item.paragraph.controls.is_empty() {
                    // Stored cell starts own their lead in the frame, not flow_before.
                    let lead = hu((item.top - cursor).max(0.))? as i32;
                    for segment in &mut item.paragraph.line_segs {
                        segment.vertical_pos += lead;
                    }
                }
                cursor = item.bottom;
                previous_style = Some(item.paragraph.para_shape_id);
            }
            if !flow.is_empty() {
                cell.paragraphs = allocation
                    .collect(flow.into_iter().map(|i| i.paragraph))
                    .map_err(message)?;
            }
            for i in refs(&source["spanIndices"], allocation)? {
                if let Some(c) = span_controls[i] {
                    allocation.insert_set(removed, c).map_err(message)?;
                }
            }
            allocation.push(&mut table.cells, cell).map_err(message)?;
        }
        let grid_work = usize::from(table.row_count)
            .checked_mul(usize::from(table.col_count))
            .and_then(|n| n.checked_mul(std::mem::size_of::<Option<usize>>()))
            .ok_or("PDF table grid work overflow")?;
        allocation.bytes(grid_work).map_err(message)?;
        table.rebuild_grid();
        table.row_sizes = allocation
            .collect(
                (0..table.row_count)
                    .map(|row| table.cells.iter().filter(|c| c.row == row).count() as i16),
            )
            .map_err(message)?;
        if preserve_paint {
            // Page-wide partition attaches paint only after all accepted cell owners are known.
        } else if family {
            for path in refs(&grid["sourcePathIndices"], allocation)? {
                let control = rule_controls
                    .get(path)
                    .ok_or("invalid source rule reference")?
                    .ok_or("source boundary has no lowering representation")?;
                allocation.insert_set(removed, control).map_err(message)?;
            }
        } else {
            for (i, c) in controls.iter().enumerate() {
                if let Control::Shape(s) = c {
                    if is_grid_rule(s, grid)? {
                        allocation.insert_set(removed, i).map_err(message)?;
                    }
                }
            }
        }
        Ok(table)
    }
}

fn table_count(controls: &[Control]) -> usize {
    controls
        .iter()
        .map(|c| match c {
            Control::Table(t) => {
                1 + t
                    .cells
                    .iter()
                    .flat_map(|c| &c.paragraphs)
                    .map(|p| table_count(&p.controls))
                    .sum::<usize>()
            }
            _ => 0,
        })
        .sum()
}

fn paint_frame(control: &Control) -> Option<&CommonObjAttr> {
    match control {
        Control::Shape(shape) => Some(shape.common()),
        Control::Picture(picture) => Some(&picture.common),
        Control::Table(table) => Some(&table.common),
        _ => None,
    }
}

/// Body table paint can use the flow plane, whereas positional source controls
/// paint in front of text. A retained backdrop can consequently cover table
/// labels even with a larger table z-order. Promote only when no earlier
/// overlapping positional paint would cross that plane boundary.
fn table_paint_layer(
    controls: &[Control],
    consumed: &BTreeSet<usize>,
    bounds: &Value,
) -> Result<i32, String> {
    let last = consumed
        .iter()
        .filter_map(|&i| controls.get(i).and_then(paint_frame))
        .map(|c| c.z_order)
        .max()
        .ok_or("table has no source paint")?;
    let (x, y, width, height) = (
        number(&bounds["x"])?,
        number(&bounds["y"])?,
        number(&bounds["width"])?,
        number(&bounds["height"])?,
    );
    for (i, control) in controls.iter().enumerate() {
        if consumed.contains(&i) {
            continue;
        }
        let Some(frame) = paint_frame(control) else {
            continue;
        };
        if frame.z_order > last {
            continue;
        }
        let cx = (frame.horizontal_offset as i32) as f32 / 100.;
        let cy = (frame.vertical_offset as i32) as f32 / 100.;
        if cx < x + width
            && cx + frame.width as f32 / 100. > x
            && cy < y + height
            && cy + frame.height as f32 / 100. > y
        {
            return Err("positional backdrop cannot share inferred table flow plane".into());
        }
    }
    Ok(last)
}

/// Reserve the temporary numeric arrays used by the common paragraph slot
/// producer. The existing helper still owns all position/paint decisions.
fn prepend_cell_paint(
    paragraph: &mut Paragraph,
    paint: Vec<Control>,
    allocation: Allocation<'_>,
) -> Result<(), String> {
    if paint.is_empty() {
        return Ok(());
    }
    let chars = paragraph.text.chars().count();
    let controls = paragraph.controls.len();
    let total = controls
        .checked_add(paint.len())
        .ok_or("PDF cell control count overflow")?;
    let maximum = chars.max(4);
    // logical_control_positions: two control indices and a char projection.
    // rebuild_char_offsets: chars, old/new offsets, gap counts and shifts.
    // Four times the final length covers geometrically grown primitive buffers;
    // these are cumulative work reservations, not allocator/RSS measurements.
    allocation
        .payload::<char>(maximum.checked_mul(8).ok_or("PDF slot work overflow")?)
        .map_err(message)?;
    allocation
        .payload::<u32>(
            maximum
                .checked_mul(8)
                .and_then(|n| n.checked_add(paragraph.line_segs.len()))
                .ok_or("PDF slot work overflow")?,
        )
        .map_err(message)?;
    allocation
        .payload::<i64>(
            chars
                .checked_mul(2)
                .and_then(|n| n.checked_add(1))
                .ok_or("PDF slot work overflow")?,
        )
        .map_err(message)?;
    allocation
        .payload::<usize>(
            total
                .checked_mul(4)
                .and_then(|n| controls.checked_mul(2).and_then(|c| n.checked_add(c)))
                .ok_or("PDF slot work overflow")?,
        )
        .map_err(message)?;
    let auxiliary = paragraph
        .field_ranges
        .len()
        .checked_add(paragraph.orphan_field_ends.len())
        .and_then(|n| n.checked_add(paragraph.title_marks.len()))
        .ok_or("PDF slot work overflow")?;
    allocation
        .bytes(auxiliary.checked_mul(128).ok_or("PDF slot work overflow")?)
        .map_err(message)?;
    allocation
        .reserve(&mut paragraph.controls, paint.len())
        .map_err(message)?;
    let missing = total.saturating_sub(paragraph.ctrl_data_records.len());
    allocation
        .reserve(&mut paragraph.ctrl_data_records, missing)
        .map_err(message)?;
    crate::model::border_paint::prepend_cell_paint(paragraph, paint);
    Ok(())
}

pub(super) fn lower(
    page: &ExtractedPage,
    doc: &mut Document,
    next_id: &mut u32,
) -> Result<Value, String> {
    let allocation = Allocation(&page.import_budget);
    let mut report = super::lowering::lower(page, doc, next_id)?;
    let section = doc.sections.len() - 1;
    let mut controls = std::mem::take(&mut doc.sections[section].paragraphs[0].controls);
    let mut span_controls = allocation.filled(page.spans.len(), None).map_err(message)?;
    let mut shape_ids = allocation.filled(page.spans.len(), None).map_err(message)?;
    let mut image_controls = allocation.filled(page.images.len(), 0).map_err(message)?;
    for source in array(&report["imageSourceControls"])? {
        let image = index(&source["image"])?;
        let control = index(&source["control"])?;
        *image_controls
            .get_mut(image)
            .ok_or("invalid image/control mapping")? = control;
    }
    let mut rule_controls = allocation.filled(page.paths.len(), None).map_err(message)?;
    for source in array(&report["ruleSourceControls"])? {
        let path = index(&source["path"])?;
        let control = index(&source["control"])?;
        *rule_controls
            .get_mut(path)
            .ok_or("invalid rule/control mapping")? = Some(control);
    }
    let nonempty: Vec<_> = allocation
        .collect(
            page.spans
                .iter()
                .enumerate()
                .filter_map(|(i, s)| (!s.text.is_empty()).then_some(i)),
        )
        .map_err(message)?;
    let mut span_cursor = 0;
    for (control_idx, c) in controls.iter().enumerate() {
        match c {
            Control::Picture(_) => {}
            Control::Shape(s) => {
                if let Some(b) = s.drawing().and_then(|d| d.text_box.as_ref()) {
                    let source = *nonempty
                        .get(span_cursor)
                        .ok_or("span/control map mismatch")?;
                    span_controls[source] = Some(control_idx);
                    shape_ids[source] = Some(b.paragraphs[0].char_shapes[0].char_shape_id);
                    span_cursor += 1;
                }
            }
            _ => {}
        }
    }
    let grids = array(&page.snapshot["tableDetection"]["tables"])?;
    let recognized = array(&page.recognition.report["tables"])?;
    let fragment_ids = std::cell::Cell::new(
        next_id
            .checked_add(u32::try_from(grids.len()).map_err(|e| e.to_string())?)
            .ok_or("table instance ID overflow")?,
    );
    let styles = SourceStyles::new(doc, &shape_ids, &page.import_budget)?;
    let typography = std::cell::RefCell::new(Vec::new());
    let builder = TableBuilder {
        table_id_base: *next_id,
        page,
        controls: &controls,
        grids,
        recognized,
        span_controls: &span_controls,
        shape_ids: &shape_ids,
        image_controls: &image_controls,
        rule_controls: &rule_controls,
        styles: &styles,
        typography: &typography,
    };
    *next_id = next_id
        .checked_add(u32::try_from(grids.len()).map_err(|e| e.to_string())?)
        .ok_or("table instance ID overflow")?;
    let mut removed = BTreeSet::new();
    let mut tables = Vec::new();
    let mut table_candidates = Vec::new();
    let mut table_removed = Vec::new();
    let mut held = Vec::new();
    let mut preserve_indices = Vec::new();
    for (candidate, grid) in grids.iter().enumerate() {
        if !grid["hierarchy"]["parent"].is_null() {
            continue;
        }
        let family = array(&grid["cells"])?.iter().any(|c| {
            c["childTableIndices"]
                .as_array()
                .is_some_and(|a| !a.is_empty())
        });
        let style_start = doc.doc_info.para_shapes.len();
        let char_style_start = doc.doc_info.char_shapes.len();
        let typography_start = typography.borrow().len();
        let border_start = doc.doc_info.border_fills.len();
        let tab_start = doc.doc_info.tab_defs.len();
        let mut candidate_removed = BTreeSet::new();
        // All source tables share the same physical cell extents and paint
        // ownership. A solid/linear root is not proof that a separately rebuilt
        // uniform border, rounded width or page-owned backdrop is equivalent.
        let source_paint = true;
        let built = builder
            .build(
                doc,
                candidate,
                &mut candidate_removed,
                &mut BTreeSet::new(),
                family,
                source_paint,
            )
            .and_then(|mut table| {
                if !source_paint {
                    table.common.z_order =
                        table_paint_layer(&controls, &candidate_removed, &grid["bbox"])?;
                }
                Ok(table)
            });
        page.import_budget.check().map_err(|e| e.to_string())?;
        match built {
            Ok(table) => {
                if source_paint {
                    allocation
                        .push(&mut preserve_indices, tables.len())
                        .map_err(message)?;
                }
                allocation
                    .push(&mut table_removed, candidate_removed)
                    .map_err(message)?;
                allocation
                    .push(&mut table_candidates, candidate)
                    .map_err(message)?;
                allocation
                    .push(
                        &mut tables,
                        Control::Table(allocation.boxed(table).map_err(message)?),
                    )
                    .map_err(message)?;
            }
            Err(reason) => {
                doc.doc_info.para_shapes.truncate(style_start);
                doc.doc_info.char_shapes.truncate(char_style_start);
                typography.borrow_mut().truncate(typography_start);
                doc.doc_info.border_fills.truncate(border_start);
                doc.doc_info.tab_defs.truncate(tab_start);
                allocation
                    .push(
                        &mut held,
                        request_json!(allocation, {"candidate":candidate,"reason":reason})?,
                    )
                    .map_err(message)?;
            }
        }
    }
    let mut paint_owners = Vec::new();
    let mut native_borders = Vec::new();
    let mut paint_result = None;
    let mut paint_work = 0;
    while !preserve_indices.is_empty() {
        paint_owners = composition::owners(
            &tables,
            &preserve_indices,
            grids,
            builder.table_id_base,
            allocation,
        )?;
        native_borders = allocation
            .collect(paint_owners.iter().map(|owner| {
                native_borders::plan(page, &controls, &rule_controls, &grids[owner.candidate])
            }))
            .map_err(message)?;
        page.import_budget.check().map_err(|e| e.to_string())?;
        let targets = allocation.collect(paint_owners
            .iter()
            .zip(&native_borders)
            .map(|(owner, native)| super::table_paint::Target {
                grid: &grids[owner.candidate],
                native_border_controls: native.as_ref().map(|n| n.controls.as_slice()),
                root: matches!(&tables[owner.root], Control::Table(t) if t.common.instance_id == owner.instance_id),
            })
            ).map_err(message)?;
        let plan = super::table_paint::partition(
            &controls,
            &rule_controls,
            super::table_paint::SourceContent {
                spans: &span_controls,
                images: &image_controls,
                resources: &doc.bin_data_content,
            },
            &targets,
            &page.snapshot["tableDetection"],
            &page.paths,
            &mut paint_work,
            allocation,
        )
        .and_then(|plan| {
            let consumed = allocation
                .set_from(
                    table_removed
                        .iter()
                        .flatten()
                        .copied()
                        .chain(plan.removed.iter().copied()),
                )
                .map_err(message)?;
            for &ti in &preserve_indices {
                table_paint_layer(&controls, &consumed, &grids[table_candidates[ti]]["bbox"])?;
            }
            Ok(plan)
        });
        page.import_budget.check().map_err(|e| e.to_string())?;
        if let Err(error) = &plan {
            if !error.unconsumed_targets.is_empty() {
                let rejected = allocation
                    .set_from(
                        error
                            .unconsumed_targets
                            .iter()
                            .map(|&i| paint_owners[i].root),
                    )
                    .map_err(message)?;
                composition::discard_roots(
                    &mut tables,
                    &mut table_candidates,
                    &mut table_removed,
                    &mut preserve_indices,
                    &mut held,
                    &rejected,
                    &error.reason,
                    allocation,
                )?;
                continue;
            }
        }
        paint_result = Some(plan);
        break;
    }
    let mut extra_paint = Vec::new();
    let mut paint_fragments = 0;
    let mut native_border_cells = 0;
    let mut native_border_fragments = 0;
    let mut external_boundary_paths = Vec::new();
    let mut cell_fill_sources = Vec::new();
    if let Some(plan) = paint_result {
        match plan {
            Ok(mut plan) => {
                let reserve_paint = |bytes| allocation.bytes(bytes).ok();
                let mut paint_resources = crate::model::border_paint::Resources::with_reservation(
                    &doc.bin_data_content,
                    &reserve_paint,
                );
                external_boundary_paths = allocation
                    .collect(
                        rule_controls
                            .iter()
                            .enumerate()
                            .filter_map(|(path, control)| {
                                control
                                    .is_some_and(|c| plan.external_boundary_controls.contains(&c))
                                    .then_some(path)
                            }),
                    )
                    .map_err(message)?;
                native_border_fragments = plan.native_border_fragments;
                cell_fill_sources = allocation.try_collect(plan.cell_fill_sources.iter().map(|&(path,target,cell)| {
                    request_json!(allocation,{"path":path,"candidate":paint_owners[target].candidate,"cell":cell}).map_err(String::from)
                }))?;
                allocation
                    .extend_set(&mut removed, plan.removed)
                    .map_err(message)?;
                let consumed = allocation
                    .set_from(
                        table_removed
                            .iter()
                            .flatten()
                            .copied()
                            .chain(removed.iter().copied()),
                    )
                    .map_err(message)?;
                for &ti in &preserve_indices {
                    let Control::Table(table) = &mut tables[ti] else {
                        unreachable!()
                    };
                    table.common.z_order = table_paint_layer(
                        &controls,
                        &consumed,
                        &grids[table_candidates[ti]]["bbox"],
                    )?;
                }
                for (target, owner) in paint_owners.iter().enumerate() {
                    let table = composition::table_mut(&mut tables[owner.root], owner.instance_id)
                        .ok_or("source paint owner missing from actual table hierarchy")?;
                    let grid = &grids[owner.candidate];
                    for (ci, cell) in table.cells.iter_mut().enumerate() {
                        if let Some(native) = &native_borders[target] {
                            let border = &native.borders[ci];
                            let id = if let Some(i) =
                                doc.doc_info.border_fills.iter().position(|b| b == border)
                            {
                                i + 1
                            } else {
                                allocation
                                    .push(
                                        &mut doc.doc_info.border_fills,
                                        allocation.clone_record(border).map_err(message)?,
                                    )
                                    .map_err(message)?;
                                doc.doc_info.border_fills.len()
                            };
                            cell.border_fill_id = u16::try_from(id).map_err(|e| e.to_string())?;
                            native_border_cells += usize::from(
                                border
                                    .borders
                                    .iter()
                                    .any(|b| b.line_type != BorderLineType::None),
                            );
                        }
                        if let Some(fill) = plan.cell_fills[target][ci].take() {
                            let mut border = allocation
                                .clone_record(
                                    doc.doc_info
                                        .border_fills
                                        .get(
                                            usize::from(cell.border_fill_id)
                                                .checked_sub(1)
                                                .ok_or("missing cell style")?,
                                        )
                                        .ok_or("missing cell style")?,
                                )
                                .map_err(message)?;
                            border.fill = fill;
                            border.raw_data = None;
                            let id = if let Some(i) =
                                doc.doc_info.border_fills.iter().position(|b| b == &border)
                            {
                                i + 1
                            } else {
                                allocation
                                    .push(&mut doc.doc_info.border_fills, border)
                                    .map_err(message)?;
                                doc.doc_info.border_fills.len()
                            };
                            cell.border_fill_id = u16::try_from(id).map_err(|e| e.to_string())?;
                        }
                        let mut cell_paint = std::mem::take(&mut plan.cells[target][ci]);
                        for paint in &mut cell_paint {
                            let id = fragment_ids.get();
                            fragment_ids
                                .set(id.checked_add(1).ok_or("cell paint instance ID overflow")?);
                            super::table_paint::anchor(
                                paint,
                                cell,
                                &grid["cells"][ci]["bbox"],
                                id,
                            )?;
                            if matches!(&paint, Control::Picture(p) if p.common.cell_border_paint.is_some())
                            {
                                crate::model::border_paint::seal_picture(
                                    paint,
                                    &mut paint_resources,
                                )
                                .ok_or("source border picture seal failed")?;
                            }
                            paint_fragments += 1;
                        }
                        prepend_cell_paint(&mut cell.paragraphs[0], cell_paint, allocation)?;
                    }
                }
                for (ri, mut paint) in plan.retained.into_iter().enumerate() {
                    let id = fragment_ids.get();
                    fragment_ids.set(id.checked_add(1).ok_or("page paint instance ID overflow")?);
                    super::table_paint::assign_id(&mut paint, id);
                    if let Some(&(target, cell, tag)) = plan.outside_owners.get(&ri) {
                        let owner = &paint_owners[target];
                        let Control::Table(table) = &tables[owner.root] else {
                            unreachable!()
                        };
                        crate::model::border_paint::bind_outside(
                            &mut paint,
                            table,
                            cell,
                            tag,
                            &mut paint_resources,
                        )
                        .ok_or("outside border binding no longer matches source frame")?;
                    }
                    allocation.push(&mut extra_paint, paint).map_err(message)?;
                }
            }
            Err(reason) => {
                let mut kept = Vec::new();
                let mut kept_removed = Vec::new();
                for (ti, (table, consumed)) in tables.into_iter().zip(table_removed).enumerate() {
                    if preserve_indices.contains(&ti) {
                        allocation.push(&mut held,
                            request_json!(allocation, {"candidate":table_candidates[ti],"reason":allocation.display(&reason)?})?,
                        ).map_err(message)?;
                    } else {
                        allocation.push(&mut kept, table).map_err(message)?;
                        allocation
                            .push(&mut kept_removed, consumed)
                            .map_err(message)?;
                    }
                }
                tables = kept;
                table_removed = kept_removed;
            }
        }
    }
    allocation
        .extend_set(&mut removed, table_removed.into_iter().flatten())
        .map_err(message)?;
    let root_count = tables.len();
    *next_id = fragment_ids.get();
    let table_count = table_count(&tables);
    let (body_frames, body_mapping, beneath) = body::lower(
        page,
        doc,
        &controls,
        &span_controls,
        &shape_ids,
        &styles,
        &typography,
        &mut removed,
        next_id,
    )?;
    // [#8362] 본문 글자보다 먼저 그린 가리는 그림은 다시 그린 본문 아래에 둔다.
    for &i in &beneath {
        match &mut controls[i] {
            Control::Shape(shape) => shape.common_mut().text_wrap = TextWrap::BehindText,
            Control::Picture(picture) => picture.common.text_wrap = TextWrap::BehindText,
            _ => {}
        }
    }
    let mut retained: Vec<_> = allocation
        .collect(
            controls
                .into_iter()
                .enumerate()
                .filter_map(|(i, c)| (!removed.contains(&i)).then_some(c)),
        )
        .map_err(message)?;
    let mut outside = Vec::new();
    let mut ordinary = Vec::new();
    for c in extra_paint {
        let is_outside = crate::model::border_paint::PaintRef::from_control(&c)
            .is_some_and(|p| p.common().outside_cell_border_paint.is_some());
        allocation
            .push(
                if is_outside {
                    &mut outside
                } else {
                    &mut ordinary
                },
                c,
            )
            .map_err(message)?;
    }
    allocation
        .reserve(&mut retained, ordinary.len())
        .map_err(message)?;
    retained.extend(ordinary);
    allocation
        .reserve(&mut retained, tables.len())
        .map_err(message)?;
    retained.extend(tables);
    allocation
        .reserve(&mut retained, body_frames.len())
        .map_err(message)?;
    retained.extend(body_frames);
    // Logical root table indices stay stable when a trailing page fragment is
    // removed by a cell-border command. Original z-order/coordinates are intact.
    allocation
        .reserve(&mut retained, outside.len())
        .map_err(message)?;
    retained.extend(outside);
    let owner = &mut doc.sections[section].paragraphs[0];
    owner.controls = retained;
    owner.char_count = 1 + owner.controls.len() as u32 * 8;
    allocation
        .set(
            &mut report,
            "mode",
            request_json!(allocation, "structured-tables-with-positional-fallback")?,
        )
        .map_err(message)?;
    allocation
        .set(
            &mut report,
            "semanticTables",
            request_json!(allocation, table_count)?,
        )
        .map_err(message)?;
    allocation
        .set(
            &mut report,
            "semanticRootTables",
            request_json!(allocation, root_count)?,
        )
        .map_err(message)?;
    allocation
        .set(
            &mut report,
            "semanticNestedTables",
            request_json!(allocation, table_count - root_count)?,
        )
        .map_err(message)?;
    allocation.set(&mut report, "nestedParagraphPolicy", request_json!(allocation, "confirmed proposals share Paragraph IR; deferred line frames do not assert authored grouping")?).map_err(message)?;
    allocation.set(&mut report, "paragraphMappingPolicy", request_json!(allocation, {
        "confirmed": "one proposal becomes one Paragraph with multiple source LineSegs",
        "deferred": "line frames retained; paragraph grouping unverified",
        "lineJoins": "inferred wrap connections; original whitespace retained",
        "bodyRegions": "confirmed owner-local paragraphs share the cell paragraph adapter; ambiguous source spans retained"
    })?).map_err(message)?;
    allocation
        .set(&mut report, "bodyParagraphMapping", body_mapping)
        .map_err(message)?;
    let mut source_typography = request_json!(allocation, {
        "policy": "recorded glyph origins; inferred spaces independent of span boundaries; HWP integer-percent character metrics",
        "fontRecovery": "unidentified source fonts use explicit metric-compatible fallback; original outlines remain unverified"
    })?;
    allocation
        .set(
            &mut source_typography,
            "lines",
            Value::Array(typography.into_inner()),
        )
        .map_err(message)?;
    allocation
        .set(&mut report, "sourceTypography", source_typography)
        .map_err(message)?;
    allocation.set(&mut report, "boundaryPaintPolicy", request_json!(allocation, "standard borders or source paint partitioned into cell-owned drawings; no page-level duplicate")?).map_err(message)?;
    allocation
        .set(
            &mut report,
            "cellOwnedPaintFragments",
            request_json!(allocation, paint_fragments)?,
        )
        .map_err(message)?;
    allocation
        .set(
            &mut report,
            "nativeBorderCells",
            request_json!(allocation, native_border_cells)?,
        )
        .map_err(message)?;
    allocation
        .set(
            &mut report,
            "nativeBorderPaintFragments",
            request_json!(allocation, native_border_fragments)?,
        )
        .map_err(message)?;
    allocation.set(&mut report, "nativeCellBackgrounds", request_json!(allocation, {"sources":cell_fill_sources,
        "policy":"single opaque rectangle behind all owned content becomes standard full-cell fill; source inset is normalized; ambiguous or partial highlights retain source paint"})?).map_err(message)?;
    allocation
        .set(
            &mut report,
            "externalBoundaryPaint",
            request_json!(allocation, {"sourcePathIndices":external_boundary_paths,
        "owner":"unchanged page source control outside logical table frame"})?,
        )
        .map_err(message)?;
    allocation
        .set(
            &mut report,
            "ruledTableCandidates",
            request_json!(allocation, grids.len())?,
        )
        .map_err(message)?;
    allocation
        .set(
            &mut report,
            "tableSemantics",
            request_json!(allocation, "inferred-from-geometry-not-authored-metadata")?,
        )
        .map_err(message)?;
    allocation
        .set(
            &mut report,
            "textBoxes",
            request_json!(
                allocation,
                owner
                    .controls
                    .iter()
                    .filter(|c| matches!(c,
        Control::Shape(s) if s.drawing().is_some_and(|d| d.text_box.is_some())))
                    .count()
            )?,
        )
        .map_err(message)?;
    allocation
        .set(
            &mut report,
            "straightRules",
            request_json!(
                allocation,
                owner
                    .controls
                    .iter()
                    .filter(|c| matches!(c,
        Control::Shape(s) if matches!(s.as_ref(), ShapeObject::Line(_))))
                    .count()
            )?,
        )
        .map_err(message)?;
    allocation.set(&mut report, "paintedRuleRectangles", request_json!(allocation, owner.controls.iter().filter(|c|matches!(c,
        Control::Shape(s) if matches!(s.as_ref(),ShapeObject::Rectangle(r) if r.drawing.text_box.is_none() && r.drawing.fill.solid.is_some()))).count())?).map_err(message)?;
    allocation
        .set(&mut report, "deferredTables", Value::Array(held))
        .map_err(message)?;
    allocation
        .set(
            &mut report,
            "hancomCompatibility",
            request_json!(allocation, "unverified-awaiting-editor-test")?,
        )
        .map_err(message)?;
    allocation
        .set(
            &mut report,
            "warnings",
            request_json!(allocation, [
        "PDF table/paragraph semantics are inferred, not recovered authored metadata",
        "PDF fonts, exact glyph advances and paragraph formatting are not fully reconstructed",
        "Hancom editor interoperability must be checked independently"
    ])?,
        )
        .map_err(message)?;
    Ok(report)
}