pdfluent 1.2.0

Pure-Rust PDF SDK with PDF/A, digital signatures, WASM, and experimental (feature-gated) XFA support.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
//! Text extraction for the compatible API (lane L1).

// Copyright (c) 2026 Innovation Trigger B.V.
//
// PDFluent is available under two licences, at your option: the GNU AGPLv3, or
// the PDFluent Commercial Licence. See the LICENSE file in this repository --
// that file travels with the copy you received, which a URL does not.

//! Typed text outputs: one struct per recorded shape (words, blocks, dicts,
//! traces), one [`TextOutput`] enum over the option-dependent `get_text`
//! results, and option structs for the clip/flags/sort/textpage families.
//! Structs whose shape has no recording yet are `#[non_exhaustive]` so the
//! lane can grow them without breaking the bindings.

use super::edit::Annot;
use super::geometry::{CompareOp, Matrix, Point, Quad, Rect};
use super::pixmap::PlacedImage;
use super::{ErrorKind, Page, PageMut, Value};
use crate::document::PdfDocument;
use crate::error::Result;
use pdf_engine::{Block, BlockKind, BoundsSource, ExtractOptions, TextSpan};
use pdf_standard_fonts::{symbol_glyph_name_to_char, StandardFont};

/// One extracted word: its box, text, and block/line/word numbers.
#[derive(Debug, Clone, PartialEq)]
pub struct TextWord {
    /// Word box, in points.
    pub rect: Rect,
    /// Word text, without surrounding whitespace.
    pub text: String,
    /// Zero-based block number on the page.
    pub block_no: i32,
    /// Zero-based line number within the block.
    pub line_no: i32,
    /// Zero-based word number within the line.
    pub word_no: i32,
}

/// One extracted block: its box, text, number, and type code.
#[derive(Debug, Clone, PartialEq)]
pub struct TextBlock {
    /// Block box, in points.
    pub rect: Rect,
    /// Block text, lines joined with newlines.
    pub text: String,
    /// Zero-based block number on the page.
    pub number: i32,
    /// Block type code (0 for text, 1 for images).
    pub block_type: i32,
}

/// One raw character: its box, character, origin, and synthesis flag.
#[derive(Debug, Clone, PartialEq)]
pub struct RawChar {
    /// Character box, in points.
    pub bbox: Rect,
    /// The character itself.
    pub ch: char,
    /// Pen origin of the character, in points.
    pub origin: Point,
    /// Whether the character was synthesised rather than read.
    pub synthetic: bool,
}

/// One span: a run of one font and size with its characters on raw extracts.
#[derive(Debug, Clone, PartialEq)]
pub struct DictSpan {
    /// Span box, in points.
    pub bbox: Rect,
    /// Pen origin of the span, in points.
    pub origin: Point,
    /// Font name.
    pub font: String,
    /// Font size, in points.
    pub size: f64,
    /// Text colour packed as an integer.
    pub color: i32,
    /// Opacity as an integer alpha value.
    pub alpha: i32,
    /// Span flags bitfield.
    pub flags: i32,
    /// Bidirectional level.
    pub bidi: i32,
    /// Per-character flags bitfield.
    pub char_flags: i32,
    /// Font ascender, in points.
    pub ascender: f64,
    /// Font descender, in points.
    pub descender: f64,
    /// Span text.
    pub text: String,
    /// Per-character boxes, populated on raw extracts, empty otherwise.
    pub chars: Vec<RawChar>,
}

/// One line: its box, writing mode, direction, and spans.
#[derive(Debug, Clone, PartialEq)]
pub struct DictLine {
    /// Line box, in points.
    pub bbox: Rect,
    /// Writing mode (0 horizontal, 1 vertical).
    pub wmode: i32,
    /// Baseline direction vector.
    pub direction: Point,
    /// Spans making up the line.
    pub spans: Vec<DictSpan>,
}

/// One dict block: text with lines, or a placed image.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum DictBlock {
    /// A text block with its lines.
    Text {
        /// Block box, in points.
        bbox: Rect,
        /// Zero-based block number on the page.
        number: i32,
        /// Block flags bitfield.
        flags: i32,
        /// Lines making up the block.
        lines: Vec<DictLine>,
    },
    /// A placed image block.
    Image {
        /// Image box, in points.
        bbox: Rect,
        /// Zero-based block number on the page.
        number: i32,
        /// Image width, in pixels.
        width: i32,
        /// Image height, in pixels.
        height: i32,
        /// Bits per component.
        bpc: i32,
        /// Colour-space code.
        colorspace: i32,
        /// Suggested file extension, without the dot.
        ext: String,
        /// Encoded image bytes.
        image_data: Vec<u8>,
        /// Image-to-page transformation.
        transform: Matrix,
        /// Horizontal resolution, in dots per inch.
        xres: i32,
        /// Vertical resolution, in dots per inch.
        yres: i32,
    },
}

/// Structured page text: page size plus dict blocks.
#[derive(Debug, Clone, PartialEq)]
pub struct TextDict {
    /// Page width, in points.
    pub width: f64,
    /// Page height, in points.
    pub height: f64,
    /// Blocks on the page, in reading order.
    pub blocks: Vec<DictBlock>,
}

/// One traced character: codepoint, glyph id, origin, and box.
#[derive(Debug, Clone, PartialEq)]
pub struct TraceChar {
    /// Unicode codepoint.
    pub codepoint: i32,
    /// Font glyph id.
    pub glyph: i32,
    /// Pen origin, in points.
    pub origin: Point,
    /// Character box, in points.
    pub bbox: Rect,
}

/// One traced span: metrics plus per-character geometry.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct TextTrace {
    /// Span box, in points.
    pub bbox: Rect,
    /// Font ascender, in points.
    pub ascender: f64,
    /// Bidirectional direction code.
    pub bidi_dir: i32,
    /// Bidirectional embedding level.
    pub bidi_level: i32,
    /// Traced characters.
    pub chars: Vec<TraceChar>,
    /// Font name.
    pub font: String,
    /// Font size, in points.
    pub size: f64,
    /// Text colour as red/green/blue fractions.
    pub color: [f64; 3],
    /// Colour-space code.
    pub colorspace: i32,
    /// Font descender, in points.
    pub descender: f64,
    /// Baseline direction vector.
    pub dir: Point,
    /// Span flags bitfield.
    pub flags: i32,
    /// Optional-content layer name, empty when unmarked.
    pub layer: String,
    /// Stroke width, or `None` for filled text.
    pub linewidth: Option<f64>,
    /// Opacity from transparent (0.0) to opaque (1.0).
    pub opacity: f64,
    /// Zero-based span number on the page.
    pub seqno: i32,
    /// Advance width of a space, in points.
    pub spacewidth: f64,
    /// Trace item type code (0 for text).
    pub span_type: i32,
    /// Writing mode (0 horizontal, 1 vertical).
    pub wmode: i32,
}

/// Text output formats accepted by `get_text` and friends.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum TextFormat {
    /// Plain text.
    #[default]
    Text,
    /// Word list.
    Words,
    /// Block list.
    Blocks,
    /// Structured dict.
    Dict,
    /// Structured dict with per-character boxes.
    RawDict,
    /// Dict serialised as JSON text.
    Json,
    /// Raw dict serialised as JSON text.
    RawJson,
    /// HTML fragment.
    Html,
    /// XHTML fragment.
    Xhtml,
    /// XML fragment.
    Xml,
}

impl TextFormat {
    /// Parse the recorded option name, or a bad-value error naming it.
    ///
    /// The name matches case-insensitively: the recordings accept an
    /// uppercase option word like the lowercase form.
    pub fn from_name(name: &str) -> Result<Self> {
        match name.to_ascii_lowercase().as_str() {
            "text" => Ok(Self::Text),
            "words" => Ok(Self::Words),
            "blocks" => Ok(Self::Blocks),
            "dict" => Ok(Self::Dict),
            "rawdict" => Ok(Self::RawDict),
            "json" => Ok(Self::Json),
            "rawjson" => Ok(Self::RawJson),
            "html" => Ok(Self::Html),
            "xhtml" => Ok(Self::Xhtml),
            "xml" => Ok(Self::Xml),
            _ => Err(ErrorKind::BadValue.to_error(format!("unknown text option {name:?}"))),
        }
    }
}

/// The option-dependent result of `get_text`: one variant per format.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum TextOutput {
    /// Plain text (`text`).
    Text(String),
    /// Word list (`words`).
    Words(Vec<TextWord>),
    /// Block list (`blocks`).
    Blocks(Vec<TextBlock>),
    /// Structured dict (`dict` and `rawdict`; raw fills span characters).
    Dict(TextDict),
    /// JSON text (`json`).
    Json(String),
    /// Raw JSON text (`rawjson`).
    RawJson(String),
    /// HTML fragment (`html`).
    Html(String),
    /// XHTML fragment (`xhtml`).
    Xhtml(String),
    /// XML fragment (`xml`).
    Xml(String),
}

/// Options for `get_text`: format, clip, flags, sort, and an optional page.
#[derive(Debug, Clone, Copy, Default)]
#[non_exhaustive]
pub struct TextOptions<'a> {
    /// Output format.
    pub format: TextFormat,
    /// Clip rectangle, or the whole page.
    pub clip: Option<Rect>,
    /// Extraction flags, or the default set when `None`.
    pub flags: Option<i32>,
    /// Sort blocks in reading order.
    pub sort: bool,
    /// Reuse this extracted page instead of extracting again.
    pub textpage: Option<TextPage<'a>>,
}

impl<'a> TextOptions<'a> {
    /// Set the output format.
    pub fn with_format(mut self, format: TextFormat) -> Self {
        self.format = format;
        self
    }

    /// Clip extraction to this rectangle.
    pub fn with_clip(mut self, clip: Rect) -> Self {
        self.clip = Some(clip);
        self
    }

    /// Set the extraction flags.
    pub fn with_flags(mut self, flags: i32) -> Self {
        self.flags = Some(flags);
        self
    }

    /// Sort blocks in reading order.
    pub fn with_sort(mut self, sort: bool) -> Self {
        self.sort = sort;
        self
    }

    /// Reuse this extracted page instead of extracting again.
    pub fn with_textpage(mut self, textpage: TextPage<'a>) -> Self {
        self.textpage = Some(textpage);
        self
    }
}

/// Options for word extraction: delimiters plus the shared text knobs.
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct WordOptions<'a> {
    /// Extra word-boundary characters, or the default set when `None`.
    pub delimiters: Option<String>,
    /// Clip rectangle, or the whole page.
    pub clip: Option<Rect>,
    /// Extraction flags, or the default set when `None`.
    pub flags: Option<i32>,
    /// Sort words in reading order.
    pub sort: bool,
    /// Reuse this extracted page instead of extracting again.
    pub textpage: Option<TextPage<'a>>,
}

impl<'a> WordOptions<'a> {
    /// Add these word-boundary characters.
    pub fn with_delimiters(mut self, delimiters: &str) -> Self {
        self.delimiters = Some(delimiters.to_string());
        self
    }

    /// Clip extraction to this rectangle.
    pub fn with_clip(mut self, clip: Rect) -> Self {
        self.clip = Some(clip);
        self
    }

    /// Set the extraction flags.
    pub fn with_flags(mut self, flags: i32) -> Self {
        self.flags = Some(flags);
        self
    }

    /// Sort words in reading order.
    pub fn with_sort(mut self, sort: bool) -> Self {
        self.sort = sort;
        self
    }

    /// Reuse this extracted page instead of extracting again.
    pub fn with_textpage(mut self, textpage: TextPage<'a>) -> Self {
        self.textpage = Some(textpage);
        self
    }
}

/// Options for block extraction: the shared text knobs.
#[derive(Debug, Clone, Copy, Default)]
#[non_exhaustive]
pub struct BlockOptions<'a> {
    /// Clip rectangle, or the whole page.
    pub clip: Option<Rect>,
    /// Extraction flags, or the default set when `None`.
    pub flags: Option<i32>,
    /// Sort blocks in reading order.
    pub sort: bool,
    /// Reuse this extracted page instead of extracting again.
    pub textpage: Option<TextPage<'a>>,
}

impl<'a> BlockOptions<'a> {
    /// Clip extraction to this rectangle.
    pub fn with_clip(mut self, clip: Rect) -> Self {
        self.clip = Some(clip);
        self
    }

    /// Set the extraction flags.
    pub fn with_flags(mut self, flags: i32) -> Self {
        self.flags = Some(flags);
        self
    }

    /// Sort blocks in reading order.
    pub fn with_sort(mut self, sort: bool) -> Self {
        self.sort = sort;
        self
    }

    /// Reuse this extracted page instead of extracting again.
    pub fn with_textpage(mut self, textpage: TextPage<'a>) -> Self {
        self.textpage = Some(textpage);
        self
    }
}

/// Options for text search: clip, hit shape, flags, hit cap, and a page.
#[derive(Debug, Clone, Copy, Default)]
#[non_exhaustive]
pub struct SearchOptions<'a> {
    /// Clip rectangle, or the whole page.
    pub clip: Option<Rect>,
    /// Return quadrilaterals instead of rectangles.
    pub quads: bool,
    /// Search flags, or the default set when `None`.
    pub flags: Option<i32>,
    /// Maximum hits, or no cap at zero.
    pub limit: i32,
    /// Search this extracted page instead of extracting again.
    pub textpage: Option<TextPage<'a>>,
}

impl<'a> SearchOptions<'a> {
    /// Clip the search to this rectangle.
    pub fn with_clip(mut self, clip: Rect) -> Self {
        self.clip = Some(clip);
        self
    }

    /// Return quadrilaterals instead of rectangles.
    pub fn with_quads(mut self, quads: bool) -> Self {
        self.quads = quads;
        self
    }

    /// Set the search flags.
    pub fn with_flags(mut self, flags: i32) -> Self {
        self.flags = Some(flags);
        self
    }

    /// Cap the number of hits (zero lifts the cap).
    pub fn with_limit(mut self, limit: i32) -> Self {
        self.limit = limit;
        self
    }

    /// Search this extracted page instead of extracting again.
    pub fn with_textpage(mut self, textpage: TextPage<'a>) -> Self {
        self.textpage = Some(textpage);
        self
    }
}

/// Search hits: rectangles by default, quadrilaterals on request.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum SearchHits {
    /// One rectangle per hit.
    Rects(Vec<Rect>),
    /// One quadrilateral per hit.
    Quads(Vec<Quad>),
}

/// Options for table search; reserved for the recorded keyword set.
#[derive(Debug, Clone, Copy, Default)]
#[non_exhaustive]
pub struct TableOptions {}

/// Found tables on a page: the table list.
#[derive(Debug, Clone, PartialEq)]
pub struct TableFinder {
    /// Tables found, in page order.
    pub tables: Vec<Table>,
}

/// One found table: its box, grid size, and cells.
#[derive(Debug, Clone, PartialEq)]
pub struct Table {
    /// Table box, in points.
    pub bbox: Rect,
    /// Number of grid rows.
    pub row_count: i32,
    /// Number of grid columns.
    pub col_count: i32,
    /// Cells in row-major order.
    pub cells: Vec<TableCell>,
}

/// One table cell: its box and text.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct TableCell {
    /// Cell box, in points.
    pub bbox: Rect,
    /// Cell text.
    pub text: String,
}

/// One font on a page: its object number and name.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct FontInfo {
    /// Font object number.
    pub xref: i32,
    /// Base font name.
    pub name: String,
    /// Resource key (`F1`).
    pub res_name: String,
    /// Font subtype (`Type1`).
    pub subtype: String,
    /// Encoding name, empty when the dictionary sets none.
    pub encoding: String,
    /// Font program extension, `n/a` when nothing is embedded.
    pub ext: String,
}

/// One extracted font: its name and raw data.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct FontData {
    /// Font name.
    pub name: String,
    /// Raw font file bytes.
    pub data: Vec<u8>,
    /// Font program extension, `n/a` when nothing is embedded.
    pub ext: String,
    /// Font subtype (`Type1`).
    pub subtype: String,
}

/// Options for OCR text pages: language and tessdata path.
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct OcrOptions {
    /// Recognition language, or the engine default when `None`.
    pub language: Option<String>,
    /// Tessdata directory, or the engine default when `None`.
    pub tessdata: Option<String>,
}

/// Options for whole-document text reads: page set plus the text knobs.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct BulkTextOptions {
    /// Zero-based pages to read, or all pages when `None`.
    pub pages: Option<Vec<i32>>,
    /// Reading method name.
    pub method: String,
    /// Worker count, or the default when `None`.
    pub concurrency: Option<i32>,
    /// Output format.
    pub format: TextFormat,
    /// Clip rectangle, or whole pages.
    pub clip: Option<Rect>,
    /// Extraction flags, or the default set when `None`.
    pub flags: Option<i32>,
    /// Sort blocks in reading order.
    pub sort: bool,
    /// Extra word-boundary characters for word reads.
    pub delimiters: Option<String>,
}

impl Default for BulkTextOptions {
    fn default() -> Self {
        Self {
            pages: None,
            method: "single".to_string(),
            concurrency: None,
            format: TextFormat::Text,
            clip: None,
            flags: None,
            sort: false,
            delimiters: None,
        }
    }
}

impl BulkTextOptions {
    /// Read only these zero-based pages.
    pub fn with_pages(mut self, pages: Vec<i32>) -> Self {
        self.pages = Some(pages);
        self
    }

    /// Set the reading method name.
    pub fn with_method(mut self, method: &str) -> Self {
        self.method = method.to_string();
        self
    }

    /// Set the worker count.
    pub fn with_concurrency(mut self, concurrency: i32) -> Self {
        self.concurrency = Some(concurrency);
        self
    }

    /// Set the output format.
    pub fn with_format(mut self, format: TextFormat) -> Self {
        self.format = format;
        self
    }

    /// Clip extraction to this rectangle.
    pub fn with_clip(mut self, clip: Rect) -> Self {
        self.clip = Some(clip);
        self
    }

    /// Set the extraction flags.
    pub fn with_flags(mut self, flags: i32) -> Self {
        self.flags = Some(flags);
        self
    }

    /// Sort blocks in reading order.
    pub fn with_sort(mut self, sort: bool) -> Self {
        self.sort = sort;
        self
    }

