PDF/Objind.pm

filter: Statementstmt 47.3% 36 / 76 Branchbran 36.1% 13 / 36 Conditioncond 5.5% 1 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 53.3% 8 / 15 Podpod 92.3% 12 / 13 totaltotal 41.6% 70 / 168 CC 29 SCAR 36.5
CC
29
Cov
38%
copy
132.0
empty
12.0
merge
12.0
CRAP 38.6
« PDF/File.pm ↑ Summary PDF/TTFont0.pm »
1 package Text::PDF::Objind;
2  
3 =head1 NAME
4  
5 Text::PDF::Objind - PDF indirect object reference. Also acts as an abstract
6 superclass for all elements in a PDF file.
7  
8 =head1 INSTANCE VARIABLES
9  
10 Instance variables differ from content variables in that they all start with
11 a space.
12  
13 =over
14  
15 =item parent
16  
17 For an object which is a reference to an object in some source, this holds the
18 reference to the source object, so that should the reference have to be
19 de-referenced, then we know where to go and get the info.
20  
21 =item objnum (R)
22  
23 The object number in the source (only for object references)
24  
25 =item objgen (R)
26  
27 The object generation in the source
28  
29 There are other instance variables which are used by the parent for file control.
30  
31 =item isfree
32  
33 This marks whether the object is in the free list and available for re-use as
34 another object elsewhere in the file.
35  
36 =item nextfree
37  
38 Holds a direct reference to the next free object in the free list.
39  
40 =back
41  
42 =head1 METHODS
43  
44 =cut
45  
46 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use vars qw(@inst %inst $uidc);
Subroutine BEGIN: called CC=1 SCAR=0.0
48 # no warnings qw(uninitialized);
49  
50 # protected keys during emptying and copying, etc.
51  
52 1 @inst = qw(parent objnum objgen isfree nextfree uid);
53 1 map {$inst{" $_"} = 1} @inst;
54 1 $uidc = "pdfuid000";
55  
56  
57 =head2 Text::PDF::Objind->new()
58  
59 Creates a new indirect object
60  
61 =cut
62  
63 sub new
64 {
65 11 ▶ my ($class) = @_;
Subroutine new: called CC=2 SCAR=7.6
Pod documented
66 11 my ($self) = {};
67  
68 11 ▶ bless $self, ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0110
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
69 }
70  
71 =head2 uid
72  
73 Returns a Unique id for this object, creating one if it didn't have one before
74  
75 =cut
76  
77 sub uid
78 129 ▶{ $_[0]->{' uid'} || ($_[0]->{' uid'} = $uidc++); }
Subroutine uid: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{" uid"} or $_[0]{" uid"} = $Text::PDF::Objind::uidc++ 29 100 129
79  
80 =head2 $r->release
81  
82 Releases ALL of the memory used by this indirect object, and all of its
83 component/child objects. This method is called automatically by
84 'C<Text::PDF::File-E<gt>release>' (so you don't have to call it yourself).
85  
86 B<NOTE>, that it is important that this method get called at some point prior
87 to the actual destruction of the object. Internally, PDF files have an
88 enormous amount of cross-references and this causes circular references within
89 our own internal data structures. Calling 'C<release()>' forces these circular
90 references to be cleaned up and the entire internal data structure purged.
91  
92 B<Developer note:> As part of the brute-force cleanup done here, this method
93 will throw a warning message whenever unexpected key values are found within
94 the C<Text::PDF::Objind> object. This is done to help ensure that unexpected
95 and unfreed values are brought to your attention, so you can bug us to keep the
96 module updated properly; otherwise the potential for memory leaks due to
97 dangling circular references will exist.
98  
99 =cut
100  
101 sub release
102 {
103 35 ▶ my ($self, $force) = @_;
Subroutine release: called CC=8 SCAR=22.6
Pod documented
104 35 my (@tofree);
105  
106 # delete stuff that we know we can, here
107  
108 35 ▶ if ($force)
Branch: 1 branch
BranchTrueFalseTotal
if ($force) { } 0 35 35
109 {
110 0 foreach my $key (keys %{$self})
111 {
112 0 push(@tofree,$self->{$key});
113 0 $self->{$key}=undef;
114 0 delete($self->{$key});
115 }
116 }
117 else
118 35 { @tofree = map { delete $self->{$_} } keys %{$self}; }
119  
120 35 ▶ while (my $item = shift @tofree)
Branch: 1 branch
BranchTrueFalseTotal
if (my $item = shift @tofree) 105 35 140
121 {
122 105 my $ref = ref($item);
123 105 ▶ if (UNIVERSAL::can($ref, 'release')) # $ref was $item
Branch: 3 branches
BranchTrueFalseTotal
if (UNIVERSAL::can($ref, 'release')) { } 30 75 105
elsif ($ref eq 'ARRAY') { } 6 69 75
elsif (UNIVERSAL::isa($ref, 'HASH')) { } 0 69 69
124 30 { $item->release($force); }
125 elsif ($ref eq 'ARRAY')
126 6 { push( @tofree, @{$item} ); }
127 elsif (UNIVERSAL::isa($ref, 'HASH'))
128 0 { release($item, $force); }
129 }
130  
131 # check that everything has gone - it better had!
132 35 foreach my $key (keys %{$self})
133 0 { warn ref($self) . " still has '$key' key left after release.\n"; }
134 }
135  
136  
137 =head2 $r->val
138  
139 Returns the val of this object or reads the object and then returns its value.
140  
141 Note that all direct subclasses *must* make their own versions of this subroutine
142 otherwise we could be in for a very deep loop!
143  
144 =cut
145  
146 sub val
147 {
148 0 ▶ my ($self) = @_;
Subroutine val: not called CC=2 SCAR=17.9
Pod documented
149
150 0 ▶ $self->{' parent'}->read_obj(@_)->val unless ($self->{' realised'});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{" realised"} 0 0 0
151 }
152  
153 =head2 $r->realise
154  
155 Makes sure that the object is fully read in, etc.
156  
157 =cut
158  
159 sub realise
160 9 ▶{ $_[0]->{' realised'} ? $_[0] : $_[0]->{' parent'}->read_obj(@_); }
Subroutine realise: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{' realised'} ? : 9 0 9
161  
162 =head2 $r->outobjdeep($fh, $pdf)
163  
164 If you really want to output this object, then you must need to read it first.
165 This also means that all direct subclasses must subclass this method or loop forever!
166  
167 =cut
168  
169 sub outobjdeep
170 {
171 0 ▶ my ($self, $fh, $pdf, %opts) = @_;
Subroutine outobjdeep: not called CC=2 SCAR=17.9
Pod documented
172  
173 0 ▶ $self->{' parent'}->read_obj($self)->outobjdeep($fh, $pdf) unless ($self->{' realised'});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{" realised"} 0 0 0
174 }
175  
176  
177 =head2 $r->outobj($fh)
178  
179 If this is a full object then outputs a reference to the object, otherwise calls
180 outobjdeep to output the contents of the object at this point.
181  
182 =cut
183  
184 sub outobj
185 {
186 28 ▶ my ($self, $fh, $pdf, %opts) = @_;
Subroutine outobj: called CC=2 SCAR=6.9
Pod documented
187  
188 28 ▶ if (defined $pdf->{' objects'}{$self->uid})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pdf->{' objects'}{$self->uid}) { } 6 22 28
189 6 { $fh->printf("%d %d R", @{$pdf->{' objects'}{$self->uid}}[0..1]); }
190 else
191 22 { $self->outobjdeep($fh, $pdf, %opts); }
192 }
193  
194  
195 =head2 $r->elementsof
196  
197 Abstract superclass function filler. Returns self here but should return
198 something more useful if an array.
199  
200 =cut
201  
202 sub elementsof
203 {
204 0 ▶ my ($self) = @_;
Subroutine elementsof: not called CC=2 SCAR=17.9
Pod documented
205  
206 0 ▶ if ($self->{' realised'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{' realised'}) { } 0 0 0
207 0 { return ($self); }
208 else
209 0 { return $self->{' parent'}->read_obj($self)->elementsof; }
210 }
211  
212  
213 =head2 $r->empty
214  
215 Empties all content from this object to free up memory or to be read to pass
216 the object into the free list. Simplistically undefs all instance variables
217 other than object number and generation.
218  
219 =cut
220  
221 sub empty
222 {
223 0 ▶ my ($self) = @_;
Subroutine empty: not called CC=3 SCAR=24.8
Pod documented
224 0 my ($k);
225  
226 0 for $k (keys %$self)
227 0 ▶ { undef $self->{$k} unless $self->dont_copy($k); }
Branch: 1 branch
BranchTrueFalseTotal
unless $self->dont_copy($k) 0 0 0
228 0 $self;
229 }
230  
231  
232 =head2 $r->merge($objind)
233  
234 This merges content information into an object reference place-holder.
235 This occurs when an object reference is read before the object definition
236 and the information in the read data needs to be merged into the object
237 place-holder
238  
239 =cut
240  
241 sub merge
242 {
243 0 ▶ my ($self, $other) = @_;
Subroutine merge: not called CC=3 SCAR=24.8
Pod documented
244 0 my ($k);
245  
246 0 for $k (keys %$other)
247 0 ▶ { $self->{$k} = $other->{$k} unless $self->dont_copy($k); }
Branch: 1 branch
BranchTrueFalseTotal
unless $self->dont_copy($k) 0 0 0
248 0 $self->{' realised'} = 1;
249 0 bless $self, ref($other);
250 }
251  
252  
253 =head2 $r->is_obj($pdf)
254  
255 Returns whether this object is a full object with its own object number or
256 whether it is purely a sub-object. $pdf indicates which output file we are
257 concerned that the object is an object in.
258  
259 =cut
260  
261 sub is_obj
262 12 ▶{ defined $_[1]->{' objects'}{$_[0]->uid}; }
Subroutine is_obj: called CC=1 SCAR=0.0
Pod documented
263  
264  
265 =head2 $r->copy($inpdf, $res, $unique, $outpdf, %opts)
266  
267 Returns a new copy of this object.
268  
269 $inpdf gives the source pdf object for the object to be copied. $outpdf gives the
270 target pdf for the object to be copied into. $outpdf may be undefined. $res may be
271 defined in which case the object is copied into that object. $unique controls
272 recursion. if $unique is non zero then new objects are always created and recursion
273 always occurs. But each time recursion occurs, $unique is incremented. Thus is $unique
274 starts with a negative value it is possible to stop the recursion at a certain depth. Of
275 course for a positive value of $unique, recursion always occurs.
276  
277 If $unique is 0 then recursion only occurs if $outpdf is not the same as $inpdf. In this
278 case, a cache is held in $outpdf to see whether a previous copy of the same object has
279 been made. If so, then that previous copy is returned otherwise a new object is made and
280 added to the cache and recursed into.
281  
282 Objects that are full objects with their own id numbers are correspondingly full objects
283 in the output pdf.
284  
285 =cut
286  
287 sub copy
288 {
289 0 ▶ my ($self, $inpdf, $res, $unique, $outpdf, %opts) = @_;
Subroutine copy: not called CC=11 SCAR=48.8
Pod documented
290 0 my ($k, $o);
291  
292 0 ▶ $outpdf = $inpdf unless $outpdf;
Branch: 1 branch
BranchTrueFalseTotal
unless $outpdf 0 0 0
293 0 $self->realise;
294 0 ▶ unless (defined $res)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $res) 0 0 0
295 {
296 0 ▶ if ($outpdf eq $inpdf && !$unique)
Branch: 2 branches
BranchTrueFalseTotal
if ($outpdf eq $inpdf and not $unique) { } 0 0 0
elsif (not $unique and defined $outpdf->{' copies'}{$self->uid}) { } 0 0 0
Condition0%0/3
$outpdf eq $inpdf and not $unique
!ll&&!rl&&r
000
MC/DC0%0/2
$outpdf eq $inpdf and not $unique
$outpdf eq $inpdf not $unique
Truth table0%0/3
A and not(B)
A= $outpdf eq $inpdfB= not $unique
ABresult
0X0
100
111
297 0 { return $self; }
298 ▶ elsif (!$unique && defined $outpdf->{' copies'}{$self->uid})
Condition0%0/3
not $unique and defined $outpdf->{" copies"}{$self->uid}
!ll&&!rl&&r
000
MC/DC0%0/2
not $unique and defined $outpdf->{" copies"}{$self->uid}
not $unique defined $outpdf->{" copies"}{$self->uid}
Truth table0%0/3
not(A) and B
A= not $uniqueB= defined $outpdf->{" copies"}{$self->uid}
ABresult
0X0
100
111
299 0 { return $outpdf->{' copies'}{$self->uid}; }
300  
301 0 $res = {};
302 0 bless $res, ref($self);
303 }
304  
305 0 ▶ if ($self->is_obj($inpdf) && ($unique || ($outpdf ne $inpdf && !defined $outpdf->{' copies'}{$self->uid})))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_obj($inpdf) and $unique || $outpdf ne $inpdf && !defined($outpdf->{" copies"}{$self->uid})) 0 0 0
Condition0%0/9
$self->is_obj($inpdf) and $unique || $outpdf ne $inpdf && !defined($outpdf->{" copies"}{$self->uid})
!ll&&!rl&&r
000
$unique or $outpdf ne $inpdf and not defined $outpdf->{" copies"}{$self->uid}
l!l&&r!l&&!r
000
$outpdf ne $inpdf and not defined $outpdf->{" copies"}{$self->uid}
!ll&&!rl&&r
000
MC/DC0%0/4
$self->is_obj($inpdf) and $unique || $outpdf ne $inpdf && !defined($outpdf->{" copies"}{$self->uid})
$self->is_obj($inpdf) $unique $outpdf ne $inpdf not defined $outpdf->{" copies"}{$self->uid}
Truth table0%0/5
A and B or C and not(D)
A= $self->is_obj($inpdf)B= $uniqueC= $outpdf ne $inpdfD= not defined $outpdf->{" copies"}{$self->uid}
ABCDresult
0XXX0
100X0
10100
11XX1
10111
306 {
307 0 $outpdf->new_obj($res);
308 # $outpdf->{' copies'}{$self->uid} = $res unless ($unique);
309 }
310
311 0 $res;
312 }
313  
314  
315  
316 sub dont_copy
317 0 ▶{ return $inst{$_[1]}; }
Subroutine dont_copy: not called CC=1 SCAR=6.9
Pod undocumented
318  
319 1 1;
320  
« PDF/File.pm ↑ Summary PDF/TTFont0.pm »