Cairo/Box.pm

filter: Statementstmt 62.7% 187 / 298 Branchbran 37.8% 56 / 148 Conditioncond 43.1% 19 / 44 MC/DCmcdc 25.0% 7 / 28 Subroutinesub 81.8% 27 / 33 Podpod 100% 27 / 27 totaltotal 55.8% 323 / 578 CC 105 SCAR 37.8
CC
105
Cov
53%
expand
182.0
shrink
182.0
slice
30.0
CRAP 43.6
« Cairo/Font.pm ↑ Summary Cairo/Util.pm »
1 package PDF::Cairo::Box;
2  
3 1 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use PDF::Cairo::Util qw(paper_size);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = "1.04";
10 1 $VERSION = eval $VERSION;
11  
12 =head1 NAME
13  
14 PDF::Cairo::Box - rectangle-manipulation library
15  
16 =head1 SYNOPSIS
17  
18 PDF::Cairo::Box is designed to simplify PDF layout for forms, graph
19 paper, calendars, practice sheets, etc.
20  
21 use PDF::Cairo::Box;
22  
23 my $page = PDF::Cairo::Box->new(paper => "a4");
24 my $half = $page->fold;
25 $half->shrink(36)->rotate;
26 $page->center($half);
27 ...
28  
29 =head1 DESCRIPTION
30  
31 =cut
32  
33 BEGIN {
34 1 ▶ require Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 our @ISA = qw(Exporter);
36 1 our @EXPORT = qw();
37 1 our @EXPORT_OK = qw();
38 1 our %EXPORT_TAGS = (all => \@EXPORT_OK);
39 }
40  
41 =head2 Creating new boxes
42  
43 =over 4
44  
45 =item B<new> %options
46  
47 =over 4
48  
49 =item width => $width,
50  
51 =item height => $height,
52  
53 =item paper => $paper_size,
54  
55 =item wide|landscape => 1,
56  
57 =item tall|portrait => 1
58  
59 =item x => $LLx,
60  
61 =item y => $LLy,
62  
63 =back
64  
65 All arguments are optional. If called with no arguments, creates a
66 zero-size box with lower-left corner at (x,y)==(0,0). If called with
67 the name of a known paper size (see L<PDF::Cairo::Papers>) and
68 optional orientation, sets width and height to the size of that paper
69 in points.
70  
71 =cut
72  
73 sub new {
74 51 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
75 51 my %options = @_;
76 my $self = {
77 x => $options{x} || 0,
78 51 ▶ y => $options{y} || 0,
Condition75%3/4
$options{'x'} || 0
l!l
438
$options{'y'} || 0
l!l
510
MC/DC100%1/1
$options{'x'} || 0
$options{'x'}
MC/DC0%0/1
$options{'y'} || 0
$options{'y'}
Truth table100%2/2
A
A= $options{'x'}
Aresult
11
00
Truth table50%1/2
A
A= $options{'y'}
Aresult
11
00
79 w => 0,
80 h => 0,
81 };
82 51 paper_size($self, %options);
83 51 bless($self,$class);
84 51 return $self;
85 }
86  
87 =item B<bounds> $box, ...
88  
89 Return a new box that contains all the boxes passed as arguments.
90  
91 =cut
92  
93 sub bounds {
94 1 ▶ shift unless ref $_[0]; # called w/ '->'
Subroutine bounds: called CC=11 SCAR=24.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 1 0 1
95 1 my ($x1, $y1, $x2, $y2);
96 1 foreach my $box (@_) {
97 2 ▶ $x1 = $box->{x} if ! defined $x1 or $box->{x} < $x1;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $x1 or $box->{"x"} < $x1 1 1 2
Condition66%2/3
not defined $x1 or $box->{"x"} < $x1
l!l&&r!l&&!r
101
MC/DC50%1/2
not defined $x1 or $box->{"x"} < $x1
not defined $x1 $box->{"x"} < $x1
Truth table66%2/3
not(A) or B
A= not defined $x1B= $box->{"x"} < $x1
ABresult
1X1
011
000
98 2 ▶ $y1 = $box->{y} if ! defined $y1 or $box->{y} < $y1;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $y1 or $box->{"y"} < $y1 1 1 2
Condition66%2/3
not defined $y1 or $box->{"y"} < $y1
l!l&&r!l&&!r
101
MC/DC50%1/2
not defined $y1 or $box->{"y"} < $y1
not defined $y1 $box->{"y"} < $y1
Truth table66%2/3
not(A) or B
A= not defined $y1B= $box->{"y"} < $y1
ABresult
1X1
011
000
99 2 my $xtmp = $box->{x} + $box->{w};
100 2 my $ytmp = $box->{y} + $box->{h};
101 2 ▶ $x2 = $xtmp if ! defined $x2 or $xtmp > $x2;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $x2 or $xtmp > $x2 1 1 2
Condition66%2/3
not defined $x2 or $xtmp > $x2
l!l&&r!l&&!r
101
MC/DC50%1/2
not defined $x2 or $xtmp > $x2
not defined $x2 $xtmp > $x2
Truth table66%2/3
not(A) or B
A= not defined $x2B= $xtmp > $x2
ABresult
1X1
011
000
102 2 ▶ $y2 = $ytmp if ! defined $y2 or $ytmp > $y2;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $y2 or $ytmp > $y2 2 0 2
Condition66%2/3
not defined $y2 or $ytmp > $y2
l!l&&r!l&&!r
110
MC/DC0%0/2
not defined $y2 or $ytmp > $y2
not defined $y2 $ytmp > $y2
Truth table66%2/3
not(A) or B
A= not defined $y2B= $ytmp > $y2
ABresult
1X1
011
000
103 }
104 1 return PDF::Cairo::Box->new(
105 x => $x1, y => $y1,
106 width => $x2 - $x1, height => $y2 - $y1
107 );
108 }
109  
110 =back
111  
112 =head2 Setting/getting box parameters
113  
114 =over 4
115  
116 =item B<bbox> [$x,$y, $width,$height]
117  
118 Return the boundaries of the current box (convenient for passing to
119 rect()). If four arguments are passed, set it to those values instead.
120  
121 =cut
122  
123 sub bbox {
124 1 ▶ my $self = shift;
Subroutine bbox: called CC=2 SCAR=9.2
Pod documented
125 1 ▶ if (@_ == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 4) { } 0 1 1
126 0 $self->xy(shift, shift);
127 0 $self->size(shift, shift);
128 }else{
129 1 return ($self->xy, $self->size);
130 }
131 0 return $self;
132 }
133  
134 =item B<height> [$height]
135  
136 Return the height of the current box. If an argument is passed, set the
137 height to that value instead.
138  
139 =cut
140  
141 sub height {
142 9 ▶ my $self = shift;
Subroutine height: called CC=2 SCAR=7.6
Pod documented
143 9 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 0 9 9
144 0 $self->{h} = $_[0];
145 }
146 9 return $self->{h};
147 }
148  
149 =item B<iswide>
150  
151 Returns true if the current box is wider than it is tall.
152  
153 =cut
154  
155 sub iswide {
156 1 ▶ my $self = shift;
Subroutine iswide: called CC=1 SCAR=0.0
Pod documented
157 1 return $self->{w} > $self->{h};
158 }
159  
160 =item B<size> [$width, $height]
161  
162 Return the (width, height) of the current box. If two arguments are
163 passed, set them to those values.
164  
165 =cut
166  
167 sub size {
168 7 ▶ my $self = shift;
Subroutine size: called CC=3 SCAR=11.0
Pod documented
169 7 ▶ if (defined $_[0] and defined $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and defined $_[1]) 1 6 7
Condition66%2/3
defined $_[0] and defined $_[1]
!ll&&!rl&&r
601
MC/DC50%1/2
defined $_[0] and defined $_[1]
defined $_[0] defined $_[1]
Truth table66%2/3
A and B
A= defined $_[0]B= defined $_[1]
ABresult
0X0
100
111
170 1 $self->{w} = $_[0];
171 1 $self->{h} = $_[1];
172 }
173 7 return ($self->{w}, $self->{h});
174 }
175  
176 =item B<width> [$width]
177  
178 Return the width of the current box. If an argument is passed, set the
179 width to that value instead.
180  
181 =cut
182  
183 sub width {
184 2 ▶ my $self = shift;
Subroutine width: called CC=2 SCAR=7.6
Pod documented
185 2 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 0 2 2
186 0 $self->{w} = $_[0];
187 }
188 2 return $self->{w};
189 }
190  
191 =item B<x> [$x]
192  
193 Return the X coordinate of the current box's lower-left corner. If an
194 argument is passed, move the box to that location instead.
195  
196 =cut
197  
198 sub x {
199 1 ▶ my $self = shift;
Subroutine x: called CC=2 SCAR=7.6
Pod documented
200 1 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 0 1 1
201 0 $self->{x} = $_[0];
202 }
203 1 return $self->{x};
204 }
205  
206 =item B<y> [$y]
207  
208 Return the Y coordinate of the current box's lower-left corner. If an
209 argument is passed, move the box to that location instead.
210  
211 =cut
212  
213 sub y {
214 4 ▶ my $self = shift;
Subroutine y: called CC=2 SCAR=7.6
Pod documented
215 4 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 0 4 4
216 0 $self->{y} = $_[0];
217 }
218 4 return $self->{y};
219 }
220  
221 =item B<xy> [$x, $y]
222  
223 Return the (X,Y) coordinates of the current box's lower-left corner.
224 If two arguments are passed, move the box to that location instead.
225  
226 =cut
227  
228 sub xy {
229 2 ▶ my $self = shift;
Subroutine xy: called CC=3 SCAR=14.2
Pod documented
230 2 ▶ if (defined $_[0] and defined $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and defined $_[1]) 0 2 2
Condition33%1/3
defined $_[0] and defined $_[1]
!ll&&!rl&&r
200
MC/DC0%0/2
defined $_[0] and defined $_[1]
defined $_[0] defined $_[1]
Truth table33%1/3
A and B
A= defined $_[0]B= defined $_[1]
ABresult
0X0
100
111
231 0 $self->{x} = $_[0];
232 0 $self->{y} = $_[1];
233 }
234 2 return ($self->{x}, $self->{y});
235 }
236  
237 =item B<cx> [$center_x]
238  
239 Return the X coordinate of the current box's center. If an argument
240 is passed, move the box to that location instead.
241  
242 =cut
243  
244 sub cx {
245 0 ▶ my $self = shift;
Subroutine cx: not called CC=2 SCAR=17.9
Pod documented
246 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 0 0 0
247 0 $self->{x} = $_[0] - $self->{w} / 2;
248 }
249 0 return $self->{x} + $self->{w} / 2;
250 }
251  
252 =item B<cy> [$center_y]
253  
254 Return the Y coordinate of the current box's center. If an argument
255 is passed, move the box to that location instead.
256  
257 =cut
258  
259 sub cy {
260 0 ▶ my $self = shift;
Subroutine cy: not called CC=2 SCAR=17.9
Pod documented
261 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 0 0 0
262 0 $self->{y} = $_[0] - $self->{h} / 2;
263 }
264 0 return $self->{y} + $self->{h} / 2;
265 }
266  
267 =item B<cxy> [$center_x, $center_y]
268  
269 Return the (X,Y) coordinates of the current box's center.
270 If two arguments are passed, move the box to that location instead.
271  
272 =cut
273  
274 sub cxy {
275 1 ▶ my $self = shift;
Subroutine cxy: called CC=3 SCAR=14.2
Pod documented
276 1 ▶ if (defined $_[0] and defined $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and defined $_[1]) 0 1 1
Condition33%1/3
defined $_[0] and defined $_[1]
!ll&&!rl&&r
100
MC/DC0%0/2
defined $_[0] and defined $_[1]
defined $_[0] defined $_[1]
Truth table33%1/3
A and B
A= defined $_[0]B= defined $_[1]
ABresult
0X0
100
111
277 0 $self->{x} = $_[0] - $self->{w} / 2;
278 0 $self->{y} = $_[1] - $self->{h} / 2;
279 }
280 1 return ($self->{x} + $self->{w} / 2, $self->{y} + $self->{h} / 2);
281 }
282  
283 =back
284  
285 =head2 Creating boxes from existing boxes
286  
287 =over 4
288  
289 =item B<copy>
290  
291 Return a new copy of the current box.
292  
293 =cut
294  
295 sub copy {
296 7 ▶ my ($self) = @_;
Subroutine copy: called CC=2 SCAR=6.9
Pod documented
297 7 my $tmp = {};
298 7 foreach my $k (keys %$self) {
299 28 $tmp->{$k} = $self->{$k};
300 }
301 7 bless($tmp, ref $self);
302 7 return $tmp;
303 }
304  
305 =item B<fold> $folds
306  
307 ISO-style paper folding, without rounding. Returns a new box created
308 by halving the long dimension of the current box $folds times (default
309 1), maintaining the current orientation. That is, folding
310 portrait-format A4 once creates portrait-format A5, and folding it
311 twice creates portrait-format A6.
312  
313 =cut
314  
315 sub fold {
316 3 ▶ my $self = shift;
Subroutine fold: called CC=6 SCAR=18.4
Pod documented
317 3 ▶ my $folds = shift || 1;
Condition100%2/2
shift() || 1
l!l
12
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
318 3 my $newbox = $self->copy;
319 3 ▶ if ($folds >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($folds >= 1) 3 0 3
320 3 my $iswide = $self->{w} > $self->{h};
321 my ($short, $long) = $iswide ?
322 ($self->{h}, $self->{w}) :
323 3 ▶ ($self->{w}, $self->{h});
Branch: 1 branch
BranchTrueFalseTotal
$iswide ? : 3 0 3
324 3 for my $i (1..$folds) {
325 4 my $tmp = $short;
326 4 $short = $long / 2;
327 4 $long = $tmp;
328 }
329 3 ▶ if ($iswide) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iswide) { } 3 0 3
330 3 $newbox->{w} = $long;
331 3 $newbox->{h} = $short;
332 }else{
333 0 $newbox->{w} = $short;
334 0 $newbox->{h} = $long;
335 }
336 }
337 3 return $newbox;
338 }
339  
340 =item B<grid> %options
341  
342 =over 4
343  
344 =item rows => $n,
345  
346 =item columns => $n,
347  
348 =item width => $size|'$size%',
349  
350 =item height => $size|'$size%',
351  
352 =item xpack => 'left|center|right',
353  
354 =item ypack => 'top|center|bottom',
355  
356 =item center => 1,
357  
358 =back
359  
360 Create a grid of new boxes by splitting the current box according to
361 the passed arguments, either by absolute size or percentage. Returns
362 an array of rows from top to bottom, with each row pointing to an array
363 of cells, left to right. This is equivalent to calling slice by columns
364 on the result of slice by rows.
365  
366 By default, leftover space is evenly distributed between the boxes
367 and the parent box. The xpack/ypack arguments aligns all of the boxes at
368 the position requested, with no space between them. The center argument
369 is equivalent to setting both xpack and ypack to center.
370  
371 =cut
372  
373 sub grid {
374 1 ▶ my $self = shift;
Subroutine grid: called CC=8 SCAR=24.3
Pod documented
375 1 my %options = @_;
376 1 my ($slice_w, $slice_h) = (0, 0);
377 1 ▶ if (defined $options{columns}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $options{'columns'}) { } 0 1 1
elsif (defined $options{'width'}) { } 1 0 1
378 0 $slice_w = $self->{w} / int($options{columns});
379 }elsif (defined $options{width}) {
380 1 ▶ if ($options{width} =~ s/%$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'width'} =~ s/%$//u) { } 0 1 1
381 0 $slice_w = $self->{w} * $` / 100;
382 }else{
383 1 $slice_w = $options{width};
384 }
385 }else{
386 0 croak "grid(): must supply both columns/width and rows/height";
387 }
388 1 ▶ if (defined $options{rows}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $options{'rows'}) { } 0 1 1
elsif (defined $options{'height'}) { } 1 0 1
389 0 $slice_h = $self->{h} / int($options{rows});
390 }elsif (defined $options{height}) {
391 1 ▶ if ($options{height} =~ s/%$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'height'} =~ s/%$//u) { } 0 1 1
392 0 $slice_h = $self->{h} * $` / 100;
393 }else{
394 1 $slice_h = $options{height};
395 }
396 }else{
397 0 croak "grid(): must supply both columns/width and rows/height";
398 }
399 1 my @result;
400 %options = (
401 center => $options{center},
402 xpack => $options{xpack},
403 ypack => $options{ypack},
404 1 );
405 1 foreach my $row ($self->slice(height => $slice_h, %options)) {
406 6 push(@result, [ $row->slice(width => $slice_w, %options) ]);
407 }
408 1 return @result;
409 }
410  
411 =item B<slice> %options
412  
413 =over 4
414  
415 =item rows => $n,
416  
417 =item columns => $n,
418  
419 =item width => $size|'$size%',
420  
421 =item height => $size|'$size%',
422  
423 =item xpack => 'left'|'center'|'right',
424  
425 =item ypack => 'top'|'center'|'bottom',
426  
427 =item center => 1,
428  
429 =back
430  
431 Returns an array of new boxes created by dividing the current box
432 according to the single argument provided. Width/height can be an
433 absolute size or percentage, while row/column must be an integer
434 greater than 1. Rows are returned left-to-right, columns
435 top-to-bottom.
436  
437 By default, leftover space is evenly distributed between the boxes
438 and the parent box. The xpack/ypack arguments aligns all of the boxes at
439 the position requested, with no space between them. The center argument
440 is equivalent to setting both xpack and ypack to center.
441  
442 =cut
443  
444 sub slice {
445 7 ▶ my $self = shift;
Subroutine slice: called CC=18 SCAR=34.0
Pod documented
446 7 my %options = @_;
447 7 my ($slice_w, $slice_h) = (0, 0);
448 7 my @result;
449 7 ▶ if (defined $options{columns}) {
Branch: 4 branches
BranchTrueFalseTotal
if (defined $options{'columns'}) { } 0 7 7
elsif (defined $options{'rows'}) { } 0 7 7
elsif (defined $options{'width'}) { } 6 1 7
elsif (defined $options{'height'}) { } 1 0 1
450 0 $slice_w = $self->{w} / int($options{columns});
451 }elsif (defined $options{rows}) {
452 0 $slice_h = $self->{h} / int($options{rows});
453 }elsif (defined $options{width}) {
454 6 ▶ if ($options{width} =~ s/%$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'width'} =~ s/%$//u) { } 0 6 6
455 0 $slice_w = $self->{w} * $` / 100;
456 }else{
457 6 $slice_w = $options{width};
458 }
459 }elsif (defined $options{height}) {
460 1 ▶ if ($options{height} =~ s/%$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'height'} =~ s/%$//u) { } 0 1 1
461 0 $slice_h = $self->{h} * $` / 100;
462 }else{
463 1 $slice_h = $options{height};
464 }
465 }
466 7 ▶ if (defined $options{center}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"center"}) 7 0 7
467 7 $options{xpack} = 'center';
468 7 $options{ypack} = 'center';
469 }
470 7 ▶ if ($slice_w) {
Branch: 2 branches
BranchTrueFalseTotal
if ($slice_w) { } 6 1 7
elsif ($slice_h) { } 1 0 1
471 # columns, from left to right
472 6 my $slices = int($self->{w} / $slice_w + 0.00001);
473 6 my $space = $self->{w} - $slices * $slice_w;
474 6 my ($margin, $gutter) = (0, 0);
475 6 ▶ if (defined $options{xpack}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{'xpack'}) { } 6 0 6
476 6 ▶ if ($options{xpack} eq 'center') {
Branch: 2 branches
BranchTrueFalseTotal
if ($options{'xpack'} eq 'center') { } 6 0 6
elsif ($options{'xpack'} eq 'right') { } 0 0 0
477 6 $margin = $space / 2;
478 }elsif ($options{xpack} eq 'right') {
479 0 $margin = $space;
480 }
481 }else{
482 0 $margin = $space / ($slices + 1);
483 0 $gutter = $margin;
484 }
485 6 my $x = $self->{x} + $margin;
486 6 foreach my $slice (1..$slices) {
487 push(@result, PDF::Cairo::Box->new(
488 x => $x, y => $self->{y},
489 width => $slice_w, height => $self->{h},
490 42 ));
491 42 $x += $slice_w + $gutter;
492 }
493 }elsif ($slice_h) {
494 # rows, from top to bottom
495 1 my $slices = int($self->{h} / $slice_h + 0.00001);
496 1 my $space = $self->{h} - $slices * $slice_h;
497 1 my ($margin, $gutter) = (0, 0);
498 1 ▶ if (defined $options{ypack}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{'ypack'}) { } 1 0 1
499 1 ▶ if ($options{ypack} eq 'center') {
Branch: 2 branches
BranchTrueFalseTotal
if ($options{'ypack'} eq 'center') { } 1 0 1
elsif ($options{'ypack'} eq 'bottom') { } 0 0 0
500 1 $margin = $space / 2;
501 }elsif ($options{ypack} eq 'bottom') {
502 0 $margin = $space;
503 }
504 }else{
505 0 $margin = $space / ($slices + 1);
506 0 $gutter = $margin;
507 }
508 1 my $y = $self->{y} + $self->{h} - $margin;
509 1 foreach my $slice (1..$slices) {
510 6 $y -= $slice_h;
511 push(@result, PDF::Cairo::Box->new(
512 x => $self->{x}, y => $y,
513 6 width => $self->{w}, height => $slice_h,
514 ));
515 6 $y -= $gutter;
516 }
517 }
518 7 return @result;
519 }
520  
521 =item B<split> %options
522  
523 =over 4
524  
525 =item width => $size|'$size%',
526  
527 =item height => $size|'$size%',
528  
529 =back
530  
531 Returns two new boxes created by splitting the current box at the
532 supplied width or height, either by absolute size or percentage.
533  
534 =cut
535  
536 sub split {
537 1 ▶ my $self = shift;
Subroutine split: called CC=5 SCAR=19.7
Pod documented
538 1 my %options = @_;
539 1 my $box1 = $self->copy;
540 1 my $box2 = $self->copy;
541 1 ▶ if (defined $options{width}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $options{'width'}) { } 0 1 1
elsif (defined $options{'height'}) { } 1 0 1
542 0 my $width = $options{width};
543 0 ▶ if ($width =~ s/%$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($width =~ s/%$//u) 0 0 0
544 0 $width = $self->{w} * $` / 100;
545 }
546 0 $box1->{w} = $width;
547 0 $box2->{w} = $self->{w} - $width;
548 0 $box2->{x} = $self->{x} + $width;
549 }elsif (defined $options{height}) {
550 1 my $height = $options{height};
551 1 ▶ if ($height =~ s/%$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($height =~ s/%$//u) 1 0 1
552 1 $height = $self->{h} * $` / 100;
553 }
554 1 $box1->{h} = $height;
555 1 $box1->{y} = $self->{y} + $self->{h} - $height;
556 1 $box2->{h} = $self->{h} - $height;
557 }else{
558 0 croak "PDF::Cairo::Box::Split: must supply either height or width";
559 }
560 1 return ($box1, $box2);
561 }
562  
563 =item B<unfold> $folds
564  
565 Reverses an ISO-style paper fold. Returns a new box created by
566 doubling the length of the short side of the current box $folds times,
567 maintaining the current orientation.
568  
569 Note that due to rounding, the official B5 is slightly larger than an
570 unfolded B6.
571  
572 =cut
573  
574 sub unfold {
575 2 ▶ my $self = shift;
Subroutine unfold: called CC=6 SCAR=18.4
Pod documented
576 2 ▶ my $folds = shift || 1;
Condition100%2/2
shift() || 1
l!l
11
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
577 2 my $newbox = $self->copy;
578 2 ▶ if ($folds >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($folds >= 1) 2 0 2
579 2 my $iswide = $self->{w} > $self->{h};
580 my ($short, $long) = $iswide ?
581 ($self->{h}, $self->{w}) :
582 2 ▶ ($self->{w}, $self->{h});
Branch: 1 branch
BranchTrueFalseTotal
$iswide ? : 2 0 2
583 2 for my $i (1..$folds) {
584 3 my $tmp = $long;
585 3 $long = $short * 2;
586 3 $short = $tmp;
587 }
588 2 ▶ if ($iswide) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iswide) { } 2 0 2
589 2 $newbox->{w} = $long;
590 2 $newbox->{h} = $short;
591 }else{
592 0 $newbox->{w} = $short;
593 0 $newbox->{h} = $long;
594 }
595 }
596 2 return $newbox;
597 }
598  
599 #TODO: figure out if I really need this, and how it should work...
600 #=item B<fill> $box, %options
601  
602 =back
603  
604 =head2 Moving boxes
605  
606 =over 4
607  
608 =item B<align> [$box, ...], %options
609  
610 =over 4
611  
612 =item top => 1,
613  
614 =item center_v => 1,
615  
616 =item bottom => 1,
617  
618 =item left => 1,
619  
620 =item center_h => 1,
621  
622 =item right => 1,
623  
624 =item center => 1,
625  
626 =back
627  
628 Align one or more boxes to the current box. For convenience, if only
629 one box is passed in the first argument, it doesn't need to be wrapped
630 in an array reference.
631  
632 =cut
633  
634 sub align {
635 1 ▶ my $self = shift;
Subroutine align: called CC=10 SCAR=28.7
Pod documented
636 1 my $boxes = shift;
637 1 ▶ $boxes = [ $boxes ] unless ref $boxes eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $boxes eq "ARRAY" 0 1 1
638 1 my %options = @_;
639 1 ▶ if ($options{center}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"center"}) 1 0 1
640 1 $options{center_v} = 1;
641 1 $options{center_h} = 1;
642 }
643 1 foreach my $box (@$boxes) {
644 1 ▶ if ($options{top}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($options{'top'}) { } 0 1 1
elsif ($options{'center_v'}) { } 1 0 1
elsif ($options{'bottom'}) { } 0 0 0
645 0 $box->{y} = $self->{y} + $self->{h} - $box->{h};
646 }elsif ($options{center_v}) {
647 1 $box->{y} = $self->{y} + ($self->{h} - $box->{h}) / 2;
648 }elsif ($options{bottom}) {
649 0 $box->{y} = $self->{y};
650 }
651 1 ▶ if ($options{left}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($options{'left'}) { } 0 1 1
elsif ($options{'center_h'}) { } 1 0 1
elsif ($options{'right'}) { } 0 0 0
652 0 $box->{x} = $self->{x};
653 }elsif ($options{center_h}) {
654 1 $box->{x} = $self->{x} + ($self->{w} - $box->{w}) / 2;
655 }elsif ($options{right}) {
656 0 $box->{x} = $self->{x} + $self->{w} - $box->{w};
657 }
658 }
659 1 return @$boxes;
660 }
661  
662 =item B<center> $box, ...
663  
664 Center one or more boxes to the current box.
665  
666 =cut
667  
668 sub center {
669 1 ▶ my $self = shift;
Subroutine center: called CC=1 SCAR=0.0
Pod documented
670 1 $self->align([ @_ ], center => 1);
671 }
672  
673 =item TODO B<distribute>
674  
675 distribute({by_row, by_col, pack}, $box, ...)
676  
677 =item B<move> $x, $y
678  
679 =item B<move> $box
680  
681 Move a box to an absolute location, or to the same (x,y)
682 position as another box.
683  
684 =cut
685  
686 sub move {
687 0 ▶ my $self = shift;
Subroutine move: not called CC=3 SCAR=24.8
Pod documented
688 0 ▶ if (ref $_[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0]) { } 0 0 0
elsif (@_) { } 0 0 0
689 0 $self->{x} = $_[0]->{x};
690 0 $self->{y} = $_[0]->{y};
691 }elsif (@_) {
692 0 $self->{x} = $_[0];
693 0 $self->{y} = $_[1];
694 }else{
695 0 croak "PDF::Cairo::Box::move: missing arguments";
696 }
697 0 return $self;
698 }
699  
700 =item B<rel_move> $delta_x, $delta_y
701  
702 Move a box relative to its current location.
703  
704 =cut
705  
706 sub rel_move {
707 1 ▶ my $self = shift;
Subroutine rel_move: called CC=4 SCAR=15.2
Pod documented
708 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1 0 1
709 1 ▶ $self->{x} = defined $self->{x} ? $self->{x} + $_[0] : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'x'} ? : 1 0 1
710 1 ▶ $self->{y} = defined $self->{y} ? $self->{y} + $_[1] : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'y'} ? : 1 0 1
711 }else{
712 0 croak "PDF::Cairo::Box::rel_move: missing arguments";
713 }
714 1 return $self;
715 }
716  
717 =back
718  
719 =head2 Resizing boxes
720  
721 =over 4
722  
723 =item B<expand> %options
724  
725 =over 4
726  
727 =item top => $size|'$size%',
728  
729 =item bottom => $size|'$size%',
730  
731 =item left => $size|'$size%',
732  
733 =item right => $size|'$size%',
734  
735 =item all => $size|'$size%',
736  
737 =back
738  
739 Increases the size of the current box, maintaining its relative
740 position.
741  
742 =cut
743  
744 sub expand {
745 0 ▶ my $self = shift;
Subroutine expand: not called CC=13 SCAR=52.0
Pod documented
746 0 my %options = @_;
747 0 ▶ if ($options{all}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"all"}) 0 0 0
748 0 foreach my $opt (qw(top bottom left right)) {
749 0 $options{$opt} = $options{all};
750 }
751 }
752 # convert percentages to absolute values before applying any changes
753 0 foreach my $opt (qw(top bottom)) {
754 0 ▶ if (defined $options{$opt} and $options{$opt} =~ s/%$//) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{$opt} and $options{$opt} =~ s/%$//u) 0 0 0
Condition0%0/3
defined $options{$opt} and $options{$opt} =~ s/%$//u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $options{$opt} and $options{$opt} =~ s/%$//u
defined $options{$opt} $options{$opt} =~ s/%$//u
Truth table0%0/3
A and B
A= defined $options{$opt}B= $options{$opt} =~ s/%$//u
ABresult
0X0
100
111
755 0 $options{$opt} = $self->{h} * $` / 100;
756 }
757 }
758 0 foreach my $opt (qw(left right)) {
759 0 ▶ if (defined $options{$opt} and $options{$opt} =~ s/%$//) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{$opt} and $options{$opt} =~ s/%$//u) 0 0 0
Condition0%0/3
defined $options{$opt} and $options{$opt} =~ s/%$//u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $options{$opt} and $options{$opt} =~ s/%$//u
defined $options{$opt} $options{$opt} =~ s/%$//u
Truth table0%0/3
A and B
A= defined $options{$opt}B= $options{$opt} =~ s/%$//u
ABresult
0X0
100
111
760 0 $options{$opt} = $self->{w} * $` / 100;
761 }
762 }
763 0 ▶ if ($options{top}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"top"}) 0 0 0
764 0 $self->{h} += $options{top};
765 }
766 0 ▶ if ($options{bottom}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"bottom"}) 0 0 0
767 0 $self->{h} += $options{bottom};
768 0 $self->{y} -= $options{bottom};
769 }
770 0 ▶ if ($options{left}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"left"}) 0 0 0
771 0 $self->{w} += $options{left};
772 0 $self->{x} -= $options{left};
773 }
774 0 ▶ if ($options{right}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"right"}) 0 0 0
775 0 $self->{w} += $options{right};
776 }
777 0 return $self;
778 }
779  
780 =item B<rotate>
781  
782 Swaps width/height of a box. Does not change its (x,y) position.
783  
784 =cut
785  
786 sub rotate {
787 1 ▶ my $self = shift;
Subroutine rotate: called CC=1 SCAR=0.0
Pod documented
788 1 my $tmpwidth = $self->{w};
789 1 $self->{w} = $self->{h};
790 1 $self->{h} = $tmpwidth;
791 1 $self->{size} = "";
792 1 return $self;
793 }
794  
795 =item B<scale> $scale
796  
797 Scale the location and size of the current box (1 = no change).
798  
799 =cut
800  
801 sub scale {
802 0 ▶ my $self = shift;
Subroutine scale: not called CC=3 SCAR=24.8
Pod documented
803 0 my $scale = shift;
804 0 ▶ if (defined $scale and $scale > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $scale and $scale > 0) 0 0 0
Condition0%0/3
defined $scale and $scale > 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $scale and $scale > 0
defined $scale $scale > 0
Truth table0%0/3
A and B
A= defined $scaleB= $scale > 0
ABresult
0X0
100
111
805 0 $self->{x} *= $scale;
806 0 $self->{y} *= $scale;
807 0 $self->{w} *= $scale;
808 0 $self->{h} *= $scale;
809 }
810 0 return $self;
811 }
812  
813 =item B<shrink> %options
814  
815 =over 4
816  
817 =item top => $size|'$size%',
818  
819 =item bottom => $size|'$size%',
820  
821 =item left => $size|'$size%',
822  
823 =item right => $size|'$size%',
824  
825 =item all => $size|'$size%',
826  
827 =back
828  
829 Reduces the size of the current box, maintaining its relative
830 position.
831  
832 =cut
833  
834 sub shrink {
835 0 ▶ my $self = shift;
Subroutine shrink: not called CC=13 SCAR=52.0
Pod documented
836 0 my %options = @_;
837 0 ▶ if ($options{all}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"all"}) 0 0 0
838 0 foreach my $opt (qw(top bottom left right)) {
839 0 $options{$opt} = $options{all};
840 }
841 }
842 # convert percentages to absolute values before applying any changes
843 0 foreach my $opt (qw(top bottom)) {
844 0 ▶ if (defined $options{$opt} and $options{$opt} =~ s/%$//) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{$opt} and $options{$opt} =~ s/%$//u) 0 0 0
Condition0%0/3
defined $options{$opt} and $options{$opt} =~ s/%$//u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $options{$opt} and $options{$opt} =~ s/%$//u
defined $options{$opt} $options{$opt} =~ s/%$//u
Truth table0%0/3
A and B
A= defined $options{$opt}B= $options{$opt} =~ s/%$//u
ABresult
0X0
100
111
845 0 $options{$opt} = $self->{h} * $` / 100;
846 }
847 }
848 0 foreach my $opt (qw(left right)) {
849 0 ▶ if (defined $options{$opt} and $options{$opt} =~ s/%$//) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{$opt} and $options{$opt} =~ s/%$//u) 0 0 0
Condition0%0/3
defined $options{$opt} and $options{$opt} =~ s/%$//u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $options{$opt} and $options{$opt} =~ s/%$//u
defined $options{$opt} $options{$opt} =~ s/%$//u
Truth table0%0/3
A and B
A= defined $options{$opt}B= $options{$opt} =~ s/%$//u
ABresult
0X0
100
111
850 0 $options{$opt} = $self->{w} * $` / 100;
851 }
852 }
853 0 ▶ if ($options{top}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"top"}) 0 0 0
854 0 $self->{h} -= $options{top};
855 }
856 0 ▶ if ($options{bottom}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"bottom"}) 0 0 0
857 0 $self->{h} -= $options{bottom};
858 0 $self->{y} += $options{bottom};
859 }
860 0 ▶ if ($options{left}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"left"}) 0 0 0
861 0 $self->{w} -= $options{left};
862 0 $self->{x} += $options{left};
863 }
864 0 ▶ if ($options{right}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"right"}) 0 0 0
865 0 $self->{w} -= $options{right};
866 }
867 0 return $self;
868 }
869  
870 =back
871  
872 =head1 BUGS
873  
874 Gosh, none I hope.
875  
876 =head1 AUTHOR
877  
878 J Greely, C<< <jgreely at cpan.org> >>
879  
880 =cut
881  
882 1 1;
« Cairo/Font.pm ↑ Summary Cairo/Util.pm »