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
//! Base action types and dictionary

use crate::objects::{Dictionary, Object, ObjectId};
use crate::structure::Destination;

/// PDF action types
#[derive(Debug, Clone, PartialEq)]
pub enum ActionType {
    /// Go to a destination in the current document
    GoTo,
    /// Go to a destination in another document
    GoToR,
    /// Go to a destination in an embedded file
    GoToE,
    /// Launch an application
    Launch,
    /// Execute a predefined action
    Named,
    /// Resolve a URI
    URI,
    /// Submit form data
    SubmitForm,
    /// Reset form fields
    ResetForm,
    /// Import form data
    ImportData,
    /// Execute JavaScript
    JavaScript,
    /// Set OCG state
    SetOCGState,
    /// Play a sound
    Sound,
    /// Play a movie
    Movie,
    /// Control multimedia presentation
    Rendition,
    /// Transition action
    Trans,
    /// 3D view action
    GoTo3DView,
}

impl ActionType {
    /// Convert to PDF name
    pub fn to_name(&self) -> &'static str {
        match self {
            ActionType::GoTo => "GoTo",
            ActionType::GoToR => "GoToR",
            ActionType::GoToE => "GoToE",
            ActionType::Launch => "Launch",
            ActionType::Named => "Named",
            ActionType::URI => "URI",
            ActionType::SubmitForm => "SubmitForm",
            ActionType::ResetForm => "ResetForm",
            ActionType::ImportData => "ImportData",
            ActionType::JavaScript => "JavaScript",
            ActionType::SetOCGState => "SetOCGState",
            ActionType::Sound => "Sound",
            ActionType::Movie => "Movie",
            ActionType::Rendition => "Rendition",
            ActionType::Trans => "Trans",
            ActionType::GoTo3DView => "GoTo3DView",
        }
    }
}

/// PDF action
#[derive(Debug, Clone)]
pub enum Action {
    /// Go to destination
    GoTo {
        /// Destination
        destination: Destination,
    },
    /// Go to remote destination
    GoToR {
        /// File specification
        file: String,
        /// Destination in the file
        destination: Option<Destination>,
        /// Whether to open in new window
        new_window: Option<bool>,
    },
    /// URI action
    URI {
        /// URI to resolve
        uri: String,
        /// Whether URI is a map
        is_map: bool,
    },
    /// Named action
    Named {
        /// Action name
        name: String,
    },
    /// Launch application
    Launch {
        /// Application/document to launch
        file: String,
        /// Parameters
        parameters: Option<String>,
        /// Whether to open in new window
        new_window: Option<bool>,
    },
    /// Next action in sequence
    Next(Box<Action>),
}

impl Action {
    /// Create GoTo action
    pub fn goto(destination: Destination) -> Self {
        Action::GoTo { destination }
    }

    /// Create URI action
    pub fn uri(uri: impl Into<String>) -> Self {
        Action::URI {
            uri: uri.into(),
            is_map: false,
        }
    }

    /// Create Named action
    pub fn named(name: impl Into<String>) -> Self {
        Action::Named { name: name.into() }
    }

    /// Create GoToR action
    pub fn goto_remote(file: impl Into<String>, destination: Option<Destination>) -> Self {
        Action::GoToR {
            file: file.into(),
            destination,
            new_window: None,
        }
    }

    /// Create Launch action
    pub fn launch(file: impl Into<String>) -> Self {
        Action::Launch {
            file: file.into(),
            parameters: None,
            new_window: None,
        }
    }

    /// Convert to dictionary
    pub fn to_dict(&self) -> Dictionary {
        let mut dict = Dictionary::new();

        match self {
            Action::GoTo { destination } => {
                dict.set("Type", Object::Name("Action".to_string()));
                dict.set("S", Object::Name("GoTo".to_string()));
                dict.set("D", Object::Array(destination.to_array().into()));
            }
            Action::GoToR {
                file,
                destination,
                new_window,
            } => {
                dict.set("Type", Object::Name("Action".to_string()));
                dict.set("S", Object::Name("GoToR".to_string()));
                dict.set("F", Object::String(file.clone()));

                if let Some(dest) = destination {
                    dict.set("D", Object::Array(dest.to_array().into()));
                }

                if let Some(nw) = new_window {
                    dict.set("NewWindow", Object::Boolean(*nw));
                }
            }
            Action::URI { uri, is_map } => {
                dict.set("Type", Object::Name("Action".to_string()));
                dict.set("S", Object::Name("URI".to_string()));
                dict.set("URI", Object::String(uri.clone()));

                if *is_map {
                    dict.set("IsMap", Object::Boolean(true));
                }
            }
            Action::Named { name } => {
                dict.set("Type", Object::Name("Action".to_string()));
                dict.set("S", Object::Name("Named".to_string()));
                dict.set("N", Object::Name(name.clone()));
            }
            Action::Launch {
                file,
                parameters,
                new_window,
            } => {
                dict.set("Type", Object::Name("Action".to_string()));
                dict.set("S", Object::Name("Launch".to_string()));
                dict.set("F", Object::String(file.clone()));

                if let Some(params) = parameters {
                    dict.set("P", Object::String(params.clone()));
                }

                if let Some(nw) = new_window {
                    dict.set("NewWindow", Object::Boolean(*nw));
                }
            }
            Action::Next(next) => {
                let next_dict = next.to_dict();
                dict = next_dict;
            }
        }

        dict
    }
}

