pdfium-render 0.9.0

A high-level idiomatic Rust wrapper around Pdfium, the C++ PDF library used by the Google Chromium project.
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
pub(crate) mod internal {
    // We want to make the PdfPageAnnotationPrivate trait private while providing a blanket
    // implementation of PdfPageAnnotationCommon for any type T where T: PdfPageAnnotationPrivate.
    // Rust complains, however, that by doing so we are leaking the private trait outside
    // the crate.

    // Instead of making the PdfPageAnnotationPrivate trait private, we leave it public but place it
    // inside this pub(crate) module in order to prevent it from being visible outside the crate.

    use crate::bindgen::{
        FPDFANNOT_COLORTYPE_FPDFANNOT_COLORTYPE_Color,
        FPDFANNOT_COLORTYPE_FPDFANNOT_COLORTYPE_InteriorColor, FPDF_ANNOTATION,
        FPDF_ANNOT_FLAG_HIDDEN, FPDF_ANNOT_FLAG_INVISIBLE, FPDF_ANNOT_FLAG_LOCKED,
        FPDF_ANNOT_FLAG_NONE, FPDF_ANNOT_FLAG_NOROTATE, FPDF_ANNOT_FLAG_NOVIEW,
        FPDF_ANNOT_FLAG_NOZOOM, FPDF_ANNOT_FLAG_PRINT, FPDF_ANNOT_FLAG_READONLY,
        FPDF_ANNOT_FLAG_TOGGLENOVIEW, FPDF_OBJECT_STRING, FPDF_PAGEOBJECT, FPDF_WCHAR, FS_RECTF,
    };
    use crate::error::{PdfiumError, PdfiumInternalError};
    use crate::pdf::color::PdfColor;
    use crate::pdf::document::page::annotation::attachment_points::PdfPageAnnotationAttachmentPoints;
    use crate::pdf::document::page::annotation::objects::PdfPageAnnotationObjects;
    use crate::pdf::document::page::annotation::{PdfPageAnnotationCommon, PdfPageAnnotationType};
    use crate::pdf::document::page::object::ownership::PdfPageObjectOwnership;
    use crate::pdf::points::PdfPoints;
    use crate::pdf::rect::PdfRect;
    use crate::pdfium::PdfiumLibraryBindingsAccessor;
    use crate::utils::dates::date_time_to_pdf_string;
    use crate::utils::mem::create_byte_buffer;
    use crate::utils::utf16le::get_string_from_pdfium_utf16le_bytes;
    use bitflags::bitflags;
    use chrono::{DateTime, Utc};
    use std::os::raw::{c_int, c_uint};

