| 1 | package Finance::GeniusTrader::Graphics::Driver::SVG; | ||
| 2 | |||
| 3 | # Copyright 2000-2004 Raphaƫl Hertzog, Fabien Fulhaber, Olivr Bossert | ||
| 4 | # This file is distributed under the terms of the General Public License | ||
| 5 | # version 2 or (at your option) any later version. | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use Finance::GeniusTrader::Graphics::Driver; | ||
| 9 | use Finance::GeniusTrader::Graphics::Zone; | ||
| 10 | use SVG; | ||
| 11 | |||
| 12 | our @ISA = qw(Finance::GeniusTrader::Graphics::Driver); | ||
| 13 | |||
| 14 | =head1 Finance::GeniusTrader::Graphics::Driver::SVG | ||
| 15 | |||
| 16 | =head2 Overview | ||
| 17 | |||
| 18 | This driver implements the drawing primitives using the SVG module available in CPAN to create Scalable Vector Graphics (SVG) files, | ||
| 19 | which is an exciting new XML-based language for Web graphics from the World Wide Web Consortium (W3C). | ||
| 20 | |||
| 21 | =head2 Links | ||
| 22 | |||
| 23 | http://www.adobe.com/svg/main.html | ||
| 24 | http://www.w3.org/TR/SVG/ | ||
| 25 | http://www.roasp.com/ | ||
| 26 | |||
| 27 | =cut | ||
| 28 | |||
| 29 | # PUBLIC DRIVER INTERFACE | ||
| 30 | sub new { | ||
| 31 | my $type = shift; | ||
| 32 | my $class = ref($type) || $type; | ||
| 33 | return bless {}, $class; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub create_picture { | ||
| 37 | my ($self, $zone) = @_; | ||
| 38 | my $i = SVG->new(width=>$zone->external_width(), | ||
| 39 | height=>$zone->external_height()); | ||
| 40 | my $p = { "img" => $i, | ||
| 41 | "zone" => $zone, | ||
| 42 | "linewidth" => 1, | ||
| 43 | 'stroke-opacity' => 1, | ||
| 44 | 'fill-opacity' => 1 | ||
| 45 | }; | ||
| 46 | return $p; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub line { | ||
| 50 | my ($self, $p, $x1, $y1, $x2, $y2, $color) = @_; | ||
| 51 | my ($xpt1, $ypt1) = _convert_coord($p, $x1, $y1); | ||
| 52 | my ($xpt2, $ypt2) = _convert_coord($p, $x2, $y2); | ||
| 53 | |||
| 54 | $p->{'img'}->line(x1=>$xpt1, y1=>$ypt1, x2=>$xpt2, y2=>$ypt2, | ||
| 55 | style=>{'stroke'=> _get_color($p, $color, 0), | ||
| 56 | 'stroke-width' => $p->{linewidth}, | ||
| 57 | 'stroke-opacity' => $p->{'stroke-opacity'}, | ||
| 58 | }); | ||
| 59 | } | ||
| 60 | |||
| 61 | sub dashed_line { | ||
| 62 | my ($self, $p, $x1, $y1, $x2, $y2, $color) = @_; | ||
| 63 | my ($xpt1, $ypt1) = _convert_coord($p, $x1, $y1); | ||
| 64 | my ($xpt2, $ypt2) = _convert_coord($p, $x2, $y2); | ||
| 65 | |||
| 66 | $p->{'img'}->line(x1=>$xpt1, y1=>$ypt1, x2=>$xpt2, y2=>$ypt2, | ||
| 67 | style=>{'stroke'=> _get_color($p, $color, 0), | ||
| 68 | 'stroke-dasharray'=>5, | ||
| 69 | 'stroke-width' => $p->{linewidth}, | ||
| 70 | 'stroke-opacity' => $p->{'stroke-opacity'}, | ||
| 71 | }); | ||
| 72 | } | ||
| 73 | |||
| 74 | sub rectangle { | ||
| 75 | my ($self, $p, $x1, $y1, $x2, $y2, $color) = @_; | ||
| 76 | my ($xpt1, $ypt1) = _convert_coord($p, $x1, $y2); | ||
| 77 | my ($xpt2, $ypt2) = _convert_coord($p, $x2, $y1); | ||
| 78 | |||
| 79 | $p->{'img'}->rectangle(x=>$xpt1, y=>$ypt1, width=>abs($xpt1 - $xpt2), | ||
| 80 | height=>abs($ypt1 - $ypt2), | ||
| 81 | style=>{'fill'=>'none', | ||
| 82 | 'stroke'=>_get_color($p, $color, 0), | ||
| 83 | 'stroke-width' => $p->{linewidth}, | ||
| 84 | 'stroke-opacity' => $p->{'stroke-opacity'}, | ||
| 85 | }); | ||
| 86 | } | ||
| 87 | |||
| 88 | sub filled_rectangle { | ||
| 89 | my ($self, $p, $x1, $y1, $x2, $y2, $color) = @_; | ||
| 90 | my ($xpt1, $ypt1) = _convert_coord($p, $x1, $y2); | ||
| 91 | my ($xpt2, $ypt2) = _convert_coord($p, $x2, $y1); | ||
| 92 | |||
| 93 | $p->{'img'}->rectangle(x=>$xpt1, y=>$ypt1, width=>abs($xpt1 - $xpt2), | ||
| 94 | height=>abs($ypt1 - $ypt2), | ||
| 95 | style=>{'fill'=>_get_color($p, $color, 1), | ||
| 96 | 'stroke'=>_get_color($p, $color, 0), | ||
| 97 | 'stroke-width' => $p->{linewidth}, | ||
| 98 | 'stroke-opacity' => $p->{'stroke-opacity'}, | ||
| 99 | 'fill-opacity' => $p->{'fill-opacity'} | ||
| 100 | }); | ||
| 101 | } | ||
| 102 | |||
| 103 | sub polygon { | ||
| 104 | my ($self, $p, $color, @points) = @_; | ||
| 105 | my ($xv, $yv); | ||
| 106 | |||
| 107 | if(scalar(@points)) { | ||
| 108 | for (my $i = 0; $i < scalar(@points); $i++) { | ||
| 109 | my ($x, $y) = _convert_coord($p, $points[$i][0], $points[$i][1]); | ||
| 110 | push(@{$xv}, $x); | ||
| 111 | push(@{$yv}, $y); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | my $points = $p->{'img'}->get_path(x=>$xv, y=>$yv, -type=>'polygon'); | ||
| 116 | $p->{'img'}->polygon(%$points, style=>{'stroke'=>_get_color($p, $color, 0), | ||
| 117 | 'fill'=>'none', | ||
| 118 | 'stroke-width' => $p->{linewidth}, | ||
| 119 | 'stroke-opacity' => $p->{'stroke-opacity'}, | ||
| 120 | }); | ||
| 121 | } | ||
| 122 | |||
| 123 | sub filled_polygon { | ||
| 124 | my ($self, $p, $color, @points) = @_; | ||
| 125 | my ($xv, $yv); | ||
| 126 | |||
| 127 | if(scalar(@points)) { | ||
| 128 | for (my $i = 0; $i < scalar(@points); $i++) { | ||
| 129 | my ($x, $y) = _convert_coord($p, $points[$i][0], $points[$i][1]); | ||
| 130 | push(@{$xv}, $x); | ||
| 131 | push(@{$yv}, $y); | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | my $points = $p->{'img'}->get_path(x=>$xv, y=>$yv, -type=>'polygon'); | ||
| 136 | $p->{'img'}->polygon(%$points, style=>{'stroke'=>_get_color($p, $color, 0), | ||
| 137 | 'fill'=>_get_color($p, $color, 1), | ||
| 138 | 'stroke-width' => $p->{linewidth}, | ||
| 139 | 'stroke-opacity' => $p->{'stroke-opacity'}, | ||
| 140 | 'fill-opacity' => $p->{'fill-opacity'} | ||
| 141 | }); | ||
| 142 | } | ||
| 143 | |||
| 144 | sub circle { | ||
| 145 | my ($self, $p, $x, $y, $width, $height, $color) = @_; | ||
| 146 | |||
| 147 | my ($cx, $cy) = _convert_coord($p, (($x + $width) / 2), | ||
| 148 | (($y + $height) / 2)); | ||
| 149 | my $rx = $width / 2; | ||
| 150 | my $ry = $height / 2; | ||
| 151 | |||
| 152 | $p->{'img'}->circle(id=>'circle', cx=>$cx, cy=>$cy, rx=>$rx, ry=>$ry, | ||
| 153 | style=>{'stroke'=>_get_color($p, $color,0), | ||
| 154 | 'fill'=>'none', | ||
| 155 | 'stroke-width' => $p->{linewidth}, | ||
| 156 | 'stroke-opacity' => $p->{'stroke-opacity'}, | ||
| 157 | }); | ||
| 158 | } | ||
| 159 | |||
| 160 | sub filled_circle { | ||
| 161 | my ($self, $p, $x, $y, $width, $height, $color) = @_; | ||
| 162 | |||
| 163 | my ($cx, $cy) = _convert_coord($p, (($x + $width) / 2), | ||
| 164 | (($y + $height) / 2)); | ||
| 165 | my $rx = $width / 2; | ||
| 166 | my $ry = $height / 2; | ||
| 167 | |||
| 168 | $p->{'img'}->circle(id=>'circle', cx=>$cx, cy=>$cy, rx=>$rx, ry=>$ry, | ||
| 169 | style=>{'fill'=>_get_color($p, $color, 1), | ||
| 170 | 'stroke'=>_get_color($p, $color, 0), | ||
| 171 | 'stroke-width' => $p->{linewidth}, | ||
| 172 | 'stroke-opacity' => $p->{'stroke-opacity'}, | ||
| 173 | 'fill-opacity' => $p->{'fill-opacity'} | ||
| 174 | }); | ||
| 175 | } | ||
| 176 | |||
| 177 | sub string { | ||
| 178 | my ($self, $p, $name, $size, $color, $x, $y, $text, $halign, | ||
| 179 | $valign, $orientation) = @_; | ||
| 180 | my ($offset_x, $offset_y) = (0, 0); | ||
| 181 | my ($nx, $ny) = _convert_coord($p, $x, $y); | ||
| 182 | |||
| 183 | $name = "Serif"; | ||
| 184 | $name = "Arial" if ($name =~ /arial/i ); | ||
| 185 | |||
| 186 | my $anchor = "middle"; | ||
| 187 | $anchor = "start" if ($halign eq $ALIGN_LEFT); | ||
| 188 | $anchor = "end" if ($halign eq $ALIGN_RIGHT); | ||
| 189 | |||
| 190 | my $baseshift = 0; | ||
| 191 | $baseshift = $size if ($valign eq $ALIGN_BOTTOM); | ||
| 192 | $baseshift = -$size if ($valign eq $ALIGN_TOP); | ||
| 193 | |||
| 194 | # Not yet implemented. Feel free to do it yourself ! :) | ||
| 195 | $p->{'img'}->text(x=>$nx, | ||
| 196 | y=>$ny, | ||
| 197 | style=>{ | ||
| 198 | 'font-size'=>$size, | ||
| 199 | 'font-family'=>$name, | ||
| 200 | 'stroke-width' => $p->{linewidth}, | ||
| 201 | 'stroke-opacity' => $p->{'stroke-opacity'}, | ||
| 202 | 'fill-opacity' => $p->{'fill-opacity'}, | ||
| 203 | 'text-anchor' => $anchor, | ||
| 204 | }, | ||
| 205 | 'fill'=>_get_color($p, $color, 1)) | ||
| 206 | ->tspan('baseline-shift' => $baseshift)->cdata($text); | ||
| 207 | } | ||
| 208 | |||
| 209 | # OUTPUT FUNCTIONS | ||
| 210 | sub save_to { | ||
| 211 | my ($self, $p, $filename) = @_; | ||
| 212 | open(FILE, "> $filename") || die "Can't write in $filename : $!\n"; | ||
| 213 | binmode FILE; | ||
| 214 | print FILE $p->{'img'}->xmlify; | ||
| 215 | close FILE; | ||
| 216 | } | ||
| 217 | |||
| 218 | sub dump { | ||
| 219 | my ($self, $p) = @_; | ||
| 220 | binmode STDOUT; | ||
| 221 | print $p->{'img'}->xmlify; | ||
| 222 | } | ||
| 223 | |||
| 224 | # PRIVATE FUNCTIONS | ||
| 225 | sub _get_color { | ||
| 226 | my ($p, $color, $fill) = @_; | ||
| 227 | if ( $#{$color} == 3 ) { | ||
| 228 | my ($red, $green, $blue, $alpha) = @{$color}; | ||
| 229 | if ( !defined($fill) || $fill == 1 ) { | ||
| 230 | $p->{'fill-opacity'} = $alpha / 255; | ||
| 231 | } else { | ||
| 232 | $p->{'stroke-opacity'} = $alpha / 255; | ||
| 233 | } | ||
| 234 | return 'rgb(' . $red . ',' . $green . ',' . $blue . ')'; | ||
| 235 | } | ||
| 236 | |||
| 237 | my ($red, $green, $blue) = @{$color}; | ||
| 238 | $p->{'stroke-opacity'} = 1; | ||
| 239 | $p->{'fill-opacity'} = 1; | ||
| 240 | return 'rgb(' . $red . ',' . $green . ',' . $blue . ')'; | ||
| 241 | } | ||
| 242 | |||
| 243 | sub _convert_coord { | ||
| 244 | my ($p, $x, $y) = @_; | ||
| 245 | return ($x, $p->{'zone'}->external_height() - $y - 1); | ||
| 246 | } | ||
| 247 | |||
| 248 | 1; |