Cairo/Util.pm

filter: Statementstmt 45.9% 40 / 87 Branchbran 25.0% 7 / 28 Conditioncond 7.1% 1 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 58.3% 7 / 12 Podpod 100% 5 / 5 totaltotal 38.7% 60 / 155 CC 21 SCAR 47.4
CC
21
Cov
37%
paper_size
240.0
BEGIN
6.0
regular_polygon
6.0
CRAP 114.4
↑ Summary Cairo/Box.pm »
1 package PDF::Cairo::Util;
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 Module::Path 'module_path';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = "1.03";
10 1 $VERSION = eval $VERSION;
11  
12 =head1 NAME
13  
14 PDF::Cairo::Util - utility functions
15  
16 =head1 SYNOPSIS
17  
18 use PDF::Cairo::Util;
19  
20 $points = cm(5);
21 $points = in(8.5);
22 $points = mm(300);
23 ($width, $height) = paper_size('usletter');
24 $hex = regular_polygon(6);
25  
26 =head1 DESCRIPTION
27  
28 =cut
29  
30 1 our %paper;
31  
32  
33 BEGIN {
34 1 ▶ require Exporter;
Subroutine BEGIN: called CC=6 SCAR=17.9
35 1 our @ISA = qw(Exporter);
36 1 our @EXPORT = qw();
37 1 our @EXPORT_OK = qw(cm in mm paper_size regular_polygon);
38 1 our %EXPORT_TAGS = (all => \@EXPORT_OK);
39 1 %paper = ();
40 1 ▶ my $file = module_path('PDF::Cairo') || "PDF/Cairo/papers.txt";
Condition50%1/2
module_path('PDF::Cairo') || 'PDF/Cairo/papers.txt'
l!l
10
MC/DC0%0/1
module_path('PDF::Cairo') || 'PDF/Cairo/papers.txt'
module_path('PDF::Cairo')
Truth table50%1/2
A
A= module_path('PDF::Cairo')
Aresult
11
00
41 1 $file =~ s/\.pm$/\/papers.txt/;
42 1 ▶ open(my $In, "<", $file) or
Branch: 1 branch
BranchTrueFalseTotal
open my $In, "<", $file or die "$0: PDF::Cairo::papers.txt ($file): $!\n" 0 1 1
43 die "$0: PDF::Cairo::papers.txt ($file): $!\n";
44 1 ▶ while (<$In>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $In)) 193 1 194
45 193 ▶ next if /^\s*$|^\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$|^\s*#/u 32 161 193
46 161 chomp;
47 161 my ($name,$w,$h,$notes);
48 161 tr/A-Z/a-z/;
49 161 ▶ if (/^([^ =]+)\s*=\s*(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^([^ =]+)\s*=\s*(.*)$/u) { } 42 119 161
50 42 $paper{$1} = $2;
51 }else{
52 119 ($name,$w,$h,$notes) = split(' ');
53 119 $paper{$name} = { w=>$w, h=>$h, notes=>$notes};
54 }
55 }
56 1 CORE::close($In);
57 }
58  
59 =head2 FUNCTIONS
60  
61 =over 4
62  
63 =item B<cm> $centimeters
64  
65 Converts the arguments from centimeters to points. Importable.
66  
67 =cut
68  
69 sub cm {
70 0 ▶ return $_[0] / 2.54 * 72;
Subroutine cm: not called CC=1 SCAR=6.9
Pod documented
71 }
72  
73 =item B<in> $inches
74  
75 Converts the arguments from inches to points. Importable.
76  
77 =cut
78  
79 sub in {
80 0 ▶ return $_[0] * 72;
Subroutine in: not called CC=1 SCAR=6.9
Pod documented
81 }
82  
83 =item B<mm> $millimeters
84  
85 Converts the arguments from millimeters to points. Importable.
86  
87 =cut
88  
89 sub mm {
90 0 ▶ return $_[0] / 25.4 * 72;
Subroutine mm: not called CC=1 SCAR=6.9
Pod documented
91 }
92  
93 =item B<paper_size> %options
94  
95 =over 4
96  
97 =item paper => $paper_size
98  
99 =item wide|landscape => 1
100  
101 =item tall|portrait => 1
102  
103 =back
104  
105 Return size in points of a paper type. The default is "US Letter"
106 (8.5x11 inches). The wide/tall options can be used to ensure that the
107 orientation of the page is as expected. Importable.
108  
109 The supported paper sizes are listed in L<PDF::Cairo::Papers>.
110  
111 =cut
112  
113 # can either be called as a method or a function;
114 # returns ($width, $height) in points
115 #
116 sub paper_size {
117 0 ▶ my $self = ref($_[0]) ? shift : {};
Subroutine paper_size: not called CC=15 SCAR=54.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
118 0 my %options = @_;
119 0 ▶ if (defined $options{width} and defined $options{height}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"width"} and defined $options{"height"}) 0 0 0
Condition0%0/3
defined $options{"width"} and defined $options{"height"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $options{"width"} and defined $options{"height"}
defined $options{"width"} defined $options{"height"}
Truth table0%0/3
A and B
A= defined $options{"width"}B= defined $options{"height"}
ABresult
0X0
100
111
120 0 $self->{w} = $options{width};
121 0 $self->{h} = $options{height};
122 0 $self->{paper} = undef;
123 0 return ($self->{w}, $self->{h});
124 }
125 0 my $size = $options{paper};
126 0 $size =~ tr/A-Z/a-z/;
127 0 ▶ if (!defined $paper{$size}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $PDF::Cairo::Util::paper{$size}) 0 0 0
128 0 croak "PDF::Cairo::paper_size: unknown size '$size'.\n";
129 }
130 # process aliases
131 0 my $rotated = 0;
132 0 ▶ if (!ref $paper{$size}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $PDF::Cairo::Util::paper{$size}) 0 0 0
133 0 my $tmp;
134 0 ($size, $tmp) = split(/,/,$paper{$size});
135 0 ▶ $rotated = !$rotated if defined $tmp and $tmp eq 'rotated';
Branch: 1 branch
BranchTrueFalseTotal
if defined $tmp and $tmp eq "rotated" 0 0 0
Condition0%0/3
defined $tmp and $tmp eq "rotated"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $tmp and $tmp eq "rotated"
defined $tmp $tmp eq "rotated"
Truth table0%0/3
A and B
A= defined $tmpB= $tmp eq "rotated"
ABresult
0X0
100
111
136 }
137 0 ▶ if ($rotated) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rotated) { } 0 0 0
138 0 $self->{w} = $paper{$size}->{h};
139 0 $self->{h} = $paper{$size}->{w};
140 }else{
141 0 $self->{w} = $paper{$size}->{w};
142 0 $self->{h} = $paper{$size}->{h};
143 }
144 0 ▶ if ($options{wide} or $options{landscape}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($options{'wide'} or $options{'landscape'}) { } 0 0 0
elsif ($options{'tall'} or $options{'portrait'}) { } 0 0 0
Condition0%0/3
$options{"wide"} or $options{"landscape"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"wide"} or $options{"landscape"}
$options{"wide"} $options{"landscape"}
Truth table0%0/3
A or B
A= $options{"wide"}B= $options{"landscape"}
ABresult
1X1
011
000
145 0 my $tmp = $self->{w};
146 0 ▶ if ($tmp < $self->{h}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp < $self->{"h"}) 0 0 0
147 0 $self->{w} = $self->{h};
148 0 $self->{h} = $tmp;
149 }
150 ▶ }elsif ($options{tall} or $options{portrait}) {
Condition0%0/3
$options{"tall"} or $options{"portrait"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"tall"} or $options{"portrait"}
$options{"tall"} $options{"portrait"}
Truth table0%0/3
A or B
A= $options{"tall"}B= $options{"portrait"}
ABresult
1X1
011
000
151 0 my $tmp = $self->{w};
152 0 ▶ if ($tmp > $self->{h}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp > $self->{"h"}) 0 0 0
153 0 $self->{w} = $self->{h};
154 0 $self->{h} = $tmp;
155 }
156 }
157 0 return ($self->{w}, $self->{h});
158 }
159  
160 =item B<regular_polygon> $sides
161  
162 Calculate the vertices of a regular polygon with $sides sides with
163 radius 1, along with the relative lengths of the inradius and edge.
164  
165 Returns a hashref:
166  
167 {
168 points => [ [$x0, $y0], ... ],
169 edge => $edge_length,
170 inradius => $inradius_length,
171 radius => 1,
172 }
173  
174 Calling the polygon($cx, $cy, $radius, $sides) method is equivalent to:
175  
176 $poly = regular_polygon($sides);
177 @points = map(@$_, @{$poly->{points}});
178 $pdf->save;
179 $pdf->translate($cx, $cy);
180 $pdf->scale($radius);
181 $pdf->poly(@points);
182 $pdf->close;
183 $pdf->restore;
184  
185 =cut
186  
187 sub regular_polygon {
188 1 ▶ use constant PI => 4 * atan2(1, 1);
Subroutine BEGIN: called CC=1 SCAR=0.0
189 0 ▶ my $sides = shift;
Subroutine regular_polygon: not called CC=2 SCAR=17.9
Pod documented
190 0 my $polygon = {};
191 0 my @points;
192 0 my $da = 2 * PI / $sides;
193 0 my $a = ($da - PI) / 2;
194 0 for my $side (1..$sides) {
195 0 push(@points, [cos($a), sin($a)]);
196 0 $a += $da;
197 }
198 0 $polygon->{points} = \@points;
199 0 $polygon->{radius} = 1;
200 0 $polygon->{edge} = 2 * sin(PI / $sides);
201 0 $polygon->{inradius} = cos(PI / $sides);
202 0 return $polygon;
203 }
204  
205 =back
206  
207 =head1 BUGS
208  
209 Gosh, none I hope.
210  
211 =head1 AUTHOR
212  
213 J Greely, C<< <jgreely at cpan.org> >>
214  
215 =cut
216  
217 1 1;
↑ Summary Cairo/Box.pm »