API2/Basic/PDF/File.pm

filter: Statementstmt 76.0% 563 / 740 Branchbran 59.5% 219 / 368 Conditioncond 49.5% 50 / 101 MC/DCmcdc 29.5% 18 / 61 Subroutinesub 93.7% 45 / 48 Podpod 100% 27 / 27 totaltotal 68.5% 922 / 1345 CC 237 SCAR 39.9
CC
237
Cov
69%
copy
210.0
out_trailer
117.6
readval
112.2
CRAP 54.0
« API2/Basic/PDF/Literal.pm ↑ Summary API2/Resource/XObject/Form/BarCode/qrcode.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::File;
10  
11 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 40 our $VERSION = '2.049'; # VERSION
14  
15 =head1 NAME
16  
17 PDF::API2::Basic::PDF::File - Low-level PDF file access
18  
19 =head1 SYNOPSIS
20  
21 $p = PDF::API2::Basic::PDF::File->open("filename.pdf", 1);
22 $p->new_obj($obj_ref);
23 $p->free_obj($obj_ref);
24 $p->append_file;
25 $p->close_file;
26 $p->release; # IMPORTANT!
27  
28 =head1 DESCRIPTION
29  
30 This class keeps track of the directory aspects of a PDF file. There are two
31 parts to the directory: the main directory object which is the parent to all
32 other objects and a chain of cross-reference tables and corresponding trailer
33 dictionaries starting with the main directory object.
34  
35 =head1 INSTANCE VARIABLES
36  
37 Within this class hierarchy, rather than making everything visible via methods,
38 which would be a lot of work, there are various instance variables which are
39 accessible via associative array referencing. To distinguish instance variables
40 from content variables (which may come from the PDF content itself), each such
41 variable will start with a space.
42  
43 Variables which do not start with a space directly reflect elements in a PDF
44 dictionary. In the case of a PDF::API2::Basic::PDF::File, the elements reflect those in the
45 trailer dictionary.
46  
47 Since some variables are not designed for class users to access, variables are
48 marked in the documentation with (R) to indicate that such an entry should only
49 be used as read-only information. (P) indicates that the information is private
50 and not designed for user use at all, but is included in the documentation for
51 completeness and to ensure that nobody else tries to use it.
52  
53 =over
54  
55 =item newroot
56  
57 This variable allows the user to create a new root entry to occur in the trailer
58 dictionary which is output when the file is written or appended. If you wish to
59 over-ride the root element in the dictionary you have, use this entry to indicate
60 that without losing the current Root entry. Notice that newroot should point to
61 a PDF level object and not just to a dictionary which does not have object status.
62  
63 =item INFILE (R)
64  
65 Contains the filehandle used to read this information into this PDF directory. Is
66 an IO object.
67  
68 =item fname (R)
69  
70 This is the filename which is reflected by INFILE, or the original IO object passed
71 in.
72  
73 =item update (R)
74  
75 This indicates that the read file has been opened for update and that at some
76 point, $p->appendfile() can be called to update the file with the changes that
77 have been made to the memory representation.
78  
79 =item maxobj (R)
80  
81 Contains the first usable object number above any that have already appeared
82 in the file so far.
83  
84 =item outlist (P)
85  
86 This is a list of Objind which are to be output when the next appendfile or outfile
87 occurs.
88  
89 =item firstfree (P)
90  
91 Contains the first free object in the free object list. Free objects are removed
92 from the front of the list and added to the end.
93  
94 =item lastfree (P)
95  
96 Contains the last free object in the free list. It may be the same as the firstfree
97 if there is only one free object.
98  
99 =item objcache (P)
100  
101 All objects are held in the cache to ensure that a system only has one occurrence of
102 each object. In effect, the objind class acts as a container type class to hold the
103 PDF object structure and it would be unfortunate if there were two identical
104 place-holders floating around a system.
105  
106 =item epos (P)
107  
108 The end location of the read-file.
109  
110 =back
111  
112 Each trailer dictionary contains a number of private instance variables which
113 hold the chain together.
114  
115 =over
116  
117 =item loc (P)
118  
119 Contains the location of the start of the cross-reference table preceding the
120 trailer.
121  
122 =item xref (P)
123  
124 Contains an anonymous array of each cross-reference table entry.
125  
126 =item prev (P)
127  
128 A reference to the previous table. Note this differs from the Prev entry which
129 is in PDF which contains the location of the previous cross-reference table.
130  
131 =back
132  
133 =head1 METHODS
134  
135 =cut
136  
137 40 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
138  
139 40 ▶use vars qw($cr $irreg_char $reg_char $ws_char $delim_char %types);
Subroutine BEGIN: called CC=1 SCAR=0.0
140  
141 40 $ws_char = '[ \t\r\n\f\0]';
142 40 $delim_char = '[][<>{}()/%]';
143 40 $reg_char = '[^][<>{}()/% \t\r\n\f\0]';
144 40 $irreg_char = '[][<>{}()/% \t\r\n\f\0]';
145 40 $cr = '\s*(?:\015|\012|(?:\015\012))';
146  
147 40 my $re_comment = qr/(?:\%[^\r\n]*)/;
148 40 my $re_whitespace = qr/(?:[ \t\r\n\f\0]|$re_comment)/;
149  
150 40 %types = (
151 'Page' => 'PDF::API2::Basic::PDF::Page',
152 'Pages' => 'PDF::API2::Basic::PDF::Pages',
153 );
154  
155 40 my $readDebug = 0;
156  
157 40 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
158 40 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
159  
160 # Now for the basic PDF types
161 40 ▶use PDF::API2::Basic::PDF::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
162  
163 40 ▶use PDF::API2::Basic::PDF::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
164 40 ▶use PDF::API2::Basic::PDF::Bool;
Subroutine BEGIN: called CC=1 SCAR=0.0
165 40 ▶use PDF::API2::Basic::PDF::Dict;
Subroutine BEGIN: called CC=1 SCAR=0.0
166 40 ▶use PDF::API2::Basic::PDF::Name;
Subroutine BEGIN: called CC=1 SCAR=0.0
167 40 ▶use PDF::API2::Basic::PDF::Number;
Subroutine BEGIN: called CC=1 SCAR=0.0
168 40 ▶use PDF::API2::Basic::PDF::Objind;
Subroutine BEGIN: called CC=1 SCAR=0.0
169 40 ▶use PDF::API2::Basic::PDF::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
170 40 ▶use PDF::API2::Basic::PDF::Page;
Subroutine BEGIN: called CC=1 SCAR=0.0
171 40 ▶use PDF::API2::Basic::PDF::Pages;
Subroutine BEGIN: called CC=1 SCAR=0.0
172 40 ▶use PDF::API2::Basic::PDF::Null;
Subroutine BEGIN: called CC=1 SCAR=0.0
173 40 ▶use POSIX qw(ceil floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
174  
175 40 ▶no warnings qw[ deprecated recursion uninitialized ];
Subroutine BEGIN: called CC=1 SCAR=0.0
176  
177  
178 =head2 PDF::API2::Basic::PDF::File->new
179  
180 Creates a new, empty file object which can act as the host to other PDF objects.
181 Since there is no file associated with this object, it is assumed that the
182 object is created in readiness for creating a new PDF file.
183  
184 =cut
185  
186 164 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
187 164 my ($class, $root) = @_;
188 164 my $self = $class->_new();
189  
190 164 ▶ unless ($root) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($root) 164 0 164
191 164 $root = PDFDict();
192 164 $root->{'Type'} = PDFName('Catalog');
193 }
194 164 $self->new_obj($root);
195 164 $self->{'Root'} = $root;
196 164 return $self;
197 }
198  
199  
200 =head2 $p = PDF::API2::Basic::PDF::File->open($filename, $update)
201  
202 Opens the file and reads all the trailers and cross reference tables to build
203 a complete directory of objects.
204  
205 $update specifies whether this file is being opened for updating and editing,
206 or simply to be read.
207  
208 $filename may be an IO object
209  
210 =cut
211  
212 16 ▶sub open {
Subroutine open: called CC=15 SCAR=28.0
Pod documented
213 16 my ($class, $filename, $update) = @_;
214 16 my ($fh, $buffer);
215  
216 16 my $self = $class->_new();
217 16 ▶ if (ref $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $filename) { } 8 8 16
218 8 $self->{' INFILE'} = $filename;
219 8 ▶ if ($update) {
Branch: 1 branch
BranchTrueFalseTotal
if ($update) 8 0 8
220 8 $self->{' update'} = 1;
221 8 $self->{' OUTFILE'} = $filename;
222 }
223 8 $fh = $filename;
224 }
225 else {
226 8 ▶ die "File '$filename' does not exist" unless -f $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $filename 0 8 8
227 8 ▶ die "File '$filename' is not readable" unless -r $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $filename 0 8 8
228 8 ▶ if ($update) {
Branch: 1 branch
BranchTrueFalseTotal
if ($update) 8 0 8
229 8 ▶ die "File '$filename' is not writable" unless -w $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -w $filename 0 8 8
230 }
231 8 ▶ $fh = IO::File->new(($update ? '+' : '') . "<$filename")
Branch: 1 branch
BranchTrueFalseTotal
$update ? : 8 0 8
Condition50%1/2
'IO::File'->new(($update ? '+' : '') . "<$filename") || die("Error opening '${filename}': $!")
l!l
80
MC/DC0%0/1
'IO::File'->new(($update ? '+' : '') . "<$filename") || die("Error opening '${filename}': $!")
'IO::File'->new(($update ? '+' : '') . "<$filename")
Truth table50%1/2
A
A= 'IO::File'->new(($update ? '+' : '') . "<$filename")
Aresult
11
00
232 || die "Error opening '$filename': $!";
233 8 $self->{' INFILE'} = $fh;
234 8 ▶ if ($update) {
Branch: 1 branch
BranchTrueFalseTotal
if ($update) 8 0 8
235 8 $self->{' update'} = 1;
236 8 $self->{' OUTFILE'} = $fh;
237 8 $self->{' fname'} = $filename;
238 }
239 }
240 16 binmode $fh, ':raw';
241 16 $fh->seek(0, 0); # go to start of file
242 16 $fh->read($buffer, 255);
243 16 ▶ unless ($buffer =~ /^\%PDF\-([12]\.\d+)\s*$cr/m) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($buffer =~ /^\%PDF\-([12]\.\d+)\s*$PDF::API2::Basic::PDF::File::cr/m) 0 16 16
244 0 croak "$filename does not appear to be a valid PDF";
245 }
246 16 $self->{' version'} = $1;
247  
248 16 $fh->seek(0, 2); # go to end of file
249 16 my $end = $fh->tell();
250 16 $self->{' epos'} = $end;
251 16 foreach my $offset (1..64) {
252 32 $fh->seek($end - 16 * $offset, 0);
253 32 $fh->read($buffer, 16 * $offset);
254 32 ▶ last if $buffer =~ m/startxref($cr|\s*)\d+($cr|\s*)\%\%eof.*?/i;
Branch: 1 branch
BranchTrueFalseTotal
if $buffer =~ /startxref($PDF::API2::Basic::PDF::File::cr|\s*)\d+($PDF::API2::Basic::PDF::File::cr|\s*)\%\%eof.*?/i 16 16 32
255 }
256 16 ▶ unless ($buffer =~ m/startxref[^\d]+([0-9]+)($cr|\s*)\%\%eof.*?/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($buffer =~ /startxref[^\d]+([0-9]+)($PDF::API2::Basic::PDF::File::cr|\s*)\%\%eof.*?/i) 0 16 16
257 0 die "Malformed PDF file $filename";
258 }
259 16 my $xpos = $1;
260 16 $self->{' xref_position'} = $xpos;
261  
262 16 my $tdict = $self->readxrtr($xpos, $self);
263 16 foreach my $key (keys %$tdict) {
264 119 $self->{$key} = $tdict->{$key};
265 }
266 16 return $self;
267 }
268  
269 =head2 $p->version($version)
270  
271 Gets/sets the PDF version (e.g. 1.4)
272  
273 =cut
274  
275 9 ▶sub version {
Subroutine version: called CC=5 SCAR=16.4
Pod documented
276 9 my $self = shift();
277  
278 9 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 7 9
279 2 my $version = shift();
280 2 ▶ croak "Invalid version $version" unless $version =~ /^([12]\.[0-9]+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $version =~ /^([12]\.[0-9]+)$/ 0 2 2
281 2 $self->header_version($version);
282 2 ▶ if ($version >= 1.4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version >= 1.4) { } 2 0 2
283 2 $self->trailer_version($version);
284 }
285 else {
286 0 delete $self->{'Root'}->{'Version'};
287 0 $self->out_obj($self->{'Root'});
288 }
289 2 return $version;
290 }
291  
292 7 my $header_version = $self->header_version();
293 7 my $trailer_version = $self->trailer_version();
294 7 ▶ return $trailer_version if $trailer_version > $header_version;
Branch: 1 branch
BranchTrueFalseTotal
if $trailer_version > $header_version 4 3 7
295 3 return $header_version;
296 }
297  
298 =head2 $version = $p->header_version($version)
299  
300 Gets/sets the PDF version stored in the file header.
301  
302 =cut
303  
304 14 ▶sub header_version {
Subroutine header_version: called CC=3 SCAR=11.0
Pod documented
305 14 my $self = shift();
306  
307 14 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 5 9 14
308 5 my $version = shift();
309 5 ▶ croak "Invalid version $version" unless $version =~ /^([12]\.[0-9]+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $version =~ /^([12]\.[0-9]+)$/ 0 5 5
310 5 $self->{' version'} = $version;
311 }
312  
313 14 return $self->{' version'};
314 }
315  
316 =head2 $version = $p->trailer_version($version)
317  
318 Gets/sets the PDF version stored in the document catalog.
319  
320 =cut
321  
322 12 ▶sub trailer_version {
Subroutine trailer_version: called CC=4 SCAR=13.9
Pod documented
323 12 my $self = shift();
324  
325 12 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 8 12
326 4 my $version = shift();
327 4 ▶ croak "Invalid version $version" unless $version =~ /^([12]\.[0-9]+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $version =~ /^([12]\.[0-9]+)$/ 0 4 4
328 4 $self->{'Root'}->{'Version'} = PDFName($version);
329 4 $self->out_obj($self->{'Root'});
330 4 return $version;
331 }
332  
333 8 ▶ return unless $self->{'Root'}->{'Version'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"Root"}{"Version"} 0 8 8
334 8 $self->{'Root'}->{'Version'}->realise();
335 8 return $self->{'Root'}->{'Version'}->val();
336 }
337  
338 =head2 $prev_version = $p->require_version($version)
339  
340 Ensures that the PDF version is at least C<$version>.
341  
342 =cut
343  
344 3 ▶sub require_version {
Subroutine require_version: called CC=2 SCAR=6.9
Pod documented
345 3 my ($self, $min_version) = @_;
346 3 my $current_version = $self->version();
347 3 ▶ $self->version($min_version) if $current_version < $min_version;
Branch: 1 branch
BranchTrueFalseTotal
if $current_version < $min_version 1 2 3
348 3 return $current_version;
349 }
350  
351 =head2 $p->release()
352  
353 Releases ALL of the memory used by the PDF document and all of its
354 component objects. After calling this method, do B<NOT> expect to
355 have anything left in the C<PDF::API2::Basic::PDF::File> object (so if
356 you need to save, be sure to do it before calling this method).
357  
358 B<NOTE>, that it is important that you call this method on any
359 C<PDF::API2::Basic::PDF::File> object when you wish to destruct it and
360 free up its memory. Internally, PDF files have an enormous number of
361 cross-references and this causes circular references within the
362 internal data structures. Calling 'C<release()>' forces a brute-force
363 cleanup of the data structures, freeing up all of the memory. Once
364 you've called this method, though, don't expect to be able to do
365 anything else with the C<PDF::API2::Basic::PDF::File> object; it'll
366 have B<no> internal state whatsoever.
367  
368 =cut
369  
370 # Maintainer's Question: Couldn't this be handled by a DESTROY method
371 # instead of requiring an explicit call to release()?
372 618 ▶sub release {
Subroutine release: called CC=9 SCAR=22.0
Pod documented
373 618 my $self = shift();
374  
375 618 ▶ return $self unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 618 618
376 618 my @tofree = values %$self;
377  
378 618 foreach my $key (keys %$self) {
379 2279 $self->{$key} = undef;
380 2279 delete $self->{$key};
381 }
382  
383 # PDFs with highly-interconnected page trees or outlines can hit Perl's
384 # recursion limit pretty easily, so disable the warning for this specific
385 # loop.
386 40 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
387  
388 618 ▶ while (my $item = shift @tofree) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $item = shift @tofree) 7136 618 7754
389 7136 ▶ if (blessed($item) and $item->can('release')) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($item) and $item->can('release')) { } 2118 5018 7136
elsif (ref $item eq 'ARRAY') { } 1555 3463 5018
elsif (ref $item eq 'HASH') { } 890 2573 3463
Condition100%3/3
builtin::blessed($item) and $item->can("release")
!ll&&!rl&&r
47242942118
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
390 2118 $item->release(1);
391 }
392 elsif (ref($item) eq 'ARRAY') {
393 1555 push @tofree, @$item;
394 }
395 elsif (ref($item) eq 'HASH') {
396 890 push @tofree, values %$item;
397 890 foreach my $key (keys %$item) {
398 3850 $item->{$key} = undef;
399 3850 delete $item->{$key};
400 }
401 }
402 else {
403 2573 $item = undef;
404 }
405 }
406 }
407  
408 =head2 $p->append_file()
409  
410 Appends the objects for output to the read file and then appends the appropriate table.
411  
412 =cut
413  
414 3 ▶sub append_file {
Subroutine append_file: called CC=5 SCAR=16.4
Pod documented
415 3 my $self = shift();
416 3 ▶ return unless $self->{' update'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{" update"} 0 3 3
417  
418 3 my $fh = $self->{' INFILE'};
419  
420 3 my $tdict = PDFDict();
421 3 $tdict->{'Prev'} = PDFNum($self->{' loc'});
422 3 $tdict->{'Info'} = $self->{'Info'};
423 3 ▶ if (defined $self->{' newroot'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{' newroot'}) { } 0 3 3
424 0 $tdict->{'Root'} = $self->{' newroot'};
425 }
426 else {
427 3 $tdict->{'Root'} = $self->{'Root'};
428 }
429 3 $tdict->{'Size'} = $self->{'Size'};
430  
431 3 foreach my $key (grep { $_ !~ m/^\s/ } keys %$self) {
432 9 ▶ $tdict->{$key} = $self->{$key} unless defined $tdict->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tdict->{$key} 0 9 9
433 }
434  
435 3 $fh->seek($self->{' epos'}, 0);
436 3 $self->out_trailer($tdict, $self->{' update'});
437 3 close $self->{' OUTFILE'};
438 }
439  
440  
441 =head2 $p->out_file($fname)
442  
443 Writes a PDF file to a file of the given filename based on the current list of
444 objects to be output. It creates the trailer dictionary based on information
445 in $self.
446  
447 $fname may be an IO object;
448  
449 =cut
450  
451 140 ▶sub out_file {
Subroutine out_file: called CC=1 SCAR=0.0
Pod documented
452 140 my ($self, $fname) = @_;
453  
454 140 $self->create_file($fname);
455 140 $self->close_file();
456 140 return $self;
457 }
458  
459  
460 =head2 $p->create_file($fname)
461  
462 Creates a new output file (no check is made of an existing open file) of
463 the given filename or IO object. Note, make sure that $p->{' version'} is set
464 correctly before calling this function.
465  
466 =cut
467  
468 140 ▶sub create_file {
Subroutine create_file: called CC=4 SCAR=15.5
Pod documented
469 140 my ($self, $filename) = @_;
470 140 my $fh;
471  
472 140 $self->{' fname'} = $filename;
473 140 ▶ if (ref $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $filename) { } 140 0 140
474 140 $fh = $filename;
475 }
476 else {
477 0 ▶ $fh = IO::File->new(">$filename") || die "Unable to open $filename for writing";
Condition0%0/2
'IO::File'->new(">$filename") || die("Unable to open $filename for writing")
l!l
00
MC/DC0%0/1
'IO::File'->new(">$filename") || die("Unable to open $filename for writing")
'IO::File'->new(">$filename")
Truth table0%0/2
A
A= 'IO::File'->new(">$filename")
Aresult
11
00
478 0 binmode($fh,':raw');
479 }
480  
481 140 $self->{' OUTFILE'} = $fh;
482 140 ▶ $fh->print('%PDF-' . ($self->{' version'} // '1.4') . "\n");
Condition50%1/2
$self->{" version"} // "1.4"
l!l
1400
MC/DC0%0/1
$self->{" version"} // "1.4"
$self->{" version"}
Truth table50%1/2
A
A= $self->{" version"}
Aresult
11
00
483 140 $fh->print("%\xC6\xCD\xCD\xB5\n"); # and some binary stuff in a comment
484 140 return $self;
485 }
486  
487  
488 =head2 $p->clone_file($fname)
489  
490 Creates a copy of the input file at the specified filename and sets it as the
491 output file for future writes. A file handle may be passed instead of a
492 filename.
493  
494 =cut
495  
496 4 ▶sub clone_file {
Subroutine clone_file: called CC=4 SCAR=14.4
Pod documented
497 4 my ($self, $filename) = @_;
498 4 my $fh;
499  
500 4 $self->{' fname'} = $filename;
501 4 ▶ if (ref $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $filename) { } 4 0 4
502 4 $fh = $filename;
503 }
504 else {
505 0 ▶ $fh = IO::File->new(">$filename") || die "Unable to open $filename for writing";
Condition0%0/2
'IO::File'->new(">$filename") || die("Unable to open $filename for writing")
l!l
00
MC/DC0%0/1
'IO::File'->new(">$filename") || die("Unable to open $filename for writing")
'IO::File'->new(">$filename")
Truth table0%0/2
A
A= 'IO::File'->new(">$filename")
Aresult
11
00
506 0 binmode($fh,':raw');
507 }
508  
509 4 $self->{' OUTFILE'} = $fh;
510  
511 4 my $in = $self->{' INFILE'};
512 4 $in->seek(0, 0);
513 4 my $data;
514 4 ▶ while (not $in->eof()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in->eof) 4 4 8
515 4 $in->read($data, 1024 * 1024);
516 4 $fh->print($data);
517 }
518 4 return $self;
519 }
520  
521 =head2 $p->close_file
522  
523 Closes up the open file for output by outputting the trailer etc.
524  
525 =cut
526  
527 145 ▶sub close_file {
Subroutine close_file: called CC=11 SCAR=25.8
Pod documented
528 145 my $self = shift();
529  
530 145 my $tdict = PDFDict();
531 145 ▶ $tdict->{'Info'} = $self->{'Info'} if defined $self->{'Info'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"Info"} 145 0 145
532 145 ▶ $tdict->{'Root'} = (defined $self->{' newroot'} and $self->{' newroot'} ne '') ? $self->{' newroot'} : $self->{'Root'};
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{' newroot'} && $self->{' newroot'} ne '' ? : 0 145 145
Condition33%1/3
defined $self->{" newroot"} and $self->{" newroot"} ne ""
!ll&&!rl&&r
14500
MC/DC0%0/2
defined $self->{" newroot"} and $self->{" newroot"} ne ""
defined $self->{" newroot"} $self->{" newroot"} ne ""
Truth table33%1/3
A and B
A= defined $self->{" newroot"}B= $self->{" newroot"} ne ""
ABresult
0X0
100
111
533  
534 # remove all freed objects from the outlist, AND the outlist_cache if not updating
535 # NO! Don't do that thing! In fact, let out_trailer do the opposite!
536  
537 145 ▶ $tdict->{'Size'} = $self->{'Size'} || PDFNum(1);
Condition66%2/3
$self->{'Size'} || PDFNum(1)
l!l&&r!l&&!r
51400
MC/DC0%0/2
$self->{'Size'} || PDFNum(1)
$self->{'Size'} PDFNum(1)
Truth table66%2/3
A || B
A= $self->{'Size'}B= PDFNum(1)
ABresult
1X1
011
000
538 145 ▶ $tdict->{'Prev'} = PDFNum($self->{' loc'}) if $self->{' loc'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{" loc"} 5 140 145
539 145 ▶ if ($self->{' update'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{" update"}) 5 140 145
540 5 foreach my $key (grep { $_ !~ m/^[\s\-]/ } keys %$self) {
541 16 ▶ $tdict->{$key} = $self->{$key} unless defined $tdict->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tdict->{$key} 0 16 16
542 }
543  
544 5 my $fh = $self->{' INFILE'};
545 5 $fh->seek($self->{' epos'}, 0);
546 }
547  
548 145 $self->out_trailer($tdict, $self->{' update'});
549 145 close($self->{' OUTFILE'});
550 145 ▶ if ($^O eq 'MacOS' and not ref($self->{' fname'})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MacOS" and not ref $self->{" fname"}) 0 145 145
Condition33%1/3
$^O eq "MacOS" and not ref $self->{" fname"}
!ll&&!rl&&r
14500
MC/DC0%0/2
$^O eq "MacOS" and not ref $self->{" fname"}
$^O eq "MacOS" not ref $self->{" fname"}
Truth table33%1/3
A and not(B)
A= $^O eq "MacOS"B= not ref $self->{" fname"}
ABresult
0X0
100
111
551 0 MacPerl::SetFileInfo('CARO', 'TEXT', $self->{' fname'});
552 }
553  
554 145 return $self;
555 }
556  
557 =head2 ($value, $str) = $p->readval($str, %opts)
558  
559 Reads a PDF value from the current position in the file. If $str is too short
560 then read some more from the current location in the file until the whole object
561 is read. This is a recursive call which may slurp in a whole big stream (unprocessed).
562  
563 Returns the recursive data structure read and also the current $str that has been
564 read from the file.
565  
566 =cut
567  
568 842 ▶sub readval {
Subroutine readval: called CC=52 SCAR=47.2
Pod documented
569 842 my ($self, $str, %opts) = @_;
570 842 my $fh = $self->{' INFILE'};
571 842 my ($result, $value);
572  
573 842 ▶ my $update = defined($opts{update}) ? $opts{update} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'update'} ? : 10 832 842
574 842 ▶ $str = update($fh, $str) if $update;
Branch: 1 branch
BranchTrueFalseTotal
if $update 832 10 842
575  
576 842 $str =~ s/^$ws_char+//; # Ignore initial white space
577 842 $str =~ s/^\%[^\015\012]*$ws_char+//; # Ignore comments
578  
579 # Dictionary
580 842 ▶ if ($str =~ m/^<</s) {
Branch: 10 branches
BranchTrueFalseTotal
if ($str =~ /^<</s) { } 146 696 842
elsif ($str =~ /^([0-9]+)(?:$PDF::API2::Basic::PDF::File::ws_char|$re_comment)+([0-9]+)(?:$PDF::API2::Basic::PDF::File::ws_char|$re_comment)+R/s) { } 152 544 696
elsif ($str =~ /^([0-9]+)(?:$PDF::API2::Basic::PDF::File::ws_char|$re_comment)+([0-9]+)(?:$PDF::API2::Basic::PDF::File::ws_char|$re_comment)+obj/s) { } 95 449 544
elsif ($str =~ m[^/($PDF::API2::Basic::PDF::File::reg_char*)]s) { } 287 162 449
elsif ($str =~ /^\(/) { } 2 160 162
elsif ($str =~ /^</) { } 0 160 160
elsif ($str =~ /^\[/) { } 74 86 160
elsif ($str =~ /^(true|false)($PDF::API2::Basic::PDF::File::irreg_char|$)/) { } 0 86 86
elsif ($str =~ /^([+-.0-9]+)($PDF::API2::Basic::PDF::File::irreg_char|$)/) { } 86 0 86
elsif ($str =~ /^null($PDF::API2::Basic::PDF::File::irreg_char|$)/) { } 0 0 0
581 146 $str = substr ($str, 2);
582 146 ▶ $str = update($fh, $str) if $update;
Branch: 1 branch
BranchTrueFalseTotal
if $update 144 2 146
583 146 $result = PDFDict();
584  
585 146 ▶ while ($str !~ m/^>>/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($str =~ /^>>/) 398 146 544
586 398 $str =~ s/^$ws_char+//; # Ignore initial white space
587 398 $str =~ s/^\%[^\015\012]*$ws_char+//; # Ignore comments
588  
589 398 ▶ if ($str =~ s|^/($reg_char+)||) {
Branch: 3 branches
BranchTrueFalseTotal
if ($str =~ s[^/($PDF::API2::Basic::PDF::File::reg_char+)][]) { } 398 0 398
elsif ($str =~ s[^/$PDF::API2::Basic::PDF::File::ws_char+][]) { } 0 0 0
elsif ($str =~ s[^//][/]) { } 0 0 0
590 398 my $key = PDF::API2::Basic::PDF::Name::name_to_string($1, $self);
591 398 ($value, $str) = $self->readval($str, %opts);
592 398 ▶ unless ((ref($value) // '') eq 'PDF::API2::Basic::PDF::Null') {
Branch: 1 branch
BranchTrueFalseTotal
unless ((ref $value // "") eq "PDF::API2::Basic::PDF::Null") 398 0 398
Condition50%1/2
ref $value // ""
l!l
3980
MC/DC0%0/1
ref $value // ""
ref $value
Truth table50%1/2
A
A= ref $value
Aresult
11
00
593 398 $result->{$key} = $value;
594 }
595 }
596 elsif ($str =~ s|^/$ws_char+||) {
597 # fixes a broken key problem of acrobat. -- fredo
598 0 ($value, $str) = $self->readval($str, %opts);
599 0 ▶ unless ((ref($value) // '') eq 'PDF::API2::Basic::PDF::Null') {
Branch: 1 branch
BranchTrueFalseTotal
unless ((ref $value // "") eq "PDF::API2::Basic::PDF::Null") 0 0 0
Condition0%0/2
ref $value // ""
l!l
00
MC/DC0%0/1
ref $value // ""
ref $value
Truth table0%0/2
A
A= ref $value
Aresult
11
00
600 0 $result->{'null'} = $value;
601 }
602 }
603 elsif ($str =~ s|^//|/|) {
604 # fixes again a broken key problem of illustrator/enfocus. -- fredo
605 0 ($value, $str) = $self->readval($str, %opts);
606 0 ▶ unless ((ref($value) // '') eq 'PDF::API2::Basic::PDF::Null') {
Branch: 1 branch
BranchTrueFalseTotal
unless ((ref $value // "") eq "PDF::API2::Basic::PDF::Null") 0 0 0
Condition0%0/2
ref $value // ""
l!l
00
MC/DC0%0/1
ref $value // ""
ref $value
Truth table0%0/2
A
A= ref $value
Aresult
11
00
607 0 $result->{'null'} = $value;
608 }
609 }
610 else {
611 0 die "Invalid dictionary key";
612 }
613 398 ▶ $str = update($fh, $str) if $update; # thanks gareth.jones@stud.man.ac.uk
Branch: 1 branch
BranchTrueFalseTotal
if $update 396 2 398
614 }
615 146 $str =~ s/^>>//;
616 146 ▶ $str = update($fh, $str) if $update;
Branch: 1 branch
BranchTrueFalseTotal
if $update 144 2 146
617 # streams can't be followed by a lone carriage-return.
618 # fredo: yes they can !!! -- use the MacOS Luke.
619 146 ▶ if (($str =~ s/^stream(?:(?:\015\012)|\012|\015)//) and ($result->{'Length'}->val != 0)) { # stream
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ s/^stream(?:(?:\015\012)|\012|\015)// and $result->{"Length"}->val != 0) 10 136 146
Condition66%2/3
$str =~ s/^stream(?:(?:\015\012)|\012|\015)// and $result->{"Length"}->val != 0
!ll&&!rl&&r
136010
MC/DC50%1/2
$str =~ s/^stream(?:(?:\015\012)|\012|\015)// and $result->{"Length"}->val != 0
$str =~ s/^stream(?:(?:\015\012)|\012|\015)// $result->{"Length"}->val != 0
Truth table66%2/3
A and B
A= $str =~ s/^stream(?:(?:\015\012)|\012|\015)//B= $result->{"Length"}->val != 0
ABresult
0X0
100
111
620 10 my $length = $result->{'Length'}->val() + 0;
621 10 $result->{' streamsrc'} = $fh;
622 10 $result->{' streamloc'} = $fh->tell - length($str);
623  
624 10 ▶ unless ($opts{'nostreams'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"nostreams"}) 10 0 10
625 10 ▶ if ($length > length($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > length $str) { } 0 10 10
626 0 $value = $str;
627 0 $length -= length($str);
628 0 read $fh, $str, $length + 11; # slurp the whole stream!
629 }
630 else {
631 10 $value = '';
632 }
633 10 $value .= substr($str, 0, $length);
634 10 $result->{' stream'} = $value;
635 10 $result->{' nofilt'} = 1;
636 10 ▶ $str = update($fh, $str, 1) if $update; # tell update we are in-stream and only need an endstream
Branch: 1 branch
BranchTrueFalseTotal
if $update 10 0 10
637 10 $str = substr($str, index($str, 'endstream') + 9);
638 }
639 }
640  
641 146 ▶ if (defined $result->{'Type'} and defined $types{$result->{'Type'}->val}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result->{"Type"} and defined $PDF::API2::Basic::PDF::File::types{$result->{"Type"}->val}) 34 112 146
Condition100%3/3
defined $result->{"Type"} and defined $PDF::API2::Basic::PDF::File::types{$result->{"Type"}->val}
!ll&&!rl&&r
872534
MC/DC100%2/2
defined $result->{"Type"} and defined $PDF::API2::Basic::PDF::File::types{$result->{"Type"}->val}
defined $result->{"Type"} defined $PDF::API2::Basic::PDF::File::types{$result->{"Type"}->val}
Truth table100%3/3
A and B
A= defined $result->{"Type"}B= defined $PDF::API2::Basic::PDF::File::types{$result->{"Type"}->val}
ABresult
0X0
100
111
642 34 bless $result, $types{$result->{'Type'}->val};
643 }
644 # gdj: FIXME: if any of the ws chars were crs, then the whole
645 # string might not have been read.
646 }
647  
648 # Indirect Object
649 elsif ($str =~ m/^([0-9]+)(?:$ws_char|$re_comment)+([0-9]+)(?:$ws_char|$re_comment)+R/s) {
650 152 my $num = $1 + 0;
651 152 $value = $2 + 0;
652 152 $str =~ s/^([0-9]+)(?:$ws_char|$re_comment)+([0-9]+)(?:$ws_char|$re_comment)+R//s;
653 152 ▶ unless ($result = $self->test_obj($num, $value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result = $self->test_obj($num, $value)) 119 33 152
654 119 $result = PDF::API2::Basic::PDF::Objind->new();
655 119 $result->{' objnum'} = $num;
656 119 $result->{' objgen'} = $value;
657 119 $self->add_obj($result, $num, $value);
658 }
659 152 $result->{' parent'} = $self;
660 152 weaken $result->{' parent'};
661  
662 # Removed to address changes being lost when an indirect object is realised twice
663 # $result->{' realised'} = 0;
664  
665 # gdj: FIXME: if any of the ws chars were crs, then the whole
666 # string might not have been read.
667 }
668  
669 # Object
670 elsif ($str =~ m/^([0-9]+)(?:$ws_char|$re_comment)+([0-9]+)(?:$ws_char|$re_comment)+obj/s) {
671 95 my $obj;
672 95 my $num = $1 + 0;
673 95 $value = $2 + 0;
674 95 $str =~ s/^([0-9]+)(?:$ws_char|$re_comment)+([0-9]+)(?:$ws_char|$re_comment)+obj//s;
675 95 my $is_xref = delete $opts{'is_xref'};
676 95 ($obj, $str) = $self->readval($str, %opts);
677 95 ▶ if ($is_xref) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_xref) { } 3 92 95
elsif ($result = $self->test_obj($num, $value)) { } 81 11 92
678 # While reading cross-reference streams, return the object without
679 # caching it. The object is syntactically required to have an
680 # object ID, but that ID is only relevant if it also shows up in a
681 # cross-reference table.
682 #
683 # Some PDF writers (notably Apple's Quartz in AppendMode) reuse the
684 # previous cross-reference stream's object ID, which results in an
685 # infinite loop if the objects are cached and the new stream object
686 # gets merged with the old stream object.
687 3 $result = $obj;
688 }
689 elsif ($result = $self->test_obj($num, $value)) {
690 81 $result->merge($obj);
691 }
692 else {
693 11 $result = $obj;
694 11 $self->add_obj($result, $num, $value);
695 11 $result->{' realised'} = 1;
696 }
697 95 ▶ $str = update($fh, $str) if $update; # thanks to kundrat@kundrat.sk
Branch: 1 branch
BranchTrueFalseTotal
if $update 91 4 95
698 95 $str =~ s/^endobj//;
699 }
700  
701 # Name
702 elsif ($str =~ m|^/($reg_char*)|s) {
703 287 $value = $1;
704 287 $str =~ s|^/($reg_char*)||s;
705 287 $result = PDF::API2::Basic::PDF::Name->from_pdf($value, $self);
706 }
707  
708 # Literal String
709 elsif ($str =~ m/^\(/) {
710 # We now need to find an unbalanced, unescaped right-paren.
711 # This can't be done with a regex.
712 2 my $value = '(';
713 2 $str = substr($str, 1);
714  
715 2 my $nested_level = 1;
716 2 while (1) {
717 # Ignore everything up to the first escaped or parenthesis character
718 2 ▶ if ($str =~ /^([^\\()]+)(.*)/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^([^\\()]+)(.*)/s) 2 0 2
719 2 $value .= $1;
720 2 $str = $2;
721 }
722  
723 # Ignore escaped parentheses
724 2 ▶ if ($str =~ /^(\\[()])/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($str =~ /^(\\[()])/) { } 0 2 2
elsif ($str =~ /^\(/) { } 0 2 2
elsif ($str =~ /^\)/) { } 2 0 2
elsif ($str =~ /^(\\[^()])/) { } 0 0 0
725 0 $value .= $1;
726 0 $str = substr($str, 2);
727 }
728  
729 # Left parenthesis: increase nesting
730 elsif ($str =~ /^\(/) {
731 0 $value .= '(';
732 0 $str = substr($str, 1);
733 0 $nested_level++;
734 }
735  
736 # Right parenthesis: decrease nesting
737 elsif ($str =~ /^\)/) {
738 2 $value .= ')';
739 2 $str = substr($str, 1);
740 2 $nested_level--;
741 2 ▶ last unless $nested_level;
Branch: 1 branch
BranchTrueFalseTotal
unless $nested_level 2 0 2
742 }
743  
744 # Other escaped character
745 elsif ($str =~ /^(\\[^()])/) {
746 0 $value .= $1;
747 0 $str = substr($str, 2);
748 }
749  
750 # If there wasn't an escaped or parenthesis character,
751 # read some more.
752 else {
753 # We don't use update because we don't want to remove
754 # whitespace or comments.
755 0 ▶ $fh->read($str, 255, length($str)) or die 'Unterminated string.';
Branch: 1 branch
BranchTrueFalseTotal
$fh->read($str, 255, length $str) or die "Unterminated string." 0 0 0
756 }
757 }
758  
759 2 $result = PDF::API2::Basic::PDF::String->from_pdf($value);
760 }
761  
762 # Hex String
763 elsif ($str =~ m/^</) {
764 0 $str =~ s/^<//;
765 0 ▶ $fh->read($str, 255, length($str)) while (0 > index($str, '>'));
Branch: 1 branch
BranchTrueFalseTotal
(index($str, ">") == -1) and do { $fh->read($str, 255, length $str) } 0 0 0
766 0 ($value, $str) = ($str =~ /^(.*?)>(.*)/s);
767 0 $result = PDF::API2::Basic::PDF::String->from_pdf('<' . $value . '>');
768 }
769  
770 # Array
771 elsif ($str =~ m/^\[/) {
772 74 $str =~ s/^\[//;
773 74 ▶ $str = update($fh, $str) if $update;
Branch: 1 branch
BranchTrueFalseTotal
if $update 74 0 74
774 74 $result = PDFArray();
775 74 ▶ while ($str !~ m/^\]/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($str =~ /^\]/) 224 74 298
776 224 $str =~ s/^$ws_char+//; # Ignore initial white space
777 224 $str =~ s/^\%[^\015\012]*$ws_char+//; # Ignore comments
778  
779 224 ($value, $str) = $self->readval($str, %opts);
780 224 $result->add_elements($value);
781 224 ▶ $str = update($fh, $str) if $update; # str might just be exhausted!
Branch: 1 branch
BranchTrueFalseTotal
if $update 224 0 224
782 }
783 74 $str =~ s/^\]//;
784 }
785  
786 # Boolean
787 elsif ($str =~ m/^(true|false)($irreg_char|$)/) {
788 0 $value = $1;
789 0 $str =~ s/^(?:true|false)//;
790 0 $result = PDF::API2::Basic::PDF::Bool->from_pdf($value);
791 }
792  
793 # Number
794 elsif ($str =~ m/^([+-.0-9]+)($irreg_char|$)/) {
795 86 $value = $1;
796 86 $str =~ s/^([+-.0-9]+)//;
797  
798 # If $str only consists of whitespace (or is empty), call update to
799 # see if this is the beginning of an indirect object or reference
800 86 ▶ if ($update and ($str =~ /^$re_whitespace*$/s or $str =~ /^$re_whitespace+[0-9]+$re_whitespace*$/s)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($update and $str =~ /^$re_whitespace*$/s || $str =~ /^$re_whitespace+[0-9]+$re_whitespace*$/s) 6 80 86
Condition100%6/6
$update and $str =~ /^$re_whitespace*$/s || $str =~ /^$re_whitespace+[0-9]+$re_whitespace*$/s
!ll&&!rl&&r
2786
$str =~ /^$re_whitespace*$/s or $str =~ /^$re_whitespace+[0-9]+$re_whitespace*$/s
l!l&&r!l&&!r
4278
MC/DC100%3/3
$update and $str =~ /^$re_whitespace*$/s || $str =~ /^$re_whitespace+[0-9]+$re_whitespace*$/s
$update $str =~ /^$re_whitespace*$/s $str =~ /^$re_whitespace+[0-9]+$re_whitespace*$/s
Truth table100%4/4
A and B or C
A= $updateB= $str =~ /^$re_whitespace*$/sC= $str =~ /^$re_whitespace+[0-9]+$re_whitespace*$/s
ABCresult
0XX0
1000
11X1
1011
801 6 $str =~ s/^$re_whitespace+/ /s;
802 6 $str =~ s/$re_whitespace+$/ /s;
803 6 $str = update($fh, $str);
804 6 ▶ if ($str =~ m/^$re_whitespace*([0-9]+)$re_whitespace+(?:R|obj)/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^$re_whitespace*([0-9]+)$re_whitespace+(?:R|obj)/s) 4 2 6
805 4 return $self->readval("$value $str", %opts);
806 }
807 }
808  
809 82 $result = PDF::API2::Basic::PDF::Number->from_pdf($value);
810 }
811  
812 # Null
813 elsif ($str =~ m/^null($irreg_char|$)/) {
814 0 $str =~ s/^null//;
815 0 $result = PDF::API2::Basic::PDF::Null->new;
816 }
817  
818 else {
819 0 die "Can't parse `$str' near " . ($fh->tell()) . " length " . length($str) . ".";
820 }
821  
822 838 $str =~ s/^$ws_char+//s;
823 838 return ($result, $str);
824 }
825  
826  
827 =head2 $ref = $p->read_obj($objind, %opts)
828  
829 Given an indirect object reference, locate it and read the object returning
830 the read in object.
831  
832 =cut
833  
834 77 ▶sub read_obj {
Subroutine read_obj: called CC=3 SCAR=11.4
Pod documented
835 77 my ($self, $objind, %opts) = @_;
836  
837 77 ▶ my $res = $self->read_objnum($objind->{' objnum'}, $objind->{' objgen'}, %opts) || return;
Condition50%1/2
$self->read_objnum($objind->{' objnum'}, $objind->{' objgen'}, %opts) || (return)
l!l
770
MC/DC0%0/1
$self->read_objnum($objind->{' objnum'}, $objind->{' objgen'}, %opts) || (return)
$self->read_objnum($objind->{' objnum'}, $objind->{' objgen'}, %opts)
Truth table50%1/2
A
A= $self->read_objnum($objind->{' objnum'}, $objind->{' objgen'}, %opts)
Aresult
11
00
838 77 ▶ $objind->merge($res) unless $objind eq $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $objind eq $res 0 77 77
839 77 return $objind;
840 }
841  
842  
843 =head2 $ref = $p->read_objnum($num, $gen, %opts)
844  
845 Returns a fully read object of given number and generation in this file
846  
847 =cut
848  
849 85 ▶sub read_objnum {
Subroutine read_objnum: called CC=14 SCAR=28.3
Pod documented
850 85 my ($self, $num, $gen, %opts) = @_;
851 85 ▶ croak 'Undefined object number in call to read_objnum($num, $gen)' unless defined $num;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $num 0 85 85
852 85 ▶ croak 'Undefined object generation in call to read_objnum($num, $gen)' unless defined $gen;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $gen 0 85 85
853 85 ▶ croak "Invalid object number '$num' in call to read_objnum" unless $num =~ /^[0-9]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $num =~ /^[0-9]+$/ 0 85 85
854 85 ▶ croak "Invalid object generation '$gen' in call to read_objnum" unless $gen =~ /^[0-9]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $gen =~ /^[0-9]+$/ 0 85 85
855  
856 85 ▶ my $object_location = $self->locate_obj($num, $gen) || return;
Condition50%1/2
$self->locate_obj($num, $gen) || (return)
l!l
850
MC/DC0%0/1
$self->locate_obj($num, $gen) || (return)
$self->locate_obj($num, $gen)
Truth table50%1/2
A
A= $self->locate_obj($num, $gen)
Aresult
11
00
857 85 my $object;
858  
859 # Compressed object
860 85 ▶ if (ref($object_location)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $object_location) 4 81 85
861 4 my ($object_stream_num, $object_stream_pos) = @{$object_location};
862  
863 4 my $object_stream = $self->read_objnum($object_stream_num, 0, %opts);
864 4 ▶ die 'Cannot find the compressed object stream' unless $object_stream;
Branch: 1 branch
BranchTrueFalseTotal
unless $object_stream 0 4 4
865  
866 4 ▶ $object_stream->read_stream() if $object_stream->{' nofilt'};
Branch: 1 branch
BranchTrueFalseTotal
if $object_stream->{" nofilt"} 4 0 4
867  
868 # An object stream starts with pairs of integers containing object numbers and
869 # stream offsets relative to the First key
870 4 my $fh;
871 my $pairs;
872 4 ▶ unless ($object_stream->{' streamfile'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object_stream->{' streamfile'}) { } 0 4 4
873 4 $pairs = substr($object_stream->{' stream'}, 0, $object_stream->{'First'}->val);
874 }
875 else {
876 0 CORE::open($fh, '<', $object_stream->{' streamfile'});
877 0 read($fh, $pairs, $object_stream->{'First'}->val());
878 }
879 4 my @map = split /\s+/, $pairs;
880  
881 # Find the offset of the object in the stream
882 4 my $index = $object_stream_pos * 2;
883 4 ▶ die "Objind $num does not exist at index $index" unless $map[$index] == $num;
Branch: 1 branch
BranchTrueFalseTotal
unless $map[$index] == $num 0 4 4
884 4 my $start = $map[$index + 1];
885  
886 # Unless this is the last object in the stream, its length is determined by the
887 # offset of the next object
888 4 my $last_object_in_stream = $map[-2];
889 4 my $length;
890 4 ▶ if ($last_object_in_stream == $num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_object_in_stream == $num) { } 2 2 4
891 2 ▶ if ($object_stream->{' stream'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object_stream->{' stream'}) { } 2 0 2
892 2 $length = length($object_stream->{' stream'}) - $object_stream->{'First'}->val() - $start;
893 }
894 else {
895 0 $length = (-s $object_stream->{' streamfile'}) - $object_stream->{'First'}->val() - $start;
896 }
897 }
898 else {
899 2 my $next_start = $map[$index + 3];
900 2 $length = $next_start - $start;
901 }
902  
903 # Read the object from the stream
904 4 my $stream = "$num 0 obj ";
905 4 ▶ unless ($object_stream->{' streamfile'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object_stream->{' streamfile'}) { } 0 4 4
906 4 $stream .= substr($object_stream->{' stream'}, $object_stream->{'First'}->val() + $start, $length);
907 }
908 else {
909 0 seek($fh, $object_stream->{'First'}->val() + $start, 0);
910 0 read($fh, $stream, $length, length($stream));
911 0 close $fh;
912 }
913  
914 4 ($object) = $self->readval($stream, %opts, update => 0);
915 4 return $object;
916 }
917  
918 81 my $current_location = $self->{' INFILE'}->tell;
919 81 $self->{' INFILE'}->seek($object_location, 0);
920 81 ($object) = $self->readval('', %opts);
921 81 $self->{' INFILE'}->seek($current_location, 0);
922 81 return $object;
923 }
924  
925  
926 =head2 $objind = $p->new_obj($obj)
927  
928 Creates a new, free object reference based on free space in the cross reference chain.
929 If nothing free then thinks up a new number. If $obj then turns that object into this
930 new object rather than returning a new object.
931  
932 =cut
933  
934 1072 ▶sub new_obj {
Subroutine new_obj: called CC=12 SCAR=38.3
Pod documented
935 1072 my ($self, $base) = @_;
936 1072 my $res;
937  
938 1072 ▶ if (defined $self->{' free'} and scalar @{$self->{' free'}} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{" free"} and scalar @{$self->{" free"};} > 0) 0 1072 1072
Condition66%2/3
defined $self->{" free"} and scalar @{$self->{" free"};} > 0
!ll&&!rl&&r
1057150
MC/DC0%0/2
defined $self->{" free"} and scalar @{$self->{" free"};} > 0
defined $self->{" free"} scalar @{$self->{" free"};} > 0
Truth table66%2/3
A and B
A= defined $self->{" free"}B= scalar @{$self->{" free"};} > 0
ABresult
0X0
100
111
939 0 $res = shift(@{$self->{' free'}});
940 0 ▶ if (defined $base) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $base) { } 0 0 0
941 0 my ($num, $gen) = @{$self->{' objects'}{$res->uid}};
942 0 $self->remove_obj($res);
943 0 $self->add_obj($base, $num, $gen);
944 0 return $self->out_obj($base);
945 }
946 else {
947 0 $self->{' objects'}{$res->uid}[2] = 0;
948 0 return $res;
949 }
950 }
951  
952 1072 my $tdict = $self;
953 1072 my $i;
954 1072 ▶ while (defined $tdict) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tdict) 1073 1072 2145
955 1073 ▶ $i = $tdict->{' xref'}{defined($i) ? $i : ''}[0];
Branch: 1 branch
BranchTrueFalseTotal
defined $i ? : 0 1073 1073
956 1073 ▶ while (defined $i and $i != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $i and $i != 0) 0 1073 1073
Condition33%1/3
defined $i and $i != 0
!ll&&!rl&&r
107300
MC/DC0%0/2
defined $i and $i != 0
defined $i $i != 0
Truth table33%1/3
A and B
A= defined $iB= $i != 0
ABresult
0X0
100
111
957 0 my ($ni, $ng) = @{$tdict->{' xref'}{$i}};
958 0 ▶ unless (defined $self->locate_obj($i, $ng)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->locate_obj($i, $ng)) 0 0 0
959 0 ▶ if (defined $base) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $base) { } 0 0 0
960 0 $self->add_obj($base, $i, $ng);
961 0 return $base;
962 }
963 else {
964 0 ▶ $res = $self->test_obj($i, $ng) || $self->add_obj(PDF::API2::Basic::PDF::Objind->new(), $i, $ng);
Condition0%0/3
$self->test_obj($i, $ng) || $self->add_obj('PDF::API2::Basic::PDF::Objind'->new, $i, $ng)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->test_obj($i, $ng) || $self->add_obj('PDF::API2::Basic::PDF::Objind'->new, $i, $ng)
$self->test_obj($i, $ng) $self->add_obj('PDF::API2::Basic::PDF::Objind'->new, $i, $ng)
Truth table0%0/3
A || B
A= $self->test_obj($i, $ng)B= $self->add_obj('PDF::API2::Basic::PDF::Objind'->new, $i, $ng)
ABresult
1X1
011
000
965 0 $self->out_obj($res);
966 0 return $res;
967 }
968 }
969 0 $i = $ni;
970 }
971 1073 $tdict = $tdict->{' prev'};
972 }
973  
974 1072 $i = $self->{' maxobj'}++;
975 1072 ▶ if (defined $base) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $base) { } 1072 0 1072
976 1072 $self->add_obj($base, $i, 0);
977 1072 $self->out_obj($base);
978 1072 return $base;
979 }
980 else {
981 0 $res = $self->add_obj(PDF::API2::Basic::PDF::Objind->new(), $i, 0);
982 0 $self->out_obj($res);
983 0 return $res;
984 }
985 }
986  
987  
988 =head2 $p->out_obj($objind)
989  
990 Indicates that the given object reference should appear in the output xref
991 table whether with data or freed.
992  
993 =cut
994  
995 2676 ▶sub out_obj {
Subroutine out_obj: called CC=2 SCAR=6.9
Pod documented
996 2676 my ($self, $obj) = @_;
997  
998 # This is why we've been keeping the outlist CACHE around; to speed
999 # up this method by orders of magnitude (it saves up from having to
1000 # grep the full outlist each time through as we'll just do a lookup
1001 # in the hash) (which is super-fast).
1002 2676 ▶ unless (exists $self->{' outlist_cache'}{$obj}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{" outlist_cache"}{$obj}) 1087 1589 2676
1003 1087 push @{$self->{' outlist'}}, $obj;
1004 # weaken $self->{' outlist'}->[-1];
1005 1087 $self->{' outlist_cache'}{$obj} = 1;
1006 }
1007 2676 return $obj;
1008 }
1009  
1010  
1011 =head2 $p->free_obj($objind)
1012  
1013 Marks an object reference for output as being freed.
1014  
1015 =cut
1016  
1017 0 ▶sub free_obj {
Subroutine free_obj: not called CC=1 SCAR=6.9
Pod documented
1018 0 my ($self, $obj) = @_;
1019  
1020 0 push @{$self->{' free'}}, $obj;
1021 0 $self->{' objects'}{$obj->uid()}[2] = 1;
1022 0 $self->out_obj($obj);
1023 }
1024  
1025  
1026 =head2 $p->remove_obj($objind)
1027  
1028 Removes the object from all places where we might remember it
1029  
1030 =cut
1031  
1032 0 ▶sub remove_obj {
Subroutine remove_obj: not called CC=2 SCAR=17.9
Pod documented
1033 0 my ($self, $objind) = @_;
1034  
1035 # who says it has to be fast
1036 0 delete $self->{' objects'}{$objind->uid()};
1037 0 delete $self->{' outlist_cache'}{$objind};
1038 0 delete $self->{' printed_cache'}{$objind};
1039 0 @{$self->{' outlist'}} = grep { $_ ne $objind } @{$self->{' outlist'}};
1040 0 @{$self->{' printed'}} = grep { $_ ne $objind } @{$self->{' printed'}};
1041 $self->{' objcache'}{$objind->{' objnum'}, $objind->{' objgen'}} = undef
1042 0 ▶ if $self->{' objcache'}{$objind->{' objnum'}, $objind->{' objgen'}} eq $objind;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{" objcache"}{join $;, $objind->{" objnum"}, $objind->{" objgen"}} eq $objind 0 0 0
1043 0 return $self;
1044 }
1045  
1046  
1047 =head2 $p->ship_out(@objects)
1048  
1049 Ships the given objects (or all objects for output if @objects is empty) to
1050 the currently open output file (assuming there is one). Freed objects are not
1051 shipped, and once an object is shipped it is switched such that this file
1052 becomes its source and it will not be shipped again unless out_obj is called
1053 again. Notice that a shipped out object can be re-output or even freed, but
1054 that it will not cause the data already output to be changed.
1055  
1056 =cut
1057  
1058 154 ▶sub ship_out {
Subroutine ship_out: called CC=11 SCAR=25.0
Pod documented
1059 154 my ($self, @objs) = @_;
1060  
1061 154 ▶ die "No output file specified" unless defined $self->{' OUTFILE'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{" OUTFILE"} 0 154 154
1062 154 my $fh = $self->{' OUTFILE'};
1063 154 seek($fh, 0, 2); # go to the end of the file
1064  
1065 154 ▶ @objs = @{$self->{' outlist'}} unless scalar @objs > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @objs > 0 154 0 154
1066 154 foreach my $objind (@objs) {
1067 890 ▶ next unless $objind->is_obj($self);
Branch: 1 branch
BranchTrueFalseTotal
unless $objind->is_obj($self) 0 890 890
1068 890 my $j = -1;
1069 890 ▶ for (my $i = 0; $i < scalar @{$self->{' outlist'}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$self->{" outlist"};}) 890 0 890
1070 890 ▶ if ($self->{' outlist'}[$i] eq $objind) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{" outlist"}[$i] eq $objind) 890 0 890
1071 890 $j = $i;
1072 890 last;
1073 }
1074 }
1075 890 ▶ next if $j < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $j < 0 0 890 890
1076 890 splice(@{$self->{' outlist'}}, $j, 1);
1077 890 delete $self->{' outlist_cache'}{$objind};
1078 0 ▶ next if grep { $_ eq $objind } @{$self->{' free'}};
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $objind;} @{$$self{" free"};} 0 890 890
1079  
1080 0 ▶ map { $fh->print("\% $_ \n") } split(/$cr/, $objind->{' comments'}) if $objind->{' comments'};
Branch: 1 branch
BranchTrueFalseTotal
if $objind->{" comments"} 0 890 890
1081 890 $self->{' locs'}{$objind->uid()} = $fh->tell();
1082 890 my ($objnum, $objgen) = @{$self->{' objects'}{$objind->uid()}}[0..1];
1083 890 $fh->printf('%d %d obj ', $objnum, $objgen);
1084 890 $objind->outobjdeep($fh, $self);
1085 890 $fh->print(" endobj\n");
1086  
1087 # Note that we've output this obj, not forgetting to update
1088 # the cache of whats printed.
1089 890 ▶ unless (exists $self->{' printed_cache'}{$objind}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{" printed_cache"}{$objind}) 890 0 890
1090 890 push @{$self->{' printed'}}, $objind;
1091 890 $self->{' printed_cache'}{$objind}++;
1092 }
1093 }
1094 154 return $self;
1095 }
1096  
1097 =head2 $p->copy($outpdf, \&filter)
1098  
1099 Iterates over every object in the file reading the object, calling filter with the object
1100 and outputting the result. if filter is not defined, then just copies input to output.
1101  
1102 =cut
1103  
1104 0 ▶sub copy {
Subroutine copy: not called CC=14 SCAR=53.5
Pod documented
1105 0 my ($self, $out, $filter) = @_;
1106 0 my ($obj, $minl, $mini, $ming);
1107  
1108 0 foreach my $key (grep { not m/^[\s\-]/ } keys %$self) {
1109 0 ▶ $out->{$key} = $self->{$key} unless defined $out->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $out->{$key} 0 0 0
1110 }
1111  
1112 0 my $tdict = $self;
1113 0 ▶ while (defined $tdict) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tdict) 0 0 0
1114 0 foreach my $i (sort {$a <=> $b} keys %{$tdict->{' xref'}}) {
1115 0 my ($nl, $ng, $nt) = @{$tdict->{' xref'}{$i}};
1116 0 ▶ next unless $nt eq 'n';
Branch: 1 branch
BranchTrueFalseTotal
unless $nt eq "n" 0 0 0
1117  
1118 0 ▶ if ($nl < $minl or $mini == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nl < $minl or $mini == 0) 0 0 0
Condition0%0/3
$nl < $minl or $mini == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$nl < $minl or $mini == 0
$nl < $minl $mini == 0
Truth table0%0/3
A or B
A= $nl < $minlB= $mini == 0
ABresult
1X1
011
000
1119 0 $mini = $i;
1120 0 $ming = $ng;
1121 0 $minl = $nl;
1122 }
1123 0 ▶ unless ($obj = $self->test_obj($i, $ng)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj = $self->test_obj($i, $ng)) 0 0 0
1124 0 $obj = PDF::API2::Basic::PDF::Objind->new();
1125 0 $obj->{' objnum'} = $i;
1126 0 $obj->{' objgen'} = $ng;
1127 0 $self->add_obj($obj, $i, $ng);
1128 0 $obj->{' parent'} = $self;
1129 0 weaken $obj->{' parent'};
1130 0 $obj->{' realised'} = 0;
1131 }
1132 0 $obj->realise;
1133 0 ▶ my $res = defined $filter ? &{$filter}($obj) : $obj;
Branch: 1 branch
BranchTrueFalseTotal
defined $filter ? : 0 0 0
1134 0 ▶ $out->new_obj($res) unless (!$res || $res->is_obj($out));
Branch: 1 branch
BranchTrueFalseTotal
unless not $res or $res->is_obj($out) 0 0 0
Condition0%0/3
not $res or $res->is_obj($out)
l!l&&r!l&&!r
000
MC/DC0%0/2
not $res or $res->is_obj($out)
not $res $res->is_obj($out)
Truth table0%0/3
not(A) or B
A= not $resB= $res->is_obj($out)
ABresult
1X1
011
000
1135 }
1136 0 $tdict = $tdict->{' prev'};
1137 }
1138  
1139 # test for linearized and remove it from output
1140 0 $obj = $self->test_obj($mini, $ming);
1141 0 ▶ if ($obj->isa('PDF::API2::Basic::PDF::Dict') && $obj->{'Linearized'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->isa("PDF::API2::Basic::PDF::Dict") and $obj->{"Linearized"}) 0 0 0
Condition0%0/3
$obj->isa("PDF::API2::Basic::PDF::Dict") and $obj->{"Linearized"}
!ll&&!rl&&r
000
MC/DC0%0/2
$obj->isa("PDF::API2::Basic::PDF::Dict") and $obj->{"Linearized"}
$obj->isa("PDF::API2::Basic::PDF::Dict") $obj->{"Linearized"}
Truth table0%0/3
A and B
A= $obj->isa("PDF::API2::Basic::PDF::Dict")B= $obj->{"Linearized"}
ABresult
0X0
100
111
1142 0 $out->free_obj($obj);
1143 }
1144  
1145 0 return $self;
1146 }
1147  
1148  
1149 =head1 PRIVATE METHODS & FUNCTIONS
1150  
1151 The following methods and functions are considered private to this class. This
1152 does not mean you cannot use them if you have a need, just that they aren't really
1153 designed for users of this class.
1154  
1155 =head2 $offset = $p->locate_obj($num, $gen)
1156  
1157 Returns a file offset to the object asked for by following the chain of cross
1158 reference tables until it finds the one you want.
1159  
1160 =cut
1161  
1162 85 ▶sub locate_obj {
Subroutine locate_obj: called CC=7 SCAR=20.3
Pod documented
1163 85 my ($self, $num, $gen) = @_;
1164  
1165 85 my $tdict = $self;
1166 85 my $seen = {};
1167 85 ▶ while (defined $tdict) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tdict) 94 0 94
1168 94 $seen->{$tdict->{' loc'}} = 1;
1169 94 ▶ if (ref $tdict->{' xref'}{$num}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tdict->{" xref"}{$num}) 85 9 94
1170 85 my $ref = $tdict->{' xref'}{$num};
1171 85 ▶ return $ref unless scalar(@$ref) == 3;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$ref == 3 4 81 85
1172  
1173 81 ▶ if ($ref->[1] == $gen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->[1] == $gen) 81 0 81
1174 81 ▶ return $ref->[0] if $ref->[2] eq 'n';
Branch: 1 branch
BranchTrueFalseTotal
if $ref->[2] eq "n" 81 0 81
1175 0 return; # if $ref->[2] eq 'f';
1176 }
1177 }
1178 9 ▶ last unless defined $tdict->{' prev'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tdict->{" prev"} 0 9 9
1179 9 $tdict = $tdict->{' prev'};
1180 }
1181 0 return;
1182 }
1183  
1184  
1185 =head2 update($fh, $str, $instream)
1186  
1187 Keeps reading $fh for more data to ensure that $str has at least a line full
1188 for C<readval> to work on. At this point we also take the opportunity to ignore
1189 comments.
1190  
1191 =cut
1192  
1193 1940 ▶sub update {
Subroutine update: called CC=13 SCAR=28.7
Pod documented
1194 1940 my ($fh, $str, $instream) = @_;
1195 1940 ▶ print STDERR 'fpos=' . tell($fh) . ' strlen=' . length($str) . "\n" if $readDebug;
Branch: 1 branch
BranchTrueFalseTotal
if $readDebug 0 1940 1940
1196 1940 ▶ if ($instream) {
Branch: 1 branch
BranchTrueFalseTotal
if ($instream) { } 10 1930 1940
1197 # we are inside a (possible binary) stream
1198 # so we fetch data till we see an 'endstream'
1199 # -- fredo/2004-09-03
1200 10 ▶ while ($str !~ m/endstream/ and not $fh->eof()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($str =~ /endstream/ or $fh->eof) 0 10 10
Condition33%1/3
$str =~ /endstream/ or $fh->eof
l!l&&r!l&&!r
1000
MC/DC0%0/2
$str =~ /endstream/ or $fh->eof
$str =~ /endstream/ $fh->eof
Truth table33%1/3
A or B
A= $str =~ /endstream/B= $fh->eof
ABresult
1X1
011
000
1201 0 ▶ print STDERR 'fpos=' . tell($fh) . ' strlen=' . length($str) . "\n" if $readDebug;
Branch: 1 branch
BranchTrueFalseTotal
if $readDebug 0 0 0
1202 0 $fh->read($str, 314, length($str));
1203 }
1204 }
1205 else {
1206 1930 $str =~ s/^$ws_char*//;
1207 1930 ▶ while ($str !~ m/$cr/ and not $fh->eof()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($str =~ /$PDF::API2::Basic::PDF::File::cr/ or $fh->eof) 107 1930 2037
Condition100%3/3
$str =~ /$PDF::API2::Basic::PDF::File::cr/ or $fh->eof
l!l&&r!l&&!r
188842107
MC/DC100%2/2
$str =~ /$PDF::API2::Basic::PDF::File::cr/ or $fh->eof
$str =~ /$PDF::API2::Basic::PDF::File::cr/ $fh->eof
Truth table100%3/3
A or B
A= $str =~ /$PDF::API2::Basic::PDF::File::cr/B= $fh->eof
ABresult
1X1
011
000
1208 107 ▶ print STDERR 'fpos=' . tell($fh) . ' strlen=' . length($str) . "\n" if $readDebug;
Branch: 1 branch
BranchTrueFalseTotal
if $readDebug 0 107 107
1209 107 $fh->read($str, 314, length($str));
1210 107 $str =~ s/^$ws_char*//so;
1211 }
1212 1930 ▶ while ($str =~ m/^\%/) { # restructured by fredo/2003-03-23
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^\%/) 1 1930 1931
1213 1 ▶ print STDERR 'fpos=' . tell($fh) . ' strlen=' . length($str) . "\n" if $readDebug;
Branch: 1 branch
BranchTrueFalseTotal
if $readDebug 0 1 1
1214 1 ▶ $fh->read($str, 314, length($str)) while ($str !~ m/$cr/ and not $fh->eof());
Branch: 1 branch
BranchTrueFalseTotal
$str =~ /$PDF::API2::Basic::PDF::File::cr/ or $fh->eof or do { $fh->read($str, 314, length $str) } 1 0 1
Condition33%1/3
$str =~ /$PDF::API2::Basic::PDF::File::cr/ or $fh->eof
l!l&&r!l&&!r
100
MC/DC0%0/2
$str =~ /$PDF::API2::Basic::PDF::File::cr/ or $fh->eof
$str =~ /$PDF::API2::Basic::PDF::File::cr/ $fh->eof
Truth table33%1/3
A or B
A= $str =~ /$PDF::API2::Basic::PDF::File::cr/B= $fh->eof
ABresult
1X1
011
000
1215 1 $str =~ s/^\%[^\015\012]*$ws_char*//so; # fixed for reportlab -- fredo
1216 }
1217 }
1218  
1219 1940 return $str;
1220 }
1221  
1222 =head2 $objind = $p->test_obj($num, $gen)
1223  
1224 Tests the cache to see whether an object reference (which may or may not have
1225 been getobj()ed) has been cached. Returns it if it has.
1226  
1227 =cut
1228  
1229 244 ▶sub test_obj {
Subroutine test_obj: called CC=1 SCAR=0.0
Pod documented
1230 244 my ($self, $num, $gen) = @_;
1231 244 return $self->{' objcache'}{$num, $gen};
1232 }
1233  
1234  
1235 =head2 $p->add_obj($objind)
1236  
1237 Adds the given object to the internal object cache.
1238  
1239 =cut
1240  
1241 1202 ▶sub add_obj {
Subroutine add_obj: called CC=1 SCAR=0.0
Pod documented
1242 1202 my ($self, $obj, $num, $gen) = @_;
1243  
1244 1202 $self->{' objcache'}{$num, $gen} = $obj;
1245 1202 $self->{' objects'}{$obj->uid()} = [$num, $gen];
1246 # weaken $self->{' objcache'}{$num, $gen};
1247 1202 return $obj;
1248 }
1249  
1250  
1251 =head2 $tdict = $p->readxrtr($xpos)
1252  
1253 Recursive function which reads each of the cross-reference and trailer tables
1254 in turn until there are no more.
1255  
1256 Returns a dictionary corresponding to the trailer chain. Each trailer also
1257 includes the corresponding cross-reference table.
1258  
1259 The structure of the xref private element in a trailer dictionary is of an
1260 anonymous hash of cross reference elements by object number. Each element
1261 consists of an array of 3 elements corresponding to the three elements read
1262 in [location, generation number, free or used]. See the PDF specification
1263 for details.
1264  
1265 =cut
1266  
1267 78 ▶sub _unpack_xref_stream {
Subroutine _unpack_xref_stream: called CC=6 SCAR=27.6
1268 78 my ($self, $width, $data) = @_;
1269  
1270 78 ▶ return unpack('C', $data) if $width == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $width == 1 26 52 78
1271 52 ▶ return unpack('n', $data) if $width == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $width == 2 52 0 52
1272 0 ▶ return unpack('N', "\x00$data") if $width == 3;
Branch: 1 branch
BranchTrueFalseTotal
if $width == 3 0 0 0
1273 0 ▶ return unpack('N', $data) if $width == 4;
Branch: 1 branch
BranchTrueFalseTotal
if $width == 4 0 0 0
1274 0 ▶ return unpack('Q>', $data) if $width == 8;
Branch: 1 branch
BranchTrueFalseTotal
if $width == 8 0 0 0
1275  
1276 0 die "Unsupported xref stream entry width: $width";
1277 }
1278  
1279 19 ▶sub readxrtr {
Subroutine readxrtr: called CC=29 SCAR=35.1
Pod documented
1280 19 my ($self, $xpos, $loop_check) = @_;
1281 19 my ($tdict, $buf, $xmin, $xnum, $xdiff);
1282  
1283 19 my $fh = $self->{' INFILE'};
1284 19 $fh->seek($xpos, 0);
1285 19 $fh->read($buf, 22);
1286 19 $buf = update($fh, $buf); # fix for broken JAWS xref calculation.
1287  
1288 19 my $xlist = {};
1289  
1290 ## seams that some products calculate wrong prev entries (short)
1291 ## so we seek ahead to find one -- fredo; save for now
1292 #while($buf !~ m/^xref$cr/i && !eof($fh))
1293 #{
1294 # $buf =~ s/^(\s+|\S+|.)//i;
1295 # $buf=update($fh,$buf);
1296 #}
1297  
1298 19 ▶ if ($buf =~ s/^xref$cr//i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($buf =~ s/^xref$PDF::API2::Basic::PDF::File::cr//i) { } 16 3 19
elsif ($buf =~ /^(\d+)\s+(\d+)\s+obj/i) { } 3 0 3
1299 # Plain XRef tables.
1300 16 ▶ while ($buf =~ m/^$ws_char*([0-9]+)$ws_char+([0-9]+)$ws_char*$cr(.*?)$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf =~ /^$PDF::API2::Basic::PDF::File::ws_char*([0-9]+)$PDF::API2::Basic::PDF::File::ws_char+([0-9]+)$PDF::API2::Basic::PDF::File::ws_char*$PDF::API2::Basic::PDF::File::cr(.*?)$/s) 21 16 37
1301 21 my $old_buf = $buf;
1302 21 $xmin = $1;
1303 21 $xnum = $2;
1304 21 $buf = $3;
1305 21 ▶ unless ($old_buf =~ /^[0-9]+ [0-9]+$cr/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($old_buf =~ /^[0-9]+ [0-9]+$PDF::API2::Basic::PDF::File::cr/) 0 21 21
1306 # See PDF 1.7 section 7.5.4: Cross-Reference Table
1307 0 warn q{Malformed xref in PDF file: subsection shall begin with a line containing two numbers separated by a SPACE (20h)};
1308 }
1309 21 $xdiff = length($buf);
1310  
1311 21 $fh->read($buf, 20 * $xnum - $xdiff + 15, $xdiff);
1312 21 ▶ while ($xnum-- > 0 and $buf =~ s/^0*([0-9]*)$ws_char+0*([0-9]+)$ws_char+([nf])$cr//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xnum-- > 0 and $buf =~ s/^0*([0-9]*)$PDF::API2::Basic::PDF::File::ws_char+0*([0-9]+)$PDF::API2::Basic::PDF::File::ws_char+([nf])$PDF::API2::Basic::PDF::File::cr//) 118 21 139
Condition66%2/3
$xnum-- > 0 and $buf =~ s/^0*([0-9]*)$PDF::API2::Basic::PDF::File::ws_char+0*([0-9]+)$PDF::API2::Basic::PDF::File::ws_char+([nf])$PDF::API2::Basic::PDF::File::cr//
!ll&&!rl&&r
210118
MC/DC50%1/2
$xnum-- > 0 and $buf =~ s/^0*([0-9]*)$PDF::API2::Basic::PDF::File::ws_char+0*([0-9]+)$PDF::API2::Basic::PDF::File::ws_char+([nf])$PDF::API2::Basic::PDF::File::cr//
$xnum-- > 0 $buf =~ s/^0*([0-9]*)$PDF::API2::Basic::PDF::File::ws_char+0*([0-9]+)$PDF::API2::Basic::PDF::File::ws_char+([nf])$PDF::API2::Basic::PDF::File::cr//
Truth table66%2/3
A and B
A= $xnum-- > 0B= $buf =~ s/^0*([0-9]*)$PDF::API2::Basic::PDF::File::ws_char+0*([0-9]+)$PDF::API2::Basic::PDF::File::ws_char+([nf])$PDF::API2::Basic::PDF::File::cr//
ABresult
0X0
100
111
1313 118 ▶ $xlist->{$xmin} = [$1, $2, $3] unless exists $xlist->{$xmin};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $xlist->{$xmin} 118 0 118
1314 118 $xmin++;
1315 }
1316 }
1317  
1318 16 ▶ if ($buf !~ /^\s*trailer\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($buf =~ /^\s*trailer\b/i) 0 16 16
1319 0 die "Malformed trailer in PDF file $self->{' fname'} at " . ($fh->tell - length($buf));
1320 }
1321  
1322 16 $buf =~ s/^\s*trailer\b//i;
1323  
1324 16 ($tdict, $buf) = $self->readval($buf);
1325 }
1326 elsif ($buf =~ m/^(\d+)\s+(\d+)\s+obj/i) {
1327 3 my ($xref_obj, $xref_gen) = ($1, $2);
1328  
1329 # XRef streams.
1330 3 ($tdict, $buf) = $self->readval($buf, is_xref => 1);
1331  
1332 3 ▶ unless ($tdict->{' stream'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tdict->{" stream"}) 0 3 3
1333 0 die "Malformed XRefStm at $xref_obj $xref_gen obj in PDF file $self->{' fname'}";
1334 }
1335 3 $tdict->read_stream(1);
1336  
1337 3 my $stream = $tdict->{' stream'};
1338 3 my @widths = map { $_->val } @{$tdict->{W}->val};
1339  
1340 3 my $start = 0;
1341 3 my $last;
1342  
1343 my @index;
1344 3 ▶ if (defined $tdict->{Index}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tdict->{'Index'}) { } 1 2 3
1345 1 @index = map { $_->val() } @{$tdict->{Index}->val};
1346 }
1347 else {
1348 2 @index = (0, $tdict->{Size}->val);
1349 }
1350  
1351 3 ▶ while (scalar @index) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @index) 3 3 6
1352 3 $start = shift(@index);
1353 3 $last = $start + shift(@index) - 1;
1354  
1355 3 for my $i ($start...$last) {
1356 # Replaced "for $xmin" because it creates a loop-specific local variable, and we
1357 # need $xmin to be correct for maxobj below.
1358 26 $xmin = $i;
1359  
1360 26 my @cols;
1361  
1362 26 for my $w (@widths) {
1363 78 my $data;
1364 78 ▶ $data = $self->_unpack_xref_stream($w, substr($stream, 0, $w, '')) if $w;
Branch: 1 branch
BranchTrueFalseTotal
if $w 78 0 78
1365  
1366 78 push @cols, $data;
1367 }
1368  
1369 26 ▶ $cols[0] = 1 unless defined $cols[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cols[0] 1 25 26
1370 26 ▶ if ($cols[0] > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cols[0] > 2) 0 26 26
1371 0 die "Invalid XRefStm entry type ($cols[0]) at $xref_obj $xref_gen obj";
1372 }
1373  
1374 26 ▶ next if exists $xlist->{$xmin};
Branch: 1 branch
BranchTrueFalseTotal
if exists $xlist->{$xmin} 0 26 26
1375  
1376 26 ▶ my @objind = ($cols[1], defined($cols[2]) ? $cols[2] : ($xmin ? 0 : 65535));
Branch: 2 branches
BranchTrueFalseTotal
defined $cols[2] ? : 25 1 26
$xmin ? : 1 0 1
1377 26 ▶ push @objind, ($cols[0] == 0 ? 'f' : 'n') if $cols[0] < 2;
Branch: 2 branches
BranchTrueFalseTotal
if $cols[0] < 2 22 4 26
$cols[0] == 0 ? : 2 20 22
1378  
1379 26 $xlist->{$xmin} = \@objind;
1380 }
1381 }
1382 }
1383 else {
1384 0 die "Malformed xref in PDF file $self->{' fname'}";
1385 }
1386  
1387 19 $tdict->{' loc'} = $xpos;
1388 19 $tdict->{' xref'} = $xlist;
1389 19 ▶ $self->{' maxobj'} = $xmin + 1 if $xmin + 1 > $self->{' maxobj'};
Branch: 1 branch
BranchTrueFalseTotal
if $xmin + 1 > $self->{" maxobj"} 16 3 19
1390 19 ▶ unless ($loop_check) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loop_check) { } 19 0 19
1391 0 $self->{' xref_seen'} = {};
1392 }
1393 else {
1394 19 ▶ $self->{' xref_seen'} //= {};
Condition100%2/2
$self->{" xref_seen"} //= {}
l!l
316
MC/DC100%1/1
$self->{" xref_seen"} //= {}
$self->{" xref_seen"}
Truth table100%2/2
A
A= $self->{" xref_seen"}
Aresult
11
00
1395 }
1396 19 $self->{' xref_seen'}->{$xpos} = 1;
1397 19 ▶ if (defined $tdict->{'Prev'} and $tdict->{'Prev'}->val() != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tdict->{"Prev"} and $tdict->{"Prev"}->val != 0) 3 16 19
Condition66%2/3
defined $tdict->{"Prev"} and $tdict->{"Prev"}->val != 0
!ll&&!rl&&r
1603
MC/DC50%1/2
defined $tdict->{"Prev"} and $tdict->{"Prev"}->val != 0
defined $tdict->{"Prev"} $tdict->{"Prev"}->val != 0
Truth table66%2/3
A and B
A= defined $tdict->{"Prev"}B= $tdict->{"Prev"}->val != 0
ABresult
0X0
100
111
1398 3 my $prev = $tdict->{'Prev'}->val();
1399 3 ▶ if ($loop_check and $self->{' xref_seen'}->{$prev}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loop_check and $self->{" xref_seen"}{$prev}) 0 3 3
Condition33%1/3
$loop_check and $self->{" xref_seen"}{$prev}
!ll&&!rl&&r
030
MC/DC0%0/2
$loop_check and $self->{" xref_seen"}{$prev}
$loop_check $self->{" xref_seen"}{$prev}
Truth table33%1/3
A and B
A= $loop_checkB= $self->{" xref_seen"}{$prev}
ABresult
0X0
100
111
1400 0 die "Malformed PDF: trailer contains a circular reference";
1401 }
1402 3 $tdict->{' prev'} = $self->readxrtr($prev, 1);
1403 }
1404 19 return $tdict;
1405 }
1406  
1407  
1408 =head2 $p->out_trailer($tdict)
1409  
1410 Outputs the body and trailer for a PDF file by outputting all the objects in
1411 the ' outlist' and then outputting a xref table for those objects and any
1412 freed ones. It then outputs the trailing dictionary and the trailer code.
1413  
1414 =cut
1415  
1416 148 ▶sub out_trailer {
Subroutine out_trailer: called CC=31 SCAR=47.7
Pod documented
1417 148 my ($self, $tdict, $update) = @_;
1418 148 my $fh = $self->{' OUTFILE'};
1419  
1420 148 ▶ while (@{$self->{' outlist'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{" outlist"};}) 154 148 302
1421 154 $self->ship_out();
1422 }
1423  
1424 # When writing new trailers, most dictionary entries get copied from the
1425 # previous trailer, but entries related to cross-reference streams should
1426 # get removed (and possibly recreated below).
1427 148 delete $tdict->{$_} for (# Entries common to streams
1428 qw(Length Filter DecodeParms F FFilter FDecodeParms DL),
1429  
1430 # Entries specific to cross-reference streams
1431 qw(Index W XRefStm));
1432  
1433 148 $tdict->{'Size'} = PDFNum($self->{' maxobj'});
1434  
1435 148 my $tloc = $fh->tell();
1436 148 my @out;
1437  
1438 148 ▶ my @xreflist = sort { $self->{' objects'}{$a->uid}[0] <=> $self->{' objects'}{$b->uid}[0] } (@{$self->{' printed'} || []}, @{$self->{' free'} || []});
Condition100%4/4
$self->{' printed'} || []
l!l
1453
$self->{' free'} || []
l!l
1453
MC/DC100%1/1
$self->{' printed'} || []
$self->{' printed'}
MC/DC100%1/1
$self->{' free'} || []
$self->{' free'}
Truth table100%2/2
A
A= $self->{' printed'}
Aresult
11
00
Truth table100%2/2
A
A= $self->{' free'}
Aresult
11
00
1439  
1440 148 my ($i, $j, $k);
1441 148 ▶ unless ($update) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($update) 140 8 148
1442 140 $i = 1;
1443 140 ▶ for ($j = 0; $j < @xreflist; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @xreflist) 868 140 1008
1444 868 my @inserts;
1445 868 $k = $xreflist[$j];
1446 868 ▶ while ($i < $self->{' objects'}{$k->uid}[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{" objects"}{$k->uid}[0]) 0 868 868
1447 0 my ($n) = PDF::API2::Basic::PDF::Objind->new();
1448 0 $self->add_obj($n, $i, 0);
1449 0 $self->free_obj($n);
1450 0 push(@inserts, $n);
1451 0 $i++;
1452 }
1453 868 splice(@xreflist, $j, 0, @inserts);
1454 868 $j += @inserts;
1455 868 $i++;
1456 }
1457 }
1458  
1459 0 ▶ my @freelist = sort { $self->{' objects'}{$a->uid}[0] <=> $self->{' objects'}{$b->uid}[0] } @{$self->{' free'} || []};
Condition100%2/2
$self->{' free'} || []
l!l
1453
MC/DC100%1/1
$self->{' free'} || []
$self->{' free'}
Truth table100%2/2
A
A= $self->{' free'}
Aresult
11
00
1460  
1461 148 $j = 0; my $first = -1; $k = 0;
1462 148 ▶ for ($i = 0; $i <= $#xreflist + 1; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#xreflist + 1) 1038 148 1186
1463 1038 ▶ if ($i > $#xreflist || $self->{' objects'}{$xreflist[$i]->uid}[0] != $j + 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > $#xreflist or $self->{' objects'}{$xreflist[$i]->uid}[0] != $j + 1) { } 160 878 1038
Condition100%3/3
$i > $#xreflist or $self->{" objects"}{$xreflist[$i]->uid}[0] != $j + 1
l!l&&r!l&&!r
14812878
MC/DC100%2/2
$i > $#xreflist or $self->{" objects"}{$xreflist[$i]->uid}[0] != $j + 1
$i > $#xreflist $self->{" objects"}{$xreflist[$i]->uid}[0] != $j + 1
Truth table100%3/3
A or B
A= $i > $#xreflistB= $self->{" objects"}{$xreflist[$i]->uid}[0] != $j + 1
ABresult
1X1
011
000
1464 160 ▶ push @out, ($first == -1 ? "0 " : "$self->{' objects'}{$xreflist[$first]->uid}[0] ") . ($i - $first) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
$first == -1 ? : 148 12 160
1465 160 ▶ if ($first == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first == -1) 148 12 160
1466 148 ▶ push @out, sprintf("%010d 65535 f \n", defined $freelist[$k] ? $self->{' objects'}{$freelist[$k]->uid}[0] : 0);
Branch: 1 branch
BranchTrueFalseTotal
defined $freelist[$k] ? : 0 148 148
1467 148 $first = 0;
1468 }
1469 160 ▶ for ($j = $first; $j < $i; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $i) 890 160 1050
1470 890 my $xref = $xreflist[$j];
1471 890 ▶ if (defined($freelist[$k]) and defined($xref) and "$freelist[$k]" eq "$xref") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $freelist[$k] and defined $xref and "$freelist[$k]" eq "$xref") { } 0 890 890
Condition33%2/6
defined $freelist[$k] and defined $xref and "$freelist[$k]" eq "$xref"
!ll&&!rl&&r
89000
defined $freelist[$k] and defined $xref
!ll&&!rl&&r
89000
MC/DC0%0/3
defined $freelist[$k] and defined $xref and "$freelist[$k]" eq "$xref"
defined $freelist[$k] defined $xref "$freelist[$k]" eq "$xref"
Truth table25%1/4
A and B and C
A= defined $freelist[$k]B= defined $xrefC= "$freelist[$k]" eq "$xref"
ABCresult
0XX0
10X0
1100
1111
1472 0 $k++;
1473 push @out, pack("A10AA5A4",
1474 sprintf("%010d", (defined $freelist[$k] ?
1475 $self->{' objects'}{$freelist[$k]->uid}[0] : 0)), " ",
1476 0 ▶ sprintf("%05d", $self->{' objects'}{$xref->uid}[1] + 1),
Branch: 1 branch
BranchTrueFalseTotal
defined $freelist[$k] ? : 0 0 0
1477 " f \n");
1478 }
1479 else {
1480 push @out, pack("A10AA5A4", sprintf("%010d", $self->{' locs'}{$xref->uid}), " ",
1481 890 sprintf("%05d", $self->{' objects'}{$xref->uid}[1]),
1482 " n \n");
1483 }
1484 }
1485 160 $first = $i;
1486 160 ▶ $j = $self->{' objects'}{$xreflist[$i]->uid}[0] if ($i < scalar @xreflist);
Branch: 1 branch
BranchTrueFalseTotal
if $i < scalar @xreflist 12 148 160
1487 }
1488 else {
1489 878 $j++;
1490 }
1491 }
1492 148 ▶ if (exists $tdict->{'Type'} and $tdict->{'Type'}->val() eq 'XRef') {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tdict->{'Type'} and $tdict->{'Type'}->val eq 'XRef') { } 0 148 148
Condition33%1/3
exists $tdict->{"Type"} and $tdict->{"Type"}->val eq "XRef"
!ll&&!rl&&r
14800
MC/DC0%0/2
exists $tdict->{"Type"} and $tdict->{"Type"}->val eq "XRef"
exists $tdict->{"Type"} $tdict->{"Type"}->val eq "XRef"
Truth table33%1/3
A and B
A= exists $tdict->{"Type"}B= $tdict->{"Type"}->val eq "XRef"
ABresult
0X0
100
111
1493 0 my (@index, @stream);
1494 0 for (@out) {
1495 0 my @a = split;
1496 0 ▶ @a == 2 ? push @index, @a : push @stream, \@a;
Branch: 1 branch
BranchTrueFalseTotal
@a == 2 ? : 0 0 0
1497 }
1498 0 my $i = $self->{' maxobj'}++;
1499 0 $self->add_obj($tdict, $i, 0);
1500 0 $self->out_obj($tdict );
1501  
1502 0 push @index, $i, 1;
1503 0 push @stream, [$tloc, 0, 'n'];
1504  
1505 0 ▶ my $len = $tloc > 0xFFFF ? 4 : 2; # don't expect files > 4 Gb
Branch: 1 branch
BranchTrueFalseTotal
$tloc > 65535 ? : 0 0 0
1506 0 ▶ my $tpl = $tloc > 0xFFFF ? 'CNC' : 'CnC'; # don't expect gennum > 255, it's absurd.
Branch: 1 branch
BranchTrueFalseTotal
$tloc > 65535 ? : 0 0 0
1507 # Adobe doesn't use them anymore anyway
1508 0 my $stream = '';
1509 0 my @prev = (0) x ($len + 2);
1510 0 for (@stream) {
1511 0 ▶ $_->[1] = 0 if $_->[2] eq 'f' and $_->[1] == 65535;
Branch: 1 branch
BranchTrueFalseTotal
if $_->[2] eq "f" and $_->[1] == 65535 0 0 0
Condition0%0/3
$_->[2] eq "f" and $_->[1] == 65535
!ll&&!rl&&r
000
MC/DC0%0/2
$_->[2] eq "f" and $_->[1] == 65535
$_->[2] eq "f" $_->[1] == 65535
Truth table0%0/3
A and B
A= $_->[2] eq "f"B= $_->[1] == 65535
ABresult
0X0
100
111
1512 0 ▶ my @line = unpack 'C*', pack $tpl, $_->[2] eq 'n' ? 1 : 0, @{$_}[0..1];
Branch: 1 branch
BranchTrueFalseTotal
$_->[2] eq 'n' ? : 0 0 0
1513  
1514 $stream .= pack 'C*', 2, # prepend filtering method, "PNG Up"
1515 0 map {($line[$_] - $prev[$_] + 256) % 256 } 0 .. $#line;
1516 0 @prev = @line;
1517 }
1518 0 $tdict->{'Size'} = PDFNum($i + 1);
1519 0 $tdict->{'Index'} = PDFArray(map PDFNum( $_ ), @index);
1520 0 $tdict->{'W'} = PDFArray(map PDFNum( $_ ), 1, $len, 1);
1521 0 $tdict->{'Filter'} = PDFName('FlateDecode');
1522  
1523 0 $tdict->{'DecodeParms'} = PDFDict();
1524 0 $tdict->{'DecodeParms'}->val->{'Predictor'} = PDFNum(12);
1525 0 $tdict->{'DecodeParms'}->val->{'Columns'} = PDFNum($len + 2);
1526  
1527 0 $stream = PDF::API2::Basic::PDF::Filter::FlateDecode->new->outfilt($stream, 1);
1528 0 $tdict->{' stream'} = $stream;
1529 0 $tdict->{' nofilt'} = 1;
1530 0 delete $tdict->{'Length'};
1531 0 $self->ship_out();
1532 }
1533 else {
1534 148 $fh->print("xref\n", @out, "trailer\n");
1535 148 $tdict->outobjdeep($fh, $self);
1536 148 $fh->print("\n");
1537 }
1538 148 $fh->print("startxref\n$tloc\n%%EOF\n");
1539 }
1540  
1541  
1542 =head2 PDF::API2::Basic::PDF::File->_new
1543  
1544 Creates a very empty PDF file object (used by new and open)
1545  
1546 =cut
1547  
1548 180 ▶sub _new {
Subroutine _new: called CC=1 SCAR=0.0
1549 180 my $class = shift();
1550 180 my $self = {};
1551  
1552 180 bless $self, $class;
1553 180 $self->{' outlist'} = [];
1554 180 $self->{' outlist_cache'} = {}; # A cache of whats in the 'outlist'
1555 180 $self->{' maxobj'} = 1;
1556 180 $self->{' objcache'} = {};
1557 180 $self->{' objects'} = {};
1558  
1559 180 return $self;
1560 }
1561  
1562 40 1;
1563  
1564 =head1 AUTHOR
1565  
1566 Martin Hosken Martin_Hosken@sil.org
1567  
1568 Copyright Martin Hosken 1999 and onwards
1569  
1570 No warranty or expression of effectiveness, least of all regarding anyone's
1571 safety, is implied in this software or documentation.
« API2/Basic/PDF/Literal.pm ↑ Summary API2/Resource/XObject/Form/BarCode/qrcode.pm »