printpdf 0.13.1

Rust library for reading and writing PDF files
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
//! Reading a PDF file into a [`PdfDocument`].
//!
//! # How a PDF file is organised
//!
//! A PDF file starts with a header such as `%PDF-1.7`, followed by numbered *objects*: numbers,
//! names, strings, arrays, dictionaries and *streams* (a dictionary plus a block of bytes, usually
//! compressed). Objects refer to each other by number. A *cross-reference table* near the end of
//! the file lists where each object starts, and the *trailer* names the root object, the
//! *catalog*. The catalog leads to the page tree, and each page leads to a *content stream*: the
//! list of drawing operators of that page, written operands first (`q` saves the graphics state,
//! `Tf` selects a font, `re` adds a rectangle, `f` fills it, ...). Fonts, images and other things
//! a page uses are *resources*, which the content stream names.
//!
//! # What parsing produces
//!
//! The syntax level (objects, the cross-reference table, stream filters) is done by [`lopdf`].
//! This module turns the loaded objects into a [`PdfDocument`]:
//!
//! - **Document info** (title, author, subject, keywords, dates, ...) becomes
//!   `metadata.info`. Keywords are split at commas. The XMP metadata stream is not read.
//! - **Pages** become [`PdfPage`]s with their `MediaBox`, `TrimBox` and `CropBox`. The boxes are
//!   inherited from parent page-tree nodes, and `TrimBox` and `CropBox` default to the media
//!   box. A page without a usable media box gets A4 and a warning. `/Rotate` is not read.
//! - **Content streams** become [`Op`]s. If a page has several streams they are joined first.
//!   An operator without a matching `Op` becomes [`Op::Unknown`]; saving drops those unless
//!   [`PdfSaveOptions::secure`](crate::PdfSaveOptions::secure) is `false`.
//! - **Fonts** that are embedded and can be read become `resources.fonts`, keyed by the resource
//!   name (`F1`, ...). The 14 standard fonts need no entry. A font that is not embedded is
//!   replaced by the closest standard font, with a warning, and a composite (Type0) font whose
//!   program cannot be read keeps only the code layout and ToUnicode CMap needed to decode its
//!   text; neither is kept in `resources.fonts`. Reading the program of an embedded simple
//!   (TrueType or Type1) font needs the `text_layout` feature.
//! - **XObjects** (reusable pieces of content, referenced by `Do`): images are decoded to
//!   [`XObject::Image`], with an `/SMask` merged in as alpha. Form XObjects become
//!   [`XObject::Form`]: the operators of the form's content stream are read as [`Op`]s, and its
//!   own `/Resources` (fonts, images, nested forms, graphics states, shadings, patterns) become a scope
//!   of its own, read by the same functions as the resources of the pages. Text that a page
//!   draws inside a form (macOS Quartz, Word and print drivers do that for the whole page) is
//!   part of the document model like any other text. A form that cannot be modelled without
//!   loss, and images that cannot be decoded (JPEG and JPEG 2000 need the `images` feature), are
//!   kept byte for byte as [`XObject::External`], so that saving does not lose them.
//! - **Patterns** (`/Pattern` resources) become [`crate::Pattern`]s: tiling patterns and axial or
//!   radial shading patterns that the model holds exactly are typed, the others (mesh shadings,
//!   tiling patterns with nested resources) are kept as they are. `/Pattern cs /P1 scn` becomes
//!   [`Op::SetFillPattern`]. See the `pattern` module for what is typed.
//! - **Graphics state parameter dictionaries** (ExtGState), **shadings** (axial and radial
//!   gradients only), **layers** (optional content groups), **bookmarks** (the top level of the
//!   outline, entries with an explicit destination) and **link annotations** (GoTo and URI
//!   actions) are read into the matching parts of the document. Link annotations are appended to
//!   the page as [`Op::LinkAnnotation`].
//!
//! Resources are collected from the catalog, the page tree and every page, and merged into one
//! document-wide map. For fonts, XObjects and shadings the first definition of a resource name
//! wins; for ExtGState the last one does.
//!
//! # Warnings and errors
//!
//! Parsing is lenient. Whatever cannot be read is skipped, and a [`PdfWarnMsg`] with a
//! [`PdfParseErrorSeverity`] is pushed to the `warnings` list the caller passes in. The page
//! and operator the message is about are in [`PdfWarnMsg::page`] and [`PdfWarnMsg::op_id`].
//! Parsing returns `Err` only when the document cannot be walked at all: lopdf cannot load the
//! file, the trailer has no `/Root`, the catalog or its `/Pages` entry is missing or has the
//! wrong type, or a page-tree node has no `/Kids`. A page object that cannot be read leaves an
//! empty page.
//!
//! Input that asks for unreasonable amounts of memory is skipped with a warning: a stream that
//! decodes to more than 256 MiB, and the references inside preserved XObjects once copying them
//! has produced more than a fixed number of objects or bytes for the document (the rest become
//! `null`).
//!
//! # Examples
//!
//! Parse, edit and save again:
//!
//! ```
//! use printpdf::*;
//!
//! let mut warnings = Vec::new();
//! let mut doc = PdfDocument::new("Original");
//! doc.pages.push(PdfPage::new(Mm(210.0), Mm(297.0), vec![]));
//! let bytes = doc.save(&PdfSaveOptions::default(), &mut warnings);
//!
//! let mut parsed = PdfDocument::parse(&bytes, &PdfParseOptions::default(), &mut warnings).unwrap();
//! assert_eq!(parsed.pages.len(), 1);
//! parsed.metadata.info.document_title = "Edited".to_string();
//! parsed.pages.push(PdfPage::new(Mm(100.0), Mm(100.0), vec![]));
//! let edited = parsed.save(&PdfSaveOptions::default(), &mut warnings);
//! assert!(edited.starts_with(b"%PDF-"));
//! ```

use std::collections::{BTreeMap, BTreeSet};
use std::sync::Arc;

use lopdf::{
    Dictionary as LopdfDictionary, Document as LopdfDocument, Object as LopdfObject, Object,
    ObjectId, Stream as LopdfStream,
};
use serde_derive::{Deserialize, Serialize};

mod form_xobject;
use form_xobject::{parse_form, PendingForm, XObjectCtx};
mod pattern;

use crate::{
    cmap::ToUnicodeCMap,
    conformance::PdfConformance,
    date::{parse_pdf_date, OffsetDateTime},
    font::ParsedFont,
    xobject::{ExternalStream, ExternalXObject},
    BuiltinFont, BuiltinOrExternalFontId, Color, DictItem, ExtendedGraphicsState,
    ExtendedGraphicsStateId, ExtendedGraphicsStateMap, FontId, LayerInternalId, Line,
    LineDashPattern, LinePoint, LinkAnnotation, Op, PageAnnotId, PageAnnotMap, PaintMode,
    PdfDocument, PdfDocumentInfo, PdfFontMap, PdfLayerMap, PdfMetadata, PdfPage, PdfResources,
    Point, Polygon, PolygonRing, Pt, RawImage, Rect, RenderingIntent, TextMatrix,
    TextRenderingMode, WindingOrder, XObject, XObjectId, XObjectMap,
};

/// Options for [`PdfDocument::parse`](crate::PdfDocument::parse).
///
/// Serialized to JSON with camelCase field names.
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PdfParseOptions {
    /// Currently has no effect: parsing never reads this field, and problems are reported as
    /// [`PdfWarnMsg`]s whatever its value.
    #[serde(default)]
    pub fail_on_error: bool,
}

/// One message about something the parser or serializer could not read, write or understand.
///
/// Serialized to JSON with camelCase field names.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PdfWarnMsg {
    /// Zero-based index of the page the message is about. Messages about the document as a whole
    /// carry 0, and so does every message raised while saving.
    pub page: usize,
    /// Index of the content stream operator (when parsing) or [`Op`] (when saving) the message
    /// is about. 0 when it is not about a single operator.
    pub op_id: usize,
    /// How serious the problem is.
    pub severity: PdfParseErrorSeverity,
    /// Human-readable description.
    pub msg: String,
}

/// How serious a [`PdfWarnMsg`] is. Serialized to JSON in kebab-case (`"error"`, `"info"`,
/// `"warning"`).
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum PdfParseErrorSeverity {
    /// Something was lost or could not be processed.
    Error,
    /// Progress or background information.
    Info,
    /// Something unusual was found; the parser used a fallback.
    Warning,
}

impl PdfWarnMsg {
    /// A message with [`PdfParseErrorSeverity::Error`].
    pub fn error(page: usize, op_id: usize, e: String) -> Self {
        PdfWarnMsg {
            page,
            op_id,
            severity: PdfParseErrorSeverity::Error,
            msg: e,
        }
    }

    /// A message with [`PdfParseErrorSeverity::Warning`].
    pub fn warning(page: usize, op_id: usize, msg: String) -> Self {
        PdfWarnMsg {
            page,
            op_id,
            severity: PdfParseErrorSeverity::Warning,
            msg,
        }
    }

    /// A message with [`PdfParseErrorSeverity::Info`].
    pub fn info(page: usize, op_id: usize, msg: String) -> Self {
        PdfWarnMsg {
            page,
            op_id,
            severity: PdfParseErrorSeverity::Info,
            msg,
        }
    }
}

struct InitialPdf {
    doc: lopdf::Document,
    objs_to_search_for_resources: Vec<(Object, Option<usize>)>,
    page_refs: Vec<(u32, u16)>,
    document_info: PdfDocumentInfo,
    /// Link annotations with the 0-based index of the page they belong to.
    link_annots: Vec<(usize, LinkAnnotation)>,
}

fn parse_pdf_from_bytes_start(
    bytes: &[u8],
    _opts: &PdfParseOptions,
    warnings: &mut Vec<PdfWarnMsg>,
) -> Result<InitialPdf, String> {
    let doc = LopdfDocument::load_mem(bytes).map_err(|e| format!("Failed to load PDF: {}", e))?;

    let mut objs_to_search_for_resources = Vec::new();

    let root_obj = doc
        .trailer
        .get(b"Root")
        .map_err(|_| "Missing Root in trailer".to_string())?;

    objs_to_search_for_resources.push((root_obj.clone(), None));

    let root_ref = match root_obj {
        Object::Reference(r) => *r,
        _ => return Err("Invalid Root reference".to_string()),
    };

    let catalog_obj = doc
        .get_object(root_ref)
        .map_err(|e| format!("Failed to get catalog: {}", e))?;

    objs_to_search_for_resources.push((catalog_obj.clone(), None));

    let catalog = catalog_obj
        .as_dict()
        .map_err(|e| format!("Catalog is not a dictionary: {}", e))?;

    if let Ok(resources) = catalog.get(b"Resources") {
        objs_to_search_for_resources.push((resources.clone(), None));
    }

    let pages_obj = catalog
        .get(b"Pages")
        .map_err(|e| format!("Missing Pages key in catalog: {}", e))?;

    objs_to_search_for_resources.push((pages_obj.clone(), None));

    // /Pages is normally an indirect reference; an inline dictionary is accepted too.
    let pages_dict = match pages_obj {
        Object::Reference(r) => doc
            .get_object(*r)
            .map_err(|e| format!("Failed to get Pages object: {}", e))?
            .as_dict()
            .map_err(|e| format!("Pages object is not a dictionary: {}", e))?,
        Object::Dictionary(d) => {
            warnings.push(PdfWarnMsg::warning(
                0,
                0,
                "Catalog /Pages is an inline dictionary instead of a reference".to_string(),
            ));
            d
        }
        _ => return Err("Pages key is not a reference".to_string()),
    };

    if let Ok(resources) = pages_dict.get(b"Resources") {
        objs_to_search_for_resources.push((resources.clone(), None));
    }

    let page_refs = collect_page_refs(pages_dict, &doc, warnings)?;

    let page_objects = page_refs
        .iter()
        .enumerate()
        .filter_map(|(i, &page_ref)| {
            doc.get_object(page_ref)
                .ok()
                .map(|obj| (obj.clone(), Some(i)))
        })
        .collect::<Vec<_>>();

    objs_to_search_for_resources.extend(page_objects);

    let mut document_info = doc
        .trailer
        .get(b"Info")
        .ok()
        .and_then(|s| get_dict_or_resolve_ref("document_info", &doc, s, warnings, None))
        .map(parse_document_info)
        .unwrap_or_default();
    // A PDF/A file names its standard in the XMP metadata, not in the info dictionary.
    if document_info.conformance == PdfConformance::default() {
        if let Some(conformance) = pdfa_conformance_from_xmp(&doc, catalog) {
            document_info.conformance = conformance;
        }
    }

    let link_annots = links_and_bookmarks::extract_link_annotations(&doc);

    Ok(InitialPdf {
        doc,
        objs_to_search_for_resources,
        page_refs,
        document_info,
        link_annots,
    })
}

/// Parses a PDF file into a [`PdfDocument`]. This is what [`PdfDocument::parse`] calls; the
/// module documentation describes what is read and what is skipped.
///
/// Problems that do not stop parsing are pushed to `warnings`. `Err` carries a description of
/// the failure when the file cannot be read at all.
pub fn parse_pdf_from_bytes(
    bytes: &[u8],
    opts: &PdfParseOptions,
    warnings: &mut Vec<PdfWarnMsg>,
) -> Result<PdfDocument, String> {
    let i = parse_pdf_from_bytes_start(bytes, opts, warnings)?;

    let fonts = parse_fonts_from_entire_pdf(&i.doc, &i.objs_to_search_for_resources, warnings);
    let font_decode = font_decode_map(&fonts);
    let xobjects = parse_xobjects_from_entire_pdf(
        &i.doc,
        &i.objs_to_search_for_resources,
        &font_decode,
        warnings,
    );
    let xobjects = process_xobjects(xobjects, warnings);

    parse_pdf_from_bytes_end(i, opts, fonts, font_decode, xobjects, warnings)
}

/// Like [`parse_pdf_from_bytes`], but decodes images with [`RawImage::decode_from_bytes_async`].
pub async fn parse_pdf_from_bytes_async(
    bytes: &[u8],
    opts: &PdfParseOptions,
    warnings: &mut Vec<PdfWarnMsg>,
) -> Result<PdfDocument, String> {
    let i = parse_pdf_from_bytes_start(bytes, opts, warnings)?;

    let fonts = parse_fonts_from_entire_pdf(&i.doc, &i.objs_to_search_for_resources, warnings);
    let font_decode = font_decode_map(&fonts);
    let xobjects = parse_xobjects_from_entire_pdf(
        &i.doc,
        &i.objs_to_search_for_resources,
        &font_decode,
        warnings,
    );
    let xobjects = process_xobjects_async(xobjects, warnings).await;

    parse_pdf_from_bytes_end(i, opts, fonts, font_decode, xobjects, warnings)
}

/// How the text of each font resource name decodes; `Tf` operators select by that name.
///
/// Builtin entries are keyed by enum, not by resource name; `Tf` falls back to
/// [`BuiltinFont::from_id`] for the F1-F14 names.
fn font_decode_map(
    fonts: &BTreeMap<BuiltinOrExternalFontId, ParsedOrBuiltinFont>,
) -> BTreeMap<String, FontTextDecodeKind> {
    fonts
        .iter()
        .filter_map(|(id, pf)| match id {
            BuiltinOrExternalFontId::External(fid) => Some((fid.0.clone(), pf.decode_kind())),
            BuiltinOrExternalFontId::Builtin(_) => None,
        })
        .collect()
}

fn parse_pdf_from_bytes_end(
    initial_pdf: InitialPdf,
    _opts: &PdfParseOptions,
    fonts: BTreeMap<BuiltinOrExternalFontId, ParsedOrBuiltinFont>,
    font_decode: BTreeMap<String, FontTextDecodeKind>,
    xobjects: BTreeMap<XObjectId, XObject>,
    warnings: &mut Vec<PdfWarnMsg>,
) -> Result<PdfDocument, String> {
    let mut pages = Vec::new();

    let InitialPdf {
        doc,
        objs_to_search_for_resources,
        page_refs,
        document_info,
        link_annots,
    } = initial_pdf;

    for (i, page_ref) in page_refs.into_iter().enumerate() {
        let parsed = doc
            .get_object(page_ref)
            .map_err(|e| format!("Failed to get page object: {}", e))
            .and_then(|obj| {
                obj.as_dict()
                    .map_err(|e| format!("Page object is not a dictionary: {}", e))
            })
            .and_then(|page_obj| parse_page(i, page_obj, &doc, &xobjects, &font_decode, warnings));

        // A page that cannot be read stays in the list as an empty page: the page numbers of
        // the bookmarks and links refer to the position in the list.
        let pdf_page = match parsed {
            Ok(page) => page,
            Err(e) => {
                warnings.push(PdfWarnMsg::error(
                    i,
                    0,
                    format!("Page {} could not be read, it is left empty: {e}", i + 1),
                ));
                PdfPage {
                    media_box: default_media_box(),
                    trim_box: default_media_box(),
                    crop_box: default_media_box(),
                    ops: Vec::new(),
                }
            }
        };

        pages.push(pdf_page);
    }

    // The serializer builds /Annots from `Op::LinkAnnotation`, so links go back on their pages as ops.
    for (page_idx, link) in link_annots {
        match pages.get_mut(page_idx) {
            Some(page) => page.ops.push(Op::LinkAnnotation { link }),
            None => warnings.push(PdfWarnMsg::warning(
                page_idx,
                0,
                "link annotation references a page that was not parsed".to_string(),
            )),
        }
    }

    let bookmarks = links_and_bookmarks::extract_bookmarks(&doc, warnings);
    let layers = layers::extract_layers(&doc);
    // `BDC /OC /Name` refers to a layer by its resource name under the page's /Properties, so the
    // layer map is keyed by those names. A layer without one gets `layer_<index>`.
    let layers: Vec<(LayerInternalId, crate::ops::Layer)> = layers
        .into_iter()
        .enumerate()
        .map(|(i, (name, layer))| {
            let key = name.unwrap_or_else(|| format!("layer_{}", i));
            (LayerInternalId(key), layer)
        })
        .collect();

    let extgstates = extract_extgstates(&doc, &objs_to_search_for_resources, warnings);

    let shadings = extract_shadings(&doc, &objs_to_search_for_resources, warnings);

    let patterns = pattern::extract_patterns(&doc, &objs_to_search_for_resources, warnings);

    let fonts = font_resources(
        fonts
            .into_iter()
            .map(|(id, pf)| (FontId(id.get_id().to_string()), pf)),
    );

    let pdf_doc = PdfDocument {
        metadata: PdfMetadata {
            info: document_info,
            xmp: None,
        },
        resources: PdfResources {
            fonts: PdfFontMap { map: fonts },
            xobjects: XObjectMap { map: xobjects },
            extgstates: ExtendedGraphicsStateMap { map: extgstates },
            shadings: crate::ShadingMap { map: shadings },
            patterns: crate::PatternMap { map: patterns },
            layers: PdfLayerMap {
                map: layers.into_iter().collect(),
            },
        },
        bookmarks: PageAnnotMap {
            map: bookmarks
                .into_iter()
                .enumerate()
                .map(|(i, bookmark)| (PageAnnotId(format!("bookmark_{}", i)), bookmark))
                .collect(),
        },
        pages,
    };

    Ok(pdf_doc)
}

/// The fonts of a scope that the document model keeps: the ones with a font program. A standard
/// font needs no entry, and a font without a program only decodes text.
fn font_resources(
    fonts: impl Iterator<Item = (FontId, ParsedOrBuiltinFont)>,
) -> BTreeMap<FontId, crate::font::PdfFont> {
    fonts
        .filter_map(|(id, pf)| {
            let parsed_font = pf.as_parsed_font()?;
            Some((id, crate::font::PdfFont::new(parsed_font)))
        })
        .collect()
}

/// An external font parsed out of the PDF, together with everything the
/// content-stream text decoder needs to know about it.
pub struct ParsedExternalFont {
    /// The parsed font program.
    pub font: ParsedFont,
    /// The ToUnicode CMap (parsed, or built from the font's cmap table when the PDF has none).
    /// Keyed by the code in the content stream: the CID for composite fonts, one byte for simple
    /// fonts.
    pub to_unicode: Option<Arc<ToUnicodeCMap>>,
    /// `true` for a composite font (`/Subtype /Type0`), whose text-showing operators carry
    /// big-endian codes of one or more bytes, and for a simple TrueType font without a Unicode
    /// `cmap`, which is read by code; `false` for any other simple (TrueType or Type1) font, with
    /// one byte per code.
    pub is_cid: bool,
    /// CID to glyph id, for composite fonts where the two differ: a `/CIDToGIDMap` stream
    /// (CIDFontType2) or the CFF charset of a CID-keyed CIDFontType0 program (ISO 32000-1,
    /// 9.7.4.2). For a simple font read by code it is the glyph of each code. `None` means
    /// CID == glyph id (Identity). Glyph ids are looked up through this map so that outlines and
    /// widths stay correct.
    pub cid_to_gid: Option<Arc<BTreeMap<u16, u16>>>,
    /// The Type0 `/Encoding` CMap when it is not Identity-H: an embedded CMap stream or
    /// Identity-V. A simple font read by code has a CMap of one-byte codes. `None` means
    /// Identity-H (2-byte codes, code == CID).
    pub encoding_cmap: Option<Arc<crate::cmap::CidCMap>>,
}

/// A font resource of the PDF: an embedded font, one of the 14 standard fonts, or a stub.
pub enum ParsedOrBuiltinFont {
    /// An embedded font program and its decoding tables.
    P(ParsedExternalFont),
    /// One of the 14 standard fonts.
    B(BuiltinFont),
    /// A composite (Type0) font whose font program could not be extracted or parsed (for
    /// example because it is not embedded). It cannot be embedded again, but its `/Encoding`
    /// CMap and ToUnicode CMap are kept so that its text still decodes.
    CidStub(
        Option<Arc<ToUnicodeCMap>>,
        Option<Arc<crate::cmap::CidCMap>>,
    ),
}

impl ParsedOrBuiltinFont {
    fn as_parsed_font(self) -> Option<ParsedFont> {
        match self {
            ParsedOrBuiltinFont::P(p) => Some(p.font),
            ParsedOrBuiltinFont::B(_) => None,
            ParsedOrBuiltinFont::CidStub(..) => None,
        }
    }

    /// How the text-showing operators decode their bytes when this font is selected.
    fn decode_kind(&self) -> FontTextDecodeKind {
        match self {
            ParsedOrBuiltinFont::B(b) => FontTextDecodeKind::Builtin(*b),
            ParsedOrBuiltinFont::P(p) => {
                if p.is_cid {
                    FontTextDecodeKind::Cid {
                        to_unicode: p.to_unicode.clone(),
                        encoding_cmap: p.encoding_cmap.clone(),
                        cid_to_gid: p.cid_to_gid.clone(),
                    }
                } else {
                    FontTextDecodeKind::Simple {
                        to_unicode: p.to_unicode.clone(),
                    }
                }
            }
            ParsedOrBuiltinFont::CidStub(to_unicode, encoding_cmap) => FontTextDecodeKind::Cid {
                to_unicode: to_unicode.clone(),
                encoding_cmap: encoding_cmap.clone(),
                // No font program, so no charset or CIDToGIDMap to resolve through.
                cid_to_gid: None,
            },
        }
    }

    #[allow(dead_code)]
    fn cmap(&self) -> Option<&ToUnicodeCMap> {
        match self {
            ParsedOrBuiltinFont::P(p) => p.to_unicode.as_deref(),
            ParsedOrBuiltinFont::B(_) => None,
            ParsedOrBuiltinFont::CidStub(c, _) => c.as_deref(),
        }
    }
}

/// How the bytes of the text-showing operators (`Tj`, `TJ`, `'`, `"`) decode for one font
/// resource. Resolved once per page from the font resources, then tracked across `Tf` operators
/// in [`PageState`]. The code width comes from the font, never from the length of the string.
#[derive(Debug, Clone)]
pub enum FontTextDecodeKind {
    /// One of the 14 standard fonts: one-byte WinAnsi codes.
    Builtin(BuiltinFont),
    /// Simple external font (TrueType/Type1): one-byte codes, decoded through
    /// the ToUnicode CMap when present, WinAnsi otherwise.
    Simple {
        /// ToUnicode CMap of the font, if it has one.
        to_unicode: Option<Arc<ToUnicodeCMap>>,
    },
    /// Composite (Type0) font, or a simple TrueType font without a Unicode `cmap` that is read
    /// by code. Content stream bytes become codes (through the codespace ranges of
    /// the `/Encoding` CMap; codes are 1 to 4 bytes wide), codes become CIDs (cidrange and
    /// cidchar entries), and CIDs become glyph ids (`cid_to_gid`: the `/CIDToGIDMap` stream of a
    /// CIDFontType2 or the CFF charset of a CID-keyed CIDFontType0). `encoding_cmap: None` is
    /// Identity-H: 2-byte big-endian codes, code == CID. `to_unicode` is keyed by the code.
    Cid {
        /// ToUnicode CMap, keyed by code.
        to_unicode: Option<Arc<ToUnicodeCMap>>,
        /// The `/Encoding` CMap, or `None` for Identity-H.
        encoding_cmap: Option<Arc<crate::cmap::CidCMap>>,
        /// CID to glyph id map, or `None` when they are identical.
        cid_to_gid: Option<Arc<BTreeMap<u16, u16>>>,
    },
}

