Create.pm

filter: Statementstmt 97.2% 600 / 617 Branchbran 74.7% 175 / 234 Conditioncond 74.1% 109 / 147 MC/DCmcdc 32.9% 26 / 79 Subroutinesub 96.0% 49 / 51 Podpod 27.5% 11 / 40 totaltotal 83.0% 970 / 1168 CC 188 SCAR 27.9
CC
188
Cov
89%
encode
36.9
add_pages
30.1
new
24.5
CRAP 16.2
« Image/GIF.pm ↑ Summary Create/Outline.pm »
1 package PDF::Create;
2  
3 18 our $VERSION = '1.28';
4  
5 =head1 NAME
6  
7 PDF::Create - Create PDF files.
8  
9 =head1 VERSION
10  
11 Version 1.28
12  
13 =cut
14  
15 18 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 18 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 18 ▶use Carp qw(confess croak cluck carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 18 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 18 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 18 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 18 ▶use PDF::Image::GIF;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 18 ▶use PDF::Image::JPEG;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 18 ▶use PDF::Create::Page;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 18 ▶use PDF::Create::Outline;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 18 my $DEBUG = 0;
29  
30 =encoding utf8
31  
32 =head1 DESCRIPTION
33  
34 C<PDF::Create> allows you to create PDF document using a number of primitives.The
35 result is as a PDF file or stream. PDF stands for Portable Document Format.
36  
37 Documents can have several pages, a table of content, an information section and
38 many other PDF elements.
39  
40 =head1 SYNOPSIS
41  
42 C<PDF::Create> provides an easy module to create PDF output from your perl script.
43 It is designed to be easy to use and simple to install and maintain. It provides a
44 couple of subroutines to handle text, fonts, images and drawing primitives. Simple
45 documents are easy to create with the supplied routines.
46  
47 In addition to be reasonable simple C<PDF::Create> is written in pure Perl and has
48 no external dependencies (libraries, other modules, etc.). It should run on any
49 platform where perl is available.
50  
51 For complex stuff some understanding of the underlying Postscript/PDF format is
52 necessary. In this case it might be better go with the more complete L<PDF::API2>
53 modules to gain more features at the expense of a steeper learning curve.
54  
55 Example PDF creation with C<PDF::Create>:
56  
57 use strict; use warnings;
58 use PDF::Create;
59  
60 my $pdf = PDF::Create->new(
61 'filename' => 'sample.pdf',
62 'Author' => 'John Doe',
63 'Title' => 'Sample PDF',
64 'CreationDate' => [ localtime ]
65 );
66  
67 # Add a A4 sized page
68 my $root = $pdf->new_page('MediaBox' => $pdf->get_page_size('A4'));
69  
70 # Add a page which inherits its attributes from $root
71 my $page1 = $root->new_page;
72  
73 # Prepare a font
74 my $font = $pdf->font('BaseFont' => 'Helvetica');
75  
76 # Prepare a Table of Content
77 my $toc = $pdf->new_outline('Title' => 'Title Page', 'Destination' => $page1);
78  
79 # Write some text
80 $page1->stringc($font, 40, 306, 426, 'PDF::Create');
81 $page1->stringc($font, 20, 306, 396, "version $PDF::Create::VERSION");
82 $page1->stringc($font, 20, 306, 300, 'by John Doe <john.doe@example.com>');
83  
84 # Add another page
85 my $page2 = $root->new_page;
86  
87 # Draw some lines
88 $page2->line(0, 0, 592, 840);
89 $page2->line(0, 840, 592, 0);
90  
91 $toc->new_outline('Title' => 'Second Page', 'Destination' => $page2);
92  
93 # Close the file and write the PDF
94 $pdf->close;
95  
96 =head1 CONSTRUCTOR
97  
98 The method C<new(%params)> create a new pdf structure for your PDF. It returns an
99 object handle which can be used to add more stuff to the PDF. The parameter keys
100 to the constructor are detailed as below:
101  
102 +--------------+------------------------------------------------------------+
103 | Key | Description |
104 +--------------+------------------------------------------------------------+
105 | | |
106 | filename | Destination file that will contain resulting PDF or '-' for|
107 | | stdout. |
108 | | |
109 | fh | Already opened filehandle that will contain resulting PDF. |
110 | | |
111 | Version | PDF Version to claim, can be 1.0 to 1.3 (default: 1. |
112 | | |
113 | PageMode | How the document should appear when opened.Possible values |
114 | | UseNone (Default), UseOutlines, UseThumbs and FullScreen |
115 | | |
116 | Author | The name of the person who created this document. |
117 | | |
118 | Creator | If the document was converted into a PDF document from |
119 | | another form, this is the name of the application that |
120 | | created the document. |
121 | | |
122 | Title | The title of the document. |
123 | | |
124 | Subject | The subject of the document. |
125 | | |
126 | Keywords | Keywords associated with the document. |
127 | | |
128 | CreationDate | The date the document was created.This is passed as an |
129 | | anonymous array in the same format as localtime returns. |
130 | | |
131 | Debug | The debug level, defaults to 0. It can be any positive |
132 | | integers. |
133 | | |
134 +--------------+------------------------------------------------------------+
135  
136 Example:
137  
138 my $pdf = PDF::Create->new(
139 'filename' => 'sample.pdf',
140 'Version' => 1.2,
141 'PageMode' => 'UseOutlines',
142 'Author' => 'John Doe',
143 'Title' => 'My Title',
144 'CreationDate' => [ localtime ]
145 );
146  
147 If you are writing a CGI you can send your PDF on the fly to stdout / directly to
148 the browser using '-' as filename.
149  
150 CGI Example:
151  
152 use CGI;
153 use PDF::Create;
154  
155 print CGI::header(-type => 'application/x-pdf', -attachment => 'sample.pdf');
156 my $pdf = PDF::Create->new(
157 'filename' => '-',
158 'Author' => 'John Doe',
159 'Title' => 'My title',
160 'CreationDate' => [ localtime ]
161 );
162  
163 =cut
164  
165 sub new {
166 33 ▶ my ($this, %params) = @_;
Subroutine new: called CC=23 SCAR=32.0
Pod undocumented
167  
168 # validate constructor keys
169 33 my %valid_constructor_keys = (
170 'fh' => 1,
171 'filename' => 1,
172 'Version' => 1,
173 'PageMode' => 1,
174 'Author' => 1,
175 'Creator' => 1,
176 'Title' => 1,
177 'Subject' => 1,
178 'Keywords' => 1,
179 'Debug' => 1,
180 'CreationDate' => 1,
181 );
182 33 foreach (keys %params) {
183 croak "Invalid constructor key '$_' received."
184 101 ▶ unless (exists $valid_constructor_keys{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $valid_constructor_keys{$_} 8 93 101
185 }
186  
187 25 ▶ if (exists $params{PageMode} && defined $params{PageMode}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"PageMode"} and defined $params{"PageMode"}) 19 6 25
Condition66%2/3
exists $params{"PageMode"} and defined $params{"PageMode"}
!ll&&!rl&&r
6019
MC/DC50%1/2
exists $params{"PageMode"} and defined $params{"PageMode"}
exists $params{"PageMode"} defined $params{"PageMode"}
Truth table66%2/3
A and B
A= exists $params{"PageMode"}B= defined $params{"PageMode"}
ABresult
0X0
100
111
188 # validate PageMode key value
189 19 my %valid_page_mode_values = (
190 'UseNone' => 1,
191 'UseOutlines' => 1,
192 'UseThumbs' => 1,
193 'FullScreen' => 1);
194 croak "Invalid value for key 'PageMode' received '". $params{PageMode} . "'"
195 19 ▶ unless (exists $valid_page_mode_values{$params{PageMode}});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $valid_page_mode_values{$params{"PageMode"}} 1 18 19
196 }
197  
198 24 ▶ if (exists $params{Debug} && defined $params{Debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"Debug"} and defined $params{"Debug"}) 2 22 24
Condition66%2/3
exists $params{"Debug"} and defined $params{"Debug"}
!ll&&!rl&&r
2202
MC/DC50%1/2
exists $params{"Debug"} and defined $params{"Debug"}
exists $params{"Debug"} defined $params{"Debug"}
Truth table66%2/3
A and B
A= exists $params{"Debug"}B= defined $params{"Debug"}
ABresult
0X0
100
111
199 # validate Debug key value
200 croak "Invalid value for key 'Debug' received '". $params{Debug} . "'"
201 2 ▶ unless (($params{Debug} =~ /^\d+$/) && ($params{Debug} >= 0));
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"Debug"} =~ /^\d+$/ and $params{"Debug"} >= 0 1 1 2
Condition66%2/3
$params{"Debug"} =~ /^\d+$/ and $params{"Debug"} >= 0
!ll&&!rl&&r
101
MC/DC50%1/2
$params{"Debug"} =~ /^\d+$/ and $params{"Debug"} >= 0
$params{"Debug"} =~ /^\d+$/ $params{"Debug"} >= 0
Truth table66%2/3
A and B
A= $params{"Debug"} =~ /^\d+$/B= $params{"Debug"} >= 0
ABresult
0X0
100
111
202 }
203  
204 23 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
0230
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
205 23 my $self = {};
206 23 bless $self, $class;
207  
208 23 $self->{'data'} = '';
209 23 ▶ $self->{'version'} = $params{'Version'} || "1.2";
Condition100%2/2
$params{'Version'} || '1.2'
l!l
185
MC/DC100%1/1
$params{'Version'} || '1.2'
$params{'Version'}
Truth table100%2/2
A
A= $params{'Version'}
Aresult
11
00
210 23 $self->{'trailer'} = {};
211  
212 23 $self->{'pages'} = PDF::Create::Page->new();
213 23 $self->{'current_page'} = $self->{'pages'};
214 # circular reference
215 23 $self->{'pages'}->{'pdf'} = $self;
216 23 weaken $self->{pages}{pdf};
217 23 $self->{'page_count'} = 0;
218 23 $self->{'outline_count'} = 0;
219  
220 # cross-reference table start address
221 23 $self->{'crossreftblstartaddr'} = 0;
222 23 $self->{'generation_number'} = 0;
223 23 $self->{'object_number'} = 0;
224  
225 23 ▶ if ( defined $params{'fh'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $params{'fh'}) { } 0 23 23
elsif (defined $params{'filename'}) { } 18 5 23
226 0 $self->{'fh'} = $params{'fh'};
227 } elsif ( defined $params{'filename'} ) {
228 18 $self->{'filename'} = $params{'filename'};
229 18 my $fh = FileHandle->new( "> $self->{'filename'}" );
230 18 ▶ carp "PDF::Create.pm: $self->{'filename'}: $!\n" unless defined $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 0 18 18
231 18 binmode $fh;
232 18 $self->{'fh'} = $fh;
233 }
234  
235 23 $self->{'catalog'} = {};
236 23 ▶ $self->{'catalog'}{'PageMode'} = $params{'PageMode'} if defined $params{'PageMode'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"PageMode"} 18 5 23
237  
238 # Header: add version
239 23 $self->add_version;
240  
241 # Info
242 23 ▶ $self->{'Author'} = $params{'Author'} if defined $params{'Author'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"Author"} 18 5 23
243 23 ▶ $self->{'Creator'} = $params{'Creator'} if defined $params{'Creator'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"Creator"} 0 23 23
244 23 ▶ $self->{'Title'} = $params{'Title'} if defined $params{'Title'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"Title"} 18 5 23
245 23 ▶ $self->{'Subject'} = $params{'Subject'} if defined $params{'Subject'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"Subject"} 0 23 23
246 23 ▶ $self->{'Keywords'} = $params{'Keywords'} if defined $params{'Keywords'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"Keywords"} 0 23 23
247  
248 # TODO: Default creation date from system date
249 23 ▶ if ( defined $params{'CreationDate'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"CreationDate"}) 0 23 23
250 $self->{'CreationDate'} =
251 sprintf "D:%4u%0.2u%0.2u%0.2u%0.2u%0.2u",
252 $params{'CreationDate'}->[5] + 1900, $params{'CreationDate'}->[4] + 1,
253 $params{'CreationDate'}->[3], $params{'CreationDate'}->[2],
254 0 $params{'CreationDate'}->[1], $params{'CreationDate'}->[0];
255 }
256 23 ▶ if ( defined $params{'Debug'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"Debug"}) 1 22 23
257 1 $DEBUG = $params{'Debug'};
258  
259 # Enable stack trace for PDF::Create internal routines
260 1 $Carp::Internal{ ('PDF::Create') }++;
261 }
262 23 debug( 1, "Debugging level $DEBUG" );
263 23 return $self;
264 }
265  
266 =head1 METHODS
267  
268 =head2 new_page(%params)
269  
270 Add a page to the document using the given parameters. C<new_page> must be called
271 first to initialize a root page, used as model for further pages.Returns a handle
272 to the newly created page. Parameters can be:
273  
274 +-----------+---------------------------------------------------------------+
275 | Key | Description |
276 +-----------+---------------------------------------------------------------+
277 | | |
278 | Parent | The parent of this page in the pages tree.This is page object.|
279 | | |
280 | Resources | Resources required by this page. |
281 | | |
282 | MediaBox | Rectangle specifying the natural size of the page,for example |
283 | | the dimensions of an A4 sheet of paper. The coordinates are |
284 | | measured in default user space units It must be the reference |
285 | | of 4 values array.You can use C<get_page_size> to get to get |
286 | | the size of standard paper sizes.C<get_page_size> knows about |
287 | | A0-A6, A4L (landscape), Letter, Legal, Broadsheet, Ledger, |
288 | | Tabloid, Executive and 36x36. |
289 | CropBox | Rectangle specifying the default clipping region for the page |
290 | | when displayed or printed. The default is the value of the |
291 | | MediaBox. |
292 | | |
293 | ArtBox | Rectangle specifying an area of the page to be used when |
294 | | placing PDF content into another application. The default is |
295 | | the value of the CropBox. [PDF 1.3] |
296 | | |
297 | TrimBox | Rectangle specifying the intended finished size of the page |
298 | | (for example, the dimensions of an A4 sheet of paper).In some |
299 | | cases,the MediaBox will be a larger rectangle, which includes |
300 | | printing instructions, cut marks or other content.The default |
301 | | is the value of the CropBox. [PDF 1.3]. |
302 | | |
303 | BleedBox | Rectangle specifying the region to which all page content |
304 | | should be clipped if the page is being output in a production |
305 | | environment. In such environments, a bleed area is desired, |
306 | | to accommodate physical limitations of cutting, folding, and |
307 | | trimming equipment. The actual printed page may include |
308 | | printer's marks that fall outside the bleed box. The default |
309 | | is the value of the CropBox.[PDF 1.3] |
310 | | |
311 | Rotate | Specifies the number of degrees the page should be rotated |
312 | | clockwise when it is displayed or printed. This value must be |
313 | | zero (the default) or a multiple of 90. The entire page, |
314 | | including contents is rotated. |
315 | | |
316 +-----------+---------------------------------------------------------------+
317  
318 Example:
319  
320 my $a4 = $pdf->new_page( 'MediaBox' => $pdf->get_page_size('A4') );
321  
322 my $page1 = $a4->new_page;
323 $page1->string($f1, 20, 306, 396, "some text on page 1");
324  
325 my $page2 = $a4->new_page;
326 $page2->string($f1, 20, 306, 396, "some text on page 2");
327  
328 =cut
329  
330 sub new_page {
331 37 ▶ my ($self, %params) = @_;
Subroutine new_page: called CC=11 SCAR=24.8
Pod documented
332  
333 37 my %valid_new_page_parameters = map { $_ => 1 } (qw/Parent Resources MediaBox CropBox ArtBox TrimBox BleedBox Rotate/);
334 37 foreach my $key (keys %params) {
335 croak "PDF::Create.pm - new_page(): Received invalid key [$key]"
336 37 ▶ unless (exists $valid_new_page_parameters{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $valid_new_page_parameters{$key} 1 36 37
337 }
338  
339 36 ▶ my $parent = $params{'Parent'} || $self->{'pages'};
Condition66%2/3
$params{'Parent'} || $self->{'pages'}
l!l&&r!l&&!r
20160
MC/DC0%0/2
$params{'Parent'} || $self->{'pages'}
$params{'Parent'} $self->{'pages'}
Truth table66%2/3
A || B
A= $params{'Parent'}B= $self->{'pages'}
ABresult
1X1
011
000
340 36 my $name = "Page " . ++$self->{'page_count'};
341 36 my $page = $parent->add( $self->reserve( $name, "Page" ), $name );
342 36 ▶ $page->{'resources'} = $params{'Resources'} if defined $params{'Resources'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"Resources"} 0 36 36
343 36 ▶ $page->{'mediabox'} = $params{'MediaBox'} if defined $params{'MediaBox'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"MediaBox"} 16 20 36
344 36 ▶ $page->{'cropbox'} = $params{'CropBox'} if defined $params{'CropBox'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"CropBox"} 0 36 36
345 36 ▶ $page->{'artbox'} = $params{'ArtBox'} if defined $params{'ArtBox'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"ArtBox"} 0 36 36
346 36 ▶ $page->{'trimbox'} = $params{'TrimBox'} if defined $params{'TrimBox'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"TrimBox"} 0 36 36
347 36 ▶ $page->{'bleedbox'} = $params{'BleedBox'} if defined $params{'BleedBox'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"BleedBox"} 0 36 36
348 36 ▶ $page->{'rotate'} = $params{'Rotate'} if defined $params{'Rotate'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"Rotate"} 0 36 36
349  
350 36 $self->{'current_page'} = $page;
351  
352 36 $page;
353 }
354  
355 =head2 font(%params)
356  
357 Prepare a font using the given arguments. This font will be added to the document
358 only if it is used at least once before the close method is called.Parameters are
359 listed below:
360  
361 +----------+----------------------------------------------------------------+
362 | Key | Description |
363 +----------+----------------------------------------------------------------+
364 | Subtype | Type of font. PDF defines some types of fonts. It must be one |
365 | | of the predefined type Type1, Type3, TrueType or Type0.In this |
366 | | version, only Type1 is supported. This is the default value. |
367 | | |
368 | Encoding | Specifies the encoding from which the new encoding differs. |
369 | | It must be one of the predefined encodings MacRomanEncoding, |
370 | | MacExpertEncoding, Symbol or WinAnsiEncoding. The default |
371 | | value is WinAnsiEncoding. |
372 | | |
373 | BaseFont | The PostScript name of the font. It can be one of the following|
374 | | base fonts: Courier, Courier-Bold, Courier-BoldOblique, |
375 | | Courier-Oblique, Helvetica, Helvetica-Bold, |
376 | | Helvetica-BoldOblique, Helvetica-Oblique, Times-Roman, |
377 | | Times-Bold, Times-Italic, Times-BoldItalic or Symbol. |
378 +----------+----------------------------------------------------------------+
379  
380 The ZapfDingbats font is not supported in this version. The default
381 font is Helvetica.
382  
383 my $f1 = $pdf->font('BaseFont' => 'Helvetica');
384  
385 =cut
386  
387 sub font {
388 37 ▶ my ($self, %params) = @_;
Subroutine font: called CC=8 SCAR=21.1
Pod documented
389  
390 my %valid_font_parameters = (
391 148 'Subtype' => { map { $_ => 1 } qw/Type0 Type1 Type3 TrueType/ },
392 148 'Encoding' => { map { $_ => 1 } qw/MacRomanEncoding MacExpertEncoding WinAnsiEncoding Symbol/ },
393 37 'BaseFont' => { map { $_ => 1 } qw/Courier Courier-Bold Courier-BoldOblique Courier-Oblique
394 Helvetica Helvetica-Bold Helvetica-BoldOblique Helvetica-Oblique
395 Times-Roman Times-Bold Times-Italic Times-BoldItalic Symbol/ },
396 );
397 37 foreach my $key (keys %params) {
398 croak "PDF::Create.pm - font(): Received invalid key [$key]"
399 106 ▶ unless (exists $valid_font_parameters{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $valid_font_parameters{$key} 1 105 106
400 105 my $value = $params{$key};
401 croak "PDF::Create.pm - font(): Received invalid value [$value] for key [$key]"
402 105 ▶ if (defined $value && !(exists $valid_font_parameters{$key}->{$value}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and not exists $valid_font_parameters{$key}{$value} 3 102 105
Condition66%2/3
defined $value and not exists $valid_font_parameters{$key}{$value}
!ll&&!rl&&r
01023
MC/DC50%1/2
defined $value and not exists $valid_font_parameters{$key}{$value}
defined $value not exists $valid_font_parameters{$key}{$value}
Truth table66%2/3
A and not(B)
A= defined $valueB= not exists $valid_font_parameters{$key}{$value}
ABresult
0X0
100
111
403 }
404  
405 33 my $num = 1 + scalar keys %{ $self->{'fonts'} };
406 $self->{'fonts'}{$num} = {
407 'Subtype' => $self->name( $params{'Subtype'} || 'Type1' ),
408 'Encoding' => $self->name( $params{'Encoding'} || 'WinAnsiEncoding' ),
409 33 ▶ 'BaseFont' => $self->name( $params{'BaseFont'} || 'Helvetica' ),
Condition50%3/6
$params{'Subtype'} || 'Type1'
l!l
330
$params{'Encoding'} || 'WinAnsiEncoding'
l!l
330
$params{'BaseFont'} || 'Helvetica'
l!l
330
MC/DC0%0/1
$params{'Subtype'} || 'Type1'
$params{'Subtype'}
MC/DC0%0/1
$params{'Encoding'} || 'WinAnsiEncoding'
$params{'Encoding'}
MC/DC0%0/1
$params{'BaseFont'} || 'Helvetica'
$params{'BaseFont'}
Truth table50%1/2
A
A= $params{'Subtype'}
Aresult
11
00
Truth table50%1/2
A
A= $params{'Encoding'}
Aresult
11
00
Truth table50%1/2
A
A= $params{'BaseFont'}
Aresult
11
00
410 'Name' => $self->name("F$num"),
411 'Type' => $self->name("Font"),
412 };
413  
414 33 $num;
415 }
416  
417 =head2 new_outline(%params)
418  
419 Adds an outline to the document using the given parameters. Return the newly
420 created outline. Parameters can be:
421  
422 +-------------+-------------------------------------------------------------+
423 | Key | Description |
424 +-------------+-------------------------------------------------------------+
425 | | |
426 | Title | The title of the outline. Mandatory. |
427 | | |
428 | Destination | The Destination of this outline item. In this version,it is |
429 | | only possible to give a page as destination. The default |
430 | | destination is the current page. |
431 | | |
432 | Parent | The parent of this outline in the outlines tree. This is an |
433 | | outline object. This way you represent the tree of your |
434 | | outlines. |
435 | | |
436 +-------------+-------------------------------------------------------------+
437  
438 Example:
439  
440 my $outline = $pdf->new_outline('Title' => 'Item 1');
441 $pdf->new_outline('Title' => 'Item 1.1', 'Parent' => $outline);
442 $pdf->new_outline('Title' => 'Item 1.2', 'Parent' => $outline);
443 $pdf->new_outline('Title' => 'Item 2');
444  
445 =cut
446  
447 sub new_outline {
448 20 ▶ my ($self, %params) = @_;
Subroutine new_outline: called CC=10 SCAR=23.3
Pod documented
449  
450 croak "PDF::Create - new_outline(): Missing required key [Title]."
451 20 ▶ unless (exists $params{'Title'});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $params{"Title"} 0 20 20
452 croak "PDF::Create - new_outline(): Required key [Title] undefined."
453 20 ▶ unless (defined $params{'Title'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"Title"} 0 20 20
454  
455 20 ▶ if (defined $params{Destination}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"Destination"}) 10 10 20
456 croak "PDF::Create - new_outline(): Invalid value for key [Destination]."
457 10 ▶ unless (ref($params{Destination}) eq 'PDF::Create::Page');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $params{"Destination"} eq "PDF::Create::Page" 0 10 10
458 }
459  
460 20 ▶ if (defined $params{Parent}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"Parent"}) 10 10 20
461 croak "PDF::Create - new_outline(): Invalid value for key [Parent]."
462 10 ▶ unless (ref($params{Parent}) eq 'PDF::Create::Outline');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $params{"Parent"} eq "PDF::Create::Outline" 0 10 10
463 }
464  
465 20 ▶ unless ( defined $self->{'outlines'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"outlines"}) 5 15 20
466 5 $self->{'outlines'} = PDF::Create::Outline->new();
467 # circular reference
468 5 $self->{'outlines'}->{'pdf'} = $self;
469 5 weaken $self->{'outlines'}->{'pdf'};
470 5 $self->{'outlines'}->{'Status'} = 'opened';
471 }
472  
473 20 ▶ my $parent = $params{'Parent'} || $self->{'outlines'};
Condition66%2/3
$params{'Parent'} || $self->{'outlines'}
l!l&&r!l&&!r
10100
MC/DC0%0/2
$params{'Parent'} || $self->{'outlines'}
$params{'Parent'} $self->{'outlines'}
Truth table66%2/3
A || B
A= $params{'Parent'}B= $self->{'outlines'}
ABresult
1X1
011
000
474 20 my $name = "Outline " . ++$self->{'outline_count'};
475 20 ▶ $params{'Destination'} = $self->{'current_page'} unless defined $params{'Destination'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"Destination"} 10 10 20
476 20 my $outline = $parent->add( $self->reserve( $name, "Outline" ), $name, %params );
477 20 $outline;
478 }
479  
480 =head2 get_page_size($name)
481  
482 Returns the size of standard paper used for MediaBox-parameter of C<new_page>.
483 C<get_page_size> has one optional parameter to specify the paper name. Possible
484 values are a0-a6, a4l,letter,broadsheet,ledger,tabloid,legal,executive and 36x36.
485 Default is a4.
486  
487 my $root = $pdf->new_page( 'MediaBox' => $pdf->get_page_size('A4') );
488  
489 =cut
490  
491 sub get_page_size {
492 27 ▶ my ($self, $name) = @_;
Subroutine get_page_size: called CC=3 SCAR=11.2
Pod documented
493  
494 27 my %pagesizes = (
495 'A0' => [ 0, 0, 2380, 3368 ],
496 'A1' => [ 0, 0, 1684, 2380 ],
497 'A2' => [ 0, 0, 1190, 1684 ],
498 'A3' => [ 0, 0, 842, 1190 ],
499 'A4' => [ 0, 0, 595, 842 ],
500 'A4L' => [ 0, 0, 842, 595 ],
501 'A5' => [ 0, 0, 421, 595 ],
502 'A6' => [ 0, 0, 297, 421 ],
503 'LETTER' => [ 0, 0, 612, 792 ],
504 'BROADSHEET' => [ 0, 0, 1296, 1584 ],
505 'LEDGER' => [ 0, 0, 1224, 792 ],
506 'TABLOID' => [ 0, 0, 792, 1224 ],
507 'LEGAL' => [ 0, 0, 612, 1008 ],
508 'EXECUTIVE' => [ 0, 0, 522, 756 ],
509 '36X36' => [ 0, 0, 2592, 2592 ],
510 );
511 27 ▶ if (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) { } 27 0 27
512 27 $name = uc($name);
513 # validate page size
514 27 ▶ croak "Invalid page size name '$name' received." unless (exists $pagesizes{$name});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $pagesizes{$name} 1 26 27
515 }
516 else {
517 0 $name = 'A4';
518 }
519  
520 26 return $pagesizes{$name};
521 }
522  
523 =head2 version($number)
524  
525 Set and return version number. Valid version numbers are 1.0, 1.1, 1.2 and 1.3.
526  
527 =cut
528  
529 sub version {
530 5 ▶ my ($self, $v) = @_;
Subroutine version: called CC=3 SCAR=11.0
Pod documented
531  
532 5 ▶ if (defined $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v) 5 0 5
533 5 ▶ croak "ERROR: Invalid version number $v received.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $v =~ /^1\.[0,1,2,3]$/ 2 3 5
534 unless ($v =~ /^1\.[0,1,2,3]$/);
535 3 $self->{'version'} = $v;
536 }
537 3 $self->{'version'};
538 }
539  
540 =head2 close(%params)
541  
542 Close does the work of creating the PDF data from the objects collected before.
543 You must call close() after you have added all the contents as most of the real
544 work building the PDF is performed there. If omit calling close you get no PDF
545 output.
546  
547 =cut
548  
549 sub close {
550 12 ▶ my ($self, %params) = @_;
Subroutine close: called CC=4 SCAR=14.0
Pod documented
551  
552 12 debug( 2, "Closing PDF" );
553 12 $self->page_stream;
554 12 ▶ $self->add_outlines if defined $self->{'outlines'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"outlines"} 3 9 12
555 12 $self->add_catalog;
556 12 $self->add_pages;
557 12 $self->add_info;
558 12 $self->add_crossrefsection;
559 12 $self->add_trailer;
560 12 ▶ $self->{'fh'}->close if defined $self->{'fh'} && defined $self->{'filename'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"fh"} and defined $self->{"filename"} 12 0 12
Condition33%1/3
defined $self->{"fh"} and defined $self->{"filename"}
!ll&&!rl&&r
0012
MC/DC0%0/2
defined $self->{"fh"} and defined $self->{"filename"}
defined $self->{"fh"} defined $self->{"filename"}
Truth table33%1/3
A and B
A= defined $self->{"fh"}B= defined $self->{"filename"}
ABresult
0X0
100
111
561 12 $self->{'data'};
562 }
563  
564 =head2 reserve($name, $type)
565  
566 Reserve the next object number for the given object type.
567  
568 =cut
569  
570 sub reserve {
571 169 ▶ my ($self, $name, $type) = @_;;
Subroutine reserve: called CC=4 SCAR=13.9
Pod documented
572  
573 169 ▶ $type = $name unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 113 56 169
574  
575 confess "Error: an object has already been reserved using this name '$name' "
576 169 ▶ if defined $self->{'reservations'}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"reservations"}{$name} 0 169 169
577 169 $self->{'object_number'}++;
578 169 debug( 2, "reserve(): name=$name type=$type number=$self->{'object_number'} generation=$self->{'generation_number'}" );
579 169 $self->{'reservations'}{$name} = [ $self->{'object_number'}, $self->{'generation_number'}, $type ];
580  
581  
582 # Annotations added here by Gary Lieberman.
583 #
584 # Store the Object ID and the Generation Number for later use when we write
585 # out the /Page object.
586 169 ▶ if ($type eq 'Annotation') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "Annotation") 2 167 169
587 2 $self->{'Annots'}{ $self->{'object_number'} } = $self->{'generation_number'};
588 }
589  
590 169 [ $self->{'object_number'}, $self->{'generation_number'} ];
591 }
592  
593 =head2 add_comment($message)
594  
595 Add comment to the document.The string will show up in the PDF as postscript-stype
596 comment:
597  
598 % this is a postscript comment
599  
600 =cut
601  
602 sub add_comment {
603 7 ▶ my ($self, $comment) = @_;
Subroutine add_comment: called CC=2 SCAR=7.0
Pod documented
604  
605 7 ▶ $comment = '' unless defined $comment;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $comment 0 7 7
606 7 debug( 2, "add_comment(): $comment" );
607 7 $self->add( "%" . $comment );
608 7 $self->cr;
609 }
610  
611 =head2 annotation(%params)
612  
613 Adds an annotation object, for the time being we only do the 'Link' - 'URI' kind
614 This is a sensitive area in the PDF document where text annotations are shown or
615 links launched. C<PDF::Create> only supports URI links at this time.
616  
617 URI links have two components,the text or graphics object and the area where the
618 mouseclick should occur.
619  
620 For the object to be clicked on you'll use standard text of drawing methods. To
621 define the click-sensitive area and the destination URI.
622  
623 Example:
624  
625 # Draw a string and undeline it to show it is a link
626 $pdf->string($f1, 10, 450, 200, 'http://www.cpan.org');
627  
628 my $line = $pdf->string_underline($f1, 10, 450, 200, 'http://www.cpan.org');
629  
630 # Create the hot area with the link to open on click
631 $pdf->annotation(
632 Subtype => 'Link',
633 URI => 'http://www.cpan.org',
634 x => 450,
635 y => 200,
636 w => $l,
637 h => 15,
638 Border => [0,0,0]
639 );
640  
641 The point (x, y) is the bottom left corner of the rectangle containing hotspot
642 rectangle, (w, h) are the width and height of the hotspot rectangle. The Border
643 describes the thickness of the border surrounding the rectangle hotspot.
644  
645 The function C<string_undeline> returns the width of the string, this can be used
646 directly for the width of the hotspot rectangle.
647  
648 =cut
649  
650 sub annotation {
651 2 ▶ my ($self, %params) = @_;
Subroutine annotation: called CC=8 SCAR=21.8
Pod documented
652  
653 2 debug( 2, "annotation(): Subtype=$params{'Subtype'}" );
654  
655 2 ▶ if ( $params{'Subtype'} eq 'Link' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'Subtype'} eq 'Link') { } 2 0 2
656 2 ▶ confess "Must specify 'URI' for Link" unless defined $params{'URI'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"URI"} 0 2 2
657 2 ▶ confess "Must specify 'x' for Link" unless defined $params{'x'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"x"} 0 2 2
658 2 ▶ confess "Must specify 'y' for Link" unless defined $params{'y'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"y"} 0 2 2
659 2 ▶ confess "Must specify 'w' for Link" unless defined $params{'w'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"w"} 0 2 2
660 2 ▶ confess "Must specify 'h' for Link" unless defined $params{'h'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"h"} 0 2 2
661  
662 2 my $num = 1 + scalar keys %{ $self->{'annotations'} };
663  
664 my $action = {
665 'Type' => $self->name('Action'),
666 'S' => $self->name('URI'),
667 2 'URI' => $self->string( $params{'URI'} ),
668 };
669 2 my $x2 = $params{'x'} + $params{'w'};
670 2 my $y2 = $params{'y'} + $params{'h'};
671  
672 $self->{'annotations'}{$num} = {
673 'Subtype' => $self->name('Link'),
674 2 'Rect' => $self->verbatim( sprintf "[%f %f %f %f]", $params{'x'}, $params{'y'}, $x2, $y2 ),
675 'A' => $self->dictionary(%$action),
676 };
677  
678 2 ▶ if ( defined $params{'Border'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"Border"}) 2 0 2
679 $self->{'annotations'}{$num}{'Border'} =
680 2 $self->verbatim( sprintf "[%f %f %f]", $params{'Border'}[0], $params{'Border'}[1], $params{'Border'}[2] );
681 }
682 2 $self->{'annot'}{$num}{'page_name'} = "Page " . $self->{'page_count'};
683 2 debug( 2, "annotation(): annotation number: $num, page name: $self->{'annot'}{$num}{'page_name'}" );
684 2 1;
685 } else {
686 0 confess "Only Annotations with Subtype 'Link' are supported for now\n";
687 }
688 }
689  
690 =head2 image($filename)
691  
692 Prepare an XObject (image) using the given arguments. This image will be added to
693 the document if it is referenced at least once before the close method is called.
694 In this version GIF,interlaced GIF and JPEG is supported.Usage of interlaced GIFs
695 are slower because they are decompressed, modified and compressed again. The gif
696 support is limited to images with a LZW minimum code size of 8. Small images with
697 few colors can have a smaller minimum code size and will not work.
698  
699 =cut
700  
701 sub image {
702 2 ▶ my ($self, $filename) = @_;
Subroutine image: called CC=10 SCAR=23.3
Pod documented
703  
704 2 my $num = 1 + scalar keys %{ $self->{'xobjects'} };
705  
706 2 my $image;
707 my $colorspace;
708 2 my @a;
709  
710 2 ▶ if ( $filename =~ /\.gif$/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($filename =~ /\.gif$/i) { } 1 1 2
elsif ($filename =~ /\.jpg$/i or $filename =~ /\.jpeg$/i) { } 1 0 1
711 1 $self->{'images'}{$num} = PDF::Image::GIF->new();
712 ▶ } elsif ( $filename =~ /\.jpg$/i || $filename =~ /\.jpeg$/i ) {
Condition33%1/3
$filename =~ /\.jpg$/i or $filename =~ /\.jpeg$/i
l!l&&r!l&&!r
100
MC/DC0%0/2
$filename =~ /\.jpg$/i or $filename =~ /\.jpeg$/i
$filename =~ /\.jpg$/i $filename =~ /\.jpeg$/i
Truth table33%1/3
A or B
A= $filename =~ /\.jpg$/iB= $filename =~ /\.jpeg$/i
ABresult
1X1
011
000
713 1 $self->{'images'}{$num} = PDF::Image::JPEG->new();
714 }
715  
716 2 $image = $self->{'images'}{$num};
717 2 ▶ if ( !$image->Open($filename) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($image->Open($filename)) 0 2 2
718 0 print $image->{error} . "\n";
719 0 return 0;
720 }
721  
722 $self->{'xobjects'}{$num} = {
723 'Subtype' => $self->name('Image'),
724 'Name' => $self->name("Image$num"),
725 'Type' => $self->name('XObject'),
726 'Width' => $self->number( $image->{width} ),
727 'Height' => $self->number( $image->{height} ),
728 'BitsPerComponent' => $self->number( $image->{bpc} ),
729 'Data' => $image->ReadData(),
730 2 'Length' => $self->number( $image->{imagesize} ),
731 };
732  
733 # Indexed colorspace?
734 2 ▶ if ($image->{colorspacesize}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($image->{'colorspacesize'}) { } 1 1 2
735 1 $colorspace = $self->reserve("ImageColorSpace$num");
736  
737 $self->{'xobjects_colorspace'}{$num} = {
738 'Data' => $image->{colorspacedata},
739 1 'Length' => $self->number( $image->{colorspacesize} ),
740 };
741  
742 1 $self->{'xobjects'}{$num}->{'ColorSpace'} = $self->array( $self->name('Indexed'), $self->name( $image->{colorspace} ),
743 $self->number(255), $self->indirect_ref(@$colorspace) );
744 } else {
745 1 $self->{'xobjects'}{$num}->{'ColorSpace'} = $self->array( $self->name( $image->{colorspace} ) );
746 }
747  
748 # Set Filter
749 2 $#a = -1;
750 2 foreach my $s ( @{ $image->{filter} } ) {
751 2 push @a, $self->name($s);
752 }
753 2 ▶ if ( $#a >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#a >= 0) 2 0 2
754 2 $self->{'xobjects'}{$num}->{'Filter'} = $self->array(@a);
755 }
756  
757 # Set additional DecodeParms
758 2 $#a = -1;
759 2 foreach my $s ( keys %{ $image->{decodeparms} } ) {
760 1 push @a, $s;
761 1 push @a, $self->number( $image->{decodeparms}{$s} );
762 }
763 2 $self->{'xobjects'}{$num}->{'DecodeParms'} = $self->array( $self->dictionary(@a) );
764  
765 # Transparent?
766 2 ▶ if ( $image->{transparent} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($image->{"transparent"}) 1 1 2
767 1 $self->{'xobjects'}{$num}->{'Mask'} = $self->array( $self->number( $image->{mask} ), $self->number( $image->{mask} ) );
768 }
769  
770 2 return { 'num' => $num, 'width' => $image->{width}, 'height' => $image->{height} };
771 }
772  
773 sub add_outlines {
774 3 ▶ my ($self, %params) = @_;
Subroutine add_outlines: called CC=12 SCAR=24.9
Pod undocumented
775  
776 3 debug( 2, "add_outlines" );
777 3 my $outlines = $self->reserve("Outlines");
778  
779 3 my ($First, $Last);
780 3 my @list = $self->{'outlines'}->list;
781 3 my $i = -1;
782 3 for my $outline (@list) {
783 18 $i++;
784 18 my $name = $outline->{'name'};
785 18 ▶ $First = $outline->{'id'} unless defined $First;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $First 3 15 18
786 18 $Last = $outline->{'id'};
787 18 my $content = { 'Title' => $self->string( $outline->{'Title'} ) };
788 18 ▶ if ( defined $outline->{'Kids'} && scalar @{ $outline->{'Kids'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $outline->{"Kids"} and scalar @{$outline->{"Kids"};}) 5 13 18
Condition50%1/2
defined $outline->{"Kids"} and scalar @{$outline->{"Kids"};}
!ll
018
MC/DC0%0/1
defined $outline->{"Kids"} and scalar @{$outline->{"Kids"};}
defined $outline->{"Kids"}
Truth table50%1/2
A
A= defined $outline->{"Kids"}
Aresult
00
11
789 5 my $t = $outline->{'Kids'};
790 5 $$content{'First'} = $self->indirect_ref( @{ $$t[0]->{'id'} } );
791 5 $$content{'Last'} = $self->indirect_ref( @{ $$t[$#$t]->{'id'} } );
792 }
793 18 my $brothers = $outline->{'Parent'}->{'Kids'};
794 18 my $j = -1;
795 18 for my $brother (@$brothers) {
796 33 $j++;
797 33 ▶ last if $brother == $outline;
Branch: 1 branch
BranchTrueFalseTotal
if $brother == $outline 18 15 33
798 }
799 18 ▶ $$content{'Next'} = $self->indirect_ref( @{ $$brothers[ $j + 1 ]->{'id'} } )
Branch: 1 branch
BranchTrueFalseTotal
if $j < $brothers->$#* 10 8 18
800 if $j < $#$brothers;
801 18 ▶ $$content{'Prev'} = $self->indirect_ref( @{ $$brothers[ $j - 1 ]->{'id'} } )
Branch: 1 branch
BranchTrueFalseTotal
if $j 10 8 18
802 if $j;
803 $outline->{'Parent'}->{'id'} = $outlines
804 18 ▶ unless defined $outline->{'Parent'}->{'id'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $outline->{"Parent"}{"id"} 3 15 18
805 18 $$content{'Parent'} = $self->indirect_ref( @{ $outline->{'Parent'}->{'id'} } );
806 $$content{'Dest'} =
807 18 $self->array( $self->indirect_ref( @{ $outline->{'Dest'}->{'id'} } ),
808 $self->name('Fit'), $self->null, $self->null, $self->null );
809 18 my $count = $outline->count;
810 18 ▶ $$content{'Count'} = $self->number($count) if $count;
Branch: 1 branch
BranchTrueFalseTotal
if $count 5 13 18
811 18 my $t = $self->add_object( $self->indirect_obj( $self->dictionary(%$content), $name ) );
812 18 $self->cr;
813 }
814  
815 # Type (required)
816 3 my $content = { 'Type' => $self->name('Outlines') };
817  
818 # Count
819 3 my $count = $self->{'outlines'}->count;
820 3 ▶ $$content{'Count'} = $self->number($count) if $count;
Branch: 1 branch
BranchTrueFalseTotal
if $count 3 0 3
821 3 $$content{'First'} = $self->indirect_ref(@$First);
822 3 $$content{'Last'} = $self->indirect_ref(@$Last);
823 3 $self->add_object( $self->indirect_obj( $self->dictionary(%$content) ) );
824 3 $self->cr;
825 }
826  
827 sub add_pages {
828 12 ▶ my ($self) = @_;
Subroutine add_pages: called CC=29 SCAR=34.0
Pod undocumented
829  
830 12 debug( 2, "add_pages():" );
831  
832 # Type (required)
833 12 my $content = { 'Type' => $self->name('Pages') };
834  
835 # Kids (required)
836 12 my $t = $self->{'pages'}->kids;
837 12 ▶ confess "Error: document MUST contains at least one page. Abort."
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$t 0 12 12
838 unless scalar @$t;
839  
840 12 my $kids = [];
841 12 map { push @$kids, $self->indirect_ref(@$_) } @$t;
842 12 $$content{'Kids'} = $self->array(@$kids);
843 12 $$content{'Count'} = $self->number( $self->{'pages'}->count );
844 12 $self->add_object( $self->indirect_obj( $self->dictionary(%$content) ) );
845 12 $self->cr;
846  
847 12 for my $font ( sort keys %{ $self->{'fonts'} } ) {
848 29 debug( 2, "add_pages(): font: $font" );
849 29 $self->{'fontobj'}{$font} = $self->reserve('Font');
850 29 $self->add_object( $self->indirect_obj( $self->dictionary( %{ $self->{'fonts'}{$font} } ), 'Font' ) );
851 29 $self->cr;
852 }
853  
854 12 for my $xobject (sort keys %{$self->{'xobjects'}}) {
855 2 debug( 2, "add_pages(): xobject: $xobject" );
856 2 $self->{'xobj'}{$xobject} = $self->reserve('XObject');
857 2 $self->add_object( $self->indirect_obj( $self->stream( %{ $self->{'xobjects'}{$xobject} } ), 'XObject' ) );
858 2 $self->cr;
859  
860 2 ▶ if ( defined $self->{'reservations'}{"ImageColorSpace$xobject"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"reservations"}{"ImageColorSpace$xobject"}) 1 1 2
861 $self->add_object(
862 1 $self->indirect_obj( $self->stream( %{ $self->{'xobjects_colorspace'}{$xobject} } ), "ImageColorSpace$xobject" ) );
863 1 $self->cr;
864 }
865 }
866  
867 12 for my $annotation (sort keys %{$self->{'annotations'}}) {
868 2 $self->{'annot'}{$annotation}{'object_info'} = $self->reserve('Annotation');
869 2 $self->add_object( $self->indirect_obj( $self->dictionary( %{ $self->{'annotations'}{$annotation} } ), 'Annotation' ) );
870 2 $self->cr;
871 }
872  
873 12 for my $page ($self->{'pages'}->list) {
874 30 my $name = $page->{'name'};
875 30 debug( 2, "add_pages: page: $name" );
876 30 ▶ my $type = 'Page' . ( defined $page->{'Kids'} && scalar @{ $page->{'Kids'} } ? 's' : '' );
Branch: 1 branch
BranchTrueFalseTotal
defined $page->{'Kids'} && scalar @{$page->{'Kids'};} ? : 11 19 30
Condition50%1/2
defined $page->{"Kids"} and scalar @{$page->{"Kids"};}
!ll
030
MC/DC0%0/1
defined $page->{"Kids"} and scalar @{$page->{"Kids"};}
defined $page->{"Kids"}
Truth table50%1/2
A
A= defined $page->{"Kids"}
Aresult
00
11
877  
878 # Type (required)
879 30 my $content = { 'Type' => $self->name($type) };
880  
881 # Resources (required, may be inherited). See page 195.
882 30 my $resources = {};
883 30 for my $k ( keys %{ $page->{'resources'} } ) {
884 33 my $v = $page->{'resources'}{$k};
885 ( $k eq 'ProcSet' ) && do {
886 16 my $l = [];
887 16 ▶ if ( ref($v) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq 'ARRAY') { } 16 0 16
888 16 map { push @$l, $self->name($_) } @$v;
889 } else {
890 0 push @$l, $self->name($v);
891 }
892 16 ▶ $$resources{'ProcSet'} = $self->array(@$l);
Condition66%2/3
$k eq "fonts" and do { my $l = {}; map {$$l{"F$_"} = $self->indirect_ref(@{$$self{"fontobj"}{$_};});} keys %{$$page{"resources"}{"fonts"};}; $resources->{"Font"} = $self->dictionary(%$l) }
!ll&&!rl&&r
1016
MC/DC50%1/2
$k eq "fonts" and do { my $l = {}; map {$$l{"F$_"} = $self->indirect_ref(@{$$self{"fontobj"}{$_};});} keys %{$$page{"resources"}{"fonts"};}; $resources->{"Font"} = $self->dictionary(%$l) }
$k eq "fonts" do { my $l = {}; map {$$l{"F$_"} = $self->indirect_ref(@{$$self{"fontobj"}{$_};});} keys %{$$page{"resources"}{"fonts"};}; $resources->{"Font"} = $self->dictionary(%$l) }
Truth table66%2/3
A and B
A= $k eq "fonts"B= do { my $l = {}; map {$$l{"F$_"} = $self->indirect_ref(@{$$self{"fontobj"}{$_};});} keys %{$$page{"resources"}{"fonts"};}; $resources->{"Font"} = $self->dictionary(%$l) }
ABresult
0X0
100
111
893 }
894 || ( $k eq 'fonts' ) && do {
895 16 my $l = {};
896 16 map { $$l{"F$_"} = $self->indirect_ref( @{ $self->{'fontobj'}{$_} } ); } keys %{ $page->{'resources'}{'fonts'} };
897 16 ▶ $$resources{'Font'} = $self->dictionary(%$l);
Condition33%1/3
$k eq "xobjects" and do { my $l = {}; map {$$l{"Image$_"} = $self->indirect_ref(@{$$self{"xobj"}{$_};});} keys %{$$page{"resources"}{"xobjects"};}; $resources->{"XObject"} = $self->dictionary(%$l) }
!ll&&!rl&&r
010
MC/DC0%0/2
$k eq "xobjects" and do { my $l = {}; map {$$l{"Image$_"} = $self->indirect_ref(@{$$self{"xobj"}{$_};});} keys %{$$page{"resources"}{"xobjects"};}; $resources->{"XObject"} = $self->dictionary(%$l) }
$k eq "xobjects" do { my $l = {}; map {$$l{"Image$_"} = $self->indirect_ref(@{$$self{"xobj"}{$_};});} keys %{$$page{"resources"}{"xobjects"};}; $resources->{"XObject"} = $self->dictionary(%$l) }
Truth table33%1/3
A and B
A= $k eq "xobjects"B= do { my $l = {}; map {$$l{"Image$_"} = $self->indirect_ref(@{$$self{"xobj"}{$_};});} keys %{$$page{"resources"}{"xobjects"};}; $resources->{"XObject"} = $self->dictionary(%$l) }
ABresult
0X0
100
111
898 }
899 33 ▶ || ( $k eq 'xobjects' ) && do {
Branch: 1 branch
BranchTrueFalseTotal
$k eq "ProcSet" and do { my $l = []; if (ref $v eq "ARRAY") { map {push @$l, $self->name($_);} @$v; } else { push @$l, $self->name($v); } ; $resources->{"ProcSet"} = $self->array(@$l) } or $k eq "fonts" and do { my $l = {}; map {$$l{"F$_"} = $self->indirect_ref(@{$$self{"fontobj"}{$_};});} keys %{$$page{"resources"}{"fonts"};}; $resources->{"Font"} = $self->dictionary(%$l) } or $k eq "xobjects" and do { my $l = {}; map {$$l{"Image$_"} = $self->indirect_ref(@{$$self{"xobj"}{$_};});} keys %{$$page{"resources"}{"xobjects"};}; $resources->{"XObject"} = $self->dictionary(%$l) } 1 32 33
Condition66%6/9
$k eq 'ProcSet' && do { my $l = []; if (ref $v eq 'ARRAY') { map {push @$l, $self->name($_);} @$v; } else { push @$l, $self->name($v); } ; $resources->{'ProcSet'} = $self->array(@$l) } || $k eq 'fonts' && do { my $l = {}; map {$$l{"F$_"} = $self->indirect_ref(@{$$self{'fontobj'}{$_};});} keys %{$$page{'resources'}{'fonts'};}; $resources->{'Font'} = $self->dictionary(%$l) } || $k eq 'xobjects' && do { my $l = {}; map {$$l{"Image$_"} = $self->indirect_ref(@{$$self{'xobj'}{$_};});} keys %{$$page{'resources'}{'xobjects'};}; $resources->{'XObject'} = $self->dictionary(%$l) }
l!l&&r!l&&!r
3210
$k eq "ProcSet" and do { my $l = []; if (ref $v eq "ARRAY") { map {push @$l, $self->name($_);} @$v; } else { push @$l, $self->name($v); } ; $resources->{"ProcSet"} = $self->array(@$l) } or $k eq "fonts" and do { my $l = {}; map {$$l{"F$_"} = $self->indirect_ref(@{$$self{"fontobj"}{$_};});} keys %{$$page{"resources"}{"fonts"};}; $resources->{"Font"} = $self->dictionary(%$l) }
l!l&&r!l&&!r
16170
$k eq "ProcSet" and do { my $l = []; if (ref $v eq "ARRAY") { map {push @$l, $self->name($_);} @$v; } else { push @$l, $self->name($v); } ; $resources->{"ProcSet"} = $self->array(@$l) }
!ll&&!rl&&r
17016
MC/DC0%0/4
$k eq 'ProcSet' && do { my $l = []; if (ref $v eq 'ARRAY') { map {push @$l, $self->name($_);} @$v; } else { push @$l, $self->name($v); } ; $resources->{'ProcSet'} = $self->array(@$l) } || $k eq 'fonts' && do { my $l = {}; map {$$l{"F$_"} = $self->indirect_ref(@{$$self{'fontobj'}{$_};});} keys %{$$page{'resources'}{'fonts'};}; $resources->{'Font'} = $self->dictionary(%$l) } || $k eq 'xobjects' && do { my $l = {}; map {$$l{"Image$_"} = $self->indirect_ref(@{$$self{'xobj'}{$_};});} keys %{$$page{'resources'}{'xobjects'};}; $resources->{'XObject'} = $self->dictionary(%$l) }
$k eq "ProcSet" do { my $l = []; if (ref $v eq "ARRAY") { map {push @$l, $self->name($_);} @$v; } else { push @$l, $self->name($v); } ; $resources->{"ProcSet"} = $self->array(@$l) } $k eq "fonts" and do { my $l = {}; map {$$l{"F$_"} = $self->indirect_ref(@{$$self{"fontobj"}{$_};});} keys %{$$page{"resources"}{"fonts"};}; $resources->{"Font"} = $self->dictionary(%$l) } $k eq 'xobjects' && do { my $l = {}; map {$$l{"Image$_"} = $self->indirect_ref(@{$$self{'xobj'}{$_};});} keys %{$$page{'resources'}{'xobjects'};}; $resources->{'XObject'} = $self->dictionary(%$l) }
Truth table14%1/7
A and B or C || D
A= $k eq "ProcSet"B= do { my $l = []; if (ref $v eq "ARRAY") { map {push @$l, $self->name($_);} @$v; } else { push @$l, $self->name($v); } ; $resources->{"ProcSet"} = $self->array(@$l) }C= $k eq "fonts" and do { my $l = {}; map {$$l{"F$_"} = $self->indirect_ref(@{$$self{"fontobj"}{$_};});} keys %{$$page{"resources"}{"fonts"};}; $resources->{"Font"} = $self->dictionary(%$l) }D= $k eq 'xobjects' && do { my $l = {}; map {$$l{"Image$_"} = $self->indirect_ref(@{$$self{'xobj'}{$_};});} keys %{$$page{'resources'}{'xobjects'};}; $resources->{'XObject'} = $self->dictionary(%$l) }
ABCDresult
11XX1
0X1X1
101X1
0X011
10011
0X000
10000
900 1 my $l = {};
901 2 map { $$l{"Image$_"} = $self->indirect_ref( @{ $self->{'xobj'}{$_} } ); }
902 1 keys %{ $page->{'resources'}{'xobjects'} };
903 1 $$resources{'XObject'} = $self->dictionary(%$l);
904 };
905 }
906 30 ▶ if ( defined( $$resources{'Annotation'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $resources->{"Annotation"}) 0 30 30
907 0 my $r = $self->add_object( $self->indirect_obj( $self->dictionary(%$resources) ) );
908 0 $self->cr;
909 0 $$content{'Resources'} = [ 'ref', [ $$r[0], $$r[1] ] ];
910 }
911 30 ▶ if ( defined( $$resources{'XObject'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $resources->{'XObject'}) { } 1 29 30
912 1 my $r = $self->add_object( $self->indirect_obj( $self->dictionary(%$resources) ) );
913 1 $self->cr;
914 1 $$content{'Resources'} = [ 'ref', [ $$r[0], $$r[1] ] ];
915 } else {
916 29 ▶ $$content{'Resources'} = $self->dictionary(%$resources)
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %$resources 15 14 29
917 if scalar keys %$resources;
918 }
919 30 for my $K ( 'MediaBox', 'CropBox', 'ArtBox', 'TrimBox', 'BleedBox' ) {
920 150 my $k = lc $K;
921 150 ▶ if ( defined $page->{$k} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $page->{$k}) 12 138 150
922 12 my $l = [];
923 12 map { push @$l, $self->number($_) } @{ $page->{$k} };
924 12 $$content{$K} = $self->array(@$l);
925 }
926 }
927 30 ▶ $$content{'Rotate'} = $self->number( $page->{'rotate'} ) if defined $page->{'rotate'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $page->{"rotate"} 0 30 30
928 30 ▶ if ( $type eq 'Page' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'Page') { } 19 11 30
929 19 $$content{'Parent'} = $self->indirect_ref( @{ $page->{'Parent'}{'id'} } );
930  
931 # Content
932 19 ▶ if ( defined $page->{'contents'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $page->{"contents"}) 18 1 19
933 18 my $contents = [];
934 18 map { push @$contents, $self->indirect_ref(@$_); } @{ $page->{'contents'} };
935 18 $$content{'Contents'} = $self->array(@$contents);
936 }
937  
938 # Annotations added here by Gary Lieberman
939 #
940 # Tell the /Page object that annotations need to be drawn.
941 19 ▶ if ( defined $self->{'annot'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"annot"}) 1 18 19
942 1 my $Annots = '[ ';
943 1 my $is_annots = 0;
944 1 foreach my $annot_number ( keys %{ $self->{'annot'} } ) {
945 2 ▶ next if ( $self->{'annot'}{$annot_number}{'page_name'} ne $name );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"annot"}{$annot_number}{"page_name"} ne $name 0 2 2
946 2 $is_annots = 1;
947 2 debug( 2,
948 sprintf "annotation number: $annot_number, page name: $self->{'annot'}{$annot_number}{'page_name'}" );
949 2 my $object_number = $self->{'annot'}{$annot_number}{'object_info'}[0];
950 2 my $generation_number = $self->{'annot'}{$annot_number}{'object_info'}[1];
951 2 debug( 2, sprintf "object_number: $object_number, generation_number: $generation_number" );
952 2 $Annots .= sprintf( "%s %s R ", $object_number, $generation_number );
953 }
954 1 ▶ $$content{'Annots'} = $self->verbatim( $Annots . ']' ) if ($is_annots);
Branch: 1 branch
BranchTrueFalseTotal
if $is_annots 1 0 1
955 }
956 } else {
957 11 my $kids = [];
958 11 map { push @$kids, $self->indirect_ref(@$_) } @{ $page->kids };
959 11 $$content{'Kids'} = $self->array(@$kids);
960 11 $$content{'Parent'} = $self->indirect_ref( @{ $page->{'Parent'}{'id'} } )
961 11 ▶ if defined $page->{'Parent'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $page->{"Parent"} 11 0 11
962 11 $$content{'Count'} = $self->number( $page->count );
963 }
964 30 $self->add_object( $self->indirect_obj( $self->dictionary(%$content), $name ) );
965 30 $self->cr;
966 }
967 }
968  
969 sub add_crossrefsection {
970 12 ▶ my ($self) = @_;
Subroutine add_crossrefsection: called CC=3 SCAR=11.0
Pod undocumented
971  
972 12 debug( 2, "add_crossrefsection():" );
973  
974 # <cross-reference section> ::=
975 # xref
976 # <cross-reference subsection>+
977 12 $self->{'crossrefstartpoint'} = $self->position;
978 12 $self->add('xref');
979 12 $self->cr;
980 confess "Fatal error: should contains at least one cross reference subsection."
981 12 ▶ unless defined $self->{'crossrefsubsection'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"crossrefsubsection"} 0 12 12
982 12 for my $subsection ( sort keys %{ $self->{'crossrefsubsection'} } ) {
983 12 $self->add_crossrefsubsection($subsection);
984 }
985 }
986  
987 sub add_crossrefsubsection {
988 12 ▶ my ($self, $subsection) = @_;
Subroutine add_crossrefsubsection: called CC=3 SCAR=11.0
Pod undocumented
989  
990 12 debug( 2, "add_crossrefsubsection():" );
991  
992 # <cross-reference subsection> ::=
993 # <object number of first entry in subsection>
994 # <number of entries in subsection>
995 # <cross-reference entry>+
996 #
997 # <cross-reference entry> ::= <in-use entry> | <free entry>
998 #
999 # <in-use entry> ::= <byte offset> <generation number> n <end-of-line>
1000 #
1001 # <end-of-line> ::= <space> <carriage return>
1002 # | <space> <linefeed>
1003 # | <carriage return> <linefeed>
1004 #
1005 # <free entry> ::=
1006 # <object number of next free object>
1007 # <generation number> f <end-of-line>
1008  
1009 12 $self->add( 0, ' ', 1 + scalar @{ $self->{'crossrefsubsection'}{$subsection} } );
1010 12 $self->cr;
1011 12 $self->add( sprintf "%010d %05d %s ", 0, 65535, 'f' );
1012 12 $self->cr;
1013 12 for my $entry ( sort { $$a[0] <=> $$b[0] } @{ $self->{'crossrefsubsection'}{$subsection} } ) {
1014 162 ▶ $self->add( sprintf "%010d %05d %s ", $$entry[1], $subsection, $$entry[2] ? 'n' : 'f' );
Branch: 1 branch
BranchTrueFalseTotal
$entry->[2] ? : 162 0 162
1015  
1016 # printf "%010d %010x %05d n\n", $$entry[1], $$entry[1], $subsection;
1017 162 $self->cr;
1018 }
1019 }
1020  
1021 sub add_trailer {
1022 12 ▶ my $self = shift;
Subroutine add_trailer: called CC=5 SCAR=16.1
Pod undocumented
1023  
1024 12 debug( 2, "add_trailer():" );
1025  
1026 # <trailer> ::= trailer
1027 # <<
1028 # <trailer key value pair>+
1029 # >>
1030 # startxref
1031 # <cross-reference table start address>
1032 # %%EOF
1033  
1034 12 my @keys = (
1035 'Size', # integer (required)
1036 'Prev', # integer (req only if more than one cross-ref section)
1037 'Root', # dictionary (required)
1038 'Info', # dictionary (optional)
1039 'ID', # array (optional) (PDF 1.1)
1040 'Encrypt' # dictionary (req if encrypted) (PDF 1.1)
1041 );
1042  
1043 # TODO: should check for required fields
1044 12 $self->add('trailer');
1045 12 $self->cr;
1046 12 $self->add('<<');
1047 12 $self->cr;
1048 12 $self->{'trailer'}{'Size'} = 1;
1049 12 map { $self->{'trailer'}{'Size'} += scalar @{ $self->{'crossrefsubsection'}{$_} } } keys %{ $self->{'crossrefsubsection'} };
1050 12 $self->{'trailer'}{'Root'} = &encode( @{ $self->indirect_ref( @{ $self->{'catalog'} } ) } );
1051 12 $self->{'trailer'}{'Info'} = &encode( @{ $self->indirect_ref( @{ $self->{'info'} } ) } )
1052 12 ▶ if defined $self->{'info'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"info"} 12 0 12
1053  
1054 12 for my $k (@keys) {
1055 72 ▶ next unless defined $self->{'trailer'}{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"trailer"}{$k} 36 36 72
1056 $self->add( "/$k ",
1057 36 ▶ ref $self->{'trailer'}{$k} eq 'ARRAY' ? join( ' ', @{ $self->{'trailer'}{$k} } ) : $self->{'trailer'}{$k} );
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{'trailer'}{$k} eq 'ARRAY' ? : 0 36 36
1058 36 $self->cr;
1059 }
1060 12 $self->add('>>');
1061 12 $self->cr;
1062 12 $self->add('startxref');
1063 12 $self->cr;
1064 12 $self->add( $self->{'crossrefstartpoint'} );
1065 12 $self->cr;
1066 12 $self->add('%%EOF');
1067 12 $self->cr;
1068 }
1069  
1070 sub cr {
1071 1320 ▶ my ($self) = @_;
Subroutine cr: called CC=1 SCAR=0.0
Pod undocumented
1072  
1073 1320 debug( 3, "cr():" );
1074 1320 $self->add( &encode('cr') );
1075 }
1076  
1077 sub page_stream {
1078 552 ▶ my ($self, $page) = @_;
Subroutine page_stream: called CC=7 SCAR=19.5
Pod undocumented
1079  
1080 552 debug( 2, "page_stream():" );
1081  
1082 552 ▶ if (defined $self->{'reservations'}{'stream_length'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"reservations"}{"stream_length"}) 540 12 552
1083 ## If it is the same page, use the same stream.
1084 $self->cr, return
1085 if defined $page
1086 && defined $self->{'stream_page'}
1087 && $page == $self->{'current_page'}
1088 540 ▶ && $self->{'stream_page'} == $page;
Branch: 1 branch
BranchTrueFalseTotal
if defined $page and defined $self->{"stream_page"} and $page == $self->{"current_page"} and $self->{"stream_page"} == $page 520 20 540
Condition88%8/9
defined $page and defined $self->{"stream_page"} and $page == $self->{"current_page"} and $self->{"stream_page"} == $page
!ll&&!rl&&r
137520
defined $page and defined $self->{"stream_page"} and $page == $self->{"current_page"}
!ll&&!rl&&r
112527
defined $page and defined $self->{"stream_page"}
!ll&&!rl&&r
110529
MC/DC75%3/4
defined $page and defined $self->{"stream_page"} and $page == $self->{"current_page"} and $self->{"stream_page"} == $page
defined $page defined $self->{"stream_page"} $page == $self->{"current_page"} $self->{"stream_page"} == $page
Truth table80%4/5
A and B and C and D
A= defined $pageB= defined $self->{"stream_page"}C= $page == $self->{"current_page"}D= $self->{"stream_page"} == $page
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1089  
1090 # Remember the position
1091 20 my $len = $self->position - $self->{'stream_pos'} + 1;
1092  
1093 # Close the stream and the object
1094 20 $self->cr;
1095 20 $self->add('endstream');
1096 20 $self->cr;
1097 20 $self->add('endobj');
1098 20 $self->cr;
1099 20 $self->cr;
1100  
1101 # Add the length
1102 20 $self->add_object( $self->indirect_obj( $self->number($len), 'stream_length' ) );
1103 20 $self->cr;
1104 }
1105  
1106 # open a new stream if needed
1107 32 ▶ if (defined $page) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $page) 20 12 32
1108  
1109 # get an object id for the stream
1110 20 my $obj = $self->reserve('stream');
1111  
1112 # release it
1113 20 delete $self->{'reservations'}{'stream'};
1114  
1115 # get another one for the length of this stream
1116 20 my $stream_length = $self->reserve('stream_length');
1117 20 push @$stream_length, 'R';
1118 20 push @{ $page->{'contents'} }, $obj;
1119  
1120 # write the beginning of the object
1121 20 push @{ $self->{'crossrefsubsection'}{ $$obj[1] } }, [ $$obj[0], $self->position, 1 ];
1122 20 $self->add("$$obj[0] $$obj[1] obj");
1123 20 $self->cr;
1124 20 $self->add('<<');
1125 20 $self->cr;
1126 20 $self->add( '/Length ', join( ' ', @$stream_length ) );
1127 20 $self->cr;
1128 20 $self->add('>>');
1129 20 $self->cr;
1130 20 $self->add('stream');
1131 20 $self->cr;
1132 20 $self->{'stream_pos'} = $self->position;
1133 20 $self->{'stream_page'} = $page;
1134 }
1135 }
1136  
1137 =head2 get_data()
1138  
1139 If you did not ask the $pdf object to write its output to a file, you can pick up
1140 the pdf code by calling this method. It returns a big string. You need to call
1141 C<close> first.
1142  
1143 =cut
1144  
1145 sub get_data {
1146 0 ▶ shift->{'data'};
Subroutine get_data: not called CC=1 SCAR=6.9
Pod documented
1147 }
1148  
1149 sub uses_font {
1150 83 ▶ my ($self, $page, $font) = @_;
Subroutine uses_font: called CC=1 SCAR=0.0
Pod undocumented
1151  
1152 83 $page->{'resources'}{'fonts'}{$font} = 1;
1153 83 $page->{'resources'}{'ProcSet'} = [ 'PDF', 'Text' ];
1154 83 $self->{'fontobj'}{$font} = 1;
1155 }
1156  
1157 sub uses_xobject {
1158 2 ▶ my ($self, $page, $xobject) = @_;
Subroutine uses_xobject: called CC=1 SCAR=0.0
Pod undocumented
1159  
1160 2 $page->{'resources'}{'xobjects'}{$xobject} = 1;
1161 2 $page->{'resources'}{'ProcSet'} = [ 'PDF', 'Text' ];
1162 2 $self->{'xobj'}{$xobject} = 1;
1163 }
1164  
1165 sub debug {
1166 7431 ▶ my ($level, $msg) = @_;
Subroutine debug: called CC=3 SCAR=16.5
Pod undocumented
1167  
1168 7431 ▶ return unless ( $DEBUG >= $level );
Branch: 1 branch
BranchTrueFalseTotal
unless $DEBUG >= $level 7431 0 7431
1169 0 ▶ my $s = scalar @_ ? sprintf $msg, @_ : $msg;
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ ? : 0 0 0
1170  
1171 0 warn "DEBUG ($level): $s\n";
1172 }
1173  
1174 sub add {
1175 2486 ▶ my $self = shift;
Subroutine add: called CC=2 SCAR=6.9
Pod undocumented
1176 2486 my $data = join '', @_;
1177  
1178 2486 $self->{'size'} += length $data;
1179 2486 ▶ if ( defined $self->{'fh'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'fh'}) { } 2476 10 2486
1180 2476 my $fh = $self->{'fh'};
1181 2476 print $fh $data;
1182 } else {
1183 10 $self->{'data'} .= $data;
1184 }
1185 }
1186  
1187 sub position {
1188 356 ▶ my ($self) = @_;
Subroutine position: called CC=1 SCAR=0.0
Pod undocumented
1189  
1190 356 $self->{'size'};
1191 }
1192  
1193 sub add_version {
1194 23 ▶ my ($self) = @_;
Subroutine add_version: called CC=1 SCAR=0.0
Pod undocumented
1195  
1196 23 debug( 2, "add_version(): $self->{'version'}" );
1197 23 $self->add( "%PDF-" . $self->{'version'} );
1198 23 $self->cr;
1199 }
1200  
1201 sub add_object {
1202 142 ▶ my ($self, $v) = @_;
Subroutine add_object: called CC=1 SCAR=0.0
Pod undocumented
1203  
1204 142 my $val = &encode(@$v);
1205 142 $self->add($val);
1206 142 $self->cr;
1207 142 debug( 3, "add_object(): $v -> $val" );
1208 142 [ $$v[1][0], $$v[1][1] ];
1209 }
1210  
1211 sub null {
1212 54 ▶ my ($self) = @_;;
Subroutine null: called CC=1 SCAR=0.0
Pod undocumented
1213  
1214 54 [ 'null', 'null' ];
1215 }
1216  
1217 sub boolean {
1218 0 ▶ my ($self, $val) = @_;
Subroutine boolean: not called CC=1 SCAR=6.9
Pod undocumented
1219  
1220 0 [ 'boolean', $val ];
1221 }
1222  
1223 sub number {
1224 112 ▶ my ($self, $val) = @_;;
Subroutine number: called CC=1 SCAR=0.0
Pod undocumented
1225  
1226 112 [ 'number', $val ];
1227 }
1228  
1229 sub name {
1230 313 ▶ my ($self, $val) = @_;
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
1231  
1232 313 [ 'name', $val ];
1233 }
1234  
1235 sub string {
1236 56 ▶ my ($self, $val) = @_;
Subroutine string: called CC=1 SCAR=0.0
Pod undocumented
1237  
1238 56 [ 'string', $val ];
1239 }
1240  
1241 sub verbatim {
1242 5 ▶ my ($self, $val) = @_;
Subroutine verbatim: called CC=1 SCAR=0.0
Pod undocumented
1243  
1244 5 [ 'verbatim', $val ];
1245 }
1246  
1247 sub array {
1248 94 ▶ my $self = shift;
Subroutine array: called CC=1 SCAR=0.0
Pod undocumented
1249  
1250 94 [ 'array', [@_] ];
1251 }
1252  
1253 sub dictionary {
1254 155 ▶ my $self = shift;
Subroutine dictionary: called CC=1 SCAR=0.0
Pod undocumented
1255  
1256 155 [ 'dictionary', {@_} ];
1257 }
1258  
1259 sub indirect_obj {
1260 142 ▶ my $self = shift;
Subroutine indirect_obj: called CC=8 SCAR=20.8
Pod undocumented
1261  
1262 142 my ($id, $gen, $type, $name);
1263 142 $name = $_[1];
1264 $type = $_[0][1]{'Type'}[1]
1265 142 ▶ if defined $_[0][1] && ref $_[0][1] eq 'HASH' && defined $_[0][1]{'Type'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0][1] and ref $_[0][1] eq "HASH" and defined $_[0][1]{"Type"} 100 42 142
Condition83%5/6
defined $_[0][1] and ref $_[0][1] eq "HASH" and defined $_[0][1]{"Type"}
!ll&&!rl&&r
2022100
defined $_[0][1] and ref $_[0][1] eq "HASH"
!ll&&!rl&&r
020122
MC/DC66%2/3
defined $_[0][1] and ref $_[0][1] eq "HASH" and defined $_[0][1]{"Type"}
defined $_[0][1] ref $_[0][1] eq "HASH" defined $_[0][1]{"Type"}
Truth table75%3/4
A and B and C
A= defined $_[0][1]B= ref $_[0][1] eq "HASH"C= defined $_[0][1]{"Type"}
ABCresult
0XX0
10X0
1100
1111
1266  
1267 142 ▶ if ( defined $name && defined $self->{'reservations'}{$name} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $name and defined $self->{'reservations'}{$name}) { } 102 40 142
elsif (defined $type and defined $self->{'reservations'}{$type}) { } 39 1 40
Condition66%2/3
defined $name and defined $self->{"reservations"}{$name}
!ll&&!rl&&r
400102
MC/DC50%1/2
defined $name and defined $self->{"reservations"}{$name}
defined $name defined $self->{"reservations"}{$name}
Truth table66%2/3
A and B
A= defined $nameB= defined $self->{"reservations"}{$name}
ABresult
0X0
100
111
1268 102 ( $id, $gen ) = @{ $self->{'reservations'}{$name} };
1269 102 delete $self->{'reservations'}{$name};
1270 ▶ } elsif ( defined $type && defined $self->{'reservations'}{$type} ) {
Condition66%2/3
defined $type and defined $self->{"reservations"}{$type}
!ll&&!rl&&r
1039
MC/DC50%1/2
defined $type and defined $self->{"reservations"}{$type}
defined $type defined $self->{"reservations"}{$type}
Truth table66%2/3
A and B
A= defined $typeB= defined $self->{"reservations"}{$type}
ABresult
0X0
100
111
1271 39 ( $id, $gen ) = @{ $self->{'reservations'}{$type} };
1272 39 delete $self->{'reservations'}{$type};
1273 } else {
1274 1 $id = ++$self->{'object_number'};
1275 1 $gen = $self->{'generation_number'};
1276 }
1277 142 debug( 3, "indirect_obj(): " . $self->position );
1278 142 push @{ $self->{'crossrefsubsection'}{$gen} }, [ $id, $self->position, 1 ];
1279 142 [ 'object', [ $id, $gen, @_ ] ];
1280 }
1281  
1282 sub indirect_ref {
1283 226 ▶ my $self = shift;
Subroutine indirect_ref: called CC=1 SCAR=0.0
Pod undocumented
1284  
1285 226 [ 'ref', [@_] ];
1286 }
1287  
1288 sub stream {
1289 3 ▶ my $self = shift;
Subroutine stream: called CC=1 SCAR=0.0
Pod undocumented
1290  
1291 3 [ 'stream', {@_} ];
1292 }
1293  
1294 sub add_info {
1295 12 ▶ my $self = shift;
Subroutine add_info: called CC=13 SCAR=28.1
Pod undocumented
1296  
1297 12 debug( 2, "add_info():" );
1298 12 my %params = @_;
1299 12 ▶ $params{'Author'} = $self->{'Author'} if defined $self->{'Author'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"Author"} 12 0 12
1300 12 ▶ $params{'Creator'} = $self->{'Creator'} if defined $self->{'Creator'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"Creator"} 0 12 12
1301 12 ▶ $params{'Title'} = $self->{'Title'} if defined $self->{'Title'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"Title"} 12 0 12
1302 12 ▶ $params{'Subject'} = $self->{'Subject'} if defined $self->{'Subject'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"Subject"} 0 12 12
1303 12 ▶ $params{'Keywords'} = $self->{'Keywords'} if defined $self->{'Keywords'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"Keywords"} 0 12 12
1304 $params{'CreationDate'} = $self->{'CreationDate'}
1305 12 ▶ if defined $self->{'CreationDate'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"CreationDate"} 0 12 12
1306  
1307 12 $self->{'info'} = $self->reserve('Info');
1308 12 my $content = {
1309 'Producer' => $self->string("PDF::Create version $VERSION"),
1310 'Type' => $self->name('Info')
1311 };
1312 $$content{'Author'} = $self->string( $params{'Author'} )
1313 12 ▶ if defined $params{'Author'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"Author"} 12 0 12
1314 $$content{'Creator'} = $self->string( $params{'Creator'} )
1315 12 ▶ if defined $params{'Creator'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"Creator"} 0 12 12
1316 $$content{'Title'} = $self->string( $params{'Title'} )
1317 12 ▶ if defined $params{'Title'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"Title"} 12 0 12
1318 $$content{'Subject'} = $self->string( $params{'Subject'} )
1319 12 ▶ if defined $params{'Subject'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"Subject"} 0 12 12
1320 $$content{'Keywords'} = $self->string( $params{'Keywords'} )
1321 12 ▶ if defined $params{'Keywords'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"Keywords"} 0 12 12
1322 $$content{'CreationDate'} = $self->string( $params{'CreationDate'} )
1323 12 ▶ if defined $params{'CreationDate'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"CreationDate"} 0 12 12
1324  
1325 12 $self->add_object( $self->indirect_obj( $self->dictionary(%$content) ), 'Info' );
1326 12 $self->cr;
1327 }
1328  
1329 sub add_catalog {
1330 12 ▶ my $self = shift;
Subroutine add_catalog: called CC=3 SCAR=11.0
Pod undocumented
1331  
1332 12 debug( 2, "add_catalog" );
1333 12 my %params = %{ $self->{'catalog'} };
1334  
1335 # Type (mandatory)
1336 12 $self->{'catalog'} = $self->reserve('Catalog');
1337 12 my $content = { 'Type' => $self->name('Catalog') };
1338  
1339 # Pages (mandatory) [indirected reference]
1340 12 my $pages = $self->reserve('Pages');
1341 12 $$content{'Pages'} = $self->indirect_ref(@$pages);
1342 12 $self->{'pages'}{'id'} = $$content{'Pages'}[1];
1343  
1344 # Outlines [indirected reference]
1345 3 $$content{'Outlines'} = $self->indirect_ref( @{ $self->{'outlines'}->{'id'} } )
1346 12 ▶ if defined $self->{'outlines'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"outlines"} 3 9 12
1347  
1348 # PageMode
1349 12 ▶ $$content{'PageMode'} = $self->name($params{'PageMode'}) if defined $params{'PageMode'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"PageMode"} 12 0 12
1350  
1351 12 $self->add_object( $self->indirect_obj( $self->dictionary(%$content) ) );
1352 12 $self->cr;
1353 }
1354  
1355 sub encode {
1356 4897 ▶ my ($type, $val) = @_;
Subroutine encode: called CC=36 SCAR=36.1
Pod undocumented
1357  
1358 4897 ▶ if ($val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val) { } 2113 2784 4897
1359 2113 debug( 4, "encode(): $type $val" );
1360 } else {
1361 2784 debug( 4, "encode(): $type (no val)" );
1362 }
1363  
1364 4897 ▶ if (!$type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 1 4896 4897
1365 1 cluck "PDF::Create::encode: empty argument, called by ";
1366 1 return 1;
1367 }
1368  
1369 ( $type eq 'null' || $type eq 'number' ) && do {
1370 916 ▶ 1; # do nothing
Condition66%2/3
$type eq "cr" and do { $val = "\n" }
!ll&&!rl&&r
160002380
MC/DC50%1/2
$type eq "cr" and do { $val = "\n" }
$type eq "cr" do { $val = "\n" }
Truth table66%2/3
A and B
A= $type eq "cr"B= do { $val = "\n" }
ABresult
0X0
100
111
1371 }
1372 || $type eq 'cr' && do {
1373 2380 ▶ $val = "\n";
Condition66%2/3
$type eq "boolean" and do { $val = $val eq "true" ? $val : ($val eq "false" ? $val : ($val eq 0 ? "false" : "true")) }
!ll&&!rl&&r
159604
MC/DC50%1/2
$type eq "boolean" and do { $val = $val eq "true" ? $val : ($val eq "false" ? $val : ($val eq 0 ? "false" : "true")) }
$type eq "boolean" do { $val = $val eq "true" ? $val : ($val eq "false" ? $val : ($val eq 0 ? "false" : "true")) }
Truth table66%2/3
A and B
A= $type eq "boolean"B= do { $val = $val eq "true" ? $val : ($val eq "false" ? $val : ($val eq 0 ? "false" : "true")) }
ABresult
0X0
100
111
1374 }
1375 || $type eq 'boolean' && do {
1376 4 ▶ $val =
Branch: 3 branches
BranchTrueFalseTotal
$val eq 'true' ? : 1 3 4
$val eq 'false' ? : 1 2 3
$val eq '0' ? : 1 1 2
Condition66%2/3
$type eq "verbatim" and do { $val = "$val" }
!ll&&!rl&&r
158808
MC/DC50%1/2
$type eq "verbatim" and do { $val = "$val" }
$type eq "verbatim" do { $val = "$val" }
Truth table66%2/3
A and B
A= $type eq "verbatim"B= do { $val = "$val" }
ABresult
0X0
100
111
1377 $val eq 'true' ? $val
1378 : $val eq 'false' ? $val
1379 : $val eq '0' ? 'false'
1380 : 'true';
1381 }
1382 || $type eq 'verbatim' && do {
1383 8 ▶ $val = "$val";
Condition66%2/3
$type eq "string" and do { $val = "" unless defined $val; $val = "($val)" }
!ll&&!rl&&r
1528060
MC/DC50%1/2
$type eq "string" and do { $val = "" unless defined $val; $val = "($val)" }
$type eq "string" do { $val = "" unless defined $val; $val = "($val)" }
Truth table66%2/3
A and B
A= $type eq "string"B= do { $val = "" unless defined $val; $val = "($val)" }
ABresult
0X0
100
111
1384 }
1385 || $type eq 'string' && do {
1386 60 ▶ $val = '' if not defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 1 59 60
1387 # TODO: split it. Quote parentheses.
1388 60 ▶ $val = "($val)";
Condition33%1/3
$type eq "number" and do { $val = "$val" }
!ll&&!rl&&r
152800
MC/DC0%0/2
$type eq "number" and do { $val = "$val" }
$type eq "number" do { $val = "$val" }
Truth table33%1/3
A and B
A= $type eq "number"B= do { $val = "$val" }
ABresult
0X0
100
111
1389 }
1390 || $type eq 'number' && do {
1391 0 ▶ $val = "$val";
Condition66%2/3
$type eq "name" and do { $val = "" unless defined $val; $val = "/$val" }
!ll&&!rl&&r
6260902
MC/DC50%1/2
$type eq "name" and do { $val = "" unless defined $val; $val = "/$val" }
$type eq "name" do { $val = "" unless defined $val; $val = "/$val" }
Truth table66%2/3
A and B
A= $type eq "name"B= do { $val = "" unless defined $val; $val = "/$val" }
ABresult
0X0
100
111
1392 }
1393 || $type eq 'name' && do {
1394 902 ▶ $val = '' if not defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 1 901 902
1395 902 ▶ $val = "/$val";
Condition100%3/3
$type eq "array" and do { my $s = "["; foreach my $v (@$val) { $s .= &encode($v->[0], $v->[1]) . ' '; } ; chop $s; $val = $s . ']' }
!ll&&!rl&&r
5306234
MC/DC100%2/2
$type eq "array" and do { my $s = "["; foreach my $v (@$val) { $s .= &encode($v->[0], $v->[1]) . ' '; } ; chop $s; $val = $s . ']' }
$type eq "array" do { my $s = "["; foreach my $v (@$val) { $s .= &encode($v->[0], $v->[1]) . ' '; } ; chop $s; $val = $s . ']' }
Truth table100%3/3
A and B
A= $type eq "array"B= do { my $s = "["; foreach my $v (@$val) { $s .= &encode($v->[0], $v->[1]) . ' '; } ; chop $s; $val = $s . ']' }
ABresult
0X0
100
111
1396 }
1397 || $type eq 'array' && do {
1398  
1399 # array, encode contents individually
1400 96 my $s = '[';
1401 96 for my $v (@$val) {
1402 234 $s .= &encode( $$v[0], $$v[1] ) . " ";
1403 }
1404 # remove the trailing space
1405 96 chop $s;
1406 96 ▶ $val = $s . "]";
Condition100%3/3
$type eq "dictionary" and do { my $s = '<<' . &encode("cr"); foreach my $v (keys %$val) { $s .= &encode("name", $v) . ' '; $s .= &encode(do { $$val{$v} }->[0], do { $$val{$v} }->[1]); $s .= &encode("cr"); } ; $val = $s . '>>' }
!ll&&!rl&&r
37510847
MC/DC100%2/2
$type eq "dictionary" and do { my $s = '<<' . &encode("cr"); foreach my $v (keys %$val) { $s .= &encode("name", $v) . ' '; $s .= &encode(do { $$val{$v} }->[0], do { $$val{$v} }->[1]); $s .= &encode("cr"); } ; $val = $s . '>>' }
$type eq "dictionary" do { my $s = '<<' . &encode("cr"); foreach my $v (keys %$val) { $s .= &encode("name", $v) . ' '; $s .= &encode(do { $$val{$v} }->[0], do { $$val{$v} }->[1]); $s .= &encode("cr"); } ; $val = $s . '>>' }
Truth table100%3/3
A and B
A= $type eq "dictionary"B= do { my $s = '<<' . &encode("cr"); foreach my $v (keys %$val) { $s .= &encode("name", $v) . ' '; $s .= &encode(do { $$val{$v} }->[0], do { $$val{$v} }->[1]); $s .= &encode("cr"); } ; $val = $s . '>>' }
ABresult
0X0
100
111
1407 }
1408 || $type eq 'dictionary' && do {
1409 155 my $s = '<<' . &encode('cr');
1410 155 for my $v ( keys %$val ) {
1411 580 $s .= &encode( 'name', $v ) . " ";
1412 580 $s .= &encode( ${ $$val{$v} }[0], ${ $$val{$v} }[1] ); # . " ";
1413 580 $s .= &encode('cr');
1414 }
1415 155 ▶ $val = $s . ">>";
Condition100%3/3
$type eq "object" and do { my $s = &encode("number", $val->[0]) . ' ' . &encode("number", $val->[1]) . ' obj'; $s .= &encode("cr"); $s .= &encode($val->[2][0], $val->[2][1]); $s .= &encode("cr"); $val = $s . 'endobj' }
!ll&&!rl&&r
2327964
MC/DC100%2/2
$type eq "object" and do { my $s = &encode("number", $val->[0]) . ' ' . &encode("number", $val->[1]) . ' obj'; $s .= &encode("cr"); $s .= &encode($val->[2][0], $val->[2][1]); $s .= &encode("cr"); $val = $s . 'endobj' }
$type eq "object" do { my $s = &encode("number", $val->[0]) . ' ' . &encode("number", $val->[1]) . ' obj'; $s .= &encode("cr"); $s .= &encode($val->[2][0], $val->[2][1]); $s .= &encode("cr"); $val = $s . 'endobj' }
Truth table100%3/3
A and B
A= $type eq "object"B= do { my $s = &encode("number", $val->[0]) . ' ' . &encode("number", $val->[1]) . ' obj'; $s .= &encode("cr"); $s .= &encode($val->[2][0], $val->[2][1]); $s .= &encode("cr"); $val = $s . 'endobj' }
ABresult
0X0
100
111
1416 }
1417 || $type eq 'object' && do {
1418 143 my $s = &encode( 'number', $$val[0] ) . " " . &encode( 'number', $$val[1] ) . " obj";
1419 143 $s .= &encode('cr');
1420 143 $s .= &encode( $$val[2][0], $$val[2][1] ); # . " ";
1421 143 $s .= &encode('cr');
1422 143 ▶ $val = $s . "endobj";
Condition66%2/3
$type eq "ref" and do { my $s = &encode("number", $val->[0]) . ' ' . &encode("number", $val->[1]) . ' R'; $val = $s }
!ll&&!rl&&r
40228
MC/DC50%1/2
$type eq "ref" and do { my $s = &encode("number", $val->[0]) . ' ' . &encode("number", $val->[1]) . ' R'; $val = $s }
$type eq "ref" do { my $s = &encode("number", $val->[0]) . ' ' . &encode("number", $val->[1]) . ' R'; $val = $s }
Truth table66%2/3
A and B
A= $type eq "ref"B= do { my $s = &encode("number", $val->[0]) . ' ' . &encode("number", $val->[1]) . ' R'; $val = $s }
ABresult
0X0
100
111
1423 }
1424 || $type eq 'ref' && do {
1425 228 my $s = &encode( 'number', $$val[0] ) . " " . &encode( 'number', $$val[1] ) . " R";
1426 228 ▶ $val = $s;
Condition66%2/3
$type eq "stream" and do { my $data = delete $val->{"Data"}; my $s = '<<' . &encode("cr"); foreach my $v (keys %$val) { $s .= &encode("name", $v) . ' '; $s .= &encode(do { $$val{$v} }->[0], do { $$val{$v} }->[1]); $s .= &encode("cr"); } ; $s .= '>>' . &encode("cr") . 'stream' . &encode("cr"); $s .= $data . &encode("cr"); $val = $s . 'endstream' . &encode("cr") }
!ll&&!rl&&r
103
MC/DC50%1/2
$type eq "stream" and do { my $data = delete $val->{"Data"}; my $s = '<<' . &encode("cr"); foreach my $v (keys %$val) { $s .= &encode("name", $v) . ' '; $s .= &encode(do { $$val{$v} }->[0], do { $$val{$v} }->[1]); $s .= &encode("cr"); } ; $s .= '>>' . &encode("cr") . 'stream' . &encode("cr"); $s .= $data . &encode("cr"); $val = $s . 'endstream' . &encode("cr") }
$type eq "stream" do { my $data = delete $val->{"Data"}; my $s = '<<' . &encode("cr"); foreach my $v (keys %$val) { $s .= &encode("name", $v) . ' '; $s .= &encode(do { $$val{$v} }->[0], do { $$val{$v} }->[1]); $s .= &encode("cr"); } ; $s .= '>>' . &encode("cr") . 'stream' . &encode("cr"); $s .= $data . &encode("cr"); $val = $s . 'endstream' . &encode("cr") }
Truth table66%2/3
A and B
A= $type eq "stream"B= do { my $data = delete $val->{"Data"}; my $s = '<<' . &encode("cr"); foreach my $v (keys %$val) { $s .= &encode("name", $v) . ' '; $s .= &encode(do { $$val{$v} }->[0], do { $$val{$v} }->[1]); $s .= &encode("cr"); } ; $s .= '>>' . &encode("cr") . 'stream' . &encode("cr"); $s .= $data . &encode("cr"); $val = $s . 'endstream' . &encode("cr") }
ABresult
0X0
100
111
1427 }
1428 4896 ▶ || $type eq 'stream' && do {
Branch: 1 branch
BranchTrueFalseTotal
$type eq "null" || $type eq "number" and do { 1 } or $type eq "cr" and do { $val = "\n" } or $type eq "boolean" and do { $val = $val eq "true" ? $val : ($val eq "false" ? $val : ($val eq 0 ? "false" : "true")) } or $type eq "verbatim" and do { $val = "$val" } or $type eq "string" and do { $val = "" unless defined $val; $val = "($val)" } or $type eq "number" and do { $val = "$val" } or $type eq "name" and do { $val = "" unless defined $val; $val = "/$val" } or $type eq "array" and do { my $s = "["; foreach my $v (@$val) { $s .= &encode($v->[0], $v->[1]) . ' '; } ; chop $s; $val = $s . ']' } or $type eq "dictionary" and do { my $s = '<<' . &encode("cr"); foreach my $v (keys %$val) { $s .= &encode("name", $v) . ' '; $s .= &encode(do { $$val{$v} }->[0], do { $$val{$v} }->[1]); $s .= &encode("cr"); } ; $val = $s . '>>' } or $type eq "object" and do { my $s = &encode("number", $val->[0]) . ' ' . &encode("number", $val->[1]) . ' obj'; $s .= &encode("cr"); $s .= &encode($val->[2][0], $val->[2][1]); $s .= &encode("cr"); $val = $s . 'endobj' } or $type eq "ref" and do { my $s = &encode("number", $val->[0]) . ' ' . &encode("number", $val->[1]) . ' R'; $val = $s } or $type eq "stream" and do { my $data = delete $val->{"Data"}; my $s = '<<' . &encode("cr"); foreach my $v (keys %$val) { $s .= &encode("name", $v) . ' '; $s .= &encode(do { $$val{$v} }->[0], do { $$val{$v} }->[1]); $s .= &encode("cr"); } ; $s .= '>>' . &encode("cr") . 'stream' . &encode("cr"); $s .= $data . &encode("cr"); $val = $s . 'endstream' . &encode("cr") } or confess("Error: unknown type '${type}'") 1 4895 4896
Condition94%37/39
$type eq "null" || $type eq "number" and do { 1 } or $type eq "cr" and do { $val = "\n" } or $type eq "boolean" and do { $val = $val eq "true" ? $val : ($val eq "false" ? $val : ($val eq 0 ? "false" : "true")) } or $type eq "verbatim" and do { $val = "$val" } or $type eq "string" and do { $val = "" unless defined $val; $val = "($val)" } or $type eq "number" and do { $val = "$val" } or $type eq "name" and do { $val = "" unless defined $val; $val = "/$val" } or $type eq "array" and do { my $s = "["; foreach my $v (@$val) { $s .= &encode($v->[0], $v->[1]) . ' '; } ; chop $s; $val = $s . ']' } or $type eq "dictionary" and do { my $s = '<<' . &encode("cr"); foreach my $v (keys %$val) { $s .= &encode("name", $v) . ' '; $s .= &encode(do { $$val{$v} }->[0], do { $$val{$v} }->[1]); $s .= &encode("cr"); } ; $val = $s . '>>' } or $type eq "object" and do { my $s = &encode("number", $val->[0]) . ' ' . &encode("number", $val->[1]) . ' obj'; $s .= &encode("cr"); $s .= &encode($val->[2][0], $val->[2][1]); $s .= &encode("cr"); $val = $s . 'endobj' } or $type eq "ref" and do { my $s = &encode("number", $val->[0]) . ' ' . &encode("number", $val->[1]) . ' R'; $val = $s } or $type eq "stream" and do { my $data = delete $val->{"Data"}; my $s = '<<' . &encode("cr"); foreach my $v (keys %$val) { $s .= &encode("name", $v) . ' '; $s .= &encode(do { $$val{$v} }->[0], do { $$val{$v} }->[1]); $s .= &encode("cr"); } ; $s .= '>>' . &encode("cr") . 'stream' . &encode("cr"); $s .= $data . &encode("cr"); $val = $s . 'endstream' . &encode("cr") }
l!l&&r!l&&!r
489231
$type eq "null" || $type eq "number" and do { 1 } or $type eq "cr" and do { $val = "\n" } or $type eq "boolean" and do { $val = $val eq "true" ? $val : ($val eq "false" ? $val : ($val eq 0 ? "false" : "true")) } or $type eq "verbatim" and do { $val = "$val" } or $type eq "string" and do { $val = "" unless defined $val; $val = "($val)" } or $type eq "number" and do { $val = "$val" } or $type eq "name" and do { $val = "" unless defined $val; $val = "/$val" } or $type eq "array" and do { my $s = "["; foreach my $v (@$val) { $s .= &encode($v->[0], $v->[1]) . ' '; } ; chop $s; $val = $s . ']' } or $type eq "dictionary" and do { my $s = '<<' . &encode("cr"); foreach my $v (keys %$val) { $s .= &encode("name", $v) . ' '; $s .= &encode(do { $$val{$v} }->[0], do { $$val{$v} }->[1]); $s .= &encode("cr"); } ; $val = $s . '>>' } or $type eq "object" and do { my $s = &encode("number", $val->[0]) . ' ' . &encode("number", $val->[1]) . ' obj'; $s .= &encode("cr"); $s .= &encode($val->[2][0], $val->[2][1]); $s .= &encode("cr"); $val = $s . 'endobj' } or $type eq "ref" and do { my $s = &encode("number", $val->[0]) . ' ' . &encode("number", $val->[1]) . ' R'; $val = $s }
l!l&&r!l&&!r
46642284
$type eq "null" || $type eq "number" and do { 1 } or $type eq "cr" and do { $val = "\n" } or $type eq "boolean" and do { $val = $val eq "true" ? $val : ($val eq "false" ? $val : ($val eq 0 ? "false" : "true")) } or $type eq "verbatim" and do { $val = "$val" } or $type eq "string" and do { $val = "" unless defined $val; $val = "($val)" } or $type eq "number" and do { $val = "$val" } or $type eq "name" and do { $val = "" unless defined $val; $val = "/$val" } or $type eq "array" and do { my $s = "["; foreach my $v (@$val) { $s .= &encode($v->[0], $v->[1]) . ' '; } ; chop $s; $val = $s . ']' } or $type eq "dictionary" and do { my $s = '<<' . &encode("cr"); foreach my $v (keys %$val) { $s .= &encode("name", $v) . ' '; $s .= &encode(do { $$val{$v} }->[0], do { $$val{$v} }->[1]); $s .= &encode("cr"); } ; $val = $s . '>>' } or $type eq "object" and do { my $s = &encode("number", $val->[0]) . ' ' . &encode("number", $val->[1]) . ' obj'; $s .= &encode("cr"); $s .= &encode($val->[2][0], $val->[2][1]); $s .= &encode("cr"); $val = $s . 'endobj' }
l!l&&r!l&&!r
452164311
$type eq "null" || $type eq "number" and do { 1 } or $type eq "cr" and do { $val = "\n" } or $type eq "boolean" and do { $val = $val eq "true" ? $val : ($val eq "false" ? $val : ($val eq 0 ? "false" : "true")) } or $type eq "verbatim" and do { $val = "$val" } or $type eq "string" and do { $val = "" unless defined $val; $val = "($val)" } or $type eq "number" and do { $val = "$val" } or $type eq "name" and do { $val = "" unless defined $val; $val = "/$val" } or $type eq "array" and do { my $s = "["; foreach my $v (@$val) { $s .= &encode($v->[0], $v->[1]) . ' '; } ; chop $s; $val = $s . ']' } or $type eq "dictionary" and do { my $s = '<<' . &encode("cr"); foreach my $v (keys %$val) { $s .= &encode("name", $v) . ' '; $s .= &encode(do { $$val{$v} }->[0], do { $$val{$v} }->[1]); $s .= &encode("cr"); } ; $val = $s . '>>' }
l!l&&r!l&&!r
436647483
$type eq "null" || $type eq "number" and do { 1 } or $type eq "cr" and do { $val = "\n" } or $type eq "boolean" and do { $val = $val eq "true" ? $val : ($val eq "false" ? $val : ($val eq 0 ? "false" : "true")) } or $type eq "verbatim" and do { $val = "$val" } or $type eq "string" and do { $val = "" unless defined $val; $val = "($val)" } or $type eq "number" and do { $val = "$val" } or $type eq "name" and do { $val = "" unless defined $val; $val = "/$val" } or $type eq "array" and do { my $s = "["; foreach my $v (@$val) { $s .= &encode($v->[0], $v->[1]) . ' '; } ; chop $s; $val = $s . ']' }
l!l&&r!l&&!r
427034592
$type eq "null" || $type eq "number" and do { 1 } or $type eq "cr" and do { $val = "\n" } or $type eq "boolean" and do { $val = $val eq "true" ? $val : ($val eq "false" ? $val : ($val eq 0 ? "false" : "true")) } or $type eq "verbatim" and do { $val = "$val" } or $type eq "string" and do { $val = "" unless defined $val; $val = "($val)" } or $type eq "number" and do { $val = "$val" } or $type eq "name" and do { $val = "" unless defined $val; $val = "/$val" }
l!l&&r!l&&!r
3368902626
$type eq "null" || $type eq "number" and do { 1 } or $type eq "cr" and do { $val = "\n" } or $type eq "boolean" and do { $val = $val eq "true" ? $val : ($val eq "false" ? $val : ($val eq 0 ? "false" : "true")) } or $type eq "verbatim" and do { $val = "$val" } or $type eq "string" and do { $val = "" unless defined $val; $val = "($val)" } or $type eq "number" and do { $val = "$val" }
l!l&&r!l&&!r
336801528
$type eq "null" || $type eq "number" and do { 1 } or $type eq "cr" and do { $val = "\n" } or $type eq "boolean" and do { $val = $val eq "true" ? $val : ($val eq "false" ? $val : ($val eq 0 ? "false" : "true")) } or $type eq "verbatim" and do { $val = "$val" } or $type eq "string" and do { $val = "" unless defined $val; $val = "($val)" }
l!l&&r!l&&!r
3308601528
$type eq "null" || $type eq "number" and do { 1 } or $type eq "cr" and do { $val = "\n" } or $type eq "boolean" and do { $val = $val eq "true" ? $val : ($val eq "false" ? $val : ($val eq 0 ? "false" : "true")) } or $type eq "verbatim" and do { $val = "$val" }
l!l&&r!l&&!r
330081588
$type eq "null" || $type eq "number" and do { 1 } or $type eq "cr" and do { $val = "\n" } or $type eq "boolean" and do { $val = $val eq "true" ? $val : ($val eq "false" ? $val : ($val eq 0 ? "false" : "true")) }
l!l&&r!l&&!r
329641596
$type eq "null" || $type eq "number" and do { 1 } or $type eq "cr" and do { $val = "\n" }
l!l&&r!l&&!r
91623801600
$type eq "null" || $type eq "number" and do { 1 }
!ll&&!rl&&r
39800916
$type eq "null" or $type eq "number"
l!l&&r!l&&!r
568603980
MC/DC0%0/14
$type eq "null" || $type eq "number" and do { 1 } or $type eq "cr" and do { $val = "\n" } or $type eq "boolean" and do { $val = $val eq "true" ? $val : ($val eq "false" ? $val : ($val eq 0 ? "false" : "true")) } or $type eq "verbatim" and do { $val = "$val" } or $type eq "string" and do { $val = "" unless defined $val; $val = "($val)" } or $type eq "number" and do { $val = "$val" } or $type eq "name" and do { $val = "" unless defined $val; $val = "/$val" } or $type eq "array" and do { my $s = "["; foreach my $v (@$val) { $s .= &encode($v->[0], $v->[1]) . ' '; } ; chop $s; $val = $s . ']' } or $type eq "dictionary" and do { my $s = '<<' . &encode("cr"); foreach my $v (keys %$val) { $s .= &encode("name", $v) . ' '; $s .= &encode(do { $$val{$v} }->[0], do { $$val{$v} }->[1]); $s .= &encode("cr"); } ; $val = $s . '>>' } or $type eq "object" and do { my $s = &encode("number", $val->[0]) . ' ' . &encode("number", $val->[1]) . ' obj'; $s .= &encode("cr"); $s .= &encode($val->[2][0], $val->[2][1]); $s .= &encode("cr"); $val = $s . 'endobj' } or $type eq "ref" and do { my $s = &encode("number", $val->[0]) . ' ' . &encode("number", $val->[1]) . ' R'; $val = $s } or $type eq "stream" and do { my $data = delete $val->{"Data"}; my $s = '<<' . &encode("cr"); foreach my $v (keys %$val) { $s .= &encode("name", $v) . ' '; $s .= &encode(do { $$val{$v} }->[0], do { $$val{$v} }->[1]); $s .= &encode("cr"); } ; $s .= '>>' . &encode("cr") . 'stream' . &encode("cr"); $s .= $data . &encode("cr"); $val = $s . 'endstream' . &encode("cr") }
$type eq "null" $type eq "number" do { 1 } $type eq "cr" and do { $val = "\n" } $type eq "boolean" and do { $val = $val eq "true" ? $val : ($val eq "false" ? $val : ($val eq 0 ? "false" : "true")) } $type eq "verbatim" and do { $val = "$val" } $type eq "string" and do { $val = "" unless defined $val; $val = "($val)" } $type eq "number" and do { $val = "$val" } $type eq "name" and do { $val = "" unless defined $val; $val = "/$val" } $type eq "array" and do { my $s = "["; foreach my $v (@$val) { $s .= &encode($v->[0], $v->[1]) . ' '; } ; chop $s; $val = $s . ']' } $type eq "dictionary" and do { my $s = '<<' . &encode("cr"); foreach my $v (keys %$val) { $s .= &encode("name", $v) . ' '; $s .= &encode(do { $$val{$v} }->[0], do { $$val{$v} }->[1]); $s .= &encode("cr"); } ; $val = $s . '>>' } $type eq "object" and do { my $s = &encode("number", $val->[0]) . ' ' . &encode("number", $val->[1]) . ' obj'; $s .= &encode("cr"); $s .= &encode($val->[2][0], $val->[2][1]); $s .= &encode("cr"); $val = $s . 'endobj' } $type eq "ref" and do { my $s = &encode("number", $val->[0]) . ' ' . &encode("number", $val->[1]) . ' R'; $val = $s } $type eq "stream" and do { my $data = delete $val->{"Data"}; my $s = '<<' . &encode("cr"); foreach my $v (keys %$val) { $s .= &encode("name", $v) . ' '; $s .= &encode(do { $$val{$v} }->[0], do { $$val{$v} }->[1]); $s .= &encode("cr"); } ; $s .= '>>' . &encode("cr") . 'stream' . &encode("cr"); $s .= $data . &encode("cr"); $val = $s . 'endstream' . &encode("cr") }
Truth table0%0/38
A or B and C or D or E or F or G or H or I or J or K or L or M or N
A= $type eq "null"B= $type eq "number"C= do { 1 }D= $type eq "cr" and do { $val = "\n" }E= $type eq "boolean" and do { $val = $val eq "true" ? $val : ($val eq "false" ? $val : ($val eq 0 ? "false" : "true")) }F= $type eq "verbatim" and do { $val = "$val" }G= $type eq "string" and do { $val = "" unless defined $val; $val = "($val)" }H= $type eq "number" and do { $val = "$val" }I= $type eq "name" and do { $val = "" unless defined $val; $val = "/$val" }J= $type eq "array" and do { my $s = "["; foreach my $v (@$val) { $s .= &encode($v->[0], $v->[1]) . ' '; } ; chop $s; $val = $s . ']' }K= $type eq "dictionary" and do { my $s = '<<' . &encode("cr"); foreach my $v (keys %$val) { $s .= &encode("name", $v) . ' '; $s .= &encode(do { $$val{$v} }->[0], do { $$val{$v} }->[1]); $s .= &encode("cr"); } ; $val = $s . '>>' }L= $type eq "object" and do { my $s = &encode("number", $val->[0]) . ' ' . &encode("number", $val->[1]) . ' obj'; $s .= &encode("cr"); $s .= &encode($val->[2][0], $val->[2][1]); $s .= &encode("cr"); $val = $s . 'endobj' }M= $type eq "ref" and do { my $s = &encode("number", $val->[0]) . ' ' . &encode("number", $val->[1]) . ' R'; $val = $s }N= $type eq "stream" and do { my $data = delete $val->{"Data"}; my $s = '<<' . &encode("cr"); foreach my $v (keys %$val) { $s .= &encode("name", $v) . ' '; $s .= &encode(do { $$val{$v} }->[0], do { $$val{$v} }->[1]); $s .= &encode("cr"); } ; $s .= '>>' . &encode("cr") . 'stream' . &encode("cr"); $s .= $data . &encode("cr"); $val = $s . 'endstream' . &encode("cr") }
ABCDEFGHIJKLMNresult
1X1XXXXXXXXXXX1
011XXXXXXXXXXX1
00X1XXXXXXXXXX1
1X01XXXXXXXXXX1
0101XXXXXXXXXX1
00X01XXXXXXXXX1
1X001XXXXXXXXX1
01001XXXXXXXXX1
00X001XXXXXXXX1
1X0001XXXXXXXX1
010001XXXXXXXX1
00X0001XXXXXXX1
1X00001XXXXXXX1
0100001XXXXXXX1
00X00001XXXXXX1
1X000001XXXXXX1
01000001XXXXXX1
00X000001XXXXX1
1X0000001XXXXX1
010000001XXXXX1
00X0000001XXXX1
1X00000001XXXX1
0100000001XXXX1
00X00000001XXX1
1X000000001XXX1
01000000001XXX1
00X000000001XX1
1X0000000001XX1
010000000001XX1
00X0000000001X1
1X00000000001X1
0100000000001X1
00X000000000011
1X0000000000011
010000000000011
00X000000000000
1X0000000000000
010000000000000
1429 3 my $data = delete $$val{'Data'};
1430 3 my $s = '<<' . &encode('cr');
1431 3 for my $v ( keys %$val ) {
1432 22 $s .= &encode( 'name', $v ) . " ";
1433 22 $s .= &encode( ${ $$val{$v} }[0], ${ $$val{$v} }[1] ); # . " ";
1434 22 $s .= &encode('cr');
1435 }
1436 3 $s .= ">>" . &encode('cr') . "stream" . &encode('cr');
1437 3 $s .= $data . &encode('cr');
1438 3 $val = $s . "endstream" . &encode('cr');
1439 }
1440 || confess "Error: unknown type '$type'";
1441  
1442 # TODO: add type 'text';
1443 4895 $val;
1444 }
1445  
1446 =head1 LIMITATIONS
1447  
1448 C<PDF::Create> comes with a couple of limitations or known caveats:
1449  
1450 =head2 PDF Size / Memory
1451  
1452 C<PDF::Create> assembles the entire PDF in memory if you create very large documents
1453 on a machine with a small amount of memory your program can fail because it runs out
1454 of memory.
1455  
1456 =head2 Small GIF images
1457  
1458 Some gif images get created with a minimal lzw code size of less than 8. C<PDF::Create>
1459 can not decode those and they must be converted.
1460  
1461 =head1 SUPPORT
1462  
1463 I support C<PDF::Create> in my spare time between work and family, so the amount
1464 of work I put in is limited.
1465  
1466 If you experience a problem make sure you are at the latest version first many of
1467 things have already been fixed.
1468  
1469 Please register bug at the CPAN bug tracking system at L<http://rt.cpan.org> or
1470 send email to C<bug-PDF-Create [at] rt.cpan.org>
1471  
1472 Be sure to include the following information:
1473  
1474 =over 4
1475  
1476 =item - PDF::Create Version you are running
1477  
1478 =item - Perl version (perl -v)
1479  
1480 =item - Operating System vendor and version
1481  
1482 =item - Details about your operating environment that might be related to the issue
1483 being described
1484  
1485 =item - Exact cut and pasted error or warning messages
1486  
1487 =item - The shortest, clearest code you can manage to write which reproduces the
1488 bug described.
1489  
1490 =back
1491  
1492 I appreciate patches against the latest released version of C<PDF::Create> which
1493 fix the bug.
1494  
1495 B<Feature request> can be submitted like bugs. If you provide patch for a feature
1496 which does not go against the C<PDF::Create> philosophy (keep it simple) then you
1497 have a good chance for it to be accepted.
1498  
1499 =head1 SEE ALSO
1500  
1501 L<Adobe PDF|http://www.adobe.com/devnet/pdf/pdf_reference.html>
1502  
1503 L<PDF::Labels> Routines to produce formatted pages of mailing labels in PDF, uses L<PDF::Create> internally.
1504  
1505 L<PDF::Haru> Perl interface to Haru Free PDF Library.
1506  
1507 L<PDF::EasyPDF> PDF creation from a one-file module, similar to L<PDF::Create>.
1508  
1509 L<PDF::CreateSimple> Yet another PDF creation module
1510  
1511 L<PDF::Report> A wrapper written for L<PDF::API2>.
1512  
1513 =head1 AUTHORS
1514  
1515 Fabien Tassin
1516  
1517 GIF and JPEG-support: Michael Gross (info@mdgrosse.net)
1518  
1519 Maintenance since 2007: Markus Baertschi (markus@markus.org)
1520  
1521 Currently maintained by Mohammad S Anwar (MANWAR) C<< <mohammad.anwar at yahoo.com> >>
1522  
1523 =head1 REPOSITORY
1524  
1525 L<https://github.com/manwar/pdf-create>
1526  
1527 =head1 COPYRIGHT
1528  
1529 Copyright 1999-2001,Fabien Tassin.All rights reserved.It may be used and modified
1530 freely, but I do request that this copyright notice remain attached to the file.
1531 You may modify this module as you wish,but if you redistribute a modified version,
1532 please attach a note listing the modifications you have made.
1533  
1534 Copyright 1999-2001 Fabien Tassin
1535  
1536 Copyright 2007 Markus Baertschi
1537  
1538 Copyright 2010 Gary Lieberman
1539  
1540 =head1 LICENSE
1541  
1542 This is free software; you can redistribute it and / or modify it under the same
1543 terms as Perl 5.6.0.
1544  
1545 =cut
1546  
1547 18 1;
« Image/GIF.pm ↑ Summary Create/Outline.pm »