    /// Add these word-boundary characters for word reads.
    pub fn with_delimiters(mut self, delimiters: &str) -> Self {
        self.delimiters = Some(delimiters.to_string());
        self
    }
}

/// Options for per-page callbacks: page set and reading method.
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct ApplyPagesOptions {
    /// Zero-based pages to visit, or all pages when `None`.
    pub pages: Option<Vec<i32>>,
    /// Reading method name.
    pub method: String,
    /// Worker count, or the default when `None`.
    pub concurrency: Option<i32>,
}

/// Insertable text: one string or a line list.
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum TextArg {
    /// One text string.
    One(String),
    /// One string per line.
    Many(Vec<String>),
}

/// An extracted text page: cached text plus the clip it was read with.
#[derive(Clone, Copy)]
#[allow(dead_code)]
pub struct TextPage<'a> {
    doc: &'a PdfDocument,
    page: usize,
    clip: Option<Rect>,
    flags: i32,
    matrix: Option<Matrix>,
}

impl<'a> std::fmt::Debug for TextPage<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TextPage")
            .field("page", &self.page)
            .field("clip", &self.clip)
            .field("flags", &self.flags)
            .field("matrix", &self.matrix)
            .finish()
    }
}

impl<'a> TextPage<'a> {
    /// Extract the text of a page.
    pub fn new(_page: Page<'a>) -> Self {
        let page = _page;
        Self {
            doc: page.document(),
            page: page.index(),
            clip: None,
            flags: 0,
            matrix: None,
        }
    }

    /// Clip extraction to this rectangle.
    pub fn with_clip(mut self, clip: Rect) -> Self {
        self.clip = Some(clip);
        self
    }

    /// Set the extraction flags.
    pub fn with_flags(mut self, flags: i32) -> Self {
        self.flags = flags;
        self
    }

    /// Blocks on the extracted page.
    pub fn extract_blocks(&self) -> Result<Vec<TextBlock>> {
        let engine = engine_for(self.doc)?;
        let capture = capture_page(&engine, self.page, false)?;
        Ok(capture.render_blocks(self.window(&capture), self.flags, false))
    }

    /// Structured dict of the extracted page.
    pub fn extract_dict(&self, _sort: bool) -> Result<TextDict> {
        let engine = engine_for(self.doc)?;
        let capture = capture_page(&engine, self.page, true)?;
        Ok(capture.render_dict(self.window(&capture), self.flags, _sort, false))
    }

    /// HTML fragment of the extracted page.
    pub fn extract_html(&self) -> Result<String> {
        let engine = engine_for(self.doc)?;
        let capture = capture_page(&engine, self.page, false)?;
        Ok(capture.render_html(self.page, self.window(&capture), self.flags, false))
    }

    /// Placed images on the extracted page.
    pub fn extract_imginfo(&self, _hashes: bool) -> Result<Vec<PlacedImage>> {
        let engine = engine_for(self.doc)?;
        let (_, height) = page_dims(&engine, self.page)?;
        let figures = capture_figures(&engine, self.page, height)?;
        Ok(figures_to_imginfo(&figures, _hashes))
    }

    /// JSON text of the extracted page.
    pub fn extract_json(&self, _sort: bool) -> Result<String> {
        let engine = engine_for(self.doc)?;
        let capture = capture_page(&engine, self.page, false)?;
        Ok(capture.render_json(self.window(&capture), self.flags, _sort, false))
    }

    /// Raw structured dict of the extracted page.
    pub fn extract_rawdict(&self, _sort: bool) -> Result<TextDict> {
        let engine = engine_for(self.doc)?;
        let capture = capture_page(&engine, self.page, true)?;
        Ok(capture.render_dict(self.window(&capture), self.flags, _sort, true))
    }

    /// Raw JSON text of the extracted page.
    pub fn extract_rawjson(&self, _sort: bool) -> Result<String> {
        let engine = engine_for(self.doc)?;
        let capture = capture_page(&engine, self.page, false)?;
        Ok(capture.render_json(self.window(&capture), self.flags, _sort, true))
    }

    /// Text selected between two points.
    pub fn extract_selection(&self, _begin: Point, _end: Point) -> Result<String> {
        let engine = engine_for(self.doc)?;
        let capture = capture_page(&engine, self.page, false)?;
        Ok(capture.render_selection(_begin, _end, self.window(&capture)))
    }

    /// Plain text of the extracted page.
    pub fn extract_text(&self, _sort: bool) -> Result<String> {
        let engine = engine_for(self.doc)?;
        let capture = capture_page(&engine, self.page, false)?;
        Ok(capture.render_text(self.window(&capture), self.flags, _sort))
    }

    /// Text inside a rectangle.
    pub fn extract_textbox(&self, _rect: Rect) -> Result<String> {
        let engine = engine_for(self.doc)?;
        let capture = capture_page(&engine, self.page, false)?;
        let clip = intersect_clips(self.clip, Some(_rect)).unwrap_or(_rect);
        let clip =
            textpage_window(capture.width, capture.height, Some(clip), self.matrix).unwrap_or(clip);
        Ok(capture.render_textbox(clip, self.flags))
    }

    /// Words on the extracted page.
    pub fn extract_words(&self, _delimiters: Option<&str>) -> Result<Vec<TextWord>> {
        let engine = engine_for(self.doc)?;
        let capture = capture_page(&engine, self.page, false)?;
        Ok(capture.render_words(self.window(&capture), self.flags, false, _delimiters))
    }

    /// XHTML fragment of the extracted page.
    pub fn extract_xhtml(&self) -> Result<String> {
        let engine = engine_for(self.doc)?;
        let capture = capture_page(&engine, self.page, false)?;
        Ok(capture.render_xhtml(self.page, self.window(&capture), self.flags, false))
    }

    /// XML fragment of the extracted page.
    pub fn extract_xml(&self) -> Result<String> {
        let engine = engine_for(self.doc)?;
        let capture = capture_page(&engine, self.page, false)?;
        Ok(capture.render_xml(self.page, self.window(&capture), self.flags, false))
    }

    /// Interned-string pool size of the extraction.
    pub fn poolsize(&self) -> Result<usize> {
        Err(ErrorKind::WrongType.to_error("TextPage.poolsize is not supported"))
    }

    /// This page's extraction window over a capture: its clip seen
    /// through its matrix. `rect` stays the raw clip: the matrix narrows
    /// what extracts, not what the page reports.
    fn window(&self, capture: &CapturedPage) -> Option<Rect> {
        textpage_window(capture.width, capture.height, self.clip, self.matrix)
    }

    /// Clip the page was extracted with.
    pub fn rect(&self) -> Result<Rect> {
        if let Some(clip) = self.clip {
            return Ok(clip);
        }
        let engine = engine_for(self.doc)?;
        let (width, height) = page_dims(&engine, self.page)?;
        Ok(Rect {
            x0: 0.0,
            y0: 0.0,
            x1: width,
            y1: height,
        })
    }

    /// Hits for a needle on the extracted page.
    pub fn search(&self, _needle: &str, _hit_max: i32, _quads: bool) -> Result<SearchHits> {
        let engine = engine_for(self.doc)?;
        let capture = capture_page(&engine, self.page, false)?;
        Ok(capture.render_search(_needle, self.window(&capture), _quads, _hit_max))
    }

    /// Compare against another value; ordering is unsupported.
    pub fn compare(&self, _other: &Self, _op: CompareOp) -> Result<bool> {
        Err(ErrorKind::BadValue.to_error("ordering is not supported for text pages"))
    }
}

impl<'a> Page<'a> {
    /// Append this page's text to another extracted page.
    pub fn extend_textpage(
        &self,
        _target: &mut TextPage<'_>,
        _flags: i32,
        _matrix: Option<Matrix>,
    ) -> Result<()> {
        let _ = self.index();
        Err(ErrorKind::EngineFault
            .to_error("extend_textpage needs an owning page; this text page is a borrowed view"))
    }

    /// Tables found on the page.
    pub fn find_tables(&self, _options: &TableOptions) -> Result<TableFinder> {
        let engine = engine_for(self.document())?;
        find_page_tables(&engine, self.index())
    }

    /// Fonts used on the page.
    pub fn get_fonts(&self, _full: bool) -> Result<Vec<FontInfo>> {
        let engine = engine_for(self.document())?;
        page_fonts_from_bytes(engine_bytes(&engine), self.index())
    }

    /// Page text in the requested format.
    pub fn get_text(&self, _options: &TextOptions) -> Result<TextOutput> {
        let options = _options;
        let (engine, page) = resolve_source(self.document(), self.index(), options.textpage)?;
        let (clip, flags) = resolve_clip_flags(options.clip, options.flags, options.textpage);
        let capture = capture_page(&engine, page, format_needs_figures(options.format))?;
        let clip = window_for(&capture, clip, options.textpage);
        Ok(render_format(
            &capture,
            options.format,
            page,
            clip,
            flags,
            options.sort,
        ))
    }

    /// Text blocks on the page.
    pub fn get_text_blocks(&self, _options: &BlockOptions) -> Result<Vec<TextBlock>> {
        let options = _options;
        let (engine, page) = resolve_source(self.document(), self.index(), options.textpage)?;
        let (clip, flags) = resolve_clip_flags(options.clip, options.flags, options.textpage);
        let capture = capture_page(&engine, page, false)?;
        let clip = window_for(&capture, clip, options.textpage);
        Ok(capture.render_blocks(clip, flags, options.sort))
    }

    /// Text selected between two points.
    pub fn get_text_selection(
        &self,
        _begin: Point,
        _end: Point,
        _clip: Option<Rect>,
    ) -> Result<String> {
        let engine = engine_for(self.document())?;
        let capture = capture_page(&engine, self.index(), false)?;
        Ok(capture.render_selection(_begin, _end, _clip))
    }

    /// Words on the page.
    pub fn get_text_words(&self, _options: &WordOptions) -> Result<Vec<TextWord>> {
        let options = _options;
        let (engine, page) = resolve_source(self.document(), self.index(), options.textpage)?;
        let (clip, flags) = resolve_clip_flags(options.clip, options.flags, options.textpage);
        let capture = capture_page(&engine, page, false)?;
        let clip = window_for(&capture, clip, options.textpage);
        Ok(capture.render_words(clip, flags, options.sort, options.delimiters.as_deref()))
    }

    /// Text inside a rectangle.
    pub fn get_textbox(&self, _rect: Rect, _textpage: Option<TextPage<'_>>) -> Result<String> {
        let (engine, page) = resolve_source(self.document(), self.index(), _textpage)?;
        let (clip, flags) = resolve_clip_flags(Some(_rect), None, _textpage);
        let capture = capture_page(&engine, page, false)?;
        let clip = window_for(&capture, clip, _textpage);
        Ok(capture.render_textbox(clip.unwrap_or(_rect), flags))
    }

    /// Extracted text page for reuse.
    pub fn get_textpage(
        &self,
        _clip: Option<Rect>,
        _flags: i32,
        _matrix: Option<Matrix>,
    ) -> Result<TextPage<'_>> {
        Ok(TextPage {
            doc: self.document(),
            page: self.index(),
            clip: _clip,
            flags: _flags,
            matrix: _matrix,
        })
    }

    /// OCR-extracted text page.
    pub fn get_textpage_ocr(&self, _options: &OcrOptions) -> Result<TextPage<'_>> {
        let _ = (self.index(), _options.language.as_deref());
        Err(ErrorKind::EngineFault
            .to_error("optical character recognition is not wired into this text page"))
    }

    /// Traced spans on the page.
    pub fn get_texttrace(&self) -> Result<Vec<TextTrace>> {
        let engine = engine_for(self.document())?;
        let capture = capture_page(&engine, self.index(), false)?;
        Ok(capture.render_trace())
    }

    /// Hits for a needle on the page.
    pub fn search_for(&self, _needle: &str, _options: &SearchOptions) -> Result<SearchHits> {
        let options = _options;
        let (engine, page) = resolve_source(self.document(), self.index(), options.textpage)?;
        let (clip, _) = resolve_clip_flags(options.clip, options.flags, options.textpage);
        let capture = capture_page(&engine, page, false)?;
        let clip = window_for(&capture, clip, options.textpage);
        Ok(capture.render_search(_needle, clip, options.quads, options.limit))
    }
}

impl PdfDocument {
    /// Fonts used by form fields.
    pub fn form_fonts(&self) -> Result<Vec<FontInfo>> {
        let engine = engine_for(self)?;
        form_fonts_from_bytes(engine_bytes(&engine))
    }

    /// Font file for an object number.
    pub fn extract_font(
        &self,
        _xref: i32,
        _info_only: bool,
        _name: Option<&str>,
    ) -> Result<FontData> {
        let engine = engine_for(self)?;
        extract_font_from_bytes(engine_bytes(&engine), _xref, _info_only)
    }

    /// Character widths of a font.
    pub fn get_char_widths(
        &self,
        _xref: i32,
        _limit: i32,
        _index: i32,
        _font_dict: Option<&Value>,
    ) -> Result<Vec<f64>> {
        let engine = engine_for(self)?;
        char_widths_from_bytes(engine_bytes(&engine), _xref, _limit, _index, _font_dict)
    }

    /// Fonts used on a page.
    pub fn get_page_fonts(&self, _page: i32, _full: bool) -> Result<Vec<FontInfo>> {
        let engine = engine_for(self)?;
        let page = checked_page_number(&engine, _page)?;
        page_fonts_from_bytes(engine_bytes(&engine), page)
    }

    /// Page text in the requested format.
    pub fn get_page_text(&self, _page: i32, _options: &TextOptions) -> Result<TextOutput> {
        let options = _options;
        let (engine, page) = resolve_source_numbered(self, _page, options.textpage)?;
        let (clip, flags) = resolve_clip_flags(options.clip, options.flags, options.textpage);
        let capture = capture_page(&engine, page, format_needs_figures(options.format))?;
        let clip = window_for(&capture, clip, options.textpage);
        Ok(render_format(
            &capture,
            options.format,
            page,
            clip,
            flags,
            options.sort,
        ))
    }

    /// Hits for a needle on a page.
    pub fn search_page_for(
        &self,
        _page: i32,
        _needle: &str,
        _options: &SearchOptions,
    ) -> Result<SearchHits> {
        let options = _options;
        let (engine, page) = resolve_source_numbered(self, _page, options.textpage)?;
        let (clip, _) = resolve_clip_flags(options.clip, options.flags, options.textpage);
        let capture = capture_page(&engine, page, false)?;
        let clip = window_for(&capture, clip, options.textpage);
        Ok(capture.render_search(_needle, clip, options.quads, options.limit))
    }

    /// Whether an object is a font.
    pub fn xref_is_font(&self, _xref: i32) -> Result<bool> {
        let engine = engine_for(self)?;
        xref_is_font_in_bytes(engine_bytes(&engine), _xref)
    }
}

/// Probe a font on a page.
pub fn check_font(_page: &Page<'_>, _fontname: &str) -> Result<Value> {
    let engine = engine_for(_page.document())?;
    check_font_in_bytes(engine_bytes(&engine), _page.index(), _fontname)
}

/// Probe font info for an object number.
pub fn check_font_info(_doc: &PdfDocument, _xref: i32) -> Result<Value> {
    let engine = engine_for(_doc)?;
    check_font_info_in_bytes(engine_bytes(&engine), _xref)
}

/// Store font info on a document.
///
/// Each record names a font object and its base name; the live font is
/// probed through [`check_font_info_in_bytes`], mismatches fail loudly,
/// and the record's name is written back to `/BaseFont` when the font
/// carries another. An empty list stores nothing.
pub fn update_font_info(_doc: &mut PdfDocument, _info: &[Value]) -> Result<()> {
    if _info.is_empty() {
        return Ok(());
    }
    let bytes = _doc.to_bytes()?;
    let mut dirty = false;
    for record in _info {
        let row = match record {
            Value::List(row) => row,
            _ => return Err(font_record_type()),
        };
        let xref = row
            .first()
            .and_then(|v| match v {
                Value::Int(n) => i32::try_from(*n).ok(),
                _ => None,
            })
            .ok_or_else(font_record_type)?;
        let name = row
            .get(3)
            .and_then(|v| match v {
                Value::Str(s) => Some(s.clone()),
                _ => None,
            })
            .ok_or_else(font_record_type)?;
        match check_font_info_in_bytes(&bytes, xref)? {
            Value::List(pair) if pair.len() == 2 => {}
            _ => match font_identity_in_bytes(&bytes, xref)? {
                Some(_) => {}
                None => {
                    return Err(
                        ErrorKind::BadValue.to_error(format!("font info names no font: {xref}"))
                    );
                }
            },
        }
        let id = (xref as u32, 0);
        let current = _doc
            .lopdf()
            .get_dictionary(id)
            .ok()
            .and_then(|dict| dict.get(b"BaseFont").ok().cloned());
        if current
            .as_ref()
            .and_then(|obj| obj.as_name().ok())
            .is_some_and(|current| current == name.as_bytes())
        {
            continue;
        }
        _doc.lopdf_mut()
            .get_dictionary_mut(id)?
            .set("BaseFont", lopdf::Object::Name(name.into_bytes()));
        dirty = true;
    }
    if dirty {
        _doc.sync_engine()?;
    }
    Ok(())
}

/// Malformed font info records are type errors.
fn font_record_type() -> crate::error::Error {
    ErrorKind::WrongType.to_error("font info records need an xref first and a base name fourth")
}

/// Mark quads with a text annotation.
pub fn page_add_text_marker<'b>(
    _page: &'b mut PageMut<'_>,
    _quads: &[Quad],
    _annot_kind: i32,
) -> Result<Annot<'b>> {
    Err(ErrorKind::EngineFault
        .to_error("text markers need the annotation lane; this lane cannot mint annotations"))
}

/// Canonical form of a character: ASCII uppercase folds to lowercase.
pub fn canon(_ch: char) -> char {
    if _ch.is_ascii_uppercase() {
        _ch.to_ascii_lowercase()
    } else {
        _ch
    }
}

/// Canonical form of a string.
pub fn chartocanon(_text: &str) -> String {
    _text.to_string()
}

/// Stylesheet for a font code.
pub fn css_for_font(
    _fontcode: &str,
    _css: Option<&str>,
    _archive: Option<Value>,
    _name: Option<&str>,
) -> Result<String> {
    Ok(_css.unwrap_or("").to_string())
}

