Annot.pm

filter: Statementstmt 97.2% 71 / 73 Branchbran 66.6% 16 / 24 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 89.1% 99 / 111 CC 14 SCAR 14.5
CC
14
Cov
89%
appendAnnotation
7.1
_appendAppearanceObject
5.1
_appendPopupObject
3.0
CRAP 4.2
↑ Summary Annot/Parsed.pm »
1 package CAM::PDF::Annot;
2  
3 2 ▶use 5.010000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '0.10';
8  
9 2 ▶use base qw(CAM::PDF);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 CAM::PDF::Annot - Perl extension for appending annotations on PDFs
15  
16 =head1 SYNOPSIS
17  
18 use strict;
19 use CAM::PDF::Annot;
20 my $pdf = CAM::PDF::Annot->new( 'pdf1.pdf' );
21 my $otherDoc = CAM::PDF::Annot->new( 'pdf2.pdf' );
22 for my $page ( 1 .. $pdf->numPages() ) {
23 my %refs;
24 for my $annotRef ( @{$pdf->getAnnotations( $page )} ) {
25 $otherDoc->appendAnnotation( $page, $pdf, $annotRef, \%refs );
26 }
27 }
28 $otherDoc->output('pdf_merged.pdf');
29  
30  
31 =head1 DESCRIPTION
32  
33 CAM::PDF::Annot is an extension to C<CAM::PDF> to ease the appending of
34 Annotation objects to pdf documents.
35  
36 =head2 EXPORT
37  
38 This module does not export any functions.
39  
40 =cut
41  
42 =head2 METHODS
43  
44 =over
45  
46 =item CAM::PDF::Annot->new( 'file.pdf' );
47  
48 Constructor method, same as C<CAM::PDF>.
49  
50 =cut
51  
52 #sub new {
53 # my $class = shift;
54 # my $self = $class->SUPER::new( @_ );
55 #
56 # bless $self, $class;
57 #}
58  
59 =item $doc->appendAnnotation($page, $doc, $annotRef, $refKeys) *NEW*
60  
61 Duplicate an annotation object from another PDF document and add it to this
62 document. It also copies its appearance object and Popup object. In case
63 this is a Text Subtype Annot object (a Reply to another Annot object) it
64 recurses to append the Annot object it refers to (using the IRT reference
65 of the object).
66  
67 It was only tested for annotations of /Type /Annot and /Subtype
68 /Square, /Circle, /Polygon and /Text. It is hardcoded to not allow any other
69 subtypes (sometime in the future this may change).
70  
71 It takes a hash reference C<$refKeys> and adds the altered keys so it can
72 be used across calls and update references across objects (and avoid
73 adding the same object more than once).
74  
75 =cut
76  
77 28 ▶sub appendAnnotation($$$\%) {
Subroutine appendAnnotation: called CC=7 SCAR=19.6
Pod documented
78 28 my ( $self, $page, $otherDoc, $otherAnnotRef, $refKeys ) = @_;
79  
80 # Sanity check: it only appends objects of /Type /Annot /Subtype /Square|Circle|Polygon|Text
81 # returns an empty hash reference
82 28 ▶ return {} if ( $otherDoc->getValue( $otherAnnotRef )->{Subtype}{value} !~ /(Square|Circle|Polygon|Text)/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $otherDoc->getValue($otherAnnotRef)->{"Subtype"}{"value"} =~ /(Square|Circle|Polygon|Text)/ 14 14 28
83  
84 # If document does not have annots in this page, create an annots property
85 14 ▶ unless ( exists $self->getPage( $page )->{Annots} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->getPage($page)->{"Annots"}) 2 12 14
86 2 $self->getPage( $page )->{Annots} = CAM::PDF::Node->new('array',[], scalar $self->getPageObjnum( $page ),'0');
87 }
88  
89 # get this page's annotation object it will be widely used
90 14 my $annots = $self->getPage( $page )->{Annots};
91 # dereferences the previous value in case the annots object was originaly a reference to the object itself...
92 14 ▶ $annots = $self->dereference( $annots->{value} )->{value} while $annots->{type} eq 'reference';
Branch: 1 branch
BranchTrueFalseTotal
$annots->{"type"} eq "reference" and do { $annots = $self->dereference($annots->{"value"})->{"value"} } 0 14 14
93  
94 # append the annot object based on the object number
95 14 my $newkey = $self->appendObject( $otherDoc, $otherAnnotRef->{value}, 0 );
96 # store the refkey for later
97 14 $$refKeys{$otherAnnotRef->{value}} = $newkey;
98  
99 # append a reference to this annot to the annotations object of this page
100 14 my $annotRef = CAM::PDF::Node->new('reference', "$newkey", $self->getPageObjnum( $page ), '0');
101 14 push @{$annots->{value}}, $annotRef;
102  
103 # Append the appearance object (if it exists)
104 14 $self->_appendAppearanceObject( $otherDoc, $annotRef, $refKeys );
105  
106 # Append the popup object (if it exists)
107 14 $self->_appendPopupObject( $page, $otherDoc, $annotRef, { $otherAnnotRef->{value} => $newkey }, $refKeys );
108  
109 # Verify if it has an IRT reference (meaning, if it refers to another annotation)
110 14 my $annotVal = $self->getValue( $annotRef );
111 14 ▶ if ( exists $annotVal->{IRT} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $annotVal->{"IRT"}) 8 6 14
112 # Check if it is a reference to an already added object
113 8 ▶ unless ( exists $refKeys->{$annotVal->{IRT}{value}} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$refKeys{$annotVal->{"IRT"}{"value"}}) 0 8 8
114 # In this case the IRT must be added
115 0 $self->appendAnnotation( $page, $otherDoc, $annotVal->{IRT}, $refKeys );
116 }
117 }
118  
119 # Since the annots object was altered, let's flag it
120 # I dont know if it is necessary to store it in cache but it seems to work
121 14 $self->{objcache}{$annots->{objnum}} = $self->dereference( $annots->{objnum} );
122 14 $self->{changes}{$annots->{objnum}} = 1;
123 14 $self->{versions}{$annots->{objnum}} = -1;
124  
125 # Now, update all the references for the object
126 14 $self->changeRefKeys( $self->{objcache}{$newkey}, $refKeys );
127  
128 14 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 14 14
129 0 return ($newkey, %$refKeys);
130 }
131 else {
132 14 return $newkey;
133 }
134 }
135  
136 14 ▶sub _appendAppearanceObject() {
Subroutine _appendAppearanceObject: called CC=5 SCAR=16.2
137 14 my ( $self, $otherDoc, $annotRef, $refKeys ) = @_;
138 14 my $annotVal = $self->getValue( $annotRef );
139 14 my %refs =();
140  
141 # Check if this annot has a reference to an APeareance object
142 # (it is expected it will have it...)
143 14 ▶ if ( exists $annotVal->{AP} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $annotVal->{"AP"}) 14 0 14
144 14 my $ap = $self->getValue( $annotVal->{AP} );
145 # Check if it wasn't already added before
146 14 ▶ unless ( exists $refKeys->{$ap->{N}{value}} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$refKeys{$ap->{"N"}{"value"}}) 14 0 14
147 14 my $apNkey = $self->appendObject( $otherDoc, $ap->{N}{value}, 0 );
148  
149 # keep track of this addition
150 14 $$refKeys{$ap->{N}{value}} = $apNkey;
151 14 $refs{$ap->{N}{value}} = $apNkey;
152 }
153 # Apparently only for reply cases (in which the APearance object seems to have more than one element
154 14 ▶ if ( exists $ap->{D} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ap->{"D"}) 8 6 14
155 8 ▶ unless ( exists $refKeys->{$ap->{D}{value}} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$refKeys{$ap->{"D"}{"value"}}) 8 0 8
156 8 my $apDkey = $self->appendObject( $otherDoc, $ap->{D}{value}, 0 );
157
158 # keep track of this addition
159 8 $$refKeys{$ap->{D}{value}} = $apDkey;
160 8 $refs{$ap->{D}{value}} = $apDkey;
161 }
162 }
163 }
164 14 return %refs;
165 }
166  
167 14 ▶sub _appendPopupObject() {
Subroutine _appendPopupObject: called CC=3 SCAR=11.0
168 14 my ( $self, $page, $otherDoc, $annotRef, $parentKeys, $refKeys ) = @_;
169 14 my $annotVal = $self->getValue( $annotRef );
170 14 my $annots = $self->getPage( $page )->{Annots};
171 14 my %refs =();
172  
173 # Now check if it has a reference to a popup object
174 # (it is bound to have it...)
175 # And also check if it wasnt already added
176 14 ▶ if ( exists $annotVal->{Popup} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $annotVal->{"Popup"}) 14 0 14
177 14 ▶ unless ( exists $refKeys->{$annotVal->{Popup}{value}} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$refKeys{$annotVal->{"Popup"}{"value"}}) 14 0 14
178 14 my $pupkey = $self->appendObject( $otherDoc, $annotVal->{Popup}{value}, 0 );
179 14 $$refKeys{$annotVal->{Popup}{value}} = $pupkey;
180 14 $refs{$annotVal->{Popup}{value}} = $pupkey;
181
182 # change its parent reference
183 14 $self->changeRefKeys( $self->{objcache}{$pupkey}, $parentKeys );
184  
185 # it also gets a place on the Annots property of the page object
186 14 my $pupRef = $self->copyObject( $annotVal->{Popup} );
187 # change the keys in the newly created one to reflect the appended annotation object
188 14 $self->changeRefKeys( $pupRef, { $pupRef->{value} => $pupkey } );
189 14 $self->setObjNum( $pupRef, $annots->{objnum} );
190 14 push @{$annots->{value}}, $pupRef;
191 }
192 }
193 14 return %refs;
194 }
195  
196 =item $doc->getAnnotations( $page )
197  
198 Returns an array reference to the Annots array of the page. The array
199 contains CAM::PDF::Nodes (see C<CAM::PDF>) of type 'reference' refering
200 to the annotations.
201  
202 =cut
203  
204 4 ▶sub getAnnotations($) {
Subroutine getAnnotations: called CC=2 SCAR=7.2
Pod documented
205 4 my ( $self, $p ) = @_;
206 4 ▶ return $self->getValue( $self->getPage( $p )->{Annots} ) || [];
Condition50%1/2
$self->getValue($self->getPage($p)->{'Annots'}) || []
l!l
40
MC/DC0%0/1
$self->getValue($self->getPage($p)->{'Annots'}) || []
$self->getValue($self->getPage($p)->{'Annots'})
Truth table50%1/2
A
A= $self->getValue($self->getPage($p)->{'Annots'})
Aresult
11
00
207 }
208  
209 2 1;
210 __END__
↑ Summary Annot/Parsed.pm »