blib/lib/CairoX/Sweet/Path.pm untested

filter: Statementstmt 0.0% 0 / 73 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod n/a totaltotal 0.0% 0 / 94 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 use 5.14.0;
2 use strict;
3 use warnings;
4 use CairoX::Sweet::Standard;
5  
6 # PODNAME: CairoX::Sweet::Path
7  
8 class CairoX::Sweet::Path using Moose {
9  
10 our $VERSION = '0.0108'; # VERSION
11 # ABSTRACT: Handles a path
12  
13 use Type::Utils qw/enum/;
14 use CairoX::Sweet::Core::LineTo;
15  
16 has move => (
17 is => 'rw',
18 isa => Maybe[MoveTo],
19 predicate => 1,
20 coerce => 1,
21 );
22 has start => (
23 is => 'rw',
24 isa => Maybe[MoveTo],
25 predicate => 1,
26 coerce => 1,
27 );
28 has color => (
29 is => 'rw',
30 isa => Maybe[Color],
31 coerce => 1,
32 predicate => 1,
33 );
34 has background_color => (
35 is => 'rw',
36 isa => Maybe[Color],
37 coerce => 1,
38 predicate => 1,
39 );
40 has width => (
41 is => 'rw',
42 isa => Maybe[Num],
43 predicate => 1,
44 );
45 has cap => (
46 is => 'rw',
47 isa => enum([qw/butt round square/]),
48 default => 'butt',
49 );
50 has join => (
51 is => 'rw',
52 isa => enum([qw/miter round bevel/]),
53 default => 'miter',
54 );
55 has commands => (
56 is => 'rw',
57 isa => ArrayRef,
58 traits => ['Array'],
59 handles => {
60 add_command => 'push',
61 all_commands => 'elements',
62 get_command => 'get',
63 count_commands => 'count',
64 },
65 );
66 around BUILDARGS($orig: $self, @args) {
67 my %args = @args;
68 if(exists $args{'start'}) {
69 $args{'start'} = CairoX::Sweet::Core::MoveTo->new(@{ $args{'start'} }, is_relative => 0);
70 }
71 elsif(exists $args{'move'}) {
72 $args{'move'} = CairoX::Sweet::Core::MoveTo->new(@{ $args{'move'} }, is_relative => 1);
73 }
74 $self->$orig(%args);
75 }
76 method BUILD {
77 if($self->has_start) {
78 $self->add_command($self->start);
79 }
80 elsif($self->has_move) {
81 $self->add_command($self->move);
82 }
83 }
84 around color, background_color($orig: $self, @args) {
85 if(scalar @args == 3) {
86 $self->$orig(\@args);
87 }
88 else {
89 $self->$orig(@args);
90 }
91 }
92 method purge {
93 $self->commands([]);
94 }
95  
96 method my $add_move($is_relative, @values) {
97 while(scalar @values >= 2) {
98 $self->add_command(CairoX::Sweet::Core::MoveTo->new(splice(@values, 0, 2), is_relative => $is_relative));
99 }
100 return $self;
101 }
102 method add_start(@values) {
103 return $self->$add_move(0, @values);
104 }
105 method add_move(@values) {
106 return $self->$add_move(1, @values);
107 }
108  
109 method my $add_line($is_relative, @values) {
110 while(scalar @values >= 2) {
111 $self->add_command(CairoX::Sweet::Core::LineTo->new(splice(@values, 0, 2), is_relative => $is_relative));
112 }
113 return $self;
114 }
115 method add_line(@values) {
116 return $self->$add_line(0, @values);
117 }
118 method add_relative_line(@values) {
119 return $self->$add_line(1, @values);
120 }
121  
122 method add_curve(@values) {
123 while(scalar @values >= 6) {
124 $self->add_command(CairoX::Sweet::Core::CurveTo->new(splice(@values, 0,6), is_relative => 0));
125 }
126 return $self;
127 }
128 method add_relative_curve(@values) {
129 while(scalar @values >= 6) {
130 $self->add_command(CairoX::Sweet::Core::CurveTo->new(splice(@values, 0, 6), is_relative => 1));
131 }
132 return $self;
133 }
134  
135 method move_path(Num :$x = 0, Num :$y = 0) {
136 return if !defined $x && !defined $y;
137  
138 foreach my $command ($self->all_commands) {
139 $command->move_location(x => $x, y => $y);
140 }
141 }
142 }
143  
144 1;
145  
146 __END__
147  
148 =pod
149  
150 =encoding utf-8
151  
152 =head1 NAME
153  
154 CairoX::Sweet::Path - Handles a path
155  
156 =head1 VERSION
157  
158 Version 0.0108, released 2015-03-21.
159  
160 =head1 SYNOPSIS
161  
162 use CairoX::Sweet;
163  
164 my $c = CairoX::Sweet->new(500, 500, '#ffffff');
165 my $path = CairoX::Sweet::Path->new(start => [35, 50], color => '#8855bb', width => 10, cap => 'round', join => 'round');
166  
167 $path->add_relative_line(qw/
168 20 -5
169 10 0
170 30 -20
171 -50 0
172 /);
173 $c->add_path($path, close => 1);
174  
175 =head1 ATTRIBUTES
176  
177 =head2 move => [x, y]
178  
179 Array reference of two numbers. Optional, can't be used together with C<start>.
180  
181 Sets the starting position of the path to C<x> and C<y> points from the current position.
182  
183 $path = CairoX::Sweet::Path->new(move => [35, 50], ...);
184  
185 =head2 start = [x, y]
186  
187 Array reference of two numbers. Optional, can't be used together with C<move>.
188  
189 Sets the starting position of the path to C<x> and C<y> points from the [0, 0] point.
190  
191 $path = CairoX::Sweet::Path->new(start => [135, 150], ...);
192  
193 =head2 color => Color
194  
195 A color. Optional. See L<CairoX::Sweet::background_color|CairoX::Sweet/"background_color"> for more information on colors.
196  
197 Sets the pen color. B<stroke> will always be called on the path, if C<color> isn't set then the current color is used.
198  
199 $path = CairoX::Sweet::Path->new(color => '#8855bb', ...);
200  
201 =head2 background_color => Color
202  
203 A color. Optional. See L<CairoX::Sweet::background_color|CairoX::Sweet/"background_color"> for more information on colors.
204  
205 Sets the background color. If C<background_color> is given, B<fill> will be called on the path.
206  
207 $path = CairoX::Sweet::Path->new(background_color => [240, 245, 240], ...);
208  
209 =head2 width => x
210  
211 A number. Optional. Sets the pen width.
212  
213 $path = CairoX::Sweet::Path->new(width => 10, ...);
214  
215 =head2 cap => cap_type
216  
217 One of C<butt>, C<round> or C<square>.
218  
219 =head2 join => join_type
220  
221 One of C<miter>, C<round> or C<bevel>.
222  
223 =head1 METHODS
224  
225 =head2 add_line(x1, y1, x2, y2, ...)
226  
227 The argument must be a list with an even number of items. The positions are absolute. Uses B<line_to> in L<Cairo>.
228  
229 $path->add_line(qw/
230 20 30
231 30 40
232 30 80
233 40 80
234 /);
235  
236 Note that the line starts at the current position, usually set by C<move> or C<start> in the constructor.
237  
238 Pushes a line onto the path. Note that any number of calls to the add_*_line, add_*_curve, add_start and add_move can be made. They will be combined into one path in the end.
239  
240 =head2 add_relative_line(x1, y1, x2, y2, ...)
241  
242 The argument must be a list a number of items. Similar to C<add_line()> except that the positions are relative to the previous current position. Uses B<line_rel_to> in L<Cairo>.
243  
244 =head2 add_curve(bezier_1ax, bezier_1ay, bezier_1bx, bezier_1by, x1, y1, ...)
245  
246 The argument must be a list a number of items divisible by six. The positions are absolute. Uses B<curve_to> in L<Cairo>.
247  
248 $path->add_curve(qw/
249 441 157 461 149 457 135
250 454 119 434 113 421 109
251 422 105 423 101 424 97
252 424 87 416 83 408 84
253 /);
254  
255 =head2 add_relative_curve(bezier_1ax, bezier_1ay, bezier_1bx, bezier_1by, x1, y1, ...)
256  
257 The argument must be a list a number of items divisible by six. Similar to C<add_curve()> except that the positions are relative to the previous current position. Uses B<curve_rel_to> in L<Cairo>.
258  
259 =head2 add_move(x, y)
260  
261 Moves the current position C<x>/C<y> points from the current position.
262  
263 =head2 add_start(x, y)
264  
265 Moves the current position to C<x>/C<y> from the 0/0 point.
266  
267 =head2 move_path(x => 30, y => 30)
268  
269 Moves all points in the path C<x> and C<y> points from their current positions. This means you can do this:
270  
271 my $c = CairoX::Sweet->new(220, 130, background_color => '#ffffff');
272 my $path = CairoX::Sweet::Path->new(start => [25, 50], color => '#8855bb', width => 10, join => 'miter');
273  
274 $path->add_line(qw/
275 45 95
276 55 95
277 /);
278 $path->add_curve(qw/
279 70 110 90 90 100 100
280 110 90 110 90 120 90
281 130 80 130 80 140 105
282 155 120 170 80 185 75
283 /);
284 $path->add_line(qw/
285 185 45
286 150 10
287 135 20
288 /);
289 $c->add_path($path, close => 1);
290  
291 $path->color('#bb99cc');
292 $path->join('round');
293 $path->cap('round');
294 $path->move_path(x => 12, y => 12);
295 $c->add_path($path);
296  
297 $c->surface->write_to_png('image.png');
298  
299 Which produces:
300  
301 =for HTML <p><img src="https://raw.githubusercontent.com/Csson/p5-CairoX-Sweet/master/static/images/move_path.png" /></p>
302  
303 =head2 purge()
304  
305 Empties lines and curves.
306  
307 =head1 SEE ALSO
308  
309 =over 4
310  
311 =item *
312  
313 L<CairoX::Sweet::Path>
314  
315 =item *
316  
317 L<Cairo>
318  
319 =back
320  
321 =head1 SOURCE
322  
323 L<https://github.com/Csson/p5-CairoX-Sweet>
324  
325 =head1 HOMEPAGE
326  
327 L<https://metacpan.org/release/CairoX-Sweet>
328  
329 =head1 AUTHOR
330  
331 Erik Carlsson <info@code301.com>
332  
333 =head1 COPYRIGHT AND LICENSE
334  
335 This software is copyright (c) 2015 by Erik Carlsson <info@code301.com>.
336  
337 This is free software; you can redistribute it and/or modify it under
338 the same terms as the Perl 5 programming language system itself.
339  
340 =cut