/// Resolve the tessdata directory.
pub fn get_tessdata(_tessdata: Option<&str>) -> Result<String> {
    if let Some(path) = _tessdata {
        return Ok(path.to_string());
    }
    Ok(std::env::var("TESSDATA_PREFIX").unwrap_or_default())
}

/// Whole-document text in the requested format.
pub fn get_text(_path: &str, _options: &BulkTextOptions) -> Result<String> {
    let options = _options;
    let format = options.format;
    let doc = PdfDocument::open(_path)?;
    let engine = engine_for(&doc)?;
    let count = engine.page_count();
    let pages: Vec<usize> = match options.pages.as_deref() {
        Some(list) => {
            let mut out = Vec::with_capacity(list.len());
            for pno in list {
                if *pno < 0 || *pno as usize >= count {
                    return Err(ErrorKind::IndexOutOfRange
                        .to_error(format!("page {pno} out of range ({count} pages)")));
                }
                out.push(*pno as usize);
            }
            out
        }
        None => (0..count).collect(),
    };
    let flags = options.flags.unwrap_or(0);
    let mut out = String::new();
    for page in pages {
        let capture = capture_page(&engine, page, false)?;
        match format {
            TextFormat::Text => {
                out.push_str(&capture.render_text(options.clip, flags, options.sort));
            }
            TextFormat::Html => {
                out.push_str(&capture.render_html(page, options.clip, flags, options.sort));
            }
            TextFormat::Xhtml => {
                out.push_str(&capture.render_xhtml(page, options.clip, flags, options.sort));
            }
            TextFormat::Xml => {
                out.push_str(&capture.render_xml(page, options.clip, flags, options.sort));
            }
            TextFormat::Json => {
                out.push_str(&capture.render_json(options.clip, flags, options.sort, false));
            }
            TextFormat::RawJson => {
                out.push_str(&capture.render_json(options.clip, flags, options.sort, true));
            }
            _ => {
                return Err(ErrorKind::BadValue.to_error(format!(
                    "bulk text has no string form for option {format:?}"
                )));
            }
        }
    }
    Ok(out)
}

/// Typeset width of a string.
pub fn get_text_length(_text: &str, _fontname: &str, _fontsize: f64, _encoding: i32) -> f64 {
    estimate_text_length(_text, _fontname, _fontsize)
}

/// Codepoint for a glyph name: the table hit, else the replacement code.
///
/// Single characters are not glyph names: they read as unknown, like any
/// name the table does not carry.
pub fn glyph_name_to_unicode(_name: &str) -> i32 {
    if _name.chars().count() < 2 {
        return 0xFFFD;
    }
    match glyph_name_to_code(_name) {
        -1 => 0xFFFD,
        code => code,
    }
}

/// Empty grid cells over a rectangle.
pub fn make_table(_rect: Rect, _cols: i32, _rows: i32) -> Vec<TableCell> {
    if _cols <= 0 || _rows <= 0 {
        return Vec::new();
    }
    let x0 = _rect.x0.min(_rect.x1);
    let y0 = _rect.y0.min(_rect.y1);
    let x1 = _rect.x0.max(_rect.x1);
    let y1 = _rect.y0.max(_rect.y1);
    let (cols, rows) = (_cols as usize, _rows as usize);
    let (w, h) = ((x1 - x0) / f64::from(_cols), (y1 - y0) / f64::from(_rows));
    let mut out = Vec::with_capacity(cols * rows);
    for row in 0..rows {
        for col in 0..cols {
            out.push(TableCell {
                bbox: Rect {
                    x0: x0 + col as f64 * w,
                    y0: y0 + row as f64 * h,
                    x1: x0 + (col + 1) as f64 * w,
                    y1: y0 + (row + 1) as f64 * h,
                },
                text: String::new(),
            });
        }
    }
    out
}

/// Fold one character into highlight hits.
pub fn on_highlight_char(_hits: &Value, _line: &Value, _ch: &Value) -> Result<Value> {
    Ok(_hits.clone())
}

/// Quad of a span box in reading direction.
pub fn recover_bbox_quad(_direction: (f64, f64), _span: &DictSpan, _bbox: Rect) -> Quad {
    orient_quad(_bbox, _direction)
}

/// Quad of a raw character in reading direction.
pub fn recover_char_quad(_direction: (f64, f64), _span: &DictSpan, _ch: &RawChar) -> Quad {
    orient_quad(_ch.bbox, _direction)
}

/// Quad covering a line.
pub fn recover_line_quad(_line: &DictLine, _spans: Option<&[DictSpan]>) -> Quad {
    let dir = (_line.direction.x, _line.direction.y);
    let bbox = match _spans {
        Some(spans) if !spans.is_empty() => {
            union_all(spans.iter().map(|s| s.bbox)).unwrap_or(_line.bbox)
        }
        _ => _line.bbox,
    };
    orient_quad(bbox, dir)
}

/// Quad of a span in reading direction.
pub fn recover_quad(_direction: (f64, f64), _span: &DictSpan) -> Quad {
    orient_quad(_span.bbox, _direction)
}

/// Quad covering span characters.
pub fn recover_span_quad(
    _direction: (f64, f64),
    _span: &DictSpan,
    _chars: Option<&[RawChar]>,
) -> Quad {
    let bbox = match _chars {
        Some(chars) if !chars.is_empty() => {
            union_all(chars.iter().map(|c| c.bbox)).unwrap_or(_span.bbox)
        }
        _ => _span.bbox,
    };
    orient_quad(bbox, _direction)
}

/// Repair a monospaced font mapping.
///
/// Reads the live widths through [`char_widths_from_bytes`]; a Courier
/// family font whose widths vary is normalised to its most common width,
/// anything else simple is already sound. Anything that is not a simple
/// Type 1 or TrueType font keeps the loud raise: repairing composite or
/// missing fonts is undefined in this version.
///
/// A bare base-14 face has no descriptor for the probe to report (it pins
/// null for one), so its identity is read from the dictionary instead.
pub fn repair_mono_font(_page: &mut PageMut<'_>, _font: &FontInfo) -> Result<()> {
    let bytes = _page.document_mut().to_bytes()?;
    let (base, subtype) = match check_font_info_in_bytes(&bytes, _font.xref)? {
        Value::List(pair) if pair.len() == 2 => {
            let text = |value: &Value| match value {
                Value::Str(s) => Some(s.clone()),
                _ => None,
            };
            match (text(&pair[0]), text(&pair[1])) {
                (Some(base), Some(subtype)) => (base, subtype),
                _ => return Err(super::not_supported("module", "repair_mono_font")),
            }
        }
        _ => match font_identity_in_bytes(&bytes, _font.xref)? {
            Some((base, subtype)) => (base, subtype),
            None => return Err(super::not_supported("module", "repair_mono_font")),
        },
    };
    if !matches!(subtype.as_str(), "Type1" | "MMType1" | "TrueType") {
        return Err(super::not_supported("module", "repair_mono_font"));
    }
    if !is_mono_family(&base) {
        return Ok(());
    }
    // The widths reader pads to its limit, so the live array sets the
    // window: only true entries vote for the common width.
    let font_id = (_font.xref as u32, 0);
    let slot = _page
        .document_mut()
        .lopdf()
        .get_dictionary(font_id)
        .ok()
        .and_then(|dict| dict.get(b"Widths").ok().cloned());
    let len = match &slot {
        Some(lopdf::Object::Reference(id)) => _page
            .document_mut()
            .lopdf()
            .get_object(*id)
            .ok()
            .and_then(|obj| obj.as_array().ok())
            .map(|arr| arr.len())
            .unwrap_or(0),
        Some(lopdf::Object::Array(arr)) => arr.len(),
        _ => 0,
    };
    if len == 0 {
        return Ok(());
    }
    let widths = char_widths_from_bytes(&bytes, _font.xref, len as i32, 0, None)?;
    if widths.iter().any(|w| w.fract() != 0.0) {
        return Ok(());
    }
    let mut counts: Vec<(i64, usize)> = Vec::new();
    for width in widths.iter().map(|w| *w as i64) {
        match counts.iter_mut().find(|(w, _)| *w == width) {
            Some(entry) => entry.1 += 1,
            None => counts.push((width, 1)),
        }
    }
    if counts.len() < 2 {
        return Ok(());
    }
    let mode = counts
        .iter()
        .max_by_key(|(_, count)| *count)
        .map(|(width, _)| *width)
        .unwrap_or(0);
    let fixed = lopdf::Object::Array(vec![lopdf::Object::Integer(mode); len]);
    let lp = _page.document_mut().lopdf_mut();
    match slot {
        Some(lopdf::Object::Reference(id)) => {
            lp.objects.insert(id, fixed);
        }
        Some(lopdf::Object::Array(_)) => {
            lp.get_dictionary_mut(font_id)?.set("Widths", fixed);
        }
        _ => return Ok(()),
    }
    _page.document_mut().sync_engine()
}

/// Whether a base font name belongs to the Courier family: an optional
/// subset prefix plus `Courier`, in any case.
fn is_mono_family(base: &str) -> bool {
    let bare = match base.split_once('+') {
        Some((prefix, rest))
            if prefix.len() == 6 && prefix.bytes().all(|b| b.is_ascii_uppercase()) =>
        {
            rest
        }
        _ => base,
    };
    bare.get(.."Courier".len())
        .is_some_and(|head| head.eq_ignore_ascii_case("Courier"))
}

/// Glyph name for a codepoint: the Unicode character name.
///
/// Code zero is the `.notdef` glyph rather than a named character; negative
/// codes read as the replacement label, matching the font method.
pub fn unicode_to_glyph_name(_code: i32) -> String {
    if _code == 0 {
        return ".notdef".to_string();
    }
    if _code < 0 {
        return format!("U+{:04X}", 0xFFFD);
    }
    super::edit::unicode_name(_code as u32)
}

impl<'a> PartialEq for TextPage<'a> {
    fn eq(&self, _other: &Self) -> bool {
        std::ptr::eq(self.doc, _other.doc)
            && self.page == _other.page
            && self.clip == _other.clip
            && self.flags == _other.flags
            && self.matrix == _other.matrix
    }
}

// ===== Lane L1: shared extraction model =====
//
// The impl blocks above are thin adapters: each one opens an engine handle
// over its document, captures the page below, and renders. The bindings
// behind other languages do the same over their own handle, so every
// language reads one model and one set of renders.

/// Recorded extraction/search flag values, shared with the engine bit tests
/// below: one definition in `constants`, re-exported here so every import
/// path reads the recorded numbers.
pub use super::constants::{
    TEXTFLAGS_SEARCH, TEXTFLAGS_TEXT, TEXT_DEHYPHENATE, TEXT_INHIBIT_SPACES,
};

/// Shorthand for the engine handle this lane reads through.
type EngineDoc = pdf_engine::PdfDocument;

/// One captured character: its box, character, origin, and synthesis flag.
#[derive(Debug, Clone)]
pub struct CapturedChar {
    /// Character box, top-left origin, y down, in points.
    pub bbox: Rect,
    /// The character itself.
    pub ch: char,
    /// Pen origin of the character, in points.
    pub origin: Point,
    /// Whether the character was synthesised rather than read.
    pub synthetic: bool,
}

/// One captured span: style, text, and per-character geometry.
#[derive(Debug, Clone)]
pub struct CapturedSpan {
    /// Span box, in points.
    pub bbox: Rect,
    /// Pen origin of the span, in points.
    pub origin: Point,
    /// Font name, empty when the page gives no unambiguous name.
    pub font: String,
    /// Font size, in points.
    pub size: f64,
    /// Fill colour as sRGB red/green/blue/alpha bytes.
    pub color: [u8; 4],
    /// Span flags bitfield.
    pub flags: i32,
    /// Bidirectional level.
    pub bidi: i32,
    /// Per-character flags bitfield.
    pub char_flags: i32,
    /// Font ascender, in ems.
    pub ascender: f64,
    /// Font descender, in ems (negative).
    pub descender: f64,
    /// Span text.
    pub text: String,
    /// Per-character boxes, in reading order.
    pub chars: Vec<CapturedChar>,
    /// Whether the span reads top to bottom rather than left to right.
    pub vertical: bool,
    /// Whether this span fills a gap between two drawn spans.
    pub gap: bool,
}

/// One captured line: its box, direction, and spans in reading order.
#[derive(Debug, Clone)]
pub struct CapturedLine {
    /// Line box, in points.
    pub bbox: Rect,
    /// Baseline direction: `(1, 0)` left to right, `(0, -1)` top to bottom.
    pub dir: (f64, f64),
    /// Spans making up the line, gap fillers included.
    pub spans: Vec<CapturedSpan>,
}

/// One captured text block: its box, number, and lines.
#[derive(Debug, Clone)]
pub struct CapturedBlock {
    /// Block box, in points.
    pub bbox: Rect,
    /// Zero-based block number on the page.
    pub number: i32,
    /// Lines making up the block, top to bottom.
    pub lines: Vec<CapturedLine>,
}

/// One captured figure: its box, pixel size, and encoded bytes.
#[derive(Debug, Clone)]
pub struct CapturedFigure {
    /// Figure box, in points.
    pub bbox: Rect,
    /// Image width, in pixels.
    pub width: i32,
    /// Image height, in pixels.
    pub height: i32,
    /// Bits per component.
    pub bits: i32,
    /// Encoded image bytes.
    pub data: Vec<u8>,
    /// File extension of the encoded bytes, without the dot.
    pub ext: String,
    /// Colour-space code.
    pub colorspace: i32,
    /// Colour-space name.
    pub colorspace_name: String,
    /// Horizontal resolution, in dots per inch.
    pub xres: i32,
    /// Vertical resolution, in dots per inch.
    pub yres: i32,
}

/// One captured page: size, text blocks, and figures.
///
/// The bindings own this: an extracted text page holds one capture per
/// page folded into it (see [`CapturedPage::merge`]), and every render
/// below reads it without touching the document again.
#[derive(Debug, Clone, Default)]
pub struct CapturedPage {
    /// Page width, in points.
    pub width: f64,
    /// Page height, in points.
    pub height: f64,
    /// Text blocks on the page, in capture order, numbered from zero.
    pub blocks: Vec<CapturedBlock>,
    /// Figures on the page, in page order. Filled only when the capture
    /// asked for them (see [`capture_page`]).
    pub figures: Vec<CapturedFigure>,
}

impl CapturedPage {
    /// Fold another capture in after this one, renumbering its blocks.
    ///
    /// The merged page keeps this page's size: blocks carry their own
    /// boxes, and a size is a property of one page, not of two.
    pub fn merge(&mut self, other: &CapturedPage) {
        let base = self.blocks.len() as i32;
        for block in &other.blocks {
            let mut next = block.clone();
            next.number += base;
            self.blocks.push(next);
        }
        self.figures.extend(other.figures.iter().cloned());
    }
}

/// Open an engine handle over a facade document's bytes.
///
/// The compat views borrow the facade, whose engine stays private to it,
/// so the lane re-opens the serialised document and reads through that.
/// The handle keeps ligature code points, the way the recordings read
/// them, and runs no model tier (see [`compat_options`]).
pub(crate) fn engine_for(doc: &PdfDocument) -> Result<EngineDoc> {
    let bytes = doc.to_bytes()?;
    engine_from_bytes(&bytes)
}

/// Open a model-free engine handle over document bytes.
fn engine_from_bytes(bytes: &[u8]) -> Result<EngineDoc> {
    let mut engine = EngineDoc::open(bytes.to_vec())
        .map_err(|e| ErrorKind::UnreadableFile.to_error(format!("cannot reopen document: {e}")))?;
    engine.set_keep_ligatures(true);
    Ok(engine)
}

/// The bytes the engine handle parses, for the low-level font readers.
fn engine_bytes(engine: &EngineDoc) -> &[u8] {
    engine.pdf().data().as_ref()
}

/// Reject an out-of-range page before the engine sees it.
fn checked_page(engine: &EngineDoc, page: usize) -> Result<()> {
    let count = engine.page_count();
    if page < count {
        Ok(())
    } else {
        Err(ErrorKind::IndexOutOfRange
            .to_error(format!("page {page} out of range ({count} pages)")))
    }
}

/// Visible page size in points, following the facade's own reading.
fn page_dims(engine: &EngineDoc, page: usize) -> Result<(f64, f64)> {
    checked_page(engine, page)?;
    let geom = engine.page_geometry(page)?;
    Ok((geom.crop_box.width(), geom.crop_box.height()))
}

/// Extraction options for one compat page: a one-page window, no learned
/// pass, the document's font names.
///
/// The learned pass stays off because this module is feature-free: it
/// must not pull model tiers, and it must not let model versions move
/// text segmentation under the bindings. The document names stay on
/// because the recordings the bindings match spell the page's fonts (#643).
fn compat_options(page: usize) -> ExtractOptions {
    let mut options = ExtractOptions::window(page, 1);
    options.learned = false;
    options.document_font_names = true;
    options
}

/// Capture one page's text, and its figures when asked.
///
/// `with_figures` runs a second interpretation pass, so only the renders
/// that show images ask for it.
pub fn capture_page(engine: &EngineDoc, page: usize, with_figures: bool) -> Result<CapturedPage> {
    checked_page(engine, page)?;
    let (width, height) = page_dims(engine, page)?;
    let options = compat_options(page);
    let blocks = engine.extract_blocks(&options)?;
    let fallback = fallback_font_name(engine, page);
    let mut out = Vec::new();
    for (number, block) in blocks.iter().filter(|b| b.page == page).enumerate() {
        out.push(capture_block(
            block,
            number as i32,
            height,
            fallback.as_deref(),
        ));
    }
    let figures = if with_figures {
        capture_figures(engine, page, height)?
    } else {
        Vec::new()
    };
    Ok(CapturedPage {
        width,
        height,
        blocks: out,
        figures,
    })
}

/// The page's only font name, for spans that carry none.
///
/// The engine names embedded fonts and leaves the base-14 ones blank; on
/// a page with exactly one font that font can only be the span's.
fn fallback_font_name(engine: &EngineDoc, page: usize) -> Option<String> {
    let fonts = page_fonts_from_bytes(engine_bytes(engine), page).ok()?;
    if fonts.len() == 1 {
        Some(fonts.into_iter().next().map(|f| f.name)?)
    } else {
        None
    }
}