/// Action dictionary wrapper
pub struct ActionDictionary {
    /// The action
    pub action: Action,
    /// Object ID if indirect
    pub object_id: Option<ObjectId>,
}

impl ActionDictionary {
    /// Create new action dictionary
    pub fn new(action: Action) -> Self {
        Self {
            action,
            object_id: None,
        }
    }

    /// Set object ID for indirect reference
    pub fn with_object_id(mut self, id: ObjectId) -> Self {
        self.object_id = Some(id);
        self
    }

    /// Convert to dictionary
    pub fn to_dict(&self) -> Dictionary {
        self.action.to_dict()
    }

    /// Get as object (direct or indirect reference)
    pub fn to_object(&self) -> Object {
        if let Some(id) = self.object_id {
            Object::Reference(id)
        } else {
            Object::Dictionary(self.to_dict())
        }
    }
}

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

    #[test]
    fn test_action_type_names() {
        assert_eq!(ActionType::GoTo.to_name(), "GoTo");
        assert_eq!(ActionType::URI.to_name(), "URI");
        assert_eq!(ActionType::Named.to_name(), "Named");
        assert_eq!(ActionType::Launch.to_name(), "Launch");
    }

    #[test]
    fn test_goto_action() {
        let dest = Destination::fit(PageDestination::PageNumber(0));
        let action = Action::goto(dest);

        let dict = action.to_dict();
        assert_eq!(dict.get("S"), Some(&Object::Name("GoTo".to_string())));
        assert!(dict.get("D").is_some());
    }

    #[test]
    fn test_uri_action() {
        let action = Action::uri("https://example.com");
        let dict = action.to_dict();

        assert_eq!(dict.get("S"), Some(&Object::Name("URI".to_string())));
        assert_eq!(
            dict.get("URI"),
            Some(&Object::String("https://example.com".to_string()))
        );
    }

    #[test]
    fn test_named_action() {
        let action = Action::named("NextPage");
        let dict = action.to_dict();

        assert_eq!(dict.get("S"), Some(&Object::Name("Named".to_string())));
        assert_eq!(dict.get("N"), Some(&Object::Name("NextPage".to_string())));
    }

    #[test]
    fn test_action_dictionary() {
        let action = Action::uri("https://example.com");
        let action_dict = ActionDictionary::new(action).with_object_id(ObjectId::new(10, 0));

        match action_dict.to_object() {
            Object::Reference(id) => {
                assert_eq!(id.number(), 10);
                assert_eq!(id.generation(), 0);
            }
            _ => panic!("Expected reference"),
        }
    }

    #[test]
    fn test_all_action_type_names() {
        assert_eq!(ActionType::GoTo.to_name(), "GoTo");
        assert_eq!(ActionType::GoToR.to_name(), "GoToR");
        assert_eq!(ActionType::GoToE.to_name(), "GoToE");
        assert_eq!(ActionType::Launch.to_name(), "Launch");
        assert_eq!(ActionType::Named.to_name(), "Named");
        assert_eq!(ActionType::URI.to_name(), "URI");
        assert_eq!(ActionType::SubmitForm.to_name(), "SubmitForm");
        assert_eq!(ActionType::ResetForm.to_name(), "ResetForm");
        assert_eq!(ActionType::ImportData.to_name(), "ImportData");
        assert_eq!(ActionType::JavaScript.to_name(), "JavaScript");
        assert_eq!(ActionType::SetOCGState.to_name(), "SetOCGState");
        assert_eq!(ActionType::Sound.to_name(), "Sound");
        assert_eq!(ActionType::Movie.to_name(), "Movie");
        assert_eq!(ActionType::Rendition.to_name(), "Rendition");
        assert_eq!(ActionType::Trans.to_name(), "Trans");
        assert_eq!(ActionType::GoTo3DView.to_name(), "GoTo3DView");
    }

    #[test]
    fn test_action_type_debug_clone_partial_eq() {
        let action_type = ActionType::GoTo;
        let cloned = action_type.clone();
        assert_eq!(action_type, cloned);

        let debug_str = format!("{action_type:?}");
        assert!(debug_str.contains("GoTo"));

        // Test inequality
        assert_ne!(ActionType::GoTo, ActionType::URI);
        assert_ne!(ActionType::Named, ActionType::Launch);
    }

    #[test]
    fn test_goto_remote_action() {
        let dest = Destination::fit(PageDestination::PageNumber(5));
        let action = Action::goto_remote("external.pdf", Some(dest));

        let dict = action.to_dict();
        assert_eq!(dict.get("S"), Some(&Object::Name("GoToR".to_string())));
        assert_eq!(
            dict.get("F"),
            Some(&Object::String("external.pdf".to_string()))
        );
        assert!(dict.get("D").is_some());
        assert_eq!(dict.get("NewWindow"), None);
    }

    #[test]
    fn test_goto_remote_action_with_new_window() {
        let action = Action::GoToR {
            file: "external.pdf".to_string(),
            destination: None,
            new_window: Some(true),
        };

        let dict = action.to_dict();
        assert_eq!(dict.get("S"), Some(&Object::Name("GoToR".to_string())));
        assert_eq!(
            dict.get("F"),
            Some(&Object::String("external.pdf".to_string()))
        );
        assert_eq!(dict.get("D"), None);
        assert_eq!(dict.get("NewWindow"), Some(&Object::Boolean(true)));
    }

    #[test]
    fn test_launch_action() {
        let action = Action::launch("notepad.exe");
        let dict = action.to_dict();

        assert_eq!(dict.get("S"), Some(&Object::Name("Launch".to_string())));
        assert_eq!(
            dict.get("F"),
            Some(&Object::String("notepad.exe".to_string()))
        );
        assert_eq!(dict.get("P"), None);
        assert_eq!(dict.get("NewWindow"), None);
    }

    #[test]
    fn test_launch_action_with_parameters() {
        let action = Action::Launch {
            file: "app.exe".to_string(),
            parameters: Some("--verbose".to_string()),
            new_window: Some(false),
        };

        let dict = action.to_dict();
        assert_eq!(dict.get("S"), Some(&Object::Name("Launch".to_string())));
        assert_eq!(dict.get("F"), Some(&Object::String("app.exe".to_string())));
        assert_eq!(
            dict.get("P"),
            Some(&Object::String("--verbose".to_string()))
        );
        assert_eq!(dict.get("NewWindow"), Some(&Object::Boolean(false)));
    }

    #[test]
    fn test_uri_action_with_is_map() {
        let action = Action::URI {
            uri: "https://example.com/map".to_string(),
            is_map: true,
        };

        let dict = action.to_dict();
        assert_eq!(dict.get("S"), Some(&Object::Name("URI".to_string())));
        assert_eq!(
            dict.get("URI"),
            Some(&Object::String("https://example.com/map".to_string()))
        );
        assert_eq!(dict.get("IsMap"), Some(&Object::Boolean(true)));
    }

    #[test]
    fn test_uri_action_without_is_map() {
        let action = Action::uri("https://example.com");

        match action {
            Action::URI { uri, is_map } => {
                assert_eq!(uri, "https://example.com");
                assert!(!is_map);
            }
            _ => panic!("Expected URI action"),
        }
    }

    #[test]
    fn test_next_action() {
        let inner_action = Action::named("FirstPage");
        let next_action = Action::Next(Box::new(inner_action));

        let dict = next_action.to_dict();
        // Next action should have the same dictionary as its inner action
        assert_eq!(dict.get("S"), Some(&Object::Name("Named".to_string())));
        assert_eq!(dict.get("N"), Some(&Object::Name("FirstPage".to_string())));
    }

    #[test]
    fn test_action_debug_clone() {
        let action = Action::uri("https://example.com");
        let cloned = action.clone();

        let debug_str = format!("{action:?}");
        assert!(debug_str.contains("URI"));
        assert!(debug_str.contains("https://example.com"));

        match (action, cloned) {
            (
                Action::URI {
                    uri: uri1,
                    is_map: map1,
                },
                Action::URI {
                    uri: uri2,
                    is_map: map2,
                },
            ) => {
                assert_eq!(uri1, uri2);
                assert_eq!(map1, map2);
            }
            _ => panic!("Expected URI actions"),
        }
    }

    #[test]
    fn test_action_dictionary_without_object_id() {
        let action = Action::named("LastPage");
        let action_dict = ActionDictionary::new(action);

        assert_eq!(action_dict.object_id, None);

        match action_dict.to_object() {
            Object::Dictionary(dict) => {
                assert_eq!(dict.get("S"), Some(&Object::Name("Named".to_string())));
                assert_eq!(dict.get("N"), Some(&Object::Name("LastPage".to_string())));
            }
            _ => panic!("Expected dictionary"),
        }
    }

    #[test]
    fn test_action_dictionary_to_dict() {
        let action = Action::uri("https://test.com");
        let action_dict = ActionDictionary::new(action);

        let dict = action_dict.to_dict();
        assert_eq!(dict.get("S"), Some(&Object::Name("URI".to_string())));
        assert_eq!(
            dict.get("URI"),
            Some(&Object::String("https://test.com".to_string()))
        );
    }

    #[test]
    fn test_goto_action_destination_handling() {
        let dest = Destination::fit(PageDestination::PageNumber(10));
        let action = Action::goto(dest.clone());

        match action {
            Action::GoTo { destination } => {
                // Verify destination is properly stored
                assert_eq!(destination.to_array().len(), dest.to_array().len());
            }
            _ => panic!("Expected GoTo action"),
        }
    }

    #[test]
    fn test_action_constructor_string_conversion() {
        // Test that Into<String> trait is used correctly
        let uri_action = Action::uri("test");
        let named_action = Action::named("test");
        let remote_action = Action::goto_remote("test.pdf", None);
        let launch_action = Action::launch("test.exe");

        match uri_action {
            Action::URI { uri, .. } => assert_eq!(uri, "test"),
            _ => panic!("Expected URI action"),
        }

        match named_action {
            Action::Named { name } => assert_eq!(name, "test"),
            _ => panic!("Expected Named action"),
        }

        match remote_action {
            Action::GoToR { file, .. } => assert_eq!(file, "test.pdf"),
            _ => panic!("Expected GoToR action"),
        }

        match launch_action {
            Action::Launch { file, .. } => assert_eq!(file, "test.exe"),
            _ => panic!("Expected Launch action"),
        }
    }

    #[test]
    fn test_action_dict_type_field() {
        let actions = vec![
            Action::uri("https://example.com"),
            Action::named("NextPage"),
            Action::launch("app.exe"),
            Action::goto_remote("file.pdf", None),
        ];

        for action in actions {
            let dict = action.to_dict();
            assert_eq!(dict.get("Type"), Some(&Object::Name("Action".to_string())));
        }
    }

    #[test]
    fn test_complex_action_chaining() {
        let inner = Action::named("PrevPage");
        let next = Action::Next(Box::new(inner));

        let dict = next.to_dict();
        // Should inherit the inner action's dictionary
        assert_eq!(dict.get("S"), Some(&Object::Name("Named".to_string())));
        assert_eq!(dict.get("N"), Some(&Object::Name("PrevPage".to_string())));
    }

    #[test]
    fn test_action_object_id_generation_increments() {
        let action1 =
            ActionDictionary::new(Action::uri("url1")).with_object_id(ObjectId::new(1, 0));
        let action2 =
            ActionDictionary::new(Action::uri("url2")).with_object_id(ObjectId::new(2, 0));

        match (action1.to_object(), action2.to_object()) {
            (Object::Reference(id1), Object::Reference(id2)) => {
                assert_eq!(id1.number(), 1);
                assert_eq!(id2.number(), 2);
                assert_ne!(id1.number(), id2.number());
            }
            _ => panic!("Expected references"),
        }
    }

    #[test]
    fn test_action_pattern_matching() {
        let actions = vec![
            Action::goto(Destination::fit(PageDestination::PageNumber(0))),
            Action::uri("https://example.com"),
            Action::named("Test"),
            Action::launch("app.exe"),
            Action::goto_remote("remote.pdf", None),
        ];

        let mut goto_count = 0;
        let mut uri_count = 0;
        let mut named_count = 0;
        let mut launch_count = 0;
        let mut gotor_count = 0;

        for action in actions {
            match action {
                Action::GoTo { .. } => goto_count += 1,
                Action::URI { .. } => uri_count += 1,
                Action::Named { .. } => named_count += 1,
                Action::Launch { .. } => launch_count += 1,
                Action::GoToR { .. } => gotor_count += 1,
                Action::Next(_) => {}
            }
        }

        assert_eq!(goto_count, 1);
        assert_eq!(uri_count, 1);
        assert_eq!(named_count, 1);
        assert_eq!(launch_count, 1);
        assert_eq!(gotor_count, 1);
    }
}