PDF/Dict.pm

filter: Statementstmt 50.3% 81 / 161 Branchbran 30.0% 27 / 90 Conditioncond 51.5% 17 / 33 MC/DCmcdc 16.6% 3 / 18 Subroutinesub 90.0% 9 / 10 Podpod 100% 5 / 5 totaltotal 44.7% 142 / 317 CC 66 SCAR 57.0
CC
66
Cov
44%
outobjdeep
482.4
copy
56.0
read_stream
31.0
CRAP 300.3
↑ Summary PDF/Filter.pm »
1 package Text::PDF::Dict;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use vars qw(@ISA $mincache $tempbase $cr);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 # no warnings qw(uninitialized);
6  
7 1 ▶use Text::PDF::Objind;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 @ISA = qw(Text::PDF::Objind);
9  
10 1 $cr = '(?:\015|\012|(?:\015\012))';
11  
12 1 ▶use Text::PDF::Filter;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 BEGIN
15 1 ▶{
Subroutine BEGIN: called CC=3 SCAR=14.2
16 1 ▶ my $temp_dir = -d '/tmp' ? '/tmp' : $ENV{TMP} || $ENV{TEMP};
Branch: 1 branch
BranchTrueFalseTotal
-d '/tmp' ? : 1 0 1
Condition0%0/3
$ENV{"TMP"} or $ENV{"TEMP"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{"TMP"} or $ENV{"TEMP"}
$ENV{"TMP"} $ENV{"TEMP"}
Truth table0%0/3
A or B
A= $ENV{"TMP"}B= $ENV{"TEMP"}
ABresult
1X1
011
000
17 1 $tempbase = sprintf("%s/%d-%d-0000", $temp_dir, $$, time());
18 1 $mincache = 32768;
19 }
20  
21 =head1 NAME
22  
23 Text::PDF::Dict - PDF Dictionaries and Streams. Inherits from L<PDF::Objind>
24  
25 =head1 INSTANCE VARIABLES
26  
27 There are various special instance variables which are used to look after,
28 particularly, streams. Each begins with a space:
29  
30 =over
31  
32 =item stream
33  
34 Holds the stream contents for output
35  
36 =item streamfile
37  
38 Holds the stream contents in an external file rather than in memory. This is
39 not the same as a PDF file stream. The data is stored in its unfiltered form.
40  
41 =item streamloc
42  
43 If both ' stream' and ' streamfile' are empty, this indicates where in the
44 source PDF the stream starts.
45  
46 =back
47  
48 =head1 METHODS
49  
50 =cut
51  
52 sub new
53 10 ▶{
Subroutine new: called CC=2 SCAR=7.0
Pod documented
54 10 my ($class, @opts) = @_;
55 10 my ($self);
56  
57 10 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 10 10
58 10 $self = $class->SUPER::new(@_);
59 10 $self->{' realised'} = 1;
60 10 return $self;
61 }
62
63  
64 =head2 $d->outobjdeep($fh)
65  
66 Outputs the contents of the dictionary to a PDF file. This is a recursive call.
67  
68 It also outputs a stream if the dictionary has a stream element. If this occurs
69 then this method will calculate the length of the stream and insert it into the
70 stream's dictionary.
71  
72 =cut
73  
74 sub outobjdeep
75 8 ▶{
Subroutine outobjdeep: called CC=45 SCAR=61.8
Pod documented
76 8 my ($self, $fh, $pdf, %opts) = @_;
77 8 my ($key, $val, $f, @filts);
78 8 my ($loc, $str, %specs, $len);
79  
80 8 ▶ 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 7 8
Condition66%4/6
defined $self->{" stream"} or defined $self->{" streamfile"} or defined $self->{" streamloc"}
l!l&&r!l&&!r
107
defined $self->{" stream"} or defined $self->{" streamfile"}
l!l&&r!l&&!r
107
MC/DC33%1/3
defined $self->{" stream"} or defined $self->{" streamfile"} or defined $self->{" streamloc"}
defined $self->{" stream"} defined $self->{" streamfile"} defined $self->{" streamloc"}
Truth table50%2/4
A or B or C
A= defined $self->{" stream"}B= defined $self->{" streamfile"}C= defined $self->{" streamloc"}
ABCresult
1XX1
01X1
0011
0000
81 {
82 1 ▶ if ($self->{'Filter'} || !defined $self->{' stream'})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'Filter'} or not defined $self->{' stream'}) { } 0 1 1
Condition33%1/3
$self->{"Filter"} or not defined $self->{" stream"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->{"Filter"} or not defined $self->{" stream"}
$self->{"Filter"} not defined $self->{" stream"}
Truth table33%1/3
A or not(B)
A= $self->{"Filter"}B= not defined $self->{" stream"}
ABresult
1X1
011
000
83 {
84 0 ▶ $self->{'Length'} = Text::PDF::Number->new(0) unless (defined $self->{'Length'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"Length"} 0 0 0
85 0 ▶ $pdf->new_obj($self->{'Length'}) unless ($self->{'Length'}->is_obj($pdf));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Length"}->is_obj($pdf) 0 0 0
86 # $pdf->out_obj($self->{'Length'});
87 }
88 else
89 {
90 1 ▶ $self->{'Length'} = Text::PDF::Number->new(length($self->{' stream'}) + ($self->{' stream'} =~ m/$cr$/o ? 0 : 1));
Branch: 1 branch
BranchTrueFalseTotal
$self->{' stream'} =~ /$Text::PDF::Dict::cr$/o ? : 0 1 1
91 }
92 }
93  
94 8 $fh->print("<<\n");
95 8 foreach ('Type', 'Subtype')
96 {
97 16 $specs{$_} = 1;
98 16 ▶ if (defined $self->{$_})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$_}) 5 11 16
99 {
100 5 $fh->print("/$_ ");
101 5 $self->{$_}->outobj($fh, $pdf, %opts);
102 5 $fh->print("\n");
103 }
104 }
105 8 foreach $key (sort {$a cmp $b} keys %{$self})
106 {
107 39 ▶ next if ($key =~ m/^[\s\-]/o || $specs{$key});
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^[\s\-]/o or $specs{$key} 24 15 39
Condition100%3/3
$key =~ /^[\s\-]/o or $specs{$key}
l!l&&r!l&&!r
19515
MC/DC100%2/2
$key =~ /^[\s\-]/o or $specs{$key}
$key =~ /^[\s\-]/o $specs{$key}
Truth table100%3/3
A or B
A= $key =~ /^[\s\-]/oB= $specs{$key}
ABresult
1X1
011
000
108 15 $val = $self->{$key};
109 15 ▶ next if ($val eq '');
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "" 0 15 15
110 15 $key = Text::PDF::Name::string_to_name ($key, $pdf);
111 15 $fh->print("/$key ");
112 15 $val->outobj($fh, $pdf, %opts);
113 15 $fh->print("\n");
114 }
115 8 $fh->print('>>');
116  
117 #now handle the stream (if any)
118 8 ▶ if (defined $self->{' streamloc'} && !defined $self->{' stream'})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{" streamloc"} and not defined $self->{" stream"}) 0 8 8
Condition33%1/3
defined $self->{" streamloc"} and not defined $self->{" stream"}
!ll&&!rl&&r
800
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
119 { # read a stream if infile
120 0 $loc = $fh->tell;
121 0 $self->read_stream;
122 0 $fh->seek($loc, 0);
123 }
124  
125 8 ▶ if (!$self->{' nofilt'}
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{" nofilt"} and defined $self->{" stream"} || defined $self->{" streamfile"} and defined $self->{"Filter"}) 0 8 8
Condition66%6/9
not $self->{" nofilt"} and defined $self->{" stream"} || defined $self->{" streamfile"} and defined $self->{"Filter"}
!ll&&!rl&&r
710
not $self->{" nofilt"} and defined $self->{" stream"} || defined $self->{" streamfile"}
!ll&&!rl&&r
071
defined $self->{" stream"} or defined $self->{" streamfile"}
l!l&&r!l&&!r
107
MC/DC0%0/4
not $self->{" nofilt"} and defined $self->{" stream"} || defined $self->{" streamfile"} and defined $self->{"Filter"}
not $self->{" nofilt"} defined $self->{" stream"} defined $self->{" streamfile"} defined $self->{"Filter"}
Truth table33%2/6
not(A) and B or C and D
A= not $self->{" nofilt"}B= defined $self->{" stream"}C= defined $self->{" streamfile"}D= defined $self->{"Filter"}
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
126 && (defined $self->{' stream'} || defined $self->{' streamfile'})
127 && defined $self->{'Filter'})
128 {
129 0 my ($hasflate) = -1;
130 0 my ($temp, $i, $temp1, @filtlist);
131
132 0 ▶ if (ref($self->{'Filter'}) eq 'Text::PDF::Name')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'Filter'} eq 'Text::PDF::Name') { } 0 0 0
133 0 { push(@filtlist, $self->{'Filter'}->val); }
134 else
135 {
136 0 ▶ for ($i = 0; $i < scalar @{$self->{'Filter'}{' val'}}; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$self->{"Filter"}{" val"};}) 0 0 0
137 0 { push (@filtlist, $self->{'Filter'}{' val'}[$i]->val); }
138 }
139 0 foreach $temp (@filtlist)
140 {
141 0 ▶ if ($temp eq 'LZWDecode') # hack to get around LZW patent
Branch: 2 branches
BranchTrueFalseTotal
if ($temp eq 'LZWDecode') { } 0 0 0
elsif ($temp eq 'FlateDecode') { } 0 0 0
142 {
143 0 ▶ if ($hasflate < -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($hasflate < -1) 0 0 0
144 {
145 0 $hasflate = $i;
146 0 next;
147 }
148 0 $temp = 'FlateDecode';
149 0 $self->{'Filter'}{' val'}[$i]{'val'} = $temp; # !!!
150 } elsif ($temp eq 'FlateDecode')
151 0 { $hasflate = -2; }
152 0 $temp1 = "Text::PDF::$temp";
153 0 push (@filts, $temp1->new);
154 }
155 0 ▶ splice(@{$self->{'Filter'}{' val'}}, $hasflate, 1) if ($hasflate > -1);
Branch: 1 branch
BranchTrueFalseTotal
if $hasflate > -1 0 0 0
156 }
157  
158 8 ▶ if (defined $self->{' stream'})
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{' stream'}) { } 1 7 8
elsif (defined $self->{' streamfile'}) { } 0 7 7
159 {
160 1 $fh->print("\nstream\n");
161 1 $loc = $fh->tell;
162 1 $str = $self->{' stream'};
163 1 ▶ unless ($self->{' nofilt'})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{" nofilt"}) 1 0 1
164 {
165 1 foreach $f (reverse @filts)
166 0 { $str = $f->outfilt($str, 1); }
167 }
168 1 $fh->print($str);
169 1 ▶ if (@filts > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (@filts > 0) 0 1 1
170 {
171 0 $len = $fh->tell - $loc + 1;
172 0 ▶ if ($self->{'Length'}{'val'} != $len)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Length"}{"val"} != $len) 0 0 0
173 {
174 0 $self->{'Length'}{'val'} = $len;
175 0 ▶ $pdf->out_obj($self->{'Length'}) if ($self->{'Length'}->is_obj($pdf));
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Length"}->is_obj($pdf) 0 0 0
176 }
177 }
178 1 ▶ $fh->print("\n") unless ($str =~ m/$cr$/o);
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /$Text::PDF::Dict::cr$/o 1 0 1
179 1 $fh->print("endstream");
180 # $self->{'Length'}->outobjdeep($fh);
181 } elsif (defined $self->{' streamfile'})
182 {
183 0 ▶ open(DICTFH, $self->{' streamfile'}) || die "Unable to open $self->{' streamfile'}";
Branch: 1 branch
BranchTrueFalseTotal
open DICTFH, $self->{" streamfile"} or die qq[Unable to open $self->{" streamfile"}] 0 0 0
184 0 binmode DICTFH;
185 0 $fh->print("\nstream\n");
186 0 $loc = $fh->tell;
187 0 ▶ while (read(DICTFH, $str, 4096))
Branch: 1 branch
BranchTrueFalseTotal
if (read DICTFH, $str, 4096) 0 0 0
188 {
189 0 ▶ unless ($self->{' nofilt'})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{" nofilt"}) 0 0 0
190 {
191 0 foreach $f (reverse @filts)
192 0 { $str = $f->outfilt($str, 0); }
193 }
194 0 $fh->print($str);
195 }
196 0 close(DICTFH);
197 0 ▶ unless ($self->{' nofilt'})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{" nofilt"}) 0 0 0
198 {
199 0 $str = '';
200 0 foreach $f (reverse @filts)
201 0 { $str = $f->outfilt($str, 1); }
202 0 $fh->print($str);
203 }
204
205 0 $len = $fh->tell - $loc + 1;
206 0 ▶ if ($self->{'Length'}{'val'} != $len)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Length"}{"val"} != $len) 0 0 0
207 {
208 0 $self->{'Length'}{'val'} = $len;
209 0 ▶ $pdf->out_obj($self->{'Length'}) if ($self->{'Length'}->is_obj($pdf));
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"Length"}->is_obj($pdf) 0 0 0
210 }
211
212 0 ▶ $fh->print("\n") unless ($str =~ m/$cr$/o);
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /$Text::PDF::Dict::cr$/o 0 0 0
213 0 $fh->print("endstream\n");
214 # $self->{'Length'}->outobjdeep($fh);
215 }
216 }
217  
218  
219 =head2 $d->read_stream($force_memory)
220  
221 Reads in a stream from a PDF file. If the stream is greater than
222 C<PDF::Dict::mincache> (defaults to 32768) bytes to be stored, then
223 the default action is to create a file for it somewhere and to use that
224 file as a data cache. If $force_memory is set, this caching will not
225 occur and the data will all be stored in the $self->{' stream'}
226 variable.
227  
228 =cut
229  
230 sub read_stream
231 1 ▶{
Subroutine read_stream: called CC=13 SCAR=34.3
Pod documented
232 1 my ($self, $force_memory) = @_;
233 1 my ($fh) = $self->{' streamsrc'};
234 1 my (@filts, $f, $last, $i, $dat);
235 1 my ($len) = $self->{'Length'}->val;
236  
237 1 $self->{' stream'} = '';
238  
239 1 ▶ if (defined $self->{'Filter'})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"Filter"}) 0 1 1
240 {
241 0 foreach $f ($self->{'Filter'}->elementsof)
242 {
243 0 my ($temp) = "Text::PDF::" . $f->val;
244 0 push(@filts, $temp->new());
245 }
246 }
247  
248 1 $last = 0;
249 1 ▶ if (defined $self->{' streamfile'})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{" streamfile"}) 0 1 1
250 {
251 0 unlink ($self->{' streamfile'});
252 0 $self->{' streamfile'} = undef;
253 }
254 1 seek ($fh, $self->{' streamloc'}, 0);
255 1 ▶ for ($i = 0; $i < $len; $i += 4096)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 1 1 2
256 {
257 1 ▶ if ($i + 4096 > $len)
Branch: 1 branch
BranchTrueFalseTotal
if ($i + 4096 > $len) { } 1 0 1
258 {
259 1 $last = 1;
260 1 read($fh, $dat, $len - $i);
261 }
262 else
263 0 { read($fh, $dat, 4096); }
264  
265 1 foreach $f (@filts)
266 0 { $dat = $f->infilt($dat, $last); }
267 1 ▶ if (!$force_memory && !defined $self->{' streamfile'} && ((length($self->{' stream'}) * 2) > $mincache))
Branch: 1 branch
BranchTrueFalseTotal
if (not $force_memory || defined $self->{" streamfile"} and length($self->{" stream"}) * 2 > $Text::PDF::Dict::mincache) 0 1 1
Condition33%2/6
not $force_memory || defined $self->{" streamfile"} and length($self->{" stream"}) * 2 > $Text::PDF::Dict::mincache
!ll&&!rl&&r
100
$force_memory || defined $self->{' streamfile'}
l!l&&r!l&&!r
100
MC/DC0%0/3
not $force_memory || defined $self->{" streamfile"} and length($self->{" stream"}) * 2 > $Text::PDF::Dict::mincache
$force_memory defined $self->{' streamfile'} length($self->{" stream"}) * 2 > $Text::PDF::Dict::mincache
Truth table25%1/4
not(A || B) and C
A= $force_memoryB= defined $self->{' streamfile'}C= length($self->{" stream"}) * 2 > $Text::PDF::Dict::mincache
ABCresult
1XX0
01X0
0000
0011
268 {
269 0 ▶ open (DICTFH, ">$tempbase") || next;
Branch: 1 branch
BranchTrueFalseTotal
open DICTFH, ">$Text::PDF::Dict::tempbase" or next 0 0 0
270 0 binmode DICTFH;
271 0 $self->{' streamfile'} = $tempbase;
272 0 $tempbase =~ s/-(\d+)$/"-" . ($1 + 1)/oe; # prepare for next use
273 0 print DICTFH $self->{' stream'};
274 0 undef $self->{' stream'};
275 }
276 1 ▶ if (defined $self->{' streamfile'})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{' streamfile'}) { } 0 1 1
277 0 { print DICTFH $dat; }
278 else
279 1 { $self->{' stream'} .= $dat; }
280 }
281
282 1 ▶ close DICTFH if (defined $self->{' streamfile'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{" streamfile"} 0 1 1
283 1 $self->{' nofilt'} = 0;
284 1 $self;
285 }
286
287 =head2 $d->val
288  
289 Returns the dictionary, which is itself.
290  
291 =cut
292  
293 sub val
294 1 ▶{ $_[0]; }
Subroutine val: called CC=1 SCAR=0.0
Pod documented
295  
296  
297 =head2 $d->copy($inpdf, $res, $unique, $outpdf, %opts)
298  
299 Copies an object. See Text::PDF::Objind::Copy() for details
300  
301 =cut
302  
303 sub copy
304 0 ▶{
Subroutine copy: not called CC=7 SCAR=40.3
Pod documented
305 0 my ($self, $inpdf, $res, $unique, $outpdf, %opts) = @_;
306 0 my ($k, $path);
307  
308 0 $res = $self->SUPER::copy($inpdf, $res, $unique, $outpdf, %opts);
309 0 $path = delete $opts{'path'};
310 0 foreach $k (keys %$self)
311 {
312 0 ▶ next if $self->dont_copy($k);
Branch: 1 branch
BranchTrueFalseTotal
if $self->dont_copy($k) 0 0 0
313 0 ▶ next if defined $res->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if defined $res->{$k} 0 0 0
314 0 ▶ if (UNIVERSAL::can($self->{$k}, "is_obj"))
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($self->{$k}, 'is_obj')) { } 0 0 0
315 {
316 0 ▶ if (grep {"$path/$k" =~ m|$_|} @{$opts{'clip'}})
Branch: 1 branch
BranchTrueFalseTotal
if (grep {("$path/$k") =~ /$_/;} @{$opts{'clip'};}) { } 0 0 0
317 0 { $res->{$k} = $self->{$k}; }
318 else
319 0 ▶ { $res->{$k} = $self->{$k}->realise->copy($inpdf, undef, $unique ? $unique + 1 : 0,
Branch: 1 branch
BranchTrueFalseTotal
$unique ? : 0 0 0
320 $outpdf, %opts, 'path' => "$path/$k"); }
321 }
322 else
323 0 { $res->{$k} = $self->{$k}; }
324 }
325 0 $res;
326 }
327  
328 1 1;
↑ Summary PDF/Filter.pm »