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
//! Defines the [PdfClipPath] struct, exposing functionality related to a clip path.
#[doc(hidden)]
use crate::bindgen::FPDF_CLIPPATH;
use crate::bindings::PdfiumLibraryBindings;
use crate::error::{PdfiumError, PdfiumInternalError};
use crate::path_segment::PdfPathSegment;
use crate::path_segments::{PdfPathSegmentIndex, PdfPathSegments, PdfPathSegmentsIterator};
use std::convert::TryInto;
use std::os::raw::c_int;
// TODO: AJRC - 22/10/22 - "clip path" is a slight misnomer, since a single clip path can actually
// contain zero or more path objects. Each path object can then return a PdfClipPathSegments object
// that implements the PdfPathSegments trait. Want to complete implementation of top-level PdfClipPath
// collection, then add clip path support to pages and page objects.
#[allow(dead_code)]
// The PdfClipPath struct is not currently used, but we expect it to be in future
pub struct PdfClipPath {
// TODO: AJRC - 22/10/22 - this will contain a collection of paths
// each of which can return a PdfClipPathSegments object
}
/// The collection of [PdfPathSegment] objects inside a single path within a clip path.
pub struct PdfClipPathSegments<'a> {
handle: FPDF_CLIPPATH,
path_index: c_int,
bindings: &'a dyn PdfiumLibraryBindings,
}
impl<'a> PdfClipPathSegments<'a> {
#[inline]
#[allow(dead_code)]
// The from_pdfium() function is not currently used, but we expect it to be in future
pub(crate) fn from_pdfium(
handle: FPDF_CLIPPATH,
path_index: c_int,
bindings: &'a dyn PdfiumLibraryBindings,
) -> Self {
Self {
handle,
path_index,
bindings,
}
}
}
impl<'a> PdfPathSegments<'a> for PdfClipPathSegments<'a> {
#[inline]
fn bindings(&self) -> &'a dyn PdfiumLibraryBindings {
self.bindings
}
#[inline]
fn len(&self) -> PdfPathSegmentIndex {
self.bindings()
.FPDFClipPath_CountPathSegments(self.handle, self.path_index)
.try_into()
.unwrap_or(0)
}
fn get(&self, index: PdfPathSegmentIndex) -> Result<PdfPathSegment<'a>, PdfiumError> {
let handle = self.bindings().FPDFClipPath_GetPathSegment(
self.handle,
self.path_index,
index as c_int,
);
if handle.is_null() {
if let Some(error) = self.bindings().get_pdfium_last_error() {
Err(PdfiumError::PdfiumLibraryInternalError(error))
} else {
// This would be an unusual situation; a null handle indicating failure,
// yet Pdfium's error code indicates success.
Err(PdfiumError::PdfiumLibraryInternalError(
PdfiumInternalError::Unknown,
))
}
} else {
Ok(PdfPathSegment::from_pdfium(handle, self.bindings()))
}
}
#[inline]
fn iter(&'a self) -> PdfPathSegmentsIterator<'a> {
PdfPathSegmentsIterator::new(self)
}
}