Phylo/Treedrawer/Swf.pm untested

filter: Statementstmt 0.0% 0 / 99 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 30 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 162 CC 5 SCAR 16.1
CC
5
Cov
0%
_draw_arc
12.0
_draw_text
12.0
_new
2.0
CRAP 5.0
1 package Bio::Phylo::Treedrawer::Swf;
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 'SWF::Builder';
7 use Bio::Phylo::Util::Logger;
8  
9 our $FONT = __PACKAGE__->_font_path;
10 my $logger = Bio::Phylo::Util::Logger->new;
11 my $PI = _PI_;
12 my %colors;
13  
14 =head1 NAME
15  
16 Bio::Phylo::Treedrawer::Swf - Graphics format writer used by treedrawer, no
17 serviceable parts inside
18  
19 =head1 DESCRIPTION
20  
21 This module creates a flash movie from a Bio::Phylo::Forest::DrawTree
22 object. It is called by the L<Bio::Phylo::Treedrawer> object, so look there to
23 learn how to create tree drawings.
24  
25  
26 =begin comment
27  
28 Type : Constructor
29 Title : _new
30 Usage : my $swf = Bio::Phylo::Treedrawer::Swf->_new(%args);
31 Function: Initializes a Bio::Phylo::Treedrawer::Swf object.
32 Alias :
33 Returns : A Bio::Phylo::Treedrawer::Swf object.
34 Args : none.
35  
36 =end comment
37  
38 =cut
39  
40 sub _new {
41 my $class = shift;
42 my %opt = looks_like_hash @_;
43 my $self = $class->SUPER::_new(
44 %opt,
45 '-api' => SWF::Builder->new(
46 'FrameRate' => 15,
47 'FrameSize' =>
48 [ 0, 0, $opt{'-drawer'}->get_width, $opt{'-drawer'}->get_height ],
49 'BackgroundColor' => 'ffffff'
50 )
51 );
52 return bless $self, $class;
53 }
54  
55 sub _finish {
56 $logger->debug("finishing drawing");
57 my $self = shift;
58 require File::Temp;
59 my ( $fh, $filename ) = File::Temp::tempfile();
60 $self->_api->save($filename);
61 my $result = do { local $/; <$fh> };
62 unlink $filename;
63 return $result;
64 }
65  
66 # -x1 => $x1,
67 # -x2 => $x2,
68 # -y1 => $y1,
69 # -y2 => $y2,
70 # -width => $width,
71 # -color => $color
72 sub _draw_curve {
73 $logger->debug("drawing curved branch");
74 my $self = shift;
75 my %args = @_;
76 my @keys = qw(-x1 -y1 -x2 -y2 -width -color);
77 my ( $x1, $y1, $x3, $y3, $width, $color ) = @args{@keys};
78 my ( $x2, $y2 ) = ( $x1, $y3 );
79 return $self->_api->new_shape->linestyle( $width || 1, $color || '000000' )
80 ->moveto( $x1, $y1 )->curveto( $x1, $y1, $x1, $y1, $x2, $y2, $x3, $y3 )
81 ->place;
82 }
83  
84 =begin comment
85  
86 # -x1 => $x1,
87 # -x2 => $x2,
88 # -y1 => $y1,
89 # -y2 => $y2,
90 # -radius => $radius
91 # -width => $width,
92 # -color => $color
93  
94 =end comment
95  
96 =cut
97  
98 sub _draw_arc {
99 $logger->debug("darwing arc");
100 my $self = shift;
101 my %args = @_;
102 my @keys = qw(-x1 -y1 -x2 -y2 -radius -width -color);
103 my ($x1, $y1, $x2, $y2, $radius, $width, $color) = @args{@keys};
104
105 # get center of arc
106 my $drawer = $self->_drawer;
107 my $cx = $drawer->get_width / 2;
108 my $cy = $drawer->get_height / 2;
109
110 # compute start and end
111 my ( $r1, $start ) = $drawer->cartesian_to_polar( $x1 - $cx, $y1 - $cy );
112 my ( $r2, $end ) = $drawer->cartesian_to_polar( $x2 - $cx, $y2 - $cy );
113 $start += 360 if $start < 0;
114 $end += 360 if $end < 0;
115 $end -= $start;
116
117 my $shape = $self->_api->new_shape->linestyle( $width || 1, $color || '000000' );
118 $shape->moveto( $x1, $y1 );
119 $shape->arcto( $start, $end, $r1, $r2 );
120 $shape->place;
121 }
122  
123 # required:
124 # -x1 => $x1,
125 # -y1 => $y1,
126 # -x2 => $x2,
127 # -y2 => $y2,
128 # -x3 => $x3,
129 # -y3 => $y3,
130 # optional:
131 # -fill => $fill,
132 # -stroke => $stroke,
133 # -width => $width,
134 # -url => $url,
135 # -api => $api,
136 sub _draw_triangle {
137 my $self = shift;
138 $logger->debug("drawing triangle @_");
139 my %args = @_;
140 my @keys = qw(-x1 -y1 -x2 -y2 -x3 -y3 -fill -stroke -width -url -api);
141 my ( $x1, $y1, $x2, $y2, $x3, $y3, $fill, $stroke, $width, $url, $api ) =
142 @args{@keys};
143 return $self->_api->new_shape # red triangle.
144 ->fillstyle( $fill || 'ffffff' )
145 ->linestyle( $width || 1, $stroke || '000000' )
146 ->moveto( int $x1, int $y1 )->lineto( int $x2, int $y2 )
147 ->lineto( int $x3, int $y3 )->lineto( int $x1, int $y1 )->place;
148 }
149  
150 # -x1 => $x1,
151 # -x2 => $x2,
152 # -y1 => $y1,
153 # -y2 => $y2,
154 # -width => $width,
155 # -color => $color
156 sub _draw_line {
157 $logger->debug("drawing line");
158 my $self = shift;
159 my %args = @_;
160 my @keys = qw(-x1 -y1 -x2 -y2 -width -color);
161 my ( $x1, $y1, $x2, $y2, $width, $color ) = @args{@keys};
162 return $self->_api->new_shape->linestyle( $width || 1, $color || '000000' )
163 ->moveto( $x1, $y1 )->lineto( $x1, $y1, $x2, $y2 )->place;
164 }
165  
166 # -x1 => $x1,
167 # -x2 => $x2,
168 # -y1 => $y1,
169 # -y2 => $y2,
170 # -width => $width,
171 # -color => $color
172 sub _draw_multi {
173 $logger->debug("drawing rectangular branch");
174 my $self = shift;
175 my %args = @_;
176 my @keys = qw(-x1 -y1 -x2 -y2 -width -color);
177 my ( $x1, $y1, $x3, $y3, $width, $color ) = @args{@keys};
178 my ( $x2, $y2 ) = ( $x1, $y3 );
179 return $self->_api->new_shape->linestyle( $width || 1, $color || '000000' )
180 ->moveto( $x1, $y1 )->lineto( $x1, $y1, $x2, $y2, $x3, $y3 )->place;
181 }
182  
183 # required:
184 # -x => $x,
185 # -y => $y,
186 # -text => $text,
187 #
188 # optional:
189 # -url => $url,
190 sub _draw_text {
191 my $self = shift;
192 if ( not $self->{'FONT'} ) {
193 $self->{'FONT'} =
194 $self->_api->new_font($Bio::Phylo::Treedrawer::Swf::FONT);
195 }
196 my %args = @_;
197 my ( $x, $y, $text, $url, $size ) = @args{qw(-x -y -text -url -size)};
198 $logger->debug("drawing text $text");
199 if ($url) {
200 $text = sprintf( '<a href="%s">%s</a>', $url, $text );
201 }
202 my $textobj =
203 $self->_api->new_html_text->font( $self->{'FONT'} )->size( $size || 12 )
204 ->text($text);
205 return $textobj->place->moveto( $x, $y );
206 }
207  
208 # -x => $x,
209 # -y => $y,
210 # -width => $width,
211 # -stroke => $color,
212 # -radius => $radius,
213 # -fill => $file,
214 # -api => $api,
215 # -url => $url,
216 sub _draw_circle {
217 $logger->debug("drawing circle");
218 my $self = shift;
219 my %args = @_;
220 my @keys = qw(-x -y -width -stroke -radius -fill -api -url);
221 my ( $x, $y, $width, $stroke, $radius, $fill, $api, $url ) = @args{@keys};
222 my $circle =
223 $self->_api->new_shape->fillstyle( $fill || '000000' )
224 ->linestyle( $width || 1, $stroke || '000000' )->circle($radius);
225 return $circle->place->moveto( $x, $y );
226 }
227  
228 =head1 SEE ALSO
229  
230 There is a mailing list at L<https://groups.google.com/forum/#!forum/bio-phylo>
231 for any user or developer questions and discussions.
232  
233 =over
234  
235 =item L<Bio::Phylo::Treedrawer>
236  
237 The SWF treedrawer is called by the L<Bio::Phylo::Treedrawer> object. Look there
238 to learn how to create tree drawings.
239  
240 =item L<Bio::Phylo::Manual>
241  
242 Also see the manual: L<Bio::Phylo::Manual> and L<http://rutgervos.blogspot.com>.
243  
244 =back
245  
246 =head1 CITATION
247  
248 If you use Bio::Phylo in published research, please cite it:
249  
250 B<Rutger A Vos>, B<Jason Caravas>, B<Klaas Hartmann>, B<Mark A Jensen>
251 and B<Chase Miller>, 2011. Bio::Phylo - phyloinformatic analysis using Perl.
252 I<BMC Bioinformatics> B<12>:63.
253 L<http://dx.doi.org/10.1186/1471-2105-12-63>
254  
255 =cut
256  
257 1;