    bitflags! {
        /// Flags specifying various characteristics of one annotation. For more details,
        /// refer to Section 8.4.2 of The PDF Reference (Sixth Edition, PDF Format 1.7),
        /// starting on page 608.
        pub struct PdfAnnotationFlags: u32 {
            /// No flags are set for this annotation.
            const None = FPDF_ANNOT_FLAG_NONE;

            /// If set, do not display the annotation if it does not belong to one of the
            /// standard annotation types and no annotation handler is available. If clear,
            /// display such an unknown annotation using an appearance stream specified by
            /// its appearance dictionary, if any.
            const Invisible = FPDF_ANNOT_FLAG_INVISIBLE;

            /// If set, do not display or print the annotation or allow it to interact
            /// with the user, regardless of its annotation type or whether an annotation
            /// handler is available.
            ///
            /// In cases where screen space is limited, the ability to hide and show annotations
            /// selectively can be used in combination with appearance streams to display
            /// auxiliary pop-up information, similar in function to online help systems.
            ///
            /// This flag was added in PDF version 1.2.
            const Hidden = FPDF_ANNOT_FLAG_HIDDEN;

            /// If set, print the annotation when the page is printed. If clear, never
            /// print the annotation, regardless of whether it is displayed on the screen.
            ///
            /// This can be useful, for example, for annotations representing interactive
            /// push buttons, which would serve no meaningful purpose on the printed page.
            ///
            /// This flag was added in PDF version 1.2.
            const Print = FPDF_ANNOT_FLAG_PRINT;

            /// If set, do not scale the annotation's appearance to match the magnification
            /// of the page. The location of the annotation on the page (defined by
            /// the upper-left corner of its annotation rectangle) remains fixed,
            /// regardless of the page magnification.
            ///
            /// This flag was added in PDF version 1.3.
            const NoZoom = FPDF_ANNOT_FLAG_NOZOOM;

            /// If set, do not rotate the annotation's appearance to match the rotation
            /// of the page. The upper-left corner of the annotation rectangle remains in
            /// a fixed location on the page, regardless of the page rotation.
            ///
            /// This flag was added in PDF version 1.3.
            const NoRotate = FPDF_ANNOT_FLAG_NOROTATE;

            /// If set, do not display the annotation on the screen or allow it to
            /// interact with the user. The annotation may be printed (depending on the
            /// setting of the Print flag) but should be considered hidden for purposes
            /// of on-screen display and user interaction.
            ///
            /// This flag was added in PDF version 1.3.
            const NoView = FPDF_ANNOT_FLAG_NOVIEW;

            /// If set, do not allow the annotation to interact with the user. The
            /// annotation may be displayed or printed (depending on the settings of the
            /// `NoView` and `Print` flags) but should not respond to mouse clicks or
            /// change its appearance in response to mouse motions.
            ///
            /// This flag is ignored for widget annotations; its function is subsumed by
            /// the `ReadOnly` flag of the associated form field.
            ///
            /// THis flag was added in PDF version 1.3.
            const ReadOnly = FPDF_ANNOT_FLAG_READONLY;

            /// If set, do not allow the annotation to be deleted or its properties
            /// (including position and size) to be modified by the user. However, this flag
            /// does not restrict changes to the annotation's contents, such as the value
            /// of a form field.
            ///
            /// THis flag was added in PDF version 1.4.
            const Locked = FPDF_ANNOT_FLAG_LOCKED;

            /// If set, invert the interpretation of the `NoView` flag for certain
            /// events. A typical use is to have an annotation that appears only when a
            /// mouse cursor is held over it.
            ///
            /// This flag was added in PDF version 1.5.
            const ToggleNoView = FPDF_ANNOT_FLAG_TOGGLENOVIEW;

            /// If set, do not allow the contents of the annotation to be modified by
            /// the user. This flag does not restrict deletion of the annotation or
            /// changes to other annotation properties, such as position and size.
            ///
            /// This flag was added in PDF version 1.7.
            const LockedContents = 1 << 10; // Not directly exposed by Pdfium, but we can support it inline.
        }
    }

