pdfium/
xobject.rs

1// PDFium-rs -- Modern Rust wrapper to PDFium, the PDF library from Google
2//
3// Copyright (c) 2025 Martin van der Werff <github (at) newinnovations.nl>
4//
5// This file is part of PDFium-rs.
6//
7// PDFium-rs is free software: you can redistribute it and/or modify it under the terms of
8// the GNU General Public License as published by the Free Software Foundation, either version 3
9// of the License, or (at your option) any later version.
10//
11// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
12// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
13// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
14// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
15// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
16// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
17// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
18// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19
20use crate::{
21    error::{PdfiumError, PdfiumResult},
22    lib,
23    pdfium_types::FPDF_XOBJECT,
24};
25
26/// # Rust interface to FPDF_XOBJECT
27pub struct PdfiumXObject {
28    handle: FPDF_XOBJECT,
29}
30
31impl PdfiumXObject {
32    pub(crate) fn new_from_handle(handle: FPDF_XOBJECT) -> PdfiumResult<Self> {
33        if handle.is_null() {
34            Err(PdfiumError::NullHandle)
35        } else {
36            #[cfg(feature = "debug_print")]
37            println!("New x_object {handle:?}");
38            Ok(Self { handle })
39        }
40    }
41}
42
43impl From<&PdfiumXObject> for FPDF_XOBJECT {
44    fn from(value: &PdfiumXObject) -> Self {
45        value.handle
46    }
47}
48
49impl Drop for PdfiumXObject {
50    /// # Closes this [`PdfiumXObject`], releasing held memory.
51    fn drop(&mut self) {
52        #[cfg(feature = "debug_print")]
53        println!("Closing x_object {:?}", self.handle);
54        lib().FPDF_CloseXObject(self);
55    }
56}