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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
use std::ffi::{c_int, CStr, CString};
use std::io::Read;
use std::ptr::NonNull;

use mupdf_sys::*;

use crate::Document;
use crate::{
    array::FzArray, context, link::LinkDestination, rust_vec_from_ffi_ptr, unsafe_impl_ffi_wrapper,
    Buffer, Colorspace, Cookie, Device, DisplayList, Error, FFIWrapper, Link, Matrix, Pixmap, Quad,
    Rect, Separations, TextPage, TextPageFlags,
};

#[derive(Debug)]
pub struct Page {
    /// Ideally we'd use `Unique` here to signify ownership but that's not stable
    pub(crate) inner: NonNull<fz_page>,
}

unsafe_impl_ffi_wrapper!(Page, fz_page, fz_drop_page);

impl Page {
    /// # Safety
    ///
    /// * `raw` may be null, that will cause this function to return an error. If it is non-null,
    ///   however, it must point to a valid, well-aligned instance of [`fz_page`].
    pub(crate) unsafe fn from_raw(raw: *mut fz_page) -> Result<Self, Error> {
        NonNull::new(raw)
            // SAFETY: Upheld by caller
            .map(|nn| unsafe { Self::from_non_null(nn) })
            .ok_or(Error::UnexpectedNullPtr)
    }

    /// # Safety
    ///
    /// * `nonnull` must point to a valid, well-aligned instance of [`fz_page`]
    pub(crate) unsafe fn from_non_null(nonnull: NonNull<fz_page>) -> Self {
        Self { inner: nonnull }
    }

    pub fn bounds(&self) -> Result<Rect, Error> {
        unsafe { ffi_try!(mupdf_bound_page(context(), self.as_ptr().cast_mut())) }.map(Into::into)
    }

    pub fn to_pixmap(
        &self,
        ctm: &Matrix,
        cs: &Colorspace,
        alpha: bool,
        show_extras: bool,
    ) -> Result<Pixmap, Error> {
        unsafe {
            ffi_try!(mupdf_page_to_pixmap(
                context(),
                self.as_ptr().cast_mut(),
                ctm.into(),
                cs.inner,
                alpha,
                show_extras
            ))
        }
        .map(|inner| unsafe { Pixmap::from_raw(inner) })
    }

    pub fn to_svg(&self, ctm: &Matrix) -> Result<String, Error> {
        let inner = unsafe {
            ffi_try!(mupdf_page_to_svg(
                context(),
                self.as_ptr().cast_mut(),
                ctm.into(),
                ptr::null_mut()
            ))
        }?;
        let mut buf = unsafe { Buffer::from_raw(inner) };
        let mut svg = String::new();
        buf.read_to_string(&mut svg)?;
        Ok(svg)
    }

    pub fn to_svg_with_cookie(&self, ctm: &Matrix, cookie: &Cookie) -> Result<String, Error> {
        let inner = unsafe {
            ffi_try!(mupdf_page_to_svg(
                context(),
                self.as_ptr().cast_mut(),
                ctm.into(),
                cookie.inner
            ))
        }?;
        let mut buf = unsafe { Buffer::from_raw(inner) };
        let mut svg = String::new();
        buf.read_to_string(&mut svg)?;
        Ok(svg)
    }

    pub fn to_text_page(&self, flags: TextPageFlags) -> Result<TextPage, Error> {
        let opts = fz_stext_options {
            flags: flags.bits() as i32,
            scale: 0.0,
            clip: fz_rect {
                x0: 0.0,
                y0: 0.0,
                x1: 0.0,
                y1: 0.0,
            },
        };

        let inner = unsafe {
            ffi_try!(mupdf_new_stext_page_from_page(
                context(),
                self.as_ptr().cast_mut(),
                &opts
            ))?
        };

        let inner = unsafe { NonNull::new_unchecked(inner) };

        Ok(TextPage { inner })
    }

    pub fn to_display_list(&self, annotations: bool) -> Result<DisplayList, Error> {
        unsafe {
            ffi_try!(mupdf_page_to_display_list(
                context(),
                self.as_ptr().cast_mut(),
                annotations
            ))
        }
        .map(|inner| unsafe { DisplayList::from_raw(inner) })
    }

