mupdf 0.6.0

Safe Rust wrapper to MuPDF
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
use std::convert::TryFrom;
use std::ffi::{CStr, CString};
use std::fmt;
use std::io::{self, BufReader, Read, Write};
use std::slice;
use std::str::FromStr;

use mupdf_sys::*;

use crate::pdf::PdfDocument;
use crate::{context, Buffer, Error, Matrix};

pub trait IntoPdfDictKey {
    fn into_pdf_dict_key(self) -> Result<PdfObject, Error>;
}

impl IntoPdfDictKey for &str {
    fn into_pdf_dict_key(self) -> Result<PdfObject, Error> {
        PdfObject::new_name(self)
    }
}

impl IntoPdfDictKey for String {
    fn into_pdf_dict_key(self) -> Result<PdfObject, Error> {
        PdfObject::new_name(&self)
    }
}

impl IntoPdfDictKey for PdfObject {
    fn into_pdf_dict_key(self) -> Result<PdfObject, Error> {
        Ok(self)
    }
}

#[derive(Debug)]
pub struct PdfObject {
    pub(crate) inner: *mut pdf_obj,
}

impl PdfObject {
    pub(crate) unsafe fn from_raw(ptr: *mut pdf_obj) -> Self {
        Self { inner: ptr }
    }

    pub(crate) unsafe fn from_raw_keep_ref(ptr: *mut pdf_obj) -> Self {
        pdf_keep_obj(context(), ptr);
        Self { inner: ptr }
    }

    pub fn try_clone(&self) -> Result<Self, Error> {
        unsafe { ffi_try!(mupdf_pdf_clone_obj(context(), self.inner)) }.map(|inner| Self { inner })
    }

    pub fn new_null() -> PdfObject {
        unsafe {
            let inner = mupdf_pdf_new_null();
            PdfObject::from_raw(inner)
        }
    }

    pub fn new_bool(b: bool) -> PdfObject {
        unsafe {
            let inner = mupdf_pdf_new_bool(b);
            PdfObject::from_raw(inner)
        }
    }

    pub fn new_int(i: i32) -> Result<PdfObject, Error> {
        unsafe { ffi_try!(mupdf_pdf_new_int(context(), i)) }
            .map(|inner| unsafe { PdfObject::from_raw(inner) })
    }

    pub fn new_real(f: f32) -> Result<PdfObject, Error> {
        unsafe { ffi_try!(mupdf_pdf_new_real(context(), f)) }
            .map(|inner| unsafe { PdfObject::from_raw(inner) })
    }

    pub fn new_string(s: &str) -> Result<PdfObject, Error> {
        let c_str = CString::new(s)?;
        unsafe { ffi_try!(mupdf_pdf_new_string(context(), c_str.as_ptr())) }
            .map(|inner| unsafe { PdfObject::from_raw(inner) })
    }

    pub fn new_name(name: &str) -> Result<PdfObject, Error> {
        let c_name = CString::new(name)?;
        unsafe { ffi_try!(mupdf_pdf_new_name(context(), c_name.as_ptr())) }
            .map(|inner| unsafe { PdfObject::from_raw(inner) })
    }

    pub fn is_indirect(&self) -> Result<bool, Error> {
        unsafe { ffi_try!(mupdf_pdf_is_indirect(context(), self.inner)) }
    }

    pub fn is_null(&self) -> Result<bool, Error> {
        unsafe { ffi_try!(mupdf_pdf_is_null(context(), self.inner)) }
    }

    pub fn is_bool(&self) -> Result<bool, Error> {
        unsafe { ffi_try!(mupdf_pdf_is_bool(context(), self.inner)) }
    }

    pub fn is_int(&self) -> Result<bool, Error> {
        unsafe { ffi_try!(mupdf_pdf_is_int(context(), self.inner)) }
    }

    pub fn is_real(&self) -> Result<bool, Error> {
        unsafe { ffi_try!(mupdf_pdf_is_real(context(), self.inner)) }
    }

    pub fn is_number(&self) -> Result<bool, Error> {
        unsafe { ffi_try!(mupdf_pdf_is_number(context(), self.inner)) }
    }

    pub fn is_string(&self) -> Result<bool, Error> {
        unsafe { ffi_try!(mupdf_pdf_is_string(context(), self.inner)) }
    }

    pub fn is_name(&self) -> Result<bool, Error> {
        unsafe { ffi_try!(mupdf_pdf_is_name(context(), self.inner)) }
    }

    pub fn is_array(&self) -> Result<bool, Error> {
        unsafe { ffi_try!(mupdf_pdf_is_array(context(), self.inner)) }
    }

    pub fn is_dict(&self) -> Result<bool, Error> {
        unsafe { ffi_try!(mupdf_pdf_is_dict(context(), self.inner)) }
    }

    pub fn is_stream(&self) -> Result<bool, Error> {
        unsafe { ffi_try!(mupdf_pdf_is_stream(context(), self.inner)) }
    }

