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
use std::ffi::CString;
use std::ptr;

use mupdf_sys::*;

use crate::{context, Device, Error, Rect};

#[derive(Debug)]
pub struct DocumentWriter {
    inner: *mut fz_document_writer,
}

impl DocumentWriter {
    pub fn new(filename: &str, format: &str, options: &str) -> Result<Self, Error> {
        let c_filename = CString::new(filename)?;
        let c_format = CString::new(format)?;
        let c_options = CString::new(options)?;
        let inner = unsafe {
            ffi_try!(mupdf_new_document_writer(
                context(),
                c_filename.as_ptr(),
                c_format.as_ptr(),
                c_options.as_ptr()
            ))
        };
        Ok(Self { inner })
    }

    pub fn begin_page(&mut self, media_box: Rect) -> Result<Device, Error> {
        unsafe {
            let dev = ffi_try!(mupdf_document_writer_begin_page(
                context(),
                self.inner,
                media_box.into()
            ));
            Ok(Device::from_raw(dev, ptr::null_mut()))
        }
    }

    pub fn end_page(&mut self) -> Result<(), Error> {
        unsafe {
            ffi_try!(mupdf_document_writer_end_page(context(), self.inner));
        }
        Ok(())
    }
}

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