oxidize-pdf 2.5.0

A pure Rust PDF generation and manipulation library with zero external dependencies
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
//! Optimized PDF Reader with LRU caching
//!
//! This module provides an optimized version of PdfReader that uses
//! an LRU cache instead of unlimited HashMap caching to control memory usage.

use super::header::PdfHeader;
use super::object_stream::ObjectStream;
use super::objects::{PdfDictionary, PdfObject};
use super::stack_safe::StackSafeContext;
use super::trailer::PdfTrailer;
use super::xref::XRefTable;
use super::{ParseError, ParseOptions, ParseResult};
use crate::memory::{LruCache, MemoryOptions, MemoryStats};
use crate::objects::ObjectId;
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufReader, Read, Seek, SeekFrom};
use std::path::Path;
use std::sync::Arc;

/// Optimized PDF reader with LRU caching
pub struct OptimizedPdfReader<R: Read + Seek> {
    reader: BufReader<R>,
    header: PdfHeader,
    xref: XRefTable,
    trailer: PdfTrailer,
    /// LRU cache for loaded objects
    object_cache: LruCache<ObjectId, Arc<PdfObject>>,
    /// Cache of object streams
    object_stream_cache: HashMap<u32, ObjectStream>,
    /// Page tree navigator
    #[allow(dead_code)]
    page_tree: Option<super::page_tree::PageTree>,
    /// Stack-safe parsing context
    #[allow(dead_code)]
    parse_context: StackSafeContext,
    /// Parsing options
    options: super::ParseOptions,
    /// Memory options
    #[allow(dead_code)]
    memory_options: MemoryOptions,
    /// Memory statistics
    memory_stats: MemoryStats,
}

impl<R: Read + Seek> OptimizedPdfReader<R> {
    /// Get parsing options
    pub fn options(&self) -> &super::ParseOptions {
        &self.options
    }

    /// Get memory statistics
    pub fn memory_stats(&self) -> &MemoryStats {
        &self.memory_stats
    }

    /// Clear the object cache
    pub fn clear_cache(&mut self) {
        self.object_cache.clear();
        self.object_stream_cache.clear();
        self.memory_stats.cached_objects = 0;
    }
}

impl OptimizedPdfReader<File> {
    /// Open a PDF file from a path with memory optimization
    pub fn open<P: AsRef<Path>>(path: P) -> ParseResult<Self> {
        let file = File::open(path)?;
        let options = super::ParseOptions::lenient();
        let memory_options = MemoryOptions::default();
        Self::new_with_options(file, options, memory_options)
    }

    /// Open a PDF file with custom memory options
    pub fn open_with_memory<P: AsRef<Path>>(
        path: P,
        memory_options: MemoryOptions,
    ) -> ParseResult<Self> {
        let file = File::open(path)?;
        let options = super::ParseOptions::lenient();
        Self::new_with_options(file, options, memory_options)
    }

    /// Open a PDF file with strict parsing
    pub fn open_strict<P: AsRef<Path>>(path: P) -> ParseResult<Self> {
        let file = File::open(path)?;
        let options = super::ParseOptions::strict();
        let memory_options = MemoryOptions::default();
        Self::new_with_options(file, options, memory_options)
    }
}

impl<R: Read + Seek> OptimizedPdfReader<R> {
    /// Create a new PDF reader from a reader
    pub fn new(reader: R) -> ParseResult<Self> {
        Self::new_with_options(
            reader,
            super::ParseOptions::default(),
            MemoryOptions::default(),
        )
    }

    /// Create a new PDF reader with custom parsing and memory options
    pub fn new_with_options(
        reader: R,
        options: super::ParseOptions,
        memory_options: MemoryOptions,
    ) -> ParseResult<Self> {
        let mut buf_reader = BufReader::new(reader);

        // Check if file is empty
        let start_pos = buf_reader.stream_position()?;
        buf_reader.seek(SeekFrom::End(0))?;
        let file_size = buf_reader.stream_position()?;
        buf_reader.seek(SeekFrom::Start(start_pos))?;

        if file_size == 0 {
            return Err(ParseError::EmptyFile);
        }

        // Parse header
        let header = PdfHeader::parse(&mut buf_reader)?;

        // Parse xref table
        let xref = XRefTable::parse_with_options(&mut buf_reader, &options)?;

        // Get trailer
        let trailer_dict = xref.trailer().ok_or(ParseError::InvalidTrailer)?.clone();

        let xref_offset = xref.xref_offset();
        let trailer = PdfTrailer::from_dict(trailer_dict, xref_offset)?;

        // Validate trailer
        trailer.validate()?;

        // Create LRU cache with configured size
        let cache_size = memory_options.cache_size.max(1);
        let object_cache = LruCache::new(cache_size);

        Ok(Self {
            reader: buf_reader,
            header,
            xref,
            trailer,
            object_cache,
            object_stream_cache: HashMap::new(),
            page_tree: None,
            parse_context: StackSafeContext::new(),
            options,
            memory_options,
            memory_stats: MemoryStats::default(),
        })
    }

    /// Get the PDF version
    pub fn version(&self) -> &super::header::PdfVersion {
        &self.header.version
    }

    /// Get the document catalog
    pub fn catalog(&mut self) -> ParseResult<&PdfDictionary> {
        // Try to get root from trailer
        let (obj_num, gen_num) = match self.trailer.root() {
            Ok(root) => root,
            Err(_) => {
                // If Root is missing, try fallback methods
                #[cfg(debug_assertions)]
                tracing::debug!("Warning: Trailer missing Root entry, attempting recovery");

                // First try the fallback method
                if let Some(root) = self.trailer.find_root_fallback() {
                    root
                } else {
                    // Last resort: scan for Catalog object
                    if let Ok(catalog_ref) = self.find_catalog_object() {
                        catalog_ref
                    } else {
                        return Err(ParseError::MissingKey("Root".to_string()));
                    }
                }
            }
        };

        let catalog = self.get_object(obj_num, gen_num)?;

        catalog.as_dict().ok_or_else(|| ParseError::SyntaxError {
            position: 0,
            message: "Catalog is not a dictionary".to_string(),
        })
    }

