Phylo/Treedrawer/Pdf.pm untested

filter: Statementstmt 0.0% 0 / 116 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 20 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 197 CC 19 SCAR 30.0
CC
19
Cov
0%
_draw_arc
56.0
_draw_triangle
20.0
_draw_text
20.0
CRAP 20.2
1 package Bio::Phylo::Treedrawer::Pdf;
2 use strict;
3 use base 'Bio::Phylo::Treedrawer::Abstract';
4 use Bio::Phylo::Util::Exceptions 'throw';
5 use Bio::Phylo::Util::CONSTANT qw'looks_like_hash _PI_';
6 use Bio::Phylo::Util::Dependency qw'PDF::API2::Lite PDF::API2::Annotation';
7 use Bio::Phylo::Util::Logger;
8 my $logger = Bio::Phylo::Util::Logger->new;
9 my $PI = _PI_;
10 my %colors;
11  
12 =head1 NAME
13  
14 Bio::Phylo::Treedrawer::Pdf - Graphics format writer used by treedrawer, no
15 serviceable parts inside
16  
17 =head1 DESCRIPTION
18  
19 This module creates a pdf file from a Bio::Phylo::Forest::DrawTree
20 object. It is called by the L<Bio::Phylo::Treedrawer> object, so look there to
21 learn how to create tree drawings.
22  
23  
24 =begin comment
25  
26 Type : Constructor
27 Title : _new
28 Usage : my $pdf = Bio::Phylo::Treedrawer::Pdf->_new(%args);
29 Function: Initializes a Bio::Phylo::Treedrawer::Pdf object.
30 Alias :
31 Returns : A Bio::Phylo::Treedrawer::Pdf object.
32 Args : none.
33  
34 =end comment
35  
36 =cut
37  
38 sub _new {
39 my $class = shift;
40 my %opt = looks_like_hash @_;
41 my $pdf = PDF::API2::Lite->new;
42 my $self = $class->SUPER::_new( %opt, '-api' => $pdf );
43 my $d = $self->_drawer;
44 my $page = $self->_api->page( $d->get_width, $d->get_height );
45 return bless $self, $class;
46 }
47  
48 =begin comment
49  
50 # finish drawing
51  
52 =end comment
53  
54 =cut
55  
56 sub _finish {
57 $logger->debug("finishing drawing");
58 my $self = shift;
59 require File::Temp;
60 my ( $fh, $filename ) = File::Temp::tempfile();
61 $self->_api->saveas($filename);
62 my $result = do { local $/; <$fh> };
63 unlink $filename;
64 return $result;
65 }
66  
67 =begin comment
68  
69 # -x1 => $x1,
70 # -x2 => $x2,
71 # -y1 => $y1,
72 # -y2 => $y2,
73 # -width => $width,
74 # -color => $color
75  
76 =end comment
77  
78 =cut
79  
80 sub _draw_curve {
81 $logger->debug("drawing curved branch");
82 my $self = shift;
83 my %args = @_;
84 my @keys = qw(-x1 -y1 -x2 -y2 -width -color);
85 my ( $x1, $y1, $x3, $y3, $linewidth, $color ) = @args{@keys};
86 my $height = $self->_drawer->get_height;
87 my ( $x2, $y2 ) = ( $x1, $y3 );
88 return $self->_api->linewidth( $linewidth || 1 )
89 ->strokecolor( $color ? "#$color" : "#000000" )
90 ->move( $x1, $height - $y1 )
91 ->curve( $x1, $height - $y1, $x2, $height - $y2, $x3, $height - $y3 )
92 ->stroke();
93 }
94  
95 =begin comment
96  
97 # -x1 => $x1,
98 # -x2 => $x2,
99 # -y1 => $y1,
100 # -y2 => $y2,
101 # -radius => $radius
102 # -width => $width,
103 # -color => $color
104  
105 =end comment
106  
107 =cut
108  
109 sub _draw_arc {
110 my $self = shift;
111 my $drawer = $self->_drawer;
112 my %args = @_;
113 my @keys = qw(-x1 -y1 -x2 -y2 -radius -width -color);
114 my ( $x1, $y1, $x2, $y2, $radius, $linewidth, $color ) = @args{@keys};
115
116 # calculate center
117 my $height = $drawer->get_height;
118 my $width = $drawer->get_width;
119 my $cx = $width / 2;
120 my $cy = $height / 2;
121
122 # compute start and end
123 my ( $r1, $start ) = $drawer->cartesian_to_polar( $x1 - $cx, $y1 - $cy );
124 my ( $r2, $end ) = $drawer->cartesian_to_polar( $x2 - $cx, $y2 - $cy );
125 $start += 360 if $start < 0;
126 $end += 360 if $end < 0;
127
128 # rotations in PDF are counter-clockwise, so need to be "inverted"
129 $start = ( $start - 360 ) * - 1;
130 $start -= 360 if $start > 360;
131
132 $end = ( $end - 360 ) * - 1;
133 $end -= 360 if $end > 360;
134
135 $logger->debug("drawing arc cx:$cx cy:$cy radius:$radius start:$start end:$end");
136
137 if ( $start != $end ) {
138 return $self->_api->linewidth( $linewidth || 1 )
139 ->strokecolor( $color ? "#$color" : "#000000" )
140 ->move( $x1, $cy + ( $cy - $y1 ) )
141 ->arc( $cx, $cy, $radius, $radius, $start, $end )
142 ->stroke();
143 }
144 }
145  
146 =begin comment
147  
148 # required:
149 # -x1 => $x1,
150 # -y1 => $y1,
151 # -x2 => $x2,
152 # -y2 => $y2,
153 # -x3 => $x3,
154 # -y3 => $y3,
155  
156 # optional:
157 # -fill => $fill,
158 # -stroke => $stroke,
159 # -width => $width,
160 # -url => $url,
161 # -api => $api,
162  
163 =end comment
164  
165 =cut
166  
167 sub _draw_triangle {
168 my $self = shift;
169 $logger->debug("drawing triangle @_");
170 my %args = @_;
171 my @keys = qw(-x1 -y1 -x2 -y2 -x3 -y3 -fill -stroke -width -url -api);
172 my ( $x1, $y1, $x2, $y2, $x3, $y3, $fill, $stroke, $width, $url, $api ) =
173 @args{@keys};
174 if ($url) {
175 $logger->warn( ref($self) . " can't embed links, yet" );
176 }
177 my $height = $self->_drawer->get_height;
178 my $pdf = $api || $self->_api;
179 return $pdf->move( $x1, $height - $y1 )->linewidth( $width || 1 )
180 ->strokecolor( $stroke ? "#$stroke" : "#000000" )
181 ->fillcolor( $fill ? "#$fill" : "white" )->poly(
182 $x1, $height - $y1, $x2, $height - $y2,
183 $x3, $height - $y3, $x1, $height - $y1,
184 )->fillstroke();
185 }
186  
187 =begin comment
188  
189 # -x1 => $x1,
190 # -x2 => $x2,
191 # -y1 => $y1,
192 # -y2 => $y2,
193 # -width => $width,
194 # -color => $color
195  
196 =end comment
197  
198 =cut
199  
200 sub _draw_line {
201 $logger->debug("drawing line");
202 my $self = shift;
203 my %args = @_;
204 my @keys = qw(-x1 -y1 -x2 -y2 -width -color);
205 my ( $x1, $y1, $x2, $y2, $width, $color ) = @args{@keys};
206 my $height = $self->_drawer->get_height;
207 return $self->_api->linewidth( $width || 1 )
208 ->strokecolor( $color ? "#$color" : "#000000" )
209 ->move( $x1, $height - $y1 )
210 ->poly( $x1, $height - $y1, $x2, $height - $y2 )->stroke();
211 }
212  
213 =begin comment
214  
215 # -x1 => $x1,
216 # -x2 => $x2,
217 # -y1 => $y1,
218 # -y2 => $y2,
219 # -width => $width,
220 # -color => $color
221  
222 =end comment
223  
224 =cut
225  
226 sub _draw_multi {
227 $logger->debug("drawing rectangular branch");
228 my $self = shift;
229 my %args = @_;
230 my @keys = qw(-x1 -y1 -x2 -y2 -width -color);
231 my ( $x1, $y1, $x3, $y3, $width, $color ) = @args{@keys};
232 my ( $x2, $y2 ) = ( $x1, $y3 );
233 my $height = $self->_drawer->get_height;
234 return $self->_api->linewidth( $width || 1 )
235 ->strokecolor( $color ? "#$color" : "#000000" )
236 ->move( $x1, $height - $y1 )
237 ->poly( $x1, $height - $y1, $x2, $height - $y2, $x3, $height - $y3 )
238 ->stroke();
239 }
240  
241 =begin comment
242  
243 # required:
244 # -x => $x,
245 # -y => $y,
246 # -text => $text,
247 #
248 # optional:
249 # -url => $url,
250  
251 =end comment
252  
253 =cut
254  
255 sub _draw_text {
256 my $self = shift;
257 if ( not $self->{'FONT'} ) {
258 $self->{'FONT'} = $self->_api->corefont('Times-Roman');
259 }
260 my %args = @_;
261 my ( $x, $y, $text, $url, $size, $rotation ) = @args{qw(-x -y -text -url -size -rotation)};
262 if ($url) {
263 $logger->warn( ref($self) . " can't embed links, yet" );
264 }
265 my $height = $self->_drawer->get_height;
266
267 # rotations in PDF are counter-clockwise, so need to be "inverted"
268 my $angle = ( $rotation->[0] - 360 ) * - 1;
269 $angle -= 360 if $angle > 360;;
270
271 $logger->debug("drawing text $text - $angle");
272
273 return $self->_api->fillcolor("#000000")
274 ->print( $self->{'FONT'}, $size || 12, $rotation->[1], $height - $rotation->[2], $angle, 0, $text, );
275 }
276  
277 =begin comment
278  
279 # -x => $x,
280 # -y => $y,
281 # -width => $width,
282 # -stroke => $color,
283 # -radius => $radius,
284 # -fill => $file,
285 # -api => $api,
286 # -url => $url,
287  
288 =end comment
289  
290 =cut
291  
292 sub _draw_circle {
293 $logger->debug("drawing circle");
294 my $self = shift;
295 my %args = @_;
296 my @keys = qw(-x -y -width -stroke -radius -fill -api -url);
297 my ( $x, $y, $width, $stroke, $radius, $fill, $api, $url ) = @args{@keys};
298 my $height = $self->_drawer->get_height;
299 my $pdf = $api || $self->_api;
300 my $circle =
301 $pdf->circle( $x, $height - $y, $radius )->linewidth( $width || 1 )
302 ->strokecolor( $stroke ? "#$stroke" : "#000000" )
303 ->fillcolor( $fill ? "#$fill" : "white" )->fillstroke();
304  
305 if ($url) {
306 $logger->warn( ref($self) . " can't embed links, yet" );
307  
308 #my $ann = PDF::API2::Annotation->new;
309 #$ann->url(
310 # $url,
311 # '-rect' => [
312 # $x - $radius,
313 # ( $height - $y ) - $radius,
314 # $x + $radius,
315 # ( $height - $y ) + $radius,
316 # ]
317 #);
318 }
319 return $circle;
320 }
321  
322 =head1 SEE ALSO
323  
324 There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
325 for any user or developer questions and discussions.
326  
327 =over
328  
329 =item L<Bio::Phylo::Treedrawer>
330  
331 The pdf treedrawer is called by the L<Bio::Phylo::Treedrawer> object. Look there
332 to learn how to create tree drawings.
333  
334 =item L<Bio::Phylo::Manual>
335  
336 Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>.
337  
338 =back
339  
340 =head1 CITATION
341  
342 If you use Bio::Phylo in published research, please cite it:
343  
344 B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
345 and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
346 I<BMC Bioinformatics> B<12>:63.
347 L<http://dx.doi.org/10.1186/1471-2105-12-63>
348  
349 =cut
350  
351 1;