| 1 | package SVG::DOM2::Attribute::Metric; | ||
| 2 | |||
| 3 | use base "XML::DOM2::Attribute"; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use Carp; | ||
| 8 | |||
| 9 | sub new | ||
| 10 | { | ||
| 11 | my ($proto, %opts) = @_; | ||
| 12 | return $proto->SUPER::new(%opts); | ||
| 13 | } | ||
| 14 | |||
| 15 | sub serialise | ||
| 16 | { | ||
| 17 | my ($self) = @_; | ||
| 18 | my $result = $self->value.$self->unit; | ||
| 19 | return $result; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub deserialise | ||
| 23 | { | ||
| 24 | my ($self, $metric) = @_; | ||
| 25 | my ($value, $unit) = $metric =~ /^([\d\.]+)(.*)$/; | ||
| 26 | $unit = $self->document->default_unit if not $unit; | ||
| 27 | $self->{'value'} = $value; | ||
| 28 | $self->{'unit'} = $unit; | ||
| 29 | return $self; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub value | ||
| 33 | { | ||
| 34 | my ($self, %opts) = @_; | ||
| 35 | # Output Format | ||
| 36 | my $format = $opts{'format'}; | ||
| 37 | # Input Value | ||
| 38 | my $cvalue = $self->{'value'}; | ||
| 39 | # Return plain if no formating | ||
| 40 | return $cvalue if not defined $format; | ||
| 41 | # Output dots per inch | ||
| 42 | my $dpi = $opts{'dpi'} || $self->document->out_dpi; | ||
| 43 | # Input dots per Inch | ||
| 44 | my $cdpi = $self->document->dpi; | ||
| 45 | # Input Format | ||
| 46 | my $cformat = $self->unit; | ||
| 47 | return $cvalue if $cformat eq $format; | ||
| 48 | # Output Value | ||
| 49 | my $result = $cvalue; | ||
| 50 | warn "CURRENT: ($cvalue, $cdpi, $cformat) - ($dpi, $format)\n"; | ||
| 51 | if($cformat eq 'px') { | ||
| 52 | if($dpi and $cdpi and $dpi != $cdpi) { | ||
| 53 | $result /= ($cdpi / 100); | ||
| 54 | $result *= ($dpi / 100); | ||
| 55 | } | ||
| 56 | } | ||
| 57 | $result /= 100 and $cformat = 'cm' if $cformat eq 'm'; | ||
| 58 | $result *= 10 and $cformat = 'cm' if $cformat eq 'mm'; | ||
| 59 | $result /= 2.54 and $cformat = 'in' if $cformat eq 'cm'; | ||
| 60 | $result /= 12 and $cformat = 'in' if $cformat eq 'ft'; | ||
| 61 | $result *= $dpi if $cformat eq 'in'; | ||
| 62 | return $result if $format eq 'px'; | ||
| 63 | # my $in = $result / $dpi; | ||
| 64 | # return $in if $format eq 'in'; | ||
| 65 | # return $in * 12 if $format eq 'ft'; | ||
| 66 | # my $cm = $in * 2.54; | ||
| 67 | # return $cm if $format eq 'cm'; | ||
| 68 | # return $cm / 100 if $format eq 'm'; | ||
| 69 | # return $cm * 10 if $format eq 'mm'; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub unit | ||
| 73 | { | ||
| 74 | my ($self) = @_; | ||
| 75 | return $self->{'unit'}; | ||
| 76 | } | ||
| 77 | |||
| 78 | return 1; |