API2.pm

filter: Statementstmt 61.9% 644 / 1040 Branchbran 40.6% 242 / 596 Conditioncond 24.7% 52 / 210 MC/DCmcdc 6.5% 8 / 123 Subroutinesub 70.0% 84 / 120 Podpod 93.8% 91 / 97 totaltotal 51.2% 1121 / 2186 CC 406 SCAR 57.0
CC
406
Cov
51%
barcode
2550.0
font
267.8
preferences
167.8
CRAP 298.5
« API2/ViewerPreferences.pm ↑ Summary API2/UniWrap.pm »
1 package PDF::API2;
2  
3 39 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 39 ▶no warnings qw[ deprecated recursion uninitialized ];
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 39 our $VERSION = '2.049'; # VERSION
7  
8 39 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 39 ▶use Encode qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 39 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 39 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 39 ▶use PDF::API2::Basic::PDF::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 39 ▶use PDF::API2::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 39 ▶use PDF::API2::Basic::PDF::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 39 ▶use PDF::API2::Basic::PDF::Pages;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 39 ▶use PDF::API2::Page;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 39 ▶use PDF::API2::Resource::XObject::Form::Hybrid;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 39 ▶use PDF::API2::Resource::ExtGState;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 39 ▶use PDF::API2::Resource::Pattern;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 39 ▶use PDF::API2::Resource::Shading;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 39 ▶use PDF::API2::NamedDestination;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 39 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 39 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 39 my @font_path = __PACKAGE__->set_font_path('/usr/share/fonts',
32 '/usr/local/share/fonts',
33 'c:/windows/fonts');
34  
35 =head1 NAME
36  
37 PDF::API2 - Create, modify, and examine PDF files
38  
39 =head1 SYNOPSIS
40  
41 use PDF::API2;
42  
43 # Create a blank PDF file
44 $pdf = PDF::API2->new();
45  
46 # Open an existing PDF file
47 $pdf = PDF::API2->open('some.pdf');
48  
49 # Add a blank page
50 $page = $pdf->page();
51  
52 # Retrieve an existing page
53 $page = $pdf->open_page($page_number);
54  
55 # Set the page size
56 $page->size('Letter');
57  
58 # Add a built-in font to the PDF
59 $font = $pdf->font('Helvetica-Bold');
60  
61 # Add an external TrueType font to the PDF
62 $font = $pdf->font('/path/to/font.ttf');
63  
64 # Add some text to the page
65 $text = $page->text();
66 $text->font($font, 20);
67 $text->position(200, 700);
68 $text->text('Hello World!');
69  
70 # Save the PDF
71 $pdf->save('/path/to/new.pdf');
72  
73 =head1 INPUT/OUTPUT METHODS
74  
75 =head2 new
76  
77 my $pdf = PDF::API2->new(%options);
78  
79 Create a new PDF.
80  
81 The following options are available:
82  
83 =over
84  
85 =item * file
86  
87 If you will be saving the PDF to disk and already know the filename, you can
88 include it here to open the file for writing immediately. C<file> may also be
89 a filehandle.
90  
91 =item * compress
92  
93 By default, most of the PDF will be compressed to save space. To turn this off
94 (generally only useful for testing or debugging), set C<compress> to 0.
95  
96 =back
97  
98 =cut
99  
100 164 ▶sub new {
Subroutine new: called CC=11 SCAR=26.0
Pod documented
101 164 my ($class, %options) = @_;
102  
103 164 my $self = {};
104 164 bless $self, $class;
105 164 $self->{'pdf'} = PDF::API2::Basic::PDF::File->new();
106  
107 164 $self->{'pdf'}->{' version'} = '1.4';
108 164 $self->{'pages'} = PDF::API2::Basic::PDF::Pages->new($self->{'pdf'});
109 164 $self->{'pages'}->proc_set(qw(PDF Text ImageB ImageC ImageI));
110 164 ▶ $self->{'pages'}->{'Resources'} ||= PDFDict();
Condition33%1/3
$self->{"pages"}{"Resources"} ||= PDFDict()
l!l&&r!l&&!r
16400
MC/DC0%0/2
$self->{"pages"}{"Resources"} ||= PDFDict()
$self->{"pages"}{"Resources"} PDFDict()
Truth table33%1/3
A ||= B
A= $self->{"pages"}{"Resources"}B= PDFDict()
ABresult
1X1
011
000
111 164 ▶ $self->{'pdf'}->new_obj($self->{'pages'}->{'Resources'}) unless $self->{'pages'}->{'Resources'}->is_obj($self->{'pdf'});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"pages"}{"Resources"}->is_obj($self->{"pdf"}) 164 0 164
112 164 $self->{'catalog'} = $self->{'pdf'}->{'Root'};
113 164 weaken $self->{'catalog'};
114 164 $self->{'fonts'} = {};
115 164 $self->{'pagestack'} = [];
116  
117 # -compress is deprecated (remove the hyphen)
118 164 ▶ if (exists $options{'-compress'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{"-compress"}) 27 137 164
119 27 ▶ $options{'compress'} //= delete $options{'-compress'};
Condition33%1/3
$options{"compress"} //= delete $options{"-compress"}
l!l&&r!l&&!r
0270
MC/DC0%0/2
$options{"compress"} //= delete $options{"-compress"}
$options{"compress"} delete $options{"-compress"}
Truth table33%1/3
A //= B
A= $options{"compress"}B= delete $options{"-compress"}
ABresult
1X1
011
000
120 }
121  
122 164 ▶ if (exists $options{'compress'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{'compress'}) { } 114 50 164
123 114 ▶ $self->{'forcecompress'} = $options{'compress'} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$options{'compress'} ? : 0 114 114
124 }
125 else {
126 50 $self->{'forcecompress'} = 1;
127 }
128 164 $self->preferences(%options);
129  
130 # -file is deprecated (remove the hyphen)
131 164 ▶ $options{'file'} //= $options{'-file'} if $options{'-file'};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"-file"} 0 164 164
Condition0%0/3
$options{"file"} //= $options{"-file"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"file"} //= $options{"-file"}
$options{"file"} $options{"-file"}
Truth table0%0/3
A //= B
A= $options{"file"}B= $options{"-file"}
ABresult
1X1
011
000
132  
133 164 ▶ if ($options{'file'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"file"}) 0 164 164
134 0 $self->{'pdf'}->create_file($options{'file'});
135 0 $self->{'partial_save'} = 1;
136 }
137  
138 # Deprecated; used by info and infoMetaAttributes but not their replacements
139 164 $self->{'infoMeta'} = [qw(Author CreationDate ModDate Creator Producer Title
140 Subject Keywords)];
141  
142 164 ▶ my $version = eval { $PDF::API2::VERSION } || 'Development Version';
Condition50%1/2
eval { do { $PDF::API2::VERSION } } || 'Development Version'
l!l
1640
MC/DC0%0/1
eval { do { $PDF::API2::VERSION } } || 'Development Version'
eval { do { $PDF::API2::VERSION } }
Truth table50%1/2
A
A= eval { do { $PDF::API2::VERSION } }
Aresult
11
00
143 164 $self->producer("PDF::API2 $version ($OSNAME)");
144  
145 164 return $self;
146 }
147  
148 =head2 open
149  
150 my $pdf = PDF::API2->open('/path/to/file.pdf', %options);
151  
152 Open an existing PDF file.
153  
154 The following option is available:
155  
156 =over
157  
158 =item * compress
159  
160 By default, most of the PDF will be compressed to save space. To turn this off
161 (generally only useful for testing or debugging), set C<compress> to 0.
162  
163 =back
164  
165 =cut
166  
167 8 ▶sub open {
Subroutine open: called CC=5 SCAR=16.3
Pod documented
168 8 my ($class, $file, %options) = @_;
169 8 ▶ croak "File '$file' does not exist" unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 8 8
170 8 ▶ croak "File '$file' is not readable" unless -r $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $file 0 8 8
171  
172 8 my $self = {};
173 8 bless $self, $class;
174 8 foreach my $parameter (keys %options) {
175 2 $self->default($parameter, $options{$parameter});
176 }
177  
178 8 my $is_writable = -w $file;
179 8 $self->{'pdf'} = PDF::API2::Basic::PDF::File->open($file, $is_writable);
180 8 _open_common($self, %options);
181 8 $self->{'pdf'}->{' fname'} = $file;
182 8 ▶ $self->{'opened_readonly'} = 1 unless $is_writable;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_writable 0 8 8
183  
184 8 return $self;
185 }
186  
187 16 ▶sub _open_common {
Subroutine _open_common: called CC=7 SCAR=19.6
188 16 my ($self, %options) = @_;
189  
190 16 $self->{'pdf'}->{'Root'}->realise();
191 16 ▶ $self->{'pdf'}->{' version'} ||= '1.3';
Condition50%1/2
$self->{"pdf"}{" version"} ||= "1.3"
l!l
160
MC/DC0%0/1
$self->{"pdf"}{" version"} ||= "1.3"
$self->{"pdf"}{" version"}
Truth table50%1/2
A
A= $self->{"pdf"}{" version"}
Aresult
11
00
192  
193 16 $self->{'pages'} = $self->{'pdf'}->{'Root'}->{'Pages'}->realise();
194 16 weaken $self->{'pages'};
195 16 my @pages = proc_pages($self->{'pdf'}, $self->{'pages'});
196 16 $self->{'pagestack'} = [sort { $a->{' pnum'} <=> $b->{' pnum'} } @pages];
197 16 weaken $self->{'pagestack'}->[$_] for (0 .. scalar @{$self->{'pagestack'}});
198  
199 16 $self->{'catalog'} = $self->{'pdf'}->{'Root'};
200 16 weaken $self->{'catalog'};
201  
202 16 $self->{'opened'} = 1;
203  
204 # -compress is deprecated (remove the hyphen)
205 16 ▶ if (exists $options{'-compress'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{"-compress"}) 2 14 16
206 2 ▶ $options{'compress'} //= delete $options{'-compress'};
Condition33%1/3
$options{"compress"} //= delete $options{"-compress"}
l!l&&r!l&&!r
020
MC/DC0%0/2
$options{"compress"} //= delete $options{"-compress"}
$options{"compress"} delete $options{"-compress"}
Truth table33%1/3
A //= B
A= $options{"compress"}B= delete $options{"-compress"}
ABresult
1X1
011
000
207 }
208  
209 16 ▶ if (exists $options{'compress'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{'compress'}) { } 2 14 16
210 2 ▶ $self->{'forcecompress'} = $options{'compress'} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$options{'compress'} ? : 0 2 2
211 }
212 else {
213 14 $self->{'forcecompress'} = 1;
214 }
215 16 $self->{'fonts'} = {};
216 16 $self->{'infoMeta'} = [qw(Author CreationDate ModDate Creator Producer Title Subject Keywords)];
217 16 return $self;
218 }
219  
220 =head2 save
221  
222 $pdf->save('/path/to/file.pdf');
223  
224 Write the PDF to disk and close the file. A filename is optional if one was
225 specified while opening or creating the PDF.
226  
227 As a side effect, the document structure is removed from memory when the file is
228 saved, so it will no longer be usable.
229  
230 =cut
231  
232 # Deprecated (renamed)
233 0 ▶sub saveas { return save(@_) } ## no critic
Subroutine saveas: not called CC=1 SCAR=6.9
Pod documented
234  
235 1 ▶sub save {
Subroutine save: called CC=10 SCAR=35.1
Pod documented
236 1 my ($self, $file) = @_;
237  
238 1 ▶ if ($self->{'partial_save'} and not $file) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'partial_save'} and not $file) { } 0 1 1
elsif ($self->{'opened_scalar'}) { } 0 1 1
Condition33%1/3
$self->{"partial_save"} and not $file
!ll&&!rl&&r
100
MC/DC0%0/2
$self->{"partial_save"} and not $file
$self->{"partial_save"} not $file
Truth table33%1/3
A and not(B)
A= $self->{"partial_save"}B= not $file
ABresult
0X0
100
111
239 0 $self->{'pdf'}->close_file();
240 }
241 elsif ($self->{'opened_scalar'}) {
242 0 ▶ croak 'A filename argument is required' unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 0 0
243 0 $self->{'pdf'}->append_file();
244 0 my $fh;
245 0 ▶ CORE::open($fh, '>', $file) or die "Unable to open $file for writing: $!";
Branch: 1 branch
BranchTrueFalseTotal
CORE::open $fh, ">", $file or die "Unable to open $file for writing: $!" 0 0 0
246 0 binmode($fh, ':raw');
247 0 print $fh ${$self->{'content_ref'}};
248 0 CORE::close($fh);
249 }
250 else {
251 1 ▶ croak 'A filename argument is required' unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 1 1
252 1 ▶ unless ($self->{'pdf'}->{' fname'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'pdf'}{' fname'}) { } 1 0 1
if (do { $self->{'pdf'}{' fname'} eq $file }) { } 1 0 1
253 0 $self->{'pdf'}->out_file($file);
254 }
255 elsif ($self->{'pdf'}->{' fname'} eq $file) {
256 1 ▶ croak "File is read-only" if $self->{'opened_readonly'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opened_readonly"} 0 1 1
257 1 $self->{'pdf'}->close_file();
258 }
259 else {
260 0 $self->{'pdf'}->clone_file($file);
261 0 $self->{'pdf'}->close_file();
262 }
263 }
264  
265 # This can be eliminated once we're confident that circular references are
266 # no longer an issue. See t/circular-references.t.
267 1 $self->close();
268  
269 1 return;
270 }
271  
272 # Deprecated (use save instead)
273 #
274 # This method allows for objects to be written to disk in advance of finally
275 # saving and closing the file. Otherwise, it's no different than just calling
276 # save when all changes have been made. There's no memory advantage since
277 # ship_out doesn't remove objects from memory.
278 0 ▶sub finishobjects {
Subroutine finishobjects: not called CC=2 SCAR=17.9
Pod documented
279 0 my ($self, @objs) = @_;
280  
281 0 ▶ if ($self->{'partial_save'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"partial_save"}) 0 0 0
282 0 $self->{'pdf'}->ship_out(@objs);
283 }
284  
285 0 return;
286 }
287  
288 # Deprecated (use save instead)
289 0 ▶sub update {
Subroutine update: not called CC=2 SCAR=17.9
Pod documented
290 0 my $self = shift();
291 0 ▶ croak "File is read-only" if $self->{'opened_readonly'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opened_readonly"} 0 0 0
292 0 $self->{'pdf'}->close_file();
293 0 return;
294 }
295  
296 =head2 close
297  
298 $pdf->close();
299  
300 Close an open file (if relevant) and remove the object structure from memory.
301  
302 PDF::API2 contains circular references, so this call is necessary in
303 long-running processes to keep from running out of memory.
304  
305 This will be called automatically when you save or stringify a PDF.
306 You should only need to call it explicitly if you are reading PDF
307 files and not writing them.
308  
309 =cut
310  
311 # Deprecated (renamed)
312 162 ▶sub release { return $_[0]->close() }
Subroutine release: called CC=1 SCAR=0.0
Pod documented
313 0 ▶sub end { return $_[0]->close() }
Subroutine end: not called CC=1 SCAR=6.9
Pod documented
314  
315 310 ▶sub close {
Subroutine close: called CC=3 SCAR=11.0
Pod documented
316 310 my $self = shift();
317 310 ▶ $self->{'pdf'}->release() if defined $self->{'pdf'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"pdf"} 274 36 310
318  
319 310 foreach my $key (keys %$self) {
320 1055 $self->{$key} = undef;
321 1055 delete $self->{$key};
322 }
323  
324 310 return;
325 }
326  
327 =head2 from_string
328  
329 my $pdf = PDF::API2->from_string($pdf_string, %options);
330  
331 Read a PDF document contained in a string.
332  
333 The following option is available:
334  
335 =over
336  
337 =item * compress
338  
339 By default, most of the PDF will be compressed to save space. To turn this off
340 (generally only useful for testing or debugging), set C<compress> to 0.
341  
342 =back
343  
344 =cut
345  
346 # Deprecated (renamed)
347 1 ▶sub openScalar { return from_string(@_); } ## no critic
Subroutine openScalar: called CC=1 SCAR=0.0
Pod documented
348 0 ▶sub open_scalar { return from_string(@_); } ## no critic
Subroutine open_scalar: not called CC=1 SCAR=6.9
Pod documented
349  
350 8 ▶sub from_string {
Subroutine from_string: called CC=3 SCAR=11.1
Pod documented
351 8 my ($class, $content, %options) = @_;
352  
353 8 my $self = {};
354 8 bless $self, $class;
355 8 foreach my $parameter (keys %options) {
356 0 $self->default($parameter, $options{$parameter});
357 }
358  
359 8 $self->{'content_ref'} = \$content;
360 8 my $fh;
361 8 ▶ CORE::open($fh, '+<', \$content) or die "Can't begin scalar IO";
Branch: 1 branch
BranchTrueFalseTotal
CORE::open $fh, "+<", \$content or die "Can't begin scalar IO" 8 0 8
362  
363 8 $self->{'pdf'} = PDF::API2::Basic::PDF::File->open($fh, 1);
364 8 _open_common($self, %options);
365 8 $self->{'opened_scalar'} = 1;
366  
367 8 return $self;
368 }
369  
370 =head2 to_string
371  
372 my $string = $pdf->to_string();
373  
374 Return the PDF document as a string.
375  
376 As a side effect, the document structure is removed from memory when the string
377 is created, so it will no longer be usable.
378  
379 =cut
380  
381 # Maintainer's note: The object is being destroyed because it contains
382 # (contained?) circular references that would otherwise result in memory not
383 # being freed if the object merely goes out of scope. If possible, the circular
384 # references should be eliminated so that to_string doesn't need to be
385 # destructive. See t/circular-references.t.
386 #
387 # I've opted not to just require a separate call to close() because it would
388 # likely introduce memory leaks in many existing programs that use this module.
389  
390 # Deprecated (renamed)
391 0 ▶sub stringify { return to_string(@_) } ## no critic
Subroutine stringify: not called CC=1 SCAR=6.9
Pod documented
392  
393 147 ▶sub to_string {
Subroutine to_string: called CC=5 SCAR=16.1
Pod documented
394 147 my $self = shift();
395  
396 147 my $string = '';
397 147 ▶ if ($self->{'opened_scalar'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'opened_scalar'}) { } 3 144 147
elsif ($self->{'opened'}) { } 4 140 144
398 3 $self->{'pdf'}->append_file();
399 3 $string = ${$self->{'content_ref'}};
400 }
401 elsif ($self->{'opened'}) {
402 4 my $fh = FileHandle->new();
403 4 ▶ CORE::open($fh, '>', \$string) || die "Can't begin scalar IO";
Branch: 1 branch
BranchTrueFalseTotal
CORE::open $fh, ">", \$string or die "Can't begin scalar IO" 4 0 4
404 4 $self->{'pdf'}->clone_file($fh);
405 4 $self->{'pdf'}->close_file();
406 4 $fh->close();
407 }
408 else {
409 140 my $fh = FileHandle->new();
410 140 ▶ CORE::open($fh, '>', \$string) || die "Can't begin scalar IO";
Branch: 1 branch
BranchTrueFalseTotal
CORE::open $fh, ">", \$string or die "Can't begin scalar IO" 140 0 140
411 140 $self->{'pdf'}->out_file($fh);
412 140 $fh->close();
413 }
414  
415 # This can be eliminated once we're confident that circular references are
416 # no longer an issue. See t/circular-references.t.
417 147 $self->close();
418  
419 147 return $string;
420 }
421  
422 =head1 METADATA METHODS
423  
424 =head2 title
425  
426 $title = $pdf->title();
427 $pdf = $pdf->title($title);
428  
429 Get/set/clear the document's title.
430  
431 =cut
432  
433 0 ▶sub title {
Subroutine title: not called CC=1 SCAR=6.9
Pod documented
434 0 my $self = shift();
435 0 return $self->info_metadata('Title', @_);
436 }
437  
438 =head2 author
439  
440 $author = $pdf->author();
441 $pdf = $pdf->author($author);
442  
443 Get/set/clear the name of the person who created the document.
444  
445 =cut
446  
447 0 ▶sub author {
Subroutine author: not called CC=1 SCAR=6.9
Pod documented
448 0 my $self = shift();
449 0 return $self->info_metadata('Author', @_);
450 }
451  
452 =head2 subject
453  
454 $subject = $pdf->subject();
455 $pdf = $pdf->subject($subject);
456  
457 Get/set/clear the subject of the document.
458  
459 =cut
460  
461 0 ▶sub subject {
Subroutine subject: not called CC=1 SCAR=6.9
Pod documented
462 0 my $self = shift();
463 0 return $self->info_metadata('Subject', @_);
464 }
465  
466 =head2 keywords
467  
468 $keywords = $pdf->keywords();
469 $pdf = $pdf->keywords($keywords);
470  
471 Get/set/clear a space-separated string of keywords associated with the document.
472  
473 =cut
474  
475 0 ▶sub keywords {
Subroutine keywords: not called CC=1 SCAR=6.9
Pod documented
476 0 my $self = shift();
477 0 return $self->info_metadata('Keywords', @_);
478 }
479  
480 =head2 creator
481  
482 $creator = $pdf->creator();
483 $pdf = $pdf->creator($creator);
484  
485 Get/set/clear the name of the product that created the document prior to its
486 conversion to PDF.
487  
488 =cut
489  
490 0 ▶sub creator {
Subroutine creator: not called CC=1 SCAR=6.9
Pod documented
491 0 my $self = shift();
492 0 return $self->info_metadata('Creator', @_);
493 }
494  
495 =head2 producer
496  
497 $producer = $pdf->producer();
498 $pdf = $pdf->producer($producer);
499  
500 Get/set/clear the name of the product that converted the original document to
501 PDF.
502  
503 PDF::API2 fills in this field when creating a PDF.
504  
505 =cut
506  
507 169 ▶sub producer {
Subroutine producer: called CC=1 SCAR=0.0
Pod documented
508 169 my $self = shift();
509 169 return $self->info_metadata('Producer', @_);
510 }
511  
512 =head2 created
513  
514 $date = $pdf->created();
515 $pdf = $pdf->created($date);
516  
517 Get/set/clear the document's creation date.
518  
519 The date format is C<D:YYYYMMDDHHmmSSOHH'mm>, where C<D:> is a static prefix
520 identifying the string as a PDF date. The date may be truncated at any point
521 after the year. C<O> is one of C<+>, C<->, or C<Z>, with the following C<HH'mm>
522 representing an offset from UTC.
523  
524 When setting the date, C<D:> will be prepended automatically if omitted.
525  
526 =cut
527  
528 1 ▶sub created {
Subroutine created: called CC=1 SCAR=0.0
Pod documented
529 1 my $self = shift();
530 1 return $self->info_metadata('CreationDate', @_);
531 }
532  
533 =head2 modified
534  
535 $date = $pdf->modified();
536 $pdf = $pdf->modified($date);
537  
538 Get/set/clear the document's modification date. The date format is as described
539 in C<created> above.
540  
541 =cut
542  
543 0 ▶sub modified {
Subroutine modified: not called CC=1 SCAR=6.9
Pod documented
544 0 my $self = shift();
545 0 return $self->info_metadata('ModDate', @_);
546 }
547  
548 32 ▶sub _is_date {
Subroutine _is_date: called CC=24 SCAR=32.0
549 32 my $value = shift();
550  
551 # PDF 1.7 section 7.9.4 describes the required date format. Other than the
552 # D: prefix and the year, all components are optional but must be present if
553 # a later component is present. No provision is made in the specification
554 # for leap seconds, etc.
555 #
556 # The Adobe PDF specifications (including 1.7) state that the offset minutes
557 # must have a trailing apostrophe. Beginning with the ISO version of the
558 # 1.7 specification, a trailing apostrophe is not permitted after the offset
559 # minutes. For compatibility, we accept either version as valid.
560 32 ▶ return unless $value =~ /^D:([0-9]{4}) # D:YYYY (required)
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^D:([0-9]{4}) # D:YYYY (required) (?:([01][0-9]) # Month (01-12) (?:([0123][0-9]) # Day (01-31) (?:([012][0-9]) # Hour (00-23) (?:([012345][0-9]) # Minute (00-59) (?:([012345][0-9]) # Second (00-59) (?:([Z+-]) # UT Offset Direction (?:([012][0-9])\'? # UT Offset Hours (?:([012345][0-9])\'? # UT Offset Minutes )?)?)?)?)?)?)?)?$/x 10 22 32
561 (?:([01][0-9]) # Month (01-12)
562 (?:([0123][0-9]) # Day (01-31)
563 (?:([012][0-9]) # Hour (00-23)
564 (?:([012345][0-9]) # Minute (00-59)
565 (?:([012345][0-9]) # Second (00-59)
566 (?:([Z+-]) # UT Offset Direction
567 (?:([012][0-9])\'? # UT Offset Hours
568 (?:([012345][0-9])\'? # UT Offset Minutes
569 )?)?)?)?)?)?)?)?$/x;
570 22 my ($year, $month, $day, $hour, $minute, $second, $od, $oh, $om)
571 = ($1, $2, $3, $4, $5, $6, $7, $8, $9);
572  
573 # Do some basic validation to catch accidental date formatting issues.
574 # Complete date validation is out of scope.
575 22 ▶ if (defined $month) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $month) 21 1 22
576 21 ▶ return unless $month >= 1 and $month <= 12;
Branch: 1 branch
BranchTrueFalseTotal
unless $month >= 1 and $month <= 12 1 20 21
Condition66%2/3
$month >= 1 and $month <= 12
!ll&&!rl&&r
0120
MC/DC50%1/2
$month >= 1 and $month <= 12
$month >= 1 $month <= 12
Truth table66%2/3
A and B
A= $month >= 1B= $month <= 12
ABresult
0X0
100
111
577 }
578 21 ▶ if (defined $day) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $day) 19 2 21
579 19 ▶ return unless $day >= 1 and $day <= 31;
Branch: 1 branch
BranchTrueFalseTotal
unless $day >= 1 and $day <= 31 1 18 19
Condition66%2/3
$day >= 1 and $day <= 31
!ll&&!rl&&r
0118
MC/DC50%1/2
$day >= 1 and $day <= 31
$day >= 1 $day <= 31
Truth table66%2/3
A and B
A= $day >= 1B= $day <= 31
ABresult
0X0
100
111
580 }
581 20 ▶ if (defined $hour) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hour) 17 3 20
582 17 ▶ return unless $hour <= 23;
Branch: 1 branch
BranchTrueFalseTotal
unless $hour <= 23 1 16 17
583 }
584 19 ▶ if (defined $minute) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $minute) 15 4 19
585 15 ▶ return unless $minute <= 59;
Branch: 1 branch
BranchTrueFalseTotal
unless $minute <= 59 0 15 15
586 }
587 19 ▶ if (defined $second) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $second) 14 5 19
588 14 ▶ return unless $second <= 59;
Branch: 1 branch
BranchTrueFalseTotal
unless $second <= 59 0 14 14
589 }
590 19 ▶ if (defined $od) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $od) 13 6 19
591 13 ▶ return if $od eq 'Z' and defined($oh);
Branch: 1 branch
BranchTrueFalseTotal
if $od eq "Z" and defined $oh 0 13 13
Condition66%2/3
$od eq "Z" and defined $oh
!ll&&!rl&&r
1120
MC/DC0%0/2
$od eq "Z" and defined $oh
$od eq "Z" defined $oh
Truth table66%2/3
A and B
A= $od eq "Z"B= defined $oh
ABresult
0X0
100
111
592 }
593 19 ▶ if (defined $oh) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $oh) 11 8 19
594 11 ▶ return unless $oh <= 23;
Branch: 1 branch
BranchTrueFalseTotal
unless $oh <= 23 2 9 11
595 }
596 17 ▶ if (defined $om) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $om) 6 11 17
597 6 ▶ return unless $om <= 59;
Branch: 1 branch
BranchTrueFalseTotal
unless $om <= 59 0 6 6
598 }
599 17 ▶ if (defined $oh and $om) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $oh and $om) 6 11 17
Condition100%3/3
defined $oh and $om
!ll&&!rl&&r
836
MC/DC100%2/2
defined $oh and $om
defined $oh $om
Truth table100%3/3
A and B
A= defined $ohB= $om
ABresult
0X0
100
111
600 # Apostrophe is required between offset hour and minute
601 6 ▶ return unless $value =~ /$oh\'$om\'?/;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /$oh\'$om\'?/ 2 4 6
602 }
603  
604 15 return 1;
605 }
606  
607 =head2 info_metadata
608  
609 # Get all keys and values
610 %info = $pdf->info_metadata();
611  
612 # Get the value of one key
613 $value = $pdf->info_metadata($key);
614  
615 # Set the value of one key
616 $pdf = $pdf->info_metadata($key, $value);
617  
618 Get/set/clear a key in the document's information dictionary. The standard keys
619 (title, author, etc.) have their own accessors, so this is primarily intended
620 for interacting with custom metadata.
621  
622 Pass C<undef> as the value in order to remove the key from the dictionary.
623  
624 =cut
625  
626 170 ▶sub info_metadata {
Subroutine info_metadata: called CC=19 SCAR=35.5
Pod documented
627 170 my $self = shift();
628 170 my $field = shift();
629  
630 # Return a hash of the Info table if called without arguments
631 170 ▶ unless (defined $field) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $field) 0 170 170
632 0 ▶ return unless exists $self->{'pdf'}->{'Info'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"pdf"}{"Info"} 0 0 0
633 0 $self->{'pdf'}->{'Info'}->realise();
634 0 my %info;
635 0 foreach my $key (keys %{$self->{'pdf'}->{'Info'}}) {
636 0 ▶ next if $key =~ /^ /;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^ / 0 0 0
637 0 ▶ next unless defined $self->{'pdf'}->{'Info'}->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"pdf"}{"Info"}{$key} 0 0 0
638 0 $info{$key} = $self->{'pdf'}->{'Info'}->{$key}->val();
639 }
640 0 return %info;
641 }
642  
643 # Set
644 170 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 167 3 170
645 167 my $value = shift();
646 167 ▶ $value = undef if defined($value) and not length($value);
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and not length $value 0 167 167
Condition66%2/3
defined $value and not length $value
!ll&&!rl&&r
11660
MC/DC0%0/2
defined $value and not length $value
defined $value not length $value
Truth table66%2/3
A and not(B)
A= defined $valueB= not length $value
ABresult
0X0
100
111
647  
648 167 ▶ if ($field eq 'CreationDate' or $field eq 'ModDate') {
Branch: 1 branch
BranchTrueFalseTotal
if ($field eq "CreationDate" or $field eq "ModDate") 1 166 167
Condition66%2/3
$field eq "CreationDate" or $field eq "ModDate"
l!l&&r!l&&!r
10166
MC/DC50%1/2
$field eq "CreationDate" or $field eq "ModDate"
$field eq "CreationDate" $field eq "ModDate"
Truth table66%2/3
A or B
A= $field eq "CreationDate"B= $field eq "ModDate"
ABresult
1X1
011
000
649 1 ▶ if (defined ($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 1 0 1
650 1 ▶ $value = 'D:' . $value unless $value =~ /^D:/;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^D:/ 0 1 1
651 1 ▶ croak "Invalid date string: $value" unless _is_date($value);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_date($value) 0 1 1
652 }
653 }
654  
655 167 ▶ unless (exists $self->{'pdf'}->{'Info'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'pdf'}{'Info'}) { } 3 164 167
656 164 ▶ return $self unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 164 164
657 164 $self->{'pdf'}->{'Info'} = PDFDict();
658 164 $self->{'pdf'}->new_obj($self->{'pdf'}->{'Info'});
659 }
660 else {
661 3 $self->{'pdf'}->{'Info'}->realise();
662 }
663  
664 167 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 166 1 167
665 166 $self->{'pdf'}->{'Info'}->{$field} = PDFStr($value);
666 }
667 else {
668 1 delete $self->{'pdf'}->{'Info'}->{$field};
669 }
670  
671 167 return $self;
672 }
673  
674 # Get
675 3 ▶ return unless $self->{'pdf'}->{'Info'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"pdf"}{"Info"} 0 3 3
676 3 $self->{'pdf'}->{'Info'}->realise();
677 3 ▶ return unless $self->{'pdf'}->{'Info'}->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"pdf"}{"Info"}{$field} 1 2 3
678 2 return $self->{'pdf'}->{'Info'}->{$field}->val();
679 }
680  
681 # Deprecated; replace with individual accessors or info_metadata
682 3 ▶sub info {
Subroutine info: called CC=11 SCAR=25.9
Pod documented
683 3 my ($self, %opt) = @_;
684  
685 3 ▶ if (not defined($self->{'pdf'}->{'Info'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{'pdf'}{'Info'}) { } 0 3 3
686 0 $self->{'pdf'}->{'Info'} = PDFDict();
687 0 $self->{'pdf'}->new_obj($self->{'pdf'}->{'Info'});
688 }
689 else {
690 3 $self->{'pdf'}->{'Info'}->realise();
691 }
692  
693 # Maintenance Note: Since we're not shifting at the beginning of
694 # this sub, this "if" will always be true
695 3 ▶ if (scalar @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 3 0 3
696 3 foreach my $k (@{$self->{'infoMeta'}}) {
697 24 ▶ next unless defined $opt{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt{$k} 23 1 24
698 1 ▶ $self->{'pdf'}->{'Info'}->{$k} = PDFStr($opt{$k} || 'NONE');
Condition50%1/2
$opt{$k} || 'NONE'
l!l
10
MC/DC0%0/1
$opt{$k} || 'NONE'
$opt{$k}
Truth table50%1/2
A
A= $opt{$k}
Aresult
11
00
699 }
700 3 $self->{'pdf'}->out_obj($self->{'pdf'}->{'Info'});
701 }
702  
703 3 ▶ if (defined $self->{'pdf'}->{'Info'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"pdf"}{"Info"}) 3 0 3
704 3 %opt = ();
705 3 foreach my $k (@{$self->{'infoMeta'}}) {
706 24 ▶ next unless defined $self->{'pdf'}->{'Info'}->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"pdf"}{"Info"}{$k} 21 3 24
707 3 $opt{$k} = $self->{'pdf'}->{'Info'}->{$k}->val();
708 3 ▶ if ( (unpack('n', $opt{$k}) == 0xfffe)
Branch: 1 branch
BranchTrueFalseTotal
if (unpack("n", $opt{$k}) == 65534 or unpack("n", $opt{$k}) == 65279) 0 3 3
Condition33%1/3
unpack("n", $opt{$k}) == 65534 or unpack("n", $opt{$k}) == 65279
l!l&&r!l&&!r
003
MC/DC0%0/2
unpack("n", $opt{$k}) == 65534 or unpack("n", $opt{$k}) == 65279
unpack("n", $opt{$k}) == 65534 unpack("n", $opt{$k}) == 65279
Truth table33%1/3
A or B
A= unpack("n", $opt{$k}) == 65534B= unpack("n", $opt{$k}) == 65279
ABresult
1X1
011
000
709 or (unpack('n', $opt{$k}) == 0xfeff))
710 {
711 0 $opt{$k} = decode('UTF-16', $self->{'pdf'}->{'Info'}->{$k}->val());
712 }
713 }
714 }
715  
716 3 return %opt;
717 }
718  
719 # Deprecated; replace with info_metadata
720 0 ▶sub infoMetaAttributes {
Subroutine infoMetaAttributes: not called CC=2 SCAR=17.9
Pod documented
721 0 my ($self, @attr) = @_;
722  
723 0 ▶ if (scalar @attr) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @attr) 0 0 0
724 0 my %at = map { $_ => 1 } @{$self->{'infoMeta'}}, @attr;
725 0 @{$self->{'infoMeta'}} = keys %at;
726 }
727  
728 0 return @{$self->{'infoMeta'}};
729 }
730  
731 =head2 xml_metadata
732  
733 $xml = $pdf->xml_metadata();
734 $pdf = $pdf->xml_metadata($xml);
735  
736 Get/set the document's XML metadata stream.
737  
738 =cut
739  
740 # Deprecated (renamed, changed set return value for consistency)
741 0 ▶sub xmpMetadata {
Subroutine xmpMetadata: not called CC=2 SCAR=17.9
Pod documented
742 0 my $self = shift();
743 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
744 0 my $value = shift();
745 0 $self->xml_metadata($value);
746 0 return $value;
747 }
748  
749 0 return $self->xml_metadata();
750 }
751  
752 0 ▶sub xml_metadata {
Subroutine xml_metadata: not called CC=3 SCAR=24.8
Pod documented
753 0 my ($self, $value) = @_;
754  
755 0 ▶ if (not defined($self->{'catalog'}->{'Metadata'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{'catalog'}{'Metadata'}) { } 0 0 0
756 0 $self->{'catalog'}->{'Metadata'} = PDFDict();
757 0 $self->{'catalog'}->{'Metadata'}->{'Type'} = PDFName('Metadata');
758 0 $self->{'catalog'}->{'Metadata'}->{'Subtype'} = PDFName('XML');
759 0 $self->{'pdf'}->new_obj($self->{'catalog'}->{'Metadata'});
760 }
761 else {
762 0 $self->{'catalog'}->{'Metadata'}->realise();
763 0 $self->{'catalog'}->{'Metadata'}->{' stream'} = unfilter($self->{'catalog'}->{'Metadata'}->{'Filter'}, $self->{'catalog'}->{'Metadata'}->{' stream'});
764 0 delete $self->{'catalog'}->{'Metadata'}->{' nofilt'};
765 0 delete $self->{'catalog'}->{'Metadata'}->{'Filter'};
766 }
767  
768 0 my $md = $self->{'catalog'}->{'Metadata'};
769  
770 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
771 0 $md->{' stream'} = $value;
772 0 delete $md->{'Filter'};
773 0 delete $md->{' nofilt'};
774 0 $self->{'pdf'}->out_obj($md);
775 0 $self->{'pdf'}->out_obj($self->{'catalog'});
776 }
777  
778 0 return $md->{' stream'};
779 }
780  
781 =head2 version
782  
783 $version = $pdf->version($new_version);
784  
785 Get/set the PDF version (e.g. 1.4).
786  
787 =cut
788  
789 5 ▶sub version {
Subroutine version: called CC=1 SCAR=0.0
Pod documented
790 5 my $self = shift();
791 5 return $self->{'pdf'}->version(@_);
792 }
793  
794 =head2 is_encrypted
795  
796 $boolean = $pdf->is_encrypted();
797  
798 Returns true if the opened PDF is encrypted.
799  
800 =cut
801  
802 # Deprecated (renamed)
803 0 ▶sub isEncrypted { return is_encrypted(@_) }
Subroutine isEncrypted: not called CC=1 SCAR=6.9
Pod documented
804  
805 0 ▶sub is_encrypted {
Subroutine is_encrypted: not called CC=2 SCAR=17.9
Pod documented
806 0 my $self = shift();
807 0 ▶ return defined($self->{'pdf'}->{'Encrypt'}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'pdf'}{'Encrypt'} ? : 0 0 0
808 }
809  
810 =head1 INTERACTIVE FEATURE METHODS
811  
812 =head2 outline
813  
814 $outline = $pdf->outline();
815  
816 Creates (if needed) and returns the document's outline tree, which is also known
817 as its bookmarks or the table of contents, depending on the PDF reader.
818  
819 To examine or modify the outline tree, see L<PDF::API2::Outline>.
820  
821 =cut
822  
823 # Deprecated (renamed)
824 4 ▶sub outlines { return outline(@_) }
Subroutine outlines: called CC=1 SCAR=0.0
Pod documented
825  
826 4 ▶sub outline {
Subroutine outline: called CC=3 SCAR=11.0
Pod documented
827 4 my $self = shift();
828  
829 4 require PDF::API2::Outlines;
830 4 my $obj = $self->{'pdf'}->{'Root'}->{'Outlines'};
831 4 ▶ if ($obj) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj) { } 1 3 4
832 1 $obj->realise();
833 1 bless $obj, 'PDF::API2::Outlines';
834 1 $obj->{' api'} = $self;
835 1 weaken $obj->{' api'};
836 }
837 else {
838 3 $obj = PDF::API2::Outlines->new($self);
839  
840 3 $self->{'pdf'}->{'Root'}->{'Outlines'} = $obj;
841 3 ▶ $self->{'pdf'}->new_obj($obj) unless $obj->is_obj($self->{'pdf'});
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->is_obj($self->{"pdf"}) 3 0 3
842 3 $self->{'pdf'}->out_obj($obj);
843 3 $self->{'pdf'}->out_obj($self->{'pdf'}->{'Root'});
844 }
845  
846 4 return $obj;
847 }
848  
849 =head2 open_action
850  
851 $pdf = $pdf->open_action($page, $location, @args);
852  
853 Set the destination in the PDF that should be displayed when the document is
854 opened.
855  
856 C<$page> may be either a page number or a page object. The other parameters are
857 as described in L<PDF::API2::NamedDestination>.
858  
859 =cut
860  
861 2 ▶sub open_action {
Subroutine open_action: called CC=2 SCAR=6.9
Pod documented
862 2 my ($self, $page, @args) = @_;
863  
864 # $page can be either a page number or a page object
865 2 ▶ $page = PDFNum($page) unless ref($page);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $page 1 1 2
866  
867 2 require PDF::API2::NamedDestination;
868 2 my $array = PDF::API2::NamedDestination::_destination($page, @args);
869 2 $self->{'catalog'}->{'OpenAction'} = $array;
870 2 $self->{'pdf'}->out_obj($self->{'catalog'});
871 2 return $self;
872 }
873  
874 =head2 page_layout
875  
876 $layout = $pdf->page_layout();
877 $pdf = $pdf->page_layout($layout);
878  
879 Get/set the page layout that should be used when the PDF is opened.
880  
881 C<$layout> is one of the following:
882  
883 =over
884  
885 =item * single_page (or undef)
886  
887 Display one page at a time.
888  
889 =item * one_column
890  
891 Display the pages in one column (a.k.a. continuous).
892  
893 =item * two_column_left
894  
895 Display the pages in two columns, with odd-numbered pages on the left.
896  
897 =item * two_column_right
898  
899 Display the pages in two columns, with odd-numbered pages on the right.
900  
901 =item * two_page_left
902  
903 Display two pages at a time, with odd-numbered pages on the left.
904  
905 =item * two_page_right
906  
907 Display two pages at a time, with odd-numbered pages on the right.
908  
909 =back
910  
911 =cut
912  
913 169 ▶sub page_layout {
Subroutine page_layout: called CC=17 SCAR=49.7
Pod documented
914 169 my $self = shift();
915  
916 169 ▶ unless (@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 0 169 169
917 0 ▶ return 'single_page' unless $self->{'catalog'}->{'PageLayout'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"catalog"}{"PageLayout"} 0 0 0
918 0 my $layout = $self->{'catalog'}->{'PageLayout'}->val();
919 0 ▶ return 'single_page' if $layout eq 'SinglePage';
Branch: 1 branch
BranchTrueFalseTotal
if $layout eq "SinglePage" 0 0 0
920 0 ▶ return 'one_column' if $layout eq 'OneColumn';
Branch: 1 branch
BranchTrueFalseTotal
if $layout eq "OneColumn" 0 0 0
921 0 ▶ return 'two_column_left' if $layout eq 'TwoColumnLeft';
Branch: 1 branch
BranchTrueFalseTotal
if $layout eq "TwoColumnLeft" 0 0 0
922 0 ▶ return 'two_column_right' if $layout eq 'TwoColumnRight';
Branch: 1 branch
BranchTrueFalseTotal
if $layout eq "TwoColumnRight" 0 0 0
923 0 ▶ return 'two_page_left' if $layout eq 'TwoPageLeft';
Branch: 1 branch
BranchTrueFalseTotal
if $layout eq "TwoPageLeft" 0 0 0
924 0 ▶ return 'two_page_right' if $layout eq 'TwoPageRight';
Branch: 1 branch
BranchTrueFalseTotal
if $layout eq "TwoPageRight" 0 0 0
925 0 warn "Unknown page layout: $layout";
926 0 return $layout;
927 }
928  
929 169 ▶ my $name = shift() // 'single_page';
Condition50%1/2
shift() // "single_page"
l!l
1690
MC/DC0%0/1
shift() // "single_page"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
930 169 ▶ my $layout = ($name eq 'single_page' ? 'SinglePage' :
Branch: 6 branches
BranchTrueFalseTotal
$name eq 'single_page' ? : 169 0 169
$name eq 'one_column' ? : 0 0 0
$name eq 'two_column_left' ? : 0 0 0
$name eq 'two_column_right' ? : 0 0 0
$name eq 'two_page_left' ? : 0 0 0
$name eq 'two_page_right' ? : 0 0 0
931 $name eq 'one_column' ? 'OneColumn' :
932 $name eq 'two_column_left' ? 'TwoColumnLeft' :
933 $name eq 'two_column_right' ? 'TwoColumnRight' :
934 $name eq 'two_page_left' ? 'TwoPageLeft' :
935 $name eq 'two_page_right' ? 'TwoPageRight' : '');
936  
937 169 ▶ croak "Invalid page layout: $name" unless $layout;
Branch: 1 branch
BranchTrueFalseTotal
unless $layout 0 169 169
938 169 $self->{'catalog'}->{'PageLayout'} = PDFName($layout);
939 169 $self->{'pdf'}->out_obj($self->{'catalog'});
940 169 return $self;
941 }
942  
943 =head2 page_mode
944  
945 # Get
946 $mode = $pdf->page_mode();
947  
948 # Set
949 $pdf = $pdf->page_mode($mode);
950  
951 Get/set the page mode, which describes how the PDF should be displayed when
952 opened.
953  
954 C<$mode> is one of the following:
955  
956 =over
957  
958 =item * none (or undef)
959  
960 Neither outlines nor thumbnails should be displayed.
961  
962 =item * outlines
963  
964 Show the document outline.
965  
966 =item * thumbnails
967  
968 Show the page thumbnails.
969  
970 =item * full_screen
971  
972 Open in full-screen mode, with no menu bar, window controls, or any other window
973 visible.
974  
975 =item * optional_content
976  
977 Show the optional content group panel.
978  
979 =item * attachments
980  
981 Show the attachments panel.
982  
983 =back
984  
985 =cut
986  
987 169 ▶sub page_mode {
Subroutine page_mode: called CC=17 SCAR=49.7
Pod documented
988 169 my $self = shift();
989  
990 169 ▶ unless (@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 0 169 169
991 0 ▶ return 'none' unless $self->{'catalog'}->{'PageMode'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"catalog"}{"PageMode"} 0 0 0
992 0 my $mode = $self->{'catalog'}->{'PageMode'}->val();
993 0 ▶ return 'none' if $mode eq 'UseNone';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "UseNone" 0 0 0
994 0 ▶ return 'outlines' if $mode eq 'UseOutlines';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "UseOutlines" 0 0 0
995 0 ▶ return 'thumbnails' if $mode eq 'UseThumbs';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "UseThumbs" 0 0 0
996 0 ▶ return 'full_screen' if $mode eq 'FullScreen';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "FullScreen" 0 0 0
997 0 ▶ return 'optional_content' if $mode eq 'UseOC';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "UseOC" 0 0 0
998 0 ▶ return 'attachments' if $mode eq 'UseAttachments';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "UseAttachments" 0 0 0
999 0 warn "Unknown page mode: $mode";
1000 0 return $mode;
1001 }
1002  
1003 169 ▶ my $name = shift() // 'none';
Condition50%1/2
shift() // "none"
l!l
1690
MC/DC0%0/1
shift() // "none"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1004 169 ▶ my $mode = ($name eq 'none' ? 'UseNone' :
Branch: 6 branches
BranchTrueFalseTotal
$name eq 'none' ? : 169 0 169
$name eq 'outlines' ? : 0 0 0
$name eq 'thumbnails' ? : 0 0 0
$name eq 'full_screen' ? : 0 0 0
$name eq 'optional_content' ? : 0 0 0
$name eq 'attachments' ? : 0 0 0
1005 $name eq 'outlines' ? 'UseOutlines' :
1006 $name eq 'thumbnails' ? 'UseThumbs' :
1007 $name eq 'full_screen' ? 'FullScreen' :
1008 $name eq 'optional_content' ? 'UseOC' :
1009 $name eq 'attachments' ? 'UseAttachments' : '');
1010  
1011 169 ▶ croak "Invalid page mode: $name" unless $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $mode 0 169 169
1012 169 $self->{'catalog'}->{'PageMode'} = PDFName($mode);
1013 169 $self->{'pdf'}->out_obj($self->{'catalog'});
1014 169 return $self;
1015 }
1016  
1017 =head2 viewer_preferences
1018  
1019 # Get
1020 %preferences = $pdf->viewer_preferences();
1021  
1022 # Set
1023 $pdf = $pdf->viewer_preferences(%preferences);
1024  
1025 Get or set PDF viewer preferences, as described in
1026 L<PDF::API2::ViewerPreferences>.
1027  
1028 =cut
1029  
1030 172 ▶sub viewer_preferences {
Subroutine viewer_preferences: called CC=2 SCAR=7.2
Pod documented
1031 172 my $self = shift();
1032 172 require PDF::API2::ViewerPreferences;
1033 172 my $prefs = PDF::API2::ViewerPreferences->new($self);
1034 172 ▶ unless (@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 0 172 172
1035 0 return $prefs->get_preferences();
1036 }
1037 172 return $prefs->set_preferences(@_);
1038 }
1039  
1040 # Deprecated; the various preferences have been split out into their own methods
1041 169 ▶sub preferences {
Subroutine preferences: called CC=31 SCAR=51.2
Pod documented
1042 169 my ($self, %options) = @_;
1043  
1044 # Page Mode Options
1045 169 ▶ if ($options{'-fullscreen'}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($options{'-fullscreen'}) { } 0 169 169
elsif ($options{'-thumbs'}) { } 0 169 169
elsif ($options{'-outlines'}) { } 0 169 169
1046 0 $self->page_mode('full_screen');
1047 }
1048 elsif ($options{'-thumbs'}) {
1049 0 $self->page_mode('thumbnails');
1050 }
1051 elsif ($options{'-outlines'}) {
1052 0 $self->page_mode('outlines');
1053 }
1054 else {
1055 169 $self->page_mode('none');
1056 }
1057  
1058 # Page Layout Options
1059 169 ▶ if ($options{'-singlepage'}) {
Branch: 4 branches
BranchTrueFalseTotal
if ($options{'-singlepage'}) { } 0 169 169
elsif ($options{'-onecolumn'}) { } 0 169 169
elsif ($options{'-twocolumnleft'}) { } 0 169 169
elsif ($options{'-twocolumnright'}) { } 0 169 169
1060 0 $self->page_layout('single_page');
1061 }
1062 elsif ($options{'-onecolumn'}) {
1063 0 $self->page_layout('one_column');
1064 }
1065 elsif ($options{'-twocolumnleft'}) {
1066 0 $self->page_layout('two_column_left');
1067 }
1068 elsif ($options{'-twocolumnright'}) {
1069 0 $self->page_layout('two_column_right');
1070 }
1071 else {
1072 169 $self->page_layout('single_page');
1073 }
1074  
1075 # Viewer Preferences
1076 169 ▶ if ($options{'-hidetoolbar'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"-hidetoolbar"}) 0 169 169
1077 0 $self->viewer_preferences(hide_toolbar => 1);
1078 }
1079 169 ▶ if ($options{'-hidemenubar'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"-hidemenubar"}) 0 169 169
1080 0 $self->viewer_preferences(hide_menubar => 1);
1081 }
1082 169 ▶ if ($options{'-hidewindowui'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"-hidewindowui"}) 0 169 169
1083 0 $self->viewer_preferences(hide_window_ui => 1);
1084 }
1085 169 ▶ if ($options{'-fitwindow'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"-fitwindow"}) 0 169 169
1086 0 $self->viewer_preferences(fit_window => 1);
1087 }
1088 169 ▶ if ($options{'-centerwindow'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"-centerwindow"}) 0 169 169
1089 0 $self->viewer_preferences(center_window => 1);
1090 }
1091 169 ▶ if ($options{'-displaytitle'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"-displaytitle"}) 0 169 169
1092 0 $self->viewer_preferences(display_doc_title => 1);
1093 }
1094 169 ▶ if ($options{'-righttoleft'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"-righttoleft"}) 0 169 169
1095 0 $self->viewer_preferences(direction => 'r2l');
1096 }
1097  
1098 169 ▶ if ($options{'-afterfullscreenthumbs'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($options{'-afterfullscreenthumbs'}) { } 0 169 169
elsif ($options{'-afterfullscreenoutlines'}) { } 0 169 169
1099 0 $self->viewer_preferences(non_full_screen_page_mode => 'thumbnails');
1100 }
1101 elsif ($options{'-afterfullscreenoutlines'}) {
1102 0 $self->viewer_preferences(non_full_screen_page_mode => 'outlines');
1103 }
1104 else {
1105 169 $self->viewer_preferences(non_full_screen_page_mode => 'none');
1106 }
1107  
1108 169 ▶ if ($options{'-printscalingnone'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"-printscalingnone"}) 0 169 169
1109 0 $self->viewer_preferences(print_scaling => 'none');
1110 }
1111  
1112 169 ▶ if ($options{'-simplex'}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($options{'-simplex'}) { } 1 168 169
elsif ($options{'-duplexfliplongedge'}) { } 1 167 168
elsif ($options{'-duplexflipshortedge'}) { } 1 166 167
1113 1 $self->viewer_preferences(duplex => 'simplex');
1114 }
1115 elsif ($options{'-duplexfliplongedge'}) {
1116 1 $self->viewer_preferences(duplex => 'duplex_long');
1117 }
1118 elsif ($options{'-duplexflipshortedge'}) {
1119 1 $self->viewer_preferences(duplex => 'duplex_short');
1120 }
1121  
1122 # Open Action
1123 169 ▶ if ($options{'-firstpage'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"-firstpage"}) 2 167 169
1124 2 my ($page, %args) = @{$options{'-firstpage'}};
1125 2 ▶ $args{'-fit'} = 1 unless keys %args;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %args 0 2 2
1126  
1127 2 ▶ if (defined $args{'-fit'}) {
Branch: 8 branches
BranchTrueFalseTotal
if (defined $args{'-fit'}) { } 2 0 2
elsif (defined $args{'-fith'}) { } 0 0 0
elsif (defined $args{'-fitb'}) { } 0 0 0
elsif (defined $args{'-fitbh'}) { } 0 0 0
elsif (defined $args{'-fitv'}) { } 0 0 0
elsif (defined $args{'-fitbv'}) { } 0 0 0
elsif (defined $args{'-fitr'}) { } 0 0 0
elsif (defined $args{'-xyz'}) { } 0 0 0
1128 2 $self->open_action($page, 'fit');
1129 }
1130 elsif (defined $args{'-fith'}) {
1131 0 $self->open_action($page, 'fith', $args{'-fith'});
1132 }
1133 elsif (defined $args{'-fitb'}) {
1134 0 $self->open_action($page, 'fitb');
1135 }
1136 elsif (defined $args{'-fitbh'}) {
1137 0 $self->open_action($page, 'fitbh', $args{'-fitbh'});
1138 }
1139 elsif (defined $args{'-fitv'}) {
1140 0 $self->open_action($page, 'fitv', $args{'-fitv'});
1141 }
1142 elsif (defined $args{'-fitbv'}) {
1143 0 $self->open_action($page, 'fitbv', $args{'-fitbv'});
1144 }
1145 elsif (defined $args{'-fitr'}) {
1146 0 $self->open_action($page, 'fitr', @{$args{'-fitr'}});
1147 }
1148 elsif (defined $args{'-xyz'}) {
1149 0 $self->open_action($page, 'xyz', @{$args{'-xyz'}});
1150 }
1151 }
1152 169 $self->{'pdf'}->out_obj($self->{'catalog'});
1153  
1154 169 return $self;
1155 }
1156  
1157 16 ▶sub proc_pages {
Subroutine proc_pages: called CC=6 SCAR=18.4
Pod undocumented
1158 16 my ($pdf, $object) = @_;
1159  
1160 16 ▶ if (defined $object->{'Resources'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $object->{"Resources"}) 16 0 16
1161 16 eval {
1162 16 $object->{'Resources'}->realise();
1163 };
1164 }
1165  
1166 16 my @pages;
1167 16 ▶ $pdf->{' apipagecount'} ||= 0;
Condition50%1/2
$pdf->{" apipagecount"} ||= 0
l!l
016
MC/DC0%0/1
$pdf->{" apipagecount"} ||= 0
$pdf->{" apipagecount"}
Truth table50%1/2
A
A= $pdf->{" apipagecount"}
Aresult
11
00
1168 16 foreach my $page ($object->{'Kids'}->elements()) {
1169 18 $page->realise();
1170 18 ▶ if ($page->{'Type'}->val() eq 'Pages') {
Branch: 1 branch
BranchTrueFalseTotal
if ($page->{'Type'}->val eq 'Pages') { } 0 18 18
1171 0 push @pages, proc_pages($pdf, $page);
1172 }
1173 else {
1174 18 $pdf->{' apipagecount'}++;
1175 18 $page->{' pnum'} = $pdf->{' apipagecount'};
1176 18 ▶ if (defined $page->{'Resources'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $page->{"Resources"}) 18 0 18
1177 18 eval {
1178 18 $page->{'Resources'}->realise();
1179 };
1180 }
1181 18 push @pages, $page;
1182 }
1183 }
1184  
1185 16 return @pages;
1186 }
1187  
1188 =head1 PAGE METHODS
1189  
1190 =head2 page
1191  
1192 # Add a page to the end of the document
1193 $page = $pdf->page();
1194  
1195 # Insert a page before the specified page number
1196 $page = $pdf->page($page_number);
1197  
1198 Returns a new page object. By default, the page is added to the end
1199 of the document. If you include an existing page number, the new page
1200 will be inserted in that position, pushing existing pages back.
1201  
1202 If C<$page_number> is -1, the new page is inserted as the second-last page; if
1203 C<$page_number> is 0, the new page is inserted as the last page.
1204  
1205 =cut
1206  
1207 142 ▶sub page {
Subroutine page: called CC=5 SCAR=16.2
Pod documented
1208 142 my $self = shift();
1209 142 ▶ my $index = shift() || 0;
Condition100%2/2
shift() || 0
l!l
2140
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1210 142 my $page;
1211 142 ▶ if ($index == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index == 0) { } 140 2 142
1212 140 $page = PDF::API2::Page->new($self->{'pdf'}, $self->{'pages'});
1213 }
1214 else {
1215 2 $page = PDF::API2::Page->new($self->{'pdf'}, $self->{'pages'}, $index - 1);
1216 }
1217 142 $page->{' apipdf'} = $self->{'pdf'};
1218 142 $page->{' api'} = $self;
1219 142 weaken $page->{' apipdf'};
1220 142 weaken $page->{' api'};
1221 142 $self->{'pdf'}->out_obj($page);
1222 142 $self->{'pdf'}->out_obj($self->{'pages'});
1223 142 ▶ if ($index == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($index == 0) { } 140 2 142
elsif ($index < 0) { } 0 2 2
1224 140 push @{$self->{'pagestack'}}, $page;
1225 140 weaken $self->{'pagestack'}->[-1];
1226 }
1227 elsif ($index < 0) {
1228 0 splice @{$self->{'pagestack'}}, $index, 0, $page;
1229 0 weaken $self->{'pagestack'}->[$index];
1230 }
1231 else {
1232 2 splice @{$self->{'pagestack'}}, $index - 1, 0, $page;
1233 2 weaken $self->{'pagestack'}->[$index - 1];
1234 }
1235 # $page->{'Resources'} = $self->{'pages'}->{'Resources'};
1236 142 return $page;
1237 }
1238  
1239 =head2 open_page
1240  
1241 $page = $pdf->open_page($page_number);
1242  
1243 Returns the L<PDF::API2::Page> object of page C<$page_number>, if it exists.
1244  
1245 If $page_number is 0 or -1, it will return the last page in the document.
1246  
1247 =cut
1248  
1249 # Deprecated (renamed)
1250 1 ▶sub openpage { return open_page(@_); } ## no critic
Subroutine openpage: called CC=1 SCAR=0.0
Pod documented
1251  
1252 6 ▶sub open_page {
Subroutine open_page: called CC=25 SCAR=46.6
Pod documented
1253 6 my $self = shift();
1254 6 ▶ my $index = shift() || 0;
Condition50%1/2
shift() || 0
l!l
60
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1255 6 my ($page, $rotate, $media, $trans);
1256  
1257 6 ▶ if ($index == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($index == 0) { } 0 6 6
elsif ($index < 0) { } 0 6 6
1258 0 $page = $self->{'pagestack'}->[-1];
1259 }
1260 elsif ($index < 0) {
1261 0 $page = $self->{'pagestack'}->[$index];
1262 }
1263 else {
1264 6 $page = $self->{'pagestack'}->[$index - 1];
1265 }
1266 6 ▶ return unless ref($page);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $page 0 6 6
1267  
1268 6 ▶ if (ref($page) ne 'PDF::API2::Page') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $page ne "PDF::API2::Page") 5 1 6
1269 5 bless $page, 'PDF::API2::Page';
1270 5 $page->{' apipdf'} = $self->{'pdf'};
1271 5 $page->{' api'} = $self;
1272 5 weaken $page->{' apipdf'};
1273 5 weaken $page->{' api'};
1274 5 $self->{'pdf'}->out_obj($page);
1275 5 ▶ if (($rotate = $page->find_prop('Rotate')) and not $page->{' opened'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rotate = $page->find_prop('Rotate') and not $page->{' opened'}) { } 0 5 5
Condition33%1/3
$rotate = $page->find_prop("Rotate") and not $page->{" opened"}
!ll&&!rl&&r
500
MC/DC0%0/2
$rotate = $page->find_prop("Rotate") and not $page->{" opened"}
$rotate = $page->find_prop("Rotate") not $page->{" opened"}
Truth table33%1/3
A and not(B)
A= $rotate = $page->find_prop("Rotate")B= not $page->{" opened"}
ABresult
0X0
100
111
1276 0 $rotate = ($rotate->val() + 360) % 360;
1277  
1278 0 ▶ if ($rotate != 0 and not $self->default('nounrotate')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rotate != 0 and not $self->default('nounrotate')) { } 0 0 0
Condition0%0/3
$rotate != 0 and not $self->default("nounrotate")
!ll&&!rl&&r
000
MC/DC0%0/2
$rotate != 0 and not $self->default("nounrotate")
$rotate != 0 not $self->default("nounrotate")
Truth table0%0/3
A and not(B)
A= $rotate != 0B= not $self->default("nounrotate")
ABresult
0X0
100
111
1279 0 $page->{'Rotate'} = PDFNum(0);
1280 0 foreach my $mediatype (qw(MediaBox CropBox BleedBox TrimBox ArtBox)) {
1281 0 ▶ if ($media = $page->find_prop($mediatype)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($media = $page->find_prop($mediatype)) { } 0 0 0
1282 0 $media = [ map { $_->val() } $media->elements() ];
1283 }
1284 else {
1285 0 $media = [0, 0, 612, 792];
1286 0 ▶ next if $mediatype ne 'MediaBox';
Branch: 1 branch
BranchTrueFalseTotal
if $mediatype ne "MediaBox" 0 0 0
1287 }
1288 0 ▶ if ($rotate == 90) {
Branch: 3 branches
BranchTrueFalseTotal
if ($rotate == 90) { } 0 0 0
elsif ($rotate == 180) { } 0 0 0
elsif ($rotate == 270) { } 0 0 0
1289 0 ▶ $trans = "0 -1 1 0 0 $media->[2] cm" if $mediatype eq 'MediaBox';
Branch: 1 branch
BranchTrueFalseTotal
if $mediatype eq "MediaBox" 0 0 0
1290 0 $media = [$media->[1], $media->[0], $media->[3], $media->[2]];
1291 }
1292 elsif ($rotate == 180) {
1293 0 ▶ $trans = "-1 0 0 -1 $media->[2] $media->[3] cm" if $mediatype eq 'MediaBox';
Branch: 1 branch
BranchTrueFalseTotal
if $mediatype eq "MediaBox" 0 0 0
1294 }
1295 elsif ($rotate == 270) {
1296 0 ▶ $trans = "0 1 -1 0 $media->[3] 0 cm" if $mediatype eq 'MediaBox';
Branch: 1 branch
BranchTrueFalseTotal
if $mediatype eq "MediaBox" 0 0 0
1297 0 $media = [$media->[1], $media->[0], $media->[3], $media->[2]];
1298 }
1299 0 $page->{$mediatype} = PDFArray(map { PDFNum($_) } @$media);
1300 }
1301 }
1302 else {
1303 0 $trans = '';
1304 }
1305 }
1306 else {
1307 5 $trans = '';
1308 }
1309  
1310 5 ▶ if (defined $page->{'Contents'} and not $page->{' opened'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $page->{"Contents"} and not $page->{" opened"}) 3 2 5
Condition66%2/3
defined $page->{"Contents"} and not $page->{" opened"}
!ll&&!rl&&r
203
MC/DC50%1/2
defined $page->{"Contents"} and not $page->{" opened"}
defined $page->{"Contents"} not $page->{" opened"}
Truth table66%2/3
A and not(B)
A= defined $page->{"Contents"}B= not $page->{" opened"}
ABresult
0X0
100
111
1311 3 $page->fixcontents();
1312 3 my $uncontent = delete $page->{'Contents'};
1313 3 my $content = $page->gfx();
1314 3 $content->add(" $trans ");
1315  
1316 3 ▶ if ($self->default('pageencaps')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->default("pageencaps")) 0 3 3
1317 0 $content->{' stream'} .= ' q ';
1318 }
1319 3 foreach my $k ($uncontent->elements()) {
1320 3 $k->realise();
1321 3 $content->{' stream'} .= ' ' . unfilter($k->{'Filter'}, $k->{' stream'}) . ' ';
1322 }
1323 3 ▶ if ($self->default('pageencaps')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->default("pageencaps")) 0 3 3
1324 0 $content->{' stream'} .= ' Q ';
1325 }
1326  
1327 # if we like compress we will do it now to do quicker saves
1328 3 ▶ if ($self->{'forcecompress'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"forcecompress"}) 3 0 3
1329 3 $content->{' stream'} = dofilter($content->{'Filter'}, $content->{' stream'});
1330 3 $content->{' nofilt'} = 1;
1331 3 delete $content->{'-docompress'};
1332 3 $content->{'Length'} = PDFNum(length($content->{' stream'}));
1333 }
1334 }
1335 5 $page->{' opened'} = 1;
1336 }
1337  
1338 6 $self->{'pdf'}->out_obj($page);
1339 6 $self->{'pdf'}->out_obj($self->{'pages'});
1340 6 $page->{' apipdf'} = $self->{'pdf'};
1341 6 $page->{' api'} = $self;
1342 6 weaken $page->{' apipdf'};
1343 6 weaken $page->{' api'};
1344 6 return $page;
1345 }
1346  
1347 =head2 import_page
1348  
1349 $page = $pdf->import_page($source_pdf, $source_page_num, $target_page_num);
1350  
1351 Imports a page from C<$source_pdf> and adds it to the specified position in
1352 C<$pdf>.
1353  
1354 If C<$source_page_num> or C<$target_page_num> is 0, -1, or unspecified, the last
1355 page in the document is used.
1356  
1357 B<Note:> If you pass a page object instead of a page number for
1358 C<$target_page_num>, the contents of the page will be merged into the existing
1359 page.
1360  
1361 B<Example:>
1362  
1363 my $pdf = PDF::API2->new();
1364 my $source = PDF::API2->open('source.pdf');
1365  
1366 # Add page 2 from the source PDF as page 1 of the new PDF
1367 my $page = $pdf->import_page($source, 2);
1368  
1369 $pdf->save('sample.pdf');
1370  
1371 B<Note:> You can only import a page from an existing PDF file.
1372  
1373 =cut
1374  
1375 # Deprecated (renamed)
1376 1 ▶sub importpage { return import_page(@_); } ## no critic
Subroutine importpage: called CC=1 SCAR=0.0
Pod documented
1377  
1378 1 ▶sub import_page {
Subroutine import_page: called CC=22 SCAR=50.4
Pod documented
1379 1 my ($self, $s_pdf, $s_idx, $t_idx) = @_;
1380 1 ▶ $s_idx ||= 0;
Condition50%1/2
$s_idx ||= 0
l!l
10
MC/DC0%0/1
$s_idx ||= 0
$s_idx
Truth table50%1/2
A
A= $s_idx
Aresult
11
00
1381 1 ▶ $t_idx ||= 0;
Condition50%1/2
$t_idx ||= 0
l!l
01
MC/DC0%0/1
$t_idx ||= 0
$t_idx
Truth table50%1/2
A
A= $t_idx
Aresult
11
00
1382 1 my ($s_page, $t_page);
1383  
1384 1 ▶ unless (ref($s_pdf) and $s_pdf->isa('PDF::API2')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $s_pdf and $s_pdf->isa("PDF::API2")) 0 1 1
Condition33%1/3
ref $s_pdf and $s_pdf->isa("PDF::API2")
!ll&&!rl&&r
001
MC/DC0%0/2
ref $s_pdf and $s_pdf->isa("PDF::API2")
ref $s_pdf $s_pdf->isa("PDF::API2")
Truth table33%1/3
A and B
A= ref $s_pdfB= $s_pdf->isa("PDF::API2")
ABresult
0X0
100
111
1385 0 die "Invalid usage: first argument must be PDF::API2 instance, not: " . ref($s_pdf);
1386 }
1387  
1388 1 ▶ if (ref($s_idx) eq 'PDF::API2::Page') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $s_idx eq 'PDF::API2::Page') { } 0 1 1
1389 0 $s_page = $s_idx;
1390 }
1391 else {
1392 1 $s_page = $s_pdf->open_page($s_idx);
1393 1 ▶ die "Unable to open page '$s_idx' in source PDF" unless defined $s_page;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s_page 0 1 1
1394 }
1395  
1396 1 ▶ if (ref($t_idx) eq 'PDF::API2::Page') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $t_idx eq 'PDF::API2::Page') { } 0 1 1
1397 0 $t_page = $t_idx;
1398 }
1399 else {
1400 1 ▶ if ($self->pages() < $t_idx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pages < $t_idx) { } 0 1 1
1401 0 $t_page = $self->page();
1402 }
1403 else {
1404 1 $t_page = $self->page($t_idx);
1405 }
1406 }
1407  
1408 # we now import into a form to keep
1409 # all that nasty resources from polluting
1410 # our very own resource naming space.
1411 1 my $xo = $self->embed_page($s_pdf, $s_page);
1412  
1413 # copy all page dimensions
1414 1 foreach my $k (qw(MediaBox ArtBox TrimBox BleedBox CropBox)) {
1415 5 my $prop = $s_page->find_prop($k);
1416 5 ▶ next unless defined $prop;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prop 5 0 5
1417  
1418 0 my $box = walk_obj({}, $s_pdf->{'pdf'}, $self->{'pdf'}, $prop);
1419 0 my $method = lc $k;
1420  
1421 0 $t_page->$method(map { $_->val() } $box->elements());
1422 }
1423  
1424 1 $t_page->gfx->formimage($xo, 0, 0, 1);
1425  
1426 # copy annotations and/or form elements as well
1427 1 ▶ if (exists $s_page->{'Annots'} and $s_page->{'Annots'} and $self->{'copyannots'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $s_page->{"Annots"} and $s_page->{"Annots"} and $self->{"copyannots"}) 0 1 1
Condition33%2/6
exists $s_page->{"Annots"} and $s_page->{"Annots"} and $self->{"copyannots"}
!ll&&!rl&&r
100
exists $s_page->{"Annots"} and $s_page->{"Annots"}
!ll&&!rl&&r
100
MC/DC0%0/3
exists $s_page->{"Annots"} and $s_page->{"Annots"} and $self->{"copyannots"}
exists $s_page->{"Annots"} $s_page->{"Annots"} $self->{"copyannots"}
Truth table25%1/4
A and B and C
A= exists $s_page->{"Annots"}B= $s_page->{"Annots"}C= $self->{"copyannots"}
ABCresult
0XX0
10X0
1100
1111
1428 # first set up the AcroForm, if required
1429 0 my $AcroForm;
1430 0 ▶ if (my $a = $s_pdf->{'pdf'}->{'Root'}->realise->{'AcroForm'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $a = $s_pdf->{"pdf"}{"Root"}->realise->{"AcroForm"}) 0 0 0
1431 0 $a->realise();
1432  
1433 0 $AcroForm = walk_obj({}, $s_pdf->{'pdf'}, $self->{'pdf'}, $a, qw(NeedAppearances SigFlags CO DR DA Q));
1434 }
1435 0 my @Fields = ();
1436 0 my @Annots = ();
1437 0 foreach my $a ($s_page->{'Annots'}->elements()) {
1438 0 $a->realise();
1439 0 my $t_a = PDFDict();
1440 0 $self->{'pdf'}->new_obj($t_a);
1441 # these objects are likely to be both annotations and Acroform fields
1442 # key names are copied from PDF Reference 1.4 (Tables)
1443 0 my @k = (
1444 qw( Type Subtype Contents P Rect NM M F BS Border AP AS C CA T Popup A AA StructParent Rotate
1445 ), # Annotations - Common (8.10)
1446 qw( Subtype Contents Open Name ), # Text Annotations (8.15)
1447 qw( Subtype Contents Dest H PA ), # Link Annotations (8.16)
1448 qw( Subtype Contents DA Q ), # Free Text Annotations (8.17)
1449 qw( Subtype Contents L BS LE IC ) , # Line Annotations (8.18)
1450 qw( Subtype Contents BS IC ), # Square and Circle Annotations (8.20)
1451 qw( Subtype Contents QuadPoints ), # Markup Annotations (8.21)
1452 qw( Subtype Contents Name ), # Rubber Stamp Annotations (8.22)
1453 qw( Subtype Contents InkList BS ), # Ink Annotations (8.23)
1454 qw( Subtype Contents Parent Open ), # Popup Annotations (8.24)
1455 qw( Subtype FS Contents Name ), # File Attachment Annotations (8.25)
1456 qw( Subtype Sound Contents Name ), # Sound Annotations (8.26)
1457 qw( Subtype Movie Contents A ), # Movie Annotations (8.27)
1458 qw( Subtype Contents H MK ), # Widget Annotations (8.28)
1459 # Printers Mark Annotations (none)
1460 # Trap Network Annotations (none)
1461 );
1462  
1463 0 ▶ push @k, (
Branch: 1 branch
BranchTrueFalseTotal
if $AcroForm 0 0 0
1464 qw( Subtype FT Parent Kids T TU TM Ff V DV AA
1465 ), # Fields - Common (8.49)
1466 qw( DR DA Q ), # Fields containing variable text (8.51)
1467 qw( Opt ), # Checkbox field (8.54)
1468 qw( Opt ), # Radio field (8.55)
1469 qw( MaxLen ), # Text field (8.57)
1470 qw( Opt TI I ), # Choice field (8.59)
1471 ) if $AcroForm;
1472  
1473 # sorting out dups
1474 0 my %ky = map { $_ => 1 } @k;
1475 # we do P separately, as it points to the page the Annotation is on
1476 0 delete $ky{'P'};
1477 # copy everything else
1478 0 foreach my $k (keys %ky) {
1479 0 ▶ next unless defined $a->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a->{$k} 0 0 0
1480 0 $a->{$k}->realise();
1481 0 $t_a->{$k} = walk_obj({}, $s_pdf->{'pdf'}, $self->{'pdf'}, $a->{$k});
1482 }
1483 0 $t_a->{'P'} = $t_page;
1484 0 push @Annots, $t_a;
1485 0 ▶ push @Fields, $t_a if ($AcroForm and $t_a->{'Subtype'}->val() eq 'Widget');
Branch: 1 branch
BranchTrueFalseTotal
if $AcroForm and $t_a->{"Subtype"}->val eq "Widget" 0 0 0
Condition0%0/3
$AcroForm and $t_a->{"Subtype"}->val eq "Widget"
!ll&&!rl&&r
000
MC/DC0%0/2
$AcroForm and $t_a->{"Subtype"}->val eq "Widget"
$AcroForm $t_a->{"Subtype"}->val eq "Widget"
Truth table0%0/3
A and B
A= $AcroFormB= $t_a->{"Subtype"}->val eq "Widget"
ABresult
0X0
100
111
1486 }
1487 0 $t_page->{'Annots'} = PDFArray(@Annots);
1488 0 ▶ $AcroForm->{'Fields'} = PDFArray(@Fields) if $AcroForm;
Branch: 1 branch
BranchTrueFalseTotal
if $AcroForm 0 0 0
1489 0 $self->{'pdf'}->{'Root'}->{'AcroForm'} = $AcroForm;
1490 }
1491 1 $t_page->{' imported'} = 1;
1492  
1493 1 $self->{'pdf'}->out_obj($t_page);
1494 1 $self->{'pdf'}->out_obj($self->{'pages'});
1495  
1496 1 return $t_page;
1497 }
1498  
1499 =head2 embed_page
1500  
1501 $xobject = $pdf->embed_page($source_pdf, $source_page_number);
1502  
1503 Returns a Form XObject created by extracting the specified page from a
1504 C<$source_pdf>.
1505  
1506 This is useful if you want to transpose the imported page somewhat differently
1507 onto a page (e.g. two-up, four-up, etc.).
1508  
1509 If $source_page_number is 0 or -1, it will return the last page in the document.
1510  
1511 B<Example:>
1512  
1513 my $pdf = PDF::API2->new();
1514 my $source = PDF::API2->open('source.pdf');
1515 my $page = $pdf->page();
1516  
1517 # Import Page 2 from the source PDF
1518 my $object = $pdf->embed_page($source, 2);
1519  
1520 # Add it to the new PDF's first page at 1/2 scale
1521 my ($x, $y) = (0, 0);
1522 $page->object($object, $x, $y, 0.5);
1523  
1524 $pdf->save('sample.pdf');
1525  
1526 B<Note:> You can only import a page from an existing PDF file.
1527  
1528 =cut
1529  
1530 # Deprecated (renamed)
1531 3 ▶sub importPageIntoForm { return embed_page(@_) }
Subroutine importPageIntoForm: called CC=1 SCAR=0.0
Pod documented
1532  
1533 4 ▶sub embed_page {
Subroutine embed_page: called CC=24 SCAR=33.9
Pod documented
1534 4 my ($self, $s_pdf, $s_idx) = @_;
1535 4 ▶ $s_idx ||= 0;
Condition50%1/2
$s_idx ||= 0
l!l
40
MC/DC0%0/1
$s_idx ||= 0
$s_idx
Truth table50%1/2
A
A= $s_idx
Aresult
11
00
1536  
1537 4 ▶ unless (ref($s_pdf) and $s_pdf->isa('PDF::API2')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $s_pdf and $s_pdf->isa("PDF::API2")) 0 4 4
Condition33%1/3
ref $s_pdf and $s_pdf->isa("PDF::API2")
!ll&&!rl&&r
004
MC/DC0%0/2
ref $s_pdf and $s_pdf->isa("PDF::API2")
ref $s_pdf $s_pdf->isa("PDF::API2")
Truth table33%1/3
A and B
A= ref $s_pdfB= $s_pdf->isa("PDF::API2")
ABresult
0X0
100
111
1538 0 croak "Invalid usage: first argument must be PDF::API2 instance, not: " . ref($s_pdf);
1539 }
1540  
1541 4 my ($s_page, $xo);
1542  
1543 4 $xo = $self->xo_form();
1544  
1545 4 ▶ if (ref($s_idx) eq 'PDF::API2::Page') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $s_idx eq 'PDF::API2::Page') { } 1 3 4
1546 1 $s_page = $s_idx;
1547 }
1548 else {
1549 3 $s_page = $s_pdf->open_page($s_idx);
1550 3 ▶ croak "Unable to open page $s_idx in source PDF" unless defined $s_page;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s_page 0 3 3
1551 }
1552  
1553 4 ▶ $s_pdf->{'embed_page_cache_id'} ||= pdfkey();
Condition33%1/3
$s_pdf->{"embed_page_cache_id"} ||= pdfkey()
l!l&&r!l&&!r
040
MC/DC0%0/2
$s_pdf->{"embed_page_cache_id"} ||= pdfkey()
$s_pdf->{"embed_page_cache_id"} pdfkey()
Truth table33%1/3
A ||= B
A= $s_pdf->{"embed_page_cache_id"}B= pdfkey()
ABresult
1X1
011
000
1554 4 ▶ $self->{'apiimportcache'} ||= {};
Condition50%1/2
$self->{"apiimportcache"} ||= {}
l!l
04
MC/DC0%0/1
$self->{"apiimportcache"} ||= {}
$self->{"apiimportcache"}
Truth table50%1/2
A
A= $self->{"apiimportcache"}
Aresult
11
00
1555 4 ▶ $self->{'apiimportcache'}->{$s_pdf->{'embed_page_cache_id'}} ||= {};
Condition50%1/2
$self->{"apiimportcache"}{$s_pdf->{"embed_page_cache_id"}} ||= {}
l!l
04
MC/DC0%0/1
$self->{"apiimportcache"}{$s_pdf->{"embed_page_cache_id"}} ||= {}
$self->{"apiimportcache"}{$s_pdf->{"embed_page_cache_id"}}
Truth table50%1/2
A
A= $self->{"apiimportcache"}{$s_pdf->{"embed_page_cache_id"}}
Aresult
11
00
1556 4 my $cache = $self->{'apiimportcache'}->{$s_pdf->{'embed_page_cache_id'}};
1557  
1558 # This should never get past MediaBox, since it's a required object.
1559 4 foreach my $k (qw(MediaBox ArtBox TrimBox BleedBox CropBox)) {
1560 # next unless defined $s_page->{$k};
1561 # my $box = walk_obj($cache, $s_pdf->{'pdf'}, $self->{'pdf'}, $s_page->{$k});
1562 12 ▶ next unless defined $s_page->find_prop($k);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s_page->find_prop($k) 10 2 12
1563 2 my $box = walk_obj($cache, $s_pdf->{'pdf'}, $self->{'pdf'}, $s_page->find_prop($k));
1564 2 $xo->bbox(map { $_->val() } $box->elements());
1565 2 last;
1566 }
1567 4 ▶ $xo->bbox(0, 0, 612, 792) unless defined $xo->{'BBox'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $xo->{"BBox"} 2 2 4
1568  
1569 4 foreach my $k (qw(Resources)) {
1570 4 $s_page->{$k} = $s_page->find_prop($k);
1571 4 ▶ next unless defined $s_page->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s_page->{$k} 0 4 4
1572 4 ▶ $s_page->{$k}->realise() if ref($s_page->{$k}) =~ /Objind$/;
Branch: 1 branch
BranchTrueFalseTotal
if ref($s_page->{$k}) =~ /Objind$/ 0 4 4
1573  
1574 4 foreach my $sk (qw(XObject ExtGState Font ProcSet Properties ColorSpace Pattern Shading)) {
1575 32 ▶ next unless defined $s_page->{$k}->{$sk};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s_page->{$k}{$sk} 27 5 32
1576 5 ▶ $s_page->{$k}->{$sk}->realise() if ref($s_page->{$k}->{$sk}) =~ /Objind$/;
Branch: 1 branch
BranchTrueFalseTotal
if ref($s_page->{$k}{$sk}) =~ /Objind$/ 0 5 5
1577 5 foreach my $ssk (keys %{$s_page->{$k}->{$sk}}) {
1578 10 ▶ next if $ssk =~ /^ /;
Branch: 1 branch
BranchTrueFalseTotal
if $ssk =~ /^ / 9 1 10
1579 1 $xo->resource($sk, $ssk, walk_obj($cache, $s_pdf->{'pdf'}, $self->{'pdf'}, $s_page->{$k}->{$sk}->{$ssk}));
1580 }
1581 }
1582 }
1583  
1584 # create a whole content stream
1585 ## technically it is possible to submit an unfinished
1586 ## (eg. newly created) source-page, but that's nonsense,
1587 ## so we expect a page fixed by open_page and die otherwise
1588 4 ▶ unless ($s_page->{' opened'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($s_page->{" opened"}) 0 4 4
1589 0 croak join(' ',
1590 "Pages may only be imported from a complete PDF.",
1591 "Save and reopen the source PDF object first");
1592 }
1593  
1594 4 ▶ if (defined $s_page->{'Contents'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s_page->{"Contents"}) 3 1 4
1595 3 $s_page->fixcontents();
1596  
1597 3 $xo->{' stream'} = '';
1598 # open_page pages only contain one stream
1599 3 my ($k) = $s_page->{'Contents'}->elements();
1600 3 $k->realise();
1601 3 ▶ if ($k->{' nofilt'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k->{' nofilt'}) { } 3 0 3
1602 # we have a finished stream here so we unfilter
1603 3 $xo->add('q', unfilter($k->{'Filter'}, $k->{' stream'}), 'Q');
1604 }
1605 else {
1606 # stream is an unfinished/unfiltered content
1607 # so we just copy it and add the required "qQ"
1608 0 $xo->add('q', $k->{' stream'}, 'Q');
1609 }
1610 3 ▶ $xo->compressFlate() if $self->{'forcecompress'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"forcecompress"} 3 0 3
1611 }
1612  
1613 4 return $xo;
1614 }
1615  
1616 # Used by embed_page and import_page
1617 16 ▶sub walk_obj {
Subroutine walk_obj: called CC=14 SCAR=28.4
Pod undocumented
1618 16 my ($object_cache, $source_pdf, $target_pdf, $source_object, @keys) = @_;
1619  
1620 16 ▶ if (ref($source_object) =~ /Objind$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($source_object) =~ /Objind$/) 1 15 16
1621 1 $source_object->realise();
1622 }
1623  
1624 16 ▶ return $object_cache->{scalar $source_object} if defined $object_cache->{scalar $source_object};
Branch: 1 branch
BranchTrueFalseTotal
if defined $$object_cache{scalar $source_object} 0 16 16
1625 # die "infinite loop while copying objects" if $source_object->{' copied'};
1626  
1627 16 my $target_object = $source_object->copy($source_pdf); ## thanks to: yaheath // Fri, 17 Sep 2004
1628  
1629 # $source_object->{' copied'} = 1;
1630 16 ▶ $target_pdf->new_obj($target_object) if $source_object->is_obj($source_pdf);
Branch: 1 branch
BranchTrueFalseTotal
if $source_object->is_obj($source_pdf) 1 15 16
1631  
1632 16 $object_cache->{scalar $source_object} = $target_object;
1633  
1634 16 ▶ if (ref($source_object) =~ /Array$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref($source_object) =~ /Array$/) { } 2 14 16
elsif (ref($source_object) =~ /Dict$/) { } 1 13 14
1635 2 $target_object->{' val'} = [];
1636 2 foreach my $k ($source_object->elements()) {
1637 8 ▶ $k->realise() if ref($k) =~ /Objind$/;
Branch: 1 branch
BranchTrueFalseTotal
if ref($k) =~ /Objind$/ 0 8 8
1638 8 $target_object->add_elements(walk_obj($object_cache, $source_pdf, $target_pdf, $k));
1639 }
1640 }
1641 elsif (ref($source_object) =~ /Dict$/) {
1642 1 ▶ @keys = keys(%$target_object) unless scalar @keys;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @keys 1 0 1
1643 1 foreach my $k (@keys) {
1644 6 ▶ next if $k =~ /^ /;
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /^ / 1 5 6
1645 5 ▶ next unless defined $source_object->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $source_object->{$k} 0 5 5
1646 5 $target_object->{$k} = walk_obj($object_cache, $source_pdf, $target_pdf, $source_object->{$k});
1647 }
1648 1 ▶ if ($source_object->{' stream'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source_object->{" stream"}) 0 1 1
1649 0 ▶ if ($target_object->{'Filter'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target_object->{'Filter'}) { } 0 0 0
1650 0 $target_object->{' nofilt'} = 1;
1651 }
1652 else {
1653 0 delete $target_object->{' nofilt'};
1654 0 $target_object->{'Filter'} = PDFArray(PDFName('FlateDecode'));
1655 }
1656 0 $target_object->{' stream'} = $source_object->{' stream'};
1657 }
1658 }
1659 16 delete $target_object->{' streamloc'};
1660 16 delete $target_object->{' streamsrc'};
1661  
1662 16 return $target_object;
1663 }
1664  
1665 =head2 page_count
1666  
1667 $integer = $pdf->page_count();
1668  
1669 Return the number of pages in the document.
1670  
1671 =cut
1672  
1673 # Deprecated (renamed)
1674 3 ▶sub pages { return page_count(@_) }
Subroutine pages: called CC=1 SCAR=0.0
Pod documented
1675  
1676 3 ▶sub page_count {
Subroutine page_count: called CC=1 SCAR=0.0
Pod documented
1677 3 my $self = shift();
1678 3 return scalar @{$self->{'pagestack'}};
1679 }
1680  
1681 =head2 page_labels
1682  
1683 $pdf = $pdf->page_labels($page_number, %options);
1684  
1685 Describes how pages should be numbered beginning at the specified page number.
1686  
1687 # Generate a 30-page PDF
1688 my $pdf = PDF::API2->new();
1689 $pdf->page() for 1..30;
1690  
1691 # Number pages i to v, 1 to 20, and A-1 to A-5, respectively
1692 $pdf->page_labels(1, style => 'roman');
1693 $pdf->page_labels(6, style => 'decimal');
1694 $pdf->page_labels(26, style => 'decimal', prefix => 'A-');
1695  
1696 $pdf->save('sample.pdf');
1697  
1698 The following options are available:
1699  
1700 =over
1701  
1702 =item * style
1703  
1704 One of C<decimal> (standard decimal arabic numerals), C<Roman> (uppercase roman
1705 numerals), C<roman> (lowercase roman numerals), C<Alpha> (uppercase letters),
1706 or C<alpha> (lowercase letters).
1707  
1708 There is no default numbering style. If omitted, the page label will be just
1709 the prefix (if set) or an empty string.
1710  
1711 =item * prefix
1712  
1713 The label prefix for pages in this range.
1714  
1715 =item * start
1716  
1717 An integer (default: 1) representing the first value to be used in this page
1718 range.
1719  
1720 =back
1721  
1722 =cut
1723  
1724 # Deprecated; replace with page_labels, updating arguments as shown
1725 8 ▶sub pageLabel {
Subroutine pageLabel: called CC=8 SCAR=20.8
Pod documented
1726 8 my $self = shift();
1727 8 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 8 8 16
1728 8 my $page_index = shift();
1729  
1730 # Pass options as a hash rather than a hashref
1731 8 ▶ my %options = %{shift() // {}};
Condition50%1/2
shift() // {}
l!l
80
MC/DC0%0/1
shift() // {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1732  
1733 # Remove leading hyphens from option names
1734 8 ▶ if (exists $options{'-prefix'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{"-prefix"}) 1 7 8
1735 1 $options{'prefix'} = delete $options{'-prefix'};
1736 }
1737 8 ▶ if (exists $options{'-start'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{"-start"}) 1 7 8
1738 1 $options{'start'} = delete $options{'-start'};
1739 }
1740 8 ▶ if (exists $options{'-style'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{"-style"}) 6 2 8
1741 6 $options{'style'} = delete $options{'-style'};
1742 6 ▶ unless ($options{'style'} =~ /^(?:[Rr]oman|[Aa]lpha|decimal)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"style"} =~ /^(?:[Rr]oman|[Aa]lpha|decimal)$/) 1 5 6
1743 1 carp "Invalid -style for page labels; defaulting to decimal";
1744 1 $options{'style'} = 'decimal';
1745 }
1746 }
1747  
1748 # page_labels doesn't have a default numbering style, to be consistent
1749 # with the spec.
1750 8 ▶ $options{'style'} //= 'D';
Condition100%2/2
$options{"style"} //= "D"
l!l
62
MC/DC100%1/1
$options{"style"} //= "D"
$options{"style"}
Truth table100%2/2
A
A= $options{"style"}
Aresult
11
00
1751  
1752 # Set one set of page labels at a time (support for multiple sets of
1753 # page labels by pageLabel was undocumented). Switch from 0-based to
1754 # 1-based numbering.
1755 8 $self->page_labels($page_index + 1, %options);
1756 }
1757  
1758 # Return nothing (page_labels returns $self, matching other setters)
1759 8 return;
1760 }
1761  
1762 8 ▶sub page_labels {
Subroutine page_labels: called CC=11 SCAR=26.0
Pod documented
1763 8 my ($self, $page_number, %options) = @_;
1764  
1765 # $page_number is 1-based in order to be consistent with other PDF::API2
1766 # methods, but the page label numbering is 0-based.
1767 8 my $page_index = $page_number - 1;
1768  
1769 8 ▶ $self->{'catalog'}->{'PageLabels'} //= PDFDict();
Condition33%1/3
$self->{"catalog"}{"PageLabels"} //= PDFDict()
l!l&&r!l&&!r
080
MC/DC0%0/2
$self->{"catalog"}{"PageLabels"} //= PDFDict()
$self->{"catalog"}{"PageLabels"} PDFDict()
Truth table33%1/3
A //= B
A= $self->{"catalog"}{"PageLabels"}B= PDFDict()
ABresult
1X1
011
000
1770 8 ▶ $self->{'catalog'}->{'PageLabels'}->{'Nums'} //= PDFArray();
Condition33%1/3
$self->{"catalog"}{"PageLabels"}{"Nums"} //= PDFArray()
l!l&&r!l&&!r
080
MC/DC0%0/2
$self->{"catalog"}{"PageLabels"}{"Nums"} //= PDFArray()
$self->{"catalog"}{"PageLabels"}{"Nums"} PDFArray()
Truth table33%1/3
A //= B
A= $self->{"catalog"}{"PageLabels"}{"Nums"}B= PDFArray()
ABresult
1X1
011
000
1771  
1772 8 my $nums = $self->{'catalog'}->{'PageLabels'}->{'Nums'};
1773 8 $nums->add_elements(PDFNum($page_index));
1774  
1775 8 my $d = PDFDict();
1776 8 ▶ if (exists $options{'style'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{"style"}) 8 0 8
1777 8 ▶ unless ($options{'style'} and $options{'style'} =~ /^([rad])/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"style"} and $options{"style"} =~ /^([rad])/i) 0 8 8
Condition33%1/3
$options{"style"} and $options{"style"} =~ /^([rad])/i
!ll&&!rl&&r
008
MC/DC0%0/2
$options{"style"} and $options{"style"} =~ /^([rad])/i
$options{"style"} $options{"style"} =~ /^([rad])/i
Truth table33%1/3
A and B
A= $options{"style"}B= $options{"style"} =~ /^([rad])/i
ABresult
0X0
100
111
1778 0 croak 'Invalid page numbering style';
1779 }
1780 8 ▶ $d->{'S'} = PDFName($1 eq 'd' ? 'D' : $1);
Branch: 1 branch
BranchTrueFalseTotal
$1 eq 'd' ? : 2 6 8
1781 }
1782  
1783 8 ▶ if (exists $options{'prefix'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{"prefix"}) 1 7 8
1784 1 ▶ $d->{'P'} = PDFStr($options{'prefix'} // '');
Condition50%1/2
$options{"prefix"} // ""
l!l
10
MC/DC0%0/1
$options{"prefix"} // ""
$options{"prefix"}
Truth table50%1/2
A
A= $options{"prefix"}
Aresult
11
00
1785 }
1786  
1787 8 ▶ if (exists $options{'start'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{"start"}) 1 7 8
1788 1 ▶ $d->{'St'} = PDFNum($options{'start'} // '');
Condition50%1/2
$options{"start"} // ""
l!l
10
MC/DC0%0/1
$options{"start"} // ""
$options{"start"}
Truth table50%1/2
A
A= $options{"start"}
Aresult
11
00
1789 }
1790  
1791 8 $nums->add_elements($d);
1792  
1793 8 return $self;
1794 }
1795  
1796 =head2 default_page_size
1797  
1798 # Set
1799 $pdf->default_page_size($size);
1800  
1801 # Get
1802 @rectangle = $pdf->default_page_size()
1803  
1804 Set the default physical size for pages in the PDF. If called without
1805 arguments, return the coordinates of the rectangle describing the default
1806 physical page size.
1807  
1808 See L<PDF::API2::Page/"Page Sizes"> for possible values.
1809  
1810 =cut
1811  
1812 1 ▶sub default_page_size {
Subroutine default_page_size: called CC=2 SCAR=9.2
Pod documented
1813 1 my $self = shift();
1814  
1815 # Set
1816 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 0 1
1817 1 return $self->default_page_boundaries(media => @_);
1818 }
1819  
1820 # Get
1821 0 my %boundaries = $self->default_page_boundaries();
1822 0 return @{$boundaries{'media'}};
1823 }
1824  
1825 =head2 default_page_boundaries
1826  
1827 # Set
1828 $pdf->default_page_boundaries(%boundaries);
1829  
1830 # Get
1831 %boundaries = $pdf->default_page_boundaries();
1832  
1833 Set default prepress page boundaries for pages in the PDF. If called without
1834 arguments, returns the coordinates of the rectangles describing each of the
1835 supported page boundaries.
1836  
1837 See the equivalent C<page_boundaries> method in L<PDF::API2::Page> for details.
1838  
1839 =cut
1840  
1841 # Called by PDF::API2::Page::boundaries via the default_page_* methods below
1842 17 ▶sub _bounding_box {
Subroutine _bounding_box: called CC=5 SCAR=16.8
1843 17 my $self = shift();
1844 17 my $type = shift();
1845  
1846 # Get
1847 17 ▶ unless (scalar @_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @_) 6 11 17
1848 6 ▶ unless ($self->{'pages'}->{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"pages"}{$type}) 1 5 6
1849 1 ▶ return if $type eq 'MediaBox';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "MediaBox" 1 0 1
1850  
1851 # Use defaults per PDF 1.7 section 14.11.2 Page Boundaries
1852 0 ▶ return $self->_bounding_box('MediaBox') if $type eq 'CropBox';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "CropBox" 0 0 0
1853 0 return $self->_bounding_box('CropBox');
1854 }
1855 5 return map { $_->val() } $self->{'pages'}->{$type}->elements();
1856 }
1857  
1858 # Set
1859 11 $self->{'pages'}->{$type} = PDFArray(map { PDFNum(float($_)) } @_);
1860 11 return $self;
1861 }
1862  
1863 3 ▶sub default_page_boundaries {
Subroutine default_page_boundaries: called CC=1 SCAR=0.0
Pod documented
1864 3 return PDF::API2::Page::boundaries(@_);
1865 }
1866  
1867 # Deprecated; use default_page_size or default_page_boundaries
1868 5 ▶sub mediabox {
Subroutine mediabox: called CC=2 SCAR=6.9
Pod documented
1869 5 my $self = shift();
1870 5 ▶ return $self->_bounding_box('MediaBox') unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 2 3 5
1871 3 return $self->_bounding_box('MediaBox', page_size(@_));
1872 }
1873  
1874 # Deprecated; use default_page_boundaries
1875 1 ▶sub cropbox {
Subroutine cropbox: called CC=2 SCAR=7.1
Pod documented
1876 1 my $self = shift();
1877 1 ▶ return $self->_bounding_box('CropBox') unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 1 1
1878 1 return $self->_bounding_box('CropBox', page_size(@_));
1879 }
1880  
1881 # Deprecated; use default_page_boundaries
1882 1 ▶sub bleedbox {
Subroutine bleedbox: called CC=2 SCAR=7.1
Pod documented
1883 1 my $self = shift();
1884 1 ▶ return $self->_bounding_box('BleedBox') unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 1 1
1885 1 return $self->_bounding_box('BleedBox', page_size(@_));
1886 }
1887  
1888 # Deprecated; use default_page_boundaries
1889 1 ▶sub trimbox {
Subroutine trimbox: called CC=2 SCAR=7.1
Pod documented
1890 1 my $self = shift();
1891 1 ▶ return $self->_bounding_box('TrimBox') unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 1 1
1892 1 return $self->_bounding_box('TrimBox', page_size(@_));
1893 }
1894  
1895 # Deprecated; use default_page_boundaries
1896 1 ▶sub artbox {
Subroutine artbox: called CC=2 SCAR=7.1
Pod documented
1897 1 my $self = shift();
1898 1 ▶ return $self->_bounding_box('ArtBox') unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 1 1
1899 1 return $self->_bounding_box('ArtBox', page_size(@_));
1900 }
1901  
1902 =head1 FONT METHODS
1903  
1904 =head2 font
1905  
1906 my $font = $pdf->font($name, %options)
1907  
1908 Add a font to the PDF. Returns the font object, to be used by
1909 L<PDF::API2::Content>.
1910  
1911 The font C<$name> is either the name of one of the L<standard 14
1912 fonts|PDF::API2::Resource::Font::CoreFont/"STANDARD FONTS"> (e.g. Helvetica),
1913 a C<Font::TTF::Font> object, or the path to a font file.
1914  
1915 my $pdf = PDF::API2->new();
1916 my $font1 = $pdf->font('Helvetica-Bold');
1917 my $font2 = $pdf->font('/path/to/ComicSans.ttf');
1918 my $page = $pdf->page();
1919 my $content = $page->text();
1920  
1921 $content->position(1 * 72, 9 * 72);
1922 $content->font($font1, 24);
1923 $content->text('Hello, World!');
1924  
1925 $content->position(0, -36);
1926 $content->font($font2, 12);
1927 $content->text('This is some sample text.');
1928  
1929 $pdf->save('sample.pdf');
1930  
1931 The path can be omitted if the font file is in the current directory or one of
1932 the directories returned by C<font_path>.
1933  
1934 TrueType (ttf/otf), Adobe PostScript Type 1 (pfa/pfb), and Adobe Glyph Bitmap
1935 Distribution Format (bdf) fonts are supported.
1936  
1937 The following C<%options> are available:
1938  
1939 =over
1940  
1941 =item * format
1942  
1943 The font format is normally detected automatically based on the file's
1944 extension. If you're using a font with an atypical extension, you can set
1945 C<format> to one of C<truetype> (TrueType or OpenType), C<type1> (PostScript
1946 Type 1), or C<bitmap> (Adobe Bitmap).
1947  
1948 =item * kerning
1949  
1950 Kerning (automatic adjustment of space between pairs of characters) is enabled
1951 by default if the font includes this information. Set this option to false to
1952 disable.
1953  
1954 =item * afm_file (PostScript Type 1 fonts only)
1955  
1956 Specifies the location of the font metrics file.
1957  
1958 =item * pfm_file (PostScript Type 1 fonts only)
1959  
1960 Specifies the location of the printer font metrics file. This option overrides
1961 the -encode option.
1962  
1963 =item * embed (TrueType fonts only)
1964  
1965 Fonts are embedded in the PDF by default, which is required to ensure that they
1966 can be viewed properly on a device that doesn't have the font installed. Set
1967 this option to false to prevent the font from being embedded.
1968  
1969 =back
1970  
1971 =cut
1972  
1973 1 ▶sub font {
Subroutine font: called CC=19 SCAR=55.9
Pod documented
1974 1 my ($self, $name, %options) = @_;
1975  
1976 1 ▶ if (exists $options{'kerning'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{"kerning"}) 0 1 1
1977 0 $options{'-dokern'} = delete $options{'kerning'};
1978 }
1979  
1980 1 require PDF::API2::Resource::Font::CoreFont;
1981 1 ▶ if (PDF::API2::Resource::Font::CoreFont->is_standard($name)) {
Branch: 2 branches
BranchTrueFalseTotal
if ('PDF::API2::Resource::Font::CoreFont'->is_standard($name)) { } 1 0 1
elsif ($name eq 'Times' and not $options{'format'}) { } 0 0 0
1982 1 return $self->corefont($name, %options);
1983 }
1984 ▶ elsif ($name eq 'Times' and not $options{'format'}) {
Condition0%0/3
$name eq "Times" and not $options{"format"}
!ll&&!rl&&r
000
MC/DC0%0/2
$name eq "Times" and not $options{"format"}
$name eq "Times" not $options{"format"}
Truth table0%0/3
A and not(B)
A= $name eq "Times"B= not $options{"format"}
ABresult
0X0
100
111
1985 # Accept Times as an alias for Times-Roman to follow the pattern set by
1986 # Courier and Helvetica.
1987 0 carp "Times is not a standard font; substituting Times-Roman";
1988 0 return $self->corefont('Times-Roman', %options);
1989 }
1990  
1991 0 my $format = $options{'format'};
1992 0 ▶ $format //= 'truetype' if UNIVERSAL::isa($name, 'Font::TTF::Font');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($name, "Font::TTF::Font") 0 0 0
Condition0%0/2
$format //= "truetype"
l!l
00
MC/DC0%0/1
$format //= "truetype"
$format
Truth table0%0/2
A
A= $format
Aresult
11
00
1993 0 ▶ $format //= ($name =~ /\.[ot]tf$/i ? 'truetype' :
Branch: 3 branches
BranchTrueFalseTotal
$name =~ /\.[ot]tf$/i ? : 0 0 0
$name =~ /\.pf[ab]$/i ? : 0 0 0
$name =~ /\.bdf$/i ? : 0 0 0
Condition0%0/2
$format //= $name =~ /\.[ot]tf$/i ? "truetype" : ($name =~ /\.pf[ab]$/i ? "type1" : ($name =~ /\.bdf$/i ? "bitmap" : ""))
l!l
00
MC/DC0%0/1
$format //= $name =~ /\.[ot]tf$/i ? "truetype" : ($name =~ /\.pf[ab]$/i ? "type1" : ($name =~ /\.bdf$/i ? "bitmap" : ""))
$format
Truth table0%0/2
A
A= $format
Aresult
11
00
1994 $name =~ /\.pf[ab]$/i ? 'type1' :
1995 $name =~ /\.bdf$/i ? 'bitmap' : '');
1996  
1997 0 ▶ if ($format eq 'truetype') {
Branch: 5 branches
BranchTrueFalseTotal
if ($format eq 'truetype') { } 0 0 0
elsif ($format eq 'type1') { } 0 0 0
elsif ($format eq 'bitmap') { } 0 0 0
elsif ($format) { } 0 0 0
elsif ($name =~ /(\..*)$/) { } 0 0 0
1998 0 ▶ $options{'embed'} //= 1;
Condition0%0/2
$options{"embed"} //= 1
l!l
00
MC/DC0%0/1
$options{"embed"} //= 1
$options{"embed"}
Truth table0%0/2
A
A= $options{"embed"}
Aresult
11
00
1999 0 return $self->ttfont($name, %options);
2000 }
2001 elsif ($format eq 'type1') {
2002 0 ▶ if (exists $options{'afm_file'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{"afm_file"}) 0 0 0
2003 0 $options{'-afmfile'} = delete $options{'afm_file'};
2004 }
2005 0 ▶ if (exists $options{'pfm_file'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{"pfm_file"}) 0 0 0
2006 0 $options{'-pfmfile'} = delete $options{'pfm_file'};
2007 }
2008 0 return $self->psfont($name, %options);
2009 }
2010 elsif ($format eq 'bitmap') {
2011 0 return $self->bdfont($name, %options);
2012 }
2013 elsif ($format) {
2014 0 croak "Unrecognized font format: $format";
2015 }
2016 elsif ($name =~ /(\..*)$/) {
2017 0 croak "Unrecognized font file extension: $1";
2018 }
2019 else {
2020 0 croak "Unrecognized font: $name";
2021 }
2022 }
2023  
2024 =head2 synthetic_font
2025  
2026 $font = $pdf->synthetic_font($base_font, %options)
2027  
2028 Creates and returns a new synthetic font object. See
2029 L<PDF::API2::Resource::Font::SynFont> for details.
2030  
2031 =cut
2032  
2033 # Deprecated (renamed)
2034 0 ▶sub synfont { return synthetic_font(@_) }
Subroutine synfont: not called CC=1 SCAR=6.9
Pod documented
2035  
2036 0 ▶sub synthetic_font {
Subroutine synthetic_font: not called CC=3 SCAR=24.8
Pod documented
2037 0 my ($self, $font, %opts) = @_;
2038  
2039 # PDF::API2 doesn't set BaseEncoding for TrueType fonts, so text
2040 # isn't searchable unless a ToUnicode CMap is included. Include
2041 # the ToUnicode CMap by default, but allow it to be disabled (for
2042 # performance and file size reasons) by setting -unicodemap to 0.
2043 0 ▶ $opts{-unicodemap} = 1 unless exists $opts{-unicodemap};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"-unicodemap"} 0 0 0
2044  
2045 0 require PDF::API2::Resource::Font::SynFont;
2046 0 my $obj = PDF::API2::Resource::Font::SynFont->new($self->{'pdf'}, $font, %opts);
2047  
2048 0 $self->{'pdf'}->out_obj($self->{'pages'});
2049 0 ▶ $obj->tounicodemap() if $opts{-unicodemap};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"-unicodemap"} 0 0 0
2050  
2051 0 return $obj;
2052 }
2053  
2054 =head2 standard_fonts
2055  
2056 @names = $pdf->standard_fonts()
2057  
2058 Returns the names of the 14 standard (built-in) fonts. See
2059 L<PDF::API2::Resource::Font::CoreFont> for details.
2060  
2061 =cut
2062  
2063 1 ▶sub standard_fonts {
Subroutine standard_fonts: called CC=1 SCAR=0.0
Pod documented
2064 1 require PDF::API2::Resource::Font::CoreFont;
2065 1 return PDF::API2::Resource::Font::CoreFont->names();
2066 }
2067  
2068 =head2 is_standard_font
2069  
2070 $boolean = PDF::API2->is_standard_font($name);
2071  
2072 Returns true if C<$name> is an exact, case-sensitive match for one of the
2073 standard font names.
2074  
2075 =cut
2076  
2077 2 ▶sub is_standard_font {
Subroutine is_standard_font: called CC=1 SCAR=0.0
Pod documented
2078 2 my $name = pop();
2079 2 require PDF::API2::Resource::Font::CoreFont;
2080 2 return PDF::API2::Resource::Font::CoreFont->is_standard($name);
2081 }
2082  
2083 =head2 font_path
2084  
2085 @directories = PDF::API2->font_path()
2086  
2087 Return the list of directories that will be searched (in order) in addition to
2088 the current directory when you add a font to a PDF without including the full
2089 path to the font file.
2090  
2091 =cut
2092  
2093 0 ▶sub font_path {
Subroutine font_path: not called CC=1 SCAR=6.9
Pod documented
2094 0 return @font_path;
2095 }
2096  
2097 =head2 add_to_font_path
2098  
2099 @directories = PDF::API2->add_to_font_path('/my/fonts', '/path/to/fonts');
2100  
2101 Add one or more directories to the list of paths to be searched for font files.
2102  
2103 Returns the font search path.
2104  
2105 =cut
2106  
2107 # Deprecated (renamed)
2108 0 ▶sub addFontDirs { return add_to_font_path(@_) }
Subroutine addFontDirs: not called CC=1 SCAR=6.9
Pod documented
2109  
2110 0 ▶sub add_to_font_path {
Subroutine add_to_font_path: not called CC=3 SCAR=24.8
Pod documented
2111 # Allow this method to be called using either :: or -> notation.
2112 0 ▶ shift() if ref($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 0 0
2113 0 ▶ shift() if $_[0] eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq __PACKAGE__ 0 0 0
2114  
2115 0 push @font_path, @_;
2116 0 return @font_path;
2117 }
2118  
2119 =head2 set_font_path
2120  
2121 @directories = PDF::API2->set_font_path('/my/fonts', '/path/to/fonts');
2122  
2123 Replace the existing font search path. This should only be necessary if you
2124 need to remove a directory from the path for some reason, or if you need to
2125 reorder the list.
2126  
2127 Returns the font search path.
2128  
2129 =cut
2130  
2131 39 ▶sub set_font_path {
Subroutine set_font_path: called CC=3 SCAR=11.3
Pod documented
2132 # Allow this method to be called using either :: or -> notation.
2133 39 ▶ shift() if ref($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 39 39
2134 39 ▶ shift() if $_[0] eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq __PACKAGE__ 39 0 39
2135  
2136 39 @font_path = ((map { "$_/PDF/API2/fonts" } @INC), @_);
2137  
2138 39 return @font_path;
2139 }
2140  
2141 0 ▶sub _find_font {
Subroutine _find_font: not called CC=4 SCAR=30.0
2142 0 my $font = shift();
2143  
2144 # Check the current directory
2145 0 ▶ return $font if -f $font;
Branch: 1 branch
BranchTrueFalseTotal
if -f $font 0 0 0
2146  
2147 # Check the font search path
2148 0 foreach my $directory (@font_path) {
2149 0 ▶ return "$directory/$font" if -f "$directory/$font";
Branch: 1 branch
BranchTrueFalseTotal
if -f "$directory/$font" 0 0 0
2150 }
2151  
2152 0 return;
2153 }
2154  
2155 53 ▶sub corefont {
Subroutine corefont: called CC=2 SCAR=7.0
Pod documented
2156 53 my ($self, $name, %opts) = @_;
2157 53 require PDF::API2::Resource::Font::CoreFont;
2158 53 my $obj = PDF::API2::Resource::Font::CoreFont->new($self->{'pdf'}, $name, %opts);
2159 53 $self->{'pdf'}->out_obj($self->{'pages'});
2160 53 ▶ $obj->tounicodemap() if $opts{-unicodemap};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"-unicodemap"} 0 53 53
2161 53 return $obj;
2162 }
2163  
2164 0 ▶sub psfont {
Subroutine psfont: not called CC=5 SCAR=34.0
Pod documented
2165 0 my ($self, $psf, %opts) = @_;
2166  
2167 0 foreach my $o (qw(-afmfile -pfmfile)) {
2168 0 ▶ next unless defined $opts{$o};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{$o} 0 0 0
2169 0 $opts{$o} = _find_font($opts{$o});
2170 }
2171 0 ▶ $psf = _find_font($psf) or croak "Unable to find font \"$psf\"";
Branch: 1 branch
BranchTrueFalseTotal
$psf = _find_font($psf) or croak(qq[Unable to find font "$psf"]) 0 0 0
2172 0 require PDF::API2::Resource::Font::Postscript;
2173 0 my $obj = PDF::API2::Resource::Font::Postscript->new($self->{'pdf'}, $psf, %opts);
2174  
2175 0 $self->{'pdf'}->out_obj($self->{'pages'});
2176 0 ▶ $obj->tounicodemap() if $opts{-unicodemap};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"-unicodemap"} 0 0 0
2177  
2178 0 return $obj;
2179 }
2180  
2181 0 ▶sub ttfont {
Subroutine ttfont: not called CC=8 SCAR=42.8
Pod documented
2182 0 my ($self, $name, %opts) = @_;
2183  
2184 # PDF::API2 doesn't set BaseEncoding for TrueType fonts, so text
2185 # isn't searchable unless a ToUnicode CMap is included. Include
2186 # the ToUnicode CMap by default, but allow it to be disabled (for
2187 # performance and file size reasons) by setting -unicodemap to 0.
2188 0 ▶ $opts{-unicodemap} = 1 unless exists $opts{-unicodemap};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"-unicodemap"} 0 0 0
2189  
2190 # -noembed is deprecated (replace with embed => 0)
2191 0 ▶ if ($opts{'-noembed'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"-noembed"}) 0 0 0
2192 0 ▶ $opts{'embed'} //= 0;
Condition0%0/2
$opts{"embed"} //= 0
l!l
00
MC/DC0%0/1
$opts{"embed"} //= 0
$opts{"embed"}
Truth table0%0/2
A
A= $opts{"embed"}
Aresult
11
00
2193 }
2194 0 ▶ $opts{'embed'} //= 1;
Condition0%0/2
$opts{"embed"} //= 1
l!l
00
MC/DC0%0/1
$opts{"embed"} //= 1
$opts{"embed"}
Truth table0%0/2
A
A= $opts{"embed"}
Aresult
11
00
2195  
2196 0 ▶ my $file = UNIVERSAL::isa($name,'Font::TTF::Font') ? $name : _find_font($name) or croak "Unable to find font \"$name\"";
Branch: 2 branches
BranchTrueFalseTotal
my $file = UNIVERSAL::isa($name, "Font::TTF::Font") ? $name : _find_font($name) or croak(qq[Unable to find font "$name"]) 0 0 0
UNIVERSAL::isa($name, 'Font::TTF::Font') ? : 0 0 0
2197 0 require PDF::API2::Resource::CIDFont::TrueType;
2198 0 my $obj = PDF::API2::Resource::CIDFont::TrueType->new($self->{'pdf'}, $file, %opts);
2199  
2200 0 $self->{'pdf'}->out_obj($self->{'pages'});
2201 0 ▶ $obj->tounicodemap() if $opts{-unicodemap};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"-unicodemap"} 0 0 0
2202  
2203 0 return $obj;
2204 }
2205  
2206 0 ▶sub bdfont {
Subroutine bdfont: not called CC=1 SCAR=6.9
Pod documented
2207 0 my ($self, @opts) = @_;
2208  
2209 0 require PDF::API2::Resource::Font::BdFont;
2210 0 my $obj = PDF::API2::Resource::Font::BdFont->new($self->{'pdf'}, @opts);
2211  
2212 0 $self->{'pdf'}->out_obj($self->{'pages'});
2213 # $obj->tounicodemap(); # does not support Unicode
2214  
2215 0 return $obj;
2216 }
2217  
2218 # Deprecated. Use Unicode-supporting TrueType fonts instead.
2219 # See PDF::API2::Resource::CIDFont::CJKFont for details.
2220 1 ▶sub cjkfont {
Subroutine cjkfont: called CC=2 SCAR=7.0
Pod documented
2221 1 my ($self, $name, %opts) = @_;
2222  
2223 1 require PDF::API2::Resource::CIDFont::CJKFont;
2224 1 my $obj = PDF::API2::Resource::CIDFont::CJKFont->new($self->{'pdf'}, $name, %opts);
2225  
2226 1 $self->{'pdf'}->out_obj($self->{'pages'});
2227 1 ▶ $obj->tounicodemap() if $opts{-unicodemap};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"-unicodemap"} 0 1 1
2228  
2229 1 return $obj;
2230 }
2231  
2232 # Deprecated. Use Unicode-supporting TrueType fonts instead.
2233 1 ▶sub unifont {
Subroutine unifont: called CC=1 SCAR=0.0
Pod documented
2234 1 my ($self, @opts) = @_;
2235  
2236 1 require PDF::API2::Resource::UniFont;
2237 1 my $obj = PDF::API2::Resource::UniFont->new($self->{'pdf'}, @opts);
2238  
2239 1 return $obj;
2240 }
2241  
2242 =head1 GRAPHICS METHODS
2243  
2244 =head2 image
2245  
2246 $object = $pdf->image($file, %options);
2247  
2248 Import a supported image type and return an object that can be placed as part of
2249 a page's content:
2250  
2251 my $pdf = PDF::API2->new();
2252 my $page = $pdf->page();
2253  
2254 my $image = $pdf->image('/path/to/image.jpg');
2255 $page->object($image, 100, 100);
2256  
2257 $pdf->save('sample.pdf');
2258  
2259 C<$file> may be either a file name, a filehandle, or a L<GD::Image> object.
2260  
2261 See L<PDF::API2::Content/"object"> for details about placing images on a page
2262 once they're imported.
2263  
2264 The image format is normally detected automatically based on the file's
2265 extension. If passed a filehandle, image formats GIF, JPEG, and PNG will be
2266 detected based on the file's header.
2267  
2268 If the file has an atypical extension or the filehandle is for a different kind
2269 of image, you can set the C<format> option to one of the supported types:
2270 C<gif>, C<jpeg>, C<png>, C<pnm>, or C<tiff>.
2271  
2272 Note: PNG images that include an alpha (transparency) channel go through a
2273 relatively slow process of splitting the image into separate RGB and alpha
2274 components as is required by images in PDFs. If you're having performance
2275 issues, install PDF::API2::XS or Image::PNG::Libpng to speed this process up by
2276 an order of magnitude; either module will be used automatically if available.
2277  
2278 =cut
2279  
2280 3 ▶sub image {
Subroutine image: called CC=20 SCAR=49.3
Pod documented
2281 3 my ($self, $file, %options) = @_;
2282  
2283 3 ▶ my $format = lc($options{'format'} // '');
Condition50%1/2
$options{"format"} // ""
l!l
03
MC/DC0%0/1
$options{"format"} // ""
$options{"format"}
Truth table50%1/2
A
A= $options{"format"}
Aresult
11
00
2284  
2285 3 ▶ if (ref($file) eq 'GD::Image') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $file eq 'GD::Image') { } 0 3 3
elsif (ref $file) { } 3 0 3
2286 0 return $self->image_gd($file, %options);
2287 }
2288 elsif (ref($file)) {
2289 3 ▶ $format ||= _detect_image_format($file);
Condition33%1/3
$format ||= _detect_image_format($file)
l!l&&r!l&&!r
030
MC/DC0%0/2
$format ||= _detect_image_format($file)
$format _detect_image_format($file)
Truth table33%1/3
A ||= B
A= $formatB= _detect_image_format($file)
ABresult
1X1
011
000
2290 }
2291 3 ▶ unless (ref($file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $file) 0 3 3
2292 0 ▶ $format ||= ($file =~ /\.jpe?g$/i ? 'jpeg' :
Branch: 5 branches
BranchTrueFalseTotal
$file =~ /\.jpe?g$/i ? : 0 0 0
$file =~ /\.png$/i ? : 0 0 0
$file =~ /\.gif$/i ? : 0 0 0
$file =~ /\.tiff?$/i ? : 0 0 0
$file =~ /\.p[bgp]m$/i ? : 0 0 0
Condition0%0/3
$format ||= $file =~ /\.jpe?g$/i ? "jpeg" : ($file =~ /\.png$/i ? "png" : ($file =~ /\.gif$/i ? "gif" : ($file =~ /\.tiff?$/i ? "tiff" : ($file =~ /\.p[bgp]m$/i ? "pnm" : ""))))
l!l&&r!l&&!r
000
MC/DC0%0/2
$format ||= $file =~ /\.jpe?g$/i ? "jpeg" : ($file =~ /\.png$/i ? "png" : ($file =~ /\.gif$/i ? "gif" : ($file =~ /\.tiff?$/i ? "tiff" : ($file =~ /\.p[bgp]m$/i ? "pnm" : ""))))
$format $file =~ /\.jpe?g$/i ? "jpeg" : ($file =~ /\.png$/i ? "png" : ($file =~ /\.gif$/i ? "gif" : ($file =~ /\.tiff?$/i ? "tiff" : ($file =~ /\.p[bgp]m$/i ? "pnm" : ""))))
Truth table0%0/3
A ||= B
A= $formatB= $file =~ /\.jpe?g$/i ? "jpeg" : ($file =~ /\.png$/i ? "png" : ($file =~ /\.gif$/i ? "gif" : ($file =~ /\.tiff?$/i ? "tiff" : ($file =~ /\.p[bgp]m$/i ? "pnm" : ""))))
ABresult
1X1
011
000
2293 $file =~ /\.png$/i ? 'png' :
2294 $file =~ /\.gif$/i ? 'gif' :
2295 $file =~ /\.tiff?$/i ? 'tiff' :
2296 $file =~ /\.p[bgp]m$/i ? 'pnm' : '');
2297 }
2298  
2299 3 ▶ if ($format eq 'jpeg') {
Branch: 8 branches
BranchTrueFalseTotal
if ($format eq 'jpeg') { } 1 2 3
elsif ($format eq 'png') { } 1 1 2
elsif ($format eq 'gif') { } 1 0 1
elsif ($format eq 'tiff') { } 0 0 0
elsif ($format eq 'pnm') { } 0 0 0
elsif ($format) { } 0 0 0
elsif (ref $file) { } 0 0 0
elsif ($file =~ /(\..*)$/) { } 0 0 0
2300 1 return $self->image_jpeg($file, %options);
2301 }
2302 elsif ($format eq 'png') {
2303 1 return $self->image_png($file, %options);
2304 }
2305 elsif ($format eq 'gif') {
2306 1 return $self->image_gif($file, %options);
2307 }
2308 elsif ($format eq 'tiff') {
2309 0 return $self->image_tiff($file, %options);
2310 }
2311 elsif ($format eq 'pnm') {
2312 0 return $self->image_pnm($file, %options);
2313 }
2314 elsif ($format) {
2315 0 croak "Unrecognized image format: $format";
2316 }
2317 elsif (ref($file)) {
2318 0 croak "Unspecified image format";
2319 }
2320 elsif ($file =~ /(\..*)$/) {
2321 0 croak "Unrecognized image extension: $1";
2322 }
2323 else {
2324 0 croak "Unrecognized image: $file";
2325 }
2326 }
2327  
2328 3 ▶sub _detect_image_format {
Subroutine _detect_image_format: called CC=6 SCAR=18.6
2329 3 my $fh = shift();
2330 3 $fh->seek(0, 0);
2331 3 binmode $fh, ':raw';
2332  
2333 3 my $test;
2334 3 my $bytes_read = $fh->read($test, 8);
2335 3 $fh->seek(0, 0);
2336 3 ▶ return unless $bytes_read and $bytes_read == 8;
Branch: 1 branch
BranchTrueFalseTotal
unless $bytes_read and $bytes_read == 8 0 3 3
Condition33%1/3
$bytes_read and $bytes_read == 8
!ll&&!rl&&r
003
MC/DC0%0/2
$bytes_read and $bytes_read == 8
$bytes_read $bytes_read == 8
Truth table33%1/3
A and B
A= $bytes_readB= $bytes_read == 8
ABresult
0X0
100
111
2337  
2338 3 ▶ return 'gif' if $test =~ /^GIF\d\d[a-z]/;
Branch: 1 branch
BranchTrueFalseTotal
if $test =~ /^GIF\d\d[a-z]/ 1 2 3
2339 2 ▶ return 'jpeg' if $test =~ /^\xFF\xD8\xFF/;
Branch: 1 branch
BranchTrueFalseTotal
if $test =~ /^\xFF\xD8\xFF/ 1 1 2
2340 1 ▶ return 'png' if $test =~ /^\x89PNG\x0D\x0A\x1A\x0A/;
Branch: 1 branch
BranchTrueFalseTotal
if $test =~ /^\x89PNG\x0D\x0A\x1A\x0A/ 1 0 1
2341 0 return;
2342 }
2343  
2344 3 ▶sub image_jpeg {
Subroutine image_jpeg: called CC=1 SCAR=0.0
Pod documented
2345 3 my ($self, $file, %opts) = @_;
2346  
2347 3 require PDF::API2::Resource::XObject::Image::JPEG;
2348 3 my $obj = PDF::API2::Resource::XObject::Image::JPEG->new($self->{'pdf'}, $file);
2349  
2350 2 $self->{'pdf'}->out_obj($self->{'pages'});
2351  
2352 2 return $obj;
2353 }
2354  
2355 4 ▶sub image_tiff {
Subroutine image_tiff: called CC=1 SCAR=0.0
Pod documented
2356 4 my ($self, $file, %opts) = @_;
2357  
2358 4 require PDF::API2::Resource::XObject::Image::TIFF;
2359 4 my $obj = PDF::API2::Resource::XObject::Image::TIFF->new($self->{'pdf'}, $file);
2360  
2361 3 $self->{'pdf'}->out_obj($self->{'pages'});
2362  
2363 3 return $obj;
2364 }
2365  
2366 3 ▶sub image_pnm {
Subroutine image_pnm: called CC=2 SCAR=7.1
Pod documented
2367 3 my ($self, $file, %opts) = @_;
2368  
2369 3 ▶ $opts{'-compress'} //= $self->{'forcecompress'};
Condition33%1/3
$opts{"-compress"} //= $self->{"forcecompress"}
l!l&&r!l&&!r
030
MC/DC0%0/2
$opts{"-compress"} //= $self->{"forcecompress"}
$opts{"-compress"} $self->{"forcecompress"}
Truth table33%1/3
A //= B
A= $opts{"-compress"}B= $self->{"forcecompress"}
ABresult
1X1
011
000
2370  
2371 3 require PDF::API2::Resource::XObject::Image::PNM;
2372 3 my $obj = PDF::API2::Resource::XObject::Image::PNM->new($self->{'pdf'}, $file, %opts);
2373  
2374 2 $self->{'pdf'}->out_obj($self->{'pages'});
2375  
2376 2 return $obj;
2377 }
2378  
2379 5 ▶sub image_png {
Subroutine image_png: called CC=1 SCAR=0.0
Pod documented
2380 5 my ($self, $file, %opts) = @_;
2381  
2382 5 require PDF::API2::Resource::XObject::Image::PNG;
2383 5 my $obj = PDF::API2::Resource::XObject::Image::PNG->new($self->{'pdf'}, $file);
2384  
2385 4 $self->{'pdf'}->out_obj($self->{'pages'});
2386  
2387 4 return $obj;
2388 }
2389  
2390 3 ▶sub image_gif {
Subroutine image_gif: called CC=1 SCAR=0.0
Pod documented
2391 3 my ($self, $file, %opts) = @_;
2392  
2393 3 require PDF::API2::Resource::XObject::Image::GIF;
2394 3 my $obj = PDF::API2::Resource::XObject::Image::GIF->new($self->{'pdf'}, $file);
2395  
2396 2 $self->{'pdf'}->out_obj($self->{'pages'});
2397  
2398 2 return $obj;
2399 }
2400  
2401 0 ▶sub image_gd {
Subroutine image_gd: not called CC=1 SCAR=6.9
Pod documented
2402 0 my ($self, $gd, %opts) = @_;
2403  
2404 0 require PDF::API2::Resource::XObject::Image::GD;
2405 0 my $obj = PDF::API2::Resource::XObject::Image::GD->new($self->{'pdf'}, $gd, undef, %opts);
2406  
2407 0 $self->{'pdf'}->out_obj($self->{'pages'});
2408  
2409 0 return $obj;
2410 }
2411  
2412 =head2 barcode
2413  
2414 $object = $pdf->barcode($format, $code, %options);
2415  
2416 Generate and return a barcode that can be placed as part of a page's content:
2417  
2418 my $pdf = PDF::API2->new();
2419 my $page = $pdf->page();
2420  
2421 my $barcode = $pdf->barcode('ean13', '0123456789012');
2422 $page->object($barcode, 100, 100);
2423  
2424 my $qr_code = $pdf->barcode('qr', 'http://www.example.com');
2425 $page->object($qr_code, 100, 300, 144 / $qr_code->width())
2426  
2427 $pdf->save('sample.pdf');
2428  
2429 C<$format> can be one of C<codabar>, C<code128>, C<code39> (a.k.a. 3 of 9),
2430 C<ean128>, C<ean13>, C<itf> (a.k.a. interleaved 2 of 5), or C<qr>.
2431  
2432 C<$code> is the value to be encoded. Start and stop characters are only
2433 required when they're not static (e.g. for Codabar).
2434  
2435 The following options are available:
2436  
2437 =over
2438  
2439 =item * bar_width
2440  
2441 The width of the smallest bar or space in points (72 points = 1 inch).
2442  
2443 If you're following a specification that gives bar width in mils (thousandths of
2444 an inch), use this conversion: C<$points = $mils / 1000 * 72>.
2445  
2446 =item * bar_height
2447  
2448 The base height of the barcode in points.
2449  
2450 =item * bar_extend
2451  
2452 If present and applicable, bars for non-printing characters (e.g. start and stop
2453 characters) will be extended downward by this many points, and printing
2454 characters will be shown below their respective bars.
2455  
2456 This is enabled by default for EAN-13 barcodes.
2457  
2458 =item * caption
2459  
2460 If present, this value will be printed, centered, beneath the barcode, and
2461 should be a human-readable representation of the barcode. This option is
2462 ignored for QR codes.
2463  
2464 =item * font
2465  
2466 A font object (created by L</"font">) that will be used to print the caption, or
2467 the printable characters when C<bar_extend> is set.
2468  
2469 Helvetica will be used by default.
2470  
2471 =item * font_size
2472  
2473 The size of the font used for printing the caption or printable characters.
2474  
2475 The default will be calculated based on the barcode size, if C<bar_extend> is
2476 set, or 10 otherwise.
2477  
2478 =item * quiet_zone
2479  
2480 A margin, in points, that will be place before the left and bottom edges of the
2481 barcode (including the caption, if present). This is used to help barcode
2482 scanners tell where the barcode begins and ends.
2483  
2484 The default is the width of one encoded character, or four squares for QR codes.
2485  
2486 =item * bar_overflow
2487  
2488 Shrinks the horizontal width of bars by this amount in points to account for ink
2489 spread when printing. This option is ignored for QR codes.
2490  
2491 The default is 0.01 points.
2492  
2493 =item * color
2494  
2495 Draw bars using this color, which may be any value accepted by
2496 L<PDF::API2::Content/"fillcolor">.
2497  
2498 The default is black.
2499  
2500 =back
2501  
2502 QR codes have
2503 L<additional options|PDF::API2::Resource::XObject::Form::BarCode::qrcode> for
2504 customizing the error correction level and other niche settings.
2505  
2506 =cut
2507  
2508 0 ▶sub barcode {
Subroutine barcode: not called CC=50 SCAR=78.4
Pod documented
2509 0 my ($self, $format, $value, %options) = @_;
2510 0 ▶ croak "Missing barcode format" unless defined $format;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $format 0 0 0
2511 0 ▶ croak "Missing barcode value" unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
2512  
2513 # Set defaults to approximately the minimums for each barcode format.
2514 0 ▶ if ($format eq 'codabar') {
Branch: 5 branches
BranchTrueFalseTotal
if ($format eq 'codabar') { } 0 0 0
elsif ($format eq 'code128' or $format eq 'ean128' or $format eq 'code39') { } 0 0 0
elsif ($format eq 'itf') { } 0 0 0
elsif ($format eq 'ean13') { } 0 0 0
elsif ($format eq 'qr') { } 0 0 0
2515 0 ▶ $options{'bar_width'} //= 1.8; # 0.025"
Condition0%0/2
$options{"bar_width"} //= "1.8"
l!l
00
MC/DC0%0/1
$options{"bar_width"} //= "1.8"
$options{"bar_width"}
Truth table0%0/2
A
A= $options{"bar_width"}
Aresult
11
00
2516 0 ▶ $options{'bar_extend'} //= 0;
Condition0%0/2
$options{"bar_extend"} //= 0
l!l
00
MC/DC0%0/1
$options{"bar_extend"} //= 0
$options{"bar_extend"}
Truth table0%0/2
A
A= $options{"bar_extend"}
Aresult
11
00
2517 0 ▶ $options{'quiet_zone'} //= 10 * $options{'bar_width'};
Condition0%0/2
$options{"quiet_zone"} //= 10 * $options{"bar_width"}
l!l
00
MC/DC0%0/1
$options{"quiet_zone"} //= 10 * $options{"bar_width"}
$options{"quiet_zone"}
Truth table0%0/2
A
A= $options{"quiet_zone"}
Aresult
11
00
2518 0 ▶ if ($options{'bar_extend'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"bar_extend"}) 0 0 0
2519 0 ▶ $options{'font_size'} //= 9 * $options{'bar_width'};
Condition0%0/2
$options{"font_size"} //= 9 * $options{"bar_width"}
l!l
00
MC/DC0%0/1
$options{"font_size"} //= 9 * $options{"bar_width"}
$options{"font_size"}
Truth table0%0/2
A
A= $options{"font_size"}
Aresult
11
00
2520 }
2521  
2522 # Minimum height is the larger of 0.25" or 15% of barcode length.
2523 0 my $length = (10 * length($value) + 2) * $options{'bar_width'};
2524 0 ▶ $options{'bar_height'} //= max(18, $length * 0.15);
Condition0%0/3
$options{"bar_height"} //= &max(18, $length * "0.15")
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"bar_height"} //= &max(18, $length * "0.15")
$options{"bar_height"} &max(18, $length * "0.15")
Truth table0%0/3
A //= B
A= $options{"bar_height"}B= &max(18, $length * "0.15")
ABresult
1X1
011
000
2525 }
2526 ▶ elsif ($format eq 'code128' or $format eq 'ean128' or $format eq 'code39') {
Condition0%0/6
$format eq "code128" or $format eq "ean128" or $format eq "code39"
l!l&&r!l&&!r
000
$format eq "code128" or $format eq "ean128"
l!l&&r!l&&!r
000
MC/DC0%0/3
$format eq "code128" or $format eq "ean128" or $format eq "code39"
$format eq "code128" $format eq "ean128" $format eq "code39"
Truth table0%0/4
A or B or C
A= $format eq "code128"B= $format eq "ean128"C= $format eq "code39"
ABCresult
1XX1
01X1
0011
0000
2527 0 ▶ $options{'bar_width'} //= 1;
Condition0%0/2
$options{"bar_width"} //= 1
l!l
00
MC/DC0%0/1
$options{"bar_width"} //= 1
$options{"bar_width"}
Truth table0%0/2
A
A= $options{"bar_width"}
Aresult
11
00
2528 0 ▶ $options{'bar_extend'} //= 0;
Condition0%0/2
$options{"bar_extend"} //= 0
l!l
00
MC/DC0%0/1
$options{"bar_extend"} //= 0
$options{"bar_extend"}
Truth table0%0/2
A
A= $options{"bar_extend"}
Aresult
11
00
2529 0 ▶ $options{'quiet_zone'} //= 11 * $options{'bar_width'};
Condition0%0/2
$options{"quiet_zone"} //= 11 * $options{"bar_width"}
l!l
00
MC/DC0%0/1
$options{"quiet_zone"} //= 11 * $options{"bar_width"}
$options{"quiet_zone"}
Truth table0%0/2
A
A= $options{"quiet_zone"}
Aresult
11
00
2530 0 ▶ if ($options{'bar_extend'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"bar_extend"}) 0 0 0
2531 0 ▶ $options{'font_size'} //= 10 * $options{'bar_width'};
Condition0%0/2
$options{"font_size"} //= 10 * $options{"bar_width"}
l!l
00
MC/DC0%0/1
$options{"font_size"} //= 10 * $options{"bar_width"}
$options{"font_size"}
Truth table0%0/2
A
A= $options{"font_size"}
Aresult
11
00
2532 }
2533  
2534 # Minimum height is the larger of 0.5" or 15% of barcode length.
2535 0 my $length = 11 * (length($value) + 1) * $options{'bar_width'};
2536 0 ▶ $options{'bar_height'} //= max(36, $length * 0.15);
Condition0%0/3
$options{"bar_height"} //= &max(36, $length * "0.15")
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"bar_height"} //= &max(36, $length * "0.15")
$options{"bar_height"} &max(36, $length * "0.15")
Truth table0%0/3
A //= B
A= $options{"bar_height"}B= &max(36, $length * "0.15")
ABresult
1X1
011
000
2537 }
2538 elsif ($format eq 'itf') {
2539 0 ▶ $options{'bar_width'} //= 1;
Condition0%0/2
$options{"bar_width"} //= 1
l!l
00
MC/DC0%0/1
$options{"bar_width"} //= 1
$options{"bar_width"}
Truth table0%0/2
A
A= $options{"bar_width"}
Aresult
11
00
2540 0 ▶ $options{'bar_height'} //= 40;
Condition0%0/2
$options{"bar_height"} //= 40
l!l
00
MC/DC0%0/1
$options{"bar_height"} //= 40
$options{"bar_height"}
Truth table0%0/2
A
A= $options{"bar_height"}
Aresult
11
00
2541 0 ▶ $options{'bar_extend'} //= 0;
Condition0%0/2
$options{"bar_extend"} //= 0
l!l
00
MC/DC0%0/1
$options{"bar_extend"} //= 0
$options{"bar_extend"}
Truth table0%0/2
A
A= $options{"bar_extend"}
Aresult
11
00
2542 0 ▶ $options{'quiet_zone'} //= 10 * $options{'bar_width'};
Condition0%0/2
$options{"quiet_zone"} //= 10 * $options{"bar_width"}
l!l
00
MC/DC0%0/1
$options{"quiet_zone"} //= 10 * $options{"bar_width"}
$options{"quiet_zone"}
Truth table0%0/2
A
A= $options{"quiet_zone"}
Aresult
11
00
2543 0 ▶ if ($options{'bar_extend'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"bar_extend"}) 0 0 0
2544 0 ▶ $options{'font_size'} //= 9 * $options{'bar_width'};
Condition0%0/2
$options{"font_size"} //= 9 * $options{"bar_width"}
l!l
00
MC/DC0%0/1
$options{"font_size"} //= 9 * $options{"bar_width"}
$options{"font_size"}
Truth table0%0/2
A
A= $options{"font_size"}
Aresult
11
00
2545 }
2546 }
2547 elsif ($format eq 'ean13') {
2548 0 ▶ $options{'bar_width'} //= 1;
Condition0%0/2
$options{"bar_width"} //= 1
l!l
00
MC/DC0%0/1
$options{"bar_width"} //= 1
$options{"bar_width"}
Truth table0%0/2
A
A= $options{"bar_width"}
Aresult
11
00
2549 0 ▶ $options{'bar_height'} //= 64.8;
Condition0%0/2
$options{"bar_height"} //= "64.8"
l!l
00
MC/DC0%0/1
$options{"bar_height"} //= "64.8"
$options{"bar_height"}
Truth table0%0/2
A
A= $options{"bar_height"}
Aresult
11
00
2550 0 ▶ $options{'quiet_zone'} //= 11 * $options{'bar_width'};
Condition0%0/2
$options{"quiet_zone"} //= 11 * $options{"bar_width"}
l!l
00
MC/DC0%0/1
$options{"quiet_zone"} //= 11 * $options{"bar_width"}
$options{"quiet_zone"}
Truth table0%0/2
A
A= $options{"quiet_zone"}
Aresult
11
00
2551 0 ▶ unless ($options{'caption'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"caption"}) 0 0 0
2552 0 ▶ $options{'bar_extend'} //= 5 * $options{'bar_width'};
Condition0%0/2
$options{"bar_extend"} //= 5 * $options{"bar_width"}
l!l
00
MC/DC0%0/1
$options{"bar_extend"} //= 5 * $options{"bar_width"}
$options{"bar_extend"}
Truth table0%0/2
A
A= $options{"bar_extend"}
Aresult
11
00
2553 }
2554 0 ▶ if ($options{'bar_extend'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"bar_extend"}) 0 0 0
2555 0 ▶ $options{'font_size'} //= 10 * $options{'bar_width'};
Condition0%0/2
$options{"font_size"} //= 10 * $options{"bar_width"}
l!l
00
MC/DC0%0/1
$options{"font_size"} //= 10 * $options{"bar_width"}
$options{"font_size"}
Truth table0%0/2
A
A= $options{"font_size"}
Aresult
11
00
2556 }
2557 }
2558 elsif ($format eq 'qr') {
2559 0 ▶ $options{'bar_width'} //= 1;
Condition0%0/2
$options{"bar_width"} //= 1
l!l
00
MC/DC0%0/1
$options{"bar_width"} //= 1
$options{"bar_width"}
Truth table0%0/2
A
A= $options{"bar_width"}
Aresult
11
00
2560 0 ▶ $options{'bar_height'} //= $options{'bar_width'};
Condition0%0/3
$options{"bar_height"} //= $options{"bar_width"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"bar_height"} //= $options{"bar_width"}
$options{"bar_height"} $options{"bar_width"}
Truth table0%0/3
A //= B
A= $options{"bar_height"}B= $options{"bar_width"}
ABresult
1X1
011
000
2561 0 ▶ $options{'quiet_zone'} //= 4 * $options{'bar_width'};
Condition0%0/2
$options{"quiet_zone"} //= 4 * $options{"bar_width"}
l!l
00
MC/DC0%0/1
$options{"quiet_zone"} //= 4 * $options{"bar_width"}
$options{"quiet_zone"}
Truth table0%0/2
A
A= $options{"quiet_zone"}
Aresult
11
00
2562 }
2563 else {
2564 0 croak "Unrecognized barcode format: $format";
2565 }
2566  
2567 0 ▶ if (exists $options{'caption'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{"caption"}) 0 0 0
2568 0 ▶ $options{'font_size'} //= 10;
Condition0%0/2
$options{"font_size"} //= 10
l!l
00
MC/DC0%0/1
$options{"font_size"} //= 10
$options{"font_size"}
Truth table0%0/2
A
A= $options{"font_size"}
Aresult
11
00
2569 }
2570 0 ▶ if ($options{'bar_extend'} or $options{'font_size'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"bar_extend"} or $options{"font_size"}) 0 0 0
Condition0%0/3
$options{"bar_extend"} or $options{"font_size"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"bar_extend"} or $options{"font_size"}
$options{"bar_extend"} $options{"font_size"}
Truth table0%0/3
A or B
A= $options{"bar_extend"}B= $options{"font_size"}
ABresult
1X1
011
000
2571 0 ▶ $options{'font'} //= $self->font('Helvetica');
Condition0%0/3
$options{"font"} //= $self->font("Helvetica")
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"font"} //= $self->font("Helvetica")
$options{"font"} $self->font("Helvetica")
Truth table0%0/3
A //= B
A= $options{"font"}B= $self->font("Helvetica")
ABresult
1X1
011
000
2572 }
2573  
2574 # Convert from new arguments to old arguments
2575 0 $options{'-color'} = delete $options{'color'};
2576 0 $options{'-fnsz'} = delete $options{'font_size'};
2577 0 $options{'-font'} = delete $options{'font'};
2578 0 $options{'-lmzn'} = delete $options{'bar_extend'};
2579 0 $options{'-mils'} = (delete $options{'bar_width'}) * 1000 / 72;
2580 0 $options{'-ofwt'} = delete $options{'bar_overflow'};
2581 0 $options{'-quzn'} = delete $options{'quiet_zone'};
2582 0 $options{'-zone'} = delete $options{'bar_height'};
2583  
2584 0 ▶ if ($format eq 'codabar') {
Branch: 7 branches
BranchTrueFalseTotal
if ($format eq 'codabar') { } 0 0 0
elsif ($format eq 'code128') { } 0 0 0
elsif ($format eq 'code39') { } 0 0 0
elsif ($format eq 'ean128') { } 0 0 0
elsif ($format eq 'ean13') { } 0 0 0
elsif ($format eq 'itf') { } 0 0 0
elsif ($format eq 'qr') { } 0 0 0
2585 0 return $self->xo_codabar(%options, -code => $value);
2586 }
2587 elsif ($format eq 'code128') {
2588 0 return $self->xo_code128(%options, -code => $value);
2589 }
2590 elsif ($format eq 'code39') {
2591 0 return $self->xo_3of9(%options, -code => $value);
2592 }
2593 elsif ($format eq 'ean128') {
2594 0 return $self->xo_code128(%options, -code => $value, -ean => 1);
2595 }
2596 elsif ($format eq 'ean13') {
2597 0 return $self->xo_ean13(%options, -code => $value);
2598 }
2599 elsif ($format eq 'itf') {
2600 0 return $self->xo_2of5int(%options, -code => $value);
2601 }
2602 elsif ($format eq 'qr') {
2603 0 my $qr_class = 'PDF::API2::Resource::XObject::Form::BarCode::qrcode';
2604 0 eval "require $qr_class";
2605 0 my $obj = $qr_class->new($self->{'pdf'}, %options, code => $value);
2606 # $self->{'pdf'}->out_obj($self->{'pages'});
2607 0 return $obj;
2608 }
2609 }
2610  
2611 1 ▶sub xo_code128 {
Subroutine xo_code128: called CC=1 SCAR=0.0
Pod documented
2612 1 my ($self, @opts) = @_;
2613  
2614 1 require PDF::API2::Resource::XObject::Form::BarCode::code128;
2615 1 my $obj = PDF::API2::Resource::XObject::Form::BarCode::code128->new($self->{'pdf'}, @opts);
2616  
2617 1 $self->{'pdf'}->out_obj($self->{'pages'});
2618  
2619 1 return $obj;
2620 }
2621  
2622 1 ▶sub xo_codabar {
Subroutine xo_codabar: called CC=1 SCAR=0.0
Pod documented
2623 1 my ($self, @opts) = @_;
2624  
2625 1 require PDF::API2::Resource::XObject::Form::BarCode::codabar;
2626 1 my $obj = PDF::API2::Resource::XObject::Form::BarCode::codabar->new($self->{'pdf'}, @opts);
2627  
2628 1 $self->{'pdf'}->out_obj($self->{'pages'});
2629  
2630 1 return $obj;
2631 }
2632  
2633 1 ▶sub xo_2of5int {
Subroutine xo_2of5int: called CC=1 SCAR=0.0
Pod documented
2634 1 my ($self, @opts) = @_;
2635  
2636 1 require PDF::API2::Resource::XObject::Form::BarCode::int2of5;
2637 1 my $obj = PDF::API2::Resource::XObject::Form::BarCode::int2of5->new($self->{'pdf'}, @opts);
2638  
2639 1 $self->{'pdf'}->out_obj($self->{'pages'});
2640  
2641 1 return $obj;
2642 }
2643  
2644 2 ▶sub xo_3of9 {
Subroutine xo_3of9: called CC=1 SCAR=0.0
Pod documented
2645 2 my ($self, @opts) = @_;
2646  
2647 2 require PDF::API2::Resource::XObject::Form::BarCode::code3of9;
2648 2 my $obj = PDF::API2::Resource::XObject::Form::BarCode::code3of9->new($self->{'pdf'}, @opts);
2649  
2650 2 $self->{'pdf'}->out_obj($self->{'pages'});
2651  
2652 2 return $obj;
2653 }
2654  
2655 1 ▶sub xo_ean13 {
Subroutine xo_ean13: called CC=1 SCAR=0.0
Pod documented
2656 1 my ($self, @opts) = @_;
2657  
2658 1 require PDF::API2::Resource::XObject::Form::BarCode::ean13;
2659 1 my $obj = PDF::API2::Resource::XObject::Form::BarCode::ean13->new($self->{'pdf'}, @opts);
2660  
2661 1 $self->{'pdf'}->out_obj($self->{'pages'});
2662  
2663 1 return $obj;
2664 }
2665  
2666 =head2 colorspace
2667  
2668 $colorspace = $pdf->colorspace($type, @arguments);
2669  
2670 Colorspaces can be added to a PDF to either specifically control the output
2671 color on a particular device (spot colors, device colors) or to save space by
2672 limiting the available colors to a defined color palette (web-safe palette, ACT
2673 file).
2674  
2675 Once added to the PDF, they can be used in place of regular hex codes or named
2676 colors:
2677  
2678 my $pdf = PDF::API2->new();
2679 my $page = $pdf->page();
2680 my $content = $page->graphics();
2681  
2682 # Add colorspaces for a spot color and the web-safe color palette
2683 my $spot = $pdf->colorspace('spot', 'PANTONE Red 032 C', '#EF3340');
2684 my $web = $pdf->colorspace('web');
2685  
2686 # Fill using the spot color with 100% coverage
2687 $content->fill_color($spot, 1.0);
2688  
2689 # Stroke using the first color of the web-safe palette
2690 $content->stroke_color($web, 0);
2691  
2692 # Add a rectangle to the page
2693 $content->rectangle(100, 100, 200, 200);
2694 $content->paint();
2695  
2696 $pdf->save('sample.pdf');
2697  
2698 The following types of colorspaces are supported
2699  
2700 =over
2701  
2702 =item * spot
2703  
2704 my $spot = $pdf->colorspace('spot', $tint, $alt_color);
2705  
2706 Spot colors are used to instruct a device (usually a printer) to use or emulate
2707 a particular ink color (C<$tint>) for parts of the document. An C<$alt_color>
2708 is provided for devices (e.g. PDF viewers) that don't know how to produce the
2709 named color. It can either be an approximation of the color in RGB, CMYK, or
2710 HSV formats, or a wildly different color (e.g. 100% magenta, C<%0F00>) to make
2711 it clear if the spot color isn't being used as expected.
2712  
2713 =item * web
2714  
2715 my $web = $pdf->colorspace('web');
2716  
2717 The web-safe color palette is a historical collection of colors that was used
2718 when many display devices only supported 256 colors.
2719  
2720 =item * act
2721  
2722 my $act = $pdf->colorspace('act', $filename);
2723  
2724 An Adobe Color Table (ACT) file provides a custom palette of colors that can be
2725 referenced by PDF graphics and text drawing commands.
2726  
2727 =item * device
2728  
2729 my $devicen = $pdf->colorspace('device', @colorspaces);
2730  
2731 A device-specific colorspace allows for precise color output on a given device
2732 (typically a printing press), bypassing the normal color interpretation
2733 performed by raster image processors (RIPs).
2734  
2735 Device colorspaces are also needed if you want to blend spot colors:
2736  
2737 my $pdf = PDF::API2->new();
2738 my $page = $pdf->page();
2739 my $content = $page->graphics();
2740  
2741 # Create a two-color device colorspace
2742 my $yellow = $pdf->colorspace('spot', 'Yellow', '%00F0');
2743 my $spot = $pdf->colorspace('spot', 'PANTONE Red 032 C', '#EF3340');
2744 my $device = $pdf->colorspace('device', $yellow, $spot);
2745  
2746 # Fill using a blend of 25% yellow and 75% spot color
2747 $content->fill_color($device, 0.25, 0.75);
2748  
2749 # Stroke using 100% spot color
2750 $content->stroke_color($device, 0, 1);
2751  
2752 # Add a rectangle to the page
2753 $content->rectangle(100, 100, 200, 200);
2754 $content->paint();
2755  
2756 $pdf->save('sample.pdf');
2757  
2758 =back
2759  
2760 =cut
2761  
2762 0 ▶sub colorspace {
Subroutine colorspace: not called CC=6 SCAR=37.4
Pod documented
2763 0 my $self = shift();
2764 0 my $type = shift();
2765  
2766 0 ▶ if ($type eq 'act') {
Branch: 5 branches
BranchTrueFalseTotal
if ($type eq 'act') { } 0 0 0
elsif ($type eq 'web') { } 0 0 0
elsif ($type eq 'hue') { } 0 0 0
elsif ($type eq 'spot') { } 0 0 0
elsif ($type eq 'device') { } 0 0 0
2767 0 my $file = shift();
2768 0 return $self->colorspace_act($file);
2769 }
2770 elsif ($type eq 'web') {
2771 0 return $self->colorspace_web();
2772 }
2773 elsif ($type eq 'hue') {
2774 # This type is undocumented until either a reference can be found for
2775 # this being a standard palette like the web color palette, or POD is
2776 # added to the Hue colorspace class that describes how to use it.
2777 0 return $self->colorspace_hue();
2778 }
2779 elsif ($type eq 'spot') {
2780 0 my $name = shift();
2781 0 my $alt_color = shift();
2782 0 return $self->colorspace_separation($name, $alt_color);
2783 }
2784 elsif ($type eq 'device') {
2785 0 my @colors = @_;
2786 0 return $self->colorspace_devicen(\@colors);
2787 }
2788 else {
2789 0 croak "Unrecognized or unsupported colorspace: $type";
2790 }
2791 }
2792  
2793 0 ▶sub colorspace_act {
Subroutine colorspace_act: not called CC=1 SCAR=6.9
Pod documented
2794 0 my ($self, $file) = @_;
2795  
2796 0 require PDF::API2::Resource::ColorSpace::Indexed::ACTFile;
2797 0 return PDF::API2::Resource::ColorSpace::Indexed::ACTFile->new($self->{'pdf'},
2798 $file);
2799 }
2800  
2801 1 ▶sub colorspace_web {
Subroutine colorspace_web: called CC=1 SCAR=0.0
Pod documented
2802 1 my $self = shift();
2803  
2804 1 require PDF::API2::Resource::ColorSpace::Indexed::WebColor;
2805 1 return PDF::API2::Resource::ColorSpace::Indexed::WebColor->new($self->{'pdf'});
2806 }
2807  
2808 0 ▶sub colorspace_hue {
Subroutine colorspace_hue: not called CC=1 SCAR=6.9
Pod documented
2809 0 my $self = shift();
2810  
2811 0 require PDF::API2::Resource::ColorSpace::Indexed::Hue;
2812 0 return PDF::API2::Resource::ColorSpace::Indexed::Hue->new($self->{'pdf'});
2813 }
2814  
2815 0 ▶sub colorspace_separation {
Subroutine colorspace_separation: not called CC=1 SCAR=6.9
Pod documented
2816 0 my ($self, $name, @clr) = @_;
2817  
2818 0 require PDF::API2::Resource::ColorSpace::Separation;
2819 0 return PDF::API2::Resource::ColorSpace::Separation->new($self->{'pdf'},
2820 pdfkey(),
2821 $name,
2822 @clr);
2823 }
2824  
2825 0 ▶sub colorspace_devicen {
Subroutine colorspace_devicen: not called CC=1 SCAR=6.9
Pod documented
2826 0 my ($self, $clrs) = @_;
2827  
2828 0 require PDF::API2::Resource::ColorSpace::DeviceN;
2829 0 return PDF::API2::Resource::ColorSpace::DeviceN->new($self->{'pdf'},
2830 pdfkey(),
2831 $clrs);
2832 }
2833  
2834 =head2 egstate
2835  
2836 $resource = $pdf->egstate();
2837  
2838 Creates and returns a new extended graphics state object, described in
2839 L<PDF::API2::Resource::ExtGState>.
2840  
2841 =cut
2842  
2843 3 ▶sub egstate {
Subroutine egstate: called CC=1 SCAR=0.0
Pod documented
2844 3 my $self = shift();
2845  
2846 3 my $obj = PDF::API2::Resource::ExtGState->new($self->{'pdf'}, pdfkey());
2847  
2848 3 $self->{'pdf'}->out_obj($self->{'pages'});
2849  
2850 3 return $obj;
2851 }
2852  
2853 8 ▶sub default {
Subroutine default: called CC=2 SCAR=6.9
Pod documented
2854 8 my ($self, $parameter, $value) = @_;
2855  
2856 # Parameter names may consist of lowercase letters, numbers, and underscores
2857 8 $parameter = lc $parameter;
2858 8 $parameter =~ s/[^a-z\d_]//g;
2859  
2860 8 my $previous_value = $self->{$parameter};
2861 8 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 2 6 8
2862 2 $self->{$parameter} = $value;
2863 }
2864 8 return $previous_value;
2865 }
2866  
2867 4 ▶sub xo_form {
Subroutine xo_form: called CC=1 SCAR=0.0
Pod undocumented
2868 4 my $self = shift();
2869  
2870 4 my $obj = PDF::API2::Resource::XObject::Form::Hybrid->new($self->{'pdf'});
2871  
2872 4 $self->{'pdf'}->out_obj($self->{'pages'});
2873  
2874 4 return $obj;
2875 }
2876  
2877 0 ▶sub pattern {
Subroutine pattern: not called CC=1 SCAR=6.9
Pod undocumented
2878 0 my ($self, %opts) = @_;
2879  
2880 0 my $obj = PDF::API2::Resource::Pattern->new($self->{'pdf'}, undef, %opts);
2881  
2882 0 $self->{'pdf'}->out_obj($self->{'pages'});
2883  
2884 0 return $obj;
2885 }
2886  
2887 0 ▶sub shading {
Subroutine shading: not called CC=1 SCAR=6.9
Pod undocumented
2888 0 my ($self, %opts) = @_;
2889  
2890 0 my $obj = PDF::API2::Resource::Shading->new($self->{'pdf'}, undef, %opts);
2891  
2892 0 $self->{'pdf'}->out_obj($self->{'pages'});
2893  
2894 0 return $obj;
2895 }
2896  
2897 0 ▶sub named_destination {
Subroutine named_destination: not called CC=8 SCAR=42.8
Pod undocumented
2898 0 my ($self, $cat, $name, $obj) = @_;
2899 0 my $root = $self->{'catalog'};
2900  
2901 0 ▶ $root->{'Names'} ||= PDFDict();
Condition0%0/3
$root->{"Names"} ||= PDFDict()
l!l&&r!l&&!r
000
MC/DC0%0/2
$root->{"Names"} ||= PDFDict()
$root->{"Names"} PDFDict()
Truth table0%0/3
A ||= B
A= $root->{"Names"}B= PDFDict()
ABresult
1X1
011
000
2902 0 ▶ $root->{'Names'}->{$cat} ||= PDFDict();
Condition0%0/3
$root->{"Names"}{$cat} ||= PDFDict()
l!l&&r!l&&!r
000
MC/DC0%0/2
$root->{"Names"}{$cat} ||= PDFDict()
$root->{"Names"}{$cat} PDFDict()
Truth table0%0/3
A ||= B
A= $root->{"Names"}{$cat}B= PDFDict()
ABresult
1X1
011
000
2903 0 ▶ $root->{'Names'}->{$cat}->{'-vals'} ||= {};
Condition0%0/2
$root->{"Names"}{$cat}{"-vals"} ||= {}
l!l
00
MC/DC0%0/1
$root->{"Names"}{$cat}{"-vals"} ||= {}
$root->{"Names"}{$cat}{"-vals"}
Truth table0%0/2
A
A= $root->{"Names"}{$cat}{"-vals"}
Aresult
11
00
2904 0 ▶ $root->{'Names'}->{$cat}->{'Limits'} ||= PDFArray();
Condition0%0/3
$root->{"Names"}{$cat}{"Limits"} ||= PDFArray()
l!l&&r!l&&!r
000
MC/DC0%0/2
$root->{"Names"}{$cat}{"Limits"} ||= PDFArray()
$root->{"Names"}{$cat}{"Limits"} PDFArray()
Truth table0%0/3
A ||= B
A= $root->{"Names"}{$cat}{"Limits"}B= PDFArray()
ABresult
1X1
011
000
2905 0 ▶ $root->{'Names'}->{$cat}->{'Names'} ||= PDFArray();
Condition0%0/3
$root->{"Names"}{$cat}{"Names"} ||= PDFArray()
l!l&&r!l&&!r
000
MC/DC0%0/2
$root->{"Names"}{$cat}{"Names"} ||= PDFArray()
$root->{"Names"}{$cat}{"Names"} PDFArray()
Truth table0%0/3
A ||= B
A= $root->{"Names"}{$cat}{"Names"}B= PDFArray()
ABresult
1X1
011
000
2906  
2907 0 ▶ unless (defined $obj) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $obj) 0 0 0
2908 0 $obj = PDF::API2::NamedDestination->new($self->{'pdf'});
2909 }
2910 0 $root->{'Names'}->{$cat}->{'-vals'}->{$name} = $obj;
2911  
2912 0 my @names = sort {$a cmp $b} keys %{$root->{'Names'}->{$cat}->{'-vals'}};
2913  
2914 0 $root->{'Names'}->{$cat}->{'Limits'}->{' val'}->[0] = PDFStr($names[0]);
2915 0 $root->{'Names'}->{$cat}->{'Limits'}->{' val'}->[1] = PDFStr($names[-1]);
2916  
2917 0 @{$root->{'Names'}->{$cat}->{'Names'}->{' val'}} = ();
2918  
2919 0 foreach my $k (@names) {
2920 0 push @{$root->{'Names'}->{$cat}->{'Names'}->{' val'}}, (
2921 PDFStr($k),
2922 0 $root->{'Names'}->{$cat}->{'-vals'}->{$k}
2923 );
2924 }
2925  
2926 0 return $obj;
2927 }
2928  
2929 39 1;
2930  
2931 __END__
« API2/ViewerPreferences.pm ↑ Summary API2/UniWrap.pm »