PDF/Dict.pm

filter: Statementstmt 49.3% 78 / 158 Branchbran 30.4% 28 / 92 Conditioncond 51.5% 17 / 33 MC/DCmcdc 16.6% 3 / 18 Subroutinesub 90.0% 9 / 10 Podpod 100% 5 / 5 totaltotal 44.3% 140 / 316 CC 66 SCAR 57.3
CC
66
Cov
43%
outobjdeep
495.4
copy
56.0
read_stream
31.0
CRAP 308.1
↑ 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 {
16 1 ▶ my $temp_dir = -d '/tmp' ? '/tmp' : $ENV{TMP} || $ENV{TEMP};
Subroutine BEGIN: called CC=3 SCAR=14.2
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 {
54 10 ▶ my ($class, @opts) = @_;
Subroutine new: called CC=2 SCAR=7.0
Pod documented
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 {
76 8 ▶ my ($self, $fh, $pdf, %opts) = @_;
Subroutine outobjdeep: called CC=45 SCAR=62.1
Pod documented
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"}) 0 7 7
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 ▶ while (($key, $val) = each %{$self})
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self;}) 39 8 47
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 ▶ next if ($val eq '');
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "" 0 15 15
109 15 $key = Text::PDF::Name::string_to_name ($key, $pdf);
110 15 $fh->print("/$key ");
111 15 $val->outobj($fh, $pdf, %opts);
112 15 $fh->print("\n");
113 }
114 8 $fh->print('>>');
115  
116 #now handle the stream (if any)
117 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
118 { # read a stream if infile
119 0 $loc = $fh->tell;
120 0 $self->read_stream;
121 0 $fh->seek($loc, 0);
122 }
123  
124 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
125 && (defined $self->{' stream'} || defined $self->{' streamfile'})
126 && defined $self->{'Filter'})
127 {
128 0 my ($hasflate) = -1;
129 0 my ($temp, $i, $temp1, @filtlist);
130
131 0 ▶ if (ref($self->{'Filter'}) eq 'Text::PDF::Name')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'Filter'} eq 'Text::PDF::Name') { } 0 0 0
132 0 { push(@filtlist, $self->{'Filter'}->val); }
133 else
134 {
135 0 ▶ for ($i = 0; $i < scalar @{$self->{'Filter'}{' val'}}; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$self->{"Filter"}{" val"};}) 0 0 0
136 0 { push (@filtlist, $self->{'Filter'}{' val'}[$i]->val); }
137 }
138 0 foreach $temp (@filtlist)
139 {
140 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
141 {
142 0 ▶ if ($hasflate < -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($hasflate < -1) 0 0 0
143 {
144 0 $hasflate = $i;
145 0 next;
146 }
147 0 $temp = 'FlateDecode';
148 0 $self->{'Filter'}{' val'}[$i]{'val'} = $temp; # !!!
149 } elsif ($temp eq 'FlateDecode')
150 0 { $hasflate = -2; }
151 0 $temp1 = "Text::PDF::$temp";
152 0 push (@filts, $temp1->new);
153 }
154 0 ▶ splice(@{$self->{'Filter'}{' val'}}, $hasflate, 1) if ($hasflate > -1);
Branch: 1 branch
BranchTrueFalseTotal
if $hasflate > -1 0 0 0
155 }
156  
157 8 ▶ if (defined $self->{' stream'})
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{' stream'}) { } 1 7 8
elsif (defined $self->{' streamfile'}) { } 0 7 7
158 {
159 1 $fh->print("\nstream\n");
160 1 $loc = $fh->tell;
161 1 $str = $self->{' stream'};
162 1 ▶ unless ($self->{' nofilt'})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{" nofilt"}) 1 0 1
163 {
164 1 foreach $f (reverse @filts)
165 0 { $str = $f->outfilt($str, 1); }
166 }
167 1 $fh->print($str);
168 1 ▶ if (@filts > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (@filts > 0) 0 1 1
169 {
170 0 $len = $fh->tell - $loc + 1;
171 0 ▶ if ($self->{'Length'}{'val'} != $len)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Length"}{"val"} != $len) 0 0 0
172 {
173 0 $self->{'Length'}{'val'} = $len;
174 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
175 }
176 }
177 1 ▶ $fh->print("\n") unless ($str =~ m/$cr$/o);
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /$Text::PDF::Dict::cr$/o 1 0 1
178 1 $fh->print("endstream");
179 # $self->{'Length'}->outobjdeep($fh);
180 } elsif (defined $self->{' streamfile'})
181 {
182 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
183 0 binmode DICTFH;
184 0 $fh->print("\nstream\n");
185 0 $loc = $fh->tell;
186 0 ▶ while (read(DICTFH, $str, 4096))
Branch: 1 branch
BranchTrueFalseTotal
if (read DICTFH, $str, 4096) 0 0 0
187 {
188 0 ▶ unless ($self->{' nofilt'})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{" nofilt"}) 0 0 0
189 {
190 0 foreach $f (reverse @filts)
191 0 { $str = $f->outfilt($str, 0); }
192 }
193 0 $fh->print($str);
194 }
195 0 close(DICTFH);
196 0 ▶ unless ($self->{' nofilt'})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{" nofilt"}) 0 0 0
197 {
198 0 $str = '';
199 0 foreach $f (reverse @filts)
200 0 { $str = $f->outfilt($str, 1); }
201 0 $fh->print($str);
202 }
203
204 0 $len = $fh->tell - $loc + 1;
205 0 ▶ if ($self->{'Length'}{'val'} != $len)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Length"}{"val"} != $len) 0 0 0
206 {
207 0 $self->{'Length'}{'val'} = $len;
208 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
209 }
210
211 0 ▶ $fh->print("\n") unless ($str =~ m/$cr$/o);
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /$Text::PDF::Dict::cr$/o 0 0 0
212 0 $fh->print("endstream\n");
213 # $self->{'Length'}->outobjdeep($fh);
214 }
215 }
216  
217  
218 =head2 $d->read_stream($force_memory)
219  
220 Reads in a stream from a PDF file. If the stream is greater than
221 C<PDF::Dict::mincache> (defaults to 32768) bytes to be stored, then
222 the default action is to create a file for it somewhere and to use that
223 file as a data cache. If $force_memory is set, this caching will not
224 occur and the data will all be stored in the $self->{' stream'}
225 variable.
226  
227 =cut
228  
229 sub read_stream
230 {
231 1 ▶ my ($self, $force_memory) = @_;
Subroutine read_stream: called CC=13 SCAR=34.3
Pod documented
232 1 my ($fh) = $self->{' streamsrc'};
233 1 my (@filts, $f, $last, $i, $dat);
234 1 my ($len) = $self->{'Length'}->val;
235  
236 1 $self->{' stream'} = '';
237  
238 1 ▶ if (defined $self->{'Filter'})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"Filter"}) 0 1 1
239 {
240 0 foreach $f ($self->{'Filter'}->elementsof)
241 {
242 0 my ($temp) = "Text::PDF::" . $f->val;
243 0 push(@filts, $temp->new());
244 }
245 }
246  
247 1 $last = 0;
248 1 ▶ if (defined $self->{' streamfile'})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{" streamfile"}) 0 1 1
249 {
250 0 unlink ($self->{' streamfile'});
251 0 $self->{' streamfile'} = undef;
252 }
253 1 seek ($fh, $self->{' streamloc'}, 0);
254 1 ▶ for ($i = 0; $i < $len; $i += 4096)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 1 1 2
255 {
256 1 ▶ if ($i + 4096 > $len)
Branch: 1 branch
BranchTrueFalseTotal
if ($i + 4096 > $len) { } 1 0 1
257 {
258 1 $last = 1;
259 1 read($fh, $dat, $len - $i);
260 }
261 else
262 0 { read($fh, $dat, 4096); }
263  
264 1 foreach $f (@filts)
265 0 { $dat = $f->infilt($dat, $last); }
266 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
267 {
268 0 ▶ open (DICTFH, ">$tempbase") || next;
Branch: 1 branch
BranchTrueFalseTotal
open DICTFH, ">$Text::PDF::Dict::tempbase" or next 0 0 0
269 0 binmode DICTFH;
270 0 $self->{' streamfile'} = $tempbase;
271 0 $tempbase =~ s/-(\d+)$/"-" . ($1 + 1)/oe; # prepare for next use
272 0 print DICTFH $self->{' stream'};
273 0 undef $self->{' stream'};
274 }
275 1 ▶ if (defined $self->{' streamfile'})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{' streamfile'}) { } 0 1 1
276 0 { print DICTFH $dat; }
277 else
278 1 { $self->{' stream'} .= $dat; }
279 }
280
281 1 ▶ close DICTFH if (defined $self->{' streamfile'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{" streamfile"} 0 1 1
282 1 $self->{' nofilt'} = 0;
283 1 $self;
284 }
285
286 =head2 $d->val
287  
288 Returns the dictionary, which is itself.
289  
290 =cut
291  
292 sub val
293 1 ▶{ $_[0]; }
Subroutine val: called CC=1 SCAR=0.0
Pod documented
294  
295  
296 =head2 $d->copy($inpdf, $res, $unique, $outpdf, %opts)
297  
298 Copies an object. See Text::PDF::Objind::Copy() for details
299  
300 =cut
301  
302 sub copy
303 {
304 0 ▶ my ($self, $inpdf, $res, $unique, $outpdf, %opts) = @_;
Subroutine copy: not called CC=7 SCAR=40.3
Pod documented
305 0 my ($k, $path);
306  
307 0 $res = $self->SUPER::copy($inpdf, $res, $unique, $outpdf, %opts);
308 0 $path = delete $opts{'path'};
309 0 foreach $k (keys %$self)
310 {
311 0 ▶ next if $self->dont_copy($k);
Branch: 1 branch
BranchTrueFalseTotal
if $self->dont_copy($k) 0 0 0
312 0 ▶ next if defined $res->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if defined $res->{$k} 0 0 0
313 0 ▶ if (UNIVERSAL::can($self->{$k}, "is_obj"))
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($self->{$k}, 'is_obj')) { } 0 0 0
314 {
315 0 ▶ if (grep {"$path/$k" =~ m|$_|} @{$opts{'clip'}})
Branch: 1 branch
BranchTrueFalseTotal
if (grep {("$path/$k") =~ /$_/;} @{$opts{'clip'};}) { } 0 0 0
316 0 { $res->{$k} = $self->{$k}; }
317 else
318 0 ▶ { $res->{$k} = $self->{$k}->realise->copy($inpdf, undef, $unique ? $unique + 1 : 0,
Branch: 1 branch
BranchTrueFalseTotal
$unique ? : 0 0 0
319 $outpdf, %opts, 'path' => "$path/$k"); }
320 }
321 else
322 0 { $res->{$k} = $self->{$k}; }
323 }
324 0 $res;
325 }
326
327  
↑ Summary PDF/Filter.pm »