/// The dictionary behind `xobj_obj`: an inline dictionary, a stream (its dictionary), or a
/// reference to either. Pushes an error to `warnings` and returns `None` for anything else.
fn get_dict_or_resolve_ref<'a>(
    id: &str,
    doc: &'a LopdfDocument,
    xobj_obj: &'a LopdfObject,
    warnings: &mut Vec<PdfWarnMsg>,
    page: Option<usize>,
) -> Option<&'a LopdfDictionary> {
    match xobj_obj {
        Object::Dictionary(dict) => Some(dict),
        // XObjects are streams, so a stream's dictionary counts as well.
        Object::Stream(stream) => Some(&stream.dict),
        Object::Reference(r) => match doc.get_dictionary(*r) {
            Ok(s) => Some(s),
            // `get_dictionary` rejects stream objects; take the stream's dictionary instead.
            Err(_) => match doc.get_object(*r) {
                Ok(Object::Stream(stream)) => Some(&stream.dict),
                Ok(Object::Dictionary(dict)) => Some(dict),
                _ => {
                    warnings.push(PdfWarnMsg::error(
                        page.unwrap_or(0),
                        0,
                        format!("{id}: Invalid dictionary reference {r:?}"),
                    ));
                    None
                }
            },
        },
        _ => {
            warnings.push(PdfWarnMsg::error(
                page.unwrap_or(0),
                0,
                "Unexpected type for XObject resource".to_string(),
            ));
            None
        }
    }
}

/// The stream behind `xobj_obj` (inline or by reference). Pushes an error to `warnings` and
/// returns `None` for anything else.
fn get_stream_or_resolve_ref<'a>(
    doc: &'a LopdfDocument,
    xobj_obj: &'a LopdfObject,
    warnings: &mut Vec<PdfWarnMsg>,
    page: Option<usize>,
) -> Option<&'a LopdfStream> {
    match xobj_obj {
        Object::Stream(dict) => Some(dict),
        Object::Reference(r) => match doc.get_object(*r) {
            Ok(Object::Stream(s)) => Some(s),
            Ok(_) => {
                warnings.push(PdfWarnMsg::error(
                    page.unwrap_or(0),
                    0,
                    format!("Invalid stream reference {r:?}"),
                ));
                None
            }
            Err(e) => {
                warnings.push(PdfWarnMsg::error(
                    page.unwrap_or(0),
                    0,
                    format!("Invalid stream reference {r:?}: {e:?}"),
                ));
                None
            }
        },
        _ => {
            warnings.push(PdfWarnMsg::error(
                page.unwrap_or(0),
                0,
                "Unexpected type for XObject resource".to_string(),
            ));
            None
        }
    }
}

/// Result of scanning the XObject resources: an image that still has to go through the (possibly
/// async) decoder, or an XObject that is already complete.
#[derive(Clone)]
pub(crate) enum PendingXObject {
    /// An image in a self-describing container format (JPEG via `DCTDecode`, JPEG 2000 via
    /// `JPXDecode`, ...). `fallback` keeps the original stream byte for byte in case the decoder
    /// does not understand the bytes, so that saving never drops the image.
    DecodeImage {
        bytes: Vec<u8>,
        smask: Option<SMaskData>,
        fallback: ExternalXObject,
    },
    /// Fully parsed: raw bitmaps, forms that cannot be modelled and unknown subtypes kept
    /// verbatim.
    Ready(XObject),
    /// A form that is read into the model; the images in its resources may still wait to be
    /// decoded.
    Form(Arc<PendingForm>),
}

/// A decoded (uncompressed) SMask alpha channel: 8-bit gray, `width * height` bytes.
#[derive(Clone)]
pub(crate) struct SMaskData {
    pub width: usize,
    pub height: usize,
    pub gray: Vec<u8>,
}

/// Merge an 8-bit gray SMask into a decoded [`RawImage`], turning RGB8 into
/// RGBA8 and R8 into RG8 (gray+alpha). Returns the image unchanged when the
/// dimensions do not match or the pixel format has no 8-bit alpha companion.
fn apply_smask_to_rawimage(mut img: RawImage, smask: SMaskData) -> RawImage {
    use crate::image_types::{RawImageData, RawImageFormat};
    if smask.width != img.width || smask.height != img.height {
        return img;
    }
    let Some(expected) = img.width.checked_mul(img.height) else {
        return img;
    };
    if smask.gray.len() < expected {
        return img;
    }
    match (&img.data_format, &img.pixels) {
        (RawImageFormat::RGB8, RawImageData::U8(rgb)) => {
            let (Some(rgb_len), Some(rgba_len)) =
                (expected.checked_mul(3), expected.checked_mul(4))
            else {
                return img;
            };
            if rgb.len() < rgb_len {
                return img;
            }
            let mut rgba = Vec::with_capacity(rgba_len);
            for (pixel, alpha) in rgb.chunks_exact(3).zip(&smask.gray).take(expected) {
                rgba.extend_from_slice(pixel);
                rgba.push(*alpha);
            }
            img.pixels = RawImageData::U8(rgba);
            img.data_format = RawImageFormat::RGBA8;
            img
        }
        (RawImageFormat::R8, RawImageData::U8(gray)) => {
            let Some(ga_len) = expected.checked_mul(2) else {
                return img;
            };
            if gray.len() < expected {
                return img;
            }
            let mut ga = Vec::with_capacity(ga_len);
            for (value, alpha) in gray.iter().zip(&smask.gray).take(expected) {
                ga.push(*value);
                ga.push(*alpha);
            }
            img.pixels = RawImageData::U8(ga);
            img.data_format = RawImageFormat::RG8;
            img
        }
        _ => img,
    }
}

fn process_xobjects(
    xobjects: BTreeMap<XObjectId, PendingXObject>,
    warnings: &mut Vec<PdfWarnMsg>,
) -> BTreeMap<XObjectId, XObject> {
    let mut map = BTreeMap::new();
    for (xobject_id, pending) in xobjects {
        match pending {
            PendingXObject::Ready(x) => {
                map.insert(xobject_id, x);
            }
            PendingXObject::Form(pending) => {
                map.insert(xobject_id, finish_form(pending, warnings));
            }
            PendingXObject::DecodeImage {
                bytes,
                smask,
                fallback,
            } => {
                warnings.push(PdfWarnMsg::info(
                    0,
                    0,
                    format!("process XObject {} ({} bytes)", xobject_id.0, bytes.len()),
                ));
                match RawImage::decode_from_bytes(&bytes, warnings) {
                    Ok(mut o) => {
                        if let Some(sm) = smask {
                            o = apply_smask_to_rawimage(o, sm);
                        }
                        map.insert(xobject_id, XObject::Image(o));
                    }
                    Err(e) => {
                        warnings.push(PdfWarnMsg::warning(
                            0,
                            0,
                            format!(
                                "could not decode image XObject {} ({} bytes): {e}; preserving \
                                 original stream",
                                xobject_id.0,
                                bytes.len()
                            ),
                        ));
                        map.insert(xobject_id, XObject::External(fallback));
                    }
                }
            }
        }
    }
    map
}

/// A form read from the PDF, with the images in its resources decoded.
fn finish_form(pending: Arc<PendingForm>, warnings: &mut Vec<PdfWarnMsg>) -> XObject {
    // Only a form that more than one place of the document uses is copied.
    let PendingForm {
        mut form,
        xobjects,
    } = Arc::try_unwrap(pending).unwrap_or_else(|shared| (*shared).clone());
    if let Some(resources) = form.resources.as_mut() {
        resources.xobjects.map = process_xobjects(xobjects, warnings);
    }
    XObject::Form(form)
}

/// Collects the `/Shading` resources (axial and radial gradients) into the typed
/// [`crate::Shading`] model. Other shading types, and function types other than 2 (exponential)
/// and 3 (stitching), are skipped with a warning; the supported shapes are the ones printpdf
/// writes itself, so its own gradients round-trip.
fn extract_shadings(
    doc: &LopdfDocument,
    objs_to_search_for_resources: &[(LopdfObject, Option<usize>)],
    warnings: &mut Vec<PdfWarnMsg>,
) -> BTreeMap<crate::ShadingId, crate::Shading> {
    let mut map = BTreeMap::new();
    for (obj, page_idx) in objs_to_search_for_resources {
        let page_num = page_idx.unwrap_or(0);
        let dict = match obj {
            LopdfObject::Dictionary(d) => Some(d),
            LopdfObject::Reference(r) => doc.get_dictionary(*r).ok(),
            _ => None,
        };
        let Some(dict) = dict else { continue };
        let resources = if let Ok(res) = dict.get(b"Resources") {
            get_dict_or_resolve_ref(
                &format!("extract_shadings Resources page {page_num}"),
                doc,
                res,
                warnings,
                Some(page_num),
            )
        } else {
            Some(dict)
        };
        let Some(resources) = resources else { continue };
        read_shadings(doc, resources, page_num, &mut map, warnings);
    }
    map
}

/// Adds the `/Shading` resources of one resource dictionary to `map`. A name that `map` has
/// already is skipped: the first definition wins, consistent with the other resources.
fn read_shadings(
    doc: &LopdfDocument,
    resources: &LopdfDictionary,
    page_num: usize,
    map: &mut BTreeMap<crate::ShadingId, crate::Shading>,
    warnings: &mut Vec<PdfWarnMsg>,
) {
    let Ok(shading_res) = resources.get(b"Shading") else {
        return;
    };
    let Some(shading_dict) = get_dict_or_resolve_ref(
        &format!("extract_shadings Shading page {page_num}"),
        doc,
        shading_res,
        warnings,
        Some(page_num),
    ) else {
        return;
    };
    for (key, value) in shading_dict.iter() {
        let id = crate::ShadingId(String::from_utf8_lossy(key).to_string());
        if map.contains_key(&id) {
            continue;
        }
        let Some(sh) = get_dict_or_resolve_ref(
            &format!("extract_shadings entry page {page_num}"),
            doc,
            value,
            warnings,
            Some(page_num),
        ) else {
            continue;
        };
        match parse_shading_dict(doc, sh) {
            Ok(parsed) => {
                map.insert(id, parsed);
            }
            Err(e) => warnings.push(PdfWarnMsg::warning(
                page_num,
                0,
                format!("shading {:?} not preserved: {e}", id.0),
            )),
        }
    }
}

/// Translate a ShadingType 2/3 dictionary (with an exponential or stitching
/// function) into [`crate::Shading`] — the exact inverse of `Shading::to_dict`.
fn parse_shading_dict(
    doc: &LopdfDocument,
    dict: &LopdfDictionary,
) -> Result<crate::Shading, String> {
    fn as_f32(o: &LopdfObject) -> Option<f32> {
        match o {
            LopdfObject::Integer(i) => Some(*i as f32),
            LopdfObject::Real(r) => Some(*r),
            _ => None,
        }
    }
    fn num_array<const N: usize>(doc: &LopdfDocument, o: &LopdfObject) -> Option<[f32; N]> {
        let o = match o {
            LopdfObject::Reference(r) => doc.get_object(*r).ok()?,
            other => other,
        };
        let arr = o.as_array().ok()?;
        let v: Vec<f32> = arr.iter().filter_map(as_f32).collect();
        <[f32; N]>::try_from(v).ok()
    }
    fn rgb(v: &[f32]) -> [f32; 3] {
        match *v {
            [gray] => [gray, gray, gray], // DeviceGray
            [r, g, b] => [r, g, b],
            _ => [0.0, 0.0, 0.0],
        }
    }
    fn resolve<'a>(doc: &'a LopdfDocument, o: &'a LopdfObject) -> &'a LopdfObject {
        match o {
            LopdfObject::Reference(r) => doc.get_object(*r).unwrap_or(o),
            other => other,
        }
    }
    fn fn_dict(o: &LopdfObject) -> Option<&LopdfDictionary> {
        match o {
            LopdfObject::Dictionary(d) => Some(d),
            LopdfObject::Stream(s) => Some(&s.dict),
            _ => None,
        }
    }

    let shading_type = dict
        .get(b"ShadingType")
        .ok()
        .and_then(as_f32)
        .ok_or("missing ShadingType")? as i64;

    let geometry = match shading_type {
        2 => {
            let coords = dict
                .get(b"Coords")
                .ok()
                .and_then(|o| num_array::<4>(doc, o))
                .ok_or("axial shading without 4-element Coords")?;
            crate::ShadingGeometry::Axial { coords }
        }
        3 => {
            let coords = dict
                .get(b"Coords")
                .ok()
                .and_then(|o| num_array::<6>(doc, o))
                .ok_or("radial shading without 6-element Coords")?;
            crate::ShadingGeometry::Radial { coords }
        }
        other => return Err(format!("unsupported ShadingType {other}")),
    };

    let extend = dict
        .get(b"Extend")
        .ok()
        .map(|o| resolve(doc, o))
        .and_then(|o| o.as_array().ok())
        .map(|a| {
            let b = |i: usize| matches!(a.get(i), Some(LopdfObject::Boolean(true)));
            (b(0), b(1))
        })
        .unwrap_or((false, false));

    let func_obj = resolve(
        doc,
        dict.get(b"Function")
            .map_err(|_| "shading without Function")?,
    );
    let func = fn_dict(func_obj).ok_or("shading Function is not a dictionary/stream")?;
    let func_type = func
        .get(b"FunctionType")
        .ok()
        .and_then(as_f32)
        .ok_or("Function without FunctionType")? as i64;

    // One exponential segment over [t0, t1] → two stops.
    fn exponential_stops(
        doc: &LopdfDocument,
        f: &LopdfDictionary,
        t0: f32,
        t1: f32,
    ) -> Result<Vec<crate::GradientStop>, String> {
        let get = |k: &[u8]| -> Vec<f32> {
            f.get(k)
                .ok()
                .and_then(|o| {
                    let o = match o {
                        LopdfObject::Reference(r) => doc.get_object(*r).ok()?,
                        other => other,
                    };
                    o.as_array().ok()
                })
                .map(|a| {
                    a.iter()
                        .filter_map(|x| match x {
                            LopdfObject::Integer(i) => Some(*i as f32),
                            LopdfObject::Real(r) => Some(*r),
                            _ => None,
                        })
                        .collect()
                })
                .unwrap_or_else(|| vec![0.0])
        };
        let c0 = get(b"C0");
        let c1 = get(b"C1");
        Ok(vec![
            crate::GradientStop {
                offset: t0,
                color: rgb(&c0),
            },
            crate::GradientStop {
                offset: t1,
                color: rgb(&c1),
            },
        ])
    }

    let mut stops = match func_type {
        2 => exponential_stops(doc, func, 0.0, 1.0)?,
        3 => {
            let bounds = func
                .get(b"Bounds")
                .ok()
                .map(|o| resolve(doc, o))
                .and_then(|o| o.as_array().ok())
                .map(|a| a.iter().filter_map(as_f32).collect::<Vec<_>>())
                .unwrap_or_default();
            let funcs = func
                .get(b"Functions")
                .ok()
                .map(|o| resolve(doc, o))
                .and_then(|o| o.as_array().ok())
                .ok_or("stitching function without Functions array")?;
            let mut edges = Vec::with_capacity(funcs.len() + 1);
            edges.push(0.0);
            edges.extend(bounds.iter().copied());
            edges.push(1.0);
            let mut all = Vec::new();
            for (i, sub) in funcs.iter().enumerate() {
                let sub = resolve(doc, sub);
                let sub = fn_dict(sub).ok_or("stitching sub-function is not a dictionary")?;
                let (t0, t1) = (
                    edges.get(i).copied().unwrap_or(0.0),
                    edges.get(i + 1).copied().unwrap_or(1.0),
                );
                all.extend(exponential_stops(doc, sub, t0, t1)?);
            }
            all
        }
        other => return Err(format!("unsupported FunctionType {other}")),
    };

    // Merge duplicate boundary stops (segment N's end == segment N+1's start).
    stops.dedup_by(|b, a| (a.offset - b.offset).abs() < 1e-6 && a.color == b.color);
    if stops.is_empty() {
        return Err("shading function produced no stops".to_string());
    }

    Ok(crate::Shading {
        geometry,
        stops,
        extend,
    })
}

// Collects the ExtGState resources of all resource dictionaries (the last definition of a name wins).
fn extract_extgstates(
    doc: &LopdfDocument,
    objs_to_search_for_resources: &[(LopdfObject, Option<usize>)],
    warnings: &mut Vec<PdfWarnMsg>,
) -> BTreeMap<ExtendedGraphicsStateId, ExtendedGraphicsState> {
    objs_to_search_for_resources
        .iter()
        .filter_map(|(obj, page_idx)| {
            let page_num = page_idx.unwrap_or(0);

            let dict = match obj {
                LopdfObject::Dictionary(d) => Some(d),
                LopdfObject::Reference(r) => doc.get_dictionary(*r).ok(),
                _ => None,
            }?;

            let resources = if let Ok(res) = dict.get(b"Resources") {
                get_dict_or_resolve_ref(
                    &format!("extract_extgstates Resources page {page_num}"),
                    doc,
                    res,
                    warnings,
                    Some(page_num),
                )
            } else {
                // If this is already a Resources dict
                Some(dict)
            }?;

            read_extgstates(doc, resources, page_num, warnings)
        })
        .fold(BTreeMap::new(), |mut acc, extgstates| {
            acc.extend(extgstates);
            acc
        })
}

/// The `/ExtGState` resources of one resource dictionary.
fn read_extgstates(
    doc: &LopdfDocument,
    resources: &LopdfDictionary,
    page_num: usize,
    warnings: &mut Vec<PdfWarnMsg>,
) -> Option<BTreeMap<ExtendedGraphicsStateId, ExtendedGraphicsState>> {
    let extgstate = resources.get(b"ExtGState").ok()?;
    let extgstate_dict = get_dict_or_resolve_ref(
        &format!("extract_extgstates ExtGState page {page_num}"),
        doc,
        extgstate,
        warnings,
        Some(page_num),
    )?;

    let gs_entries = extgstate_dict
        .iter()
        .filter_map(|(key, value)| {
            let gs_dict = get_dict_or_resolve_ref(
                &format!("extract_extgstates GsDict page {page_num}"),
                doc,
                value,
                warnings,
                Some(page_num),
            )?;
            let gs_id = ExtendedGraphicsStateId(String::from_utf8_lossy(key).to_string());
            let gs = extgstate::parse_extgstate(gs_dict);
            Some((gs_id, gs))
        })
        .collect::<BTreeMap<_, _>>();

    Some(gs_entries)
}

async fn process_xobjects_async(
    xobjects: BTreeMap<XObjectId, PendingXObject>,
    warnings: &mut Vec<PdfWarnMsg>,
) -> BTreeMap<XObjectId, XObject> {
    let mut map = BTreeMap::new();
    for (xobject_id, pending) in xobjects {
        match pending {
            PendingXObject::Ready(x) => {
                map.insert(xobject_id, x);
            }
            // The images inside a form are decoded without the async decoder.
            PendingXObject::Form(pending) => {
                map.insert(xobject_id, finish_form(pending, warnings));
            }
            PendingXObject::DecodeImage {
                bytes,
                smask,
                fallback,
            } => match RawImage::decode_from_bytes_async(&bytes, warnings).await {
                Ok(mut o) => {
                    if let Some(sm) = smask {
                        o = apply_smask_to_rawimage(o, sm);
                    }
                    map.insert(xobject_id, XObject::Image(o));
                }
                Err(e) => {
                    warnings.push(PdfWarnMsg::warning(
                        0,
                        0,
                        format!(
                            "could not decode image XObject {}: {e}; preserving original stream",
                            xobject_id.0
                        ),
                    ));
                    map.insert(xobject_id, XObject::External(fallback));
                }
            },
        }
    }
    map
}

/// Maximum recursion depth (container nesting + reference hops) when inlining
/// indirect references into a preserved XObject dictionary. Real-world SVG
/// conversions nest forms → resources → patterns → shadings → functions →
/// ICC streams, so this must be generous; actual cycles are caught by the
/// reference path stack, not the depth cap.
const MAX_RESOLVE_DEPTH: usize = 96;

/// Most objects that inlining references into the preserved XObjects of one document may copy.
const MAX_INLINED_NODES: usize = 2_000_000;

/// Most bytes of nested streams and strings that the same inlining may copy for one document.
const MAX_INLINED_BYTES: usize = 256 * 1024 * 1024;

/// What is left of the inlining budget of one document.
///
/// Inlining copies an object every time it is reached, so objects that are shared at many
/// levels (each level refers to the next one twice) multiply: a few kilobytes of input can
/// stand for more copies than fit in memory. Once the budget is spent, the rest becomes `null`.
struct InlineBudget {
    nodes: usize,
    bytes: usize,
    /// Something was replaced by `null` because the budget ran out.
    exhausted: bool,
    /// The warning about it has been written.
    reported: bool,
}

impl InlineBudget {
    fn new() -> Self {
        InlineBudget {
            nodes: MAX_INLINED_NODES,
            bytes: MAX_INLINED_BYTES,
            exhausted: false,
            reported: false,
        }
    }

    /// Takes one object that holds `bytes` bytes of data. Returns `false` when the budget does
    /// not cover it.
    fn take(&mut self, bytes: usize) -> bool {
        match (self.nodes.checked_sub(1), self.bytes.checked_sub(bytes)) {
            (Some(nodes), Some(left)) => {
                self.nodes = nodes;
                self.bytes = left;
                true
            }
            _ => {
                self.exhausted = true;
                false
            }
        }
    }
}

/// Convert a lopdf object into a [`DictItem`], resolving indirect references
/// by *inlining* the referenced objects.
///
/// Preserved XObjects are carried across the parse/save round trip as
/// [`ExternalXObject`]s. Their dictionaries routinely reference other objects
/// (a Form XObject's `/Resources`, pattern functions, ICC profiles, ...) — but
/// object ids are meaningless in the re-saved document, so a verbatim
/// `Reference` would dangle. Inlining the referenced object keeps the XObject
/// self-contained; `into_lopdf_with_indirect_streams` re-hoists nested streams
/// into indirect objects at save time.
///
/// `ref_path` holds the reference ids currently being resolved (the *path*,
/// not all visited ids — legitimate documents share objects diamond-style);
/// re-entering an id on the same path is a cycle and yields `Null`. Every object copied is
/// taken from `budget`; when it is spent the rest yields `Null` as well.
fn dictitem_from_lopdf_resolved(
    doc: &LopdfDocument,
    obj: &LopdfObject,
    depth: usize,
    ref_path: &mut Vec<ObjectId>,
    budget: &mut InlineBudget,
) -> DictItem {
    if depth > MAX_RESOLVE_DEPTH {
        return DictItem::Null;
    }
    let bytes = match obj {
        Object::String(s, _) => s.len(),
        Object::Name(n) => n.len(),
        Object::Stream(s) => s.content.len(),
        _ => 0,
    };
    if !budget.take(bytes) {
        return DictItem::Null;
    }
    match obj {
        Object::Reference(r) => {
            if ref_path.contains(r) {
                // reference cycle
                return DictItem::Null;
            }
            match doc.get_object(*r) {
                Ok(target) => {
                    ref_path.push(*r);
                    let item =
                        dictitem_from_lopdf_resolved(doc, target, depth + 1, ref_path, budget);
                    ref_path.pop();
                    item
                }
                Err(_) => DictItem::Null,
            }
        }
        Object::Array(objects) => DictItem::Array(
            objects
                .iter()
                .map(|o| dictitem_from_lopdf_resolved(doc, o, depth + 1, ref_path, budget))
                .collect(),
        ),
        Object::Dictionary(dict) => DictItem::Dict {
            map: dict
                .iter()
                .map(|(k, v)| {
                    (
                        String::from_utf8_lossy(k).to_string(),
                        dictitem_from_lopdf_resolved(doc, v, depth + 1, ref_path, budget),
                    )
                })
                .collect(),
        },
        Object::Stream(stream) => DictItem::Stream {
            stream: external_stream_preserved(doc, stream, depth + 1, ref_path, budget),
        },
        other => DictItem::from_lopdf(other),
    }
}

/// Preserve a lopdf stream verbatim as an [`ExternalStream`]: the dictionary
/// with references inlined (minus `/Length`, which is recomputed at save time)
/// and the *raw, still-encoded* content, so existing `/Filter` entries remain
/// valid.
fn external_stream_preserved(
    doc: &LopdfDocument,
    stream: &LopdfStream,
    depth: usize,
    ref_path: &mut Vec<ObjectId>,
    budget: &mut InlineBudget,
) -> ExternalStream {
    let dict = stream
        .dict
        .iter()
        .filter(|(k, _)| k.as_slice() != b"Length")
        .map(|(k, v)| {
            (
                String::from_utf8_lossy(k).to_string(),
                dictitem_from_lopdf_resolved(doc, v, depth, ref_path, budget),
            )
        })
        .collect();
    ExternalStream {
        dict,
        content: stream.content.clone(),
        // The content is already encoded exactly as the preserved /Filter
        // describes; re-compressing it would corrupt the stream.
        compress: false,
    }
}

/// Pushes a warning, once per document, when `budget` ran out while copying the references of
/// the XObject `xobject_id`.
fn warn_if_budget_spent(
    budget: &mut InlineBudget,
    xobject_id: &XObjectId,
    page: usize,
    warnings: &mut Vec<PdfWarnMsg>,
) {
    if budget.exhausted && !budget.reported {
        budget.reported = true;
        warnings.push(PdfWarnMsg::warning(
            page,
            0,
            format!(
                "XObject {}: the references inside it exceed the limit for one document \
                 ({MAX_INLINED_NODES} objects or {MAX_INLINED_BYTES} bytes); the rest are \
                 replaced by null",
                xobject_id.0
            ),
        ));
    }
}

