oxidize-pdf 2.5.1

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
//! Named actions for executing predefined PDF viewer operations

use crate::objects::{Dictionary, Object};

/// Standard named actions defined in ISO 32000-1
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum StandardNamedAction {
    /// Go to next page
    NextPage,
    /// Go to previous page
    PrevPage,
    /// Go to first page
    FirstPage,
    /// Go to last page
    LastPage,
    /// Go back to previous view
    GoBack,
    /// Go forward to next view
    GoForward,
    /// Print the document
    Print,
    /// Save the document
    SaveAs,
    /// Open the document
    Open,
    /// Close the document
    Close,
    /// Quit the application
    Quit,
    /// Enter full screen mode
    FullScreen,
    /// Find text in document
    Find,
    /// Find next occurrence
    FindNext,
    /// Open page thumbnails
    PageThumbs,
    /// Open bookmarks panel
    Bookmarks,
    /// Fit page in window
    FitPage,
    /// Fit page width
    FitWidth,
    /// Fit page height
    FitHeight,
    /// Actual size (100%)
    ActualSize,
    /// Single page layout
    SinglePage,
    /// Continuous page layout
    OneColumn,
    /// Two column layout
    TwoColumns,
}

impl StandardNamedAction {
    /// Convert to action name
    pub fn to_name(&self) -> &'static str {
        match self {
            StandardNamedAction::NextPage => "NextPage",
            StandardNamedAction::PrevPage => "PrevPage",
            StandardNamedAction::FirstPage => "FirstPage",
            StandardNamedAction::LastPage => "LastPage",
            StandardNamedAction::GoBack => "GoBack",
            StandardNamedAction::GoForward => "GoForward",
            StandardNamedAction::Print => "Print",
            StandardNamedAction::SaveAs => "SaveAs",
            StandardNamedAction::Open => "Open",
            StandardNamedAction::Close => "Close",
            StandardNamedAction::Quit => "Quit",
            StandardNamedAction::FullScreen => "FullScreen",
            StandardNamedAction::Find => "Find",
            StandardNamedAction::FindNext => "FindNext",
            StandardNamedAction::PageThumbs => "PageThumbs",
            StandardNamedAction::Bookmarks => "Bookmarks",
            StandardNamedAction::FitPage => "FitPage",
            StandardNamedAction::FitWidth => "FitWidth",
            StandardNamedAction::FitHeight => "FitHeight",
            StandardNamedAction::ActualSize => "ActualSize",
            StandardNamedAction::SinglePage => "SinglePage",
            StandardNamedAction::OneColumn => "OneColumn",
            StandardNamedAction::TwoColumns => "TwoColumns",
        }
    }
}

/// Named action - execute a predefined action
#[derive(Debug, Clone)]
pub enum NamedAction {
    /// Standard named action
    Standard(StandardNamedAction),
    /// Custom named action
    Custom(String),
}

impl NamedAction {
    /// Create standard named action
    pub fn standard(action: StandardNamedAction) -> Self {
        NamedAction::Standard(action)
    }

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

    /// Navigation actions
    pub fn next_page() -> Self {
        NamedAction::Standard(StandardNamedAction::NextPage)
    }

    pub fn prev_page() -> Self {
        NamedAction::Standard(StandardNamedAction::PrevPage)
    }

    pub fn first_page() -> Self {
        NamedAction::Standard(StandardNamedAction::FirstPage)
    }

    pub fn last_page() -> Self {
        NamedAction::Standard(StandardNamedAction::LastPage)
    }

    /// View actions
    pub fn go_back() -> Self {
        NamedAction::Standard(StandardNamedAction::GoBack)
    }

    pub fn go_forward() -> Self {
        NamedAction::Standard(StandardNamedAction::GoForward)
    }

    /// Document actions
    pub fn print() -> Self {
        NamedAction::Standard(StandardNamedAction::Print)
    }

    pub fn save_as() -> Self {
        NamedAction::Standard(StandardNamedAction::SaveAs)
    }

    /// View mode actions
    pub fn full_screen() -> Self {
        NamedAction::Standard(StandardNamedAction::FullScreen)
    }

    pub fn fit_page() -> Self {
        NamedAction::Standard(StandardNamedAction::FitPage)
    }

    pub fn fit_width() -> Self {
        NamedAction::Standard(StandardNamedAction::FitWidth)
    }

    /// Get action name
    pub fn name(&self) -> &str {
        match self {
            NamedAction::Standard(std) => std.to_name(),
            NamedAction::Custom(name) => name,
        }
    }

