pdfium_render/pdf/path/
segments.rs

1//! Defines the [PdfPathSegments] trait, a collection of all the `PdfPathSegment` objects in a
2//! path page object, a font glyph path, or a clip path.
3
4use crate::bindings::PdfiumLibraryBindings;
5use crate::error::PdfiumError;
6use crate::pdf::path::segment::PdfPathSegment;
7use std::ops::{Range, RangeInclusive};
8
9/// The zero-based index of a single [PdfPathSegment] inside its containing [PdfPathSegments] collection.
10pub type PdfPathSegmentIndex = u32;
11
12/// The collection of [PdfPathSegment] objects inside a path page object, a font glyph path,
13/// or a clip path.
14pub trait PdfPathSegments<'a> {
15    /// Returns the [PdfiumLibraryBindings] used by this [PdfPathSegments] collection.
16    fn bindings(&self) -> &'a dyn PdfiumLibraryBindings;
17
18    /// Returns the number of path segments in this [PdfPathSegments] collection.
19    fn len(&self) -> PdfPathSegmentIndex;
20
21    /// Returns `true` if this [PdfPathSegments] collection is empty.
22    #[inline]
23    fn is_empty(&self) -> bool {
24        self.len() == 0
25    }
26
27    /// Returns a Range from `0..(number of path segments)` for this [PdfPathSegments] collection.
28    #[inline]
29    fn as_range(&self) -> Range<PdfPathSegmentIndex> {
30        0..self.len()
31    }
32
33    /// Returns an inclusive Range from `0..=(number of path segments - 1)` for this [PdfPathSegments] collection.
34    #[inline]
35    fn as_range_inclusive(&self) -> RangeInclusive<PdfPathSegmentIndex> {
36        if self.is_empty() {
37            0..=0
38        } else {
39            0..=(self.len() - 1)
40        }
41    }
42
43    /// Returns a single [PdfPathSegment] from this [PdfPathSegments] collection.
44    fn get(&self, index: PdfPathSegmentIndex) -> Result<PdfPathSegment<'a>, PdfiumError>;
45
46    /// Returns an iterator over all the path segments in this [PdfPathSegments] collection.
47    fn iter(&'a self) -> PdfPathSegmentsIterator<'a>;
48}
49
50/// An iterator over all the [PdfPathSegment] objects in a [PdfPathSegments] collection.
51pub struct PdfPathSegmentsIterator<'a> {
52    segments: &'a dyn PdfPathSegments<'a>,
53    next_index: PdfPathSegmentIndex,
54}
55
56impl<'a> PdfPathSegmentsIterator<'a> {
57    #[inline]
58    pub(crate) fn new(segments: &'a dyn PdfPathSegments<'a>) -> Self {
59        PdfPathSegmentsIterator {
60            segments,
61            next_index: 0,
62        }
63    }
64}
65
66impl<'a> Iterator for PdfPathSegmentsIterator<'a> {
67    type Item = PdfPathSegment<'a>;
68
69    fn next(&mut self) -> Option<Self::Item> {
70        let next = self.segments.get(self.next_index);
71
72        self.next_index += 1;
73
74        next.ok()
75    }
76}