    /// Internal crate-specific functionality common to all [PdfPageAnnotation] objects.
    pub(crate) trait PdfPageAnnotationPrivate<'a>:
        PdfPageAnnotationCommon + PdfiumLibraryBindingsAccessor<'a>
    {
        /// Returns the internal `FPDF_ANNOTATION` handle for this [PdfPageAnnotation].
        fn handle(&self) -> FPDF_ANNOTATION;

        /// Returns the ownership hierarchy for this [PdfPageAnnotation].
        fn ownership(&self) -> &PdfPageObjectOwnership;

        /// Returns the [PdfPageAnnotationType] of this [PdfPageAnnotation].
        fn get_annotation_type(&self) -> PdfPageAnnotationType {
            PdfPageAnnotationType::from_pdfium(unsafe {
                self.bindings().FPDFAnnot_GetSubtype(self.handle())
            })
            .unwrap_or(PdfPageAnnotationType::Unknown)
        }

        /// Returns the string value associated with the given key in the annotation dictionary
        /// of this [PdfPageAnnotation], if any.
        fn get_string_value(&self, key: &str) -> Option<String> {
            if !self
                .bindings()
                .is_true(unsafe { self.bindings().FPDFAnnot_HasKey(self.handle(), key) })
            {
                // The key does not exist.

                return None;
            }

            if (unsafe { self.bindings().FPDFAnnot_GetValueType(self.handle(), key) }) as u32
                != FPDF_OBJECT_STRING
            {
                // The key exists, but the value associated with the key is not a string.

                return None;
            }

            // Retrieving the string value from Pdfium is a two-step operation. First, we call
            // FPDFAnot_GetStringValue() with a null buffer; this will retrieve the length of
            // the value in bytes, assuming the key exists. If the length is zero, then there
            // is no such key, or the key's value is not a string.

            // If the length is non-zero, then we reserve a byte buffer of the given
            // length and call FPDFAnot_GetStringValue() again with a pointer to the buffer;
            // this will write the string value into the buffer.

            let buffer_length = unsafe {
                self.bindings().FPDFAnnot_GetStringValue(
                    self.handle(),
                    key,
                    std::ptr::null_mut(),
                    0,
                )
            };

            if buffer_length <= 2 {
                // A buffer length of 2 indicates that the string value for the given key is
                // an empty UTF16-LE string, so there is no point in retrieving it.

                return None;
            }

            let mut buffer = create_byte_buffer(buffer_length as usize);

            let result = unsafe {
                self.bindings().FPDFAnnot_GetStringValue(
                    self.handle(),
                    key,
                    buffer.as_mut_ptr() as *mut FPDF_WCHAR,
                    buffer_length,
                )
            };

            assert_eq!(result, buffer_length);

            Some(get_string_from_pdfium_utf16le_bytes(buffer).unwrap_or_default())
        }

        /// Sets the string value associated with the given key in the annotation dictionary
        /// of this [PdfPageAnnotation].
        fn set_string_value(&mut self, key: &str, value: &str) -> Result<(), PdfiumError> {
            // Attempt to update the modification date first, before we apply the given value update.
            // That way, if updating the date fails, we can fail early.

            #[allow(clippy::collapsible_if)] // Prefer to keep the intent clear
            if key != "M"
            // Don't update the modification date if the key we have been given to update
            // is itself the modification date!
            {
                self.set_string_value("M", &date_time_to_pdf_string(Utc::now()))?;
            }

            // With the modification date updated, we can now update the key and value
            // we were given.

            if self.bindings().is_true(unsafe {
                self.bindings()
                    .FPDFAnnot_SetStringValue_str(self.handle(), key, value)
            }) {
                Ok(())
            } else {
                Err(PdfiumError::PdfiumLibraryInternalError(
                    PdfiumInternalError::Unknown,
                ))
            }
        }

        /// Internal implementation of [PdfPageAnnotationCommon::name()].
        #[inline]
        fn name_impl(&self) -> Option<String> {
            self.get_string_value("NM")
        }

        /// Internal implementation of [PdfPageAnnotationCommon::bounds()].
        #[inline]
        fn bounds_impl(&self) -> Result<PdfRect, PdfiumError> {
            let mut rect = FS_RECTF {
                left: 0_f32,
                bottom: 0_f32,
                right: 0_f32,
                top: 0_f32,
            };

            let result = unsafe { self.bindings().FPDFAnnot_GetRect(self.handle(), &mut rect) };

            PdfRect::from_pdfium_as_result(result, rect, self.bindings())
        }

        /// Internal implementation of [PdfPageAnnotationCommon::set_bounds()].
        #[inline]
        fn set_bounds_impl(&mut self, bounds: PdfRect) -> Result<(), PdfiumError> {
            if self.bindings().is_true(unsafe {
                self.bindings()
                    .FPDFAnnot_SetRect(self.handle(), &bounds.as_pdfium())
            }) {
                self.set_string_value("M", &date_time_to_pdf_string(Utc::now()))
            } else {
                Err(PdfiumError::PdfiumLibraryInternalError(
                    PdfiumInternalError::Unknown,
                ))
            }
        }

        /// Internal implementation of [PdfPageAnnotationCommon::set_position()].
        fn set_position_impl(&mut self, x: PdfPoints, y: PdfPoints) -> Result<(), PdfiumError> {
            let bounds = self
                .bounds()
                .unwrap_or(PdfRect::new_from_values(0.0, 0.0, 1.0, 1.0));

            let width = bounds.width();

            let height = bounds.height();

            self.set_bounds(PdfRect::new(y, x, y + height, x + width))
        }

        /// Internal implementation of [PdfPageAnnotationCommon::set_width()].
        fn set_width_impl(&mut self, width: PdfPoints) -> Result<(), PdfiumError> {
            let bounds = self
                .bounds()
                .unwrap_or(PdfRect::new_from_values(0.0, 0.0, 1.0, 1.0));

            let height = bounds.height();

            self.set_bounds(PdfRect::new(
                bounds.bottom(),
                bounds.left(),
                bounds.bottom() + height,
                bounds.left() + width,
            ))
        }

        /// Internal implementation of [PdfPageAnnotationCommon::set_height()].
        fn set_height_impl(&mut self, height: PdfPoints) -> Result<(), PdfiumError> {
            let bounds = self
                .bounds()
                .unwrap_or(PdfRect::new_from_values(0.0, 0.0, 1.0, 1.0));

            let width = bounds.width();

            self.set_bounds(PdfRect::new(
                bounds.bottom(),
                bounds.left(),
                bounds.bottom() + height,
                bounds.left() + width,
            ))
        }

        /// Internal implementation of [PdfPageAnnotationCommon::contents()].
        #[inline]
        fn contents_impl(&self) -> Option<String> {
            self.get_string_value("Contents")
        }

        /// Internal implementation of [PdfPageAnnotationCommon::set_contents()].
        #[inline]
        fn set_contents_impl(&mut self, contents: &str) -> Result<(), PdfiumError> {
            self.set_string_value("Contents", contents)
        }

        /// Internal implementation of [PdfPageAnnotationCommon::creator()].
        #[inline]
        fn creator_impl(&self) -> Option<String> {
            self.get_string_value("T")
        }

        /// Internal implementation of [PdfPageAnnotationCommon::set_creator()].
        #[inline]
        fn set_creator_impl(&mut self, creator: &str) -> Result<(), PdfiumError> {
            self.set_string_value("T", creator)
        }

        /// Internal implementation of [PdfPageAnnotationCommon::creation_date()].
        #[inline]
        fn creation_date_impl(&self) -> Option<String> {
            self.get_string_value("CreationDate")
        }

        /// Internal implementation of [PdfPageAnnotationCommon::set_creation_date()].
        #[inline]
        fn set_creation_date_impl(&mut self, date: DateTime<Utc>) -> Result<(), PdfiumError> {
            self.set_string_value("CreationDate", &date_time_to_pdf_string(date))
        }

        /// Internal implementation of [PdfPageAnnotationCommon::modification_date()].
        #[inline]
        fn modification_date_impl(&self) -> Option<String> {
            self.get_string_value("M")
        }

        /// Internal implementation of [PdfPageAnnotationCommon::set_modification_date()].
        #[inline]
        fn set_modification_date_impl(&mut self, date: DateTime<Utc>) -> Result<(), PdfiumError> {
            self.set_string_value("M", &date_time_to_pdf_string(date))
        }

        /// Internal implementation of [PdfPageAnnotationCommon::is_markup_annotation()].
        #[inline]
        fn is_markup_annotation_impl(&self) -> bool {
            // We take advantage of the fact that all markup annotations support attachment points,
            // and the only type of annotation (other than markup annotations) that supports
            // attachment points is the Link annotation.

            self.has_attachment_points_impl()
                && self.get_annotation_type() != PdfPageAnnotationType::Link
        }

        /// Internal implementation of [PdfPageAnnotationCommon::has_attachment_points()].
        #[inline]
        fn has_attachment_points_impl(&self) -> bool {
            self.bindings()
                .is_true(unsafe { self.bindings().FPDFAnnot_HasAttachmentPoints(self.handle()) })
        }

        /// Internal implementation of [PdfPageAnnotationCommon::fill_color()].
        #[inline]
        fn fill_color_impl(&self) -> Result<PdfColor, PdfiumError> {
            let mut r: c_uint = 0;

            let mut g: c_uint = 0;

            let mut b: c_uint = 0;

            let mut a: c_uint = 0;

            if self.bindings().is_true(unsafe {
                self.bindings().FPDFAnnot_GetColor(
                    self.handle(),
                    FPDFANNOT_COLORTYPE_FPDFANNOT_COLORTYPE_InteriorColor,
                    &mut r,
                    &mut g,
                    &mut b,
                    &mut a,
                )
            }) {
                Ok(PdfColor::new(r as u8, g as u8, b as u8, a as u8))
            } else {
                // The FPDFAnnot_GetColor() function returns false if the annotation
                // is using appearance streams. In this case, the Pdfium documentation
                // states that we must use FPDFPath_GetFillColor() instead; that function
                // is deprecated, and says to use FPDFPageObj_GetFillColor().

                if self.bindings().is_true(unsafe {
                    self.bindings().FPDFPageObj_GetFillColor(
                        self.handle() as FPDF_PAGEOBJECT,
                        &mut r,
                        &mut g,
                        &mut b,
                        &mut a,
                    )
                }) {
                    Ok(PdfColor::new(r as u8, g as u8, b as u8, a as u8))
                } else {
                    Err(PdfiumError::PdfiumLibraryInternalError(
                        PdfiumInternalError::Unknown,
                    ))
                }
            }
        }

        /// Internal implementation of [PdfPageAnnotationCommon::set_fill_color()].
        #[inline]
        fn set_fill_color_impl(&mut self, fill_color: PdfColor) -> Result<(), PdfiumError> {
            if self.bindings().is_true(unsafe {
                self.bindings().FPDFAnnot_SetColor(
                    self.handle(),
                    FPDFANNOT_COLORTYPE_FPDFANNOT_COLORTYPE_InteriorColor,
                    fill_color.red() as c_uint,
                    fill_color.green() as c_uint,
                    fill_color.blue() as c_uint,
                    fill_color.alpha() as c_uint,
                )
            }) {
                Ok(())
            } else {
                // The FPDFAnnot_SetColor() function returns false if the annotation
                // is using appearance streams. In this case, the Pdfium documentation
                // states that we must use FPDFPath_SetFillColor() instead; that function
                // is deprecated, and says to use FPDFPageObj_SetFillColor().

                if self.bindings().is_true(unsafe {
                    self.bindings().FPDFPageObj_SetFillColor(
                        self.handle() as FPDF_PAGEOBJECT,
                        fill_color.red() as c_uint,
                        fill_color.green() as c_uint,
                        fill_color.blue() as c_uint,
                        fill_color.alpha() as c_uint,
                    )
                }) {
                    Ok(())
                } else {
                    Err(PdfiumError::PdfiumLibraryInternalError(
                        PdfiumInternalError::Unknown,
                    ))
                }
            }
        }

        /// Internal implementation of [PdfPageAnnotationCommon::stroke_color()].
        #[inline]
        fn stroke_color_impl(&self) -> Result<PdfColor, PdfiumError> {
            let mut r: c_uint = 0;

            let mut g: c_uint = 0;

            let mut b: c_uint = 0;

            let mut a: c_uint = 0;

            if self.bindings().is_true(unsafe {
                self.bindings().FPDFAnnot_GetColor(
                    self.handle(),
                    FPDFANNOT_COLORTYPE_FPDFANNOT_COLORTYPE_Color,
                    &mut r,
                    &mut g,
                    &mut b,
                    &mut a,
                )
            }) {
                Ok(PdfColor::new(r as u8, g as u8, b as u8, a as u8))
            } else {
                // The FPDFAnnot_GetColor() function returns false if the annotation
                // is using appearance streams. In this case, the Pdfium documentation
                // states that we must use FPDFPath_GetStrokeColor() instead; that function
                // is deprecated, and says to use FPDFPageObj_GetStrokeColor().

                if self.bindings().is_true(unsafe {
                    self.bindings().FPDFPageObj_GetStrokeColor(
                        self.handle() as FPDF_PAGEOBJECT,
                        &mut r,
                        &mut g,
                        &mut b,
                        &mut a,
                    )
                }) {
                    Ok(PdfColor::new(r as u8, g as u8, b as u8, a as u8))
                } else {
                    Err(PdfiumError::PdfiumLibraryInternalError(
                        PdfiumInternalError::Unknown,
                    ))
                }
            }
        }

        /// Internal implementation of [PdfPageAnnotationCommon::set_stroke_color()].
        #[inline]
        fn set_stroke_color_impl(&mut self, stroke_color: PdfColor) -> Result<(), PdfiumError> {
            if self.bindings().is_true(unsafe {
                self.bindings().FPDFAnnot_SetColor(
                    self.handle(),
                    FPDFANNOT_COLORTYPE_FPDFANNOT_COLORTYPE_Color,
                    stroke_color.red() as c_uint,
                    stroke_color.green() as c_uint,
                    stroke_color.blue() as c_uint,
                    stroke_color.alpha() as c_uint,
                )
            }) {
                Ok(())
            } else {
                // The FPDFAnnot_SetColor() function returns false if the annotation
                // is using appearance streams. In this case, the Pdfium documentation
                // states that we must use FPDFPath_SetStrokeColor() instead; that function
                // is deprecated, and says to use FPDFPageObj_SetStrokeColor().

                if self.bindings().is_true(unsafe {
                    self.bindings().FPDFPageObj_SetStrokeColor(
                        self.handle() as FPDF_PAGEOBJECT,
                        stroke_color.red() as c_uint,
                        stroke_color.green() as c_uint,
                        stroke_color.blue() as c_uint,
                        stroke_color.alpha() as c_uint,
                    )
                }) {
                    Ok(())
                } else {
                    Err(PdfiumError::PdfiumLibraryInternalError(
                        PdfiumInternalError::Unknown,
                    ))
                }
            }
        }

        /// Returns all the flags currently set on this annotation.
        #[inline]
        fn get_flags_impl(&self) -> PdfAnnotationFlags {
            PdfAnnotationFlags::from_bits_truncate(
                (unsafe { self.bindings().FPDFAnnot_GetFlags(self.handle()) }) as u32,
            )
        }

        /// Sets all the flags on this annotation.
        #[inline]
        fn set_flags_impl(&mut self, flags: PdfAnnotationFlags) -> bool {
            self.bindings().is_true(unsafe {
                self.bindings()
                    .FPDFAnnot_SetFlags(self.handle(), flags.bits() as c_int)
            })
        }

        /// Sets or clears a single flag on this annotation.
        fn update_one_flag_impl(
            &mut self,
            flag: PdfAnnotationFlags,
            value: bool,
        ) -> Result<(), PdfiumError> {
            let mut flags = self.get_flags_impl();

            flags.set(flag, value);

            if self.set_flags_impl(flags) {
                Ok(())
            } else {
                Err(PdfiumError::PdfiumLibraryInternalError(
                    crate::error::PdfiumInternalError::Unknown,
                ))
            }
        }

        /// Internal implementation of [PdfPageAnnotationCommon::objects()].
        fn objects_impl(&self) -> &PdfPageAnnotationObjects<'_>;

        /// Internal implementation of [PdfPageAnnotationCommon::attachment_points()].
        fn attachment_points_impl(&self) -> &PdfPageAnnotationAttachmentPoints<'_>;
    }
}