/// Preserve an XObject stream verbatim for the round trip. Pushes a warning, once per document,
/// when `budget` ran out while copying the references of the stream.
fn preserve_xobject_stream(
    doc: &LopdfDocument,
    stream: &LopdfStream,
    xobject_id: &XObjectId,
    page: usize,
    budget: &mut InlineBudget,
    warnings: &mut Vec<PdfWarnMsg>,
) -> ExternalXObject {
    let stream = external_stream_preserved(doc, stream, 0, &mut Vec::new(), budget);
    warn_if_budget_spent(budget, xobject_id, page, warnings);
    ExternalXObject {
        stream,
        // Leave width/height/dpi unset: placement comes from the content
        // stream's own `cm` + the preserved `/BBox`/`/Matrix`, and a `Some`
        // width/height would make the serializer inject an extra scale.
        width: None,
        height: None,
        dpi: None,
    }
}

/// Number of color components for a (resolved) `/ColorSpace` entry, for the
/// raw-bitmap path. Returns `None` for colorspaces that `RawImage` cannot
/// represent losslessly (Indexed, Separation, DeviceN, ...).
fn colorspace_components(doc: &LopdfDocument, cs: &LopdfObject, depth: usize) -> Option<usize> {
    if depth > MAX_RESOLVE_DEPTH {
        return None;
    }
    match cs {
        Object::Reference(r) => colorspace_components(doc, doc.get_object(*r).ok()?, depth + 1),
        Object::Name(n) => match n.as_slice() {
            b"DeviceRGB" | b"CalRGB" => Some(3),
            b"DeviceGray" | b"CalGray" => Some(1),
            _ => None,
        },
        Object::Array(arr) => {
            let first = arr.first()?;
            let family = match first {
                Object::Name(n) => n.as_slice(),
                _ => return None,
            };
            match family {
                b"ICCBased" => {
                    let stream = arr.get(1)?;
                    let stream = match stream {
                        Object::Reference(r) => match doc.get_object(*r).ok()? {
                            Object::Stream(s) => s,
                            _ => return None,
                        },
                        Object::Stream(s) => s,
                        _ => return None,
                    };
                    match stream.dict.get(b"N").ok()? {
                        Object::Integer(1) => Some(1),
                        Object::Integer(3) => Some(3),
                        _ => None,
                    }
                }
                b"CalRGB" => Some(3),
                b"CalGray" => Some(1),
                _ => None,
            }
        }
        _ => None,
    }
}

/// The `/Filter` chain of a stream as a list of names.
fn stream_filters(stream: &LopdfStream) -> Vec<Vec<u8>> {
    match stream.dict.get(b"Filter") {
        Ok(Object::Name(n)) => vec![n.clone()],
        Ok(Object::Array(arr)) => arr
            .iter()
            .filter_map(|o| match o {
                Object::Name(n) => Some(n.clone()),
                _ => None,
            })
            .collect(),
        _ => Vec::new(),
    }
}

/// Most bytes one stream of the input document may decode to. A few kilobytes of Flate data can
/// stand for gigabytes, so every stream that is decoded here is bounded.
const MAX_STREAM_BYTES: usize = 256 * 1024 * 1024;

/// How [`decode_limited`] ended.
enum Decoded {
    Data(Vec<u8>),
    /// A filter of the stream cannot be undone, or its data is damaged.
    Undecodable,
    /// The decoded data would be larger than the limit.
    TooLarge,
}

/// Decodes the filters of `stream` into at most `limit` bytes.
fn decode_limited(stream: &LopdfStream, limit: usize) -> Decoded {
    match stream.decompressed_content_with_limit(limit) {
        Ok(data) => Decoded::Data(data),
        Err(lopdf::Error::Decompress(lopdf::DecompressError::MemoryLimitExceeded { .. })) => {
            Decoded::TooLarge
        }
        Err(_) => Decoded::Undecodable,
    }
}

/// The decoded bytes of `stream`, or its stored bytes when a filter cannot be undone. Pushes a
/// warning that names `what` and returns `None` when the decoded data is larger than `limit`
/// bytes.
fn decode_stream(
    stream: &LopdfStream,
    limit: usize,
    what: &str,
    page: usize,
    warnings: &mut Vec<PdfWarnMsg>,
) -> Option<Vec<u8>> {
    match decode_limited(stream, limit) {
        Decoded::Data(data) => Some(data),
        Decoded::Undecodable => Some(stream.content.clone()),
        Decoded::TooLarge => {
            warnings.push(PdfWarnMsg::error(
                page,
                0,
                format!("{what}: the decoded stream is larger than {limit} bytes; skipped"),
            ));
            None
        }
    }
}

/// Resolve and decode the `/SMask` of an image stream to 8-bit gray alpha.
fn extract_smask(
    doc: &LopdfDocument,
    stream: &LopdfStream,
    page_num: usize,
    warnings: &mut Vec<PdfWarnMsg>,
) -> Option<SMaskData> {
    let smask_obj = stream.dict.get(b"SMask").ok()?;
    let smask_stream = match smask_obj {
        Object::Reference(r) => match doc.get_object(*r).ok()? {
            Object::Stream(s) => s,
            _ => return None,
        },
        Object::Stream(s) => s,
        _ => return None,
    };
    let width = usize::try_from(smask_stream.dict.get(b"Width").ok()?.as_i64().ok()?).ok()?;
    let height = usize::try_from(smask_stream.dict.get(b"Height").ok()?.as_i64().ok()?).ok()?;
    let bpc = smask_stream
        .dict
        .get(b"BitsPerComponent")
        .ok()
        .and_then(|o| o.as_i64().ok())
        .unwrap_or(8);
    if bpc != 8 {
        return None;
    }
    // The data cannot be larger than a stream may decode to, so a mask that declares more is
    // incomplete.
    let size = width.checked_mul(height)?;
    if size > MAX_STREAM_BYTES {
        return None;
    }
    let gray = decode_stream(smask_stream, MAX_STREAM_BYTES, "SMask", page_num, warnings)?;
    if gray.len() < size {
        return None;
    }
    Some(SMaskData {
        width,
        height,
        gray,
    })
}

/// Try to construct a [`RawImage`] directly from a raw-bitmap image stream
/// (no filter, or a lossless filter lopdf can undo). This is how printpdf
/// itself writes images by default (`FlateDecode` over raw pixels), so this
/// path is what makes printpdf's own output round-trippable: the pixel data
/// carries no container magic bytes, so `RawImage::decode_from_bytes` (which
/// sniffs PNG/JPEG/... signatures) can never decode it.
#[cfg(feature = "images")]
pub(crate) fn raw_bitmap_from_stream(
    doc: &LopdfDocument,
    stream: &LopdfStream,
) -> Option<RawImage> {
    raw_bitmap_from_stream_in(doc, stream, 0, &mut Vec::new())
}

/// [`raw_bitmap_from_stream`], with the page and the warnings of the parse that reads the image.
fn raw_bitmap_from_stream_in(
    doc: &LopdfDocument,
    stream: &LopdfStream,
    page_num: usize,
    warnings: &mut Vec<PdfWarnMsg>,
) -> Option<RawImage> {
    use crate::image_types::{RawImageData, RawImageFormat};

    let width = usize::try_from(stream.dict.get(b"Width").ok()?.as_i64().ok()?).ok()?;
    let height = usize::try_from(stream.dict.get(b"Height").ok()?.as_i64().ok()?).ok()?;
    if width == 0 || height == 0 {
        return None;
    }
    let bpc = stream
        .dict
        .get(b"BitsPerComponent")
        .ok()
        .and_then(|o| o.as_i64().ok())
        .unwrap_or(8);
    // Samples of 8 and 16 bits are read; other depths are kept as they are.
    let bytes_per_sample: usize = match bpc {
        8 => 1,
        16 => 2,
        _ => return None,
    };
    let components = colorspace_components(doc, stream.dict.get(b"ColorSpace").ok()?, 0)?;
    // /ImageMask stencils and predictor-encoded data have extra semantics the
    // raw path does not model; preserve those streams instead.
    if let Ok(im) = stream.dict.get(b"ImageMask") {
        if im.as_bool().unwrap_or(false) {
            return None;
        }
    }
    if let Ok(Object::Dictionary(dp)) = stream.dict.get(b"DecodeParms") {
        if dp
            .get(b"Predictor")
            .map(|p| p.as_i64().unwrap_or(1))
            .unwrap_or(1)
            > 1
        {
            return None;
        }
    }

    // The data cannot be larger than a stream may decode to, so a bitmap that declares more is
    // incomplete.
    let expected = width
        .checked_mul(height)?
        .checked_mul(components)?
        .checked_mul(bytes_per_sample)?;
    if expected > MAX_STREAM_BYTES {
        return None;
    }
    // A stream that cannot be decoded, or that is too large, is left to the caller, which tries it
    // as a container file and otherwise keeps it as it is (saying so when it is too large).
    let Decoded::Data(data) = decode_limited(stream, MAX_STREAM_BYTES) else {
        return None;
    };

    match (bpc, components) {
        (8, 3) | (8, 1) => {
            if data.len() < expected {
                return None;
            }
            let format = if components == 3 {
                RawImageFormat::RGB8
            } else {
                RawImageFormat::R8
            };
            let mut pixels = data;
            pixels.truncate(expected);
            let mut img = RawImage {
                pixels: RawImageData::U8(pixels),
                width,
                height,
                data_format: format,
                tag: Vec::new(),
            };
            if let Some(smask) = extract_smask(doc, stream, page_num, warnings) {
                img = apply_smask_to_rawimage(img, smask);
            }
            Some(img)
        }
        (16, 3) | (16, 1) => {
            let format = if components == 3 {
                RawImageFormat::RGB16
            } else {
                RawImageFormat::R16
            };
            let pixels: Vec<u16> = data
                .get(..expected)?
                .as_chunks::<2>()
                .0
                .iter()
                .map(|c| u16::from_be_bytes(*c))
                .collect();
            Some(RawImage {
                pixels: RawImageData::U16(pixels),
                width,
                height,
                data_format: format,
                tag: Vec::new(),
            })
        }
        _ => None,
    }
}

/// Parses all XObjects of a Resources dictionary into `xobj_map`. Images are decoded (or queued
/// for decoding); everything else, such as Form XObjects, or images with an unsupported filter or
/// color space, is kept verbatim so that a parse and save never drops page content. A name that
/// `xobj_map` has already is skipped: the first definition of a name wins.
fn parse_xobjects_internal(
    doc: &LopdfDocument,
    resources: &LopdfDictionary,
    warnings: &mut Vec<PdfWarnMsg>,
    page: Option<usize>,
    xobj_map: &mut BTreeMap<XObjectId, PendingXObject>,
    ctx: &mut XObjectCtx<'_>,
) {
    let page_num = page.unwrap_or_default();
    let xobj_obj = match resources.get(b"XObject") {
        Ok(s) => s,
        Err(_) => return,
    };

    // The XObject entry may be a dictionary or a reference.
    let xobj_dict = match get_dict_or_resolve_ref(
        &format!("parse_xobjects_internal 1 page {page_num}"),
        doc,
        xobj_obj,
        warnings,
        page,
    ) {
        Some(s) => s,
        None => return,
    };

    for (key, value) in xobj_dict.iter() {
        let xobject_id = XObjectId(String::from_utf8_lossy(key).to_string());
        if xobj_map.contains_key(&xobject_id) {
            continue;
        }

        // Every XObject is a stream object.
        let stream = match get_stream_or_resolve_ref(doc, value, warnings, page) {
            Some(s) => s,
            None => continue,
        };

        let subtype = match stream.dict.get(b"Subtype") {
            Ok(Object::Name(o)) => o.clone(),
            _ => {
                // Missing /Subtype: sniff — image streams have /Width + /Height.
                if stream.dict.get(b"Width").is_ok() && stream.dict.get(b"Height").is_ok() {
                    b"Image".to_vec()
                } else {
                    warnings.push(PdfWarnMsg::warning(
                        page_num,
                        0,
                        format!(
                            "parse-xobjects: XObject {} has no /Subtype; preserving verbatim",
                            xobject_id.0
                        ),
                    ));
                    let preserved =
                        preserve_xobject_stream(doc, stream, &xobject_id, page_num, &mut ctx.budget, warnings);
                    xobj_map.insert(
                        xobject_id,
                        PendingXObject::Ready(XObject::External(preserved)),
                    );
                    continue;
                }
            }
        };

        match subtype.as_slice() {
            b"Image" => {
                let filters = stream_filters(stream);
                let has_container_format = filters
                    .iter()
                    .any(|f| f == b"DCTDecode" || f == b"JPXDecode");
                let what = format!("XObject {}", xobject_id.0);

                let entry = if has_container_format {
                    // JPEG / JPEG2000: the (possibly Flate-wrapped) stream
                    // content *is* the container file.
                    let bytes = if filters.len() == 1 {
                        Some(stream.content.clone())
                    } else {
                        decode_stream(stream, MAX_STREAM_BYTES, &what, page_num, warnings)
                    };
                    match bytes {
                        Some(bytes) => PendingXObject::DecodeImage {
                            bytes,
                            smask: extract_smask(doc, stream, page_num, warnings),
                            fallback: preserve_xobject_stream(
                                doc, stream, &xobject_id, page_num, &mut ctx.budget, warnings,
                            ),
                        },
                        // Too large to decode: the stream stays as it is.
                        None => PendingXObject::Ready(XObject::External(
                            preserve_xobject_stream(
                                doc, stream, &xobject_id, page_num, &mut ctx.budget, warnings,
                            ),
                        )),
                    }
                } else if let Some(img) = raw_bitmap_from_stream_in(doc, stream, page_num, warnings)
                {
                    // Without the `images` feature the bitmap stream is kept verbatim as an
                    // external XObject instead of a decoded image.
                    if cfg!(feature = "images") {
                        PendingXObject::Ready(XObject::Image(img))
                    } else {
                        PendingXObject::Ready(XObject::External(preserve_xobject_stream(
                            doc, stream, &xobject_id, page_num, &mut ctx.budget, warnings,
                        )))
                    }
                } else {
                    // Could still be an embedded container file without a
                    // filter entry (rare), or something we cannot model —
                    // try the decoder, fall back to byte-level preservation.
                    match decode_stream(stream, MAX_STREAM_BYTES, &what, page_num, warnings) {
                        Some(bytes) => PendingXObject::DecodeImage {
                            bytes,
                            smask: extract_smask(doc, stream, page_num, warnings),
                            fallback: preserve_xobject_stream(
                                doc, stream, &xobject_id, page_num, &mut ctx.budget, warnings,
                            ),
                        },
                        None => PendingXObject::Ready(XObject::External(
                            preserve_xobject_stream(
                                doc, stream, &xobject_id, page_num, &mut ctx.budget, warnings,
                            ),
                        )),
                    }
                };
                xobj_map.insert(xobject_id, entry);
            }
            b"Form" => {
                // A form that can be modelled becomes a `XObject::Form`; otherwise it is kept
                // verbatim, references inlined.
                let entry = match parse_form(doc, value, stream, &xobject_id, page_num, ctx, warnings)
                {
                    Some(form) => PendingXObject::Form(form),
                    None => PendingXObject::Ready(XObject::External(preserve_xobject_stream(
                        doc,
                        stream,
                        &xobject_id,
                        page_num,
                        &mut ctx.budget,
                        warnings,
                    ))),
                };
                xobj_map.insert(xobject_id, entry);
            }
            _other => {
                // Any other subtype is kept verbatim, references inlined.
                let preserved =
                    preserve_xobject_stream(doc, stream, &xobject_id, page_num, &mut ctx.budget, warnings);
                xobj_map.insert(
                    xobject_id,
                    PendingXObject::Ready(XObject::External(preserved)),
                );
            }
        }
    }
}

fn parse_fonts_from_entire_pdf(
    doc: &LopdfDocument,
    objs_to_search_for_resources: &[(LopdfObject, Option<usize>)],
    warnings: &mut Vec<PdfWarnMsg>,
) -> BTreeMap<BuiltinOrExternalFontId, ParsedOrBuiltinFont> {
    let mut map = BuiltinFont::all_ids()
        .iter()
        .map(|b| {
            (
                BuiltinOrExternalFontId::Builtin(*b),
                ParsedOrBuiltinFont::B(*b),
            )
        })
        .collect::<BTreeMap<_, _>>();

    for (obj, page_idx) in objs_to_search_for_resources {
        let page_num = page_idx.unwrap_or(0);
        let dict = match obj {
            LopdfObject::Dictionary(d) => Some(d),
            LopdfObject::Reference(r) => doc.get_dictionary(*r).ok(),
            _ => None,
        };
        let Some(dict) = dict else { continue };
        let Ok(resources_obj) = dict.get(b"Resources") else {
            continue;
        };
        let Some(resources_dict) = get_dict_or_resolve_ref(
            &format!("parse_fonts_from_entire_pdf 1 page {page_num}"),
            doc,
            resources_obj,
            warnings,
            Some(page_num),
        ) else {
            continue;
        };
        // The first definition of a font name wins, so names that are already in `map` are not
        // read again.
        let fonts =
            parsefont::parse_fonts_enhanced(doc, resources_dict, warnings, Some(page_num), &map);
        for (font_id, parsed_font) in fonts {
            map.entry(BuiltinOrExternalFontId::External(font_id))
                .or_insert(parsed_font);
        }
    }
    map
}

fn parse_xobjects_from_entire_pdf(
    doc: &LopdfDocument,
    objs_to_search_for_resources: &[(LopdfObject, Option<usize>)],
    font_decode: &BTreeMap<String, FontTextDecodeKind>,
    warnings: &mut Vec<PdfWarnMsg>,
) -> BTreeMap<XObjectId, PendingXObject> {
    let mut xobjects = BTreeMap::new();
    let mut ctx = XObjectCtx::new(font_decode);
    for (obj, page_idx) in objs_to_search_for_resources {
        let page_num = page_idx.unwrap_or(0);
        let dict = match obj {
            LopdfObject::Dictionary(d) => Some(d),
            LopdfObject::Reference(r) => doc.get_dictionary(*r).ok(),
            _ => None,
        };
        let Some(dict) = dict else { continue };
        let Ok(resources_obj) = dict.get(b"Resources") else {
            continue;
        };
        let Some(resources_dict) = get_dict_or_resolve_ref(
            &format!("parse_xobjects_from_entire_pdf 1 page {page_num}"),
            doc,
            resources_obj,
            warnings,
            Some(page_num),
        ) else {
            continue;
        };
        parse_xobjects_internal(
            doc,
            resources_dict,
            warnings,
            Some(page_num),
            &mut xobjects,
            &mut ctx,
        );
    }
    xobjects
}

/// Recursively collects page object references from a Pages tree dictionary.
fn collect_page_refs(
    dict: &LopdfDictionary,
    doc: &LopdfDocument,
    warnings: &mut Vec<PdfWarnMsg>,
) -> Result<Vec<ObjectId>, String> {
    let mut pages = Vec::new();
    let mut visited = std::collections::BTreeSet::new();
    collect_page_refs_inner(dict, doc, &mut pages, &mut visited, warnings, 0)?;
    Ok(pages)
}

fn collect_page_refs_inner(
    dict: &LopdfDictionary,
    doc: &LopdfDocument,
    pages: &mut Vec<ObjectId>,
    visited: &mut std::collections::BTreeSet<ObjectId>,
    warnings: &mut Vec<PdfWarnMsg>,
    depth: usize,
) -> Result<(), String> {
    // A malformed (or hostile) page tree can contain reference cycles; without
    // the visited set + depth cap this recursion would never terminate.
    if depth > 64 {
        warnings.push(PdfWarnMsg::warning(
            0,
            0,
            "Pages tree deeper than 64 levels, ignoring deeper nodes".to_string(),
        ));
        return Ok(());
    }

    // The Pages tree must have a "Kids" array (possibly behind a reference).
    let kids = dict
        .get(b"Kids")
        .map_err(|e| format!("Pages dictionary missing Kids key: {}", e))?;
    let kids = match kids {
        Object::Reference(r) => doc
            .get_object(*r)
            .map_err(|e| format!("Failed to resolve Pages.Kids reference: {}", e))?,
        other => other,
    };

    let page_refs = kids
        .as_array()
        .map(|s| {
            s.iter()
                .filter_map(|k| k.as_reference().ok())
                .collect::<Vec<_>>()
        })
        .map_err(|_| "Pages.Kids is not an array".to_string())?;

    for r in page_refs {
        if !visited.insert(r) {
            warnings.push(PdfWarnMsg::warning(
                0,
                0,
                format!(
                    "Pages tree contains object {:?} more than once, skipping",
                    r
                ),
            ));
            continue;
        }
        let kid_obj = match doc.get_object(r) {
            Ok(o) => o,
            Err(e) => {
                warnings.push(PdfWarnMsg::warning(
                    0,
                    0,
                    format!("Failed to get pages-tree kid object {:?}: {}", r, e),
                ));
                continue;
            }
        };

        if let Ok(kid_dict) = kid_obj.as_dict() {
            // /Type is required by the spec but missing in plenty of real
            // documents; infer the node kind from the presence of /Kids.
            let type_name = match kid_dict.get(b"Type") {
                Ok(Object::Name(t)) => Some(t.clone()),
                _ => None,
            };
            match type_name.as_deref() {
                Some(b"Page") => pages.push(r),
                Some(b"Pages") => {
                    collect_page_refs_inner(kid_dict, doc, pages, visited, warnings, depth + 1)?
                }
                Some(other) => {
                    warnings.push(PdfWarnMsg::warning(
                        0,
                        0,
                        format!(
                            "Unknown pages-tree kid type {:?}, skipping",
                            String::from_utf8_lossy(other)
                        ),
                    ));
                }
                None => {
                    if kid_dict.has(b"Kids") {
                        collect_page_refs_inner(kid_dict, doc, pages, visited, warnings, depth + 1)?
                    } else {
                        warnings.push(PdfWarnMsg::warning(
                            0,
                            0,
                            format!("Pages-tree kid {:?} missing /Type, treating as /Page", r),
                        ));
                        pages.push(r);
                    }
                }
            }
        }
    }

    Ok(())
}

/// Look up an inheritable page attribute (`MediaBox`, `CropBox`, `Resources`,
/// `Rotate`, ...): first on the page dictionary itself, then up the `/Parent`
/// chain (ISO 32000-1, 7.7.3.4). Depth-capped to survive cyclic parent links.
fn get_inheritable_page_attr<'a>(
    page: &'a LopdfDictionary,
    doc: &'a LopdfDocument,
    key: &[u8],
) -> Option<&'a Object> {
    let mut current = page;
    for _ in 0..64 {
        if let Ok(obj) = current.get(key) {
            return Some(obj);
        }
        let parent = current.get(b"Parent").ok()?;
        let parent_ref = parent.as_reference().ok()?;
        current = doc.get_dictionary(parent_ref).ok()?;
    }
    None
}

/// Append the bytes of one content stream, given an entry of the `/Contents`
/// value (reference or inline stream). Warns instead of failing on bad
/// entries: a partly readable page is better than no document.
fn append_content_stream(
    doc: &LopdfDocument,
    obj: &Object,
    page_num: usize,
    content_data: &mut Vec<u8>,
    warnings: &mut Vec<PdfWarnMsg>,
) {
    let stream = match obj {
        Object::Reference(r) => match doc.get_object(*r).and_then(|o| o.as_stream()) {
            Ok(s) => s,
            Err(e) => {
                warnings.push(PdfWarnMsg::error(
                    page_num,
                    0,
                    format!("Failed to resolve content stream {:?}: {}", r, e),
                ));
                return;
            }
        },
        Object::Stream(s) => s,
        _other => {
            warnings.push(PdfWarnMsg::error(
                page_num,
                0,
                "Content entry is not a stream".to_string(),
            ));
            return;
        }
    };
    // The streams of one page share the limit of a stream.
    let limit = MAX_STREAM_BYTES.saturating_sub(content_data.len());
    let Some(data) = decode_stream(stream, limit, "Page content", page_num, warnings) else {
        return;
    };
    content_data.extend(data);
    // PDF 32000-1, 7.8.2: when /Contents is an array, the division between
    // streams can fall in the middle of a token; the streams must be parsed
    // as if separated by (at least) a byte of whitespace.
    content_data.push(b'\n');
}

