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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
pub(crate) mod internal {
// We want to make the PdfPageObjectPrivate trait private while providing a blanket
// implementation of PdfPageObjectCommon for any type T where T: PdfPageObjectPrivate.
// Rust complains, however, that by doing so we are leaking the private trait outside
// the crate.
// Instead of making the PdfPageObjectPrivate trait private, we leave it public but place it
// inside this pub(crate) module in order to prevent it from being visible outside the crate.
use crate::bindgen::{
FPDF_ANNOTATION, FPDF_DOCUMENT, FPDF_PAGE, FPDF_PAGEOBJECT, FS_MATRIX, FS_RECTF,
};
use crate::error::{PdfiumError, PdfiumInternalError};
use crate::pdf::document::page::annotation::objects::PdfPageAnnotationObjects;
use crate::pdf::document::page::object::group::PdfPageGroupObject;
use crate::pdf::document::page::object::{
PdfPageObject, PdfPageObjectCommon, PdfPageObjectOwnership, PdfPageObjectType,
};
use crate::pdf::document::page::objects::private::internal::PdfPageObjectsPrivate;
use crate::pdf::document::page::objects::PdfPageObjects;
use crate::pdf::document::page::{
PdfPage, PdfPageContentRegenerationStrategy, PdfPageIndexCache,
};
use crate::pdf::matrix::{PdfMatrix, PdfMatrixValue};
use crate::pdf::quad_points::PdfQuadPoints;
use crate::pdf::rect::PdfRect;
use crate::pdfium::PdfiumLibraryBindingsAccessor;
use std::os::raw::c_double;
#[cfg(any(feature = "pdfium_future", feature = "pdfium_7350"))]
use crate::pdf::document::page::objects::common::{PdfPageObjectIndex, PdfPageObjectsCommon};
/// Internal crate-specific functionality common to all [PdfPageObject] objects.
pub(crate) trait PdfPageObjectPrivate<'a>:
PdfPageObjectCommon<'a> + PdfiumLibraryBindingsAccessor<'a>
{
/// Returns the internal `FPDF_PAGEOBJECT` handle for this [PdfPageObject].
fn object_handle(&self) -> FPDF_PAGEOBJECT;
/// Returns the ownership hierarchy for this [PdfPageObject].
fn ownership(&self) -> &PdfPageObjectOwnership;
/// Sets the ownership hierarchy for this [PdfPageObject].
fn set_ownership(&mut self, ownership: PdfPageObjectOwnership);
/// Adds this [PdfPageObject] to the given [PdfPageObjects] collection.
#[inline]
fn add_object_to_page(
&mut self,
page_objects: &mut PdfPageObjects,
) -> Result<(), PdfiumError> {
self.add_object_to_page_handle(
page_objects.document_handle(),
page_objects.page_handle(),
)
}
fn add_object_to_page_handle(
&mut self,
document_handle: FPDF_DOCUMENT,
page_handle: FPDF_PAGE,
) -> Result<(), PdfiumError> {
unsafe {
self.bindings()
.FPDFPage_InsertObject(page_handle, self.object_handle());
}
self.set_ownership(PdfPageObjectOwnership::owned_by_page(
document_handle,
page_handle,
));
self.regenerate_content_after_mutation()
}
#[cfg(any(feature = "pdfium_future", feature = "pdfium_7350"))]
/// Adds this [PdfPageObject] to the given [PdfPageObjects] collection, inserting
/// it into the existing collection at the given positional index.
#[inline]
fn insert_object_on_page(
&mut self,
page_objects: &mut PdfPageObjects,
index: PdfPageObjectIndex,
) -> Result<(), PdfiumError> {
if index > page_objects.len() {
// FPDFPage_InsertObjectAtIndex() will return false if the given index
// is out of bounds. Avoid this.
self.add_object_to_page_handle(
page_objects.document_handle(),
page_objects.page_handle(),
)
} else {
self.insert_object_on_page_handle(
page_objects.document_handle(),
page_objects.page_handle(),
index,
)
}
}
#[cfg(any(feature = "pdfium_future", feature = "pdfium_7350"))]
fn insert_object_on_page_handle(
&mut self,
document_handle: FPDF_DOCUMENT,
page_handle: FPDF_PAGE,
index: PdfPageObjectIndex,
) -> Result<(), PdfiumError> {
if unsafe {
self.bindings()
.is_true(self.bindings().FPDFPage_InsertObjectAtIndex(
page_handle,
self.object_handle(),
index,
))
} {
self.set_ownership(PdfPageObjectOwnership::owned_by_page(
document_handle,
page_handle,
));
self.regenerate_content_after_mutation()
} else {
Err(PdfiumError::PdfiumLibraryInternalError(
PdfiumInternalError::Unknown,
))
}
}
/// Removes this [PdfPageObject] from the [PdfPageObjects] collection that contains it.
fn remove_object_from_page(&mut self) -> Result<(), PdfiumError> {
match self.ownership() {
PdfPageObjectOwnership::Page(ownership) => {
if self.bindings().is_true(unsafe {
self.bindings()
.FPDFPage_RemoveObject(ownership.page_handle(), self.object_handle())
}) {
match PdfPageIndexCache::get_content_regeneration_strategy_for_page(
ownership.document_handle(),
ownership.page_handle(),
) {
Some(PdfPageContentRegenerationStrategy::AutomaticOnEveryChange)
| None => {
PdfPage::regenerate_content_immut_for_handle(
ownership.page_handle(),
self.bindings(),
)?;
}
_ => {}
}
self.set_ownership(PdfPageObjectOwnership::unowned());
self.regenerate_content_after_mutation()
} else {
Err(PdfiumError::PdfiumLibraryInternalError(
PdfiumInternalError::Unknown,
))
}
}
_ => Err(PdfiumError::OwnershipNotAttachedToPage),
}
}
/// Adds this [PdfPageObject] to the given [PdfPageAnnotationObjects] collection.
fn add_object_to_annotation(
&mut self,
annotation_objects: &PdfPageAnnotationObjects,
) -> Result<(), PdfiumError> {
match annotation_objects.ownership() {
PdfPageObjectOwnership::AttachedAnnotation(ownership) => {
if self.bindings().is_true(unsafe {
self.bindings().FPDFAnnot_AppendObject(
ownership.annotation_handle(),
self.object_handle(),
)
}) {
self.set_ownership(PdfPageObjectOwnership::owned_by_attached_annotation(
ownership.document_handle(),
ownership.page_handle(),
ownership.annotation_handle(),
));
self.regenerate_content_after_mutation()
} else {
Err(PdfiumError::PdfiumLibraryInternalError(
PdfiumInternalError::Unknown,
))
}
}
PdfPageObjectOwnership::UnattachedAnnotation(ownership) => {
if self.bindings().is_true(unsafe {
self.bindings().FPDFAnnot_AppendObject(
ownership.annotation_handle(),
self.object_handle(),
)
}) {
self.set_ownership(PdfPageObjectOwnership::owned_by_unattached_annotation(
ownership.document_handle(),
ownership.annotation_handle(),
));
self.regenerate_content_after_mutation()
} else {
Err(PdfiumError::PdfiumLibraryInternalError(
PdfiumInternalError::Unknown,
))
}
}
_ => Err(PdfiumError::OwnershipNotAttachedToAnnotation),
}
}
/// Removes this [PdfPageObject] from the [PdfPageAnnotationsObjects] collection that contains it.
// We use inversion of control here so that PdfPageAnnotationsObjects doesn't need to care
// whether the page object being removed is a single object or a group.
fn remove_object_from_annotation(&mut self) -> Result<(), PdfiumError> {
match self.ownership() {
PdfPageObjectOwnership::AttachedAnnotation(ownership) => {
if let Some(index) =
self.get_index_for_annotation(ownership.annotation_handle())
{
if self.bindings().is_true(unsafe {
self.bindings()
.FPDFAnnot_RemoveObject(ownership.annotation_handle(), index)
}) {
match PdfPageIndexCache::get_content_regeneration_strategy_for_page(
ownership.document_handle(),
ownership.page_handle(),
) {
Some(
PdfPageContentRegenerationStrategy::AutomaticOnEveryChange,
)
| None => {
PdfPage::regenerate_content_immut_for_handle(
ownership.page_handle(),
self.bindings(),
)?;
}
_ => {}
}
self.set_ownership(PdfPageObjectOwnership::unowned());
self.regenerate_content_after_mutation()
} else {
Err(PdfiumError::PdfiumLibraryInternalError(
PdfiumInternalError::Unknown,
))
}
} else {
Err(PdfiumError::OwnershipNotAttachedToAnnotation)
}
}
PdfPageObjectOwnership::UnattachedAnnotation(ownership) => {
if let Some(index) =
self.get_index_for_annotation(ownership.annotation_handle())
{
if self.bindings().is_true(unsafe {
self.bindings()
.FPDFAnnot_RemoveObject(ownership.annotation_handle(), index)
}) {
self.set_ownership(PdfPageObjectOwnership::unowned());
self.regenerate_content_after_mutation()
} else {
Err(PdfiumError::PdfiumLibraryInternalError(
PdfiumInternalError::Unknown,
))
}
} else {
Err(PdfiumError::OwnershipNotAttachedToAnnotation)
}
}
_ => Err(PdfiumError::OwnershipNotAttachedToAnnotation),
}
}
// Perform a linear scan over the given annotation's page objects collection,
// returning the index of this object if it exists in the collection. Matching
// an object to its index in a page objects collection is necessary, for instance,
// when removing objects from annotations; Pdfium only allows removing objects
// from annotations by index.
fn get_index_for_annotation(&self, annotation_handle: FPDF_ANNOTATION) -> Option<i32> {
let mut result = None;
for i in 0..(unsafe { self.bindings().FPDFAnnot_GetObjectCount(annotation_handle) }) {
if self.object_handle()
== unsafe { self.bindings().FPDFAnnot_GetObject(annotation_handle, i) }
{
result = Some(i);
break;
}
}
result
}
/// Internal implementation of [PdfPageObjectCommon::has_transparency()].
fn has_transparency_impl(&self) -> bool {
let bindings = self.bindings();
bindings.is_true(unsafe { bindings.FPDFPageObj_HasTransparency(self.object_handle()) })
}
/// Internal implementation of [PdfPageObjectCommon::bounds()].
fn bounds_impl(&self) -> Result<PdfQuadPoints, PdfiumError> {
match PdfPageObjectType::from_pdfium(unsafe {
self.bindings().FPDFPageObj_GetType(self.object_handle())
} as u32)
{
Ok(PdfPageObjectType::Text) | Ok(PdfPageObjectType::Image) => {
// Text and image page objects support tight fitting bounds via the
// FPDFPageObject_GetRotatedBounds() function.
let mut points = PdfQuadPoints::ZERO.as_pdfium();
let result = unsafe {
self.bindings()
.FPDFPageObj_GetRotatedBounds(self.object_handle(), &mut points)
};
PdfQuadPoints::from_pdfium_as_result(result, points, self.bindings())
}
_ => {
// All other page objects support the FPDFPageObj_GetBounds() function.
let mut left = 0.0;
let mut bottom = 0.0;
let mut right = 0.0;
let mut top = 0.0;
let result = unsafe {
self.bindings().FPDFPageObj_GetBounds(
self.object_handle(),
&mut left,
&mut bottom,
&mut right,
&mut top,
)
};
PdfRect::from_pdfium_as_result(
result,
FS_RECTF {
left,
top,
right,
bottom,
},
self.bindings(),
)
.map(|r| r.to_quad_points())
}
}
}
/// Internal implementation of [PdfPageObjectCommon::transform()].
#[inline]
fn transform_impl(
&self,
a: PdfMatrixValue,
b: PdfMatrixValue,
c: PdfMatrixValue,
d: PdfMatrixValue,
e: PdfMatrixValue,
f: PdfMatrixValue,
) -> Result<(), PdfiumError> {
unsafe {
self.bindings().FPDFPageObj_Transform(
self.object_handle(),
a as c_double,
b as c_double,
c as c_double,
d as c_double,
e as c_double,
f as c_double,
);
}
self.regenerate_content_after_mutation()
}
/// Internal implementation of [PdfPageObjectCommon::matrix()].
fn get_matrix_impl(&self) -> Result<PdfMatrix, PdfiumError> {
let mut matrix = FS_MATRIX {
a: 0.0,
b: 0.0,
c: 0.0,
d: 0.0,
e: 0.0,
f: 0.0,
};
if self.bindings().is_true(unsafe {
self.bindings()
.FPDFPageObj_GetMatrix(self.object_handle(), &mut matrix)
}) {
Ok(PdfMatrix::from_pdfium(matrix))
} else {
Err(PdfiumError::PdfiumLibraryInternalError(
PdfiumInternalError::Unknown,
))
}
}
/// Resets the raw transformation matrix for this page object, overwriting
/// the existing transformation matrix.
fn reset_matrix_impl(&self, matrix: PdfMatrix) -> Result<(), PdfiumError> {
if self.bindings().is_true(unsafe {
self.bindings()
.FPDFPageObj_SetMatrix(self.object_handle(), &matrix.as_pdfium())
}) {
self.regenerate_content_after_mutation()
} else {
Err(PdfiumError::PdfiumLibraryInternalError(
PdfiumInternalError::Unknown,
))
}
}
/// Regenerate the containing page's content stream to reflect a change to the objects
/// within the page objects container. The page's content regeneration strategy is
/// taken into account.
fn regenerate_content_after_mutation(&self) -> Result<(), PdfiumError> {
let (document_handle, page_handle) = match self.ownership() {
PdfPageObjectOwnership::Page(ownership) => (
Some(ownership.document_handle()),
Some(ownership.page_handle()),
),
PdfPageObjectOwnership::AttachedAnnotation(ownership) => (
Some(ownership.document_handle()),
Some(ownership.page_handle()),
),
_ => (None, None),
};
if let (Some(document_handle), Some(page_handle)) = (document_handle, page_handle) {
if let Some(content_regeneration_strategy) =
PdfPageIndexCache::get_content_regeneration_strategy_for_page(
document_handle,
page_handle,
)
{
if content_regeneration_strategy
== PdfPageContentRegenerationStrategy::AutomaticOnEveryChange
{
PdfPage::regenerate_content_immut_for_handle(page_handle, self.bindings())
} else {
Ok(())
}
} else {
Err(PdfiumError::SourcePageIndexNotInCache)
}
} else {
Ok(())
}
}
/// Copies this [PdfPageObject] object into a new [PdfPageXObjectFormObject], then adds
/// the new form object to the page objects collection of the given [PdfPage],
/// returning the new form object.
fn copy_to_page_impl<'b>(
&mut self,
page: &mut PdfPage<'b>,
) -> Result<PdfPageObject<'b>, PdfiumError> {
let mut object = PdfPageObject::from_pdfium(
self.object_handle(),
*self.ownership(),
page.bindings(),
);
let (document_handle, page_handle) = match object.ownership() {
PdfPageObjectOwnership::Page(ownership) => (
Some(ownership.document_handle()),
Some(ownership.page_handle()),
),
PdfPageObjectOwnership::AttachedAnnotation(ownership) => (
Some(ownership.document_handle()),
Some(ownership.page_handle()),
),
_ => (None, None),
};
if let (Some(document_handle), Some(page_handle)) = (document_handle, page_handle) {
let mut group = PdfPageGroupObject::from_pdfium(document_handle, page_handle);
group.push(&mut object)?;
group.copy_to_page(page)
} else {
Err(PdfiumError::OwnershipNotAttachedToPage)
}
}
/// Drops the page object by calling `FPDFPageObj_Destroy()`, freeing held memory.
/// This will this object's `FPDF_OBJECT` handle. If the page object is attached
/// to a page or an annotation, no action will be taken; Pdfium will manage
/// deallocation of the object's memory automatically.
#[inline]
fn drop_impl(&self) {
if !self.ownership().is_owned() {
// Responsibility for de-allocation lies with us, not Pdfium, since
// the object is not attached to a page or an annotation.
unsafe {
self.bindings().FPDFPageObj_Destroy(self.object_handle());
}
}
}
}
}
#[cfg(test)]
mod tests {
use crate::prelude::*;
use crate::utils::test::test_bind_to_pdfium;
#[test]
fn test_object_get_translation() -> Result<(), PdfiumError> {
// Tests to make sure we can retrieve the correct horizontal and vertical translation deltas
// from an object after applying a translation transformation.
let pdfium = test_bind_to_pdfium();
let mut document = pdfium.create_new_pdf()?;
let mut page = document
.pages_mut()
.create_page_at_start(PdfPagePaperSize::a4())?;
let mut object = PdfPagePathObject::new_rect(
&document,
PdfRect::new_from_values(100.0, 100.0, 400.0, 400.0),
Some(PdfColor::RED),
Some(PdfPoints::new(1.0)),
None,
)?;
object.translate(PdfPoints::new(250.0), PdfPoints::new(350.0))?;
let object = page.objects_mut().add_path_object(object)?;
assert_eq!(object.get_horizontal_translation().value, 250.0);
assert_eq!(object.get_vertical_translation().value, 350.0);
assert_eq!(object.get_horizontal_scale(), 1.0);
assert_eq!(object.get_vertical_scale(), 1.0);
assert_eq!(object.get_rotation_clockwise_degrees(), 0.0);
Ok(())
}
#[test]
fn test_object_get_scale() -> Result<(), PdfiumError> {
// Tests to make sure we can retrieve the correct horizontal and vertical scale factors
// from an object after applying a scale transformation.
let pdfium = test_bind_to_pdfium();
let mut document = pdfium.create_new_pdf()?;
let mut page = document
.pages_mut()
.create_page_at_start(PdfPagePaperSize::a4())?;
let mut object = PdfPagePathObject::new_rect(
&document,
PdfRect::new_from_values(100.0, 100.0, 400.0, 400.0),
Some(PdfColor::RED),
Some(PdfPoints::new(1.0)),
None,
)?;
object.scale(1.75, 2.25)?;
let object = page.objects_mut().add_path_object(object)?;
assert_eq!(object.get_horizontal_scale(), 1.75);
assert_eq!(object.get_vertical_scale(), 2.25);
assert_eq!(object.get_horizontal_translation().value, 0.0);
assert_eq!(object.get_vertical_translation().value, 0.0);
assert_eq!(object.get_rotation_clockwise_degrees(), 0.0);
Ok(())
}
#[test]
fn test_object_get_rotation() -> Result<(), PdfiumError> {
// Tests to make sure we can retrieve the correct clockwise rotation angle from an object
// after applying a rotation transformation.
let pdfium = test_bind_to_pdfium();
let mut document = pdfium.create_new_pdf()?;
let mut page = document
.pages_mut()
.create_page_at_start(PdfPagePaperSize::a4())?;
let mut object = PdfPagePathObject::new_rect(
&document,
PdfRect::new_from_values(100.0, 100.0, 400.0, 400.0),
Some(PdfColor::RED),
Some(PdfPoints::new(1.0)),
None,
)?;
object.rotate_clockwise_degrees(35.0)?;
let object = page.objects_mut().add_path_object(object)?;
assert_eq!(object.get_rotation_clockwise_degrees(), 35.0);
assert_eq!(object.get_horizontal_translation().value, 0.0);
assert_eq!(object.get_vertical_translation().value, 0.0);
assert_eq!(object.get_horizontal_scale(), 0.8191520571708679); // Rotating affects the scale factors
assert_eq!(object.get_vertical_scale(), 0.8191520571708679); // Rotating affects the scale factors
Ok(())
}
#[test]
fn test_object_get_skew() -> Result<(), PdfiumError> {
// Tests to make sure we can retrieve the correct skew axes values from an object
// after applying a skew transformation.
let pdfium = test_bind_to_pdfium();
let mut document = pdfium.create_new_pdf()?;
let mut page = document
.pages_mut()
.create_page_at_start(PdfPagePaperSize::a4())?;
let mut object = PdfPagePathObject::new_rect(
&document,
PdfRect::new_from_values(100.0, 100.0, 400.0, 400.0),
Some(PdfColor::RED),
Some(PdfPoints::new(1.0)),
None,
)?;
object.skew_degrees(15.5, 25.5)?;
let object = page.objects_mut().add_path_object(object)?;
assert_eq!(
(object.get_x_axis_skew_degrees() * 10.0).round() / 10.0,
15.5
); // Handles the returned value being a tiny bit off, e.g. 15.4999 instead of 15.5
assert_eq!(
(object.get_y_axis_skew_degrees() * 10.0).round() / 10.0,
25.5
); // Handles the returned value being a tiny bit off, e.g. 25.4999 instead of 25.5
assert_eq!(object.get_horizontal_translation().value, 0.0);
assert_eq!(object.get_vertical_translation().value, 0.0);
assert_eq!(object.get_horizontal_scale(), 1.0);
assert_eq!(object.get_vertical_scale(), 1.0);
assert_eq!(
(object.get_rotation_counter_clockwise_degrees() * 10.0).round() / 10.0,
15.5
); // Rotation angle will be the same as the x axis skew angle.
Ok(())
}
}