#[cfg(test)]
mod tests {
    use crate::pdf::document::page::annotation::private::internal::{
        PdfAnnotationFlags, PdfPageAnnotationPrivate,
    };
    use crate::prelude::*;
    use crate::utils::test::test_bind_to_pdfium;

    #[test]
    fn test_get_annotation_flags() -> Result<(), PdfiumError> {
        let pdfium = test_bind_to_pdfium();
        let document = pdfium.load_pdf_from_file("test/form-test.pdf", None)?;
        let page = document.pages().first()?;
        let annotation = page
            .annotations()
            .iter()
            .find(|annotation| annotation.as_form_field().is_some())
            .unwrap();
        let widget = annotation.as_widget_annotation().unwrap();

        let flags = widget.get_flags_impl();

        assert!(!flags.contains(PdfAnnotationFlags::LockedContents));
        assert_eq!(widget.is_editable(), true);

        assert!(!flags.contains(PdfAnnotationFlags::Locked));
        assert_eq!(widget.is_locked(), false);

        assert!(!flags.contains(PdfAnnotationFlags::ReadOnly));
        assert_eq!(widget.is_read_only(), false);

        assert!(flags.contains(PdfAnnotationFlags::Print));
        assert_eq!(widget.is_printed(), true);

        assert!(!flags.contains(PdfAnnotationFlags::Hidden));
        assert_eq!(widget.is_hidden(), false);

        assert!(!flags.contains(PdfAnnotationFlags::Invisible));
        assert_eq!(widget.is_invisible_if_unsupported(), false);

        Ok(())
    }