/// Capture one engine block: regroup its spans into lines.
fn capture_block(
    block: &Block,
    number: i32,
    page_height: f64,
    fallback_font: Option<&str>,
) -> CapturedBlock {
    let mut horizontal: Vec<&TextSpan> = Vec::new();
    let mut vertical: Vec<&TextSpan> = Vec::new();
    for span in &block.spans {
        if span_is_vertical(span) {
            vertical.push(span);
        } else {
            horizontal.push(span);
        }
    }
    horizontal.sort_by(|a, b| {
        let ya = a.y + a.height / 2.0;
        let yb = b.y + b.height / 2.0;
        yb.partial_cmp(&ya).unwrap_or(std::cmp::Ordering::Equal)
    });
    // Cluster top to bottom by y overlap; a span that overlaps no open
    // line opens one.
    let mut clusters: Vec<Vec<&TextSpan>> = Vec::new();
    let mut ranges: Vec<(f64, f64)> = Vec::new();
    for span in horizontal {
        let (lo, hi) = (span.y, span.y + span.height);
        let mut placed = false;
        for (i, cluster) in clusters.iter_mut().enumerate() {
            let (clo, chi) = ranges[i];
            if lo < chi && hi > clo {
                cluster.push(span);
                ranges[i] = (clo.min(lo), chi.max(hi));
                placed = true;
                break;
            }
        }
        if !placed {
            clusters.push(vec![span]);
            ranges.push((lo, hi));
        }
    }
    let mut lines: Vec<CapturedLine> = clusters
        .into_iter()
        .map(|spans| capture_hline(spans, page_height, fallback_font))
        .collect();
    // Cluster left to right by x overlap; a span that overlaps no open
    // line opens one.
    vertical.sort_by(|a, b| {
        let xa = a.x + a.width / 2.0;
        let xb = b.x + b.width / 2.0;
        xa.partial_cmp(&xb).unwrap_or(std::cmp::Ordering::Equal)
    });
    let mut vclusters: Vec<Vec<&TextSpan>> = Vec::new();
    let mut vranges: Vec<(f64, f64)> = Vec::new();
    for span in vertical {
        let (lo, hi) = (span.x, span.x + span.width);
        let mut placed = false;
        for (i, cluster) in vclusters.iter_mut().enumerate() {
            let (clo, chi) = vranges[i];
            if lo < chi && hi > clo {
                cluster.push(span);
                vranges[i] = (clo.min(lo), chi.max(hi));
                placed = true;
                break;
            }
        }
        if !placed {
            vclusters.push(vec![span]);
            vranges.push((lo, hi));
        }
    }
    for spans in vclusters {
        lines.push(capture_vline(spans, page_height, fallback_font));
    }
    lines.sort_by(|a, b| {
        let ya = (a.bbox.y0 + a.bbox.y1) / 2.0;
        let yb = (b.bbox.y0 + b.bbox.y1) / 2.0;
        ya.partial_cmp(&yb).unwrap_or(std::cmp::Ordering::Equal)
    });
    let bbox = union_all(lines.iter().map(|l| l.bbox)).unwrap_or(Rect {
        x0: 0.0,
        y0: 0.0,
        x1: 0.0,
        y1: 0.0,
    });
    CapturedBlock {
        bbox,
        number,
        lines,
    }
}

/// Whether a span reads top to bottom: its transform turns the baseline
/// upright, which a quarter turn shows as dominant off-diagonal terms.
fn span_is_vertical(span: &TextSpan) -> bool {
    span.transform
        .is_some_and(|t| t[1].abs() > t[0].abs() && t[2].abs() > t[3].abs())
}

/// Capture one horizontal line: x-ordered spans plus gap fillers.
fn capture_hline(
    mut spans: Vec<&TextSpan>,
    page_height: f64,
    fallback_font: Option<&str>,
) -> CapturedLine {
    spans.sort_by(|a, b| a.x.partial_cmp(&b.x).unwrap_or(std::cmp::Ordering::Equal));
    let made: Vec<CapturedSpan> = spans
        .iter()
        .map(|s| capture_span(s, page_height, fallback_font, false))
        .collect();
    let threshold = gap_threshold(&made);
    let made = merge_runs(made, threshold, (1.0, 0.0));
    let mut out: Vec<CapturedSpan> = Vec::with_capacity(made.len() * 2);
    for (i, span) in made.into_iter().enumerate() {
        let gap = if i == 0 {
            None
        } else {
            let prev = &out[out.len() - 1];
            gap_span(prev, &span, threshold)
        };
        if let Some(gap) = gap {
            out.push(gap);
        }
        out.push(span);
    }
    let bbox = union_all(out.iter().map(|s| s.bbox)).unwrap_or(Rect {
        x0: 0.0,
        y0: 0.0,
        x1: 0.0,
        y1: 0.0,
    });
    CapturedLine {
        bbox,
        dir: (1.0, 0.0),
        spans: out,
    }
}

/// Capture one vertical line: spans ordered along the reading
/// direction, then folded into runs like a horizontal line.
fn capture_vline(
    mut spans: Vec<&TextSpan>,
    page_height: f64,
    fallback_font: Option<&str>,
) -> CapturedLine {
    let dir = spans.first().map(|s| vline_dir(s)).unwrap_or((0.0, -1.0));
    spans.sort_by(|a, b| {
        let pa = a.x * dir.0 + (page_height - a.y) * dir.1;
        let pb = b.x * dir.0 + (page_height - b.y) * dir.1;
        pa.partial_cmp(&pb).unwrap_or(std::cmp::Ordering::Equal)
    });
    let made: Vec<CapturedSpan> = spans
        .iter()
        .map(|s| capture_span(s, page_height, fallback_font, true))
        .collect();
    let threshold = vgap_threshold(&made);
    let out = merge_runs(made, threshold, dir);
    let bbox = union_all(out.iter().map(|s| s.bbox)).unwrap_or(Rect {
        x0: 0.0,
        y0: 0.0,
        x1: 0.0,
        y1: 0.0,
    });
    CapturedLine {
        bbox,
        dir,
        spans: out,
    }
}

/// Reading direction of a quarter-turned span in the lane's y-down frame:
/// the transform's baseline row, normalized. A span without a transform
/// reads up the page, the only turned direction the fixtures show.
fn vline_dir(span: &TextSpan) -> (f64, f64) {
    match span.transform {
        Some(t) => {
            let len = (t[0] * t[0] + t[1] * t[1]).sqrt();
            if len > 0.0 {
                (t[0] / len, -t[1] / len)
            } else {
                (0.0, -1.0)
            }
        }
        None => (0.0, -1.0),
    }
}

/// Whether two adjacent captured spans read as one: same style, and
/// either close enough that no gap filler would split them, or already
/// joined by whitespace the engine drew (an explicit space span).
///
/// The engine hands over one span per glyph while the recordings show one
/// span per content run (`Test page` reads as a single span, `Form` up the
/// page as one), so runs merge here. A wide gap with no whitespace on
/// either side stays split: the gap filler materialises it as its own space
/// span, the way the recordings show column gaps (`Deceased`, ` `, `MM ...`).
/// The gap runs along the line's reading direction.
fn mergeable(prev: &CapturedSpan, next: &CapturedSpan, threshold: f64, dir: (f64, f64)) -> bool {
    if prev.vertical != next.vertical || prev.gap || next.gap {
        return false;
    }
    if prev.font != next.font
        || prev.size.to_bits() != next.size.to_bits()
        || prev.color != next.color
        || prev.flags != next.flags
        || prev.bidi != next.bidi
    {
        return false;
    }
    let gap = if dir.0.abs() >= dir.1.abs() {
        next.bbox.x0 - prev.bbox.x1
    } else if dir.1 < 0.0 {
        prev.bbox.y0 - next.bbox.y1
    } else {
        next.bbox.y0 - prev.bbox.y1
    };
    gap <= threshold
        || prev.text.ends_with(char::is_whitespace)
        || next.text.starts_with(char::is_whitespace)
}

/// Fold adjacent mergeable spans into content runs, in order.
///
/// Merging only regroups characters, so line text is unchanged: pairs the
/// gap filler would have joined with a space stay split for it, and pairs
/// it would have left alone join without one.
fn merge_runs(mut spans: Vec<CapturedSpan>, threshold: f64, dir: (f64, f64)) -> Vec<CapturedSpan> {
    let mut out: Vec<CapturedSpan> = Vec::with_capacity(spans.len());
    for span in spans.drain(..) {
        let join = out
            .last()
            .is_some_and(|prev| mergeable(prev, &span, threshold, dir));
        if join {
            if let Some(prev) = out.last_mut() {
                prev.text.push_str(&span.text);
                prev.chars.extend(span.chars);
                prev.bbox = union_all([prev.bbox, span.bbox].into_iter()).unwrap_or(prev.bbox);
            }
            continue;
        }
        out.push(span);
    }
    out
}

/// The x gap past which two spans read as separate words: half the
/// line's mean advance, the engine's own rule for joining spans.
fn gap_threshold(spans: &[CapturedSpan]) -> f64 {
    let mut width = 0.0;
    let mut chars = 0usize;
    let mut size = 0.0f64;
    for span in spans {
        width += (span.bbox.x1 - span.bbox.x0).max(0.0);
        chars += span.text.chars().filter(|c| !c.is_whitespace()).count();
        size = size.max(span.size);
    }
    if chars > 0 && width > 0.0 {
        0.5 * width / chars as f64
    } else {
        0.25 * size.max(1.0)
    }
}

/// The y gap past which two turned spans read as separate runs: half the
/// line's mean advance along the baseline, mirroring [`gap_threshold`].
fn vgap_threshold(spans: &[CapturedSpan]) -> f64 {
    let mut height = 0.0;
    let mut chars = 0usize;
    let mut size = 0.0f64;
    for span in spans {
        height += (span.bbox.y1 - span.bbox.y0).max(0.0);
        chars += span.text.chars().filter(|c| !c.is_whitespace()).count();
        size = size.max(span.size);
    }
    if chars > 0 && height > 0.0 {
        0.5 * height / chars as f64
    } else {
        0.25 * size.max(1.0)
    }
}

/// A synthetic space span bridging two spans, or `None` when they sit
/// close or either side already ends in whitespace.
fn gap_span(prev: &CapturedSpan, next: &CapturedSpan, threshold: f64) -> Option<CapturedSpan> {
    let gap = next.bbox.x0 - prev.bbox.x1;
    if gap <= threshold {
        return None;
    }
    if prev.text.ends_with(char::is_whitespace) || next.text.starts_with(char::is_whitespace) {
        return None;
    }
    let bbox = Rect {
        x0: prev.bbox.x1,
        y0: prev.bbox.y0.min(next.bbox.y0),
        x1: next.bbox.x0,
        y1: prev.bbox.y1.max(next.bbox.y1),
    };
    let origin = Point {
        x: bbox.x0,
        y: next.origin.y,
    };
    Some(CapturedSpan {
        bbox,
        origin,
        font: next.font.clone(),
        size: next.size,
        color: next.color,
        flags: next.flags,
        bidi: next.bidi,
        char_flags: next.char_flags | 512,
        ascender: next.ascender,
        descender: next.descender,
        text: " ".to_string(),
        chars: vec![CapturedChar {
            bbox,
            ch: ' ',
            origin,
            synthetic: true,
        }],
        vertical: false,
        gap: true,
    })
}

/// Cut a compat span font name to its first 24 characters (#643).
///
/// The recordings never spell a name longer than 24: f1040's
/// `ITCFranklinGothicStd-Demi` (25 characters in the document) arrives as
/// `ITCFranklinGothicStd-Dem`, the only recorded name over the cut. The
/// engine reports the document's name exactly; the cut lives here, on the
/// compat side, where the recording was read.
fn truncate_font_name(name: &str) -> String {
    name.chars().take(24).collect()
}

/// Capture one engine span with per-character boxes.
fn capture_span(
    span: &TextSpan,
    page_height: f64,
    fallback_font: Option<&str>,
    vertical: bool,
) -> CapturedSpan {
    let font = truncate_font_name(
        &span
            .font_name
            .clone()
            .or_else(|| fallback_font.map(str::to_string))
            .unwrap_or_default(),
    );
    let (ascender, descender) = span_metrics(span, &font);
    let chars = align_chars(span, page_height, ascender, descender, vertical);
    let bbox = union_all(chars.iter().map(|c| c.bbox))
        .unwrap_or_else(|| flip_box(span.x, span.y, span.width, span.height, page_height));
    let origin = chars.first().map(|c| c.origin).unwrap_or(Point {
        x: bbox.x0,
        y: bbox.y1 - descender.abs() * span.font_size,
    });
    let mut flags = if span.font_name.is_some() { 4 } else { 0 };
    if span.is_bold {
        flags |= 16;
    }
    if span.is_italic {
        flags |= 64;
    }
    CapturedSpan {
        bbox,
        origin,
        font,
        size: span.font_size,
        color: span.color.unwrap_or([0, 0, 0, 255]),
        flags,
        bidi: 0,
        char_flags: if span.is_bold { 24 } else { 16 },
        ascender,
        descender,
        text: span.text.clone(),
        chars,
        vertical,
        gap: false,
    }
}

/// Recorded vertical metrics (ascender, descender) in ems for the
/// base-14 faces the lane's fixtures exercise, read off the committed
/// recordings: the simple fixture's Helvetica-12 span reports 1.075/-0.299
/// (f32-quantized), and the structured fixture's Helvetica-Bold-16 word
/// boxes solve to 1.07/-0.307 against the engine's own baselines. The
/// engine's AFM pair (0.718/-0.207) describes the same faces and does not
/// match what the lane has to reproduce.
fn base14_metrics(font: &str) -> Option<(f64, f64)> {
    match font {
        "Helvetica" => Some((f64::from(1.075f32), f64::from(-0.299f32))),
        "Helvetica-Bold" => Some((1.07, -0.307)),
        _ => None,
    }
}

/// A span's vertical metrics in ems: the recorded base-14 pair when the
/// face is one, else the embedded font's ascent and descent, else
/// Helvetica's recorded pair when no font binary is reachable.
fn span_metrics(span: &TextSpan, font: &str) -> (f64, f64) {
    if let Some(recorded) = base14_metrics(font) {
        return recorded;
    }
    span.font_metrics
        .map(|m| (m.ascent / 1000.0, m.descent / 1000.0))
        .unwrap_or((1.075, -0.299))
}

/// Pair each character of a span with its glyph box.
///
/// Glyph boxes are one per drawn glyph while characters count the span's
/// whitespace too, so the walk spends a glyph per character and lets a
/// whitespace character pass glyph-less while characters still outnumber
/// glyphs. Characters without a glyph take an interpolated box from the
/// pen cursor so every character answers a box.
///
/// Advance spans read metric boxes: the glyph edges along the baseline
/// with the font's ascender above it and descender below, which is the
/// recorded shape (`Test page` spans y 79.1-95.588 at baseline 92, not the
/// 80-92 advance slab). Anything else keeps the flipped glyph box.
fn align_chars(
    span: &TextSpan,
    page_height: f64,
    ascender: f64,
    descender: f64,
    vertical: bool,
) -> Vec<CapturedChar> {
    let size = span.font_size;
    let metric = span.bounds_source == BoundsSource::Advance && size > 0.0;
    let chars: Vec<char> = span.text.chars().collect();
    let glyphs = &span.char_bounds;
    let mut at = 0usize;
    let advance = if chars.is_empty() {
        span.font_size.max(1.0) * 0.5
    } else if span.width > 0.0 {
        span.width / chars.len() as f64
    } else {
        span.font_size.max(1.0) * 0.5
    };
    let mut cursor_x = span.x;
    let mut cursor_y = span.y + span.height;
    let mut out = Vec::with_capacity(chars.len());
    for (i, ch) in chars.iter().enumerate() {
        let chars_left = chars.len() - i;
        let glyphs_left = glyphs.len().saturating_sub(at);
        let boxed = if ch.is_whitespace() && chars_left > glyphs_left {
            None
        } else if at < glyphs.len() {
            let g = glyphs[at];
            at += 1;
            Some(g)
        } else if glyphs.is_empty() {
            None
        } else {
            Some(glyphs[glyphs.len() - 1])
        };
        let up = boxed.unwrap_or_else(|| {
            if vertical {
                let hi = cursor_y;
                let lo = (cursor_y - advance).max(span.y);
                cursor_y = lo;
                [span.x, lo, span.x + span.width.max(advance), hi]
            } else {
                let lo = cursor_x;
                let hi = cursor_x + advance;
                cursor_x = hi;
                [lo, span.y, hi, span.y + span.height]
            }
        });
        if boxed.is_some() {
            if vertical {
                cursor_y = up[1].min(cursor_y);
            } else {
                cursor_x = up[2].max(cursor_x);
            }
        }
        let (bbox, origin) = if metric && !vertical {
            // Baseline runs along y-up `up[1]` (the span's own y for an
            // interpolated character); the box spans ascender to descender.
            let base_up = if boxed.is_some() { up[1] } else { span.y };
            let base = page_height - base_up;
            (
                Rect {
                    x0: up[0],
                    y0: base - ascender * size,
                    x1: up[2],
                    y1: base - descender * size,
                },
                Point { x: up[0], y: base },
            )
        } else if metric && vertical && boxed.is_some() {
            // A quarter-turned advance box carries the baseline in x and
            // the advance along it: the glyph at `up[1]` runs `up[2]-up[0]`
            // up the page, with ascender and descender across the baseline.
            let bx = up[0];
            let run = (up[2] - up[0]).max(0.0);
            (
                Rect {
                    x0: bx - ascender * size,
                    y0: page_height - (up[1] + run),
                    x1: bx - descender * size,
                    y1: page_height - up[1],
                },
                Point {
                    x: bx,
                    y: page_height - up[1],
                },
            )
        } else {
            let bbox = Rect {
                x0: up[0],
                y0: page_height - up[3],
                x1: up[2],
                y1: page_height - up[1],
            };
            let origin = if vertical {
                Point {
                    x: bbox.x1 - descender.abs() * span.font_size,
                    y: bbox.y1,
                }
            } else {
                Point {
                    x: bbox.x0,
                    y: bbox.y1 - descender.abs() * span.font_size,
                }
            };
            (bbox, origin)
        };
        out.push(CapturedChar {
            bbox,
            ch: *ch,
            origin,
            synthetic: false,
        });
    }
    out
}

