Skip to main content

pdfium_render/pdf/document/page/object/
content_marks.rs

1//! Defines the [PdfPageObjectContentMarks] struct, exposing functionality related to the
2//! collection of content marks associated with a [PdfPageObject].
3
4use crate::bindgen::FPDF_PAGEOBJECT;
5use crate::bindings::PdfiumLibraryBindings;
6use crate::pdf::document::page::object::content_mark::PdfPageObjectContentMark;
7use std::os::raw::c_ulong;
8
9/// The collection of content marks associated with a `PdfPageObject`.
10///
11/// Content marks provide a bridge between page objects and the PDF structure tree.
12/// Use the [PdfPageObjectContentMarks::iter()] method to iterate over all marks,
13/// or [PdfPageObjectContentMarks::get()] to access a mark by index.
14pub struct PdfPageObjectContentMarks<'a> {
15    object_handle: FPDF_PAGEOBJECT,
16    bindings: &'a dyn PdfiumLibraryBindings,
17}
18
19impl<'a> PdfPageObjectContentMarks<'a> {
20    pub(crate) fn from_pdfium(object_handle: FPDF_PAGEOBJECT, bindings: &'a dyn PdfiumLibraryBindings) -> Self {
21        Self {
22            object_handle,
23            bindings,
24        }
25    }
26
27    /// Returns the number of content marks associated with this page object.
28    pub fn len(&self) -> usize {
29        let count = self.bindings.FPDFPageObj_CountMarks(self.object_handle);
30
31        if count < 0 { 0 } else { count as usize }
32    }
33
34    /// Returns `true` if this page object has no content marks.
35    pub fn is_empty(&self) -> bool {
36        self.len() == 0
37    }
38
39    /// Returns the content mark at the given index, or `None` if the index is out of bounds.
40    pub fn get(&self, index: usize) -> Option<PdfPageObjectContentMark<'_>> {
41        if index >= self.len() {
42            return None;
43        }
44
45        let handle = self.bindings.FPDFPageObj_GetMark(self.object_handle, index as c_ulong);
46
47        if handle.is_null() {
48            None
49        } else {
50            Some(PdfPageObjectContentMark::from_pdfium(handle, self.bindings))
51        }
52    }
53
54    /// Returns an iterator over all content marks associated with this page object.
55    pub fn iter(&self) -> PdfPageObjectContentMarksIterator<'_> {
56        PdfPageObjectContentMarksIterator { marks: self, index: 0 }
57    }
58}
59
60/// An iterator over the content marks in a [PdfPageObjectContentMarks] collection.
61pub struct PdfPageObjectContentMarksIterator<'a> {
62    marks: &'a PdfPageObjectContentMarks<'a>,
63    index: usize,
64}
65
66impl<'a> Iterator for PdfPageObjectContentMarksIterator<'a> {
67    type Item = PdfPageObjectContentMark<'a>;
68
69    fn next(&mut self) -> Option<Self::Item> {
70        if self.index >= self.marks.len() {
71            None
72        } else {
73            let result = self.marks.get(self.index);
74            self.index += 1;
75            result
76        }
77    }
78}