pdfium_render/pdf/document/page/annotation/
attachment_points.rs1use crate::bindgen::FPDF_ANNOTATION;
6use crate::bindings::PdfiumLibraryBindings;
7use crate::error::{PdfiumError, PdfiumInternalError};
8use crate::pdf::quad_points::PdfQuadPoints;
9use std::ops::{Range, RangeInclusive};
10
11pub type PdfPageAnnotationAttachmentPointIndex = usize;
14
15pub struct PdfPageAnnotationAttachmentPoints<'a> {
18 annotation_handle: FPDF_ANNOTATION,
19 bindings: &'a dyn PdfiumLibraryBindings,
20}
21
22impl<'a> PdfPageAnnotationAttachmentPoints<'a> {
23 #[inline]
24 pub(crate) fn from_pdfium(annotation_handle: FPDF_ANNOTATION, bindings: &'a dyn PdfiumLibraryBindings) -> Self {
25 PdfPageAnnotationAttachmentPoints {
26 annotation_handle,
27 bindings,
28 }
29 }
30
31 pub fn len(&self) -> PdfPageAnnotationAttachmentPointIndex {
33 if self
34 .bindings
35 .is_true(self.bindings.FPDFAnnot_HasAttachmentPoints(self.annotation_handle))
36 {
37 self.bindings.FPDFAnnot_CountAttachmentPoints(self.annotation_handle)
38 as PdfPageAnnotationAttachmentPointIndex
39 } else {
40 0
41 }
42 }
43
44 #[inline]
46 pub fn is_empty(&self) -> bool {
47 self.len() == 0
48 }
49
50 #[inline]
53 pub fn as_range(&self) -> Range<PdfPageAnnotationAttachmentPointIndex> {
54 0..self.len()
55 }
56
57 #[inline]
60 pub fn as_range_inclusive(&self) -> RangeInclusive<PdfPageAnnotationAttachmentPointIndex> {
61 if self.is_empty() { 0..=0 } else { 0..=(self.len() - 1) }
62 }
63
64 pub fn get(&self, index: PdfPageAnnotationAttachmentPointIndex) -> Result<PdfQuadPoints, PdfiumError> {
67 if index >= self.len() {
68 return Err(PdfiumError::PageAnnotationAttachmentPointIndexOutOfBounds);
69 }
70
71 let mut result = PdfQuadPoints::ZERO.as_pdfium();
72
73 if self.bindings.is_true(self.bindings.FPDFAnnot_GetAttachmentPoints(
74 self.annotation_handle,
75 index,
76 &mut result,
77 )) {
78 Ok(PdfQuadPoints::from_pdfium(result))
79 } else {
80 Err(PdfiumError::PdfiumLibraryInternalError(PdfiumInternalError::Unknown))
81 }
82 }
83
84 #[inline]
87 pub fn first(&self) -> Result<PdfQuadPoints, PdfiumError> {
88 if !self.is_empty() {
89 self.get(0)
90 } else {
91 Err(PdfiumError::NoAttachmentPointsInPageAnnotation)
92 }
93 }
94
95 #[inline]
98 pub fn last(&self) -> Result<PdfQuadPoints, PdfiumError> {
99 if !self.is_empty() {
100 self.get(self.len() - 1)
101 } else {
102 Err(PdfiumError::NoAttachmentPointsInPageAnnotation)
103 }
104 }
105
106 #[inline]
109 pub fn create_attachment_point_at_end(&mut self, attachment_point: PdfQuadPoints) -> Result<(), PdfiumError> {
110 if self.bindings.is_true(
111 self.bindings
112 .FPDFAnnot_AppendAttachmentPoints(self.annotation_handle, &attachment_point.as_pdfium()),
113 ) {
114 Ok(())
115 } else {
116 Err(PdfiumError::PdfiumLibraryInternalError(PdfiumInternalError::Unknown))
117 }
118 }
119
120 pub fn set_attachment_point_at_index(
123 &mut self,
124 index: PdfPageAnnotationAttachmentPointIndex,
125 attachment_point: PdfQuadPoints,
126 ) -> Result<(), PdfiumError> {
127 if self.bindings.is_true(self.bindings.FPDFAnnot_SetAttachmentPoints(
128 self.annotation_handle,
129 index,
130 &attachment_point.as_pdfium(),
131 )) {
132 Ok(())
133 } else {
134 Err(PdfiumError::PdfiumLibraryInternalError(PdfiumInternalError::Unknown))
135 }
136 }
137
138 #[inline]
140 pub fn iter(&self) -> PdfPageAnnotationAttachmentPointsIterator<'_> {
141 PdfPageAnnotationAttachmentPointsIterator::new(self)
142 }
143}
144
145pub struct PdfPageAnnotationAttachmentPointsIterator<'a> {
147 attachment_points: &'a PdfPageAnnotationAttachmentPoints<'a>,
148 next_index: PdfPageAnnotationAttachmentPointIndex,
149}
150
151impl<'a> PdfPageAnnotationAttachmentPointsIterator<'a> {
152 #[inline]
153 pub(crate) fn new(attachment_points: &'a PdfPageAnnotationAttachmentPoints<'a>) -> Self {
154 PdfPageAnnotationAttachmentPointsIterator {
155 attachment_points,
156 next_index: 0,
157 }
158 }
159}
160
161impl<'a> Iterator for PdfPageAnnotationAttachmentPointsIterator<'a> {
162 type Item = PdfQuadPoints;
163
164 fn next(&mut self) -> Option<Self::Item> {
165 let next = self.attachment_points.get(self.next_index);
166
167 self.next_index += 1;
168
169 next.ok()
170 }
171}