« blib/lib/SVG/Estimate/Path/VerticalLineto.pm
↑ Summary
blib/lib/SVG/Estimate/Path/CubicBezier.pm »
| 1 | package SVG::Estimate::Polyline; | ||
| 2 | $SVG::Estimate::Polyline::VERSION = '1.0116'; | ||
| 3 | use Moo; | ||
| 4 | use Clone qw/clone/; | ||
| 5 | use List::Util qw/pairs/; | ||
| 6 | |||
| 7 | extends 'SVG::Estimate::Shape'; | ||
| 8 | with 'SVG::Estimate::Role::Pythagorean'; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | SVG::Estimate::Polyline - Handles estimating multi-part lines. | ||
| 13 | |||
| 14 | =head1 VERSION | ||
| 15 | |||
| 16 | version 1.0116 | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | my $line = SVG::Estimate::Polyline->new( | ||
| 21 | transformer => $transform, | ||
| 22 | start_point => [45,13], | ||
| 23 | points => '20,20 40,25 60,40 80,120 120,140 200,180', | ||
| 24 | ); | ||
| 25 | |||
| 26 | my $length = $line->length; | ||
| 27 | |||
| 28 | =head1 INHERITANCE | ||
| 29 | |||
| 30 | This class extends L<SVG::Estimate::Shape> and consumes L<SVG::Estimate::Role::Pythagorean>. | ||
| 31 | |||
| 32 | =head1 METHODS | ||
| 33 | |||
| 34 | =head2 new() | ||
| 35 | |||
| 36 | Constructor. | ||
| 37 | |||
| 38 | =over | ||
| 39 | |||
| 40 | =item points | ||
| 41 | |||
| 42 | A string listing points for the polyline as defined by L<http://www.w3.org/TR/SVG/shapes.html>. | ||
| 43 | |||
| 44 | =back | ||
| 45 | |||
| 46 | =cut | ||
| 47 | |||
| 48 | has points => ( | ||
| 49 | is => 'ro', | ||
| 50 | required => 1, | ||
| 51 | ); | ||
| 52 | |||
| 53 | =head2 parsed_points() | ||
| 54 | |||
| 55 | Returns an array reference of array references marking the parsed C<points> string. | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | has parsed_points => ( | ||
| 60 | is => 'ro', | ||
| 61 | lazy => 1, | ||
| 62 | default => sub { | ||
| 63 | return []; | ||
| 64 | }, | ||
| 65 | ); | ||
| 66 | |||
| 67 | ##Blatantly stolen from Image::SVG::Path | ||
| 68 | |||
| 69 | # Match the e or E in an exponent. | ||
| 70 | |||
| 71 | my $e = qr/[eE]/; | ||
| 72 | |||
| 73 | # These regular expressions are directly taken from the SVG grammar, | ||
| 74 | # https://www.w3.org/TR/SVG/paths.html#PathDataBNF | ||
| 75 | |||
| 76 | our $sign = qr/\+|\-/; | ||
| 77 | |||
| 78 | our $wsp = qr/[\x20\x09\x0D\x0A]/; | ||
| 79 | |||
| 80 | our $comma_wsp = qr/(?:$wsp+,?$wsp*|,$wsp*)/; | ||
| 81 | |||
| 82 | # The following regular expression splits the path into pieces Note we | ||
| 83 | # only split on '-' or '+' when not preceeded by 'e'. This regular | ||
| 84 | # expression is not following the SVG grammar, it is going our own | ||
| 85 | # way. | ||
| 86 | |||
| 87 | my $split_re = qr/ | ||
| 88 | (?: | ||
| 89 | $wsp*,$wsp* | ||
| 90 | | | ||
| 91 | (?<!$e)(?=-) | ||
| 92 | | | ||
| 93 | (?<!$e)(?:\+) | ||
| 94 | | | ||
| 95 | $wsp+ | ||
| 96 | ) | ||
| 97 | /x; | ||
| 98 | |||
| 99 | sub BUILDARGS { | ||
| 100 | my ($class, @args) = @_; | ||
| 101 | ##Upgrade to hashref | ||
| 102 | my $args = @args % 2 ? $args[0] : { @args }; | ||
| 103 | my @pairs = $class->_get_pairs($args->{points}); | ||
| 104 | my ($min_x, $max_x, $min_y, $max_y) = (1e10, -1e10, 1e10, -1e10); | ||
| 105 | my $first = 1; | ||
| 106 | my $start = []; | ||
| 107 | my $length = 0; | ||
| 108 | PAIR: foreach my $point (@pairs) { | ||
| 109 | if ($args->{transformer}->has_transforms) { | ||
| 110 | $point = $args->{transformer}->transform($point); | ||
| 111 | } | ||
| 112 | $min_x = $point->[0] if $point->[0] < $min_x; | ||
| 113 | $max_x = $point->[0] if $point->[0] > $max_x; | ||
| 114 | $min_y = $point->[1] if $point->[1] < $min_y; | ||
| 115 | $max_y = $point->[1] if $point->[1] > $max_y; | ||
| 116 | if ($first) { | ||
| 117 | $first = 0; | ||
| 118 | $start = $point; | ||
| 119 | next PAIR; | ||
| 120 | } | ||
| 121 | $length += $class->pythagorean($start, $point); | ||
| 122 | $start = $point; | ||
| 123 | } | ||
| 124 | $args->{parsed_points} = \@pairs; | ||
| 125 | $args->{min_x} = $min_x; | ||
| 126 | $args->{max_x} = $max_x; | ||
| 127 | $args->{min_y} = $min_y; | ||
| 128 | $args->{max_y} = $max_y; | ||
| 129 | $args->{draw_start} = clone $pairs[0]; | ||
| 130 | $args->{draw_end} = clone $pairs[-1]; | ||
| 131 | $args->{shape_length} = $length; | ||
| 132 | return $args; | ||
| 133 | } | ||
| 134 | |||
| 135 | ##This method is here so that Polygon can wrap it to add a closing point. | ||
| 136 | |||
| 137 | sub _get_pairs { | ||
| 138 | my ($class, $string) = @_; | ||
| 139 | my @points = split $split_re, $string; | ||
| 140 | my @pairs = pairs @points; | ||
| 141 | return @pairs; | ||
| 142 | } | ||
| 143 | |||
| 144 | 1; |