    pub fn run(&self, device: &Device, ctm: &Matrix) -> Result<(), Error> {
        unsafe {
            ffi_try!(mupdf_run_page(
                context(),
                self.as_ptr().cast_mut(),
                device.dev,
                ctm.into(),
                ptr::null_mut()
            ))
        }
    }

    pub fn run_with_cookie(
        &self,
        device: &Device,
        ctm: &Matrix,
        cookie: &Cookie,
    ) -> Result<(), Error> {
        unsafe {
            ffi_try!(mupdf_run_page(
                context(),
                self.as_ptr().cast_mut(),
                device.dev,
                ctm.into(),
                cookie.inner
            ))
        }
    }

    pub fn run_contents(&self, device: &Device, ctm: &Matrix) -> Result<(), Error> {
        unsafe {
            ffi_try!(mupdf_run_page_contents(
                context(),
                self.as_ptr().cast_mut(),
                device.dev,
                ctm.into(),
                ptr::null_mut()
            ))
        }
    }

    pub fn run_contents_with_cookie(
        &self,
        device: &Device,
        ctm: &Matrix,
        cookie: &Cookie,
    ) -> Result<(), Error> {
        unsafe {
            ffi_try!(mupdf_run_page_contents(
                context(),
                self.as_ptr().cast_mut(),
                device.dev,
                ctm.into(),
                cookie.inner
            ))
        }
    }

    pub fn run_annotations(&self, device: &Device, ctm: &Matrix) -> Result<(), Error> {
        unsafe {
            ffi_try!(mupdf_run_page_annots(
                context(),
                self.as_ptr().cast_mut(),
                device.dev,
                ctm.into(),
                ptr::null_mut()
            ))
        }
    }

    pub fn run_annotations_with_cookie(
        &self,
        device: &Device,
        ctm: &Matrix,
        cookie: &Cookie,
    ) -> Result<(), Error> {
        unsafe {
            ffi_try!(mupdf_run_page_annots(
                context(),
                self.as_ptr().cast_mut(),
                device.dev,
                ctm.into(),
                cookie.inner
            ))
        }
    }

    pub fn run_widgets(&self, device: &Device, ctm: &Matrix) -> Result<(), Error> {
        unsafe {
            ffi_try!(mupdf_run_page_widgets(
                context(),
                self.as_ptr().cast_mut(),
                device.dev,
                ctm.into(),
                ptr::null_mut()
            ))
        }
    }

    pub fn run_widgets_with_cookie(
        &self,
        device: &Device,
        ctm: &Matrix,
        cookie: &Cookie,
    ) -> Result<(), Error> {
        unsafe {
            ffi_try!(mupdf_run_page_widgets(
                context(),
                self.as_ptr().cast_mut(),
                device.dev,
                ctm.into(),
                cookie.inner
            ))
        }
    }

    pub fn links(&self) -> Result<LinkIter, Error> {
        let next = unsafe { ffi_try!(mupdf_load_links(context(), self.inner.as_ptr())) }?;

        let doc_ptr = unsafe { (*self.inner.as_ptr()).doc };
        let doc = unsafe { Document::from_raw(fz_keep_document(context(), doc_ptr)) };

        Ok(LinkIter { next, doc })
    }

    pub fn separations(&self) -> Result<Separations, Error> {
        unsafe { ffi_try!(mupdf_page_separations(context(), self.as_ptr().cast_mut())) }
            .map(|inner| unsafe { Separations::from_raw(inner) })
    }

    pub fn search(&self, needle: &str, hit_max: u32) -> Result<FzArray<Quad>, Error> {
        let c_needle = CString::new(needle)?;
        let hit_max = if hit_max < 1 { 16 } else { hit_max };
        let mut hit_count = 0;
        unsafe {
            ffi_try!(mupdf_search_page(
                context(),
                self.as_ptr().cast_mut(),
                c_needle.as_ptr(),
                hit_max as c_int,
                &mut hit_count
            ))
        }
        .and_then(|quads| unsafe { rust_vec_from_ffi_ptr(quads, hit_count) })
    }
}

impl Clone for Page {
    fn clone(&self) -> Self {
        let inner = self.inner;
        unsafe {
            fz_keep_page(context(), inner.as_ptr());
        }
        // SAFETY: If it was safe to construct `self`, it's safe to construct another instance of
        // `Self`.
        unsafe { Page::from_non_null(inner) }
    }
}

