pdfium_render/pdf/document/page/annotation/attachment_points.rs
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
//! Defines the [PdfPageAnnotationAttachmentPoints] struct, a collection of all the
//! attachment points that visually associate a `PdfPageAnnotation` object with one or more
//! `PdfPageObject` objects on a `PdfPage`.
use crate::bindgen::FPDF_ANNOTATION;
use crate::bindings::PdfiumLibraryBindings;
use crate::error::{PdfiumError, PdfiumInternalError};
use crate::pdf::quad_points::PdfQuadPoints;
use crate::pdf::rect::PdfRect;
use std::ops::{Range, RangeInclusive};
/// The zero-based index of a single attachment point inside its containing
/// [PdfPageAnnotationAttachmentPoints] collection.
pub type PdfPageAnnotationAttachmentPointIndex = usize;
/// A set of all the attachment points that visually connect a `PdfPageAnnotation` object
/// to one or more `PdfPageObject` objects on a `PdfPage`.
pub struct PdfPageAnnotationAttachmentPoints<'a> {
annotation_handle: FPDF_ANNOTATION,
bindings: &'a dyn PdfiumLibraryBindings,
}
impl<'a> PdfPageAnnotationAttachmentPoints<'a> {
#[inline]
pub(crate) fn from_pdfium(
annotation_handle: FPDF_ANNOTATION,
bindings: &'a dyn PdfiumLibraryBindings,
) -> Self {
PdfPageAnnotationAttachmentPoints {
annotation_handle,
bindings,
}
}
/// Returns the number of attachment points in this [PdfPageAnnotationAttachmentPoints] collection.
pub fn len(&self) -> PdfPageAnnotationAttachmentPointIndex {
if self.bindings.is_true(
self.bindings
.FPDFAnnot_HasAttachmentPoints(self.annotation_handle),
) {
self.bindings
.FPDFAnnot_CountAttachmentPoints(self.annotation_handle)
as PdfPageAnnotationAttachmentPointIndex
} else {
// Attachment points are not supported for this annotation type.
0
}
}
/// Returns `true` if this [PdfPageAnnotationAttachmentPoints] collection is empty.
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Returns a Range from `0..(number of attachment points)` for this
/// [PdfPageAnnotationAttachmentPoints] collection.
#[inline]
pub fn as_range(&self) -> Range<PdfPageAnnotationAttachmentPointIndex> {
0..self.len()
}
/// Returns an inclusive Range from `0..=(number of attachment points - 1)` for this
/// [PdfPageAnnotationAttachmentPoints] collection.
#[inline]
pub fn as_range_inclusive(&self) -> RangeInclusive<PdfPageAnnotationAttachmentPointIndex> {
if self.is_empty() {
0..=0
} else {
0..=(self.len() - 1)
}
}
/// Returns a single attachment point, expressed as a set of [PdfQuadPoints], from this
/// [PdfPageAnnotationAttachmentPoints] collection.
pub fn get(
&self,
index: PdfPageAnnotationAttachmentPointIndex,
) -> Result<PdfQuadPoints, PdfiumError> {
if index >= self.len() {
return Err(PdfiumError::PageAnnotationAttachmentPointIndexOutOfBounds);
}
let mut result = PdfQuadPoints::from_rect(PdfRect::ZERO).as_pdfium();
if self
.bindings
.is_true(self.bindings.FPDFAnnot_GetAttachmentPoints(
self.annotation_handle,
index,
&mut result,
))
{
Ok(PdfQuadPoints::from_pdfium(result))
} else {
Err(PdfiumError::PdfiumLibraryInternalError(
PdfiumInternalError::Unknown,
))
}
}
/// Returns the first attachment point, expressed as a set of [PdfQuadPoints],
/// in this [PdfPageAnnotationAttachmentPoints] collection.
#[inline]
pub fn first(&self) -> Result<PdfQuadPoints, PdfiumError> {
if !self.is_empty() {
self.get(0)
} else {
Err(PdfiumError::NoAttachmentPointsInPageAnnotation)
}
}
/// Returns the last attachment point, expressed as a set of [PdfQuadPoints],
/// in this [PdfPageAnnotationAttachmentPoints] collection.
#[inline]
pub fn last(&self) -> Result<PdfQuadPoints, PdfiumError> {
if !self.is_empty() {
self.get(self.len() - 1)
} else {
Err(PdfiumError::NoAttachmentPointsInPageAnnotation)
}
}
/// Creates a new attachment point from the given set of [PdfQuadPoints],
/// and appends it to the end of this [PdfPageAnnotationAttachmentPoints] collection.
#[inline]
pub fn create_attachment_point_at_end(
&mut self,
attachment_point: PdfQuadPoints,
) -> Result<(), PdfiumError> {
if self
.bindings
.is_true(self.bindings.FPDFAnnot_AppendAttachmentPoints(
self.annotation_handle,
&attachment_point.as_pdfium(),
))
{
Ok(())
} else {
Err(PdfiumError::PdfiumLibraryInternalError(
PdfiumInternalError::Unknown,
))
}
}
/// Replaces the attachment at the given index in this [PdfPageAnnotationAttachmentPoints]
/// collection with the given updated set of [PdfQuadPoints].
pub fn set_attachment_point_at_index(
&mut self,
index: PdfPageAnnotationAttachmentPointIndex,
attachment_point: PdfQuadPoints,
) -> Result<(), PdfiumError> {
if self
.bindings
.is_true(self.bindings.FPDFAnnot_SetAttachmentPoints(
self.annotation_handle,
index,
&attachment_point.as_pdfium(),
))
{
Ok(())
} else {
Err(PdfiumError::PdfiumLibraryInternalError(
PdfiumInternalError::Unknown,
))
}
}
/// Returns an iterator over all the attachment points in this [PdfPageAnnotationAttachmentPoints] collection.
#[inline]
pub fn iter(&self) -> PdfPageAnnotationAttachmentPointsIterator {
PdfPageAnnotationAttachmentPointsIterator::new(self)
}
}
/// An iterator over all the attachment points in a [PdfPageAnnotationAttachmentPoints] collection.
pub struct PdfPageAnnotationAttachmentPointsIterator<'a> {
attachment_points: &'a PdfPageAnnotationAttachmentPoints<'a>,
next_index: PdfPageAnnotationAttachmentPointIndex,
}
impl<'a> PdfPageAnnotationAttachmentPointsIterator<'a> {
#[inline]
pub(crate) fn new(attachment_points: &'a PdfPageAnnotationAttachmentPoints<'a>) -> Self {
PdfPageAnnotationAttachmentPointsIterator {
attachment_points,
next_index: 0,
}
}
}
impl<'a> Iterator for PdfPageAnnotationAttachmentPointsIterator<'a> {
type Item = PdfQuadPoints;
fn next(&mut self) -> Option<Self::Item> {
let next = self.attachment_points.get(self.next_index);
self.next_index += 1;
next.ok()
}
}