pdfni 0.2.0

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

use serde::Serialize;

use crate::columns::{Participant, ParticipantKind, split_regions};
use crate::error::Result;
use crate::extract::ExtractOptions;
use crate::furniture;
use crate::model::{BBox, Cell, Table};
use crate::extract::extract_from_parts;
use crate::query::ExtractQuery;
use crate::reader::read_pages_and_text;
use crate::text::{
    TextChar, TextFont, TextLine, TextPage, TextWord, build_text_lines_with, derotate_xy,
    is_whitespace_str,
};

/// 段落併合の行送り上限(代表 font_size 比)
const PARA_LEADING_FACTOR: f64 = 1.8;
/// 段落併合の代表 font_size 比の上限(大きい方 / 小さい方)
const PARA_FS_RATIO_MAX: f64 = 1.33;
/// 見出し level 1 の本文代表サイズ比
const HEADING_L1_FACTOR: f64 = 1.8;
/// 見出し level 2 の本文代表サイズ比
const HEADING_L2_FACTOR: f64 = 1.4;
/// 見出し level 3 の本文代表サイズ比
const HEADING_L3_FACTOR: f64 = 1.2;
/// 本文代表サイズの丸め単位(pt)
const BODY_FS_BIN: f64 = 0.1;
/// リスト字下げ許容の font_size 比
const LIST_INDENT_FACTOR: f64 = 0.3;
/// リスト字下げ許容の下限 pt
const LIST_INDENT_MIN_PT: f64 = 2.0;

/// 文書全体
#[derive(Debug, Clone, Serialize)]
pub struct DocDoc {
    pub pages: Vec<DocPage>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub warnings: Vec<String>,
}

/// 1ページ分の文書モデル
#[derive(Debug, Clone, Serialize)]
pub struct DocPage {
    /// pages 配列内の 0 始まり位置
    pub index: usize,
    pub width: f64,
    pub height: f64,
    pub fonts: Vec<TextFont>,
    pub chars: Vec<TextChar>,
    pub blocks: Vec<DocBlock>,
}

/// ページ内の1ブロック
#[derive(Debug, Clone, Serialize)]
#[serde(tag = "type")]
pub enum DocBlock {
    #[serde(rename = "table")]
    Table(Table),
    #[serde(rename = "text")]
    Text(TextBlock),
}

/// 本文ブロック
#[derive(Debug, Clone, Serialize)]
pub struct TextBlock {
    #[serde(flatten)]
    pub kind: TextBlockKind,
    #[serde(default, skip_serializing_if = "TextBlockRole::is_body")]
    pub role: TextBlockRole,
    pub text: String,
    pub left: f64,
    pub right: f64,
    pub top: f64,
    pub bottom: f64,
    pub dir: String,
    pub rot: i32,
    pub lines: Vec<TextLine>,
}

/// 本文ブロックの種別
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
#[serde(tag = "kind")]
pub enum TextBlockKind {
    #[serde(rename = "paragraph")]
    Paragraph,
    #[serde(rename = "heading")]
    Heading { level: u8 },
    #[serde(rename = "list")]
    List { ordered: bool },
}

/// 本文ブロックの役割(ヘッダ・フッタ検出)
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum TextBlockRole {
    #[default]
    Body,
    Header,
    Footer,
}

impl TextBlockRole {
    fn is_body(&self) -> bool {
        *self == TextBlockRole::Body
    }
}

/// 文字・フォント情報を除いた文書内容ビュー
#[derive(Debug, Clone, Serialize)]
pub struct DocContent {
    pub pages: Vec<ContentPage>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub warnings: Vec<String>,
}

/// 1ページ分の内容ビュー
#[derive(Debug, Clone, Serialize)]
pub struct ContentPage {
    /// pages 配列内の 0 始まり位置
    pub index: usize,
    pub width: f64,
    pub height: f64,
    pub blocks: Vec<ContentBlock>,
}

/// 内容ビューの1ブロック
#[derive(Debug, Clone, Serialize)]
#[serde(tag = "type")]
pub enum ContentBlock {
    #[serde(rename = "table")]
    Table(Table),
    #[serde(rename = "text")]
    Text(ContentText),
}

/// 内容ビューの本文ブロック
#[derive(Debug, Clone, Serialize)]
pub struct ContentText {
    #[serde(flatten)]
    pub kind: TextBlockKind,
    #[serde(default, skip_serializing_if = "TextBlockRole::is_body")]
    pub role: TextBlockRole,
    pub text: String,
    pub left: f64,
    pub right: f64,
    pub top: f64,
    pub bottom: f64,
    pub lines: Vec<ContentLine>,
}

/// 内容ビューの1行
#[derive(Debug, Clone, Serialize)]
pub struct ContentLine {
    pub text: String,
    pub left: f64,
    pub right: f64,
    pub top: f64,
    pub bottom: f64,
}

impl DocDoc {
    pub fn into_content(self) -> DocContent {
        let pages = self
            .pages
            .into_iter()
            .map(|page| ContentPage {
                index: page.index,
                width: page.width,
                height: page.height,
                blocks: page.blocks.into_iter().map(block_to_content).collect(),
            })
            .collect();
        DocContent {
            pages,
            warnings: self.warnings,
        }
    }
}

fn block_to_content(block: DocBlock) -> ContentBlock {
    match block {
        DocBlock::Table(table) => ContentBlock::Table(table),
        DocBlock::Text(tb) => {
            let lines = tb
                .lines
                .iter()
                .map(|line| ContentLine {
                    text: join_line_words(line),
                    left: line.left,
                    right: line.right,
                    top: line.top,
                    bottom: line.bottom,
                })
                .collect();
            ContentBlock::Text(ContentText {
                kind: tb.kind,
                role: tb.role,
                text: tb.text,
                left: tb.left,
                right: tb.right,
                top: tb.top,
                bottom: tb.bottom,
                lines,
            })
        }
    }
}

/// 文書単位のヘッダ・フッタ役割付与
pub fn assign_header_footer(doc: &mut DocDoc) {
    furniture::assign_header_footer(doc);
}

/// 文書単位のリスト検出
pub fn assign_lists(doc: &mut DocDoc) {
    for page in &mut doc.pages {
        assign_lists_page(page);
    }
}

/// 文書単位の見出し推定
pub fn assign_headings(doc: &mut DocDoc) {
    let Some(body_fs) = body_rep_font_size(doc) else {
        return;
    };

    for page in &mut doc.pages {
        for block in &mut page.blocks {
            let DocBlock::Text(tb) = block else {
                continue;
            };
            if tb.role != TextBlockRole::Body {
                continue;
            }
            if tb.kind != TextBlockKind::Paragraph {
                continue;
            }
            if tb.lines.len() > 2 {
                continue;
            }
            let Some(block_fs) = block_rep_font_size(&page.chars, tb) else {
                continue;
            };
            let level = if block_fs >= body_fs * HEADING_L1_FACTOR {
                1
            } else if block_fs >= body_fs * HEADING_L2_FACTOR {
                2
            } else if block_fs >= body_fs * HEADING_L3_FACTOR {
                3
            } else {
                continue;
            };
            tb.kind = TextBlockKind::Heading { level };
        }
    }
}

pub(crate) fn document_to_markdown(doc: &DocDoc, escape_markdown: bool) -> String {
    let mut parts = Vec::new();
    for page in &doc.pages {
        for block in &page.blocks {
            if let Some(s) = block_to_markdown(block, page, escape_markdown) {
                parts.push(s);
            }
        }
    }
    parts.join("\n\n")
}

/// 空表・空白のみ本文は None
fn block_to_markdown(block: &DocBlock, page: &DocPage, escape_markdown: bool) -> Option<String> {
    match block {
        DocBlock::Text(tb) => {
            if tb.role != TextBlockRole::Body {
                return None;
            }
            match &tb.kind {
                TextBlockKind::Heading { level } => {
                    let prefix = "#".repeat(*level as usize);
                    let out: Vec<String> = format_text_block_lines(tb, page, escape_markdown)
                        .into_iter()
                        .filter(|l| !l.trim().is_empty())
                        .map(|l| format!("{prefix} {l}"))
                        .collect();
                    if out.is_empty() {
                        return None;
                    }
                    Some(out.join("\n"))
                }
                TextBlockKind::Paragraph => {
                    let body = format_text_block_lines(tb, page, escape_markdown)
                        .into_iter()
                        .filter(|l| !l.trim().is_empty())
                        .collect::<Vec<_>>()
                        .join(HARD_BREAK);
                    if body.trim().is_empty() {
                        return None;
                    }
                    Some(body)
                }
                TextBlockKind::List { ordered } => {
                    list_to_markdown(tb, page, *ordered, escape_markdown)
                }
            }
        }
        DocBlock::Table(table) => table_to_markdown(table),
    }
}

/// 段落内の行を縦に保つハード改行
const HARD_BREAK: &str = "  \n";

/// 行ごとの Markdown 化。書体があればインライン強調
/// 行情報が使えない場合は結合済み text の1要素
fn format_text_block_lines(tb: &TextBlock, page: &DocPage, escape_markdown: bool) -> Vec<String> {
    if join_paragraph_text(&tb.lines) == tb.text {
        if let Some(lines) = styled_md_lines(&tb.lines, &page.fonts, &page.chars, escape_markdown)
        {
            return lines;
        }
        let plain = plain_md_lines(&tb.lines, escape_markdown);
        if !plain.is_empty() {
            return plain;
        }
    }
    vec![format_md_body(&tb.text, escape_markdown)]
}

/// 見出し・段落・リスト項目本文のエスケープ適用
fn format_md_body(text: &str, escape_markdown: bool) -> String {
    if escape_markdown {
        escape_text_block_text(text)
    } else {
        sanitize_md_text(text)
    }
}

fn list_to_markdown(
    tb: &TextBlock,
    page: &DocPage,
    ordered: bool,
    escape_markdown: bool,
) -> Option<String> {
    let mut out_lines = Vec::new();
    for (num, body_lines) in list_item_body_groups(&tb.lines) {
        let rendered = styled_md_lines(&body_lines, &page.fonts, &page.chars, escape_markdown)
            .unwrap_or_else(|| plain_md_lines(&body_lines, escape_markdown));
        let body = rendered.join(HARD_BREAK);
        if body.trim().is_empty() {
            continue;
        }
        if ordered {
            let Some(n) = num else {
                continue;
            };
            out_lines.push(format!("{n}. {body}"));
        } else {
            out_lines.push(format!("- {body}"));
        }
    }
    if out_lines.is_empty() {
        None
    } else {
        Some(out_lines.join("\n"))
    }
}

/// 書体属性
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct MdStyle {
    bold: bool,
    italic: bool,
}

impl MdStyle {
    fn normal() -> Self {
        Self {
            bold: false,
            italic: false,
        }
    }

    fn is_normal(self) -> bool {
        !self.bold && !self.italic
    }
}

/// 行内の本文断片
enum MdUnit {
    /// グリフ由来の文字列
    Text { text: String, style: MdStyle },
    /// 単語間に挿入する空白
    Gap(String),
}

/// 行ごとの書体付き Markdown。強調は行内で完結
fn styled_md_lines(
    lines: &[TextLine],
    fonts: &[TextFont],
    chars: &[TextChar],
    escape_markdown: bool,
) -> Option<Vec<String>> {
    if !lines_have_char_refs(lines) {
        return None;
    }
    let mut out = Vec::new();
    for line in lines {
        let units = line_to_md_units(line, fonts, chars)?;
        let runs = coalesce_md_units(&units);
        let rendered = render_md_runs(&runs, escape_markdown);
        if !rendered.trim().is_empty() {
            out.push(rendered);
        }
    }
    Some(out)
}

fn plain_md_lines(lines: &[TextLine], escape_markdown: bool) -> Vec<String> {
    let mut out = Vec::new();
    for line in lines {
        let text = join_line_words(line);
        if text.trim().is_empty() {
            continue;
        }
        out.push(format_md_body(&text, escape_markdown));
    }
    out
}

/// 装飾に使える文字参照があるか
fn lines_have_char_refs(lines: &[TextLine]) -> bool {
    lines
        .iter()
        .any(|l| l.words.iter().any(|w| !w.chars.is_empty()))
}

fn line_to_md_units(
    line: &TextLine,
    fonts: &[TextFont],
    chars: &[TextChar],
) -> Option<Vec<MdUnit>> {
    let mut units = Vec::new();
    for (i, word) in line.words.iter().enumerate() {
        if i > 0 {
            units.push(MdUnit::Gap(" ".into()));
        }
        units.extend(word_to_md_units(word, fonts, chars)?);
    }
    Some(units)
}

fn word_to_md_units(
    word: &TextWord,
    fonts: &[TextFont],
    chars: &[TextChar],
) -> Option<Vec<MdUnit>> {
    if word.chars.is_empty() {
        if word.text.is_empty() {
            return Some(Vec::new());
        }
        // 文字参照が無く本文だけある単語は復元不能
        return None;
    }
    let mut units = Vec::with_capacity(word.chars.len());
    let mut rebuilt = String::new();
    for &ci in &word.chars {
        let Some(ch) = chars.get(ci as usize) else {
            // 不正な文字インデックス
            return None;
        };
        let style = md_style_of_font(fonts, ch.font);
        rebuilt.push_str(&ch.text);
        units.push(MdUnit::Text {
            text: ch.text.clone(),
            style,
        });
    }
    if rebuilt != word.text {
        return None;
    }
    Some(units)
}

fn md_style_of_font(fonts: &[TextFont], font_idx: u32) -> MdStyle {
    match fonts.get(font_idx as usize) {
        Some(f) => MdStyle {
            bold: f.bold,
            italic: f.italic,
        },
        None => MdStyle::normal(),
    }
}

/// 同一書体の断片の結合。間の挿入空白は区間へ取り込み
fn coalesce_md_units(units: &[MdUnit]) -> Vec<(String, MdStyle)> {
    let mut runs: Vec<(String, MdStyle)> = Vec::new();
    let mut pending_gap = String::new();

    for unit in units {
        match unit {
            MdUnit::Gap(g) => pending_gap.push_str(g),
            MdUnit::Text { text, style } => {
                if text.is_empty() {
                    continue;
                }
                if let Some((last_text, last_style)) = runs.last_mut() {
                    if *last_style == *style {
                        last_text.push_str(&pending_gap);
                        last_text.push_str(text);
                        pending_gap.clear();
                        continue;
                    }
                }
                if !pending_gap.is_empty() {
                    if let Some((last_text, last_style)) = runs.last_mut() {
                        if last_style.is_normal() {
                            last_text.push_str(&pending_gap);
                        } else {
                            runs.push((std::mem::take(&mut pending_gap), MdStyle::normal()));
                        }
                    } else {
                        runs.push((std::mem::take(&mut pending_gap), MdStyle::normal()));
                    }
                    pending_gap.clear();
                }
                if let Some((last_text, last_style)) = runs.last_mut() {
                    if *last_style == *style {
                        last_text.push_str(text);
                        continue;
                    }
                }
                runs.push((text.clone(), *style));
            }
        }
    }
    if !pending_gap.is_empty() {
        if let Some((last_text, last_style)) = runs.last_mut() {
            if last_style.is_normal() {
                last_text.push_str(&pending_gap);
            } else {
                runs.push((pending_gap, MdStyle::normal()));
            }
        } else {
            runs.push((pending_gap, MdStyle::normal()));
        }
    }
    runs
}

fn render_md_runs(runs: &[(String, MdStyle)], escape_markdown: bool) -> String {
    let mut out = String::new();
    for (text, style) in runs {
        out.push_str(&render_md_run(text, *style, escape_markdown));
    }
    out
}

fn render_md_run(text: &str, style: MdStyle, escape_markdown: bool) -> String {
    if style.is_normal() {
        return format_md_body(text, escape_markdown);
    }
    // 区間端の空白は強調の外
    let lead = text.chars().take_while(|&c| c == ' ').count();
    let trail = text
        .chars()
        .rev()
        .take_while(|&c| c == ' ')
        .count()
        .min(text.chars().count().saturating_sub(lead));
    let core: String = text
        .chars()
        .skip(lead)
        .take(text.chars().count() - lead - trail)
        .collect();
    let mut out = String::with_capacity(text.len() + 6);
    for _ in 0..lead {
        out.push(' ');
    }
    if !core.is_empty() {
        let body = format_md_body(&core, escape_markdown);
        let marker = if style.bold && style.italic {
            "***"
        } else if style.bold {
            "**"
        } else {
            "*"
        };
        out.push_str(marker);
        out.push_str(&body);
        out.push_str(marker);
    }
    for _ in 0..trail {
        out.push(' ');
    }
    out
}

/// リスト項目の番号とマーカー除去後の本文行
fn list_item_body_groups(lines: &[TextLine]) -> Vec<(Option<u32>, Vec<TextLine>)> {
    let mut entries = Vec::new();
    let mut i = 0;
    while i < lines.len() {
        if list_marker_kind(&lines[i]).is_none() {
            i += 1;
            continue;
        }
        let num = match list_marker_kind(&lines[i]) {
            Some(ListMarkerKind::Ordered { n, .. }) => Some(n),
            _ => None,
        };
        let start = i;
        i += 1;
        while i < lines.len() && list_marker_kind(&lines[i]).is_none() {
            i += 1;
        }
        let body_lines = list_item_body_lines(&lines[start..i]);
        entries.push((num, body_lines));
    }
    entries
}

/// GFM パイプテーブルへの直列化
fn table_to_markdown(table: &Table) -> Option<String> {
    if table.n_rows == 0 || table.n_cols == 0 {
        return None;
    }
    let n_cols = table.n_cols;
    let mut lines = Vec::with_capacity(table.data.len().max(1) + 1);

    let header = table.data.first().map(|r| r.as_slice()).unwrap_or(&[]);
    lines.push(format_table_row(header, n_cols));

    let mut sep = String::from("|");
    for _ in 0..n_cols {
        sep.push_str(" --- |");
    }
    lines.push(sep);

    for row in table.data.iter().skip(1) {
        lines.push(format_table_row(row, n_cols));
    }

    Some(lines.join("\n"))
}

fn format_table_row(row: &[Cell], n_cols: usize) -> String {
    let mut s = String::from("|");
    for i in 0..n_cols {
        s.push(' ');
        if let Some(cell) = row.get(i) {
            s.push_str(&escape_cell_text(&cell.text));
        }
        s.push_str(" |");
    }
    s
}

/// セル text の制御文字置換と GFM エスケープ
fn escape_cell_text(text: &str) -> String {
    let sanitized = sanitize_md_text(text);
    let mut out = String::with_capacity(sanitized.len());
    for c in sanitized.chars() {
        match c {
            '\\' => out.push_str("\\\\"),
            '|' => out.push_str("\\|"),
            _ => out.push(c),
        }
    }
    out
}

/// 見出し・段落 text の制御文字置換と本文エスケープ
fn escape_text_block_text(text: &str) -> String {
    let sanitized = sanitize_md_text(text);
    let mut body = String::with_capacity(sanitized.len() + 1);
    for c in sanitized.chars() {
        if c == '\\' {
            body.push_str("\\\\");
        } else {
            body.push(c);
        }
    }
    // エスケープは先頭の連続 U+0020 直後のブロック構文文字1文字だけ
    let n_space = body.bytes().take_while(|&b| b == b' ').count();
    if let Some(first) = body[n_space..].chars().next() {
        if matches!(first, '#' | '*' | '-' | '+' | '>' | '|') {
            let mut out = String::with_capacity(body.len() + 1);
            out.push_str(&body[..n_space]);
            out.push('\\');
            out.push_str(&body[n_space..]);
            return out;
        }
    }
    body
}

/// 改行・タブ・C0 制御文字の U+0020 置換
pub(crate) fn sanitize_md_text(text: &str) -> String {
    text.chars()
        .map(|c| if is_md_control_char(c) { ' ' } else { c })
        .collect()
}

/// Markdown 直列化で空白へ置換する制御文字か
fn is_md_control_char(c: char) -> bool {
    matches!(c, '\u{0000}'..='\u{001F}' | '\u{2028}' | '\u{2029}')
}

pub fn extract_markdown_from_bytes(
    data: &[u8],
    password: Option<&str>,
    options: &ExtractOptions,
) -> Result<String> {
    extract_markdown_from_bytes_with_query(data, password, options, &ExtractQuery::default())
}

/// ページ・矩形で絞った Markdown 生成
pub fn extract_markdown_from_bytes_with_query(
    data: &[u8],
    password: Option<&str>,
    options: &ExtractOptions,
    query: &ExtractQuery,
) -> Result<String> {
    let doc = extract_document_from_bytes_with_query(data, password, options, query)?;
    Ok(document_to_markdown(&doc, options.escape_markdown))
}

pub fn extract_content_from_bytes(
    data: &[u8],
    password: Option<&str>,
    options: &ExtractOptions,
) -> Result<DocContent> {
    extract_content_from_bytes_with_query(data, password, options, &ExtractQuery::default())
}

/// ページ・矩形で絞った内容ビュー構築
pub fn extract_content_from_bytes_with_query(
    data: &[u8],
    password: Option<&str>,
    options: &ExtractOptions,
    query: &ExtractQuery,
) -> Result<DocContent> {
    Ok(extract_document_from_bytes_with_query(data, password, options, query)?.into_content())
}

