blib/lib/Graphics/Primitive/Canvas.pm

filter: Statementstmt 100% 32 / 32 Branchbran 50.0% 2 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 95.9% 47 / 49 SCAR 11.0
CC
3
Cov
94%
restore
2.0
do
2.0
BEGIN
1.0
CRAP 3.0
« blib/lib/Graphics/Primitive/Insets.pm ↑ Summary blib/lib/Graphics/Primitive/Driver/TextLayout.pm »
1 package Graphics::Primitive::Canvas;
2 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use MooseX::Storage;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 extends 'Graphics::Primitive::Component';
6  
7 1 with 'MooseX::Clone';
8 1 with Storage (format => 'JSON', io => 'File');
9  
10 1 ▶use Graphics::Primitive::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 has path => (
13 isa => 'Graphics::Primitive::Path',
14 is => 'rw',
15 1 ▶ default => sub { Graphics::Primitive::Path->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
16 1 handles => [
17 'arc', 'close_path', 'current_point', 'curve_to', 'line_to',
18 'move_to', 'rectangle', 'rel_curve_to', 'rel_line_to', 'rel_move_to'
19 ]
20 );
21  
22 has paths => (
23 traits => [qw(Array Clone)],
24 isa => 'ArrayRef',
25 is => 'rw',
26 1 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
27 1 handles => {
28 add_path => 'push',
29 path_count => 'count',
30 get_path => 'get',
31 }
32 );
33  
34 has saved_paths => (
35 traits => [qw(Array Copy)],
36 isa => 'ArrayRef',
37 is => 'rw',
38 1 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
39 1 handles => {
40 push_path => 'push',
41 pop_path => 'pop',
42 saved_path_count => 'count',
43 }
44 );
45  
46 sub do {
47 2 ▶ my ($self, $op) = @_;
Subroutine do: called CC=2 SCAR=7.0
Pod documented
48  
49 2 $self->add_path({ op => $op, path => $self->path->clone });
50 # Don't replace the current path if we are preserving.
51 2 ▶ unless($op->preserve) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($op->preserve) 2 0 2
52 2 $self->path(Graphics::Primitive::Path->new);
53 }
54 }
55  
56 sub save {
57 2 ▶ my ($self) = @_;
Subroutine save: called CC=1 SCAR=0.0
Pod documented
58  
59 2 $self->push_path($self->path->clone);
60 }
61  
62 sub restore {
63 2 ▶ my ($self) = @_;
Subroutine restore: called CC=2 SCAR=7.1
Pod documented
64  
65 2 ▶ return if($self->saved_path_count < 1);
Branch: 1 branch
BranchTrueFalseTotal
if $self->saved_path_count < 1 0 2 2
66  
67 2 $self->path($self->pop_path);
68 }
69  
70 1 __PACKAGE__->meta->make_immutable;
71  
72 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 1 1;
74 __END__
75 =head1 NAME
76  
77 Graphics::Primitive::Canvas - Component composed of paths
78  
79 =head1 DESCRIPTION
80  
81 Graphics::Primitive::Canvas is a component for drawing arbitrary things. It
82 holds L<Paths|Graphics::Primitive::Path> and
83 L<Operations|Graphics::Primitive::Operation>.
84  
85 =head1 SYNOPSIS
86  
87 use Graphics::Primitive::Canvas;
88  
89 my $canvas = Graphics::Primitive::Canvas->new;
90 $canvas->move_to($point); # or just $x, $y
91 $canvas->do($op);
92  
93 =head1 DESCRIPTION
94  
95 The Canvas is a container for multiple L<Paths|Graphics::Primitive::Path>.
96 It has a I<path> that is the operative path for all path-related
97 methods. You can treat the Canvas as if it was a path, calling methods
98 like I<line_to> or I<move_to>.
99  
100 When you are ready to perform an operation on the path, call the I<do> method
101 with the operation you want to call as an argument. Drawing a line and
102 stroking it would look like:
103  
104 $canvas->move_to(0, 0);
105 $canvas->line_to(10, 10);
106 my $op = Graphics::Primitive::Operation::Stroke->new;
107 $stroke->brush->color(
108 Graphics::Color::RGB->new(red => 0, blue => 1, green => 1)
109 );
110 $canvas->do($op);
111  
112 When you instantiate a Canvas a newly instantiated path resides in
113 I<path>. After you call I<do> that current path is moved to the I<paths>
114 list and new path is placed in I<current_path>. If you want to keep the path
115 around you can call I<save> before I<do> then call I<restore> to put a saved
116 copy of the path back into I<path>.
117  
118 =head1 METHODS
119  
120 =head2 Constructor
121  
122 =over 4
123  
124 =item I<new>
125  
126 Creates a new Graphics::Primitive::Canvas
127  
128 =back
129  
130 =head2 Instance Methods
131  
132 =over 4
133  
134 =item I<do>
135  
136 Given an operation, pushes the current path onto the path stack.
137  
138 FIXME: Example
139  
140 =item I<path>
141  
142 The current path this canvas is using.
143  
144 =item I<path_count>
145  
146 Count of paths in I<paths>.
147  
148 =item I<paths>
149  
150 Arrayref of hashrefs representing paths combined with their operations:
151
152 [
153 {
154 path => $path,
155 op => $op
156 },
157 ]
158  
159 =item I<restore>
160  
161 Replace the current path by popping the top path from the saved path list.
162  
163 =item I<save>
164  
165 Copy the current path and push it onto the stack of saved paths.
166  
167 =item I<saved_paths>
168  
169 List of saved paths. Add to the list with I<save> and pop from it using
170 I<restore>.
171  
172 =item I<saved_path_count>
173  
174 Count of paths saved in I<saved_paths>.
175  
176 =back
177  
178 =head1 AUTHOR
179  
180 Cory Watson <gphat@cpan.org>
181  
182 =head1 COPYRIGHT & LICENSE
183  
184 Copyright 2008-2010 by Cory G Watson.
185  
186 You can redistribute and/or modify this code under the same terms as Perl
187 itself.
« blib/lib/Graphics/Primitive/Insets.pm ↑ Summary blib/lib/Graphics/Primitive/Driver/TextLayout.pm »