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