use crate::bindgen::{FPDF_ANNOTATION, FPDF_DOCUMENT, FPDF_PAGE};
use crate::error::{PdfiumError, PdfiumInternalError};
use crate::pdf::document::page::object::ownership::PdfPageObjectOwnership;
use crate::pdf::document::page::object::private::internal::PdfPageObjectPrivate;
use crate::pdf::document::page::object::PdfPageObject;
use crate::pdf::document::page::objects::common::{
PdfPageObjectIndex, PdfPageObjectsCommon, PdfPageObjectsIterator,
};
use crate::pdf::document::page::objects::private::internal::PdfPageObjectsPrivate;
use crate::pdfium::PdfiumLibraryBindingsAccessor;
use std::marker::PhantomData;
use std::os::raw::c_int;
pub struct PdfPageAnnotationObjects<'a> {
annotation_handle: FPDF_ANNOTATION,
ownership: PdfPageObjectOwnership,
lifetime: PhantomData<&'a FPDF_ANNOTATION>,
}
impl<'a> PdfPageAnnotationObjects<'a> {
#[inline]
pub(crate) fn from_pdfium(
document_handle: FPDF_DOCUMENT,
page_handle: FPDF_PAGE,
annotation_handle: FPDF_ANNOTATION,
) -> Self {
Self {
annotation_handle,
ownership: PdfPageObjectOwnership::owned_by_attached_annotation(
document_handle,
page_handle,
annotation_handle,
),
lifetime: PhantomData,
}
}
#[inline]
pub(crate) fn annotation_handle(&self) -> FPDF_ANNOTATION {
self.annotation_handle
}
}
impl<'a> PdfPageObjectsPrivate<'a> for PdfPageAnnotationObjects<'a> {
#[inline]
fn ownership(&self) -> &PdfPageObjectOwnership {
&self.ownership
}
#[inline]
fn len_impl(&self) -> PdfPageObjectIndex {
(unsafe {
self.bindings()
.FPDFAnnot_GetObjectCount(self.annotation_handle())
}) as PdfPageObjectIndex
}
fn get_impl(&self, index: PdfPageObjectIndex) -> Result<PdfPageObject<'a>, PdfiumError> {
let object_handle = unsafe {
self.bindings()
.FPDFAnnot_GetObject(self.annotation_handle(), index as c_int)
};
if object_handle.is_null() {
if index >= self.len() {
Err(PdfiumError::PageObjectIndexOutOfBounds)
} else {
Err(PdfiumError::PdfiumLibraryInternalError(
PdfiumInternalError::Unknown,
))
}
} else {
Ok(PdfPageObject::from_pdfium(
object_handle,
*self.ownership(),
self.bindings(),
))
}
}
#[inline]
fn iter_impl(&'a self) -> PdfPageObjectsIterator<'a> {
PdfPageObjectsIterator::new(self)
}
#[inline]
fn add_object_impl(
&mut self,
mut object: PdfPageObject<'a>,
) -> Result<PdfPageObject<'a>, PdfiumError> {
object.add_object_to_annotation(self).map(|_| object)
}
#[inline]
fn remove_object_impl(
&mut self,
mut object: PdfPageObject<'a>,
) -> Result<PdfPageObject<'a>, PdfiumError> {
object.remove_object_from_annotation().map(|_| object)
}
}
impl<'a> PdfiumLibraryBindingsAccessor<'a> for PdfPageAnnotationObjects<'a> {}
#[cfg(feature = "thread_safe")]
unsafe impl<'a> Send for PdfPageAnnotationObjects<'a> {}
#[cfg(feature = "thread_safe")]
unsafe impl<'a> Sync for PdfPageAnnotationObjects<'a> {}