mupdf 0.8.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
use std::fmt;

use crate::{
    ColorParams, Colorspace, Error, Image, LineCap, LineJoin, Matrix, NativeDevice, Path,
    PathWalker, Point, Quad, Rect, Shade, StrokeState, Text,
};

const POINT_EPSILON: f32 = 1.0e-4;

/// The painting operation represented by a [`Drawing`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DrawingType {
    /// Fill-only path.
    Fill,
    /// Stroke-only path.
    Stroke,
    /// Combined fill and stroke path.
    FillStroke,
}

/// One path item in a [`Drawing`], matching PyMuPDF's `Page.get_drawings()` items.
#[derive(Debug, Clone, PartialEq)]
pub enum DrawingItem {
    /// A line segment from the first point to the second point.
    Line(Point, Point),
    /// A cubic Bézier curve from the first point to the fourth point.
    ///
    /// The second and third points are the control points.
    Curve(Point, Point, Point, Point),
    /// An axis-aligned rectangle and its orientation.
    ///
    /// The orientation is `1` for anti-clockwise rectangles and `-1` for clockwise rectangles,
    /// matching PyMuPDF's `("re", rect, orientation)` item.
    Rect { rect: Rect, orientation: i32 },
    /// A quadrilateral detected from four consecutive stroke line segments.
    Quad(Quad),
}

/// Stroke dash pattern information for a [`Drawing`].
#[derive(Debug, Clone, PartialEq)]
pub struct DrawingDashes {
    /// Dash lengths, scaled into page coordinates.
    pub dashes: Vec<f32>,
    /// Dash phase, scaled into page coordinates.
    pub phase: f32,
}

impl fmt::Display for DrawingDashes {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.dashes.is_empty() {
            return write!(f, "[] {}", self.phase);
        }

        write!(f, "[")?;
        for (idx, dash) in self.dashes.iter().enumerate() {
            if idx > 0 {
                write!(f, " ")?;
            }
            write!(f, "{dash}")?;
        }
        write!(f, "] {}", self.phase)
    }
}

/// A vector drawing path extracted from a page.
///
/// This mirrors the default (`extended = false`) PyMuPDF `Page.get_drawings()` output: each
/// drawing contains path items plus the common fill / stroke properties used to paint them.
#[derive(Debug, Clone, PartialEq)]
pub struct Drawing {
    /// Path items: lines, curves, rectangles or quads.
    pub items: Vec<DrawingItem>,
    /// Whether this path fills, strokes, or does both.
    pub drawing_type: DrawingType,
    /// Bounding rectangle of the path geometry.
    pub rect: Rect,
    /// Stroke color converted to DeviceRGB, if this drawing is stroked.
    pub color: Option<[f32; 3]>,
    /// Fill color converted to DeviceRGB, if this drawing is filled.
    pub fill: Option<[f32; 3]>,
    /// Whether fill uses the even-odd rule, if this drawing is filled.
    pub even_odd: Option<bool>,
    /// PyMuPDF-compatible `closePath` flag for replaying the path.
    ///
    /// This is `false` when the extracted items already contain any required closing segment.
    pub close_path: Option<bool>,
    /// Stroke line width in page coordinates, if this drawing is stroked.
    pub width: Option<f32>,
    /// Stroke line caps `(start, dash, end)`, if this drawing is stroked.
    pub line_cap: Option<(LineCap, LineCap, LineCap)>,
    /// Stroke line join, if this drawing is stroked.
    pub line_join: Option<LineJoin>,
    /// Stroke dashes, if this drawing is stroked.
    pub dashes: Option<DrawingDashes>,
    /// Fill opacity, if this drawing is filled.
    pub fill_opacity: Option<f32>,
    /// Stroke opacity, if this drawing is stroked.
    pub stroke_opacity: Option<f32>,
    /// Sequence number in the page appearance stream.
    pub seqno: usize,
    /// Optional-content layer name active for this path.
    pub layer: Option<String>,
}

#[derive(Default)]
pub(crate) struct DrawingDevice {
    drawings: Vec<Drawing>,
    error: Option<Error>,
    seqno: usize,
    layer: Option<String>,
}