    /// Get the document info dictionary
    pub fn info(&mut self) -> ParseResult<Option<&PdfDictionary>> {
        match self.trailer.info() {
            Some((obj_num, gen_num)) => {
                let info = self.get_object(obj_num, gen_num)?;
                Ok(info.as_dict())
            }
            None => Ok(None),
        }
    }

    /// Get an object by reference
    pub fn get_object(&mut self, obj_num: u32, gen_num: u16) -> ParseResult<&PdfObject> {
        let object_id = ObjectId::new(obj_num, gen_num);

        // Check LRU cache first
        if let Some(cached_obj) = self.object_cache.get(&object_id) {
            self.memory_stats.cache_hits += 1;
            // Convert Arc<PdfObject> to &PdfObject
            // This is safe because we maintain the Arc in the cache
            let ptr = Arc::as_ptr(cached_obj);
            return Ok(unsafe { &*ptr });
        }

        self.memory_stats.cache_misses += 1;

        // Load object from disk
        let obj = self.load_object_from_disk(obj_num, gen_num)?;

        // Store in LRU cache
        let arc_obj = Arc::new(obj);
        self.object_cache.put(object_id, arc_obj);
        self.memory_stats.cached_objects = self.object_cache.len();

        // Return reference to cached object
        // The Arc is owned by the cache, so we can safely return a reference
        // We need to get it from the cache to ensure lifetime
        self.object_cache
            .get(&object_id)
            .map(|arc| unsafe { &*Arc::as_ptr(arc) })
            .ok_or(ParseError::SyntaxError {
                position: 0,
                message: "Object not in cache after insertion".to_string(),
            })
    }

    /// Internal method to load an object from disk
    fn load_object_from_disk(&mut self, obj_num: u32, gen_num: u16) -> ParseResult<PdfObject> {
        // Check if this is a compressed object
        if let Some(ext_entry) = self.xref.get_extended_entry(obj_num) {
            if let Some((stream_obj_num, index_in_stream)) = ext_entry.compressed_info {
                // This is a compressed object - need to extract from object stream
                return self.get_compressed_object_direct(
                    obj_num,
                    gen_num,
                    stream_obj_num,
                    index_in_stream,
                );
            }
        }

        // Get xref entry
        let entry = self
            .xref
            .get_entry(obj_num)
            .ok_or(ParseError::InvalidReference(obj_num, gen_num))?;

        if !entry.in_use {
            // Free object
            return Ok(PdfObject::Null);
        }

        if entry.generation != gen_num {
            return Err(ParseError::InvalidReference(obj_num, gen_num));
        }

        // Seek to object position
        self.reader.seek(std::io::SeekFrom::Start(entry.offset))?;

        // Parse object header (obj_num gen_num obj)
        let mut lexer =
            super::lexer::Lexer::new_with_options(&mut self.reader, self.options.clone());

        // Read object number with recovery
        let token = lexer.next_token()?;
        let read_obj_num = match token {
            super::lexer::Token::Integer(n) => n as u32,
            _ => {
                // Try fallback recovery
                if self.options.lenient_syntax {
                    if self.options.collect_warnings {
                        tracing::debug!(
                            "Warning: Using expected object number {obj_num} instead of parsed token"
                        );
                    }
                    obj_num
                } else {
                    return Err(ParseError::SyntaxError {
                        position: entry.offset as usize,
                        message: "Expected object number".to_string(),
                    });
                }
            }
        };

        if read_obj_num != obj_num && !self.options.lenient_syntax {
            return Err(ParseError::SyntaxError {
                position: entry.offset as usize,
                message: format!(
                    "Object number mismatch: expected {obj_num}, found {read_obj_num}"
                ),
            });
        }

        // Read generation number
        let token = lexer.next_token()?;
        let read_gen_num = match token {
            super::lexer::Token::Integer(n) => n as u16,
            _ => {
                if self.options.lenient_syntax {
                    if self.options.collect_warnings {
                        tracing::debug!(
                            "Warning: Using generation 0 instead of parsed token for object {obj_num}"
                        );
                    }
                    0
                } else {
                    return Err(ParseError::SyntaxError {
                        position: entry.offset as usize,
                        message: "Expected generation number".to_string(),
                    });
                }
            }
        };

        if read_gen_num != gen_num && !self.options.lenient_syntax {
            return Err(ParseError::SyntaxError {
                position: entry.offset as usize,
                message: format!(
                    "Generation number mismatch: expected {gen_num}, found {read_gen_num}"
                ),
            });
        }

        // Read 'obj' keyword
        let token = lexer.next_token()?;
        match token {
            super::lexer::Token::Obj => {}
            _ => {
                if self.options.lenient_syntax {
                    if self.options.collect_warnings {
                        tracing::debug!("Warning: Missing 'obj' keyword for object {obj_num}");
                    }
                } else {
                    return Err(ParseError::SyntaxError {
                        position: entry.offset as usize,
                        message: "Expected 'obj' keyword".to_string(),
                    });
                }
            }
        }

        // Parse the object
        let object = PdfObject::parse(&mut lexer)?;

        // Skip 'endobj' if present
        if let Ok(token) = lexer.peek_token() {
            if let super::lexer::Token::EndObj = token {
                let _ = lexer.next_token();
            } else if !self.options.lenient_syntax && self.options.collect_warnings {
                tracing::debug!("Warning: Missing 'endobj' for object {obj_num}");
            }
        }

        Ok(object)
    }

