Skip to main content

pdfium_render/pdf/document/page/
structure_tree.rs

1//! Defines the [PdfStructureTree] struct, the root element of a tree of elements that
2//! together describe the logical hierarchy of a single [PdfPage].
3
4use crate::bindgen::{FPDF_PAGE, FPDF_STRUCTTREE};
5use crate::pdfium::PdfiumLibraryBindingsAccessor;
6use std::marker::PhantomData;
7
8#[cfg(doc)]
9use crate::pdf::document::page::PdfPage;
10
11/// The root element of a tree of elements that together describe the logical hierarchy
12/// of a single [PdfPage].
13///
14/// More information on the structure tree can be found in The PDF Reference, Sixth Edition,
15/// in Section 10.6.1, beginning on page 856.
16pub struct PdfPageStructureTree<'a> {
17    page_handle: FPDF_PAGE,
18    tree_handle: FPDF_STRUCTTREE,
19    is_open: bool,
20    lifetime: PhantomData<&'a FPDF_STRUCTTREE>,
21}
22
23impl<'a> PdfPageStructureTree<'a> {
24    pub(crate) fn from_pdfium(page_handle: FPDF_PAGE) -> Self {
25        // Avoid opening the structure tree immediately; there is a performance cost
26        // to doing so, which we only want to incur if the user actually wants to
27        // walk the tree. See: https://github.com/ajrcarey/pdfium-render/issues/280
28
29        PdfPageStructureTree {
30            page_handle,
31            tree_handle: 0 as FPDF_STRUCTTREE,
32            is_open: false,
33            lifetime: PhantomData,
34        }
35    }
36
37    /// Returns the internal `FPDF_STRUCTTREE` handle for this [PdfStructureTree].
38    #[inline]
39    pub(crate) fn handle(&self) -> FPDF_STRUCTTREE {
40        self.tree_handle
41    }
42}
43
44impl<'a> Drop for PdfPageStructureTree<'a> {
45    #[inline]
46    fn drop(&mut self) {
47        if self.is_open {
48            unsafe {
49                self.bindings().FPDF_StructTree_Close(self.handle());
50            }
51        }
52    }
53}
54
55impl<'a> PdfiumLibraryBindingsAccessor<'a> for PdfPageStructureTree<'a> {}
56
57#[cfg(feature = "thread_safe")]
58unsafe impl<'a> Send for PdfPageStructureTree<'a> {}
59
60#[cfg(feature = "thread_safe")]
61unsafe impl<'a> Sync for PdfPageStructureTree<'a> {}