blib/lib/GD/Window.pm

filter: Statementstmt 25.7% 25 / 97 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 29.4% 5 / 17 Podpod 20.0% 2 / 10 totaltotal 16.5% 32 / 193 CC 31 SCAR 45.7
CC
31
Cov
16%
new
210.0
AUTOLOAD
132.0
boundsSafe
20.0
CRAP 96.5
↑ Summary
1 package GD::Window;
2  
3 1 ▶use 5.008006;
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 qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use vars qw/$VERSION $AUTOLOAD/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 require Exporter;
10  
11 1 our @ISA = qw(Exporter);
12  
13 1 our %EXPORT_TAGS = ( 'all' => [ qw(
14
15 ) ] );
16  
17 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
18  
19 1 our @EXPORT = qw(
20
21 );
22  
23 1 our $VERSION = '0.02';
24  
25  
26 1 my %imWindowedFuncs = (
27 setPixel => {x => [0], 'y' => [1]},
28 line => {x => [0,2], 'y' => [1,3]},
29 dashedLine => {x => [0,2], 'y' => [1,3]},
30 rectangle => {x => [0,2], 'y' => [1,3]},
31 filledRectangle => {x => [0,2], 'y' => [1,3]},
32 ellipse => {x => [0], 'y' => [1], w => [2], h => [3]},
33 filledEllipse => {x => [0], 'y' => [1], w => [2], h => [3]},
34 arc => {x => [0], 'y' => [1], w => [2], h => [3]},
35 filledArc => {x => [0], 'y' => [1], w => [2], h => [3]},
36 fill => {x => [0], 'y' => [1]},
37 fillToBorder => {x => [0], 'y' => [1]},
38 copy => {x => [1], 'y' => [2]},
39 copyMerge => {x => [1], 'y' => [2]},
40 copyMergeGray => {x => [1], 'y' => [2]},
41 copyResized => {x => [1], 'y' => [2], w => [5], h => [6]},
42 copyResampled => {x => [1], 'y' => [2], w => [5], h => [6]},
43 copyRotated => {x => [1], 'y' => [2]},
44 string => {x => [1], 'y' => [2]},
45 stringUp => {x => [1], 'y' => [2]},
46 char => {x => [1], 'y' => [2]},
47 charUp => {x => [1], 'y' => [2]},
48 stringFT => {x => [4], 'y' => [5]},
49 stringFTCircle => {x => [0], 'y' => [1]},
50 clip => {x => [0,2], 'y' => [1,3]},
51 );
52  
53 1 my %imWindowedPolyFuncs = (openPolygon => 1,
54 unclosedPolygon => 1,
55 filledPolygon => 1);
56  
57  
58 1 my $invertY_g = 0;
59  
60  
61 0 ▶sub new {
Subroutine new: not called CC=14 SCAR=53.5
Pod documented
62 0 my $that = shift;
63 0 ▶ my $class = ref($that) || $that;
Condition0%0/3
ref $that || $that
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table0%0/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
64 0 my ($im, $imX1, $imY1, $imX2, $imY2,
65 $winX1, $winY1, $winX2, $winY2,
66 %args) = @_;
67  
68 0 ▶ if (scalar(@_) < 9) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ < 9) 0 0 0
69 0 croak "Missing some arguments for new GD::Window";
70 }
71  
72 # Fill in the window's boundary
73 my $self = { im => $im,
74 minX => $winX1 > $winX2 ? $winX2 : $winX1,
75 minY => $winY1 > $winY2 ? $winY2 : $winY1,
76 maxX => $winX1 > $winX2 ? $winX1 : $winX2,
77 maxY => $winY1 > $winY2 ? $winY1 : $winY2,
78 imMinX => $imX1 > $imX2 ? $imX2 : $imX1,
79 imMinY => $imY1 > $imY2 ? $imY2 : $imY1,
80 imMaxX => $imX1 > $imX2 ? $imX1 : $imX2,
81 imMaxY => $imY1 > $imY2 ? $imY1 : $imY2,
82 passThroughIfUnsupported => $args{passThrough},
83 useImage => $args{useImage},
84 0 ▶ invertY => defined $args{invertY} ? $args{invertY} : $invertY_g,
Branch: 9 branches
BranchTrueFalseTotal
$winX1 > $winX2 ? : 0 0 0
$winY1 > $winY2 ? : 0 0 0
$winX1 > $winX2 ? : 0 0 0
$winY1 > $winY2 ? : 0 0 0
$imX1 > $imX2 ? : 0 0 0
$imY1 > $imY2 ? : 0 0 0
$imX1 > $imX2 ? : 0 0 0
$imY1 > $imY2 ? : 0 0 0
defined $args{'invertY'} ? : 0 0 0
85 };
86  
87 0 ▶ if ($self->{useImage}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'useImage'}) { } 0 0 0
88 # Need to make our own internal image
89 0 $self->{parentIm} = $im;
90 0 $self->{im} = GD::Image->new(($self->{maxX} - $self->{minX}), ($self->{maxY} - $self->{minY}), 1);
91 0 $self->{scaleX} = 1;
92 0 $self->{scaleY} = 1;
93 0 $self->{srcImMinX} = $self->{imMinX};
94 0 $self->{srcImMinY} = $self->{imMinY};
95 0 $self->{imMinX} = 0;
96 0 $self->{imMinY} = 0;
97 }
98 else {
99 0 $self->{scaleX} = ($self->{imMaxX} - $self->{imMinX})/($self->{maxX} - $self->{minX});
100 0 $self->{scaleY} = ($self->{imMaxY} - $self->{imMinY})/($self->{maxY} - $self->{minY});
101 }
102  
103 # print "PostSub: $postSub\n";
104 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
105 0 die "Failed eval of autoloaded sub: $@";
106 }
107  
108 0 bless ($self, $class);
109 0 return $self;
110 }
111  
112  
113 ##
114 # The autoload function catches all the supported image functions and
115 # creates the appropriate transformations for them.
116 #
117 # If passThrough is defined on window creation, then unsupported
118 # functions will simply be forwarded to the image. Otherwise,
119 # we will croak.
120 ##
121 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=11 SCAR=48.8
122 0 my ($self) = @_;
123 0 my ($name) = ($AUTOLOAD =~ /::([^:]+)$/);
124 0 my @args = @_;
125  
126 0 ▶ if (exists $imWindowedFuncs{$name}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $imWindowedFuncs{$name}) { } 0 0 0
elsif (exists $imWindowedPolyFuncs{$name}) { } 0 0 0
127 0 my $fi = $imWindowedFuncs{$name};
128  
129 # Create the function that should be called
130 0 my $body = " my \$s = shift;my \@args = \@_;\n";
131  
132 0 foreach my $dim (qw(x y w h)) {
133 0 ▶ if ($fi->{$dim}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fi->{$dim}) 0 0 0
134 0 foreach my $idx (@{$fi->{$dim}}) {
135 0 $body .= " \$args[$idx] = \$s->translate". uc($dim) ."(\$args[$idx]);\n";
136 }
137 }
138 }
139 # $body .= " print \"calling $name with \$s->{im} -> \@args\\n\"; my \$res = \$s->{im}->$name(\@args); \$s->postRenderAdjustment(); return \$res; \n";
140 0 $body .= " my \$res = \$s->{im}->$name(\@args); \$s->postRenderAdjustment(); return \$res; \n";
141 # print "Eval: $body\n";
142 0 eval "sub $name { $body }; return $name(\@args);";
143 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
144 0 die "Failed eval of autoloaded sub: $@";
145 }
146 }
147 elsif (exists $imWindowedPolyFuncs{$name}) {
148  
149 0 my $body = qq ^
150 my (\$self, \$poly, \$color) = \@_;
151 my \$transPoly = GD::Polygon->new;
152  
153 # Need to go through all the points and adjust them
154 foreach my \$vertex (\$poly->vertices) {
155 \$transPoly->addPt(\$self->translateX(\$vertex->[0]), \$self->translateY(\$vertex->[1]));
156 }
157 \$self->{im}->$name(\$transPoly, \$color);
158 ^;
159 # print "Adding \@{\$vertex}\n";
160 # my \@v = \$transPoly->vertices;
161 #foreach my \$v (\@v) {
162 # print "poly: \@{\$v}\n";
163 #}
164  
165 # print "BODY: $body\n";
166 0 eval "sub $name { $body }; return $name(\@args);";
167 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
168 0 die "Failed eval of autoloaded sub: $@";
169 }
170  
171 }
172 else {
173 0 ▶ if ($self->{passThroughIfUnsupported} || $self->{useImage}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'passThroughIfUnsupported'} or $self->{'useImage'}) { } 0 0 0
Condition0%0/3
$self->{"passThroughIfUnsupported"} or $self->{"useImage"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"passThroughIfUnsupported"} or $self->{"useImage"}
$self->{"passThroughIfUnsupported"} $self->{"useImage"}
Truth table0%0/3
A or B
A= $self->{"passThroughIfUnsupported"}B= $self->{"useImage"}
ABresult
1X1
011
000
174 0 eval("sub $name { my \$s = shift; my \$res = \$s->{im}->$name(\@_); \$s->postRenderAdjustment(); return \$res;}; return $name(\@_);");
175 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
176 0 die "Failed eval of autoloaded sub: $@";
177 }
178 }
179 else {
180 0 croak "Sub $name is not supported by GD::Window";
181 }
182 }
183 }
184  
185  
186  
187 ##
188 # Methods that can't be handled by the AUTOLOAD
189 ##
190 0 ▶sub boundsSafe {
Subroutine boundsSafe: not called CC=4 SCAR=30.0
Pod undocumented
191 0 my ($self, $x, $y) = @_;
192 return ($x >= $self->{minX} &&
193 $x <= $self->{maxX} &&
194 $y >= $self->{minY} &&
195 0 ▶ $y <= $self->{maxY});
Condition0%0/9
$x >= $self->{'minX'} && $x <= $self->{'maxX'} && $y >= $self->{'minY'} && $y <= $self->{'maxY'}
!ll&&!rl&&r
000
$x >= $self->{"minX"} and $x <= $self->{"maxX"} and $y >= $self->{"minY"}
!ll&&!rl&&r
000
$x >= $self->{"minX"} and $x <= $self->{"maxX"}
!ll&&!rl&&r
000
MC/DC0%0/4
$x >= $self->{'minX'} && $x <= $self->{'maxX'} && $y >= $self->{'minY'} && $y <= $self->{'maxY'}
$x >= $self->{"minX"} $x <= $self->{"maxX"} $y >= $self->{"minY"} $y <= $self->{'maxY'}
Truth table0%0/5
A and B and C && D
A= $x >= $self->{"minX"}B= $x <= $self->{"maxX"}C= $y >= $self->{"minY"}D= $y <= $self->{'maxY'}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
196 }
197  
198 0 ▶sub bounds {
Subroutine bounds: not called CC=2 SCAR=17.9
Pod undocumented
199 0 my ($self) = @_;
200 0 ▶ if (!$self->{invertY}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'invertY'}) { } 0 0 0
201 return($self->{minX},
202 $self->{minY},
203 $self->{maxX},
204 0 $self->{maxY});
205 }
206 else {
207 return($self->{minX},
208 $self->{maxY},
209 $self->{maxX},
210 0 $self->{minY});
211 }
212 }
213  
214  
215 0 ▶sub dimensions {
Subroutine dimensions: not called CC=1 SCAR=6.9
Pod undocumented
216 0 my ($self) = @_;
217 return($self->{maxX} - $self->{minX},
218 0 $self->{maxY} - $self->{minY});
219 }
220  
221  
222 ##
223 # Public Methods for changing globals
224 ##
225 0 ▶sub invertY {
Subroutine invertY: not called CC=2 SCAR=17.9
Pod documented
226 0 my ($that, $val) = @_;
227  
228 0 ▶ if (ref($that)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $that) { } 0 0 0
229 0 $that->{invertY} = $val;
230 }
231 else {
232 0 $invertY_g = $val;
233 }
234  
235 }
236  
237  
238 ##
239 # Private Methods
240 ##
241 0 ▶sub translateX {
Subroutine translateX: not called CC=1 SCAR=6.9
Pod undocumented
242 0 my ($self, $x) = @_;
243 0 my $newX = ($x - $self->{minX}) * $self->{scaleX} + $self->{imMinX};
244 # print "translate from $x to $newX, $self->{scaleX}, $self->{minX}, $self->{imMinX}\n";
245 0 return $newX;
246 }
247  
248 0 ▶sub translateY {
Subroutine translateY: not called CC=2 SCAR=17.9
Pod undocumented
249 0 my ($self, $y) = @_;
250 0 ▶ if ($self->{invertY}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"invertY"}) 0 0 0
251 0 $y = $self->{maxY} - $y;
252 }
253 0 my $newY = ($y - $self->{minY}) * $self->{scaleY} + $self->{imMinY};
254 # print "translate from $y to $newY, $self->{scaleY}\n";
255 0 return $newY;
256 }
257  
258 0 ▶sub translateW {
Subroutine translateW: not called CC=1 SCAR=6.9
Pod undocumented
259 0 my ($self, $w) = @_;
260 0 return $w * $self->{scaleX};
261 }
262  
263 0 ▶sub translateH {
Subroutine translateH: not called CC=1 SCAR=6.9
Pod undocumented
264 0 my ($self, $h) = @_;
265 0 return $h * $self->{scaleY};
266 }
267  
268 0 ▶sub postRenderAdjustment {
Subroutine postRenderAdjustment: not called CC=2 SCAR=17.9
Pod undocumented
269 0 my $self = shift;
270 0 ▶ return if !$self->{useImage};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"useImage"} 0 0 0
271 $self->{parentIm}->copyResampled(
272 $self->{im}, $self->{srcImMinX}, $self->{srcImMinY}, 0, 0,
273 ($self->{imMaxX} - $self->{srcImMinX}), ($self->{imMaxY} - $self->{srcImMinY}),
274 0 ($self->{maxX} - $self->{minX}), ($self->{maxY} - $self->{minY}));
275 };
276  
277  
278 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
279  
280  
281 1 1;
282 __END__
↑ Summary