/// 見出し推定済みの文書モデル
pub fn extract_document_from_bytes(
    data: &[u8],
    password: Option<&str>,
    options: &ExtractOptions,
) -> Result<DocDoc> {
    extract_document_from_bytes_with_query(data, password, options, &ExtractQuery::default())
}

/// ページ・矩形で絞った文書モデル構築
pub fn extract_document_from_bytes_with_query(
    data: &[u8],
    password: Option<&str>,
    options: &ExtractOptions,
    query: &ExtractQuery,
) -> Result<DocDoc> {
    let (parts, text_pages, warnings) =
        read_pages_and_text(data, password, &options.reader, query)?;
    let tables_doc = extract_from_parts(parts, options);

    let mut pages = Vec::with_capacity(text_pages.len());
    for (i, (page, text_page)) in tables_doc
        .pages
        .into_iter()
        .zip(text_pages.into_iter())
        .enumerate()
    {
        let blocks = build_blocks_owned(&text_page, page.tables, options.bidi);
        pages.push(DocPage {
            index: i,
            width: text_page.width,
            height: text_page.height,
            fonts: text_page.fonts,
            chars: text_page.chars,
            blocks,
        });
    }

    let mut doc = DocDoc { pages, warnings };
    if options.detect_header_footer {
        assign_header_footer(&mut doc);
    }
    if options.detect_lists {
        assign_lists(&mut doc);
    }
    assign_headings(&mut doc);
    Ok(doc)
}

/// 本文代表 font_size(0.1pt 丸め最頻値。同数タイは小さい方)
fn body_rep_font_size(doc: &DocDoc) -> Option<f64> {
    let mut counts: std::collections::HashMap<i64, usize> = std::collections::HashMap::new();
    for page in &doc.pages {
        for block in &page.blocks {
            let DocBlock::Text(tb) = block else {
                continue;
            };
            if tb.role != TextBlockRole::Body {
                continue;
            }
            for line in &tb.lines {
                for &idx in &line.chars {
                    let ch = &page.chars[idx as usize];
                    if is_whitespace_str(&ch.text) {
                        continue;
                    }
                    let fs = ch.font_size;
                    if fs.is_finite() && fs > 0.0 {
                        let key = (fs / BODY_FS_BIN).round() as i64;
                        *counts.entry(key).or_insert(0) += 1;
                    }
                }
            }
        }
    }
    if counts.is_empty() {
        return None;
    }
    let mut best_key = i64::MAX;
    let mut best_count = 0usize;
    for (&key, &count) in &counts {
        if count > best_count || (count == best_count && key < best_key) {
            best_count = count;
            best_key = key;
        }
    }
    Some(best_key as f64 * BODY_FS_BIN)
}

/// ブロック構成行の代表 font_size の中央値
pub(crate) fn block_rep_font_size(chars: &[TextChar], tb: &TextBlock) -> Option<f64> {
    let mut vals = Vec::with_capacity(tb.lines.len());
    for line in &tb.lines {
        let fs = line_rep_font_size(chars, line)?;
        vals.push(fs);
    }
    median_f64(&mut vals)
}

/// 読み順付きブロック列の構築
pub fn build_blocks(page: &TextPage, tables: &[Table]) -> Vec<DocBlock> {
    build_blocks_owned(page, tables.to_vec(), false)
}

/// 表を所有権ごと受け取る版
fn build_blocks_owned(page: &TextPage, tables: Vec<Table>, bidi: bool) -> Vec<DocBlock> {
    let valid_cells = collect_valid_cells(&tables);
    let raw_lines = build_text_lines_with(page, bidi);
    let body_lines: Vec<TextLine> = raw_lines
        .into_iter()
        .filter_map(|line| filter_table_owned_line(page, line, &valid_cells))
        .collect();

    let (parts, word_part_ids, table_part_ids, other_part_ids) =
        collect_participants(&body_lines, &tables);

    if let Some(leaf_of) = split_regions(page.width, page.height, &parts) {
        return build_blocks_with_regions(
            page,
            body_lines,
            tables,
            &leaf_of,
            &word_part_ids,
            &table_part_ids,
            &other_part_ids,
        );
    }

    let text_blocks = build_text_blocks(page, body_lines);

    let n_tables = tables.len();
    let mut keyed: Vec<(f64, f64, usize, DocBlock)> =
        Vec::with_capacity(n_tables + text_blocks.len());
    for (i, table) in tables.into_iter().enumerate() {
        let top = table.bbox.top;
        let left = table.bbox.x0;
        keyed.push((top, left, i, DocBlock::Table(table)));
    }
    let mut gen_no = n_tables;
    for tb in text_blocks {
        let top = tb.top;
        let left = tb.left;
        keyed.push((top, left, gen_no, DocBlock::Text(tb)));
        gen_no += 1;
    }

    keyed.sort_by(|a, b| {
        a.0.total_cmp(&b.0)
            .then_with(|| a.1.total_cmp(&b.1))
            .then_with(|| a.2.cmp(&b.2))
    });
    keyed.into_iter().map(|(_, _, _, b)| b).collect()
}

/// 対象本文行か(横書き・正立)
fn is_target_body_line(line: &TextLine) -> bool {
    line.dir == "ltr" && line.rot == 0
}

/// 参加要素列(安定ID順)
fn collect_participants(
    body_lines: &[TextLine],
    tables: &[Table],
) -> (
    Vec<Participant>,
    Vec<Vec<usize>>,
    Vec<usize>,
    Vec<Option<usize>>,
) {
    let mut parts = Vec::new();
    let mut word_part_ids: Vec<Vec<usize>> = vec![Vec::new(); body_lines.len()];
    let mut other_part_ids: Vec<Option<usize>> = vec![None; body_lines.len()];
    let mut table_part_ids = Vec::with_capacity(tables.len());

    for (li, line) in body_lines.iter().enumerate() {
        if !is_target_body_line(line) {
            continue;
        }
        for word in &line.words {
            let id = parts.len();
            word_part_ids[li].push(id);
            parts.push(Participant {
                left: word.left,
                right: word.right,
                top: word.top,
                bottom: word.bottom,
                kind: ParticipantKind::Word { line: li },
            });
        }
    }
    for table in tables {
        let id = parts.len();
        table_part_ids.push(id);
        parts.push(Participant {
            left: table.bbox.x0,
            right: table.bbox.x1,
            top: table.bbox.top,
            bottom: table.bbox.bottom,
            kind: ParticipantKind::Table,
        });
    }
    for (li, line) in body_lines.iter().enumerate() {
        if is_target_body_line(line) {
            continue;
        }
        let id = parts.len();
        other_part_ids[li] = Some(id);
        parts.push(Participant {
            left: line.left,
            right: line.right,
            top: line.top,
            bottom: line.bottom,
            kind: ParticipantKind::OtherLine,
        });
    }

    (parts, word_part_ids, table_part_ids, other_part_ids)
}

/// X分割ありのページの葉領域ごとのブロック列構築
fn build_blocks_with_regions(
    page: &TextPage,
    body_lines: Vec<TextLine>,
    tables: Vec<Table>,
    leaf_of: &[usize],
    word_part_ids: &[Vec<usize>],
    table_part_ids: &[usize],
    other_part_ids: &[Option<usize>],
) -> Vec<DocBlock> {
    let n_leaves = leaf_of.iter().copied().max().map(|m| m + 1).unwrap_or(0);
    let mut leaf_lines: Vec<Vec<TextLine>> = (0..n_leaves).map(|_| Vec::new()).collect();

    for (li, line) in body_lines.into_iter().enumerate() {
        if is_target_body_line(&line) {
            let word_leaves: Vec<usize> =
                word_part_ids[li].iter().map(|&pid| leaf_of[pid]).collect();
            for (leaf, frag) in reconstitute_line_by_leaves(page, line, &word_leaves) {
                if leaf < leaf_lines.len() {
                    leaf_lines[leaf].push(frag);
                }
            }
        } else if let Some(pid) = other_part_ids[li] {
            let leaf = leaf_of[pid];
            if leaf < leaf_lines.len() {
                leaf_lines[leaf].push(line);
            }
        }
    }

    let n_tables = tables.len();
    let mut keyed: Vec<(usize, f64, f64, usize, DocBlock)> = Vec::new();

    for (i, table) in tables.into_iter().enumerate() {
        let leaf = leaf_of[table_part_ids[i]];
        let top = table.bbox.top;
        let left = table.bbox.x0;
        keyed.push((leaf, top, left, i, DocBlock::Table(table)));
    }

    let mut gen_no = n_tables;
    for (leaf, lines) in leaf_lines.into_iter().enumerate() {
        if lines.is_empty() {
            continue;
        }
        let text_blocks = build_text_blocks(page, lines);
        for tb in text_blocks {
            let top = tb.top;
            let left = tb.left;
            keyed.push((leaf, top, left, gen_no, DocBlock::Text(tb)));
            gen_no += 1;
        }
    }

    keyed.sort_by(|a, b| {
        a.0.cmp(&b.0)
            .then_with(|| a.1.total_cmp(&b.1))
            .then_with(|| a.2.total_cmp(&b.2))
            .then_with(|| a.3.cmp(&b.3))
    });
    keyed.into_iter().map(|(_, _, _, _, b)| b).collect()
}

/// 対象本文行の葉ごとの行断片への振り分け
fn reconstitute_line_by_leaves(
    page: &TextPage,
    line: TextLine,
    word_leaves: &[usize],
) -> Vec<(usize, TextLine)> {
    if line.words.is_empty() || word_leaves.is_empty() {
        return Vec::new();
    }
    debug_assert_eq!(line.words.len(), word_leaves.len());

    let first = word_leaves[0];
    if word_leaves.iter().all(|&l| l == first) {
        return vec![(first, line)];
    }

    // 各グリフが属する単語(無ければ単語外)
    let mut char_word: Vec<(u32, usize)> = Vec::new();
    for (wi, w) in line.words.iter().enumerate() {
        for &c in &w.chars {
            char_word.push((c, wi));
        }
    }
    char_word.sort_unstable_by_key(|&(c, _)| c);

    let find_word = |cidx: u32| -> Option<usize> {
        char_word
            .binary_search_by_key(&cidx, |&(c, _)| c)
            .ok()
            .map(|i| char_word[i].1)
    };

    // 葉の出現順(語順)
    let mut leaf_order: Vec<usize> = Vec::new();
    for &l in word_leaves {
        if !leaf_order.contains(&l) {
            leaf_order.push(l);
        }
    }

    // 単語外空白の葉(進行軸順で直前語、行頭は直後語)
    let mut free_ws_leaf: Vec<(u32, usize)> = Vec::new();
    let mut last_word: Option<usize> = None;
    for (pos, &cidx) in line.chars.iter().enumerate() {
        if let Some(wi) = find_word(cidx) {
            last_word = Some(wi);
            continue;
        }
        if !is_whitespace_str(&page.chars[cidx as usize].text) {
            continue;
        }
        if let Some(wi) = last_word {
            free_ws_leaf.push((cidx, word_leaves[wi]));
        } else {
            let mut next = None;
            for &c2 in &line.chars[pos + 1..] {
                if let Some(wi) = find_word(c2) {
                    next = Some(wi);
                    break;
                }
            }
            if let Some(wi) = next {
                free_ws_leaf.push((cidx, word_leaves[wi]));
            }
        }
    }
    free_ws_leaf.sort_unstable_by_key(|&(c, _)| c);

    let free_leaf = |cidx: u32| -> Option<usize> {
        free_ws_leaf
            .binary_search_by_key(&cidx, |&(c, _)| c)
            .ok()
            .map(|i| free_ws_leaf[i].1)
    };

    // 各葉への積み方は元の line.chars 順
    let mut chars_by_leaf: Vec<Vec<u32>> = vec![Vec::new(); leaf_order.len()];
    let leaf_slot = |leaf: usize| -> Option<usize> { leaf_order.iter().position(|&l| l == leaf) };
    for &cidx in &line.chars {
        let leaf = if let Some(wi) = find_word(cidx) {
            word_leaves[wi]
        } else if let Some(l) = free_leaf(cidx) {
            l
        } else {
            continue;
        };
        if let Some(slot) = leaf_slot(leaf) {
            chars_by_leaf[slot].push(cidx);
        }
    }

    let mut out = Vec::new();
    for (slot, &leaf) in leaf_order.iter().enumerate() {
        let words: Vec<TextWord> = line
            .words
            .iter()
            .zip(word_leaves.iter())
            .filter(|(_, l)| **l == leaf)
            .map(|(w, _)| w.clone())
            .collect();
        if words.is_empty() {
            continue;
        }
        let chars = chars_by_leaf[slot].clone();
        let Some((left, right, top, bottom)) = non_ws_union_bbox(page, &chars) else {
            continue;
        };
        out.push((
            leaf,
            TextLine {
                left,
                right,
                top,
                bottom,
                dir: line.dir.clone(),
                rot: line.rot,
                words,
                chars,
            },
        ));
    }
    out
}

fn collect_valid_cells(tables: &[Table]) -> Vec<BBox> {
    let mut out = Vec::new();
    for table in tables {
        for cell in table.data.iter().flatten() {
            if cell.text.is_empty() {
                continue;
            }
            if cell.bbox.width() <= 0.0 || cell.bbox.height() <= 0.0 {
                continue;
            }
            out.push(cell.bbox);
        }
    }
    out
}

/// 表所有グリフを除いた行
fn filter_table_owned_line(page: &TextPage, line: TextLine, cells: &[BBox]) -> Option<TextLine> {
    if cells.is_empty() {
        return Some(line);
    }

    let mut any_owned = false;
    let mut any_free_non_ws = false;
    let mut owned_idx = std::collections::HashSet::new();
    for &idx in &line.chars {
        let ch = &page.chars[idx as usize];
        let owned = glyph_table_owned(ch, cells);
        if owned {
            any_owned = true;
            owned_idx.insert(idx);
        } else if !is_whitespace_str(&ch.text) {
            any_free_non_ws = true;
        }
    }
    if !any_free_non_ws {
        return None;
    }
    if !any_owned {
        return Some(line);
    }

    let new_chars: Vec<u32> = line
        .chars
        .into_iter()
        .filter(|idx| !owned_idx.contains(idx))
        .collect();

    let mut new_words = Vec::new();
    for word in line.words {
        let wchars: Vec<u32> = word
            .chars
            .into_iter()
            .filter(|idx| !owned_idx.contains(idx))
            .collect();
        if let Some(w) = remake_word(page, &wchars) {
            new_words.push(w);
        }
    }
    if new_words.is_empty() {
        return None;
    }

    let (left, right, top, bottom) = non_ws_union_bbox(page, &new_chars)?;
    Some(TextLine {
        left,
        right,
        top,
        bottom,
        dir: line.dir,
        rot: line.rot,
        words: new_words,
        chars: new_chars,
    })
}

/// グリフ中心が有効セルに含まれるか
fn glyph_table_owned(ch: &TextChar, cells: &[BBox]) -> bool {
    let cx = (ch.left + ch.right) / 2.0;
    let cy = (ch.top + ch.bottom) / 2.0;
    cells.iter().any(|c| c.contains_point(cx, cy))
}

fn remake_word(page: &TextPage, chars: &[u32]) -> Option<TextWord> {
    if chars.is_empty() {
        return None;
    }
    let mut left = f64::INFINITY;
    let mut right = f64::NEG_INFINITY;
    let mut top = f64::INFINITY;
    let mut bottom = f64::NEG_INFINITY;
    let mut any = false;
    let mut text = String::new();
    let mut out_chars = Vec::with_capacity(chars.len());
    for &i in chars {
        let ch = &page.chars[i as usize];
        out_chars.push(i);
        text.push_str(&ch.text);
        if is_whitespace_str(&ch.text) {
            continue;
        }
        any = true;
        left = left.min(ch.left);
        right = right.max(ch.right);
        top = top.min(ch.top);
        bottom = bottom.max(ch.bottom);
    }
    if !any {
        return None;
    }
    Some(TextWord {
        text,
        left,
        right,
        top,
        bottom,
        chars: out_chars,
    })
}

/// 非空白グリフ bbox の和集合
fn non_ws_union_bbox(page: &TextPage, chars: &[u32]) -> Option<(f64, f64, f64, f64)> {
    let mut left = f64::INFINITY;
    let mut right = f64::NEG_INFINITY;
    let mut top = f64::INFINITY;
    let mut bottom = f64::NEG_INFINITY;
    let mut any = false;
    for &i in chars {
        let ch = &page.chars[i as usize];
        if is_whitespace_str(&ch.text) {
            continue;
        }
        any = true;
        left = left.min(ch.left);
        right = right.max(ch.right);
        top = top.min(ch.top);
        bottom = bottom.max(ch.bottom);
    }
    if any {
        Some((left, right, top, bottom))
    } else {
        None
    }
}

/// 行の代表値(ソート・併合で使い回す)
struct LineRep {
    baseline: f64,
    font_size: Option<f64>,
    progress: (f64, f64),
    stream_index: u32,
}

fn build_text_blocks(page: &TextPage, lines: Vec<TextLine>) -> Vec<TextBlock> {
    if lines.is_empty() {
        return Vec::new();
    }

    // 書字方向グループ (dir, rot) ごとの最小 chars インデックス
    let mut group_keys: Vec<(String, i32)> = Vec::new();
    let mut group_lines: Vec<Vec<(LineRep, TextLine)>> = Vec::new();
    let mut group_min: Vec<u32> = Vec::new();
    let mut key_pos: std::collections::HashMap<(String, i32), usize> =
        std::collections::HashMap::new();

    for line in lines {
        let key = (line.dir.clone(), line.rot);
        let rep = LineRep {
            baseline: line_rep_baseline(page, &line),
            font_size: line_rep_font_size(&page.chars, &line),
            progress: line_progress_interval(page, &line),
            stream_index: line_stream_index(&line),
        };
        let min_idx = rep.stream_index;
        if let Some(&pos) = key_pos.get(&key) {
            group_min[pos] = group_min[pos].min(min_idx);
            group_lines[pos].push((rep, line));
        } else {
            let pos = group_keys.len();
            key_pos.insert(key.clone(), pos);
            group_keys.push(key);
            group_min.push(min_idx);
            group_lines.push(vec![(rep, line)]);
        }
    }

    let mut order: Vec<usize> = (0..group_keys.len()).collect();
    order.sort_by(|&a, &b| group_min[a].cmp(&group_min[b]).then_with(|| a.cmp(&b)));

    let mut blocks = Vec::new();
    for gi in order {
        let mut glines = std::mem::take(&mut group_lines[gi]);
        glines.sort_by(|a, b| {
            a.0.baseline
                .total_cmp(&b.0.baseline)
                .then_with(|| a.0.stream_index.cmp(&b.0.stream_index))
        });

        let mut para: Vec<TextLine> = Vec::new();
        let mut prev_rep: Option<LineRep> = None;
        for (rep, line) in glines {
            if para.is_empty() {
                para.push(line);
                prev_rep = Some(rep);
                continue;
            }
            let prev = prev_rep.as_ref().unwrap();
            if can_merge_line_reps(prev, &rep) {
                para.push(line);
                prev_rep = Some(rep);
            } else {
                blocks.push(make_text_block(std::mem::take(&mut para)));
                para.push(line);
                prev_rep = Some(rep);
            }
        }
        if !para.is_empty() {
            blocks.push(make_text_block(para));
        }
    }
    blocks
}

/// 隣接2行を1段落に併合できるか
fn can_merge_line_reps(a: &LineRep, b: &LineRep) -> bool {
    let Some(fs_a) = a.font_size else {
        return false;
    };
    let Some(fs_b) = b.font_size else {
        return false;
    };
    let gap = (b.baseline - a.baseline).abs();
    if gap > fs_a.max(fs_b) * PARA_LEADING_FACTOR {
        return false;
    }
    if !intervals_overlap(a.progress, b.progress) {
        return false;
    }
    let lo = fs_a.min(fs_b);
    let hi = fs_a.max(fs_b);
    if lo <= 0.0 {
        return false;
    }
    let ratio = hi / lo;
    ratio <= PARA_FS_RATIO_MAX
}

fn make_text_block(lines: Vec<TextLine>) -> TextBlock {
    let dir = lines[0].dir.clone();
    let rot = lines[0].rot;
    let text = join_paragraph_text(&lines);
    let mut left = f64::INFINITY;
    let mut right = f64::NEG_INFINITY;
    let mut top = f64::INFINITY;
    let mut bottom = f64::NEG_INFINITY;
    for line in &lines {
        left = left.min(line.left);
        right = right.max(line.right);
        top = top.min(line.top);
        bottom = bottom.max(line.bottom);
    }
    TextBlock {
        kind: TextBlockKind::Paragraph,
        role: TextBlockRole::Body,
        text,
        left,
        right,
        top,
        bottom,
        dir,
        rot,
        lines,
    }
}

