Debian Codegraph @

debian/missing-sources/lopdf/tests/simple_object_stream_test.rs

raw
1 use lopdf::{dictionary, Document, Object, ObjectStream, Stream};
2
3 #[test]
4 fn test_object_stream_builder() {
5 let builder = ObjectStream::builder();
6 assert_eq!(builder.get_max_objects(), 100);
7 assert_eq!(builder.get_compression_level(), 6);
8 }
9
10 #[test]
11 fn test_object_stream_add_and_build() {
12 let mut obj_stream = ObjectStream::builder().build();
13
14 // Add some objects
15 obj_stream.add_object((1, 0), Object::Integer(42)).unwrap();
16 obj_stream.add_object((2, 0), Object::Boolean(true)).unwrap();
17 obj_stream.add_object((3, 0), Object::Name(b"Test".to_vec())).unwrap();
18
19 // Build the stream content
20 let content = obj_stream.build_stream_content().unwrap();
21 assert!(!content.is_empty());
22
23 // Convert to stream object
24 let stream_obj = obj_stream.to_stream_object().unwrap();
25 assert_eq!(stream_obj.dict.get(b"Type").unwrap(), &Object::Name(b"ObjStm".to_vec()));
26 assert_eq!(stream_obj.dict.get(b"N").unwrap(), &Object::Integer(3));
27 }
28
29 #[test]
30 fn test_save_with_object_streams() {
31 let mut doc = Document::with_version("1.5");
32 let pages_id = doc.new_object_id();
33
34 let font_id = doc.add_object(dictionary! {
35 "Type" => "Font",
36 "Subtype" => "Type1",
37 "BaseFont" => "Helvetica"
38 });
39
40 let resources_id = doc.add_object(dictionary! {
41 "Font" => dictionary! {
42 "F1" => font_id,
43 },
44 });
45
46 let content = lopdf::content::Content {
47 operations: vec![
48 lopdf::content::Operation::new("BT", vec![]),
49 lopdf::content::Operation::new("Tf", vec!["F1".into(), 12.into()]),
50 lopdf::content::Operation::new("Td", vec![50.into(), 700.into()]),
51 lopdf::content::Operation::new("Tj", vec![Object::string_literal("Test Document")]),
52 lopdf::content::Operation::new("ET", vec![]),
53 ],
54 };
55
56 let content_id = doc.add_object(Stream::new(
57 dictionary! {},
58 content.encode().unwrap()
59 ));
60
61 let page_id = doc.add_object(dictionary! {
62 "Type" => "Page",
63 "Parent" => pages_id,
64 "Contents" => content_id,
65 "Resources" => resources_id,
66 "MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()],
67 });
68
69 doc.objects.insert(pages_id, Object::Dictionary(dictionary! {
70 "Type" => "Pages",
71 "Kids" => vec![page_id.into()],
72 "Count" => 1,
73 }));
74
75 let catalog_id = doc.add_object(dictionary! {
76 "Type" => "Catalog",
77 "Pages" => pages_id,
78 });
79
80 doc.trailer.set("Root", catalog_id);
81
82 // Test save_modern
83 let mut buffer = Vec::new();
84 let result = doc.save_modern(&mut buffer);
85 assert!(result.is_ok());
86
87 // Verify PDF was created
88 let content = String::from_utf8_lossy(&buffer);
89 assert!(content.starts_with("%PDF-1.5"));
90
91 // Verify object streams were created
92 assert!(content.contains("/ObjStm"), "Object streams should be created");
93
94 // Verify that structural objects are NOT present as individual objects
95 assert!(!content.contains("2 0 obj\n<</Type/Pages"), "Pages object should be compressed");
96 assert!(!content.contains("3 0 obj\n<</Type/Page"), "Page object should be compressed");
97 }