blib/lib/CGI/Imagemap.pm

filter: Statementstmt 89.1% 74 / 83 Branchbran 68.1% 30 / 44 Conditioncond 79.1% 38 / 48 MC/DCmcdc 26.9% 7 / 26 Subroutinesub 100% 10 / 10 Podpod 66.6% 2 / 3 totaltotal 75.2% 161 / 214 CC 38 SCAR 27.0
CC
38
Cov
81%
_poly
25.1
action
15.0
addmap
4.8
CRAP 14.8
↑ Summary
1 package CGI::Imagemap;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 $VERSION = 2.0;
5  
6 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
7 2 return bless {
8 DEFAULT => undef,
9 ISMAP => [],
10 MINDIST => -1
11 }, shift;
12 }
13  
14 3 ▶sub addmap {
Subroutine addmap: called CC=3 SCAR=15.7
Pod documented
15 3 my $self = shift;
16 3 ▶ if( $_[0] eq '-file' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] eq "-file") 0 3 3
17 0 ▶ open(my $map, $_[1]) || die("Unable to open map '$_[1]': $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $map, $_[1] or die "Unable to open map '$_[1]': $!" 0 0 0
18 0 @_ = grep {! /^\s*(?:#|$)/ } <$map>;
19 0 close($map);
20 }
21 3 push @{$self->{ISMAP}}, @_;
22 }
23  
24  
25 8 ▶sub action {
Subroutine action: called CC=15 SCAR=27.1
Pod documented
26 8 my($self, $x, $y) = @_;
27  
28 8 $self->{MINDIST} = -1;
29
30 8 ▶ die "No map specified" unless defined($self->{ISMAP});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"ISMAP"} 0 8 8
31  
32 8 POINT: foreach ( @{$self->{ISMAP}} ){
33 31 my ($shape, $URI, $points) = split(/\s+/, $_, 3);
34 31 ▶ $self->{DEFAULT} ||= $URI if $shape =~ /default/i;
Branch: 1 branch
BranchTrueFalseTotal
if $shape =~ /default/i 2 29 31
Condition66%2/3
$self->{"DEFAULT"} ||= $URI
l!l&&r!l&&!r
110
MC/DC0%0/2
$self->{"DEFAULT"} ||= $URI
$self->{"DEFAULT"} $URI
Truth table66%2/3
A ||= B
A= $self->{"DEFAULT"}B= $URI
ABresult
1X1
011
000
35 31 ▶ $self->_rect ($x, $y, $points) && return $URI if $shape =~ /rect/i;
Branch: 1 branch
BranchTrueFalseTotal
if $shape =~ /rect/i 7 24 31
Condition100%4/4
$shape =~ /rect/i && ($self->_rect($x, $y, $points) && (return $URI))
!ll
247
$self->_rect($x, $y, $points) and return $URI
!ll
61
MC/DC0%0/2
$shape =~ /rect/i && ($self->_rect($x, $y, $points) && (return $URI))
$shape =~ /rect/i $self->_rect($x, $y, $points)
Truth table66%2/3
A && B
A= $shape =~ /rect/iB= $self->_rect($x, $y, $points)
ABresult
0X0
100
111
36 30 ▶ $self->_poly ($x, $y, $points) && return $URI if $shape =~ /poly/i;
Branch: 1 branch
BranchTrueFalseTotal
if $shape =~ /poly/i 3 27 30
Condition100%4/4
$shape =~ /poly/i && ($self->_poly($x, $y, $points) && (return $URI))
!ll
273
$self->_poly($x, $y, $points) and return $URI
!ll
21
MC/DC0%0/2
$shape =~ /poly/i && ($self->_poly($x, $y, $points) && (return $URI))
$shape =~ /poly/i $self->_poly($x, $y, $points)
Truth table66%2/3
A && B
A= $shape =~ /poly/iB= $self->_poly($x, $y, $points)
ABresult
0X0
100
111
37 29 ▶ $self->_circle($x, $y, $points) && return $URI if $shape =~ /circle/i;
Branch: 1 branch
BranchTrueFalseTotal
if $shape =~ /circle/i 5 24 29
Condition100%4/4
$shape =~ /circle/i && ($self->_circle($x, $y, $points) && (return $URI))
!ll
245
$self->_circle($x, $y, $points) and return $URI
!ll
32
MC/DC0%0/2
$shape =~ /circle/i && ($self->_circle($x, $y, $points) && (return $URI))
$shape =~ /circle/i $self->_circle($x, $y, $points)
Truth table66%2/3
A && B
A= $shape =~ /circle/iB= $self->_circle($x, $y, $points)
ABresult
0X0
100
111
38 27 ▶ $self->_oval ($x, $y, $points) && return $URI if $shape =~ /oval/i;
Branch: 1 branch
BranchTrueFalseTotal
if $shape =~ /oval/i 6 21 27
Condition100%4/4
$shape =~ /oval/i && ($self->_oval($x, $y, $points) && (return $URI))
!ll
216
$self->_oval($x, $y, $points) and return $URI
!ll
51
MC/DC0%0/2
$shape =~ /oval/i && ($self->_oval($x, $y, $points) && (return $URI))
$shape =~ /oval/i $self->_oval($x, $y, $points)
Truth table66%2/3
A && B
A= $shape =~ /oval/iB= $self->_oval($x, $y, $points)
ABresult
0X0
100
111
39 $self->_point ($x, $y, $points) &&
40 26 ▶ ($self->{DEFAULT} = $URI) if $shape =~ /point/i;
Branch: 1 branch
BranchTrueFalseTotal
if $shape =~ /point/i 8 18 26
Condition80%4/5
$shape =~ /point/i && ($self->_point($x, $y, $points) && ($self->{'DEFAULT'} = $URI))
!ll
188
$self->_point($x, $y, $points) and $self->{"DEFAULT"} = $URI
!ll&&!rl&&r
170
MC/DC0%0/3
$shape =~ /point/i && ($self->_point($x, $y, $points) && ($self->{'DEFAULT'} = $URI))
$shape =~ /point/i $self->_point($x, $y, $points) $self->{"DEFAULT"} = $URI
Truth table0%0/4
A && B and C
A= $shape =~ /point/iB= $self->_point($x, $y, $points)C= $self->{"DEFAULT"} = $URI
ABCresult
0XX0
10X0
1100
1111
41 }
42 3 return $self->{DEFAULT};
43 }
44  
45 # set default action if this point is the closest so far
46 # does not check for validity of parameters
47 8 ▶sub _point {
Subroutine _point: called CC=3 SCAR=11.0
48 8 my($self, $x, $y, $target) = @_;
49 8 my($dist2, $Tx, $Ty) = 0;
50  
51 8 ($Tx, $Ty) = $target =~ m/(\d+),\s*(\d+)/;
52 8 $dist2 = ($x - $Tx)**2 + ($y - $Ty)**2;
53  
54 8 ▶ if( $self->{MINDIST} == -1 || $dist2 < $self->{MINDIST} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"MINDIST"} == -1 or $dist2 < $self->{"MINDIST"}) 7 1 8
Condition100%3/3
$self->{"MINDIST"} == -1 or $dist2 < $self->{"MINDIST"}
l!l&&r!l&&!r
611
MC/DC100%2/2
$self->{"MINDIST"} == -1 or $dist2 < $self->{"MINDIST"}
$self->{"MINDIST"} == -1 $dist2 < $self->{"MINDIST"}
Truth table100%3/3
A or B
A= $self->{"MINDIST"} == -1B= $dist2 < $self->{"MINDIST"}
ABresult
1X1
011
000
55 7 $self->{MINDIST} = $dist2;
56 7 return 1;
57 }
58 1 return 0;
59 }
60  
61 # return true if point is in given rectangle
62 7 ▶sub _rect {
Subroutine _rect: called CC=4 SCAR=14.0
63 7 my($self, $x, $y, $target) = @_;
64 7 my($ulx,$uly,$llx,$lly) = $target =~ m/(\d+),(\d+)\s+(\d+),(\d+)/;
65  
66 7 ▶ return ($x >= $ulx && $y >= $uly && $x <= $llx && $y <= $lly);
Condition77%7/9
$x >= $ulx && $y >= $uly && $x <= $llx && $y <= $lly
!ll&&!rl&&r
601
$x >= $ulx and $y >= $uly and $x <= $llx
!ll&&!rl&&r
241
$x >= $ulx and $y >= $uly
!ll&&!rl&&r
205
MC/DC50%2/4
$x >= $ulx && $y >= $uly && $x <= $llx && $y <= $lly
$x >= $ulx $y >= $uly $x <= $llx $y <= $lly
Truth table60%3/5
A and B and C && D
A= $x >= $ulxB= $y >= $ulyC= $x <= $llxD= $y <= $lly
ABCDresult
0XXX0
10XX0
110X0
11100
11111
67 }
68  
69 6 ▶sub _oval{
Subroutine _oval: called CC=1 SCAR=0.0
70 6 my($self, $x, $y, $target) = @_;
71 6 my($cx, $cy, $major, $minor) = $target =~ m/(\d+),(\d+)\s+(\d+),(\d+)/;
72  
73 # Ellipse equation: (x-$cx)^2/major^2 + (y-$cy)^2/minor^2 = 1
74 # If the point ($x,$y) plugged into the equation is > 1 =>
75 # pt is outside, if <=1, pt. is inside..
76 6 return (($x-$cx)**2/$major**2 + ($y-$cy)**2/$minor**2)<=1;
77 }
78  
79 # return true if point is in circle
80 5 ▶sub _circle {
Subroutine _circle: called CC=1 SCAR=0.0
81 5 my($self, $x, $y, $target) = @_;
82 5 my($cx,$cy,$ex,$ey) = $target =~ m/(\d+),(\d+)\s+(\d+),(\d+)/;
83  
84 5 my($distanceP,$distanceE);
85  
86 # compare squares of distance from center of edgepoint and given point
87  
88 5 $distanceP = ($cx - $x)**2 + ($cy - $y)**2;
89 5 $distanceE = ($cx - $ex)**2 + ($cy - $ey)**2;
90  
91 5 return ($distanceP <= $distanceE);
92 }
93  
94 # return true if point is in given polygon
95 3 ▶sub _poly {
Subroutine _poly: called CC=17 SCAR=32.2
96 3 my($self, $x, $y, $target) = @_;
97 3 my($pn, @px, @py) = 0;
98 3 my($i,$intersections,$dy,$dx,$b,$m,$x1,$y1,$x2,$y2);
99  
100 # We'll treat the test point as the origin, so translate each
101 # point in the polygon appropriately
102 3 ▶ while($target =~ s/\s*(\d+),(\d+),?//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target =~ s/\s*(\d+),(\d+),?//) 9 3 12
103 9 $px[$pn] = $1 - $x;
104 9 $py[$pn] = $2 - $y;
105 9 $pn++;
106 }
107  
108 # A polygon with less than 3 points is an error
109 3 ▶ return 0 if $pn < 3;
Branch: 1 branch
BranchTrueFalseTotal
if $pn < 3 0 3 3
110  
111 # Close the polygon
112 3 $px[$pn] = $px[0];
113 3 $py[$pn] = $py[0];
114  
115 # Now count the number of line segments in the polygon that intersect
116 # the left side of the X axis. If it's an odd number we are inside the
117 # polygon.
118  
119 # Assume no intersection
120 3 $intersections=0;
121  
122 3 ▶ for($i = 0; $i < $pn; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $pn) 9 3 12
123 9 $x1 = $px[$i ]; $y1 = $py[$i ];
124 9 $x2 = $px[$i+1]; $y2 = $py[$i+1];
125  
126 # Line is completely to the right of the Y axis
127 9 ▶ next if( ($x1>0) && ($x2>0) );
Branch: 1 branch
BranchTrueFalseTotal
if $x1 > 0 and $x2 > 0 4 5 9
Condition100%3/3
$x1 > 0 and $x2 > 0
!ll&&!rl&&r
414
MC/DC100%2/2
$x1 > 0 and $x2 > 0
$x1 > 0 $x2 > 0
Truth table100%3/3
A and B
A= $x1 > 0B= $x2 > 0
ABresult
0X0
100
111
128  
129 # Line doesn't intersect the X axis at all
130 5 ▶ next if( (($y1<=>0)==($y2<=>0)) && (($y1!=0)&&($y2!=0)) );
Branch: 1 branch
BranchTrueFalseTotal
if ($y1 <=> 0) == ($y2 <=> 0) and ($y1 != 0 and $y2 != 0) 4 1 5
Condition50%3/6
($y1 <=> 0) == ($y2 <=> 0) and ($y1 != 0 and $y2 != 0)
!ll&&!rl&&r
104
$y1 != 0 and $y2 != 0
!ll&&!rl&&r
004
MC/DC33%1/3
($y1 <=> 0) == ($y2 <=> 0) and ($y1 != 0 and $y2 != 0)
($y1 <=> 0) == ($y2 <=> 0) $y1 != 0 $y2 != 0
Truth table50%2/4
A and B and C
A= ($y1 <=> 0) == ($y2 <=> 0)B= $y1 != 0C= $y2 != 0
ABCresult
0XX0
10X0
1100
1111
131  
132 # Special case.. if the Y on the bottom=0, we ignore this intersection
133 # (otherwise a line endpoint counts as 2 hits instead of 1)
134 1 ▶ if( $y2>$y1 ){
Branch: 2 branches
BranchTrueFalseTotal
if ($y2 > $y1) { } 0 1 1
elsif ($y1 > $y2) { } 1 0 1
135 0 ▶ next if $y2==0;
Branch: 1 branch
BranchTrueFalseTotal
if $y2 == 0 0 0 0
136 }
137 elsif( $y1>$y2 ){
138 1 ▶ next if $y1==0;
Branch: 1 branch
BranchTrueFalseTotal
if $y1 == 0 0 1 1
139 }
140 else {
141 # Horizontal span overlaying the X axis. Consider it an intersection
142 # iff. it extends into the left side of the X axis
143 0 ▶ $intersections++ if ( ($x1 < 0) || ($x2 < 0) );
Branch: 1 branch
BranchTrueFalseTotal
if $x1 < 0 or $x2 < 0 0 0 0
Condition0%0/3
$x1 < 0 or $x2 < 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$x1 < 0 or $x2 < 0
$x1 < 0 $x2 < 0
Truth table0%0/3
A or B
A= $x1 < 0B= $x2 < 0
ABresult
1X1
011
000
144 0 next;
145 }
146  
147 # We know line must intersect the X axis, so see where
148 1 $dx = $x2 - $x1;
149  
150 # Special case.. if a vertical line, it intersects
151 1 ▶ unless ( $dx ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dx) 0 1 1
152 0 $intersections++;
153 0 next;
154 }
155  
156 1 $dy = $y2 - $y1;
157 1 $m = $dy / $dx;
158 1 $b = $y2 - $m * $x2;
159 1 ▶ next if ( ( (0 - $b) / $m ) > 0 );
Branch: 1 branch
BranchTrueFalseTotal
if (0 - $b) / $m > 0 0 1 1
160  
161 1 $intersections++;
162 }
163  
164 # If there were an odd number of intersections to the left of the origin
165 # (the clicked-on point) then it is within the polygon
166 3 return ($intersections % 2);
167 }
168  
169 2 1;
170  
171 __END__
↑ Summary