struct CollectedPath {
    items: Vec<DrawingItem>,
    rect: Rect,
    close_path: Option<bool>,
}

impl DrawingDevice {
    pub(crate) fn finish(&mut self) -> Result<Vec<Drawing>, Error> {
        if let Some(error) = self.error.take() {
            return Err(error);
        }
        Ok(std::mem::take(&mut self.drawings))
    }

    fn set_error(&mut self, error: Error) {
        if self.error.is_none() {
            self.error = Some(error);
        }
    }

    fn has_error(&self) -> bool {
        self.error.is_some()
    }

    fn collect_path(
        &mut self,
        path: &Path,
        ctm: &Matrix,
        drawing_type: DrawingType,
    ) -> Result<Option<CollectedPath>, Error> {
        let mut walker = DrawingPathWalker::new(ctm, drawing_type != DrawingType::Fill);
        path.walk(&mut walker)?;
        if walker.items.is_empty() {
            return Ok(None);
        }

        Ok(Some(CollectedPath {
            items: walker.items,
            rect: walker.path_rect.unwrap_or_default(),
            close_path: walker.close_path,
        }))
    }

    fn color_to_rgb(
        &mut self,
        color_space: &Colorspace,
        color: &[f32],
        cp: ColorParams,
    ) -> Option<[f32; 3]> {
        let rgb = Colorspace::device_rgb();
        match color_space.convert_color(color, &rgb, None, cp) {
            Ok(color) if color.len() >= 3 => Some([color[0], color[1], color[2]]),
            Ok(_) => {
                self.set_error(Error::InvalidArgument(
                    "color conversion returned fewer than 3 components".to_owned(),
                ));
                None
            }
            Err(error) => {
                self.set_error(error);
                None
            }
        }
    }

    fn append_or_merge(&mut self, drawing: Drawing) {
        if drawing.drawing_type == DrawingType::Stroke {
            if let Some(previous) = self.drawings.last_mut() {
                if previous.drawing_type == DrawingType::Fill && previous.items == drawing.items {
                    previous.drawing_type = DrawingType::FillStroke;
                    previous.color = drawing.color;
                    previous.width = drawing.width;
                    previous.line_cap = drawing.line_cap;
                    previous.line_join = drawing.line_join;
                    previous.dashes = drawing.dashes;
                    previous.stroke_opacity = drawing.stroke_opacity;
                    if previous.close_path.is_none() {
                        previous.close_path = drawing.close_path;
                    }
                    return;
                }
            }
        }

        self.drawings.push(drawing);
    }

    fn increase_seqno(&mut self) {
        self.seqno += 1;
    }
}

impl NativeDevice for DrawingDevice {
    fn fill_path(
        &mut self,
        path: &Path,
        even_odd: bool,
        ctm: Matrix,
        color_space: &Colorspace,
        color: &[f32],
        alpha: f32,
        cp: ColorParams,
    ) {
        if self.has_error() {
            return;
        }

        let path = match self.collect_path(path, &ctm, DrawingType::Fill) {
            Ok(Some(path)) => path,
            Ok(None) => return,
            Err(error) => {
                self.set_error(error);
                return;
            }
        };
        let Some(fill) = self.color_to_rgb(color_space, color, cp) else {
            return;
        };

        self.append_or_merge(Drawing {
            items: path.items,
            drawing_type: DrawingType::Fill,
            rect: path.rect,
            color: None,
            fill: Some(fill),
            even_odd: Some(even_odd),
            close_path: path.close_path,
            width: None,
            line_cap: None,
            line_join: None,
            dashes: None,
            fill_opacity: Some(alpha),
            stroke_opacity: None,
            seqno: self.seqno,
            layer: self.layer.clone(),
        });
        self.increase_seqno();
    }

