| 1 | package SVG::DOM2::Attribute::Style; | ||
| 2 | |||
| 3 | use base "XML::DOM2::Attribute"; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use Carp; | ||
| 8 | |||
| 9 | use SVG::DOM2::Attribute::Colour; | ||
| 10 | use SVG::DOM2::Attribute::Metric; | ||
| 11 | use SVG::DOM2::Attribute::Opacity; | ||
| 12 | |||
| 13 | # Each element in a style attribute is really another attribute | ||
| 14 | # condensed, to make this a simpler api we are going to load | ||
| 15 | # up the apropriate attribute class for each element. | ||
| 16 | |||
| 17 | my %style_part = ( | ||
| 18 | 'fill' => sub { SVG::DOM2::Attribute::Colour->new(@_) }, | ||
| 19 | 'fill-opacity' => sub { SVG::DOM2::Attribute::Opacity->new(@_) }, | ||
| 20 | 'stroke' => sub { SVG::DOM2::Attribute::Colour->new(@_) }, | ||
| 21 | 'stroke-width' => sub { SVG::DOM2::Attribute::Metric->new(@_) }, | ||
| 22 | 'stroke-opacity' => sub { SVG::DOM2::Attribute::Opacity->new(@_) }, | ||
| 23 | ); | ||
| 24 | |||
| 25 | sub new | ||
| 26 | { | ||
| 27 | my ($proto, %opts) = @_; | ||
| 28 | return bless \%opts, $proto; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub serialise | ||
| 32 | { | ||
| 33 | my ($self) = @_; | ||
| 34 | my $result = ''; | ||
| 35 | my $style = $self->style; | ||
| 36 | foreach my $name (keys(%{$style})) { | ||
| 37 | my $value = $self->_style($name)->value; | ||
| 38 | $result .= ';' if $result; | ||
| 39 | $result .= $name.':'.$value; | ||
| 40 | } | ||
| 41 | return $result; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub deserialise | ||
| 45 | { | ||
| 46 | my ($self, $attr) = @_; | ||
| 47 | my %result; | ||
| 48 | foreach my $style (split(/;/, $attr)) { | ||
| 49 | my ($name, $value) = split(/:/, $style); | ||
| 50 | $self->_style($name, $value); | ||
| 51 | } | ||
| 52 | return $self; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub style | ||
| 56 | { | ||
| 57 | my ($self) = @_; | ||
| 58 | return $self->{'style'}; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub fill | ||
| 62 | { | ||
| 63 | my ($self, $set) = @_; | ||
| 64 | my %result; | ||
| 65 | $result{'color'} = $self->fill_color($set); | ||
| 66 | $result{'opacity'} = $self->fill_opacity($set); | ||
| 67 | return \%result; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub stroke | ||
| 71 | { | ||
| 72 | my ($self, $set) = @_; | ||
| 73 | my %result; | ||
| 74 | $result{'color'} = $self->stroke_color($set); | ||
| 75 | $result{'opacity'} = $self->stroke_opacity($set); | ||
| 76 | $result{'width'} = $self->stroke_width($set); | ||
| 77 | return \%result; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub fill_color { shift->_style('fill', @_); } | ||
| 81 | sub fill_opacity { shift->_style('fill-opacity', @_); } | ||
| 82 | sub stroke_color { shift->_style('stroke', @_); } | ||
| 83 | sub stroke_width { shift->_style('stroke-width', @_); } | ||
| 84 | sub stroke_opacity { shift->_style('stroke-opacity', @_); } | ||
| 85 | sub stroke_linecap { shift->_style('stroke-linecap', @_); } | ||
| 86 | sub stroke_linejoin { shift->_style('stroke-linejoin', @_); } | ||
| 87 | sub stroke_miterlimit { shift->_style('stroke-miterlimit', @_); } | ||
| 88 | sub stroke_dasharray { shift->_style('stroke-dasharray', @_); } | ||
| 89 | |||
| 90 | sub _style | ||
| 91 | { | ||
| 92 | my ($self, $attr, $set) = @_; | ||
| 93 | if($set and defined($style_part{$attr})) { | ||
| 94 | $self->{'style'}->{$attr} = $style_part{$attr}->( value => $set, owner => $self, name => $attr ); | ||
| 95 | } elsif($set) { | ||
| 96 | $self->{'style'}->{$attr} = XML::DOM2::Attribute->new( value => $set, owner => $self, name => $attr ); | ||
| 97 | } | ||
| 98 | return $self->{'style'}->{$attr}; | ||
| 99 | } | ||
| 100 | |||
| 101 | return 1; |