    #[test]
    fn test_set_annotation_flags() -> Result<(), PdfiumError> {
        let pdfium = test_bind_to_pdfium();
        let mut document = pdfium.load_pdf_from_file("test/form-test.pdf", None)?;
        let mut page = document.pages_mut().first()?;
        let mut annotation = page
            .annotations_mut()
            .iter()
            .find(|annotation| annotation.as_form_field().is_some())
            .unwrap();
        let widget = annotation.as_widget_annotation_mut().unwrap();

        assert_eq!(widget.is_editable(), true);
        assert_eq!(widget.is_locked(), false);
        assert_eq!(widget.is_read_only(), false);
        assert_eq!(widget.is_printed(), true);
        assert_eq!(widget.is_hidden(), false);
        assert_eq!(widget.is_invisible_if_unsupported(), false);

        let mut flags = widget.get_flags_impl();

        flags.set(PdfAnnotationFlags::ReadOnly, true);
        flags.set(PdfAnnotationFlags::Locked, true);
        flags.set(PdfAnnotationFlags::LockedContents, true);
        flags.set(PdfAnnotationFlags::Print, false);
        flags.set(PdfAnnotationFlags::Hidden, true);
        flags.set(PdfAnnotationFlags::Invisible, true);

        assert!(widget.set_flags_impl(flags));

        assert_eq!(widget.is_editable(), false);
        assert_eq!(widget.is_locked(), true);
        assert_eq!(widget.is_read_only(), true);
        assert_eq!(widget.is_printed(), false);
        assert_eq!(widget.is_hidden(), true);
        assert_eq!(widget.is_invisible_if_unsupported(), true);

        Ok(())
    }

