1use 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 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 #[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 #[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 #[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 #[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 #[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 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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 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 {}