DOM2/Attribute/Path.pm untested

filter: Statementstmt 0.0% 0 / 84 Branchbran 0.0% 0 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 124 CC 14 SCAR 35.6
CC
14
Cov
0%
deserialise
72.0
serialise
30.0
_instruction
12.0
CRAP 35.2
1 package SVG::DOM2::Attribute::Path;
2  
3 use base "XML::DOM2::Attribute";
4  
5 use strict;
6 use warnings;
7 use Carp;
8  
9 my %imap = (
10 m => [qw/x y/], # move to
11 z => [], # close path
12 l => [qw/x y/], # line to
13 h => ['x'], # horz line
14 v => ['y'], # vert line
15 c => [qw/x1 y1 x2 y2 x y/], # Curveto Cubic Bezier
16 s => [qw/x1 y1 x y/], # Smooth Cubic Bezier
17 q => [qw/x1 y1 x y/], # Curceto Quadratic Bézier
18 t => [qw/x y/], # Smooth Quadratic Bézier
19 a => [qw/rx ry xr l s x y/], # Eliptical Arc Curve
20 );
21  
22 sub new
23 {
24 my ($proto, %opts) = @_;
25 return $proto->SUPER::new(%opts);
26 }
27  
28 sub serialise
29 {
30 my ($self) = @_;
31  
32 my $lastmode = '';
33 my $result = '';
34 foreach my $inst ($self->instructions) {
35 my %inst = %{$inst};
36 $result .= ' ' if length($result);
37 my $rel = $inst{'rel'};
38 my $mode = $inst{'mode'};
39 my @c = @inst{@{$imap{$mode}}};
40 $mode = uc($mode) if not $rel;
41 $result .= $mode.' ' if $mode ne $lastmode;
42 $result .= join(' ', @c) if @c;
43 $lastmode = $mode;
44 }
45 $result =~ s/\s+$//;
46 return $result;
47 }
48  
49 sub deserialise
50 {
51 my ($self, $path) = @_;
52  
53 $path =~ s/(\+|-)/ $1/g;
54 $path =~ s/([MmZzLlHhVvCcSsQqTtAa])/ $1 /g;
55 $path =~ s/,/ /g;
56 $path =~ s/^\s+//;
57 $path =~ s/\s+$//;
58  
59 my @path = split(/\s+/, $path);
60 my @inst;
61 my $mode;
62 for(my $i = 0; $i <= $#path; $i++) {
63 my $s = $path[$i];
64 next if not defined($s);
65 if(defined($imap{lc($s)})) {
66 $mode = $s;
67 if(not @{$imap{lc($mode)}}) {
68 # Make sure blind instructions are added
69 push @inst, _instruction($mode);
70 } else {
71 next;
72 }
73 } elsif($s =~ /^\-*\d+\.*\d*$/) {
74 # Data for current mode
75 my $length = @{$imap{lc($mode)}};
76 my $end = $i + $length;
77 # Next i if no co-ords to gather
78 next if not $end;
79 # Add the instruction
80 push @inst, _instruction($mode, @path[$i..$end]);
81 # Inplicit lineto after moveto
82 $mode = 'l' if($mode eq 'm');
83 $mode = 'L' if($mode eq 'M');
84 $i += $length - 1;
85 } else {
86 die "Error in path, unexpected instruction '$s' - ".join(', ', keys(%imap))."\n";
87 }
88 }
89 $self->{'path'} = \@inst;
90 return $self;
91 }
92  
93 sub _instruction
94 {
95 my ($mode, @c) = @_;
96 my $reletive = ($mode =~ /A-Z/) ? 1 : 0;
97 $mode = lc($mode);
98 my %inst = ( mode => $mode, rel => $reletive );
99 @inst{@{$imap{$mode}}} = @c if @c;
100 return \%inst;
101 }
102  
103 sub instructions
104 {
105 my ($self) = @_;
106 return @{$self->{'path'}};
107 }
108  
109 return 1;