| 1 | =head1 NAME | ||
| 2 | |||
| 3 | Graphics::Simple::Fig -- implement Graphics::Simple in Fig (e.g. xfig) 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::Fig> 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::Fig; | ||
| 31 | |||
| 32 | my $fileno = 'fig000'; | ||
| 33 | |||
| 34 | @ISA = 'Graphics::Simple::Window'; | ||
| 35 | |||
| 36 | sub _construct { | ||
| 37 | my($type, $x, $y, $fn) = @_; | ||
| 38 | $fn = sub { '/tmp/'.$fileno++.".fig" } 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 | return ""; # Not yet | ||
| 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 = "2 1 0 1 0 7 100 0 -1 0.000 0 0 -1 0 0 ".(@_/2)."\n"; | ||
| 77 | my $f; | ||
| 78 | $s .= join ' ',@_; | ||
| 79 | $s .= "\n"; | ||
| 80 | push @{$this->{Objs}}, $s; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub _ellipse { | ||
| 84 | my($this, $name, $x1, $y1, $x2, $y2) = @_; | ||
| 85 | my $xr = $x2-$x1; my $yr = $y2-$y1; | ||
| 86 | my ($xsca, $ysca, $r); | ||
| 87 | if($xr > $yr) { | ||
| 88 | $xsca = 1; | ||
| 89 | $ysca = $yr/$xr; | ||
| 90 | $r = $xr; | ||
| 91 | } else { | ||
| 92 | $ysca = 1; | ||
| 93 | $xsca = $xr/$yr; | ||
| 94 | $r = $yr; | ||
| 95 | } | ||
| 96 | my $s = sprintf($this->_widthstr.$this->_colorstr." gsave newpath | ||
| 97 | %f %f translate %f %f scale %f 0 moveto 0 0 %f 0 360 arc | ||
| 98 | ", | ||
| 99 | ($x2+$x1)/2, ($y2+$y1)/2, | ||
| 100 | $xsca, $ysca, | ||
| 101 | $r, | ||
| 102 | $r); | ||
| 103 | $this->{Obj}{$name} = $s." stroke grestore "; | ||
| 104 | push @{$this->{Objs}}, $name; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub _text { | ||
| 108 | my($this, $name, $x, $y, $text) = @_; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub _wait { | ||
| 112 | my($this) = @_; | ||
| 113 | # use Data::Dumper; print Dumper($this); | ||
| 114 | my $fn = $this->{FN}; | ||
| 115 | $fn = $fn->() if(ref $fn); | ||
| 116 | open FIGOUT, ">$fn"; | ||
| 117 | print FIGOUT "#FIG 3.2\nPortrait\nCenter\nMetric\nA4\n100.0\nSingle\n-2\n50 2\n"; | ||
| 118 | for(@{$this->{Objs}}) { | ||
| 119 | print FIGOUT $this->{Obj}{$_}; | ||
| 120 | } | ||
| 121 | print FIGOUT " showpage \n"; | ||
| 122 | close FIGOUT; | ||
| 123 | print "Output written to '$fn'\n"; | ||
| 124 | } | ||
| 125 | |||
| 126 | =head1 BUGS | ||
| 127 | |||
| 128 | Writes much too much code, e.g. by setting width and color for each object. | ||
| 129 | Should check if it is already set and leave the old setting and other | ||
| 130 | such optimizations. | ||
| 131 | |||
| 132 | =head1 AUTHOR | ||
| 133 | |||
| 134 | Copyright(C) Tuomas J. Lukka 1999. All rights reserved. | ||
| 135 | This software may be distributed under the same conditions as Perl itself. | ||
| 136 | |||
| 137 | =cut |