fn join_paragraph_text(lines: &[TextLine]) -> String {
    if lines.is_empty() {
        return String::new();
    }
    let mut out = join_line_words(&lines[0]);
    for i in 1..lines.len() {
        let prev = &lines[i - 1];
        let next = &lines[i];
        let next_str = join_line_words(next);
        let prev_last = prev.words.last().and_then(|w| w.text.chars().last());
        let next_first = next.words.first().and_then(|w| w.text.chars().next());
        match (prev_last, next_first) {
            (Some('\u{00AD}'), _) => {
                if out.ends_with('\u{00AD}') {
                    out.pop();
                }
                out.push_str(&next_str);
            }
            (Some('-'), Some(nf)) => {
                let before = prev.words.last().and_then(|w| {
                    let mut it = w.text.chars().rev();
                    let _hyphen = it.next();
                    it.next()
                });
                if before.is_some_and(is_latin_letter) && is_latin_letter(nf) && nf.is_lowercase() {
                    if out.ends_with('-') {
                        out.pop();
                    }
                    out.push_str(&next_str);
                } else {
                    out.push_str(&next_str);
                }
            }
            (Some(pl), Some(nf)) if is_cjk(pl) || is_cjk(nf) => {
                out.push_str(&next_str);
            }
            (Some(_), Some(_)) => {
                out.push(' ');
                out.push_str(&next_str);
            }
            _ => {
                if !next_str.is_empty() {
                    if !out.is_empty() {
                        out.push(' ');
                    }
                    out.push_str(&next_str);
                }
            }
        }
    }
    out
}

fn join_line_words(line: &TextLine) -> String {
    let mut s = String::new();
    for (i, w) in line.words.iter().enumerate() {
        if i > 0 {
            s.push(' ');
        }
        s.push_str(&w.text);
    }
    s
}

/// ページ内のリスト検出
fn assign_lists_page(page: &mut DocPage) {
    let blocks = std::mem::take(&mut page.blocks);
    let mut out = Vec::with_capacity(blocks.len());
    let mut iter = blocks.into_iter().peekable();
    while let Some(block) = iter.next() {
        match block {
            DocBlock::Text(tb)
                if tb.role == TextBlockRole::Body && tb.kind == TextBlockKind::Paragraph =>
            {
                let mut run: Vec<TextBlock> = vec![tb];
                while let Some(DocBlock::Text(next)) = iter.peek() {
                    if next.role != TextBlockRole::Body
                        || next.kind != TextBlockKind::Paragraph
                        || next.dir != run[0].dir
                        || next.rot != run[0].rot
                    {
                        break;
                    }
                    if let Some(DocBlock::Text(next_tb)) = iter.next() {
                        run.push(next_tb);
                    }
                }
                out.extend(detect_lists_in_run(run, &page.chars));
            }
            other => out.push(other),
        }
    }
    page.blocks = out;
}

/// 番号マーカーの記号型
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum OrderedMarkerStyle {
    /// `1.`
    Dot,
    /// `1)`
    Paren,
    /// `(1)`
    LParen,
}

/// リストマーカー種別
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ListMarkerKind {
    Bullet,
    Ordered { style: OrderedMarkerStyle, n: u32 },
}

/// 連続ラン内のリスト検出と非成立部の元ブロック保持
fn detect_lists_in_run(run: Vec<TextBlock>, chars: &[TextChar]) -> Vec<DocBlock> {
    let mut lines: Vec<TextLine> = Vec::new();
    let mut origin: Vec<usize> = Vec::new();
    for (bi, block) in run.iter().enumerate() {
        for line in &block.lines {
            lines.push(line.clone());
            origin.push(bi);
        }
    }
    let n = lines.len();
    let mut consumed = vec![false; n];
    let mut list_at: std::collections::HashMap<usize, TextBlock> = std::collections::HashMap::new();

    let mut i = 0;
    while i < n {
        let Some(start_kind) = list_marker_kind(&lines[i]) else {
            i += 1;
            continue;
        };
        let run_left = lines[i].left;
        let ordered = matches!(start_kind, ListMarkerKind::Ordered { .. });
        let mut last_marker: Option<ListMarkerKind> = None;
        let mut items: Vec<Vec<TextLine>> = Vec::new();
        let mut item_indices: Vec<Vec<usize>> = Vec::new();
        let mut j = i;
        while j < n {
            let Some(kind) = list_marker_kind(&lines[j]) else {
                break;
            };
            if !marker_continues_run(last_marker, kind) {
                break;
            }
            if !list_lefts_aligned(run_left, lines[j].left, chars, &lines[i], &lines[j]) {
                break;
            }
            last_marker = Some(kind);
            let marker_left = lines[j].left;
            let marker_fs = line_rep_font_size(chars, &lines[j]);
            let mut item = vec![lines[j].clone()];
            let mut idx = vec![j];
            j += 1;
            while j < n && list_marker_kind(&lines[j]).is_none() {
                let prev = item.last().unwrap();
                if is_hang_continuation(chars, prev, &lines[j], marker_left, marker_fs) {
                    item.push(lines[j].clone());
                    idx.push(j);
                    j += 1;
                } else {
                    break;
                }
            }
            items.push(item);
            item_indices.push(idx);
        }

        if items.len() >= 2 {
            let start_idx = item_indices[0][0];
            for idxs in &item_indices {
                for &fi in idxs {
                    consumed[fi] = true;
                }
            }
            list_at.insert(start_idx, make_list_block(items, ordered));
            i = j;
        } else {
            i += 1;
        }
    }

    let mut block_untouched = vec![true; run.len()];
    for i in 0..n {
        if consumed[i] {
            block_untouched[origin[i]] = false;
        }
    }
    let mut run_opt: Vec<Option<TextBlock>> = run.into_iter().map(Some).collect();

    let mut out: Vec<DocBlock> = Vec::new();
    let mut idx = 0;
    while idx < n {
        if consumed[idx] {
            if let Some(lb) = list_at.remove(&idx) {
                out.push(DocBlock::Text(lb));
            }
            idx += 1;
            while idx < n && consumed[idx] && !list_at.contains_key(&idx) {
                idx += 1;
            }
        } else {
            let bi = origin[idx];
            if block_untouched[bi] {
                let block = run_opt[bi].take().expect("intact block should be untaken");
                let block_len = block.lines.len();
                out.push(DocBlock::Text(block));
                idx += block_len;
            } else {
                let mut group_lines: Vec<TextLine> = Vec::new();
                while idx < n && !consumed[idx] && origin[idx] == bi {
                    group_lines.push(lines[idx].clone());
                    idx += 1;
                }
                if !group_lines.is_empty() {
                    out.push(DocBlock::Text(make_text_block(group_lines)));
                }
            }
        }
    }
    out
}

/// リスト区間へのマーカー追加可否
fn marker_continues_run(last: Option<ListMarkerKind>, kind: ListMarkerKind) -> bool {
    match (last, kind) {
        (None, _) => true,
        (Some(ListMarkerKind::Bullet), ListMarkerKind::Bullet) => true,
        (
            Some(ListMarkerKind::Ordered { style: s0, n: n0 }),
            ListMarkerKind::Ordered { style: s1, n: n1 },
        ) => s0 == s1 && n1 == n0.saturating_add(1),
        _ => false,
    }
}

/// 行頭リストマーカーの判定
fn list_marker_kind(line: &TextLine) -> Option<ListMarkerKind> {
    if line.dir == "ttb" {
        return None;
    }
    let first = line.words.first()?;
    if line.words.len() < 2 {
        return None;
    }
    if is_bullet_marker_word(&first.text) {
        return Some(ListMarkerKind::Bullet);
    }
    if let Some((style, n)) = parse_ordered_marker_word(&first.text) {
        return Some(ListMarkerKind::Ordered { style, n });
    }
    None
}

fn is_bullet_marker_word(text: &str) -> bool {
    if text == "-" || text == "*" {
        return true;
    }
    let mut chars = text.chars();
    matches!((chars.next(), chars.next()), (Some(c), None) if is_bullet_glyph(c))
}

fn parse_ordered_marker_word(text: &str) -> Option<(OrderedMarkerStyle, u32)> {
    let b = text.as_bytes();
    if b.len() < 2 {
        return None;
    }
    // (N)
    if b[0] == b'(' && b[b.len() - 1] == b')' && b.len() >= 3 {
        let digits = &b[1..b.len() - 1];
        let n = parse_marker_digits(digits)?;
        return Some((OrderedMarkerStyle::LParen, n));
    }
    // N.
    if b[b.len() - 1] == b'.' {
        let digits = &b[..b.len() - 1];
        let n = parse_marker_digits(digits)?;
        return Some((OrderedMarkerStyle::Dot, n));
    }
    // N)
    if b[b.len() - 1] == b')' {
        let digits = &b[..b.len() - 1];
        let n = parse_marker_digits(digits)?;
        return Some((OrderedMarkerStyle::Paren, n));
    }
    None
}

fn parse_marker_digits(digits: &[u8]) -> Option<u32> {
    if digits.is_empty() || digits.len() > 3 || !digits.iter().all(|c| c.is_ascii_digit()) {
        return None;
    }
    std::str::from_utf8(digits).ok()?.parse().ok()
}

fn is_list_marker_word(text: &str) -> bool {
    is_bullet_marker_word(text) || parse_ordered_marker_word(text).is_some()
}

fn is_bullet_glyph(c: char) -> bool {
    matches!(
        c,
        '\u{2022}' | '\u{25E6}' | '\u{25AA}' | '\u{25CF}' | '\u{25CB}' | '\u{25A0}' | '\u{25C6}'
    )
}

/// マーカー左端の字下げ一致
fn list_lefts_aligned(
    left_a: f64,
    left_b: f64,
    chars: &[TextChar],
    line_a: &TextLine,
    line_b: &TextLine,
) -> bool {
    let fs_a = line_rep_font_size(chars, line_a).unwrap_or(0.0);
    let fs_b = line_rep_font_size(chars, line_b).unwrap_or(0.0);
    let tol = list_indent_tol(fs_a.max(fs_b));
    (left_a - left_b).abs() <= tol
}

fn list_indent_tol(font_size: f64) -> f64 {
    let scaled = if font_size.is_finite() && font_size > 0.0 {
        font_size * LIST_INDENT_FACTOR
    } else {
        0.0
    };
    LIST_INDENT_MIN_PT.max(scaled)
}

fn is_hang_continuation(
    chars: &[TextChar],
    prev: &TextLine,
    next: &TextLine,
    marker_left: f64,
    marker_fs: Option<f64>,
) -> bool {
    if next.dir == "ttb" || list_marker_kind(next).is_some() {
        return false;
    }
    let Some(mfs) = marker_fs else {
        return false;
    };
    let Some(nfs) = line_rep_font_size(chars, next) else {
        return false;
    };
    if !(mfs.is_finite() && mfs > 0.0 && nfs.is_finite() && nfs > 0.0) {
        return false;
    }
    let hang = list_indent_tol(mfs);
    if next.left + f64::EPSILON < marker_left + hang {
        return false;
    }
    let gap = (line_rep_baseline_from_chars(chars, next)
        - line_rep_baseline_from_chars(chars, prev))
    .abs();
    gap <= mfs.max(nfs) * PARA_LEADING_FACTOR
}

fn make_list_block(items: Vec<Vec<TextLine>>, ordered: bool) -> TextBlock {
    let mut all_lines: Vec<TextLine> = Vec::new();
    let mut item_texts: Vec<String> = Vec::new();
    for item in items {
        let body_lines = list_item_body_lines(&item);
        let body = join_paragraph_text(&body_lines);
        if !body.trim().is_empty() {
            item_texts.push(body);
        }
        all_lines.extend(item);
    }
    let dir = all_lines
        .first()
        .map(|l| l.dir.clone())
        .unwrap_or_else(|| "ltr".into());
    let rot = all_lines.first().map(|l| l.rot).unwrap_or(0);
    let mut left = f64::INFINITY;
    let mut right = f64::NEG_INFINITY;
    let mut top = f64::INFINITY;
    let mut bottom = f64::NEG_INFINITY;
    for line in &all_lines {
        left = left.min(line.left);
        right = right.max(line.right);
        top = top.min(line.top);
        bottom = bottom.max(line.bottom);
    }
    TextBlock {
        kind: TextBlockKind::List { ordered },
        role: TextBlockRole::Body,
        text: item_texts.join("\n"),
        left,
        right,
        top,
        bottom,
        dir,
        rot,
        lines: all_lines,
    }
}

/// マーカーを除いた項目本文行
fn list_item_body_lines(item: &[TextLine]) -> Vec<TextLine> {
    if item.is_empty() {
        return Vec::new();
    }
    let mut out = Vec::with_capacity(item.len());
    let mut first = item[0].clone();
    if first
        .words
        .first()
        .is_some_and(|w| is_list_marker_word(&w.text))
    {
        first.words.remove(0);
        rebuild_line_from_words(&mut first);
    }
    out.push(first);
    out.extend(item.iter().skip(1).cloned());
    out
}

/// 単語列に合わせた行の chars と bbox
fn rebuild_line_from_words(line: &mut TextLine) {
    line.chars = line
        .words
        .iter()
        .flat_map(|w| w.chars.iter().copied())
        .collect();
    if line.words.is_empty() {
        line.right = line.left;
        return;
    }
    line.left = line
        .words
        .iter()
        .map(|w| w.left)
        .fold(f64::INFINITY, f64::min);
    line.right = line
        .words
        .iter()
        .map(|w| w.right)
        .fold(f64::NEG_INFINITY, f64::max);
    line.top = line
        .words
        .iter()
        .map(|w| w.top)
        .fold(f64::INFINITY, f64::min);
    line.bottom = line
        .words
        .iter()
        .map(|w| w.bottom)
        .fold(f64::NEG_INFINITY, f64::max);
}

/// chars 参照での行代表ベースライン
fn line_rep_baseline_from_chars(chars: &[TextChar], line: &TextLine) -> f64 {
    let vertical = line.dir == "ttb";
    let mut vals = Vec::new();
    for &idx in &line.chars {
        let Some(ch) = chars.get(idx as usize) else {
            continue;
        };
        if is_whitespace_str(&ch.text) {
            continue;
        }
        let (px, py) = derotate_xy(ch.transform[4], ch.transform[5], line.rot);
        let v = if vertical { px } else { py };
        if v.is_finite() {
            vals.push(v);
        }
    }
    median_f64(&mut vals).unwrap_or(line.top)
}

/// 行のストリーム順キー
fn line_stream_index(line: &TextLine) -> u32 {
    line.chars.iter().copied().min().unwrap_or(u32::MAX)
}

/// 行の代表ベースライン
fn line_rep_baseline(page: &TextPage, line: &TextLine) -> f64 {
    let vertical = line.dir == "ttb";
    let mut vals = Vec::new();
    for &idx in &line.chars {
        let ch = &page.chars[idx as usize];
        if is_whitespace_str(&ch.text) {
            continue;
        }
        let (px, py) = derotate_xy(ch.transform[4], ch.transform[5], line.rot);
        let v = if vertical { px } else { py };
        if v.is_finite() {
            vals.push(v);
        }
    }
    median_f64(&mut vals).unwrap_or(0.0)
}

/// 行の代表 font_size
fn line_rep_font_size(chars: &[TextChar], line: &TextLine) -> Option<f64> {
    let mut vals = Vec::new();
    for &idx in &line.chars {
        let ch = &chars[idx as usize];
        if is_whitespace_str(&ch.text) {
            continue;
        }
        if ch.font_size.is_finite() && ch.font_size > 0.0 {
            vals.push(ch.font_size);
        }
    }
    median_f64(&mut vals)
}

/// 行の進行軸区間
fn line_progress_interval(page: &TextPage, line: &TextLine) -> (f64, f64) {
    let vertical = line.dir == "ttb";
    let mut min_p = f64::INFINITY;
    let mut max_p = f64::NEG_INFINITY;
    let mut any = false;
    for &idx in &line.chars {
        let ch = &page.chars[idx as usize];
        if is_whitespace_str(&ch.text) {
            continue;
        }
        any = true;
        for &(x, y) in &[
            (ch.left, ch.top),
            (ch.right, ch.top),
            (ch.left, ch.bottom),
            (ch.right, ch.bottom),
        ] {
            let (px, py) = derotate_xy(x, y, line.rot);
            let p = if vertical { py } else { px };
            min_p = min_p.min(p);
            max_p = max_p.max(p);
        }
    }
    if any { (min_p, max_p) } else { (0.0, 0.0) }
}

/// 進行軸区間の共有長が正か
fn intervals_overlap(a: (f64, f64), b: (f64, f64)) -> bool {
    let lo = a.0.max(b.0);
    let hi = a.1.min(b.1);
    hi > lo
}

/// 有限値の中央値
fn median_f64(values: &mut [f64]) -> Option<f64> {
    let n = values.len();
    if n == 0 {
        return None;
    }
    let cmp = |a: &f64, b: &f64| a.total_cmp(b);
    if n % 2 == 1 {
        let mid = n / 2;
        values.select_nth_unstable_by(mid, cmp);
        Some(values[mid])
    } else {
        let hi = n / 2;
        values.select_nth_unstable_by(hi, cmp);
        let upper = values[hi];
        let lower = values[..hi]
            .iter()
            .copied()
            .fold(f64::NEG_INFINITY, f64::max);
        Some((lower + upper) / 2.0)
    }
}

fn is_latin_letter(c: char) -> bool {
    matches!(c,
        'A'..='Z'
        | 'a'..='z'
        | '\u{00C0}'..='\u{00D6}'
        | '\u{00D8}'..='\u{00F6}'
        | '\u{00F8}'..='\u{00FF}'
        | '\u{0100}'..='\u{017F}'
        | '\u{0180}'..='\u{024F}'
        | '\u{1E00}'..='\u{1EFF}'
    )
}