/// Flip a y-up user-space box into the lane's y-down frame.
fn flip_box(x: f64, y: f64, w: f64, h: f64, page_height: f64) -> Rect {
    Rect {
        x0: x,
        y0: page_height - (y + h),
        x1: x + w,
        y1: page_height - y,
    }
}

/// Union of boxes, or `None` for an empty iterator.
pub(crate) fn union_all(boxes: impl Iterator<Item = Rect>) -> Option<Rect> {
    let mut out: Option<Rect> = None;
    for b in boxes {
        out = Some(match out {
            None => b,
            Some(o) => Rect {
                x0: o.x0.min(b.x0),
                y0: o.y0.min(b.y0),
                x1: o.x1.max(b.x1),
                y1: o.y1.max(b.y1),
            },
        });
    }
    out
}

/// Capture one page's figures: boxes flipped y-down, pixel fields unknown.
///
/// Port note (P1, #643): the freeze-base engine no longer hands over figure
/// bytes, so boxes come from [`extract_image_boxes`](EngineDoc::extract_image_boxes),
/// which reports `[left, bottom, right, top]` in the blocks' frame, and the
/// pixel fields stay at their unknown defaults. Presence, order and boxes
/// are intact; `data` is empty and `width`/`height` are zero until a byte
/// source for figures lands.
fn capture_figures(
    engine: &EngineDoc,
    page: usize,
    page_height: f64,
) -> Result<Vec<CapturedFigure>> {
    let all = engine.extract_image_boxes()?;
    let mut out = Vec::new();
    for bbox in all.get(page).cloned().unwrap_or_default() {
        let [x0, y0, x1, y1] = bbox;
        out.push(CapturedFigure {
            bbox: Rect {
                x0,
                y0: page_height - y1,
                x1,
                y1: page_height - y0,
            },
            width: 0,
            height: 0,
            bits: 8,
            data: Vec::new(),
            ext: String::new(),
            colorspace: 3,
            colorspace_name: "DeviceRGB".to_string(),
            xres: 96,
            yres: 96,
        });
    }
    Ok(out)
}

/// Read width, height, bit depth, and colour type from PNG bytes.
///
/// Kept for the figure byte source (see [`capture_figures`]); unused until
/// that lands.
#[allow(dead_code)]
fn png_dims(data: &[u8]) -> Option<(i32, i32, i32, u8)> {
    const SIG: [u8; 8] = [137, 80, 78, 71, 13, 10, 26, 10];
    if data.len() < 33 || data[0..8] != SIG || &data[12..16] != b"IHDR" {
        return None;
    }
    let num =
        |at: usize| u32::from_be_bytes([data[at], data[at + 1], data[at + 2], data[at + 3]]) as i32;
    Some((num(16), num(20), i32::from(data[24]), data[25]))
}

// ===== Capture end marker L1A =====

// ===== Lane L1: renders over a capture =====

/// Whether two rectangles share an area (edge touches do not count).
fn overlaps(a: Rect, b: Rect) -> bool {
    a.x0 < b.x1 && a.x1 > b.x0 && a.y0 < b.y1 && a.y1 > b.y0
}

/// Extraction window of a text page: the clip (or the full page when the
/// page has none) seen through the inverse of the page matrix.
///
/// The recordings pin the rule: a text page built under a 2x scale reads
/// only the quarter of the page the window keeps (`2025` clips to `2`,
/// right-hand columns drop), while `rect` still reports the untransformed
/// page. No matrix, an identity matrix, or a singular one (no inverse)
/// leaves the clip alone.
pub fn textpage_window(
    page_width: f64,
    page_height: f64,
    clip: Option<Rect>,
    matrix: Option<Matrix>,
) -> Option<Rect> {
    let Some(matrix) = matrix else {
        return clip;
    };
    if matrix.a == 1.0
        && matrix.b == 0.0
        && matrix.c == 0.0
        && matrix.d == 1.0
        && matrix.e == 0.0
        && matrix.f == 0.0
    {
        return clip;
    }
    if matrix.a * matrix.d - matrix.b * matrix.c == 0.0 {
        return clip;
    }
    let base = clip.unwrap_or(Rect {
        x0: 0.0,
        y0: 0.0,
        x1: page_width,
        y1: page_height,
    });
    Some(base * matrix.invert(None))
}

/// Intersection of two optional clips: content must satisfy both.
fn intersect_clips(first: Option<Rect>, second: Option<Rect>) -> Option<Rect> {
    match (first, second) {
        (None, clip) | (clip, None) => clip,
        (Some(a), Some(b)) => Some(Rect {
            x0: a.x0.max(b.x0),
            y0: a.y0.max(b.y0),
            x1: a.x1.min(b.x1),
            y1: a.y1.min(b.y1),
        }),
    }
}

/// One span reduced to the characters a clip keeps.
struct FilteredSpan<'c> {
    span: &'c CapturedSpan,
    chars: Vec<&'c CapturedChar>,
}

impl FilteredSpan<'_> {
    /// Text of the kept characters, in order.
    fn text(&self) -> String {
        self.chars.iter().map(|c| c.ch).collect()
    }

    /// Union of the kept characters' boxes.
    fn bbox(&self) -> Rect {
        union_all(self.chars.iter().map(|c| c.bbox)).unwrap_or(Rect {
            x0: 0.0,
            y0: 0.0,
            x1: 0.0,
            y1: 0.0,
        })
    }
}

/// One line reduced to the spans a clip keeps.
struct FilteredLine<'c> {
    index: usize,
    dir: (f64, f64),
    spans: Vec<FilteredSpan<'c>>,
}

impl FilteredLine<'_> {
    /// Line text: span texts in order, gap fillers dropped on inhibit.
    fn text(&self, inhibit_spaces: bool) -> String {
        let mut out = String::new();
        for span in &self.spans {
            if inhibit_spaces && span.span.gap {
                continue;
            }
            out.push_str(&span.text());
        }
        out
    }

    /// Union of the kept spans' boxes.
    fn bbox(&self, inhibit_spaces: bool) -> Rect {
        let boxes = self.spans.iter().filter_map(|s| {
            if inhibit_spaces && s.span.gap {
                None
            } else {
                Some(s.bbox())
            }
        });
        union_all(boxes).unwrap_or(Rect {
            x0: 0.0,
            y0: 0.0,
            x1: 0.0,
            y1: 0.0,
        })
    }

    /// Kept characters in reading order, gap fillers dropped on inhibit.
    fn chars(&self, inhibit_spaces: bool) -> Vec<&CapturedChar> {
        let mut out = Vec::new();
        for span in &self.spans {
            if inhibit_spaces && span.span.gap {
                continue;
            }
            out.extend(span.chars.iter().copied());
        }
        out
    }
}

/// One block reduced to the lines a clip keeps.
struct FilteredBlock<'c> {
    number: i32,
    lines: Vec<FilteredLine<'c>>,
}

impl FilteredBlock<'_> {
    /// Union of the kept lines' boxes.
    fn bbox(&self, inhibit_spaces: bool) -> Rect {
        union_all(self.lines.iter().map(|l| l.bbox(inhibit_spaces))).unwrap_or(Rect {
            x0: 0.0,
            y0: 0.0,
            x1: 0.0,
            y1: 0.0,
        })
    }

    /// Block text: one line per kept line, each followed by a newline
    /// when the next original line is kept too, then a closing newline.
    ///
    /// A line the clip drops takes its newline with it, so a block read
    /// through a clip that spares nothing reads empty, and one read
    /// through a clip that spares everything reads as unclipped.
    fn text(&self, inhibit_spaces: bool, dehyphenate: bool) -> String {
        let mut out = String::new();
        for (i, line) in self.lines.iter().enumerate() {
            out.push_str(&line.text(inhibit_spaces));
            let adjacent = self
                .lines
                .get(i + 1)
                .is_some_and(|next| next.index == line.index + 1);
            if adjacent {
                out.push('\n');
            }
        }
        if dehyphenate {
            out = dehyphenate_block(&out);
        }
        if !self.lines.is_empty() {
            out.push('\n');
        }
        out
    }
}

/// Reduce a capture to the content a clip keeps, optionally sorted.
///
/// Sorting is stable by bottom edge, then left edge, and keeps the
/// original block numbers.
fn filter_blocks(capture: &CapturedPage, clip: Option<Rect>, sort: bool) -> Vec<FilteredBlock<'_>> {
    let mut blocks = Vec::new();
    for block in &capture.blocks {
        let mut lines = Vec::new();
        for (index, line) in block.lines.iter().enumerate() {
            let mut spans = Vec::new();
            for span in &line.spans {
                let chars: Vec<&CapturedChar> = span
                    .chars
                    .iter()
                    .filter(|c| clip.is_none_or(|r| overlaps(c.bbox, r)))
                    .collect();
                if !chars.is_empty() {
                    spans.push(FilteredSpan { span, chars });
                }
            }
            if !spans.is_empty() {
                lines.push(FilteredLine {
                    index,
                    dir: line.dir,
                    spans,
                });
            }
        }
        if !lines.is_empty() {
            blocks.push(FilteredBlock {
                number: block.number,
                lines,
            });
        }
    }
    if sort {
        // Stable by (bottom edge, left edge); `sort_by` keeps the capture
        // order between equals.
        blocks.sort_by(|a, b| {
            let ba = a.bbox(false);
            let bb = b.bbox(false);
            ba.y1
                .partial_cmp(&bb.y1)
                .unwrap_or(std::cmp::Ordering::Equal)
                .then_with(|| {
                    ba.x0
                        .partial_cmp(&bb.x0)
                        .unwrap_or(std::cmp::Ordering::Equal)
                })
        });
    }
    blocks
}

/// Rejoin words a line break hyphenated: a line ending in `-` whose next
/// line opens lowercase loses the hyphen and the break.
fn dehyphenate_block(text: &str) -> String {
    let lines: Vec<&str> = text.split('\n').collect();
    let mut out = String::new();
    let mut i = 0;
    while i < lines.len() {
        let line = lines[i];
        let next = lines.get(i + 1);
        let joins = line.ends_with('-')
            && !line.ends_with("--")
            && next.is_some_and(|n| n.chars().next().is_some_and(|c| c.is_lowercase()));
        if joins {
            out.push_str(&line[..line.len() - 1]);
            out.push_str(next.unwrap_or(&""));
            i += 2;
            if i < lines.len() {
                out.push('\n');
            }
        } else {
            out.push_str(line);
            if i + 1 < lines.len() {
                out.push('\n');
            }
            i += 1;
        }
    }
    out
}

impl CapturedPage {
    /// Plain text of the capture: block texts in order.
    pub fn render_text(&self, clip: Option<Rect>, flags: i32, sort: bool) -> String {
        let inhibit = flags & TEXT_INHIBIT_SPACES != 0;
        let dehyphenate = flags & TEXT_DEHYPHENATE != 0;
        let mut out = String::new();
        for block in filter_blocks(self, clip, sort) {
            out.push_str(&block.text(inhibit, dehyphenate));
        }
        out
    }

    /// Words of the capture: non-whitespace runs with their boxes.
    ///
    /// Extra `delimiters` split words further and fall out; the survivors
    /// renumber from zero within their line. Block and line numbers stay
    /// the originals, through clips and sorts alike.
    pub fn render_words(
        &self,
        clip: Option<Rect>,
        flags: i32,
        sort: bool,
        delimiters: Option<&str>,
    ) -> Vec<TextWord> {
        let inhibit = flags & TEXT_INHIBIT_SPACES != 0;
        let delims: Vec<char> = delimiters.unwrap_or("").chars().collect();
        let is_split = |c: char| c.is_whitespace() || delims.contains(&c);
        let mut out = Vec::new();
        for block in filter_blocks(self, clip, sort) {
            for line in &block.lines {
                let mut word_no = 0;
                for span in line.spans.iter().filter(|s| !s.span.gap || !inhibit) {
                    if span.span.gap {
                        continue;
                    }
                    let text = span.text();
                    let chars: Vec<char> = text.chars().collect();
                    let mut start: Option<usize> = None;
                    let mut close = |start: usize, end: usize, out: &mut Vec<TextWord>| {
                        let word: String = chars[start..end].iter().collect();
                        let bbox = union_all(span.chars[start..end].iter().map(|c| c.bbox))
                            .unwrap_or(Rect {
                                x0: 0.0,
                                y0: 0.0,
                                x1: 0.0,
                                y1: 0.0,
                            });
                        out.push(TextWord {
                            rect: bbox,
                            text: word,
                            block_no: block.number,
                            line_no: line.index as i32,
                            word_no,
                        });
                        word_no += 1;
                    };
                    for (i, ch) in chars.iter().enumerate() {
                        if is_split(*ch) {
                            if let Some(s) = start.take() {
                                close(s, i, &mut out);
                            }
                        } else if start.is_none() {
                            start = Some(i);
                        }
                    }
                    if let Some(s) = start.take() {
                        close(s, chars.len(), &mut out);
                    }
                }
            }
        }
        out
    }

    /// Blocks of the capture: one entry per text block.
    pub fn render_blocks(&self, clip: Option<Rect>, flags: i32, sort: bool) -> Vec<TextBlock> {
        let inhibit = flags & TEXT_INHIBIT_SPACES != 0;
        let dehyphenate = flags & TEXT_DEHYPHENATE != 0;
        filter_blocks(self, clip, sort)
            .iter()
            .map(|block| TextBlock {
                rect: block.bbox(inhibit),
                text: block.text(inhibit, dehyphenate),
                number: block.number,
                block_type: 0,
            })
            .collect()
    }

    /// Structured dict of the capture: text blocks, then figures.
    ///
    /// Text blocks keep their original numbers; figures follow after them
    /// in page order. Raw mode fills per-character boxes and drops the
    /// span text key (the bindings pick the key from the mode).
    pub fn render_dict(&self, clip: Option<Rect>, flags: i32, sort: bool, raw: bool) -> TextDict {
        let inhibit = flags & TEXT_INHIBIT_SPACES != 0;
        let mut blocks: Vec<DictBlock> = filter_blocks(self, clip, sort)
            .iter()
            .map(|block| {
                let lines = block
                    .lines
                    .iter()
                    .map(|line| DictLine {
                        bbox: line.bbox(inhibit),
                        wmode: 0,
                        direction: Point {
                            x: line.dir.0,
                            y: line.dir.1,
                        },
                        spans: line
                            .spans
                            .iter()
                            .filter(|s| !(inhibit && s.span.gap))
                            .map(|kept| {
                                let span = kept.span;
                                DictSpan {
                                    bbox: kept.bbox(),
                                    origin: kept
                                        .chars
                                        .first()
                                        .map(|c| c.origin)
                                        .unwrap_or(span.origin),
                                    font: span.font.clone(),
                                    size: span.size,
                                    color: pack_color(span.color),
                                    alpha: i32::from(span.color[3]),
                                    flags: span.flags,
                                    bidi: span.bidi,
                                    char_flags: span.char_flags,
                                    ascender: span.ascender,
                                    descender: span.descender,
                                    text: kept.text(),
                                    chars: if raw {
                                        kept.chars
                                            .iter()
                                            .map(|c| RawChar {
                                                bbox: c.bbox,
                                                ch: c.ch,
                                                origin: c.origin,
                                                synthetic: c.synthetic,
                                            })
                                            .collect()
                                    } else {
                                        Vec::new()
                                    },
                                }
                            })
                            .collect(),
                    })
                    .collect();
                DictBlock::Text {
                    bbox: block.bbox(inhibit),
                    number: block.number,
                    flags: 0,
                    lines,
                }
            })
            .collect();
        let mut figures: Vec<&CapturedFigure> = self
            .figures
            .iter()
            .filter(|f| clip.is_none_or(|r| overlaps(f.bbox, r)))
            .collect();
        if sort {
            figures.sort_by(|a, b| {
                a.bbox
                    .y1
                    .partial_cmp(&b.bbox.y1)
                    .unwrap_or(std::cmp::Ordering::Equal)
                    .then_with(|| {
                        a.bbox
                            .x0
                            .partial_cmp(&b.bbox.x0)
                            .unwrap_or(std::cmp::Ordering::Equal)
                    })
            });
        }
        let mut number = blocks.len() as i32;
        for figure in figures {
            blocks.push(DictBlock::Image {
                bbox: figure.bbox,
                number,
                width: figure.width,
                height: figure.height,
                bpc: figure.bits,
                colorspace: figure.colorspace,
                ext: figure.ext.clone(),
                image_data: figure.data.clone(),
                transform: Matrix {
                    a: f64::from(figure.width),
                    b: 0.0,
                    c: -0.0,
                    d: f64::from(figure.height),
                    e: figure.bbox.x0,
                    f: figure.bbox.y0,
                },
                xres: figure.xres,
                yres: figure.yres,
            });
            number += 1;
        }
        TextDict {
            width: self.width,
            height: self.height,
            blocks,
        }
    }

