Skip to main content

pdfium_render/pdf/document/page/annotation/
widget.rs

1//! Defines the [PdfPageWidgetAnnotation] struct, exposing functionality related to a single
2//! user annotation of type [PdfPageAnnotationType::Widget].
3
4use crate::bindgen::{FPDF_ANNOTATION, FPDF_DOCUMENT, FPDF_FORMHANDLE, FPDF_PAGE};
5use crate::bindings::PdfiumLibraryBindings;
6use crate::pdf::document::page::annotation::attachment_points::PdfPageAnnotationAttachmentPoints;
7use crate::pdf::document::page::annotation::objects::PdfPageAnnotationObjects;
8use crate::pdf::document::page::annotation::private::internal::PdfPageAnnotationPrivate;
9use crate::pdf::document::page::field::PdfFormField;
10use crate::pdf::document::page::object::ownership::PdfPageObjectOwnership;
11use crate::pdf::document::page::objects::private::internal::PdfPageObjectsPrivate;
12use crate::pdfium::PdfiumLibraryBindingsAccessor;
13use std::marker::PhantomData;
14
15#[cfg(doc)]
16use crate::pdf::document::page::annotation::{PdfPageAnnotation, PdfPageAnnotationType};
17
18/// A single [PdfPageAnnotation] of type [PdfPageAnnotationType::Widget].
19///
20/// Widget annotation types can wrap form fields. To access the form field, use the
21/// [PdfPageWidgetAnnotation::form_field()] function.
22pub struct PdfPageWidgetAnnotation<'a> {
23    annotation_handle: FPDF_ANNOTATION,
24    objects: PdfPageAnnotationObjects<'a>,
25    attachment_points: PdfPageAnnotationAttachmentPoints<'a>,
26    form_field: Option<PdfFormField<'a>>,
27    lifetime: PhantomData<&'a FPDF_ANNOTATION>,
28}
29
30impl<'a> PdfPageWidgetAnnotation<'a> {
31    pub(crate) fn from_pdfium(
32        document_handle: FPDF_DOCUMENT,
33        page_handle: FPDF_PAGE,
34        annotation_handle: FPDF_ANNOTATION,
35        form_handle: Option<FPDF_FORMHANDLE>,
36        bindings: &'a dyn PdfiumLibraryBindings,
37    ) -> Self {
38        PdfPageWidgetAnnotation {
39            annotation_handle,
40            objects: PdfPageAnnotationObjects::from_pdfium(
41                document_handle,
42                page_handle,
43                annotation_handle,
44            ),
45            attachment_points: PdfPageAnnotationAttachmentPoints::from_pdfium(annotation_handle),
46            form_field: form_handle.and_then(|form_handle| {
47                PdfFormField::from_pdfium(form_handle, annotation_handle, bindings)
48            }),
49            lifetime: PhantomData,
50        }
51    }
52
53    /// Returns an immutable reference to the [PdfFormField] wrapped by this [PdfPageWidgetAnnotation],
54    /// if any.
55    #[inline]
56    pub fn form_field(&self) -> Option<&PdfFormField<'_>> {
57        self.form_field.as_ref()
58    }
59
60    /// Returns a mutable reference to the [PdfFormField] wrapped by this [PdfPageWidgetAnnotation],
61    /// if any.
62    #[inline]
63    pub fn form_field_mut(&mut self) -> Option<&mut PdfFormField<'a>> {
64        self.form_field.as_mut()
65    }
66}
67
68impl<'a> PdfPageAnnotationPrivate<'a> for PdfPageWidgetAnnotation<'a> {
69    #[inline]
70    fn handle(&self) -> FPDF_ANNOTATION {
71        self.annotation_handle
72    }
73
74    #[inline]
75    fn ownership(&self) -> &PdfPageObjectOwnership {
76        self.objects_impl().ownership()
77    }
78
79    #[inline]
80    fn objects_impl(&self) -> &PdfPageAnnotationObjects<'_> {
81        &self.objects
82    }
83
84    #[inline]
85    fn attachment_points_impl(&self) -> &PdfPageAnnotationAttachmentPoints<'_> {
86        &self.attachment_points
87    }
88}
89
90impl<'a> PdfiumLibraryBindingsAccessor<'a> for PdfPageWidgetAnnotation<'a> {}
91
92#[cfg(feature = "thread_safe")]
93unsafe impl<'a> Send for PdfPageWidgetAnnotation<'a> {}
94
95#[cfg(feature = "thread_safe")]
96unsafe impl<'a> Sync for PdfPageWidgetAnnotation<'a> {}