blib/lib/Image/Base/SVG.pm

filter: Statementstmt 90.2% 111 / 123 Branchbran 79.1% 38 / 48 Conditioncond 78.2% 18 / 23 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 100% 21 / 21 Podpod 90.0% 9 / 10 totaltotal 83.1% 197 / 237 CC 33 SCAR 14.0
CC
33
Cov
86%
save
8.1
new
5.2
set
5.1
CRAP 4.1
↑ Summary
1 # Copyright 2011, 2012, 2019 Kevin Ryde
2  
3 # This file is part of Image-Base-SVG.
4 #
5 # Image-Base-SVG 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-SVG 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-SVG. If not, see <http://www.gnu.org/licenses/>.
17  
18  
19 package Image::Base::SVG;
20 3 ▶use 5.006; # SVG is 5.6 for weakening
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use SVG; # version 2.50 needs an import() to create methods
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 3 ▶use vars '$VERSION', '@ISA';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 $VERSION = 5;
27  
28 3 ▶use Image::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 @ISA = ('Image::Base');
30  
31 # uncomment this to run the ### lines
32 # use Smart::Comments '###';
33  
34  
35 25 ▶sub new {
Subroutine new: called CC=5 SCAR=16.6
Pod documented
36 25 my ($class, %params) = @_;
37 ### Image-Base-SVG new(): %params
38  
39 # $obj->new(...) means make a copy, with some extra settings
40 25 ▶ if (ref $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) 0 25 25
41 0 my $self = $class;
42 0 $class = ref $self;
43 0 croak "Cannot clone $class yet ..."
44  
45 # if (! defined $params{'-svg_object'}) {
46 # $params{'-svg_object'} = $self->{'-svg_object'}->cloneNode;
47 # }
48 # # inherit everything else
49 # %params = (%$self, %params);
50 # ### copy params: \%params
51 }
52  
53 25 my $svg = delete $params{'-svg_object'};
54 25 ▶ if (! $svg) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($svg) 24 1 25
55 $svg = SVG->new ((exists $params{'-width'}
56 ? (width => delete $params{'-width'})
57 : ()),
58 (exists $params{'-height'} ?
59 24 ▶ (height => delete $params{'-height'})
Branch: 2 branches
BranchTrueFalseTotal
exists $params{'-width'} ? : 17 7 24
exists $params{'-height'} ? : 17 7 24
60 : ()));
61 }
62  
63 25 my $self = bless { -svg_object => $svg }, $class;
64 ### %params
65 25 $self->set (%params);
66 25 return $self;
67 }
68  
69 # these two not documented yet
70 3 my %key_to_cdata = ('-title' => 'title',
71 '-description' => 'desc');
72  
73 3 my %key_to_attribute = ('-width' => 'width',
74 '-height' => 'height');
75 31 ▶sub _get {
Subroutine _get: called CC=4 SCAR=14.0
76 31 my ($self, $key) = @_;
77 ### _get(): $key
78  
79 31 ▶ if (my $tagname = $key_to_cdata{$key}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $tagname = $key_to_cdata{$key}) { } 8 23 31
elsif (my $aname = $key_to_attribute{$key}) { } 4 19 23
80 8 my $elem;
81 8 ▶ return (($elem = _get_tag($self,$tagname))
Condition33%1/3
($elem = _get_tag($self, $tagname)) && $elem->getCDATA
!ll&&!rl&&r
008
MC/DC0%0/2
($elem = _get_tag($self, $tagname)) && $elem->getCDATA
($elem = _get_tag($self, $tagname)) $elem->getCDATA
Truth table33%1/3
A && B
A= ($elem = _get_tag($self, $tagname))B= $elem->getCDATA
ABresult
0X0
100
111
82 && $elem->getCDATA);
83  
84 } elsif (my $aname = $key_to_attribute{$key}) {
85 4 return _svg_element($self)->getAttribute ($aname);
86  
87 } else {
88 19 return $self->{$key};
89 }
90 }
91  
92 29 ▶sub set {
Subroutine set: called CC=5 SCAR=16.3
Pod documented
93 29 my $self = shift;
94 29 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 7 29 36
95 7 my $key = shift;
96 7 ▶ @_ or croak "Odd number of arguments to set()";
Branch: 1 branch
BranchTrueFalseTotal
@_ or croak("Odd number of arguments to set()") 7 0 7
97 7 my $value = shift;
98  
99 7 ▶ if (my $tagname = $key_to_cdata{$key}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $tagname = $key_to_cdata{$key}) { } 4 3 7
elsif (my $aname = $key_to_attribute{$key}) { } 0 3 3
100 4 my $elem = _get_or_create_tag($self,$tagname);
101 4 $elem->cdata ($value);
102  
103 } elsif (my $aname = $key_to_attribute{$key}) {
104 ### $aname
105 ### $value
106 0 _svg_element($self)->setAttribute ($aname, $value);
107  
108 } else {
109 3 $self->{$key} = $value;
110 }
111 }
112 }
113 8 ▶sub _get_tag {
Subroutine _get_tag: called CC=1 SCAR=0.0
114 8 my ($self,$tagname) = @_;
115 8 my $svg = $self->{'-svg_object'};
116 8 return ($svg->getElements($tagname))[0];
117 }
118 4 ▶sub _get_or_create_tag {
Subroutine _get_or_create_tag: called CC=2 SCAR=7.2
119 4 my ($self,$tagname) = @_;
120 4 my $svg = $self->{'-svg_object'};
121 4 my @elems = $svg->getElements($tagname);
122 4 ▶ if (@elems) {
Branch: 1 branch
BranchTrueFalseTotal
if (@elems) { } 0 4 4
123 0 return $elems[0];
124 } else {
125 4 return $svg->tag($tagname);
126 }
127 }
128 4 ▶sub _svg_element {
Subroutine _svg_element: called CC=2 SCAR=7.1
129 4 my ($self) = @_;
130 4 my $svg = $self->{'-svg_object'};
131 ### docroot: $svg->{'-docroot'}
132 ### elems: join(',',$svg->getElements())
133  
134 4 ▶ return ($svg->getElements($svg->{'-docroot'}))[0]
Condition50%1/2
($svg->getElements($svg->{'-docroot'}))[0] || die('Oops, -docroot element not found')
l!l
40
MC/DC0%0/1
($svg->getElements($svg->{'-docroot'}))[0] || die('Oops, -docroot element not found')
($svg->getElements($svg->{'-docroot'}))[0]
Truth table50%1/2
A
A= ($svg->getElements($svg->{'-docroot'}))[0]
Aresult
11
00
135 || die "Oops, -docroot element not found";
136 }
137  
138 4 ▶sub xy {
Subroutine xy: called CC=2 SCAR=6.9
Pod documented
139 4 my ($self, $x, $y, $colour) = @_;
140 ### Image-Base-SVG xy(): @_[1 .. $#_]
141  
142 4 my $svg = $self->{'-svg_object'};
143 4 ▶ if (@_ == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 3) { } 2 2 4
144 2 return undef; # no pixel fetching available
145 } else {
146 2 $svg->rectangle (x => $x, y => $y,
147 width => 1, height => 1,
148 fill => $colour);
149 }
150 }
151  
152 4 ▶sub rectangle {
Subroutine rectangle: called CC=5 SCAR=16.1
Pod documented
153 4 my ($self, $x1,$y1, $x2,$y2, $colour, $fill) = @_;
154 ### Image-Base-SVG rectangle(): @_[1 .. $#_]
155  
156 4 ▶ $fill ||= ($x1 == $x2 || $y1 == $y2); # 1xN or Nx1 done filled
Condition83%5/6
$fill ||= $x1 == $x2 || $y1 == $y2
l!l&&r!l&&!r
112
$x1 == $x2 || $y1 == $y2
l!l&&r!l&&!r
102
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
157 4 ▶ if (! $fill) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fill) 2 2 4
158 2 $x1 += .5; # for stroke width 1
159 2 $y1 += .5;
160 2 $x2 -= .5;
161 2 $y2 -= .5;
162 }
163 4 ▶ $self->{'-svg_object'}->rectangle (x => $x1,
Branch: 1 branch
BranchTrueFalseTotal
$fill ? : 2 2 4
164 y => $y1,
165 width => $x2-$x1+1,
166 height => $y2-$y1+1,
167 ($fill?'fill':'stroke') => $colour);
168 }
169  
170 4 ▶sub ellipse {
Subroutine ellipse: called CC=5 SCAR=16.1
Pod documented
171 4 my ($self, $x1,$y1, $x2,$y2, $colour, $fill) = @_;
172 ### Image-Base-SVG ellipse(): @_[1 .. $#_]
173  
174 4 ▶ $fill ||= ($x1 == $x2 || $y1 == $y2);
Condition100%6/6
$fill ||= $x1 == $x2 || $y1 == $y2
l!l&&r!l&&!r
121
$x1 == $x2 || $y1 == $y2
l!l&&r!l&&!r
111
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
175 4 my $rx = ($x2-$x1+1) / 2;
176 4 my $ry = ($y2-$y1+1) / 2;
177 4 ▶ if (! $fill) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fill) 1 3 4
178 1 $rx -= .5; # for stroke width 1
179 1 $ry -= .5;
180 }
181 4 ▶ $self->{'-svg_object'}->ellipse (cx => ($x1+$x2+1) / 2,
Branch: 1 branch
BranchTrueFalseTotal
$fill ? : 3 1 4
182 cy => ($y1+$y2+1) / 2,
183 rx => $rx,
184 ry => $ry,
185 ($fill?'fill':'stroke') => $colour);
186 }
187  
188 1 ▶sub line {
Subroutine line: called CC=1 SCAR=0.0
Pod documented
189 1 my ($self, $x1,$y1, $x2,$y2, $colour, $fill) = @_;
190 ### Image-Base-SVG rectangle(): @_[1 .. $#_]
191  
192 1 $self->{'-svg_object'}->line (x1 => $x1+.5,
193 y1 => $y1+.5,
194 x2 => $x2+.5,
195 y2 => $y2+.5,
196 stroke => $colour,
197 'stroke-linecap' => "square");
198 }
199  
200 3 ▶sub diamond {
Subroutine diamond: called CC=5 SCAR=16.1
Pod documented
201 3 my ($self, $x1,$y1, $x2,$y2, $colour, $fill) = @_;
202 ### Image-Base-SVG diamond(): @_[1 .. $#_]
203  
204 3 ▶ $fill ||= ($x1 == $x2 || $y1 == $y2); # 1xN or Nx1 done filled
Condition83%5/6
$fill ||= $x1 == $x2 || $y1 == $y2
l!l&&r!l&&!r
111
$x1 == $x2 || $y1 == $y2
l!l&&r!l&&!r
101
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
205 3 ▶ if ($fill) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fill) { } 2 1 3
206 2 $x2++;
207 2 $y2++;
208 } else {
209 1 $x1 += .5; # for stroke width 1
210 1 $y1 += .5;
211 1 $x2 += .5;
212 1 $y2 += .5;
213 }
214 3 my $xm = ($x1+$x2)/2;
215 3 my $ym = ($y1+$y2)/2;
216 3 ▶ $self->{'-svg_object'}->polygon (points => "$xm,$y1 $x1,$ym $xm,$y2 $x2,$ym",
Branch: 1 branch
BranchTrueFalseTotal
$fill ? : 2 1 3
217 ($fill?'fill':'stroke') => $colour);
218 }
219  
220 1 ▶sub load {
Subroutine load: called CC=3 SCAR=11.2
Pod documented
221 1 my ($self, $filename) = @_;
222 1 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 1 0 1
223 1 $self->set('-file', $filename);
224 } else {
225 0 $filename = $self->get('-file');
226 }
227 ### $filename
228  
229 # stringize any oopery to stop SVG::Parser being clever ... maybe
230 1 $filename = "$filename";
231  
232 # use SVG::Parser qw(SVG::Parser::SAX=XML::LibXML::SAX::Parser);
233 # use SVG::Parser qw(SVG::Parser::SAX=XML::LibXML::SAX::Parser);
234  
235 1 ▶ eval 'use SVG::Parser; 1' or die;
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval "use SVG::Parser; 1" or die 1 0 1
236 1 my $parser = SVG::Parser->new (
237 # -debug => 1,
238 );
239 1 my $svg = $parser->parse_file ($filename);
240 1 $self->{'-svg_object'} = $svg;
241 }
242  
243 1 ▶sub save {
Subroutine save: called CC=5 SCAR=20.9
Pod documented
244 1 my ($self, $filename) = @_;
245 ### Image-Base-SVG save(): @_
246 1 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 1 0 1
247 1 $self->set('-file', $filename);
248 } else {
249 0 $filename = $self->get('-file');
250 }
251 ### $filename
252  
253 1 ▶ open my $fh, '>', $filename,
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $filename or croak("Cannot create ${filename}: $!") 1 0 1
254 or croak "Cannot create $filename: $!";
255  
256 1 ▶ if (! $self->save_fh ($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->save_fh($fh)) 0 1 1
257 0 my $err = "Error writing $filename: $!";
258 0 { local $!; close $fh; }
259 0 croak $err;
260 }
261 1 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("Error closing ${filename}: $!") 1 0 1
262 or croak "Error closing $filename: $!";
263 }
264  
265 # not yet documented ...
266 1 ▶sub save_fh {
Subroutine save_fh: called CC=1 SCAR=0.0
Pod undocumented
267 1 my ($self, $fh) = @_;
268 ### save_fh() ...
269 ### elements: $self->{'-elements'}
270 ### height: $self->{'-height'}
271  
272 1 my $svg = $self->{'-svg_object'};
273 # $svg->comment ("\n\tGenerated using ".ref($self)." version ".$self->VERSION."\n");
274 1 return print $fh $svg->xmlify;
275 }
276  
277 # sub _add_comment {
278 # my ($self) = @_;
279 # my $svg_element = _svg_element($self);
280 # my $generated
281 # = "\n\tGenerated using ".ref($self)." version ".$self->VERSION."\n";
282 # foreach my $comment ($svg_element->getElements('comment')) {
283 # if ($comment->cdata eq $generated) {
284 # return;
285 # }
286 # }
287 # $self->{'-svg_object'}->comment ($generated);
288 # }
289  
290 3 1;
291 __END__
↑ Summary