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    handle: FPDF_STRUCTTREE,
18    lifetime: PhantomData<&'a FPDF_STRUCTTREE>,
19}
20
21impl<'a> PdfPageStructureTree<'a> {
22    pub(crate) fn from_pdfium(page_handle: FPDF_PAGE) -> Self {
23        let handle = unsafe {
24            PdfPageStructureTree {
25                handle: 0 as FPDF_STRUCTTREE,
26                lifetime: PhantomData,
27            }
28            .bindings()
29            .FPDF_StructTree_GetForPage(page_handle)
30        };
31
32        PdfPageStructureTree {
33            handle,
34            lifetime: PhantomData,
35        }
36    }
37
38    /// Returns the internal `FPDF_STRUCTTREE` handle for this [PdfStructureTree].
39    #[inline]
40    pub(crate) fn handle(&self) -> FPDF_STRUCTTREE {
41        self.handle
42    }
43}
44
45impl<'a> Drop for PdfPageStructureTree<'a> {
46    #[inline]
47    fn drop(&mut self) {
48        unsafe {
49            self.bindings().FPDF_StructTree_Close(self.handle());
50        }
51    }
52}
53
54impl<'a> PdfiumLibraryBindingsAccessor<'a> for PdfPageStructureTree<'a> {}
55
56#[cfg(feature = "thread_safe")]
57unsafe impl<'a> Send for PdfPageStructureTree<'a> {}
58
59#[cfg(feature = "thread_safe")]
60unsafe impl<'a> Sync for PdfPageStructureTree<'a> {}