SVG/Element.pm

filter: Statementstmt 66.4% 216 / 325 Branchbran 60.3% 93 / 154 Conditioncond 39.5% 17 / 43 MC/DCmcdc 25.0% 6 / 24 Subroutinesub 68.2% 28 / 41 Podpod 3.0% 1 / 33 totaltotal 58.2% 361 / 620 SCAR 65.5
CC
106
Cov
62%
perlify
306.0
get_path
46.3
animate
30.0
CRAP 700.8
« SVG/DOM.pm ↑ Summary SVG.pm »
1 package SVG::Element;
2  
3 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 25 our $VERSION = '2.87';
7  
8 =pod
9  
10 =encoding UTF-8
11  
12 =head1 NAME
13  
14 SVG::Element - Generate the element bits for SVG.pm
15  
16 =head1 AUTHOR
17  
18 Ronan Oger, cpan@roitsystems.com
19  
20 =head1 SEE ALSO
21  
22 For description of the methods see L<SVG>.
23  
24 =cut
25  
26 25 ▶use SVG::XML;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 25 ▶use SVG::DOM;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 25 ▶use SVG::Extension;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 25 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 25 our $AUTOLOAD;
32  
33 25 my @autosubs = qw(
34 animateMotion animateColor animateTransform circle ellipse rect polyline
35 path polygon line title desc defs
36 altGlyph altGlyphDef altGlyphItem clipPath color-profile
37 cursor definition-src font-face-format font-face-name
38 font-face-src font-face-url foreignObject glyph
39 glyphRef hkern marker mask metadata missing-glyph
40 mpath switch symbol textPath tref tspan view vkern marker textbox
41 flowText style script
42 image a g
43 );
44  
45 25 our %autosubs = map { $_ => 1 } @autosubs;
46  
47 #-------------------------------------------------------------------------------
48  
49 sub new {
50 157 ▶ my ( $proto, $name, %attrs ) = @_;
Subroutine new: called CC=11 SCAR=24.3
Pod undocumented
51 157 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
01570
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
52 157 my $self = { -name => $name };
53 157 foreach my $key ( keys %attrs ) {
54  
55 #handle escapes for special elements such as anchor
56 743 ▶ if ( $key =~ /^-/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^-/) 542 201 743
57 542 ▶ if ( $key eq '-href' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "-href") 4 538 542
58 4 $self->{'xlink:href'} = $attrs{$key};
59 4 ▶ $self->{'xlink:type'} = $attrs{-type} if $attrs{-type};
Branch: 1 branch
BranchTrueFalseTotal
if $attrs{"-type"} 0 4 4
60 4 ▶ $self->{'xlink:role'} = $attrs{-role} if $attrs{-role};
Branch: 1 branch
BranchTrueFalseTotal
if $attrs{"-role"} 0 4 4
61 4 ▶ $self->{'xlink:title'} = $attrs{-title} if $attrs{-title};
Branch: 1 branch
BranchTrueFalseTotal
if $attrs{"-title"} 2 2 4
62 4 ▶ $self->{'xlink:show'} = $attrs{-show} if $attrs{-show};
Branch: 1 branch
BranchTrueFalseTotal
if $attrs{"-show"} 1 3 4
63 $self->{'xlink:arcrole'} = $attrs{-arcrole}
64 4 ▶ if $attrs{-arcrole};
Branch: 1 branch
BranchTrueFalseTotal
if $attrs{"-arcrole"} 0 4 4
65 $self->{'xlink:actuate'} = $attrs{-actuate}
66 4 ▶ if $attrs{-actuate};
Branch: 1 branch
BranchTrueFalseTotal
if $attrs{"-actuate"} 1 3 4
67 4 next;
68 }
69 }
70 739 $self->{$key} = $attrs{$key};
71 }
72  
73 157 return bless( $self, $class );
74 }
75  
76 #-------------------------------------------------------------------------------
77  
78 sub release {
79 9 ▶ my $self = shift;
Subroutine release: called CC=4 SCAR=14.3
Pod undocumented
80  
81 9 foreach my $key ( keys( %{$self} ) ) {
82 84 ▶ next if $key =~ /^-/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^-/ 79 5 84
83 5 ▶ if ( ref( $self->{$key} ) =~ /^SVG/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($self->{$key}) =~ /^SVG/) 0 5 5
84 0 eval { $self->{$key}->release; };
85 }
86 5 delete( $self->{$key} );
87 }
88  
89 9 return $self;
90 }
91  
92 sub xmlify {
93 130 ▶ my $self = shift;
Subroutine xmlify: called CC=27 SCAR=33.2
Pod undocumented
94 130 ▶ my $ns = $self->{-namespace} || $self->{-docref}->{-namespace} || undef;
Condition40%2/5
$self->{'-namespace'} || $self->{'-docref'}{'-namespace'} || undef
l!l
0130
$self->{"-namespace"} or $self->{"-docref"}{"-namespace"}
l!l&&r!l&&!r
00130
MC/DC0%0/2
$self->{'-namespace'} || $self->{'-docref'}{'-namespace'} || undef
$self->{"-namespace"} $self->{"-docref"}{"-namespace"}
Truth table33%1/3
A or B
A= $self->{"-namespace"}B= $self->{"-docref"}{"-namespace"}
ABresult
1X1
011
000
95 130 my $xml = '';
96  
97 #prep the attributes
98 130 my %attrs;
99 130 foreach my $k ( keys( %{$self} ) ) {
100 1707 ▶ if ( $k =~ /^-/ ) { next; }
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /^-/) 1432 275 1707
101 275 ▶ if ( ref( $self->{$k} ) eq 'ARRAY' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $self->{$k} eq 'ARRAY') { } 0 275 275
elsif (ref $self->{$k} eq 'HASH') { } 5 270 275
elsif (ref $self->{$k} eq '') { } 270 0 270
102 0 $attrs{$k} = join( ', ', @{ $self->{$k} } );
103 }
104 elsif ( ref( $self->{$k} ) eq 'HASH' ) {
105 5 $attrs{$k} = cssstyle( %{ $self->{$k} } );
106 }
107 elsif ( ref( $self->{$k} ) eq '' ) {
108 270 $attrs{$k} = $self->{$k};
109 }
110 }
111  
112 #prep the tag
113 130 ▶ if ( $self->{-comment} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'-comment'}) { } 22 108 130
elsif ($self->{'-name'} eq 'document') { } 23 85 108
114 22 $xml .= $self->xmlcomment( $self->{-comment} );
115 22 return $xml;
116 }
117 elsif ( $self->{-name} eq 'document' ) {
118  
119 #write the xml header
120 23 ▶ $xml .= $self->xmldecl unless $self->{-inline};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"-inline"} 21 2 23
121  
122 $xml .= $self->xmlpi( $self->{-document}->{-pi} )
123 23 ▶ if $self->{-document}->{-pi};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"-document"}{"-pi"} 2 21 23
124  
125 #and write the dtd if this is inline
126 23 ▶ $xml .= $self->dtddecl unless $self->{-inline};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"-inline"} 21 2 23
127  
128 #rest of the xml
129 23 foreach my $k ( @{ $self->{-childs} } ) {
130 23 ▶ if ( ref($k) =~ /^SVG::Element/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($k) =~ /^SVG::Element/) 23 0 23
131 23 $xml .= $k->xmlify($ns);
132 }
133 }
134  
135 23 return $xml;
136 }
137 my $is_cdataish
138 = defined $self->{-cdata}
139 || defined $self->{-CDATA}
140 85 ▶ || defined $self->{-cdata_noxmlesc};
Condition100%6/6
defined $self->{'-cdata'} || defined $self->{'-CDATA'} || defined $self->{'-cdata_noxmlesc'}
l!l&&r!l&&!r
15169
defined $self->{"-cdata"} or defined $self->{"-CDATA"}
l!l&&r!l&&!r
12370
MC/DC100%3/3
defined $self->{'-cdata'} || defined $self->{'-CDATA'} || defined $self->{'-cdata_noxmlesc'}
defined $self->{"-cdata"} defined $self->{"-CDATA"} defined $self->{'-cdata_noxmlesc'}
Truth table100%4/4
A or B || C
A= defined $self->{"-cdata"}B= defined $self->{"-CDATA"}C= defined $self->{'-cdata_noxmlesc'}
ABCresult
1XX1
01X1
0011
0000
141 85 ▶ if ( defined $self->{-childs} || $is_cdataish ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'-childs'} or $is_cdataish) { } 49 36 85
Condition100%3/3
defined $self->{"-childs"} or $is_cdataish
l!l&&r!l&&!r
351436
MC/DC100%2/2
defined $self->{"-childs"} or $is_cdataish
defined $self->{"-childs"} $is_cdataish
Truth table100%3/3
A or B
A= defined $self->{"-childs"}B= $is_cdataish
ABresult
1X1
011
000
142 $xml .= $self->{-docref}->{-elsep}
143 49 ▶ unless ( $self->{-inline} && $self->{-name} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"-inline"} and $self->{"-name"} 47 2 49
Condition66%2/3
$self->{"-inline"} and $self->{"-name"}
!ll&&!rl&&r
4702
MC/DC50%1/2
$self->{"-inline"} and $self->{"-name"}
$self->{"-inline"} $self->{"-name"}
Truth table66%2/3
A and B
A= $self->{"-inline"}B= $self->{"-name"}
ABresult
0X0
100
111
144 49 $xml .= $self->{-docref}->{-indent} x $self->{-docref}->{-level};
145 49 $xml .= xmltagopen_ln( $self->{-name}, $ns, %attrs );
146 49 $self->{-docref}->{-level}++;
147 49 foreach my $k ( @{ $self->{-childs} } ) {
148 70 ▶ if ( ref($k) =~ /^SVG::Element/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($k) =~ /^SVG::Element/) 70 0 70
149 70 $xml .= $k->xmlify($ns);
150 }
151 }
152  
153 49 ▶ if ( defined $self->{-cdata} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"-cdata"}) 12 37 49
154 12 $xml .= $self->xmlescp( $self->{-cdata} );
155 }
156 49 ▶ if ( defined $self->{-CDATA} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"-CDATA"}) 3 46 49
157 3 $xml .= '<![CDATA[' . $self->{-CDATA} . ']]>';
158 }
159 49 ▶ if ( defined $self->{-cdata_noxmlesc} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"-cdata_noxmlesc"}) 1 48 49
160 1 $xml .= $self->{-cdata_noxmlesc};
161 }
162  
163 #return without writing the tag out if it the document tag
164 49 $self->{-docref}->{-level}--;
165 49 ▶ unless ($is_cdataish) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_cdataish) 33 16 49
166 33 $xml .= $self->{-docref}->{-elsep};
167 33 $xml .= $self->{-docref}->{-indent} x $self->{-docref}->{-level};
168 }
169 49 $xml .= xmltagclose_ln( $self->{-name}, $ns );
170 }
171 else {
172 36 $xml .= $self->{-docref}->{-elsep};
173 36 $xml .= $self->{-docref}->{-indent} x $self->{-docref}->{-level};
174 36 $xml .= xmltag_ln( $self->{-name}, $ns, %attrs );
175 }
176  
177 #return the finished tag
178 85 return $xml;
179 }
180  
181 sub perlify {
182 0 ▶ my $self = shift;
Subroutine perlify: not called CC=17 SCAR=57.2
Pod undocumented
183 0 my $code = '';
184  
185 #prep the attributes
186 0 my %attrs;
187 0 foreach my $k ( keys( %{$self} ) ) {
188 0 ▶ next if $k =~ /^-/;
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /^-/ 0 0 0
189 0 ▶ if ( ref( $self->{$k} ) eq 'ARRAY' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $self->{$k} eq 'ARRAY') { } 0 0 0
elsif (ref $self->{$k} eq 'HASH') { } 0 0 0
elsif (ref $self->{$k} eq '') { } 0 0 0
190 0 $attrs{$k} = join( ', ', @{ $self->{$k} } );
191 }
192 elsif ( ref( $self->{$k} ) eq 'HASH' ) {
193 0 $attrs{$k} = cssstyle( %{ $self->{$k} } );
194 }
195 elsif ( ref( $self->{$k} ) eq '' ) {
196 0 $attrs{$k} = $self->{$k};
197 }
198 }
199  
200 0 ▶ if ( $self->{-comment} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'-comment'}) { } 0 0 0
elsif ($self->{'-pi'}) { } 0 0 0
elsif ($self->{'-name'} eq 'document') { } 0 0 0
201 0 $code .= "->comment($self->{-comment})";
202 0 return $code;
203 }
204 elsif ( $self->{-pi} ) {
205 0 $code .= "->pi($self->{-pi})";
206 0 return $code;
207 }
208 elsif ( $self->{-name} eq 'document' ) {
209  
210 #write the xml header
211 #$xml .= $self->xmldecl;
212 #and write the dtd if this is inline
213 #$xml .= $self->dtddecl unless $self->{-inline};
214 0 foreach my $k ( @{ $self->{-childs} } ) {
215 0 ▶ if ( ref($k) =~ /^SVG::Element/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($k) =~ /^SVG::Element/) 0 0 0
216 0 $code .= $k->perlify();
217 }
218 }
219 0 return $code;
220 }
221  
222 0 ▶ if ( defined $self->{-childs} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'-childs'}) { } 0 0 0
223 0 $code .= $self->{-docref}->{-elsep};
224 0 $code .= $self->{-docref}->{-indent} x $self->{-docref}->{-level};
225 $code
226 .= $self->{-name} . '('
227 0 . ( join ', ', ( map {"$_=>'$attrs{$_}'"} sort keys %attrs ) )
228 . ')';
229 0 ▶ if ( $self->{-cdata} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'-cdata'}) { } 0 0 0
elsif ($self->{'-CDATA'}) { } 0 0 0
elsif ($self->{'-cdata_noxmlesc'}) { } 0 0 0
230 0 $code .= "->cdata($self->{-cdata})";
231 }
232 elsif ( $self->{-CDATA} ) {
233 0 $code .= "->CDATA($self->{-CDATA})";
234 }
235 elsif ( $self->{-cdata_noxmlesc} ) {
236 0 $code .= "->cdata_noxmlesc($self->{-cdata_noxmlesc})";
237 }
238  
239 0 $self->{-docref}->{-level}++;
240 0 foreach my $k ( @{ $self->{-childs} } ) {
241 0 ▶ if ( ref($k) =~ /^SVG::Element/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($k) =~ /^SVG::Element/) 0 0 0
242 0 $code .= $k->perlify();
243 }
244 }
245 0 $self->{-docref}->{-level}--;
246 }
247 else {
248 0 $code .= $self->{-docref}->{-elsep};
249 0 $code .= $self->{-docref}->{-indent} x $self->{-docref}->{-level};
250 $code
251 .= $self->{-name} . '('
252 0 . ( join ', ', ( map {"$_=>'$attrs{$_}'"} sort keys %attrs ) )
253 . ')';
254 }
255  
256 0 return $code;
257 }
258 25 *toperl = \&perlify;
259  
260 sub addchilds {
261 122 ▶ my $self = shift;
Subroutine addchilds: called CC=1 SCAR=0.0
Pod undocumented
262 122 push @{ $self->{-childs} }, @_;
263 122 return $self;
264 }
265  
266 sub tag {
267 124 ▶ my ( $self, $name, %attrs ) = @_;
Subroutine tag: called CC=10 SCAR=23.0
Pod undocumented
268  
269 124 ▶ unless ( $self->{-parent} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"-parent"}) 102 22 124
270  
271 #traverse down the tree until you find a non-document entry
272 102 ▶ while ( $self->{-document} ) { $self = $self->{-document} }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"-document"}) 69 102 171
273 }
274 124 my $tag = new SVG::Element( $name, %attrs );
275  
276 #define the element namespace
277 124 ▶ $tag->{-namespace} = $attrs{-namespace} if ( $attrs{-namespace} );
Branch: 1 branch
BranchTrueFalseTotal
if $attrs{"-namespace"} 0 124 124
278  
279 #add the tag to the document element
280 124 $tag->{-docref} = $self->{-docref};
281 124 weaken( $tag->{-docref} );
282  
283 #create the empty idlist hash ref unless it already exists
284 $tag->{-docref}->{-idlist} = {}
285 124 ▶ unless ( defined $tag->{-docref}->{-idlist} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tag->{"-docref"}{"-idlist"} 33 91 124
286  
287 #verify that the current id is unique. compain on exception
288 #>>>TBD: add -strictids option to disable this check if desired
289 124 ▶ if ( $tag->{id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag->{"id"}) 13 111 124
290 13 ▶ if ( $self->getElementByID( $tag->{id} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getElementByID($tag->{"id"})) 2 11 13
291 2 $self->error( $tag->{id} => 'ID already exists in document' );
292 1 return;
293 }
294 }
295  
296 #add the current id reference to the document id hash
297 122 ▶ if ( defined( $tag->{id} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag->{"id"}) 11 111 122
298 11 $tag->{-docref}->{-idlist}->{ $tag->{id} } = $tag;
299 }
300  
301 #create the empty idlist hash ref unless it already exists
302 $tag->{-docref}->{-elist} = {}
303 122 ▶ unless ( defined $tag->{-docref}->{-elist} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tag->{"-docref"}{"-elist"} 33 89 122
304  
305 #create the empty idlist hash ref unless it already exists
306 $tag->{-docref}->{-elist}->{ $tag->{-name} } = []
307 122 ▶ unless ( defined $tag->{-docref}->{-elist}->{ $tag->{-name} } );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tag->{"-docref"}{"-elist"}{$tag->{"-name"}} 98 24 122
308  
309 #add the current element ref to the corresponding element-hash array
310 # -elist is a hash of element names. key name is element, content is object ref.
311  
312 # add the reference to $tag to the array of refs that belong to the
313 # key $tag->{-name}.
314 122 unshift @{ $tag->{-docref}->{-elist}->{ $tag->{-name} } }, $tag;
315  
316 # attach element to the DOM of the document
317 122 $tag->{-parent} = $self;
318 122 weaken( $tag->{-parent} );
319 122 $tag->{-parentname} = $self->{-name};
320 122 $self->addchilds($tag);
321  
322 122 return ($tag);
323 }
324  
325 25 *element = \&tag;
326  
327 sub anchor {
328 3 ▶ my ( $self, %attrs ) = @_;
Subroutine anchor: called CC=1 SCAR=0.0
Pod undocumented
329 3 my $an = $self->tag( 'a', %attrs );
330  
331 #$an->{'xlink:href'}=$attrs{-href} if(defined $attrs{-href});
332 #$an->{'target'}=$attrs{-target} if(defined $attrs{-target});
333 3 return ($an);
334 }
335  
336 sub svg {
337 33 ▶ my ( $self, %attrs ) = @_;
Subroutine svg: called CC=3 SCAR=11.0
Pod undocumented
338 33 my $svg = $self->tag( 'svg', %attrs );
339 33 ▶ $svg->{'height'} = '100%' unless ( $svg->{'height'} );
Branch: 1 branch
BranchTrueFalseTotal
unless $svg->{"height"} 32 1 33
340 33 ▶ $svg->{'width'} = '100%' unless ( $svg->{'width'} );
Branch: 1 branch
BranchTrueFalseTotal
unless $svg->{"width"} 32 1 33
341 33 return ($svg);
342 }
343  
344 sub rectangle {
345 1 ▶ my ( $self, %attrs ) = @_;
Subroutine rectangle: called CC=1 SCAR=0.0
Pod undocumented
346 1 return $self->tag( 'rect', %attrs );
347 }
348  
349 #sub image {
350 # my ($self,%attrs)=@_;
351 # my $im=$self->tag('image',%attrs);
352 # #$im->{'xlink:href'}=$attrs{-href} if(defined $attrs{-href});
353 # return $im;
354 #}
355  
356 sub use {
357 0 ▶ my ( $self, %attrs ) = @_;
Subroutine use: not called CC=2 SCAR=17.9
Pod undocumented
358 0 my $u = $self->tag( 'use', %attrs );
359 0 ▶ $u->{'xlink:href'} = $attrs{-href} if ( defined $attrs{-href} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $attrs{"-href"} 0 0 0
360 0 return $u;
361 }
362  
363 sub text {
364 20 ▶ my ( $self, %attrs ) = @_;
Subroutine text: called CC=4 SCAR=14.2
Pod undocumented
365 20 my $pre = '';
366 20 ▶ $pre = $attrs{-type} || 'std';
Condition50%1/2
$attrs{'-type'} || 'std'
l!l
020
MC/DC0%0/1
$attrs{'-type'} || 'std'
$attrs{'-type'}
Truth table50%1/2
A
A= $attrs{'-type'}
Aresult
11
00
367 20 my %get_pre = (
368 std => 'text',
369 path => 'textPath',
370 span => 'tspan',
371 );
372  
373 20 $pre = $get_pre{ lc($pre) };
374 20 my $text = $self->tag( $pre, %attrs );
375 20 ▶ $text->{'xlink:href'} = $attrs{-href} if ( defined $attrs{-href} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $attrs{"-href"} 0 20 20
376 20 ▶ $text->{'target'} = $attrs{-target} if ( defined $attrs{-target} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $attrs{"-target"} 0 20 20
377 20 return ($text);
378 }
379  
380 sub comment {
381 19 ▶ my ( $self, @text ) = @_;
Subroutine comment: called CC=1 SCAR=0.0
Pod undocumented
382 19 my $tag = $self->tag('comment');
383 19 $tag->{-comment} = [@text];
384 19 return $tag;
385 }
386  
387 sub pi {
388 4 ▶ my ( $self, @text ) = @_;
Subroutine pi: called CC=4 SCAR=13.9
Pod undocumented
389 4 ▶ return $self->{-document}->{-pi} unless scalar @text;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @text 2 2 4
390 2 my @pi;
391 2 ▶ @pi = @{ $self->{-document}->{-pi} } if $self->{-document}->{-pi};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"-document"}{"-pi"} 1 1 2
392 2 ▶ unshift( @text, @pi ) if @pi;
Branch: 1 branch
BranchTrueFalseTotal
if @pi 1 1 2
393 2 $self->{-document}->{-pi} = \@text;
394 2 my $tag = $self->tag('pi');
395 2 return $tag;
396 }
397  
398 =pod
399  
400 =head2 get_path
401  
402 Documented as L<SVG/get_path>.
403  
404 =cut
405  
406 sub get_path {
407 1 ▶ my ( $self, %attrs ) = @_;
Subroutine get_path: called CC=12 SCAR=38.4
Pod documented
408  
409 1 ▶ my $type = $attrs{-type} || 'path';
Condition50%1/2
$attrs{'-type'} || 'path'
l!l
10
MC/DC0%0/1
$attrs{'-type'} || 'path'
$attrs{'-type'}
Truth table50%1/2
A
A= $attrs{'-type'}
Aresult
11
00
410 1 my @x = @{ $attrs{x} };
411 1 my @y = @{ $attrs{y} };
412 1 my $points;
413  
414 # we need a path-like point string returned
415 1 ▶ if ( lc($type) eq 'path' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (lc $type eq 'path') { } 0 1 1
elsif (lc($type) =~ /^poly/) { } 1 0 1
416 0 my $char = 'M';
417 $char = ' m '
418 0 ▶ if ( defined $attrs{-relative} && lc( $attrs{-relative} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $attrs{"-relative"} and lc $attrs{"-relative"} 0 0 0
Condition0%0/3
defined $attrs{"-relative"} and lc $attrs{"-relative"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $attrs{"-relative"} and lc $attrs{"-relative"}
defined $attrs{"-relative"} lc $attrs{"-relative"}
Truth table0%0/3
A and B
A= defined $attrs{"-relative"}B= lc $attrs{"-relative"}
ABresult
0X0
100
111
419 0 ▶ while (@x) {
Branch: 1 branch
BranchTrueFalseTotal
if (@x) 0 0 0
420  
421 #scale each value
422 0 my $x = shift @x;
423 0 my $y = shift @y;
424  
425 #append the scaled value to the graph
426 0 $points .= "$char $x $y ";
427 0 $char = ' L ';
428 $char = ' l '
429 if ( defined $attrs{-relative}
430 0 ▶ && lc( $attrs{-relative} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $attrs{"-relative"} and lc $attrs{"-relative"} 0 0 0
Condition0%0/3
defined $attrs{"-relative"} and lc $attrs{"-relative"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $attrs{"-relative"} and lc $attrs{"-relative"}
defined $attrs{"-relative"} lc $attrs{"-relative"}
Truth table0%0/3
A and B
A= defined $attrs{"-relative"}B= lc $attrs{"-relative"}
ABresult
0X0
100
111
431 }
432 $points .= ' z '
433 0 ▶ if ( defined $attrs{-closed} && lc( $attrs{-closed} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $attrs{"-closed"} and lc $attrs{"-closed"} 0 0 0
Condition0%0/3
defined $attrs{"-closed"} and lc $attrs{"-closed"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $attrs{"-closed"} and lc $attrs{"-closed"}
defined $attrs{"-closed"} lc $attrs{"-closed"}
Truth table0%0/3
A and B
A= defined $attrs{"-closed"}B= lc $attrs{"-closed"}
ABresult
0X0
100
111
434 0 my %out = ( d => $points );
435 0 return \%out;
436 }
437 elsif ( lc($type) =~ /^poly/ ) {
438 1 ▶ while (@x) {
Branch: 1 branch
BranchTrueFalseTotal
if (@x) 5 1 6
439  
440 #scale each value
441 5 my $x = shift @x;
442 5 my $y = shift @y;
443  
444 #append the scaled value to the graph
445 5 $points .= "$x,$y ";
446 }
447 }
448 1 my %out = ( points => $points );
449 1 return \%out;
450 }
451  
452 sub make_path {
453 0 ▶ my ( $self, %attrs ) = @_;
Subroutine make_path: not called CC=1 SCAR=6.9
Pod undocumented
454 0 return get_path(%attrs);
455 }
456  
457 sub set_path {
458 0 ▶ my ( $self, %attrs ) = @_;
Subroutine set_path: not called CC=1 SCAR=6.9
Pod undocumented
459 0 return get_path(%attrs);
460 }
461  
462 sub animate {
463 0 ▶ my ( $self, %attrs ) = @_;
Subroutine animate: not called CC=5 SCAR=34.0
Pod undocumented
464 0 my %rtr = %attrs;
465 0 my $method = $rtr{'-method'}; # Set | Transform | Motion | Color
466  
467 0 $method = lc($method);
468  
469 # we do not want this to pollute the generation of the tag
470 0 delete $rtr{-method}; #bug report from briac.
471  
472 0 my %animation_method = (
473 transform => 'animateTransform',
474 motion => 'animateMotion',
475 color => 'animateColor',
476 set => 'set',
477 attribute => 'animate',
478 );
479  
480 0 ▶ my $name = $animation_method{$method} || 'animate';
Condition0%0/2
$animation_method{$method} || 'animate'
l!l
00
MC/DC0%0/1
$animation_method{$method} || 'animate'
$animation_method{$method}
Truth table0%0/2
A
A= $animation_method{$method}
Aresult
11
00
481  
482 #list of legal entities for each of the 5 methods of animations
483 0 my %legal = (
484 animate => q{ begin dur end min max restart repeatCount
485 repeatDur fill attributeType attributeName additive
486 accumulate calcMode values keyTimes keySplines
487 from to by },
488 animateTransform => q{ begin dur end min max restart repeatCount
489 repeatDur fill additive accumulate calcMode values
490 keyTimes keySplines from to by calcMode path keyPoints
491 rotate origin type attributeName attributeType },
492 animateMotion => q{ begin dur end min max restart repeatCount
493 repeatDur fill additive accumulate calcMode values
494 to by keyTimes keySplines from path keyPoints
495 rotate origin },
496 animateColor => q{ begin dur end min max restart repeatCount
497 repeatDur fill additive accumulate calcMode values
498 keyTimes keySplines from to by },
499 set => q{ begin dur end min max restart repeatCount repeatDur
500 fill to },
501 );
502  
503 0 foreach my $k ( keys %rtr ) {
504 0 ▶ next if ( $k =~ /\-/ );
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /\-/ 0 0 0
505  
506 0 ▶ if ( $legal{$name} !~ /\b$k\b/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($legal{$name} =~ /\b$k\b/) 0 0 0
507 0 $self->error( "$name.$k" => 'Illegal animation command' );
508 }
509 }
510  
511 0 return $self->tag( $name, %rtr );
512 }
513  
514 sub group {
515 12 ▶ my ( $self, %attrs ) = @_;
Subroutine group: called CC=1 SCAR=0.0
Pod undocumented
516 12 return $self->tag( 'g', %attrs );
517 }
518  
519 sub STYLE {
520 0 ▶ my ( $self, %attrs ) = @_;
Subroutine STYLE: not called CC=3 SCAR=24.8
Pod undocumented
521  
522 0 ▶ $self->{style} = $self->{style} || {};
Condition0%0/2
$self->{'style'} || {}
l!l
00
MC/DC0%0/1
$self->{'style'} || {}
$self->{'style'}
Truth table0%0/2
A
A= $self->{'style'}
Aresult
11
00
523 0 foreach my $k ( keys %attrs ) {
524 0 $self->{style}->{$k} = $attrs{$k};
525 }
526  
527 0 return $self;
528 }
529  
530 sub mouseaction {
531 0 ▶ my ( $self, %attrs ) = @_;
Subroutine mouseaction: not called CC=3 SCAR=24.8
Pod undocumented
532  
533 0 ▶ $self->{mouseaction} = $self->{mouseaction} || {};
Condition0%0/2
$self->{'mouseaction'} || {}
l!l
00
MC/DC0%0/1
$self->{'mouseaction'} || {}
$self->{'mouseaction'}
Truth table0%0/2
A
A= $self->{'mouseaction'}
Aresult
11
00
534 0 foreach my $k ( keys %attrs ) {
535 0 $self->{mouseaction}->{$k} = $attrs{$k};
536 }
537  
538 0 return $self;
539 }
540  
541 sub attrib {
542 3 ▶ my ( $self, $name, $val ) = @_;
Subroutine attrib: called CC=5 SCAR=19.3
Pod undocumented
543  
544 #verify that the current id is unique. compain on exception
545 3 ▶ if ( $name eq 'id' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "id") 0 3 3
546 0 ▶ if ( $self->getElementByID($val) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getElementByID($val)) 0 0 0
547 0 $self->error( $val => 'ID already exists in document' );
548 0 return;
549 }
550 }
551  
552 3 ▶ if ( not defined $val ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $val) { } 2 1 3
553 2 ▶ if ( scalar(@_) == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 2) { } 2 0 2
554  
555 # two arguments only - retrieve
556 2 return $self->{$name};
557 }
558 else {
559  
560 # 3rd argument is undef - delete
561 0 delete $self->{$name};
562 }
563 }
564 else {
565  
566 # 3 defined arguments - set
567 1 $self->{$name} = $val;
568 }
569  
570 1 return $self;
571 }
572 25 *attr = \&attrib;
573 25 *attribute = \&attrib;
574  
575 sub cdata {
576 19 ▶ my ( $self, @txt ) = @_;
Subroutine cdata: called CC=1 SCAR=0.0
Pod undocumented
577 19 $self->{-cdata} = join( ' ', @txt );
578 19 return ($self);
579 }
580  
581 sub CDATA {
582 2 ▶ my ( $self, @txt ) = @_;
Subroutine CDATA: called CC=1 SCAR=0.0
Pod undocumented
583 2 $self->{-CDATA} = join( '\n', @txt );
584 2 return ($self);
585 }
586  
587 sub cdata_noxmlesc {
588 1 ▶ my ( $self, @txt ) = @_;
Subroutine cdata_noxmlesc: called CC=1 SCAR=0.0
Pod undocumented
589 1 $self->{-cdata_noxmlesc} = join( '\n', @txt );
590 1 return ($self);
591 }
592  
593 sub filter {
594 0 ▶ my ( $self, %attrs ) = @_;
Subroutine filter: not called CC=1 SCAR=6.9
Pod undocumented
595 0 return $self->tag( 'filter', %attrs );
596 }
597  
598 sub fe {
599 1 ▶ my ( $self, %attrs ) = @_;
Subroutine fe: called CC=3 SCAR=11.2
Pod undocumented
600  
601 1 ▶ return 0 unless ( $attrs{'-type'} );
Branch: 1 branch
BranchTrueFalseTotal
unless $attrs{"-type"} 0 1 1
602 1 my %allowed = (
603 blend => 'feBlend',
604 colormatrix => 'feColorMatrix',
605 componenttrans => 'feComponentTrans',
606 Componenttrans => 'feComponentTrans',
607 composite => 'feComposite',
608 convolvematrix => 'feConvolveMatrix',
609 diffuselighting => 'feDiffuseLighting',
610 displacementmap => 'feDisplacementMap',
611 distantlight => 'feDistantLight',
612 flood => 'feFlood',
613 funca => 'feFuncA',
614 funcb => 'feFuncB',
615 funcg => 'feFuncG',
616 funcr => 'feFuncR',
617 gaussianblur => 'feGaussianBlur',
618 image => 'feImage',
619 merge => 'feMerge',
620 mergenode => 'feMergeNode',
621 morphology => 'feMorphology',
622 offset => 'feOffset',
623 pointlight => 'fePointLight',
624 specularlighting => 'feSpecularLighting',
625 spotlight => 'feSpotLight',
626 tile => 'feTile',
627 turbulence => 'feTurbulence',
628 );
629  
630 1 my $key = lc( $attrs{'-type'} );
631 1 ▶ my $fe_name = $allowed{ lc($key) } || 'error:illegal_filter_element';
Condition50%1/2
$allowed{lc $key} || 'error:illegal_filter_element'
l!l
10
MC/DC0%0/1
$allowed{lc $key} || 'error:illegal_filter_element'
$allowed{lc $key}
Truth table50%1/2
A
A= $allowed{lc $key}
Aresult
11
00
632 1 delete $attrs{'-type'};
633  
634 1 return $self->tag( $fe_name, %attrs );
635 }
636  
637 sub pattern {
638 0 ▶ my ( $self, %attrs ) = @_;
Subroutine pattern: not called CC=1 SCAR=6.9
Pod undocumented
639 0 return $self->tag( 'pattern', %attrs );
640 }
641  
642 sub set {
643 0 ▶ my ( $self, %attrs ) = @_;
Subroutine set: not called CC=1 SCAR=6.9
Pod undocumented
644 0 return $self->tag( 'set', %attrs );
645 }
646  
647 sub stop {
648 0 ▶ my ( $self, %attrs ) = @_;
Subroutine stop: not called CC=1 SCAR=6.9
Pod undocumented
649 0 return $self->tag( 'stop', %attrs );
650 }
651  
652 sub gradient {
653 0 ▶ my ( $self, %attrs ) = @_;
Subroutine gradient: not called CC=3 SCAR=24.8
Pod undocumented
654  
655 0 ▶ my $type = $attrs{'-type'} || 'linear';
Condition0%0/2
$attrs{'-type'} || 'linear'
l!l
00
MC/DC0%0/1
$attrs{'-type'} || 'linear'
$attrs{'-type'}
Truth table0%0/2
A
A= $attrs{'-type'}
Aresult
11
00
656 0 ▶ unless ( $type =~ /^(linear|radial)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type =~ /^(linear|radial)$/) 0 0 0
657 0 $type = 'linear';
658 }
659 0 delete $attrs{'-type'};
660  
661 0 return $self->tag( $type . 'Gradient', %attrs );
662 }
663  
664 #-------------------------------------------------------------------------------
665 # Internal methods
666  
667 sub error {
668 12 ▶ my ( $self, $command, $error ) = @_;
Subroutine error: called CC=3 SCAR=11.3
Pod undocumented
669  
670 12 ▶ if ( $self->{-docref}->{-raiseerror} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'-docref'}{'-raiseerror'}) { } 1 11 12
elsif ($self->{'-docref'}{'-printerror'}) { } 0 11 11
671 1 die "$command: $error\n";
672 }
673 elsif ( $self->{-docref}->{-printerror} ) {
674 0 print STDERR "$command: $error\n";
675 }
676  
677 11 $self->{errors}{$command} = $error;
678 }
679  
680 # This AUTOLOAD method is activated when '-auto' is passed to SVG.pm
681 sub autoload {
682 1298 ▶ my $self = shift;
Subroutine autoload: called CC=4 SCAR=13.9
Pod undocumented
683 1298 my ( $package, $sub ) = ( $AUTOLOAD =~ /(.*)::([^:]+)$/ );
684  
685 1298 ▶ if ( $sub eq 'DESTROY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub eq 'DESTROY') { } 9 1289 1298
686 9 return $self->release();
687 }
688 else {
689  
690 # the import routine may call us with a tag name involving '-'s
691 1289 my $tag = $sub;
692 1289 $sub =~ tr/-/_/;
693  
694 # N.B.: The \ on \@_ makes sure that the incoming arguments are
695 # used and not the ones passed when the subroutine was created.
696 # eval "sub $package\:\:$sub (\$;\@) { return shift->tag('$tag',\@_) }";
697 #per rt.perl.org comment by slaven.
698  
699 1289 ▶ if ( !$package->can($sub) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($package->can($sub)) 1142 147 1289
700 ## no critic (TestingAndDebugging::ProhibitNoStrict)
701 25 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
702 1142 *{ $package . '::' . $sub }
703 1142 ▶ = sub { return shift->tag( $tag, @_ ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
704 }
705 1289 ▶ return $self->$sub(@_) if $self;
Branch: 1 branch
BranchTrueFalseTotal
if $self 7 1282 1289
706 }
707 }
708  
709 #-------------------------------------------------------------------------------
710 # GD Routines
711  
712 sub colorAllocate {
713 0 ▶ my ( $self, $red, $green, $blue ) = @_;
Subroutine colorAllocate: not called CC=1 SCAR=6.9
Pod undocumented
714 0 return 'rgb(' . int($red) . ',' . int($green) . ',' . int($blue) . ')';
715 }
716  
717 #-------------------------------------------------------------------------------
718  
719 25 1;
« SVG/DOM.pm ↑ Summary SVG.pm »