    /// Convert to dictionary
    pub fn to_dict(&self) -> Dictionary {
        let mut dict = Dictionary::new();
        dict.set("Type", Object::Name("Action".to_string()));
        dict.set("S", Object::Name("Named".to_string()));
        dict.set("N", Object::Name(self.name().to_string()));
        dict
    }
}

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

    #[test]
    fn test_standard_named_actions() {
        assert_eq!(StandardNamedAction::NextPage.to_name(), "NextPage");
        assert_eq!(StandardNamedAction::Print.to_name(), "Print");
        assert_eq!(StandardNamedAction::FullScreen.to_name(), "FullScreen");
    }

    #[test]
    fn test_named_action_standard() {
        let action = NamedAction::next_page();
        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_named_action_custom() {
        let action = NamedAction::custom("CustomAction");
        let dict = action.to_dict();

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

    #[test]
    fn test_navigation_actions() {
        let actions = [
            NamedAction::next_page(),
            NamedAction::prev_page(),
            NamedAction::first_page(),
            NamedAction::last_page(),
        ];

        let names = ["NextPage", "PrevPage", "FirstPage", "LastPage"];

        for (action, expected_name) in actions.iter().zip(names.iter()) {
            assert_eq!(action.name(), *expected_name);
        }
    }

    #[test]
    fn test_view_actions() {
        let action = NamedAction::fit_page();
        assert_eq!(action.name(), "FitPage");

        let action = NamedAction::full_screen();
        assert_eq!(action.name(), "FullScreen");
    }

    #[test]
    fn test_all_standard_named_actions() {
        let actions = [
            (StandardNamedAction::NextPage, "NextPage"),
            (StandardNamedAction::PrevPage, "PrevPage"),
            (StandardNamedAction::FirstPage, "FirstPage"),
            (StandardNamedAction::LastPage, "LastPage"),
            (StandardNamedAction::GoBack, "GoBack"),
            (StandardNamedAction::GoForward, "GoForward"),
            (StandardNamedAction::Print, "Print"),
            (StandardNamedAction::SaveAs, "SaveAs"),
            (StandardNamedAction::Open, "Open"),
            (StandardNamedAction::Close, "Close"),
            (StandardNamedAction::Quit, "Quit"),
            (StandardNamedAction::FullScreen, "FullScreen"),
            (StandardNamedAction::Find, "Find"),
            (StandardNamedAction::FindNext, "FindNext"),
            (StandardNamedAction::PageThumbs, "PageThumbs"),
            (StandardNamedAction::Bookmarks, "Bookmarks"),
            (StandardNamedAction::FitPage, "FitPage"),
            (StandardNamedAction::FitWidth, "FitWidth"),
            (StandardNamedAction::FitHeight, "FitHeight"),
            (StandardNamedAction::ActualSize, "ActualSize"),
            (StandardNamedAction::SinglePage, "SinglePage"),
            (StandardNamedAction::OneColumn, "OneColumn"),
            (StandardNamedAction::TwoColumns, "TwoColumns"),
        ];

        for (action, expected_name) in actions.iter() {
            assert_eq!(action.to_name(), *expected_name);
        }
    }

    #[test]
    fn test_standard_named_action_debug() {
        let action = StandardNamedAction::Print;
        let debug_str = format!("{action:?}");
        assert!(debug_str.contains("Print"));
    }

    #[test]
    fn test_standard_named_action_clone() {
        let action = StandardNamedAction::FullScreen;
        let cloned = action;
        assert_eq!(action, cloned);
        assert_eq!(action.to_name(), cloned.to_name());
    }

    #[test]
    fn test_standard_named_action_partial_eq() {
        assert_eq!(StandardNamedAction::Print, StandardNamedAction::Print);
        assert_ne!(StandardNamedAction::Print, StandardNamedAction::SaveAs);
        assert_eq!(StandardNamedAction::NextPage, StandardNamedAction::NextPage);
        assert_ne!(StandardNamedAction::NextPage, StandardNamedAction::PrevPage);
    }

    #[test]
    fn test_named_action_debug() {
        let action = NamedAction::print();
        let debug_str = format!("{action:?}");
        assert!(debug_str.contains("Standard"));
        assert!(debug_str.contains("Print"));

        let custom_action = NamedAction::custom("MyCustomAction");
        let debug_str = format!("{custom_action:?}");
        assert!(debug_str.contains("Custom"));
        assert!(debug_str.contains("MyCustomAction"));
    }

    #[test]
    fn test_named_action_clone() {
        let action = NamedAction::fit_width();
        let cloned = action.clone();
        assert_eq!(action.name(), cloned.name());

        let custom_action = NamedAction::custom("TestAction");
        let cloned_custom = custom_action.clone();
        assert_eq!(custom_action.name(), cloned_custom.name());
    }

    #[test]
    fn test_document_actions() {
        let print_action = NamedAction::print();
        assert_eq!(print_action.name(), "Print");

        let save_action = NamedAction::save_as();
        assert_eq!(save_action.name(), "SaveAs");

        let dict = print_action.to_dict();
        assert_eq!(dict.get("S"), Some(&Object::Name("Named".to_string())));
        assert_eq!(dict.get("N"), Some(&Object::Name("Print".to_string())));
    }

    #[test]
    fn test_view_mode_actions() {
        let fit_page = NamedAction::fit_page();
        assert_eq!(fit_page.name(), "FitPage");

        let fit_width = NamedAction::fit_width();
        assert_eq!(fit_width.name(), "FitWidth");

        let full_screen = NamedAction::full_screen();
        assert_eq!(full_screen.name(), "FullScreen");
    }

    #[test]
    fn test_navigation_history_actions() {
        let go_back = NamedAction::go_back();
        assert_eq!(go_back.name(), "GoBack");

        let go_forward = NamedAction::go_forward();
        assert_eq!(go_forward.name(), "GoForward");

        let dict = go_back.to_dict();
        assert_eq!(dict.get("N"), Some(&Object::Name("GoBack".to_string())));
    }

    #[test]
    fn test_named_action_factory_methods() {
        // Test all factory methods return correct action types
        assert!(matches!(
            NamedAction::next_page(),
            NamedAction::Standard(StandardNamedAction::NextPage)
        ));
        assert!(matches!(
            NamedAction::prev_page(),
            NamedAction::Standard(StandardNamedAction::PrevPage)
        ));
        assert!(matches!(
            NamedAction::first_page(),
            NamedAction::Standard(StandardNamedAction::FirstPage)
        ));
        assert!(matches!(
            NamedAction::last_page(),
            NamedAction::Standard(StandardNamedAction::LastPage)
        ));
        assert!(matches!(
            NamedAction::go_back(),
            NamedAction::Standard(StandardNamedAction::GoBack)
        ));
        assert!(matches!(
            NamedAction::go_forward(),
            NamedAction::Standard(StandardNamedAction::GoForward)
        ));
        assert!(matches!(
            NamedAction::print(),
            NamedAction::Standard(StandardNamedAction::Print)
        ));
        assert!(matches!(
            NamedAction::save_as(),
            NamedAction::Standard(StandardNamedAction::SaveAs)
        ));
        assert!(matches!(
            NamedAction::full_screen(),
            NamedAction::Standard(StandardNamedAction::FullScreen)
        ));
        assert!(matches!(
            NamedAction::fit_page(),
            NamedAction::Standard(StandardNamedAction::FitPage)
        ));
        assert!(matches!(
            NamedAction::fit_width(),
            NamedAction::Standard(StandardNamedAction::FitWidth)
        ));

        assert!(matches!(
            NamedAction::custom("Test"),
            NamedAction::Custom(_)
        ));
    }

    #[test]
    fn test_named_action_dictionary_structure() {
        let action = NamedAction::standard(StandardNamedAction::Find);
        let dict = action.to_dict();

        // Verify all required fields are present
        assert_eq!(dict.get("Type"), Some(&Object::Name("Action".to_string())));
        assert_eq!(dict.get("S"), Some(&Object::Name("Named".to_string())));
        assert_eq!(dict.get("N"), Some(&Object::Name("Find".to_string())));

        // Verify only expected fields are present
        assert_eq!(dict.len(), 3);
    }

    #[test]
    fn test_custom_named_action_edge_cases() {
        // Test empty string
        let empty_action = NamedAction::custom("");
        assert_eq!(empty_action.name(), "");

        // Test special characters
        let special_action = NamedAction::custom("Action_With-Special.Chars123");
        assert_eq!(special_action.name(), "Action_With-Special.Chars123");

        // Test unicode
        let unicode_action = NamedAction::custom("アクション");
        assert_eq!(unicode_action.name(), "アクション");
    }

    #[test]
    fn test_named_action_match_patterns() {
        let standard_action = NamedAction::print();
        let custom_action = NamedAction::custom("MyAction");

        // Test pattern matching works correctly
        match standard_action {
            NamedAction::Standard(std_action) => {
                assert_eq!(std_action, StandardNamedAction::Print);
            }
            NamedAction::Custom(_) => panic!("Should be standard action"),
        }

        match custom_action {
            NamedAction::Standard(_) => panic!("Should be custom action"),
            NamedAction::Custom(name) => {
                assert_eq!(name, "MyAction");
            }
        }
    }
}