Skip to main content

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

1//! Defines the [PdfPageLinkAnnotation] struct, exposing functionality related to a single
2//! user annotation of type [PdfPageAnnotationType::Link].
3
4use crate::bindgen::{FPDF_ANNOTATION, FPDF_DOCUMENT, FPDF_PAGE};
5use crate::error::{PdfiumError, PdfiumInternalError};
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::object::ownership::PdfPageObjectOwnership;
10use crate::pdf::document::page::objects::private::internal::PdfPageObjectsPrivate;
11use crate::pdf::link::PdfLink;
12use crate::pdfium::PdfiumLibraryBindingsAccessor;
13use std::marker::PhantomData;
14
15#[cfg(doc)]
16use {
17    crate::pdf::document::page::annotation::PdfPageAnnotation,
18    crate::pdf::document::page::annotation::PdfPageAnnotationType,
19};
20
21/// A single [PdfPageAnnotation] of type [PdfPageAnnotationType::Link].
22pub struct PdfPageLinkAnnotation<'a> {
23    handle: FPDF_ANNOTATION,
24    objects: PdfPageAnnotationObjects<'a>,
25    attachment_points: PdfPageAnnotationAttachmentPoints<'a>,
26    lifetime: PhantomData<&'a FPDF_ANNOTATION>,
27}
28
29impl<'a> PdfPageLinkAnnotation<'a> {
30    pub(crate) fn from_pdfium(
31        document_handle: FPDF_DOCUMENT,
32        page_handle: FPDF_PAGE,
33        annotation_handle: FPDF_ANNOTATION,
34    ) -> Self {
35        PdfPageLinkAnnotation {
36            handle: annotation_handle,
37            objects: PdfPageAnnotationObjects::from_pdfium(
38                document_handle,
39                page_handle,
40                annotation_handle,
41            ),
42            attachment_points: PdfPageAnnotationAttachmentPoints::from_pdfium(annotation_handle),
43            lifetime: PhantomData,
44        }
45    }
46
47    /// Returns the [PdfLink] associated with this [PdfPageLinkAnnotation], if any.
48    pub fn link(&self) -> Result<PdfLink<'_>, PdfiumError> {
49        let handle = unsafe { self.bindings().FPDFAnnot_GetLink(self.handle) };
50
51        if handle.is_null() {
52            Err(PdfiumError::PdfiumLibraryInternalError(
53                PdfiumInternalError::Unknown,
54            ))
55        } else {
56            let document_handle = match self.objects.ownership() {
57                PdfPageObjectOwnership::Page(ownership) => Some(ownership.document_handle()),
58                PdfPageObjectOwnership::AttachedAnnotation(ownership) => {
59                    Some(ownership.document_handle())
60                }
61                PdfPageObjectOwnership::UnattachedAnnotation(ownership) => {
62                    Some(ownership.document_handle())
63                }
64                _ => None,
65            };
66
67            if let Some(document_handle) = document_handle {
68                Ok(PdfLink::from_pdfium(handle, document_handle))
69            } else {
70                Err(PdfiumError::OwnershipNotAttachedToDocument)
71            }
72        }
73    }
74
75    /// Sets the [PdfLink] associated with this [PdfPageLinkAnnotation] to the given URI.
76    pub fn set_link(&mut self, uri: &str) -> Result<(), PdfiumError> {
77        if self
78            .bindings()
79            .is_true(unsafe { self.bindings().FPDFAnnot_SetURI(self.handle(), uri) })
80        {
81            Ok(())
82        } else {
83            Err(PdfiumError::PdfiumLibraryInternalError(
84                PdfiumInternalError::Unknown,
85            ))
86        }
87    }
88
89    /// Returns a mutable collection of all the attachment points in this [PdfPageLinkAnnotation].
90    #[inline]
91    pub fn attachment_points_mut(&mut self) -> &mut PdfPageAnnotationAttachmentPoints<'a> {
92        &mut self.attachment_points
93    }
94}
95
96impl<'a> PdfPageAnnotationPrivate<'a> for PdfPageLinkAnnotation<'a> {
97    #[inline]
98    fn handle(&self) -> FPDF_ANNOTATION {
99        self.handle
100    }
101
102    #[inline]
103    fn ownership(&self) -> &PdfPageObjectOwnership {
104        self.objects_impl().ownership()
105    }
106
107    #[inline]
108    fn objects_impl(&self) -> &PdfPageAnnotationObjects<'_> {
109        &self.objects
110    }
111
112    #[inline]
113    fn attachment_points_impl(&self) -> &PdfPageAnnotationAttachmentPoints<'_> {
114        &self.attachment_points
115    }
116}
117
118impl<'a> PdfiumLibraryBindingsAccessor<'a> for PdfPageLinkAnnotation<'a> {}
119
120#[cfg(feature = "thread_safe")]
121unsafe impl<'a> Send for PdfPageLinkAnnotation<'a> {}
122
123#[cfg(feature = "thread_safe")]
124unsafe impl<'a> Sync for PdfPageLinkAnnotation<'a> {}