    /// Substring search over line texts: one box per hit.
    ///
    /// Matching is case-sensitive; a hit that touches the clip counts
    /// whole; `limit` at or below zero lifts the cap.
    pub fn render_search(
        &self,
        needle: &str,
        clip: Option<Rect>,
        quads: bool,
        limit: i32,
    ) -> SearchHits {
        if needle.is_empty() {
            return if quads {
                SearchHits::Quads(Vec::new())
            } else {
                SearchHits::Rects(Vec::new())
            };
        }
        let mut rects = Vec::new();
        let mut boxes: Vec<(Rect, (f64, f64))> = Vec::new();
        'page: for block in filter_blocks(self, None, false) {
            for line in &block.lines {
                let chars = line.chars(false);
                let text: String = chars.iter().map(|c| c.ch).collect();
                let starts: Vec<usize> = text
                    .char_indices()
                    .map(|(i, _)| i)
                    .chain(std::iter::once(text.len()))
                    .collect();
                for (at, _) in text.match_indices(needle) {
                    let from = starts.iter().position(|s| *s == at).unwrap_or(0);
                    let count = needle.chars().count();
                    let hit: Vec<Rect> = chars[from..(from + count).min(chars.len())]
                        .iter()
                        .map(|c| c.bbox)
                        .collect();
                    if let Some(bbox) = union_all(hit.into_iter()) {
                        if clip.is_none_or(|r| overlaps(bbox, r)) {
                            boxes.push((bbox, line.dir));
                            if limit > 0 && boxes.len() >= limit as usize {
                                break 'page;
                            }
                        }
                    }
                }
            }
        }
        if quads {
            SearchHits::Quads(
                boxes
                    .into_iter()
                    .map(|(b, dir)| orient_quad(b, dir))
                    .collect(),
            )
        } else {
            rects.extend(boxes.into_iter().map(|(b, _)| b));
            SearchHits::Rects(rects)
        }
    }

    /// Text inside a rectangle: clipped text without the trailing newline.
    pub fn render_textbox(&self, rect: Rect, flags: i32) -> String {
        let mut text = self.render_text(Some(rect), flags, false);
        if text.ends_with('\n') {
            text.pop();
        }
        text
    }

    /// Text between two points in reading order.
    ///
    /// The points order themselves, so a reversed selection reads the
    /// same. The first and last spanned lines clip to the points' x
    /// edges; the lines between read whole. Lines join with newlines and
    /// no trailing newline.
    pub fn render_selection(&self, begin: Point, end: Point, clip: Option<Rect>) -> String {
        let (lo, hi) = if (begin.y, begin.x) <= (end.y, end.x) {
            (begin, end)
        } else {
            (end, begin)
        };
        // Spanned lines first; the x edges apply to the first and last.
        let mut rows: Vec<&CapturedLine> = Vec::new();
        for block in &self.blocks {
            for line in &block.lines {
                if line.bbox.y1 > lo.y && line.bbox.y0 < hi.y {
                    rows.push(line);
                }
            }
        }
        let last = rows.len().saturating_sub(1);
        let mut picked: Vec<String> = Vec::new();
        for (i, line) in rows.iter().enumerate() {
            let mut text = String::new();
            for span in &line.spans {
                for ch in &span.chars {
                    if clip.is_some_and(|r| !overlaps(ch.bbox, r)) {
                        continue;
                    }
                    let cx = (ch.bbox.x0 + ch.bbox.x1) / 2.0;
                    if i == 0 && cx < lo.x {
                        continue;
                    }
                    if i == last && cx > hi.x {
                        continue;
                    }
                    text.push(ch.ch);
                }
            }
            picked.push(text);
        }
        picked.join("\n")
    }

    /// Traced spans of the capture: metrics plus per-character geometry.
    pub fn render_trace(&self) -> Vec<TextTrace> {
        let mut out = Vec::new();
        let mut seqno = 0;
        for block in &self.blocks {
            for line in &block.lines {
                for span in line.spans.iter().filter(|s| !s.gap) {
                    let [r, g, b, a] = span.color;
                    let spacewidth = span
                        .chars
                        .iter()
                        .find(|c| c.ch == ' ')
                        .map(|c| (c.bbox.x1 - c.bbox.x0).max(0.0))
                        .filter(|w| *w > 0.0)
                        .unwrap_or(span.size * 0.278);
                    out.push(TextTrace {
                        bbox: span.bbox,
                        ascender: span.ascender,
                        bidi_dir: 2,
                        bidi_level: span.bidi,
                        chars: span
                            .chars
                            .iter()
                            .map(|c| TraceChar {
                                codepoint: c.ch as i32,
                                glyph: 0,
                                origin: c.origin,
                                bbox: c.bbox,
                            })
                            .collect(),
                        font: span.font.clone(),
                        size: span.size,
                        color: [
                            f64::from(r) / 255.0,
                            f64::from(g) / 255.0,
                            f64::from(b) / 255.0,
                        ],
                        colorspace: 3,
                        descender: span.descender,
                        dir: Point {
                            x: line.dir.0,
                            y: line.dir.1,
                        },
                        flags: span.flags,
                        layer: String::new(),
                        linewidth: None,
                        opacity: f64::from(a) / 255.0,
                        seqno,
                        spacewidth,
                        span_type: 0,
                        wmode: 0,
                    });
                    seqno += 1;
                }
            }
        }
        out
    }
}

/// Pack sRGB bytes into the dict colour integer.
fn pack_color(color: [u8; 4]) -> i32 {
    (i32::from(color[0]) << 16) | (i32::from(color[1]) << 8) | i32::from(color[2])
}

/// Corners of a box oriented along a reading direction: upper-left
/// first for horizontal text, lower-left first past a quarter turn.
pub(crate) fn orient_quad(bbox: Rect, dir: (f64, f64)) -> Quad {
    if dir.0.abs() >= dir.1.abs() {
        Quad {
            ul: Point {
                x: bbox.x0,
                y: bbox.y0,
            },
            ur: Point {
                x: bbox.x1,
                y: bbox.y0,
            },
            ll: Point {
                x: bbox.x0,
                y: bbox.y1,
            },
            lr: Point {
                x: bbox.x1,
                y: bbox.y1,
            },
        }
    } else {
        Quad {
            ul: Point {
                x: bbox.x0,
                y: bbox.y1,
            },
            ur: Point {
                x: bbox.x0,
                y: bbox.y0,
            },
            ll: Point {
                x: bbox.x1,
                y: bbox.y1,
            },
            lr: Point {
                x: bbox.x1,
                y: bbox.y0,
            },
        }
    }
}

// ===== Renders end marker L1B =====

// ===== Lane L1: markup renders =====

/// A JSON number: integers bare, floats shortest with `.0` kept whole.
fn json_num(value: f64) -> String {
    if value.fract() == 0.0 && value.abs() < 1e15 {
        format!("{value:.1}")
    } else {
        format!("{value}")
    }
}

/// A JSON string: double-quoted, controls and non-ASCII backslash-escaped.
fn json_str(text: &str) -> String {
    let mut out = String::with_capacity(text.len() + 2);
    out.push('"');
    for ch in text.chars() {
        match ch {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            c if (c as u32) < 0x20 => out.push_str(&format!("\\u{:04x}", c as u32)),
            c if (c as u32) < 0x7f => out.push(c),
            c if (c as u32) < 0x10000 => out.push_str(&format!("\\u{:04x}", c as u32)),
            c => {
                let v = c as u32 - 0x10000;
                out.push_str(&format!(
                    "\\u{:04x}\\u{:04x}",
                    0xd800 + (v >> 10),
                    0xdc00 + (v & 0x3ff)
                ));
            }
        }
    }
    out.push('"');
    out
}

/// Indentation for one JSON nesting level: a single space per depth.
fn pad(out: &mut String, depth: usize) {
    for _ in 0..depth {
        out.push(' ');
    }
}

impl CapturedPage {
    /// Dict serialised as JSON text, in the recorded key order and layout.
    ///
    /// Figures stay out: no recording shows an image block in JSON, and a
    /// guessed byte encoding would pin the wrong shape.
    pub fn render_json(&self, clip: Option<Rect>, flags: i32, sort: bool, raw: bool) -> String {
        let inhibit = flags & TEXT_INHIBIT_SPACES != 0;
        let blocks = filter_blocks(self, clip, sort);
        let mut out = String::from("{\n");
        pad(&mut out, 1);
        out.push_str(&format!("\"width\":{},", json_num(self.width)));
        out.push('\n');
        pad(&mut out, 1);
        out.push_str(&format!("\"height\":{},", json_num(self.height)));
        out.push('\n');
        pad(&mut out, 1);
        out.push_str("\"blocks\":");
        if blocks.is_empty() {
            out.push_str("[]\n}");
            return out;
        }
        out.push_str("[\n");
        for (bi, block) in blocks.iter().enumerate() {
            pad(&mut out, 2);
            out.push_str("{\n");
            Self::json_kv(&mut out, 3, "\"type\"", "0", true);
            Self::json_kv(&mut out, 3, "\"number\"", &block.number.to_string(), true);
            Self::json_kv(&mut out, 3, "\"flags\"", "0", true);
            Self::json_rect(&mut out, 3, "\"bbox\"", &block.bbox(inhibit), true);
            pad(&mut out, 3);
            out.push_str("\"lines\":");
            if block.lines.is_empty() {
                out.push_str("[]\n");
            } else {
                out.push_str("[\n");
                for (li, line) in block.lines.iter().enumerate() {
                    pad(&mut out, 4);
                    out.push_str("{\n");
                    pad(&mut out, 5);
                    out.push_str("\"spans\":");
                    let spans: Vec<&FilteredSpan> = line
                        .spans
                        .iter()
                        .filter(|s| !(inhibit && s.span.gap))
                        .collect();
                    if spans.is_empty() {
                        out.push_str("[]\n");
                    } else {
                        out.push_str("[\n");
                        for (si, kept) in spans.iter().enumerate() {
                            Self::json_span(&mut out, kept, raw);
                            if si + 1 < spans.len() {
                                out.push(',');
                            }
                            out.push('\n');
                        }
                        pad(&mut out, 5);
                        out.push_str("],\n");
                    }
                    Self::json_kv(&mut out, 5, "\"wmode\"", "0", true);
                    pad(&mut out, 5);
                    out.push_str("\"dir\":[\n");
                    pad(&mut out, 6);
                    out.push_str(&format!("{},", json_num(line.dir.0)));
                    out.push('\n');
                    pad(&mut out, 6);
                    out.push_str(&json_num(line.dir.1));
                    out.push('\n');
                    pad(&mut out, 5);
                    out.push_str("],\n");
                    Self::json_rect(&mut out, 5, "\"bbox\"", &line.bbox(inhibit), false);
                    pad(&mut out, 4);
                    out.push('}');
                    if li + 1 < block.lines.len() {
                        out.push(',');
                    }
                    out.push('\n');
                }
                pad(&mut out, 3);
                out.push_str("]\n");
            }
            pad(&mut out, 2);
            out.push('}');
            if bi + 1 < blocks.len() {
                out.push(',');
            }
            out.push('\n');
        }
        pad(&mut out, 1);
        out.push_str("]\n}");
        out
    }

    /// One `"key":value` JSON line at `depth`, comma unless last.
    fn json_kv(out: &mut String, depth: usize, key: &str, value: &str, comma: bool) {
        pad(out, depth);
        out.push_str(key);
        out.push(':');
        out.push_str(value);
        if comma {
            out.push(',');
        }
        out.push('\n');
    }

    /// One `"key":[x0,y0,x1,y1]` JSON rectangle at `depth`.
    fn json_rect(out: &mut String, depth: usize, key: &str, rect: &Rect, comma: bool) {
        pad(out, depth);
        out.push_str(key);
        out.push_str(":[\n");
        for (i, v) in [rect.x0, rect.y0, rect.x1, rect.y1].iter().enumerate() {
            pad(out, depth + 1);
            out.push_str(&json_num(*v));
            if i < 3 {
                out.push(',');
            }
            out.push('\n');
        }
        pad(out, depth);
        out.push(']');
        if comma {
            out.push(',');
        }
        out.push('\n');
    }

    /// One span object: the recorded key order, characters for raw mode.
    fn json_span(out: &mut String, kept: &FilteredSpan, raw: bool) {
        let span = kept.span;
        pad(out, 6);
        out.push_str("{\n");
        Self::json_kv(out, 7, "\"size\"", &json_num(span.size), true);
        Self::json_kv(out, 7, "\"flags\"", &span.flags.to_string(), true);
        Self::json_kv(out, 7, "\"bidi\"", &span.bidi.to_string(), true);
        Self::json_kv(out, 7, "\"char_flags\"", &span.char_flags.to_string(), true);
        Self::json_kv(out, 7, "\"font\"", &json_str(&span.font), true);
        Self::json_kv(
            out,
            7,
            "\"color\"",
            &pack_color(span.color).to_string(),
            true,
        );
        Self::json_kv(
            out,
            7,
            "\"alpha\"",
            &i32::from(span.color[3]).to_string(),
            true,
        );
        Self::json_kv(out, 7, "\"ascender\"", &json_num(span.ascender), true);
        Self::json_kv(out, 7, "\"descender\"", &json_num(span.descender), true);
        if raw {
            pad(out, 7);
            out.push_str("\"chars\":");
            if kept.chars.is_empty() {
                out.push_str("[]\n");
            } else {
                out.push_str("[\n");
                for (i, ch) in kept.chars.iter().enumerate() {
                    pad(out, 8);
                    out.push_str("{\n");
                    pad(out, 9);
                    out.push_str("\"origin\":[\n");
                    pad(out, 10);
                    out.push_str(&format!("{},", json_num(ch.origin.x)));
                    out.push('\n');
                    pad(out, 10);
                    out.push_str(&json_num(ch.origin.y));
                    out.push('\n');
                    pad(out, 9);
                    out.push_str("],\n");
                    Self::json_rect(out, 9, "\"bbox\"", &ch.bbox, true);
                    let mut c = String::from("\"c\":");
                    c.push_str(&json_str(&ch.ch.to_string()));
                    c.push(',');
                    pad(out, 9);
                    out.push_str(&c);
                    out.push('\n');
                    pad(out, 9);
                    out.push_str(&format!("\"synthetic\":{}", ch.synthetic));
                    out.push('\n');
                    pad(out, 8);
                    out.push('}');
                    if i + 1 < kept.chars.len() {
                        out.push(',');
                    }
                    out.push('\n');
                }
                pad(out, 7);
                out.push_str("],\n");
            }
        } else {
            Self::json_kv(out, 7, "\"text\"", &json_str(&kept.text()), true);
        }
        pad(out, 7);
        out.push_str("\"origin\":[\n");
        let origin = kept.chars.first().map(|c| c.origin).unwrap_or(span.origin);
        pad(out, 8);
        out.push_str(&format!("{},", json_num(origin.x)));
        out.push('\n');
        pad(out, 8);
        out.push_str(&json_num(origin.y));
        out.push('\n');
        pad(out, 7);
        out.push_str("],\n");
        Self::json_rect(out, 7, "\"bbox\"", &kept.bbox(), false);
        pad(out, 6);
        out.push('}');
    }

    /// HTML fragment of the capture: one paragraph per line, one span
    /// per span, positioned by baseline and size.
    pub fn render_html(
        &self,
        page_no: usize,
        clip: Option<Rect>,
        flags: i32,
        sort: bool,
    ) -> String {
        let inhibit = flags & TEXT_INHIBIT_SPACES != 0;
        let mut out = format!(
            "<div id=\"page{page_no}\" style=\"width:{:.1}pt;height:{:.1}pt\">\n",
            self.width, self.height
        );
        for block in filter_blocks(self, clip, sort) {
            for line in &block.lines {
                let spans: Vec<&FilteredSpan> = line
                    .spans
                    .iter()
                    .filter(|s| !(inhibit && s.span.gap))
                    .collect();
                if spans.is_empty() {
                    continue;
                }
                let first = spans[0].span;
                let top = spans[0]
                    .chars
                    .first()
                    .map(|c| c.origin.y)
                    .unwrap_or(first.origin.y)
                    - 0.8 * first.size;
                out.push_str(&format!(
                    "<p style=\"top:{top:.1}pt;left:{:.1}pt;line-height:{:.1}pt\">",
                    line.bbox(inhibit).x0,
                    first.size
                ));
                for kept in spans {
                    let span = kept.span;
                    let bold = span.flags & 16 != 0;
                    let family = html_family(&span.font);
                    let serif = if span.flags & 4 != 0 {
                        "serif"
                    } else {
                        "sans-serif"
                    };
                    if bold {
                        out.push_str("<b>");
                    }
                    out.push_str(&format!(
                        "<span style=\"font-family:{family},{serif};font-size:{:.1}pt;color:#{:02x}{:02x}{:02x}\">",
                        span.size, span.color[0], span.color[1], span.color[2]
                    ));
                    out.push_str(&markup_escape(&kept.text()));
                    out.push_str("</span>");
                    if bold {
                        out.push_str("</b>");
                    }
                }
                out.push_str("</p>\n");
            }
        }
        out.push_str("</div>\n");
        out
    }

    /// XHTML fragment of the capture: one element per block run, large
    /// type set as headings, bold spans marked.
    pub fn render_xhtml(
        &self,
        page_no: usize,
        clip: Option<Rect>,
        flags: i32,
        sort: bool,
    ) -> String {
        let inhibit = flags & TEXT_INHIBIT_SPACES != 0;
        let mut out = format!("<div id=\"page{page_no}\">\n");
        for block in filter_blocks(self, clip, sort) {
            let mut run: Option<(u8, String)> = None;
            let flush = |run: &mut Option<(u8, String)>, out: &mut String| {
                if let Some((level, text)) = run.take() {
                    match level {
                        1 => out.push_str(&format!("<h1>{text}</h1>")),
                        3 => out.push_str(&format!("<h3>{text}</h3>")),
                        _ => out.push_str(&format!("<p>{text}</p>")),
                    }
                }
            };
            for line in &block.lines {
                let spans: Vec<&FilteredSpan> = line
                    .spans
                    .iter()
                    .filter(|s| !(inhibit && s.span.gap))
                    .collect();
                if spans.is_empty() {
                    continue;
                }
                let size = spans.iter().map(|s| s.span.size).fold(0.0f64, f64::max);
                let level = if size >= 18.0 {
                    1
                } else if size >= 10.0 {
                    3
                } else {
                    0
                };
                let mut text = String::new();
                for kept in spans {
                    let part = markup_escape(&kept.text());
                    if kept.span.flags & 16 != 0 {
                        text.push_str(&format!("<b>{part}</b>"));
                    } else {
                        text.push_str(&part);
                    }
                }
                match run.as_mut() {
                    Some((at, acc)) if *at == level => {
                        let gap = !(acc.ends_with(char::is_whitespace)
                            || text.starts_with(char::is_whitespace));
                        if gap {
                            acc.push(' ');
                        }
                        acc.push_str(&text);
                    }
                    _ => {
                        flush(&mut run, &mut out);
                        run = Some((level, text));
                    }
                }
            }
            flush(&mut run, &mut out);
            out.push('\n');
        }
        out.push_str("</div>\n");
        out
    }