/// Parses a single page dictionary into a PdfPage.
fn parse_page(
    num: usize,
    page: &LopdfDictionary,
    doc: &LopdfDocument,
    xobjects: &BTreeMap<XObjectId, XObject>,
    font_decode: &BTreeMap<String, FontTextDecodeKind>,
    warnings: &mut Vec<PdfWarnMsg>,
) -> Result<PdfPage, String> {
    // MediaBox is required but may be inherited from the parent Pages node. Without one, fall
    // back to A4 with a warning.
    let media_box = match get_inheritable_page_attr(page, doc, b"MediaBox") {
        Some(obj) => match parse_rect(obj) {
            Ok(r) => r,
            Err(e) => {
                warnings.push(PdfWarnMsg::warning(
                    num,
                    0,
                    format!("Invalid MediaBox: {e}; defaulting to A4"),
                ));
                default_media_box()
            }
        },
        None => {
            warnings.push(PdfWarnMsg::warning(
                num,
                0,
                "Page missing MediaBox (also not inherited); defaulting to A4".to_string(),
            ));
            default_media_box()
        }
    };
    // TrimBox and CropBox are optional (and inheritable); use MediaBox as default.
    let trim_box = match get_inheritable_page_attr(page, doc, b"TrimBox").map(parse_rect) {
        Some(Ok(r)) => r,
        _ => media_box.clone(),
    };
    let crop_box = match get_inheritable_page_attr(page, doc, b"CropBox").map(parse_rect) {
        Some(Ok(r)) => r,
        _ => media_box.clone(),
    };

    // Get the Contents entry (could be a reference, an array, or a stream).
    // Contents is *optional* — a page without it is simply empty.
    let mut content_data = Vec::new();
    match page.get(b"Contents") {
        Ok(Object::Array(arr)) => {
            for obj in arr {
                append_content_stream(doc, obj, num, &mut content_data, warnings);
            }
        }
        Ok(Object::Reference(r)) => {
            // A /Contents reference may point at a stream *or* at an array of
            // stream references.
            match doc.get_object(*r) {
                Ok(Object::Array(arr)) => {
                    for obj in arr {
                        append_content_stream(doc, obj, num, &mut content_data, warnings);
                    }
                }
                Ok(other) => {
                    append_content_stream(doc, other, num, &mut content_data, warnings);
                }
                Err(e) => {
                    warnings.push(PdfWarnMsg::error(
                        num,
                        0,
                        format!("Failed to get content stream: {}", e),
                    ));
                }
            }
        }
        Ok(other) => {
            append_content_stream(doc, other, num, &mut content_data, warnings);
        }
        Err(_) => {
            warnings.push(PdfWarnMsg::info(
                num,
                0,
                "Page has no /Contents (empty page)".to_string(),
            ));
        }
    }

    // A page whose content stream fails to tokenize becomes an empty page
    // with a warning instead of failing the entire document.
    let pattern_spaces = get_inheritable_page_attr(page, doc, b"Resources")
        .and_then(|resources| {
            get_dict_or_resolve_ref("page /Resources", doc, resources, &mut Vec::new(), Some(num))
        })
        .map(|resources| pattern::pattern_space_names(doc, resources))
        .unwrap_or_default();
    let ops = match decode_content(&content_data) {
        Ok(operations) => convert_operations(
            num,
            &operations,
            xobjects,
            font_decode,
            &pattern_spaces,
            warnings,
        )?,
        Err(e) => {
            warnings.push(PdfWarnMsg::error(num, 0, e));
            Vec::new()
        }
    };

    Ok(PdfPage {
        media_box,
        trim_box,
        crop_box,
        ops,
    })
}

/// Splits the bytes of a content stream (a page's or a form's) into operators and operands.
fn decode_content(bytes: &[u8]) -> Result<Vec<lopdf::content::Operation>, String> {
    lopdf::content::Content::decode(bytes)
        .map(|content| content.operations)
        .map_err(|e| format!("Failed to decode content stream: {}", e))
}

/// Turns the operators of a content stream into [`Op`]s. `font_decode` says how the text of each
/// font resource name decodes: it is the scope the content stream names its resources in, the
/// resources of the document for a page, and the ones of the form for a form. `pattern_spaces`
/// are the names of the `[/Pattern ...]` color spaces of that scope. `page` is the page the
/// warnings are about.
fn convert_operations(
    page: usize,
    operations: &[lopdf::content::Operation],
    xobjects: &BTreeMap<XObjectId, XObject>,
    font_decode: &BTreeMap<String, FontTextDecodeKind>,
    pattern_spaces: &BTreeSet<String>,
    warnings: &mut Vec<PdfWarnMsg>,
) -> Result<Vec<Op>, String> {
    let mut state = PageState {
        font_decode: font_decode.clone(),
        pattern_spaces: pattern_spaces.clone(),
        ..PageState::default()
    };
    let mut ops = Vec::new();
    for (op_id, op) in operations.iter().enumerate() {
        ops.extend(parse_op(page, op_id, op, &mut state, xobjects, warnings)?);
    }
    Ok(ops)
}

/// A4 portrait in PDF points, the fallback when a document specifies no
/// usable MediaBox anywhere.
fn default_media_box() -> Rect {
    Rect::from_xywh(Pt(0.0), Pt(0.0), Pt(595.28), Pt(841.89))
}

/// State carried across operators while one page's content stream is converted by
/// [`parse_op`]: the current font, text position, open path and so on.
#[derive(Debug, Clone, Default)]
pub struct PageState {
    /// True if we are inside a `BT ... ET` text block
    pub in_text_mode: bool,

    /// Current font resource and size (only relevant if `in_text_mode` = true)
    pub current_font: Option<crate::BuiltinOrExternalFontId>,

    /// Size selected by the last `Tf`.
    pub current_font_size: Option<crate::units::Pt>,

    /// Current font resource name (e.g., "F5") - tracked only to emit warnings if text appears without Tf
    pub current_font_resource: Option<String>,

    /// How text-showing operator bytes decode for each font resource name of
    /// this page's resources. Filled in by `parse_page` before the ops run.
    pub font_decode: BTreeMap<String, FontTextDecodeKind>,

    /// The decode kind of the currently selected font (set by `Tf`).
    pub current_font_decode: Option<FontTextDecodeKind>,

    /// The names of the `[/Pattern ...]` color spaces in the resources of the content stream.
    /// `cs` and `CS` with one of them, or with `Pattern`, selects the pattern color space, which
    /// [`Op::SetFillPattern`] and [`Op::SetOutlinePattern`] imply.
    pub pattern_spaces: std::collections::BTreeSet<String>,

    /// Current transformation matrix stack. Each entry is a 6-float array [a b c d e f]. The
    /// stack has a depth limit; `q` operators beyond it are counted in
    /// [`PageState::untracked_saves`].
    pub transform_stack: Vec<[f32; 6]>,

    /// Number of open `q` operators beyond the depth limit of [`PageState::transform_stack`]. A
    /// `Q` ends one of these before it pops the stack.
    pub untracked_saves: usize,

    /// Absolute translation of the text line matrix. `Td` is relative, so it is accumulated here
    /// and emitted as an absolute [`Op::SetTextCursor`]. Reset by `BT`, set by `Tm`.
    pub text_line: [f32; 2],
    /// Text leading (from `TL`/`TD`), so `T*` advances the tracked line by the
    /// right amount.
    pub text_leading: f32,

    /// Name of the current marked content, if any (set by BMC/BDC). The stack has a depth limit;
    /// `BMC` and `BDC` operators beyond it are counted in
    /// [`PageState::untracked_marked_content`].
    pub current_marked_content: Vec<String>,

    /// Number of open `BMC` and `BDC` operators beyond the depth limit of
    /// [`PageState::current_marked_content`]. An `EMC` ends one of these before it pops the stack.
    pub untracked_marked_content: usize,

    /// The path under construction (`m`, `l`, `c`, `re`, ...), until a painting operator ends it.
    pub path_builder: PathBuilder,

    /// Font and size of the last `SetFont` op that was emitted.
    pub last_emitted_font_size: Option<(FontId, Pt)>,

    /// Holds a rectangle whose winding order is set by `W` and `W*`.
    pub rectangle_builder: RectangleBuilder,
}

// A content stream can open `q`, `BMC` and `BDC` without ever closing them. Past these depths the
// operators are only counted: their ops are still emitted and the matching `Q` and `EMC` still
// balance, but the stacks stay small. ISO 32000-1, Annex C, gives 28 as the `q` depth a reader has
// to support.
const MAX_GRAPHICS_STATE_DEPTH: usize = 256;
const MAX_MARKED_CONTENT_DEPTH: usize = 256;

/// Holds the rectangle that `W` and `W*` set the winding order of.
#[derive(Debug, Clone, Default)]
pub struct RectangleBuilder {
    rectangle: Option<Rect>,
}

impl RectangleBuilder {
    /// Replaces the held rectangle with one at `(x, y)` of the given size.
    pub fn new(&mut self, x: Pt, y: Pt, width: Pt, height: Pt) {
        self.rectangle = Some(Rect::from_xywh(x, y, width, height));
    }

    /// Sets the winding order of the held rectangle, if there is one.
    pub fn set_winding_order(&mut self, winding_order: WindingOrder) {
        if let Some(ref mut rectangle) = self.rectangle {
            rectangle.winding_order = Some(winding_order);
        }
    }

    /// [`Op::DrawRectangle`] for the held rectangle, or no ops when there is none.
    pub fn get_ops(&self) -> Vec<Op> {
        match &self.rectangle {
            Some(rectangle) => vec![Op::DrawRectangle {
                rectangle: rectangle.clone(),
            }],
            None => vec![],
        }
    }

    /// Drops the held rectangle.
    pub fn clear(&mut self) {
        self.rectangle = None;
    }
}

/// One segment of a subpath.
#[derive(Debug, Clone, PartialEq)]
pub enum PathOperation {
    /// Starts a new subpath at the point (`m`).
    MoveTo(Point),
    /// Straight line to the point (`l`).
    LineTo(Point),
    /// Cubic Bezier curve to `endpoint` (`c`, `v`, `y`).
    CurveTo {
        /// First control point.
        control1: Point,
        /// Second control point.
        control2: Point,
        /// End point of the curve.
        endpoint: Point,
    },
    /// Closes the subpath with a straight line to its start (`h`).
    ClosePath,
}

/// A subpath: a sequence of segments and whether it was closed with `h`.
#[derive(Debug, Clone, PartialEq)]
pub struct PdfSubPath {
    operations: Vec<PathOperation>,
    is_closed: bool,
}

/// A path (list of subpaths) with the paint mode and winding order that were chosen by the
/// operator that ended it.
#[derive(Debug, Clone, PartialEq)]
pub struct PdfPath {
    subpaths: Vec<PdfSubPath>,
    paint_mode: PaintMode,
    winding_order: WindingOrder,
}

/// Collects the path operators (`m`, `l`, `c`, `v`, `y`, `h`, `re`) of a content stream until a
/// painting operator turns them into a [`PdfPath`].
#[derive(Debug, Clone, Default)]
pub struct PathBuilder {
    current_subpath: Vec<PathOperation>,
    subpaths: Vec<Vec<PathOperation>>,
    current_point: Option<Point>,
    start_point: Option<Point>,
    /// True once `W` or `W*` has emitted the clip of the path under construction.
    clip_emitted: bool,
}

impl PathBuilder {
    /// `m`: starts a new subpath at `point`.
    pub fn move_to(&mut self, point: Point) {
        if !self.current_subpath.is_empty() {
            self.subpaths
                .push(std::mem::take(&mut self.current_subpath));
        }

        self.current_subpath.push(PathOperation::MoveTo(point));
        self.current_point = Some(point);
        self.start_point = Some(point);
    }

    /// `l`: line to `point`; starts a subpath there if there is no current point.
    pub fn line_to(&mut self, point: Point) {
        if self.current_point.is_none() {
            self.move_to(point);
            return;
        }

        self.current_subpath.push(PathOperation::LineTo(point));
        self.current_point = Some(point);
    }

    /// `c`: cubic curve with two control points.
    pub fn curve_to(&mut self, control1: Point, control2: Point, endpoint: Point) {
        if self.current_point.is_none() {
            self.move_to(control1);
            self.curve_to(control1, control2, endpoint);
            return;
        }

        self.current_subpath.push(PathOperation::CurveTo {
            control1,
            control2,
            endpoint,
        });
        self.current_point = Some(endpoint);
    }

    /// v-curve: current point is first control point
    pub fn v_curve_to(&mut self, control2: Point, endpoint: Point) {
        if let Some(current) = self.current_point {
            self.curve_to(current, control2, endpoint);
        } else {
            // If no current point, treat as regular curve_to
            self.move_to(control2);
            self.curve_to(control2, control2, endpoint);
        }
    }

    /// y-curve: endpoint is second control point
    pub fn y_curve_to(&mut self, control1: Point, endpoint: Point) {
        if self.current_point.is_none() {
            self.move_to(control1);
        }

        self.curve_to(control1, endpoint, endpoint);
    }

    /// `h`: closes the current subpath. Does nothing on an empty subpath.
    pub fn close_path(&mut self) {
        if self.current_subpath.is_empty() {
            return;
        }

        self.current_subpath.push(PathOperation::ClosePath);

        // Connect back to start point
        if let (Some(start), Some(current)) = (self.start_point, self.current_point) {
            if current != start {
                self.current_point = Some(start);
            }
        }
    }

    /// Forgets all subpaths and the current point.
    pub fn clear(&mut self) {
        self.current_subpath.clear();
        self.subpaths.clear();
        self.current_point = None;
        self.start_point = None;
        self.clip_emitted = false;
    }

    #[cfg(test)]
    pub fn finalize_subpath(&mut self) {
        if !self.current_subpath.is_empty() {
            self.subpaths
                .push(std::mem::take(&mut self.current_subpath));
            self.current_point = None;
            self.start_point = None;
        }
    }

    #[cfg(test)]
    pub fn is_empty(&self) -> bool {
        self.current_subpath.is_empty() && self.subpaths.is_empty()
    }

    /// The collected subpaths as a [`PdfPath`] with the given paint mode and winding order.
    pub fn build(&self, paint_mode: PaintMode, winding_order: WindingOrder) -> PdfPath {
        let mut all_subpaths = self.subpaths.clone();
        if !self.current_subpath.is_empty() {
            all_subpaths.push(self.current_subpath.clone());
        }

        let subpaths = all_subpaths
            .into_iter()
            .map(|ops| {
                let is_closed = ops.iter().any(|op| matches!(op, PathOperation::ClosePath));
                PdfSubPath {
                    operations: ops,
                    is_closed,
                }
            })
            .collect();

        PdfPath {
            subpaths,
            paint_mode,
            winding_order,
        }
    }
}

/// Converts a path to an op. A single open subpath that is only stroked becomes
/// [`Op::DrawLine`]; anything else becomes [`Op::DrawPolygon`] with one ring per subpath.
pub fn path_to_op(path: &PdfPath) -> Op {
    let mut all_rings = Vec::new();

    for subpath in &path.subpaths {
        let points = subpath_to_line_points(subpath);
        if !points.is_empty() {
            all_rings.push(PolygonRing { points });
        }
    }

    if matches!(path.subpaths.as_slice(), [only] if !only.is_closed)
        && path.paint_mode == PaintMode::Stroke
    {
        if let Some(ring) = all_rings.first() {
            return Op::DrawLine {
                line: Line {
                    points: ring.points.clone(),
                    is_closed: false,
                },
            };
        }
    }

    Op::DrawPolygon {
        polygon: Polygon {
            rings: all_rings,
            mode: path.paint_mode,
            winding_order: path.winding_order,
        },
    }
}

/// Parse a PDF content stream into path operations
#[cfg(test)]
fn parse_pdf_stream(stream: &str) -> Vec<(String, Vec<f32>)> {
    let mut operations = Vec::new();
    let mut current_operands = Vec::new();

    // Simple tokenizer for PDF path operations
    let tokens = stream.split_whitespace().peekable();

    for token in tokens {
        // Check if token is a number
        if let Ok(num) = token.parse::<f32>() {
            current_operands.push(num);
            continue;
        }

        // If not a number, it's an operator
        if token == "m"
            || token == "l"
            || token == "c"
            || token == "v"
            || token == "y"
            || token == "h"
            || token == "f"
            || token == "S"
            || token == "s"
            || token == "f*"
            || token == "B"
            || token == "B*"
            || token == "b"
            || token == "b*"
        {
            // Store the operation
            operations.push((token.to_string(), current_operands.clone()));
            current_operands = Vec::new();
        }
    }

    operations
}

/// Apply a parsed operation to the PathBuilder
#[cfg(test)]
fn apply_path_operation(builder: &mut PathBuilder, op: &(String, Vec<f32>)) {
    match op.0.as_str() {
        "m" => {
            if op.1.len() >= 2 {
                builder.move_to(Point {
                    x: Pt(op.1[0]),
                    y: Pt(op.1[1]),
                });
            }
        }
        "l" => {
            if op.1.len() >= 2 {
                builder.line_to(Point {
                    x: Pt(op.1[0]),
                    y: Pt(op.1[1]),
                });
            }
        }
        "c" => {
            if op.1.len() >= 6 {
                builder.curve_to(
                    Point {
                        x: Pt(op.1[0]),
                        y: Pt(op.1[1]),
                    },
                    Point {
                        x: Pt(op.1[2]),
                        y: Pt(op.1[3]),
                    },
                    Point {
                        x: Pt(op.1[4]),
                        y: Pt(op.1[5]),
                    },
                );
            }
        }
        "v" => {
            if op.1.len() >= 4 {
                builder.v_curve_to(
                    Point {
                        x: Pt(op.1[0]),
                        y: Pt(op.1[1]),
                    },
                    Point {
                        x: Pt(op.1[2]),
                        y: Pt(op.1[3]),
                    },
                );
            }
        }
        "y" => {
            if op.1.len() >= 4 {
                builder.y_curve_to(
                    Point {
                        x: Pt(op.1[0]),
                        y: Pt(op.1[1]),
                    },
                    Point {
                        x: Pt(op.1[2]),
                        y: Pt(op.1[3]),
                    },
                );
            }
        }
        "h" => {
            builder.close_path();
        }
        "f" | "S" | "s" | "f*" | "B" | "B*" | "b" | "b*" => {
            // These are painting operators, not path construction
            // For the test, we handle them by finalizing the current subpath
            builder.finalize_subpath();
        }
        _ => {}
    }
}

/// Convert a PdfSubPath to a sequence of LinePoints with proper bezier flags
fn subpath_to_line_points(subpath: &PdfSubPath) -> Vec<LinePoint> {
    let mut points = Vec::new();

    for operation in &subpath.operations {
        match operation {
            PathOperation::MoveTo(point) => {
                points.push(LinePoint {
                    p: *point,
                    bezier: false,
                });
            }
            PathOperation::LineTo(point) => {
                points.push(LinePoint {
                    p: *point,
                    bezier: false,
                });
            }
            PathOperation::CurveTo {
                control1,
                control2,
                endpoint,
            } => {
                // First control point - always bezier=true
                points.push(LinePoint {
                    p: *control1,
                    bezier: true,
                });
                // Second control point - always bezier=true
                points.push(LinePoint {
                    p: *control2,
                    bezier: true,
                });
                // Endpoint - always bezier=false
                points.push(LinePoint {
                    p: *endpoint,
                    bezier: false,
                });
            }
            PathOperation::ClosePath => {
                // Nothing to add for close path
            }
        }
    }

    points
}

#[test]
fn test_2() {
    let pdf_stream = "
    20.5 344.5 m
    20.5 344.5 22 333.5 10.5 346.5 c
    S"
    .trim();

    let expected_output = Op::DrawLine {
        line: Line {
            points: vec![
                LinePoint {
                    p: Point {
                        x: Pt(20.5),
                        y: Pt(344.5),
                    },
                    bezier: false,
                },
                LinePoint {
                    p: Point {
                        x: Pt(20.5),
                        y: Pt(344.5),
                    },
                    bezier: true,
                },
                LinePoint {
                    p: Point {
                        x: Pt(22.0),
                        y: Pt(333.5),
                    },
                    bezier: true,
                },
                LinePoint {
                    p: Point {
                        x: Pt(10.5),
                        y: Pt(346.5),
                    },
                    bezier: false,
                },
            ],
            is_closed: false,
        },
    };

    // Parse using the PathBuilder
    let mut builder = PathBuilder::default();
    let ops = parse_pdf_stream(pdf_stream);
    for op in ops {
        apply_path_operation(&mut builder, &op);
    }

    // Build path and generate commands
    let path = builder.build(PaintMode::Stroke, WindingOrder::NonZero);
    let draw_op = path_to_op(&path);

    // Verify bezier flags are correctly set
    assert_eq!(draw_op, expected_output);
}

#[test]
fn test_bezier_path_parsing() {
    // Test case from tiger.svg
    let pdf_stream = "-122.3 84.285 m -122.3 84.285 -122.2 86.179 -123.03 86.16 c -123.85 86.141 \
                      -140.3 38.066 -160.83 40.309 c -160.83 40.309 -143.05 32.956 -122.3 84.285 \
                      c h f";

    // Parse using the PathBuilder
    let mut builder = PathBuilder::default();
    let ops = parse_pdf_stream(pdf_stream);
    for op in ops {
        apply_path_operation(&mut builder, &op);
    }

    // Build path and generate commands
    let path = builder.build(PaintMode::Fill, WindingOrder::NonZero);
    let draw_op = path_to_op(&path);

    // Verify bezier flags are correctly set
    if let Op::DrawPolygon { polygon } = draw_op {
        assert_eq!(polygon.rings.len(), 1);
        assert_eq!(polygon.rings[0].points.len(), 10);

        // Check flags for points
        let points = &polygon.rings[0].points;
        assert!(!points[0].bezier); // Start point
        assert!(points[1].bezier); // Control point 1
        assert!(points[2].bezier); // Control point 2
        assert!(!points[3].bezier); // End point
        assert!(points[4].bezier); // Next control point 1
                                   // And so on...
    } else {
        panic!("Expected DrawPolygon");
    }
}

/// Decode the byte string of one text-showing operator according to the
/// currently selected font (see [`FontTextDecodeKind`]).
///
/// * Built-in / simple fonts: one byte per code → decoded to a
///   [`crate::text::TextItem::Text`] (ToUnicode CMap first, WinAnsi fallback),
///   which the serializer re-encodes correctly for whichever font ends up
///   selected at save time.
/// * Composite (Type0) fonts: two-byte big-endian CIDs → [`crate::text::TextItem::GlyphIds`]
///   with each glyph's Unicode string attached from the ToUnicode CMap, so
///   text extraction and regenerated ToUnicode CMaps stay correct.
/// * No font information: the string is decoded with
///   [`decode_tj_string_as_glyph_ids`](crate::text::decode_tj_string_as_glyph_ids).
fn decode_show_text_string(
    bytes: &[u8],
    kind: Option<&FontTextDecodeKind>,
    page: usize,
    op_id: usize,
    warnings: &mut Vec<PdfWarnMsg>,
) -> crate::text::TextItem {
    use crate::text::{decode_simple_font_bytes, Codepoint, TextItem};
    match kind {
        Some(FontTextDecodeKind::Builtin(_)) => {
            TextItem::Text(decode_simple_font_bytes(bytes, None))
        }
        Some(FontTextDecodeKind::Simple { to_unicode }) => {
            TextItem::Text(decode_simple_font_bytes(bytes, to_unicode.as_deref()))
        }
        Some(FontTextDecodeKind::Cid {
            to_unicode,
            encoding_cmap,
            cid_to_gid,
        }) => {
            // bytes -> codes: variable-width per the /Encoding CMap's codespace
            // ranges, or fixed 2-byte big-endian for Identity-H (`None`).
            let codes: Vec<(u32, u8)> = match encoding_cmap {
                Some(cmap) => cmap.split_codes(bytes),
                None => {
                    if !bytes.len().is_multiple_of(2) {
                        warnings.push(PdfWarnMsg::warning(
                            page,
                            op_id,
                            format!(
                                "CID font string has odd byte length {}, dropping \
                                 trailing byte",
                                bytes.len()
                            ),
                        ));
                    }
                    bytes
                        .as_chunks::<2>()
                        .0
                        .iter()
                        .map(|c| (u32::from(u16::from_be_bytes(*c)), 2))
                        .collect()
                }
            };
            let glyphs = codes
                .into_iter()
                .map(|(code, len)| {
                    // ToUnicode is keyed by the content-stream CODE; the glyph id
                    // stored on the Codepoint must be the real GID: code -> CID
                    // (encoding CMap) -> GID (CIDToGIDMap stream / CFF charset),
                    // so outlines, widths and re-subsetting stay correct.
                    let cid_str = to_unicode
                        .as_deref()
                        .and_then(|cmap| cmap.lookup_string(code));
                    let cid = encoding_cmap
                        .as_deref()
                        .map_or(code, |m| m.cid_for_code(code, len));
                    let cid = u16::try_from(cid).unwrap_or(0);
                    let gid = cid_to_gid
                        .as_deref()
                        .and_then(|m| m.get(&cid).copied())
                        .unwrap_or(cid);
                    match cid_str {
                        Some(s) => Codepoint::with_cid(gid, 0.0, s),
                        None => Codepoint::new(gid, 0.0),
                    }
                })
                .collect();
            TextItem::GlyphIds(glyphs)
        }
        None => {
            // No font information: text shown before any usable `Tf`, which is rare.
            let items = crate::text::decode_tj_string_as_glyph_ids(bytes);
            items
                .into_iter()
                .next()
                .unwrap_or(TextItem::GlyphIds(Vec::new()))
        }
    }
}

/// Decode a whole `TJ` array (strings interleaved with kerning offsets).
fn decode_show_text_array(
    arr: &[Object],
    kind: Option<&FontTextDecodeKind>,
    page: usize,
    op_id: usize,
    warnings: &mut Vec<PdfWarnMsg>,
) -> Vec<crate::text::TextItem> {
    use crate::text::TextItem;
    let mut items = Vec::new();
    for obj in arr {
        match obj {
            Object::String(bytes, _) => {
                items.push(decode_show_text_string(bytes, kind, page, op_id, warnings));
            }
            Object::Integer(i) => items.push(TextItem::Offset(*i as f32)),
            Object::Real(r) => items.push(TextItem::Offset(*r)),
            _ => {}
        }
    }
    items
}

