| 1 | package SVG::Estimate::Polyline; | ||||||||||
| 2 | 10 | $SVG::Estimate::Polyline::VERSION = '1.0116'; | |||||||||
| 3 | 10 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 10 | ▶ | use Clone qw/clone/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 10 | ▶ | use List::Util qw/pairs/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 10 | extends 'SVG::Estimate::Shape'; | |||||||||
| 8 | 10 | 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 | 10 | 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 | 0 | ▶ | return []; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 64 | }, | ||||||||||
| 65 | 10 | ); | |||||||||
| 66 | |||||||||||
| 67 | ##Blatantly stolen from Image::SVG::Path | ||||||||||
| 68 | |||||||||||
| 69 | # Match the e or E in an exponent. | ||||||||||
| 70 | |||||||||||
| 71 | 10 | 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 | 10 | our $sign = qr/\+|\-/; | |||||||||
| 77 | |||||||||||
| 78 | 10 | our $wsp = qr/[\x20\x09\x0D\x0A]/; | |||||||||
| 79 | |||||||||||
| 80 | 10 | 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 | 10 | 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 | 7 | ▶ | my ($class, @args) = @_; | ||||||||
|
Subroutine
BUILDARGS: called CC=9 SCAR=22.0
Pod
undocumented
| |||||||||||
| 101 | ##Upgrade to hashref | ||||||||||
| 102 | 7 | ▶ | my $args = @args % 2 ? $args[0] : { @args }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 7 | my @pairs = $class->_get_pairs($args->{points}); | |||||||||
| 104 | 7 | my ($min_x, $max_x, $min_y, $max_y) = (1e10, -1e10, 1e10, -1e10); | |||||||||
| 105 | 7 | my $first = 1; | |||||||||
| 106 | 7 | my $start = []; | |||||||||
| 107 | 7 | my $length = 0; | |||||||||
| 108 | 7 | PAIR: foreach my $point (@pairs) { | |||||||||
| 109 | 75 | ▶ | if ($args->{transformer}->has_transforms) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | 20 | $point = $args->{transformer}->transform($point); | |||||||||
| 111 | } | ||||||||||
| 112 | 75 | ▶ | $min_x = $point->[0] if $point->[0] < $min_x; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | 75 | ▶ | $max_x = $point->[0] if $point->[0] > $max_x; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | 75 | ▶ | $min_y = $point->[1] if $point->[1] < $min_y; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | 75 | ▶ | $max_y = $point->[1] if $point->[1] > $max_y; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 116 | 75 | ▶ | if ($first) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | 7 | $first = 0; | |||||||||
| 118 | 7 | $start = $point; | |||||||||
| 119 | 7 | next PAIR; | |||||||||
| 120 | } | ||||||||||
| 121 | 68 | $length += $class->pythagorean($start, $point); | |||||||||
| 122 | 68 | $start = $point; | |||||||||
| 123 | } | ||||||||||
| 124 | 7 | $args->{parsed_points} = \@pairs; | |||||||||
| 125 | 7 | $args->{min_x} = $min_x; | |||||||||
| 126 | 7 | $args->{max_x} = $max_x; | |||||||||
| 127 | 7 | $args->{min_y} = $min_y; | |||||||||
| 128 | 7 | $args->{max_y} = $max_y; | |||||||||
| 129 | 7 | $args->{draw_start} = clone $pairs[0]; | |||||||||
| 130 | 7 | $args->{draw_end} = clone $pairs[-1]; | |||||||||
| 131 | 7 | $args->{shape_length} = $length; | |||||||||
| 132 | 7 | 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 | 7 | ▶ | my ($class, $string) = @_; | ||||||||
|
Subroutine
_get_pairs: called CC=1 SCAR=0.0
| |||||||||||
| 139 | 7 | my @points = split $split_re, $string; | |||||||||
| 140 | 7 | my @pairs = pairs @points; | |||||||||
| 141 | 7 | return @pairs; | |||||||||
| 142 | } | ||||||||||
| 143 | |||||||||||
| 144 | 10 | 1; | |||||||||