    /// Get a compressed object directly (returns owned object)
    fn get_compressed_object_direct(
        &mut self,
        obj_num: u32,
        _gen_num: u16,
        stream_obj_num: u32,
        _index_in_stream: u32,
    ) -> ParseResult<PdfObject> {
        // First get the object stream
        if !self.object_stream_cache.contains_key(&stream_obj_num) {
            // Load the stream object
            let stream_obj = self.load_object_from_disk(stream_obj_num, 0)?;

            if let PdfObject::Stream(stream) = stream_obj {
                let obj_stream = ObjectStream::parse(stream, &ParseOptions::default())?;
                self.object_stream_cache.insert(stream_obj_num, obj_stream);
            } else {
                return Err(ParseError::SyntaxError {
                    position: 0,
                    message: "Object stream is not a stream object".to_string(),
                });
            }
        }

        // Get object from stream
        let obj_stream = self
            .object_stream_cache
            .get(&stream_obj_num)
            .ok_or_else(|| ParseError::SyntaxError {
                position: 0,
                message: "Object stream not found in cache".to_string(),
            })?;

        obj_stream
            .get_object(obj_num)
            .cloned()
            .ok_or(ParseError::InvalidReference(obj_num, 0))
    }

    /// Find catalog object by scanning (fallback method)
    fn find_catalog_object(&mut self) -> ParseResult<(u32, u16)> {
        // This is a simplified implementation
        // In a real scenario, we would scan through objects to find the catalog
        for obj_num in 1..100 {
            if let Ok(PdfObject::Dictionary(dict)) = self.get_object(obj_num, 0) {
                if let Some(PdfObject::Name(type_name)) = dict.get("Type") {
                    if type_name.0.as_bytes() == b"Catalog" {
                        return Ok((obj_num, 0));
                    }
                }
            }
        }
        Err(ParseError::MissingKey("Catalog".to_string()))
    }

    /// Get a reference to the inner reader
    pub fn reader(&mut self) -> &mut BufReader<R> {
        &mut self.reader
    }
}