/// Decode a text-showing string to a plain String (for the `'` and `"`
/// operators, whose printpdf ops carry `String` text).
fn decode_show_text_to_string(
    bytes: &[u8],
    kind: Option<&FontTextDecodeKind>,
    page: usize,
    op_id: usize,
    warnings: &mut Vec<PdfWarnMsg>,
) -> String {
    use crate::text::TextItem;
    match decode_show_text_string(bytes, kind, page, op_id, warnings) {
        TextItem::Text(s) => s,
        TextItem::GlyphIds(glyphs) => glyphs
            .iter()
            .map(|g| g.cid.clone().unwrap_or_else(|| '\u{FFFD}'.to_string()))
            .collect(),
        TextItem::Offset(_) => String::new(),
    }
}

/// Records an opened marked content sequence (`BMC`, `BDC`) on the stack of `state`. Past the
/// depth limit it is only counted, with one warning where the limit is crossed.
fn open_marked_content(
    state: &mut PageState,
    tag: String,
    page: usize,
    op_id: usize,
    warnings: &mut Vec<PdfWarnMsg>,
) {
    if state.current_marked_content.len() < MAX_MARKED_CONTENT_DEPTH {
        state.current_marked_content.push(tag);
        return;
    }
    if state.untracked_marked_content == 0 {
        warnings.push(PdfWarnMsg::warning(
            page,
            op_id,
            format!(
                "Marked content nested deeper than {MAX_MARKED_CONTENT_DEPTH} levels; \
                 the nesting is no longer tracked"
            ),
        ));
    }
    state.untracked_marked_content = state.untracked_marked_content.saturating_add(1);
}

/// Converts one content stream operator into zero or more [`Op`]s.
///
/// `state` carries the current font, text position and open path between calls: path operators
/// (`m`, `l`, `c`, ...) accumulate in it and an op is emitted only when a painting operator
/// (`S`, `f`, `B`, ...) ends the path. `page` and `op_id` are used for warnings only. A malformed
/// operator pushes a warning and produces no op; an unknown one becomes [`Op::Unknown`]. The
/// `Result` is `Err` for no case today. `_xobjects` is unused.
pub fn parse_op(
    page: usize,
    op_id: usize,
    op: &lopdf::content::Operation,
    state: &mut PageState,
    _xobjects: &BTreeMap<XObjectId, XObject>,
    warnings: &mut Vec<PdfWarnMsg>,
) -> Result<Vec<Op>, String> {
    use crate::units::Pt;
    let mut out_ops = Vec::new();
    match op.operator.as_str() {
        "q" => {
            if state.transform_stack.len() < MAX_GRAPHICS_STATE_DEPTH {
                let top = state
                    .transform_stack
                    .last()
                    .copied()
                    .unwrap_or([1.0, 0.0, 0.0, 1.0, 0.0, 0.0]);
                state.transform_stack.push(top);
            } else {
                if state.untracked_saves == 0 {
                    warnings.push(PdfWarnMsg::warning(
                        page,
                        op_id,
                        format!(
                            "'q' nested deeper than {MAX_GRAPHICS_STATE_DEPTH} levels; \
                             the nesting is no longer tracked"
                        ),
                    ));
                }
                state.untracked_saves = state.untracked_saves.saturating_add(1);
            }
            out_ops.push(Op::SaveGraphicsState);
        }
        "Q" => {
            if state.untracked_saves > 0 {
                state.untracked_saves = state.untracked_saves.saturating_sub(1);
            } else if state.transform_stack.pop().is_none() {
                // Unbalanced `Q`: warn, and still emit the op.
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    "Warning: 'Q' with empty transform stack".to_string(),
                ));
            }
            out_ops.push(Op::RestoreGraphicsState);
        }
        "MP" => {
            let [operand] = op.operands.as_slice() else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    "MP expects 1 operand".to_string(),
                ));
                return Ok(Vec::new());
            };

            let id = match as_name(operand) {
                Some(name) => name,
                None => {
                    warnings.push(PdfWarnMsg::error(
                        page,
                        op_id,
                        "MP operand is not a name".to_string(),
                    ));
                    return Ok(Vec::new());
                }
            };

            out_ops.push(Op::Marker { id });
        }
        "CS" => {
            let [operand] = op.operands.as_slice() else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    "CS expects 1 operand".to_string(),
                ));
                return Ok(Vec::new());
            };

            let id = match as_name(operand) {
                Some(name) => name,
                None => {
                    warnings.push(PdfWarnMsg::error(
                        page,
                        op_id,
                        "CS operand is not a name".to_string(),
                    ));
                    return Ok(Vec::new());
                }
            };

            // The pattern color space is implied by `Op::SetOutlinePattern`.
            if id == "Pattern" || state.pattern_spaces.contains(&id) {
                return Ok(Vec::new());
            }
            out_ops.push(Op::SetColorSpaceStroke { id });
        }
        "cs" => {
            let [operand] = op.operands.as_slice() else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    "cs expects 1 operand".to_string(),
                ));
                return Ok(Vec::new());
            };

            let id = match as_name(operand) {
                Some(name) => name,
                None => {
                    warnings.push(PdfWarnMsg::error(
                        page,
                        op_id,
                        "cs operand is not a name".to_string(),
                    ));
                    return Ok(Vec::new());
                }
            };

            // The pattern color space is implied by `Op::SetFillPattern`.
            if id == "Pattern" || state.pattern_spaces.contains(&id) {
                return Ok(Vec::new());
            }
            out_ops.push(Op::SetColorSpaceFill { id });
        }
        "ri" => {
            let [operand] = op.operands.as_slice() else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    "ri expects 1 operand".to_string(),
                ));
                return Ok(Vec::new());
            };
            let intent = match as_name(operand) {
                Some(i) => i,
                None => {
                    warnings.push(PdfWarnMsg::error(
                        page,
                        op_id,
                        "ri operand is not a name".to_string(),
                    ));
                    return Ok(Vec::new());
                }
            };
            let intent = match intent.as_str() {
                "AbsoluteColorimetric" => RenderingIntent::AbsoluteColorimetric,
                "RelativeColorimetric" => RenderingIntent::RelativeColorimetric,
                "Saturation" => RenderingIntent::Saturation,
                "Perceptual" => RenderingIntent::Perceptual,
                other => {
                    warnings.push(PdfWarnMsg::error(
                        page,
                        op_id,
                        format!(
                            "Unknown rendering intent '{}', defaulting to RelativeColorimetric",
                            other
                        ),
                    ));
                    RenderingIntent::RelativeColorimetric
                }
            };
            out_ops.push(Op::SetRenderingIntent { intent });
        }
        "rg" => {
            if let [r, g, b] = op.operands.as_slice() {
                let r = to_f32(r);
                let g = to_f32(g);
                let b = to_f32(b);
                out_ops.push(Op::SetFillColor {
                    col: Color::Rgb(crate::Rgb {
                        r,
                        g,
                        b,
                        icc_profile: None,
                    }),
                });
            } else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    "Warning: 'rg' expects 3 operands".to_string(),
                ));
            }
        }
        "RG" => {
            if let [r, g, b] = op.operands.as_slice() {
                let r = to_f32(r);
                let g = to_f32(g);
                let b = to_f32(b);
                out_ops.push(Op::SetOutlineColor {
                    col: Color::Rgb(crate::Rgb {
                        r,
                        g,
                        b,
                        icc_profile: None,
                    }),
                });
            } else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    "Warning: 'RG' expects 3 operands".to_string(),
                ));
            }
        }
        "k" => {
            if let [c, m, y, k] = op.operands.as_slice() {
                let c = to_f32(c);
                let m = to_f32(m);
                let y = to_f32(y);
                let k = to_f32(k);
                out_ops.push(Op::SetFillColor {
                    col: Color::Cmyk(crate::Cmyk {
                        c,
                        m,
                        y,
                        k,
                        icc_profile: None,
                    }),
                });
            } else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    "Warning: 'k' expects 4 operands".to_string(),
                ));
            }
        }
        "K" => {
            if let [c, m, y, k] = op.operands.as_slice() {
                let c = to_f32(c);
                let m = to_f32(m);
                let y = to_f32(y);
                let k = to_f32(k);
                out_ops.push(Op::SetOutlineColor {
                    col: Color::Cmyk(crate::Cmyk {
                        c,
                        m,
                        y,
                        k,
                        icc_profile: None,
                    }),
                });
            } else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    "Warning: 'K' expects 4 operands".to_string(),
                ));
            }
        }
        "g" => {
            if let [gray] = op.operands.as_slice() {
                let gray = to_f32(gray);
                out_ops.push(Op::SetFillColor {
                    col: Color::Greyscale(crate::Greyscale {
                        percent: gray,
                        icc_profile: None,
                    }),
                });
            } else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    "Warning: 'g' expects 1 operand".to_string(),
                ));
            }
        }
        "G" => {
            if let [gray] = op.operands.as_slice() {
                let gray = to_f32(gray);
                out_ops.push(Op::SetOutlineColor {
                    col: Color::Greyscale(crate::Greyscale {
                        percent: gray,
                        icc_profile: None,
                    }),
                });
            } else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    "Warning: 'G' expects 1 operand".to_string(),
                ));
            }
        }

        "TJ" => {
            if !state.in_text_mode {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    "Warning: 'TJ' outside of text mode!".to_string(),
                ));
            }

            if op.operands.is_empty() {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    "Warning: 'TJ' with no operands".to_string(),
                ));
            } else if let Some(arr) = op.operands.first().and_then(|o| o.as_array().ok()) {
                // Warn if no font was set via Tf operator
                if state.current_font_resource.is_none() {
                    warnings.push(PdfWarnMsg::warning(
                        page,
                        op_id,
                        "Warning: 'TJ' without prior 'Tf' (font setup). Renderer will use default font.".to_string(),
                    ));
                }

                // Decode according to the *current font*: 1-byte WinAnsi codes
                // for builtin/simple fonts, 2-byte CIDs for composite fonts.
                let text_items = decode_show_text_array(
                    arr,
                    state.current_font_decode.as_ref(),
                    page,
                    op_id,
                    warnings,
                );

                out_ops.push(Op::ShowText { items: text_items });
            } else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    "Warning: 'TJ' operand is not an array".to_string(),
                ));
            }
        }

        "Tj" => {
            if !state.in_text_mode {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    "Warning: 'Tj' outside of text mode!".to_string(),
                ));
            }

            if op.operands.is_empty() {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    "Warning: 'Tj' with no operands".to_string(),
                ));
            } else if let Some(lopdf::Object::String(bytes, _format)) = op.operands.first() {
                // Warn if no font was set via Tf operator
                if state.current_font_resource.is_none() {
                    warnings.push(PdfWarnMsg::warning(
                        page,
                        op_id,
                        "Warning: 'Tj' without prior 'Tf' (font setup). Renderer will use default font.".to_string(),
                    ));
                }

                // Decode according to the current font (see decode_show_text_string).
                let text_items = vec![decode_show_text_string(
                    bytes,
                    state.current_font_decode.as_ref(),
                    page,
                    op_id,
                    warnings,
                )];

                out_ops.push(Op::ShowText { items: text_items });
            } else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    "Warning: 'Tj' operand is not string".to_string(),
                ));
            }
        }
        "T*" => {
            // Move to the start of the next line: keep x, drop y by the leading.
            state.text_line[1] -= state.text_leading;
            out_ops.push(Op::AddLineBreak);
        }
        "TL" => {
            if let [operand] = op.operands.as_slice() {
                let val = to_f32(operand);
                state.text_leading = val;
                out_ops.push(Op::SetLineHeight { lh: Pt(val) });
            } else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    format!("Warning: 'TL' expects 1 operand, got {}", op.operands.len()),
                ));
            }
        }
        "Ts" => {
            if let [operand] = op.operands.as_slice() {
                let rise = to_f32(operand);
                out_ops.push(Op::SetLineOffset { multiplier: rise });
            } else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    format!("Warning: 'Ts' expects 1 operand, got {}", op.operands.len()),
                ));
            }
        }
        "Tw" => {
            if let [operand] = op.operands.as_slice() {
                let spacing = to_f32(operand);
                out_ops.push(Op::SetWordSpacing { pt: Pt(spacing) });
            } else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    format!("Warning: 'Tw' expects 1 operand, got {}", op.operands.len()),
                ));
            }
        }
        "Tz" => {
            if let [operand] = op.operands.as_slice() {
                let scale_percent = to_f32(operand);
                out_ops.push(Op::SetHorizontalScaling {
                    percent: scale_percent,
                });
            } else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    format!("Warning: 'Tz' expects 1 operand, got {}", op.operands.len()),
                ));
            }
        }
        "TD" => {
            if let [tx, ty] = op.operands.as_slice() {
                // TD = Td + set leading to -ty. Relative move; track it and the leading.
                let tx = to_f32(tx);
                let ty = to_f32(ty);
                state.text_line[0] += tx;
                state.text_line[1] += ty;
                state.text_leading = -ty;
                out_ops.push(Op::MoveTextCursorAndSetLeading { tx, ty });
            } else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    format!("'TD' expects 2 operands, got {}", op.operands.len()),
                ));
            }
        }
        "Tm" => {
            if let [a, b, c, d, e, f] = op.operands.as_slice() {
                let a = to_f32(a);
                let b = to_f32(b);
                let c = to_f32(c);
                let d = to_f32(d);
                let e = to_f32(e);
                let f = to_f32(f);
                // Tm sets the text line matrix absolutely; track its translation
                // so a following relative `Td` accumulates from here.
                state.text_line = [e, f];
                out_ops.push(Op::SetTextMatrix {
                    matrix: TextMatrix::Raw([a, b, c, d, e, f]),
                });
            } else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    "Warning: 'Tm' expects 6 operands".to_string(),
                ));
            }
        }
        "Tc" => {
            let [operand] = op.operands.as_slice() else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    "Tc expects 1 operand".to_string(),
                ));
                return Ok(Vec::new());
            };
            let spacing = to_f32(operand);
            out_ops.push(Op::SetCharacterSpacing {
                multiplier: spacing,
            });
        }
        "Tr" => {
            let [operand] = op.operands.as_slice() else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    "Tr expects 1 operand".to_string(),
                ));
                return Ok(Vec::new());
            };
            let mode = match operand {
                Object::Integer(i) => *i,
                Object::Real(r) => *r as i64,
                _ => {
                    warnings.push(PdfWarnMsg::error(
                        page,
                        op_id,
                        "Tr operand is not numeric".to_string(),
                    ));
                    return Ok(Vec::new());
                }
            };
            out_ops.push(Op::SetTextRenderingMode {
                mode: TextRenderingMode::from_i64(mode),
            });
        }
        "gs" => {
            let [operand] = op.operands.as_slice() else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    "gs expects 1 operand".to_string(),
                ));
                return Ok(Vec::new());
            };

            let gs_name = match as_name(operand) {
                Some(name) => name,
                None => {
                    warnings.push(PdfWarnMsg::error(
                        page,
                        op_id,
                        "gs operand is not a name".to_string(),
                    ));
                    return Ok(Vec::new());
                }
            };

            out_ops.push(Op::LoadGraphicsState {
                gs: ExtendedGraphicsStateId(gs_name),
            });
        }

        "BT" => {
            // `BT` resets the text matrix and the text line matrix.
            state.in_text_mode = true;
            state.text_line = [0.0, 0.0];
            out_ops.push(Op::StartTextSection);
        }
        "ET" => {
            state.in_text_mode = false;
            out_ops.push(Op::EndTextSection);
        }

        "Tf" => {
            if let [font, size] = op.operands.as_slice() {
                if let Some(font_name) = as_name(font) {
                    state.current_font = Some(BuiltinOrExternalFontId::from_str(&font_name));
                    state.current_font_resource = Some(font_name.clone());
                    // The document's own font resources win over the F1-F14 builtin naming: a
                    // foreign PDF may call an embedded font "F1".
                    state.current_font_decode = match state.font_decode.get(&font_name) {
                        Some(kind) => Some(kind.clone()),
                        None => BuiltinFont::from_id(&font_name).map(FontTextDecodeKind::Builtin),
                    };
                }
                let size_val = to_f32(size);
                state.current_font_size = Some(crate::units::Pt(size_val));

                if let (Some(font_resource), Some(sz)) =
                    (&state.current_font_resource, &state.current_font_size)
                {
                    // Builtin or external is decided by the resolved decode
                    // kind (falling back to the F1-F14 name heuristic when the
                    // document's resources don't describe the font).
                    let font_handle = match &state.current_font_decode {
                        Some(FontTextDecodeKind::Builtin(b)) => {
                            crate::ops::PdfFontHandle::Builtin(*b)
                        }
                        Some(_) => crate::ops::PdfFontHandle::External(crate::FontId(
                            font_resource.clone(),
                        )),
                        None => {
                            if let Some(builtin) = BuiltinFont::from_id(font_resource) {
                                crate::ops::PdfFontHandle::Builtin(builtin)
                            } else {
                                crate::ops::PdfFontHandle::External(crate::FontId(
                                    font_resource.clone(),
                                ))
                            }
                        }
                    };

                    out_ops.push(Op::SetFont {
                        font: font_handle,
                        size: *sz,
                    });
                }
            } else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    format!(
                        "Warning: 'Tf' expects 2 operands, got {}",
                        op.operands.len()
                    ),
                ));
                out_ops.push(Op::Unknown {
                    key: "Tf".into(),
                    value: op.operands.iter().map(DictItem::from_lopdf).collect(),
                });
            }
        }

        "Td" => {
            if let [tx, ty] = op.operands.as_slice() {
                // `Td` is relative to the text line matrix: accumulate it and emit an absolute
                // cursor, since `SetTextCursor` is absolute.
                let tx = to_f32(tx);
                let ty = to_f32(ty);
                state.text_line[0] += tx;
                state.text_line[1] += ty;
                out_ops.push(Op::SetTextCursor {
                    pos: crate::graphics::Point {
                        x: crate::units::Pt(state.text_line[0]),
                        y: crate::units::Pt(state.text_line[1]),
                    },
                });
            }
        }

        // Marked content (`BMC`, `BDC`, `EMC`). `BDC /OC /Name` is optional content (a layer).
        // Marked content can nest.
        "BDC" => {
            // Typically something like: [Name("Span"), Properties as a Dictionary]
            // In case of optional content: [Name("OC"), Name("MyLayer")]
            let [tag, properties] = op.operands.as_slice() else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    "BDC expects 2 operands".to_string(),
                ));
                return Ok(Vec::new());
            };
            let Some(marked_content_nm) = as_name(tag) else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    "BDC operand is not a name".to_string(),
                ));
                return Ok(Vec::new());
            };
            if marked_content_nm.as_str() == "OC" {
                let Some(layer_nm) = as_name(properties) else {
                    warnings.push(PdfWarnMsg::error(
                        page,
                        op_id,
                        "BDC OC layer operand is not a name".to_string(),
                    ));
                    return Ok(Vec::new());
                };
                // `BeginLayer` rather than `BeginOptionalContent`: the serializer builds the
                // page's /Properties entries only from `BeginLayer` ops. Both write the same bytes.
                out_ops.push(Op::BeginLayer {
                    layer_id: crate::LayerInternalId(layer_nm),
                });
            } else {
                match properties.as_dict() {
                    Ok(dict) => {
                        out_ops.push(Op::BeginMarkedContentWithProperties {
                            tag: marked_content_nm.clone(),
                            properties: DictItem::from_lopdf(&Object::Dictionary(dict.to_owned())),
                        });
                    }
                    Err(_err) => {
                        warnings.push(PdfWarnMsg::warning(
                            page,
                            op_id,
                            "BDC properties is not a dictionary".to_string(),
                        ));
                        out_ops.push(Op::Unknown {
                            key: "BDC".into(),
                            value: op.operands.iter().map(DictItem::from_lopdf).collect(),
                        });
                    }
                }
            }
            open_marked_content(state, marked_content_nm, page, op_id, warnings);
        }
        "BMC" => {
            let [operand] = op.operands.as_slice() else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    "BMC expects 1 operand".to_string(),
                ));
                return Ok(Vec::new());
            };
            let marked_content_nm = match as_name(operand) {
                Some(t) => t,
                None => {
                    warnings.push(PdfWarnMsg::error(
                        page,
                        op_id,
                        "BMC operand is not a name".to_string(),
                    ));
                    return Ok(Vec::new());
                }
            };
            out_ops.push(Op::BeginMarkedContent {
                tag: marked_content_nm.clone(),
            });
            open_marked_content(state, marked_content_nm, page, op_id, warnings);
        }
        "EMC" => {
            if state.untracked_marked_content > 0 {
                state.untracked_marked_content = state.untracked_marked_content.saturating_sub(1);
                out_ops.push(Op::EndMarkedContent {});
            } else if let Some(_marked_content_str) = state.current_marked_content.pop() {
                out_ops.push(Op::EndMarkedContent {});
            } else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    "Warning: 'EMC' with no current_marked_content".to_string(),
                ));
            }
        }

        "cm" => {
            if let [a, b, c, d, e, f] = op.operands.as_slice() {
                let floats = [to_f32(a), to_f32(b), to_f32(c), to_f32(d), to_f32(e), to_f32(f)];
                if let Some(top) = state.transform_stack.last_mut() {
                    // multiply top by these floats
                    let combined = crate::matrix::CurTransMat::combine_matrix(*top, floats);
                    *top = combined;
                }
                out_ops.push(Op::SetTransformationMatrix {
                    matrix: crate::matrix::CurTransMat::Raw(floats),
                });
            } else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    "Warning: 'cm' expects 6 floats".to_string(),
                ));
            }
        }

        "m" => {
            // Start a new subpath
            let x = to_f32(op.operands.first().unwrap_or(&lopdf::Object::Null));
            let y = to_f32(op.operands.get(1).unwrap_or(&lopdf::Object::Null));
            state.path_builder.move_to(Point { x: Pt(x), y: Pt(y) });
        }
        "l" => {
            let x = to_f32(op.operands.first().unwrap_or(&lopdf::Object::Null));
            let y = to_f32(op.operands.get(1).unwrap_or(&lopdf::Object::Null));
            state.path_builder.line_to(Point { x: Pt(x), y: Pt(y) });
        }
        "c" => {
            // c x1 y1 x2 y2 x3 y3
            if let [x1, y1, x2, y2, x3, y3] = op.operands.as_slice() {
                let x1 = to_f32(x1);
                let y1 = to_f32(y1);
                let x2 = to_f32(x2);
                let y2 = to_f32(y2);
                let x3 = to_f32(x3);
                let y3 = to_f32(y3);

                state.path_builder.curve_to(
                    Point {
                        x: Pt(x1),
                        y: Pt(y1),
                    },
                    Point {
                        x: Pt(x2),
                        y: Pt(y2),
                    },
                    Point {
                        x: Pt(x3),
                        y: Pt(y3),
                    },
                );
            } else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    format!("'c' expects 6 operands, got {}", op.operands.len()),
                ));
            }
        }
        "v" => {
            // v x2 y2 x3 y3
            // The first control point is implied to be the current point
            if let [x2, y2, x3, y3] = op.operands.as_slice() {
                let x2 = to_f32(x2);
                let y2 = to_f32(y2);
                let x3 = to_f32(x3);
                let y3 = to_f32(y3);

                state.path_builder.v_curve_to(
                    Point {
                        x: Pt(x2),
                        y: Pt(y2),
                    },
                    Point {
                        x: Pt(x3),
                        y: Pt(y3),
                    },
                );
            } else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    format!("'v' expects 4 operands, got {}", op.operands.len()),
                ));
            }
        }
        "y" => {
            // y x1 y1 x3 y3
            // The second control point is implied to be the final point
            if let [x1, y1, x3, y3] = op.operands.as_slice() {
                let x1 = to_f32(x1);
                let y1 = to_f32(y1);
                let x3 = to_f32(x3);
                let y3 = to_f32(y3);

                state.path_builder.y_curve_to(
                    Point {
                        x: Pt(x1),
                        y: Pt(y1),
                    },
                    Point {
                        x: Pt(x3),
                        y: Pt(y3),
                    },
                );
            } else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    format!("'y' expects 4 operands, got {}", op.operands.len()),
                ));
            }
        }
        "h" => {
            // closepath, i.e. connect last point to first point
            state.path_builder.close_path();
        }
        "re" => {
            if let [x, y, width, height] = op.operands.as_slice() {
                let x = Pt(to_f32(x));
                let y = Pt(to_f32(y));
                let width = Pt(to_f32(width));
                let height = Pt(to_f32(height));
                // `re` appends a closed rectangle to the current path (ISO 32000-1, 8.5.2.1); the
                // operator after it (`f`, `S`, `B`, `W n`) fills, strokes or clips it.
                let corner = |dx: f32, dy: f32| Point {
                    x: Pt(x.0 + dx),
                    y: Pt(y.0 + dy),
                };
                state.path_builder.move_to(corner(0.0, 0.0));
                state.path_builder.line_to(corner(width.0, 0.0));
                state.path_builder.line_to(corner(width.0, height.0));
                state.path_builder.line_to(corner(0.0, height.0));
                state.path_builder.close_path();
            } else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    "re expects 4 operands".to_string(),
                ));
            }
        }
        "S" => {
            // Stroke - doesn't close the path
            let path = state
                .path_builder
                .build(PaintMode::Stroke, WindingOrder::NonZero);
            if !path.subpaths.is_empty() {
                out_ops.push(path_to_op(&path));
            }
            state.path_builder.clear();
        }
        "f" => {
            // Fill with non-zero winding rule
            let path = state
                .path_builder
                .build(PaintMode::Fill, WindingOrder::NonZero);
            if !path.subpaths.is_empty() {
                out_ops.push(path_to_op(&path));
            }
            state.path_builder.clear();
        }
        "f*" => {
            // Fill with even-odd winding rule
            let path = state
                .path_builder
                .build(PaintMode::Fill, WindingOrder::EvenOdd);
            if !path.subpaths.is_empty() {
                out_ops.push(path_to_op(&path));
            }
            state.path_builder.clear();
        }
        "B" => {
            // Fill and stroke with non-zero winding
            let path = state
                .path_builder
                .build(PaintMode::FillStroke, WindingOrder::NonZero);
            if !path.subpaths.is_empty() {
                out_ops.push(path_to_op(&path));
            }
            state.path_builder.clear();
        }
        "B*" => {
            // Fill and stroke with even-odd winding
            let path = state
                .path_builder
                .build(PaintMode::FillStroke, WindingOrder::EvenOdd);
            if !path.subpaths.is_empty() {
                out_ops.push(path_to_op(&path));
            }
            state.path_builder.clear();
        }
        "b" => {
            // Close, fill, and stroke with non-zero winding
            state.path_builder.close_path();
            let path = state
                .path_builder
                .build(PaintMode::FillStroke, WindingOrder::NonZero);
            if !path.subpaths.is_empty() {
                out_ops.push(path_to_op(&path));
            }
            state.path_builder.clear();
        }
        "b*" => {
            // Close, fill, and stroke with even-odd winding
            state.path_builder.close_path();
            let path = state
                .path_builder
                .build(PaintMode::FillStroke, WindingOrder::EvenOdd);
            if !path.subpaths.is_empty() {
                out_ops.push(path_to_op(&path));
            }
            state.path_builder.clear();
        }
        "s" => {
            // Close and stroke
            state.path_builder.close_path();
            let path = state
                .path_builder
                .build(PaintMode::Stroke, WindingOrder::NonZero);
            if !path.subpaths.is_empty() {
                out_ops.push(path_to_op(&path));
            }
            state.path_builder.clear();
        }
        "n" => {
            // End path without filling or stroking (a preceding `W` / `W*`
            // already took it as the clip).
            state.path_builder.clear();
        }
        "W" => {
            // Set clip path using non-zero winding rule. `W` does not consume the path, so a
            // second `W` on a growing path would emit the whole path again and the output would
            // grow with the square of the input: a path is clipped once.
            if state.path_builder.clip_emitted {
                warnings.push(PdfWarnMsg::warning(
                    page,
                    op_id,
                    "'W' on a path that is already clipped; skipped".to_string(),
                ));
            } else {
                let path = state
                    .path_builder
                    .build(PaintMode::Clip, WindingOrder::NonZero);
                if !path.subpaths.is_empty() {
                    out_ops.push(path_to_op(&path));
                    state.path_builder.clip_emitted = true;
                }
            }
            // Note: We don't clear the path here since clipping doesn't consume the path
            state
                .rectangle_builder
                .set_winding_order(WindingOrder::NonZero);
        }
        "W*" => {
            // Set clip path using even-odd winding rule; a path is clipped once (see `W`).
            if state.path_builder.clip_emitted {
                warnings.push(PdfWarnMsg::warning(
                    page,
                    op_id,
                    "'W*' on a path that is already clipped; skipped".to_string(),
                ));
            } else {
                let path = state
                    .path_builder
                    .build(PaintMode::Clip, WindingOrder::EvenOdd);
                if !path.subpaths.is_empty()
                    || out_ops.last().is_some_and(|last_op| match last_op {
                        Op::DrawRectangle { .. } => true,
                        _ => false,
                    })
                {
                    out_ops.push(path_to_op(&path));
                    state.path_builder.clip_emitted = true;
                }
            }
            // Note: We don't clear the path here since clipping doesn't consume the path
            state
                .rectangle_builder
                .set_winding_order(WindingOrder::EvenOdd);
        }

        "w" => {
            if let Some(val) = op.operands.first() {
                let width = to_f32(val);
                out_ops.push(Op::SetOutlineThickness {
                    pt: crate::units::Pt(width),
                });
            }
        }

        "M" => {
            if let Some(val) = op.operands.first() {
                let limit = to_f32(val);
                out_ops.push(Op::SetMiterLimit { limit: Pt(limit) });
            }
        }

        "j" => {
            // "0 j" => miter join, "1 j" => round join, "2 j" => bevel
            if let Some(val) = op.operands.first() {
                let style_num = to_f32(val).round() as i64;
                let style = match style_num {
                    0 => crate::graphics::LineJoinStyle::Miter,
                    1 => crate::graphics::LineJoinStyle::Round,
                    2 => crate::graphics::LineJoinStyle::Bevel,
                    _ => crate::graphics::LineJoinStyle::Miter, // fallback
                };
                out_ops.push(Op::SetLineJoinStyle { join: style });
            }
        }

        "J" => {
            // "0 J" => butt cap, "1 J" => round cap, "2 J" => projecting square
            if let Some(val) = op.operands.first() {
                let style_num = to_f32(val).round() as i64;
                let style = match style_num {
                    0 => crate::graphics::LineCapStyle::Butt,
                    1 => crate::graphics::LineCapStyle::Round,
                    2 => crate::graphics::LineCapStyle::ProjectingSquare,
                    _ => crate::graphics::LineCapStyle::Butt, // fallback
                };
                out_ops.push(Op::SetLineCapStyle { cap: style });
            }
        }

        "d" => {
            // "d [2 2] 0" => dash 2 on, 2 off, offset=0
            // operand 0 is the dash array, operand 1 is the dash offset
            if let [arr_obj, offset] = op.operands.as_slice() {
                if let Ok(arr) = arr_obj.as_array() {
                    let pattern = arr.iter().map(to_f32).collect();
                    let offset = to_f32(offset);
                    let dash = LineDashPattern { offset, pattern };
                    out_ops.push(Op::SetLineDashPattern { dash });
                }
            }
        }

        // A name as the last operand selects a pattern. The operands before it are the color of
        // an uncolored tiling pattern.
        "sc" | "scn" | "SC" | "SCN" if matches!(op.operands.last(), Some(Object::Name(_))) => {
            let stroke = matches!(op.operator.as_str(), "SC" | "SCN");
            let (name, components) = match op.operands.split_last() {
                Some((Object::Name(name), components)) => (name, components),
                _ => return Ok(Vec::new()),
            };
            let id = crate::PatternId(String::from_utf8_lossy(name).into_owned());
            let floats = components.iter().map(to_f32).collect::<Vec<_>>();
            let color = match floats.as_slice() {
                [] => None,
                [gray] => Some(Color::Greyscale(crate::Greyscale {
                    percent: *gray,
                    icc_profile: None,
                })),
                [r, g, b] => Some(Color::Rgb(crate::Rgb {
                    r: *r,
                    g: *g,
                    b: *b,
                    icc_profile: None,
                })),
                [c, m, y, k] => Some(Color::Cmyk(crate::Cmyk {
                    c: *c,
                    m: *m,
                    y: *y,
                    k: *k,
                    icc_profile: None,
                })),
                other => {
                    warnings.push(PdfWarnMsg::warning(
                        page,
                        op_id,
                        format!(
                            "{} sets a pattern with {} color components, which is not 1, 3 or 4; \
                             the color is dropped",
                            op.operator,
                            other.len()
                        ),
                    ));
                    None
                }
            };
            out_ops.push(if stroke {
                Op::SetOutlinePattern { id, color }
            } else {
                Op::SetFillPattern { id, color }
            });
        }

        "sc" | "scn" => {
            // The operand count picks the color space: 1 gray, 3 RGB, 4 CMYK.
            let floats = op.operands.iter().map(to_f32).collect::<Vec<_>>();
            match floats.as_slice() {
                [gray] => {
                    // grayscale
                    out_ops.push(Op::SetFillColor {
                        col: Color::Greyscale(crate::Greyscale {
                            percent: *gray,
                            icc_profile: None,
                        }),
                    });
                }
                [r, g, b] => {
                    // rgb
                    out_ops.push(Op::SetFillColor {
                        col: Color::Rgb(crate::Rgb {
                            r: *r,
                            g: *g,
                            b: *b,
                            icc_profile: None,
                        }),
                    });
                }
                [c, m, y, k] => {
                    // cmyk
                    out_ops.push(Op::SetFillColor {
                        col: Color::Cmyk(crate::Cmyk {
                            c: *c,
                            m: *m,
                            y: *y,
                            k: *k,
                            icc_profile: None,
                        }),
                    });
                }
                _ => {
                    // fallback
                    out_ops.push(Op::Unknown {
                        key: op.operator.clone(),
                        value: op.operands.iter().map(DictItem::from_lopdf).collect(),
                    });
                }
            }
        }

        // Stroke color; the operand count picks the color space as for `sc`.
        "SC" | "SCN" => {
            let floats = op.operands.iter().map(to_f32).collect::<Vec<_>>();
            match floats.as_slice() {
                [gray] => {
                    out_ops.push(Op::SetOutlineColor {
                        col: Color::Greyscale(crate::Greyscale {
                            percent: *gray,
                            icc_profile: None,
                        }),
                    });
                }
                [r, g, b] => {
                    out_ops.push(Op::SetOutlineColor {
                        col: Color::Rgb(crate::Rgb {
                            r: *r,
                            g: *g,
                            b: *b,
                            icc_profile: None,
                        }),
                    });
                }
                [c, m, y, k] => {
                    out_ops.push(Op::SetOutlineColor {
                        col: Color::Cmyk(crate::Cmyk {
                            c: *c,
                            m: *m,
                            y: *y,
                            k: *k,
                            icc_profile: None,
                        }),
                    });
                }
                _ => {
                    out_ops.push(Op::Unknown {
                        key: op.operator.clone(),
                        value: op.operands.iter().map(DictItem::from_lopdf).collect(),
                    });
                }
            }
        }

        // `'` and `"` show text like `Tj`. Without these arms they would become `Op::Unknown`,
        // which the default (`secure`) save drops.
        "'" => {
            if let Some(lopdf::Object::String(bytes, _)) = op.operands.first() {
                let text = decode_show_text_to_string(
                    bytes,
                    state.current_font_decode.as_ref(),
                    page,
                    op_id,
                    warnings,
                );
                out_ops.push(Op::MoveToNextLineShowText { text });
            } else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    "Warning: `'` operand is not a string".to_string(),
                ));
            }
        }
        "\"" => {
            if let [word_spacing, char_spacing, text] = op.operands.as_slice() {
                let word_spacing = to_f32(word_spacing);
                let char_spacing = to_f32(char_spacing);
                if let lopdf::Object::String(bytes, _) = text {
                    let text = decode_show_text_to_string(
                        bytes,
                        state.current_font_decode.as_ref(),
                        page,
                        op_id,
                        warnings,
                    );
                    out_ops.push(Op::SetSpacingMoveAndShowText {
                        word_spacing,
                        char_spacing,
                        text,
                    });
                } else {
                    warnings.push(PdfWarnMsg::error(
                        page,
                        op_id,
                        "Warning: `\"` third operand is not a string".to_string(),
                    ));
                }
            } else {
                warnings.push(PdfWarnMsg::error(
                    page,
                    op_id,
                    format!(
                        "Warning: `\"` expects 3 operands, got {}",
                        op.operands.len()
                    ),
                ));
            }
        }

        // `sh` paints a named /Shading resource over the current clip.
        "sh" => {
            if let Some(name_str) = as_name(op.operands.first().unwrap_or(&lopdf::Object::Null)) {
                out_ops.push(Op::PaintShading {
                    id: crate::ShadingId(name_str),
                });
            }
        }

        "Do" => {
            if let Some(name_str) = as_name(op.operands.first().unwrap_or(&lopdf::Object::Null)) {
                let xobj_id = crate::XObjectId(name_str);
                // The document's own `cm` operators position the XObject; `no_auto_scale` stops the
                // serializer from scaling the unit square again.
                out_ops.push(Op::UseXobject {
                    id: xobj_id,
                    transform: crate::xobject::XObjectTransform {
                        no_auto_scale: true,
                        ..Default::default()
                    },
                });
            }
        }

        // Anything else is kept as `Op::Unknown`, with a warning.
        other => {
            warnings.push(PdfWarnMsg::error(
                page,
                op_id,
                format!("Info: unhandled operator '{}'", other),
            ));
            out_ops.push(Op::Unknown {
                key: op.operator.clone(),
                value: op.operands.iter().map(DictItem::from_lopdf).collect(),
            });
        }
    }

    Ok(out_ops)
}

