| 1 | package SVG::Estimate::Path; | ||
| 2 | $SVG::Estimate::Path::VERSION = '1.0116'; | ||
| 3 | use Moo; | ||
| 4 | use Image::SVG::Path qw/extract_path_info/; | ||
| 5 | use SVG::Estimate::Path::Moveto; | ||
| 6 | use SVG::Estimate::Path::Lineto; | ||
| 7 | use SVG::Estimate::Path::CubicBezier; | ||
| 8 | use SVG::Estimate::Path::QuadraticBezier; | ||
| 9 | use SVG::Estimate::Path::HorizontalLineto; | ||
| 10 | use SVG::Estimate::Path::VerticalLineto; | ||
| 11 | use SVG::Estimate::Path::Arc; | ||
| 12 | use Clone qw/clone/; | ||
| 13 | use Ouch; | ||
| 14 | |||
| 15 | extends 'SVG::Estimate::Shape'; | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | SVG::Estimate::Path - Handles estimating arbitrary vectors. | ||
| 20 | |||
| 21 | =head1 VERSION | ||
| 22 | |||
| 23 | version 1.0116 | ||
| 24 | |||
| 25 | =head1 SYNOPSIS | ||
| 26 | |||
| 27 | my $path = SVG::Estimate::Path->new( | ||
| 28 | transformer => $transform, | ||
| 29 | start_point => [45,13], | ||
| 30 | d => 'M150 0 L75 200 L225 200 Z', | ||
| 31 | ); | ||
| 32 | |||
| 33 | my $length = $path->length; | ||
| 34 | |||
| 35 | =head1 INHERITANCE | ||
| 36 | |||
| 37 | This class extends L<SVG::Estimate::Shape>. | ||
| 38 | |||
| 39 | =head1 METHODS | ||
| 40 | |||
| 41 | =head2 new() | ||
| 42 | |||
| 43 | Constructor. | ||
| 44 | |||
| 45 | =over | ||
| 46 | |||
| 47 | =item d | ||
| 48 | |||
| 49 | An SVG path string as described L<http://www.w3.org/TR/SVG/paths.html>. | ||
| 50 | |||
| 51 | =back | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | has d => ( is => 'ro', required => 1, ); | ||
| 56 | has commands => ( is => 'ro', ); | ||
| 57 | has internal_travel_length => ( is => 'ro', required => 1, ); | ||
| 58 | has summarize => ( is => 'ro', default => sub { 0 }, ); | ||
| 59 | |||
| 60 | sub BUILDARGS { | ||
| 61 | my ($class, @args) = @_; | ||
| 62 | ##Upgrade to hashref | ||
| 63 | my $args = @args % 2 ? $args[0] : { @args }; | ||
| 64 | |||
| 65 | if (! exists $args->{d} || $args->{d} =~ /^\s*$/) { | ||
| 66 | $args->{d} = "m 0 0"; | ||
| 67 | } | ||
| 68 | my @path_info = extract_path_info($args->{d}, { absolute => 1, no_shortcuts=> 1, }); | ||
| 69 | my @commands = (); | ||
| 70 | |||
| 71 | my $first_flag = 1; | ||
| 72 | my $first; | ||
| 73 | my $cursor = clone $args->{start_point}; | ||
| 74 | $args->{length} = 0; | ||
| 75 | foreach my $subpath (@path_info) { | ||
| 76 | $subpath->{transformer} = $args->{transformer}; | ||
| 77 | ##On the first command, set the start point to the moveto destination, otherwise the travel length gets counted twice. | ||
| 78 | $subpath->{start_point} = clone $cursor; | ||
| 79 | my $command = $subpath->{type} eq 'moveto' ? SVG::Estimate::Path::Moveto->new($subpath) | ||
| 80 | : $subpath->{type} eq 'line-to' ? SVG::Estimate::Path::Lineto->new($subpath) | ||
| 81 | : $subpath->{type} eq 'cubic-bezier' ? SVG::Estimate::Path::CubicBezier->new($subpath) | ||
| 82 | : $subpath->{type} eq 'quadratic-bezier' ? SVG::Estimate::Path::QuadraticBezier->new($subpath) | ||
| 83 | : $subpath->{type} eq 'horizontal-line-to' ? SVG::Estimate::Path::HorizontalLineto->new($subpath) | ||
| 84 | : $subpath->{type} eq 'vertical-line-to' ? SVG::Estimate::Path::VerticalLineto->new($subpath) | ||
| 85 | : $subpath->{type} eq 'arc' ? SVG::Estimate::Path::Arc->new($subpath) | ||
| 86 | : $subpath->{type} eq 'closepath' ? '' #Placeholder so we don't fall through | ||
| 87 | : ouch('unknown_path', "Unknown subpath type ".$subpath->{type}) ; ##Something bad happened | ||
| 88 | if ($subpath->{type} eq 'closepath') { | ||
| 89 | $subpath->{point} = clone $first->point; | ||
| 90 | $command = SVG::Estimate::Path::Lineto->new($subpath); | ||
| 91 | } | ||
| 92 | $cursor = clone $command->end_point; | ||
| 93 | if ($first_flag) { | ||
| 94 | ##Save the first point in order to handle a closepath, if it exists | ||
| 95 | $first_flag = 0; | ||
| 96 | $first = $command; ##According to SVG, this will be a Moveto. | ||
| 97 | $args->{min_x} = $command->min_x; | ||
| 98 | $args->{max_x} = $command->max_x; | ||
| 99 | $args->{min_y} = $command->min_y; | ||
| 100 | $args->{max_y} = $command->max_y; | ||
| 101 | $args->{draw_start} = $command->end_point; | ||
| 102 | } | ||
| 103 | elsif ($subpath->{type} eq 'moveto') { | ||
| 104 | $first = $command; ##Save for the next close, if it comes | ||
| 105 | } | ||
| 106 | $args->{shape_length} += $command->shape_length; | ||
| 107 | $args->{internal_travel_length} += $command->travel_length; | ||
| 108 | $args->{min_x} = $command->min_x if $command->min_x < $args->{min_x}; | ||
| 109 | $args->{max_x} = $command->max_x if $command->max_x > $args->{max_x}; | ||
| 110 | $args->{min_y} = $command->min_y if $command->min_y < $args->{min_y}; | ||
| 111 | $args->{max_y} = $command->max_y if $command->max_y > $args->{max_y}; | ||
| 112 | if (exists $args->{summarize} && $args->{summarize}) { | ||
| 113 | $command->summarize_myself; | ||
| 114 | } | ||
| 115 | push @commands, $command; | ||
| 116 | } | ||
| 117 | $args->{draw_end} = $cursor; | ||
| 118 | |||
| 119 | $args->{commands} = \@commands; | ||
| 120 | return $args; | ||
| 121 | } | ||
| 122 | |||
| 123 | ##Return the internally calculated travel length, not the standard one use by SVG::Estimate::Shape | ||
| 124 | sub travel_length { | ||
| 125 | my $self = shift; | ||
| 126 | my $length = $self->internal_travel_length; | ||
| 127 | return $length; | ||
| 128 | } | ||
| 129 | |||
| 130 | 1; |