Builder/Page.pm

filter: Statementstmt 65.5% 200 / 305 Branchbran 45.4% 70 / 154 Conditioncond 42.1% 32 / 76 MC/DCmcdc 17.0% 8 / 47 Subroutinesub 81.0% 30 / 37 Podpod 70.8% 17 / 24 totaltotal 55.5% 357 / 643 CC 106 SCAR 33.5
CC
106
Cov
56%
gfx
64.1
text
64.1
_bbox
44.5
CRAP 28.6
« Builder/Basic/PDF/Pages.pm ↑ Summary Builder/Resource/ColorSpace/Indexed/Hue.pm »
1 package PDF::Builder::Page;
2  
3 39 ▶use base 'PDF::Builder::Basic::PDF::Pages';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 39 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 39 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 39 our $VERSION = '3.028'; # VERSION
9 39 our $LAST_UPDATE = '3.028'; # manually update whenever code is changed
10  
11 39 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 39 ▶use POSIX qw(floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 39 ▶use Scalar::Util qw(looks_like_number weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 39 ▶use PDF::Builder::Basic::PDF::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 39 ▶use PDF::Builder::Content;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 39 ▶use PDF::Builder::Content::Text;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 39 ▶use PDF::Builder::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =head1 NAME
21  
22 PDF::Builder::Page - Methods to interact with individual pages
23  
24 Inherits from L<PDF::Builder::Basic::PDF::Pages>
25  
26 =head1 SYNOPSIS
27  
28 my $pdf = PDF::Builder->new();
29  
30 # Add a page to a new or existing PDF
31 my $page = $pdf->page();
32  
33 # Set the physical (media) page size
34 # Set prepress page boundaries, a convenience function for those times when
35 # it is not necessary to set other prepress (print-related) page boundaries
36 $page->size('letter'); # by common size name
37 #$page->size([0, 0, 612, 792]); # by points LLx,LLy, URx,URy
38  
39 # alternately, can set (or get) prepress page boundaries
40 $page->boundaries('media' => '12x18', 'trim' => 0.5 * 72);
41  
42 # Add an image
43 my $image = $pdf->image('/path/to/file.jpg');
44 $page->object($image, $x,$y, $w,$h);
45  
46 # Create a content object for text
47 my $text = $page->text();
48  
49 # Create a content object for drawing shapes
50 my $canvas = $page->graphics(); # or gfx()
51  
52 # Now to draw graphics (using $canvas object) and text (using $text object).
53 # NOTE that everything in the graphics (canvas) object will be laid down on
54 # the page BEFORE anything in the text object is laid down. That is,
55 # text will cover graphics, but not vice-versa. This is simply due to
56 # the order in which the objects were defined.
57
58 =head1 METHODS
59  
60 =head2 new
61  
62 $page = PDF::Builder::Page->new($pdf, $parent, $index)
63  
64 =over
65  
66 Returns a page object (called from $pdf->page()).
67  
68 =back
69  
70 =cut
71  
72 192 ▶sub new {
Subroutine new: called CC=4 SCAR=15.6
Pod documented
73 192 my ($class, $pdf, $parent, $index) = @_;
74 192 my $self = {};
75  
76 192 ▶ $class = ref($class) if ref($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 192 192
77 192 $self = $class->SUPER::new($pdf, $parent);
78 192 $self->{'Type'} = PDFName('Page');
79 192 $self->proc_set(qw( PDF Text ImageB ImageC ImageI ));
80 192 delete $self->{'Count'};
81 192 delete $self->{'Kids'};
82 192 $parent->add_page($self, $index);
83  
84 # copy global UU (if exists) to local, possibly to be overridden
85 # we can access UserUnit (should be not 1.0, if exists) but not userUnit
86 192 ▶ if (defined $self->{'Parent'}->{'UserUnit'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'Parent'}{'UserUnit'}) { } 0 192 192
87 0 my $UU = $self->{'Parent'}->{'UserUnit'}->{'val'};
88 0 $self->{' userUnit'} = $UU;
89 # AND set the local one if global is not 1.0
90 # (some readers don't let a page inherit the global UU)
91 0 ▶ if ($UU != 1.0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($UU != 1) 0 0 0
92 0 $self->userunit($UU);
93 }
94 } else {
95 # not setting a global userUnit, so default this one to 1.0
96 192 $self->{' userUnit'} = 1;
97 # don't set a local UserUnit for now
98 }
99  
100 192 return $self;
101 }
102  
103 #=head2 coerce
104 #
105 # $page = PDF::Builder::Page->coerce($pdf, $pdfpage)
106 #
107 #=over
108 #
109 #Returns a page object converted from $pdfpage (called from $pdf->open_page()).
110 #
111 #=back
112 #
113 #=cut
114  
115 # appears to be unused TBD
116  
117 0 ▶sub coerce {
Subroutine coerce: not called CC=1 SCAR=6.9
Pod undocumented
118 0 my ($class, $pdf, $page) = @_;
119 0 my $self = $page;
120 0 bless $self, $class;
121 0 $self->{' apipdf'} = $pdf;
122 0 weaken $self->{' apipdf'};
123 0 return $self;
124 }
125  
126 #=head2 update
127 #
128 # $page->update()
129 #
130 #=over
131 #
132 #Marks a page to be updated (by $pdf->update()).
133 #
134 #=back
135 #
136 #=cut
137  
138 # appears to be internal routine
139 # replace any use by call to out_obj: $self->{' apipdf'}->out_obj($self);
140  
141 # PDF::API2 2.042: DEPRECATED
142 # Marking the page as dirty should only be needed in rare cases
143 # when the page hash is being edited directly rather than through the API. In
144 # that case, the out_obj call can be made manually. There's no reason (that I
145 # can think of) to have a specific call just (and only) for Page objects.
146  
147 1 ▶sub update {
Subroutine update: called CC=1 SCAR=0.0
Pod undocumented
148 1 my $self = shift;
149  
150 1 $self->{' apipdf'}->out_obj($self);
151 1 return $self;
152 }
153  
154 =head2 Page Size Methods
155  
156 =head3 Page Sizes
157  
158 PDF page sizes are stored as rectangular coordinates. For convenience,
159 PDF::Builder also supports a number of aliases and shortcuts that are more
160 human-friendly. The following formats are available:
161  
162 =over
163  
164 =item a standard paper size
165  
166 $page->boundaries('media' => 'A4');
167  
168 Aliases for the most common paper sizes are built in (case-insensitive).
169 US: Letter, Legal, Ledger, Tabloid (and others)
170 Metric: 4A0, 2A0, A0 - A6, 4B0, 2B0, and B0 - B6 (and others)
171  
172 See L<PDF::Builder::Resource::PaperSizes> for the full list.
173  
174 =item a "WxH" string in inches
175  
176 $page->boundaries('media' => '8.5x11');
177  
178 Many US paper sizes are commonly identified by their size in inches rather than
179 by a particular name. These can be passed as strings with the width and height
180 separated by an C<x>.
181 Examples: C<4x6>, C<12x18>, C<8.5x11>
182  
183 =item a number representing a reduction (in points) from the next-larger box
184  
185 For example, a 12" x 18" physical sheet to be trimmed down to an 11" x 17" sheet
186 can be specified as follows:
187  
188 # Note: There are 72 points per inch
189 $page->boundaries('media' => '12x18', 'trim' => 0.5 * 72);
190  
191 # Equivalent
192 $page->boundaries('media' => [0, 0, 12 * 72, 18 * 72],
193 'trim' => [0.5 * 72, 0.5 * 72, 11.5 * 72, 17.5 * 72]);
194  
195 This example shows a 12" x 18" physical sheet that will be reduced to a final
196 size of 11" x 17" by trimming 0.5" from each edge. The smaller page boundary is
197 assumed to be centered within the larger one.
198  
199 The "next-larger box" follows this order, stopping at the first defined value:
200  
201 art -> trim -> bleed -> media
202 crop -> media
203  
204 This option isn't available for the media box, since it is by definition, the
205 largest boundary.
206  
207 =item [$width, $height] in points
208  
209 $page->boundaries('media' => [8.5 * 72, 11 * 7.2]);
210  
211 For other page or boundary sizes, the width and height (in points) can be given
212 directly as an array.
213  
214 =item [$x1, $y1, $x2, $y2] in points
215  
216 $page->boundaries('media' => [0, 0, 8.5 * 72, 11 * 72]);
217  
218 Finally, the absolute (raw) coordinates of the bottom-left and top-right corners
219 of a rectangle can be specified.
220  
221 =back
222  
223 =cut
224  
225 # returns ARRAY of 4 elements
226 5 ▶sub _to_rectangle {
Subroutine _to_rectangle: called CC=9 SCAR=30.5
227 5 my $value = shift();
228  
229 # An array of two or four numbers in points
230 5 ▶ if (ref($value) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 0 5 5
231 0 ▶ if (@$value == 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$value == 2) { } 0 0 0
elsif (@$value == 4) { } 0 0 0
232 0 return (0, 0, @$value);
233 } elsif (@$value == 4) {
234 0 return (@$value);
235 }
236 0 croak "Page boundary array must contain two or four numbers";
237 }
238  
239 # WxH in inches
240 5 ▶ if ($value =~ /^([0-9.]+)\s*x\s*([0-9.]+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^([0-9.]+)\s*x\s*([0-9.]+)$/) 2 3 5
241 2 my ($w, $h) = ($1, $2);
242 2 ▶ if (looks_like_number($w) and looks_like_number($h)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($w) and &looks_like_number($h)) 2 0 2
Condition33%1/3
&looks_like_number($w) and &looks_like_number($h)
!ll&&!rl&&r
002
MC/DC0%0/2
&looks_like_number($w) and &looks_like_number($h)
&looks_like_number($w) &looks_like_number($h)
Truth table33%1/3
A and B
A= &looks_like_number($w)B= &looks_like_number($h)
ABresult
0X0
100
111
243 2 return (0, 0, $w * 72, $h * 72);
244 }
245 }
246  
247 # Common names for page sizes
248 3 my %page_sizes = PDF::Builder::Resource::PaperSizes::get_paper_sizes();
249  
250 3 ▶ if ($page_sizes{lc $value}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page_sizes{lc $value}) 3 0 3
251 3 return (0, 0, @{$page_sizes{lc $value}});
252 }
253  
254 0 ▶ if (ref($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) { } 0 0 0
255 0 croak "Unrecognized page size";
256 } else {
257 0 croak "Unrecognized page size: $value";
258 }
259 }
260  
261 =head3 Page Boundaries
262  
263 PDF defines five page boundaries. When creating PDFs for print shops, you'll
264 most commonly use just the media box and trim box. Traditional print shops may
265 also use the bleed box when adding printer's marks and other information.
266  
267 =over
268  
269 =item media
270  
271 The media box defines the boundaries of the physical medium on which the page is
272 to be printed. It may include any extended area surrounding the finished page
273 for bleed, printing marks, or other such purposes. The default value is as
274 defined for PDF, a US letter page (8.5" x 11").
275 B<CAUTION:> Most printers can I<not> print all the way to the edge of the
276 physical medium (paper, etc.). Some space around the edges will be reserved
277 for pinch rollers, etc. to move the paper without smearing around toner or ink.
278  
279 =item crop
280  
281 The crop box defines the region to which the contents of the page shall be
282 clipped (cropped) when displayed or printed. The default value is the page's
283 media box.
284 This is a historical page boundary. You'll likely want to set the bleed and/or
285 trim boxes instead.
286  
287 =item bleed
288  
289 The bleed box defines the region to which the contents of the page shall be
290 clipped when output in a production environment. This may include any extra
291 bleed area needed to accommodate the physical limitations of cutting, folding,
292 and trimming equipment. The actual printed page (media box) may include
293 printing marks that fall outside the bleed box. The default value is the page's
294 crop box.
295  
296 =item trim
297  
298 The trim box defines the intended dimensions of the finished page after
299 trimming. It may be smaller than the media box to allow for production-related
300 content, such as printing instructions, cut marks, or color bars. The default
301 value is the page's crop box.
302  
303 =item art
304  
305 The art box defines the extent of the page's meaningful content (including
306 potential white space) as intended by the page's creator. The default value is
307 the page's crop box.
308  
309 =back
310  
311 =head3 userunit
312  
313 $page->userunit($value)
314  
315 =over
316  
317 Sets the User Unit for this one page.
318 See L<PDF::Builder::Docs/User Units> for more information.
319  
320 =back
321  
322 =cut
323  
324 0 ▶sub userunit {
Subroutine userunit: not called CC=2 SCAR=17.9
Pod documented
325 0 my ($self, $value) = @_;
326  
327 0 ▶ if (float($value) <= 0.0) {
Branch: 1 branch
BranchTrueFalseTotal
if (float($value) <= 0) 0 0 0
328 0 warn "Invalid User Unit value '$value', set to 1.0";
329 0 $value = 1.0;
330 }
331  
332 # assume that even if $value is 1.0, it's being called for some reason
333 # (perhaps overriding non-1.0 global setting)
334 0 $PDF::Builder::global_pdf->verCheckOutput(1.6, "set User Unit");
335 0 $self->{' userUnit'} = float($value); # this is local (page) UU
336 0 $self->{'UserUnit'} = PDFNum(float($value));
337  
338 0 return $self;
339 }
340  
341 107 ▶sub _bbox {
Subroutine _bbox: called CC=21 SCAR=38.0
342 107 my ($box, $self, @corners) = @_;
343 # $box is the box name (e.g., MediaBox)
344 # $self points to the page object
345 # at least one element in @corners if setting. get if no elements.
346  
347 # if 1 or 3 elements in @corners, and [0] contains a letter, it's a name
348 107 my $isName = 0;
349 107 ▶ if (scalar @corners && $corners[0] =~ m/[a-z]/i) { $isName = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @corners and $corners[0] =~ /[a-z]/i) 29 78 107
Condition100%3/3
scalar @corners and $corners[0] =~ /[a-z]/i
!ll&&!rl&&r
582029
MC/DC100%2/2
scalar @corners and $corners[0] =~ /[a-z]/i
scalar @corners $corners[0] =~ /[a-z]/i
Truth table100%3/3
A and B
A= scalar @cornersB= $corners[0] =~ /[a-z]/i
ABresult
0X0
100
111
350  
351 107 ▶ if (scalar @corners == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @corners == 0) { } 58 49 107
elsif (scalar @corners == 3) { } 0 49 49
352 # is a query ('get')
353 # if previously set for this page, just return array
354 # if not set, check parent (PDF), then up chain as far as MediaBox
355 58 ▶ if (defined $self->{$box}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$box}) { } 43 15 58
356 43 return map { $_->val() } $self->{$box}->elements();
357 } else {
358 15 my $pdf = $self->{' api'};
359 15 ▶ if (defined $pdf->{'pages'}->{$box}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pdf->{'pages'}{$box}) { } 11 4 15
360 # parent (global) box is defined
361 11 return map { $_->val() } $pdf->{'pages'}->{$box}->elements();
362 } else {
363 # go up the chain until find defined global
364 4 ▶ if ($box eq 'ArtBox' || $box eq 'TrimBox' || $box eq 'BleedBox') {
Branch: 1 branch
BranchTrueFalseTotal
if ($box eq "ArtBox" or $box eq "TrimBox" or $box eq "BleedBox") 3 1 4
Condition100%6/6
$box eq "ArtBox" or $box eq "TrimBox" or $box eq "BleedBox"
l!l&&r!l&&!r
211
$box eq "ArtBox" or $box eq "TrimBox"
l!l&&r!l&&!r
112
MC/DC100%3/3
$box eq "ArtBox" or $box eq "TrimBox" or $box eq "BleedBox"
$box eq "ArtBox" $box eq "TrimBox" $box eq "BleedBox"
Truth table100%4/4
A or B or C
A= $box eq "ArtBox"B= $box eq "TrimBox"C= $box eq "BleedBox"
ABCresult
1XX1
01X1
0011
0000
365 3 $box = 'CropBox';
366 }
367 4 ▶ if ($box eq 'CropBox' && !defined $pdf->{'pages'}->{'CropBox'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($box eq "CropBox" and not defined $pdf->{"pages"}{"CropBox"}) 4 0 4
Condition33%1/3
$box eq "CropBox" and not defined $pdf->{"pages"}{"CropBox"}
!ll&&!rl&&r
004
MC/DC0%0/2
$box eq "CropBox" and not defined $pdf->{"pages"}{"CropBox"}
$box eq "CropBox" not defined $pdf->{"pages"}{"CropBox"}
Truth table33%1/3
A and not(B)
A= $box eq "CropBox"B= not defined $pdf->{"pages"}{"CropBox"}
ABresult
0X0
100
111
368 4 $box = 'MediaBox';
369 }
370 4 ▶ if ($box ne 'CropBox' && $box ne 'MediaBox') {
Branch: 1 branch
BranchTrueFalseTotal
if ($box ne "CropBox" and $box ne "MediaBox") 0 4 4
Condition33%1/3
$box ne "CropBox" and $box ne "MediaBox"
!ll&&!rl&&r
040
MC/DC0%0/2
$box ne "CropBox" and $box ne "MediaBox"
$box ne "CropBox" $box ne "MediaBox"
Truth table33%1/3
A and B
A= $box ne "CropBox"B= $box ne "MediaBox"
ABresult
0X0
100
111
371 # invalid box name. silent error: just return Media Box
372 0 $box = 'MediaBox';
373 }
374 4 return map { $_->val() } $pdf->{'pages'}->{$box}->elements();
375 }
376 }
377
378 } elsif (scalar @corners == 3) {
379 # have a name and one option (orient)
380 0 my ($name, %opts) = @corners;
381 # copy dashed option names to preferred undashed names
382 0 ▶ if (defined $opts{'-orient'} && !defined $opts{'orient'}) { $opts{'orient'} = delete($opts{'-orient'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"-orient"} and not defined $opts{"orient"}) 0 0 0
Condition0%0/3
defined $opts{"-orient"} and not defined $opts{"orient"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opts{"-orient"} and not defined $opts{"orient"}
defined $opts{"-orient"} not defined $opts{"orient"}
Truth table0%0/3
A and not(B)
A= defined $opts{"-orient"}B= not defined $opts{"orient"}
ABresult
0X0
100
111
383  
384 0 @corners = page_size(($name)); # now 4 numeric values
385 0 ▶ if (defined $opts{'orient'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"orient"}) 0 0 0
386 0 ▶ if ($opts{'orient'} =~ m/^l/i) { # 'landscape' or just 'l'
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"orient"} =~ /^l/i) 0 0 0
387 # 0 0 W H -> 0 0 H W
388 0 my $temp;
389 0 $temp = $corners[2]; $corners[2] = $corners[3]; $corners[3] = $temp;
390 }
391 }
392 } else {
393 # name without [orient] option, or numeric coordinates given
394 49 @corners = page_size(@corners);
395 }
396  
397 # scale down size if User Unit given (e.g., Letter => 0 0 8.5 11)
398 # we have a global userUnit, and possibly a page userUnit overriding it
399 49 ▶ if ($isName) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isName) 29 20 49
400 29 my $UU = $self->{' userUnit'};
401 29 ▶ if ($UU != 1.0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($UU != 1) 0 29 29
402 0 ▶ for (my $i=0; $i<4; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 4) 0 0 0
403 0 $corners[$i] /= $UU;
404 }
405 }
406 }
407  
408 49 $self->{$box} = PDFArray( map { PDFNum(float($_)) } @corners );
409 # return 4 element array of box corners
410 49 return @corners;
411 }
412  
413 =head3 mediabox
414  
415 $page->mediabox($alias)
416  
417 $page->mediabox($alias, 'orient' => 'orientation')
418  
419 $page->mediabox($w,$h)
420  
421 $page->mediabox($llx,$lly, $urx,$ury)
422  
423 ($llx,$lly, $urx,$ury) = $page->mediabox()
424  
425 =over
426  
427 Sets or gets the Media Box for this one page.
428 See L<PDF::Builder::Docs/Media Box> for more information.
429 The method always returns the current bounds (after any set operation).
430  
431 =back
432  
433 =cut
434  
435 71 ▶sub mediabox {
Subroutine mediabox: called CC=1 SCAR=0.0
Pod documented
436 71 return _bbox('MediaBox', @_);
437 }
438  
439 #=head4 get_mediabox
440 #
441 # ($llx,$lly, $urx,$ury) = $page->get_mediabox()
442 #
443 #=over
444 #
445 #Gets the Media Box corner coordinates based on best estimates or the default.
446 #These are in the order given in a mediabox call (4 coordinates).
447 #
448 #This method is B<Deprecated>, and has been B<removed>. Use
449 #the global (C<$pdf>) or page (C<$page>) mediabox() call with no parameters
450 #instead.
451 #
452 #=back
453 #
454 #=cut
455 #
456 #sub get_mediabox {
457 # my $self = shift();
458 # return $self->mediabox();
459 #}
460  
461 =head3 cropbox
462  
463 $page->cropbox($alias)
464  
465 $page->cropbox($alias, 'orient' => 'orientation')
466  
467 $page->cropbox($w,$h)
468  
469 $page->cropbox($llx,$lly, $urx,$ury)
470  
471 ($llx,$lly, $urx,$ury) = $page->cropbox()
472  
473 =over
474  
475 Sets or gets the Crop Box for this one page.
476 See L<PDF::Builder::Docs/Crop Box> for more information.
477 The method always returns the current bounds (after any set operation).
478  
479 =back
480  
481 =cut
482  
483 9 ▶sub cropbox {
Subroutine cropbox: called CC=1 SCAR=0.0
Pod documented
484 9 return _bbox('CropBox', @_);
485 }
486  
487 #=head4 get_cropbox
488 #
489 # ($llx,$lly, $urx,$ury) = $page->get_cropbox()
490 #
491 #=over
492 #
493 #Gets the Crop Box based on best estimates or the default.
494 #
495 #This method is B<Deprecated>, and has been B<removed>. Use
496 #the global (C<$pdf>) or page (C<$page>) cropbox() call with no parameters
497 #instead.
498 #
499 #=back
500 #
501 #=cut
502 #
503 #sub get_cropbox {
504 # my $self = shift();
505 # return $self->cropbox();
506 #}
507  
508 =head3 bleedbox
509  
510 $page->bleedbox($alias)
511  
512 $page->bleedbox($alias, 'orient' => 'orientation')
513  
514 $page->bleedbox($w,$h)
515  
516 $page->bleedbox($llx,$lly, $urx,$ury)
517  
518 ($llx,$lly, $urx,$ury) = $page->bleedbox()
519  
520 =over
521  
522 Sets or gets or gets the Bleed Box for this one page.
523 See L<PDF::Builder::Docs/Bleed Box> for more information.
524 The method always returns the current bounds (after any set operation).
525  
526 =back
527  
528 =cut
529  
530 9 ▶sub bleedbox {
Subroutine bleedbox: called CC=1 SCAR=0.0
Pod documented
531 9 return _bbox('BleedBox', @_);
532 }
533  
534 #=head4 get_bleedbox
535 #
536 # ($llx,$lly, $urx,$ury) = $page->get_bleedbox()
537 #
538 #=over
539 #
540 #Gets the Bleed Box based on best estimates or the default.
541 #
542 #This method is B<Deprecated>, and has been B<removed>. Use
543 #the global (C<$pdf>) or page (C<$page>) bleedbox() call with no parameters
544 #instead.
545 #
546 #=back
547 #
548 #=cut
549 #
550 #sub get_bleedbox {
551 # my $self = shift();
552 # return $self->bleedbox();
553 #}
554  
555 =head3 trimbox
556  
557 $page->trimbox($alias)
558  
559 $page->trimbox($alias, 'orient' => 'orientation')
560  
561 $page->trimbox($w,$h)
562  
563 $page->trimbox($llx,$lly, $urx,$ury)
564  
565 ($llx,$lly, $urx,$ury) = $page->trimbox()
566  
567 =over
568  
569 Sets or gets the Trim Box for this one page.
570 See L<PDF::Builder::Docs/Trim Box> for more information.
571 The method always returns the current bounds (after any set operation).
572  
573 =back
574  
575 =cut
576  
577 9 ▶sub trimbox {
Subroutine trimbox: called CC=1 SCAR=0.0
Pod documented
578 9 return _bbox('TrimBox', @_);
579 }
580  
581 #=head4 get_trimbox
582 #
583 # ($llx,$lly, $urx,$ury) = $page->get_trimbox()
584 #
585 #=over
586 #
587 #Gets the Trim Box based on best estimates or the default.
588 #
589 #This method is B<Deprecated>, and has been B<removed>. Use
590 #the global (C<$pdf>) or page (C<$page>) trimbox() call with no parameters
591 #instead.
592 #
593 #=back
594 #
595 #=cut
596 #
597 #sub get_trimbox {
598 # my $self = shift();
599 # return $self->trimbox();
600 #}
601  
602 =head3 artbox
603  
604 $page->artbox($alias)
605  
606 $page->artbox($alias, 'orient' => 'orientation')
607  
608 $page->artbox($w,$h)
609  
610 $page->artbox($llx,$lly, $urx,$ury)
611  
612 ($llx,$lly, $urx,$ury) = $page->artbox()
613  
614 =over
615  
616 Sets or gets the Art Box for this one page.
617 See L<PDF::Builder::Docs/Art Box> for more information.
618 The method always returns the current bounds (after any set operation).
619  
620 =back
621  
622 =cut
623  
624 9 ▶sub artbox {
Subroutine artbox: called CC=1 SCAR=0.0
Pod documented
625 9 return _bbox('ArtBox', @_);
626 }
627  
628 #=head4 get_artbox
629 #
630 # ($llx,$lly, $urx,$ury) = $page->get_artbox()
631 #
632 #=over
633 #
634 #Gets the Art Box based on best estimates or the default.
635 #
636 #This method is B<Deprecated>, and has been B<removed>. Use
637 #the global (C<$pdf>) or page (C<$page>) artbox() call with no parameters
638 #instead.
639 #
640 #=back
641 #
642 #=cut
643 #
644 #sub get_artbox {
645 # my $self = shift();
646 # return $self->artbox();
647 #}
648  
649 =head3 size
650  
651 $page->size($size) # Set
652  
653 @rectangle = $page->size() # Get
654  
655 =over
656  
657 Set the physical page size or return the coordinates of the rectangle enclosing
658 the physical page size.
659 This is an alternate method provided for compatibility with PDF::API2.
660  
661 # Set the physical page (media) size using a common size name
662 $page->size('letter');
663  
664 # Set the page size using coordinates in points (X1, Y1, X2, Y2)
665 $page->size([0, 0, 612, 792]);
666  
667 # Get the page coordinates in points
668 my @rectangle = $page->size();
669  
670 See Page Sizes above for possible values.
671 The size method is a convenient shortcut for setting the PDF's media box when
672 other prepress (print-related) page boundaries aren't required. It's equivalent
673 to the following:
674  
675 # Set
676 $page = $page->boundaries('media' => $size);
677  
678 # Get
679 @rectangle = $page->boundaries()->{'media'}->@*;
680  
681 =back
682  
683 =cut
684
685 1 ▶sub size {
Subroutine size: called CC=2 SCAR=9.2
Pod documented
686 1 my $self = shift;
687  
688 1 ▶ if (@_) { # Set (also returns object, for easy chaining)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1 0 1
689 1 return $self->boundaries('media' => @_);
690 } else { # Get
691 0 my %boundaries = $self->boundaries();
692 0 return @{ $boundaries{'media'} };
693 }
694 }
695  
696 =head3 boundaries
697  
698 $page = $page->boundaries(%boundaries)
699  
700 %boundaries = $page->boundaries()
701  
702 =over
703  
704 Set prepress page boundaries to facilitate printing. Returns the current page
705 boundaries if called without arguments.
706 This is an alternate method provided for compatibility with PDF::API2.
707  
708 # Set
709 %boundaries = $page->boundaries(
710 # 13x19 inch physical sheet size
711 'media' => '13x19',
712 # sheet content is 11x17 with 0.25" bleed
713 'bleed' => [0.75 * 72, 0.75 * 72, 12.25 * 72, 18.25 * 72],
714 # 11x17 final trimmed size
715 'trim' => 0.25 * 72,
716 );
717  
718 The C<%boundaries> hash contains one or more page boundary keys (see Page
719 Boundaries) to set or replace, each with a corresponding size (see Page Sizes).
720 The resulting boundaries hash will be returned.
721  
722 # Get
723 %boundaries = $page->boundaries();
724 ($x1,$y1, $x2,$y2) = $page->boundaries('trim');
725  
726 If called without arguments, the returned hashref will contain (Get) all five
727 boundaries. If called with one string argument, it returns the coordinates for
728 the specified page (media, crop, bleed, trim, or art) boundary. If more than
729 one boundary type is given, only the first is processed, and a warning is
730 given that the remainder are ignored.
731  
732 =back
733  
734 =cut
735  
736 5 ▶sub boundaries {
Subroutine boundaries: called CC=19 SCAR=36.2
Pod documented
737 5 my $self = shift();
738  
739 # Get
740 5 ▶ if (@_ == 0) { # empty list -- do all boxes
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 0) { } 0 5 5
elsif (@_ == 1) { } 0 5 5
741 0 my %boundaries;
742 0 foreach my $box (qw( Media Crop Bleed Trim Art )) {
743 0 my @xxx = $self->_bounding_box($box . 'Box');
744 0 $boundaries{lc($box)} = \@xxx;
745 }
746 0 return %boundaries;
747 } elsif (@_ == 1) { # request one specific box
748 0 my $box = shift();
749 # apparently it is normal to have an array of boxes, and use only first
750 #if (@_) { # more than one box in arg list?
751 # carp "More than one box requested for boundaries(). Using only first ($box)";
752 #}
753 0 my @coordinates = $self->_bounding_box(ucfirst($box) . 'Box');
754 0 return @coordinates;
755 }
756  
757 # Set
758 5 my %boxes = @_; # should be even count
759 5 foreach my $box (qw( media crop bleed trim art )) {
760 25 ▶ next unless exists $boxes{$box};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $boxes{$box} 18 7 25
761  
762 # Special case: A single number as the value for anything other than
763 # MediaBox means to take the next larger size and reduce it by this
764 # amount in points on all four sides, provided the larger size was also
765 # included.
766 7 my $value = $boxes{$box};
767 7 my @rectangle;
768 7 ▶ if ($box ne 'media' and not ref($value) and looks_like_number($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($box ne 'media' and not ref $value and &looks_like_number($value)) { } 2 5 7
Condition66%4/6
$box ne "media" and not ref $value and &looks_like_number($value)
!ll&&!rl&&r
502
$box ne "media" and not ref $value
!ll&&!rl&&r
502
MC/DC33%1/3
$box ne "media" and not ref $value and &looks_like_number($value)
$box ne "media" not ref $value &looks_like_number($value)
Truth table50%2/4
A and not(B) and C
A= $box ne "media"B= not ref $valueC= &looks_like_number($value)
ABCresult
0XX0
10X0
1100
1111
769 2 ▶ my $parent = ($box eq 'crop' ? 'media' :
Branch: 3 branches
BranchTrueFalseTotal
$box eq 'crop' ? : 0 2 2
$box eq 'bleed' ? : 0 2 2
$box eq 'trim' ? : 2 0 2
770 $box eq 'bleed' ? 'media' :
771 $box eq 'trim' ? 'bleed' : 'trim');
772 2 ▶ $parent = 'bleed' if $parent eq 'trim' and not $boxes{'trim'};
Branch: 1 branch
BranchTrueFalseTotal
if $parent eq "trim" and not $boxes{"trim"} 0 2 2
Condition33%1/3
$parent eq "trim" and not $boxes{"trim"}
!ll&&!rl&&r
200
MC/DC0%0/2
$parent eq "trim" and not $boxes{"trim"}
$parent eq "trim" not $boxes{"trim"}
Truth table33%1/3
A and not(B)
A= $parent eq "trim"B= not $boxes{"trim"}
ABresult
0X0
100
111
773 2 ▶ $parent = 'media' if $parent eq 'bleed' and not $boxes{'bleed'};
Branch: 1 branch
BranchTrueFalseTotal
if $parent eq "bleed" and not $boxes{"bleed"} 2 0 2
Condition33%1/3
$parent eq "bleed" and not $boxes{"bleed"}
!ll&&!rl&&r
002
MC/DC0%0/2
$parent eq "bleed" and not $boxes{"bleed"}
$parent eq "bleed" not $boxes{"bleed"}
Truth table33%1/3
A and not(B)
A= $parent eq "bleed"B= not $boxes{"bleed"}
ABresult
0X0
100
111
774 2 ▶ $parent = 'media' if $parent eq 'bleed' and not $boxes{'bleed'};
Branch: 1 branch
BranchTrueFalseTotal
if $parent eq "bleed" and not $boxes{"bleed"} 0 2 2
Condition33%1/3
$parent eq "bleed" and not $boxes{"bleed"}
!ll&&!rl&&r
200
MC/DC0%0/2
$parent eq "bleed" and not $boxes{"bleed"}
$parent eq "bleed" not $boxes{"bleed"}
Truth table33%1/3
A and not(B)
A= $parent eq "bleed"B= not $boxes{"bleed"}
ABresult
0X0
100
111
775 2 ▶ unless ($boxes{$parent}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($boxes{$parent}) 0 2 2
776 0 croak "Single-number argument for $box requires $parent";
777 }
778  
779 2 @rectangle = @{$boxes{$parent}};
780 2 $rectangle[0] += $value;
781 2 $rectangle[1] += $value;
782 2 $rectangle[2] -= $value;
783 2 $rectangle[3] -= $value;
784 } else {
785 5 @rectangle = _to_rectangle($value);
786 }
787  
788 7 my $box_name = ucfirst($box) . 'Box';
789 7 $self->_bounding_box($box_name, @rectangle);
790 7 $boxes{$box} = [@rectangle];
791 }
792  
793 5 return %boxes;
794 }
795  
796 8 ▶sub _bounding_box {
Subroutine _bounding_box: called CC=5 SCAR=18.5
797 8 my $self = shift();
798 8 my $type = shift();
799  
800 # Get
801 8 ▶ unless (scalar @_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @_) 4 4 8
802 4 my $box = $self->find_prop($type);
803 4 ▶ unless ($box) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($box) 0 4 4
804 # Default to letter (for historical PDF::API2 reasons, not per the
805 # PDF specification)
806 0 ▶ return (0, 0, 612, 792) if $type eq 'MediaBox';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "MediaBox" 0 0 0
807  
808 # Use defaults per PDF 1.7 section 14.11.2 Page Boundaries
809 0 ▶ return $self->_bounding_box('MediaBox') if $type eq 'CropBox';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "CropBox" 0 0 0
810 0 return $self->_bounding_box('CropBox');
811 }
812 4 return map { $_->val() } $box->elements();
813 }
814  
815 # Set
816 4 $self->{$type} = PDFArray(map { PDFNum(float($_)) } page_size(@_));
817 4 return $self;
818 }
819  
820 159 ▶sub fixcontents {
Subroutine fixcontents: called CC=4 SCAR=15.5
Pod undocumented
821 159 my ($self) = @_;
822  
823 159 ▶ $self->{'Contents'} = $self->{'Contents'} || PDFArray();
Condition66%2/3
$self->{'Contents'} || PDFArray()
l!l&&r!l&&!r
91500
MC/DC0%0/2
$self->{'Contents'} || PDFArray()
$self->{'Contents'} PDFArray()
Truth table66%2/3
A || B
A= $self->{'Contents'}B= PDFArray()
ABresult
1X1
011
000
824 159 ▶ if (ref($self->{'Contents'}) =~ /Objind$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($self->{"Contents"}) =~ /Objind$/) 0 159 159
825 0 $self->{'Contents'}->realise();
826 }
827 159 ▶ if (ref($self->{'Contents'}) !~ /Array$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref($self->{"Contents"}) =~ /Array$/) 0 159 159
828 0 $self->{'Contents'} = PDFArray($self->{'Contents'});
829 }
830 159 return;
831 }
832  
833 152 ▶sub content {
Subroutine content: called CC=4 SCAR=14.6
Pod undocumented
834 152 my ($self, $obj, $dir) = @_;
835  
836 152 ▶ if (defined($dir) && $dir > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dir and $dir > 0) { } 0 152 152
Condition33%1/3
defined $dir and $dir > 0
!ll&&!rl&&r
01520
MC/DC0%0/2
defined $dir and $dir > 0
defined $dir $dir > 0
Truth table33%1/3
A and B
A= defined $dirB= $dir > 0
ABresult
0X0
100
111
837 0 $self->precontent($obj);
838 } else {
839 152 $self->addcontent($obj);
840 }
841 152 ▶ $self->{' apipdf'}->new_obj($obj) unless $obj->is_obj($self->{' apipdf'});
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->is_obj($self->{" apipdf"}) 152 0 152
842 152 $obj->{' apipdf'} = $self->{' apipdf'};
843 152 $obj->{' api'} = $self->{' api'};
844 152 $obj->{' apipage'} = $self;
845  
846 152 weaken $obj->{' apipdf'};
847 152 weaken $obj->{' api'};
848 152 weaken $obj->{' apipage'};
849  
850 152 return $obj;
851 }
852  
853 152 ▶sub addcontent {
Subroutine addcontent: called CC=1 SCAR=0.0
Pod undocumented
854 152 my ($self, @objs) = @_;
855  
856 152 $self->fixcontents();
857 152 $self->{'Contents'}->add_elements(@objs);
858 152 return;
859 }
860  
861 0 ▶sub precontent {
Subroutine precontent: not called CC=1 SCAR=6.9
Pod undocumented
862 0 my ($self, @objs) = @_;
863  
864 0 $self->fixcontents();
865 0 unshift(@{$self->{'Contents'}->val()}, @objs);
866 0 return;
867 }
868  
869 =head2 Page Operation Methods
870  
871 =head3 gfx, graphics
872  
873 $gfx = $page->gfx(%opts)
874  
875 $gfx = $page->gfx($prepend)
876  
877 $gfx = $page->gfx()
878  
879 =over
880  
881 Returns a graphics content object, for drawing paths and shapes.
882  
883 You may specify the "prepend" flag in the old or new way. The old way is to
884 give a single boolean value (0 false, non-zero true). The new way is to give
885 a hash element named 'prepend', with the same values.
886  
887 =over
888  
889 =item gfx(boolean_value $prepend)
890  
891 =item gfx('prepend' => boolean_value)
892  
893 =back
894  
895 If $prepend is I<true>, or the option 'prepend' is given with a I<true> value,
896 the content will be prepended to the page description (at the beginning of
897 the page's content stream).
898 Otherwise, it will be appended.
899 The default is I<false>.
900  
901 =over
902  
903 =item gfx('compress' => boolean_value)
904  
905 =back
906  
907 You may specify a compression flag saying whether the drawing instructions
908 are to be compressed. If not given, the default is for the overall PDF
909 compression setting to be used (I<on> by default).
910  
911 You may have more than one I<gfx> object. They and I<text> objects will be
912 output as objects and streams I<in the order B<that the objects are>
913 defined>, with all actions pertaining
914 to this I<gfx> object appearing in one stream. However, note that graphics
915 and text objects are not fully independent of each other: the exit state
916 (linewidth, strokecolor, etc.) of one object is the entry state of the next
917 object in line to be output, and so on.
918  
919 If you intermix multiple I<gfx> and I<text> objects on
920 a page, the results may be confusing. Say you have $gfx1, $text1, $gfx2, and
921 $text2 on your page (I<created in that order>). PDF::Builder will output all the
922 $gfx1->I<action> calls in one stream, then all the $text1->I<action> calls in
923 the next stream, and likewise for $gfx2 usage and finally $text2.
924  
925 Then it's PDF's turn to confuse you. PDF will process the entire $gfx1 object
926 stream, accumulating the graphics state to the end of the stream, and using
927 that as the entry state into $text1. In a similar manner, $gfx2 and $text2 are
928 read, processed, and rendered. Thus, a change in, say, the dash pattern in the
929 middle of $gfx1, I<after> you have output some $gfx2, $text1, and $text2
930 material, may suddenly show up at the beginning of $text1 (and continue through
931 $gfx2 and $text2)!
932  
933 It is possible to use multiple graphics objects, to avoid having to change
934 settings constantly, but you may want to consider resetting all your settings
935 at the first call to each object, so that you are starting from a known base.
936 This may most easily be done by using $I<type>->restore() and ->save() just
937 after creating $I<type>:
938  
939 $text1 = $page->text();
940 $text1->save();
941 $grfx1 = $page->gfx();
942 $grfx1->restore();
943 $grfx1->save();
944 $text2 = $page->text();
945 $text2->restore();
946 $text2->save();
947 $grfx2 = $page->gfx();
948 $grfx1->restore();
949  
950 I<Generally,> you will want to define the graphics object first, and then the
951 text object after that. This defines the order in which the streams will be
952 rendered (graphics first), which is usually desirable if you're setting a
953 background color, or have other graphics with which you want to overlay text
954 over. Sometimes, though, you may wish to overlay text with graphics, in which
955 case you might either define the objects text and then graphics, or define a
956 second graphics stream to lay over the text. Most of the time it really doesn't
957 matter which comes first (as text and graphics don't interact or overlay), but
958 in any case, be aware of states carried over from the end of one stream
959 into the next.
960  
961 B<Alternate name:> C<graphics>
962  
963 This has been added for PDF::API2 compatibility.
964  
965 =back
966  
967 =cut
968  
969 3 ▶sub graphics { return gfx(@_); } ## no critic
Subroutine graphics: called CC=1 SCAR=0.0
Pod documented
970  
971 133 ▶sub gfx {
Subroutine gfx: called CC=14 SCAR=41.6
Pod documented
972 133 my ($self, @params) = @_;
973  
974 133 my ($prepend, $compress);
975 133 $prepend = $compress = 0; # default for both is False
976 133 ▶ if (scalar @params == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (scalar @params == 0) { } 133 0 133
elsif (scalar @params == 1) { } 0 0 0
elsif (scalar(@params) % 2) { } 0 0 0
977 # gfx() call. no change
978 } elsif (scalar @params == 1) {
979 # one scalar value, $prepend
980 0 $prepend = $params[0];
981 } elsif ((scalar @params)%2) {
982 # odd number of values, can't be a hash list
983 0 carp "Invalid parameters passed to gfx or graphics call!";
984 } else {
985 # hash list with at least one element
986 0 my %hash = @params;
987 # copy dashed hash names to preferred undashed names
988 0 ▶ if (defined $hash{'-prepend'} && !defined $hash{'prepend'}) { $hash{'prepend'} = delete($hash{'-prepend'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash{"-prepend"} and not defined $hash{"prepend"}) 0 0 0
Condition0%0/3
defined $hash{"-prepend"} and not defined $hash{"prepend"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $hash{"-prepend"} and not defined $hash{"prepend"}
defined $hash{"-prepend"} not defined $hash{"prepend"}
Truth table0%0/3
A and not(B)
A= defined $hash{"-prepend"}B= not defined $hash{"prepend"}
ABresult
0X0
100
111
989 0 ▶ if (defined $hash{'-compress'} && !defined $hash{'compress'}) { $hash{'compress'} = delete($hash{'-compress'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash{"-compress"} and not defined $hash{"compress"}) 0 0 0
Condition0%0/3
defined $hash{"-compress"} and not defined $hash{"compress"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $hash{"-compress"} and not defined $hash{"compress"}
defined $hash{"-compress"} not defined $hash{"compress"}
Truth table0%0/3
A and not(B)
A= defined $hash{"-compress"}B= not defined $hash{"compress"}
ABresult
0X0
100
111
990  
991 0 ▶ if (defined $hash{'prepend'}) { $prepend = $hash{'prepend'}; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash{"prepend"}) 0 0 0
992 0 ▶ if (defined $hash{'compress'}) { $compress = $hash{'compress'}; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash{"compress"}) 0 0 0
993 }
994 0 ▶ if ($prepend) { $prepend = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($prepend) 0 133 133
995 133 ▶ if ($self->{' api'}->{'forcecompress'} eq 'flate' ||
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{" api"}{"forcecompress"} eq "flate" or $self->{" api"}{"forcecompress"} =~ /^[1-9]\d*$/) 11 122 133
Condition66%2/3
$self->{" api"}{"forcecompress"} eq "flate" or $self->{" api"}{"forcecompress"} =~ /^[1-9]\d*$/
l!l&&r!l&&!r
110122
MC/DC50%1/2
$self->{" api"}{"forcecompress"} eq "flate" or $self->{" api"}{"forcecompress"} =~ /^[1-9]\d*$/
$self->{" api"}{"forcecompress"} eq "flate" $self->{" api"}{"forcecompress"} =~ /^[1-9]\d*$/
Truth table66%2/3
A or B
A= $self->{" api"}{"forcecompress"} eq "flate"B= $self->{" api"}{"forcecompress"} =~ /^[1-9]\d*$/
ABresult
1X1
011
000
996 11 $self->{' api'}->{'forcecompress'} =~ m/^[1-9]\d*$/) { $compress = 1; }
997  
998 133 my $gfx = PDF::Builder::Content->new();
999 133 ▶ $gfx->compressFlate() if $compress;
Branch: 1 branch
BranchTrueFalseTotal
if $compress 11 122 133
1000 133 $self->content($gfx, $prepend);
1001  
1002 133 return $gfx;
1003 }
1004  
1005 =head3 text
1006  
1007 $text = $page->text(%opts)
1008  
1009 $text = $page->text($prepend)
1010  
1011 $text = $page->text()
1012  
1013 =over
1014  
1015 Returns a text content object, for writing text.
1016 See L<PDF::Builder::Content> for details.
1017  
1018 You may specify the "prepend" flag in the old or new way. The old way is to
1019 give a single boolean value (0 false, non-zero true). The new way is to give
1020 a hash element named 'prepend', with the same values.
1021  
1022 =over
1023  
1024 =item text(boolean_value $prepend)
1025  
1026 =item text('prepend' => boolean_value)
1027  
1028 =back
1029  
1030 If $prepend is I<true>, or the option 'prepend' is given with a I<true> value,
1031 the content will be prepended to the page description (at the beginning of
1032 the page's content stream).
1033 Otherwise, it will be appended.
1034 The default is I<false>.
1035  
1036 =over
1037  
1038 =item text('compress' => boolean_value)
1039  
1040 =back
1041  
1042 You may specify a compression flag saying whether the text content is
1043 to be compressed. If not given, the default is for the overall PDF
1044 compression setting to be used (I<on> by default).
1045  
1046 Please see the discussion above in C<gfx()> regarding multiple graphics and
1047 text objects on one page, how they are grouped into PDF objects and streams,
1048 and the rendering consequences of running through one entire object (stream)
1049 at a time, before moving on to the next. Even if you have only one graphics
1050 and one text stream, the order in which they are defined has consequences for
1051 how text overlays graphics or vice-versa.
1052  
1053 The I<text> object has many settings and attributes of its own, but shares many
1054 with graphics (I<gfx>), such as strokecolor, fillcolor, linewidth, linedash,
1055 and the like. Thus there is some overlap in attributes, and graphics and text
1056 calls can affect each other.
1057  
1058 =back
1059  
1060 =cut
1061  
1062 19 ▶sub text {
Subroutine text: called CC=14 SCAR=41.6
Pod documented
1063 19 my ($self, @params) = @_;
1064  
1065 19 my ($prepend, $compress);
1066 19 $prepend = $compress = 0; # default for both is False
1067 19 ▶ if (scalar @params == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (scalar @params == 0) { } 19 0 19
elsif (scalar @params == 1) { } 0 0 0
elsif (scalar(@params) % 2) { } 0 0 0
1068 # text() call. no change
1069 } elsif (scalar @params == 1) {
1070 # one scalar value, $prepend
1071 0 $prepend = $params[0];
1072 } elsif ((scalar @params)%2) {
1073 # odd number of values, can't be a hash list
1074 0 carp "Invalid parameters passed to text() call!";
1075 } else {
1076 # hash list with at least one element
1077 0 my %hash = @params;
1078 # copy dashed hash names to preferred undashed names
1079 0 ▶ if (defined $hash{'-prepend'} && !defined $hash{'prepend'}) { $hash{'prepend'} = delete($hash{'-prepend'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash{"-prepend"} and not defined $hash{"prepend"}) 0 0 0
Condition0%0/3
defined $hash{"-prepend"} and not defined $hash{"prepend"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $hash{"-prepend"} and not defined $hash{"prepend"}
defined $hash{"-prepend"} not defined $hash{"prepend"}
Truth table0%0/3
A and not(B)
A= defined $hash{"-prepend"}B= not defined $hash{"prepend"}
ABresult
0X0
100
111
1080 0 ▶ if (defined $hash{'-compress'} && !defined $hash{'compress'}) { $hash{'compress'} = delete($hash{'-compress'}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash{"-compress"} and not defined $hash{"compress"}) 0 0 0
Condition0%0/3
defined $hash{"-compress"} and not defined $hash{"compress"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $hash{"-compress"} and not defined $hash{"compress"}
defined $hash{"-compress"} not defined $hash{"compress"}
Truth table0%0/3
A and not(B)
A= defined $hash{"-compress"}B= not defined $hash{"compress"}
ABresult
0X0
100
111
1081  
1082 0 ▶ if (defined $hash{'prepend'}) { $prepend = $hash{'prepend'}; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash{"prepend"}) 0 0 0
1083 0 ▶ if (defined $hash{'compress'}) { $compress = $hash{'compress'}; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash{"compress"}) 0 0 0
1084 }
1085 0 ▶ if ($prepend) { $prepend = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($prepend) 0 19 19
1086 19 ▶ if ($self->{' api'}->{'forcecompress'} eq 'flate' ||
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{" api"}{"forcecompress"} eq "flate" or $self->{" api"}{"forcecompress"} =~ /^[1-9]\d*$/) 5 14 19
Condition66%2/3
$self->{" api"}{"forcecompress"} eq "flate" or $self->{" api"}{"forcecompress"} =~ /^[1-9]\d*$/
l!l&&r!l&&!r
5014
MC/DC50%1/2
$self->{" api"}{"forcecompress"} eq "flate" or $self->{" api"}{"forcecompress"} =~ /^[1-9]\d*$/
$self->{" api"}{"forcecompress"} eq "flate" $self->{" api"}{"forcecompress"} =~ /^[1-9]\d*$/
Truth table66%2/3
A or B
A= $self->{" api"}{"forcecompress"} eq "flate"B= $self->{" api"}{"forcecompress"} =~ /^[1-9]\d*$/
ABresult
1X1
011
000
1087 5 $self->{' api'}->{'forcecompress'} =~ m/^[1-9]\d*$/) { $compress = 1; }
1088  
1089 19 my $text = PDF::Builder::Content::Text->new();
1090 19 ▶ $text->compressFlate() if $compress;
Branch: 1 branch
BranchTrueFalseTotal
if $compress 5 14 19
1091 19 $self->content($text, $prepend);
1092  
1093 19 return $text;
1094 }
1095  
1096 =head3 rotate, rotation
1097  
1098 $page->rotate($deg)
1099  
1100 =over
1101  
1102 Rotates the page by the given degrees, which must be a multiple of 90.
1103 An angle that is not a multiple of 90 will be rounded to the nearest 90
1104 degrees, with a message.
1105  
1106 Note that the rotation angle is I<clockwise> for a positive amount!
1107 E.g., a rotation of +90 (or -270) will have the bottom edge of the paper at
1108 the left of the screen.
1109 After rotating the page 180 degrees, C<[0, 0]> (originally lower left corner)
1110 will be be in the top right corner of the page, rather than the bottom left.
1111 X will increase to the right, and Y will increase downward.
1112  
1113 (This allows you to auto-rotate to landscape without changing the mediabox!
1114 There are other ways to accomplish this end, such as using the C<size()>
1115 method, which will not change the coordinate system (move the origin).)
1116  
1117 B<Note> that some users have reported problems with using C<rotate>, that
1118 the dimensions were limited to the smaller of the original height or width. If
1119 you experience this, be sure to check whether you are doing some sort of I<crop
1120 box> or other clipping, that might not rotate as expected with the rest of the
1121 page. In other words, you might need to manually adjust the crop box dimensions.
1122  
1123 Do not confuse this C<rotate()> call with the I<graphics context> rotation
1124 (Content.pm) C<rotate()>, which permits any angle, is of opposite direction,
1125 and does not shift the origin!
1126  
1127 B<Alternate name:> C<rotation>
1128  
1129 This has been added for PDF::API2 compatibility.
1130  
1131 =back
1132  
1133 =cut
1134  
1135 0 ▶sub rotation { return rotate(@_); } ## no critic
Subroutine rotation: not called CC=1 SCAR=6.9
Pod documented
1136  
1137 0 ▶sub rotate {
Subroutine rotate: not called CC=3 SCAR=24.8
Pod documented
1138 0 my ($self, $degrees) = @_;
1139  
1140 0 ▶ $degrees //= 0;
Condition0%0/2
$degrees //= 0
l!l
00
MC/DC0%0/1
$degrees //= 0
$degrees
Truth table0%0/2
A
A= $degrees
Aresult
11
00
1141 # Ignore rotation of 360 or more (in either direction)
1142 0 $degrees = $degrees % 360; # range [0, 360)
1143  
1144 0 ▶ if ($degrees % 90) {
Branch: 1 branch
BranchTrueFalseTotal
if ($degrees % 90) 0 0 0
1145 0 my $deg = int(($degrees + 45)/90)*90;
1146 0 carp "page rotate($degrees) invalid, not multiple of 90 degrees.\nChanged to $deg";
1147 0 $degrees = $deg;
1148 }
1149  
1150 0 $self->{'Rotate'} = PDFNum($degrees);
1151  
1152 0 return $self;
1153 }
1154  
1155 =head3 object
1156  
1157 $page = $page->object($object, $x,$y, $scale_x,$scale_y)
1158  
1159 =over
1160  
1161 Places an image or other external object (a.k.a. XObject) on the page in the
1162 specified location.
1163  
1164 If C<$x> and C<$y> are omitted, the object will be placed at C<[0, 0]>.
1165  
1166 For images, C<$scale_x> and C<$scale_y> represent the width and height of the
1167 image on the page in points. If C<$scale_x> is omitted, it will default to 72
1168 pixels per inch. If C<$scale_y> is omitted, the image will be scaled
1169 proportionally based on the image dimensions.
1170  
1171 For other external objects, the scale is a multiplier, where 1 (the default)
1172 represents 100% (i.e. no change).
1173  
1174 If the object to be placed depends on a coordinate transformation (e.g. rotation
1175 or skew), first create a content object using L</"graphics">, then call
1176 L<PDF::Builder::Content/"object"> after making the appropriate transformations.
1177  
1178 =back
1179  
1180 =cut
1181  
1182 0 ▶sub object {
Subroutine object: not called CC=1 SCAR=6.9
Pod documented
1183 0 my $self = shift();
1184 0 $self->graphics()->object(@_);
1185 0 return $self;
1186 }
1187  
1188 =head3 annotation
1189  
1190 $ant = $page->annotation()
1191  
1192 =over
1193  
1194 Returns a new annotation object.
1195  
1196 =back
1197  
1198 =cut
1199  
1200 6 ▶sub annotation {
Subroutine annotation: called CC=4 SCAR=13.9
Pod documented
1201 6 my $self = shift;
1202  
1203 6 ▶ unless (exists $self->{'Annots'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->{'Annots'}) { } 1 5 6
if (do { ref($self->{"Annots"}) =~ /Objind/ }) 1 0 1
1204 5 $self->{'Annots'} = PDFArray();
1205 5 $self->{' apipdf'}->out_obj($self);
1206 0 } elsif (ref($self->{'Annots'}) =~ /Objind/) {
1207 1 $self->{'Annots'}->realise();
1208 }
1209  
1210 6 require PDF::Builder::Annotation;
1211 6 my $ant = PDF::Builder::Annotation->new();
1212 6 $self->{'Annots'}->add_elements($ant);
1213 6 $self->{' apipdf'}->new_obj($ant);
1214 6 $ant->{' apipdf'} = $self->{' apipdf'};
1215 6 $ant->{' apipage'} = $self;
1216 6 weaken $ant->{' apipdf'};
1217 6 weaken $ant->{' apipage'};
1218  
1219 6 ▶ if ($self->{'Annots'}->is_obj($self->{' apipdf'})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Annots"}->is_obj($self->{" apipdf"})) 1 5 6
1220 1 $self->{' apipdf'}->out_obj($self->{'Annots'});
1221 }
1222  
1223 6 return $ant;
1224 }
1225  
1226 =head3 resource
1227  
1228 $page->resource($type, $key, $obj)
1229  
1230 =over
1231  
1232 Adds a resource to the page-inheritance tree.
1233  
1234 B<Example:>
1235  
1236 $co->resource('Font', $fontkey, $fontobj);
1237 $co->resource('XObject', $imagekey, $imageobj);
1238 $co->resource('Shading', $shadekey, $shadeobj);
1239 $co->resource('ColorSpace', $spacekey, $speceobj);
1240  
1241 B<Note:> You only have to add the required resources if
1242 they are NOT handled by the *font*, *image*, *shade* or *space*
1243 methods.
1244  
1245 =back
1246  
1247 =cut
1248  
1249 32 ▶sub resource {
Subroutine resource: called CC=13 SCAR=29.1
Pod documented
1250 32 my ($self, $type, $key, $obj, $force) = @_;
1251  
1252 32 my $dict = $self->find_prop('Resources');
1253  
1254 32 ▶ $dict = $dict || $self->{'Resources'} || PDFDict();
Condition33%2/6
$dict || $self->{'Resources'} || PDFDict()
l!l&&r!l&&!r
3200
$dict or $self->{"Resources"}
l!l&&r!l&&!r
3200
MC/DC0%0/3
$dict || $self->{'Resources'} || PDFDict()
$dict $self->{"Resources"} PDFDict()
Truth table25%1/4
A or B || C
A= $dictB= $self->{"Resources"}C= PDFDict()
ABCresult
1XX1
01X1
0011
0000
1255  
1256 32 ▶ $dict->realise() if ref($dict) =~ /Objind$/;
Branch: 1 branch
BranchTrueFalseTotal
if ref($dict) =~ /Objind$/ 0 32 32
1257  
1258 32 ▶ $dict->{$type} = $dict->{$type} || PDFDict();
Condition66%2/3
$dict->{$type} || PDFDict()
l!l&&r!l&&!r
3290
MC/DC0%0/2
$dict->{$type} || PDFDict()
$dict->{$type} PDFDict()
Truth table66%2/3
A || B
A= $dict->{$type}B= PDFDict()
ABresult
1X1
011
000
1259 32 ▶ $dict->{$type}->realise() if ref($dict->{$type}) =~ /Objind$/;
Branch: 1 branch
BranchTrueFalseTotal
if ref($dict->{$type}) =~ /Objind$/ 0 32 32
1260  
1261 32 ▶ unless (defined $obj) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj) { } 30 2 32
1262 2 ▶ return $dict->{$type}->{$key} || undef;
Condition50%1/2
$dict->{$type}{$key} || undef
l!l
02
MC/DC0%0/1
$dict->{$type}{$key} || undef
$dict->{$type}{$key}
Truth table50%1/2
A
A= $dict->{$type}{$key}
Aresult
11
00
1263 } else {
1264 30 ▶ if ($force) {
Branch: 1 branch
BranchTrueFalseTotal
if ($force) { } 0 30 30
1265 0 $dict->{$type}->{$key} = $obj;
1266 } else {
1267 30 ▶ $dict->{$type}->{$key} = $dict->{$type}->{$key} || $obj;
Condition33%1/3
$dict->{$type}{$key} || $obj
l!l&&r!l&&!r
0300
MC/DC0%0/2
$dict->{$type}{$key} || $obj
$dict->{$type}{$key} $obj
Truth table33%1/3
A || B
A= $dict->{$type}{$key}B= $obj
ABresult
1X1
011
000
1268 }
1269  
1270 30 ▶ $self->{' apipdf'}->out_obj($dict) if $dict->is_obj($self->{' apipdf'});
Branch: 1 branch
BranchTrueFalseTotal
if $dict->is_obj($self->{" apipdf"}) 0 30 30
1271 30 ▶ $self->{' apipdf'}->out_obj($dict->{$type}) if $dict->{$type}->is_obj($self->{' apipdf'});
Branch: 1 branch
BranchTrueFalseTotal
if $dict->{$type}->is_obj($self->{" apipdf"}) 0 30 30
1272 30 ▶ $self->{' apipdf'}->out_obj($obj) if $obj->is_obj($self->{' apipdf'});
Branch: 1 branch
BranchTrueFalseTotal
if $obj->is_obj($self->{" apipdf"}) 28 2 30
1273 30 $self->{' apipdf'}->out_obj($self);
1274  
1275 30 return $dict;
1276 }
1277 }
1278  
1279 0 ▶sub ship_out {
Subroutine ship_out: not called CC=2 SCAR=17.9
Pod undocumented
1280 0 my ($self, $pdf) = @_;
1281  
1282 0 $pdf->ship_out($self);
1283 0 ▶ if (defined $self->{'Contents'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"Contents"}) 0 0 0
1284 0 $pdf->ship_out($self->{'Contents'}->elements());
1285 }
1286 0 return $self;
1287 }
1288  
1289 #sub outobjdeep {
1290 # my ($self, @opts) = @_;
1291 #
1292 # foreach my $k (qw/ api apipdf /) {
1293 # $self->{" $k"} = undef;
1294 # delete($self->{" $k"});
1295 # }
1296 # return $self->SUPER::outobjdeep(@opts);
1297 #}
1298  
1299 39 1;
« Builder/Basic/PDF/Pages.pm ↑ Summary Builder/Resource/ColorSpace/Indexed/Hue.pm »