    fn stroke_path(
        &mut self,
        path: &Path,
        stroke_state: &StrokeState,
        ctm: Matrix,
        color_space: &Colorspace,
        color: &[f32],
        alpha: f32,
        cp: ColorParams,
    ) {
        if self.has_error() {
            return;
        }

        let path = match self.collect_path(path, &ctm, DrawingType::Stroke) {
            Ok(Some(path)) => path,
            Ok(None) => return,
            Err(error) => {
                self.set_error(error);
                return;
            }
        };
        let Some(stroke_color) = self.color_to_rgb(color_space, color, cp) else {
            return;
        };
        let path_factor = (ctm.a * ctm.d - ctm.b * ctm.c).abs().sqrt();
        let dashes = stroke_state
            .dashes()
            .into_iter()
            .map(|dash| dash * path_factor)
            .collect();

        self.append_or_merge(Drawing {
            items: path.items,
            drawing_type: DrawingType::Stroke,
            rect: path.rect,
            color: Some(stroke_color),
            fill: None,
            even_odd: None,
            close_path: Some(path.close_path.unwrap_or(false)),
            width: Some(stroke_state.line_width() * path_factor),
            line_cap: Some((
                stroke_state.start_cap(),
                stroke_state.dash_cap(),
                stroke_state.end_cap(),
            )),
            line_join: Some(stroke_state.line_join()),
            dashes: Some(DrawingDashes {
                dashes,
                phase: stroke_state.dash_phase() * path_factor,
            }),
            fill_opacity: None,
            stroke_opacity: Some(alpha),
            seqno: self.seqno,
            layer: self.layer.clone(),
        });
        self.increase_seqno();
    }

    fn fill_text(
        &mut self,
        _text: &Text,
        _ctm: Matrix,
        _color_space: &Colorspace,
        _color: &[f32],
        _alpha: f32,
        _cp: ColorParams,
    ) {
        self.increase_seqno();
    }

    fn stroke_text(
        &mut self,
        _text: &Text,
        _stroke_state: &StrokeState,
        _ctm: Matrix,
        _color_space: &Colorspace,
        _color: &[f32],
        _alpha: f32,
        _cp: ColorParams,
    ) {
        self.increase_seqno();
    }

    fn ignore_text(&mut self, _text: &Text, _ctm: Matrix) {
        self.increase_seqno();
    }

    fn fill_shade(&mut self, _shade: &Shade, _ctm: Matrix, _alpha: f32, _cp: ColorParams) {
        self.increase_seqno();
    }

    fn fill_image(&mut self, _img: &Image, _ctm: Matrix, _alpha: f32, _cp: ColorParams) {
        self.increase_seqno();
    }

    fn fill_image_mask(
        &mut self,
        _img: &Image,
        _ctm: Matrix,
        _color_space: &Colorspace,
        _color: &[f32],
        _alpha: f32,
        _cp: ColorParams,
    ) {
        self.increase_seqno();
    }

    fn begin_layer(&mut self, name: &str) {
        self.layer = Some(name.to_owned());
    }

    fn end_layer(&mut self) {
        self.layer = None;
    }
}

struct DrawingPathWalker<'a> {
    ctm: &'a Matrix,
    detect_quads: bool,
    items: Vec<DrawingItem>,
    path_rect: Option<Rect>,
    last_point: Point,
    first_point: Point,
    have_move: bool,
    line_count: usize,
    close_path: Option<bool>,
}

impl<'a> DrawingPathWalker<'a> {
    fn new(ctm: &'a Matrix, detect_quads: bool) -> Self {
        Self {
            ctm,
            detect_quads,
            items: Vec::new(),
            path_rect: None,
            last_point: Point::new(0.0, 0.0),
            first_point: Point::new(0.0, 0.0),
            have_move: false,
            line_count: 0,
            close_path: None,
        }
    }

    fn transform_point(&self, x: f32, y: f32) -> Point {
        Point::new(x, y).transform(self.ctm)
    }

    fn include_point(&mut self, point: Point) {
        self.path_rect = Some(match self.path_rect {
            Some(rect) => Rect::new(
                rect.x0.min(point.x),
                rect.y0.min(point.y),
                rect.x1.max(point.x),
                rect.y1.max(point.y),
            ),
            None => Rect::new(point.x, point.y, point.x, point.y),
        });
    }