#[derive(Debug)]
pub struct LinkIter {
    next: *mut fz_link,
    doc: Document,
}

impl Iterator for LinkIter {
    type Item = Link;

    fn next(&mut self) -> Option<Self::Item> {
        if self.next.is_null() {
            return None;
        }

        let node = self.next;
        unsafe {
            self.next = (*node).next;
            let bounds = (*node).rect.into();
            let uri = CStr::from_ptr((*node).uri);
            let dest = LinkDestination::from_uri(&self.doc, uri).unwrap();

            Some(Link {
                bounds,
                dest,
                uri: uri.to_string_lossy().into_owned(),
            })
        }
    }
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Font {
    pub name: String,
    pub family: String,
    pub weight: String,
    pub style: String,
    pub size: u32,
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct BBox {
    pub x: i32,
    pub y: i32,
    pub w: u32,
    pub h: u32,
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Line {
    pub wmode: u32,
    pub bbox: BBox,
    pub font: Font,
    pub x: i32,
    pub y: i32,
    pub text: String,
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Block {
    pub r#type: String,
    pub bbox: BBox,
    pub lines: Vec<Line>,
}

// StructuredText
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct StextPage {
    pub blocks: Vec<Block>,
}

#[cfg(test)]
mod test {
    use crate::{document::test_document, Document, Matrix};

    #[test]
    #[cfg(feature = "serde")]
    fn test_get_stext_page_as_json() {
        let doc = test_document!("..", "files/dummy.pdf").unwrap();
        let page = doc.load_page(0).unwrap();
        let text_page = page.to_text_page(crate::TextPageFlags::empty()).unwrap();

        let json = text_page.to_json(1.0).unwrap();
        let stext_page: crate::page::StextPage = serde_json::from_str(json.as_str()).unwrap();

        for block in stext_page.blocks {
            if block.r#type == "text" {
                for line in block.lines {
                    assert_eq!(&line.text, &"Dummy PDF file".to_string());
                }
            }
        }
    }

    #[test]
    fn test_page_to_svg() {
        let doc = test_document!("..", "files/dummy.pdf").unwrap();
        let page0 = doc.load_page(0).unwrap();
        let svg = page0.to_svg(&Matrix::IDENTITY).unwrap();
        assert!(!svg.is_empty());
    }

    #[test]
    fn test_page_to_display_list() {
        let doc = test_document!("..", "files/dummy.pdf").unwrap();
        let page0 = doc.load_page(0).unwrap();
        let _dl = page0.to_display_list(true).unwrap();
        let _dl = page0.to_display_list(false).unwrap();
    }

    #[test]
    fn test_page_to_text_page() {
        use crate::TextPageFlags;

        let doc = test_document!("..", "files/dummy.pdf").unwrap();
        let page0 = doc.load_page(0).unwrap();
        let _tp = page0.to_text_page(TextPageFlags::PRESERVE_IMAGES).unwrap();
    }

    #[test]
    fn test_page_links() {
        use crate::Link;

        let doc = test_document!("..", "files/dummy.pdf").unwrap();
        let page0 = doc.load_page(0).unwrap();
        let links_iter = page0.links().unwrap();
        let links: Vec<Link> = links_iter.collect();
        assert_eq!(links.len(), 0);
    }

    #[test]
    fn test_page_separations() {
        let doc = test_document!("..", "files/dummy.pdf").unwrap();
        let page0 = doc.load_page(0).unwrap();
        let seps = page0.separations().unwrap();
        assert_eq!(seps.len(), 0);
    }

    #[test]
    fn test_page_search() {
        use crate::{Point, Quad};

        let doc = test_document!("..", "files/dummy.pdf").unwrap();
        let page0 = doc.load_page(0).unwrap();
        let hits = page0.search("Dummy", 1).unwrap();
        assert_eq!(hits.len(), 1);
        assert_eq!(
            &*hits,
            [Quad {
                ul: Point {
                    x: 56.8,
                    y: 69.32953
                },
                ur: Point {
                    x: 115.85159,
                    y: 69.32953
                },
                ll: Point {
                    x: 56.8,
                    y: 87.29713
                },
                lr: Point {
                    x: 115.85159,
                    y: 87.29713
                }
            }]
        );

        let hits = page0.search("Not Found", 1).unwrap();
        assert_eq!(hits.len(), 0);
    }
}