| 1 | =head1 NAME | ||
| 2 | |||
| 3 | Graphics::Simple::PostScript -- implement Graphics::Simple in PostScript files | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | use Graphics::Simple; | ||
| 8 | # use those operations | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | The module C<Graphics::Simple::PostScript> is an implementation | ||
| 13 | of the C<Graphics::Simple> API. | ||
| 14 | |||
| 15 | =head1 DEVICE-DEPENDENT OPERATIONS | ||
| 16 | |||
| 17 | =head2 stop | ||
| 18 | |||
| 19 | Waiting is implemented by writing the current image as a snapshot to a file. | ||
| 20 | The problem is that this only applies to the one window - code that | ||
| 21 | has two windows will not show all the upgrades that GnomeCanvas shows. | ||
| 22 | This should be adressed somehow - the problem is that we don't want | ||
| 23 | to duplicate all the static images in other windows if only one is changing. | ||
| 24 | |||
| 25 | The files are currently written into C</tmp/>. | ||
| 26 | |||
| 27 | =cut | ||
| 28 | |||
| 29 | |||
| 30 | package Graphics::Simple::PostScript; | ||
| 31 | |||
| 32 | my $fileno = 'dump000'; | ||
| 33 | |||
| 34 | @ISA = 'Graphics::Simple::Window'; | ||
| 35 | |||
| 36 | sub _construct { | ||
| 37 | my($type, $x, $y, $fn) = @_; | ||
| 38 | $fn = sub { '/tmp/'.$fileno++.".ps" } if !defined $fn; | ||
| 39 | bless { | ||
| 40 | X => $x, | ||
| 41 | Y => $y, | ||
| 42 | FN => $fn | ||
| 43 | }, $type; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub _finish { | ||
| 47 | $_[0]->_wait | ||
| 48 | } | ||
| 49 | |||
| 50 | sub _clear { | ||
| 51 | delete $_[0]{Objs}; | ||
| 52 | delete $_[0]{Obj}; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub _colorstr { | ||
| 56 | my($this, $color) = @_; | ||
| 57 | if(!defined $color) { | ||
| 58 | $color = $this->{Current_Color}; | ||
| 59 | } | ||
| 60 | my $c = Graphics::Simple::get_float_color($color); | ||
| 61 | " $c->[0] $c->[1] $c->[2] setrgbcolor "; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub _widthstr { | ||
| 65 | my($this, $width) = @_; | ||
| 66 | if(!defined $width) { | ||
| 67 | $width = $this->{Current_LineWidth}; | ||
| 68 | } | ||
| 69 | " $width setlinewidth "; | ||
| 70 | |||
| 71 | } | ||
| 72 | |||
| 73 | sub _line { | ||
| 74 | my $this = shift; | ||
| 75 | my $name = shift; | ||
| 76 | my $s = $this->_widthstr.$this->_colorstr."\n newpath "; | ||
| 77 | my $f; | ||
| 78 | while(@_) { | ||
| 79 | my ($x,$y) = (shift,shift); | ||
| 80 | $s .= " $x $y ".($f ? 'lineto ':'moveto '); | ||
| 81 | $f = 1; | ||
| 82 | } | ||
| 83 | $this->{Obj}{$name} = $s." stroke "; | ||
| 84 | push @{$this->{Objs}}, $name; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub _ellipse { | ||
| 88 | my($this, $name, $x1, $y1, $x2, $y2) = @_; | ||
| 89 | my $xr = $x2-$x1; my $yr = $y2-$y1; | ||
| 90 | my ($xsca, $ysca, $r); | ||
| 91 | if($xr > $yr) { | ||
| 92 | $xsca = 1; | ||
| 93 | $ysca = $yr/$xr; | ||
| 94 | $r = $xr; | ||
| 95 | } else { | ||
| 96 | $ysca = 1; | ||
| 97 | $xsca = $xr/$yr; | ||
| 98 | $r = $yr; | ||
| 99 | } | ||
| 100 | my $s = sprintf($this->_widthstr.$this->_colorstr." gsave newpath | ||
| 101 | %f %f translate %f %f scale %f 0 moveto 0 0 %f 0 360 arc | ||
| 102 | ", | ||
| 103 | ($x2+$x1)/2, ($y2+$y1)/2, | ||
| 104 | $xsca, $ysca, | ||
| 105 | $r, | ||
| 106 | $r); | ||
| 107 | $this->{Obj}{$name} = $s." stroke grestore "; | ||
| 108 | push @{$this->{Objs}}, $name; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub _text { | ||
| 112 | my($this, $name, $x, $y, $text) = @_; | ||
| 113 | } | ||
| 114 | |||
| 115 | sub _wait { | ||
| 116 | my($this) = @_; | ||
| 117 | # use Data::Dumper; print Dumper($this); | ||
| 118 | my $fn = $this->{FN}; | ||
| 119 | $fn = $fn->() if(ref $fn); | ||
| 120 | open PSOUT, ">$fn"; | ||
| 121 | for(@{$this->{Objs}}) { | ||
| 122 | print PSOUT $this->{Obj}{$_}; | ||
| 123 | } | ||
| 124 | print PSOUT " showpage \n"; | ||
| 125 | close PSOUT; | ||
| 126 | print "Output written to '$fn'\n"; | ||
| 127 | } | ||
| 128 | |||
| 129 | =head1 BUGS | ||
| 130 | |||
| 131 | Writes much too much code, e.g. by setting width and color for each object. | ||
| 132 | Should check if it is already set and leave the old setting and other | ||
| 133 | such optimizations. | ||
| 134 | |||
| 135 | =head1 AUTHOR | ||
| 136 | |||
| 137 | Copyright(C) Tuomas J. Lukka 1999. All rights reserved. | ||
| 138 | This software may be distributed under the same conditions as Perl itself. | ||
| 139 | |||
| 140 | =cut |