    #[test]
    fn test_update_one_annotation_flag() -> Result<(), PdfiumError> {
        let pdfium = test_bind_to_pdfium();
        let mut document = pdfium.load_pdf_from_file("test/form-test.pdf", None)?;
        let mut page = document.pages_mut().first()?;
        let mut annotation = page
            .annotations_mut()
            .iter()
            .find(|annotation| annotation.as_form_field().is_some())
            .unwrap();
        let widget = annotation.as_widget_annotation_mut().unwrap();

        assert_eq!(widget.is_editable(), true);
        assert_eq!(widget.is_locked(), false);
        assert_eq!(widget.is_read_only(), false);
        assert_eq!(widget.is_printed(), true);
        assert_eq!(widget.is_hidden(), false);
        assert_eq!(widget.is_invisible_if_unsupported(), false);

        widget.set_is_editable(false)?;
        assert_eq!(widget.is_editable(), false);

        widget.set_is_locked(true)?;
        assert_eq!(widget.is_locked(), true);

        widget.set_is_read_only(true)?;
        assert_eq!(widget.is_read_only(), true);

        widget.set_is_printed(false)?;
        assert_eq!(widget.is_printed(), false);

        widget.set_is_hidden(true)?;
        assert_eq!(widget.is_hidden(), true);

        widget.set_is_invisible_if_unsupported(true)?;
        assert_eq!(widget.is_invisible_if_unsupported(), true);

        assert_eq!(widget.is_editable(), false);
        assert_eq!(widget.is_locked(), true);
        assert_eq!(widget.is_read_only(), true);
        assert_eq!(widget.is_printed(), false);
        assert_eq!(widget.is_hidden(), true);
        assert_eq!(widget.is_invisible_if_unsupported(), true);

        Ok(())
    }
}