blib/lib/Graphics/Primitive/Driver.pm untested

filter: Statementstmt 0.0% 0 / 45 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 92 SCAR 56.1
CC
16
Cov
0%
draw
110.0
prepare
30.0
finalize
12.0
CRAP 272.0
1 package Graphics::Primitive::Driver;
2 use Moose::Role;
3  
4 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 has 'height' => (
12 is => 'rw',
13 isa => 'Num'
14 );
15 has 'width' => (
16 is => 'rw',
17 isa => 'Num'
18 );
19  
20 sub draw {
21 my ($self, $comp) = @_;
22  
23 if($comp->page) {
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 if($self->{FIRST_PAGE}) {
28 $self->_finish_page;
29 } else {
30 $self->{FIRST_PAGE} = 1;
31 }
32 $self->_resize($comp->width, $comp->height);
33 }
34  
35 die('Components must be objects.') unless ref($comp);
36 # The order of this is important, since isa will return true for any
37 # superclass...
38 # TODO Check::ISA
39 if($comp->isa('Graphics::Primitive::Canvas')) {
40 $self->_draw_canvas($comp);
41 } elsif($comp->isa('Graphics::Primitive::Image')) {
42 $self->_draw_image($comp);
43 } elsif($comp->isa('Graphics::Primitive::TextBox')) {
44 $self->_draw_textbox($comp);
45 } elsif($comp->isa('Graphics::Primitive::Component')) {
46 $self->_draw_component($comp);
47 }
48  
49 if($comp->isa('Graphics::Primitive::Container')) {
50 if($comp->can('components')) {
51 foreach my $subcomp (@{ $comp->components }) {
52 $self->draw($subcomp);
53 }
54 }
55 }
56 }
57  
58 sub finalize {
59 my ($self, $comp) = @_;
60  
61 $comp->finalize($self);
62  
63 if($comp->isa('Graphics::Primitive::Container')) {
64 foreach my $c (@{ $comp->components }) {
65 next unless defined($c) && defined($c)
66 && $c->visible;
67 $self->finalize($c);
68 }
69 }
70 }
71  
72 sub prepare {
73 my ($self, $comp) = @_;
74  
75 unless(defined($self->width)) {
76 $self->width($comp->width);
77 }
78 unless(defined($self->height)) {
79 $self->height($comp->height);
80 }
81  
82 $comp->prepare($self);
83  
84 # TODO Check::ISA
85 if($comp->isa('Graphics::Primitive::Container')) {
86 foreach my $c (@{ $comp->components }) {
87 next unless defined($c) && $c->visible;
88 $self->prepare($c);
89 }
90 }
91 }
92  
93 no Moose;
94 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.