    /// XML fragment of the capture: blocks, lines, fonts, characters.
    pub fn render_xml(&self, page_no: usize, clip: Option<Rect>, flags: i32, sort: bool) -> String {
        let inhibit = flags & TEXT_INHIBIT_SPACES != 0;
        let mut out = format!(
            "<page id=\"page{page_no}\" width=\"{}\" height=\"{}\">\n",
            self.width, self.height
        );
        for block in filter_blocks(self, clip, sort) {
            let bbox = block.bbox(inhibit);
            out.push_str(&format!(
                "<block bbox=\"{} {} {} {}\" justify=\"unknown\">\n",
                bbox.x0, bbox.y0, bbox.x1, bbox.y1
            ));
            for line in &block.lines {
                let bbox = line.bbox(inhibit);
                let dir = if line.dir.0.abs() >= line.dir.1.abs() {
                    "1 0"
                } else {
                    "0 -1"
                };
                out.push_str(&format!(
                    "<line bbox=\"{} {} {} {}\" wmode=\"0\" dir=\"{dir}\" flags=\"0\" text=\"{}\">\n",
                    bbox.x0,
                    bbox.y0,
                    bbox.x1,
                    bbox.y1,
                    markup_escape(&line.text(inhibit))
                ));
                let spans: Vec<&FilteredSpan> = line
                    .spans
                    .iter()
                    .filter(|s| !(inhibit && s.span.gap))
                    .collect();
                let mut i = 0;
                while i < spans.len() {
                    let span = spans[i].span;
                    let mut j = i + 1;
                    while j < spans.len()
                        && spans[j].span.font == span.font
                        && spans[j].span.size == span.size
                    {
                        j += 1;
                    }
                    out.push_str(&format!(
                        "<font name=\"{}\" size=\"{}\">\n",
                        markup_escape(&span.font),
                        span.size
                    ));
                    for kept in &spans[i..j] {
                        for ch in &kept.chars {
                            let q = char_quad(ch, kept.span.vertical);
                            out.push_str(&format!(
                                "<char c=\"{}\" quad=\"{} {} {} {} {} {} {} {}\" x=\"{}\" y=\"{}\" bidi=\"0\" color=\"#{:02x}{:02x}{:02x}\" alpha=\"#{:02x}\" flags=\"{}\"/>\n",
                                markup_escape(&ch.ch.to_string()),
                                q[0],
                                q[1],
                                q[2],
                                q[3],
                                q[4],
                                q[5],
                                q[6],
                                q[7],
                                ch.origin.x,
                                ch.origin.y,
                                span.color[0],
                                span.color[1],
                                span.color[2],
                                span.color[3],
                                span.char_flags
                            ));
                        }
                    }
                    out.push_str("</font>\n");
                    i = j;
                }
                out.push_str("</line>\n");
            }
            out.push_str("</block>\n");
        }
        out.push_str("</page>\n");
        out
    }
}

/// Eight quad numbers for one character: box corners in reading order.
fn char_quad(ch: &CapturedChar, vertical: bool) -> [f64; 8] {
    let b = ch.bbox;
    if vertical {
        [b.x0, b.y1, b.x0, b.y0, b.x1, b.y1, b.x1, b.y0]
    } else {
        [b.x0, b.y0, b.x1, b.y0, b.x0, b.y1, b.x1, b.y1]
    }
}

/// A font name as an HTML family: Helvetica reads as Arial, anything
/// else keeps the name up to its style suffix.
fn html_family(font: &str) -> String {
    if font.starts_with("Helvetica") {
        return "Arial".to_string();
    }
    font.split('-').next().unwrap_or(font).to_string()
}

/// Escape text for HTML, XHTML, and XML: the four markup characters by
/// entity, everything else non-ASCII numeric.
fn markup_escape(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    for ch in text.chars() {
        match ch {
            '&' => out.push_str("&amp;"),
            '<' => out.push_str("&lt;"),
            '>' => out.push_str("&gt;"),
            '"' => out.push_str("&quot;"),
            c if (c as u32) < 0x7f => out.push(c),
            c => out.push_str(&format!("&#x{:x};", c as u32)),
        }
    }
    out
}

// ===== Markup end marker L1C =====

// ===== Lane L1: image info, tables, fonts =====

/// Placed-image records for captured figures: boxes, sizes, and, on
/// request, a content digest of the encoded bytes.
pub fn figures_to_imginfo(figures: &[CapturedFigure], hashes: bool) -> Vec<PlacedImage> {
    figures
        .iter()
        .enumerate()
        .map(|(number, figure)| PlacedImage {
            bbox: figure.bbox,
            transform: Matrix {
                a: f64::from(figure.width),
                b: 0.0,
                c: -0.0,
                d: f64::from(figure.height),
                e: figure.bbox.x0,
                f: figure.bbox.y0,
            },
            width: figure.width,
            height: figure.height,
            bits: figure.bits,
            size: figure.data.len() as i32,
            number: number as i32,
            colorspace_name: figure.colorspace_name.clone(),
            has_mask: false,
            xres: figure.xres,
            yres: figure.yres,
            digest: if hashes {
                Some(fnv1a_64(&figure.data).to_be_bytes().to_vec())
            } else {
                None
            },
        })
        .collect()
}

/// FNV-1a over bytes: this lane's content digest. It names no standard;
/// it only tells identical bytes from changed ones.
fn fnv1a_64(data: &[u8]) -> u64 {
    let mut hash: u64 = 0xcbf29ce484222325;
    for byte in data {
        hash ^= u64::from(*byte);
        hash = hash.wrapping_mul(0x100000001b3);
    }
    hash
}

/// Tables of one page: runs of table-cell blocks, split at anything
/// else and at a fresh grid origin.
pub fn find_page_tables(engine: &EngineDoc, page: usize) -> Result<TableFinder> {
    checked_page(engine, page)?;
    let (_, height) = page_dims(engine, page)?;
    let options = compat_options(page);
    let blocks = engine.extract_blocks(&options)?;
    let mut tables = Vec::new();
    let mut run: Vec<&Block> = Vec::new();
    for block in blocks.iter().filter(|b| b.page == page) {
        let cell = match block.kind {
            BlockKind::TableCell { row, column, .. } => Some((row, column)),
            _ => None,
        };
        match cell {
            Some((0, 0)) if !run.is_empty() => {
                tables.push(cells_to_table(&run, height));
                run = vec![block];
            }
            Some(_) => run.push(block),
            None if !run.is_empty() => {
                tables.push(cells_to_table(&run, height));
                run = Vec::new();
            }
            None => {}
        }
    }
    if !run.is_empty() {
        tables.push(cells_to_table(&run, height));
    }
    Ok(TableFinder { tables })
}

/// One table from a run of cell blocks, in row-major order.
fn cells_to_table(run: &[&Block], page_height: f64) -> Table {
    let mut cells: Vec<(u16, u16, Rect, String)> = Vec::new();
    for block in run {
        let (row, column) = match block.kind {
            BlockKind::TableCell { row, column, .. } => (row, column),
            _ => continue,
        };
        let [x0, y0, x1, y1] = block.bbox;
        let bbox = Rect {
            x0,
            y0: page_height - y1,
            x1,
            y1: page_height - y0,
        };
        cells.push((row, column, bbox, block.text.clone()));
    }
    cells.sort_by(|a, b| (a.0, a.1).cmp(&(b.0, b.1)));
    let row_count = cells.iter().map(|c| c.0).max().unwrap_or(0) as i32 + 1;
    let col_count = cells.iter().map(|c| c.1).max().unwrap_or(0) as i32 + 1;
    let bbox = union_all(cells.iter().map(|c| c.2)).unwrap_or(Rect {
        x0: 0.0,
        y0: 0.0,
        x1: 0.0,
        y1: 0.0,
    });
    Table {
        bbox,
        row_count,
        col_count,
        cells: cells
            .into_iter()
            .map(|(_, _, bbox, text)| TableCell { bbox, text })
            .collect(),
    }
}

/// Fonts of one page from document bytes: object number and base name.
pub fn page_fonts_from_bytes(bytes: &[u8], page: usize) -> Result<Vec<FontInfo>> {
    let doc = lopdf_open(bytes)?;
    resource_fonts(&doc, page)
}

/// Fonts of the form dictionary from document bytes, empty without one.
pub fn form_fonts_from_bytes(bytes: &[u8]) -> Result<Vec<FontInfo>> {
    let doc = lopdf_open(bytes)?;
    let root = doc
        .trailer
        .get(b"Root")
        .ok()
        .map(|o| lopdf_resolve(&doc, o));
    let form = root
        .and_then(|o| o.as_dict().ok())
        .and_then(|d| d.get(b"AcroForm").ok())
        .map(|o| lopdf_resolve(&doc, o));
    let drill = form
        .and_then(|o| o.as_dict().ok())
        .and_then(|d| d.get(b"DR").ok())
        .map(|o| lopdf_resolve(&doc, o));
    let fonts = drill
        .and_then(|o| o.as_dict().ok())
        .and_then(|d| d.get(b"Font").ok())
        .map(|o| lopdf_resolve(&doc, o))
        .and_then(|o| o.as_dict().ok());
    let mut out = Vec::new();
    if let Some(dict) = fonts {
        collect_fonts(&doc, dict, &mut out);
    }
    Ok(out)
}

/// Font file of one object: base name plus the descriptor's font
/// program, or no bytes in info-only mode.
pub fn extract_font_from_bytes(bytes: &[u8], xref: i32, info_only: bool) -> Result<FontData> {
    let doc = lopdf_open(bytes)?;
    let obj = lopdf_object(&doc, xref)?;
    let dict = obj
        .as_dict()
        .map_err(|_| ErrorKind::BadValue.to_error(format!("object {xref} is not a font")))?;
    if !dict_is_font(dict) {
        return Err(ErrorKind::BadValue.to_error(format!("object {xref} is not a font")));
    }
    let name = font_base_name(dict).unwrap_or_default();
    let subtype = font_subtype(dict);
    let ext = font_ext(&doc, dict);
    if info_only {
        return Ok(FontData {
            name,
            data: Vec::new(),
            ext,
            subtype,
        });
    }
    let data = dict
        .get(b"FontDescriptor")
        .ok()
        .map(|o| lopdf_resolve(&doc, o))
        .and_then(|o| o.as_dict().ok())
        .and_then(|d| {
            [
                b"FontFile3".as_slice(),
                b"FontFile2".as_slice(),
                b"FontFile".as_slice(),
            ]
            .into_iter()
            .filter_map(|key| d.get(key).ok())
            .map(|o| lopdf_resolve(&doc, o))
            .filter_map(|o| o.as_stream().ok())
            .next()
        })
        .map(stream_bytes)
        .unwrap_or_default();
    Ok(FontData {
        name,
        data,
        ext,
        subtype,
    })
}

/// Font file of one named font: base name plus the descriptor's font
/// program, or nothing when no font carries the name.
pub fn extract_named_font_from_bytes(bytes: &[u8], named: &str) -> Result<Option<FontData>> {
    let doc = lopdf_open(bytes)?;
    let mut ids: Vec<u32> = doc.objects.keys().map(|id| id.0).collect();
    ids.sort_unstable();
    ids.dedup();
    for xref in ids {
        let Ok(obj) = lopdf_object(&doc, xref as i32) else {
            continue;
        };
        let Ok(dict) = obj.as_dict() else {
            continue;
        };
        if !dict_is_font(dict) {
            continue;
        }
        let base = font_base_name(dict).unwrap_or_default();
        if base == named {
            let data = extract_font_from_bytes(bytes, xref as i32, false)?;
            return Ok(Some(data));
        }
    }
    Ok(None)
}

/// Character widths of one font: `limit` entries from code `index` on.
pub fn char_widths_from_bytes(
    bytes: &[u8],
    xref: i32,
    limit: i32,
    index: i32,
    font_dict: Option<&Value>,
) -> Result<Vec<f64>> {
    let doc = lopdf_open(bytes)?;
    let obj = lopdf_object(&doc, xref)?;
    let dict = obj
        .as_dict()
        .map_err(|_| ErrorKind::BadValue.to_error(format!("object {xref} is not a font")))?;
    let mut first = dict
        .get(b"FirstChar")
        .ok()
        .and_then(|o| o.as_i64().ok())
        .unwrap_or(0);
    let mut missing = dict
        .get(b"MissingWidth")
        .ok()
        .and_then(lopdf_number)
        .unwrap_or(0.0);
    let mut widths: Vec<f64> = dict
        .get(b"Widths")
        .ok()
        .and_then(|o| o.as_array().ok())
        .map(|arr| flatten_numbers(arr))
        .unwrap_or_default();
    if let Some(Value::Dict(map)) = font_dict {
        if let Some(Value::List(items)) = map.get("Widths").or_else(|| map.get("widths")) {
            widths = items.iter().filter_map(value_number).collect();
        }
        if let Some(f) = map.get("FirstChar").or_else(|| map.get("first_char")) {
            if let Some(n) = value_number(f) {
                first = n as i64;
            }
        }
        if let Some(m) = map.get("MissingWidth").or_else(|| map.get("missing_width")) {
            if let Some(n) = value_number(m) {
                missing = n;
            }
        }
    }
    let limit = limit.clamp(0, 65536) as usize;
    let index = index.max(0) as i64;
    let mut out = Vec::with_capacity(limit);
    for i in 0..limit {
        let code = first + index + i as i64;
        let at = code.checked_sub(first).unwrap_or(-1);
        let width = if at >= 0 {
            widths.get(at as usize).copied().unwrap_or(missing)
        } else {
            missing
        };
        out.push(width);
    }
    Ok(out)
}

/// Whether one object is a font dictionary: unknown numbers raise.
pub fn xref_is_font_in_bytes(bytes: &[u8], xref: i32) -> Result<bool> {
    let doc = lopdf_open(bytes)?;
    let obj = lopdf_object(&doc, xref)?;
    Ok(obj.as_dict().is_ok_and(dict_is_font))
}

/// Probe one page's font by name: name and object number, or nothing.
pub fn check_font_in_bytes(bytes: &[u8], page: usize, fontname: &str) -> Result<Value> {
    let doc = lopdf_open(bytes)?;
    for font in resource_fonts(&doc, page)? {
        if font.name.eq_ignore_ascii_case(fontname) {
            return Ok(Value::List(vec![
                Value::Str(font.name),
                Value::Int(i64::from(font.xref)),
            ]));
        }
    }
    Ok(Value::Null)
}

/// Probe one object's font identity: base name and subtype for a font
/// carrying a descriptor, or nothing.
///
/// A bare base-14 name has no descriptor to report: the recording pins
/// null for one (l1-module-checkfontinfo over the fixture's Helvetica).
pub fn check_font_info_in_bytes(bytes: &[u8], xref: i32) -> Result<Value> {
    let doc = lopdf_open(bytes)?;
    let Ok(obj) = lopdf_object(&doc, xref) else {
        return Ok(Value::Null);
    };
    let Some(dict) = obj.as_dict().ok().filter(|d| dict_is_font(d)) else {
        return Ok(Value::Null);
    };
    if dict.get(b"FontDescriptor").is_err() {
        return Ok(Value::Null);
    }
    let name = font_base_name(dict).unwrap_or_default();
    let subtype = dict
        .get(b"Subtype")
        .ok()
        .and_then(|o| o.as_name().ok())
        .map(|n| String::from_utf8_lossy(n).into_owned())
        .unwrap_or_default();
    Ok(Value::List(vec![Value::Str(name), Value::Str(subtype)]))
}

/// A font's identity without a descriptor: base name and subtype straight
/// from the dictionary.
///
/// The probe pins null for a bare base-14 face (no descriptor to report),
/// but repair and info-store still need its identity, which the dictionary
/// carries. Nothing for a missing object or a non-font.
fn font_identity_in_bytes(bytes: &[u8], xref: i32) -> Result<Option<(String, String)>> {
    let doc = lopdf_open(bytes)?;
    let Ok(obj) = lopdf_object(&doc, xref) else {
        return Ok(None);
    };
    let Some(dict) = obj.as_dict().ok().filter(|d| dict_is_font(d)) else {
        return Ok(None);
    };
    let subtype = dict
        .get(b"Subtype")
        .ok()
        .and_then(|o| o.as_name().ok())
        .map(|n| String::from_utf8_lossy(n).into_owned())
        .unwrap_or_default();
    Ok(Some((font_base_name(dict).unwrap_or_default(), subtype)))
}

/// Open bytes for low-level reads.
fn lopdf_open(bytes: &[u8]) -> Result<lopdf::Document> {
    lopdf::Document::load_mem(bytes)
        .map_err(|e| ErrorKind::UnreadableFile.to_error(format!("cannot parse document: {e}")))
}

/// Follow reference chains a bounded depth.
fn lopdf_resolve<'a>(doc: &'a lopdf::Document, obj: &'a lopdf::Object) -> &'a lopdf::Object {
    let mut current = obj;
    for _ in 0..8 {
        match current {
            lopdf::Object::Reference(id) => match doc.get_object(*id) {
                Ok(next) => current = next,
                Err(_) => break,
            },
            _ => break,
        }
    }
    current
}

/// Fetch one numbered object.
fn lopdf_object(doc: &lopdf::Document, xref: i32) -> Result<&lopdf::Object> {
    if xref <= 0 {
        return Err(ErrorKind::BadValue.to_error(format!("bad object number {xref}")));
    }
    let id = doc
        .objects
        .keys()
        .filter(|id| id.0 == xref as u32)
        .min()
        .copied();
    match id.and_then(|id| doc.get_object(id).ok()) {
        Some(obj) => Ok(obj),
        None => Err(ErrorKind::BadValue.to_error(format!("bad object number {xref}"))),
    }
}

/// Whether a dictionary reads as a font.
fn dict_is_font(dict: &lopdf::Dictionary) -> bool {
    if let Ok(obj) = dict.get(b"Subtype") {
        if let Ok(name) = obj.as_name() {
            return matches!(
                name,
                b"Type0"
                    | b"Type1"
                    | b"Type1C"
                    | b"MMType1"
                    | b"Type3"
                    | b"TrueType"
                    | b"CIDFontType0"
                    | b"CIDFontType2"
            );
        }
    }
    dict.get(b"Type")
        .ok()
        .and_then(|o| o.as_name().ok())
        .is_some_and(|name| name == b"Font")
}

