Skip to main content

pdfium_render/
config.rs

1//! Defines the [PdfiumLibraryConfig] struct, used to pass custom initialization configuration
2//! to a new Pdfium library instance.
3
4use crate::bindgen::{
5    FPDF_LIBRARY_CONFIG, FPDF_RENDERER_TYPE_FPDF_RENDERERTYPE_AGG,
6    FPDF_RENDERER_TYPE_FPDF_RENDERERTYPE_SKIA,
7};
8use crate::error::PdfiumError;
9use std::ffi::{CString, NulError};
10use std::os::raw::{c_char, c_uint};
11use std::pin::Pin;
12use std::ptr::null_mut;
13use std::str::FromStr;
14
15#[cfg(not(target_arch = "wasm32"))]
16use std::os::raw::c_void;
17
18#[cfg(any(feature = "pdfium_future", feature = "pdfium_7763",))]
19use crate::bindgen::{
20    FPDF_FONT_BACKEND_TYPE_FPDF_FONTBACKENDTYPE_FONTATIONS,
21    FPDF_FONT_BACKEND_TYPE_FPDF_FONTBACKENDTYPE_FREETYPE,
22};
23
24#[derive(Clone)]
25pub struct PdfiumLibraryConfig {
26    user_font_paths: Pin<Box<Vec<CString>>>,
27
28    #[cfg(not(target_arch = "wasm32"))]
29    v8_isolate_ptr: *mut c_void,
30    #[cfg(not(target_arch = "wasm32"))]
31    v8_embedder_slot_idx: c_uint,
32    #[cfg(not(target_arch = "wasm32"))]
33    v8_platform_ptr: *mut c_void,
34
35    renderer_type: c_uint,
36
37    #[cfg(any(feature = "pdfium_future", feature = "pdfium_7763",))]
38    font_library_type: c_uint,
39}
40
41impl PdfiumLibraryConfig {
42    /// Creates a new [PdfiumLibraryConfig] object with all settings initialized to
43    /// their default values.
44    pub fn new() -> Self {
45        PdfiumLibraryConfig {
46            user_font_paths: Box::pin(vec![]),
47
48            #[cfg(not(target_arch = "wasm32"))]
49            v8_isolate_ptr: null_mut(),
50            #[cfg(not(target_arch = "wasm32"))]
51            v8_embedder_slot_idx: 0,
52            #[cfg(not(target_arch = "wasm32"))]
53            v8_platform_ptr: null_mut(),
54
55            renderer_type: FPDF_RENDERER_TYPE_FPDF_RENDERERTYPE_SKIA,
56
57            #[cfg(any(feature = "pdfium_future", feature = "pdfium_7763",))]
58            font_library_type: FPDF_FONT_BACKEND_TYPE_FPDF_FONTBACKENDTYPE_FREETYPE,
59        }
60    }
61
62    /// Clears any user-specified paths that should be interrogated by Pdfium when
63    /// attempting to load any custom fonts referenced in a PDF document.
64    #[inline]
65    pub fn clear_user_font_paths(self) -> Self {
66        self.set_user_font_paths(&[]).unwrap()
67    }
68
69    #[cfg(target_arch = "wasm32")]
70    /// Sets the list of user-specified paths that should be interrogated by Pdfium when
71    /// attempting to load any custom fonts referenced in a PDF document.
72    ///
73    /// Since the browser does not provide a font loading mechanism, this list of font paths
74    /// is empty when compiling to WASM.
75    #[inline]
76    pub fn set_platform_default_user_font_paths(self) -> Self {
77        self.clear_user_font_paths()
78    }
79
80    #[cfg(not(target_arch = "wasm32"))]
81    #[cfg(target_os = "linux")]
82    /// Sets the list of user-specified paths that should be interrogated by Pdfium when
83    /// attempting to load any custom fonts referenced in a PDF document.
84    ///
85    /// On Linux systems, the platform default font paths are `/usr/share/fonts/truetype/`
86    /// and `/usr/local/share/fonts/`.
87    #[inline]
88    pub fn set_platform_default_user_font_paths(self) -> Self {
89        self.set_user_font_paths(&["/usr/share/fonts/truetype/", "/usr/local/share/fonts/"])
90            .unwrap()
91    }
92
93    #[cfg(not(target_arch = "wasm32"))]
94    #[cfg(target_os = "macos")]
95    /// Sets the list of user-specified paths that should be interrogated by Pdfium when
96    /// attempting to load any custom fonts referenced in a PDF document.
97    ///
98    /// On macOS systems, the platform default font paths are `/Library/Fonts/` and
99    /// `/System/Library/Fonts/`.
100    #[inline]
101    pub fn set_platform_default_user_font_paths(self) -> Self {
102        self.set_user_font_paths(&["/Library/Fonts/", "/System/Library/Fonts/"])
103            .unwrap()
104    }
105
106    #[cfg(not(target_arch = "wasm32"))]
107    #[cfg(target_os = "windows")]
108    /// Sets the list of user-specified paths that should be interrogated by Pdfium when
109    /// attempting to load any custom fonts referenced in a PDF document.
110    ///
111    /// On Windows systems, the platform default font path is `C:\Windows\Fonts\`.
112    #[inline]
113    pub fn set_platform_default_user_font_paths(self) -> Self {
114        self.set_user_font_paths(&["C:\\Windows\\Fonts\\"]).unwrap()
115    }
116
117    /// Sets the list of user-specified paths that should be interrogated by Pdfium when
118    /// attempting to load any custom fonts referenced in a PDF document.
119    pub fn set_user_font_paths(mut self, paths: &[&str]) -> Result<Self, PdfiumError> {
120        let cstr_paths = paths
121            .iter()
122            .map(|path| CString::from_str(path))
123            .collect::<Result<Vec<CString>, NulError>>();
124
125        match cstr_paths {
126            Ok(paths) => {
127                self.user_font_paths = Box::pin(paths);
128                Ok(self)
129            }
130            Err(e) => Err(PdfiumError::InvalidUserFontPath(e)),
131        }
132    }
133
134    #[cfg(not(target_arch = "wasm32"))]
135    /// Sets the pointer to the `v8::Isolate` to use. If `NULL`, Pdfium will create one.
136    #[inline]
137    pub unsafe fn set_v8_isolate_ptr(mut self, ptr: *mut c_void) -> Self {
138        self.v8_isolate_ptr = ptr;
139        self
140    }
141
142    #[cfg(not(target_arch = "wasm32"))]
143    /// Sets the embedder data slot to use in the `v8::Isolate` to store Pdfium's per-isolate
144    /// data. The value needs to be in the range `[0, v8::Internals::kNumIsolateDataLots)`.
145    /// Note that `0` is fine for most embedders.
146    #[inline]
147    pub unsafe fn set_v8_embedder_slot(mut self, idx: c_uint) -> Self {
148        self.v8_embedder_slot_idx = idx;
149        self
150    }
151
152    #[cfg(not(target_arch = "wasm32"))]
153    /// Sets the pointer to the `v8::Platform` to use.
154    #[inline]
155    pub unsafe fn set_v8_platform_ptr(mut self, ptr: &mut c_void) -> Self {
156        self.v8_platform_ptr = ptr;
157        self
158    }
159
160    /// Sets Pdfium's graphics renderer to the Anti-Grain Geometry library, <https://sourceforge.net/projects/agg/>.
161    #[inline]
162    pub fn set_renderer_anti_grain_geometry(mut self) -> Self {
163        self.renderer_type = FPDF_RENDERER_TYPE_FPDF_RENDERERTYPE_AGG;
164        self
165    }
166
167    /// Sets Pdfium's graphics renderer to Skia, <https://skia.org/>.
168    #[inline]
169    pub fn set_renderer_skia(mut self) -> Self {
170        self.renderer_type = FPDF_RENDERER_TYPE_FPDF_RENDERERTYPE_SKIA;
171        self
172    }
173
174    #[cfg(any(feature = "pdfium_future", feature = "pdfium_7763",))]
175    /// Sets Pdfium's font handler to FreeType, <https://freetype.org/>.
176    #[inline]
177    pub fn set_font_backend_freetype(mut self) -> Self {
178        self.font_library_type = FPDF_FONT_BACKEND_TYPE_FPDF_FONTBACKENDTYPE_FREETYPE;
179        self
180    }
181
182    #[cfg(any(feature = "pdfium_future", feature = "pdfium_7763",))]
183    /// Sets Pdfium's font handler to Fontations, <https://github.com/googlefonts/fontations/>.
184    #[inline]
185    pub fn set_font_backend_fontations(mut self) -> Self {
186        self.font_library_type = FPDF_FONT_BACKEND_TYPE_FPDF_FONTBACKENDTYPE_FONTATIONS;
187        self
188    }
189
190    /// Returns a `FPDF_LIBRARY_CONFIG` instance from this [PdfiumLibraryConfig] instance
191    /// that can be passed to Pdfium's `FPDF_FPDF_InitLibraryWithConfig()` function.
192    pub(crate) fn as_pdfium(&self) -> FPDF_LIBRARY_CONFIG {
193        FPDF_LIBRARY_CONFIG {
194            version: 2,
195            m_pUserFontPaths: if self.user_font_paths.is_empty() {
196                std::ptr::null_mut()
197            } else {
198                self.user_font_paths
199                    .iter()
200                    .map(|path| path.as_ptr())
201                    .collect::<Vec<*const c_char>>()
202                    .as_mut_slice()
203                    .as_mut_ptr()
204            },
205
206            #[cfg(not(target_arch = "wasm32"))]
207            m_pIsolate: self.v8_isolate_ptr,
208            #[cfg(not(target_arch = "wasm32"))]
209            m_v8EmbedderSlot: self.v8_embedder_slot_idx,
210            #[cfg(not(target_arch = "wasm32"))]
211            m_pPlatform: self.v8_platform_ptr,
212
213            #[cfg(target_arch = "wasm32")]
214            m_pIsolate: null_mut(),
215            #[cfg(target_arch = "wasm32")]
216            m_v8EmbedderSlot: 0,
217            #[cfg(target_arch = "wasm32")]
218            m_pPlatform: null_mut(),
219
220            m_RendererType: self.renderer_type,
221
222            #[cfg(any(feature = "pdfium_future", feature = "pdfium_7763",))]
223            m_FontLibraryType: self.font_library_type,
224        }
225    }
226}
227
228#[cfg(feature = "thread_safe")]
229unsafe impl Sync for PdfiumLibraryConfig {}
230
231#[cfg(feature = "thread_safe")]
232unsafe impl Send for PdfiumLibraryConfig {}