1use crate::bindgen::{
2 FPDF_LIBRARY_CONFIG, FPDF_RENDERER_TYPE_FPDF_RENDERERTYPE_AGG,
3 FPDF_RENDERER_TYPE_FPDF_RENDERERTYPE_SKIA,
4};
5use crate::error::PdfiumError;
6use std::ffi::{CString, NulError};
7use std::os::raw::{c_char, c_uint};
8use std::pin::Pin;
9use std::ptr::null_mut;
10use std::str::FromStr;
11
12#[cfg(not(target_arch = "wasm32"))]
13use std::os::raw::c_void;
14
15#[cfg(any(feature = "pdfium_future", feature = "pdfium_7763",))]
16use crate::bindgen::{
17 FPDF_FONT_BACKEND_TYPE_FPDF_FONTBACKENDTYPE_FONTATIONS,
18 FPDF_FONT_BACKEND_TYPE_FPDF_FONTBACKENDTYPE_FREETYPE,
19};
20
21#[derive(Clone)]
22pub struct PdfiumLibraryConfig {
23 user_font_paths: Pin<Box<Vec<CString>>>,
24
25 #[cfg(not(target_arch = "wasm32"))]
26 v8_isolate_ptr: *mut c_void,
27 #[cfg(not(target_arch = "wasm32"))]
28 v8_embedder_slot_idx: c_uint,
29 #[cfg(not(target_arch = "wasm32"))]
30 v8_platform_ptr: *mut c_void,
31
32 renderer_type: c_uint,
33
34 #[cfg(any(feature = "pdfium_future", feature = "pdfium_7763",))]
35 font_library_type: c_uint,
36}
37
38impl PdfiumLibraryConfig {
39 pub fn new() -> Self {
42 PdfiumLibraryConfig {
43 user_font_paths: Box::pin(vec![]),
44
45 #[cfg(not(target_arch = "wasm32"))]
46 v8_isolate_ptr: null_mut(),
47 #[cfg(not(target_arch = "wasm32"))]
48 v8_embedder_slot_idx: 0,
49 #[cfg(not(target_arch = "wasm32"))]
50 v8_platform_ptr: null_mut(),
51
52 renderer_type: FPDF_RENDERER_TYPE_FPDF_RENDERERTYPE_SKIA,
53
54 #[cfg(any(feature = "pdfium_future", feature = "pdfium_7763",))]
55 font_library_type: FPDF_FONT_BACKEND_TYPE_FPDF_FONTBACKENDTYPE_FREETYPE,
56 }
57 }
58
59 #[inline]
62 pub fn clear_user_font_paths(self) -> Self {
63 self.set_user_font_paths(&[]).unwrap()
64 }
65
66 #[cfg(target_arch = "wasm32")]
67 #[inline]
71 pub fn set_platform_default_user_font_paths(self) -> Self {
72 self.clear_user_font_paths()
73 }
74
75 #[cfg(not(target_arch = "wasm32"))]
76 #[cfg(target_os = "linux")]
77 #[inline]
80 pub fn set_platform_default_user_font_paths(self) -> Self {
81 self.set_user_font_paths(&["/usr/share/fonts/truetype/", "/usr/local/share/fonts/"])
82 .unwrap()
83 }
84
85 #[cfg(not(target_arch = "wasm32"))]
86 #[cfg(target_os = "macos")]
87 #[inline]
90 pub fn set_platform_default_user_font_paths(self) -> Self {
91 self.set_user_font_paths(&["/Library/Fonts/", "/System/Library/Fonts/"])
92 .unwrap()
93 }
94
95 #[cfg(not(target_arch = "wasm32"))]
96 #[cfg(target_os = "windows")]
97 #[inline]
100 pub fn set_platform_default_user_font_paths(self) -> Self {
101 self.set_user_font_paths(&["C:\\Windows\\Fonts\\"]).unwrap()
102 }
103
104 pub fn set_user_font_paths(mut self, paths: &[&str]) -> Result<Self, PdfiumError> {
107 let cstr_paths = paths
108 .iter()
109 .map(|path| CString::from_str(path))
110 .collect::<Result<Vec<CString>, NulError>>();
111
112 match cstr_paths {
113 Ok(paths) => {
114 self.user_font_paths = Box::pin(paths);
115 Ok(self)
116 }
117 Err(e) => Err(PdfiumError::InvalidUserFontPath(e)),
118 }
119 }
120
121 #[cfg(not(target_arch = "wasm32"))]
122 #[inline]
124 pub unsafe fn set_v8_isolate_ptr(mut self, ptr: *mut c_void) -> Self {
125 self.v8_isolate_ptr = ptr;
126 self
127 }
128
129 #[cfg(not(target_arch = "wasm32"))]
130 #[inline]
134 pub unsafe fn set_v8_embedder_slot(mut self, idx: c_uint) -> Self {
135 self.v8_embedder_slot_idx = idx;
136 self
137 }
138
139 #[cfg(not(target_arch = "wasm32"))]
140 #[inline]
142 pub unsafe fn set_v8_platform_ptr(mut self, ptr: &mut c_void) -> Self {
143 self.v8_platform_ptr = ptr;
144 self
145 }
146
147 #[inline]
149 pub fn set_renderer_anti_grain_geometry(mut self) -> Self {
150 self.renderer_type = FPDF_RENDERER_TYPE_FPDF_RENDERERTYPE_AGG;
151 self
152 }
153
154 #[inline]
156 pub fn set_renderer_skia(mut self) -> Self {
157 self.renderer_type = FPDF_RENDERER_TYPE_FPDF_RENDERERTYPE_SKIA;
158 self
159 }
160
161 #[cfg(any(feature = "pdfium_future", feature = "pdfium_7763",))]
162 #[inline]
164 pub fn set_font_backend_freetype(mut self) -> Self {
165 self.font_library_type = FPDF_FONT_BACKEND_TYPE_FPDF_FONTBACKENDTYPE_FREETYPE;
166 self
167 }
168
169 #[cfg(any(feature = "pdfium_future", feature = "pdfium_7763",))]
170 #[inline]
172 pub fn set_font_backend_fontations(mut self) -> Self {
173 self.font_library_type = FPDF_FONT_BACKEND_TYPE_FPDF_FONTBACKENDTYPE_FONTATIONS;
174 self
175 }
176
177 #[inline]
178 pub(crate) fn as_pdfium(&self) -> FPDF_LIBRARY_CONFIG {
179 FPDF_LIBRARY_CONFIG {
180 version: 2,
181 m_pUserFontPaths: self
182 .user_font_paths
183 .iter()
184 .map(|path| path.as_ptr())
185 .collect::<Vec<*const c_char>>()
186 .as_mut_slice()
187 .as_mut_ptr(),
188
189 #[cfg(not(target_arch = "wasm32"))]
190 m_pIsolate: self.v8_isolate_ptr,
191 #[cfg(not(target_arch = "wasm32"))]
192 m_v8EmbedderSlot: self.v8_embedder_slot_idx,
193 #[cfg(not(target_arch = "wasm32"))]
194 m_pPlatform: self.v8_platform_ptr,
195
196 #[cfg(target_arch = "wasm32")]
197 m_pIsolate: null_mut(),
198 #[cfg(target_arch = "wasm32")]
199 m_v8EmbedderSlot: 0,
200 #[cfg(target_arch = "wasm32")]
201 m_pPlatform: null_mut(),
202
203 m_RendererType: self.renderer_type,
204
205 #[cfg(any(feature = "pdfium_future", feature = "pdfium_7763",))]
206 m_FontLibraryType: self.font_library_type,
207 }
208 }
209}
210
211#[cfg(feature = "thread_safe")]
212unsafe impl Sync for PdfiumLibraryConfig {}
213
214#[cfg(feature = "thread_safe")]
215unsafe impl Send for PdfiumLibraryConfig {}