/// A font dictionary's base name without any subset prefix.
fn font_base_name(dict: &lopdf::Dictionary) -> Option<String> {
    let raw = dict
        .get(b"BaseFont")
        .ok()
        .and_then(|o| o.as_name().ok().or_else(|| o.as_str().ok()))?;
    let mut name = String::from_utf8_lossy(raw).into_owned();
    let bytes = name.as_bytes();
    if bytes.len() > 7 && bytes[6] == b'+' && bytes[..6].iter().all(|b| b.is_ascii_uppercase()) {
        name = name[7..].to_string();
    }
    Some(name)
}

/// Fonts named by one page's resources.
fn resource_fonts(doc: &lopdf::Document, page: usize) -> Result<Vec<FontInfo>> {
    let pages = doc.get_pages();
    let key = pages.keys().nth(page).copied().ok_or_else(|| {
        ErrorKind::BadValue.to_error(format!("page {page} out of range ({} pages)", pages.len()))
    })?;
    let id = pages[&key];
    let obj = doc
        .get_object(id)
        .map_err(|e| ErrorKind::UnreadableFile.to_error(format!("cannot read page {page}: {e}")))?;
    let fonts = obj
        .as_dict()
        .ok()
        .and_then(|d| d.get(b"Resources").ok())
        .map(|o| lopdf_resolve(doc, o))
        .and_then(|o| o.as_dict().ok())
        .and_then(|d| d.get(b"Font").ok())
        .map(|o| lopdf_resolve(doc, o))
        .and_then(|o| o.as_dict().ok());
    let mut out = Vec::new();
    if let Some(dict) = fonts {
        collect_fonts(doc, dict, &mut out);
    }
    Ok(out)
}

/// Collect one font dictionary's entries.
fn collect_fonts(doc: &lopdf::Document, dict: &lopdf::Dictionary, out: &mut Vec<FontInfo>) {
    for (key, obj) in dict.iter() {
        let xref = match obj {
            lopdf::Object::Reference(id) => id.0 as i32,
            _ => 0,
        };
        let resolved = lopdf_resolve(doc, obj);
        if let Ok(font) = resolved.as_dict() {
            if dict_is_font(font) {
                out.push(FontInfo {
                    xref,
                    name: font_base_name(font).unwrap_or_default(),
                    res_name: String::from_utf8_lossy(key).into_owned(),
                    subtype: font_subtype(font),
                    encoding: font_encoding(font),
                    ext: font_ext(doc, font),
                });
            }
        }
    }
}

/// A font dictionary's subtype name, empty when unset.
fn font_subtype(dict: &lopdf::Dictionary) -> String {
    dict.get(b"Subtype")
        .ok()
        .and_then(|o| o.as_name().ok())
        .map(|name| String::from_utf8_lossy(name).into_owned())
        .unwrap_or_default()
}

/// A font dictionary's encoding name, empty when unset.
fn font_encoding(dict: &lopdf::Dictionary) -> String {
    let Ok(obj) = dict.get(b"Encoding") else {
        return String::new();
    };
    if let Ok(name) = obj.as_name() {
        return String::from_utf8_lossy(name).into_owned();
    }
    obj.as_dict()
        .ok()
        .and_then(|d| d.get(b"BaseEncoding").ok())
        .and_then(|o| o.as_name().ok())
        .map(|name| String::from_utf8_lossy(name).into_owned())
        .unwrap_or_default()
}

/// A font dictionary's program extension, `n/a` when nothing is embedded.
fn font_ext(doc: &lopdf::Document, dict: &lopdf::Dictionary) -> String {
    let descriptor = dict
        .get(b"FontDescriptor")
        .ok()
        .map(|o| lopdf_resolve(doc, o))
        .and_then(|o| o.as_dict().ok());
    let Some(descriptor) = descriptor else {
        return "n/a".to_string();
    };
    if let Some(stream) = descriptor
        .get(b"FontFile3")
        .ok()
        .map(|o| lopdf_resolve(doc, o))
        .and_then(|o| o.as_stream().ok())
    {
        let subtype = stream
            .dict
            .get(b"Subtype")
            .ok()
            .and_then(|o| o.as_name().ok())
            .unwrap_or(b"");
        return match subtype {
            b"OpenType" => "otf".to_string(),
            b"TrueType" => "ttf".to_string(),
            _ => "cff".to_string(),
        };
    }
    if descriptor.get(b"FontFile2").is_ok() {
        return "ttf".to_string();
    }
    if descriptor.get(b"FontFile").is_ok() {
        return "pfb".to_string();
    }
    "n/a".to_string()
}

/// Bytes of a font program stream: decoded, or raw when undecodable.
fn stream_bytes(stream: &lopdf::Stream) -> Vec<u8> {
    stream
        .decompressed_content()
        .unwrap_or_else(|_| stream.content.clone())
}

/// A lopdf number as a float.
fn lopdf_number(obj: &lopdf::Object) -> Option<f64> {
    match obj {
        lopdf::Object::Integer(v) => Some(*v as f64),
        lopdf::Object::Real(v) => Some(f64::from(*v)),
        _ => None,
    }
}

/// Flatten a widths array, descending into range subarrays.
fn flatten_numbers(objects: &[lopdf::Object]) -> Vec<f64> {
    let mut out = Vec::new();
    for obj in objects {
        match obj {
            lopdf::Object::Array(inner) => out.extend(flatten_numbers(inner)),
            _ => {
                if let Some(n) = lopdf_number(obj) {
                    out.push(n);
                }
            }
        }
    }
    out
}

/// A dynamic value as a float.
fn value_number(value: &Value) -> Option<f64> {
    match value {
        Value::Int(v) => Some(*v as f64),
        Value::Float(v) => Some(*v),
        _ => None,
    }
}

// ===== Fonts end marker L1D =====

// ===== Lane L1: glyph names, width estimates, quads =====

/// Latin glyph names and their code points: the ASCII letters, digits,
/// and punctuation of the base encodings, plus the common ligatures.
const LATIN_GLYPH_NAMES: [(&str, char); 135] = [
    ("A", 'A'),
    ("AE", 'Æ'),
    ("B", 'B'),
    ("C", 'C'),
    ("D", 'D'),
    ("E", 'E'),
    ("F", 'F'),
    ("G", 'G'),
    ("H", 'H'),
    ("I", 'I'),
    ("J", 'J'),
    ("K", 'K'),
    ("L", 'L'),
    ("M", 'M'),
    ("N", 'N'),
    ("O", 'O'),
    ("OE", 'Œ'),
    ("P", 'P'),
    ("Q", 'Q'),
    ("R", 'R'),
    ("S", 'S'),
    ("T", 'T'),
    ("U", 'U'),
    ("V", 'V'),
    ("W", 'W'),
    ("X", 'X'),
    ("Y", 'Y'),
    ("Z", 'Z'),
    ("a", 'a'),
    ("ae", 'æ'),
    ("ampersand", '&'),
    ("asciicircum", '^'),
    ("asciitilde", '~'),
    ("asterisk", '*'),
    ("at", '@'),
    ("b", 'b'),
    ("backslash", '\\'),
    ("bar", '|'),
    ("braceleft", '{'),
    ("braceright", '}'),
    ("bracketleft", '['),
    ("bracketright", ']'),
    ("bullet", '•'),
    ("c", 'c'),
    ("colon", ':'),
    ("comma", ','),
    ("copyright", '©'),
    ("d", 'd'),
    ("dagger", '†'),
    ("daggerdbl", '‡'),
    ("dollar", '$'),
    ("dotlessi", 'ı'),
    ("dotlessj", 'ȷ'),
    ("e", 'e'),
    ("eight", '8'),
    ("ellipsis", '…'),
    ("emdash", '—'),
    ("endash", '–'),
    ("equal", '='),
    ("exclam", '!'),
    ("f", 'f'),
    ("ff", 'ff'),
    ("ffi", 'ffi'),
    ("ffl", 'ffl'),
    ("fi", 'fi'),
    ("five", '5'),
    ("fl", 'fl'),
    ("four", '4'),
    ("fraction", '⁄'),
    ("g", 'g'),
    ("germandbls", 'ß'),
    ("grave", '`'),
    ("greater", '>'),
    ("guillemotleft", '«'),
    ("guillemotright", '»'),
    ("guilsinglleft", '‹'),
    ("guilsinglright", '›'),
    ("h", 'h'),
    ("hyphen", '\u{2010}'),
    ("hyphenminus", '-'),
    ("i", 'i'),
    ("j", 'j'),
    ("k", 'k'),
    ("l", 'l'),
    ("less", '<'),
    ("m", 'm'),
    ("minus", '−'),
    ("multiply", '×'),
    ("n", 'n'),
    ("nine", '9'),
    ("numbersign", '#'),
    ("o", 'o'),
    ("oe", 'œ'),
    ("one", '1'),
    ("p", 'p'),
    ("paragraph", ''),
    ("parenleft", '('),
    ("parenright", ')'),
    ("percent", '%'),
    ("period", '.'),
    ("perthousand", '‰'),
    ("plus", '+'),
    ("q", 'q'),
    ("question", '?'),
    ("quotedbl", '"'),
    ("quotedblbase", '„'),
    ("quotedblleft", '"'),
    ("quotedblright", '"'),
    ("quoteleft", '\u{2018}'),
    ("quoteright", '\u{2019}'),
    ("quotesinglbase", '‚'),
    ("quotesingle", '\''),
    ("r", 'r'),
    ("registered", '®'),
    ("s", 's'),
    ("section", '§'),
    ("semicolon", ';'),
    ("seven", '7'),
    ("six", '6'),
    ("slash", '/'),
    ("space", ' '),
    ("sterling", '£'),
    ("t", 't'),
    ("three", '3'),
    ("trademark", '™'),
    ("two", '2'),
    ("u", 'u'),
    ("underscore", '_'),
    ("v", 'v'),
    ("w", 'w'),
    ("x", 'x'),
    ("y", 'y'),
    ("yen", '¥'),
    ("zero", '0'),
    ("z", 'z'),
];

/// A glyph name's code point: the Latin table, then the symbol fonts.
///
/// Unknown names answer -1: no mapping is known, and zero would read as
/// a NUL mapping rather than as missing.
pub fn glyph_name_to_code(name: &str) -> i32 {
    if let Some((_, ch)) = LATIN_GLYPH_NAMES.iter().find(|(n, _)| *n == name) {
        return *ch as i32;
    }
    if let Some(ch) = symbol_glyph_name_to_char(name) {
        return ch as i32;
    }
    -1
}

/// A code point's glyph name: the Latin table inverted, then a small set
/// of certain symbol names, else the `.notdef` fallback.
pub fn code_to_glyph_name(code: i32) -> String {
    let ch = char::from_u32(code as u32).unwrap_or('\0');
    if let Some((name, _)) = LATIN_GLYPH_NAMES.iter().find(|(_, c)| *c == ch) {
        return (*name).to_string();
    }
    const SYMBOLS: [(char, &str); 24] = [
        ('Α', "Alpha"),
        ('Β', "Beta"),
        ('Δ', "Delta"),
        ('Ω', "Omega"),
        ('α', "alpha"),
        ('β', "beta"),
        ('δ', "delta"),
        ('π', "pi"),
        ('ω', "omega"),
        ('←', "arrowleft"),
        ('↑', "arrowup"),
        ('→', "arrowright"),
        ('↓', "arrowdown"),
        ('∞', "infinity"),
        ('≠', "notequal"),
        ('≤', "lessequal"),
        ('≥', "greaterequal"),
        ('∂', "partialdiff"),
        ('∑', "summation"),
        ('∏', "product"),
        ('√', "radical"),
        ('∫', "integral"),
        ('°', "degree"),
        ('±', "plusminus"),
    ];
    if let Some((_, name)) = SYMBOLS.iter().find(|(c, _)| *c == ch) {
        return (*name).to_string();
    }
    ".notdef".to_string()
}

/// Whether a font name reads as a monospace face.
fn is_monospace_name(fontname: &str) -> bool {
    if let Some(face) = StandardFont::from_base_font(fontname) {
        return matches!(
            face,
            StandardFont::Courier
                | StandardFont::CourierBold
                | StandardFont::CourierOblique
                | StandardFont::CourierBoldOblique
        );
    }
    fontname.to_ascii_lowercase().contains("courier")
        || fontname.to_ascii_lowercase().contains("mono")
}

/// An estimate of one string's set width: monospace faces advance a
/// fixed em fraction, proportional faces sum integer thousandths of an em.
///
/// The recorded module pins fix several characters bit for bit; the rest
/// keep the tiered estimate.
pub fn estimate_text_length(text: &str, fontname: &str, fontsize: f64) -> f64 {
    if fontsize <= 0.0 || text.is_empty() {
        return 0.0;
    }
    if is_monospace_name(fontname) {
        return 0.6 * fontsize * text.chars().count() as f64;
    }
    let units: i32 = text.chars().map(proportional_width_units).sum();
    f64::from(units) * fontsize / 1000.0
}

/// One character's width in thousandths of an em on a proportional face.
///
/// The first arms are the recorded module pins; the remaining tiers are
/// estimates for characters no recording covers.
fn proportional_width_units(ch: char) -> i32 {
    match ch {
        ' ' => 278,
        'h' | 'e' | 'o' => 556,
        'l' => 222,
        'M' => 833,
        'r' => 333,
        '.' => 334,
        'i' | '!' | '|' | '\'' | ',' | ':' | ';' | 'I' => 320,
        'm' | 'w' | 'W' => 850,
        '0'..='9' => 550,
        c if c.is_ascii_uppercase() => 680,
        c if (c as u32) < 0x7f => 550,
        _ => 600,
    }
}

/// Resolve the page an option struct points at: an embedded text page
/// replaces the view's own page, as a cached extraction would.
fn resolve_source(
    doc: &PdfDocument,
    page: usize,
    textpage: Option<TextPage<'_>>,
) -> Result<(EngineDoc, usize)> {
    match textpage {
        Some(tp) => Ok((engine_for(tp.doc)?, tp.page)),
        None => Ok((engine_for(doc)?, page)),
    }
}

/// Resolve a numbered page, rejecting negatives before the engine.
fn resolve_source_numbered(
    doc: &PdfDocument,
    page: i32,
    textpage: Option<TextPage<'_>>,
) -> Result<(EngineDoc, usize)> {
    match textpage {
        Some(tp) => Ok((engine_for(tp.doc)?, tp.page)),
        None => {
            let engine = engine_for(doc)?;
            let page = checked_page_number(&engine, page)?;
            Ok((engine, page))
        }
    }
}

/// Validate a signed page number against the page count.
fn checked_page_number(engine: &EngineDoc, page: i32) -> Result<usize> {
    if page < 0 {
        return Err(ErrorKind::IndexOutOfRange.to_error(format!("page {page} out of range")));
    }
    checked_page(engine, page as usize)?;
    Ok(page as usize)
}

/// Resolve the effective clip and flags of an option struct over an
/// optional text page: both clips filter, an explicit flags word wins.
fn resolve_clip_flags(
    clip: Option<Rect>,
    flags: Option<i32>,
    textpage: Option<TextPage<'_>>,
) -> (Option<Rect>, i32) {
    match textpage {
        Some(tp) => (intersect_clips(tp.clip, clip), flags.unwrap_or(tp.flags)),
        None => (clip, flags.unwrap_or(0)),
    }
}

/// Fold a text page's matrix into a resolved clip over a capture: without
/// a text page the clip stands as resolved.
fn window_for(
    capture: &CapturedPage,
    clip: Option<Rect>,
    textpage: Option<TextPage<'_>>,
) -> Option<Rect> {
    match textpage {
        Some(tp) => textpage_window(capture.width, capture.height, clip, tp.matrix),
        None => clip,
    }
}

// ===== Glyph end marker L1E =====

// ===== Lane L1: format dispatch =====

/// Render one capture in the requested format.
fn render_format(
    capture: &CapturedPage,
    format: TextFormat,
    page: usize,
    clip: Option<Rect>,
    flags: i32,
    sort: bool,
) -> TextOutput {
    match format {
        TextFormat::Text => TextOutput::Text(capture.render_text(clip, flags, sort)),
        TextFormat::Words => TextOutput::Words(capture.render_words(clip, flags, sort, None)),
        TextFormat::Blocks => TextOutput::Blocks(capture.render_blocks(clip, flags, sort)),
        TextFormat::Dict => TextOutput::Dict(capture.render_dict(clip, flags, sort, false)),
        TextFormat::RawDict => TextOutput::Dict(capture.render_dict(clip, flags, sort, true)),
        TextFormat::Json => TextOutput::Json(capture.render_json(clip, flags, sort, false)),
        TextFormat::RawJson => TextOutput::RawJson(capture.render_json(clip, flags, sort, true)),
        TextFormat::Html => TextOutput::Html(capture.render_html(page, clip, flags, sort)),
        TextFormat::Xhtml => TextOutput::Xhtml(capture.render_xhtml(page, clip, flags, sort)),
        TextFormat::Xml => TextOutput::Xml(capture.render_xml(page, clip, flags, sort)),
    }
}

/// Whether a format shows figures (only the dict family does).
fn format_needs_figures(format: TextFormat) -> bool {
    matches!(format, TextFormat::Dict | TextFormat::RawDict)
}

// ===== Dispatch end marker L1F =====

#[cfg(test)]
mod tests {
    use super::*;

    /// The base-14 metric table carries the recorded values bit for bit.
    #[test]
    fn base14_table_matches_recordings() {
        assert_eq!(
            base14_metrics("Helvetica"),
            Some((1.0750000476837158, -0.29899999499320984))
        );
        assert_eq!(base14_metrics("Helvetica-Bold"), Some((1.07, -0.307)));
        assert_eq!(base14_metrics("Times-Roman"), None);
    }

    #[test]
    fn compat_font_name_is_cut_to_24_characters() {
        assert_eq!(
            truncate_font_name("ITCFranklinGothicStd-Demi"),
            "ITCFranklinGothicStd-Dem"
        );
        assert_eq!(truncate_font_name("Helvetica"), "Helvetica");
        assert_eq!(truncate_font_name(&"a".repeat(24)), "a".repeat(24));
        // The cut counts characters, not bytes: a multibyte name neither
        // panics nor splits a character.
        assert_eq!(truncate_font_name(&"é".repeat(30)), "é".repeat(24));
    }
}