blib/lib/Games/RolePlay/MapGen/Exporter/SVG/_GDSVG.pm untested

filter: Statementstmt 0.0% 0 / 100 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 173 CC 9 SCAR 19.2
CC
9
Cov
0%
_get_color
20.0
_build_style
12.0
colorAllocate
6.0
CRAP 6.8
1 # vi:filetype=perl:
2  
3 package Games::RolePlay::MapGen::Exporter::SVG::_GDSVG;
4  
5 # NOTE: Most of this code is ripped from GD::SVG v0.28,
6 # /usr/local/share/perl/5.8.8/GD/SVG.pm on my machine. ... why not just use
7 # GD::SVG directly? It does a lot I don't need and does a lot I need in a way
8 # that differs from what I needed.
9 #
10 # Why not submit patches and things? Well, I feel like a lot of the changes
11 # here are going for things that are really application specific.
12 #
13 # Why not subclass? GD::SVG seems like a work in progress, and I don't want to
14 # re-write this every time that changes. This is more sane imo.
15 #
16 # -Paul
17  
18 use common::sense;
19 use SVG;
20 use Carp;
21  
22 ### GD Emulation
23  
24 # new {{{
25 sub new {
26 my ($class, $width, $height, $debug) = @_;
27  
28 my $this = bless {}, $class;
29 my $img = SVG->new(width=>$width, height=>$height);
30  
31 $this->{img} = $img;
32 $this->{width} = $width;
33 $this->{height} = $height;
34  
35 $this->{foreground} = $this->colorAllocate(0, 0, 0);
36  
37 return $this;
38 }
39 # }}}
40 # svg {{{
41 sub svg {
42 my $this = shift;
43  
44 $this->{img}->xmlify(
45 -standalone => 'no',
46 -sysid => "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd",
47 -pubid => "-//W3C//DTD SVG 1.1//EN" );
48 }
49 # }}}
50 # colorAllocate {{{
51 sub colorAllocate {
52 my ($this, $r, $g, $b) = @_;
53 $r ||= 0;
54 $g ||= 0;
55 $b ||= 0;
56  
57 my $new_index = (defined $this->{colors_added}) ? scalar @{$this->{colors_added}} : 0;
58 $this->{colors}->{$new_index} = [$r, $g, $b];
59  
60 push (@{$this->{colors_added}}, $new_index);
61 return $new_index;
62 }
63 # }}}
64 # line {{{
65 sub line {
66 my ($this, $x1, $y1, $x2, $y2, $color_index) = @_;
67  
68 my ($img, $id) = $this->_prep($x1, $y1);
69 my $style = $this->_build_style($id, $color_index, $color_index);
70 $style->{'stroke-linecap'} = 'square';
71 $style->{'shape-rendering'} = 'crispEdges';
72  
73 my $result = $img->line(
74 x1 => $x1, y1=>$y1,
75 x2 => $x2, y2=>$y2,
76 id => $id,
77 _flatten_hash($style),
78 );
79  
80 return $result;
81 }
82 # }}}
83 # rectangle {{{
84 sub rectangle {
85 my ($this, $x1, $y1, $x2, $y2, $color_index, $fill) = @_;
86  
87 my ($img, $id) = $this->_prep($x1, $y1);
88 my $style = $this->_build_style($id, $color_index, $fill);
89 $style->{'shape-rendering'} = 'crispEdges';
90  
91 $img->rectangle( x => $x1, y => $y1, width => $x2-$x1, height => $y2-$y1, id => $id, style => $style );
92 }
93 # }}}
94 # filledRectangle {{{
95 sub filledRectangle {
96 my ($this, $x1, $y1, $x2, $y2, $color) = @_;
97  
98 $this->rectangle($x1, $y1, $x2, $y2, $color, $color);
99 }
100 # }}}
101 # arc {{{
102 sub arc {
103 my ($this, $cx, $cy, $width, $height, $start, $end, $color_index, $fill) = @_;
104  
105 return $this->ellipse($cx, $cy, $width, $height, $color_index, $fill)
106 if ($start == 0 and $end == 360) or ($end == 360 and $start == 0);
107  
108 my ($img, $id) = $this->_prep($cy, $cx);
109 my ($nstart, $nend, $large, $sweep, $a, $b) = _calculate_arc_params($start, $end, $width, $height);
110 my ($startx, $starty) = _calculate_point_coords($cx, $cy, $width, $height, $nstart);
111 my ($endx, $endy) = _calculate_point_coords($cx, $cy, $width, $height, $nend);
112  
113 my $style = $this->_build_style($id, $color_index, $fill);
114  
115 return $img->path( d => "M$startx, $starty A$a, $b 0 $large, $sweep $endx, $endy", style => $style );
116 }
117 # }}}
118 # ellipse {{{
119 sub ellipse {
120 my ($this, $x1, $y1, $width, $height, $color_index, $fill) = @_;
121 my ($img, $id) = $this->_prep($x1, $y1);
122 my $style = $this->_build_style($id, $color_index, $fill);
123  
124 # GD uses diameters, SVG uses radii
125 $width = $width / 2;
126 $height = $height / 2;
127  
128 return $img->ellipse( cx => $x1, cy => $y1, rx => $width, ry => $height, id => $id, style => $style, );
129 }
130 # }}}
131  
132 ### Internal functions
133  
134 # _prep {{{
135 sub _prep {
136 my ($this, @params) = @_;
137 my $img = $this->{img};
138 my $id = $this->_create_id;
139  
140 return ($img, $id);
141 }
142 # }}}
143 # _build_style {{{
144 sub _build_style {
145 my ($this, $id, $color, $fill, $stroke_opacity) = @_;
146 my $fill_opacity = ($fill) ? '1.0' : 0;
147  
148 $fill = defined $fill ? $this->_get_color($fill) : 'none';
149 return {
150 stroke => $this->_get_color($color),
151 opacity => 1,
152 fill => $fill,
153 };
154 }
155 # }}}
156 # _create_id {{{
157 sub _create_id {
158 my $this = shift;
159 my $f = (caller(2))[3];
160 $f = (split "::", $f)[-1];
161  
162 $f . (++ $this->{id}{$f});
163 }
164 # }}}
165 # _get_color {{{
166 sub _get_color {
167 my ($this, $index) = @_;
168  
169 confess "somebody gave me a bum index!" unless length $index > 0;
170  
171 return ($index) if ($index =~ /rgb/); # Already allocated.
172 return ($index) if ($index eq 'none'); # Generate by callbacks using none for fill
173
174 my ($r, $g, $b) = @{$this->{colors}{$index}};
175 my $color = "rgb($r, $g, $b)";
176  
177 return $color;
178 }
179 # }}}
180  
181 # precalculated cyclic transcendentals {{{
182 # NOTE: This clever little hack is (unsurprisingly) directly from GD::SVG, he
183 # explains it as, "Kludge - use precalculated values of cos(theta) and
184 # sin(theta) so that I do no have to examine quadrants." It may be a kluge,
185 # but it's good thinking.
186  
187 my @cosT = (qw/1024 1023 1023 1022 1021 1020 1018 1016 1014 1011 1008
188 1005 1001 997 993 989 984 979 973 968 962 955 949 942 935 928 920 912
189 904 895 886 877 868 858 848 838 828 817 806 795 784 772 760 748 736
190 724 711 698 685 671 658 644 630 616 601 587 572 557 542 527 512 496
191 480 464 448 432 416 400 383 366 350 333 316 299 282 265 247 230 212
192 195 177 160 142 124 107 89 71 53 35 17 0 -17 -35 -53 -71 -89 -107 -124
193 -142 -160 -177 -195 -212 -230 -247 -265 -282 -299 -316 -333 -350 -366
194 -383 -400 -416 -432 -448 -464 -480 -496 -512 -527 -542 -557 -572 -587
195 -601 -616 -630 -644 -658 -671 -685 -698 -711 -724 -736 -748 -760 -772
196 -784 -795 -806 -817 -828 -838 -848 -858 -868 -877 -886 -895 -904 -912
197 -920 -928 -935 -942 -949 -955 -962 -968 -973 -979 -984 -989 -993 -997
198 -1001 -1005 -1008 -1011 -1014 -1016 -1018 -1020 -1021 -1022 -1023
199 -1023 -1024 -1023 -1023 -1022 -1021 -1020 -1018 -1016 -1014 -1011
200 -1008 -1005 -1001 -997 -993 -989 -984 -979 -973 -968 -962 -955 -949
201 -942 -935 -928 -920 -912 -904 -895 -886 -877 -868 -858 -848 -838 -828
202 -817 -806 -795 -784 -772 -760 -748 -736 -724 -711 -698 -685 -671 -658
203 -644 -630 -616 -601 -587 -572 -557 -542 -527 -512 -496 -480 -464 -448
204 -432 -416 -400 -383 -366 -350 -333 -316 -299 -282 -265 -247 -230 -212
205 -195 -177 -160 -142 -124 -107 -89 -71 -53 -35 -17 0 17 35 53 71 89 107
206 124 142 160 177 195 212 230 247 265 282 299 316 333 350 366 383 400
207 416 432 448 464 480 496 512 527 542 557 572 587 601 616 630 644 658
208 671 685 698 711 724 736 748 760 772 784 795 806 817 828 838 848 858
209 868 877 886 895 904 912 920 928 935 942 949 955 962 968 973 979 984
210 989 993 997 1001 1005 1008 1011 1014 1016 1018 1020 1021 1022 1023
211 1023/);
212  
213 my @sinT = (qw/0 17 35 53 71 89 107 124 142 160 177 195 212 230 247
214 265 282 299 316 333 350 366 383 400 416 432 448 464 480 496 512 527
215 542 557 572 587 601 616 630 644 658 671 685 698 711 724 736 748 760
216 772 784 795 806 817 828 838 848 858 868 877 886 895 904 912 920 928
217 935 942 949 955 962 968 973 979 984 989 993 997 1001 1005 1008 1011
218 1014 1016 1018 1020 1021 1022 1023 1023 1024 1023 1023 1022 1021 1020
219 1018 1016 1014 1011 1008 1005 1001 997 993 989 984 979 973 968 962 955
220 949 942 935 928 920 912 904 895 886 877 868 858 848 838 828 817 806
221 795 784 772 760 748 736 724 711 698 685 671 658 644 630 616 601 587
222 572 557 542 527 512 496 480 464 448 432 416 400 383 366 350 333 316
223 299 282 265 247 230 212 195 177 160 142 124 107 89 71 53 35 17 0 -17
224 -35 -53 -71 -89 -107 -124 -142 -160 -177 -195 -212 -230 -247 -265 -282
225 -299 -316 -333 -350 -366 -383 -400 -416 -432 -448 -464 -480 -496 -512
226 -527 -542 -557 -572 -587 -601 -616 -630 -644 -658 -671 -685 -698 -711
227 -724 -736 -748 -760 -772 -784 -795 -806 -817 -828 -838 -848 -858 -868
228 -877 -886 -895 -904 -912 -920 -928 -935 -942 -949 -955 -962 -968 -973
229 -979 -984 -989 -993 -997 -1001 -1005 -1008 -1011 -1014 -1016 -1018
230 -1020 -1021 -1022 -1023 -1023 -1024 -1023 -1023 -1022 -1021 -1020
231 -1018 -1016 -1014 -1011 -1008 -1005 -1001 -997 -993 -989 -984 -979
232 -973 -968 -962 -955 -949 -942 -935 -928 -920 -912 -904 -895 -886 -877
233 -868 -858 -848 -838 -828 -817 -806 -795 -784 -772 -760 -748 -736 -724
234 -711 -698 -685 -671 -658 -644 -630 -616 -601 -587 -572 -557 -542 -527
235 -512 -496 -480 -464 -448 -432 -416 -400 -383 -366 -350 -333 -316 -299
236 -282 -265 -247 -230 -212 -195 -177 -160 -142 -124 -107 -89 -71 -53 -35
237 -17 /);
238 # }}}
239 # _calculate_arc_params {{{
240 sub _calculate_arc_params {
241 my ($start, $end, $width, $height) = @_;
242  
243 # GD uses diameters, SVG uses radii
244 my $a = $width / 2;
245 my $b = $height / 2;
246
247 while ($start < 0 ) { $start += 360; }
248 while ($end < 0 ) { $end += 360; }
249 while ($end < $start ) { $end += 360; }
250  
251 my $large = (abs $start - $end > 180) ? 1 : 0;
252 my $sweep = 1; # Always CW with GD
253  
254 return ($start, $end, $large, $sweep, $a, $b);
255 }
256 # }}}
257 # _calculate_point_coords {{{
258 sub _calculate_point_coords {
259 my ($cx, $cy, $width, $height, $angle) = @_;
260  
261 my $x = ( $cosT[$angle % 360] * $width) / (2 * 1024) + $cx;
262 my $y = ( $sinT[$angle % 360] * $height) / (2 * 1024) + $cy;
263  
264 return ($x, $y);
265 }
266 # }}}
267  
268 # _flatten_hash {{{
269 sub _flatten_hash {
270 my $hash = shift;
271  
272 map {($_=>$hash->{$_})} keys %$hash;
273 }
274 # }}}
275  
276 "true";