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
use crate::glob_defines::{
OP_PATH_CONST_3BEZIER_V1, OP_PATH_CONST_3BEZIER_V2, OP_PATH_CONST_4BEZIER,
OP_PATH_CONST_CLIP_NZ, OP_PATH_CONST_CLIP_EO, OP_PATH_CONST_LINE_TO,
OP_PATH_CONST_MOVE_TO, OP_PATH_PAINT_END,
OP_PATH_PAINT_FILL_NZ, OP_PATH_PAINT_FILL_EO,
OP_PATH_PAINT_FILL_STROKE_CLOSE_NZ, OP_PATH_PAINT_FILL_STROKE_CLOSE_EO,
OP_PATH_PAINT_STROKE, OP_PATH_PAINT_STROKE_CLOSE,
};
use crate::Point;
use lopdf;
use std::iter::{FromIterator, IntoIterator};
#[derive(Debug, Clone, Default)]
pub struct Line {
/// 2D Points for the line
pub points: Vec<(Point, bool)>,
/// Is the line closed or open?
pub is_closed: bool,
}
impl FromIterator<(Point, bool)> for Line {
fn from_iter<I: IntoIterator<Item = (Point, bool)>>(iter: I) -> Self {
let mut points = Vec::new();
for i in iter {
points.push(i);
}
Line {
points,
..Default::default()
}
}
}
impl Line {
/// Sets if the line is closed or not
#[inline]
pub fn set_closed(&mut self, is_closed: bool) {
self.is_closed = is_closed;
}
pub fn into_stream_op(self) -> Vec<lopdf::content::Operation> {
use lopdf::content::Operation;
let mut operations = Vec::<Operation>::new();
if self.points.is_empty() {
return operations;
};
operations.push(Operation::new(
OP_PATH_CONST_MOVE_TO,
vec![self.points[0].0.x.into(), self.points[0].0.y.into()],
));
// Skip first element
let mut current = 1;
let max_len = self.points.len();
// Loop over every points, determine if v, y, c or l operation should be used and build
// curve / line accordingly
while current < max_len {
let p1 = &self.points[current - 1]; // prev pt
let p2 = &self.points[current]; // current pt
if p1.1 && p2.1 {
// current point is a bezier handle
// valid bezier curve must have two sequential bezier handles
// we also can"t build a valid cubic bezier curve if the cuve contains less than
// four points. If p3 or p4 is marked as "next point is bezier handle" or not, doesn"t matter
if let Some(p3) = self.points.get(current + 1) {
if let Some(p4) = self.points.get(current + 2) {
if p1.0 == p2.0 {
// first control point coincides with initial point of curve
operations.push(Operation::new(
OP_PATH_CONST_3BEZIER_V1,
vec![p3.0.x.into(), p3.0.y.into(), p4.0.x.into(), p4.0.y.into()],
));
} else if p2.0 == p3.0 {
// first control point coincides with final point of curve
operations.push(Operation::new(
OP_PATH_CONST_3BEZIER_V2,
vec![p2.0.x.into(), p2.0.y.into(), p4.0.x.into(), p4.0.y.into()],
));
} else {
// regular bezier curve with four points
operations.push(Operation::new(
OP_PATH_CONST_4BEZIER,
vec![
p2.0.x.into(),
p2.0.y.into(),
p3.0.x.into(),
p3.0.y.into(),
p4.0.x.into(),
p4.0.y.into(),
],
));
}
current += 3;
continue;
}
}
}
// normal straight line
operations.push(Operation::new(
OP_PATH_CONST_LINE_TO,
vec![p2.0.x.into(), p2.0.y.into()],
));
current += 1;
}
// not filled, not closed but only stroked (regular path)
if self.is_closed {
operations.push(Operation::new(OP_PATH_PAINT_STROKE_CLOSE, vec![]));
} else {
operations.push(Operation::new(OP_PATH_PAINT_STROKE, vec![]));
}
operations
}
}
#[derive(Debug, Clone, Copy)]
pub enum WindingOrder {
EvenOdd,
NonZero,
}
impl Default for WindingOrder {
fn default() -> Self {
WindingOrder::NonZero
}
}
impl WindingOrder {
pub fn get_clip_op(&self) -> &'static str {
match self {
WindingOrder::NonZero => OP_PATH_CONST_CLIP_NZ,
WindingOrder::EvenOdd => OP_PATH_CONST_CLIP_EO,
}
}
pub fn get_fill_op(&self) -> &'static str {
match self {
WindingOrder::NonZero => OP_PATH_PAINT_FILL_NZ,
WindingOrder::EvenOdd => OP_PATH_PAINT_FILL_EO,
}
}
pub fn get_fill_stroke_close_op(&self) -> &'static str {
match self {
WindingOrder::NonZero => OP_PATH_PAINT_FILL_STROKE_CLOSE_NZ,
WindingOrder::EvenOdd => OP_PATH_PAINT_FILL_STROKE_CLOSE_EO,
}
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum PolygonMode {
Clip,
Fill,
Stroke,
FillStroke,
}
impl Default for PolygonMode {
fn default() -> PolygonMode {
PolygonMode::Fill
}
}
#[derive(Debug, Clone, Default)]
pub struct Polygon {
/// 2D Points for the line
pub rings: Vec<Vec<(Point, bool)>>,
/// What type of polygon is this?
pub mode: PolygonMode,
/// Winding order to use for constructing this polygon
pub winding_order: WindingOrder,
}
impl FromIterator<(Point, bool)> for Polygon {
fn from_iter<I: IntoIterator<Item = (Point, bool)>>(iter: I) -> Self {
let mut points = Vec::new();
for i in iter {
points.push(i);
}
Polygon {
rings: vec![points],
..Default::default()
}
}
}
impl Polygon {
pub fn into_stream_op(self) -> Vec<lopdf::content::Operation> {
use lopdf::content::Operation;
let mut operations = Vec::<Operation>::new();
if self.rings.is_empty() {
return operations;
};
for ring in self.rings.iter() {
operations.push(Operation::new(
OP_PATH_CONST_MOVE_TO,
vec![ring[0].0.x.into(), ring[0].0.y.into()],
));
// Skip first element
let mut current = 1;
let max_len = ring.len();
// Loop over every points, determine if v, y, c or l operation should be used and build
// curve / line accordingly
while current < max_len {
let p1 = &ring[current - 1]; // prev pt
let p2 = &ring[current]; // current pt
if p1.1 && p2.1 {
// current point is a bezier handle
// valid bezier curve must have two sequential bezier handles
// we also can"t build a valid cubic bezier curve if the cuve contains less than
// four points. If p3 or p4 is marked as "next point is bezier handle" or not, doesn"t matter
if let Some(p3) = ring.get(current + 1) {
if let Some(p4) = ring.get(current + 2) {
if p1.0 == p2.0 {
// first control point coincides with initial point of curve
operations.push(Operation::new(
OP_PATH_CONST_3BEZIER_V1,
vec![p3.0.x.into(), p3.0.y.into(), p4.0.x.into(), p4.0.y.into()],
));
} else if p2.0 == p3.0 {
// first control point coincides with final point of curve
operations.push(Operation::new(
OP_PATH_CONST_3BEZIER_V2,
vec![p2.0.x.into(), p2.0.y.into(), p4.0.x.into(), p4.0.y.into()],
));
} else {
// regular bezier curve with four points
operations.push(Operation::new(
OP_PATH_CONST_4BEZIER,
vec![
p2.0.x.into(),
p2.0.y.into(),
p3.0.x.into(),
p3.0.y.into(),
p4.0.x.into(),
p4.0.y.into(),
],
));
}
current += 3;
continue;
}
}
}
// normal straight line
operations.push(Operation::new(
OP_PATH_CONST_LINE_TO,
vec![p2.0.x.into(), p2.0.y.into()],
));
current += 1;
}
}
match self.mode {
PolygonMode::Clip => {
// set the path as a clipping path
operations.push(Operation::new(self.winding_order.get_clip_op(), vec![]));
},
PolygonMode::Fill => {
// is not stroked, only filled
// closed-ness doesn't matter in this case, an area is always closed
operations.push(Operation::new(self.winding_order.get_fill_op(), vec![]));
},
PolygonMode::Stroke => {
// same as line with is_closed = true
operations.push(Operation::new(OP_PATH_PAINT_STROKE_CLOSE, vec![]));
},
PolygonMode::FillStroke => {
operations.push(Operation::new(self.winding_order.get_fill_stroke_close_op(), vec![]));
},
}
if !operations.is_empty() {
operations.push(Operation::new(OP_PATH_PAINT_END, vec![]));
}
operations
}
}