Builder/Basic/PDF/File.pm

filter: Statementstmt 69.3% 591 / 852 Branchbran 51.2% 239 / 466 Conditioncond 43.8% 71 / 162 MC/DCmcdc 18.8% 19 / 101 Subroutinesub 93.6% 44 / 47 Podpod 96.1% 25 / 26 totaltotal 59.7% 989 / 1654 CC 305 SCAR 51.4
CC
305
Cov
61%
readxrtr
599.1
copy
210.0
out_trailer
130.8
CRAP 171.0
« Builder/Basic/PDF/Filter/FlateDecode.pm ↑ Summary Builder/Resource/XObject/Image/TIFF/File_GT.pm »
1 #=======================================================================
2 #
3 # THIS IS A REUSED PERL MODULE, FOR PROPER LICENCING TERMS SEE BELOW:
4 #
5 # Copyright Martin Hosken <Martin_Hosken@sil.org>
6 #
7 # Modified for PDF::API2 by Alfred Reibenschuh <alfredreibenschuh@gmx.net>
8 #
9 # No warranty or expression of effectiveness, least of all regarding
10 # anyone's safety, is implied in this software or documentation.
11 #
12 # This specific module is licensed under the Perl Artistic License.
13 # Effective 28 January 2021, the original author and copyright holder,
14 # Martin Hosken, has given permission to use and redistribute this module
15 # under the MIT license.
16 #
17 #=======================================================================
18 package PDF::Builder::Basic::PDF::File;
19  
20 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 40 our $VERSION = '3.028'; # VERSION
24 40 our $LAST_UPDATE = '3.028'; # manually update whenever code is changed
25  
26 =head1 NAME
27  
28 PDF::Builder::Basic::PDF::File - Holds the trailers and cross-reference tables for a PDF file
29  
30 =head1 SYNOPSIS
31  
32 $p = PDF::Builder::Basic::PDF::File->open("filename.pdf", 1);
33 $p->new_obj($obj_ref);
34 $p->free_obj($obj_ref);
35 $p->append_file();
36 $p->close_file();
37 $p->release(); # IMPORTANT!
38  
39 =head1 DESCRIPTION
40  
41 This class keeps track of the directory aspects of a PDF file. There are two
42 parts to the directory: the main directory object, which is the parent to all
43 other objects, and a chain of cross-reference tables and corresponding trailer
44 dictionaries, starting with the main directory object.
45  
46 =head1 INSTANCE VARIABLES
47  
48 Within this class hierarchy, rather than making everything visible via methods,
49 which would be a lot of work, there are various instance variables which are
50 accessible via associative array referencing. To distinguish instance variables
51 from content variables (which may come from the PDF content itself), each such
52 variable name will start with a space.
53  
54 Variable names which do not start with a space directly reflect elements in a
55 PDF dictionary. In the case of a C<PDF::Builder::Basic::PDF::File>, the
56 elements reflect those in the trailer dictionary.
57  
58 Since some variables are not designed for class users to access, variables are
59 marked in the documentation with B<(R)> to indicate that such an entry should
60 only be used as B<read-only> information. B<(P)> indicates that the information
61 is B<private>, and not designed for user use at all, but is included in the
62 documentation for completeness and to ensure that nobody else tries to use it.
63  
64 =over
65  
66 =item newroot
67  
68 This variable allows the user to create a new root entry to occur in the trailer
69 dictionary which is output when the file is written or appended. If you wish to
70 override the root element in the dictionary you have, use this entry to indicate
71 that without losing the current Root entry. Notice that newroot should point to
72 a PDF level object and not just to a dictionary, which does not have object
73 status.
74  
75 =item INFILE (R)
76  
77 Contains the filehandle used to read this information into this PDF directory.
78 It is an IO object.
79  
80 =item fname (R)
81  
82 This is the filename which is reflected by INFILE, or the original IO object
83 passed in.
84  
85 =item update (R)
86  
87 This indicates that the read file has been opened for update and that at some
88 point, C<< $p->appendfile() >> can be called to update the file with the
89 changes that have been made to the memory representation.
90  
91 =item maxobj (R)
92  
93 Contains the first usable object number above any that have already appeared
94 in the file so far.
95  
96 =item outlist (P)
97  
98 This is a list of Objind which are to be output when the next C<appendfile()>
99 or C<outfile()> occurs.
100  
101 =item firstfree (P)
102  
103 Contains the first free object in the free object list. Free objects are removed
104 from the front of the list and added to the end.
105  
106 =item lastfree (P)
107  
108 Contains the last free object in the free list. It may be the same as the
109 C<firstfree> if there is only one free object.
110  
111 =item objcache (P)
112  
113 All objects are held in the cache to ensure that a system only has one
114 occurrence of each object. In effect, the objind class acts as a container type
115 class to hold the PDF object structure, and it would be unfortunate if there
116 were two identical place-holders floating around a system.
117  
118 =item epos (P)
119  
120 The end location of the read-file.
121  
122 =back
123  
124 Each trailer dictionary contains a number of private instance variables which
125 hold the chain together.
126  
127 =over
128  
129 =item loc (P)
130  
131 Contains the location of the start of the cross-reference table preceding the
132 trailer.
133  
134 =item xref (P)
135  
136 Contains an anonymous array of each cross-reference table entry.
137  
138 =item prev (P)
139  
140 A reference to the previous table. Note this differs from the Prev entry which
141 is in PDF, which contains the location of the previous cross-reference table.
142  
143 =back
144  
145 =cut
146  
147 40 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
148  
149 40 ▶use vars qw($cr $irreg_char $reg_char $ws_char $delim_char %types);
Subroutine BEGIN: called CC=1 SCAR=0.0
150  
151 40 $ws_char = '[ \t\r\n\f\0]';
152 40 $delim_char = '[][<>{}()/%]';
153 40 $reg_char = '[^][<>{}()/% \t\r\n\f\0]';
154 40 $irreg_char = '[][<>{}()/% \t\r\n\f\0]';
155 # \015 = x0D = CR or \r, \012 = x0A = LF or \n
156 # TBD a line-end character is space CR ' \r', space LF ' \n', or CR LF '\r\n'
157 # have seen working PDFs with just a CR and space CR
158 40 $cr = '\s*(?:\015|\012|(?:\015\012))';
159  
160 40 my $re_comment = qr/(?:\%[^\r\n]*)/;
161 40 my $re_whitespace = qr/(?:[ \t\r\n\f\0]|$re_comment)/;
162  
163 40 %types = (
164 'Page' => 'PDF::Builder::Basic::PDF::Page',
165 'Pages' => 'PDF::Builder::Basic::PDF::Pages',
166 );
167  
168 40 my $readDebug = 0;
169  
170 40 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
171 40 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
172  
173 # Now for the basic PDF types
174 40 ▶use PDF::Builder::Basic::PDF::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
175  
176 40 ▶use PDF::Builder::Basic::PDF::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
177 40 ▶use PDF::Builder::Basic::PDF::Bool;
Subroutine BEGIN: called CC=1 SCAR=0.0
178 40 ▶use PDF::Builder::Basic::PDF::Dict;
Subroutine BEGIN: called CC=1 SCAR=0.0
179 40 ▶use PDF::Builder::Basic::PDF::Name;
Subroutine BEGIN: called CC=1 SCAR=0.0
180 40 ▶use PDF::Builder::Basic::PDF::Number;
Subroutine BEGIN: called CC=1 SCAR=0.0
181 40 ▶use PDF::Builder::Basic::PDF::Objind;
Subroutine BEGIN: called CC=1 SCAR=0.0
182 40 ▶use PDF::Builder::Basic::PDF::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
183 40 ▶use PDF::Builder::Basic::PDF::Page;
Subroutine BEGIN: called CC=1 SCAR=0.0
184 40 ▶use PDF::Builder::Basic::PDF::Pages;
Subroutine BEGIN: called CC=1 SCAR=0.0
185 40 ▶use PDF::Builder::Basic::PDF::Null;
Subroutine BEGIN: called CC=1 SCAR=0.0
186 40 ▶use POSIX qw(ceil floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
187  
188 =head1 METHODS
189  
190 =head2 new
191  
192 PDF::Builder::Basic::PDF::File->new()
193  
194 =over
195  
196 Creates a new, empty file object which can act as the host to other PDF objects.
197 Since there is no file associated with this object, it is assumed that the
198 object is created in readiness for creating a new PDF file.
199  
200 =back
201  
202 =cut
203  
204 234 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
205 234 my ($class, $root) = @_;
206 234 my $self = $class->_new();
207  
208 234 ▶ unless ($root) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($root) 234 0 234
209 234 $root = PDFDict();
210 234 $root->{'Type'} = PDFName('Catalog');
211 }
212 234 $self->new_obj($root);
213  
214 234 $self->{'Root'} = $root;
215  
216 234 return $self;
217 }
218  
219 =head2 open
220  
221 $p = PDF::Builder::Basic::PDF::File->open($filename, $update, %options)
222  
223 =over
224  
225 Opens the file and reads all the trailers and cross reference tables to build
226 a complete directory of objects.
227  
228 C<$filename> may be a string or an IO object.
229  
230 C<$update> specifies whether this file is being opened for updating and editing
231 (I<TRUE> value), or simply to be read (I<FALSE> or undefined value).
232  
233 C<%options> may include
234  
235 =over
236  
237 =item diags => 1
238  
239 If C<diags> is set to 1, various warning messages will be given if a
240 suspicious PDF structure is found, and some fixup may be attempted. There is
241 no guarantee that any fixup will change the PDF to legitimate, or that there
242 won't be other problems found further down the line. If this flag is I<not>
243 given, and a structural problem is found, it is fairly likely that errors (and
244 even a program B<crash>) may happen further along. If you experience crashes
245 when reading in a PDF file, try running with C<diags> and see what is reported.
246  
247 There are many PDF files out "in the wild" which, while failing to conform to
248 Adobe's standards, appear to be tolerated by PDF Readers. Thus, Builder will
249 not fail on them, but merely comment on their existence.
250  
251 =back
252  
253 =back
254  
255 =cut
256  
257 18 ▶sub open {
Subroutine open: called CC=21 SCAR=43.6
Pod documented
258 18 my ($class, $filename, $update, %options) = @_;
259 # copy dashed option names to preferred undashed names
260 0 ▶ if (defined $options{'-diags'} && !defined $options{'diags'}) { $options{'diags'} = delete($options{'-diags'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"-diags"} and not defined $options{"diags"}) 0 18 18
Condition33%1/3
defined $options{"-diags"} and not defined $options{"diags"}
!ll&&!rl&&r
1800
MC/DC0%0/2
defined $options{"-diags"} and not defined $options{"diags"}
defined $options{"-diags"} not defined $options{"diags"}
Truth table33%1/3
A and not(B)
A= defined $options{"-diags"}B= not defined $options{"diags"}
ABresult
0X0
100
111
261 18 my ($fh, $buffer);
262 18 ▶ $options{'diags'} = 0 if not defined $options{'diags'}; # default
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options{"diags"} 18 0 18
263  
264 18 my $comment = ''; # any comment jammed into the PDF header
265 18 my $self = $class->_new();
266 18 ▶ if (ref $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $filename) { } 18 0 18
267 18 $self->{' INFILE'} = $filename;
268 18 ▶ if ($update) {
Branch: 1 branch
BranchTrueFalseTotal
if ($update) 18 0 18
269 18 $self->{' update'} = 1;
270 18 $self->{' OUTFILE'} = $filename;
271 }
272 18 $fh = $filename;
273 } else {
274 0 ▶ die "PDF file '$filename' to open does not exist!" unless -f $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $filename 0 0 0
275 0 ▶ die "PDF file '$filename' to open is not readable!" unless -r $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $filename 0 0 0
276 # requesting to update (write) to file? needs to be r/w
277 0 ▶ if ($update) {
Branch: 1 branch
BranchTrueFalseTotal
if ($update) 0 0 0
278 0 ▶ die "PDF file '$filename' to update is not writable!" unless -w $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -w $filename 0 0 0
279 }
280 0 ▶ $fh = IO::File->new(($update ? '+' : '') . "<$filename");
Branch: 1 branch
BranchTrueFalseTotal
$update ? : 0 0 0
281 0 ▶ if (!$fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 0 0
282 0 die "File '$filename' unable to open! $!";
283 }
284 0 $self->{' INFILE'} = $fh;
285 0 ▶ if ($update) {
Branch: 1 branch
BranchTrueFalseTotal
if ($update) 0 0 0
286 0 $self->{' update'} = 1;
287 0 $self->{' OUTFILE'} = $fh;
288 0 $self->{' fname'} = $filename;
289 }
290 }
291 18 binmode $fh, ':raw';
292 18 $fh->seek(0, 0); # go to start of file
293 18 $fh->read($buffer, 255);
294 18 ▶ unless ($buffer =~ m/^\%PDF\-(\d+\.\d+)(.*?)$cr/mo) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($buffer =~ /^\%PDF\-(\d+\.\d+)(.*?)$PDF::Builder::Basic::PDF::File::cr/mo) 0 18 18
295 0 die "$filename does not contain a valid PDF version number";
296 }
297 18 $self->{' version'} = $1;
298 # can't run verCheckInput() yet, as full ' version' not set
299 18 ▶ if (defined $2 && length($2) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $2 and length $2 > 0) 0 18 18
Condition33%1/3
defined $2 and length $2 > 0
!ll&&!rl&&r
0180
MC/DC0%0/2
defined $2 and length $2 > 0
defined $2 length $2 > 0
Truth table33%1/3
A and B
A= defined $2B= length $2 > 0
ABresult
0X0
100
111
300 0 $comment = $2; # save for output as comment
301 # since we just echo the original header + comment, unless that causes
302 # problems in some Readers, we can just leave it be (no call to strip
303 # out inline comment and create a separate comment further along).
304 }
305  
306 # there should always be 'startxref' within 16*64 bytes of end
307 18 $fh->seek(0, 2); # go to end of file
308 18 my $end = $fh->tell();
309 18 $self->{' epos'} = $end;
310 18 foreach my $offset (1 .. 64) {
311 36 $fh->seek($end - 16 * $offset, 0);
312 36 $fh->read($buffer, 16 * $offset);
313 36 ▶ last if $buffer =~ m/startxref($cr|\s*)\d+($cr|\s*)\%\%eof.*?/i;
Branch: 1 branch
BranchTrueFalseTotal
if $buffer =~ /startxref($PDF::Builder::Basic::PDF::File::cr|\s*)\d+($PDF::Builder::Basic::PDF::File::cr|\s*)\%\%eof.*?/i 18 18 36
314 }
315 18 ▶ unless ($buffer =~ m/startxref[^\d]+([0-9]+)($cr|\s*)\%\%eof.*?/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($buffer =~ /startxref[^\d]+([0-9]+)($PDF::Builder::Basic::PDF::File::cr|\s*)\%\%eof.*?/i) 0 18 18
316 0 ▶ if ($options{'diags'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"diags"} == 1) 0 0 0
317 0 warn "Malformed PDF file $filename";
318 }
319 }
320 18 my $xpos = $1; # offset given after 'startxref'
321 # should point to either xref table ('xref'), or object with xref stream
322 18 $self->{' xref_position'} = $xpos;
323  
324 18 my $tdict = $self->readxrtr($xpos, %options);
325 18 foreach my $key (keys %$tdict) {
326 133 $self->{$key} = $tdict->{$key};
327 }
328  
329 18 return $self;
330 } # end of open()
331  
332 =head2 version
333  
334 $new_version = $p->pdf_version($version, %opts) # Set
335  
336 $ver = $p->pdf_version() # Get
337  
338 =over
339  
340 Gets/sets the PDF version (e.g., 1.5). Setting sets both the header and
341 trailer versions. Getting returns the higher of header and trailer versions.
342  
343 For compatibility with earlier releases, if no decimal point is given, assume
344 "1." precedes the number given.
345  
346 A warning message is given if you attempt to I<decrease> the PDF version, as you
347 might have already read in a higher level file, or used a higher level feature.
348 This message is suppressed if the 'silent' option is given with any value.
349  
350 =back
351  
352 =cut
353  
354 31 ▶sub pdf_version {
Subroutine pdf_version: called CC=11 SCAR=28.0
Pod undocumented
355 31 my $self = shift();
356  
357 # current version is the higher of trailer and header versions
358 31 my $header_version = $self->header_version();
359 31 my $trailer_version = $self->trailer_version();
360 31 ▶ my $old_version = (defined $trailer_version &&
Branch: 1 branch
BranchTrueFalseTotal
defined $trailer_version && $trailer_version > $header_version ? : 5 26 31
Condition100%3/3
defined $trailer_version and $trailer_version > $header_version
!ll&&!rl&&r
2245
MC/DC100%2/2
defined $trailer_version and $trailer_version > $header_version
defined $trailer_version $trailer_version > $header_version
Truth table100%3/3
A and B
A= defined $trailer_versionB= $trailer_version > $header_version
ABresult
0X0
100
111
361 $trailer_version > $header_version)?
362 $trailer_version: $header_version;
363  
364 31 ▶ if (@_) { # Set, possibly with options
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 28 31
365 3 my $version = shift();
366 3 my %opts = @_;
367 # copy dashed option names to preferred undashed names
368 0 ▶ if (defined $opts{'-silent'} && !defined $opts{'silent'}) { $opts{'silent'} = delete($opts{'-silent'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"-silent"} and not defined $opts{"silent"}) 0 3 3
Condition33%1/3
defined $opts{"-silent"} and not defined $opts{"silent"}
!ll&&!rl&&r
300
MC/DC0%0/2
defined $opts{"-silent"} and not defined $opts{"silent"}
defined $opts{"-silent"} not defined $opts{"silent"}
Truth table33%1/3
A and not(B)
A= defined $opts{"-silent"}B= not defined $opts{"silent"}
ABresult
0X0
100
111
369
370 # 1.x and 2.x versions allowed
371 0 ▶ if ($version =~ m/^\d+$/) { $version = "1.$version"; } # no x.? assume it's 1.something
Branch: 1 branch
BranchTrueFalseTotal
if ($version =~ /^\d+$/) 0 3 3
372 # check if well formed 1.x and 2.x
373 3 ▶ if ($version !~ /^[12]\.[0-9]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($version =~ /^[12]\.[0-9]+$/) 0 3 3
374 0 ▶ croak "Invalid version '$version' ignored" unless defined $opts{'silent'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"silent"} 0 0 0
375 0 return $old_version;
376 }
377  
378 3 ▶ if ($old_version > $version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_version > $version) 0 3 3
379 0 ▶ croak "Warning: call to header_version() to LOWER the output PDF version number!" unless defined $opts{'silent'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"silent"} 0 0 0
380 }
381
382 # have already squawked about any problems with $version
383 3 $self->header_version($version, 'silent'=>1);
384 #if ($version >= 1.4) { # min 1.4 level
385 3 $self->trailer_version($version, 'silent'=>1);
386 #}
387 #else {
388 # delete $self->{'Root'}->{'Version'};
389 # $self->out_obj($self->{'Root'});
390 #}
391 3 return $version;
392 }
393  
394 # Get
395 28 return $old_version;
396 }
397  
398 =head2 header_version
399  
400 $new_version = $p->header_version($version, %opts) # Set
401  
402 $version = $p->header_version() # Get
403  
404 =over
405  
406 Gets/sets the PDF version stored in the file header.
407  
408 For compatibility with earlier releases, if no decimal point is given, assume
409 "1." precedes the number given.
410  
411 A warning message is given if you attempt to I<decrease> the PDF version, as you
412 might have already read in a higher level file, or used a higher level feature.
413 This message is suppressed if the 'silent' option is given with any value.
414  
415 =back
416  
417 =cut
418  
419 39 ▶sub header_version {
Subroutine header_version: called CC=9 SCAR=27.7
Pod documented
420 39 my $self = shift();
421  
422 # current (header) version
423 39 my $old_version = $self->{' version'};
424  
425 39 ▶ if (@_) { # Set, permits versions 1.x and 2.x
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 33 39
426 6 my $version = shift();
427 6 my %opts = @_;
428 # copy dashed option names to preferred undashed names
429 0 ▶ if (defined $opts{'-silent'} && !defined $opts{'silent'}) { $opts{'silent'} = delete($opts{'-silent'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"-silent"} and not defined $opts{"silent"}) 0 6 6
Condition33%1/3
defined $opts{"-silent"} and not defined $opts{"silent"}
!ll&&!rl&&r
600
MC/DC0%0/2
defined $opts{"-silent"} and not defined $opts{"silent"}
defined $opts{"-silent"} not defined $opts{"silent"}
Truth table33%1/3
A and not(B)
A= defined $opts{"-silent"}B= not defined $opts{"silent"}
ABresult
0X0
100
111
430
431 # 1.x and 2.x versions allowed
432 0 ▶ if ($version =~ m/^\d+$/) { $version = "1.$version"; } # no x.? assume it's 1.something
Branch: 1 branch
BranchTrueFalseTotal
if ($version =~ /^\d+$/) 0 6 6
433 # check if well formed 1.x and 2.x
434 6 ▶ if ($version !~ /^[12]\.[0-9]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($version =~ /^[12]\.[0-9]+$/) 0 6 6
435 0 ▶ croak "Invalid header_version '$version' ignored" unless defined $opts{'silent'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"silent"} 0 0 0
436 0 return $old_version;
437 }
438  
439 6 ▶ if ($old_version > $version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_version > $version) 0 6 6
440 0 ▶ croak "Warning: call to header_version() to LOWER the output PDF version number!" unless defined $opts{'silent'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"silent"} 0 0 0
441 }
442
443 6 $self->{' version'} = $version;
444 6 return $version;
445 }
446  
447 # Get
448 33 return $old_version;
449 }
450  
451 =head2 trailer_version
452  
453 $new_version = $p->trailer_version($version, %opts) # Set
454  
455 $version = $p->trailer_version() # Get
456  
457 =over
458  
459 Gets/sets the PDF version stored in the document catalog.
460  
461 Note that the minimum PDF level for a trailer version is 1.4. It is not
462 permitted to set a PDF level of 1.3 or lower. An existing PDF (read in) of
463 1.3 or below returns undefined.
464  
465 For compatibility with earlier releases, if no decimal point is given, assume
466 "1." precedes the number given.
467  
468 A warning message is given if you attempt to I<decrease> the PDF version, as you
469 might have already read in a higher level file, or used a higher level feature.
470 This message is suppressed if the 'silent' option is given with any value.
471  
472 =back
473  
474 =cut
475  
476 37 ▶sub trailer_version {
Subroutine trailer_version: called CC=11 SCAR=28.5
Pod documented
477 37 my $self = shift();
478  
479 37 my $old_version = undef;
480 37 ▶ if ($self->{'Root'}->{'Version'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Root"}{"Version"}) 12 25 37
481 12 $self->{'Root'}->{'Version'}->realise();
482 12 $old_version = $self->{'Root'}->{'Version'}->val();
483 }
484  
485 37 ▶ if (@_) { # Set, allows versions 1.x and 2.x
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 5 32 37
486 5 my $version = shift();
487 5 my %opts = @_;
488 # copy dashed option names to preferred undashed names
489 0 ▶ if (defined $opts{'-silent'} && !defined $opts{'silent'}) { $opts{'silent'} = delete($opts{'-silent'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"-silent"} and not defined $opts{"silent"}) 0 5 5
Condition33%1/3
defined $opts{"-silent"} and not defined $opts{"silent"}
!ll&&!rl&&r
500
MC/DC0%0/2
defined $opts{"-silent"} and not defined $opts{"silent"}
defined $opts{"-silent"} not defined $opts{"silent"}
Truth table33%1/3
A and not(B)
A= defined $opts{"-silent"}B= not defined $opts{"silent"}
ABresult
0X0
100
111
490
491 # 1.x and 2.x versions allowed
492 0 ▶ if ($version =~ m/^\d+$/) { $version = "1.$version"; } # no x.? assume it's 1.something
Branch: 1 branch
BranchTrueFalseTotal
if ($version =~ /^\d+$/) 0 5 5
493 # check if well formed 1.x and 2.x
494 5 ▶ if ($version !~ /^[12]\.[0-9]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($version =~ /^[12]\.[0-9]+$/) 0 5 5
495 0 ▶ croak "Invalid trailer_version '$version' ignored" unless defined $opts{'silent'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"silent"} 0 0 0
496 0 return $old_version;
497 }
498  
499 5 ▶ if (defined $old_version && $old_version > $version) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old_version and $old_version > $version) 0 5 5
Condition66%2/3
defined $old_version and $old_version > $version
!ll&&!rl&&r
320
MC/DC0%0/2
defined $old_version and $old_version > $version
defined $old_version $old_version > $version
Truth table66%2/3
A and B
A= defined $old_versionB= $old_version > $version
ABresult
0X0
100
111
500 0 ▶ croak "Warning: call to trailer_version() to LOWER the output PDF version number!" unless defined $opts{'silent'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"silent"} 0 0 0
501 }
502
503 5 $self->{'Root'}->{'Version'} = PDFName($version);
504 5 $self->out_obj($self->{'Root'});
505 5 return $version;
506 }
507  
508 # Get
509 32 return $old_version;
510 }
511  
512 =head2 require_version
513  
514 $prev_version = $p->require_version($version)
515  
516 =over
517  
518 Ensures that the PDF version is at least C<$version>.
519 Silently sets the version to the higher level.
520  
521 =back
522  
523 =cut
524  
525 3 ▶sub require_version {
Subroutine require_version: called CC=2 SCAR=6.9
Pod documented
526 3 my ($self, $min_version) = @_;
527 3 my $current_version = $self->pdf_version();
528 3 ▶ $self->pdf_version($min_version) if $current_version < $min_version;
Branch: 1 branch
BranchTrueFalseTotal
if $current_version < $min_version 1 2 3
529 3 return $current_version;
530 }
531  
532 =head2 release
533  
534 $p->release()
535  
536 =over
537  
538 Releases ALL of the memory used by the PDF document and all of its
539 component objects. After calling this method, do B<NOT> expect to
540 have anything left in the C<PDF::Builder::Basic::PDF::File> object
541 (so if you need to save, be sure to do it before calling this method).
542  
543 B<NOTE>, that it is important that you call this method on any
544 C<PDF::Builder::Basic::PDF::File> object when you wish to destroy it and
545 free up its memory. Internally, PDF files have an enormous number of
546 cross-references, and this causes circular references within the
547 internal data structures. Calling C<release()> causes a brute-force
548 cleanup of the data structures, freeing up all of the memory. Once
549 you've called this method, though, don't expect to be able to do
550 anything else with the C<PDF::Builder::Basic::PDF::File> object; it'll
551 have B<no> internal state whatsoever.
552  
553 =back
554  
555 =cut
556  
557 # Maintainer's Question: Couldn't this be handled by a DESTROY method
558 # instead of requiring an explicit call to release()?
559 178 ▶sub release {
Subroutine release: called CC=9 SCAR=22.0
Pod documented
560 178 my $self = shift();
561  
562 178 ▶ return $self unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 178 178
563 178 my @tofree = values %$self;
564  
565 178 foreach my $key (keys %$self) {
566 2891 $self->{$key} = undef;
567 2891 delete $self->{$key};
568 }
569  
570 # PDFs with highly-interconnected page trees or outlines can hit Perl's
571 # recursion limit pretty easily, so disable the warning for this specific
572 # loop.
573 40 ▶ no warnings 'recursion'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
574  
575 178 ▶ while (my $item = shift @tofree) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $item = shift @tofree) 8712 178 8890
576 8712 ▶ if (blessed($item) and $item->can('release')) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($item) and $item->can('release')) { } 2529 6183 8712
elsif (ref $item eq 'ARRAY') { } 1850 4333 6183
elsif (ref $item eq 'HASH') { } 1062 3271 4333
Condition100%3/3
builtin::blessed($item) and $item->can("release")
!ll&&!rl&&r
58433402529
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
577 2529 $item->release(1);
578 } elsif (ref($item) eq 'ARRAY') {
579 1850 push @tofree, @$item;
580 } elsif (ref($item) eq 'HASH') {
581 1062 push @tofree, values %$item;
582 1062 foreach my $key (keys %$item) {
583 4575 $item->{$key} = undef;
584 4575 delete $item->{$key};
585 }
586 } else {
587 3271 $item = undef;
588 }
589 }
590  
591 178 return;
592 } # end of release()
593  
594 =head2 append_file
595  
596 $p->append_file()
597  
598 =over
599  
600 Appends the objects for output to the read file and then appends the
601 appropriate table.
602  
603 =back
604  
605 =cut
606  
607 8 ▶sub append_file {
Subroutine append_file: called CC=6 SCAR=18.3
Pod documented
608 8 my $self = shift();
609 8 ▶ return unless $self->{' update'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{" update"} 0 8 8
610  
611 8 my $fh = $self->{' INFILE'};
612  
613 # hack to upgrade pdf-version number to support requested features in
614 # higher versions than the pdf was originally created. WARNING: new version
615 # must be exactly SAME length as the old (e.g., 1.6 replacing 1.4), or
616 # problems are likely with overwriting header. perhaps some day we will
617 # need to check the old version being ovewritten, and adjust something to
618 # avoid corrupting the file.
619 8 ▶ my $version = $self->{' version'} || 1.4;
Condition50%1/2
$self->{' version'} || 1.4
l!l
80
MC/DC0%0/1
$self->{' version'} || 1.4
$self->{' version'}
Truth table50%1/2
A
A= $self->{' version'}
Aresult
11
00
620 8 $fh->seek(0, 0);
621 # assume that any existing EOL after version will be reused
622 8 $fh->print("%PDF-$version");
623  
624 8 my $tdict = PDFDict();
625 8 $tdict->{'Prev'} = PDFNum($self->{' loc'});
626 8 $tdict->{'Info'} = $self->{'Info'};
627 8 ▶ if (defined $self->{' newroot'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{' newroot'}) { } 0 8 8
628 0 $tdict->{'Root'} = $self->{' newroot'};
629 } else {
630 8 $tdict->{'Root'} = $self->{'Root'};
631 }
632 8 $tdict->{'Size'} = $self->{'Size'};
633  
634 8 foreach my $key (grep { $_ !~ m/^\s/ } keys %$self) {
635 25 ▶ $tdict->{$key} = $self->{$key} unless defined $tdict->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tdict->{$key} 0 25 25
636 }
637  
638 8 $fh->seek($self->{' epos'}, 0);
639 8 $self->out_trailer($tdict, $self->{' update'});
640 8 close $self->{' OUTFILE'};
641  
642 8 return;
643 } # end of append_file()
644  
645 =head2 out_file
646  
647 $p->out_file($fname)
648  
649 =over
650  
651 Writes a PDF file to a file of the given filename, based on the current list of
652 objects to be output. It creates the trailer dictionary based on information
653 in C<$self>.
654  
655 $fname may be a string or an IO object.
656  
657 =back
658  
659 =cut
660  
661 170 ▶sub out_file {
Subroutine out_file: called CC=1 SCAR=0.0
Pod documented
662 170 my ($self, $fname) = @_;
663  
664 170 $self = $self->create_file($fname);
665 170 $self = $self->close_file();
666  
667 170 return $self;
668 }
669  
670 =head2 create_file
671  
672 $p->create_file($fname)
673  
674 =over
675  
676 Creates a new output file (no check is made of an existing open file) of
677 the given filename or IO object. Note: make sure that C<< $p->{' version'} >>
678 is set correctly before calling this function.
679  
680 =back
681  
682 =cut
683  
684 170 ▶sub create_file {
Subroutine create_file: called CC=4 SCAR=15.5
Pod documented
685 170 my ($self, $filename) = @_;
686 170 my $fh;
687  
688 170 $self->{' fname'} = $filename;
689 170 ▶ if (ref $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $filename) { } 170 0 170
690 170 $fh = $filename;
691 } else {
692 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
693 0 binmode($fh,':raw');
694 }
695  
696 170 $self->{' OUTFILE'} = $fh;
697 170 ▶ $fh->print('%PDF-' . ($self->{' version'} // '1.4') . "\n");
Condition50%1/2
$self->{" version"} // "1.4"
l!l
1700
MC/DC0%0/1
$self->{" version"} // "1.4"
$self->{" version"}
Truth table50%1/2
A
A= $self->{" version"}
Aresult
11
00
698 170 $fh->print("%\xC6\xCD\xCD\xB5\n"); # and some binary stuff in a comment.
699  
700 # PDF spec requires 4 or more "binary" bytes (128 or higher value) in a
701 # comment immediately following the PDF-x.y header, to alert reader that
702 # there is binary data. Actual values are apparently arbitrary. This DOES
703 # mean that other comments can NOT be inserted between the header and the
704 # binary comment! PDF::Builder always outputs this comment, so is always
705 # claiming binary data (no harm done?).
706  
707 170 return $self;
708 }
709  
710 =head2 close_file
711  
712 $p->close_file()
713  
714 =over
715  
716 Closes up the open file for output, by outputting the trailer, etc.
717  
718 =back
719  
720 =cut
721  
722 170 ▶sub close_file {
Subroutine close_file: called CC=11 SCAR=32.6
Pod documented
723 170 my $self = shift();
724  
725 170 my $tdict = PDFDict();
726 170 ▶ $tdict->{'Info'} = $self->{'Info'} if defined $self->{'Info'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"Info"} 170 0 170
727 170 ▶ $tdict->{'Root'} = (defined $self->{' newroot'} and $self->{' newroot'} ne '') ? $self->{' newroot'} : $self->{'Root'};
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{' newroot'} && $self->{' newroot'} ne '' ? : 0 170 170
Condition33%1/3
defined $self->{" newroot"} and $self->{" newroot"} ne ""
!ll&&!rl&&r
17000
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
728  
729 # remove all freed objects from the outlist, AND the outlist_cache if not updating
730 # NO! Don't do that thing! In fact, let out_trailer do the opposite!
731  
732 170 ▶ $tdict->{'Size'} = $self->{'Size'} || PDFNum(1);
Condition33%1/3
$self->{'Size'} || PDFNum(1)
l!l&&r!l&&!r
01700
MC/DC0%0/2
$self->{'Size'} || PDFNum(1)
$self->{'Size'} PDFNum(1)
Truth table33%1/3
A || B
A= $self->{'Size'}B= PDFNum(1)
ABresult
1X1
011
000
733 170 ▶ $tdict->{'Prev'} = PDFNum($self->{' loc'}) if $self->{' loc'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{" loc"} 0 170 170
734 170 ▶ if ($self->{' update'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{" update"}) 0 170 170
735 0 foreach my $key (grep { $_ !~ m/^[\s\-]/ } keys %$self) {
736 0 ▶ $tdict->{$key} = $self->{$key} unless defined $tdict->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tdict->{$key} 0 0 0
737 }
738  
739 0 my $fh = $self->{' INFILE'};
740 0 $fh->seek($self->{' epos'}, 0);
741 }
742  
743 170 $self->out_trailer($tdict, $self->{' update'});
744 170 close($self->{' OUTFILE'});
745 170 ▶ if ($^O eq 'MacOS' and not ref($self->{' fname'})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MacOS" and not ref $self->{" fname"}) 0 170 170
Condition33%1/3
$^O eq "MacOS" and not ref $self->{" fname"}
!ll&&!rl&&r
17000
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
746 0 MacPerl::SetFileInfo('CARO', 'TEXT', $self->{' fname'});
747 }
748  
749 170 return $self;
750 } # end of close_file()
751  
752 =head2 readval
753  
754 ($value, $str) = $p->readval($str, %opts)
755  
756 =over
757  
758 Reads a PDF value from the current position in the file. If C<$str> is too
759 short, read some more from the current location in the file until the whole
760 object is read. This is a recursive call which may slurp in a whole big stream
761 (unprocessed).
762  
763 Returns the recursive data structure read and also the current C<$str> that has
764 been read from the file.
765  
766 =back
767  
768 =cut
769  
770 1479 ▶sub readval {
Subroutine readval: called CC=46 SCAR=43.8
Pod documented
771 1479 my ($self, $str, %opts) = @_;
772 1479 my $fh = $self->{' INFILE'};
773 1479 my ($result, $value);
774  
775 1479 ▶ my $update = defined($opts{'update'}) ? $opts{'update'} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'update'} ? : 10 1469 1479
776 1479 ▶ $str = update($fh, $str) if $update;
Branch: 1 branch
BranchTrueFalseTotal
if $update 1469 10 1479
777  
778 1479 $str =~ s/^$ws_char+//; # Ignore initial white space
779 1479 $str =~ s/^\%[^\015\012]*$ws_char+//; # Ignore comments
780  
781 1479 ▶ if ($str =~ m/^<</s) {
Branch: 10 branches
BranchTrueFalseTotal
if ($str =~ /^<</s) { } 162 1317 1479
elsif ($str =~ /^([0-9]+)(?:$PDF::Builder::Basic::PDF::File::ws_char|$re_comment)+([0-9]+)(?:$PDF::Builder::Basic::PDF::File::ws_char|$re_comment)+R/s) { } 166 1151 1317
elsif ($str =~ /^([0-9]+)(?:$PDF::Builder::Basic::PDF::File::ws_char|$re_comment)+([0-9]+)(?:$PDF::Builder::Basic::PDF::File::ws_char|$re_comment)+obj/s) { } 104 1047 1151
elsif ($str =~ m[^/($PDF::Builder::Basic::PDF::File::reg_char*)]s) { } 577 470 1047
elsif ($str =~ /^\(/) { } 2 468 470
elsif ($str =~ /^</) { } 0 468 468
elsif ($str =~ /^\[/) { } 98 370 468
elsif ($str =~ /^(true|false)($PDF::Builder::Basic::PDF::File::irreg_char|$)/) { } 0 370 370
elsif ($str =~ /^([+-.0-9]+)($PDF::Builder::Basic::PDF::File::irreg_char|$)/) { } 370 0 370
elsif ($str =~ /^null($PDF::Builder::Basic::PDF::File::irreg_char|$)/) { } 0 0 0
782 # Dictionary
783 162 $str = substr ($str, 2);
784 162 ▶ $str = update($fh, $str) if $update;
Branch: 1 branch
BranchTrueFalseTotal
if $update 160 2 162
785 162 $result = PDFDict();
786  
787 162 ▶ while ($str !~ m/^>>/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($str =~ /^>>/) 457 162 619
788 457 $str =~ s/^$ws_char+//; # Ignore initial white space
789 457 $str =~ s/^\%[^\015\012]*$ws_char+//; # Ignore comments
790  
791 457 ▶ if ($str =~ s|^/($reg_char+)||) {
Branch: 3 branches
BranchTrueFalseTotal
if ($str =~ s[^/($PDF::Builder::Basic::PDF::File::reg_char+)][]) { } 457 0 457
elsif ($str =~ s[^/$PDF::Builder::Basic::PDF::File::ws_char+][]) { } 0 0 0
elsif ($str =~ s[^//][/]) { } 0 0 0
792 457 my $key = PDF::Builder::Basic::PDF::Name::name_to_string($1, $self);
793 457 ($value, $str) = $self->readval($str, %opts);
794 # per Vadim Repin (RT 131147) CHG 1. His conclusion is that
795 # it is highly unlikely, but remotely possible, that there
796 # could be legitimate use of Null objects that should NOT be
797 # prevented from bubbling up. If such a case is discovered, we
798 # might have to try Klaus Ethgen's more limited (in scope)
799 # patch in ./Pages.pm. See full discussion in RT 131147 for
800 # details on what's going on and how this fixes it.
801 #$result->{$key} = $value; # original code
802 457 ▶ $result->{$key} = $value
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "PDF::Builder::Basic::PDF::Null" 457 0 457
803 unless ref($value) eq 'PDF::Builder::Basic::PDF::Null';
804 } elsif ($str =~ s|^/$ws_char+||) {
805 # fixes a broken key problem of acrobat. -- fredo
806 0 ($value, $str) = $self->readval($str, %opts);
807 0 $result->{'null'} = $value;
808 } elsif ($str =~ s|^//|/|) {
809 # fixes again a broken key problem of illustrator/enfocus. -- fredo
810 0 ($value, $str) = $self->readval($str, %opts);
811 0 $result->{'null'} = $value;
812 } else {
813 0 die "Invalid dictionary key";
814 }
815 457 ▶ $str = update($fh, $str) if $update; # thanks gareth.jones@stud.man.ac.uk
Branch: 1 branch
BranchTrueFalseTotal
if $update 455 2 457
816 }
817  
818 162 $str =~ s/^>>//;
819 162 ▶ $str = update($fh, $str) if $update;
Branch: 1 branch
BranchTrueFalseTotal
if $update 160 2 162
820 # streams can't be followed by a lone carriage-return.
821 # fredo: yes they can !!! -- use the MacOS, Luke.
822 # TBD isn't this covered by $cr as space CR?
823 162 ▶ 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) 11 151 162
Condition66%2/3
$str =~ s/^stream(?:(?:\015\012)|\012|\015)// and $result->{"Length"}->val != 0
!ll&&!rl&&r
151011
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
824 11 my $length = $result->{'Length'}->val() + 0; # ensure seen as decimal
825 11 $result->{' streamsrc'} = $fh;
826 11 $result->{' streamloc'} = $fh->tell() - length($str);
827  
828 11 ▶ unless ($opts{'nostreams'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"nostreams"}) 11 0 11
829 11 ▶ if ($length > length($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > length $str) { } 0 11 11
830 0 $value = $str;
831 0 $length -= length($str);
832 0 read $fh, $str, $length + 11; # slurp the whole stream!
833 } else {
834 11 $value = '';
835 }
836 11 $value .= substr($str, 0, $length);
837 11 $result->{' stream'} = $value;
838 11 $result->{' nofilt'} = 1;
839 11 ▶ $str = update($fh, $str, 1) if $update; # tell update we are in-stream and only need an endstream
Branch: 1 branch
BranchTrueFalseTotal
if $update 11 0 11
840 11 $str = substr($str, index($str, 'endstream') + 9);
841 }
842 }
843  
844 162 ▶ if (defined $result->{'Type'} and defined $types{$result->{'Type'}->val()}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result->{"Type"} and defined $PDF::Builder::Basic::PDF::File::types{$result->{"Type"}->val}) 38 124 162
Condition100%3/3
defined $result->{"Type"} and defined $PDF::Builder::Basic::PDF::File::types{$result->{"Type"}->val}
!ll&&!rl&&r
962838
MC/DC100%2/2
defined $result->{"Type"} and defined $PDF::Builder::Basic::PDF::File::types{$result->{"Type"}->val}
defined $result->{"Type"} defined $PDF::Builder::Basic::PDF::File::types{$result->{"Type"}->val}
Truth table100%3/3
A and B
A= defined $result->{"Type"}B= defined $PDF::Builder::Basic::PDF::File::types{$result->{"Type"}->val}
ABresult
0X0
100
111
845 38 bless $result, $types{$result->{'Type'}->val()};
846 }
847 # gdj: FIXME: if any of the ws chars were crs, then the whole
848 # string might not have been read.
849  
850 } elsif ($str =~ m/^([0-9]+)(?:$ws_char|$re_comment)+([0-9]+)(?:$ws_char|$re_comment)+R/s) {
851 # Indirect Object
852 166 my $num = $1 + 0; # ensure seen as decimal value
853 166 $value = $2 + 0;
854 166 $str =~ s/^([0-9]+)(?:$ws_char|$re_comment)+([0-9]+)(?:$ws_char|$re_comment)+R//s;
855 166 ▶ unless ($result = $self->test_obj($num, $value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result = $self->test_obj($num, $value)) 131 35 166
856 131 $result = PDF::Builder::Basic::PDF::Objind->new();
857 131 $result->{' objnum'} = $num;
858 131 $result->{' objgen'} = $value;
859 131 $self->add_obj($result, $num, $value);
860 }
861 166 $result->{' parent'} = $self;
862 166 weaken $result->{' parent'};
863 #$result->{' realised'} = 0;
864 # removed to address changes being lost when an indirect object
865 # is realised twice
866 # gdj: FIXME: if any of the ws chars were crs, then the whole
867 # string might not have been read.
868  
869 } elsif ($str =~ m/^([0-9]+)(?:$ws_char|$re_comment)+([0-9]+)(?:$ws_char|$re_comment)+obj/s) {
870 # Object
871 104 my $obj;
872 104 my $num = $1 + 0; # ensure seen as decimal value
873 104 $value = $2 + 0;
874 104 $str =~ s/^([0-9]+)(?:$ws_char|$re_comment)+([0-9]+)(?:$ws_char|$re_comment)+obj//s;
875 104 ($obj, $str) = $self->readval($str, %opts);
876 104 ▶ if ($result = $self->test_obj($num, $value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result = $self->test_obj($num, $value)) { } 90 14 104
877 90 $result->merge($obj);
878 } else {
879 14 $result = $obj;
880 14 $self->add_obj($result, $num, $value);
881 14 $result->{' realised'} = 1;
882 }
883 104 ▶ $str = update($fh, $str) if $update; # thanks to kundrat@kundrat.sk
Branch: 1 branch
BranchTrueFalseTotal
if $update 100 4 104
884 104 $str =~ s/^endobj//;
885  
886 } elsif ($str =~ m|^/($reg_char*)|s) {
887 # Name
888 577 $value = $1;
889 577 $str =~ s|^/($reg_char*)||s;
890 577 $result = PDF::Builder::Basic::PDF::Name->from_pdf($value, $self);
891  
892 } elsif ($str =~ m/^\(/) {
893 # Literal String
894 # We now need to find an unbalanced, unescaped right-paren.
895 # This can't be done with a regex.
896 2 my $value = '(';
897 2 $str = substr($str, 1);
898  
899 2 my $nested_level = 1;
900 2 while (1) {
901 # Ignore everything up to the first escaped or parenthesis character
902 2 ▶ if ($str =~ /^([^\\()]+)(.*)/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^([^\\()]+)(.*)/s) 2 0 2
903 2 $value .= $1;
904 2 $str = $2;
905 }
906  
907 # Ignore escaped parentheses
908 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
909 0 $value .= $1;
910 0 $str = substr($str, 2);
911  
912 } elsif ($str =~ /^\(/) {
913 # Left parenthesis: increase nesting
914 0 $value .= '(';
915 0 $str = substr($str, 1);
916 0 $nested_level++;
917  
918 } elsif ($str =~ /^\)/) {
919 # Right parenthesis: decrease nesting
920 2 $value .= ')';
921 2 $str = substr($str, 1);
922 2 $nested_level--;
923 2 ▶ last unless $nested_level;
Branch: 1 branch
BranchTrueFalseTotal
unless $nested_level 2 0 2
924  
925 } elsif ($str =~ /^(\\[^()])/) {
926 # Other escaped character
927 0 $value .= $1;
928 0 $str = substr($str, 2);
929  
930 } else {
931 # If there wasn't an escaped or parenthesis character,
932 # read some more.
933  
934 # We don't use update because we don't want to remove
935 # whitespace or comments.
936 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
937 }
938 } # end while(TRUE) loop
939  
940 2 $result = PDF::Builder::Basic::PDF::String->from_pdf($value);
941 # end Literal String check
942  
943 } elsif ($str =~ m/^</) {
944 # Hex String
945 0 $str =~ s/^<//;
946 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
947 0 ($value, $str) = ($str =~ /^(.*?)>(.*)/s);
948 0 $result = PDF::Builder::Basic::PDF::String->from_pdf('<' . $value . '>');
949  
950 } elsif ($str =~ m/^\[/) {
951 # Array
952 98 $str =~ s/^\[//;
953 98 ▶ $str = update($fh, $str) if $update;
Branch: 1 branch
BranchTrueFalseTotal
if $update 98 0 98
954 98 $result = PDFArray();
955 98 ▶ while ($str !~ m/^\]/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($str =~ /^\]/) 782 98 880
956 782 $str =~ s/^$ws_char+//; # Ignore initial white space
957 782 $str =~ s/^\%[^\015\012]*$ws_char+//; # Ignore comments
958  
959 782 ($value, $str) = $self->readval($str, %opts);
960 782 $result->add_elements($value);
961 782 ▶ $str = update($fh, $str) if $update; # str might just be exhausted!
Branch: 1 branch
BranchTrueFalseTotal
if $update 782 0 782
962 }
963 98 $str =~ s/^\]//;
964  
965 } elsif ($str =~ m/^(true|false)($irreg_char|$)/) {
966 # Boolean
967 0 $value = $1;
968 0 $str =~ s/^(?:true|false)//;
969 0 $result = PDF::Builder::Basic::PDF::Bool->from_pdf($value);
970  
971 } elsif ($str =~ m/^([+-.0-9]+)($irreg_char|$)/) {
972 # Number
973 370 $value = $1;
974 370 $str =~ s/^([+-.0-9]+)//;
975  
976 # If $str only consists of whitespace (or is empty), call update to
977 # see if this is the beginning of an indirect object or reference
978 370 ▶ 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 364 370
Condition100%6/6
$update and $str =~ /^$re_whitespace*$/s || $str =~ /^$re_whitespace+[0-9]+$re_whitespace*$/s
!ll&&!rl&&r
23626
$str =~ /^$re_whitespace*$/s or $str =~ /^$re_whitespace+[0-9]+$re_whitespace*$/s
l!l&&r!l&&!r
42362
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
979 6 $str =~ s/^$re_whitespace+/ /s;
980 6 $str =~ s/$re_whitespace+$/ /s;
981 6 $str = update($fh, $str);
982 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
983 4 return $self->readval("$value $str", %opts);
984 }
985 }
986  
987 366 $result = PDF::Builder::Basic::PDF::Number->from_pdf($value);
988  
989 } elsif ($str =~ m/^null($irreg_char|$)/) {
990 # Null
991 0 $str =~ s/^null//;
992 0 $result = PDF::Builder::Basic::PDF::Null->new();
993  
994 } else {
995 0 die "Can't parse `$str' near " . ($fh->tell()) . " length " . length($str) . ".";
996 }
997  
998 1475 $str =~ s/^$ws_char+//s;
999 1475 return ($result, $str);
1000 } # end of readval()
1001  
1002 =head2 read_obj
1003  
1004 $ref = $p->read_obj($objind, %opts)
1005  
1006 =over
1007  
1008 Given an indirect object reference, locate it and read the object returning
1009 the read-in object.
1010  
1011 =back
1012  
1013 =cut
1014  
1015 86 ▶sub read_obj {
Subroutine read_obj: called CC=3 SCAR=11.4
Pod documented
1016 86 my ($self, $objind, %opts) = @_;
1017  
1018 86 ▶ 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
860
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
1019 86 ▶ $objind->merge($res) unless $objind eq $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $objind eq $res 0 86 86
1020  
1021 86 return $objind;
1022 }
1023  
1024 =head2 read_objnum
1025  
1026 $ref = $p->read_objnum($num, $gen, %opts)
1027  
1028 =over
1029  
1030 Returns a fully read object of given number and generation in this file
1031  
1032 =back
1033  
1034 =cut
1035  
1036 94 ▶sub read_objnum {
Subroutine read_objnum: called CC=14 SCAR=28.3
Pod documented
1037 94 my ($self, $num, $gen, %opts) = @_;
1038  
1039 94 ▶ croak 'Undefined object number in call to read_objnum($num, $gen)' unless defined $num;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $num 0 94 94
1040 94 ▶ croak 'Undefined object generation in call to read_objnum($num, $gen)' unless defined $gen;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $gen 0 94 94
1041 94 ▶ croak "Invalid object number '$num' in call to read_objnum" unless $num =~ /^[0-9]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $num =~ /^[0-9]+$/ 0 94 94
1042 94 ▶ croak "Invalid object generation '$gen' in call to read_objnum" unless $gen =~ /^[0-9]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $gen =~ /^[0-9]+$/ 0 94 94
1043  
1044 94 ▶ my $object_location = $self->locate_obj($num, $gen) || return;
Condition50%1/2
$self->locate_obj($num, $gen) || (return)
l!l
940
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
1045 94 my $object;
1046  
1047 # Compressed object
1048 94 ▶ if (ref($object_location)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $object_location) 4 90 94
1049 4 my ($object_stream_num, $object_stream_pos) = @{$object_location};
1050  
1051 4 my $object_stream = $self->read_objnum($object_stream_num, 0, %opts);
1052 4 ▶ die 'Cannot find the compressed object stream' unless $object_stream;
Branch: 1 branch
BranchTrueFalseTotal
unless $object_stream 0 4 4
1053  
1054 4 ▶ $object_stream->read_stream() if $object_stream->{' nofilt'};
Branch: 1 branch
BranchTrueFalseTotal
if $object_stream->{" nofilt"} 4 0 4
1055  
1056 # An object stream starts with pairs of integers containing object numbers and
1057 # stream offsets relative to the First key
1058 4 my $fh;
1059 my $pairs;
1060 4 ▶ unless ($object_stream->{' streamfile'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object_stream->{' streamfile'}) { } 0 4 4
1061 4 $pairs = substr($object_stream->{' stream'}, 0, $object_stream->{'First'}->val());
1062 } else {
1063 0 CORE::open($fh, '<', $object_stream->{' streamfile'});
1064 0 read($fh, $pairs, $object_stream->{'First'}->val());
1065 }
1066 4 my @map = split /\s+/, $pairs;
1067  
1068 # Find the offset of the object in the stream
1069 4 my $index = $object_stream_pos * 2;
1070 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
1071 4 my $start = $map[$index + 1];
1072  
1073 # Unless this is the last object in the stream, its length is
1074 # determined by the offset of the next object.
1075 4 my $last_object_in_stream = $map[-2];
1076 4 my $length;
1077 4 ▶ if ($last_object_in_stream == $num) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_object_in_stream == $num) { } 2 2 4
1078 2 ▶ if ($object_stream->{' stream'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object_stream->{' stream'}) { } 2 0 2
1079 2 $length = length($object_stream->{' stream'}) - $object_stream->{'First'}->val() - $start;
1080 } else {
1081 0 $length = (-s $object_stream->{' streamfile'}) - $object_stream->{'First'}->val() - $start;
1082 }
1083 } else {
1084 2 my $next_start = $map[$index + 3];
1085 2 $length = $next_start - $start;
1086 }
1087  
1088 # Read the object from the stream
1089 4 my $stream = "$num 0 obj ";
1090 4 ▶ unless ($object_stream->{' streamfile'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object_stream->{' streamfile'}) { } 0 4 4
1091 4 $stream .= substr($object_stream->{' stream'}, $object_stream->{'First'}->val() + $start, $length);
1092 } else {
1093 0 seek($fh, $object_stream->{'First'}->val() + $start, 0);
1094 0 read($fh, $stream, $length, length($stream));
1095 0 close $fh;
1096 }
1097  
1098 4 ($object) = $self->readval($stream, %opts, update => 0);
1099 4 return $object;
1100 }
1101  
1102 90 my $current_location = $self->{' INFILE'}->tell();
1103 90 $self->{' INFILE'}->seek($object_location, 0);
1104 90 ($object) = $self->readval('', %opts);
1105 90 $self->{' INFILE'}->seek($current_location, 0);
1106  
1107 90 return $object;
1108 } # end of read_objnum()
1109  
1110 =head2 new_obj
1111  
1112 $objind = $p->new_obj($obj)
1113  
1114 =over
1115  
1116 Creates a new, free object reference based on free space in the cross reference
1117 chain. If nothing is free, then think up a new number. If C<$obj>, then turns
1118 that object into this new object rather than returning a new object.
1119  
1120 =back
1121  
1122 =cut
1123  
1124 1399 ▶sub new_obj {
Subroutine new_obj: called CC=12 SCAR=38.3
Pod documented
1125 1399 my ($self, $base) = @_;
1126 1399 my $res;
1127  
1128 1399 ▶ if (defined $self->{' free'} and scalar @{$self->{' free'}} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{" free"} and scalar @{$self->{" free"};} > 0) 0 1399 1399
Condition66%2/3
defined $self->{" free"} and scalar @{$self->{" free"};} > 0
!ll&&!rl&&r
1385140
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
1129 0 $res = shift(@{$self->{' free'}});
1130 0 ▶ if (defined $base) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $base) { } 0 0 0
1131 0 my ($num, $gen) = @{$self->{' objects'}{$res->uid()}};
1132 0 $self->remove_obj($res);
1133 0 $self->add_obj($base, $num, $gen);
1134 0 return $self->out_obj($base);
1135 } else {
1136 0 $self->{' objects'}{$res->uid()}[2] = 0;
1137 0 return $res;
1138 }
1139 }
1140  
1141 1399 my $tdict = $self;
1142 1399 my $i;
1143 1399 ▶ while (defined $tdict) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tdict) 1400 1399 2799
1144 1400 ▶ $i = $tdict->{' xref'}{defined($i) ? $i : ''}[0];
Branch: 1 branch
BranchTrueFalseTotal
defined $i ? : 0 1400 1400
1145 1400 ▶ while (defined $i and $i != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $i and $i != 0) 0 1400 1400
Condition33%1/3
defined $i and $i != 0
!ll&&!rl&&r
140000
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
1146 0 my ($ni, $ng) = @{$tdict->{' xref'}{$i}};
1147 0 ▶ unless (defined $self->locate_obj($i, $ng)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->locate_obj($i, $ng)) 0 0 0
1148 0 ▶ if (defined $base) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $base) { } 0 0 0
1149 0 $self->add_obj($base, $i, $ng);
1150 0 return $base;
1151 } else {
1152 0 ▶ $res = $self->test_obj($i, $ng) || $self->add_obj(PDF::Builder::Basic::PDF::Objind->new(), $i, $ng);
Condition0%0/3
$self->test_obj($i, $ng) || $self->add_obj('PDF::Builder::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::Builder::Basic::PDF::Objind'->new, $i, $ng)
$self->test_obj($i, $ng) $self->add_obj('PDF::Builder::Basic::PDF::Objind'->new, $i, $ng)
Truth table0%0/3
A || B
A= $self->test_obj($i, $ng)B= $self->add_obj('PDF::Builder::Basic::PDF::Objind'->new, $i, $ng)
ABresult
1X1
011
000
1153 0 $self->out_obj($res);
1154 0 return $res;
1155 }
1156 }
1157 0 $i = $ni;
1158 }
1159 1400 $tdict = $tdict->{' prev'};
1160 }
1161  
1162 1399 $i = $self->{' maxobj'}++;
1163 1399 ▶ if (defined $base) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $base) { } 1399 0 1399
1164 1399 $self->add_obj($base, $i, 0);
1165 1399 $self->out_obj($base);
1166 1399 return $base;
1167 } else {
1168 0 $res = $self->add_obj(PDF::Builder::Basic::PDF::Objind->new(), $i, 0);
1169 0 $self->out_obj($res);
1170 0 return $res;
1171 }
1172 }
1173  
1174 =head2 out_obj
1175  
1176 $p->out_obj($obj)
1177  
1178 =over
1179  
1180 Indicates that the given object reference should appear in the output xref
1181 table whether with data or freed.
1182  
1183 =back
1184  
1185 =cut
1186  
1187 3053 ▶sub out_obj {
Subroutine out_obj: called CC=2 SCAR=6.9
Pod documented
1188 3053 my ($self, $obj) = @_;
1189  
1190 # This is why we've been keeping the outlist CACHE around; to speed
1191 # up this method by orders of magnitude (it saves up from having to
1192 # grep the full outlist each time through as we'll just do a lookup
1193 # in the hash) (which is super-fast).
1194 3053 ▶ unless (exists $self->{' outlist_cache'}{$obj}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{" outlist_cache"}{$obj}) 1416 1637 3053
1195 1416 push @{$self->{' outlist'}}, $obj;
1196 # weaken $self->{' outlist'}->[-1];
1197 1416 $self->{' outlist_cache'}{$obj} = 1;
1198 }
1199  
1200 3053 return $obj;
1201 }
1202  
1203 =head2 free_obj
1204  
1205 $p->free_obj($obj)
1206  
1207 =over
1208  
1209 Marks an object reference for output as being freed.
1210  
1211 =back
1212  
1213 =cut
1214  
1215 0 ▶sub free_obj {
Subroutine free_obj: not called CC=1 SCAR=6.9
Pod documented
1216 0 my ($self, $obj) = @_;
1217  
1218 0 push @{$self->{' free'}}, $obj;
1219 0 $self->{' objects'}{$obj->uid()}[2] = 1;
1220 0 $self->out_obj($obj);
1221  
1222 0 return;
1223 }
1224  
1225 =head2 remove_obj
1226  
1227 $p->remove_obj($objind)
1228  
1229 =over
1230  
1231 Removes the object from all places where we might remember it.
1232  
1233 =back
1234  
1235 =cut
1236  
1237 0 ▶sub remove_obj {
Subroutine remove_obj: not called CC=2 SCAR=17.9
Pod documented
1238 0 my ($self, $objind) = @_;
1239  
1240 # who says it has to be fast
1241 0 delete $self->{' objects'}{$objind->uid()};
1242 0 delete $self->{' outlist_cache'}{$objind};
1243 0 delete $self->{' printed_cache'}{$objind};
1244 0 @{$self->{' outlist'}} = grep { $_ ne $objind } @{$self->{' outlist'}};
1245 0 @{$self->{' printed'}} = grep { $_ ne $objind } @{$self->{' printed'}};
1246 $self->{' objcache'}{$objind->{' objnum'}, $objind->{' objgen'}} = undef
1247 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
1248  
1249 0 return $self;
1250 }
1251  
1252 =head2 ship_out
1253  
1254 $p->ship_out(@objects)
1255  
1256 $p->ship_out()
1257  
1258 =over
1259  
1260 Ships the given objects (or all objects for output if C<@objects> is empty) to
1261 the currently open output file (assuming there is one). Freed objects are not
1262 shipped, and once an object is shipped it is switched such that this file
1263 becomes its source and it will not be shipped again unless out_obj is called
1264 again. Notice that a shipped out object can be re-output or even freed, but
1265 that it will not cause the data already output to be changed.
1266  
1267 =back
1268  
1269 =cut
1270  
1271 183 ▶sub ship_out {
Subroutine ship_out: called CC=11 SCAR=25.0
Pod documented
1272 183 my ($self, @objects) = @_;
1273  
1274 183 ▶ return unless defined $self->{' OUTFILE'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{" OUTFILE"} 0 183 183
1275 183 my $fh = $self->{' OUTFILE'};
1276 183 seek($fh, 0, 2); # go to the end of the file
1277  
1278 183 ▶ @objects = @{$self->{' outlist'}} unless scalar @objects > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @objects > 0 183 0 183
1279 183 foreach my $objind (@objects) {
1280 1066 ▶ next unless $objind->is_obj($self);
Branch: 1 branch
BranchTrueFalseTotal
unless $objind->is_obj($self) 0 1066 1066
1281 1066 my $j = -1;
1282 1066 ▶ for (my $i = 0; $i < scalar @{$self->{' outlist'}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$self->{" outlist"};}) 1066 0 1066
1283 1066 ▶ if ($self->{' outlist'}[$i] eq $objind) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{" outlist"}[$i] eq $objind) 1066 0 1066
1284 1066 $j = $i;
1285 1066 last;
1286 }
1287 }
1288 1066 ▶ next if $j < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $j < 0 0 1066 1066
1289 1066 splice(@{$self->{' outlist'}}, $j, 1);
1290 1066 delete $self->{' outlist_cache'}{$objind};
1291 0 ▶ next if grep { $_ eq $objind } @{$self->{' free'}};
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $objind;} @{$$self{" free"};} 0 1066 1066
1292  
1293 0 ▶ map { $fh->print("\% $_ \n") } split(/$cr/, $objind->{' comments'}) if $objind->{' comments'};
Branch: 1 branch
BranchTrueFalseTotal
if $objind->{" comments"} 0 1066 1066
1294 1066 $self->{' locs'}{$objind->uid()} = $fh->tell();
1295 1066 my ($objnum, $objgen) = @{$self->{' objects'}{$objind->uid()}}[0..1];
1296 1066 $fh->printf("%d %d obj\n", $objnum, $objgen);
1297 1066 $objind->outobjdeep($fh, $self);
1298 1066 $fh->print("\nendobj\n");
1299  
1300 # Note that we've output this obj, not forgetting to update
1301 # the cache of what's printed.
1302 1066 ▶ unless (exists $self->{' printed_cache'}{$objind}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{" printed_cache"}{$objind}) 1066 0 1066
1303 1066 push @{$self->{' printed'}}, $objind;
1304 1066 $self->{' printed_cache'}{$objind}++;
1305 }
1306 }
1307  
1308 183 return $self;
1309 } # end of ship_out()
1310  
1311 =head2 copy
1312  
1313 $p->copy($outpdf, \&filter)
1314  
1315 =over
1316  
1317 Iterates over every object in the file reading the object, calling C<filter>
1318 with the object, and outputting the result. If C<filter> is not defined,
1319 just copies input to output.
1320  
1321 =back
1322  
1323 =cut
1324  
1325 0 ▶sub copy {
Subroutine copy: not called CC=14 SCAR=53.5
Pod documented
1326 0 my ($self, $outpdf, $filter) = @_;
1327 0 my ($obj, $minl, $mini, $ming);
1328  
1329 0 foreach my $key (grep { not m/^[\s\-]/ } keys %$self) {
1330 0 ▶ $outpdf->{$key} = $self->{$key} unless defined $outpdf->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $outpdf->{$key} 0 0 0
1331 }
1332  
1333 0 my $tdict = $self;
1334 0 ▶ while (defined $tdict) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tdict) 0 0 0
1335 0 foreach my $i (sort {$a <=> $b} keys %{$tdict->{' xref'}}) {
1336 0 my ($nl, $ng, $nt) = @{$tdict->{' xref'}{$i}};
1337 0 ▶ next unless $nt eq 'n';
Branch: 1 branch
BranchTrueFalseTotal
unless $nt eq "n" 0 0 0
1338  
1339 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
1340 0 $mini = $i;
1341 0 $ming = $ng;
1342 0 $minl = $nl;
1343 }
1344 0 ▶ unless ($obj = $self->test_obj($i, $ng)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj = $self->test_obj($i, $ng)) 0 0 0
1345 0 $obj = PDF::Builder::Basic::PDF::Objind->new();
1346 0 $obj->{' objnum'} = $i;
1347 0 $obj->{' objgen'} = $ng;
1348 0 $self->add_obj($obj, $i, $ng);
1349 0 $obj->{' parent'} = $self;
1350 0 weaken $obj->{' parent'};
1351 0 $obj->{' realised'} = 0;
1352 }
1353 0 $obj->realise();
1354 0 ▶ my $res = defined $filter ? &{$filter}($obj) : $obj;
Branch: 1 branch
BranchTrueFalseTotal
defined $filter ? : 0 0 0
1355 0 ▶ $outpdf->new_obj($res) unless (!$res || $res->is_obj($outpdf));
Branch: 1 branch
BranchTrueFalseTotal
unless not $res or $res->is_obj($outpdf) 0 0 0
Condition0%0/3
not $res or $res->is_obj($outpdf)
l!l&&r!l&&!r
000
MC/DC0%0/2
not $res or $res->is_obj($outpdf)
not $res $res->is_obj($outpdf)
Truth table0%0/3
not(A) or B
A= not $resB= $res->is_obj($outpdf)
ABresult
1X1
011
000
1356 }
1357 0 $tdict = $tdict->{' prev'};
1358 }
1359  
1360 # test for linearized and remove it from output
1361 0 $obj = $self->test_obj($mini, $ming);
1362 0 ▶ if ($obj->isa('PDF::Builder::Basic::PDF::Dict') && $obj->{'Linearized'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->isa("PDF::Builder::Basic::PDF::Dict") and $obj->{"Linearized"}) 0 0 0
Condition0%0/3
$obj->isa("PDF::Builder::Basic::PDF::Dict") and $obj->{"Linearized"}
!ll&&!rl&&r
000
MC/DC0%0/2
$obj->isa("PDF::Builder::Basic::PDF::Dict") and $obj->{"Linearized"}
$obj->isa("PDF::Builder::Basic::PDF::Dict") $obj->{"Linearized"}
Truth table0%0/3
A and B
A= $obj->isa("PDF::Builder::Basic::PDF::Dict")B= $obj->{"Linearized"}
ABresult
0X0
100
111
1363 0 $outpdf->free_obj($obj);
1364 }
1365  
1366 0 return $self;
1367 } # end of copy()
1368  
1369 =head1 PRIVATE METHODS & FUNCTIONS
1370  
1371 The following methods and functions are considered B<private> to this class.
1372 This does not mean you cannot use them if you have a need, just that they
1373 aren't really designed for users of this class.
1374  
1375 =head2 locate_obj
1376  
1377 $offset = $p->locate_obj($num, $gen)
1378  
1379 =over
1380  
1381 Returns a file offset to the object asked for by following the chain of cross
1382 reference tables until it finds the one you want.
1383  
1384 =back
1385  
1386 =cut
1387  
1388 94 ▶sub locate_obj {
Subroutine locate_obj: called CC=7 SCAR=20.6
Pod documented
1389 94 my ($self, $num, $gen) = @_;
1390  
1391 94 my $tdict = $self;
1392 94 my $seen = {};
1393 94 ▶ while (defined $tdict) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tdict) 103 0 103
1394 103 $seen->{$tdict->{' loc'}} = 1;
1395 103 ▶ if (ref $tdict->{' xref'}{$num}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tdict->{" xref"}{$num}) 94 9 103
1396 94 my $ref = $tdict->{' xref'}{$num};
1397 94 ▶ return $ref unless scalar(@$ref) == 3;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$ref == 3 4 90 94
1398  
1399 90 ▶ if ($ref->[1] == $gen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->[1] == $gen) 90 0 90
1400 90 ▶ return $ref->[0] if $ref->[2] eq 'n';
Branch: 1 branch
BranchTrueFalseTotal
if $ref->[2] eq "n" 90 0 90
1401 0 return; # if $ref->[2] eq 'f';
1402 }
1403 }
1404 9 $tdict = $tdict->{' prev'};
1405 9 ▶ if ($seen->{$tdict->{' loc'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$seen{$tdict->{" loc"}}) 0 9 9
1406 0 die "Malformed PDF: trailer contains a loop or repeated object ID";
1407 }
1408 }
1409  
1410 0 return;
1411 }
1412  
1413 =head2 update
1414  
1415 update($fh, $str, $instream)
1416  
1417 =over
1418  
1419 Keeps reading C<$fh> for more data to ensure that C<$str> has at least a line
1420 full for C<readval> to work on. At this point we also take the opportunity to
1421 ignore comments.
1422  
1423 =back
1424  
1425 =cut
1426  
1427 3262 ▶sub update {
Subroutine update: called CC=13 SCAR=28.7
Pod documented
1428 3262 my ($fh, $str, $instream) = @_;
1429  
1430 3262 ▶ print STDERR 'fpos=' . tell($fh) . ' strlen=' . length($str) . "\n" if $readDebug;
Branch: 1 branch
BranchTrueFalseTotal
if $readDebug 0 3262 3262
1431 3262 ▶ if ($instream) {
Branch: 1 branch
BranchTrueFalseTotal
if ($instream) { } 11 3251 3262
1432 # we are inside a (possible binary) stream
1433 # so we fetch data till we see an 'endstream'
1434 # -- fredo/2004-09-03
1435 11 ▶ while ($str !~ m/endstream/ and not $fh->eof()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($str =~ /endstream/ or $fh->eof) 0 11 11
Condition33%1/3
$str =~ /endstream/ or $fh->eof
l!l&&r!l&&!r
1100
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
1436 0 ▶ print STDERR 'fpos=' . tell($fh) . ' strlen=' . length($str) . "\n" if $readDebug;
Branch: 1 branch
BranchTrueFalseTotal
if $readDebug 0 0 0
1437 0 $fh->read($str, 314, length($str));
1438 }
1439 } else {
1440 3251 $str =~ s/^$ws_char*//;
1441 3251 ▶ while ($str !~ m/$cr/ and not $fh->eof()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($str =~ /$PDF::Builder::Basic::PDF::File::cr/ or $fh->eof) 128 3251 3379
Condition100%3/3
$str =~ /$PDF::Builder::Basic::PDF::File::cr/ or $fh->eof
l!l&&r!l&&!r
320942128
MC/DC100%2/2
$str =~ /$PDF::Builder::Basic::PDF::File::cr/ or $fh->eof
$str =~ /$PDF::Builder::Basic::PDF::File::cr/ $fh->eof
Truth table100%3/3
A or B
A= $str =~ /$PDF::Builder::Basic::PDF::File::cr/B= $fh->eof
ABresult
1X1
011
000
1442 128 ▶ print STDERR 'fpos=' . tell($fh) . ' strlen=' . length($str) . "\n" if $readDebug;
Branch: 1 branch
BranchTrueFalseTotal
if $readDebug 0 128 128
1443 128 $fh->read($str, 314, length($str));
1444 128 $str =~ s/^$ws_char*//so;
1445 }
1446 3251 ▶ while ($str =~ m/^\%/) { # restructured by fredo/2003-03-23
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^\%/) 1 3251 3252
1447 1 ▶ print STDERR 'fpos=' . tell($fh) . ' strlen=' . length($str) . "\n" if $readDebug;
Branch: 1 branch
BranchTrueFalseTotal
if $readDebug 0 1 1
1448 1 ▶ $fh->read($str, 314, length($str)) while ($str !~ m/$cr/ and not $fh->eof());
Branch: 1 branch
BranchTrueFalseTotal
$str =~ /$PDF::Builder::Basic::PDF::File::cr/ or $fh->eof or do { $fh->read($str, 314, length $str) } 1 0 1
Condition33%1/3
$str =~ /$PDF::Builder::Basic::PDF::File::cr/ or $fh->eof
l!l&&r!l&&!r
100
MC/DC0%0/2
$str =~ /$PDF::Builder::Basic::PDF::File::cr/ or $fh->eof
$str =~ /$PDF::Builder::Basic::PDF::File::cr/ $fh->eof
Truth table33%1/3
A or B
A= $str =~ /$PDF::Builder::Basic::PDF::File::cr/B= $fh->eof
ABresult
1X1
011
000
1449 1 $str =~ s/^\%[^\015\012]*$ws_char*//so; # fixed for reportlab -- fredo
1450 }
1451 }
1452  
1453 3262 return $str;
1454 } # end of update()
1455  
1456 =head2 test_obj
1457  
1458 $objind = $p->test_obj($num, $gen)
1459  
1460 =over
1461  
1462 Tests the cache to see whether an object reference (which may or may not have
1463 been getobj()ed) has been cached. Returns it if it has.
1464  
1465 =back
1466  
1467 =cut
1468  
1469 270 ▶sub test_obj {
Subroutine test_obj: called CC=1 SCAR=0.0
Pod documented
1470 270 my ($self, $num, $gen) = @_;
1471  
1472 270 return $self->{' objcache'}{$num, $gen};
1473 }
1474  
1475 =head2 add_obj
1476  
1477 $p->add_obj($objind)
1478  
1479 =over
1480  
1481 Adds the given object to the internal object cache.
1482  
1483 =back
1484  
1485 =cut
1486  
1487 1544 ▶sub add_obj {
Subroutine add_obj: called CC=1 SCAR=0.0
Pod documented
1488 1544 my ($self, $obj, $num, $gen) = @_;
1489  
1490 1544 $self->{' objcache'}{$num, $gen} = $obj;
1491 1544 $self->{' objects'}{$obj->uid()} = [$num, $gen];
1492 # weaken $self->{' objcache'}{$num, $gen};
1493 1544 return $obj;
1494 }
1495  
1496 =head2 readxrtr
1497  
1498 $tdict = $p->readxrtr($xpos, %options)
1499  
1500 =over
1501  
1502 Recursive function which reads each of the cross-reference and trailer tables
1503 in turn until there are no more.
1504  
1505 Returns a dictionary corresponding to the trailer chain. Each trailer also
1506 includes the corresponding cross-reference table.
1507  
1508 The structure of the xref private element in a trailer dictionary is of an
1509 anonymous hash of cross reference elements by object number. Each element
1510 consists of an array of 3 elements corresponding to the three elements read
1511 in [location, generation number, free or used]. See the PDF specification
1512 for details.
1513  
1514 See C<open> for options allowed.
1515  
1516 =back
1517  
1518 =cut
1519  
1520 78 ▶sub _unpack_xref_stream {
Subroutine _unpack_xref_stream: called CC=10 SCAR=37.7
1521 78 my ($self, $width, $data) = @_;
1522  
1523 # handle some oddball cases
1524 78 ▶ if ($width == 3) {
Branch: 4 branches
BranchTrueFalseTotal
if ($width == 3) { } 0 78 78
elsif ($width == 5) { } 0 78 78
elsif ($width == 6) { } 0 78 78
elsif ($width == 7) { } 0 78 78
1525 0 $data = "\x00$data";
1526 0 $width = 4;
1527 } elsif ($width == 5) {
1528 0 $data = "\x00\x00\x00$data";
1529 0 $width = 8;
1530 } elsif ($width == 6) {
1531 0 $data = "\x00\x00$data";
1532 0 $width = 8;
1533 } elsif ($width == 7) {
1534 0 $data = "\x00$data";
1535 0 $width = 8;
1536 }
1537 # in all cases, "Network" (Big-Endian) byte order assumed
1538 78 ▶ return unpack('C', $data) if $width == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $width == 1 26 52 78
1539 52 ▶ return unpack('n', $data) if $width == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $width == 2 52 0 52
1540 0 ▶ return unpack('N', $data) if $width == 4;
Branch: 1 branch
BranchTrueFalseTotal
if $width == 4 0 0 0
1541 0 ▶ if ($width == 8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($width == 8) 0 0 0
1542 # Some ways other packages handle this, without Perl-64, according
1543 # to Vadim Repin. Possibly they end up converting the value to
1544 # "double" behind the scenes if on a 32-bit platform?
1545 # PDF::Tiny return hex unpack('H16', $data);
1546 # CAM::PDF my @b = unpack('C*', $data);
1547 # my $i=0; ($i <<= 8) += shift @b while @b; return $i;
1548
1549 0 ▶ if (substr($data, 0, 4) eq "\x00\x00\x00\x00") {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($data, 0, 4) eq "\000\000\000\000") { } 0 0 0
1550 # can treat as 32 bit unsigned int
1551 0 return unpack('N', substr($data, 4, 4));
1552 } else {
1553 # requires 64-bit platform (chip and Perl), else fatal error
1554 # it may blow up and produce a smoking crater if 32-bit Perl!
1555 # also note that Q needs Big-Endian flag (>) specified, else
1556 # it will use the native chip order (Big- or Little- Endian)
1557 0 return unpack('Q>', $data);
1558 }
1559 }
1560  
1561 0 die "Unsupported field width: $width. 1-8 supported.";
1562 }
1563  
1564 21 ▶sub readxrtr {
Subroutine readxrtr: called CC=75 SCAR=64.0
Pod documented
1565 21 my ($self, $xpos, %options) = @_;
1566 # $xpos SHOULD be pointing to "xref" keyword
1567 # UNLESS an xref stream is in use (v 1.5+)
1568 # copy dashed option names to preferred undashed names
1569 0 ▶ if (defined $options{'-diags'} && !defined $options{'diags'}) { $options{'diags'} = delete($options{'-diags'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"-diags"} and not defined $options{"diags"}) 0 21 21
Condition33%1/3
defined $options{"-diags"} and not defined $options{"diags"}
!ll&&!rl&&r
2100
MC/DC0%0/2
defined $options{"-diags"} and not defined $options{"diags"}
defined $options{"-diags"} not defined $options{"diags"}
Truth table33%1/3
A and not(B)
A= defined $options{"-diags"}B= not defined $options{"diags"}
ABresult
0X0
100
111
1570  
1571 21 my ($tdict, $buf, $xmin, $xnum, $xdiff);
1572  
1573 21 my $fh = $self->{' INFILE'};
1574 21 $fh->seek($xpos, 0);
1575 21 $fh->read($buf, 22); # 22 should overlap into first subsection
1576 21 $buf = update($fh, $buf); # fix for broken JAWS xref calculation.
1577  
1578 21 my $xlist = {};
1579  
1580 ## it seems that some products calculate wrong prev entries (short)
1581 ## so we seek ahead to find one -- fredo; save for now
1582 #while ($buf !~ m/^xref$cr/i && !eof($fh)) {
1583 # $buf =~ s/^(\s+|\S+|.)//i;
1584 # $buf = update($fh, $buf);
1585 #}
1586  
1587 21 ▶ if ($buf =~ s/^xref$cr//i) { # xref table, remove xrefEOL from buffer
Branch: 2 branches
BranchTrueFalseTotal
if ($buf =~ s/^xref$PDF::Builder::Basic::PDF::File::cr//i) { } 18 3 21
elsif ($buf =~ /^(\d+)\s+(\d+)\s+obj/i) { } 3 0 3
1588 # Plain XRef tables.
1589 #
1590 # look to match startobj# count# EOL of first (or only) subsection
1591 # supposed to be single ASCII space between numbers, but this is
1592 # more lenient for some writers, allowing 1 or more whitespace
1593 18 my $subsection_count = 0;
1594 18 my $entry_format_error = 0;
1595 18 my $xrefListEmpty = 0;
1596  
1597 18 ▶ while ($buf =~ m/^$ws_char*([0-9]+)$ws_char+([0-9]+)$ws_char*$cr(.*?)$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf =~ /^$PDF::Builder::Basic::PDF::File::ws_char*([0-9]+)$PDF::Builder::Basic::PDF::File::ws_char+([0-9]+)$PDF::Builder::Basic::PDF::File::ws_char*$PDF::Builder::Basic::PDF::File::cr(.*?)$/s) 23 18 41
1598 23 my $old_buf = $buf;
1599 23 $xmin = $1; # starting object number of this subsection
1600 23 $xnum = $2; # number of entries in this subsection
1601 23 $buf = $3; # remainder of buffer
1602 23 $subsection_count++;
1603 # go back and warn if other than single space separating numbers
1604 23 ▶ unless ($old_buf =~ /^[0-9]+ [0-9]+$cr/) { #orig 'warn'
Branch: 1 branch
BranchTrueFalseTotal
unless ($old_buf =~ /^[0-9]+ [0-9]+$PDF::Builder::Basic::PDF::File::cr/) 0 23 23
1605 0 ▶ if ($options{'diags'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"diags"} == 1) 0 0 0
1606 # See PDF 1.7 section 7.5.4: Cross-Reference Table
1607 0 warn "Malformed xref: subsection header needs a single\n" .
1608 "ASCII space between the numbers and no extra spaces.\n";
1609 }
1610 }
1611 23 $xdiff = length($buf); # how much remaining in buffer
1612  
1613 # in case xnum == 0 is permitted (or used and tolerated by readers),
1614 # skip over entry reads and go to next subsection
1615 23 ▶ if ($xnum < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xnum < 1) 0 23 23
1616 0 ▶ if ($options{'diags'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"diags"} == 1) 0 0 0
1617 0 warn "Xref subsection has 0 entries. Skipped.\n";
1618 }
1619 0 $xrefListEmpty = 1;
1620 0 next;
1621 }
1622  
1623 # read chunk of entire subsection list
1624 23 my $entry_size = 20;
1625 # test read first entry, see if $cr in expected place, adjust size
1626 23 $fh->read($buf, $entry_size * 1 - $xdiff + 15, $xdiff);
1627 23 ▶ if ($buf =~ m/^(.*?)$cr/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf =~ /^(.*?)$PDF::Builder::Basic::PDF::File::cr/) 23 0 23
1628 23 $entry_size = length($1) + 2;
1629 }
1630 23 ▶ if ($entry_size != 20 && $options{'diags'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry_size != 20 and $options{"diags"} == 1) 0 23 23
Condition33%1/3
$entry_size != 20 and $options{"diags"} == 1
!ll&&!rl&&r
2300
MC/DC0%0/2
$entry_size != 20 and $options{"diags"} == 1
$entry_size != 20 $options{"diags"} == 1
Truth table33%1/3
A and B
A= $entry_size != 20B= $options{"diags"} == 1
ABresult
0X0
100
111
1631 0 warn "Xref entries supposed to be 20 bytes long, are $entry_size.\n";
1632 }
1633 23 $xdiff = length($buf);
1634  
1635 # read remaining entries
1636 23 $fh->read($buf, $entry_size * $xnum - $xdiff + 15, $xdiff);
1637 # each entry is two integers and flag. spec says single ASCII space
1638 # between each field and certain length for each (10, 5, 1), so
1639 # this appears to be more lenient than spec
1640 # is object 0 supposed to be in subsection 1, or is any place OK?
1641 23 ▶ while ($xnum-- > 0 and
Branch: 1 branch
BranchTrueFalseTotal
if ($xnum-- > 0 and $buf =~ /^$PDF::Builder::Basic::PDF::File::ws_char*(\d+)$PDF::Builder::Basic::PDF::File::ws_char+(\d+)$PDF::Builder::Basic::PDF::File::ws_char+([nf])$PDF::Builder::Basic::PDF::File::ws_char*$PDF::Builder::Basic::PDF::File::cr/) 132 23 155
Condition66%2/3
$xnum-- > 0 and $buf =~ /^$PDF::Builder::Basic::PDF::File::ws_char*(\d+)$PDF::Builder::Basic::PDF::File::ws_char+(\d+)$PDF::Builder::Basic::PDF::File::ws_char+([nf])$PDF::Builder::Basic::PDF::File::ws_char*$PDF::Builder::Basic::PDF::File::cr/
!ll&&!rl&&r
230132
MC/DC50%1/2
$xnum-- > 0 and $buf =~ /^$PDF::Builder::Basic::PDF::File::ws_char*(\d+)$PDF::Builder::Basic::PDF::File::ws_char+(\d+)$PDF::Builder::Basic::PDF::File::ws_char+([nf])$PDF::Builder::Basic::PDF::File::ws_char*$PDF::Builder::Basic::PDF::File::cr/
$xnum-- > 0 $buf =~ /^$PDF::Builder::Basic::PDF::File::ws_char*(\d+)$PDF::Builder::Basic::PDF::File::ws_char+(\d+)$PDF::Builder::Basic::PDF::File::ws_char+([nf])$PDF::Builder::Basic::PDF::File::ws_char*$PDF::Builder::Basic::PDF::File::cr/
Truth table66%2/3
A and B
A= $xnum-- > 0B= $buf =~ /^$PDF::Builder::Basic::PDF::File::ws_char*(\d+)$PDF::Builder::Basic::PDF::File::ws_char+(\d+)$PDF::Builder::Basic::PDF::File::ws_char+([nf])$PDF::Builder::Basic::PDF::File::ws_char*$PDF::Builder::Basic::PDF::File::cr/
ABresult
0X0
100
111
1642 $buf =~ m/^$ws_char*(\d+)$ws_char+(\d+)$ws_char+([nf])$ws_char*$cr/) {
1643 # check if format doesn't match spec
1644 132 ▶ if ($buf =~ m/^\d{10} \d{5} [nf]$cr/ ||
Branch: 1 branch
BranchTrueFalseTotal
if ($buf =~ /^\d{10} \d{5} [nf]$PDF::Builder::Basic::PDF::File::cr/ or $entry_format_error) { } 132 0 132
Condition33%1/3
$buf =~ /^\d{10} \d{5} [nf]$PDF::Builder::Basic::PDF::File::cr/ or $entry_format_error
l!l&&r!l&&!r
13200
MC/DC0%0/2
$buf =~ /^\d{10} \d{5} [nf]$PDF::Builder::Basic::PDF::File::cr/ or $entry_format_error
$buf =~ /^\d{10} \d{5} [nf]$PDF::Builder::Basic::PDF::File::cr/ $entry_format_error
Truth table33%1/3
A or B
A= $buf =~ /^\d{10} \d{5} [nf]$PDF::Builder::Basic::PDF::File::cr/B= $entry_format_error
ABresult
1X1
011
000
1645 $entry_format_error) {
1646 # format OK or have already reported format problem
1647 } else {
1648 0 ▶ if ($options{'diags'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"diags"} == 1) 0 0 0
1649 0 warn "Xref entry readable, but doesn't meet PDF spec.\n";
1650 }
1651 0 $entry_format_error++;
1652 }
1653  
1654 132 $buf =~ s/^$ws_char*(\d+)$ws_char+(\d+)$ws_char+([nf])$ws_char*$cr//;
1655 # $1 = object's starting offset in file (n) or
1656 # next object in free list (f) [0 if last]
1657 # $2 = generation number (n) or 65535 for object 0 (f) or
1658 # next generation number (f)
1659 # $3 = flag (n = object in use, f = free)
1660 # buf reduced by entry just processed
1661 132 ▶ if (exists $xlist->{$xmin}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $xlist->{$xmin}) { } 0 132 132
1662 0 ▶ if ($options{'diags'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"diags"} == 1) 0 0 0
1663 0 warn "Duplicate object number $xmin in xref table ignored.\n";
1664 }
1665 } else {
1666 132 $xlist->{$xmin} = [$1, $2, $3];
1667 132 ▶ if ($xmin == 0 && $subsection_count > 1 && $options{'diags'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xmin == 0 and $subsection_count > 1 and $options{"diags"} == 1) 0 132 132
Condition50%3/6
$xmin == 0 and $subsection_count > 1 and $options{"diags"} == 1
!ll&&!rl&&r
13200
$xmin == 0 and $subsection_count > 1
!ll&&!rl&&r
114180
MC/DC0%0/3
$xmin == 0 and $subsection_count > 1 and $options{"diags"} == 1
$xmin == 0 $subsection_count > 1 $options{"diags"} == 1
Truth table50%2/4
A and B and C
A= $xmin == 0B= $subsection_count > 1C= $options{"diags"} == 1
ABCresult
0XX0
10X0
1100
1111
1668 0 warn "Xref object 0 entry not in first subsection.\n";
1669 }
1670 }
1671 132 $xmin++;
1672 } # traverse one subsection for objects xmin through xmin+xnum-1
1673 # go back for next subsection (if any)
1674 } # loop through xref subsections
1675 # fall through to here when run out of xref subsections
1676 # xlist should have two or more object entries, may not be contiguous
1677  
1678 # should have an object 0
1679 # at this point, no idea if object 0 was in first subsection (legal?)
1680 # could attempt a fixup if no object 0 found. many fixups are quite
1681 # risky and could end up corrupting the free list.
1682 # there's no guarantee that a proper free list will result, but any
1683 # error should hopefully be caught further on
1684 18 ▶ if (!exists $xlist->{'0'} && !$xrefListEmpty) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $xlist->{0} or $xrefListEmpty) 0 18 18
Condition33%1/3
exists $xlist->{0} or $xrefListEmpty
l!l&&r!l&&!r
1800
MC/DC0%0/2
exists $xlist->{0} or $xrefListEmpty
exists $xlist->{0} $xrefListEmpty
Truth table33%1/3
A or B
A= exists $xlist->{0}B= $xrefListEmpty
ABresult
1X1
011
000
1685 # for now, 1 subsection starting with 1, and only object 1 in
1686 # free list, try to fix up
1687 0 ▶ if ($subsection_count == 1 && exists $xlist->{'1'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subsection_count == 1 and exists $xlist->{'1'}) { } 0 0 0
Condition0%0/3
$subsection_count == 1 and exists $xlist->{1}
!ll&&!rl&&r
000
MC/DC0%0/2
$subsection_count == 1 and exists $xlist->{1}
$subsection_count == 1 exists $xlist->{1}
Truth table0%0/3
A and B
A= $subsection_count == 1B= exists $xlist->{1}
ABresult
0X0
100
111
1688 # apparently a common enough error in PDF writers
1689  
1690 0 ▶ if ($xlist->{'1'}[0] == 0 && # only member of free list
Branch: 1 branch
BranchTrueFalseTotal
if ($xlist->{'1'}[0] == 0 and $xlist->{'1'}[1] == 65535 and $xlist->{'1'}[2] eq 'f') { } 0 0 0
Condition0%0/6
$xlist->{1}[0] == 0 and $xlist->{1}[1] == 65535 and $xlist->{1}[2] eq "f"
!ll&&!rl&&r
000
$xlist->{1}[0] == 0 and $xlist->{1}[1] == 65535
!ll&&!rl&&r
000
MC/DC0%0/3
$xlist->{1}[0] == 0 and $xlist->{1}[1] == 65535 and $xlist->{1}[2] eq "f"
$xlist->{1}[0] == 0 $xlist->{1}[1] == 65535 $xlist->{1}[2] eq "f"
Truth table0%0/4
A and B and C
A= $xlist->{1}[0] == 0B= $xlist->{1}[1] == 65535C= $xlist->{1}[2] eq "f"
ABCresult
0XX0
10X0
1100
1111
1691 $xlist->{'1'}[1] == 65535 &&
1692 $xlist->{'1'}[2] eq 'f') {
1693 # object 1 appears to be the free list head, so shift
1694 # down all objects
1695 0 ▶ if ($options{'diags'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"diags"} == 1) 0 0 0
1696 0 warn "xref appears to be mislabeled starting with 1. Shift down all elements.\n";
1697 }
1698 0 my $next = 1;
1699 0 ▶ while (exists $xlist->{$next}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $xlist->{$next}) 0 0 0
1700 0 $xlist->{$next - 1} = $xlist->{$next};
1701 0 $next++;
1702 }
1703 0 delete $xlist->{--$next};
1704  
1705 } else {
1706 # if object 1 does not appear to be a free list head,
1707 # insert a new object 0
1708 0 ▶ if ($options{'diags'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"diags"} == 1) 0 0 0
1709 0 warn "Xref appears to be missing object 0. Insert a new one.\n";
1710 }
1711 0 $xlist->{'0'} = [0, 65535, 'f'];
1712 }
1713 } else {
1714 0 ▶ if ($options{'diags'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"diags"} == 1) 0 0 0
1715 0 warn "Malformed cross reference list in PDF file $self->{' fname'} -- no object 0 (free list head)\n";
1716 }
1717 0 $xlist->{'0'} = [0, 65535, 'f'];
1718 }
1719 } # no object 0 entry
1720  
1721 # build/validate the free list (and no active objects have f flag)
1722 18 my @free_list;
1723 18 foreach (sort {$a <=> $b} keys %{ $xlist }) {
1724 # if 'f' flag, is in free list
1725 132 ▶ if ($xlist->{$_}[2] eq 'f') {
Branch: 2 branches
BranchTrueFalseTotal
if ($xlist->{$_}[2] eq 'f') { } 18 114 132
elsif ($xlist->{$_}[2] eq 'n') { } 114 0 114
1726 18 ▶ if ($xlist->{$_}[1] <= 0 && $options{'diags'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xlist->{$_}[1] <= 0 and $options{'diags'} == 1) { } 0 18 18
Condition33%1/3
$xlist->{$_}[1] <= 0 and $options{"diags"} == 1
!ll&&!rl&&r
1800
MC/DC0%0/2
$xlist->{$_}[1] <= 0 and $options{"diags"} == 1
$xlist->{$_}[1] <= 0 $options{"diags"} == 1
Truth table33%1/3
A and B
A= $xlist->{$_}[1] <= 0B= $options{"diags"} == 1
ABresult
0X0
100
111
1727 0 warn "Xref free list entry $_ with bad next generation number.\n";
1728 } else {
1729 18 push @free_list, $_; # should be in numeric order (0 first)
1730 }
1731 } elsif ($xlist->{$_}[2] eq 'n') {
1732 114 ▶ if ($xlist->{$_}[0] <= 0 && $options{'diags'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xlist->{$_}[0] <= 0 and $options{"diags"} == 1) 0 114 114
Condition33%1/3
$xlist->{$_}[0] <= 0 and $options{"diags"} == 1
!ll&&!rl&&r
11400
MC/DC0%0/2
$xlist->{$_}[0] <= 0 and $options{"diags"} == 1
$xlist->{$_}[0] <= 0 $options{"diags"} == 1
Truth table33%1/3
A and B
A= $xlist->{$_}[0] <= 0B= $options{"diags"} == 1
ABresult
0X0
100
111
1733 0 warn "Xref active object $_ entry with bad length ".($xlist->{$_}[1])."\n";
1734 }
1735 114 ▶ if ($xlist->{$_}[1] < 0 && $options{'diags'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xlist->{$_}[1] < 0 and $options{"diags"} == 1) 0 114 114
Condition33%1/3
$xlist->{$_}[1] < 0 and $options{"diags"} == 1
!ll&&!rl&&r
11400
MC/DC0%0/2
$xlist->{$_}[1] < 0 and $options{"diags"} == 1
$xlist->{$_}[1] < 0 $options{"diags"} == 1
Truth table33%1/3
A and B
A= $xlist->{$_}[1] < 0B= $options{"diags"} == 1
ABresult
0X0
100
111
1736 0 warn "Xref active object $_ entry with bad generation number ".($xlist->{$_}[1])."\n";
1737 }
1738 } else {
1739 0 ▶ if ($options{'diags'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"diags"} == 1) 0 0 0
1740 0 warn "Xref entry has flag that is not 'f' or 'n'.\n";
1741 }
1742 }
1743 } # go through xlist and build free_list and check entries
1744 # traverse free list and check that "next object" is also in free list
1745 18 my $next_free = 0; # object 0 should always be in free list
1746 18 ▶ if ($xlist->{'0'}[1] != 65535 && $options{'diags'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xlist->{0}[1] != 65535 and $options{"diags"} == 1) 0 18 18
Condition33%1/3
$xlist->{0}[1] != 65535 and $options{"diags"} == 1
!ll&&!rl&&r
1800
MC/DC0%0/2
$xlist->{0}[1] != 65535 and $options{"diags"} == 1
$xlist->{0}[1] != 65535 $options{"diags"} == 1
Truth table33%1/3
A and B
A= $xlist->{0}[1] != 65535B= $options{"diags"} == 1
ABresult
0X0
100
111
1747 0 warn "Object 0 next generation is not 65535.\n";
1748 }
1749 do {
1750 18 ▶ if ($xlist->{$next_free}[2] ne 'f') {
Branch: 1 branch
BranchTrueFalseTotal
if ($xlist->{$next_free}[2] ne 'f') { } 0 18 18
1751 0 ▶ if ($options{'diags'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"diags"} == 1) 0 0 0
1752 0 warn "Corrupted free object list: next=$next_free is not a free object.\n";
1753 }
1754 0 $next_free = 0; # force end of free list
1755 } else {
1756 18 $next_free = $xlist->{$next_free}[0];
1757 }
1758 # remove this entry from free list array
1759 18 splice(@free_list, index(@free_list, $next_free), 1);
1760 18 ▶ } while ($next_free && exists $xlist->{$next_free});
Branch: 1 branch
BranchTrueFalseTotal
$next_free and exists $xlist->{$next_free} and do { do { if ($xlist->{$next_free}[2] ne "f") { if ($options{"diags"} == 1) { warn "Corrupted free object list: next=$next_free is not a free object.\n"; } ; $next_free = 0; } else { $next_free = $xlist->{$next_free}[0]; } ; splice @free_list, index(@free_list, $next_free), 1 } } 0 18 18
Condition33%1/3
$next_free and exists $xlist->{$next_free}
!ll&&!rl&&r
0180
MC/DC0%0/2
$next_free and exists $xlist->{$next_free}
$next_free exists $xlist->{$next_free}
Truth table33%1/3
A and B
A= $next_freeB= exists $xlist->{$next_free}
ABresult
0X0
100
111
1761 18 ▶ if (scalar @free_list && $options{'diags'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @free_list and $options{"diags"} == 1) 0 18 18
Condition33%1/3
scalar @free_list and $options{"diags"} == 1
!ll&&!rl&&r
1800
MC/DC0%0/2
scalar @free_list and $options{"diags"} == 1
scalar @free_list $options{"diags"} == 1
Truth table33%1/3
A and B
A= scalar @free_listB= $options{"diags"} == 1
ABresult
0X0
100
111
1762 0 warn "Corrupted xref list: object(s) @free_list marked as free, but are not in free chain.\n";
1763 }
1764  
1765 # done with cross reference table, so go on to trailer
1766 18 ▶ if ($buf !~ /^\s*trailer\b/i && $options{'diags'} == 1) { #orig 'die'
Branch: 1 branch
BranchTrueFalseTotal
if (not $buf =~ /^\s*trailer\b/i and $options{"diags"} == 1) 0 18 18
Condition33%1/3
not $buf =~ /^\s*trailer\b/i and $options{"diags"} == 1
!ll&&!rl&&r
1800
MC/DC0%0/2
not $buf =~ /^\s*trailer\b/i and $options{"diags"} == 1
not $buf =~ /^\s*trailer\b/i $options{"diags"} == 1
Truth table33%1/3
not(A) and B
A= not $buf =~ /^\s*trailer\b/iB= $options{"diags"} == 1
ABresult
0X0
100
111
1767 0 warn "Malformed trailer in PDF file $self->{' fname'} at " . ($fh->tell() - length($buf));
1768 }
1769  
1770 18 $buf =~ s/^\s*trailer\b//i;
1771  
1772 18 ($tdict, $buf) = $self->readval($buf);
1773  
1774 } elsif ($buf =~ m/^(\d+)\s+(\d+)\s+obj/i) { # object for xref stream
1775 3 my ($xref_obj, $xref_gen) = ($1, $2);
1776 3 $PDF::Builder::global_pdf->verCheckOutput(1.5, "importing cross-reference stream");
1777 # XRef streams
1778 3 ($tdict, $buf) = $self->readval($buf);
1779  
1780 3 ▶ unless ($tdict->{' stream'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tdict->{" stream"}) 0 3 3
1781 0 ▶ if ($options{'diags'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"diags"} == 1) 0 0 0
1782 0 warn "Malformed XRefStm at $xref_obj $xref_gen obj in PDF file $self->{' fname'}";
1783 }
1784 }
1785 3 $tdict->read_stream(1);
1786  
1787 3 my $stream = $tdict->{' stream'};
1788 3 my @widths = map { $_->val() } @{$tdict->{'W'}->val()};
1789  
1790 3 my $start = 0;
1791 3 my $last;
1792  
1793 my @index;
1794 3 ▶ if (defined $tdict->{'Index'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tdict->{'Index'}) { } 1 2 3
1795 1 @index = map { $_->val() } @{$tdict->{'Index'}->val()};
1796 } else {
1797 2 @index = (0, $tdict->{'Size'}->val());
1798 }
1799  
1800 3 ▶ while (scalar @index) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @index) 3 3 6
1801 3 $start = shift(@index);
1802 3 $last = $start + shift(@index) - 1;
1803  
1804 3 for my $i ($start...$last) {
1805 # Replaced "for $xmin" because it creates a loop-specific local
1806 # variable, and we need $xmin to be correct for maxobj below.
1807 26 $xmin = $i;
1808  
1809 26 my @cols;
1810  
1811 26 for my $w (@widths) {
1812 78 my $data;
1813 78 ▶ $data = $self->_unpack_xref_stream($w, substr($stream, 0, $w, '')) if $w;
Branch: 1 branch
BranchTrueFalseTotal
if $w 78 0 78
1814  
1815 78 push @cols, $data;
1816 }
1817  
1818 26 ▶ $cols[0] = 1 unless defined $cols[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cols[0] 1 25 26
1819 26 ▶ if ($cols[0] > 2 && $options{'diags'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cols[0] > 2 and $options{"diags"} == 1) 0 26 26
Condition33%1/3
$cols[0] > 2 and $options{"diags"} == 1
!ll&&!rl&&r
2600
MC/DC0%0/2
$cols[0] > 2 and $options{"diags"} == 1
$cols[0] > 2 $options{"diags"} == 1
Truth table33%1/3
A and B
A= $cols[0] > 2B= $options{"diags"} == 1
ABresult
0X0
100
111
1820 0 warn "Invalid XRefStm entry type ($cols[0]) at $xref_obj $xref_gen obj";
1821 }
1822  
1823 26 ▶ next if exists $xlist->{$xmin};
Branch: 1 branch
BranchTrueFalseTotal
if exists $xlist->{$xmin} 0 26 26
1824  
1825 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
1826 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
1827  
1828 26 $xlist->{$xmin} = \@objind;
1829 }
1830 }
1831  
1832 } else { #orig 'die'
1833 0 ▶ if ($options{'diags'} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"diags"} == 1) 0 0 0
1834 0 warn "Malformed xref in PDF file $self->{' fname'}";
1835 }
1836 }
1837  
1838 # did we get to here without managing to set $xmin?
1839 21 ▶ $xmin ||= 0;
Condition50%1/2
$xmin ||= 0
l!l
210
MC/DC0%0/1
$xmin ||= 0
$xmin
Truth table50%1/2
A
A= $xmin
Aresult
11
00
1840  
1841 21 $tdict->{' loc'} = $xpos;
1842 21 $tdict->{' xref'} = $xlist;
1843 21 ▶ $self->{' maxobj'} = $xmin + 1 if $xmin + 1 > $self->{' maxobj'};
Branch: 1 branch
BranchTrueFalseTotal
if $xmin + 1 > $self->{" maxobj"} 18 3 21
1844 $tdict->{' prev'} = $self->readxrtr($tdict->{'Prev'}->val(), %options)
1845 21 ▶ if (defined $tdict->{'Prev'} and $tdict->{'Prev'}->val() != 0);
Branch: 1 branch
BranchTrueFalseTotal
if defined $tdict->{"Prev"} and $tdict->{"Prev"}->val != 0 3 18 21
Condition66%2/3
defined $tdict->{"Prev"} and $tdict->{"Prev"}->val != 0
!ll&&!rl&&r
1803
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
1846 21 ▶ delete $tdict->{' prev'} unless defined $tdict->{' prev'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tdict->{" prev"} 18 3 21
1847  
1848 21 return $tdict;
1849 } # end of readxrtr()
1850  
1851 =head2 out_trailer
1852  
1853 $p->out_trailer($tdict, $update)
1854  
1855 $p->out_trailer($tdict)
1856  
1857 =over
1858  
1859 Outputs the body and trailer for a PDF file by outputting all the objects in
1860 the ' outlist' and then outputting a xref table for those objects and any
1861 freed ones. It then outputs the trailing dictionary and the trailer code.
1862  
1863 =back
1864  
1865 =cut
1866  
1867 178 ▶sub out_trailer {
Subroutine out_trailer: called CC=32 SCAR=48.7
Pod documented
1868 178 my ($self, $tdict, $update) = @_;
1869  
1870 178 my $fh = $self->{' OUTFILE'};
1871  
1872 178 ▶ while (@{$self->{' outlist'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{" outlist"};}) 183 178 361
1873 183 $self->ship_out();
1874 }
1875  
1876 178 ▶ if (defined $self->{'Size'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'Size'}) { } 8 170 178
1877 8 $tdict->{'Size'} = PDFNum($self->{' maxobj'} -1 );
1878 } else {
1879 170 $tdict->{'Size'} = PDFNum($self->{' maxobj'} );
1880 }
1881  
1882 178 my $tloc = $fh->tell();
1883 ## $fh->print("xref\n");
1884 # instead of directly outputting (fh->print) xreflist, we accumulate in @out
1885 178 my @out;
1886 178 ▶ my @xreflist = sort { $self->{' objects'}{$a->uid()}[0] <=> $self->{' objects'}{$b->uid()}[0] } (@{$self->{' printed'} || []}, @{$self->{' free'} || []});
Condition100%4/4
$self->{' printed'} || []
l!l
1753
$self->{' free'} || []
l!l
1753
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
1887  
1888 178 my ($i, $j, $k);
1889 178 ▶ unless ($update) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($update) 170 8 178
1890 170 $i = 1;
1891 170 ▶ for ($j = 0; $j < @xreflist; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @xreflist) 1044 170 1214
1892 1044 my @inserts;
1893 1044 $k = $xreflist[$j];
1894 1044 ▶ while ($i < $self->{' objects'}{$k->uid()}[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{" objects"}{$k->uid}[0]) 0 1044 1044
1895 0 my ($n) = PDF::Builder::Basic::PDF::Objind->new();
1896 0 $self->add_obj($n, $i, 0);
1897 0 $self->free_obj($n);
1898 0 push(@inserts, $n);
1899 0 $i++;
1900 }
1901 1044 splice(@xreflist, $j, 0, @inserts);
1902 1044 $j += @inserts;
1903 1044 $i++;
1904 }
1905 }
1906  
1907 0 ▶ my @freelist = sort { $self->{' objects'}{$a->uid()}[0] <=> $self->{' objects'}{$b->uid()}[0] } @{$self->{' free'} || []};
Condition100%2/2
$self->{' free'} || []
l!l
1753
MC/DC100%1/1
$self->{' free'} || []
$self->{' free'}
Truth table100%2/2
A
A= $self->{' free'}
Aresult
11
00
1908  
1909 178 $j = 0; my $first = -1; $k = 0;
1910 178 ▶ for ($i = 0; $i <= $#xreflist + 1; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#xreflist + 1) 1244 178 1422
1911 1244 ▶ 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) { } 190 1054 1244
Condition100%3/3
$i > $#xreflist or $self->{" objects"}{$xreflist[$i]->uid}[0] != $j + 1
l!l&&r!l&&!r
178121054
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
1912 ## $fh->print(($first == -1 ? "0 " : "$self->{' objects'}{$xreflist[$first]->uid()}[0] ") . ($i - $first) . "\n");
1913 190 ▶ push @out, ($first == -1 ? "0 " : "$self->{' objects'}{$xreflist[$first]->uid()}[0] ") . ($i - $first) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
$first == -1 ? : 178 12 190
1914 190 ▶ if ($first == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first == -1) 178 12 190
1915 ## $fh->printf("%010d 65535 f \n", defined $freelist[$k] ? $self->{' objects'}{$freelist[$k]->uid()}[0] : 0);
1916 178 ▶ 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 178 178
1917 178 $first = 0;
1918 }
1919 190 ▶ for ($j = $first; $j < $i; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $i) 1066 190 1256
1920 1066 my $xref = $xreflist[$j];
1921 1066 ▶ if (defined $freelist[$k] && defined $xref && "$freelist[$k]" eq "$xref") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $freelist[$k] and defined $xref and "$freelist[$k]" eq "$xref") { } 0 1066 1066
Condition33%2/6
defined $freelist[$k] and defined $xref and "$freelist[$k]" eq "$xref"
!ll&&!rl&&r
106600
defined $freelist[$k] and defined $xref
!ll&&!rl&&r
106600
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
1922 0 $k++;
1923 ## $fh->print(pack("A10AA5A4",
1924 push(@out, pack("A10AA5A4",
1925 sprintf("%010d", (defined $freelist[$k] ?
1926 $self->{' objects'}{$freelist[$k]->uid()}[0] : 0)), " ",
1927 0 ▶ sprintf("%05d", $self->{' objects'}{$xref->uid()}[1] + 1),
Branch: 1 branch
BranchTrueFalseTotal
defined $freelist[$k] ? : 0 0 0
1928 " f \n"));
1929 } else {
1930 ## $fh->print(pack("A10AA5A4", sprintf("%010d", $self->{' locs'}{$xref->uid()}), " ",
1931 push(@out, pack("A10AA5A4", sprintf("%010d", $self->{' locs'}{$xref->uid()}), " ",
1932 1066 sprintf("%05d", $self->{' objects'}{$xref->uid()}[1]),
1933 " n \n"));
1934 }
1935 }
1936 190 $first = $i;
1937 190 ▶ $j = $self->{' objects'}{$xreflist[$i]->uid()}[0] if ($i < scalar @xreflist);
Branch: 1 branch
BranchTrueFalseTotal
if $i < scalar @xreflist 12 178 190
1938  
1939 } else {
1940 1054 $j++;
1941 }
1942 } # end for loop through xreflists
1943 ## $fh->print("trailer\n");
1944 ## $tdict->outobjdeep($fh, $self);
1945 ## $fh->print("\nstartxref\n$tloc\n%%EOF\n");
1946 ## start new code for 117184 fix by Vadim. @out has array of xref content
1947 178 ▶ 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 178 178
Condition33%1/3
exists $tdict->{"Type"} and $tdict->{"Type"}->val eq "XRef"
!ll&&!rl&&r
17800
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
1948  
1949 0 my (@index, @stream);
1950 0 for (@out) { # @out is the accumulated cross reference list
1951 0 my @a = split;
1952 0 ▶ @a == 2 ? push @index, @a : push @stream, \@a;
Branch: 1 branch
BranchTrueFalseTotal
@a == 2 ? : 0 0 0
1953 }
1954 0 my $i = $self->{' maxobj'}++;
1955 0 $self->add_obj($tdict, $i, 0);
1956 0 $self->out_obj($tdict);
1957  
1958 0 push @index, $i, 1;
1959 0 push @stream, [ $tloc, 0, 'n' ];
1960  
1961 0 ▶ my $len = $tloc > 0xFFFF ? 4 : 2; # don't expect files > 4 Gb
Branch: 1 branch
BranchTrueFalseTotal
$tloc > 65535 ? : 0 0 0
1962 0 ▶ my $tpl = $tloc > 0xFFFF ? 'CNC' : 'CnC'; # don't expect gennum > 255, it's absurd.
Branch: 1 branch
BranchTrueFalseTotal
$tloc > 65535 ? : 0 0 0
1963 # Adobe doesn't use them anymore anyway
1964 0 my $sstream = '';
1965 0 my @prev = ( 0 ) x ( $len + 2 ); # init prev to all 0's
1966 0 for (@stream) {
1967 # OK to zero out gennum of 65535 for a cross reference stream,
1968 # rather than just truncating to 255 -- Vadim
1969 0 ▶ $_->[ 1 ] = 0 if $_->[ 1 ] == 65535 and
Branch: 1 branch
BranchTrueFalseTotal
if $_->[1] == 65535 and $_->[2] eq "f" 0 0 0
Condition0%0/3
$_->[1] == 65535 and $_->[2] eq "f"
!ll&&!rl&&r
000
MC/DC0%0/2
$_->[1] == 65535 and $_->[2] eq "f"
$_->[1] == 65535 $_->[2] eq "f"
Truth table0%0/3
A and B
A= $_->[1] == 65535B= $_->[2] eq "f"
ABresult
0X0
100
111
1970 $_->[ 2 ] eq 'f';
1971 # make sure is 0..255, since will pack with 'C' code -- Phil
1972 0 ▶ if ($_->[1] > 0xFF) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->[1] > 255) 0 0 0
1973 0 print "generation number ".($_->[1])." in entry '$_->[0] $_->[1] $_->[2]' exceeds 256, reduced to ".($_->[1] & 0x00FF)."\n";
1974 }
1975 0 $_->[ 1 ] &= 0x00FF;
1976 0 ▶ my @line = unpack 'C*', pack $tpl, $_->[ 2 ] eq 'n'? 1 : 0, @{ $_ }[ 0 .. 1 ];
Branch: 1 branch
BranchTrueFalseTotal
$_->[2] eq 'n' ? : 0 0 0
1977  
1978 $sstream .= pack 'C*', 2, # prepend filtering method, "PNG Up"
1979 0 map {($line[ $_ ] - $prev[ $_ ] + 256) % 256} 0 .. $#line;
1980 0 @prev = @line;
1981 }
1982 # build a dictionary for the cross reference stream
1983 0 $tdict->{'Size'} = PDFNum($i + 1);
1984 0 $tdict->{'Index'} = PDFArray(map { PDFNum($_) } @index);
1985 0 $tdict->{'W'} = PDFArray(map { PDFNum($_) } 1, $len, 1);
1986 0 $tdict->{'Filter'} = PDFName('FlateDecode');
1987  
1988 # it's compressed
1989 0 $tdict->{'DecodeParms'} = PDFDict();
1990 0 $tdict->{'DecodeParms'}->val()->{'Predictor'} = PDFNum(12);
1991 0 $tdict->{'DecodeParms'}->val()->{'Columns'} = PDFNum($len + 2);
1992  
1993 0 $sstream = PDF::Builder::Basic::PDF::Filter::FlateDecode->new()->outfilt($sstream, 1);
1994 0 $tdict->{' stream'} = $sstream;
1995 0 $tdict->{' nofilt'} = 1;
1996 0 delete $tdict->{'Length'};
1997 0 $self->ship_out();
1998 } else {
1999 # delete may be moved later by Vadim closer to where XRefStm created
2000 178 delete $tdict->{'XRefStm'};
2001 # almost the original code
2002 178 $fh->print("xref\n", @out, "trailer\n");
2003 178 $tdict->outobjdeep($fh, $self);
2004 178 $fh->print("\n");
2005 }
2006 178 $fh->print("startxref\n$tloc\n%%EOF\n");
2007 ## end of new code
2008  
2009 178 return;
2010 } # end of out_trailer()
2011  
2012 =head2 _new
2013  
2014 PDF::Builder::Basic::PDF::File->_new()
2015  
2016 =over
2017  
2018 Creates a very empty PDF file object (used by new() and open())
2019  
2020 =back
2021  
2022 =cut
2023  
2024 252 ▶sub _new {
Subroutine _new: called CC=1 SCAR=0.0
2025 252 my $class = shift();
2026 252 my $self = {};
2027  
2028 252 bless $self, $class;
2029 252 $self->{' outlist'} = [];
2030 252 $self->{' outlist_cache'} = {}; # A cache of what's in the 'outlist'
2031 252 $self->{' maxobj'} = 1;
2032 252 $self->{' objcache'} = {};
2033 252 $self->{' objects'} = {};
2034  
2035 252 return $self;
2036 }
2037  
2038 40 1;
2039  
2040 =head1 AUTHOR
2041  
2042 Martin Hosken Martin_Hosken@sil.org
2043  
2044 Copyright Martin Hosken 1999
2045  
2046 No warranty or expression of effectiveness, least of all regarding anyone's
2047 safety, is implied in this software or documentation.
« Builder/Basic/PDF/Filter/FlateDecode.pm ↑ Summary Builder/Resource/XObject/Image/TIFF/File_GT.pm »