    pub fn as_bool(&self) -> Result<bool, Error> {
        unsafe { ffi_try!(mupdf_pdf_to_bool(context(), self.inner)) }
    }

    pub fn as_int(&self) -> Result<i32, Error> {
        unsafe { ffi_try!(mupdf_pdf_to_int(context(), self.inner)) }
    }

    pub fn as_float(&self) -> Result<f32, Error> {
        unsafe { ffi_try!(mupdf_pdf_to_float(context(), self.inner)) }
    }

    pub fn as_indirect(&self) -> Result<i32, Error> {
        unsafe { ffi_try!(mupdf_pdf_to_indirect(context(), self.inner)) }
    }

    pub fn as_name(&self) -> Result<&[u8], Error> {
        let name_ptr = unsafe { ffi_try!(mupdf_pdf_to_name(context(), self.inner)) }?;
        let c_name = unsafe { CStr::from_ptr(name_ptr) };
        Ok(c_name.to_bytes())
    }

    pub fn as_string(&self) -> Result<&str, Error> {
        let str_ptr = unsafe { ffi_try!(mupdf_pdf_to_string(context(), self.inner)) }?;
        let c_str = unsafe { CStr::from_ptr(str_ptr) };
        Ok(c_str.to_str().unwrap())
    }

    pub fn as_bytes(&self) -> Result<&[u8], Error> {
        let mut len = 0;
        let ptr = unsafe { ffi_try!(mupdf_pdf_to_bytes(context(), self.inner, &mut len)) }?;
        Ok(unsafe { slice::from_raw_parts(ptr, len) })
    }

    pub fn resolve(&self) -> Result<Option<Self>, Error> {
        let inner = unsafe { ffi_try!(mupdf_pdf_resolve_indirect(context(), self.inner)) }?;
        if inner.is_null() {
            return Ok(None);
        }
        Ok(Some(Self { inner }))
    }

    pub fn read_stream(&self) -> Result<Vec<u8>, Error> {
        let inner = unsafe { ffi_try!(mupdf_pdf_read_stream(context(), self.inner)) }?;
        let buf = unsafe { Buffer::from_raw(inner) };
        let buf_len = buf.len();
        let mut reader = BufReader::new(buf);
        let mut output = Vec::with_capacity(buf_len);
        reader.read_to_end(&mut output)?;
        Ok(output)
    }

    pub fn read_raw_stream(&self) -> Result<Vec<u8>, Error> {
        let inner = unsafe { ffi_try!(mupdf_pdf_read_raw_stream(context(), self.inner)) }?;
        let buf = unsafe { Buffer::from_raw(inner) };
        let buf_len = buf.len();
        let mut reader = BufReader::new(buf);
        let mut output = Vec::with_capacity(buf_len);
        reader.read_to_end(&mut output)?;
        Ok(output)
    }

    pub fn write_object(&mut self, obj: &PdfObject) -> Result<(), Error> {
        unsafe { ffi_try!(mupdf_pdf_write_object(context(), self.inner, obj.inner)) }
    }

    pub fn write_stream_buffer(&mut self, buf: &Buffer) -> Result<(), Error> {
        unsafe {
            ffi_try!(mupdf_pdf_write_stream_buffer(
                context(),
                self.inner,
                buf.inner,
                0
            ))
        }
    }

    pub fn write_stream_string(&mut self, string: &str) -> Result<(), Error> {
        let buf = Buffer::from_str(string)?;
        self.write_stream_buffer(&buf)
    }

    pub fn write_raw_stream_buffer(&mut self, buf: &Buffer) -> Result<(), Error> {
        unsafe {
            ffi_try!(mupdf_pdf_write_stream_buffer(
                context(),
                self.inner,
                buf.inner,
                1
            ))
        }
    }

    pub fn write_raw_stream_string(&mut self, string: &str) -> Result<(), Error> {
        let buf = Buffer::from_str(string)?;
        self.write_raw_stream_buffer(&buf)
    }

    pub fn get_array(&self, index: i32) -> Result<Option<Self>, Error> {
        let inner = unsafe { ffi_try!(mupdf_pdf_array_get(context(), self.inner, index)) }?;
        if inner.is_null() {
            return Ok(None);
        }
        Ok(Some(Self { inner }))
    }

    pub fn dict_len(&self) -> Result<usize, Error> {
        unsafe { ffi_try!(mupdf_pdf_dict_len(context(), self.inner)) }.map(|size| size as usize)
    }

    pub fn get_dict_val(&self, idx: i32) -> Result<Option<Self>, Error> {
        let inner = unsafe { ffi_try!(mupdf_pdf_dict_get_val(context(), self.inner, idx)) }?;
        if inner.is_null() {
            return Ok(None);
        }
        Ok(Some(Self { inner }))
    }
    pub fn get_dict_key(&self, idx: i32) -> Result<Option<Self>, Error> {
        let inner = unsafe { ffi_try!(mupdf_pdf_dict_get_key(context(), self.inner, idx)) }?;
        if inner.is_null() {
            return Ok(None);
        }
        Ok(Some(Self { inner }))
    }

