| 1 | package Finance::GeniusTrader::Graphics::Driver; | ||
| 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::Conf; | ||
| 9 | |||
| 10 | use vars qw(@EXPORT @ISA | ||
| 11 | |||
| 12 | $FONT_SIZE_TINY $FONT_SIZE_SMALL $FONT_SIZE_MEDIUM | ||
| 13 | $FONT_SIZE_LARGE $FONT_SIZE_GIANT | ||
| 14 | |||
| 15 | $FONT_ARIAL $FONT_TIMES $FONT_HELVETICA | ||
| 16 | $FONT_SANS_SERIF $FONT_FIXED $FONT_PROPORTIONNAL $FONT_SERIF | ||
| 17 | |||
| 18 | $ALIGN_LEFT $ALIGN_CENTER $ALIGN_RIGHT $ALIGN_TOP $ALIGN_BOTTOM | ||
| 19 | |||
| 20 | $ORIENTATION_UP $ORIENTATION_DOWN $ORIENTATION_LEFT | ||
| 21 | $ORIENTATION_RIGHT | ||
| 22 | |||
| 23 | $COLOR_WHITE $COLOR_BLACK $COLOR_RED $COLOR_GREEN $COLOR_BLUE | ||
| 24 | ); | ||
| 25 | |||
| 26 | require Exporter; | ||
| 27 | @ISA = qw(Exporter); | ||
| 28 | @EXPORT = qw( | ||
| 29 | $FONT_SIZE_TINY $FONT_SIZE_SMALL $FONT_SIZE_MEDIUM | ||
| 30 | $FONT_SIZE_LARGE $FONT_SIZE_GIANT | ||
| 31 | |||
| 32 | $FONT_ARIAL $FONT_TIMES $FONT_HELVETICA | ||
| 33 | $FONT_SANS_SERIF $FONT_FIXED $FONT_PROPORTIONNAL $FONT_SERIF | ||
| 34 | |||
| 35 | $ALIGN_LEFT $ALIGN_CENTER $ALIGN_RIGHT $ALIGN_TOP $ALIGN_BOTTOM | ||
| 36 | |||
| 37 | $ORIENTATION_UP $ORIENTATION_DOWN $ORIENTATION_LEFT | ||
| 38 | $ORIENTATION_RIGHT | ||
| 39 | |||
| 40 | $COLOR_WHITE $COLOR_BLACK $COLOR_RED $COLOR_GREEN $COLOR_BLUE | ||
| 41 | ); | ||
| 42 | |||
| 43 | Finance::GeniusTrader::Conf::load(); | ||
| 44 | |||
| 45 | $FONT_SIZE_TINY = 8; | ||
| 46 | $FONT_SIZE_SMALL = 12; | ||
| 47 | $FONT_SIZE_MEDIUM = 14; | ||
| 48 | $FONT_SIZE_LARGE = 18; | ||
| 49 | $FONT_SIZE_GIANT = 30; | ||
| 50 | |||
| 51 | $FONT_ARIAL = Finance::GeniusTrader::Conf::get("Path::Font::Arial") || die("Undefined font.\nPlease set the Path::Font::Arial value in the ~/.gt/options file.\n"); | ||
| 52 | $FONT_TIMES = Finance::GeniusTrader::Conf::get("Path::Font::Times") || $FONT_ARIAL; | ||
| 53 | $FONT_HELVETICA = Finance::GeniusTrader::Conf::get("Path::Font::Arial") || $FONT_ARIAL; | ||
| 54 | $FONT_SANS_SERIF = Finance::GeniusTrader::Conf::get("Path::Font::Arial") || $FONT_ARIAL; | ||
| 55 | $FONT_FIXED = Finance::GeniusTrader::Conf::get("Path::Font::Courier") || $FONT_ARIAL; | ||
| 56 | $FONT_PROPORTIONNAL = Finance::GeniusTrader::Conf::get("Path::Font::Times") || $FONT_ARIAL; | ||
| 57 | $FONT_SERIF = Finance::GeniusTrader::Conf::get("Path::Font::Times") || $FONT_ARIAL; | ||
| 58 | |||
| 59 | $ALIGN_LEFT = "left"; | ||
| 60 | $ALIGN_CENTER = "center"; | ||
| 61 | $ALIGN_RIGHT = "right"; | ||
| 62 | $ALIGN_TOP = "top"; | ||
| 63 | $ALIGN_BOTTOM = "bottom"; | ||
| 64 | |||
| 65 | $ORIENTATION_UP = 3.1415 / 2; | ||
| 66 | $ORIENTATION_DOWN = - 3.1415 / 2; | ||
| 67 | $ORIENTATION_RIGHT = 0; | ||
| 68 | $ORIENTATION_LEFT = - 3.1415; | ||
| 69 | |||
| 70 | $COLOR_WHITE = [ 255, 255, 255, 0 ]; | ||
| 71 | $COLOR_BLACK = [ 0, 0, 0, 0 ]; | ||
| 72 | $COLOR_RED = [ 255, 0, 0, 0 ]; | ||
| 73 | $COLOR_GREEN = [ 0, 255, 0, 0 ]; | ||
| 74 | $COLOR_BLUE = [ 0, 0, 255, 0 ]; | ||
| 75 | |||
| 76 | |||
| 77 | =head1 Finance::GeniusTrader::Graphics::Driver | ||
| 78 | |||
| 79 | A graphic driver is a well defined interface that let you actually | ||
| 80 | generate a picture by using drawing primitives. Those primitives | ||
| 81 | are used by "drawable" objects that implements the | ||
| 82 | display($driver, $picture) method. | ||
| 83 | |||
| 84 | =head1 Drawing API | ||
| 85 | |||
| 86 | =head2 $picture = $driver->create_picture($rootzone) | ||
| 87 | |||
| 88 | This does create the empty picture on which you'll draw various things. | ||
| 89 | The picture has the size corresponding to the given "zone". | ||
| 90 | |||
| 91 | =head2 $driver->line($picture, $x1, $y1, $x2, $y2, $color) | ||
| 92 | |||
| 93 | =head2 $driver->dashed_line($picture, $x1, $y1, $x2, $y2, $color) | ||
| 94 | |||
| 95 | =head2 $driver->antialiased_line($picture, $x1, $y1, $x2, $y2, $color) | ||
| 96 | |||
| 97 | =cut | ||
| 98 | sub antialiased_line { | ||
| 99 | my $self = shift; | ||
| 100 | $self->line(@_); | ||
| 101 | } | ||
| 102 | |||
| 103 | =head2 $driver->rectangle($picture, $x1, $y1, $x2, $y2, $color) | ||
| 104 | |||
| 105 | =head2 $driver->filled_rectangle($picture, $x1, $y1, $x2, $y2, $color) | ||
| 106 | |||
| 107 | =head2 $driver->polygon($picture, $color, @points) | ||
| 108 | |||
| 109 | =head2 $driver->filled_polygon($picture, $color, @points) | ||
| 110 | |||
| 111 | =head2 $driver->circle($picture, $x, $y, $width, $height, $color) | ||
| 112 | |||
| 113 | The last 7 methods are simple drawing methods. The coordinates are | ||
| 114 | absolute (ie as expressed in the $rootzone). Take care to convert | ||
| 115 | them if needed. | ||
| 116 | |||
| 117 | For the rectangles, you give the lower left corner and the upper | ||
| 118 | right corner. | ||
| 119 | |||
| 120 | The (0,0) coordinate is the lower left corner. | ||
| 121 | |||
| 122 | =head2 my $oldwidth = $driver->line_width($picture, $width) | ||
| 123 | |||
| 124 | This method changes the default width of displayed lines. It returns the | ||
| 125 | previous width so that you can restore it to its previous value once | ||
| 126 | you're finished with the operation needing a special line width. | ||
| 127 | |||
| 128 | If $width isn't given, it only returns the actual width. | ||
| 129 | |||
| 130 | =cut | ||
| 131 | sub line_width { | ||
| 132 | my ($self, $picture, $width) = @_; | ||
| 133 | if (! exists $picture->{'linewidth'}) { | ||
| 134 | $picture->{'linewidth'} = 1; | ||
| 135 | } | ||
| 136 | my $old = $picture->{'linewidth'}; | ||
| 137 | if (defined $width) { | ||
| 138 | $picture->{'linewidth'} = $width; | ||
| 139 | } | ||
| 140 | return $old; | ||
| 141 | } | ||
| 142 | |||
| 143 | =head2 $driver->string($p, $name, $size, $color, $x, $y, $text, $halign, $valign, $orientation) | ||
| 144 | |||
| 145 | This method is used to draw texts on the picture. | ||
| 146 | |||
| 147 | Horizontal/vertical align : $ALIGN_LEFT, $ALIGN_CENTER, $ALIGN_RIGHT | ||
| 148 | |||
| 149 | Orientation : $ORIENTATION_UP, $ORIENTATION_DOWN, $ORIENTATION_RIGHT, $ORIENTATION_LEFT | ||
| 150 | |||
| 151 | The text is displayed near (x,y) by follwing the required alignments. | ||
| 152 | |||
| 153 | =head1 Output API | ||
| 154 | |||
| 155 | =head2 $driver->save_to($p, $filename) | ||
| 156 | |||
| 157 | Save the picture in the given filename. | ||
| 158 | |||
| 159 | =head2 $driver->dump($p) | ||
| 160 | |||
| 161 | Dump the picture to the standard output. | ||
| 162 | |||
| 163 | =head1 Generic functions | ||
| 164 | |||
| 165 | Those functions don't need to be reimplemented, they are implemented with the | ||
| 166 | other primitives. | ||
| 167 | |||
| 168 | =head2 $driver->cross($picture, $x1, $y1, $x2, $y2, $color) | ||
| 169 | |||
| 170 | Draw a cross. | ||
| 171 | |||
| 172 | =cut | ||
| 173 | sub cross { | ||
| 174 | my ($self, $p, $x1, $y1, $x2, $y2, $color) = @_; | ||
| 175 | $self->line($p, $x1, $y1, $x2, $y2, $color); | ||
| 176 | $self->line($p, $x1, $y2, $x2, $y1, $color); | ||
| 177 | } | ||
| 178 | |||
| 179 | =head1 DATA STRUCTURE | ||
| 180 | |||
| 181 | =head2 Colors | ||
| 182 | |||
| 183 | Colors are simple RGB triplet associated to an alpha channel : [ R, G, B, A ] | ||
| 184 | They are only array references. | ||
| 185 | |||
| 186 | Some variables are available for the most common colors : | ||
| 187 | |||
| 188 | =over | ||
| 189 | |||
| 190 | =item $COLOR_WHITE | ||
| 191 | |||
| 192 | =item $COLOR_BLACK | ||
| 193 | |||
| 194 | =item $COLOR_RED | ||
| 195 | |||
| 196 | =item $COLOR_GREEN | ||
| 197 | |||
| 198 | =item $COLOR_BLUE | ||
| 199 | |||
| 200 | =back | ||
| 201 | |||
| 202 | =head2 Fonts | ||
| 203 | |||
| 204 | Font names are simple strings (true type font names). Font size are numbers. | ||
| 205 | |||
| 206 | Some variables are available for the most common values : | ||
| 207 | |||
| 208 | =over | ||
| 209 | |||
| 210 | =item $FONT_SIZE_TINY | ||
| 211 | |||
| 212 | =item $FONT_SIZE_SMALL | ||
| 213 | |||
| 214 | =item $FONT_SIZE_MEDIUM | ||
| 215 | |||
| 216 | =item $FONT_SIZE_LARGE | ||
| 217 | |||
| 218 | =item $FONT_SIZE_GIANT | ||
| 219 | |||
| 220 | =back | ||
| 221 | |||
| 222 | =over | ||
| 223 | |||
| 224 | =item $FONT_ARIAL | ||
| 225 | |||
| 226 | =item $FONT_TIMES | ||
| 227 | |||
| 228 | =item $FONT_HELVETICA | ||
| 229 | |||
| 230 | =item $FONT_SERIF | ||
| 231 | |||
| 232 | =item $FONT_SANS_SERIF | ||
| 233 | |||
| 234 | =item $FONT_FIXED | ||
| 235 | |||
| 236 | =item $FONT_PROPORTIONNAL | ||
| 237 | |||
| 238 | =back | ||
| 239 | |||
| 240 | =cut | ||
| 241 | |||
| 242 | 1; |