blib/lib/SVG/ChristmasTree.pm

filter: Statementstmt 100% 119 / 119 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 20 / 20 Podpod 40.0% 2 / 5 totaltotal 97.9% 141 / 144 CC 5 SCAR 2.9
CC
5
Cov
100%
_build_height
2.0
_build_triangle_heights
2.0
_build_triangles
2.0
CRAP 1.3
↑ Summary
1 =head1 NAME
2  
3 SVG::ChristmasTree - Perl extension to draw Christmas trees with SVG
4  
5 =head1 DESCRIPTION
6  
7 Perl extension to draw Christmas trees with SVG
8  
9 =head1 SYNOPSIS
10  
11 # Default tree
12 my $tree = SVG::ChristmasTree->new;
13 print $tree->as_xml;
14  
15 # Or change things
16 my $tree = SVG::ChristmasTree->new({
17 layers => 5,
18 leaf_colour => 'rgb(0,255,0)',
19 pot_colour => 'rgb(0,0,255)',
20 star_color => 'rgb(255,0,0)',
21 });
22 print $tree->as_xml;
23  
24 =cut
25  
26 package SVG::ChristmasTree;
27  
28 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use SVG;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use Math::Trig qw[deg2rad tan];
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 1 with 'MooseX::Getopt';
37  
38 1 our $VERSION = '0.0.7';
39  
40 # Constants that we haven't made into attributes yet
41 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 TREE_WIDTH => 600, # Width of the bottom tree layer
43 TOP_ANGLE => 90, # Angle at the top of the tree triangles
44 LAYER_SIZE_RATIO => (5/6), # How much smaller each layer gets
45 LAYER_STACKING => 0.5, # How far up a layer triangle does the next one start
46 POT_TOP_WIDTH => 300, # Width of the top of the pot
47 POT_BOT_WIDTH => 200, # Width of the bottom of the pot
48 TRUNK_WIDTH => 100, # Width of the trunk
49 BAUBLE_RADIUS => 20, # Radius of a bauble
50 1 };
51  
52 =head1 Methods
53  
54 =head2 $tree = SVG::ChristmasTree->new(\%args)
55  
56 Constructs and returns a new SVG::ChristmasTree object. With no arguments,
57 a default tree design is created, but it is possible to change that by
58 passing the following attributes to the method.
59  
60 =over 4
61  
62 =item width INT
63  
64 The width of the tree diagram in "pixels". The default is 1,000.
65  
66 =item layers INT
67  
68 The number of layers in the tree. The default tree has four layers.
69  
70 =item trunk_length INT
71  
72 The length of the trunk in "pixels". The default length is 100.
73  
74 =item leaf_colour STR
75  
76 The colour of the tree's leaves. This must be defined as an SVG RGB value.
77 The default value is "rgb(0,127,0)".
78  
79 =item bauble_colour STR
80  
81 The colour of the baubles that hang on the tree. This must be defined as an
82 SVG RGB value. The default value is "rgb(212,175,55)".
83  
84 =item trunk colour STR
85  
86 The colour of the tree trunk. This must be defined as an SVG RGB value. The
87 default value is "rgb(139,69,19)".
88  
89 =item pot_colour STR
90  
91 The colour of the pot. This must be defined as an SVG RGB value. The default
92 value is "rgb(191,0,0)".
93  
94 =item pot_height INT
95  
96 The height of the pot in "pixels". The default height is 200.
97  
98 =item star_colour STR
99  
100 The colour of the star. This must be defined as an SVG RGB value. The default
101 value is "rgb(212,175,55)".
102  
103 =item star_size INT
104  
105 The size of the star in "pixels". The start will be in a square of the defined
106 size.The default size is 80.
107  
108 =back
109  
110 =head2 $tree->as_xml
111  
112 Returns the SVG document as XML. You will usually want to store the returned
113 value in a variable, print it to C<STDOUT> or write it to a file.
114  
115 =cut
116  
117 1 has width => (
118 isa => 'Int',
119 is => 'ro',
120 default => 1_000,
121 );
122  
123 1 has height => (
124 isa => 'Int',
125 is => 'ro',
126 lazy_build => 1,
127 init_arg => undef,
128 );
129  
130 # Height is calculated from all the other stuff
131 1 ▶sub _build_height {
Subroutine _build_height: called CC=2 SCAR=6.9
132 1 my $self = shift;
133  
134 # Pot height ...
135 1 my $height = $self->pot_height;
136 # ... plus the trunk length ...
137 1 $height += $self->trunk_length;
138 # ... for most of the layers ...
139 1 for (0 .. $self->layers - 2) {
140 # ... add LAYER_STACKING of the height ...
141 3 $height += $self->triangle_heights->[$_] * LAYER_STACKING;
142 }
143 # ... add all of the last layer ...
144 1 $height += $self->triangle_heights->[-1];
145 # ... and (finally) half of the star
146 1 $height += $self->star_size / 2;
147  
148 1 return int($height + 0.5);
149 }
150  
151 1 has triangle_heights => (
152 isa => 'ArrayRef',
153 is => 'ro',
154 lazy_build => 1,
155 init_arg => undef,
156 );
157  
158 1 ▶sub _build_triangle_heights {
Subroutine _build_triangle_heights: called CC=2 SCAR=6.9
159 1 my $self = shift;
160  
161 1 my @heights;
162 1 my $width = TREE_WIDTH;
163 1 for (1 .. $self->layers) {
164 4 push @heights, $self->_triangle_height($width, TOP_ANGLE);
165 4 $width *= LAYER_SIZE_RATIO;
166 }
167  
168 1 return \@heights;
169 }
170  
171 4 ▶sub _triangle_height {
Subroutine _triangle_height: called CC=1 SCAR=0.0
172 4 my $self = shift;
173 4 my ($base, $top_angle) = @_;
174  
175 # Assume $top_angle is in degrees
176 4 $top_angle = deg2rad($top_angle) / 2;
177 # If I remember my trig correctly...
178 4 return ($base / 2) / tan($top_angle);
179 }
180  
181 1 has svg => (
182 isa => 'SVG',
183 is => 'ro',
184 lazy_build => 1,
185 init_arg => undef,
186 );
187  
188 1 ▶sub _build_svg {
Subroutine _build_svg: called CC=1 SCAR=0.0
189 1 my $self = shift;
190  
191 1 return SVG->new(
192 width => $self->width,
193 height => $self->height,
194 );
195 }
196  
197 1 has layers => (
198 isa => 'Int',
199 is => 'ro',
200 default => 4,
201 );
202  
203 1 has trunk_length => (
204 isa => 'Int',
205 is => 'ro',
206 default => 100,
207 );
208  
209 1 has leaf_colour => (
210 isa => 'Str',
211 is => 'ro',
212 default => 'rgb(0,127,0)',
213 );
214  
215 1 has bauble_colour => (
216 isa => 'Str',
217 is => 'ro',
218 default => 'rgb(212,175,55)',
219 );
220  
221 1 has trunk_colour => (
222 isa => 'Str',
223 is => 'ro',
224 default => 'rgb(139,69,19)',
225 );
226  
227 1 has pot_colour => (
228 isa => 'Str',
229 is => 'ro',
230 default => 'rgb(191,0,0)',
231 );
232  
233 1 has star_colour => (
234 isa => 'Str',
235 is => 'ro',
236 default => 'rgb(212,175,55)',
237 );
238  
239 1 has pot_height => (
240 isa => 'Int',
241 is => 'ro',
242 default => 200,
243 );
244  
245 1 has star_size => (
246 isa => 'Int',
247 is => 'ro',
248 default => 80,
249 );
250  
251 1 has triangles => (
252 isa => 'ArrayRef',
253 is => 'ro',
254 lazy_build => 1,
255 init_arg => undef,
256 );
257  
258 1 ▶sub _build_triangles {
Subroutine _build_triangles: called CC=2 SCAR=6.9
259 1 my $self = shift;
260  
261 1 my $width = TREE_WIDTH;
262 1 my $tri_bottom = $self->height - $self->pot_height - $self->trunk_length;
263  
264 1 my @triangles;
265 1 for (1 .. $self->layers) {
266 4 push @triangles, $self->_triangle(TOP_ANGLE, $width, $tri_bottom);
267 4 $width *= LAYER_SIZE_RATIO;
268 4 $tri_bottom -= $triangles[-1]->{h} * LAYER_STACKING;
269 }
270  
271 1 return \@triangles;
272 }
273  
274 1 ▶sub as_xml {
Subroutine as_xml: called CC=2 SCAR=6.9
Pod documented
275 1 my $self = shift;
276  
277 1 $self->pot;
278 1 $self->trunk;
279  
280 1 for (@{$self->triangles}) {
281 4 my $h = $self->_triangle(TOP_ANGLE, $_->{w}, $_->{b});
282 4 $self->bauble($self->_mid_y - ($_->{w}/2), $_->{b});
283 4 $self->bauble($self->_mid_y + ($_->{w}/2), $_->{b});
284 $self->_coloured_shape(
285 4 $_->{x}, $_->{y}, $self->leaf_colour,
286 );
287 }
288  
289 1 $self->star;
290  
291 1 return $self->svg->xmlify;
292 }
293  
294 1 ▶sub pot {
Subroutine pot: called CC=1 SCAR=0.0
Pod undocumented
295 1 my $self = shift;
296  
297 1 my $pot_top = $self->height - $self->pot_height;
298  
299 1 $self->_coloured_shape(
300 [ $self->_mid_y - (POT_BOT_WIDTH / 2),
301 $self->_mid_y - (POT_TOP_WIDTH / 2),
302 $self->_mid_y + (POT_TOP_WIDTH / 2),
303 $self->_mid_y + (POT_BOT_WIDTH / 2) ],
304 [ $self->height, $pot_top, $pot_top, $self->height ],
305 $self->pot_colour,
306 );
307 }
308  
309 1 ▶sub trunk {
Subroutine trunk: called CC=1 SCAR=0.0
Pod documented
310 1 my $self = shift;
311  
312 1 my $trunk_bottom = $self->height - $self->pot_height;
313 1 my $trunk_top = $trunk_bottom - $self->trunk_length;
314  
315 1 $self->_coloured_shape(
316 [ $self->_mid_y - (TRUNK_WIDTH / 2), $self->_mid_y - (TRUNK_WIDTH / 2),
317 $self->_mid_y + (TRUNK_WIDTH / 2), $self->_mid_y + (TRUNK_WIDTH / 2) ],
318 [ $trunk_bottom, $trunk_top, $trunk_top, $trunk_bottom ],
319 $self->trunk_colour,
320 );
321 }
322  
323 8 ▶sub _triangle {
Subroutine _triangle: called CC=1 SCAR=0.0
324 8 my $self = shift;
325 8 my ($top_angle, $base, $bottom) = @_;
326  
327 8 my ($x, $y);
328  
329 # Assume $top_angle is in degrees
330 8 $top_angle = deg2rad($top_angle) / 2;
331 # If I remember my trig correctly...
332 8 my $height = ($base / 2) / tan($top_angle);
333  
334 8 $x = [ $self->_mid_y - ($base / 2), $self->_mid_y, $self->_mid_y + ($base / 2) ];
335 8 $y = [ $bottom, $bottom - $height, $bottom ];
336  
337 return {
338 8 x => $x, # array ref of x points
339 y => $y, # array ref of y points
340 h => $height, # height of the triangle
341 w => $base, # length of the base of the triangle
342 b => $bottom, # y-coord of the bottom of the triangle
343 };
344 }
345  
346 8 ▶sub bauble {
Subroutine bauble: called CC=1 SCAR=0.0
Pod undocumented
347 8 my $self = shift;
348 8 my ($x, $y) = @_;
349  
350 8 $self->svg->circle(
351 cx => $x,
352 cy => $y + BAUBLE_RADIUS,
353 r => BAUBLE_RADIUS,
354 style => {
355 fill => $self->bauble_colour,
356 stroke => $self->bauble_colour,
357 },
358 );
359 }
360  
361 1 ▶sub star {
Subroutine star: called CC=1 SCAR=0.0
Pod undocumented
362 1 my $self = shift;
363 1 my ($x, $y, $delta_x, $delta_y);
364  
365 1 $delta_x = $self->_mid_y;
366 1 $delta_y = 0;
367  
368 # coordinates for a polyline star centered at 0,0
369 1 $x = [ 0, 0.125, 0.5, 0.25, 0.375, 0, -0.375, -0.25, -0.5, -0.125, 0 ];
370 1 $y = [ 0, 0.375, 0.375, 0.625, 1, 0.75, 1, 0.625, 0.375, 0.375, 0 ];
371  
372 # multiple by size
373 1 $x = [map { $_ * $self->star_size } @$x];
374 1 $y = [map { $_ * $self->star_size } @$y];
375  
376 # move to the placement we want (centered, on top of tree)
377 1 $x = [map { $_ + $delta_x } @$x];
378 1 $y = [map { $_ + $delta_y } @$y];
379  
380 1 $self->_coloured_shape(
381 $x,
382 $y,
383 $self->star_colour,
384 );
385 }
386  
387 41 ▶sub _mid_y {
Subroutine _mid_y: called CC=1 SCAR=0.0
388 41 my $self = shift;
389  
390 41 return $self->width / 2;
391 }
392  
393 7 ▶sub _coloured_shape {
Subroutine _coloured_shape: called CC=1 SCAR=0.0
394 7 my $self = shift;
395 7 my ($x, $y, $colour) = @_;
396  
397 7 my $path = $self->svg->get_path(
398 x => $x,
399 y => $y,
400 -type => 'polyline',
401 -closed => 1,
402 );
403  
404 7 $self->svg->polyline(
405 %$path,
406 style => {
407 fill => $colour,
408 stroke => $colour,
409 },
410 );
411 }
412  
413 1 __PACKAGE__->meta()->make_immutable();
414  
415 =head1 AUTHOR
416  
417 Dave Cross <dave@perlhacks.com>
418  
419 =head1 COPYRIGHT AND LICENCE
420  
421 Copyright (c) 2018, Magnum Solutions Ltd. All Rights Reserved.
422  
423 This library is free software; you can redistribute it and/or modify it
424 under the same terms as Perl itself.
425  
426 =cut
427  
428 1 1;
↑ Summary