| 1 | package Finance::GeniusTrader::Graphics::Driver::Postscript; | ||
| 2 | |||
| 3 | # Copyright 2004 Oliver 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 PostScript::Simple; | ||
| 11 | use Finance::GeniusTrader::Conf; | ||
| 12 | |||
| 13 | our @ISA = qw(Finance::GeniusTrader::Graphics::Driver); | ||
| 14 | |||
| 15 | =head1 Finance::GeniusTrader::Graphics::Driver::Postscript | ||
| 16 | |||
| 17 | This driver implements the drawing primitives using the PostScripts::Simple-module. | ||
| 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 | my $i = new PostScript::Simple(eps => 1, | ||
| 31 | colour => 1, | ||
| 32 | xsize => $zone->external_width(), | ||
| 33 | ysize => $zone->external_height(), | ||
| 34 | units => "pt", | ||
| 35 | reencode => "ISOLatin1Encoding"); | ||
| 36 | my $p = { "img" => $i, "zone" => $zone }; | ||
| 37 | return $p; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub line { | ||
| 41 | my ($self, $p, $x1, $y1, $x2, $y2, $color) = @_; | ||
| 42 | my ($xpt1, $ypt1) = _convert_coord($p, $x1, $y1); | ||
| 43 | my ($xpt2, $ypt2) = _convert_coord($p, $x2, $y2); | ||
| 44 | Finance::GeniusTrader::Conf::default("Graphic::Linewidth", "1"); | ||
| 45 | $self->{'linewidth'} = Finance::GeniusTrader::Conf::get("Graphic::Linewidth"); | ||
| 46 | |||
| 47 | $p->{'img'}->setlinewidth( $self->{'linewidth'} ); | ||
| 48 | $p->{'img'}->line($xpt1, $ypt1, $xpt2, $ypt2, _get_color($p, $color)); | ||
| 49 | } | ||
| 50 | |||
| 51 | sub antialiased_line { | ||
| 52 | my ($self, $p, $x1, $y1, $x2, $y2, $color) = @_; | ||
| 53 | $self->line($p, $x1, $y1, $x2, $y2, $color); | ||
| 54 | } | ||
| 55 | |||
| 56 | sub dashed_line { | ||
| 57 | my ($self, $p, $x1, $y1, $x2, $y2, $color) = @_; | ||
| 58 | |||
| 59 | # Not yet implemented | ||
| 60 | } | ||
| 61 | |||
| 62 | sub rectangle { | ||
| 63 | my ($self, $p, $x1, $y1, $x2, $y2, $color) = @_; | ||
| 64 | my ($xpt1, $ypt1) = _convert_coord($p, $x1, $y1); | ||
| 65 | my ($xpt2, $ypt2) = _convert_coord($p, $x2, $y2); | ||
| 66 | |||
| 67 | $p->{'img'}->setcolour( _get_color($p, $color) ); | ||
| 68 | $p->{'img'}->box({filled=>0}, $xpt1, $ypt1, $xpt2, $ypt2); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub filled_rectangle { | ||
| 72 | my ($self, $p, $x1, $y1, $x2, $y2, $color) = @_; | ||
| 73 | my ($xpt1, $ypt1) = _convert_coord($p, $x1, $y1); | ||
| 74 | my ($xpt2, $ypt2) = _convert_coord($p, $x2, $y2); | ||
| 75 | $p->{'img'}->setcolour( _get_color($p, $color) ); | ||
| 76 | $p->{'img'}->box({filled=>1}, $xpt1, $ypt1, $xpt2, $ypt2); | ||
| 77 | } | ||
| 78 | |||
| 79 | sub polygon { | ||
| 80 | my ($self, $p, $color, @points) = @_; | ||
| 81 | |||
| 82 | my @pts = (); | ||
| 83 | if(scalar(@points)) { | ||
| 84 | for (my $i = 0; $i < scalar(@points); $i++) { | ||
| 85 | my ($x1, $x2) = _convert_coord($p, $points[$i][0], $points[$i][1]); | ||
| 86 | push @pts, $x1; | ||
| 87 | push @pts, $x2; | ||
| 88 | } | ||
| 89 | $p->{'img'}->setcolour( _get_color($p, $color) ); | ||
| 90 | $p->{'img'}->polygon({filled=>0}, @pts); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | sub filled_polygon { | ||
| 95 | my ($self, $p, $color, @points) = @_; | ||
| 96 | |||
| 97 | my @pts = (); | ||
| 98 | if(scalar(@points)) { | ||
| 99 | for (my $i = 0; $i < scalar(@points); $i++) { | ||
| 100 | my ($x1, $x2) = _convert_coord($p, $points[$i][0], $points[$i][1]); | ||
| 101 | push @pts, $x1; | ||
| 102 | push @pts, $x2; | ||
| 103 | } | ||
| 104 | $p->{'img'}->setcolour( _get_color($p, $color) ); | ||
| 105 | $p->{'img'}->polygon({filled=>1}, @pts); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | sub circle { | ||
| 110 | my ($self, $p, $x, $y, $width, $height, $color) = @_; | ||
| 111 | |||
| 112 | my ($x, $y) = _convert_coord($p, $x, $y); | ||
| 113 | $width = int($width/2); | ||
| 114 | $height = int($height/2); | ||
| 115 | |||
| 116 | $p->{'img'}->setcolour( _get_color($p, $color) ); | ||
| 117 | $p->{'img'}->circle($x, $y, ($width+$height)/2); | ||
| 118 | } | ||
| 119 | |||
| 120 | sub string { | ||
| 121 | my ($self, $p, $name, $size, $color, $x, $y, $text, $halign, | ||
| 122 | $valign, $orientation) = @_; | ||
| 123 | my ($offset_x, $offset_y) = (0, 0); | ||
| 124 | $halign = $ALIGN_CENTER if (! defined($halign)); | ||
| 125 | $valign = $ALIGN_BOTTOM if (! defined($valign)); | ||
| 126 | $orientation = $ORIENTATION_RIGHT if (! defined($orientation)); | ||
| 127 | |||
| 128 | my $rotate = 0; | ||
| 129 | $rotate = 90 if ($orientation eq $ORIENTATION_DOWN); | ||
| 130 | $rotate = -90 if ($orientation eq $ORIENTATION_UP); | ||
| 131 | |||
| 132 | my $nname = "Times-Roman-iso"; | ||
| 133 | $nname = "Helvetica-iso" if ($name =~ /arial/); | ||
| 134 | $p->{'img'}->setfont($nname, $size); | ||
| 135 | |||
| 136 | if ($valign eq $ALIGN_TOP) { | ||
| 137 | $y -= $size; | ||
| 138 | } elsif ($valign == $ALIGN_CENTER) { | ||
| 139 | $y -= ($size/2); | ||
| 140 | } | ||
| 141 | my ($nx, $ny) = _convert_coord($p, $x, $y); | ||
| 142 | |||
| 143 | my $alg = "centre"; | ||
| 144 | if ($halign eq $ALIGN_LEFT) { | ||
| 145 | $alg = "left"; | ||
| 146 | } elsif ($halign eq $ALIGN_RIGHT) { | ||
| 147 | $alg = "right"; | ||
| 148 | } | ||
| 149 | $p->{'img'}->text( { rotate => $rotate, align => $alg }, $nx, $ny, $text); | ||
| 150 | } | ||
| 151 | |||
| 152 | # OUTPUT FUNCTIONS | ||
| 153 | sub save_to { | ||
| 154 | my ($self, $p, $filename) = @_; | ||
| 155 | $p->{'img'}->output($filename); | ||
| 156 | } | ||
| 157 | |||
| 158 | sub dump { | ||
| 159 | my ($self, $p) = @_; | ||
| 160 | print $p->{'img'}->get(); | ||
| 161 | } | ||
| 162 | |||
| 163 | # PRIVATE FUNCTIONS | ||
| 164 | sub _get_color { | ||
| 165 | my ($p, $color) = @_; | ||
| 166 | return @{$color}[0..2]; | ||
| 167 | } | ||
| 168 | |||
| 169 | sub _convert_coord { | ||
| 170 | my ($p, $x, $y) = @_; | ||
| 171 | return ($x, $y); #$p->{'zone'}->external_height() - $y); | ||
| 172 | } | ||
| 173 | |||
| 174 | 1; |