fn is_cjk(c: char) -> bool {
    matches!(c,
        // Hangul Jamo
        '\u{1100}'..='\u{11FF}'
        // CJK Radicals Supplement
        | '\u{2E80}'..='\u{2EFF}'
        // Kangxi Radicals
        | '\u{2F00}'..='\u{2FDF}'
        // Ideographic Description Characters
        | '\u{2FF0}'..='\u{2FFF}'
        // CJK Symbols and Punctuation
        | '\u{3000}'..='\u{303F}'
        // Hiragana
        | '\u{3040}'..='\u{309F}'
        // Katakana
        | '\u{30A0}'..='\u{30FF}'
        // Bopomofo
        | '\u{3100}'..='\u{312F}'
        // Hangul Compatibility Jamo
        | '\u{3130}'..='\u{318F}'
        // Kanbun / Bopomofo Extended
        | '\u{3190}'..='\u{31EF}'
        // Katakana Phonetic Extensions
        | '\u{31F0}'..='\u{31FF}'
        // Enclosed CJK Letters and Months
        | '\u{3200}'..='\u{32FF}'
        // CJK Compatibility
        | '\u{3300}'..='\u{33FF}'
        // CJK Unified Ideographs Extension A
        | '\u{3400}'..='\u{4DBF}'
        // CJK Unified Ideographs
        | '\u{4E00}'..='\u{9FFF}'
        // Hangul Syllables
        | '\u{AC00}'..='\u{D7AF}'
        // CJK Compatibility Ideographs
        | '\u{F900}'..='\u{FAFF}'
        // Vertical Forms
        | '\u{FE10}'..='\u{FE1F}'
        // CJK Compatibility Forms
        | '\u{FE30}'..='\u{FE4F}'
        // Halfwidth and Fullwidth Forms
        | '\u{FF00}'..='\u{FFEF}'
        // CJK Unified Ideographs Extension B〜 / Compatibility Supplement
        | '\u{20000}'..='\u{2FA1F}'
        // CJK Unified Ideographs Extension G〜H
        | '\u{30000}'..='\u{323AF}'
    )
}

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

    fn font(vertical: bool) -> TextFont {
        TextFont {
            name: "F".into(),
            ascent: 0.8,
            descent: -0.2,
            vertical,
            bold: false,
            italic: false,
        }
    }

    fn ch(text: &str, x: f64, y: f64, adv_x: f64, adv_y: f64, font_size: f64) -> TextChar {
        TextChar {
            text: text.into(),
            left: x,
            right: x + adv_x.abs().max(1.0),
            top: y,
            bottom: y + font_size,
            transform: [font_size, 0.0, 0.0, -font_size, x, y],
            advance: [adv_x, adv_y],
            glyph_width: None,
            font: 0,
            font_size,
            rot: 0,
            upright: true,
            synthetic: false,
        }
    }

    fn ch_at(
        text: &str,
        x: f64,
        y: f64,
        adv_x: f64,
        adv_y: f64,
        font_size: f64,
        font_idx: u32,
        vertical: bool,
    ) -> TextChar {
        TextChar {
            text: text.into(),
            left: x,
            right: x + if vertical {
                font_size
            } else {
                adv_x.abs().max(1.0)
            },
            top: y,
            bottom: y + if vertical {
                adv_y.abs().max(font_size)
            } else {
                font_size
            },
            transform: [font_size, 0.0, 0.0, -font_size, x, y],
            advance: [adv_x, adv_y],
            glyph_width: None,
            font: font_idx,
            font_size,
            rot: 0,
            upright: !vertical,
            synthetic: false,
        }
    }

    fn ch_rot(text: &str, x: f64, y: f64, advance: f64, font_size: f64, rot: i32) -> TextChar {
        let (transform, glyph_advance, left, right, top, bottom) = match rot {
            90 => (
                [0.0, -font_size, -font_size, 0.0, x, y],
                [0.0, -advance],
                x - font_size,
                x,
                y - advance,
                y,
            ),
            180 => (
                [-font_size, 0.0, 0.0, font_size, x, y],
                [-advance, 0.0],
                x - advance,
                x,
                y - font_size,
                y,
            ),
            270 => (
                [0.0, font_size, font_size, 0.0, x, y],
                [0.0, advance],
                x,
                x + font_size,
                y,
                y + advance,
            ),
            _ => (
                [font_size, 0.0, 0.0, -font_size, x, y],
                [advance, 0.0],
                x,
                x + advance,
                y,
                y + font_size,
            ),
        };
        TextChar {
            text: text.into(),
            left,
            right,
            top,
            bottom,
            transform,
            advance: glyph_advance,
            glyph_width: None,
            font: 0,
            font_size,
            rot,
            upright: rot == 0,
            synthetic: false,
        }
    }

    fn page(chars: Vec<TextChar>) -> TextPage {
        TextPage {
            width: 600.0,
            height: 800.0,
            fonts: vec![font(false)],
            chars,
        }
    }

    fn cell(text: &str, left: f64, top: f64, right: f64, bottom: f64) -> Cell {
        Cell {
            text: text.into(),
            bbox: BBox {
                x0: left,
                top,
                x1: right,
                bottom,
            },
        }
    }

    fn table_one(cells: Vec<Vec<Cell>>, left: f64, top: f64, right: f64, bottom: f64) -> Table {
        Table {
            extraction_method: "lattice",
            bbox: BBox {
                x0: left,
                top,
                x1: right,
                bottom,
            },
            n_rows: cells.len(),
            n_cols: cells.first().map(|r| r.len()).unwrap_or(0),
            data: cells,
        }
    }

    fn text_blocks(blocks: &[DocBlock]) -> Vec<&TextBlock> {
        blocks
            .iter()
            .filter_map(|b| match b {
                DocBlock::Text(t) => Some(t),
                _ => None,
            })
            .collect()
    }

    fn word_line(text: &str, x: f64, y: f64, fs: f64) -> Vec<TextChar> {
        let mut chars = Vec::new();
        let mut cx = x;
        let adv = fs * 0.5;
        for c in text.chars() {
            let s = c.to_string();
            chars.push(ch(&s, cx, y, adv, 0.0, fs));
            cx += adv;
        }
        chars
    }

    #[test]
    fn paragraph_merges_close_lines() {
        let fs = 10.0;
        let adv = 5.0;
        // 行送り 15 ≤ 1.8*10 → 併合
        let p = page(vec![
            ch("A", 0.0, 100.0, adv, 0.0, fs),
            ch("B", adv, 100.0, adv, 0.0, fs),
            ch("C", 0.0, 115.0, adv, 0.0, fs),
            ch("D", adv, 115.0, adv, 0.0, fs),
        ]);
        let blocks = build_blocks(&p, &[]);
        let tb = text_blocks(&blocks);
        assert_eq!(tb.len(), 1);
        assert_eq!(tb[0].text, "AB CD");
        assert_eq!(tb[0].lines.len(), 2);
        assert_eq!(tb[0].kind, TextBlockKind::Paragraph);
    }

    #[test]
    fn paragraph_splits_on_large_leading() {
        let fs = 10.0;
        let adv = 5.0;
        // 行送り 20 > 1.8*10 → 分割
        let p = page(vec![
            ch("A", 0.0, 100.0, adv, 0.0, fs),
            ch("B", 0.0, 120.0, adv, 0.0, fs),
        ]);
        let blocks = build_blocks(&p, &[]);
        let tb = text_blocks(&blocks);
        assert_eq!(tb.len(), 2);
        assert_eq!(tb[0].text, "A");
        assert_eq!(tb[1].text, "B");
    }

    #[test]
    fn paragraph_splits_on_font_size_ratio() {
        let adv = 5.0;
        // 10 と 20 で比 2.0 > 1.33
        let p = page(vec![
            ch("A", 0.0, 100.0, adv, 0.0, 10.0),
            ch("B", 0.0, 112.0, adv, 0.0, 20.0),
        ]);
        let blocks = build_blocks(&p, &[]);
        let tb = text_blocks(&blocks);
        assert_eq!(tb.len(), 2);
    }

    #[test]
    fn paragraph_splits_on_progress_non_overlap() {
        let fs = 10.0;
        let adv = 5.0;
        // 進行軸区間が重ならない
        let p = page(vec![
            ch("A", 0.0, 100.0, adv, 0.0, fs),
            ch("B", 100.0, 110.0, adv, 0.0, fs),
        ]);
        let blocks = build_blocks(&p, &[]);
        let tb = text_blocks(&blocks);
        assert_eq!(tb.len(), 2);
    }

    #[test]
    fn join_latin_inserts_space() {
        let fs = 10.0;
        let mut chars = word_line("hello", 0.0, 100.0, fs);
        chars.extend(word_line("world", 0.0, 115.0, fs));
        let p = page(chars);
        let blocks = build_blocks(&p, &[]);
        let tb = text_blocks(&blocks);
        assert_eq!(tb.len(), 1);
        assert_eq!(tb[0].text, "hello world");
    }

    #[test]
    fn join_cjk_no_space() {
        let fs = 10.0;
        let mut chars = word_line("日本", 0.0, 100.0, fs);
        chars.extend(word_line("語文", 0.0, 115.0, fs));
        let p = page(chars);
        let blocks = build_blocks(&p, &[]);
        let tb = text_blocks(&blocks);
        assert_eq!(tb.len(), 1);
        assert_eq!(tb[0].text, "日本語文");
    }

    #[test]
    fn join_cjk_ext_b_no_space() {
        let fs = 10.0;
        let mut chars = word_line("株式", 0.0, 100.0, fs);
        chars.extend(word_line("\u{20BB7}", 0.0, 115.0, fs));
        let p = page(chars);
        let blocks = build_blocks(&p, &[]);
        let tb = text_blocks(&blocks);
        assert_eq!(tb.len(), 1);
        assert_eq!(tb[0].text, "株式\u{20BB7}");
    }

    #[test]
    fn join_hyphen_removed_when_latin_lowercase() {
        let fs = 10.0;
        let mut chars = word_line("hyphen-", 0.0, 100.0, fs);
        chars.extend(word_line("ation", 0.0, 115.0, fs));
        let p = page(chars);
        let blocks = build_blocks(&p, &[]);
        let tb = text_blocks(&blocks);
        assert_eq!(tb.len(), 1);
        assert_eq!(tb[0].text, "hyphenation");
    }

    #[test]
    fn join_hyphen_kept_no_space_when_not_lowercase() {
        let fs = 10.0;
        let mut chars = word_line("FOO-", 0.0, 100.0, fs);
        chars.extend(word_line("BAR", 0.0, 115.0, fs));
        let p = page(chars);
        let blocks = build_blocks(&p, &[]);
        let tb = text_blocks(&blocks);
        assert_eq!(tb.len(), 1);
        assert_eq!(tb[0].text, "FOO-BAR");
    }

    #[test]
    fn join_soft_hyphen_always_removed() {
        let fs = 10.0;
        let mut chars = word_line("soft\u{00AD}", 0.0, 100.0, fs);
        chars.extend(word_line("ware", 0.0, 115.0, fs));
        let p = page(chars);
        let blocks = build_blocks(&p, &[]);
        let tb = text_blocks(&blocks);
        assert_eq!(tb.len(), 1);
        assert_eq!(tb[0].text, "software");
    }

    #[test]
    fn join_hyphen_digit_keeps_hyphen_no_space() {
        let fs = 10.0;
        let mut chars = word_line("end-", 0.0, 100.0, fs);
        chars.extend(word_line("123", 0.0, 115.0, fs));
        let p = page(chars);
        let blocks = build_blocks(&p, &[]);
        let tb = text_blocks(&blocks);
        assert_eq!(tb.len(), 1);
        assert_eq!(tb[0].text, "end-123");
    }

    #[test]
    fn table_excludes_fully_owned_line() {
        let fs = 10.0;
        let adv = 5.0;
        // 表セル内の文字と外の文字
        let p = page(vec![
            ch("T", 10.0, 10.0, adv, 0.0, fs),
            ch("A", 15.0, 10.0, adv, 0.0, fs),
            ch("X", 0.0, 100.0, adv, 0.0, fs),
        ]);
        let t = table_one(
            vec![vec![cell("TA", 5.0, 5.0, 40.0, 30.0)]],
            5.0,
            5.0,
            40.0,
            30.0,
        );
        let blocks = build_blocks(&p, &[t]);
        let tb = text_blocks(&blocks);
        assert_eq!(tb.len(), 1);
        assert_eq!(tb[0].text, "X");
        assert!(matches!(blocks[0], DocBlock::Table(_)) || matches!(blocks[1], DocBlock::Table(_)));
    }

    #[test]
    fn table_partial_line_rebuilt() {
        let fs = 10.0;
        let adv = 5.0;
        // 同一行: 表内 A と表外 B(x を離して同一ベースライン)
        let p = page(vec![
            ch("A", 10.0, 100.0, adv, 0.0, fs),
            ch("B", 80.0, 100.0, adv, 0.0, fs),
        ]);
        let t = table_one(
            vec![vec![cell("A", 5.0, 95.0, 30.0, 120.0)]],
            5.0,
            95.0,
            30.0,
            120.0,
        );
        let blocks = build_blocks(&p, &[t]);
        let tb = text_blocks(&blocks);
        assert_eq!(tb.len(), 1);
        assert_eq!(tb[0].text, "B");
        assert_eq!(tb[0].lines[0].chars, vec![1]);
        assert_eq!(tb[0].lines[0].words[0].text, "B");
    }

    #[test]
    fn table_bbox_non_cell_glyph_remains() {
        let fs = 10.0;
        let adv = 5.0;
        // 表 bbox 内だがセル外(キャプション想定)
        let p = page(vec![
            ch("C", 10.0, 50.0, adv, 0.0, fs),
            ch("T", 10.0, 100.0, adv, 0.0, fs),
        ]);
        let t = Table {
            extraction_method: "lattice",
            bbox: BBox {
                x0: 0.0,
                top: 40.0,
                x1: 100.0,
                bottom: 130.0,
            },
            n_rows: 1,
            n_cols: 1,
            data: vec![vec![cell("T", 5.0, 95.0, 40.0, 120.0)]],
        };
        let blocks = build_blocks(&p, &[t]);
        let tb = text_blocks(&blocks);
        assert_eq!(tb.len(), 1);
        assert_eq!(tb[0].text, "C");
    }

    #[test]
    fn invalid_cells_do_not_own_glyphs() {
        let fs = 10.0;
        let adv = 5.0;
        let p = page(vec![ch("A", 10.0, 10.0, adv, 0.0, fs)]);
        // 空 text・ゼロ面積は無効
        let t = table_one(
            vec![vec![
                cell("", 5.0, 5.0, 40.0, 30.0),
                Cell {
                    text: "Z".into(),
                    bbox: BBox {
                        x0: 5.0,
                        top: 5.0,
                        x1: 5.0,
                        bottom: 30.0,
                    },
                },
            ]],
            5.0,
            5.0,
            40.0,
            30.0,
        );
        let blocks = build_blocks(&p, &[t]);
        let tb = text_blocks(&blocks);
        assert_eq!(tb.len(), 1);
        assert_eq!(tb[0].text, "A");
    }

    #[test]
    fn reading_order_interleaves_table_and_paragraph() {
        let fs = 10.0;
        let adv = 5.0;
        let p = page(vec![
            ch("T", 0.0, 10.0, adv, 0.0, fs),  // top para
            ch("B", 0.0, 200.0, adv, 0.0, fs), // bottom para
        ]);
        let t = table_one(
            vec![vec![cell("X", 50.0, 80.0, 90.0, 120.0)]],
            50.0,
            80.0,
            90.0,
            120.0,
        );
        let blocks = build_blocks(&p, &[t]);
        assert_eq!(blocks.len(), 3);
        match &blocks[0] {
            DocBlock::Text(tb) => assert_eq!(tb.text, "T"),
            _ => panic!("expected top text"),
        }
        match &blocks[1] {
            DocBlock::Table(tb) => assert!((tb.bbox.top - 80.0).abs() < 1e-9),
            _ => panic!("expected table"),
        }
        match &blocks[2] {
            DocBlock::Text(tb) => assert_eq!(tb.text, "B"),
            _ => panic!("expected bottom text"),
        }
    }

    #[test]
    fn reading_order_tie_break_by_generation() {
        let fs = 10.0;
        let adv = 5.0;
        // 本文と表が完全に同座標 (top,left)
        let p = page(vec![ch("A", 10.0, 10.0, adv, 0.0, fs)]);
        let t = table_one(
            vec![vec![cell("Z", 100.0, 100.0, 140.0, 130.0)]],
            10.0,
            10.0,
            50.0,
            40.0,
        );
        // 本文 bbox が top=10,left=10、表 bbox も top=10,left=10
        // 表の生成番号 0 < 本文 1 → 表が先
        let blocks = build_blocks(&p, &[t]);
        assert_eq!(blocks.len(), 2);
        assert!(matches!(blocks[0], DocBlock::Table(_)));
        assert!(matches!(blocks[1], DocBlock::Text(_)));
    }

    #[test]
    fn empty_page_yields_no_blocks() {
        let p = page(vec![]);
        let blocks = build_blocks(&p, &[]);
        assert!(blocks.is_empty());
    }

    #[test]
    fn tables_only_page() {
        let p = page(vec![]);
        let t = table_one(
            vec![vec![cell("X", 0.0, 0.0, 10.0, 10.0)]],
            0.0,
            0.0,
            10.0,
            10.0,
        );
        let blocks = build_blocks(&p, &[t]);
        assert_eq!(blocks.len(), 1);
        assert!(matches!(blocks[0], DocBlock::Table(_)));
    }

    #[test]
    fn all_lines_table_owned() {
        let fs = 10.0;
        let adv = 5.0;
        let p = page(vec![
            ch("A", 10.0, 10.0, adv, 0.0, fs),
            ch("B", 15.0, 10.0, adv, 0.0, fs),
        ]);
        let t = table_one(
            vec![vec![cell("AB", 5.0, 5.0, 40.0, 30.0)]],
            5.0,
            5.0,
            40.0,
            30.0,
        );
        let blocks = build_blocks(&p, &[t]);
        assert_eq!(blocks.len(), 1);
        assert!(matches!(blocks[0], DocBlock::Table(_)));
        assert!(text_blocks(&blocks).is_empty());
    }

    #[test]
    fn rotated_group_builds_paragraph() {
        let fs = 10.0;
        let adv = 5.0;
        let p = page(vec![
            ch_rot("A", 100.0, 60.0, adv, fs, 90),
            ch_rot("B", 100.0, 60.0 - adv, adv, fs, 90),
            ch_rot("C", 100.0, 60.0 - adv * 2.0, adv, fs, 90),
        ]);
        let blocks = build_blocks(&p, &[]);
        let tb = text_blocks(&blocks);
        assert_eq!(tb.len(), 1);
        assert_eq!(tb[0].rot, 90);
        assert_eq!(tb[0].dir, "ltr");
        assert_eq!(tb[0].text, "ABC");
    }

    #[test]
    fn mixed_vertical_and_horizontal() {
        let fs = 10.0;
        let adv = 5.0;
        let p = TextPage {
            width: 600.0,
            height: 800.0,
            fonts: vec![font(false), font(true)],
            chars: vec![
                {
                    let mut c = ch("H", 0.0, 100.0, adv, 0.0, fs);
                    c.font = 0;
                    c
                },
                {
                    let mut c = ch("i", adv, 100.0, adv, 0.0, fs);
                    c.font = 0;
                    c
                },
                ch_at("", 200.0, 50.0, 0.0, 10.0, fs, 1, true),
                ch_at("", 200.0, 60.0, 0.0, 10.0, fs, 1, true),
            ],
        };
        let blocks = build_blocks(&p, &[]);
        let tb = text_blocks(&blocks);
        assert_eq!(tb.len(), 2);
        // 表示座標 top で読む: 縦書き top=50 が先
        assert_eq!(tb[0].dir, "ttb");
        assert_eq!(tb[0].text, "あい");
        assert_eq!(tb[1].dir, "ltr");
        assert_eq!(tb[1].text, "Hi");
    }

    #[test]
    fn json_kind_and_type_fields() {
        let fs = 10.0;
        let p = page(vec![ch("A", 0.0, 100.0, 5.0, 0.0, fs)]);
        let blocks = build_blocks(&p, &[]);
        let json = serde_json::to_string(&blocks[0]).unwrap();
        assert!(json.contains(r#""type":"text""#));
        assert!(json.contains(r#""kind":"paragraph""#));
        assert!(!json.contains(r#""level""#));
    }

    #[test]
    fn json_heading_shape_for_type() {
        let tb = TextBlock {
            kind: TextBlockKind::Heading { level: 2 },
            role: TextBlockRole::Body,
            text: "Title".into(),
            left: 0.0,
            right: 10.0,
            top: 0.0,
            bottom: 10.0,
            dir: "ltr".into(),
            rot: 0,
            lines: vec![],
        };
        let json = serde_json::to_string(&DocBlock::Text(tb)).unwrap();
        assert!(json.contains(r#""type":"text""#));
        assert!(json.contains(r#""kind":"heading""#));
        assert!(json.contains(r#""level":2"#));
    }

    #[test]
    fn json_list_shape_for_type() {
        let tb = TextBlock {
            kind: TextBlockKind::List { ordered: false },
            role: TextBlockRole::Body,
            text: "a\nb".into(),
            left: 0.0,
            right: 10.0,
            top: 0.0,
            bottom: 10.0,
            dir: "ltr".into(),
            rot: 0,
            lines: vec![],
        };
        let json = serde_json::to_string(&DocBlock::Text(tb)).unwrap();
        assert!(json.contains(r#""kind":"list""#));
        assert!(json.contains(r#""ordered":false"#));
    }

    #[test]
    fn content_view_drops_chars_and_fonts() {
        let fs = 10.0;
        let p = page(vec![ch("A", 0.0, 100.0, 5.0, 0.0, fs)]);
        let blocks = build_blocks(&p, &[]);
        let doc = DocDoc {
            pages: vec![DocPage {
                index: 0,
                width: p.width,
                height: p.height,
                fonts: p.fonts.clone(),
                chars: p.chars.clone(),
                blocks,
            }],
            warnings: vec!["w".to_string()],
        };
        let content = doc.into_content();
        let json = serde_json::to_string(&content).unwrap();
        assert!(!json.contains(r#""chars""#));
        assert!(!json.contains(r#""fonts""#));
        assert!(json.contains(r#""type":"text""#));
        let ContentBlock::Text(tb) = &content.pages[0].blocks[0] else {
            panic!("expected text block");
        };
        assert_eq!(tb.text, "A");
        assert_eq!(tb.lines.len(), 1);
        assert_eq!(tb.lines[0].text, "A");
        assert_eq!(content.warnings, vec!["w".to_string()]);
    }

    /// リスト検出テスト用の行組み立て
    fn push_list_line(
        chars: &mut Vec<TextChar>,
        words: &[(&str, f64)],
        top: f64,
        fs: f64,
    ) -> TextLine {
        let bottom = top + fs;
        let mut line_words = Vec::new();
        let mut line_chars = Vec::new();
        for &(text, left) in words {
            let mut w_chars = Vec::new();
            let mut x = left;
            let adv = fs * 0.5;
            for c in text.chars() {
                let s = c.to_string();
                let idx = chars.len() as u32;
                chars.push(TextChar {
                    text: s,
                    left: x,
                    right: x + adv,
                    top,
                    bottom,
                    transform: [fs, 0.0, 0.0, -fs, x, top],
                    advance: [adv, 0.0],
                    glyph_width: None,
                    font: 0,
                    font_size: fs,
                    rot: 0,
                    upright: true,
                    synthetic: false,
                });
                w_chars.push(idx);
                line_chars.push(idx);
                x += adv;
            }
            let right = if w_chars.is_empty() {
                left
            } else {
                chars[*w_chars.last().unwrap() as usize].right
            };
            line_words.push(TextWord {
                text: text.into(),
                left,
                right,
                top,
                bottom,
                chars: w_chars,
            });
        }
        let left = line_words
            .iter()
            .map(|w| w.left)
            .fold(f64::INFINITY, f64::min);
        let right = line_words
            .iter()
            .map(|w| w.right)
            .fold(f64::NEG_INFINITY, f64::max);
        TextLine {
            left,
            right,
            top,
            bottom,
            dir: "ltr".into(),
            rot: 0,
            words: line_words,
            chars: line_chars,
        }
    }

    fn para_from_lines(lines: Vec<TextLine>) -> DocBlock {
        DocBlock::Text(make_text_block(lines))
    }

    fn doc_from_para_lines(chars: Vec<TextChar>, lines: Vec<TextLine>) -> DocDoc {
        DocDoc {
            pages: vec![DocPage {
                index: 0,
                width: 600.0,
                height: 800.0,
                fonts: vec![],
                chars,
                blocks: vec![para_from_lines(lines)],
            }],
            warnings: Vec::new(),
        }
    }

    #[test]
    fn list_two_bullet_items_become_list() {
        let fs = 10.0;
        let mut chars = Vec::new();
        let lines = vec![
            push_list_line(
                &mut chars,
                &[("\u{2022}", 50.0), ("Alpha", 60.0)],
                100.0,
                fs,
            ),
            push_list_line(&mut chars, &[("\u{2022}", 50.0), ("Beta", 60.0)], 115.0, fs),
        ];
        let mut doc = doc_from_para_lines(chars, lines);
        assign_lists(&mut doc);
        let tb = text_blocks(&doc.pages[0].blocks);
        assert_eq!(tb.len(), 1);
        assert_eq!(tb[0].kind, TextBlockKind::List { ordered: false });
        assert_eq!(tb[0].text, "Alpha\nBeta");
        assert_eq!(document_to_markdown(&doc, true), "- Alpha\n- Beta");
    }

    #[test]
    fn list_single_bullet_stays_paragraph() {
        let fs = 10.0;
        let mut chars = Vec::new();
        let lines = vec![push_list_line(
            &mut chars,
            &[("\u{2022}", 50.0), ("Only", 60.0)],
            100.0,
            fs,
        )];
        let mut doc = doc_from_para_lines(chars, lines);
        assign_lists(&mut doc);
        let tb = text_blocks(&doc.pages[0].blocks);
        assert_eq!(tb.len(), 1);
        assert_eq!(tb[0].kind, TextBlockKind::Paragraph);
        assert!(tb[0].text.contains("Only"));
        assert_eq!(document_to_markdown(&doc, true), "• Only");
    }

    #[test]
    fn list_hanging_continuation_hard_break() {
        let fs = 10.0;
        let mut chars = Vec::new();
        let lines = vec![
            push_list_line(
                &mut chars,
                &[("\u{2022}", 50.0), ("First", 60.0)],
                100.0,
                fs,
            ),
            push_list_line(&mut chars, &[("line", 58.0)], 112.0, fs),
            push_list_line(
                &mut chars,
                &[("\u{2022}", 50.0), ("Second", 60.0)],
                124.0,
                fs,
            ),
        ];
        let mut doc = doc_from_para_lines(chars, lines);
        assign_lists(&mut doc);
        let tb = text_blocks(&doc.pages[0].blocks);
        assert_eq!(tb.len(), 1);
        assert_eq!(tb[0].kind, TextBlockKind::List { ordered: false });
        assert_eq!(tb[0].text, "First line\nSecond");
        assert_eq!(
            document_to_markdown(&doc, true),
            "- First  \nline\n- Second"
        );
    }

    #[test]
    fn list_different_indent_not_merged() {
        let fs = 10.0;
        let mut chars = Vec::new();
        let lines = vec![
            push_list_line(&mut chars, &[("\u{2022}", 50.0), ("A", 60.0)], 100.0, fs),
            push_list_line(&mut chars, &[("\u{2022}", 80.0), ("B", 90.0)], 115.0, fs),
        ];
        let mut doc = doc_from_para_lines(chars, lines);
        assign_lists(&mut doc);
        let tb = text_blocks(&doc.pages[0].blocks);
        assert!(tb.iter().all(|t| t.kind == TextBlockKind::Paragraph));
    }

    #[test]
    fn list_ordered_number_detected() {
        let fs = 10.0;
        let mut chars = Vec::new();
        let lines = vec![
            push_list_line(&mut chars, &[("1.", 50.0), ("One", 70.0)], 100.0, fs),
            push_list_line(&mut chars, &[("2.", 50.0), ("Two", 70.0)], 115.0, fs),
        ];
        let mut doc = doc_from_para_lines(chars, lines);
        assign_lists(&mut doc);
        let tb = text_blocks(&doc.pages[0].blocks);
        assert_eq!(tb[0].kind, TextBlockKind::List { ordered: true });
        assert_eq!(tb[0].text, "One\nTwo");
        assert_eq!(document_to_markdown(&doc, true), "1. One\n2. Two");
    }

    #[test]
    fn list_ordered_section_number_not_marker() {
        let fs = 10.0;
        let mut chars = Vec::new();
        let lines = vec![
            push_list_line(&mut chars, &[("1.1", 50.0), ("Sec", 80.0)], 100.0, fs),
            push_list_line(&mut chars, &[("1.2", 50.0), ("Sub", 80.0)], 115.0, fs),
        ];
        let mut doc = doc_from_para_lines(chars, lines);
        assign_lists(&mut doc);
        let tb = text_blocks(&doc.pages[0].blocks);
        assert!(tb.iter().all(|t| t.kind == TextBlockKind::Paragraph));
    }

    #[test]
    fn list_ordered_non_consecutive_not_list() {
        let fs = 10.0;
        let mut chars = Vec::new();
        let lines = vec![
            push_list_line(&mut chars, &[("1.", 50.0), ("A", 70.0)], 100.0, fs),
            push_list_line(&mut chars, &[("3.", 50.0), ("C", 70.0)], 115.0, fs),
        ];
        let mut doc = doc_from_para_lines(chars, lines);
        assign_lists(&mut doc);
        let tb = text_blocks(&doc.pages[0].blocks);
        assert!(tb.iter().all(|t| t.kind == TextBlockKind::Paragraph));
    }

    #[test]
    fn list_ordered_repeat_then_consecutive() {
        let fs = 10.0;
        let mut chars = Vec::new();
        let lines = vec![
            push_list_line(&mut chars, &[("1.", 50.0), ("A", 70.0)], 100.0, fs),
            push_list_line(&mut chars, &[("1.", 50.0), ("B", 70.0)], 115.0, fs),
            push_list_line(&mut chars, &[("2.", 50.0), ("C", 70.0)], 130.0, fs),
        ];
        let mut doc = doc_from_para_lines(chars, lines);
        assign_lists(&mut doc);
        let tb = text_blocks(&doc.pages[0].blocks);
        assert!(
            tb.iter()
                .any(|t| t.kind == TextBlockKind::List { ordered: true })
        );
        let list = tb
            .iter()
            .find(|t| t.kind == TextBlockKind::List { ordered: true })
            .unwrap();
        assert_eq!(list.text, "B\nC");
    }

    #[test]
    fn list_paren_number_detected() {
        let fs = 10.0;
        let mut chars = Vec::new();
        let lines = vec![
            push_list_line(&mut chars, &[("1)", 50.0), ("One", 70.0)], 100.0, fs),
            push_list_line(&mut chars, &[("2)", 50.0), ("Two", 70.0)], 115.0, fs),
        ];
        let mut doc = doc_from_para_lines(chars, lines);
        assign_lists(&mut doc);
        let tb = text_blocks(&doc.pages[0].blocks);
        assert_eq!(tb[0].kind, TextBlockKind::List { ordered: true });
        assert_eq!(tb[0].text, "One\nTwo");
        assert_eq!(document_to_markdown(&doc, true), "1. One\n2. Two");
    }

    #[test]
    fn list_lparen_number_detected() {
        let fs = 10.0;
        let mut chars = Vec::new();
        let lines = vec![
            push_list_line(&mut chars, &[("(1)", 50.0), ("One", 70.0)], 100.0, fs),
            push_list_line(&mut chars, &[("(2)", 50.0), ("Two", 70.0)], 115.0, fs),
        ];
        let mut doc = doc_from_para_lines(chars, lines);
        assign_lists(&mut doc);
        let tb = text_blocks(&doc.pages[0].blocks);
        assert_eq!(tb[0].kind, TextBlockKind::List { ordered: true });
        assert_eq!(document_to_markdown(&doc, true), "1. One\n2. Two");
    }

    #[test]
    fn list_ordered_mixed_style_not_list() {
        let fs = 10.0;
        let mut chars = Vec::new();
        let lines = vec![
            push_list_line(&mut chars, &[("1.", 50.0), ("One", 70.0)], 100.0, fs),
            push_list_line(&mut chars, &[("(2)", 50.0), ("Two", 70.0)], 115.0, fs),
            push_list_line(&mut chars, &[("2)", 50.0), ("Three", 70.0)], 130.0, fs),
        ];
        let mut doc = doc_from_para_lines(chars, lines);
        assign_lists(&mut doc);
        let tb = text_blocks(&doc.pages[0].blocks);
        assert!(tb.iter().all(|t| t.kind == TextBlockKind::Paragraph));
    }

    #[test]
    fn list_ascii_dash_two_items() {
        let fs = 10.0;
        let mut chars = Vec::new();
        let lines = vec![
            push_list_line(&mut chars, &[("-", 40.0), ("red", 50.0)], 100.0, fs),
            push_list_line(&mut chars, &[("-", 40.0), ("blue", 50.0)], 115.0, fs),
        ];
        let mut doc = doc_from_para_lines(chars, lines);
        assign_lists(&mut doc);
        let tb = text_blocks(&doc.pages[0].blocks);
        assert_eq!(tb[0].kind, TextBlockKind::List { ordered: false });
        assert_eq!(document_to_markdown(&doc, true), "- red\n- blue");
    }

    #[test]
    fn list_item_body_escape_leading_hash() {
        let fs = 10.0;
        let mut chars = Vec::new();
        let lines = vec![
            push_list_line(&mut chars, &[("\u{2022}", 50.0), ("#tag", 60.0)], 100.0, fs),
            push_list_line(&mut chars, &[("\u{2022}", 50.0), ("ok", 60.0)], 115.0, fs),
        ];
        let mut doc = doc_from_para_lines(chars, lines);
        assign_lists(&mut doc);
        assert_eq!(document_to_markdown(&doc, true), "- \\#tag\n- ok");
    }

    #[test]
    fn list_star_footnote_runs_not_list_when_glued() {
        let fs = 10.0;
        let mut chars = Vec::new();
        let lines = vec![
            push_list_line(&mut chars, &[("*****", 50.0), ("Note", 80.0)], 100.0, fs),
            push_list_line(&mut chars, &[("*****", 50.0), ("More", 80.0)], 115.0, fs),
        ];
        let mut doc = doc_from_para_lines(chars, lines);
        assign_lists(&mut doc);
        let tb = text_blocks(&doc.pages[0].blocks);
        assert!(tb.iter().all(|t| t.kind == TextBlockKind::Paragraph));
    }

    #[test]
    fn list_before_heading_prevents_list_as_heading() {
        let fs = 10.0;
        let mut chars = Vec::new();
        let lines = vec![
            push_list_line(
                &mut chars,
                &[("\u{2022}", 50.0), ("BigA", 60.0)],
                40.0,
                20.0,
            ),
            push_list_line(
                &mut chars,
                &[("\u{2022}", 50.0), ("BigB", 60.0)],
                65.0,
                20.0,
            ),
            push_list_line(&mut chars, &[("body", 50.0)], 200.0, fs),
            push_list_line(&mut chars, &[("text", 50.0)], 215.0, fs),
            push_list_line(&mut chars, &[("here", 50.0)], 230.0, fs),
            push_list_line(&mut chars, &[("more", 50.0)], 245.0, fs),
            push_list_line(&mut chars, &[("lines", 50.0)], 260.0, fs),
        ];
        let mut doc = doc_from_para_lines(chars, lines);
        assign_lists(&mut doc);
        assign_headings(&mut doc);
        let kinds = text_kinds(&doc);
        let list = kinds.iter().find(|(t, _)| t.contains("BigA")).unwrap();
        assert_eq!(list.1, TextBlockKind::List { ordered: false });
    }

    /// マーカー無しブロックだけの Doc を組み立てる
    fn doc_from_blocks(chars: Vec<TextChar>, blocks: Vec<DocBlock>) -> DocDoc {
        DocDoc {
            pages: vec![DocPage {
                index: 0,
                width: 600.0,
                height: 800.0,
                fonts: vec![],
                chars,
                blocks,
            }],
            warnings: Vec::new(),
        }
    }

    #[test]
    fn assign_lists_preserves_non_marker_input_verbatim() {
        // リストが 1 つも無い入力で assign_lists 前後の Doc JSON が完全一致することを確認する
        let fs = 10.0;
        let mut chars = Vec::new();
        let l1 = push_list_line(&mut chars, &[("hello", 50.0)], 100.0, fs);
        let l2 = push_list_line(&mut chars, &[("world", 50.0)], 115.0, fs);
        let l3 = push_list_line(&mut chars, &[("goodbye", 50.0)], 200.0, fs);
        let blocks = vec![
            para_from_lines(vec![l1, l2]),
            para_from_lines(vec![l3]),
        ];
        let mut doc = doc_from_blocks(chars, blocks);
        let before = serde_json::to_string(&doc).unwrap();
        assign_lists(&mut doc);
        let after = serde_json::to_string(&doc).unwrap();
        assert_eq!(before, after);
    }

    #[test]
    fn assign_lists_keeps_two_independent_paragraphs() {
        // マーカーが 1 つだけ含まれても List 不成立なら元の段落境界を保つ
        let fs = 10.0;
        let mut chars = Vec::new();
        let l1 = push_list_line(&mut chars, &[("first", 50.0)], 100.0, fs);
        let l2 = push_list_line(&mut chars, &[("para", 50.0)], 115.0, fs);
        let l3 = push_list_line(&mut chars, &[("second", 50.0)], 200.0, fs);
        let l4 = push_list_line(&mut chars, &[("para", 50.0)], 215.0, fs);
        let blocks = vec![
            para_from_lines(vec![l1, l2]),
            para_from_lines(vec![l3, l4]),
        ];
        let mut doc = doc_from_blocks(chars, blocks);
        assign_lists(&mut doc);
        let tb = text_blocks(&doc.pages[0].blocks);
        assert_eq!(tb.len(), 2);
        assert_eq!(tb[0].text, "first para");
        assert_eq!(tb[1].text, "second para");
    }

    #[test]
    fn heading_size_paragraph_survives_assign_lists() {
        // タイトルサイズと本文サイズの独立ブロックを並べたページで、
        // assign_lists がブロック境界を潰さず assign_headings がタイトルを Heading にする
        let title_fs = 18.0;
        let body_fs = 10.0;
        let mut chars = Vec::new();
        let title = push_list_line(&mut chars, &[("Title", 50.0)], 40.0, title_fs);
        let b1 = push_list_line(&mut chars, &[("body1", 50.0)], 100.0, body_fs);
        let b2 = push_list_line(&mut chars, &[("body2", 50.0)], 115.0, body_fs);
        let blocks = vec![
            para_from_lines(vec![title]),
            para_from_lines(vec![b1, b2]),
        ];
        let mut doc = doc_from_blocks(chars, blocks);
        assign_lists(&mut doc);
        assign_headings(&mut doc);
        let tb = text_blocks(&doc.pages[0].blocks);
        assert_eq!(tb.len(), 2);
        assert_eq!(tb[0].kind, TextBlockKind::Heading { level: 1 });
        assert_eq!(tb[0].text, "Title");
        assert_eq!(tb[1].kind, TextBlockKind::Paragraph);
    }

    #[test]
    fn list_run_breaks_on_rot_mismatch() {
        // (dir, rot) が異なる隣接ブロックはランに含めない
        let fs = 10.0;
        let mut chars = Vec::new();
        let mut a = push_list_line(&mut chars, &[("\u{2022}", 50.0), ("A", 60.0)], 100.0, fs);
        let mut b = push_list_line(&mut chars, &[("\u{2022}", 50.0), ("B", 60.0)], 115.0, fs);
        a.rot = 0;
        b.rot = 90;
        let blocks = vec![
            para_from_lines(vec![a]),
            para_from_lines(vec![b]),
        ];
        let mut doc = doc_from_blocks(chars, blocks);
        assign_lists(&mut doc);
        let tb = text_blocks(&doc.pages[0].blocks);
        assert_eq!(tb.len(), 2);
        assert!(tb.iter().all(|t| t.kind == TextBlockKind::Paragraph));
    }

    #[test]
    fn list_splits_block_middle_around_list_span() {
        // 1 ブロックの中央にリスト成立区間があるときブロックを前後に切る
        let fs = 10.0;
        let mut chars = Vec::new();
        let intro = push_list_line(&mut chars, &[("intro", 50.0)], 100.0, fs);
        let b1 = push_list_line(&mut chars, &[("\u{2022}", 50.0), ("A", 60.0)], 115.0, fs);
        let b2 = push_list_line(&mut chars, &[("\u{2022}", 50.0), ("B", 60.0)], 130.0, fs);
        let outro = push_list_line(&mut chars, &[("outro", 50.0)], 145.0, fs);
        let mut doc = doc_from_para_lines(chars, vec![intro, b1, b2, outro]);
        assign_lists(&mut doc);
        let tb = text_blocks(&doc.pages[0].blocks);
        assert_eq!(tb.len(), 3);
        assert_eq!(tb[0].kind, TextBlockKind::Paragraph);
        assert_eq!(tb[0].text, "intro");
        assert_eq!(tb[1].kind, TextBlockKind::List { ordered: false });
        assert_eq!(tb[1].text, "A\nB");
        assert_eq!(tb[2].kind, TextBlockKind::Paragraph);
        assert_eq!(tb[2].text, "outro");
    }

    /// 本文と候補見出しから DocDoc を組み立てる
    fn doc_from_body_and_titles(body_fs: f64, body_text: &str, titles: &[(f64, &str)]) -> DocDoc {
        let mut chars = Vec::new();
        // 本文を複数グリフで最頻値を確保
        let mut y = 200.0;
        for _ in 0..5 {
            chars.extend(word_line(body_text, 0.0, y, body_fs));
            y += body_fs * 2.5; // 行送りを大きくして段落分割
        }
        y = 20.0;
        for &(fs, text) in titles {
            chars.extend(word_line(text, 0.0, y, fs));
            y += fs * 3.0;
        }
        let p = page(chars);
        let blocks = build_blocks(&p, &[]);
        DocDoc {
            pages: vec![DocPage {
                index: 0,
                width: p.width,
                height: p.height,
                fonts: p.fonts,
                chars: p.chars,
                blocks,
            }],
            warnings: Vec::new(),
        }
    }

    fn text_kinds(doc: &DocDoc) -> Vec<(&str, TextBlockKind)> {
        doc.pages
            .iter()
            .flat_map(|p| p.blocks.iter())
            .filter_map(|b| match b {
                DocBlock::Text(t) => Some((t.text.as_str(), t.kind.clone())),
                _ => None,
            })
            .collect()
    }

    #[test]
    fn heading_level1_at_1_8x() {
        // 本文 10、見出し 18 → level 1
        let mut doc = doc_from_body_and_titles(10.0, "body", &[(18.0, "H1")]);
        assign_headings(&mut doc);
        let kinds = text_kinds(&doc);
        let h1 = kinds.iter().find(|(t, _)| *t == "H1").unwrap();
        assert_eq!(h1.1, TextBlockKind::Heading { level: 1 });
    }

    #[test]
    fn heading_level2_at_1_4x() {
        // 本文 10、見出し 14 → level 2(1.4 以上 1.8 未満)
        let mut doc = doc_from_body_and_titles(10.0, "body", &[(14.0, "H2")]);
        assign_headings(&mut doc);
        let kinds = text_kinds(&doc);
        let h2 = kinds.iter().find(|(t, _)| *t == "H2").unwrap();
        assert_eq!(h2.1, TextBlockKind::Heading { level: 2 });
    }

    #[test]
    fn heading_level3_at_1_2x() {
        // 本文 10、見出し 12 → level 3(1.2 以上 1.4 未満)
        let mut doc = doc_from_body_and_titles(10.0, "body", &[(12.0, "H3")]);
        assign_headings(&mut doc);
        let kinds = text_kinds(&doc);
        let h3 = kinds.iter().find(|(t, _)| *t == "H3").unwrap();
        assert_eq!(h3.1, TextBlockKind::Heading { level: 3 });
    }

    #[test]
    fn heading_below_1_2x_stays_paragraph() {
        // 本文と同サイズは Paragraph のまま
        let mut doc = doc_from_body_and_titles(10.0, "body", &[(10.0, "same")]);
        assign_headings(&mut doc);
        let kinds = text_kinds(&doc);
        let same = kinds.iter().find(|(t, _)| *t == "same").unwrap();
        assert_eq!(same.1, TextBlockKind::Paragraph);
        for (t, k) in &kinds {
            if *t == "body" {
                assert_eq!(*k, TextBlockKind::Paragraph);
            }
        }
    }

    #[test]
    fn heading_skips_blocks_with_3_plus_lines() {
        // 3 行以上の大サイズブロックは対象外
        let fs_body = 10.0;
        let fs_big = 20.0;
        let mut chars = Vec::new();
        let mut y = 200.0;
        for _ in 0..5 {
            chars.extend(word_line("body", 0.0, y, fs_body));
            y += fs_body * 2.5;
        }
        // 大サイズ 3 行(行送り 15 ≤ 1.8*20 で 1 ブロックに併合)
        chars.extend(word_line("AAA", 0.0, 20.0, fs_big));
        chars.extend(word_line("BBB", 0.0, 35.0, fs_big));
        chars.extend(word_line("CCC", 0.0, 50.0, fs_big));
        let p = page(chars);
        let blocks = build_blocks(&p, &[]);
        let mut doc = DocDoc {
            pages: vec![DocPage {
                index: 0,
                width: p.width,
                height: p.height,
                fonts: p.fonts,
                chars: p.chars,
                blocks,
            }], warnings: Vec::new(),
        };
        // 3 行ブロックがあること
        let multi = text_blocks(&doc.pages[0].blocks)
            .into_iter()
            .find(|t| t.lines.len() >= 3)
            .expect("3-line block");
        assert_eq!(multi.lines.len(), 3);
        assign_headings(&mut doc);
        let multi = text_blocks(&doc.pages[0].blocks)
            .into_iter()
            .find(|t| t.lines.len() >= 3)
            .unwrap();
        assert_eq!(multi.kind, TextBlockKind::Paragraph);
    }

    #[test]
    fn heading_mode_tie_picks_smaller() {
        // 10.0 と 12.0 が同数 → 本文代表は 10.0
        // 12.0 の単独ブロックは 1.2 倍ちょうど → level 3
        let mut chars = Vec::new();
        let mut y = 100.0;
        for _ in 0..3 {
            chars.extend(word_line("aaaa", 0.0, y, 10.0));
            y += 30.0;
        }
        for _ in 0..3 {
            chars.extend(word_line("bbbb", 0.0, y, 12.0));
            y += 30.0;
        }
        // 候補: 18 → 1.8*10 = level 1(代表が 12 だと 1.5 で level 2 になる)
        chars.extend(word_line("TITLE", 0.0, 20.0, 18.0));
        let p = page(chars);
        let blocks = build_blocks(&p, &[]);
        let mut doc = DocDoc {
            pages: vec![DocPage {
                index: 0,
                width: p.width,
                height: p.height,
                fonts: p.fonts,
                chars: p.chars,
                blocks,
            }], warnings: Vec::new(),
        };
        assign_headings(&mut doc);
        let kinds = text_kinds(&doc);
        let title = kinds.iter().find(|(t, _)| *t == "TITLE").unwrap();
        assert_eq!(
            title.1,
            TextBlockKind::Heading { level: 1 },
            "tie should pick smaller body fs (10), so 18 is level 1"
        );
    }

    #[test]
    fn heading_no_body_samples_is_noop() {
        // 本文ブロックゼロ(表のみ)では何もしない
        let t = table_one(
            vec![vec![cell("X", 0.0, 0.0, 10.0, 10.0)]],
            0.0,
            0.0,
            10.0,
            10.0,
        );
        let p = page(vec![]);
        let blocks = build_blocks(&p, &[t.clone()]);
        let mut doc = DocDoc {
            pages: vec![DocPage {
                index: 0,
                width: p.width,
                height: p.height,
                fonts: p.fonts,
                chars: p.chars,
                blocks,
            }], warnings: Vec::new(),
        };
        // 手動で Heading 相当の Text を足さず、表のみ
        assert!(text_blocks(&doc.pages[0].blocks).is_empty());
        assign_headings(&mut doc);
        assert_eq!(doc.pages[0].blocks.len(), 1);
        assert!(matches!(doc.pages[0].blocks[0], DocBlock::Table(_)));
    }

    #[test]
    fn heading_ignores_table_glyphs_for_body_mode() {
        // 表内の大サイズは母集団に入らない
        let fs_body = 10.0;
        let fs_table = 30.0;
        let mut chars = word_line("body", 0.0, 200.0, fs_body);
        chars.extend(word_line("body", 0.0, 230.0, fs_body));
        chars.extend(word_line("body", 0.0, 260.0, fs_body));
        // 表セル内の大文字
        chars.extend(word_line("TAB", 10.0, 100.0, fs_table));
        // 見出し候補 14 → 本文 10 なら level 2
        chars.extend(word_line("Head", 0.0, 20.0, 14.0));
        let p = page(chars);
        let t = table_one(
            vec![vec![cell("TAB", 5.0, 95.0, 80.0, 140.0)]],
            5.0,
            95.0,
            80.0,
            140.0,
        );
        let blocks = build_blocks(&p, &[t]);
        let mut doc = DocDoc {
            pages: vec![DocPage {
                index: 0,
                width: p.width,
                height: p.height,
                fonts: p.fonts,
                chars: p.chars,
                blocks,
            }], warnings: Vec::new(),
        };
        assign_headings(&mut doc);
        let kinds = text_kinds(&doc);
        let head = kinds.iter().find(|(t, _)| *t == "Head").unwrap();
        assert_eq!(
            head.1,
            TextBlockKind::Heading { level: 2 },
            "table large glyphs must not shift body mode"
        );
    }

    fn text_block(kind: TextBlockKind, text: &str) -> DocBlock {
        DocBlock::Text(TextBlock {
            kind,
            role: TextBlockRole::Body,
            text: text.into(),
            left: 0.0,
            right: 10.0,
            top: 0.0,
            bottom: 10.0,
            dir: "ltr".into(),
            rot: 0,
            lines: vec![],
        })
    }

    fn doc_with_blocks(blocks: Vec<DocBlock>) -> DocDoc {
        DocDoc {
            pages: vec![DocPage {
                index: 0,
                width: 600.0,
                height: 800.0,
                fonts: vec![],
                chars: vec![],
                blocks,
            }],
            warnings: Vec::new(),
        }
    }

    #[test]
    fn markdown_heading_and_paragraph() {
        let doc = doc_with_blocks(vec![
            text_block(TextBlockKind::Heading { level: 1 }, "Title"),
            text_block(TextBlockKind::Heading { level: 2 }, "Section"),
            text_block(TextBlockKind::Heading { level: 3 }, "Sub"),
            text_block(TextBlockKind::Paragraph, "body text"),
        ]);
        assert_eq!(
            document_to_markdown(&doc, true),
            "# Title\n\n## Section\n\n### Sub\n\nbody text"
        );
    }

    #[test]
    fn markdown_gfm_table_shape() {
        let t = Table {
            extraction_method: "lattice",
            bbox: BBox {
                x0: 0.0,
                top: 0.0,
                x1: 100.0,
                bottom: 50.0,
            },
            n_rows: 3,
            n_cols: 2,
            data: vec![
                vec![
                    cell("H1", 0.0, 0.0, 10.0, 10.0),
                    cell("H2", 10.0, 0.0, 20.0, 10.0),
                ],
                vec![
                    cell("A", 0.0, 10.0, 10.0, 20.0),
                    cell("B", 10.0, 10.0, 20.0, 20.0),
                ],
                vec![
                    cell("C", 0.0, 20.0, 10.0, 30.0),
                    cell("D", 10.0, 20.0, 20.0, 30.0),
                ],
            ],
        };
        let doc = doc_with_blocks(vec![DocBlock::Table(t)]);
        assert_eq!(
            document_to_markdown(&doc, true),
            "| H1 | H2 |\n| --- | --- |\n| A | B |\n| C | D |"
        );
    }

    #[test]
    fn markdown_table_single_row_header_only() {
        let t = Table {
            extraction_method: "lattice",
            bbox: BBox {
                x0: 0.0,
                top: 0.0,
                x1: 50.0,
                bottom: 20.0,
            },
            n_rows: 1,
            n_cols: 2,
            data: vec![vec![
                cell("Only", 0.0, 0.0, 10.0, 10.0),
                cell("Header", 10.0, 0.0, 20.0, 10.0),
            ]],
        };
        let doc = doc_with_blocks(vec![DocBlock::Table(t)]);
        assert_eq!(
            document_to_markdown(&doc, true),
            "| Only | Header |\n| --- | --- |"
        );
    }

    #[test]
    fn markdown_table_empty_cells_from_merge() {
        // 結合セル由来の空セル
        let t = Table {
            extraction_method: "lattice",
            bbox: BBox {
                x0: 0.0,
                top: 0.0,
                x1: 50.0,
                bottom: 30.0,
            },
            n_rows: 2,
            n_cols: 2,
            data: vec![
                vec![
                    cell("H", 0.0, 0.0, 10.0, 10.0),
                    cell("", 10.0, 0.0, 20.0, 10.0),
                ],
                vec![
                    cell("", 0.0, 10.0, 10.0, 20.0),
                    cell("V", 10.0, 10.0, 20.0, 20.0),
                ],
            ],
        };
        let doc = doc_with_blocks(vec![DocBlock::Table(t)]);
        assert_eq!(
            document_to_markdown(&doc, true),
            "| H |  |\n| --- | --- |\n|  | V |"
        );
    }

    #[test]
    fn markdown_table_pad_and_truncate_cells() {
        let t = Table {
            extraction_method: "lattice",
            bbox: BBox {
                x0: 0.0,
                top: 0.0,
                x1: 50.0,
                bottom: 30.0,
            },
            n_rows: 2,
            n_cols: 2,
            data: vec![
                // 不足: 1 セル → 空セル埋め
                vec![cell("H", 0.0, 0.0, 10.0, 10.0)],
                // 超過: 3 セル → n_cols で打ち切り
                vec![
                    cell("A", 0.0, 10.0, 10.0, 20.0),
                    cell("B", 10.0, 10.0, 20.0, 20.0),
                    cell("DROP", 20.0, 10.0, 30.0, 20.0),
                ],
            ],
        };
        let doc = doc_with_blocks(vec![DocBlock::Table(t)]);
        assert_eq!(
            document_to_markdown(&doc, true),
            "| H |  |\n| --- | --- |\n| A | B |"
        );
    }

    #[test]
    fn markdown_escape_backslash_before_pipe() {
        // `\` を先に `\\` へ、その後 `|` を `\|` へ
        let t = Table {
            extraction_method: "lattice",
            bbox: BBox {
                x0: 0.0,
                top: 0.0,
                x1: 50.0,
                bottom: 20.0,
            },
            n_rows: 1,
            n_cols: 1,
            data: vec![vec![cell(r"a\b|c", 0.0, 0.0, 10.0, 10.0)]],
        };
        let doc = doc_with_blocks(vec![DocBlock::Table(t)]);
        assert_eq!(document_to_markdown(&doc, true), "| a\\\\b\\|c |\n| --- |");
    }

    #[test]
    fn markdown_control_chars_to_space() {
        let mixed = "a\nb\rc\u{2028}d\u{2029}e\tf\u{0001}g";
        let doc = doc_with_blocks(vec![
            text_block(TextBlockKind::Heading { level: 1 }, mixed),
            text_block(TextBlockKind::Paragraph, mixed),
            DocBlock::Table(Table {
                extraction_method: "lattice",
                bbox: BBox {
                    x0: 0.0,
                    top: 0.0,
                    x1: 20.0,
                    bottom: 20.0,
                },
                n_rows: 1,
                n_cols: 1,
                data: vec![vec![cell(mixed, 0.0, 0.0, 10.0, 10.0)]],
            }),
        ]);
        let md = document_to_markdown(&doc, true);
        assert_eq!(
            md,
            "# a b c d e f g\n\na b c d e f g\n\n| a b c d e f g |\n| --- |"
        );
        assert!(!md.contains('\t'));
        assert!(!md.contains('\u{2028}'));
        assert!(!md.contains('\u{2029}'));
        assert!(!md.contains('\u{0001}'));
    }

    #[test]
    fn markdown_escape_leading_block_markers() {
        let doc = doc_with_blocks(vec![
            text_block(TextBlockKind::Paragraph, "# not a heading"),
            text_block(TextBlockKind::Paragraph, "  - not a list"),
            text_block(TextBlockKind::Paragraph, "* star"),
            text_block(TextBlockKind::Paragraph, "+ plus"),
            text_block(TextBlockKind::Paragraph, "> quote"),
            text_block(TextBlockKind::Paragraph, "| pipe"),
            text_block(TextBlockKind::Paragraph, "### triple"),
            text_block(TextBlockKind::Paragraph, r"a\b mid"),
            text_block(TextBlockKind::Heading { level: 1 }, "# title-ish"),
            text_block(TextBlockKind::Paragraph, "mid # not escaped"),
        ]);
        assert_eq!(
            document_to_markdown(&doc, true),
            "\\# not a heading\n\n  \\- not a list\n\n\\* star\n\n\\+ plus\n\n\
             \\> quote\n\n\\| pipe\n\n\\### triple\n\na\\\\b mid\n\n# \\# title-ish\n\n\
             mid # not escaped"
        );
    }

    #[test]
    fn markdown_escape_markdown_false_skips_body_escape() {
        let doc = doc_with_blocks(vec![
            text_block(TextBlockKind::Paragraph, "# keep"),
            text_block(TextBlockKind::Paragraph, "- keep"),
            text_block(TextBlockKind::Paragraph, r"a\b"),
            text_block(TextBlockKind::Heading { level: 2 }, "# h"),
            DocBlock::Table(Table {
                extraction_method: "lattice",
                bbox: BBox {
                    x0: 0.0,
                    top: 0.0,
                    x1: 20.0,
                    bottom: 20.0,
                },
                n_rows: 1,
                n_cols: 1,
                data: vec![vec![cell(r"a|b\c", 0.0, 0.0, 10.0, 10.0)]],
            }),
        ]);
        // 本文エスケープはオフ、見出し接頭辞とセル・制御文字は常時
        assert_eq!(
            document_to_markdown(&doc, false),
            "# keep\n\n- keep\n\na\\b\n\n## # h\n\n| a\\|b\\\\c |\n| --- |"
        );
    }

    #[test]
    fn markdown_join_pages_and_blocks() {
        let doc = DocDoc {
            pages: vec![
                DocPage {
                    index: 0,
                    width: 600.0,
                    height: 800.0,
                    fonts: vec![],
                    chars: vec![],
                    blocks: vec![
                        text_block(TextBlockKind::Paragraph, "p1"),
                        text_block(TextBlockKind::Paragraph, "p2"),
                    ],
                },
                DocPage {
                    index: 1,
                    width: 600.0,
                    height: 800.0,
                    fonts: vec![],
                    chars: vec![],
                    blocks: vec![], // 空ページは要素を出さない
                },
                DocPage {
                    index: 2,
                    width: 600.0,
                    height: 800.0,
                    fonts: vec![],
                    chars: vec![],
                    blocks: vec![text_block(TextBlockKind::Paragraph, "p3")],
                },
            ], warnings: Vec::new(),
        };
        let md = document_to_markdown(&doc, true);
        assert_eq!(md, "p1\n\np2\n\np3");
        assert!(!md.ends_with('\n'));
    }

    #[test]
    fn markdown_empty_doc_and_empty_paragraph() {
        let empty = DocDoc { pages: vec![], warnings: Vec::new() };
        assert_eq!(document_to_markdown(&empty, true), "");

        let empty_pages = DocDoc {
            pages: vec![DocPage {
                index: 0,
                width: 1.0,
                height: 1.0,
                fonts: vec![],
                chars: vec![],
                blocks: vec![],
            }], warnings: Vec::new(),
        };
        assert_eq!(document_to_markdown(&empty_pages, true), "");

        // 空段落は非空ブロックではない
        let empty_para = doc_with_blocks(vec![
            text_block(TextBlockKind::Paragraph, ""),
            text_block(TextBlockKind::Paragraph, "x"),
        ]);
        assert_eq!(document_to_markdown(&empty_para, true), "x");
    }

    #[test]
    fn markdown_skips_zero_dim_tables() {
        let zero_rows = Table {
            extraction_method: "lattice",
            bbox: BBox {
                x0: 0.0,
                top: 0.0,
                x1: 10.0,
                bottom: 10.0,
            },
            n_rows: 0,
            n_cols: 2,
            data: vec![],
        };
        let zero_cols = Table {
            extraction_method: "lattice",
            bbox: BBox {
                x0: 0.0,
                top: 0.0,
                x1: 10.0,
                bottom: 10.0,
            },
            n_rows: 2,
            n_cols: 0,
            data: vec![vec![], vec![]],
        };
        let doc = doc_with_blocks(vec![
            DocBlock::Table(zero_rows),
            text_block(TextBlockKind::Paragraph, "keep"),
            DocBlock::Table(zero_cols),
        ]);
        assert_eq!(document_to_markdown(&doc, true), "keep");
    }

    /// 制御文字・空白のみの見出し・段落は Markdown に出さない
    #[test]
    fn markdown_skips_whitespace_only_text_blocks() {
        let doc = doc_with_blocks(vec![
            text_block(TextBlockKind::Heading { level: 1 }, "\t\n\u{0001}"),
            text_block(TextBlockKind::Paragraph, "\r\n  \t"),
            text_block(TextBlockKind::Paragraph, "keep"),
            text_block(TextBlockKind::Heading { level: 2 }, "   "),
            text_block(TextBlockKind::Paragraph, "\u{2028}\u{2029}"),
        ]);
        assert_eq!(document_to_markdown(&doc, true), "keep");
    }

    /// ベースラインの揃った2列ページ(行が列をまたぐ)
    fn two_col_aligned_page() -> TextPage {
        let fs = 10.0;
        let adv = 5.0;
        let mut chars = Vec::new();
        let left = ["A", "B", "C", "D", "E"];
        let right = ["F", "G", "H", "I", "J"];
        for i in 0..5 {
            let y = 20.0 + i as f64 * 15.0;
            chars.push(ch(left[i], 20.0, y, adv, 0.0, fs));
            chars.push(ch(right[i], 100.0, y, adv, 0.0, fs));
        }
        TextPage {
            width: 200.0,
            height: 300.0,
            fonts: vec![font(false)],
            chars,
        }
    }

    #[test]
    fn columns_aligned_two_col_reading_order() {
        let p = two_col_aligned_page();
        let blocks = build_blocks(&p, &[]);
        let tb = text_blocks(&blocks);
        assert_eq!(tb.len(), 2);
        assert_eq!(tb[0].text, "A B C D E");
        assert_eq!(tb[1].text, "F G H I J");
        assert!(tb[0].left < tb[1].left);
    }

    #[test]
    fn columns_line_fragment_whitespace_and_bbox() {
        let fs = 10.0;
        let adv = 5.0;
        let mut chars = Vec::new();
        // ストリーム順と進行軸順が逆転する行:
        // chars 配列は右語 F → 列間空白 → 左語 A → 先頭空白相当の順で積み、
        // 表示 x は A が左・F が右。build_text_lines が進行軸順に並べ替えた
        // line.chars の相対順を断片が保つことを検証する
        // インデックス: 0=F(右), 1=空白(列間), 2=A(左), 3=空白(A 左隣)
        chars.push(ch("F", 100.0, 20.0, adv, 0.0, fs));
        chars.push(ch(" ", 50.0, 20.0, 5.0, 0.0, fs));
        chars.push(ch("A", 20.0, 20.0, adv, 0.0, fs));
        chars.push(ch(" ", 10.0, 20.0, 5.0, 0.0, fs));
        let left = ["B", "C", "D", "E"];
        let right = ["G", "H", "I", "J"];
        for i in 0..4 {
            let y = 20.0 + (i + 1) as f64 * 15.0;
            // 右列を先にストリーム投入(x は左が小さい)
            chars.push(ch(right[i], 100.0, y, adv, 0.0, fs));
            chars.push(ch(left[i], 20.0, y, adv, 0.0, fs));
        }
        let p = TextPage {
            width: 200.0,
            height: 300.0,
            fonts: vec![font(false)],
            chars,
        };
        let blocks = build_blocks(&p, &[]);
        let tb = text_blocks(&blocks);
        assert_eq!(tb.len(), 2);
        assert_eq!(tb[0].text, "A B C D E");
        assert_eq!(tb[1].text, "F G H I J");

        let left0 = &tb[0].lines[0];
        assert_eq!(left0.words.len(), 1);
        assert_eq!(left0.words[0].text, "A");
        // 先頭空白は直後の単語(A)側、列間空白は直前の単語(A)側
        let left_ws: Vec<_> = left0
            .chars
            .iter()
            .filter(|&&i| is_whitespace_str(&p.chars[i as usize].text))
            .copied()
            .collect();
        assert_eq!(left_ws.len(), 2);
        // 進行軸順: 空白(x=10, idx3) → A(idx2) → 空白(x=50, idx1)
        // 断片 chars は元 line.chars の相対順(進行軸順)を保ち、
        // ストリーム index 昇順ではない
        assert_eq!(left0.chars, vec![3, 2, 1]);
        // bbox は非空白のみ(A)
        assert!((left0.left - 20.0).abs() < 1e-9);
        assert!((left0.right - 25.0).abs() < 1e-9);

        let right0 = &tb[1].lines[0];
        assert_eq!(right0.words.len(), 1);
        assert_eq!(right0.words[0].text, "F");
        assert!(
            right0
                .chars
                .iter()
                .all(|&i| !is_whitespace_str(&p.chars[i as usize].text))
        );
        assert_eq!(right0.chars, vec![0]);
        assert!((right0.left - 100.0).abs() < 1e-9);
    }

    #[test]
    fn columns_staggered_two_col_leaf_assign_only() {
        let fs = 10.0;
        let adv = 5.0;
        let mut chars = Vec::new();
        let left = ["A", "B", "C", "D", "E"];
        let right = ["F", "G", "H", "I", "J"];
        for i in 0..5 {
            let yl = 20.0 + i as f64 * 16.0;
            let yr = 28.0 + i as f64 * 16.0;
            chars.push(ch(left[i], 20.0, yl, adv, 0.0, fs));
            chars.push(ch(right[i], 100.0, yr, adv, 0.0, fs));
        }
        let p = TextPage {
            width: 200.0,
            height: 300.0,
            fonts: vec![font(false)],
            chars,
        };
        let blocks = build_blocks(&p, &[]);
        let tb = text_blocks(&blocks);
        // 行は元々別なので断片分割は不要。葉割り当てで左→右
        let texts: Vec<&str> = tb.iter().map(|t| t.text.as_str()).collect();
        let joined = texts.join(" ");
        let left_pos = joined.find('A').expect("A");
        let right_pos = joined.find('F').expect("F");
        assert!(left_pos < right_pos);
        for t in &tb {
            for line in &t.lines {
                // 1行1語(列をまたがない)
                assert_eq!(line.words.len(), 1);
            }
        }
    }

    #[test]
    fn columns_table_and_two_col_order() {
        let fs = 10.0;
        let adv = 5.0;
        let mut chars = Vec::new();
        let left = ["A", "B", "C", "D", "E"];
        let right = ["F", "G", "H", "I", "J"];
        for i in 0..5 {
            let y = 40.0 + i as f64 * 15.0;
            chars.push(ch(left[i], 20.0, y, adv, 0.0, fs));
            chars.push(ch(right[i], 100.0, y, adv, 0.0, fs));
        }
        let p = TextPage {
            width: 200.0,
            height: 300.0,
            fonts: vec![font(false)],
            chars,
        };
        // 全幅の表(Y分割で上帯へ)
        let t = table_one(
            vec![vec![cell("T", 20.0, 5.0, 180.0, 24.0)]],
            20.0,
            5.0,
            180.0,
            24.0,
        );
        let blocks = build_blocks(&p, &[t]);
        assert!(matches!(blocks[0], DocBlock::Table(_)));
        let tb = text_blocks(&blocks);
        assert_eq!(tb.len(), 2);
        assert_eq!(tb[0].text, "A B C D E");
        assert_eq!(tb[1].text, "F G H I J");
        // 生成番号: 表0、左本文1、右本文2 → 葉順でも表が先頭
        assert_eq!(blocks.len(), 3);
    }

    #[test]
    fn columns_single_column_matches_legacy() {
        let fs = 10.0;
        let adv = 5.0;
        let p = page(vec![
            ch("A", 0.0, 100.0, adv, 0.0, fs),
            ch("B", adv, 100.0, adv, 0.0, fs),
            ch("C", 0.0, 115.0, adv, 0.0, fs),
            ch("D", adv, 115.0, adv, 0.0, fs),
        ]);
        let blocks = build_blocks(&p, &[]);
        let tb = text_blocks(&blocks);
        assert_eq!(tb.len(), 1);
        assert_eq!(tb[0].text, "AB CD");
        assert_eq!(tb[0].lines.len(), 2);
        assert_eq!(tb[0].kind, TextBlockKind::Paragraph);
        assert!((tb[0].left - 0.0).abs() < 1e-9);
        assert!((tb[0].top - 100.0).abs() < 1e-9);
    }

    #[test]
    fn columns_vertical_line_stays_one_block() {
        let fs = 10.0;
        let adv = 5.0;
        let mut chars = Vec::new();
        let left = ["A", "B", "C", "D", "E"];
        let right = ["F", "G", "H", "I", "J"];
        for i in 0..5 {
            let y = 20.0 + i as f64 * 15.0;
            chars.push(ch(left[i], 20.0, y, adv, 0.0, fs));
            chars.push(ch(right[i], 100.0, y, adv, 0.0, fs));
        }
        // 縦書き行(OtherLine)
        chars.push(ch_at("", 170.0, 30.0, 0.0, 10.0, fs, 1, true));
        chars.push(ch_at("", 170.0, 40.0, 0.0, 10.0, fs, 1, true));
        let p = TextPage {
            width: 200.0,
            height: 300.0,
            fonts: vec![font(false), font(true)],
            chars,
        };
        let blocks = build_blocks(&p, &[]);
        let tb = text_blocks(&blocks);
        let vert: Vec<_> = tb.iter().filter(|t| t.dir == "ttb").collect();
        assert_eq!(vert.len(), 1);
        assert_eq!(vert[0].text, "あい");
        assert_eq!(vert[0].lines.len(), 1);
        assert_eq!(vert[0].lines[0].words.len(), 1);
    }

    #[test]
    fn columns_headings_and_markdown_pipeline() {
        let fs = 10.0;
        let adv = 5.0;
        let mut chars = Vec::new();
        // 左列先頭を大きめ見出し相当
        chars.push(ch("T", 20.0, 20.0, adv, 0.0, 18.0));
        chars.push(ch("U", 100.0, 20.0, adv, 0.0, fs));
        let left = ["B", "C", "D", "E"];
        let right = ["V", "W", "X", "Y"];
        for i in 0..4 {
            let y = 20.0 + (i + 1) as f64 * 15.0;
            chars.push(ch(left[i], 20.0, y, adv, 0.0, fs));
            chars.push(ch(right[i], 100.0, y, adv, 0.0, fs));
        }
        let p = TextPage {
            width: 200.0,
            height: 300.0,
            fonts: vec![font(false)],
            chars,
        };
        let blocks = build_blocks(&p, &[]);
        let mut doc = DocDoc {
            pages: vec![DocPage {
                index: 0,
                width: p.width,
                height: p.height,
                fonts: p.fonts.clone(),
                chars: p.chars.clone(),
                blocks,
            }], warnings: Vec::new(),
        };
        assign_headings(&mut doc);
        let md = document_to_markdown(&doc, true);
        // 左列が先に出る
        let pos_t = md.find('T').expect("T");
        let pos_u = md.find('U').expect("U");
        assert!(pos_t < pos_u);
        let kinds = text_kinds(&doc);
        let title = kinds.iter().find(|(t, _)| *t == "T").unwrap();
        assert_eq!(title.1, TextBlockKind::Heading { level: 1 });
        assert!(md.starts_with("# T") || md.contains("# T"));
    }

    // --- ヘッダ・フッタ ---

    /// 1行・代表 font_size 付きの本文ブロックを作る
    fn furniture_block(
        text: &str,
        left: f64,
        top: f64,
        right: f64,
        bottom: f64,
        fs: f64,
        chars: &mut Vec<TextChar>,
    ) -> TextBlock {
        let mut indices = Vec::new();
        let mut x = left;
        for ch_c in text.chars() {
            let s = ch_c.to_string();
            let idx = chars.len() as u32;
            chars.push(TextChar {
                text: s.clone(),
                left: x,
                right: x + 1.0,
                top,
                bottom,
                transform: [fs, 0.0, 0.0, -fs, x, top],
                advance: [1.0, 0.0],
                glyph_width: None,
                font: 0,
                font_size: fs,
                rot: 0,
                upright: true,
                synthetic: false,
            });
            indices.push(idx);
            x += 1.0;
        }
        let word = TextWord {
            text: text.into(),
            left,
            right,
            top,
            bottom,
            chars: indices.clone(),
        };
        let line = TextLine {
            left,
            right,
            top,
            bottom,
            dir: "ltr".into(),
            rot: 0,
            words: vec![word],
            chars: indices,
        };
        TextBlock {
            kind: TextBlockKind::Paragraph,
            role: TextBlockRole::Body,
            text: text.into(),
            left,
            right,
            top,
            bottom,
            dir: "ltr".into(),
            rot: 0,
            lines: vec![line],
        }
    }

    fn furniture_page(
        index: usize,
        width: f64,
        height: f64,
        blocks: Vec<TextBlock>,
        chars: Vec<TextChar>,
    ) -> DocPage {
        DocPage {
            index,
            width,
            height,
            fonts: vec![],
            chars,
            blocks: blocks.into_iter().map(DocBlock::Text).collect(),
        }
    }

    fn roles_of(doc: &DocDoc) -> Vec<Vec<TextBlockRole>> {
        doc.pages
            .iter()
            .map(|p| {
                p.blocks
                    .iter()
                    .filter_map(|b| match b {
                        DocBlock::Text(t) => Some(t.role),
                        _ => None,
                    })
                    .collect()
            })
            .collect()
    }

    #[test]
    fn extract_options_detect_header_footer_default_false() {
        assert!(!ExtractOptions::default().detect_header_footer);
    }

    #[test]
    fn extract_options_escape_markdown_default_true() {
        assert!(ExtractOptions::default().escape_markdown);
        let parsed: ExtractOptions = serde_json::from_str("{}").unwrap();
        assert!(parsed.escape_markdown);
        let off: ExtractOptions = serde_json::from_str(r#"{"escape_markdown":false}"#).unwrap();
        assert!(!off.escape_markdown);
    }

    #[test]
    fn extract_options_detect_lists_default_true() {
        assert!(ExtractOptions::default().detect_lists);
        let parsed: ExtractOptions = serde_json::from_str("{}").unwrap();
        assert!(parsed.detect_lists);
        let off: ExtractOptions = serde_json::from_str(r#"{"detect_lists":false}"#).unwrap();
        assert!(!off.detect_lists);
    }

    #[test]
    fn extract_options_bidi_default_false() {
        assert!(!ExtractOptions::default().bidi);
        let parsed: ExtractOptions = serde_json::from_str("{}").unwrap();
        assert!(!parsed.bidi);
        let on: ExtractOptions = serde_json::from_str(r#"{"bidi":true}"#).unwrap();
        assert!(on.bidi);
    }

    #[test]
    fn furniture_band_boundary_exactly_one_sixth() {
        // height=600 → 1/6=100, 5/6=500。bottom ちょうど 100 は header 候補
        let h = 600.0;
        let mut ca = Vec::new();
        let mut cb = Vec::new();
        let h1 = furniture_block("HDR", 10.0, 80.0, 50.0, 100.0, 10.0, &mut ca);
        let bd1 = furniture_block("body", 10.0, 200.0, 50.0, 220.0, 10.0, &mut ca);
        let h2 = furniture_block("HDR", 10.0, 80.0, 50.0, 100.0, 10.0, &mut cb);
        let bd2 = furniture_block("body", 10.0, 200.0, 50.0, 220.0, 10.0, &mut cb);
        let mut doc = DocDoc {
            pages: vec![
                furniture_page(0, 400.0, h, vec![h1, bd1], ca),
                furniture_page(1, 400.0, h, vec![h2, bd2], cb),
            ], warnings: Vec::new(),
        };
        assign_header_footer(&mut doc);
        assert_eq!(
            roles_of(&doc),
            vec![
                vec![TextBlockRole::Header, TextBlockRole::Body],
                vec![TextBlockRole::Header, TextBlockRole::Body],
            ]
        );

        // footer: top ちょうど 500
        let mut ca = Vec::new();
        let mut cb = Vec::new();
        let f1 = furniture_block("FTR", 10.0, 500.0, 50.0, 520.0, 10.0, &mut ca);
        let bd1 = furniture_block("body", 10.0, 200.0, 50.0, 220.0, 10.0, &mut ca);
        let f2 = furniture_block("FTR", 10.0, 500.0, 50.0, 520.0, 10.0, &mut cb);
        let bd2 = furniture_block("body", 10.0, 200.0, 50.0, 220.0, 10.0, &mut cb);
        let mut doc = DocDoc {
            pages: vec![
                furniture_page(0, 400.0, h, vec![bd1, f1], ca),
                furniture_page(1, 400.0, h, vec![bd2, f2], cb),
            ], warnings: Vec::new(),
        };
        assign_header_footer(&mut doc);
        let roles = roles_of(&doc);
        assert!(roles[0].contains(&TextBlockRole::Footer));
        assert!(roles[1].contains(&TextBlockRole::Footer));
    }

    #[test]
    fn furniture_edge_gap_exactly_30() {
        let h = 600.0;
        // 2要素ヘッダ: 1つ目 bottom=20, 2つ目 top=50 → gap=30
        let mut pages = Vec::new();
        for pi in 0..2 {
            let mut chars = Vec::new();
            let a = furniture_block("A", 10.0, 5.0, 40.0, 20.0, 10.0, &mut chars);
            let b = furniture_block("B", 10.0, 50.0, 40.0, 65.0, 10.0, &mut chars);
            let body = furniture_block("body", 10.0, 200.0, 50.0, 220.0, 10.0, &mut chars);
            pages.push(furniture_page(pi, 400.0, h, vec![a, b, body], chars));
        }
        let mut doc = DocDoc { pages, warnings: Vec::new() };
        assign_header_footer(&mut doc);
        for page in &doc.pages {
            let roles: Vec<_> = page
                .blocks
                .iter()
                .filter_map(|b| match b {
                    DocBlock::Text(t) => Some((t.text.as_str(), t.role)),
                    _ => None,
                })
                .collect();
            assert_eq!(roles[0], ("A", TextBlockRole::Header));
            assert_eq!(roles[1], ("B", TextBlockRole::Header));
            assert_eq!(roles[2], ("body", TextBlockRole::Body));
        }

        // gap 30 超(31)で2要素目は不成立
        let mut pages = Vec::new();
        for pi in 0..2 {
            let mut chars = Vec::new();
            let a = furniture_block("A", 10.0, 5.0, 40.0, 20.0, 10.0, &mut chars);
            let b = furniture_block("B", 10.0, 51.0, 40.0, 66.0, 10.0, &mut chars);
            let body = furniture_block("body", 10.0, 200.0, 50.0, 220.0, 10.0, &mut chars);
            pages.push(furniture_page(pi, 400.0, h, vec![a, b, body], chars));
        }
        let mut doc = DocDoc { pages, warnings: Vec::new() };
        assign_header_footer(&mut doc);
        for page in &doc.pages {
            let roles: Vec<_> = page
                .blocks
                .iter()
                .filter_map(|b| match b {
                    DocBlock::Text(t) => Some((t.text.as_str(), t.role)),
                    _ => None,
                })
                .collect();
            assert_eq!(roles[0], ("A", TextBlockRole::Header));
            assert_eq!(roles[1], ("B", TextBlockRole::Body));
        }
    }

    #[test]
    fn furniture_font_ratio_exactly_1_05() {
        let h = 600.0;
        let mut ca = Vec::new();
        let mut cb = Vec::new();
        let a = furniture_block("HDR", 10.0, 10.0, 50.0, 25.0, 10.0, &mut ca);
        let ba = furniture_block("body", 10.0, 200.0, 50.0, 220.0, 10.0, &mut ca);
        let b = furniture_block("HDR", 10.0, 10.0, 50.0, 25.0, 10.5, &mut cb);
        let bb = furniture_block("body", 10.0, 200.0, 50.0, 220.0, 10.0, &mut cb);
        let mut doc = DocDoc {
            pages: vec![
                furniture_page(0, 400.0, h, vec![a, ba], ca),
                furniture_page(1, 400.0, h, vec![b, bb], cb),
            ], warnings: Vec::new(),
        };
        assign_header_footer(&mut doc);
        assert_eq!(roles_of(&doc)[0][0], TextBlockRole::Header);
        assert_eq!(roles_of(&doc)[1][0], TextBlockRole::Header);

        // 1.05 超
        let mut ca = Vec::new();
        let mut cb = Vec::new();
        let a = furniture_block("HDR", 10.0, 10.0, 50.0, 25.0, 10.0, &mut ca);
        let ba = furniture_block("body", 10.0, 200.0, 50.0, 220.0, 10.0, &mut ca);
        let b = furniture_block("HDR", 10.0, 10.0, 50.0, 25.0, 10.51, &mut cb);
        let bb = furniture_block("body", 10.0, 200.0, 50.0, 220.0, 10.0, &mut cb);
        let mut doc = DocDoc {
            pages: vec![
                furniture_page(0, 400.0, h, vec![a, ba], ca),
                furniture_page(1, 400.0, h, vec![b, bb], cb),
            ], warnings: Vec::new(),
        };
        assign_header_footer(&mut doc);
        assert_eq!(roles_of(&doc)[0][0], TextBlockRole::Body);
        assert_eq!(roles_of(&doc)[1][0], TextBlockRole::Body);
    }

    #[test]
    fn furniture_bbox_touch_only_no_match() {
        let h = 600.0;
        let mut ca = Vec::new();
        let mut cb = Vec::new();
        // 接するだけ: a.right == b.left
        let a = furniture_block("HDR", 10.0, 10.0, 50.0, 25.0, 10.0, &mut ca);
        let ba = furniture_block("body", 10.0, 200.0, 50.0, 220.0, 10.0, &mut ca);
        let b = furniture_block("HDR", 50.0, 10.0, 90.0, 25.0, 10.0, &mut cb);
        let bb = furniture_block("body", 10.0, 200.0, 50.0, 220.0, 10.0, &mut cb);
        let mut doc = DocDoc {
            pages: vec![
                furniture_page(0, 400.0, h, vec![a, ba], ca),
                furniture_page(1, 400.0, h, vec![b, bb], cb),
            ], warnings: Vec::new(),
        };
        assign_header_footer(&mut doc);
        assert_eq!(roles_of(&doc)[0][0], TextBlockRole::Body);
        assert_eq!(roles_of(&doc)[1][0], TextBlockRole::Body);
    }

    #[test]
    fn furniture_same_top_tiebreak_by_block_index() {
        let h = 600.0;
        // 同一 top の2候補: 添字昇順で C_p が並ぶ。各ページ同じ text 列で一致
        let mut pages = Vec::new();
        for pi in 0..2 {
            let mut chars = Vec::new();
            let a = furniture_block("A", 10.0, 10.0, 40.0, 25.0, 10.0, &mut chars);
            let b = furniture_block("B", 50.0, 10.0, 80.0, 25.0, 10.0, &mut chars);
            let body = furniture_block("body", 10.0, 200.0, 50.0, 220.0, 10.0, &mut chars);
            pages.push(furniture_page(pi, 400.0, h, vec![a, b, body], chars));
        }
        let mut doc = DocDoc { pages, warnings: Vec::new() };
        assign_header_footer(&mut doc);
        // k=0 で A どうし一致、k=1 で B どうし(隣接 gap=10-25 負=重なり相当で OK)
        // top same: A bottom 25, B top 10 → gap = 10-25 = -15 ≤ 30
        for page in &doc.pages {
            let roles: Vec<_> = page
                .blocks
                .iter()
                .filter_map(|b| match b {
                    DocBlock::Text(t) => Some((t.text.as_str(), t.role)),
                    _ => None,
                })
                .collect();
            assert_eq!(roles[0].1, TextBlockRole::Header);
            assert_eq!(roles[1].1, TextBlockRole::Header);
        }
    }

    #[test]
    fn furniture_empty_doc_and_empty_pages() {
        let mut empty = DocDoc { pages: vec![], warnings: Vec::new() };
        assign_header_footer(&mut empty);
        assert!(empty.pages.is_empty());

        let mut pages = DocDoc {
            pages: vec![
                furniture_page(0, 400.0, 600.0, vec![], vec![]),
                furniture_page(1, 400.0, 600.0, vec![], vec![]),
            ], warnings: Vec::new(),
        };
        assign_header_footer(&mut pages);
        assert!(roles_of(&pages).iter().all(|r| r.is_empty()));
    }

    #[test]
    fn furniture_single_page_no_role() {
        let mut chars = Vec::new();
        let h = furniture_block("HDR", 10.0, 10.0, 50.0, 25.0, 10.0, &mut chars);
        let mut doc = DocDoc {
            pages: vec![furniture_page(0, 400.0, 600.0, vec![h], chars)], warnings: Vec::new(),
        };
        assign_header_footer(&mut doc);
        assert_eq!(roles_of(&doc)[0][0], TextBlockRole::Body);
    }

    #[test]
    fn furniture_non_positive_page_height() {
        let mut ca = Vec::new();
        let mut cb = Vec::new();
        let a = furniture_block("HDR", 10.0, 10.0, 50.0, 25.0, 10.0, &mut ca);
        let b = furniture_block("HDR", 10.0, 10.0, 50.0, 25.0, 10.0, &mut cb);
        let mut doc = DocDoc {
            pages: vec![
                furniture_page(0, 400.0, 0.0, vec![a], ca),
                furniture_page(1, 400.0, f64::NAN, vec![b], cb),
            ], warnings: Vec::new(),
        };
        assign_header_footer(&mut doc);
        assert_eq!(roles_of(&doc)[0][0], TextBlockRole::Body);
        assert_eq!(roles_of(&doc)[1][0], TextBlockRole::Body);
    }

    #[test]
    fn furniture_mirror_d2_and_rescue() {
        // 3ページ鏡映フッタ: 全幅で bbox 交差する "N  Author" / "Author  N" は骨格不一致
        // d=2 で 1 と 3 が一致、中央は救済
        // 簡約: 全ページ "Page N" 同位置(d=1 で足りる)ではなく、
        // 鏡映は骨格不一致なので d=1 では落ち、d=2 で "Page 1" vs "Page 3" は d=2 で一致
        let h = 600.0;
        let mut pages = Vec::new();
        for (pi, n) in [(0usize, 1u32), (1, 2), (2, 3)] {
            let mut chars = Vec::new();
            let text = format!("Page {n}");
            // 奇偶で x を少しずらすが交差は残す
            let left = if pi % 2 == 0 { 100.0 } else { 120.0 };
            let f = furniture_block(&text, left, 520.0, left + 80.0, 540.0, 10.0, &mut chars);
            let body = furniture_block("body", 10.0, 200.0, 50.0, 220.0, 10.0, &mut chars);
            pages.push(furniture_page(pi, 400.0, h, vec![body, f], chars));
        }
        let mut doc = DocDoc { pages, warnings: Vec::new() };
        assign_header_footer(&mut doc);
        for page in &doc.pages {
            let footer = page.blocks.iter().find_map(|b| match b {
                DocBlock::Text(t) if t.text.starts_with("Page") => Some(t.role),
                _ => None,
            });
            assert_eq!(footer, Some(TextBlockRole::Footer));
        }

        // 3ページで中央のみ骨格が鏡映(数字位置が違う): "X 1" / "2 X" / "X 3"
        // residual は "X" で一致、値 1,2,3
        let mut pages = Vec::new();
        let texts = ["X 1", "2 X", "X 3"];
        for (pi, text) in texts.iter().enumerate() {
            let mut chars = Vec::new();
            let f = furniture_block(text, 50.0, 520.0, 150.0, 540.0, 10.0, &mut chars);
            let body = furniture_block("body", 10.0, 200.0, 50.0, 220.0, 10.0, &mut chars);
            pages.push(furniture_page(pi, 400.0, h, vec![body, f], chars));
        }
        let mut doc = DocDoc { pages, warnings: Vec::new() };
        assign_header_footer(&mut doc);
        // d=1: 骨格不一致で不成立。d=2: "X 1" vs "X 3" は骨格一致・値+2 で成立
        // 中央 "2 X" は救済(鏡映一致 + 値連続)
        for page in &doc.pages {
            let role = page.blocks.iter().find_map(|b| match b {
                DocBlock::Text(t) if t.text != "body" => Some(t.role),
                _ => None,
            });
            assert_eq!(
                role,
                Some(TextBlockRole::Footer),
                "page {}",
                page.index
            );
        }
    }

    #[test]
    fn furniture_idempotent_reapply() {
        let h = 600.0;
        let mut pages = Vec::new();
        for pi in 0..2 {
            let mut chars = Vec::new();
            let hd = furniture_block("HDR", 10.0, 10.0, 50.0, 25.0, 10.0, &mut chars);
            let body = furniture_block("body", 10.0, 200.0, 50.0, 220.0, 10.0, &mut chars);
            pages.push(furniture_page(pi, 400.0, h, vec![hd, body], chars));
        }
        let mut doc = DocDoc { pages, warnings: Vec::new() };
        assign_header_footer(&mut doc);
        let once = roles_of(&doc);
        assign_header_footer(&mut doc);
        assert_eq!(roles_of(&doc), once);
        // 既に Header が付いた状態から再適用しても同じ
        assert_eq!(once[0][0], TextBlockRole::Header);
    }

    #[test]
    fn furniture_assign_headings_excludes_non_body() {
        // 大きい font のヘッダが母集団に入ると mode がずれる構成
        let h = 600.0;
        let mut pages = Vec::new();
        for pi in 0..2 {
            let mut chars = Vec::new();
            // ヘッダを多数の大きいグリフで
            let hd = furniture_block("HHHHHH", 10.0, 10.0, 80.0, 30.0, 20.0, &mut chars);
            // 本文
            let body = furniture_block("bbbbbb", 10.0, 200.0, 80.0, 220.0, 10.0, &mut chars);
            // 見出し候補(本文の 1.8 倍 = 18)
            let title = furniture_block("Title", 10.0, 100.0, 80.0, 120.0, 18.0, &mut chars);
            pages.push(furniture_page(
                pi,
                400.0,
                h,
                vec![hd, title, body],
                chars,
            ));
        }
        let mut doc = DocDoc { pages, warnings: Vec::new() };
        assign_header_footer(&mut doc);
        assign_headings(&mut doc);
        // ヘッダ除外後の mode は 10 → Title 18 は level 1
        for page in &doc.pages {
            for b in &page.blocks {
                if let DocBlock::Text(t) = b {
                    if t.text == "Title" {
                        assert_eq!(t.kind, TextBlockKind::Heading { level: 1 });
                    }
                    if t.text == "HHHHHH" {
                        assert_eq!(t.role, TextBlockRole::Header);
                        assert_eq!(t.kind, TextBlockKind::Paragraph);
                    }
                }
            }
        }
    }

    #[test]
    fn furniture_markdown_skips_non_body() {
        let doc = doc_with_blocks(vec![
            DocBlock::Text(TextBlock {
                kind: TextBlockKind::Paragraph,
                role: TextBlockRole::Header,
                text: "header".into(),
                left: 0.0,
                right: 10.0,
                top: 0.0,
                bottom: 10.0,
                dir: "ltr".into(),
                rot: 0,
                lines: vec![],
            }),
            text_block(TextBlockKind::Paragraph, "body"),
            DocBlock::Text(TextBlock {
                kind: TextBlockKind::Paragraph,
                role: TextBlockRole::Footer,
                text: "footer".into(),
                left: 0.0,
                right: 10.0,
                top: 0.0,
                bottom: 10.0,
                dir: "ltr".into(),
                rot: 0,
                lines: vec![],
            }),
        ]);
        assert_eq!(document_to_markdown(&doc, true), "body");
    }

    #[test]
    fn furniture_json_body_role_preserves_paragraph_and_heading_shapes() {
        let tb = TextBlock {
            kind: TextBlockKind::Paragraph,
            role: TextBlockRole::Body,
            text: "x".into(),
            left: 0.0,
            right: 1.0,
            top: 2.0,
            bottom: 3.0,
            dir: "ltr".into(),
            rot: 0,
            lines: vec![],
        };
        let json = serde_json::to_string(&DocBlock::Text(tb)).unwrap();
        assert_eq!(
            json,
            r#"{"type":"text","kind":"paragraph","text":"x","left":0.0,"right":1.0,"top":2.0,"bottom":3.0,"dir":"ltr","rot":0,"lines":[]}"#
        );

        let tb = TextBlock {
            kind: TextBlockKind::Heading { level: 2 },
            role: TextBlockRole::Body,
            text: "Title".into(),
            left: 10.0,
            right: 20.0,
            top: 30.0,
            bottom: 40.0,
            dir: "ttb".into(),
            rot: 90,
            lines: vec![],
        };
        let json = serde_json::to_string(&DocBlock::Text(tb)).unwrap();
        assert_eq!(
            json,
            r#"{"type":"text","kind":"heading","level":2,"text":"Title","left":10.0,"right":20.0,"top":30.0,"bottom":40.0,"dir":"ttb","rot":90,"lines":[]}"#
        );

        let tb = TextBlock {
            kind: TextBlockKind::Paragraph,
            role: TextBlockRole::Header,
            text: "x".into(),
            left: 0.0,
            right: 1.0,
            top: 0.0,
            bottom: 1.0,
            dir: "ltr".into(),
            rot: 0,
            lines: vec![],
        };
        let json = serde_json::to_string(&DocBlock::Text(tb)).unwrap();
        assert!(json.contains(r#""role":"header""#));
    }

    #[test]
    fn furniture_static_header_two_pages() {
        let h = 600.0;
        let mut pages = Vec::new();
        for pi in 0..2 {
            let mut chars = Vec::new();
            let hd = furniture_block("Company", 10.0, 10.0, 80.0, 25.0, 10.0, &mut chars);
            let body = furniture_block("body", 10.0, 200.0, 50.0, 220.0, 10.0, &mut chars);
            pages.push(furniture_page(pi, 400.0, h, vec![hd, body], chars));
        }
        let mut doc = DocDoc { pages, warnings: Vec::new() };
        assign_header_footer(&mut doc);
        assert_eq!(roles_of(&doc)[0][0], TextBlockRole::Header);
        assert_eq!(roles_of(&doc)[1][0], TextBlockRole::Header);
        assert_eq!(document_to_markdown(&doc, true), "body\n\nbody");
    }

    fn font_style(name: &str, bold: bool, italic: bool) -> TextFont {
        TextFont {
            name: name.into(),
            ascent: 0.8,
            descent: -0.2,
            vertical: false,
            bold,
            italic,
        }
    }

    /// 文字を追加しインデックスを返す
    fn push_styled_ch(chars: &mut Vec<TextChar>, text: &str, x: f64, y: f64, font_idx: u32) -> u32 {
        let idx = chars.len() as u32;
        let adv = (text.chars().count() as f64 * 5.0).max(1.0);
        chars.push(TextChar {
            text: text.into(),
            left: x,
            right: x + adv,
            top: y,
            bottom: y + 10.0,
            transform: [10.0, 0.0, 0.0, -10.0, x, y],
            advance: [adv, 0.0],
            glyph_width: None,
            font: font_idx,
            font_size: 10.0,
            rot: 0,
            upright: true,
            synthetic: false,
        });
        idx
    }

    fn word_from_indices(chars: &[TextChar], indices: &[u32], left: f64, top: f64) -> TextWord {
        let text: String = indices
            .iter()
            .filter_map(|&i| chars.get(i as usize).map(|c| c.text.as_str()))
            .collect();
        let right = indices
            .last()
            .and_then(|&i| chars.get(i as usize).map(|c| c.right))
            .unwrap_or(left);
        TextWord {
            text,
            left,
            right,
            top,
            bottom: top + 10.0,
            chars: indices.to_vec(),
        }
    }

    fn line_from_words(words: Vec<TextWord>) -> TextLine {
        let left = words.iter().map(|w| w.left).fold(f64::INFINITY, f64::min);
        let right = words
            .iter()
            .map(|w| w.right)
            .fold(f64::NEG_INFINITY, f64::max);
        let top = words.first().map(|w| w.top).unwrap_or(0.0);
        let bottom = top + 10.0;
        let line_chars: Vec<u32> = words.iter().flat_map(|w| w.chars.iter().copied()).collect();
        TextLine {
            left,
            right,
            top,
            bottom,
            dir: "ltr".into(),
            rot: 0,
            words,
            chars: line_chars,
        }
    }

    fn doc_styled(
        fonts: Vec<TextFont>,
        chars: Vec<TextChar>,
        kind: TextBlockKind,
        lines: Vec<TextLine>,
    ) -> DocDoc {
        let text = match &kind {
            TextBlockKind::List { .. } => {
                // リストは assign 後の text 相当を手で組まない。lines から項目本文を結合
                let mut entries = Vec::new();
                let mut i = 0;
                while i < lines.len() {
                    if list_marker_kind(&lines[i]).is_none() {
                        i += 1;
                        continue;
                    }
                    let start = i;
                    i += 1;
                    while i < lines.len() && list_marker_kind(&lines[i]).is_none() {
                        i += 1;
                    }
                    let body = join_paragraph_text(&list_item_body_lines(&lines[start..i]));
                    if !body.trim().is_empty() {
                        entries.push(body);
                    }
                }
                entries.join("\n")
            }
            _ => join_paragraph_text(&lines),
        };
        let left = lines.iter().map(|l| l.left).fold(f64::INFINITY, f64::min);
        let right = lines
            .iter()
            .map(|l| l.right)
            .fold(f64::NEG_INFINITY, f64::max);
        let top = lines.iter().map(|l| l.top).fold(f64::INFINITY, f64::min);
        let bottom = lines
            .iter()
            .map(|l| l.bottom)
            .fold(f64::NEG_INFINITY, f64::max);
        DocDoc {
            pages: vec![DocPage {
                index: 0,
                width: 600.0,
                height: 800.0,
                fonts,
                chars,
                blocks: vec![DocBlock::Text(TextBlock {
                    kind,
                    role: TextBlockRole::Body,
                    text,
                    left,
                    right,
                    top,
                    bottom,
                    dir: "ltr".into(),
                    rot: 0,
                    lines,
                })],
            }],
            warnings: Vec::new(),
        }
    }

    #[test]
    fn markdown_emphasis_four_styles() {
        let fonts = vec![
            font_style("R", false, false),
            font_style("B", true, false),
            font_style("I", false, true),
            font_style("BI", true, true),
        ];
        let mut chars = Vec::new();
        let i0 = push_styled_ch(&mut chars, "plain", 0.0, 0.0, 0);
        let i1 = push_styled_ch(&mut chars, "bold", 40.0, 0.0, 1);
        let i2 = push_styled_ch(&mut chars, "ital", 80.0, 0.0, 2);
        let i3 = push_styled_ch(&mut chars, "both", 120.0, 0.0, 3);
        let line = line_from_words(vec![
            word_from_indices(&chars, &[i0], 0.0, 0.0),
            word_from_indices(&chars, &[i1], 40.0, 0.0),
            word_from_indices(&chars, &[i2], 80.0, 0.0),
            word_from_indices(&chars, &[i3], 120.0, 0.0),
        ]);
        let doc = doc_styled(fonts, chars, TextBlockKind::Paragraph, vec![line]);
        assert_eq!(
            document_to_markdown(&doc, true),
            "plain **bold** *ital* ***both***"
        );
    }

    #[test]
    fn markdown_emphasis_mid_word_switch() {
        let fonts = vec![font_style("R", false, false), font_style("B", true, false)];
        let mut chars = Vec::new();
        let a = push_styled_ch(&mut chars, "He", 0.0, 0.0, 1);
        let b = push_styled_ch(&mut chars, "llo", 10.0, 0.0, 0);
        let line = line_from_words(vec![word_from_indices(&chars, &[a, b], 0.0, 0.0)]);
        let doc = doc_styled(fonts, chars, TextBlockKind::Paragraph, vec![line]);
        assert_eq!(document_to_markdown(&doc, true), "**He**llo");
    }

    #[test]
    fn markdown_emphasis_merges_same_style_words() {
        let fonts = vec![font_style("B", true, false)];
        let mut chars = Vec::new();
        let a = push_styled_ch(&mut chars, "foo", 0.0, 0.0, 0);
        let b = push_styled_ch(&mut chars, "bar", 30.0, 0.0, 0);
        let line = line_from_words(vec![
            word_from_indices(&chars, &[a], 0.0, 0.0),
            word_from_indices(&chars, &[b], 30.0, 0.0),
        ]);
        let doc = doc_styled(fonts, chars, TextBlockKind::Paragraph, vec![line]);
        assert_eq!(document_to_markdown(&doc, true), "**foo bar**");
    }

    #[test]
    fn markdown_emphasis_per_line_hard_break() {
        let fonts = vec![font_style("I", false, true)];
        let mut chars = Vec::new();
        let a = push_styled_ch(&mut chars, "hello", 0.0, 0.0, 0);
        let b = push_styled_ch(&mut chars, "world", 0.0, 12.0, 0);
        let lines = vec![
            line_from_words(vec![word_from_indices(&chars, &[a], 0.0, 0.0)]),
            line_from_words(vec![word_from_indices(&chars, &[b], 0.0, 12.0)]),
        ];
        let doc = doc_styled(fonts, chars, TextBlockKind::Paragraph, lines);
        assert_eq!(document_to_markdown(&doc, true), "*hello*  \n*world*");
    }

    #[test]
    fn markdown_emphasis_cjk_hard_break() {
        let fonts = vec![font_style("B", true, false)];
        let mut chars = Vec::new();
        let a = push_styled_ch(&mut chars, "", 0.0, 0.0, 0);
        let b = push_styled_ch(&mut chars, "", 10.0, 12.0, 0);
        let lines = vec![
            line_from_words(vec![word_from_indices(&chars, &[a], 0.0, 0.0)]),
            line_from_words(vec![word_from_indices(&chars, &[b], 10.0, 12.0)]),
        ];
        let doc = doc_styled(fonts, chars, TextBlockKind::Paragraph, lines);
        assert_eq!(document_to_markdown(&doc, true), "**漢**  \n**字**");
    }

    #[test]
    fn markdown_emphasis_heading_prefix_not_emphasized() {
        let fonts = vec![font_style("B", true, false)];
        let mut chars = Vec::new();
        let a = push_styled_ch(&mut chars, "Title", 0.0, 0.0, 0);
        let line = line_from_words(vec![word_from_indices(&chars, &[a], 0.0, 0.0)]);
        let doc = doc_styled(
            fonts,
            chars,
            TextBlockKind::Heading { level: 2 },
            vec![line],
        );
        assert_eq!(document_to_markdown(&doc, true), "## **Title**");
    }

    #[test]
    fn markdown_emphasis_list_marker_not_emphasized() {
        let fonts = vec![font_style("B", true, false), font_style("R", false, false)];
        let mut chars = Vec::new();
        // マーカーを太字、本文を通常
        let m1 = push_styled_ch(&mut chars, "\u{2022}", 50.0, 100.0, 0);
        let b1 = push_styled_ch(&mut chars, "Alpha", 60.0, 100.0, 0);
        let m2 = push_styled_ch(&mut chars, "\u{2022}", 50.0, 115.0, 0);
        let b2 = push_styled_ch(&mut chars, "Beta", 60.0, 115.0, 0);
        let lines = vec![
            line_from_words(vec![
                word_from_indices(&chars, &[m1], 50.0, 100.0),
                word_from_indices(&chars, &[b1], 60.0, 100.0),
            ]),
            line_from_words(vec![
                word_from_indices(&chars, &[m2], 50.0, 115.0),
                word_from_indices(&chars, &[b2], 60.0, 115.0),
            ]),
        ];
        let doc = doc_styled(fonts, chars, TextBlockKind::List { ordered: false }, lines);
        assert_eq!(document_to_markdown(&doc, true), "- **Alpha**\n- **Beta**");
    }

    #[test]
    fn markdown_emphasis_ordered_list() {
        let fonts = vec![font_style("I", false, true)];
        let mut chars = Vec::new();
        let m1 = push_styled_ch(&mut chars, "1.", 50.0, 100.0, 0);
        let b1 = push_styled_ch(&mut chars, "One", 70.0, 100.0, 0);
        let m2 = push_styled_ch(&mut chars, "2.", 50.0, 115.0, 0);
        let b2 = push_styled_ch(&mut chars, "Two", 70.0, 115.0, 0);
        let lines = vec![
            line_from_words(vec![
                word_from_indices(&chars, &[m1], 50.0, 100.0),
                word_from_indices(&chars, &[b1], 70.0, 100.0),
            ]),
            line_from_words(vec![
                word_from_indices(&chars, &[m2], 50.0, 115.0),
                word_from_indices(&chars, &[b2], 70.0, 115.0),
            ]),
        ];
        let doc = doc_styled(fonts, chars, TextBlockKind::List { ordered: true }, lines);
        assert_eq!(document_to_markdown(&doc, true), "1. *One*\n2. *Two*");
    }

    #[test]
    fn markdown_emphasis_escape_star_and_backslash() {
        let fonts = vec![font_style("B", true, false)];
        let mut chars = Vec::new();
        let a = push_styled_ch(&mut chars, "a*b\\c", 0.0, 0.0, 0);
        let line = line_from_words(vec![word_from_indices(&chars, &[a], 0.0, 0.0)]);
        let doc = doc_styled(fonts, chars, TextBlockKind::Paragraph, vec![line]);
        // 本文中の * はエスケープ対象外、\ は \\
        assert_eq!(document_to_markdown(&doc, true), r"**a*b\\c**");
        assert_eq!(document_to_markdown(&doc, false), r"**a*b\c**");
    }

    #[test]
    fn markdown_emphasis_invalid_font_index_is_normal() {
        let fonts = vec![font_style("B", true, false)];
        let mut chars = Vec::new();
        // font 99 は不正 → 通常書体
        let a = push_styled_ch(&mut chars, "x", 0.0, 0.0, 99);
        let line = line_from_words(vec![word_from_indices(&chars, &[a], 0.0, 0.0)]);
        let doc = doc_styled(fonts, chars, TextBlockKind::Paragraph, vec![line]);
        assert_eq!(document_to_markdown(&doc, true), "x");
    }

    #[test]
    fn markdown_emphasis_invalid_char_index_falls_back() {
        let fonts = vec![font_style("B", true, false)];
        let chars = Vec::new();
        // 存在しない char index
        let line = TextLine {
            left: 0.0,
            right: 10.0,
            top: 0.0,
            bottom: 10.0,
            dir: "ltr".into(),
            rot: 0,
            words: vec![TextWord {
                text: "bold".into(),
                left: 0.0,
                right: 10.0,
                top: 0.0,
                bottom: 10.0,
                chars: vec![0],
            }],
            chars: vec![0],
        };
        let doc = DocDoc {
            pages: vec![DocPage {
                index: 0,
                width: 600.0,
                height: 800.0,
                fonts,
                chars,
                blocks: vec![DocBlock::Text(TextBlock {
                    kind: TextBlockKind::Paragraph,
                    role: TextBlockRole::Body,
                    text: "bold".into(),
                    left: 0.0,
                    right: 10.0,
                    top: 0.0,
                    bottom: 10.0,
                    dir: "ltr".into(),
                    rot: 0,
                    lines: vec![line],
                })],
            }], warnings: Vec::new(),
        };
        // 復元不能のため TextBlock.text へフォールバック(強調なし)
        assert_eq!(document_to_markdown(&doc, true), "bold");
    }

    #[test]
    fn markdown_emphasis_no_char_refs_falls_back() {
        let doc = doc_with_blocks(vec![text_block(TextBlockKind::Paragraph, "fallback")]);
        assert_eq!(document_to_markdown(&doc, true), "fallback");
    }
}