/// The PDF/A variant that the XMP metadata of `catalog` names (`pdfaid:part` and
/// `pdfaid:conformance`), if it has any.
fn pdfa_conformance_from_xmp(doc: &LopdfDocument, catalog: &LopdfDictionary) -> Option<PdfConformance> {
    let metadata = match catalog.get(b"Metadata").ok()? {
        Object::Reference(id) => doc.get_object(*id).ok()?,
        other => other,
    };
    let Object::Stream(stream) = metadata else {
        return None;
    };
    let bytes = stream.decompressed_content_with_limit(16 * 1024 * 1024).ok()?;
    let text = String::from_utf8_lossy(&bytes);
    let (part, level) = crate::preflight::xmp::pdfa_id(&text);
    let level = level.unwrap_or_default();
    match (part?.as_str(), level.as_str()) {
        ("1", "A") => Some(PdfConformance::A1A_2005_PDF_1_4),
        ("1", _) => Some(PdfConformance::A1B_2005_PDF_1_4),
        ("2", "A") => Some(PdfConformance::A2A_2011_PDF_1_7),
        ("2", "U") => Some(PdfConformance::A2U_2011_PDF_1_7),
        ("2", _) => Some(PdfConformance::A2B_2011_PDF_1_7),
        ("3", _) => Some(PdfConformance::A3_2012_PDF_1_7),
        _ => None,
    }
}

/// Unix epoch, used when a date is missing or unreadable.
fn default_date() -> OffsetDateTime {
    OffsetDateTime::epoch()
}

/// Reads the document information dictionary (`/Info` of the trailer).
///
/// Missing or unreadable entries fall back to empty strings, `false` or the Unix epoch.
/// `/Keywords` is split at commas, and text strings are decoded from UTF-16BE (with byte order
/// mark) or UTF-8. The metadata date is set to the modification date, and the version to 1.
pub fn parse_document_info(dict: &LopdfDictionary) -> PdfDocumentInfo {
    let trapped = dict
        .get(b"Trapped")
        .ok()
        .and_then(|obj| match &obj {
            Object::String(s, _) => Some(s == b"True"),
            Object::Name(n) => Some(n == b"True"),
            _ => None,
        })
        .unwrap_or_default();

    let creation_date = dict
        .get(b"CreationDate")
        .ok()
        .and_then(|obj| match &obj {
            Object::String(s, _) => parse_pdf_date(String::from_utf8_lossy(s).as_ref()).ok(),
            _ => None,
        })
        .unwrap_or_else(default_date);

    let modification_date = dict
        .get(b"ModDate")
        .ok()
        .and_then(|obj| match &obj {
            Object::String(s, _) => parse_pdf_date(String::from_utf8_lossy(s).as_ref()).ok(),
            _ => None,
        })
        .unwrap_or_else(default_date);

    // If metadata date wasn't separately written, we default to the modification date.
    let metadata_date = modification_date;

    let conformance = dict
        .get(b"GTS_PDFXVersion")
        .ok()
        .and_then(|obj| match &obj {
            Object::String(s, _) => Some(PdfConformance::from_identifier_string(
                &String::from_utf8_lossy(s),
            )),
            _ => None,
        })
        .unwrap_or_default();

    let document_title = dict
        .get(b"Title")
        .ok()
        .and_then(|obj| match &obj {
            Object::String(s, _) => Some(decode_text_from_utf16be(s)),
            _ => None,
        })
        .unwrap_or_default();

    let author = dict
        .get(b"Author")
        .ok()
        .and_then(|obj| match &obj {
            Object::String(s, _) => Some(decode_text_from_utf16be(s)),
            _ => None,
        })
        .unwrap_or_default();

    let creator = dict
        .get(b"Creator")
        .ok()
        .and_then(|obj| match &obj {
            Object::String(s, _) => Some(decode_text_from_utf16be(s)),
            _ => None,
        })
        .unwrap_or_default();

    let producer = dict
        .get(b"Producer")
        .ok()
        .and_then(|obj| match &obj {
            Object::String(s, _) => Some(decode_text_from_utf16be(s)),
            _ => None,
        })
        .unwrap_or_default();

    let subject = dict
        .get(b"Subject")
        .ok()
        .and_then(|obj| match &obj {
            Object::String(s, _) => Some(decode_text_from_utf16be(s)),
            _ => None,
        })
        .unwrap_or_default();

    let identifier = dict
        .get(b"Identifier")
        .ok()
        .and_then(|obj| match &obj {
            Object::String(s, _) => Some(decode_text_from_utf16be(s)),
            _ => None,
        })
        .unwrap_or_default();

    let keywords = dict
        .get(b"Keywords")
        .ok()
        .and_then(|obj| match &obj {
            Object::String(s, _) => Some(decode_text_from_utf16be(s)),
            _ => None,
        })
        .map(|joined| {
            joined
                .split(',')
                .map(|s| s.trim().to_string())
                .filter(|s| !s.is_empty())
                .collect()
        })
        .unwrap_or_default();

    PdfDocumentInfo {
        trapped,
        version: 1, // No version key was written, so we default to 1.
        creation_date,
        modification_date,
        metadata_date,
        conformance,
        document_title,
        author,
        creator,
        producer,
        keywords,
        subject,
        identifier,
        // The output intent of a parsed file is not read.
        output_intent: None,
    }
}

/// Parses a PDF rectangle from an Object (an array of four numbers).
fn parse_rect(obj: &Object) -> Result<crate::graphics::Rect, String> {
    if let Object::Array(arr) = obj {
        let [llx, lly, urx, ury] = arr.as_slice() else {
            return Err("Rectangle array does not have 4 elements".to_string());
        };
        let number = |o: &Object| match o {
            Object::Integer(i) => Ok(*i as f32),
            Object::Real(r) => Ok(*r),
            _ => Err("Rectangle element is not a number".to_string()),
        };
        // In PDF the rectangle is given as [llx, lly, urx, ury].
        let x = number(llx)?;
        let y = number(lly)?;
        let urx = number(urx)?;
        let ury = number(ury)?;
        let width = urx - x;
        let height = ury - y;
        Ok(crate::graphics::Rect::from_xywh(
            crate::units::Pt(x),
            crate::units::Pt(y),
            crate::units::Pt(width),
            crate::units::Pt(height),
        ))
    } else {
        Err("Rectangle is not an array".to_string())
    }
}

/// Helper to parse an operand into f32
fn to_f32(obj: &Object) -> f32 {
    match obj {
        Object::Integer(i) => *i as f32,
        Object::Real(r) => *r,
        _ => 0.0,
    }
}

/// Helper to parse an operand as a PDF name
fn as_name(obj: &Object) -> Option<String> {
    if let Object::Name(ref bytes) = obj {
        Some(String::from_utf8_lossy(bytes).to_string())
    } else {
        None
    }
}

/// The pages of the document in page-tree order, each with its page number (starting at 1).
///
/// This is the list [`LopdfDocument::get_pages`] gives, read one page at a time: `get_pages`
/// collects the pages into a map first, and that reserves memory for the number of pages the
/// `/Count` entries of the page tree announce, which a file can set to any number.
pub(crate) fn numbered_pages(doc: &LopdfDocument) -> Vec<(usize, ObjectId)> {
    let mut pages = Vec::new();
    for (index, id) in doc.page_iter().enumerate() {
        pages.push((index.saturating_add(1), id));
    }
    pages
}

mod links_and_bookmarks {
    use std::collections::{BTreeMap, BTreeSet};

    use lopdf::{Dictionary, Document, Object, ObjectId};

    use crate::{
        annotation::{
            Actions, BorderArray, ColorArray, DashPhase, Destination, HighlightingMode,
            LinkAnnotation, PageAnnotation,
        },
        graphics::Rect,
        PdfWarnMsg, Pt,
    };

    /// Reads the top level of the outline (`/First` and `/Next` links; children are not
    /// followed) as bookmarks with a title and a page number. Entries without a title or a
    /// `/Dest` array are skipped. Returns an empty vector if the catalog has no outline. A
    /// `/Next` chain that returns to an entry it has read ends there, with a warning.
    pub fn extract_bookmarks(doc: &Document, warnings: &mut Vec<PdfWarnMsg>) -> Vec<PageAnnotation> {
        try_extract_bookmarks(doc, warnings).unwrap_or_default()
    }

    fn try_extract_bookmarks(
        doc: &Document,
        warnings: &mut Vec<PdfWarnMsg>,
    ) -> Option<Vec<PageAnnotation>> {
        let catalog_id = match doc.trailer.get(b"Root").ok()? {
            Object::Reference(id) => *id,
            _ => return None,
        };
        let catalog = doc.get_object(catalog_id).ok()?.as_dict().ok()?;

        let outlines_ref = match catalog.get(b"Outlines").ok()? {
            Object::Reference(id) => *id,
            _ => return None,
        };
        let outlines = doc.get_object(outlines_ref).ok()?.as_dict().ok()?;

        let mut current_ref = match outlines.get(b"First").ok()? {
            Object::Reference(id) => Some(*id),
            _ => None,
        };

        // Build a mapping from page object id to page number.
        let page_map: BTreeMap<ObjectId, usize> = super::numbered_pages(doc)
            .into_iter()
            .map(|(num, id)| (id, num))
            .collect();

        let mut bookmarks = Vec::new();
        let mut visited = BTreeSet::new();
        while let Some(bm_ref) = current_ref {
            if !visited.insert(bm_ref) {
                warnings.push(PdfWarnMsg::warning(
                    0,
                    0,
                    format!(
                        "Bookmarks: the /Next chain returns to object {} {} after {} entries; \
                         the rest is ignored",
                        bm_ref.0,
                        bm_ref.1,
                        visited.len()
                    ),
                ));
                break;
            }
            let bm_dict = doc.get_object(bm_ref).ok()?.as_dict().ok()?;
            let title = match bm_dict.get(b"Title").ok()? {
                Object::String(s, _) => s.clone(),
                _ => {
                    current_ref = next_bookmark(bm_dict);
                    continue;
                }
            };
            let dest_array = match bm_dict.get(b"Dest").ok()? {
                Object::Array(arr) => arr,
                _ => {
                    current_ref = next_bookmark(bm_dict);
                    continue;
                }
            };
            // The first element should be a reference to a page.
            let page_num = if let Some(Object::Reference(page_id)) = dest_array.first() {
                *page_map.get(page_id).unwrap_or(&0)
            } else {
                0
            };

            bookmarks.push(PageAnnotation {
                name: decode_possible_utf16be(&title),
                page: page_num,
            });

            current_ref = next_bookmark(bm_dict);
        }
        Some(bookmarks)
    }

    /// Decodes a byte array that might be UTF-16BE encoded.
    /// Returns a String regardless of the input encoding.
    fn decode_possible_utf16be(bytes: &[u8]) -> String {
        // Early return if not UTF-16BE BOM
        let Some(body) = bytes.strip_prefix(&[0xFE, 0xFF]) else {
            return String::from_utf8_lossy(bytes).to_string();
        };

        // Decode as UTF-16BE after the BOM
        let (pairs, rest) = body.as_chunks::<2>();
        let mut chars: Vec<u16> = pairs.iter().map(|p| u16::from_be_bytes(*p)).collect();

        // Handle odd length (shouldn't happen in well-formed UTF-16)
        if let [high] = rest {
            chars.push(u16::from(*high) << 8);
        }

        String::from_utf16_lossy(&chars)
    }

    fn next_bookmark(dict: &Dictionary) -> Option<ObjectId> {
        dict.get(b"Next").ok().and_then(|obj| match obj {
            Object::Reference(id) => Some(*id),
            _ => None,
        })
    }

