Debian Codegraph @

debian/missing-sources/lopdf/examples/final_compression_test.rs

raw
1 use lopdf::{Document, SaveOptions, Object};
2
3 fn main() {
4 println!("Final Object Stream Compression Test\n");
5 println!("{}", "=".repeat(60));
6
7 // Create test document with various object types
8 let mut doc = Document::with_version("1.5");
9
10 // Add some compressible objects (fonts, annotations, etc.)
11 let font1 = doc.add_object(lopdf::dictionary! {
12 "Type" => "Font",
13 "Subtype" => "Type1",
14 "BaseFont" => "Helvetica"
15 });
16
17 let font2 = doc.add_object(lopdf::dictionary! {
18 "Type" => "Font",
19 "Subtype" => "Type1",
20 "BaseFont" => "Times-Roman"
21 });
22
23 // Add a page (should NOT be compressed)
24 let page_id = doc.add_object(lopdf::dictionary! {
25 "Type" => "Page",
26 "MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()],
27 "Contents" => lopdf::Object::Array(vec![]),
28 "Resources" => lopdf::dictionary! {
29 "Font" => lopdf::dictionary! {
30 "F1" => font1,
31 "F2" => font2
32 }
33 }
34 });
35
36 // Add pages tree (should NOT be compressed)
37 let pages_id = doc.add_object(lopdf::dictionary! {
38 "Type" => "Pages",
39 "Kids" => vec![page_id.into()],
40 "Count" => 1
41 });
42
43 // Add catalog (should NOT be compressed - it's in trailer)
44 let catalog_id = doc.add_object(lopdf::dictionary! {
45 "Type" => "Catalog",
46 "Pages" => pages_id
47 });
48
49 doc.trailer.set("Root", catalog_id);
50
51 // Add more compressible objects
52 for i in 0..10 {
53 doc.add_object(lopdf::dictionary! {
54 "Type" => "Annot",
55 "Subtype" => "Text",
56 "Contents" => format!("Annotation {}", i)
57 });
58 }
59
60 println!("Created test document with:");
61 println!(" - 2 Font objects (should be compressed)");
62 println!(" - 1 Page object (should NOT be compressed)");
63 println!(" - 1 Pages object (should NOT be compressed)");
64 println!(" - 1 Catalog object (should NOT be compressed)");
65 println!(" - 10 Annotation objects (should be compressed)");
66
67 // Save with object streams
68 let options = SaveOptions {
69 use_object_streams: true,
70 use_xref_streams: true,
71 ..Default::default()
72 };
73
74 let mut buffer = Vec::new();
75 doc.save_with_options(&mut buffer, options).unwrap();
76
77 println!("\nSaved with object streams: {} bytes", buffer.len());
78
79 // Load back and analyze
80 let loaded = Document::load_mem(&buffer).unwrap();
81 println!("\nAnalyzing saved document:");
82 println!(" Total objects: {}", loaded.objects.len());
83
84 // Find object streams
85 let mut obj_stream_count = 0;
86 let mut compressed_count = 0;
87
88 for (_id, obj) in &loaded.objects {
89 if let Object::Stream(stream) = obj {
90 if let Ok(type_obj) = stream.dict.get(b"Type") {
91 if let Ok(type_name) = type_obj.as_name() {
92 if type_name == b"ObjStm" {
93 obj_stream_count += 1;
94
95 // Count objects in stream
96 if let Ok(n) = stream.dict.get(b"N").and_then(|o| o.as_i64()) {
97 compressed_count += n as usize;
98 }
99 }
100 }
101 }
102 }
103 }
104
105 println!(" Object streams: {}", obj_stream_count);
106 println!(" Compressed objects: {}", compressed_count);
107
108 // Verify critical objects are NOT compressed
109 println!("\nVerifying critical objects:");
110
111 // Check catalog
112 if let Ok(root_ref) = loaded.trailer.get(b"Root") {
113 if let Object::Reference(cat_id) = root_ref {
114 check_not_compressed(&loaded, *cat_id, "Catalog");
115 }
116 }
117
118 // Check pages tree
119 if let Ok(catalog) = loaded.catalog() {
120 if let Ok(pages_ref) = catalog.get(b"Pages") {
121 if let Object::Reference(pages_id) = pages_ref {
122 check_not_compressed(&loaded, *pages_id, "Pages tree");
123 }
124 }
125 }
126
127 // Check page objects
128 let pages = loaded.get_pages();
129 for (num, &page_id) in pages.iter() {
130 check_not_compressed(&loaded, page_id, &format!("Page {}", num));
131 }
132
133 println!("\n{}", "=".repeat(60));
134 println!("Test complete! Object streams are working correctly.");
135 }
136
137 fn check_not_compressed(doc: &Document, id: (u32, u16), name: &str) {
138 if let Some(xref_entry) = doc.reference_table.get(id.0) {
139 match xref_entry {
140 lopdf::xref::XrefEntry::Normal { .. } => {
141 println!(" ✓ {} is NOT compressed", name);
142 }
143 lopdf::xref::XrefEntry::Compressed { container, index } => {
144 println!(" ✗ ERROR: {} is compressed in stream {} at index {}!", name, container, index);
145 }
146 _ => {}
147 }
148 }
149 }