API3/Compat/API2/Basic/PDF/Objind.pm

filter: Statementstmt 14.7% 14 / 95 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 16.6% 3 / 18 Podpod 85.7% 12 / 14 totaltotal 15.4% 29 / 188 CC 33 SCAR 34.9
CC
33
Cov
9%
__release
90.0
copy
56.0
release
42.0
CRAP 32.8
« API3/Compat/API2/Basic/TTF/Bsln.pm ↑ Summary API3/Compat/API2/Basic/TTF/Mort/Subtable.pm »
1 #=======================================================================
2 # ____ ____ _____ _ ____ ___ ____
3 # | _ \| _ \| ___| _ _ / \ | _ \_ _| |___ \
4 # | |_) | | | | |_ (_) (_) / _ \ | |_) | | __) |
5 # | __/| |_| | _| _ _ / ___ \| __/| | / __/
6 # |_| |____/|_| (_) (_) /_/ \_\_| |___| |_____|
7 #
8 # A Perl Module Chain to faciliate the Creation and Modification
9 # of High-Quality "Portable Document Format (PDF)" Files.
10 #
11 #=======================================================================
12 #
13 # THIS IS A REUSED PERL MODULE, FOR PROPER LICENCING TERMS SEE BELOW:
14 #
15 #
16 # Copyright Martin Hosken <Martin_Hosken@sil.org>
17 #
18 # No warranty or expression of effectiveness, least of all regarding
19 # anyone's safety, is implied in this software or documentation.
20 #
21 # This specific module is licensed under the Perl Artistic License.
22 #
23 #
24 # $Id: Objind.pm,v 2.1 2007/03/10 12:18:36 areibens Exp $
25 #
26 #=======================================================================
27 package PDF::API3::Compat::API2::Basic::PDF::Objind;
28
29 =head1 NAME
30
31 PDF::API3::Compat::API2::Basic::PDF::Objind - PDF indirect object reference. Also acts as an abstract
32 superclass for all elements in a PDF file.
33
34 =head1 INSTANCE VARIABLES
35
36 Instance variables differ from content variables in that they all start with
37 a space.
38
39 =over
40
41 =item parent
42
43 For an object which is a reference to an object in some source, this holds the
44 reference to the source object, so that should the reference have to be
45 de-referenced, then we know where to go and get the info.
46
47 =item objnum (R)
48
49 The object number in the source (only for object references)
50
51 =item objgen (R)
52
53 The object generation in the source
54
55 There are other instance variables which are used by the parent for file control.
56
57 =item isfree
58
59 This marks whether the object is in the free list and available for re-use as
60 another object elsewhere in the file.
61
62 =item nextfree
63
64 Holds a direct reference to the next free object in the free list.
65
66 =back
67
68 =head1 METHODS
69
70 =cut
71
72 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 1 ▶use vars qw(@inst %inst $uidc);
Subroutine BEGIN: called CC=1 SCAR=0.0
74 1 ▶no warnings qw[ deprecated recursion uninitialized ];
Subroutine BEGIN: called CC=1 SCAR=0.0
75
76 # protected keys during emptying and copying, etc.
77
78 1 @inst = qw(parent objnum objgen isfree nextfree uid realised);
79 1 map {$inst{" $_"} = 1} @inst;
80 1 $uidc = "pdfuid000";
81
82
83 =head2 PDF::API3::Compat::API2::Basic::PDF::Objind->new()
84
85 Creates a new indirect object
86
87 =cut
88
89 sub new
90 0 ▶{
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
91 0 my ($class) = @_;
92 0 my ($self) = {};
93
94 0 ▶ bless $self, ref $class || $class;
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
95 }
96
97 =head2 uid
98
99 Returns a Unique id for this object, creating one if it didn't have one before
100
101 =cut
102
103 sub uid
104 0 ▶{ $_[0]->{' uid'} || ($_[0]->{' uid'} = $uidc++); }
Subroutine uid: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{" uid"} or $_[0]{" uid"} = $PDF::API3::Compat::API2::Basic::PDF::Objind::uidc++ 0 0 0
105
106 =head2 $r->release
107
108 Releases ALL of the memory used by this indirect object, and all of its
109 component/child objects. This method is called automatically by
110 'C<PDF::API3::Compat::API2::Basic::PDF::File-E<gt>release>' (so you don't have to call it yourself).
111
112 B<NOTE>, that it is important that this method get called at some point prior
113 to the actual destruction of the object. Internally, PDF files have an
114 enormous amount of cross-references and this causes circular references within
115 our own internal data structures. Calling 'C<release()>' forces these circular
116 references to be cleaned up and the entire internal data structure purged.
117
118 B<Developer note:> As part of the brute-force cleanup done here, this method
119 will throw a warning message whenever unexpected key values are found within
120 the C<PDF::API3::Compat::API2::Basic::PDF::Objind> object. This is done to help ensure that unexpected
121 and unfreed values are brought to your attention, so you can bug us to keep the
122 module updated properly; otherwise the potential for memory leaks due to
123 dangling circular references will exist.
124
125 =cut
126
127 sub __release
128 0 ▶{
Subroutine __release: not called CC=9 SCAR=45.0
129 0 my ($self, $force) = @_;
130 0 my (@tofree);
131
132 0 ▶ return($self) unless(ref $self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
133 # delete stuff that we know we can, here
134
135 0 ▶ if ($force)
Branch: 1 branch
BranchTrueFalseTotal
if ($force) { } 0 0 0
136 {
137 0 foreach my $key (keys %{$self})
138 {
139 0 push(@tofree,$self->{$key});
140 0 $self->{$key}=undef;
141 0 delete($self->{$key});
142 }
143 }
144 else
145 0 { @tofree = map { delete $self->{$_} } keys %{$self}; }
146
147 0 ▶ while (my $item = shift @tofree)
Branch: 1 branch
BranchTrueFalseTotal
if (my $item = shift @tofree) 0 0 0
148 {
149 0 my $ref = ref($item);
150 0 ▶ if (UNIVERSAL::can($item, 'release'))
Branch: 3 branches
BranchTrueFalseTotal
if (UNIVERSAL::can($item, 'release')) { } 0 0 0
elsif ($ref eq 'ARRAY') { } 0 0 0
elsif (UNIVERSAL::isa($ref, 'HASH')) { } 0 0 0
151 0 { $item->release($force); }
152 elsif ($ref eq 'ARRAY')
153 0 { push( @tofree, @{$item} ); }
154 elsif (UNIVERSAL::isa($ref, 'HASH'))
155 0 { release($item, $force); }
156 }
157
158 # check that everything has gone - it better had!
159 0 foreach my $key (keys %{$self})
160 {
161 # warn ref($self) . " still has '$key' key left after release.\n";
162 0 $self->{$key}=undef;
163 0 delete($self->{$key});
164 }
165 }
166
167 sub release
168 0 ▶{
Subroutine release: not called CC=6 SCAR=37.4
Pod documented
169 0 my ($self) = @_;
170
171 0 my @tofree = values %$self;
172 0 %$self = ();
173
174 0 ▶ while(my $item = shift @tofree)
Branch: 1 branch
BranchTrueFalseTotal
if (my $item = shift @tofree) 0 0 0
175 {
176 0 ▶ my $ref = ref($item) || next; # common case: value is not reference
Condition0%0/2
ref $item || (next)
l!l
00
MC/DC0%0/1
ref $item || (next)
ref $item
Truth table0%0/2
A
A= ref $item
Aresult
11
00
177 0 ▶ if(UNIVERSAL::can($item, 'release'))
Branch: 3 branches
BranchTrueFalseTotal
if (UNIVERSAL::can($item, 'release')) { } 0 0 0
elsif ($ref eq 'ARRAY') { } 0 0 0
elsif (UNIVERSAL::isa($ref, 'HASH')) { } 0 0 0
178 {
179 0 $item->release();
180 }
181 elsif($ref eq 'ARRAY')
182 {
183 0 push @tofree, @$item;
184 }
185 elsif(UNIVERSAL::isa($ref, 'HASH'))
186 {
187 0 release($item);
188 }
189 }
190
191 }
192
193 =head2 $r->val
194
195 Returns the val of this object or reads the object and then returns its value.
196
197 Note that all direct subclasses *must* make their own versions of this subroutine
198 otherwise we could be in for a very deep loop!
199
200 =cut
201
202 sub val
203 0 ▶{
Subroutine val: not called CC=2 SCAR=17.9
Pod documented
204 0 my ($self) = @_;
205
206 0 ▶ $self->{' parent'}->read_obj(@_)->val unless ($self->{' realised'});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{" realised"} 0 0 0
207 }
208
209 =head2 $r->realise
210
211 Makes sure that the object is fully read in, etc.
212
213 =cut
214
215 sub realise
216 0 ▶{ $_[0]->{' realised'} ? $_[0] : $_[0]->{' parent'}->read_obj(@_); }
Subroutine realise: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{' realised'} ? : 0 0 0
217
218 =head2 $r->outobjdeep($fh, $pdf)
219
220 If you really want to output this object, then you must need to read it first.
221 This also means that all direct subclasses must subclass this method or loop forever!
222
223 =cut
224
225 sub outobjdeep
226 0 ▶{
Subroutine outobjdeep: not called CC=2 SCAR=17.9
Pod documented
227 0 my ($self, $fh, $pdf, %opts) = @_;
228
229 0 ▶ $self->{' parent'}->read_obj($self)->outobjdeep($fh, $pdf,%opts) unless ($self->{' realised'});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{" realised"} 0 0 0
230 }
231
232 sub outxmldeep
233 0 ▶{
Subroutine outxmldeep: not called CC=2 SCAR=17.9
Pod undocumented
234 0 my ($self, $fh, $pdf, %opts) = @_;
235
236 0 ▶ $self->{' parent'}->read_obj($self)->outxmldeep($fh, $pdf,%opts) unless ($self->{' realised'});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{" realised"} 0 0 0
237 }
238
239
240 =head2 $r->outobj($fh)
241
242 If this is a full object then outputs a reference to the object, otherwise calls
243 outobjdeep to output the contents of the object at this point.
244
245 =cut
246
247 sub outobj
248 0 ▶{
Subroutine outobj: not called CC=2 SCAR=17.9
Pod documented
249 0 my ($self, $fh, $pdf, %opts) = @_;
250
251 0 ▶ if (defined $pdf->{' objects'}{$self->uid})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pdf->{' objects'}{$self->uid}) { } 0 0 0
252 0 { $fh->printf("%d %d R", @{$pdf->{' objects'}{$self->uid}}[0..1]); }
253 else
254 0 { $self->outobjdeep($fh, $pdf, %opts); }
255 }
256
257 sub outxml
258 0 ▶{
Subroutine outxml: not called CC=2 SCAR=17.9
Pod undocumented
259 0 my ($self, $fh, $pdf, %opts) = @_;
260
261 0 ▶ if (defined $pdf->{' objects'}{$self->uid})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pdf->{' objects'}{$self->uid}) { } 0 0 0
262 0 { $opts{-xmlfh}->printf("<Ref id=\"%d %d\" />", @{$pdf->{' objects'}{$self->uid}}[0..1]); }
263 else
264 0 { $self->outxmldeep($fh, $pdf, %opts); }
265 }
266
267
268 =head2 $r->elementsof
269
270 Abstract superclass function filler. Returns self here but should return
271 something more useful if an array.
272
273 =cut
274
275 sub elementsof
276 0 ▶{
Subroutine elementsof: not called CC=2 SCAR=17.9
Pod documented
277 0 my ($self) = @_;
278
279 0 ▶ if ($self->{' realised'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{' realised'}) { } 0 0 0
280 0 { return ($self); }
281 else
282 0 { return $self->{' parent'}->read_obj($self)->elementsof; }
283 }
284
285
286 =head2 $r->empty
287
288 Empties all content from this object to free up memory or to be read to pass
289 the object into the free list. Simplistically undefs all instance variables
290 other than object number and generation.
291
292 =cut
293
294 sub empty
295 0 ▶{
Subroutine empty: not called CC=3 SCAR=24.8
Pod documented
296 0 my ($self) = @_;
297 0 my ($k);
298
299 0 for $k (keys %$self)
300 0 ▶ { undef $self->{$k} unless $inst{$k}; }
Branch: 1 branch
BranchTrueFalseTotal
unless $PDF::API3::Compat::API2::Basic::PDF::Objind::inst{$k} 0 0 0
301 0 $self;
302 }
303
304
305 =head2 $r->merge($objind)
306
307 This merges content information into an object reference place-holder.
308 This occurs when an object reference is read before the object definition
309 and the information in the read data needs to be merged into the object
310 place-holder
311
312 =cut
313
314 sub merge
315 0 ▶{
Subroutine merge: not called CC=3 SCAR=24.8
Pod documented
316 0 my ($self, $other) = @_;
317 0 my ($k);
318
319 0 for $k (keys %$other)
320 0 ▶ { $self->{$k} = $other->{$k} unless $inst{$k}; }
Branch: 1 branch
BranchTrueFalseTotal
unless $PDF::API3::Compat::API2::Basic::PDF::Objind::inst{$k} 0 0 0
321 0 $self->{' realised'} = 1;
322 0 bless $self, ref($other);
323 }
324
325
326 =head2 $r->is_obj($pdf)
327
328 Returns whether this object is a full object with its own object number or
329 whether it is purely a sub-object. $pdf indicates which output file we are
330 concerned that the object is an object in.
331
332 =cut
333
334 sub is_obj
335 0 ▶{ defined $_[1]->{' objects'}{$_[0]->uid}; }
Subroutine is_obj: not called CC=1 SCAR=6.9
Pod documented
336
337
338 =head2 $r->copy($pdf, $res)
339
340 Returns a new copy of this object. The object is assumed to be some kind
341 of associative array and the copy is a deep copy for elements which are
342 not PDF objects, according to $pdf, and shallow copy for those that are.
343 Notice that calling C<copy> on an object forces at least a one level
344 copy even if it is a PDF object. The returned object loses its PDF
345 object status though.
346
347 If $res is defined then the copy goes into that object rather than creating a
348 new one. It is up to the caller to bless $res, etc. Notice that elements from
349 $self are not copied into $res if there is already an entry for them existing
350 in $res.
351
352 =cut
353
354 sub copy
355 0 ▶{
Subroutine copy: not called CC=7 SCAR=40.3
Pod documented
356 0 my ($self, $pdf, $res) = @_;
357 0 my ($k);
358
359 0 ▶ unless (defined $res)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $res) 0 0 0
360 {
361 0 $res = {};
362 0 bless $res, ref($self);
363 }
364 0 foreach $k (keys %$self)
365 {
366 0 ▶ next if $inst{$k};
Branch: 1 branch
BranchTrueFalseTotal
if $PDF::API3::Compat::API2::Basic::PDF::Objind::inst{$k} 0 0 0
367 0 ▶ next if defined $res->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if defined $res->{$k} 0 0 0
368 0 ▶ if (UNIVERSAL::can($self->{$k}, "is_obj") && !$self->{$k}->is_obj($pdf))
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($self->{$k}, 'is_obj') and not $self->{$k}->is_obj($pdf)) { } 0 0 0
Condition0%0/3
UNIVERSAL::can($self->{$k}, "is_obj") and not $self->{$k}->is_obj($pdf)
!ll&&!rl&&r
000
MC/DC0%0/2
UNIVERSAL::can($self->{$k}, "is_obj") and not $self->{$k}->is_obj($pdf)
UNIVERSAL::can($self->{$k}, "is_obj") not $self->{$k}->is_obj($pdf)
Truth table0%0/3
A and not(B)
A= UNIVERSAL::can($self->{$k}, "is_obj")B= not $self->{$k}->is_obj($pdf)
ABresult
0X0
100
111
369 0 { $res->{$k} = $self->{$k}->copy($pdf); }
370 else
371 0 { $res->{$k} = $self->{$k}; }
372 }
373 0 $res;
374 }
375
376 1 1;
377
« API3/Compat/API2/Basic/TTF/Bsln.pm ↑ Summary API3/Compat/API2/Basic/TTF/Mort/Subtable.pm »