    pub fn get_dict<K: IntoPdfDictKey>(&self, key: K) -> Result<Option<Self>, Error> {
        let key = key.into_pdf_dict_key()?;
        let inner = unsafe { ffi_try!(mupdf_pdf_dict_get(context(), self.inner, key.inner)) }?;
        if inner.is_null() {
            return Ok(None);
        }
        Ok(Some(Self { inner }))
    }

    pub fn get_dict_inheritable<K: IntoPdfDictKey>(&self, key: K) -> Result<Option<Self>, Error> {
        let key = key.into_pdf_dict_key()?;
        let inner = unsafe {
            ffi_try!(mupdf_pdf_dict_get_inheritable(
                context(),
                self.inner,
                key.inner
            ))
        }?;
        if inner.is_null() {
            return Ok(None);
        }
        Ok(Some(Self { inner }))
    }

    #[allow(clippy::len_without_is_empty)]
    pub fn len(&self) -> Result<usize, Error> {
        unsafe { ffi_try!(mupdf_pdf_array_len(context(), self.inner)) }.map(|size| size as usize)
    }

    pub fn array_put(&mut self, index: i32, value: Self) -> Result<(), Error> {
        unsafe {
            ffi_try!(mupdf_pdf_array_put(
                context(),
                self.inner,
                index,
                value.inner
            ))
        }
    }

    pub fn array_push(&mut self, value: Self) -> Result<(), Error> {
        unsafe { ffi_try!(mupdf_pdf_array_push(context(), self.inner, value.inner)) }
    }

    pub fn array_delete(&mut self, index: i32) -> Result<(), Error> {
        unsafe { ffi_try!(mupdf_pdf_array_delete(context(), self.inner, index)) }
    }

    pub fn dict_put<K: IntoPdfDictKey>(&mut self, key: K, value: Self) -> Result<(), Error> {
        let key_obj = key.into_pdf_dict_key()?;
        unsafe {
            ffi_try!(mupdf_pdf_dict_put(
                context(),
                self.inner,
                key_obj.inner,
                value.inner
            ))
        }
    }

    pub fn dict_delete<K: IntoPdfDictKey>(&mut self, key: K) -> Result<(), Error> {
        let key_obj = key.into_pdf_dict_key()?;
        unsafe { ffi_try!(mupdf_pdf_dict_delete(context(), self.inner, key_obj.inner)) }
    }

    fn print(&self, tight: bool, ascii: bool) -> Result<String, Error> {
        let ptr =
            unsafe { ffi_try!(mupdf_pdf_obj_to_string(context(), self.inner, tight, ascii)) }?;
        let c_str = unsafe { CStr::from_ptr(ptr) };
        let s = c_str.to_string_lossy().into_owned();
        unsafe { fz_free(context(), ptr.cast()) };
        Ok(s)
    }

    pub fn document(&self) -> Option<PdfDocument> {
        unsafe {
            let ptr = mupdf_pdf_get_bound_document(context(), self.inner);
            if ptr.is_null() {
                return None;
            }
            Some(PdfDocument::from_raw(ptr))
        }
    }

    pub fn page_ctm(&self) -> Result<Matrix, Error> {
        unsafe { ffi_try!(mupdf_pdf_page_obj_transform(context(), self.inner)) }.map(Into::into)
    }
}

impl Write for PdfObject {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        let len = buf.len();
        let mut fz_buf = Buffer::with_capacity(len);
        fz_buf.write(buf)?;
        self.write_stream_buffer(&fz_buf)
            .map_err(io::Error::other)?;
        Ok(len)
    }

    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}

impl Drop for PdfObject {
    fn drop(&mut self) {
        if !self.inner.is_null() {
            unsafe {
                pdf_drop_obj(context(), self.inner);
            }
        }
    }
}

impl Clone for PdfObject {
    fn clone(&self) -> PdfObject {
        self.try_clone().unwrap()
    }
}

impl fmt::Display for PdfObject {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let s = self.print(true, false).unwrap();
        f.write_str(&s)
    }
}

impl From<bool> for PdfObject {
    fn from(b: bool) -> PdfObject {
        PdfObject::new_bool(b)
    }
}

impl TryFrom<i32> for PdfObject {
    type Error = Error;

    fn try_from(i: i32) -> Result<PdfObject, Self::Error> {
        PdfObject::new_int(i)
    }
}

impl TryFrom<f32> for PdfObject {
    type Error = Error;

    fn try_from(f: f32) -> Result<PdfObject, Self::Error> {
        PdfObject::new_real(f)
    }
}

impl TryFrom<&str> for PdfObject {
    type Error = Error;

    fn try_from(s: &str) -> Result<PdfObject, Self::Error> {
        PdfObject::new_string(s)
    }
}

impl TryFrom<String> for PdfObject {
    type Error = Error;

    fn try_from(s: String) -> Result<PdfObject, Self::Error> {
        PdfObject::new_string(&s)
    }
}