/// Helper function to get memory usage info for a PdfObject
pub fn estimate_object_size(obj: &PdfObject) -> usize {
    match obj {
        PdfObject::Null => 8,
        PdfObject::Boolean(_) => 16,
        PdfObject::Integer(_) => 16,
        PdfObject::Real(_) => 16,
        PdfObject::String(s) => 24 + s.as_bytes().len(),
        PdfObject::Name(n) => 24 + n.0.len(),
        PdfObject::Array(arr) => {
            24 + arr.len() * 8 + arr.0.iter().map(estimate_object_size).sum::<usize>()
        }
        PdfObject::Dictionary(dict) => {
            24 + dict.0.len() * 16
                + dict
                    .0
                    .iter()
                    .map(|(k, v)| k.0.len() + estimate_object_size(v))
                    .sum::<usize>()
        }
        PdfObject::Stream(s) => {
            48 + s.data.len() + estimate_object_size(&PdfObject::Dictionary(s.dict.clone()))
        }
        PdfObject::Reference(_, _) => 16,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parser::objects::{PdfArray, PdfDictionary, PdfName, PdfStream, PdfString};
    use std::io::Cursor;

    fn create_minimal_pdf() -> Vec<u8> {
        // Offsets calculated from actual file:
        // Header: 0-9
        // Object 1: 9-58   (offset: 0000000009)
        // Object 2: 58-115 (offset: 0000000058)
        // Object 3: 115-186 (offset: 0000000115)
        // XRef table: 186 (startxref: 186)
        b"%PDF-1.4\n\
1 0 obj\n\
<< /Type /Catalog /Pages 2 0 R >>\n\
endobj\n\
2 0 obj\n\
<< /Type /Pages /Kids [3 0 R] /Count 1 >>\n\
endobj\n\
3 0 obj\n\
<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>\n\
endobj\n\
xref\n\
0 4\n\
0000000000 65535 f \n\
0000000009 00000 n \n\
0000000058 00000 n \n\
0000000115 00000 n \n\
trailer\n\
<< /Size 4 /Root 1 0 R >>\n\
startxref\n\
186\n\
%%EOF\n"
            .to_vec()
    }

    fn create_empty_pdf() -> Vec<u8> {
        Vec::new()
    }

    fn create_invalid_pdf() -> Vec<u8> {
        b"Not a PDF file".to_vec()
    }

    #[test]
    fn test_memory_options_integration() {
        let options = MemoryOptions::default().with_cache_size(100);
        assert_eq!(options.cache_size, 100);

        let options = MemoryOptions::default().with_cache_size(0);
        assert_eq!(options.cache_size, 0);
    }

    #[test]
    fn test_object_size_estimation_basic_types() {
        // Null
        let obj = PdfObject::Null;
        assert_eq!(estimate_object_size(&obj), 8);

        // Boolean
        let obj = PdfObject::Boolean(true);
        assert_eq!(estimate_object_size(&obj), 16);

        let obj = PdfObject::Boolean(false);
        assert_eq!(estimate_object_size(&obj), 16);

        // Integer
        let obj = PdfObject::Integer(42);
        assert_eq!(estimate_object_size(&obj), 16);

        let obj = PdfObject::Integer(-1000);
        assert_eq!(estimate_object_size(&obj), 16);

        // Real
        let obj = PdfObject::Real(3.14159);
        assert_eq!(estimate_object_size(&obj), 16);

        // Reference
        let obj = PdfObject::Reference(5, 0);
        assert_eq!(estimate_object_size(&obj), 16);
    }

    #[test]
    fn test_object_size_estimation_string_types() {
        // Empty string
        let obj = PdfObject::String(PdfString::new(b"".to_vec()));
        assert_eq!(estimate_object_size(&obj), 24);

        // Short string
        let obj = PdfObject::String(PdfString::new(b"Hello".to_vec()));
        assert_eq!(estimate_object_size(&obj), 24 + 5);

        // Long string
        let long_text = "A".repeat(1000);
        let obj = PdfObject::String(PdfString::new(long_text.as_bytes().to_vec()));
        assert_eq!(estimate_object_size(&obj), 24 + 1000);

        // Name objects
        let obj = PdfObject::Name(PdfName::new("Type".to_string()));
        assert_eq!(estimate_object_size(&obj), 24 + 4);

        let obj = PdfObject::Name(PdfName::new("".to_string()));
        assert_eq!(estimate_object_size(&obj), 24);
    }

    #[test]
    fn test_object_size_estimation_array() {
        // Empty array
        let obj = PdfObject::Array(PdfArray(vec![]));
        assert_eq!(estimate_object_size(&obj), 24);

        // Simple array
        let obj = PdfObject::Array(PdfArray(vec![
            PdfObject::Integer(1),
            PdfObject::Integer(2),
            PdfObject::Integer(3),
        ]));
        assert_eq!(estimate_object_size(&obj), 24 + 3 * 8 + 3 * 16);

        // Nested array
        let inner_array = PdfObject::Array(PdfArray(vec![
            PdfObject::Integer(10),
            PdfObject::Integer(20),
        ]));
        let obj = PdfObject::Array(PdfArray(vec![PdfObject::Integer(1), inner_array]));
        let expected = 24 + 2 * 8 + 16 + (24 + 2 * 8 + 2 * 16);
        assert_eq!(estimate_object_size(&obj), expected);
    }

    #[test]
    fn test_object_size_estimation_dictionary() {
        // Empty dictionary
        let obj = PdfObject::Dictionary(PdfDictionary::new());
        assert_eq!(estimate_object_size(&obj), 24);

        // Simple dictionary
        let mut dict = PdfDictionary::new();
        dict.insert(
            "Type".to_string(),
            PdfObject::Name(PdfName::new("Catalog".to_string())),
        );
        dict.insert("Count".to_string(), PdfObject::Integer(5));

        let obj = PdfObject::Dictionary(dict);
        let expected = 24 + 2 * 16 + (4 + 24 + 7) + (5 + 16);
        assert_eq!(estimate_object_size(&obj), expected);
    }

    #[test]
    fn test_object_size_estimation_stream() {
        let mut dict = PdfDictionary::new();
        dict.insert("Length".to_string(), PdfObject::Integer(10));

        let stream = PdfObject::Stream(PdfStream {
            dict: dict.clone(),
            data: b"Hello Test".to_vec(),
        });

        let dict_size = estimate_object_size(&PdfObject::Dictionary(dict));
        let expected = 48 + 10 + dict_size;
        assert_eq!(estimate_object_size(&stream), expected);
    }

    #[test]
    fn test_object_size_estimation_complex_structure() {
        // Complex nested structure
        let mut inner_dict = PdfDictionary::new();
        inner_dict.insert(
            "Font".to_string(),
            PdfObject::Name(PdfName::new("Helvetica".to_string())),
        );
        inner_dict.insert("Size".to_string(), PdfObject::Integer(12));

        let array = PdfObject::Array(PdfArray(vec![
            PdfObject::String(PdfString::new(b"Text content".to_vec())),
            PdfObject::Dictionary(inner_dict),
            PdfObject::Reference(10, 0),
        ]));

        let mut main_dict = PdfDictionary::new();
        main_dict.insert(
            "Type".to_string(),
            PdfObject::Name(PdfName::new("Page".to_string())),
        );
        main_dict.insert("Contents".to_string(), array);

        let obj = PdfObject::Dictionary(main_dict);

        // The size should be > 0 and reasonable
        let size = estimate_object_size(&obj);
        assert!(size > 100);
        assert!(size < 1000);
    }

    #[test]
    fn test_optimized_reader_empty_file() {
        let data = create_empty_pdf();
        let cursor = Cursor::new(data);

        let result = OptimizedPdfReader::new(cursor);
        assert!(result.is_err());
        if let Err(ParseError::EmptyFile) = result {
            // Expected error
        } else {
            panic!("Expected EmptyFile error");
        }
    }

    #[test]
    fn test_optimized_reader_invalid_file() {
        let data = create_invalid_pdf();
        let cursor = Cursor::new(data);

        let result = OptimizedPdfReader::new(cursor);
        assert!(result.is_err());
        // Should fail during header parsing
    }

    #[test]
    fn test_optimized_reader_creation_with_options() {
        let data = create_minimal_pdf();
        let cursor = Cursor::new(data);

        let parse_options = ParseOptions {
            lenient_syntax: true,
            collect_warnings: false,
            ..Default::default()
        };

        let memory_options = MemoryOptions::default().with_cache_size(50);

        let result = OptimizedPdfReader::new_with_options(cursor, parse_options, memory_options);
        if result.is_err() {
            // Skip test if PDF parsing fails due to incomplete implementation
            return;
        }

        let reader = result.unwrap();
        assert!(reader.options().lenient_syntax);
        assert!(!reader.options().collect_warnings);
    }

    #[test]
    fn test_optimized_reader_version_access() {
        let data = create_minimal_pdf();
        let cursor = Cursor::new(data);

        let result = OptimizedPdfReader::new(cursor);
        if result.is_err() {
            // Skip test if PDF parsing fails
            return;
        }

        let reader = result.unwrap();
        let version = reader.version();

        // Should have parsed version from %PDF-1.4
        assert_eq!(version.major, 1);
        assert_eq!(version.minor, 4);
    }

    #[test]
    fn test_memory_options_validation() {
        let data = create_minimal_pdf();
        let cursor = Cursor::new(data);

        // Test that cache size of 0 gets converted to 1
        let memory_options = MemoryOptions::default().with_cache_size(0);
        let parse_options = ParseOptions::default();

        let result = OptimizedPdfReader::new_with_options(cursor, parse_options, memory_options);
        if result.is_err() {
            // The memory option validation should still work even if PDF parsing fails
            let memory_opts = MemoryOptions::default().with_cache_size(0);
            let cache_size = memory_opts.cache_size.max(1);
            assert_eq!(cache_size, 1);
        }
    }

    #[test]
    fn test_estimate_object_size_edge_cases() {
        // Very large array
        let large_array = PdfObject::Array(PdfArray((0..1000).map(PdfObject::Integer).collect()));
        let size = estimate_object_size(&large_array);
        assert!(size > 16000); // Should be substantial

        // Very large dictionary
        let mut large_dict = PdfDictionary::new();
        for i in 0..100 {
            large_dict.insert(
                format!("Key{i}"),
                PdfObject::String(PdfString::new(format!("Value{i}").as_bytes().to_vec())),
            );
        }
        let obj = PdfObject::Dictionary(large_dict);
        let size = estimate_object_size(&obj);
        assert!(size > 1000);
    }

    #[test]
    fn test_memory_options_default_values() {
        let options = MemoryOptions::default();

        // Verify reasonable defaults
        assert!(options.cache_size > 0);
        assert!(options.cache_size < 10000); // Should be reasonable
    }

    #[test]
    fn test_memory_options_builder_pattern() {
        let options = MemoryOptions::default().with_cache_size(500);

        assert_eq!(options.cache_size, 500);
    }

    #[test]
    fn test_object_size_estimation_consistency() {
        // Same objects should have same size
        let obj1 = PdfObject::String(PdfString::new(b"Test".to_vec()));
        let obj2 = PdfObject::String(PdfString::new(b"Test".to_vec()));

        assert_eq!(estimate_object_size(&obj1), estimate_object_size(&obj2));

        // Different content should have different sizes
        let obj3 = PdfObject::String(PdfString::new(b"Different".to_vec()));
        assert_ne!(estimate_object_size(&obj1), estimate_object_size(&obj3));
    }

    #[test]
    fn test_object_size_estimation_zero_values() {
        // Integer zero
        let obj = PdfObject::Integer(0);
        assert_eq!(estimate_object_size(&obj), 16);

        // Real zero
        let obj = PdfObject::Real(0.0);
        assert_eq!(estimate_object_size(&obj), 16);

        // Reference zero
        let obj = PdfObject::Reference(0, 0);
        assert_eq!(estimate_object_size(&obj), 16);
    }

    #[test]
    fn test_object_size_estimation_negative_values() {
        let obj = PdfObject::Integer(-42);
        assert_eq!(estimate_object_size(&obj), 16);

        let obj = PdfObject::Real(-3.14159);
        assert_eq!(estimate_object_size(&obj), 16);
    }

    #[test]
    fn test_object_size_estimation_unicode_strings() {
        // Unicode string
        let unicode_text = "Hello 世界 🌍";
        let obj = PdfObject::String(PdfString::new(unicode_text.as_bytes().to_vec()));
        let expected_size = 24 + unicode_text.len();
        assert_eq!(estimate_object_size(&obj), expected_size);
    }

    #[test]
    fn test_object_size_estimation_mixed_array() {
        let obj = PdfObject::Array(PdfArray(vec![
            PdfObject::Null,
            PdfObject::Boolean(true),
            PdfObject::Integer(42),
            PdfObject::Real(3.14),
            PdfObject::String(PdfString::new(b"test".to_vec())),
            PdfObject::Name(PdfName::new("Name".to_string())),
            PdfObject::Reference(1, 0),
        ]));

        let expected = 24 + 7 * 8 + 8 + 16 + 16 + 16 + (24 + 4) + (24 + 4) + 16;
        assert_eq!(estimate_object_size(&obj), expected);
    }

    #[test]
    fn test_find_catalog_object_range() {
        // Test that find_catalog_object scans a reasonable range
        // This is mainly testing the logic bounds - it scans objects 1-99
        let data = create_minimal_pdf();
        let cursor = Cursor::new(data);

        // We can't easily test the actual scanning without a real PDF,
        // but we can verify the implementation exists and has reasonable bounds
        if let Ok(mut reader) = OptimizedPdfReader::new(cursor) {
            // The method exists and should scan objects 1-99
            // In a real test with proper PDF, this would find the catalog
            let _result = reader.find_catalog_object();
            // Result depends on the actual PDF content, so we don't assert specific outcomes
        }
    }

    #[test]
    fn test_memory_stats_tracking() {
        // Test that memory stats are properly initialized
        let data = create_minimal_pdf();
        let cursor = Cursor::new(data);

        if let Ok(reader) = OptimizedPdfReader::new(cursor) {
            // Memory stats should be initialized
            assert_eq!(reader.memory_stats.cache_hits, 0);
            assert_eq!(reader.memory_stats.cache_misses, 0);
            assert_eq!(reader.memory_stats.cached_objects, 0);
        }
    }

    // =============================================================================
    // RIGOROUS TESTS FOR OPTIMIZED READER
    // =============================================================================

    mod rigorous {
        use super::*;

        #[test]
        fn test_lru_cache_hit_tracking() {
            let data = create_minimal_pdf();
            let cursor = Cursor::new(data);

            let mut reader =
                OptimizedPdfReader::new(cursor).expect("Minimal PDF must parse successfully");

            // Initial state: 0 hits, 0 misses
            assert_eq!(
                reader.memory_stats().cache_hits,
                0,
                "Cache hits must start at 0"
            );
            assert_eq!(
                reader.memory_stats().cache_misses,
                0,
                "Cache misses must start at 0"
            );

            // First access: should be cache miss
            let _ = reader.get_object(1, 0);
            assert_eq!(
                reader.memory_stats().cache_misses,
                1,
                "First access must be cache miss"
            );
            assert_eq!(reader.memory_stats().cache_hits, 0, "No cache hits yet");

            // Second access: should be cache hit
            let _ = reader.get_object(1, 0);
            assert_eq!(
                reader.memory_stats().cache_hits,
                1,
                "Second access must be cache hit"
            );
            assert_eq!(
                reader.memory_stats().cache_misses,
                1,
                "Cache misses unchanged"
            );

            // Third access: another cache hit
            let _ = reader.get_object(1, 0);
            assert_eq!(
                reader.memory_stats().cache_hits,
                2,
                "Third access must increment cache hits"
            );
        }

        #[test]
        fn test_lru_cache_capacity_enforcement() {
            let data = create_minimal_pdf();
            let cursor = Cursor::new(data);

            // Create reader with small cache (size 2)
            let memory_options = MemoryOptions::default().with_cache_size(2);
            let parse_options = ParseOptions::default();

            let mut reader =
                OptimizedPdfReader::new_with_options(cursor, parse_options, memory_options)
                    .expect("Minimal PDF must parse successfully");

            // Load object 1 (cache size: 1)
            let _ = reader.get_object(1, 0);
            assert_eq!(
                reader.memory_stats().cached_objects,
                1,
                "Cache should have 1 object"
            );

            // Load object 2 (cache size: 2)
            let _ = reader.get_object(2, 0);
            assert_eq!(
                reader.memory_stats().cached_objects,
                2,
                "Cache should have 2 objects"
            );

            // Load object 3 (cache size: still 2, evicts LRU)
            let _ = reader.get_object(3, 0);
            assert_eq!(
                reader.memory_stats().cached_objects,
                2,
                "Cache must not exceed capacity of 2"
            );
        }

        #[test]
        fn test_cache_clear_resets_stats() {
            let data = create_minimal_pdf();
            let cursor = Cursor::new(data);

            let mut reader =
                OptimizedPdfReader::new(cursor).expect("Minimal PDF must parse successfully");

            // Load some objects
            let _ = reader.get_object(1, 0);
            let _ = reader.get_object(1, 0); // cache hit

            // Verify stats before clear
            assert!(reader.memory_stats().cache_hits > 0);
            assert!(reader.memory_stats().cached_objects > 0);

            // Clear cache
            reader.clear_cache();

            // Cached objects should be 0 after clear
            assert_eq!(
                reader.memory_stats().cached_objects,
                0,
                "Cache should be empty after clear"
            );

            // Stats for hits/misses remain (cumulative)
            // But next access will be miss
            let _ = reader.get_object(1, 0);
            assert!(
                reader.memory_stats().cache_misses >= 2,
                "Access after clear must be cache miss"
            );
        }

        #[test]
        fn test_empty_file_error_handling() {
            let data = create_empty_pdf();
            let cursor = Cursor::new(data);

            let result = OptimizedPdfReader::new(cursor);

            assert!(result.is_err(), "Empty file must return error");
            match result {
                Err(ParseError::EmptyFile) => {
                    // Expected specific error type
                }
                Err(other) => panic!("Expected EmptyFile error, got: {:?}", other),
                Ok(_) => panic!("Should not succeed with empty file"),
            }
        }

        #[test]
        fn test_invalid_header_error_handling() {
            let data = create_invalid_pdf();
            let cursor = Cursor::new(data);

            let result = OptimizedPdfReader::new(cursor);

            assert!(result.is_err(), "Invalid PDF must return error");
            // Error should occur during header parsing
        }

        #[test]
        fn test_version_parsing_exact_values() {
            let data = create_minimal_pdf();
            let cursor = Cursor::new(data);

            let reader =
                OptimizedPdfReader::new(cursor).expect("Minimal PDF must parse successfully");

            let version = reader.version();

            // Minimal PDF is version 1.4
            assert_eq!(version.major, 1, "PDF major version must be 1");
            assert_eq!(version.minor, 4, "PDF minor version must be 4");
        }

        #[test]
        fn test_options_accessibility() {
            let data = create_minimal_pdf();
            let cursor = Cursor::new(data);

            let parse_options = ParseOptions {
                lenient_syntax: true,
                collect_warnings: false,
                ..Default::default()
            };
            let memory_options = MemoryOptions::default().with_cache_size(100);

            let reader =
                OptimizedPdfReader::new_with_options(cursor, parse_options, memory_options)
                    .expect("Minimal PDF must parse successfully");

            let opts = reader.options();

            assert_eq!(
                opts.lenient_syntax, true,
                "Options must match provided values"
            );
            assert_eq!(
                opts.collect_warnings, false,
                "Options must match provided values"
            );
        }

        #[test]
        fn test_catalog_access_requires_valid_trailer() {
            let data = create_minimal_pdf();
            let cursor = Cursor::new(data);

            let mut reader =
                OptimizedPdfReader::new(cursor).expect("Minimal PDF must parse successfully");

            // Catalog should be accessible
            let catalog_result = reader.catalog();

            if catalog_result.is_ok() {
                let catalog = catalog_result.unwrap();

                // Catalog must be a dictionary with Type = Catalog
                assert_eq!(
                    catalog.get("Type"),
                    Some(&PdfObject::Name(PdfName("Catalog".to_string()))),
                    "Catalog must have /Type /Catalog"
                );
            } else {
                // If catalog fails, should be specific error
                assert!(matches!(
                    catalog_result.unwrap_err(),
                    ParseError::MissingKey(_) | ParseError::SyntaxError { .. }
                ));
            }
        }

        #[test]
        fn test_info_none_when_absent() {
            let data = create_minimal_pdf();
            let cursor = Cursor::new(data);

            let mut reader =
                OptimizedPdfReader::new(cursor).expect("Minimal PDF must parse successfully");

            let info_result = reader.info();

            if info_result.is_ok() {
                let info = info_result.unwrap();
                // Minimal PDF has no Info dictionary in trailer
                assert!(
                    info.is_none(),
                    "Info should be None when not present in trailer"
                );
            }
        }

        #[test]
        fn test_get_object_wrong_generation() {
            let data = create_minimal_pdf();
            let cursor = Cursor::new(data);

            let mut reader =
                OptimizedPdfReader::new(cursor).expect("Minimal PDF must parse successfully");

            // Object 1 0 exists, but try accessing with wrong generation
            let result = reader.get_object(1, 5); // Wrong generation number

            // Should either return error or Null for free object
            if result.is_err() {
                assert!(matches!(
                    result.unwrap_err(),
                    ParseError::InvalidReference(_, _)
                ));
            }
        }

        #[test]
        fn test_get_nonexistent_object() {
            let data = create_minimal_pdf();
            let cursor = Cursor::new(data);

            let mut reader =
                OptimizedPdfReader::new(cursor).expect("Minimal PDF must parse successfully");

            // Try accessing object that doesn't exist
            let result = reader.get_object(9999, 0);

            assert!(
                result.is_err(),
                "Accessing nonexistent object must return error"
            );
            assert!(matches!(
                result.unwrap_err(),
                ParseError::InvalidReference(_, _)
            ));
        }

        #[test]
        fn test_memory_options_min_cache_size() {
            let data = create_minimal_pdf();
            let cursor = Cursor::new(data);

            // Even with cache_size = 0, implementation enforces minimum of 1
            let memory_options = MemoryOptions::default().with_cache_size(0);
            let parse_options = ParseOptions::default();

            let mut reader =
                OptimizedPdfReader::new_with_options(cursor, parse_options, memory_options)
                    .expect("Minimal PDF must parse successfully");

            // Should be able to cache at least 1 object
            let _ = reader.get_object(1, 0);
            assert_eq!(
                reader.memory_stats().cached_objects,
                1,
                "Must cache at least 1 object even with cache_size=0"
            );
        }

        #[test]
        fn test_estimate_object_size_exact_values() {
            // Test exact size calculations for primitive types

            // Null: 8 bytes
            assert_eq!(estimate_object_size(&PdfObject::Null), 8);

            // Boolean: 16 bytes
            assert_eq!(estimate_object_size(&PdfObject::Boolean(true)), 16);
            assert_eq!(estimate_object_size(&PdfObject::Boolean(false)), 16);

            // Integer: 16 bytes
            assert_eq!(estimate_object_size(&PdfObject::Integer(0)), 16);
            assert_eq!(estimate_object_size(&PdfObject::Integer(42)), 16);
            assert_eq!(estimate_object_size(&PdfObject::Integer(-1000)), 16);

            // Real: 16 bytes
            assert_eq!(estimate_object_size(&PdfObject::Real(0.0)), 16);
            assert_eq!(estimate_object_size(&PdfObject::Real(3.14159)), 16);

            // Reference: 16 bytes
            assert_eq!(estimate_object_size(&PdfObject::Reference(1, 0)), 16);
            assert_eq!(estimate_object_size(&PdfObject::Reference(999, 5)), 16);
        }

        #[test]
        fn test_estimate_string_size_formula() {
            // String size = 24 + byte_length

            // Empty string
            let empty = PdfObject::String(PdfString::new(vec![]));
            assert_eq!(estimate_object_size(&empty), 24);

            // 10 bytes
            let ten_bytes = PdfObject::String(PdfString::new(b"0123456789".to_vec()));
            assert_eq!(estimate_object_size(&ten_bytes), 24 + 10);

            // 100 bytes
            let hundred_bytes = PdfObject::String(PdfString::new(vec![b'X'; 100]));
            assert_eq!(estimate_object_size(&hundred_bytes), 24 + 100);
        }

        #[test]
        fn test_estimate_array_size_formula() {
            // Array size = 24 + (len * 8) + sum(element sizes)

            // Empty array: 24
            let empty = PdfObject::Array(PdfArray(vec![]));
            assert_eq!(estimate_object_size(&empty), 24);

            // 3 integers: 24 + (3*8) + (3*16) = 24 + 24 + 48 = 96
            let three_ints = PdfObject::Array(PdfArray(vec![
                PdfObject::Integer(1),
                PdfObject::Integer(2),
                PdfObject::Integer(3),
            ]));
            assert_eq!(estimate_object_size(&three_ints), 24 + 24 + 48);
        }

        #[test]
        fn test_estimate_dictionary_size_formula() {
            // Dictionary size = 24 + (len * 16) + sum(key_len + value_size)

            // Empty dict: 24
            let empty = PdfObject::Dictionary(PdfDictionary::new());
            assert_eq!(estimate_object_size(&empty), 24);

            // Single entry: 24 + 16 + ("Type".len + Name("Page").size)
            let mut dict = PdfDictionary::new();
            dict.insert(
                "Type".to_string(),
                PdfObject::Name(PdfName::new("Page".to_string())),
            );
            let obj = PdfObject::Dictionary(dict);
            let expected = 24 + 16 + 4 + (24 + 4); // key_len=4, name_size=24+4
            assert_eq!(estimate_object_size(&obj), expected);
        }

        #[test]
        fn test_cache_isolation_between_instances() {
            let data = create_minimal_pdf();

            // Create two independent readers
            let cursor1 = Cursor::new(data.clone());
            let cursor2 = Cursor::new(data);

            let mut reader1 =
                OptimizedPdfReader::new(cursor1).expect("Minimal PDF must parse successfully");
            let mut reader2 =
                OptimizedPdfReader::new(cursor2).expect("Minimal PDF must parse successfully");

            // Load object in reader1
            let _ = reader1.get_object(1, 0);
            assert_eq!(reader1.memory_stats().cached_objects, 1);

            // reader2 should have independent cache (empty)
            assert_eq!(
                reader2.memory_stats().cached_objects,
                0,
                "Readers must have independent caches"
            );

            // Load in reader2
            let _ = reader2.get_object(1, 0);
            assert_eq!(
                reader2.memory_stats().cached_objects,
                1,
                "reader2 cache should now have 1 object"
            );
            assert_eq!(
                reader1.memory_stats().cached_objects,
                1,
                "reader1 cache unchanged"
            );
        }

        #[test]
        fn test_reader_with_strict_options() {
            let data = create_minimal_pdf();
            let cursor = Cursor::new(data);

            let parse_options = ParseOptions::strict();
            let memory_options = MemoryOptions::default();

            let reader =
                OptimizedPdfReader::new_with_options(cursor, parse_options, memory_options)
                    .expect("Minimal PDF must parse successfully");

            let opts = reader.options();
            assert_eq!(
                opts.strict_mode, true,
                "Strict options must have strict_mode=true"
            );
        }

        #[test]
        fn test_reader_with_lenient_options() {
            let data = create_minimal_pdf();
            let cursor = Cursor::new(data);

            let parse_options = ParseOptions::lenient();
            let memory_options = MemoryOptions::default();

            let reader =
                OptimizedPdfReader::new_with_options(cursor, parse_options, memory_options)
                    .expect("Minimal PDF must parse successfully");

            let opts = reader.options();
            assert_eq!(
                opts.strict_mode, false,
                "Lenient options must have strict_mode=false"
            );
        }

        // =============================================================================
        // COVERAGE EXPANSION: Tests for open*() functions (previously uncovered)
        // =============================================================================

        #[test]
        fn test_open_from_file_path() {
            use std::io::Write;
            use tempfile::NamedTempFile;

            // Create temp PDF file
            let mut temp_file = NamedTempFile::new().expect("Failed to create temp file");
            temp_file
                .write_all(&create_minimal_pdf())
                .expect("Failed to write PDF data");

            let path = temp_file.path();

            // Test open() function
            let result = OptimizedPdfReader::open(path);

            assert!(result.is_ok(), "open() must succeed with valid PDF file");

            let reader = result.unwrap();

            // Verify it's using lenient options
            assert_eq!(
                reader.options().strict_mode,
                false,
                "open() must use lenient parsing"
            );

            // Verify version was parsed correctly
            assert_eq!(reader.version().major, 1);
            assert_eq!(reader.version().minor, 4);
        }

        #[test]
        fn test_open_with_memory_options() {
            use std::io::Write;
            use tempfile::NamedTempFile;

            let mut temp_file = NamedTempFile::new().expect("Failed to create temp file");
            temp_file
                .write_all(&create_minimal_pdf())
                .expect("Failed to write PDF data");

            let path = temp_file.path();

            // Custom memory options with small cache
            let memory_options = MemoryOptions::default().with_cache_size(10);

            // Test open_with_memory() function
            let result = OptimizedPdfReader::open_with_memory(path, memory_options);

            assert!(result.is_ok(), "open_with_memory() must succeed");

            let mut reader = result.unwrap();

            // Verify lenient parsing
            assert_eq!(reader.options().strict_mode, false);

            // Verify cache works with custom size
            let _ = reader.get_object(1, 0);
            assert_eq!(
                reader.memory_stats().cached_objects,
                1,
                "Cache should respect custom memory options"
            );
        }

        #[test]
        fn test_open_strict_mode() {
            use std::io::Write;
            use tempfile::NamedTempFile;

            let mut temp_file = NamedTempFile::new().expect("Failed to create temp file");
            temp_file
                .write_all(&create_minimal_pdf())
                .expect("Failed to write PDF data");

            let path = temp_file.path();

            // Test open_strict() function
            let result = OptimizedPdfReader::open_strict(path);

            assert!(result.is_ok(), "open_strict() must succeed with valid PDF");

            let reader = result.unwrap();

            // Verify strict mode is enabled
            assert_eq!(
                reader.options().strict_mode,
                true,
                "open_strict() must use strict parsing"
            );

            // Verify version parsing still works
            assert_eq!(reader.version().major, 1);
            assert_eq!(reader.version().minor, 4);
        }

        #[test]
        fn test_open_nonexistent_file() {
            use std::path::PathBuf;

            // Try to open file that doesn't exist
            let path = PathBuf::from("/tmp/this_file_does_not_exist_xyz_123.pdf");

            let result = OptimizedPdfReader::open(&path);

            assert!(result.is_err(), "open() must fail with nonexistent file");

            // Should get IO error (file not found)
            match result {
                Err(ParseError::Io(_)) => {
                    // Expected error type
                }
                Err(other) => panic!("Expected IO error, got: {:?}", other),
                Ok(_) => panic!("Should not succeed with nonexistent file"),
            }
        }

        #[test]
        fn test_load_object_from_disk_free_object() {
            // This tests the "free object" path in load_object_from_disk
            // We need a PDF with a free entry in xref

            // PDF with free object at position 0
            let pdf_with_free = b"%PDF-1.4\n\
1 0 obj\n\
<< /Type /Catalog /Pages 2 0 R >>\n\
endobj\n\
2 0 obj\n\
<< /Type /Pages /Kids [3 0 R] /Count 1 >>\n\
endobj\n\
3 0 obj\n\
<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>\n\
endobj\n\
xref\n\
0 4\n\
0000000000 65535 f \n\
0000000009 00000 n \n\
0000000058 00000 n \n\
0000000115 00000 n \n\
trailer\n\
<< /Size 4 /Root 1 0 R >>\n\
startxref\n\
186\n\
%%EOF\n"
                .to_vec();

            let cursor = Cursor::new(pdf_with_free);
            let mut reader =
                OptimizedPdfReader::new(cursor).expect("PDF with free object must parse");

            // Try to get object 0 (free object)
            let result = reader.get_object(0, 65535);

            // Free objects return Null (not an error)
            if let Ok(obj) = result {
                assert!(
                    matches!(obj, PdfObject::Null),
                    "Free object should return Null"
                );
            }
        }

        #[test]
        fn test_find_catalog_when_trailer_missing_root() {
            // Test the fallback catalog finding logic
            // This is tested indirectly through catalog() function

            let data = create_minimal_pdf();
            let cursor = Cursor::new(data);

            let mut reader = OptimizedPdfReader::new(cursor).expect("Minimal PDF must parse");

            // catalog() should use find_catalog_object if Root is missing
            let result = reader.catalog();

            // With valid minimal PDF, catalog should be found
            if let Ok(catalog) = result {
                assert_eq!(
                    catalog.get("Type"),
                    Some(&PdfObject::Name(PdfName("Catalog".to_string()))),
                    "Catalog must have /Type /Catalog"
                );
            }
        }

        #[test]
        fn test_load_object_generation_mismatch_strict() {
            // Test that strict mode rejects generation number mismatches
            // Use a properly formatted PDF with correct xref but intentionally
            // request wrong generation number

            let data = create_minimal_pdf();
            let cursor = Cursor::new(data);

            // Create with STRICT options
            let parse_options = ParseOptions::strict();
            let memory_options = MemoryOptions::default();

            let mut reader =
                OptimizedPdfReader::new_with_options(cursor, parse_options, memory_options)
                    .expect("Minimal PDF must parse in strict mode");

            // Object 1 exists with generation 0
            // Try to access with wrong generation number (5) in strict mode
            let result = reader.get_object(1, 5);

            // In strict mode, should get InvalidReference error
            assert!(
                result.is_err(),
                "Strict mode must reject generation number mismatch"
            );

            if let Err(e) = result {
                assert!(
                    matches!(e, ParseError::InvalidReference(_, _)),
                    "Expected InvalidReference error, got: {:?}",
                    e
                );
            }
        }
    }
}