    /// Link annotations of all pages, with the zero-based index of their page, so that the parser
    /// can put them back on the page as `Op::LinkAnnotation`. Only `/Subtype /Link` annotations
    /// with a `/Rect` and a GoTo or URI action in `/A` are read.
    pub fn extract_link_annotations(doc: &Document) -> Vec<(usize, LinkAnnotation)> {
        let pages = super::numbered_pages(doc);
        let page_map: BTreeMap<ObjectId, usize> =
            pages.iter().map(|&(num, id)| (id, num)).collect();

        pages
            .iter()
            .flat_map(|&(page_num, page_id)| {
                let page_idx = page_num.saturating_sub(1);
                let page_dict = doc.get_object(page_id).ok()?.as_dict().ok()?;
                let annots = page_dict.get(b"Annots").ok()?.as_array().ok()?;
                Some(
                    annots
                        .iter()
                        .filter_map(|annot_obj| {
                            // Expect a reference to an annotation dictionary
                            // (tolerate inline dictionaries too).
                            let annot_dict = match annot_obj {
                                Object::Reference(id) => {
                                    doc.get_object(*id).ok()?.as_dict().ok()?
                                }
                                Object::Dictionary(d) => d,
                                _ => return None,
                            };
                            if annot_dict.get(b"Subtype").ok()?.as_name().ok()? != b"Link" {
                                return None;
                            }
                            let rect_arr = annot_dict.get(b"Rect").ok()?.as_array().ok()?;
                            if rect_arr.len() != 4 {
                                return None;
                            }
                            let coords: Vec<f32> = rect_arr
                                .iter()
                                .filter_map(|obj| match obj {
                                    Object::Real(r) => Some(*r),
                                    Object::Integer(i) => Some(*i as f32),
                                    _ => None,
                                })
                                .collect();
                            let &[llx, lly, urx, ury] = coords.as_slice() else {
                                return None;
                            };
                            // /Rect is [llx lly urx ury]; from_xywh wants
                            // x, y, width, height.
                            let rect =
                                Rect::from_xywh(Pt(llx), Pt(lly), Pt(urx - llx), Pt(ury - lly));

                            let action_dict = annot_dict.get(b"A").ok()?.as_dict().ok()?;
                            let actions = extract_action(action_dict, &page_map)?;

                            let border = annot_dict
                                .get(b"Border")
                                .ok()
                                .and_then(|obj| obj.as_array().ok())
                                .map(|s| extract_border_array(s.as_slice()))
                                .unwrap_or_else(BorderArray::default);

                            let color = annot_dict
                                .get(b"C")
                                .ok()
                                .and_then(|obj| obj.as_array().ok())
                                .map(|s| extract_color_array(s.as_slice()))
                                .unwrap_or_else(ColorArray::default);

                            let highlighting = annot_dict
                                .get(b"H")
                                .and_then(|obj| obj.as_name())
                                .map(|n| match n {
                                    n if n == b"N" => HighlightingMode::None,
                                    n if n == b"I" => HighlightingMode::Invert,
                                    n if n == b"O" => HighlightingMode::Outline,
                                    n if n == b"P" => HighlightingMode::Push,
                                    _ => HighlightingMode::Invert,
                                })
                                .unwrap_or(HighlightingMode::Invert);

                            Some((
                                page_idx,
                                LinkAnnotation {
                                    rect,
                                    actions,
                                    border,
                                    color,
                                    highlighting,
                                },
                            ))
                        })
                        .collect::<Vec<_>>(),
                )
            })
            .flatten()
            .collect()
    }

    /// Converts a PDF array into our BorderArray.
    fn extract_border_array(arr: &[Object]) -> BorderArray {
        let nums: Vec<f32> = arr
            .iter()
            .filter_map(|obj| match obj {
                Object::Integer(i) => Some(*i as f32),
                Object::Real(r) => Some(*r),
                _ => None,
            })
            .collect();
        match *nums.as_slice() {
            [a, b, c] => BorderArray::Solid([a, b, c]),
            [a, b, c, phase] => BorderArray::Dashed(
                [a, b, c],
                DashPhase {
                    dash_array: vec![],
                    phase,
                },
            ),
            _ => BorderArray::default(),
        }
    }

    /// Converts a PDF array into our ColorArray.
    fn extract_color_array(arr: &[Object]) -> ColorArray {
        let nums: Vec<f32> = arr
            .iter()
            .filter_map(|obj| match obj {
                Object::Integer(i) => Some(*i as f32),
                Object::Real(r) => Some(*r),
                _ => None,
            })
            .collect();

        match *nums.as_slice() {
            [] => ColorArray::Transparent,
            [gray] => ColorArray::Gray([gray]),
            [r, g, b] => ColorArray::Rgb([r, g, b]),
            [c, m, y, k] => ColorArray::Cmyk([c, m, y, k]),
            _ => ColorArray::default(),
        }
    }

    /// Converts a PDF action dictionary into our Actions enum.
    fn extract_action(dict: &Dictionary, page_map: &BTreeMap<ObjectId, usize>) -> Option<Actions> {
        let action_type = dict.get(b"S").ok()?.as_name().ok()?;
        match action_type {
            n if n == b"GoTo" => {
                let arr = dict.get(b"D").ok()?.as_array().ok()?;
                let page = if let Some(Object::Reference(page_id)) = arr.first() {
                    *page_map.get(page_id).unwrap_or(&0)
                } else {
                    0
                };
                let left = arr.get(2).and_then(|obj| match obj {
                    Object::Real(r) => Some(*r),
                    Object::Integer(i) => Some(*i as f32),
                    _ => None,
                });
                let top = arr.get(3).and_then(|obj| match obj {
                    Object::Real(r) => Some(*r),
                    Object::Integer(i) => Some(*i as f32),
                    _ => None,
                });
                let zoom = arr.get(4).and_then(|obj| match obj {
                    Object::Real(r) => Some(*r),
                    Object::Integer(i) => Some(*i as f32),
                    _ => None,
                });
                Some(Actions::Goto(Destination::Xyz {
                    page,
                    left,
                    top,
                    zoom,
                }))
            }
            n if n == b"URI" => {
                let uri = String::from_utf8_lossy(dict.get(b"URI").ok()?.as_str().ok()?);
                Some(Actions::Uri(uri.to_string()))
            }
            _ => None,
        }
    }
}

mod layers {

    use std::collections::{BTreeMap, BTreeSet};

    use lopdf::{Document, Object, ObjectId};

    use crate::ops::{Layer, LayerIntent, LayerSubtype};

    /// Resolve one level of indirection, returning the target object.
    fn resolve<'a>(doc: &'a Document, obj: &'a Object) -> &'a Object {
        match obj {
            Object::Reference(r) => doc.get_object(*r).unwrap_or(obj),
            other => other,
        }
    }

    /// Extract layers (optional content groups) from the PDF document,
    /// together with the resource name under which each is exposed to content
    /// streams.
    ///
    /// Content streams reference layers as `BDC /OC /SomeName`, where
    /// `/SomeName` is a key of the page's `/Resources` → `/Properties`
    /// dictionary that points at the OCG object. The layer map of the parsed
    /// document must be keyed by those names — the parser stores them in
    /// `Op::BeginOptionalContent { layer_id }` — otherwise every re-saved
    /// `BDC` references a property that does not exist and viewers drop the
    /// layer content ("Marked Content 'X' is unknown").
    ///
    /// Returns `(Some(resource_name), layer)` for OCGs reachable from some
    /// page's `/Properties`, `(None, layer)` for OCGs only listed in the
    /// catalog's `/OCProperties`.
    pub fn extract_layers(doc: &Document) -> Vec<(Option<String>, Layer)> {
        // OCG object id -> resource name (first one wins across pages).
        let mut ocg_names: BTreeMap<ObjectId, String> = BTreeMap::new();
        for (_page_num, page_id) in super::numbered_pages(doc) {
            let Ok(page_dict) = doc.get_dictionary(page_id) else {
                continue;
            };
            let Ok(res_obj) = page_dict.get(b"Resources") else {
                continue;
            };
            let Object::Dictionary(res_dict) = resolve(doc, res_obj) else {
                continue;
            };
            let Ok(props_obj) = res_dict.get(b"Properties") else {
                continue;
            };
            let Object::Dictionary(props) = resolve(doc, props_obj) else {
                continue;
            };
            for (name, val) in props.iter() {
                if let Object::Reference(ocg_id) = val {
                    ocg_names
                        .entry(*ocg_id)
                        .or_insert_with(|| String::from_utf8_lossy(name).to_string());
                }
            }
        }

        let catalog = match doc.trailer.get(b"Root").ok().map(|o| resolve(doc, o)) {
            Some(Object::Dictionary(dict)) => dict,
            _ => return vec![],
        };

        let ocprops = match catalog.get(b"OCProperties").ok().map(|o| resolve(doc, o)) {
            Some(Object::Dictionary(dict)) => dict,
            _ => return vec![],
        };

        let ocgs = match ocprops.get(b"OCGs").ok().map(|o| resolve(doc, o)) {
            Some(Object::Array(arr)) => arr,
            _ => return vec![],
        };

        // Also, if available, get the "ON" array from the "D" dictionary
        // to decide which layers are turned on by default.
        let default_on = match ocprops.get(b"D").ok().map(|o| resolve(doc, o)) {
            Some(Object::Dictionary(d_dict)) => {
                if let Ok(Object::Array(on_arr)) = d_dict.get(b"ON") {
                    on_arr
                        .iter()
                        .filter_map(|obj| {
                            if let Object::Reference(id) = obj {
                                Some(*id)
                            } else {
                                None
                            }
                        })
                        .collect::<BTreeSet<ObjectId>>()
                } else {
                    BTreeSet::new()
                }
            }
            _ => BTreeSet::new(),
        };

        let mut layers = Vec::new();
        for obj in ocgs {
            let layer_ref = match obj {
                Object::Reference(id) => id,
                _ => continue,
            };
            let layer_dict = match doc.get_object(*layer_ref) {
                Ok(Object::Dictionary(dict)) => dict,
                _ => continue,
            };

            let name = match layer_dict.get(b"Name").ok() {
                Some(Object::String(s, _)) => s.clone(),
                _ => continue,
            };

            // Extract the intent from the "Intent" key: either a name, an
            // inline array of names, or a reference to such an array.
            let intent = match layer_dict.get(b"Intent").ok().map(|o| resolve(doc, o)) {
                Some(Object::Array(arr)) => match arr.first() {
                    Some(Object::Name(n)) if n.as_slice() == b"View" => LayerIntent::View,
                    _ => LayerIntent::Design,
                },
                Some(Object::Name(n)) if n.as_slice() == b"View" => LayerIntent::View,
                _ => LayerIntent::Design,
            };

            let (creator, usage_subtype) =
                match layer_dict.get(b"Usage").ok().map(|o| resolve(doc, o)) {
                    Some(Object::Dictionary(usage_dict)) => {
                        if let Some(Object::Dictionary(creator_info)) =
                            usage_dict.get(b"CreatorInfo").ok().map(|o| resolve(doc, o))
                        {
                            let creator = match creator_info.get(b"Creator").ok() {
                                Some(Object::String(s, _)) => {
                                    String::from_utf8_lossy(s).to_string()
                                }
                                _ => String::new(),
                            };
                            let usage_subtype = match creator_info.get(b"Subtype").ok() {
                                Some(Object::Name(n)) => match n.as_slice() {
                                    b"Artwork" => LayerSubtype::Artwork,
                                    _ => LayerSubtype::Artwork,
                                },
                                _ => LayerSubtype::Artwork,
                            };
                            (creator, usage_subtype)
                        } else {
                            (String::new(), LayerSubtype::Artwork)
                        }
                    }
                    _ => (String::new(), LayerSubtype::Artwork),
                };

            // Decide final intent: if this layer's object ID is in the default "ON" set,
            // we treat its intent as View.
            let final_intent = if default_on.contains(layer_ref) {
                LayerIntent::View
            } else {
                intent
            };

            layers.push((
                ocg_names.get(layer_ref).cloned(),
                Layer {
                    name: String::from_utf8_lossy(&name).to_string(),
                    creator,
                    intent: final_intent,
                    usage: usage_subtype,
                },
            ));
        }

        layers
    }
}

mod extgstate {

    use std::collections::HashSet;

    use lopdf::{Dictionary as LoDictionary, Object};

    use crate::{
        graphics::{
            BlendMode, ChangedField, ExtendedGraphicsState, LineCapStyle, LineDashPattern,
            LineJoinStyle, OverprintMode, RenderingIntent,
        },
        BuiltinFont, BuiltinOrExternalFontId, FontId,
    };

    /// Given a PDF ExtGState dictionary, parse it into an ExtendedGraphicsState.
    pub fn parse_extgstate(dict: &LoDictionary) -> ExtendedGraphicsState {
        let mut gs = ExtendedGraphicsState::default();
        let mut changed = HashSet::new();

        if let Ok(obj) = dict.get(b"LW") {
            if let Some(num) = parse_f32(obj) {
                gs.line_width = num;
                changed.insert(ChangedField::LineWidth);
            }
        }

        if let Ok(obj) = dict.get(b"LC") {
            if let Some(num) = parse_i64(obj) {
                gs.line_cap = match num {
                    0 => LineCapStyle::Butt,
                    1 => LineCapStyle::Round,
                    2 => LineCapStyle::ProjectingSquare,
                    _ => LineCapStyle::Butt,
                };
                changed.insert(ChangedField::LineCap);
            }
        }

        if let Ok(obj) = dict.get(b"LJ") {
            if let Some(num) = parse_i64(obj) {
                gs.line_join = match num {
                    0 => LineJoinStyle::Miter,
                    1 => LineJoinStyle::Round,
                    2 => LineJoinStyle::Bevel,
                    _ => LineJoinStyle::Miter,
                };
                changed.insert(ChangedField::LineJoin);
            }
        }

        if let Ok(obj) = dict.get(b"ML") {
            if let Some(num) = parse_f32(obj) {
                gs.miter_limit = num;
                changed.insert(ChangedField::MiterLimit);
            }
        }

        if let Ok(obj) = dict.get(b"FL") {
            if let Some(num) = parse_f32(obj) {
                gs.flatness_tolerance = num;
                changed.insert(ChangedField::FlatnessTolerance);
            }
        }

        if let Ok(obj) = dict.get(b"RI") {
            if let Some(name) = parse_name(obj) {
                gs.rendering_intent = match name.as_str() {
                    "AbsoluteColorimetric" => RenderingIntent::AbsoluteColorimetric,
                    "RelativeColorimetric" => RenderingIntent::RelativeColorimetric,
                    "Saturation" => RenderingIntent::Saturation,
                    "Perceptual" => RenderingIntent::Perceptual,
                    _ => RenderingIntent::RelativeColorimetric,
                };
                changed.insert(ChangedField::RenderingIntent);
            }
        }

        if let Ok(obj) = dict.get(b"SA") {
            if let Some(b) = parse_bool(obj) {
                gs.stroke_adjustment = b;
                changed.insert(ChangedField::StrokeAdjustment);
            }
        }

        if let Ok(obj) = dict.get(b"OP") {
            if let Some(b) = parse_bool(obj) {
                gs.overprint_fill = b;
                changed.insert(ChangedField::OverprintFill);
            }
        }

        if let Ok(obj) = dict.get(b"op") {
            if let Some(b) = parse_bool(obj) {
                gs.overprint_stroke = b;
                changed.insert(ChangedField::OverprintStroke);
            }
        }

        if let Ok(obj) = dict.get(b"OPM") {
            if let Some(num) = parse_i64(obj) {
                gs.overprint_mode = match num {
                    0 => OverprintMode::EraseUnderlying,
                    1 => OverprintMode::KeepUnderlying,
                    _ => OverprintMode::EraseUnderlying,
                };
                changed.insert(ChangedField::OverprintMode);
            }
        }

        // `CA` is the stroking alpha, `ca` the nonstroking (fill) alpha.
        if let Ok(obj) = dict.get(b"CA") {
            if let Some(num) = parse_f32(obj) {
                gs.current_stroke_alpha = num;
                changed.insert(ChangedField::CurrentStrokeAlpha);
            }
        }

        if let Ok(obj) = dict.get(b"ca") {
            if let Some(num) = parse_f32(obj) {
                gs.current_fill_alpha = num;
                changed.insert(ChangedField::CurrentFillAlpha);
            }
        }

        if let Ok(obj) = dict.get(b"BM") {
            if let Some(name) = parse_name(obj) {
                gs.blend_mode = parse_blend_mode(&name);
                changed.insert(ChangedField::BlendMode);
            }
        }

        if let Ok(obj) = dict.get(b"AIS") {
            if let Some(b) = parse_bool(obj) {
                gs.alpha_is_shape = b;
                changed.insert(ChangedField::AlphaIsShape);
            }
        }

        if let Ok(obj) = dict.get(b"TK") {
            if let Some(b) = parse_bool(obj) {
                gs.text_knockout = b;
                changed.insert(ChangedField::TextKnockout);
            }
        }

        if let Ok(obj) = dict.get(b"D") {
            if let Ok(arr) = obj.as_array() {
                let pattern = arr.iter().filter_map(parse_f32).collect();
                gs.line_dash_pattern = Some(LineDashPattern {
                    offset: 0.0,
                    pattern,
                }); // default offset = 0
                changed.insert(ChangedField::LineDashPattern);
            }
        }

        if let Ok(obj) = dict.get(b"Font") {
            if let Some(name) = parse_name(obj) {
                gs.font = Some(match BuiltinFont::from_id(&name) {
                    Some(s) => BuiltinOrExternalFontId::Builtin(s),
                    None => BuiltinOrExternalFontId::External(FontId(name)),
                });
                changed.insert(ChangedField::Font);
            }
        }

        if let Ok(obj) = dict.get(b"SM") {
            if let Some(name) = parse_name(obj) {
                // Only `/SM /None` is understood.
                if name == "None" {
                    gs.soft_mask = None;
                } else {
                    // Other soft masks are dropped.
                    gs.soft_mask = None;
                }
                changed.insert(ChangedField::SoftMask);
            }
        }

        // Other keys (black generation, under-color removal, transfer functions, halftones) are
        // not read.

        gs.changed_fields = changed;
        gs
    }

    /// Helper: Convert a PDF Object into an f32, if possible.
    fn parse_f32(obj: &Object) -> Option<f32> {
        match obj {
            Object::Real(r) => Some(*r),
            Object::Integer(i) => Some(*i as f32),
            _ => None,
        }
    }

    /// Helper: Convert a PDF Object into an i64, if possible.
    fn parse_i64(obj: &Object) -> Option<i64> {
        match obj {
            Object::Integer(i) => Some(*i),
            Object::Real(r) => Some(*r as i64),
            _ => None,
        }
    }

    /// Helper: Convert a PDF Object into a bool, if possible.
    fn parse_bool(obj: &Object) -> Option<bool> {
        match obj {
            Object::Boolean(b) => Some(*b),
            _ => None,
        }
    }

    /// Helper: Convert a PDF Name object into a String.
    fn parse_name(obj: &Object) -> Option<String> {
        match obj {
            Object::Name(bytes) => String::from_utf8(bytes.clone()).ok(),
            _ => None,
        }
    }

    /// Helper: Convert a name string into a BlendMode.
    fn parse_blend_mode(s: &str) -> BlendMode {
        use crate::graphics::{BlendMode, NonSeperableBlendMode, SeperableBlendMode};
        match s {
            "Normal" => BlendMode::Seperable(SeperableBlendMode::Normal),
            "Multiply" => BlendMode::Seperable(SeperableBlendMode::Multiply),
            "Screen" => BlendMode::Seperable(SeperableBlendMode::Screen),
            "Overlay" => BlendMode::Seperable(SeperableBlendMode::Overlay),
            "Darken" => BlendMode::Seperable(SeperableBlendMode::Darken),
            "Lighten" => BlendMode::Seperable(SeperableBlendMode::Lighten),
            "ColorDodge" => BlendMode::Seperable(SeperableBlendMode::ColorDodge),
            "ColorBurn" => BlendMode::Seperable(SeperableBlendMode::ColorBurn),
            "HardLight" => BlendMode::Seperable(SeperableBlendMode::HardLight),
            "SoftLight" => BlendMode::Seperable(SeperableBlendMode::SoftLight),
            "Difference" => BlendMode::Seperable(SeperableBlendMode::Difference),
            "Exclusion" => BlendMode::Seperable(SeperableBlendMode::Exclusion),
            "Hue" => BlendMode::NonSeperable(NonSeperableBlendMode::Hue),
            "Saturation" => BlendMode::NonSeperable(NonSeperableBlendMode::Saturation),
            "Color" => BlendMode::NonSeperable(NonSeperableBlendMode::Color),
            "Luminosity" => BlendMode::NonSeperable(NonSeperableBlendMode::Luminosity),
            _ => BlendMode::Seperable(SeperableBlendMode::Normal),
        }
    }
}

mod parsefont {
    use std::collections::BTreeMap;
    use std::sync::Arc;

    use lopdf::{Dictionary, Document, Object};

    use super::{decode_stream, ParsedExternalFont, ParsedOrBuiltinFont, MAX_STREAM_BYTES};
    use crate::{
        cmap::ToUnicodeCMap,
        deserialize::{get_dict_or_resolve_ref, PdfWarnMsg},
        font::ParsedFont,
        BuiltinFont, FontId,
    };

    /// Build a code -> unicode map from the font's own cmap table, for fonts
    /// that ship without a usable `/ToUnicode` CMap. This makes glyph runs
    /// extractable and lets the serializer regenerate a correct ToUnicode CMap
    /// instead of mapping every glyph to U+FFFD.
    ///
    /// ToUnicode is keyed by the content-stream code — the CID under Identity-H.
    /// When the descendant font maps CIDs to different glyph ids (`cid_to_gid`),
    /// the font-cmap gid must be re-keyed through the inverse of that map;
    /// otherwise code == gid.
    fn synthesize_to_unicode(
        font: &ParsedFont,
        cid_to_gid: Option<&BTreeMap<u16, u16>>,
    ) -> Option<ToUnicodeCMap> {
        let gid_to_cid: Option<BTreeMap<u16, u16>> =
            cid_to_gid.map(|m| m.iter().map(|(cid, gid)| (*gid, *cid)).collect());
        let mut mappings: BTreeMap<u32, Vec<u32>> = BTreeMap::new();
        // The full scalar-value range: supplementary-plane codepoints (CJK Ext B+,
        // emoji) are exactly what the CJK fonts hitting this path use.
        for cp in 0x0000..=0x10FFFFu32 {
            // skip UTF-16 surrogate range, not scalar values
            if (0xD800..=0xDFFF).contains(&cp) {
                continue;
            }
            if let Some(gid) = font.lookup_glyph_index(cp) {
                let code = gid_to_cid
                    .as_ref()
                    .and_then(|m| m.get(&gid).copied())
                    .unwrap_or(gid);
                mappings.entry(code as u32).or_insert_with(|| vec![cp]);
            }
        }
        if mappings.is_empty() {
            None
        } else {
            Some(ToUnicodeCMap { mappings })
        }
    }

    /// Resolve a Type0 font's `/Encoding` into a [`crate::cmap::CidCMap`], or
    /// `None` for Identity-H (the fast path: 2-byte codes, code == CID).
    ///
    /// Fully resolved: embedded CMap streams (codespace/cidrange/cidchar/
    /// notdefrange, `usecmap` of the identity CMaps) and `Identity-V` (decodes
    /// identically to Identity-H; the vertical writing mode itself is not
    /// preserved — re-saves are horizontal, and parsing says so). Predefined
    /// CJK CMaps require Adobe's CMap data files, which printpdf does not ship:
    /// those warn and fall back to 2-byte identity.
    fn extract_encoding_cmap(
        doc: &Document,
        font_dict: &Dictionary,
        font_id: &FontId,
        warnings: &mut Vec<PdfWarnMsg>,
        page_num: usize,
    ) -> Option<crate::cmap::CidCMap> {
        use crate::cmap::CidCMap;

        fn from_stream(
            s: &lopdf::Stream,
            font_id: &FontId,
            warnings: &mut Vec<PdfWarnMsg>,
            page_num: usize,
        ) -> Option<CidCMap> {
            let data = decode_stream(
                s,
                MAX_STREAM_BYTES,
                &format!("Type0 font {} /Encoding CMap", font_id.0),
                page_num,
                warnings,
            )?;
            match CidCMap::parse(&String::from_utf8_lossy(&data)) {
                Ok((cmap, unresolved_usecmap)) => {
                    if let Some(base) = unresolved_usecmap {
                        warnings.push(PdfWarnMsg::warning(
                            page_num,
                            0,
                            format!(
                                "Type0 font {}: embedded CMap uses /{base} as its \
                                 usecmap base, which printpdf cannot resolve — codes \
                                 the CMap itself does not map will decode as notdef",
                                font_id.0
                            ),
                        ));
                    }
                    if cmap.wmode == 1 {
                        warnings.push(PdfWarnMsg::warning(
                            page_num,
                            0,
                            format!(
                                "Type0 font {}: vertical writing (WMode 1) is decoded \
                                 but not preserved — re-saves are horizontal",
                                font_id.0
                            ),
                        ));
                    }
                    Some(cmap)
                }
                Err(e) => {
                    warnings.push(PdfWarnMsg::warning(
                        page_num,
                        0,
                        format!(
                            "Type0 font {}: embedded /Encoding CMap failed to parse \
                             ({e}); assuming 2-byte Identity codes",
                            font_id.0
                        ),
                    ));
                    None
                }
            }
        }

        match font_dict.get(b"Encoding") {
            Ok(Object::Name(n)) if n.as_slice() == b"Identity-H" => None,
            Ok(Object::Name(n)) if n.as_slice() == b"Identity-V" => {
                warnings.push(PdfWarnMsg::warning(
                    page_num,
                    0,
                    format!(
                        "Type0 font {} uses Identity-V: codes decode correctly, but \
                         vertical writing is not preserved — re-saves are horizontal",
                        font_id.0
                    ),
                ));
                Some(CidCMap::identity(1))
            }
            Ok(Object::Name(n)) => {
                warnings.push(PdfWarnMsg::warning(
                    page_num,
                    0,
                    format!(
                        "Type0 font {} uses the predefined CMap /{} — printpdf does \
                         not ship Adobe's CMap data; text may decode and re-save \
                         incorrectly",
                        font_id.0,
                        String::from_utf8_lossy(n),
                    ),
                ));
                None
            }
            Ok(Object::Stream(s)) => from_stream(s, font_id, warnings, page_num),
            Ok(Object::Reference(r)) => match doc.get_object(*r) {
                Ok(Object::Stream(s)) => from_stream(s, font_id, warnings, page_num),
                _ => None,
            },
            _ => None, // absent: Identity-H by convention
        }
    }

