API2/Basic/PDF/Objind.pm

filter: Statementstmt 84.9% 62 / 73 Branchbran 72.2% 26 / 36 Conditioncond 64.7% 11 / 17 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 77.7% 14 / 18 Podpod 92.3% 12 / 13 totaltotal 77.2% 129 / 167 SCAR 35.8
CC
28
Cov
79%
empty
12.0
copy
8.3
release
8.2
CRAP 35.7
« API2/Resource/XObject/Form/BarCode.pm ↑ Summary API2/Resource/XObject/Image/JPEG.pm »
1 # Code in the PDF::API2::Basic::PDF namespace was originally copied from the
2 # Text::PDF distribution.
3 #
4 # Copyright Martin Hosken <Martin_Hosken@sil.org>
5 #
6 # Martin Hosken's code may be used under the terms of the MIT license.
7 # Subsequent versions of the code have the same license as PDF::API2.
8  
9 package PDF::API2::Basic::PDF::Objind;
10  
11 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 40 our $VERSION = '2.044'; # VERSION
15  
16 =head1 NAME
17  
18 PDF::API2::Basic::PDF::Objind - Low-level PDF indirect object
19  
20 =head1 INSTANCE VARIABLES
21  
22 Instance variables differ from content variables in that they all start with
23 a space.
24  
25 =over
26  
27 =item parent
28  
29 For an object which is a reference to an object in some source, this holds the
30 reference to the source object, so that should the reference have to be
31 de-referenced, then we know where to go and get the info.
32  
33 =item objnum (R)
34  
35 The object number in the source (only for object references)
36  
37 =item objgen (R)
38  
39 The object generation in the source
40  
41 There are other instance variables which are used by the parent for file control.
42  
43 =item isfree
44  
45 This marks whether the object is in the free list and available for re-use as
46 another object elsewhere in the file.
47  
48 =item nextfree
49  
50 Holds a direct reference to the next free object in the free list.
51  
52 =back
53  
54 =head1 METHODS
55  
56 =cut
57  
58 40 ▶use Scalar::Util qw(blessed reftype weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 40 ▶use vars qw($uidc @inst %inst);
Subroutine BEGIN: called CC=1 SCAR=0.0
61 40 $uidc = "pdfuid000";
62  
63 # protected keys during emptying and copying, etc.
64 40 @inst = qw(parent objnum objgen isfree nextfree uid realised);
65 40 $inst{" $_"} = 1 for @inst;
66  
67 =head2 PDF::API2::Basic::PDF::Objind->new()
68  
69 Creates a new indirect object
70  
71 =cut
72  
73 sub new {
74 1751 ▶ my ($class) = @_;
Subroutine new: called CC=2 SCAR=8.1
Pod documented
75  
76 1751 ▶ bless {}, ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
017510
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
77 }
78  
79 =head2 uid
80  
81 Returns a Unique id for this object, creating one if it didn't have one before
82  
83 =cut
84  
85 sub uid {
86 17887 ▶ $_[0]->{' uid'} || ($_[0]->{' uid'} = $uidc++);
Subroutine uid: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{" uid"} or $_[0]{" uid"} = $PDF::API2::Basic::PDF::Objind::uidc++ 5442 12445 17887
87 }
88  
89 =head2 $r->release
90  
91 Releases ALL of the memory used by this indirect object, and all of
92 its component/child objects. This method is called automatically by
93 'C<PDF::API2::Basic::PDF::File-E<gt>release>' (so you don't have to
94 call it yourself).
95  
96 B<Note:> it is important that this method get called at some point
97 prior to the actual destruction of the object. Internally, PDF files
98 have an enormous amount of cross-references and this causes circular
99 references within our own internal data structures. Calling
100 'C<release()>' forces these circular references to be cleaned up and
101 the entire internal data structure purged.
102  
103 =cut
104  
105 # Maintainer's Question: Couldn't this be handled by a DESTROY method
106 # instead of requiring an explicit call to release()?
107 sub release {
108 6678 ▶ my ($self) = @_;
Subroutine release: called CC=8 SCAR=21.0
Pod documented
109  
110 6678 my @tofree = values %$self;
111 6678 %$self = ();
112  
113 # PDFs with highly-interconnected page trees or outlines can hit Perl's
114 # recursion limit pretty easily, so disable the warning for this specific
115 # loop.
116 40 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
117  
118 6678 ▶ while (my $item = shift @tofree) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $item = shift @tofree) 17597 6678 24275
119 # common case: value is not reference
120 17597 ▶ my $ref = ref($item) || next;
Condition100%2/2
ref $item || (next)
l!l
579411803
MC/DC100%1/1
ref $item || (next)
ref $item
Truth table100%2/2
A
A= ref $item
Aresult
11
00
121  
122 5794 ▶ if (blessed($item) and $item->can('release')) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($item) and $item->can('release')) { } 5064 730 5794
elsif ($ref eq 'ARRAY') { } 709 21 730
elsif (defined builtin::reftype($ref) and builtin::reftype($ref) eq 'HASH') { } 0 42 42
Condition100%3/3
builtin::blessed($item) and $item->can("release")
!ll&&!rl&&r
72825064
MC/DC100%2/2
builtin::blessed($item) and $item->can("release")
builtin::blessed($item) $item->can("release")
Truth table100%3/3
A and B
A= builtin::blessed($item)B= $item->can("release")
ABresult
0X0
100
111
123 5064 $item->release();
124 }
125 elsif ($ref eq 'ARRAY') {
126 709 push @tofree, @$item;
127 }
128 ▶ elsif (defined(reftype($ref)) and reftype($ref) eq 'HASH') {
Condition33%1/3
defined builtin::reftype($ref) and builtin::reftype($ref) eq "HASH"
!ll&&!rl&&r
2100
MC/DC0%0/2
defined builtin::reftype($ref) and builtin::reftype($ref) eq "HASH"
defined builtin::reftype($ref) builtin::reftype($ref) eq "HASH"
Truth table33%1/3
A and B
A= defined builtin::reftype($ref)B= builtin::reftype($ref) eq "HASH"
ABresult
0X0
100
111
129 0 release($item);
130 }
131 }
132 }
133  
134 =head2 $r->val
135  
136 Returns the value of this object or reads the object and then returns
137 its value.
138  
139 Note that all direct subclasses *must* make their own versions of this
140 subroutine otherwise we could be in for a very deep loop!
141  
142 =cut
143  
144 sub val {
145 0 ▶ my ($self) = @_;
Subroutine val: not called CC=2 SCAR=17.9
Pod documented
146  
147 0 ▶ $self->{' parent'}->read_obj(@_)->val unless $self->{' realised'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{" realised"} 0 0 0
148 }
149  
150 =head2 $r->realise
151  
152 Makes sure that the object is fully read in, etc.
153  
154 =cut
155  
156 sub realise {
157 1931 ▶ my $self = shift();
Subroutine realise: called CC=3 SCAR=11.0
Pod documented
158 1931 ▶ return $self if $self->{' realised'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{" realised"} 1825 106 1931
159 106 ▶ return $self->{' parent'}->read_obj($self, @_) if $self->{' objnum'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{" objnum"} 77 29 106
160 29 return $self;
161 }
162  
163 =head2 $r->outobjdeep($fh, $pdf)
164  
165 If you really want to output this object, then you must need to read it first.
166 This also means that all direct subclasses must subclass this method or loop forever!
167  
168 =cut
169  
170 sub outobjdeep {
171 0 ▶ my ($self, $fh, $pdf) = @_;
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 =head2 $r->outobj($fh)
177  
178 If this is a full object then outputs a reference to the object, otherwise calls
179 outobjdeep to output the contents of the object at this point.
180  
181 =cut
182  
183 sub outobj {
184 5212 ▶ my ($self, $fh, $pdf) = @_;
Subroutine outobj: called CC=2 SCAR=6.9
Pod documented
185  
186 5212 ▶ if (defined $pdf->{' objects'}{$self->uid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pdf->{' objects'}{$self->uid}) { } 1048 4164 5212
187 1048 $fh->printf("%d %d R", @{$pdf->{' objects'}{$self->uid}}[0..1]);
188 }
189 else {
190 4164 $self->outobjdeep($fh, $pdf);
191 }
192 }
193  
194 =head2 $r->elements
195  
196 Abstract superclass function filler. Returns self here but should return
197 something more useful if an array.
198  
199 =cut
200  
201 0 ▶sub elementsof { return elements(@_) }
Subroutine elementsof: not called CC=1 SCAR=6.9
Pod undocumented
202  
203 sub elements {
204 3 ▶ my ($self) = @_;
Subroutine elements: called CC=2 SCAR=7.6
Pod documented
205  
206 3 ▶ if ($self->{' realised'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{' realised'}) { } 3 0 3
207 3 return $self;
208 }
209 else {
210 0 return $self->{' parent'}->read_obj($self)->elements();
211 }
212 }
213  
214  
215 =head2 $r->empty
216  
217 Empties all content from this object to free up memory or to be read to pass
218 the object into the free list. Simplistically undefs all instance variables
219 other than object number and generation.
220  
221 =cut
222  
223 sub empty {
224 0 ▶ my ($self) = @_;
Subroutine empty: not called CC=3 SCAR=24.8
Pod documented
225  
226 0 for my $k (keys %$self) {
227 0 ▶ undef $self->{$k} unless $inst{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless $PDF::API2::Basic::PDF::Objind::inst{$k} 0 0 0
228 }
229  
230 0 return $self;
231 }
232  
233  
234 =head2 $r->merge($objind)
235  
236 This merges content information into an object reference place-holder.
237 This occurs when an object reference is read before the object definition
238 and the information in the read data needs to be merged into the object
239 place-holder
240  
241 =cut
242  
243 sub merge {
244 81 ▶ my ($self, $other) = @_;
Subroutine merge: called CC=4 SCAR=13.9
Pod documented
245  
246 81 for my $k (keys %$other) {
247 375 ▶ next if $inst{$k};
Branch: 1 branch
BranchTrueFalseTotal
if $PDF::API2::Basic::PDF::Objind::inst{$k} 81 294 375
248 294 $self->{$k} = $other->{$k};
249  
250 # This doesn't seem like the right place to do this, but I haven't
251 # yet found all of the places where Parent is being set
252 294 ▶ weaken $self->{$k} if $k eq 'Parent';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "Parent" 22 272 294
253 }
254 81 $self->{' realised'} = 1;
255 81 bless $self, ref($other);
256 }
257  
258  
259 =head2 $r->is_obj($pdf)
260  
261 Returns whether this object is a full object with its own object number or
262 whether it is purely a sub-object. $pdf indicates which output file we are
263 concerned that the object is an object in.
264  
265 =cut
266  
267 sub is_obj {
268 2412 ▶ return defined $_[1]->{' objects'}{$_[0]->uid};
Subroutine is_obj: called CC=1 SCAR=0.0
Pod documented
269 }
270  
271  
272 =head2 $r->copy($pdf, $res)
273  
274 Returns a new copy of this object. The object is assumed to be some kind
275 of associative array and the copy is a deep copy for elements which are
276 not PDF objects, according to $pdf, and shallow copy for those that are.
277 Notice that calling C<copy> on an object forces at least a one level
278 copy even if it is a PDF object. The returned object loses its PDF
279 object status though.
280  
281 If $res is defined then the copy goes into that object rather than creating a
282 new one. It is up to the caller to bless $res, etc. Notice that elements from
283 $self are not copied into $res if there is already an entry for them existing
284 in $res.
285  
286 =cut
287  
288 sub copy {
289 1023 ▶ my ($self, $pdf, $res) = @_;
Subroutine copy: called CC=8 SCAR=21.1
Pod documented
290  
291 1023 ▶ unless (defined $res) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $res) 1023 0 1023
292 1023 $res = {};
293 1023 bless $res, ref($self);
294 }
295 1023 foreach my $k (keys %$self) {
296 3089 ▶ next if $inst{$k};
Branch: 1 branch
BranchTrueFalseTotal
if $PDF::API2::Basic::PDF::Objind::inst{$k} 2062 1027 3089
297 1027 ▶ next if defined $res->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if defined $res->{$k} 0 1027 1027
298 1027 ▶ if (blessed($self->{$k}) and $self->{$k}->can('is_obj') and not $self->{$k}->is_obj($pdf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self->{$k}) and $self->{$k}->can('is_obj') and not $self->{$k}->is_obj($pdf)) { } 147 880 1027
Condition66%4/6
builtin::blessed($self->{$k}) and $self->{$k}->can("is_obj") and not $self->{$k}->is_obj($pdf)
!ll&&!rl&&r
8800147
builtin::blessed($self->{$k}) and $self->{$k}->can("is_obj")
!ll&&!rl&&r
8800147
MC/DC33%1/3
builtin::blessed($self->{$k}) and $self->{$k}->can("is_obj") and not $self->{$k}->is_obj($pdf)
builtin::blessed($self->{$k}) $self->{$k}->can("is_obj") not $self->{$k}->is_obj($pdf)
Truth table50%2/4
A and B and not(C)
A= builtin::blessed($self->{$k})B= $self->{$k}->can("is_obj")C= not $self->{$k}->is_obj($pdf)
ABCresult
0XX0
10X0
1100
1111
299 147 $res->{$k} = $self->{$k}->copy($pdf);
300 }
301 else {
302 880 $res->{$k} = $self->{$k};
303 }
304 }
305 1023 return $res;
306 }
307  
308 40 1;
« API2/Resource/XObject/Form/BarCode.pm ↑ Summary API2/Resource/XObject/Image/JPEG.pm »