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
use crate::bindgen::FS_RECTF;
use crate::bindings::PdfiumLibraryBindings;
use crate::error::PdfiumError;
use crate::page::PdfRect;
use crate::page_text::PdfPageText;
use crate::prelude::PdfPageTextSegment;
use std::os::raw::c_int;
pub type PdfPageTextSegmentIndex = usize;
pub struct PdfPageTextSegments<'a> {
text: &'a PdfPageText<'a>,
characters: i32,
bindings: &'a dyn PdfiumLibraryBindings,
}
impl<'a> PdfPageTextSegments<'a> {
#[inline]
pub(crate) fn new(
text: &'a PdfPageText<'a>,
characters: i32,
bindings: &'a dyn PdfiumLibraryBindings,
) -> Self {
PdfPageTextSegments {
text,
characters,
bindings,
}
}
#[inline]
pub fn len(&self) -> PdfPageTextSegmentIndex {
self.bindings
.FPDFText_CountRects(*self.text.handle(), 0, self.characters) as usize
}
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
#[inline]
pub fn get(&self, index: PdfPageTextSegmentIndex) -> Result<PdfPageTextSegment, PdfiumError> {
if index >= self.len() {
return Err(PdfiumError::TextSegmentIndexOutOfBounds);
}
let mut left = 0.0;
let mut bottom = 0.0;
let mut right = 0.0;
let mut top = 0.0;
let result = self.bindings.FPDFText_GetRect(
*self.text.handle(),
index as c_int,
&mut left,
&mut top,
&mut right,
&mut bottom,
);
PdfRect::from_pdfium_as_result(
result,
FS_RECTF {
left: left as f32,
top: top as f32,
right: right as f32,
bottom: bottom as f32,
},
self.bindings,
)
.map(|rect| PdfPageTextSegment::from_pdfium(self.text, rect))
}
#[inline]
pub fn iter(&self) -> PdfPageTextSegmentsIterator {
PdfPageTextSegmentsIterator::new(self)
}
}
pub struct PdfPageTextSegmentsIterator<'a> {
segments: &'a PdfPageTextSegments<'a>,
next_index: PdfPageTextSegmentIndex,
}
impl<'a> PdfPageTextSegmentsIterator<'a> {
#[inline]
pub(crate) fn new(segments: &'a PdfPageTextSegments<'a>) -> Self {
PdfPageTextSegmentsIterator {
segments,
next_index: 0,
}
}
}
impl<'a> Iterator for PdfPageTextSegmentsIterator<'a> {
type Item = PdfPageTextSegment<'a>;
fn next(&mut self) -> Option<Self::Item> {
let next = self.segments.get(self.next_index);
self.next_index += 1;
next.ok()
}
}