    /// The descendant font's CID -> glyph-id map, when it is not identity.
    ///
    /// Two sources, matching how viewers resolve Identity-H codes (ISO 32000-1,
    /// 9.7.4.2):
    /// - CIDFontType2: an explicit `/CIDToGIDMap` stream — 2 big-endian bytes per
    ///   CID. (The `/Identity` name, or no entry, means CID == GID.)
    /// - CIDFontType0 with a CID-keyed CFF program: the CFF charset, inverted.
    ///
    /// Only non-identity entries are stored; lookups fall back to CID == GID.
    ///
    /// The CFF-charset source is checked against the document's ToUnicode CMap and the font's own
    /// cmap: some producers (older printpdf versions among them) wrote glyph ids as Identity-H
    /// codes even for CID-keyed CFF fonts whose charset says otherwise. PDFium renders such
    /// files with code == GID, and reading them per spec would scramble glyph identities on
    /// re-save. When the identity reading explains the document's own code, unicode and glyph
    /// triple better than the charset reading, the charset map is discarded. An explicit
    /// `/CIDToGIDMap` stream is never second-guessed.
    fn extract_cid_to_gid_map(
        doc: &Document,
        font_dict: &Dictionary,
        font_id: &FontId,
        parsed_font: &ParsedFont,
        to_unicode: Option<&ToUnicodeCMap>,
        warnings: &mut Vec<PdfWarnMsg>,
        page_num: usize,
    ) -> Option<BTreeMap<u16, u16>> {
        let descendant = get_descendant_font_dict(doc, font_dict, font_id, warnings, page_num)?;

        let mut stream_to_map = |s: &lopdf::Stream| -> Option<BTreeMap<u16, u16>> {
            // Already-plain streams return the content as-is on error paths of some lopdf
            // versions; the raw bytes are the fallback.
            let data = decode_stream(
                s,
                MAX_STREAM_BYTES,
                &format!("font {} /CIDToGIDMap", font_id.0),
                page_num,
                warnings,
            )?;
            Some(
                data.as_chunks::<2>()
                    .0
                    .iter()
                    .enumerate()
                    // A CID is 16 bits wide.
                    .take(1 << 16)
                    .filter_map(|(cid, b)| {
                        let cid = u16::try_from(cid).ok()?;
                        let gid = u16::from_be_bytes(*b);
                        (gid != cid).then_some((cid, gid))
                    })
                    .collect(),
            )
        };

        match descendant.get(b"CIDToGIDMap") {
            Ok(Object::Stream(s)) => return stream_to_map(s),
            Ok(Object::Reference(r)) => {
                if let Ok(Object::Stream(s)) = doc.get_object(*r) {
                    return stream_to_map(s);
                }
            }
            _ => {} // absent or /Identity
        }

        // CID-keyed CFF: invert the charset's gid -> cid map. Identity entries are
        // dropped so `None`/empty means "no remapping needed".
        #[cfg(feature = "text_layout")]
        let bytes = parsed_font.source_bytes()?;
        #[cfg(feature = "text_layout")]
        let bytes = bytes.as_slice();
        #[cfg(not(feature = "text_layout"))]
        let bytes = parsed_font.original_bytes.as_slice();

        let map: BTreeMap<u16, u16> = crate::font::cff_charset_gid_to_cid_map(bytes, 0)?
            .into_iter()
            .filter_map(|(gid, cid)| (gid != cid).then_some((cid, gid)))
            .collect();
        if map.is_empty() {
            return None;
        }

        // Disambiguate spec-correct files from legacy code==GID files: for every
        // ToUnicode entry whose character the font's cmap knows, check which
        // reading of the code lands on the cmap's glyph.
        if let Some(tu) = to_unicode {
            let mut spec_hits = 0usize;
            let mut identity_hits = 0usize;
            for (code, unis) in tu.mappings.iter() {
                let Some(&cp) = unis.first() else { continue };
                let Ok(code16) = u16::try_from(*code) else {
                    continue;
                };
                let Some(cmap_gid) = parsed_font.lookup_glyph_index(cp) else {
                    continue;
                };
                let spec_gid = map.get(&code16).copied().unwrap_or(code16);
                if spec_gid == cmap_gid {
                    spec_hits += 1;
                }
                if code16 == cmap_gid {
                    identity_hits += 1;
                }
            }
            if identity_hits > spec_hits {
                warnings.push(PdfWarnMsg::info(
                    page_num,
                    0,
                    format!(
                        "font {}: CID-keyed CFF charset is not identity, but the \
                         document's codes match glyph ids, not CIDs ({} vs {} cmap \
                         agreements) — treating codes as glyph ids (legacy printpdf \
                         convention, #280)",
                        font_id.0, identity_hits, spec_hits
                    ),
                ));
                return None;
            }
        }

        Some(map)
    }

    /// Parses the `/Font` resources of one resource dictionary, keyed by resource name.
    ///
    /// A font whose name is a key of `known` is skipped: the first definition of a name wins.
    pub(crate) fn parse_fonts_enhanced(
        doc: &Document,
        resources: &Dictionary,
        warnings: &mut Vec<PdfWarnMsg>,
        page: Option<usize>,
        known: &BTreeMap<crate::BuiltinOrExternalFontId, ParsedOrBuiltinFont>,
    ) -> BTreeMap<FontId, ParsedOrBuiltinFont> {
        let mut fonts_map = BTreeMap::new();
        let page_num = page.unwrap_or_default();

        let fonts_dict = match get_fonts_dict_from_resources(doc, resources, warnings, page_num) {
            Some(dict) => dict,
            None => return fonts_map,
        };

        for (key, value) in fonts_dict.iter() {
            let font_id = FontId(String::from_utf8_lossy(key).to_string());
            if known.contains_key(&crate::BuiltinOrExternalFontId::External(font_id.clone())) {
                continue;
            }

            let font_dict = match get_dict_or_resolve_ref(
                &format!("parse_fonts font_dict page {page_num}"),
                doc,
                value,
                warnings,
                page,
            ) {
                Some(s) => s,
                None => continue,
            };

            let font_type = match get_font_subtype(font_dict, &font_id, warnings, page_num) {
                Some(t) => t,
                None => continue,
            };

            // Extract ToUnicode CMap directly from the font dictionary (common to all font types)
            let to_unicode_cmap = extract_to_unicode_cmap(doc, font_dict, warnings, page_num);

            if &font_type == b"Type0" {
                // Resolve the Type0 /Encoding: how content-stream bytes become codes
                // (codespace ranges — 1–4 bytes, variable width) and codes become
                // CIDs. `None` = Identity-H, the fast path. Embedded CMap streams
                // and Identity-V are resolved fully; predefined CJK CMaps
                // (90ms-RKSJ-H, GBK-EUC-H, …) need Adobe's CMap data files, which
                // printpdf does not ship — those warn and fall back to 2-byte
                // identity.
                let encoding_cmap =
                    extract_encoding_cmap(doc, font_dict, &font_id, warnings, page_num)
                        .map(Arc::new);
                // An external font is a Type0 (CID) font; this branch does not need `text_layout`.
                match process_type0_font(doc, font_dict, &font_id, warnings, page_num) {
                    Some(parsed_font) => {
                        let cid_to_gid = extract_cid_to_gid_map(
                            doc,
                            font_dict,
                            &font_id,
                            &parsed_font,
                            to_unicode_cmap.as_ref(),
                            warnings,
                            page_num,
                        )
                        .map(Arc::new);
                        // Without a ToUnicode CMap, glyph runs would extract as
                        // U+FFFD and the re-saved ToUnicode would be garbage;
                        // synthesize one from the font's own cmap table.
                        let to_unicode = to_unicode_cmap
                            .or_else(|| synthesize_to_unicode(&parsed_font, cid_to_gid.as_deref()))
                            .map(Arc::new);
                        fonts_map.insert(
                            font_id,
                            ParsedOrBuiltinFont::P(ParsedExternalFont {
                                font: parsed_font,
                                to_unicode,
                                is_cid: true,
                                cid_to_gid,
                                encoding_cmap,
                            }),
                        );
                    }
                    None => {
                        // Keep the code layout + ToUnicode for decoding even
                        // though the font itself cannot be re-embedded.
                        warnings.push(PdfWarnMsg::warning(
                            page_num,
                            0,
                            format!(
                                "Type0 font {} has no parsable font program; text will decode \
                                 via its ToUnicode CMap but the font cannot be re-embedded",
                                font_id.0
                            ),
                        ));
                        fonts_map.insert(
                            font_id,
                            ParsedOrBuiltinFont::CidStub(
                                to_unicode_cmap.map(Arc::new),
                                encoding_cmap,
                            ),
                        );
                    }
                }
            } else {
                match process_standard_font(doc, font_dict, &font_id, warnings, page_num) {
                    Some(ParsedOrBuiltinFont::B(builtin)) => {
                        fonts_map.insert(font_id, ParsedOrBuiltinFont::B(builtin));
                    }
                    Some(ParsedOrBuiltinFont::P(p)) => {
                        // Without a ToUnicode CMap the text of a simple font follows its
                        // `/Encoding` (base encoding, `/Differences`) and, for a CFF program,
                        // the program's own encoding.
                        let to_unicode = to_unicode_cmap
                            .or_else(|| simple_font_encoding_cmap(doc, font_dict))
                            .map(Arc::new);
                        fonts_map.insert(
                            font_id,
                            ParsedOrBuiltinFont::P(keep_glyph_codes(ParsedExternalFont {
                                to_unicode,
                                ..p
                            })),
                        );
                    }
                    Some(ParsedOrBuiltinFont::CidStub(..)) | None => {}
                }
            }
        }

        fonts_map
    }

    /// A CMap of one-byte codes, every code its own CID.
    #[cfg(feature = "text_layout")]
    const ONE_BYTE_CMAP: &str = "/CIDInit /ProcSet findresource begin 12 dict begin begincmap \
        /CMapName /OneByte def /CMapType 1 def \
        1 begincodespacerange <00> <FF> endcodespacerange \
        1 begincidrange <00> <FF> 0 endcidrange endcmap end end";

    /// Reads a simple font whose program has no Unicode `cmap` as a font of one-byte codes, with
    /// the glyph of each code taken from the program's own map. A lookup by character finds the
    /// wrong glyph or none in such a font, so its text keeps the glyph ids. The characters come
    /// from the ToUnicode CMap or the `/Encoding`, and from WinAnsi for a font that has neither.
    /// A font without such a map is returned as it is.
    #[cfg(feature = "text_layout")]
    fn keep_glyph_codes(font: ParsedExternalFont) -> ParsedExternalFont {
        if font.is_cid {
            return font;
        }
        let Some(glyphs) = font.font.source_bytes().and_then(|bytes| {
            crate::font::code_to_glyph_without_unicode(bytes.as_slice(), font.font.original_index)
        }) else {
            return font;
        };
        let Ok((codes, _)) = crate::cmap::CidCMap::parse(ONE_BYTE_CMAP) else {
            return font;
        };
        let to_unicode = font
            .to_unicode
            .clone()
            .unwrap_or_else(|| Arc::new(win_ansi_cmap()));
        ParsedExternalFont {
            is_cid: true,
            to_unicode: Some(to_unicode),
            cid_to_gid: Some(Arc::new(glyphs)),
            encoding_cmap: Some(Arc::new(codes)),
            ..font
        }
    }

    /// The characters of the codes 0x20 to 0xFF in WinAnsi.
    #[cfg(feature = "text_layout")]
    fn win_ansi_cmap() -> ToUnicodeCMap {
        let mappings = (0x20u8..=0xFF)
            .filter_map(|code| {
                let c = crate::text::win_ansi_char(code);
                (c != '\u{FFFD}').then(|| (u32::from(code), vec![u32::from(c)]))
            })
            .collect();
        ToUnicodeCMap { mappings }
    }

    /// Without `text_layout` a simple font has no font program to read.
    #[cfg(not(feature = "text_layout"))]
    fn keep_glyph_codes(font: ParsedExternalFont) -> ParsedExternalFont {
        font
    }

    /// The characters of the codes of a simple font from its `/Encoding` and the built-in encoding
    /// of its CFF program, as a CMap. `None` when the font names no encoding at all.
    fn simple_font_encoding_cmap(doc: &Document, font_dict: &Dictionary) -> Option<ToUnicodeCMap> {
        let resolve = |o: &Object| match o {
            Object::Reference(r) => doc.get_object(*r).ok().cloned(),
            other => Some(other.clone()),
        };
        let encoding = crate::encoding::SimpleEncoding::from_font_dict(font_dict, resolve);
        #[cfg(feature = "text_layout")]
        let builtin = {
            let mut ignored = Vec::new();
            let program = get_font_descriptor(doc, font_dict, &FontId(String::new()), &mut ignored, 0)
                .and_then(|descriptor| descriptor.get(b"FontFile3").ok())
                .and_then(|file| get_font_data(doc, file, &FontId(String::new()), &mut ignored, 0));
            program.map(|bytes| crate::font::cff_builtin_encoding(&bytes)).unwrap_or_default()
        };
        #[cfg(not(feature = "text_layout"))]
        let builtin = BTreeMap::new();
        if encoding.is_empty() && builtin.is_empty() {
            return None;
        }
        Some(encoding.to_unicode(&builtin))
    }

    /// Parses the `/ToUnicode` CMap stream of a font dictionary. Warns and returns `None` when
    /// it cannot be parsed.
    pub(crate) fn extract_to_unicode_cmap(
        doc: &Document,
        font_dict: &Dictionary,
        warnings: &mut Vec<PdfWarnMsg>,
        page_num: usize,
    ) -> Option<ToUnicodeCMap> {
        if let Ok(to_unicode_ref) = font_dict.get(b"ToUnicode") {
            let to_unicode_stream = match to_unicode_ref {
                Object::Stream(s) => Some(s),
                Object::Reference(r) => match doc.get_object(*r) {
                    Ok(Object::Stream(s)) => Some(s),
                    _ => None,
                },
                _ => None,
            };

            if let Some(stream) = to_unicode_stream {
                let content = decode_stream(
                    stream,
                    MAX_STREAM_BYTES,
                    "ToUnicode CMap",
                    page_num,
                    warnings,
                )?;

                if let Ok(cmap_str) = String::from_utf8(content) {
                    match ToUnicodeCMap::parse(&cmap_str) {
                        Ok(cmap) => {
                            return Some(cmap);
                        }
                        Err(e) => {
                            warnings.push(PdfWarnMsg::warning(
                                page_num,
                                0,
                                format!("Failed to parse ToUnicode CMap: {}", e),
                            ));
                        }
                    }
                }
            }
        }

        None
    }

    /// Get fonts dictionary from PDF resources
    fn get_fonts_dict_from_resources<'a>(
        doc: &'a Document,
        resources: &'a Dictionary,
        warnings: &mut Vec<PdfWarnMsg>,
        page_num: usize,
    ) -> Option<&'a Dictionary> {
        let font_map = match resources.get(b"Font") {
            Ok(s) => s,
            Err(_) => return None,
        };

        get_dict_or_resolve_ref(
            &format!("parse_fonts Font page {page_num}"),
            doc,
            font_map,
            warnings,
            Some(page_num),
        )
    }

    /// Get font subtype from font dictionary
    fn get_font_subtype(
        font_dict: &Dictionary,
        font_id: &FontId,
        warnings: &mut Vec<PdfWarnMsg>,
        page_num: usize,
    ) -> Option<Vec<u8>> {
        match font_dict.get(b"Subtype") {
            Ok(Object::Name(n)) => Some(n.clone()),
            _ => {
                warnings.push(PdfWarnMsg::warning(
                    page_num,
                    0,
                    format!("Font {} missing Subtype", font_id.0),
                ));
                None
            }
        }
    }

    /// Process a Type0 (composite) font
    fn process_type0_font(
        doc: &Document,
        font_dict: &Dictionary,
        font_id: &FontId,
        warnings: &mut Vec<PdfWarnMsg>,
        page_num: usize,
    ) -> Option<ParsedFont> {
        let descendant_font_dict =
            get_descendant_font_dict(doc, font_dict, font_id, warnings, page_num)?;

        let font_descriptor =
            get_font_descriptor(doc, descendant_font_dict, font_id, warnings, page_num)?;

        process_font_data(doc, font_dict, font_descriptor, font_id, warnings, page_num)
    }

    /// Get the descendant font dictionary for a Type0 font
    fn get_descendant_font_dict<'a>(
        doc: &'a Document,
        font_dict: &'a Dictionary,
        font_id: &FontId,
        warnings: &mut Vec<PdfWarnMsg>,
        page_num: usize,
    ) -> Option<&'a Dictionary> {
        match font_dict.get(b"DescendantFonts") {
            Ok(Object::Array(arr)) if !arr.is_empty() => {
                let first = arr.first();
                match first.and_then(|f| f.as_dict().ok()).or_else(|| {
                    first
                        .and_then(|f| f.as_reference().ok())
                        .and_then(|id| doc.get_dictionary(id).ok())
                }) {
                    Some(d) => Some(d),
                    None => {
                        warnings.push(PdfWarnMsg::warning(
                            page_num,
                            0,
                            format!("Cannot resolve descendant font for {}", font_id.0),
                        ));
                        None
                    }
                }
            }
            Ok(Object::Reference(id)) => doc.get_dictionary(*id).ok(),
            _ => {
                warnings.push(PdfWarnMsg::warning(
                    page_num,
                    0,
                    format!("Type0 font {} missing DescendantFonts", font_id.0),
                ));
                None
            }
        }
    }

    /// Get the font descriptor dictionary
    fn get_font_descriptor<'a>(
        doc: &'a Document,
        font_dict: &'a Dictionary,
        font_id: &FontId,
        warnings: &mut Vec<PdfWarnMsg>,
        page_num: usize,
    ) -> Option<&'a Dictionary> {
        match font_dict.get(b"FontDescriptor") {
            Ok(Object::Dictionary(fd)) => Some(fd),
            Ok(Object::Reference(r)) => match doc.get_dictionary(*r) {
                Ok(fd) => Some(fd),
                Err(_) => {
                    warnings.push(PdfWarnMsg::warning(
                        page_num,
                        0,
                        format!("Cannot resolve FontDescriptor for {}", font_id.0),
                    ));
                    None
                }
            },
            _ => {
                warnings.push(PdfWarnMsg::warning(
                    page_num,
                    0,
                    format!("Font {} missing FontDescriptor", font_id.0),
                ));
                None
            }
        }
    }

    /// Process font data (extract, decompress, parse) and handle ToUnicode CMap
    fn process_font_data(
        doc: &Document,
        _font_dict: &Dictionary,
        font_descriptor: &Dictionary,
        font_id: &FontId,
        warnings: &mut Vec<PdfWarnMsg>,
        page_num: usize,
    ) -> Option<ParsedFont> {
        for font_file_key in &[b"FontFile", b"FontFile2" as &[u8], b"FontFile3"] {
            if let Ok(font_file_ref) = font_descriptor.get(font_file_key) {
                let font_data = get_font_data(doc, font_file_ref, font_id, warnings, page_num)?;

                warnings.push(PdfWarnMsg::info(
                    page_num,
                    0,
                    format!(
                        "Found font data for {} ({} bytes)",
                        font_id.0,
                        font_data.len()
                    ),
                ));

                let mut font_warnings = Vec::new();
                if let Some(parsed_font) = ParsedFont::from_bytes(&font_data, 0, &mut font_warnings)
                {
                    for fw in font_warnings {
                        warnings.push(PdfWarnMsg::warning(page_num, 0, format!("{fw:?}")));
                    }
                    return Some(parsed_font);
                } else {
                    warnings.push(PdfWarnMsg::error(
                        page_num,
                        0,
                        format!("Failed to parse font data for {}", font_id.0),
                    ));
                }
            }
        }
        None
    }

    /// Get and decompress font data from a font file reference
    fn get_font_data(
        doc: &Document,
        font_file_ref: &Object,
        font_id: &FontId,
        warnings: &mut Vec<PdfWarnMsg>,
        page_num: usize,
    ) -> Option<Vec<u8>> {
        let font_stream = match font_file_ref {
            Object::Stream(s) => s,
            Object::Reference(r) => match doc.get_object(*r) {
                Ok(Object::Stream(s)) => s,
                _ => return None,
            },
            _ => return None,
        };

        decode_stream(
            font_stream,
            MAX_STREAM_BYTES,
            &format!("Font {} font file", font_id.0),
            page_num,
            warnings,
        )
    }

    /// Process a Type1 font
    #[cfg(feature = "text_layout")]
    fn process_type1_font(
        doc: &Document,
        font_dict: &Dictionary,
        font_id: &FontId,
        warnings: &mut Vec<PdfWarnMsg>,
        page_num: usize,
    ) -> Option<ParsedFont> {
        let font_descriptor = get_font_descriptor(doc, font_dict, font_id, warnings, page_num)?;

        process_font_data(doc, font_dict, font_descriptor, font_id, warnings, page_num)
    }

    /// Process a standard font (Type1, TrueType, etc.)
    fn process_standard_font(
        _doc: &Document,
        font_dict: &Dictionary,
        font_id: &FontId,
        warnings: &mut Vec<PdfWarnMsg>,
        page_num: usize,
    ) -> Option<ParsedOrBuiltinFont> {
        let basefont = match font_dict.get(b"BaseFont") {
            Ok(Object::Name(basefont_bytes)) => String::from_utf8_lossy(basefont_bytes).to_string(),
            _ => return None,
        };

        match BuiltinFont::from_id(&basefont) {
            Some(builtin) => Some(ParsedOrBuiltinFont::B(builtin)),
            None => {
                #[cfg(feature = "text_layout")]
                {
                    match process_type1_font(_doc, font_dict, font_id, warnings, page_num) {
                        Some(parsed_font) => {
                            Some(ParsedOrBuiltinFont::P(ParsedExternalFont {
                                font: parsed_font,
                                to_unicode: None,
                                is_cid: false,
                                // Simple font: one-byte codes, no CID indirection.
                                cid_to_gid: None,
                                encoding_cmap: None,
                            }))
                        }
                        None => {
                            let substitute = builtin_substitute(&basefont);
                            warnings.push(PdfWarnMsg::warning(
                                page_num,
                                0,
                                format!(
                                    "Font {} ({}) has no embedded font program; substituting \
                                     builtin {:?}",
                                    font_id.0, basefont, substitute
                                ),
                            ));
                            Some(ParsedOrBuiltinFont::B(substitute))
                        }
                    }
                }
                #[cfg(not(feature = "text_layout"))]
                {
                    let substitute = builtin_substitute(&basefont);
                    warnings.push(PdfWarnMsg::warning(
                        page_num,
                        0,
                        format!(
                            "Font {} ({}) has no embedded font program; substituting builtin \
                             {:?}",
                            font_id.0, basefont, substitute
                        ),
                    ));
                    Some(ParsedOrBuiltinFont::B(substitute))
                }
            }
        }
    }

    /// Picks the closest standard-14 font for a simple font that is not embedded (`Arial`,
    /// `TimesNewRomanPSMT`, ...), so that its text stays readable and can be written again.
    fn builtin_substitute(basefont: &str) -> BuiltinFont {
        use crate::BuiltinFont::*;
        // Strip the "ABCDEF+" subset prefix if present.
        let name = basefont
            .rsplit('+')
            .next()
            .unwrap_or(basefont)
            .to_ascii_lowercase();
        let bold = name.contains("bold");
        let italic = name.contains("italic") || name.contains("oblique");
        if name.contains("times")
            || name.contains("serif")
            || name.contains("georgia")
            || name.contains("garamond")
            || name.contains("book")
        {
            match (bold, italic) {
                (true, true) => TimesBoldItalic,
                (true, false) => TimesBold,
                (false, true) => TimesItalic,
                (false, false) => TimesRoman,
            }
        } else if name.contains("courier") || name.contains("mono") || name.contains("consol") {
            match (bold, italic) {
                (true, true) => CourierBoldOblique,
                (true, false) => CourierBold,
                (false, true) => CourierOblique,
                (false, false) => Courier,
            }
        } else if name.contains("symbol") {
            Symbol
        } else if name.contains("zapf") || name.contains("dingbat") {
            ZapfDingbats
        } else {
            match (bold, italic) {
                (true, true) => HelveticaBoldOblique,
                (true, false) => HelveticaBold,
                (false, true) => HelveticaOblique,
                (false, false) => Helvetica,
            }
        }
    }
}

/// Decodes a PDF text string: UTF-16BE when it starts with the byte order mark `FE FF`,
/// otherwise UTF-8 (lossy).
fn decode_text_from_utf16be(bytes: &[u8]) -> String {
    let Some(body) = bytes.strip_prefix(&[0xFE, 0xFF]) else {
        // not UTF-16BE encoded, use UTF-8
        return String::from_utf8_lossy(bytes).into_owned();
    };

    let (chunks, remainder) = body.as_chunks::<2>();
    let string = char::decode_utf16(chunks.iter().copied().map(u16::from_be_bytes))
        .map(|r| r.unwrap_or(char::REPLACEMENT_CHARACTER))
        .collect();
    if remainder.is_empty() {
        string
    } else {
        string + "\u{FFFD}"
    }
}