| 1 | package Finance::GeniusTrader::Graphics::Driver::GD; | ||
| 2 | |||
| 3 | # Copyright 2000-2002 Raphaƫl Hertzog, Fabien Fulhaber | ||
| 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 GD; | ||
| 10 | use Finance::GeniusTrader::Graphics::Zone; | ||
| 11 | |||
| 12 | our @ISA = qw(Finance::GeniusTrader::Graphics::Driver); | ||
| 13 | |||
| 14 | =head1 Finance::GeniusTrader::Graphics::Driver::GD | ||
| 15 | |||
| 16 | The GD driver implements the drawing primitives using the GD module | ||
| 17 | that lets you create PNG images. | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | # PUBLIC DRIVER INTERFACE | ||
| 22 | sub new { | ||
| 23 | my $type = shift; | ||
| 24 | my $class = ref($type) || $type; | ||
| 25 | return bless {}, $class; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub create_picture { | ||
| 29 | my ($self, $zone) = @_; | ||
| 30 | # Use true colors | ||
| 31 | if ($GD::VERSION >= 2.0) { | ||
| 32 | GD::Image->trueColor(1); | ||
| 33 | } | ||
| 34 | my $i = new GD::Image($zone->external_width(), $zone->external_height()); | ||
| 35 | my $p = { "img" => $i, "zone" => $zone, colors => {} }; | ||
| 36 | return $p; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub line { | ||
| 40 | my ($self, $p, $x1, $y1, $x2, $y2, $color) = @_; | ||
| 41 | $p->{'img'}->line( _convert_coord($p, $x1, $y1), | ||
| 42 | _convert_coord($p, $x2, $y2), | ||
| 43 | _get_color($p, $color) ); | ||
| 44 | } | ||
| 45 | |||
| 46 | sub antialiased_line { | ||
| 47 | my ($self, $p, $x1, $y1, $x2, $y2, $color) = @_; | ||
| 48 | if ($GD::VERSION >= 2.0) { | ||
| 49 | $p->{'img'}->setAntiAliased(_get_color($p, $color)); | ||
| 50 | $p->{'img'}->line( _convert_coord($p, $x1, $y1), | ||
| 51 | _convert_coord($p, $x2, $y2), | ||
| 52 | gdAntiAliased ); | ||
| 53 | } else { | ||
| 54 | line(@_); | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | sub dashed_line { | ||
| 59 | my ($self, $p, $x1, $y1, $x2, $y2, $color) = @_; | ||
| 60 | $p->{'img'}->dashedLine( _convert_coord($p, $x1, $y1), | ||
| 61 | _convert_coord($p, $x2, $y2), | ||
| 62 | _get_color($p, $color) ); | ||
| 63 | } | ||
| 64 | |||
| 65 | sub line_width { | ||
| 66 | my ($self, $p, $width) = @_; | ||
| 67 | my $old = Finance::GeniusTrader::Graphics::Driver::line_width(@_); | ||
| 68 | if ($GD::VERSION >= 2.0) { | ||
| 69 | if (defined $width) { | ||
| 70 | $p->{'img'}->setThickness($width); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | return $old; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub rectangle { | ||
| 77 | my ($self, $p, $x1, $y1, $x2, $y2, $color) = @_; | ||
| 78 | $p->{'img'}->rectangle( _convert_coord($p, $x1, $y2), | ||
| 79 | _convert_coord($p, $x2, $y1), | ||
| 80 | _get_color($p, $color) ); | ||
| 81 | } | ||
| 82 | |||
| 83 | sub filled_rectangle { | ||
| 84 | my ($self, $p, $x1, $y1, $x2, $y2, $color) = @_; | ||
| 85 | $p->{'img'}->filledRectangle( _convert_coord($p, $x1, $y2), | ||
| 86 | _convert_coord($p, $x2, $y1), | ||
| 87 | _get_color($p, $color) ); | ||
| 88 | } | ||
| 89 | |||
| 90 | sub polygon { | ||
| 91 | my ($self, $p, $color, @points) = @_; | ||
| 92 | my $polygon = new GD::Polygon; | ||
| 93 | |||
| 94 | if(scalar(@points)) { | ||
| 95 | for (my $i = 0; $i < scalar(@points); $i++) { | ||
| 96 | $polygon->addPt(_convert_coord($p, $points[$i][0], $points[$i][1])); | ||
| 97 | } | ||
| 98 | $p->{'img'}->polygon($polygon, _get_color($p, $color)); | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | sub filled_polygon { | ||
| 103 | my ($self, $p, $color, @points) = @_; | ||
| 104 | my $polygon = new GD::Polygon; | ||
| 105 | |||
| 106 | if(scalar(@points)) { | ||
| 107 | for (my $i = 0; $i < scalar(@points); $i++) { | ||
| 108 | $polygon->addPt(_convert_coord($p, $points[$i][0], $points[$i][1])); | ||
| 109 | } | ||
| 110 | $p->{'img'}->filledPolygon($polygon, _get_color($p, $color)); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | sub circle { | ||
| 115 | my ($self, $p, $x, $y, $width, $height, $color) = @_; | ||
| 116 | $p->{'img'}->arc( _convert_coord($p, $x, $y), | ||
| 117 | $width, $height, | ||
| 118 | 0, 360, _get_color($p, $color)); | ||
| 119 | } | ||
| 120 | |||
| 121 | sub string { | ||
| 122 | my ($self, $p, $name, $size, $color, $x, $y, $text, $halign, | ||
| 123 | $valign, $orientation) = @_; | ||
| 124 | my ($offset_x, $offset_y) = (0, 0); | ||
| 125 | $halign = $ALIGN_CENTER if (! defined($halign)); | ||
| 126 | $valign = $ALIGN_BOTTOM if (! defined($valign)); | ||
| 127 | $orientation = $ORIENTATION_RIGHT if (! defined($orientation)); | ||
| 128 | my @b = GD::Image->stringTTF( _get_color($p, $color), $name, $size, | ||
| 129 | $orientation, 0, 0, $text); | ||
| 130 | die "Can't generate text : $@" if (! scalar(@b)); | ||
| 131 | if ($orientation eq $ORIENTATION_UP) { | ||
| 132 | ($halign eq $ALIGN_CENTER) && ($offset_x = int(($b[0] - $b[6]) / 2)); | ||
| 133 | ($halign eq $ALIGN_LEFT) && ($offset_x = $b[0] - $b[6]); | ||
| 134 | ($halign eq $ALIGN_RIGHT) && ($offset_x = 0); | ||
| 135 | ($valign eq $ALIGN_CENTER) && ($offset_y = int(($b[1] - $b[3]) /2)); | ||
| 136 | ($valign eq $ALIGN_TOP) && ($offset_y = $b[1] - $b[3]); | ||
| 137 | ($valign eq $ALIGN_BOTTOM) && ($offset_y = 0); | ||
| 138 | } elsif ($orientation eq $ORIENTATION_DOWN) { | ||
| 139 | ($halign eq $ALIGN_CENTER) && ($offset_x = int(($b[0] - $b[6]) / 2)); | ||
| 140 | ($halign eq $ALIGN_LEFT) && ($offset_x = 0); | ||
| 141 | ($halign eq $ALIGN_RIGHT) && ($offset_x = $b[0] - $b[6]); | ||
| 142 | ($valign eq $ALIGN_CENTER) && ($offset_y = int(($b[1] - $b[3]) / 2)); | ||
| 143 | ($valign eq $ALIGN_TOP) && ($offset_y = 0); | ||
| 144 | ($valign eq $ALIGN_BOTTOM) && ($offset_y = $b[1] - $b[3]); | ||
| 145 | } elsif ($orientation eq $ORIENTATION_LEFT) { | ||
| 146 | ($halign eq $ALIGN_CENTER) && ($offset_x = int(($b[0] - $b[2]) / 2)); | ||
| 147 | ($halign eq $ALIGN_LEFT) && ($offset_x = $b[0] - $b[2]); | ||
| 148 | ($halign eq $ALIGN_RIGHT) && ($offset_x = 0); | ||
| 149 | ($valign eq $ALIGN_CENTER) && ($offset_y = int(($b[1] - $b[7]) / 2)); | ||
| 150 | ($valign eq $ALIGN_TOP) && ($offset_y = 0); | ||
| 151 | ($valign eq $ALIGN_BOTTOM) && ($offset_y = $b[1] - $b[7]); | ||
| 152 | } elsif ($orientation eq $ORIENTATION_RIGHT) { | ||
| 153 | ($halign eq $ALIGN_CENTER) && ($offset_x = int(($b[0] - $b[2]) / 2)); | ||
| 154 | ($halign eq $ALIGN_LEFT) && ($offset_x = 0); | ||
| 155 | ($halign eq $ALIGN_RIGHT) && ($offset_x = $b[0] - $b[2]); | ||
| 156 | ($valign eq $ALIGN_CENTER) && ($offset_y = int(($b[1] - $b[7]) / 2)); | ||
| 157 | ($valign eq $ALIGN_TOP) && ($offset_y = $b[1] - $b[7]); | ||
| 158 | ($valign eq $ALIGN_BOTTOM) && ($offset_y = 0); | ||
| 159 | } | ||
| 160 | my ($nx, $ny) = _convert_coord($p, $x, $y); | ||
| 161 | $p->{'img'}->stringFT( _get_color($p, $color), $name, $size, | ||
| 162 | $orientation, $nx + $offset_x, $ny + $offset_y, | ||
| 163 | $text); | ||
| 164 | } | ||
| 165 | |||
| 166 | # OUTPUT FUNCTIONS | ||
| 167 | sub save_to { | ||
| 168 | my ($self, $p, $filename) = @_; | ||
| 169 | open(FILE, "> $filename") || die "Can't write in $filename : $!\n"; | ||
| 170 | binmode FILE; | ||
| 171 | print FILE $p->{'img'}->png; | ||
| 172 | close FILE; | ||
| 173 | } | ||
| 174 | |||
| 175 | sub dump { | ||
| 176 | my ($self, $p) = @_; | ||
| 177 | binmode STDOUT; | ||
| 178 | print $p->{'img'}->png; | ||
| 179 | } | ||
| 180 | |||
| 181 | # PRIVATE FUNCTIONS | ||
| 182 | sub _get_color { | ||
| 183 | my ($p, $color) = @_; | ||
| 184 | if ($GD::VERSION >= 2.0) { | ||
| 185 | my $color_name = "@{$color}"; | ||
| 186 | if (! exists $p->{'colors'}{$color_name}) { | ||
| 187 | if (scalar @{$color} > 3) { | ||
| 188 | $p->{'colors'}{$color_name} = $p->{'img'}->colorAllocateAlpha(@{$color}); | ||
| 189 | } else { | ||
| 190 | $p->{'colors'}{$color_name} = $p->{'img'}->colorResolve(@{$color}); | ||
| 191 | } | ||
| 192 | } | ||
| 193 | return $p->{'colors'}{$color_name}; | ||
| 194 | } else { | ||
| 195 | return $p->{'img'}->colorResolve(@{$color}[0..2]); | ||
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | sub _convert_coord { | ||
| 200 | my ($p, $x, $y) = @_; | ||
| 201 | return ($x, $p->{'zone'}->external_height() - $y - 1); | ||
| 202 | } | ||
| 203 | |||
| 204 | 1; |