lib/Graphics/Primitive/Driver.pm

filter: Statementstmt 84.4% 38 / 45 Branchbran 63.3% 19 / 30 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 5 / 5 Podpod 100% 3 / 3 totaltotal 70.1% 68 / 97 SCAR 35.1
CC
22
Cov
71%
draw
22.6
prepare
7.1
finalize
6.9
CRAP 33.3
« blib/lib/Graphics/Primitive/Driver.pm ↑ Summary blib/lib/Graphics/Primitive/ComponentList.pm »
1 package Graphics::Primitive::Driver;
2 2 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 requires qw(
5 _draw_arc _draw_bezier _draw_canvas _draw_circle _draw_component
6 _draw_ellipse _draw_line _draw_path _draw_polygon _draw_rectangle
7 _draw_textbox _do_fill _do_stroke _finish_page _resize data
8 get_textbox_layout reset write
9 );
10  
11 2 has 'height' => (
12 is => 'rw',
13 isa => 'Num'
14 );
15 2 has 'width' => (
16 is => 'rw',
17 isa => 'Num'
18 );
19  
20 sub draw {
21 2 ▶ my ($self, $comp) = @_;
Subroutine draw: called CC=11 SCAR=31.2
Pod documented
22  
23 2 ▶ if($comp->page) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comp->page) 0 2 2
24 # FIRST_PAGE is a little protection to ensure that we don't call
25 # show page on the first page, as that would mean we'd have an
26 # empty first page all the time.
27 0 ▶ if($self->{FIRST_PAGE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'FIRST_PAGE'}) { } 0 0 0
28 0 $self->_finish_page;
29 } else {
30 0 $self->{FIRST_PAGE} = 1;
31 }
32 0 $self->_resize($comp->width, $comp->height);
33 }
34  
35 2 ▶ die('Components must be objects.') unless ref($comp);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $comp 0 2 2
36 # The order of this is important, since isa will return true for any
37 # superclass...
38 # TODO Check::ISA
39 2 ▶ if($comp->isa('Graphics::Primitive::Canvas')) {
Branch: 4 branches
BranchTrueFalseTotal
if ($comp->isa('Graphics::Primitive::Canvas')) { } 0 2 2
elsif ($comp->isa('Graphics::Primitive::Image')) { } 0 2 2
elsif ($comp->isa('Graphics::Primitive::TextBox')) { } 0 2 2
elsif ($comp->isa('Graphics::Primitive::Component')) { } 2 0 2
40 0 $self->_draw_canvas($comp);
41 } elsif($comp->isa('Graphics::Primitive::Image')) {
42 0 $self->_draw_image($comp);
43 } elsif($comp->isa('Graphics::Primitive::TextBox')) {
44 0 $self->_draw_textbox($comp);
45 } elsif($comp->isa('Graphics::Primitive::Component')) {
46 2 $self->_draw_component($comp);
47 }
48  
49 2 ▶ if($comp->isa('Graphics::Primitive::Container')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comp->isa("Graphics::Primitive::Container")) 1 1 2
50 1 ▶ if($comp->can('components')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comp->can("components")) 1 0 1
51 1 foreach my $subcomp (@{ $comp->components }) {
52 1 $self->draw($subcomp);
53 }
54 }
55 }
56 }
57  
58 sub finalize {
59 4 ▶ my ($self, $comp) = @_;
Subroutine finalize: called CC=6 SCAR=19.3
Pod documented
60  
61 4 $comp->finalize($self);
62  
63 4 ▶ if($comp->isa('Graphics::Primitive::Container')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comp->isa("Graphics::Primitive::Container")) 2 2 4
64 2 foreach my $c (@{ $comp->components }) {
65 2 ▶ next unless defined($c) && defined($c)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c and defined $c and $c->visible 0 2 2
Condition33%2/6
defined $c and defined $c and $c->visible
!ll&&!rl&&r
002
defined $c and defined $c
!ll&&!rl&&r
002
MC/DC0%0/3
defined $c and defined $c and $c->visible
defined $c defined $c $c->visible
Truth table25%1/4
A and B and C
A= defined $cB= defined $cC= $c->visible
ABCresult
0XX0
10X0
1100
1111
66 && $c->visible;
67 2 $self->finalize($c);
68 }
69 }
70 }
71  
72 sub prepare {
73 4 ▶ my ($self, $comp) = @_;
Subroutine prepare: called CC=7 SCAR=19.6
Pod documented
74  
75 4 ▶ unless(defined($self->width)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->width) 2 2 4
76 2 $self->width($comp->width);
77 }
78 4 ▶ unless(defined($self->height)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->height) 2 2 4
79 2 $self->height($comp->height);
80 }
81  
82 4 $comp->prepare($self);
83  
84 # TODO Check::ISA
85 4 ▶ if($comp->isa('Graphics::Primitive::Container')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comp->isa("Graphics::Primitive::Container")) 2 2 4
86 2 foreach my $c (@{ $comp->components }) {
87 2 ▶ next unless defined($c) && $c->visible;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c and $c->visible 0 2 2
Condition33%1/3
defined $c and $c->visible
!ll&&!rl&&r
002
MC/DC0%0/2
defined $c and $c->visible
defined $c $c->visible
Truth table33%1/3
A and B
A= defined $cB= $c->visible
ABresult
0X0
100
111
88 2 $self->prepare($c);
89 }
90 }
91 }
92  
93 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
94 2 1;
95 __END__
96  
97 =head1 NAME
98  
99 Graphics::Primitive::Driver - Role for driver implementations
100  
101 =head1 DESCRIPTION
102  
103 What good is a library agnostic intermediary representation of graphical
104 components if you can't feed them to a library specific implementation that
105 turns them into drawings? Psht, none!
106  
107 To write a driver for Graphics::Primitive implement this role.
108  
109 =head1 SYNOPSIS
110  
111 my $c = Graphics::Primitive::Component->new({
112 origin => Geometry::Primitive::Point->new({
113 x => $x, y => $y
114 }),
115 width => 500, height => 350
116 });
117  
118 =head1 CANVASES
119  
120 When a path is added to the internal list via I<do>, it is stored in the
121 I<paths> attribute as a hashref. The hashref has two keys: B<path> and B<op>.
122 The path is, well, the path. The op is the operation provided to I<do>. As
123 canvases are just lists of paths you should consult the next section as well.
124  
125 =head1 PATHS AND HINTING
126  
127 Paths are lists of primitives. Primitives are all descendants of
128 L<Geometry::Shape> and therefore have I<point_start> and I<point_end>. These
129 two attributes allow the chaining of primitives. To draw a path you should
130 iterate over the primitives, drawing each.
131  
132 When you pull each path from the arrayref you should pull it's accompanying
133 hints via I<get_hint> (the indexes match). The hint may provide you with
134 additional information:
135  
136 =head2 PRIMITIVE HINTS
137  
138 =over 4
139  
140 =item I<contiguous>
141  
142 True if this primitive is contiguous with the previous one. Example: Used to
143 determine if a new sub-path is needed for the Cairo driver.
144  
145 =back
146  
147 =head2 OPERATION HINTS
148  
149 =over 4
150  
151 =item I<preserve>
152  
153 =back
154  
155 =head1 WARNING
156  
157 Only this class or the driver itself should call methods starting with an
158 underscore, as this interface may change.
159  
160 =head1 METHODS
161  
162 =over 4
163  
164 =item I<_do_stroke ($strokeop)>
165  
166 Perform a stroke.
167  
168 =item I<_do_fill ($fillop)>
169  
170 Perform a fill.
171  
172 =item I<_draw_arc ($arc)>
173  
174 Draw an arc.
175  
176 =item I<_draw_canvas ($canvas)>
177  
178 Draw a canvas.
179  
180 =item I<_draw_component ($comp)>
181  
182 Draw a component.
183  
184 =item I<_draw_line ($line)>
185  
186 Draw a line.
187  
188 =item I<_draw_rectangle ($rect)>
189  
190 Draw a rectangle.
191  
192 =item I<_draw_textbox>
193  
194 Draw a textbox.
195  
196 =item I<_resize ($width, $height)>
197  
198 Resize the current working surface to the size specified.
199  
200 =item I<_finish_page>
201  
202 Finish the current 'page' and start a new one. Some drivers that are not
203 paginated may need to emulate this behaviour.
204  
205 =item I<data>
206  
207 Retrieve the results of this driver's operations.
208  
209 =item I<draw>
210  
211 Draws the given Graphics::Primitive::Component. If the component is a
212 container then all components therein are drawn, recursively.
213  
214 =item I<get_text_bounding_box>
215  
216 Given a L<Font|Graphics::Primitive::Font> and a string, returns a bounding box
217 of the rendered text.
218  
219 =item I<finalize>
220  
221 Finalize the supplied component and any child components, recursively.
222  
223 =item I<prepare>
224  
225 Prepare the supplied component and any child components, recursively.
226  
227 =item I<write>
228  
229 Write out the results of this driver's operations to the specified file.
230  
231 =back
232  
233 =head1 AUTHOR
234  
235 Cory Watson, C<< <gphat@cpan.org> >>
236  
237 =head1 COPYRIGHT & LICENSE
238  
239 Copyright 2008-2010 by Cory G Watson.
240  
241 This program is free software; you can redistribute it and/or modify it
242 under the same terms as Perl itself.
« blib/lib/Graphics/Primitive/Driver.pm ↑ Summary blib/lib/Graphics/Primitive/ComponentList.pm »