pdfium_render/pdf/document/page/
objects.rspub mod common;
pub(crate) mod private; use crate::bindgen::{FPDF_DOCUMENT, FPDF_PAGE};
use crate::bindings::PdfiumLibraryBindings;
use crate::error::{PdfiumError, PdfiumInternalError};
use crate::pdf::document::page::object::group::PdfPageGroupObject;
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 std::os::raw::c_int;
pub struct PdfPageObjects<'a> {
document_handle: FPDF_DOCUMENT,
page_handle: FPDF_PAGE,
ownership: PdfPageObjectOwnership,
bindings: &'a dyn PdfiumLibraryBindings,
}
impl<'a> PdfPageObjects<'a> {
#[inline]
pub(crate) fn from_pdfium(
document_handle: FPDF_DOCUMENT,
page_handle: FPDF_PAGE,
bindings: &'a dyn PdfiumLibraryBindings,
) -> Self {
Self {
document_handle,
page_handle,
ownership: PdfPageObjectOwnership::owned_by_page(document_handle, page_handle),
bindings,
}
}
#[inline]
pub(crate) fn document_handle(&self) -> FPDF_DOCUMENT {
self.document_handle
}
#[inline]
pub(crate) fn page_handle(&self) -> FPDF_PAGE {
self.page_handle
}
pub fn create_group<F>(&'a self, predicate: F) -> Result<PdfPageGroupObject<'a>, PdfiumError>
where
F: Fn(&PdfPageObject) -> bool,
{
let mut result = self.create_empty_group();
for mut object in self.iter().filter(predicate) {
result.push(&mut object)?;
}
Ok(result)
}
#[inline]
pub fn create_empty_group(&self) -> PdfPageGroupObject<'a> {
PdfPageGroupObject::from_pdfium(self.document_handle(), self.page_handle(), self.bindings())
}
}
impl<'a> PdfPageObjectsPrivate<'a> for PdfPageObjects<'a> {
#[inline]
fn ownership(&self) -> &PdfPageObjectOwnership {
&self.ownership
}
#[inline]
fn bindings(&self) -> &'a dyn PdfiumLibraryBindings {
self.bindings
}
#[inline]
fn len_impl(&self) -> PdfPageObjectIndex {
self.bindings.FPDFPage_CountObjects(self.page_handle) as PdfPageObjectIndex
}
fn get_impl(&self, index: PdfPageObjectIndex) -> Result<PdfPageObject<'a>, PdfiumError> {
if index >= self.len() {
return Err(PdfiumError::PageObjectIndexOutOfBounds);
}
let object_handle = self
.bindings
.FPDFPage_GetObject(self.page_handle, index as c_int);
if object_handle.is_null() {
Err(PdfiumError::PdfiumLibraryInternalError(
PdfiumInternalError::Unknown,
))
} else {
Ok(PdfPageObject::from_pdfium(
object_handle,
self.ownership().clone(),
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_page(self).map(|_| object)
}
#[inline]
fn remove_object_impl(
&mut self,
mut object: PdfPageObject<'a>,
) -> Result<PdfPageObject<'a>, PdfiumError> {
object.remove_object_from_page().map(|_| object)
}
}