Skip to main content

pdfkit/
types.rs

1use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign};
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Copy, PartialEq, Deserialize, Serialize)]
6pub struct PdfRect {
7    pub x: f64,
8    pub y: f64,
9    pub width: f64,
10    pub height: f64,
11}
12
13#[derive(Debug, Clone, Copy, PartialEq, Deserialize)]
14pub struct PdfPoint {
15    pub x: f64,
16    pub y: f64,
17}
18
19#[derive(Debug, Clone, Copy, PartialEq, Deserialize)]
20pub struct PdfSize {
21    pub width: f64,
22    pub height: f64,
23}
24
25#[derive(Debug, Clone, Copy, PartialEq, Deserialize)]
26pub struct PdfEdgeInsets {
27    pub top: f64,
28    pub left: f64,
29    pub bottom: f64,
30    pub right: f64,
31}
32
33#[derive(Debug, Clone, Copy, PartialEq, Deserialize)]
34pub struct PdfColor {
35    pub red: f64,
36    pub green: f64,
37    pub blue: f64,
38    pub alpha: f64,
39}
40
41#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
42pub struct PdfTextRange {
43    pub location: usize,
44    pub length: usize,
45}
46
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48#[repr(i32)]
49pub enum DisplayBox {
50    MediaBox = 0,
51    CropBox = 1,
52    BleedBox = 2,
53    TrimBox = 3,
54    ArtBox = 4,
55}
56
57impl DisplayBox {
58    #[must_use]
59    pub const fn as_raw(self) -> i32 {
60        self as i32
61    }
62}
63
64#[derive(Debug, Clone, Copy, PartialEq, Eq)]
65#[repr(i32)]
66pub enum PdfBorderStyle {
67    Solid = 0,
68    Dashed = 1,
69    Beveled = 2,
70    Inset = 3,
71    Underline = 4,
72}
73
74impl PdfBorderStyle {
75    #[must_use]
76    pub const fn from_raw(raw: i32) -> Option<Self> {
77        match raw {
78            0 => Some(Self::Solid),
79            1 => Some(Self::Dashed),
80            2 => Some(Self::Beveled),
81            3 => Some(Self::Inset),
82            4 => Some(Self::Underline),
83            _ => None,
84        }
85    }
86}
87
88#[derive(Debug, Clone, Copy, PartialEq, Eq)]
89#[repr(i32)]
90pub enum PdfActionNamedName {
91    None = 0,
92    NextPage = 1,
93    PreviousPage = 2,
94    FirstPage = 3,
95    LastPage = 4,
96    GoBack = 5,
97    GoForward = 6,
98    GoToPage = 7,
99    Find = 8,
100    Print = 9,
101    ZoomIn = 10,
102    ZoomOut = 11,
103}
104
105impl PdfActionNamedName {
106    #[must_use]
107    pub const fn as_raw(self) -> i32 {
108        self as i32
109    }
110
111    #[must_use]
112    pub const fn from_raw(raw: i32) -> Option<Self> {
113        match raw {
114            0 => Some(Self::None),
115            1 => Some(Self::NextPage),
116            2 => Some(Self::PreviousPage),
117            3 => Some(Self::FirstPage),
118            4 => Some(Self::LastPage),
119            5 => Some(Self::GoBack),
120            6 => Some(Self::GoForward),
121            7 => Some(Self::GoToPage),
122            8 => Some(Self::Find),
123            9 => Some(Self::Print),
124            10 => Some(Self::ZoomIn),
125            11 => Some(Self::ZoomOut),
126            _ => None,
127        }
128    }
129}
130
131#[derive(Debug, Clone, Copy, PartialEq, Eq)]
132#[repr(i32)]
133pub enum PdfLineStyle {
134    None = 0,
135    Square = 1,
136    Circle = 2,
137    Diamond = 3,
138    OpenArrow = 4,
139    ClosedArrow = 5,
140}
141
142impl PdfLineStyle {
143    #[must_use]
144    pub const fn from_raw(raw: i32) -> Option<Self> {
145        match raw {
146            0 => Some(Self::None),
147            1 => Some(Self::Square),
148            2 => Some(Self::Circle),
149            3 => Some(Self::Diamond),
150            4 => Some(Self::OpenArrow),
151            5 => Some(Self::ClosedArrow),
152            _ => None,
153        }
154    }
155}
156
157#[derive(Debug, Clone, Copy, PartialEq, Eq)]
158#[repr(i32)]
159pub enum PdfTextAnnotationIconType {
160    Comment = 0,
161    Key = 1,
162    Note = 2,
163    Help = 3,
164    NewParagraph = 4,
165    Paragraph = 5,
166    Insert = 6,
167}
168
169impl PdfTextAnnotationIconType {
170    #[must_use]
171    pub const fn as_raw(self) -> i32 {
172        self as i32
173    }
174
175    #[must_use]
176    pub const fn from_raw(raw: i32) -> Option<Self> {
177        match raw {
178            0 => Some(Self::Comment),
179            1 => Some(Self::Key),
180            2 => Some(Self::Note),
181            3 => Some(Self::Help),
182            4 => Some(Self::NewParagraph),
183            5 => Some(Self::Paragraph),
184            6 => Some(Self::Insert),
185            _ => None,
186        }
187    }
188}
189
190#[derive(Debug, Clone, Copy, PartialEq, Eq)]
191#[repr(i32)]
192pub enum PdfMarkupType {
193    Highlight = 0,
194    StrikeOut = 1,
195    Underline = 2,
196    Redact = 3,
197}
198
199impl PdfMarkupType {
200    #[must_use]
201    pub const fn from_raw(raw: i32) -> Option<Self> {
202        match raw {
203            0 => Some(Self::Highlight),
204            1 => Some(Self::StrikeOut),
205            2 => Some(Self::Underline),
206            3 => Some(Self::Redact),
207            _ => None,
208        }
209    }
210}
211
212#[derive(Debug, Clone, Copy, PartialEq, Eq)]
213#[repr(i32)]
214pub enum PdfDisplayMode {
215    SinglePage = 0,
216    SinglePageContinuous = 1,
217    TwoUp = 2,
218    TwoUpContinuous = 3,
219}
220
221impl PdfDisplayMode {
222    #[must_use]
223    pub const fn from_raw(raw: i32) -> Option<Self> {
224        match raw {
225            0 => Some(Self::SinglePage),
226            1 => Some(Self::SinglePageContinuous),
227            2 => Some(Self::TwoUp),
228            3 => Some(Self::TwoUpContinuous),
229            _ => None,
230        }
231    }
232}
233
234#[derive(Debug, Clone, Copy, PartialEq, Eq)]
235#[repr(i32)]
236pub enum PdfDisplayDirection {
237    Vertical = 0,
238    Horizontal = 1,
239}
240
241impl PdfDisplayDirection {
242    #[must_use]
243    pub const fn from_raw(raw: i32) -> Option<Self> {
244        match raw {
245            0 => Some(Self::Vertical),
246            1 => Some(Self::Horizontal),
247            _ => None,
248        }
249    }
250}
251
252#[derive(Debug, Clone, Copy, PartialEq, Eq)]
253#[repr(i32)]
254pub enum PdfInterpolationQuality {
255    None = 0,
256    Low = 1,
257    High = 2,
258}
259
260impl PdfInterpolationQuality {
261    #[must_use]
262    pub const fn from_raw(raw: i32) -> Option<Self> {
263        match raw {
264            0 => Some(Self::None),
265            1 => Some(Self::Low),
266            2 => Some(Self::High),
267            _ => None,
268        }
269    }
270}
271
272#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
273pub struct PdfAreaOfInterest(u64);
274
275impl PdfAreaOfInterest {
276    pub const NONE: Self = Self(0);
277    pub const PAGE: Self = Self(1 << 0);
278    pub const TEXT: Self = Self(1 << 1);
279    pub const ANNOTATION: Self = Self(1 << 2);
280    pub const LINK: Self = Self(1 << 3);
281    pub const CONTROL: Self = Self(1 << 4);
282    pub const TEXT_FIELD: Self = Self(1 << 5);
283    pub const ICON: Self = Self(1 << 6);
284    pub const POPUP: Self = Self(1 << 7);
285    pub const IMAGE: Self = Self(1 << 8);
286    pub const ANY: Self = Self(i64::MAX as u64);
287
288    #[must_use]
289    pub const fn from_bits(bits: u64) -> Self {
290        Self(bits)
291    }
292
293    #[must_use]
294    pub const fn bits(self) -> u64 {
295        self.0
296    }
297
298    #[must_use]
299    pub const fn is_empty(self) -> bool {
300        self.0 == 0
301    }
302
303    #[must_use]
304    pub const fn contains(self, other: Self) -> bool {
305        (self.0 & other.0) == other.0
306    }
307
308    #[must_use]
309    pub const fn intersects(self, other: Self) -> bool {
310        (self.0 & other.0) != 0
311    }
312}
313
314impl BitOr for PdfAreaOfInterest {
315    type Output = Self;
316
317    fn bitor(self, rhs: Self) -> Self::Output {
318        Self(self.0 | rhs.0)
319    }
320}
321
322impl BitOrAssign for PdfAreaOfInterest {
323    fn bitor_assign(&mut self, rhs: Self) {
324        self.0 |= rhs.0;
325    }
326}
327
328impl BitAnd for PdfAreaOfInterest {
329    type Output = Self;
330
331    fn bitand(self, rhs: Self) -> Self::Output {
332        Self(self.0 & rhs.0)
333    }
334}
335
336impl BitAndAssign for PdfAreaOfInterest {
337    fn bitand_assign(&mut self, rhs: Self) {
338        self.0 &= rhs.0;
339    }
340}
341
342#[derive(Debug, Clone, Copy, PartialEq, Eq)]
343#[repr(i32)]
344pub enum PdfWidgetControlType {
345    Unknown = -1,
346    PushButton = 0,
347    RadioButton = 1,
348    CheckBox = 2,
349}
350
351impl PdfWidgetControlType {
352    #[must_use]
353    pub const fn from_raw(raw: i32) -> Option<Self> {
354        match raw {
355            -1 => Some(Self::Unknown),
356            0 => Some(Self::PushButton),
357            1 => Some(Self::RadioButton),
358            2 => Some(Self::CheckBox),
359            _ => None,
360        }
361    }
362}
363
364#[derive(Debug, Clone, Copy, PartialEq, Eq)]
365#[repr(i32)]
366pub enum PdfWidgetCellState {
367    Mixed = -1,
368    Off = 0,
369    On = 1,
370}
371
372impl PdfWidgetCellState {
373    #[must_use]
374    pub const fn as_raw(self) -> i32 {
375        self as i32
376    }
377
378    #[must_use]
379    pub const fn from_raw(raw: i32) -> Option<Self> {
380        match raw {
381            -1 => Some(Self::Mixed),
382            0 => Some(Self::Off),
383            1 => Some(Self::On),
384            _ => None,
385        }
386    }
387}
388
389#[derive(Debug, Clone, Copy, PartialEq, Eq)]
390#[repr(i32)]
391pub enum PdfPrintScalingMode {
392    None = 0,
393    ToFit = 1,
394    DownToFit = 2,
395}
396
397impl PdfPrintScalingMode {
398    #[must_use]
399    pub const fn as_raw(self) -> i32 {
400        self as i32
401    }
402
403    #[must_use]
404    pub const fn from_raw(raw: i32) -> Option<Self> {
405        match raw {
406            0 => Some(Self::None),
407            1 => Some(Self::ToFit),
408            2 => Some(Self::DownToFit),
409            _ => None,
410        }
411    }
412}
413
414#[derive(Debug, Clone, Copy, PartialEq, Eq)]
415#[repr(i32)]
416pub enum PdfDocumentPermissions {
417    None = 0,
418    User = 1,
419    Owner = 2,
420}
421
422impl PdfDocumentPermissions {
423    #[must_use]
424    pub const fn from_raw(raw: i32) -> Option<Self> {
425        match raw {
426            0 => Some(Self::None),
427            1 => Some(Self::User),
428            2 => Some(Self::Owner),
429            _ => None,
430        }
431    }
432}
433
434#[derive(Debug, Clone, Copy, PartialEq, Eq)]
435#[repr(u64)]
436pub enum PdfSelectionGranularity {
437    Character = 0,
438    Word = 1,
439    Line = 2,
440}
441
442impl PdfSelectionGranularity {
443    #[must_use]
444    pub const fn as_raw(self) -> u64 {
445        self as u64
446    }
447
448    #[must_use]
449    pub const fn from_raw(raw: u64) -> Option<Self> {
450        match raw {
451            0 => Some(Self::Character),
452            1 => Some(Self::Word),
453            2 => Some(Self::Line),
454            _ => None,
455        }
456    }
457}
458
459#[derive(Debug, Clone, Deserialize)]
460pub struct PdfDocumentInfo {
461    pub document_url: Option<String>,
462    pub major_version: i32,
463    pub minor_version: i32,
464    pub is_encrypted: bool,
465    pub is_locked: bool,
466    pub permissions_status: i32,
467    pub access_permissions: u64,
468    pub allows_printing: bool,
469    pub allows_copying: bool,
470    pub allows_document_changes: bool,
471    pub allows_document_assembly: bool,
472    pub allows_content_accessibility: bool,
473    pub allows_commenting: bool,
474    pub allows_form_field_entry: bool,
475    pub page_class: String,
476}
477
478impl PdfDocumentInfo {
479    #[must_use]
480    pub fn permissions_status_enum(&self) -> Option<PdfDocumentPermissions> {
481        PdfDocumentPermissions::from_raw(self.permissions_status)
482    }
483}
484
485#[derive(Debug, Clone, Deserialize)]
486pub struct PdfDocumentAttributes {
487    pub title: Option<String>,
488    pub author: Option<String>,
489    pub subject: Option<String>,
490    pub creator: Option<String>,
491    pub producer: Option<String>,
492    pub creation_date: Option<String>,
493    pub modification_date: Option<String>,
494    pub keywords: Option<Vec<String>>,
495}
496
497#[derive(Debug, Clone, Default, PartialEq, Serialize)]
498pub struct PdfPageImageInitializationOptions {
499    pub media_box: Option<PdfRect>,
500    pub rotation: Option<i32>,
501    pub upscale_if_smaller: bool,
502    pub compression_quality: Option<f64>,
503}
504
505impl PdfPageImageInitializationOptions {
506    pub fn with_media_box(mut self, value: PdfRect) -> Self {
507        self.media_box = Some(value);
508        self
509    }
510
511    pub fn with_rotation(mut self, value: i32) -> Self {
512        self.rotation = Some(value);
513        self
514    }
515
516    pub fn with_upscale_if_smaller(mut self, value: bool) -> Self {
517        self.upscale_if_smaller = value;
518        self
519    }
520
521    pub fn with_compression_quality(mut self, value: f64) -> Self {
522        self.compression_quality = Some(value);
523        self
524    }
525}
526
527#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
528pub struct PdfDocumentWriteOptions {
529    pub owner_password: Option<String>,
530    pub user_password: Option<String>,
531    pub access_permissions: Option<u64>,
532    pub burn_in_annotations: bool,
533    pub save_text_from_ocr: bool,
534    pub save_images_as_jpeg: bool,
535    pub optimize_images_for_screen: bool,
536}
537
538impl PdfDocumentWriteOptions {
539    pub fn with_owner_password(mut self, value: impl Into<String>) -> Self {
540        self.owner_password = Some(value.into());
541        self
542    }
543
544    pub fn with_user_password(mut self, value: impl Into<String>) -> Self {
545        self.user_password = Some(value.into());
546        self
547    }
548
549    pub fn with_access_permissions(mut self, value: u64) -> Self {
550        self.access_permissions = Some(value);
551        self
552    }
553
554    pub fn with_burn_in_annotations(mut self, value: bool) -> Self {
555        self.burn_in_annotations = value;
556        self
557    }
558
559    pub fn with_save_text_from_ocr(mut self, value: bool) -> Self {
560        self.save_text_from_ocr = value;
561        self
562    }
563
564    pub fn with_save_images_as_jpeg(mut self, value: bool) -> Self {
565        self.save_images_as_jpeg = value;
566        self
567    }
568
569    pub fn with_optimize_images_for_screen(mut self, value: bool) -> Self {
570        self.optimize_images_for_screen = value;
571        self
572    }
573}
574
575#[derive(Debug, Clone, Deserialize)]
576pub struct PdfBorderInfo {
577    pub style: i32,
578    pub line_width: f64,
579    pub dash_pattern: Option<Vec<f64>>,
580}
581
582impl PdfBorderInfo {
583    #[must_use]
584    pub fn style_enum(&self) -> Option<PdfBorderStyle> {
585        PdfBorderStyle::from_raw(self.style)
586    }
587}
588
589#[derive(Debug, Clone, Deserialize)]
590pub struct PdfAnnotationInfo {
591    pub annotation_type: Option<String>,
592    pub bounds: PdfRect,
593    pub contents: Option<String>,
594    pub should_display: bool,
595    pub should_print: bool,
596    pub has_appearance_stream: bool,
597    pub user_name: Option<String>,
598    pub modification_date: Option<String>,
599    pub color: Option<PdfColor>,
600    pub highlighted: bool,
601    pub action_type: Option<String>,
602    pub border: Option<PdfBorderInfo>,
603}
604
605#[derive(Debug, Clone, Deserialize)]
606pub struct PdfDestinationInfo {
607    pub page_label: Option<String>,
608    pub page_index: Option<usize>,
609    pub point: PdfPoint,
610    pub zoom: f64,
611}
612
613#[derive(Debug, Clone, Deserialize)]
614pub struct PdfAppearanceCharacteristicsInfo {
615    pub control_type: i32,
616    pub background_color: Option<PdfColor>,
617    pub border_color: Option<PdfColor>,
618    pub rotation: i32,
619    pub caption: Option<String>,
620    pub rollover_caption: Option<String>,
621    pub down_caption: Option<String>,
622}
623
624impl PdfAppearanceCharacteristicsInfo {
625    #[must_use]
626    pub fn control_type_enum(&self) -> Option<PdfWidgetControlType> {
627        PdfWidgetControlType::from_raw(self.control_type)
628    }
629}
630
631#[derive(Debug, Clone, Deserialize)]
632pub struct PdfViewInfo {
633    pub display_mode: i32,
634    pub display_direction: i32,
635    pub displays_page_breaks: bool,
636    pub page_break_margins: PdfEdgeInsets,
637    pub display_box: i32,
638    pub displays_as_book: bool,
639    pub displays_rtl: bool,
640    pub background_color: Option<PdfColor>,
641    pub interpolation_quality: i32,
642    pub page_shadows_enabled: bool,
643    pub scale_factor: f64,
644    pub min_scale_factor: f64,
645    pub max_scale_factor: f64,
646    pub auto_scales: bool,
647    pub scale_factor_for_size_to_fit: f64,
648    pub in_markup_mode: bool,
649    pub has_document_view: bool,
650    pub visible_page_count: usize,
651    pub current_page_label: Option<String>,
652}
653
654impl PdfViewInfo {
655    #[must_use]
656    pub fn display_mode_enum(&self) -> Option<PdfDisplayMode> {
657        PdfDisplayMode::from_raw(self.display_mode)
658    }
659
660    #[must_use]
661    pub fn display_direction_enum(&self) -> Option<PdfDisplayDirection> {
662        PdfDisplayDirection::from_raw(self.display_direction)
663    }
664
665    #[must_use]
666    pub fn interpolation_quality_enum(&self) -> Option<PdfInterpolationQuality> {
667        PdfInterpolationQuality::from_raw(self.interpolation_quality)
668    }
669
670    #[must_use]
671    pub fn display_box_enum(&self) -> Option<DisplayBox> {
672        match self.display_box {
673            0 => Some(DisplayBox::MediaBox),
674            1 => Some(DisplayBox::CropBox),
675            2 => Some(DisplayBox::BleedBox),
676            3 => Some(DisplayBox::TrimBox),
677            4 => Some(DisplayBox::ArtBox),
678            _ => None,
679        }
680    }
681}
682
683#[derive(Debug, Clone, Copy, PartialEq, Eq)]
684#[repr(i32)]
685pub enum PdfThumbnailLayoutMode {
686    Vertical = 0,
687    Horizontal = 1,
688}
689
690impl PdfThumbnailLayoutMode {
691    #[must_use]
692    pub const fn as_raw(self) -> i32 {
693        self as i32
694    }
695
696    #[must_use]
697    pub const fn from_raw(raw: i32) -> Option<Self> {
698        match raw {
699            0 => Some(Self::Vertical),
700            1 => Some(Self::Horizontal),
701            _ => None,
702        }
703    }
704}
705
706#[derive(Debug, Clone, Deserialize)]
707pub struct PdfThumbnailViewInfo {
708    pub has_pdf_view: bool,
709    pub background_color: Option<PdfColor>,
710    pub selected_pages_count: usize,
711    pub thumbnail_size: PdfSize,
712    pub maximum_number_of_columns: usize,
713    pub allows_dragging: bool,
714    pub allows_multiple_selection: bool,
715}