Skip to main content

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