    fn check_quad(&mut self) -> bool {
        if self.items.len() < 4 {
            return false;
        }

        let len = self.items.len();
        let [DrawingItem::Line(ul, line0_end), DrawingItem::Line(ll, line1_end), DrawingItem::Line(lr, line2_end), DrawingItem::Line(ur, line3_end)] =
            &self.items[len - 4..]
        else {
            return false;
        };

        if !points_nearly_equal(*line0_end, *ll)
            || !points_nearly_equal(*line1_end, *lr)
            || !points_nearly_equal(*line2_end, *ur)
            || !points_nearly_equal(*line3_end, *ul)
        {
            return false;
        }

        let quad = Quad::new(*ul, *ur, *ll, *lr);
        self.items.truncate(len - 4);
        self.items.push(DrawingItem::Quad(quad));
        self.line_count = 0;
        true
    }

    fn check_rect(&mut self) -> bool {
        self.line_count = 0;
        if self.items.len() < 3 {
            return false;
        }

        let len = self.items.len();
        let (ll, lr, ur, ul) = match (&self.items[len - 3], &self.items[len - 1]) {
            (DrawingItem::Line(ll, lr), DrawingItem::Line(ur, ul)) => (*ll, *lr, *ur, *ul),
            _ => return false,
        };

        if !nearly_equal(ll.y, lr.y)
            || !nearly_equal(ll.x, ul.x)
            || !nearly_equal(ur.y, ul.y)
            || !nearly_equal(ur.x, lr.x)
        {
            return false;
        }

        let orientation = if ul.y < lr.y { 1 } else { -1 };
        let rect = Rect::new(
            ll.x.min(lr.x).min(ur.x).min(ul.x),
            ll.y.min(lr.y).min(ur.y).min(ul.y),
            ll.x.max(lr.x).max(ur.x).max(ul.x),
            ll.y.max(lr.y).max(ur.y).max(ul.y),
        );

        self.items.truncate(len - 3);
        self.items.push(DrawingItem::Rect { rect, orientation });
        true
    }
}

fn nearly_equal(a: f32, b: f32) -> bool {
    (a - b).abs() <= POINT_EPSILON
}

fn points_nearly_equal(a: Point, b: Point) -> bool {
    nearly_equal(a.x, b.x) && nearly_equal(a.y, b.y)
}

impl PathWalker for DrawingPathWalker<'_> {
    fn move_to(&mut self, x: f32, y: f32) {
        let point = self.transform_point(x, y);
        self.last_point = point;
        self.first_point = point;
        self.have_move = true;
        self.line_count = 0;
        self.include_point(point);
    }

    fn line_to(&mut self, x: f32, y: f32) {
        let point = self.transform_point(x, y);
        self.include_point(point);
        self.items.push(DrawingItem::Line(self.last_point, point));
        self.last_point = point;
        self.line_count += 1;
        if self.detect_quads && self.line_count == 4 {
            self.check_quad();
        }
    }

    fn curve_to(&mut self, cx1: f32, cy1: f32, cx2: f32, cy2: f32, ex: f32, ey: f32) {
        self.line_count = 0;
        let control1 = self.transform_point(cx1, cy1);
        let control2 = self.transform_point(cx2, cy2);
        let end = self.transform_point(ex, ey);
        self.include_point(control1);
        self.include_point(control2);
        self.include_point(end);
        self.items
            .push(DrawingItem::Curve(self.last_point, control1, control2, end));
        self.last_point = end;
    }

    fn close(&mut self) {
        if self.line_count == 3 && self.check_rect() {
            self.last_point = self.first_point;
            self.close_path = Some(false);
            self.have_move = false;
            return;
        }

        self.line_count = 0;
        if self.have_move {
            if !points_nearly_equal(self.first_point, self.last_point) {
                self.items
                    .push(DrawingItem::Line(self.last_point, self.first_point));
            }
            self.last_point = self.first_point;
            self.have_move = false;
            self.close_path = Some(false);
        } else {
            self.close_path = Some(true);
        }
    }
}