API2/Basic/PDF/Dict.pm

filter: Statementstmt 81.3% 122 / 150 Branchbran 67.8% 57 / 84 Conditioncond 66.6% 26 / 39 MC/DCmcdc 31.8% 7 / 22 Subroutinesub 92.8% 13 / 14 Podpod 85.7% 6 / 7 totaltotal 73.1% 231 / 316 SCAR 48.2
CC
63
Cov
75%
outobjdeep
66.0
read_stream
30.1
type
4.2
CRAP 124.3
« API2/NamedDestination.pm ↑ Summary API2/Resource/ColorSpace/Separation.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::Dict;
10  
11 40 ▶use base 'PDF::API2::Basic::PDF::Objind';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 40 ▶no warnings qw[ deprecated recursion uninitialized ];
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 40 our $VERSION = '2.044'; # VERSION
17  
18 40 our $mincache = 16 * 1024 * 1024;
19  
20 40 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 40 ▶use PDF::API2::Basic::PDF::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 40 ▶use PDF::API2::Basic::PDF::Filter;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 40 ▶use PDF::API2::Basic::PDF::Name;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 =head1 NAME
26  
27 PDF::API2::Basic::PDF::Dict - Low-level dictionary and stream objects
28  
29 =head1 INSTANCE VARIABLES
30  
31 There are various special instance variables which are used to look after,
32 particularly, streams. Each begins with a space:
33  
34 =over
35  
36 =item stream
37  
38 Holds the stream contents for output
39  
40 =item streamfile
41  
42 Holds the stream contents in an external file rather than in memory. This is
43 not the same as a PDF file stream. The data is stored in its unfiltered form.
44  
45 =item streamloc
46  
47 If both ' stream' and ' streamfile' are empty, this indicates where in the
48 source PDF the stream starts.
49  
50 =back
51  
52 =head1 METHODS
53  
54 =cut
55  
56 sub new {
57 1632 ▶ my $class = shift();
Subroutine new: called CC=2 SCAR=7.0
Pod documented
58 1632 ▶ $class = ref($class) if ref($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 1632 1632
59  
60 1632 my $self = $class->SUPER::new(@_);
61 1632 $self->{' realised'} = 1;
62 1632 return $self;
63 }
64  
65 =head2 $type = $d->type($type)
66  
67 Get/Set the standard Type key. It can be passed, and will return, a text value rather than a Name object.
68  
69 =cut
70  
71 sub type {
72 28 ▶ my $self = shift();
Subroutine type: called CC=4 SCAR=14.5
Pod documented
73 28 ▶ if (scalar @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 28 0 28
74 28 my $type = shift();
75 28 ▶ $self->{'Type'} = ref($type) ? $type : PDF::API2::Basic::PDF::Name->new($type);
Branch: 1 branch
BranchTrueFalseTotal
ref $type ? : 0 28 28
76 }
77 28 ▶ return unless exists $self->{'Type'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"Type"} 0 28 28
78 28 return $self->{'Type'}->val();
79 }
80  
81 =head2 @filters = $d->filter(@filters)
82  
83 Get/Set one or more filters being used by the optional stream attached to the dictionary.
84  
85 =cut
86  
87 sub filter {
88 14 ▶ my ($self, @filters) = @_;
Subroutine filter: called CC=2 SCAR=7.0
Pod documented
89  
90 # Developer's Note: the PDF specification allows Filter to be
91 # either a name or an array, but other parts of this codebase
92 # expect an array. If these are updated uncomment the
93 # commented-out lines in order to accept both types.
94  
95 # if (scalar @filters == 1) {
96 # $self->{'Filter'} = ref($filters[0]) ? $filters[0] : PDF::API2::Basic::PDF::Name->new($filters[0]);
97 # }
98 # elsif (scalar @filters) {
99 14 ▶ @filters = map { ref($_) ? $_ : PDF::API2::Basic::PDF::Name->new($_) } @filters;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 14 14
100 14 $self->{'Filter'} = PDF::API2::Basic::PDF::Array->new(@filters);
101 # }
102 }
103  
104 # Undocumented alias, which may be removed in a future release
105 14 ▶sub filters { return filter(@_); }
Subroutine filters: called CC=1 SCAR=0.0
Pod undocumented
106  
107 =head2 $d->outobjdeep($fh)
108  
109 Outputs the contents of the dictionary to a PDF file. This is a recursive call.
110  
111 It also outputs a stream if the dictionary has a stream element. If this occurs
112 then this method will calculate the length of the stream and insert it into the
113 stream's dictionary.
114  
115 =cut
116  
117 sub outobjdeep {
118 1353 ▶ my ($self, $fh, $pdf) = @_;
Subroutine outobjdeep: called CC=43 SCAR=41.9
Pod documented
119  
120 1353 ▶ if (defined $self->{' stream'} or defined $self->{' streamfile'} or defined $self->{' streamloc'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{" stream"} or defined $self->{" streamfile"} or defined $self->{" streamloc"}) 1 1218 1219
Condition83%5/6
defined $self->{" stream"} or defined $self->{" streamfile"} or defined $self->{" streamloc"}
l!l&&r!l&&!r
13501218
defined $self->{" stream"} or defined $self->{" streamfile"}
l!l&&r!l&&!r
13411218
MC/DC66%2/3
defined $self->{" stream"} or defined $self->{" streamfile"} or defined $self->{" streamloc"}
defined $self->{" stream"} defined $self->{" streamfile"} defined $self->{" streamloc"}
Truth table75%3/4
A or B or C
A= defined $self->{" stream"}B= defined $self->{" streamfile"}C= defined $self->{" streamloc"}
ABCresult
1XX1
01X1
0011
0000
121 135 ▶ if ($self->{'Filter'} and $self->{' nofilt'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'Filter'} and $self->{' nofilt'}) { } 9 126 135
elsif ($self->{'Filter'} or not defined $self->{' stream'}) { } 15 111 126
Condition100%3/3
$self->{"Filter"} and $self->{" nofilt"}
!ll&&!rl&&r
111159
MC/DC100%2/2
$self->{"Filter"} and $self->{" nofilt"}
$self->{"Filter"} $self->{" nofilt"}
Truth table100%3/3
A and B
A= $self->{"Filter"}B= $self->{" nofilt"}
ABresult
0X0
100
111
122 9 ▶ $self->{'Length'} ||= PDF::API2::Basic::PDF::Number->new(length($self->{' stream'}));
Condition66%2/3
$self->{"Length"} ||= "PDF::API2::Basic::PDF::Number"->new(length $self->{" stream"})
l!l&&r!l&&!r
270
MC/DC0%0/2
$self->{"Length"} ||= "PDF::API2::Basic::PDF::Number"->new(length $self->{" stream"})
$self->{"Length"} "PDF::API2::Basic::PDF::Number"->new(length $self->{" stream"})
Truth table66%2/3
A ||= B
A= $self->{"Length"}B= "PDF::API2::Basic::PDF::Number"->new(length $self->{" stream"})
ABresult
1X1
011
000
123 }
124 ▶ elsif ($self->{'Filter'} or not defined $self->{' stream'}) {
Condition66%2/3
$self->{"Filter"} or not defined $self->{" stream"}
l!l&&r!l&&!r
150111
MC/DC50%1/2
$self->{"Filter"} or not defined $self->{" stream"}
$self->{"Filter"} not defined $self->{" stream"}
Truth table66%2/3
A or not(B)
A= $self->{"Filter"}B= not defined $self->{" stream"}
ABresult
1X1
011
000
125 15 ▶ $self->{'Length'} = PDF::API2::Basic::PDF::Number->new(0) unless defined $self->{'Length'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"Length"} 15 0 15
126 15 ▶ $pdf->new_obj($self->{'Length'}) unless $self->{'Length'}->is_obj($pdf);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Length"}->is_obj($pdf) 15 0 15
127 }
128 else {
129 111 $self->{'Length'} = PDF::API2::Basic::PDF::Number->new(length($self->{' stream'}));
130 }
131 }
132  
133 1353 $fh->print('<< ');
134 1353 foreach my $key (sort {
135 23027 ▶ $a eq 'Type' ? -1 : $b eq 'Type' ? 1 :
Branch: 4 branches
BranchTrueFalseTotal
$a eq 'Type' ? : 752 22275 23027
$b eq 'Type' ? : 640 21635 22275
$a eq 'Subtype' ? : 82 21553 21635
$b eq 'Subtype' ? : 78 21475 21553
136 $a eq 'Subtype' ? -1 : $b eq 'Subtype' ? 1 : $a cmp $b
137 } keys %$self) {
138 9611 ▶ next if $key =~ m/^[\s\-]/o;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^[\s\-]/o 6240 3371 9611
139 3371 ▶ next unless $self->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$key} 0 3371 3371
140 3371 $fh->print('/' . PDF::API2::Basic::PDF::Name::string_to_name($key, $pdf) . ' ');
141 3371 $self->{$key}->outobj($fh, $pdf);
142 3371 $fh->print(' ');
143 }
144 1353 $fh->print('>>');
145  
146 # Now handle the stream (if any)
147 1353 my (@filters, $loc);
148  
149 1353 ▶ if (defined $self->{' streamloc'} and not defined $self->{' stream'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{" streamloc"} and not defined $self->{" stream"}) 0 1353 1353
Condition33%1/3
defined $self->{" streamloc"} and not defined $self->{" stream"}
!ll&&!rl&&r
135300
MC/DC0%0/2
defined $self->{" streamloc"} and not defined $self->{" stream"}
defined $self->{" streamloc"} not defined $self->{" stream"}
Truth table33%1/3
A and not(B)
A= defined $self->{" streamloc"}B= not defined $self->{" stream"}
ABresult
0X0
100
111
150 # read a stream if in file
151 0 $loc = $fh->tell();
152 0 $self->read_stream();
153 0 $fh->seek($loc, 0);
154 }
155  
156 1353 ▶ if (not $self->{' nofilt'} and defined $self->{'Filter'} and (defined $self->{' stream'} or defined $self->{' streamfile'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{" nofilt"} and defined $self->{"Filter"} and defined $self->{" stream"} || defined $self->{" streamfile"}) 15 1338 1353
Condition66%6/9
not $self->{" nofilt"} and defined $self->{"Filter"} and defined $self->{" stream"} || defined $self->{" streamfile"}
!ll&&!rl&&r
1338150
not $self->{" nofilt"} and defined $self->{"Filter"}
!ll&&!rl&&r
9132915
defined $self->{" stream"} or defined $self->{" streamfile"}
l!l&&r!l&&!r
1500
MC/DC0%0/4
not $self->{" nofilt"} and defined $self->{"Filter"} and defined $self->{" stream"} || defined $self->{" streamfile"}
not $self->{" nofilt"} defined $self->{"Filter"} defined $self->{" stream"} defined $self->{" streamfile"}
Truth table40%2/5
not(A) and B and C or D
A= not $self->{" nofilt"}B= defined $self->{"Filter"}C= defined $self->{" stream"}D= defined $self->{" streamfile"}
ABCDresult
0XXX0
10XX0
11000
111X1
11011
157 15 my $hasflate = -1;
158 15 for my $i (0 .. scalar(@{$self->{'Filter'}{' val'}}) - 1) {
159 15 my $filter = $self->{'Filter'}{' val'}[$i]->val();
160 # hack to get around LZW patent
161 15 ▶ if ($filter eq 'LZWDecode') {
Branch: 2 branches
BranchTrueFalseTotal
if ($filter eq 'LZWDecode') { } 0 15 15
elsif ($filter eq 'FlateDecode') { } 13 2 15
162 0 ▶ if ($hasflate < -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hasflate < -1) 0 0 0
163 0 $hasflate = $i;
164 0 next;
165 }
166 0 $filter = 'FlateDecode';
167 0 $self->{'Filter'}{' val'}[$i]{'val'} = $filter; # !!!
168 }
169 elsif ($filter eq 'FlateDecode') {
170 13 $hasflate = -2;
171 }
172 15 my $filter_class = "PDF::API2::Basic::PDF::Filter::$filter";
173 15 push (@filters, $filter_class->new());
174 }
175 15 ▶ splice(@{$self->{'Filter'}{' val'}}, $hasflate, 1) if $hasflate > -1;
Branch: 1 branch
BranchTrueFalseTotal
if $hasflate > -1 0 15 15
176 }
177  
178 1353 ▶ if (defined $self->{' stream'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{' stream'}) { } 134 1219 1353
elsif (defined $self->{' streamfile'}) { } 1 1218 1219
179 134 $fh->print(" stream\n");
180 134 $loc = $fh->tell();
181 134 my $stream = $self->{' stream'};
182 134 ▶ unless ($self->{' nofilt'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{" nofilt"}) 126 8 134
183 126 foreach my $filter (reverse @filters) {
184 15 $stream = $filter->outfilt($stream, 1);
185 }
186 }
187 134 $fh->print($stream);
188 ## $fh->print("\n"); # newline goes into endstream
189  
190 }
191 elsif (defined $self->{' streamfile'}) {
192 1 ▶ open(my $dictfh, "<", $self->{' streamfile'}) || die "Unable to open $self->{' streamfile'}";
Branch: 1 branch
BranchTrueFalseTotal
open my $dictfh, "<", $self->{" streamfile"} or die qq[Unable to open $self->{" streamfile"}] 0 1 1
193 1 binmode($dictfh, ':raw');
194  
195 1 $fh->print(" stream\n");
196 1 $loc = $fh->tell();
197 1 my $stream;
198 1 ▶ while (read($dictfh, $stream, 4096)) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $dictfh, $stream, 4096) 1 1 2
199 1 ▶ unless ($self->{' nofilt'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{" nofilt"}) 0 1 1
200 0 foreach my $filter (reverse @filters) {
201 0 $stream = $filter->outfilt($stream, 0);
202 }
203 }
204 1 $fh->print($stream);
205 }
206 1 close $dictfh;
207 1 ▶ unless ($self->{' nofilt'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{" nofilt"}) 0 1 1
208 0 $stream = '';
209 0 foreach my $filter (reverse @filters) {
210 0 $stream = $filter->outfilt($stream, 1);
211 }
212 0 $fh->print($stream);
213 }
214 ## $fh->print("\n"); # newline goes into endstream
215 }
216  
217 1353 ▶ if (defined $self->{' stream'} or defined $self->{' streamfile'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{" stream"} or defined $self->{" streamfile"}) 135 1218 1353
Condition100%3/3
defined $self->{" stream"} or defined $self->{" streamfile"}
l!l&&r!l&&!r
13411218
MC/DC100%2/2
defined $self->{" stream"} or defined $self->{" streamfile"}
defined $self->{" stream"} defined $self->{" streamfile"}
Truth table100%3/3
A or B
A= defined $self->{" stream"}B= defined $self->{" streamfile"}
ABresult
1X1
011
000
218 135 my $length = $fh->tell() - $loc;
219 135 ▶ unless ($self->{'Length'}{'val'} == $length) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"Length"}{"val"} == $length) 15 120 135
220 15 $self->{'Length'}{'val'} = $length;
221 15 ▶ $pdf->out_obj($self->{'Length'}) if $self->{'Length'}->is_obj($pdf);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Length"}->is_obj($pdf) 15 0 15
222 }
223  
224 135 $fh->print("\nendstream"); # next is endobj which has the final cr
225 }
226 }
227  
228 =head2 $d->read_stream($force_memory)
229  
230 Reads in a stream from a PDF file. If the stream is greater than
231 C<PDF::Dict::mincache> (defaults to 32768) bytes to be stored, then
232 the default action is to create a file for it somewhere and to use that
233 file as a data cache. If $force_memory is set, this caching will not
234 occur and the data will all be stored in the $self->{' stream'}
235 variable.
236  
237 =cut
238  
239 sub read_stream {
240 7 ▶ my ($self, $force_memory) = @_;
Subroutine read_stream: called CC=16 SCAR=34.0
Pod documented
241  
242 7 my $fh = $self->{' streamsrc'};
243 7 my $len = $self->{'Length'}->val();
244  
245 7 $self->{' stream'} = '';
246  
247 7 my @filters;
248 7 ▶ if (defined $self->{'Filter'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"Filter"}) 3 4 7
249 3 my $i = 0;
250 3 foreach my $filter ($self->{'Filter'}->elements()) {
251 3 my $filter_class = "PDF::API2::Basic::PDF::Filter::" . $filter->val();
252 3 ▶ unless ($self->{'DecodeParms'}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'DecodeParms'}) { } 0 3 3
if (do { $self->{'Filter'}->isa('PDF::API2::Basic::PDF::Name') and $self->{'DecodeParms'}->isa('PDF::API2::Basic::PDF::Dict') }) { } 0 0 0
elsif ($self->{'DecodeParms'}->isa('PDF::API2::Basic::PDF::Array')) { } 0 0 0
253 3 push(@filters, $filter_class->new());
254 }
255 ▶ elsif ($self->{'Filter'}->isa('PDF::API2::Basic::PDF::Name') and $self->{'DecodeParms'}->isa('PDF::API2::Basic::PDF::Dict')) {
Condition0%0/3
$self->{"Filter"}->isa("PDF::API2::Basic::PDF::Name") and $self->{"DecodeParms"}->isa("PDF::API2::Basic::PDF::Dict")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"Filter"}->isa("PDF::API2::Basic::PDF::Name") and $self->{"DecodeParms"}->isa("PDF::API2::Basic::PDF::Dict")
$self->{"Filter"}->isa("PDF::API2::Basic::PDF::Name") $self->{"DecodeParms"}->isa("PDF::API2::Basic::PDF::Dict")
Truth table0%0/3
A and B
A= $self->{"Filter"}->isa("PDF::API2::Basic::PDF::Name")B= $self->{"DecodeParms"}->isa("PDF::API2::Basic::PDF::Dict")
ABresult
0X0
100
111
256 0 push(@filters, $filter_class->new($self->{'DecodeParms'}));
257 }
258 elsif ($self->{'DecodeParms'}->isa('PDF::API2::Basic::PDF::Array')) {
259 0 my $parms = $self->{'DecodeParms'}->val->[$i];
260 0 push(@filters, $filter_class->new($parms));
261 }
262 else {
263 0 push(@filters, $filter_class->new());
264 }
265 3 $i++;
266 }
267 }
268  
269 7 my $last = 0;
270 7 ▶ if (defined $self->{' streamfile'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{" streamfile"}) 0 7 7
271 0 unlink ($self->{' streamfile'});
272 0 $self->{' streamfile'} = undef;
273 }
274 7 seek $fh, $self->{' streamloc'}, 0;
275  
276 7 my $dictfh;
277 7 my $readlen = 4096;
278 7 ▶ for (my $i = 0; $i < $len; $i += $readlen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 7 7 14
279 7 my $data;
280 7 ▶ unless ($i + $readlen > $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i + $readlen > $len) { } 7 0 7
281 0 read $fh, $data, $readlen;
282 }
283 else {
284 7 $last = 1;
285 7 read $fh, $data, $len - $i;
286 }
287  
288 7 foreach my $filter (@filters) {
289 3 $data = $filter->infilt($data, $last);
290 }
291  
292 # Start using a temporary file if the stream gets too big
293 7 ▶ if (not $force_memory and not defined $self->{' streamfile'} and (length($self->{' stream'}) + length($data)) > $mincache) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $force_memory || defined $self->{" streamfile"} and length($self->{" stream"}) + length($data) > $PDF::API2::Basic::PDF::Dict::mincache) 0 7 7
Condition66%4/6
not $force_memory || defined $self->{" streamfile"} and length($self->{" stream"}) + length($data) > $PDF::API2::Basic::PDF::Dict::mincache
!ll&&!rl&&r
340
$force_memory || defined $self->{' streamfile'}
l!l&&r!l&&!r
304
MC/DC0%0/3
not $force_memory || defined $self->{" streamfile"} and length($self->{" stream"}) + length($data) > $PDF::API2::Basic::PDF::Dict::mincache
$force_memory defined $self->{' streamfile'} length($self->{" stream"}) + length($data) > $PDF::API2::Basic::PDF::Dict::mincache
Truth table50%2/4
not(A || B) and C
A= $force_memoryB= defined $self->{' streamfile'}C= length($self->{" stream"}) + length($data) > $PDF::API2::Basic::PDF::Dict::mincache
ABCresult
1XX0
01X0
0000
0011
294 0 $dictfh = File::Temp->new(TEMPLATE => 'pdfXXXXX', SUFFIX => 'dat', TMPDIR => 1);
295 0 $self->{' streamfile'} = $dictfh->filename();
296 0 print $dictfh $self->{' stream'};
297 0 undef $self->{' stream'};
298 }
299  
300 7 ▶ if (defined $self->{' streamfile'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{' streamfile'}) { } 0 7 7
301 0 print $dictfh $data;
302 }
303 else {
304 7 $self->{' stream'} .= $data;
305 }
306 }
307  
308 7 ▶ close $dictfh if defined $self->{' streamfile'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{" streamfile"} 0 7 7
309 7 $self->{' nofilt'} = 0;
310 7 return $self;
311 }
312  
313 =head2 $d->val
314  
315 Returns the dictionary, which is itself.
316  
317 =cut
318  
319 sub val {
320 0 ▶ return $_[0];
Subroutine val: not called CC=1 SCAR=6.9
Pod documented
321 }
322  
323 40 1;
« API2/NamedDestination.pm ↑ Summary API2/Resource/ColorSpace/Separation.pm »