Phylo/Treedrawer/Pdf.pm untested

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