Simple.pm untested

filter: Statementstmt 0.0% 0 / 96 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 25 totaltotal 0.0% 0 / 191 CC 13 SCAR 18.8
CC
13
Cov
0%
map_color
20.0
new_window
12.0
startargs
12.0
CRAP 6.5
1 =head1 NAME
2  
3 Graphics::Simple -- a simple , device-independent graphics API for Perl
4  
5 =head1 SYNOPSIS
6  
7 use Graphics::Simple;
8  
9 line 100,100,200,200;
10 circle 50,50,25;
11 stop(); clear(); # Wait for a button press, clear the page
12  
13 =head1 DESCRIPTION
14  
15 Ever had a Commodore C-64 or Vic-20 or some other of the machines
16 of that era? Where doing graphics was as simple as
17  
18 line 20,20,50,30;
19  
20 and you didn't have to go through things like C<XOpenDisplay> etc.
21  
22 This module tries to bring back the spirit of that era in a modern
23 environment:
24 this module presents a simple, unified API to several different
25 graphics devices - currently X (using Gtk and Gnome) and PostScript.
26  
27 The interface is primarily made easy-to-use, starting from the idea
28 that the above C<line> command must work. Therefore, it exports
29 most of the primitives by default (you can turn this off).
30  
31 However, everything is not sacrificed in the name
32 of simplicity: believing in "simple things simple, complicated
33 things possible", this module also allows multiple windows
34 (all the primitives also work as methods of window objects)
35 as well as raw access to the underlying devices - although the
36 device-independence is then lost.
37 In future plans are some sort of interactions with the devices
38 with which it is possible as well as the addition of more devices.
39  
40 The C<use> command currently accepts the forms
41  
42 use Graphics::Simple;
43 use Graphics::Simple qw/line circle/;
44 use Graphics::Simple 300,400; # portrait paper
45 use Graphics::Simple 300,400, qw/line circle/;
46  
47 i.e. the optional size of the default window first and then
48 normal C<Exporter> arguments.
49  
50 C<Graphics::Simple> has several different back-ends, currently
51 GnomeCanvas, TkCanvas, PostScript and (not fully working yet) Fig.
52 Other backends are expected.
53  
54 To start C<Graphics::Simple> with a given backend, you should set
55 the environment variable C<GSIMPL> to the value, e.g. by running
56 your script with the command
57  
58 GSIMPL='PostScript' perl gt1.pl
59  
60 or by setting the environment variable permanently in your shell, by
61  
62 GSIMPL=PostScript
63 export GSIMPL
64  
65 or
66  
67 setenv GSIMPL PostScript
68  
69 depending on which shell you use.
70  
71 =cut
72  
73 # Handle output files as well
74  
75 @Graphics::Simple::Window::ISA = Graphics::Simple;
76  
77 package Graphics::Simple;
78  
79 require Exporter;
80 @ISA='Exporter';
81  
82 @EXPORT = qw/line linewidth circle text clear stop new_window line_to color arrow/;
83  
84 @DefSize = (300,300);
85  
86 sub import {
87 if($_[1] =~ /^[0-9]+$/) {
88 @DefSize = splice @_,1,2;
89 }
90 goto &Exporter::import;
91 }
92  
93  
94 $VERSION='0.04';
95  
96 @impl = qw/
97 GSGtk.pm
98 /;
99  
100 # require 'GSGtk.pm';
101 # require 'GSPS.pm';
102  
103 $impl = GnomeCanvas;
104 # $impl = PostScript;
105 if($ENV{GSIMPL}) {
106 $impl = $ENV{GSIMPL}
107 }
108  
109 sub new_window {
110 my($x, $y) = @_;
111 ($x,$y) = @DefSize if(!$x) ;
112 eval "use Graphics::Simple::$impl";
113 die("Couldn't get implementation! '$@'") if $@;
114 my($win) = "Graphics::Simple::$impl"->_construct($x,$y) ;
115 $win->{Current_Color} = '#000000';
116 $win->{Current_LineWidth} = 1;
117 return $win;
118 }
119  
120 $Graphics::ObjectName = "GOAAA0";
121  
122 sub startargs {
123 my ($win, $name);
124 $win = (UNIVERSAL::isa($_[0],Graphics::Simple::Window) ?
125 shift :
126 $Graphics::Simple::Window ||
127 ($Graphics::Simple::Window = new_window()));
128 $name = (!ref $_[0] && $_[0] =~ /^[a-zA-Z]/ ?
129 shift :
130 $Graphics::ObjectName++);
131 return ($win,$name);
132 }
133  
134 sub startwin {
135 $win = (UNIVERSAL::isa($_[0],Graphics::Simple::Window) ?
136 shift :
137 $Graphics::Simple::Window ||
138 ($Graphics::Simple::Window = new_window()));
139 return $win;
140 }
141  
142 =head2 line [$win_to], [$name], $x1, $y1, $x2, $y2, ...
143  
144 Draws a line through the points given.
145  
146 =cut
147  
148 sub line {
149 my ($win_to, $name) = &startargs;
150 $win_to->_line($name, @_);
151 }
152  
153 =head2 arrow [$win_to], [$name], $x1, $y1, $x2, $y2, ...
154  
155 Like line, but makes an arrowhead in the end.
156  
157 =cut
158  
159 sub arrow {
160 my ($win, $name) = &startargs;
161 $win->line($name, @_);
162 $win->_arrow_head(@_[-4,-3,-2,-1]);
163 }
164  
165 sub _arrow_head {
166 my($win, $x1,$y1,$x2,$y2) = @_;
167 my $l = sqrt(($x2-$x1)**2 + ($y2-$y1)**2);
168 return if abs($l) < 0.0000001;
169 my $vx = ($x2-$x1)/$l;
170 my $vy = ($y2-$y1)/$l;
171 # my $par = 5 * sqrt($win->{Current_LineWidth});
172 # my $perp = 4 * sqrt($win->{Current_LineWidth});
173 my $par = 10;
174 my $perp = 7;
175 $win->line($x2-$par*$vx-$perp*$vy,
176 $y2-$par*$vy+$perp*$vx,
177 $x2, $y2,
178 $x2-$par*$vx+$perp*$vy,
179 $y2-$par*$vy-$perp*$vx);
180 }
181  
182 =head2 line_to [$win], [$name], $x1, $y1, $x2, $y2, ...
183  
184 Called several times in a sequence, starts and continues adding
185 points to a line. If called with no coordinates, finishes the
186 current line. This is just a convenient wrapper over a C<line> call with
187 all the parameters given - a faster way would just be to collect your
188 parameters to an array.
189  
190 =cut
191  
192 sub line_to {
193 my ($win_to, $name) = &startargs;
194 if(@_) {
195 # $win_to->{Turtle_Name} = $name; # XXX
196 push @{$win_to->{Turtle_Pts}}, @_;
197 } else {
198 my $p = delete $win_to->{Turtle_Pts};
199 $win_to->_line($name, @$p);
200 }
201 }
202  
203 =head2 circle [$win], [$name], $x, $y, $radius
204  
205 Duh.
206  
207 =cut
208  
209 sub circle {
210 my ($win_to, $name) = &startargs;
211 my($x, $y, $r) = @_;
212 # $win_to->_circle($name, @_);
213 $win_to->_ellipse($name, $x-$r, $y-$r, $x+$r, $y+$r);
214 }
215  
216 =head2 ellipse [$win], [$name], $x1, $y1, $x2, $y2
217  
218 The ellipse enclosed in the rectangle given by its two corners
219  
220 =cut
221  
222 sub ellipse {
223 my ($win_to, $name) = &startargs;
224 $win_to->_ellipse($name, @_);
225 }
226  
227 =head2 text [$win], [$name], $x, $y, $string
228  
229 Duh...
230  
231 =cut
232  
233 sub text {
234 my ($win_to, $name) = &startargs;
235 $win_to->_text($name, @_);
236 }
237  
238 =head2 image [$win], [$name], $x, $y, $width, $height, $depth, $string
239  
240 This command creates a rectangular bit- or pixmap. The $depth parameter
241 is the number of bytes per pixel (1 for B/W or 3 for RGB) and the string
242 is a string of packed bytes that describe the image.
243  
244 For instance, the module PDL is a good source for such strings.
245  
246 =cut
247  
248 sub image {
249 my($win_to, $name) = &startargs;
250 $win_to->_image($name, @_);
251 }
252  
253 =head2 clear, stop
254  
255 stop [$win]
256 clear [$win]
257  
258 C<clear> removes all the drawn elements from the window. <wait>
259 waits for a button press. These are usually coupled:
260  
261 stop; clear;
262  
263 =cut
264  
265 sub clear {
266 my ($win_to) = &startargs;
267 $win_to->_clear();
268 }
269  
270 sub stop {
271 my ($win_to) = &startargs;
272 $win_to->_wait();
273 }
274  
275  
276 =head2 set_window, get_window
277  
278 See the source - undocumented and potentially changing api
279  
280 =cut
281  
282 # We may get rid of these later.
283 sub set_window {
284 my ($win) = @_;
285 $Graphics::Simple::Window = $win;
286 }
287  
288 sub get_window {
289 return $Graphics::Simple::Window;
290 }
291  
292  
293 =head2 push_window, pop_window
294  
295 C<Graphics::Simple> maintains a simple window stack so that subroutines
296 can easily use
297  
298 push_window $win;
299 line ...
300 pop_window();
301  
302 to avoid having too many method calls.
303  
304 =cut
305  
306 {my @windows;
307 sub push_window {
308 push @windows, $Window;
309 $Window = $_[0];
310 }
311  
312 sub pop_window {
313 $Window = pop @windows;
314 }
315 }
316  
317 =head2 color [$win,] $color;
318  
319 Set the current color to $color. Currently, the colors known are
320  
321 red green blue black white
322  
323 as well as any RGB color with the X syntax:
324  
325 color '#FFFF00';
326  
327 is yellow. You can also give an array ref of three numbers between
328 0 and 1 for RGB colors.
329  
330 =cut
331  
332 {
333 my %colors = (
334 red => '#FF0000',
335 green => '#00FF00',
336 blue => '#0000FF',
337 black => '#000000',
338 white => '#FFFFFF',
339 );
340 sub map_color {
341 my($color) = @_;
342 if(ref $color) {
343 $color = sprintf("#%02x%02x%02x", map {$_*255} @$color)
344 }
345 $color = $colors{$color} if $colors{$color};
346 die("Invalid color '$color': must be of form '#hhhhhh'")
347 unless $color =~ /^#[0-9a-fA-F]{6}?$/;
348 return $color;
349 }
350 my $x = '[0-9a-fA-F]{2}?';
351 sub get_float_color {
352 my($color) = @_;
353 $color =~ /^#($x)($x)($x)$/ or die("$color not in X format");
354 return [map {hex($_)/255.0} $1,$2,$3];
355 }
356 }
357  
358 sub color {
359 my ($win_to) = &startwin;
360 my $color = map_color(shift);
361 $win_to->{Current_Color} = $color;
362 $win_to->_color_changed();
363 }
364  
365 sub _color_changed { }
366  
367 =head2 linewidth [$win,] $width
368  
369 =cut
370  
371 sub linewidth {
372 my($win_to) = &startwin;
373 $win_to->{Current_LineWidth} = shift;
374 $win_to->_linewidth_changed();
375 }
376  
377 sub _linewidth_changed { }
378  
379 sub END {
380 if(my $win = $Graphics::Simple::Window) {
381 $win->_finish();
382 }
383 }
384  
385 1;
386  
387 =head1 BUGS
388  
389 This is an alpha proof-of-principle version - the API may still change
390 wildly.
391  
392 =head1 AUTHOR
393  
394 Copyright(C) Tuomas J. Lukka 1999. All rights reserved.
395 This software may be distributed under the same conditions as Perl itself.
396  
397 =cut