SVGout.pm

filter: Statementstmt 23.0% 27 / 117 Branchbran 7.4% 4 / 54 Conditioncond 0.0% 0 / 38 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 38.1% 8 / 21 Podpod 100% 9 / 9 totaltotal 18.5% 48 / 259 CC 44 SCAR 34.3
CC
44
Cov
15%
_start_out
110.0
rectangle
30.0
ellipse
30.0
CRAP 31.0
↑ Summary Multiplex.pm »
1 # Copyright 2011, 2012 Kevin Ryde
2  
3 # This file is part of Image-Base-Other.
4 #
5 # Image-Base-Other is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # Image-Base-Other is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Image-Base-Other. If not, see <http://www.gnu.org/licenses/>.
17  
18  
19 package Image::Base::SVGout;
20 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 2 ▶use Image::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 @ISA = ('Image::Base');
27  
28 2 $VERSION = 9;
29  
30 # uncomment this to run the ### lines
31 #use Devel::Comments '###';
32  
33 4 ▶sub new {
Subroutine new: called CC=4 SCAR=20.7
Pod documented
34 4 my ($class, %params) = @_;
35  
36 4 ▶ if (ref $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) 0 4 4
37 0 my $self = $class;
38 0 $class = ref $self;
39 0 ▶ if ($self->{'-filehandle'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"-filehandle"}) 0 0 0
40 0 croak "Cannot clone SVGout after drawing begun";
41 }
42 0 %params = (%$self, %params); # inherit
43 ### copy params: \%params
44 }
45  
46 4 ▶ if (defined $params{'-file'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"-file"}) 0 4 4
47 0 croak "Cannot load initial -file, Image::Base::SVGout is output-only";
48 }
49 4 return bless \%params, $class;
50 }
51  
52 4 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=8.1
53 4 my ($self) = @_;
54 4 ▶ if ($self->{'-filehandle'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"-filehandle"}) 0 4 4
55 0 $self->save; # closing </svg>
56 }
57 }
58  
59 1 ▶sub set {
Subroutine set: called CC=6 SCAR=30.1
Pod documented
60 1 my ($self, %param) = @_;
61 1 ▶ if ($self->{'-filehandle'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"-filehandle"}) 0 1 1
62 0 foreach my $key ('-file', '-title', '-description') {
63 0 ▶ if (exists $param{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param{$key}) 0 0 0
64 0 _nochange_str ($self, $key, $param{$key});
65 }
66 }
67 0 foreach my $key ('-width', '-height') {
68 0 ▶ if (exists $param{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param{$key}) 0 0 0
69 0 _nochange_str ($self, $key, $param{$key});
70 }
71 }
72 }
73 1 %$self = (%$self, %param);
74 }
75 0 ▶sub _nochange_str {
Subroutine _nochange_str: not called CC=4 SCAR=30.0
76 0 my ($self, $key, $newval) = @_;
77 0 ▶ if (defined $self->{$key}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$key} and !defined($newval) || $newval ne $self->{$key}) 0 0 0
Condition0%0/6
defined $self->{$key} and !defined($newval) || $newval ne $self->{$key}
!ll&&!rl&&r
000
not defined $newval or $newval ne $self->{$key}
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $self->{$key} and !defined($newval) || $newval ne $self->{$key}
defined $self->{$key} not defined $newval $newval ne $self->{$key}
Truth table0%0/4
A and not(B) or C
A= defined $self->{$key}B= not defined $newvalC= $newval ne $self->{$key}
ABCresult
0XX0
1000
11X1
1011
78 && (! defined $newval
79 || $newval ne $self->{$key})) {
80 0 croak "Cannot change $key after output has begun";
81 }
82 }
83 0 ▶sub _nochange_num {
Subroutine _nochange_num: not called CC=4 SCAR=30.0
84 0 my ($self, $key, $newval) = @_;
85 0 ▶ if (defined $self->{$key}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$key} and !defined($newval) || $newval != $self->{$key}) 0 0 0
Condition0%0/6
defined $self->{$key} and !defined($newval) || $newval != $self->{$key}
!ll&&!rl&&r
000
not defined $newval or $newval != $self->{$key}
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $self->{$key} and !defined($newval) || $newval != $self->{$key}
defined $self->{$key} not defined $newval $newval != $self->{$key}
Truth table0%0/4
A and not(B) or C
A= defined $self->{$key}B= not defined $newvalC= $newval != $self->{$key}
ABCresult
0XX0
1000
11X1
1011
86 && (! defined $newval
87 || $newval != $self->{$key})) {
88 0 croak "Cannot change $key after output has begun";
89 }
90 }
91  
92 0 ▶sub _out {
Subroutine _out: not called CC=3 SCAR=24.8
93 0 my $self = shift;
94 0 ▶ my $fh = $self->{'-filehandle'} || _start_out($self);
Condition0%0/3
$self->{'-filehandle'} || _start_out($self)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'-filehandle'} || _start_out($self)
$self->{'-filehandle'} _start_out($self)
Truth table0%0/3
A || B
A= $self->{'-filehandle'}B= _start_out($self)
ABresult
1X1
011
000
95 0 ▶ print $fh @_, "\n"
Branch: 1 branch
BranchTrueFalseTotal
print $fh @_, "\n" or croak(qq[Error writing $self->{"-file"}: $!]) 0 0 0
96 or croak "Error writing $self->{'-file'}: $!";
97 }
98  
99  
100  
101 # require Fcntl;
102 # sysopen FH, $filename, Fcntl::O_WRONLY() | Fcntl::O_TRUNC() | Fcntl::O_CREAT()
103 # or croak "Cannot create $filename: $!";
104 #
105 # if (! $self->save_fh (\*FH)) {
106 # my $err = "Error writing $filename: $!";
107 # { local $!; close FH; }
108 # croak $err;
109 # }
110 # close FH
111 # or croak "Error closing $filename: $!";
112  
113 0 ▶sub _start_out {
Subroutine _start_out: not called CC=10 SCAR=47.0
114 0 my ($self) = @_;
115  
116 0 ▶ if ($self->{'save_done'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"save_done"}) 0 0 0
117 0 croak "Cannot draw more after save()";
118 }
119  
120 0 my $filename = $self->{'-file'};
121 0 ▶ if (! defined $filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filename) 0 0 0
122 0 croak "No -file set";
123 }
124  
125 0 my $width = $self->{'-width'};
126 0 my $height = $self->{'-height'};
127 0 ▶ if (! defined $width || ! defined $height) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $width and defined $height) 0 0 0
Condition0%0/3
defined $width and defined $height
!ll&&!rl&&r
000
MC/DC0%0/2
defined $width and defined $height
defined $width defined $height
Truth table0%0/3
A and B
A= defined $widthB= defined $height
ABresult
0X0
100
111
128 0 croak "No -width / -height set";
129 }
130  
131 0 my $class = ref $self;
132 0 my $version = $self->VERSION;
133  
134 0 foreach ($width,$height,$class,$version) {
135 0 $_ = _entitize($_);
136 }
137  
138 0 my ($title, $description);
139 0 ▶ open my $fh, '>', $filename
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $filename or croak("Cannot write file ${filename}: $!") 0 0 0
140 or croak "Cannot write file $filename: $!";
141  
142 print $fh <<"HERE",
143 <?xml version="1.0" encoding="utf-8"?>
144 <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
145 <svg xmlns="http://www.w3.org/2000/svg" width="$width" height="$height">
146 <!-- Generated by $class version $version -->
147 HERE
148  
149 (defined ($title = $self->{'-title'})
150 ? ("<title>", _entitize($title), "</title>\n")
151 : ()),
152  
153 (defined ($description = $self->{'-description'})
154 0 ▶ ? ("<desc>", _entitize($self->{'-description'}), "</desc>")
Branch: 3 branches
BranchTrueFalseTotal
print $fh qq[<?xml version="1.0" encoding="utf-8"?>\n<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">\n<svg xmlns="http://www.w3.org/2000/svg" width="$width" height="$height">\n<!-- Generated by $class version $version -->\n], defined($title = $self->{"-title"}) ? ("<title>", _entitize($title), "</title>\n") : (), defined($description = $self->{"-description"}) ? ("<desc>", _entitize($self->{"-description"}), "</desc>") : () or croak("Error writing ${filename}: $!") 0 0 0
defined($title = $self->{'-title'}) ? : 0 0 0
defined($description = $self->{'-description'}) ? : 0 0 0
155 : ())
156  
157 or croak "Error writing $filename: $!";
158  
159 0 return ($self->{'-filehandle'} = $fh);
160 }
161  
162 0 ▶sub _close_out {
Subroutine _close_out: not called CC=3 SCAR=24.8
163 0 my ($self) = @_;
164  
165 0 ▶ if (my $fh = delete $self->{'-filehandle'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fh = delete $self->{"-filehandle"}) 0 0 0
166 0 ▶ close $fh or croak "Error closing $self->{'-file'}: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak(qq[Error closing $self->{"-file"}: $!]) 0 0 0
167 }
168 }
169  
170 0 ▶sub save {
Subroutine save: not called CC=2 SCAR=17.9
Pod documented
171 0 my ($self, $filename) = @_;
172 ### Image-Base-SVGout save(): @_
173 0 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 0 0 0
174 0 $self->set('-file', $filename);
175 } else {
176 0 $filename = $self->get('-file');
177 }
178 ### $filename
179  
180 0 _out ($self, "</svg>");
181 0 $self->{'save_done'} = 1;
182 0 _close_out ($self);
183 }
184  
185 0 ▶sub xy {
Subroutine xy: not called CC=2 SCAR=17.9
Pod documented
186 0 my ($self, $x, $y, $colour) = @_;
187 ### Image-Base-SVGout xy(): @_[1 .. $#_]
188  
189 0 ▶ if (@_ == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 3) { } 0 0 0
190 0 return undef; # no fetch
191 } else {
192 0 _out ($self,
193 '<rect x="', $x,
194 '" y="', $y,
195 '" width="1" height="1" fill="', _entitize($colour), '"/>');
196 }
197 }
198  
199 0 ▶sub rectangle {
Subroutine rectangle: not called CC=5 SCAR=34.0
Pod documented
200 0 my ($self, $x1,$y1, $x2,$y2, $colour, $fill) = @_;
201 ### Image-Base-SVGout rectangle(): @_[1 .. $#_]
202  
203 0 ▶ $fill ||= ($x1 == $x2 || $y1 == $y2); # 1xN or Nx1 done filled
Condition0%0/6
$fill ||= $x1 == $x2 || $y1 == $y2
l!l&&r!l&&!r
000
$x1 == $x2 || $y1 == $y2
l!l&&r!l&&!r
000
MC/DC0%0/3
$fill ||= $x1 == $x2 || $y1 == $y2
$fill $x1 == $x2 $y1 == $y2
Truth table0%0/4
A ||= B || C
A= $fillB= $x1 == $x2C= $y1 == $y2
ABCresult
1XX1
01X1
0011
0000
204 0 ▶ if (! $fill) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fill) 0 0 0
205 0 $x1 += .5; # for stroke width 1
206 0 $y1 += .5;
207 0 $x2 -= .5;
208 0 $y2 -= .5;
209 }
210 0 ▶ _out ($self,
Branch: 1 branch
BranchTrueFalseTotal
$fill ? : 0 0 0
211 '<rect x="', $x1,
212 '" y="', $y1,
213 '" width="', $x2-$x1+1,
214 '" height="', $y2-$y1+1, '" ',
215 ($fill?'fill':'stroke'), '="', _entitize($colour),
216 '"/>');
217 }
218  
219 0 ▶sub ellipse {
Subroutine ellipse: not called CC=5 SCAR=34.0
Pod documented
220 0 my ($self, $x1,$y1, $x2,$y2, $colour, $fill) = @_;
221 ### Image-Base-SVGout rectangle(): @_[1 .. $#_]
222  
223 0 ▶ $fill ||= ($x1 == $x2 || $y1 == $y2); # 1xN or Nx1 done filled
Condition0%0/6
$fill ||= $x1 == $x2 || $y1 == $y2
l!l&&r!l&&!r
000
$x1 == $x2 || $y1 == $y2
l!l&&r!l&&!r
000
MC/DC0%0/3
$fill ||= $x1 == $x2 || $y1 == $y2
$fill $x1 == $x2 $y1 == $y2
Truth table0%0/4
A ||= B || C
A= $fillB= $x1 == $x2C= $y1 == $y2
ABCresult
1XX1
01X1
0011
0000
224 0 my $rx = ($x2-$x1+1) / 2;
225 0 my $ry = ($y2-$y1+1) / 2;
226 0 ▶ if (! $fill) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fill) 0 0 0
227 0 $rx -= .5; # for stroke width 1
228 0 $ry -= .5;
229 }
230 0 ▶ _out ($self,
Branch: 1 branch
BranchTrueFalseTotal
$fill ? : 0 0 0
231 '<ellipse cx="' .(($x1+$x2+1) / 2),
232 '" cy="', (($y1+$y2+1) / 2),
233 '" rx="', $rx,
234 '" ry="', $ry,'" ',
235 ($fill?'fill':'stroke'),'="', _entitize($colour), '"/>');
236 }
237  
238 0 ▶sub line {
Subroutine line: not called CC=1 SCAR=6.9
Pod documented
239 0 my ($self, $x1,$y1, $x2,$y2, $colour, $fill) = @_;
240 ### Image-Base-SVGout rectangle(): @_[1 .. $#_]
241  
242 0 _out ($self,
243 '<line x1="', $x1+.5,
244 '" y1="', $y1+.5,
245 '" x2="', $x2+.5,
246 '" y2="', $y2+.5,
247 '" stroke="', _entitize($colour),
248 '" stroke-linecap="square"/>');
249 }
250  
251 0 ▶sub diamond {
Subroutine diamond: not called CC=5 SCAR=34.0
Pod documented
252 0 my ($self, $x1,$y1, $x2,$y2, $colour, $fill) = @_;
253 ### Image-Base-SVGout diamond(): @_[1 .. $#_]
254  
255 0 ▶ $fill ||= ($x1 == $x2 || $y1 == $y2); # 1xN or Nx1 done filled
Condition0%0/6
$fill ||= $x1 == $x2 || $y1 == $y2
l!l&&r!l&&!r
000
$x1 == $x2 || $y1 == $y2
l!l&&r!l&&!r
000
MC/DC0%0/3
$fill ||= $x1 == $x2 || $y1 == $y2
$fill $x1 == $x2 $y1 == $y2
Truth table0%0/4
A ||= B || C
A= $fillB= $x1 == $x2C= $y1 == $y2
ABCresult
1XX1
01X1
0011
0000
256 0 ▶ if ($fill) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fill) { } 0 0 0
257 0 $x2++;
258 0 $y2++;
259 } else {
260 0 $x1 += .5; # for stroke width 1
261 0 $y1 += .5;
262 0 $x2 += .5;
263 0 $y2 += .5;
264 }
265 0 my $xm = ($x1+$x2)/2;
266 0 my $ym = ($y1+$y2)/2;
267 0 ▶ _out ($self,
Branch: 1 branch
BranchTrueFalseTotal
$fill ? : 0 0 0
268 '<polygon points="',
269 $xm,',',$y1,' ',
270 $x1,',',$ym,' ',
271 $xm,',',$y2,' ',
272 $x2,',',$ym,'" ',
273 ($fill?'fill':'stroke'),'="', _entitize($colour), '"/>');
274 }
275  
276 0 ▶sub load {
Subroutine load: not called CC=1 SCAR=6.9
Pod documented
277 0 my ($self, $filename) = @_;
278 0 croak "Image::Base::SVGout is output-only";
279 }
280  
281 # Could leave wide chars as utf8 bytes, and latin1 bytes upgraded, if apply
282 # the right layers to the open and in new enough perl. For now send all
283 # non-ascii-printable to numbered.
284 #
285 2 my %entity = ('&' => '&amp;',
286 '"' => '&quot;',
287 '<' => '&lt;',
288 '>' => '&gt;',
289 );
290 0 ▶sub _entitize {
Subroutine _entitize: not called CC=2 SCAR=17.9
291 0 my ($value) = @_;
292 0 $value =~ s{([&"<>]|[^\t\r\n\x20-\x7F])}
293 0 ▶ { $entity{$1} || ('&#'.ord($1).';') }eg;
Condition0%0/2
$entity{$1} || '&#' . ord($1) . ';'
l!l
00
MC/DC0%0/1
$entity{$1} || '&#' . ord($1) . ';'
$entity{$1}
Truth table0%0/2
A
A= $entity{$1}
Aresult
11
00
294 0 return $value;
295 }
296  
297 2 1;
298 __END__
↑ Summary Multiplex.pm »