- Optification is done by auto builder now
[gnuplot] / term / gpr.trm
1 /* Hello, Emacs, this is -*-C-*-
2  * $Id: gpr.trm,v 1.13 2006/07/21 02:35:46 sfeam Exp $
3  *
4  */
5
6 /* GNUPLOT - gpr.trm */
7
8 /*[
9  * Copyright 1990 - 1993, 1998, 2004
10  *
11  * Permission to use, copy, and distribute this software and its
12  * documentation for any purpose with or without fee is hereby granted,
13  * provided that the above copyright notice appear in all copies and
14  * that both that copyright notice and this permission notice appear
15  * in supporting documentation.
16  *
17  * Permission to modify the software is granted, but not the right to
18  * distribute the complete modified source code.  Modifications are to
19  * be distributed as patches to the released version.  Permission to
20  * distribute binaries produced by compiling modified sources is granted,
21  * provided you
22  *   1. distribute the corresponding source modifications from the
23  *    released version in the form of a patch file along with the binaries,
24  *   2. add special version identification to distinguish your version
25  *    in addition to the base release version number,
26  *   3. provide your name and address as the primary contact for the
27  *    support of your modified version, and
28  *   4. retain our contact information in regard to use of the base
29  *    software.
30  * Permission to distribute the released version of the source code along
31  * with corresponding source modifications in the form of a patch file is
32  * granted with same provisions 2 through 4 for binary distributions.
33  *
34  * This software is provided "as is" without express or implied warranty
35  * to the extent permitted by applicable law.
36 ]*/
37
38 /*
39  * This file is included by ../term.c.
40  *
41  * This terminal driver supports:
42  *   APOLLO's GPR windowing system
43  *
44  * AUTHORS
45  *  Michael Aramini
46  *  Roque D Oliveira , oliveria@caen.engin.umich.edu
47  *
48  * send your comments or suggestions to (gnuplot-info@lists.sourceforge.net).
49  *
50  */
51
52 /*
53  * adapted to the new terminal layout by Stefan Bodewig (Dec. 1995)
54  */
55
56 #include "driver.h"
57
58 #ifdef TERM_REGISTER
59 register_term(gpr)
60 #endif
61
62 #ifdef TERM_PROTO
63 TERM_PUBLIC void GPR_init __PROTO((void));
64 TERM_PUBLIC void GPR_graphics __PROTO((void));
65 TERM_PUBLIC void GPR_text __PROTO((void));
66 TERM_PUBLIC void GPR_linetype __PROTO((int linetype));
67 TERM_PUBLIC void GPR_move __PROTO((unsigned int x, unsigned int y));
68 TERM_PUBLIC void GPR_vector __PROTO((unsigned int x, unsigned int y));
69 TERM_PUBLIC void GPR_put_text __PROTO((unsigned int x, unsigned int y, const char str[]));
70 TERM_PUBLIC int GPR_text_angle __PROTO((int ang));
71 TERM_PUBLIC int GPR_justify_text __PROTO((enum JUSTIFY mode));
72 TERM_PUBLIC void GPR_reset __PROTO((void));
73 /* landscape window */
74 /*
75 #define GPR_XMAX 720
76 #define GPR_YMAX 450
77 */
78 /* portrait window */
79 #define GPR_XMAX 585
80 #define GPR_YMAX 735
81 #define GPR_VCHAR 19
82 #define GPR_HCHAR 10
83 #define GPR_VTIC (GPR_YMAX/80)
84 #define GPR_HTIC (GPR_XMAX/80)
85 #endif /* TERM_PROTO */
86
87 #ifndef TERM_PROTO_ONLY
88 #ifdef TERM_BODY
89 void GPRold_linetype __PROTO((int linetype));
90 static void check __PROTO((char *messagex));
91 int gpr_isa_pad __PROTO((void));
92 #include <apollo/base.h>
93 #include <apollo/error.h>
94 #include <apollo/gpr.h>
95 #include <apollo/pad.h>
96
97 #define GPR_XLAST (GPR_XMAX - 1)
98 #define GPR_YLAST (GPR_YMAX - 1)
99
100
101 gpr_$direction_t gpr_path = gpr_$right;
102 /* text angle, 0=horizontal, 1=vertical */
103 int gpr_ang = 0;
104 /* text is flush left */
105 enum JUSTIFY gpr_justify = LEFT;
106
107 static status_$t status;
108 /* set it to 1 when debugging program */
109 unsigned int Debug = 0;
110
111
112 static void
113 check(char *messagex)
114 {
115     if (status.all = status_$ok) {
116         error_$print(status);
117         printf("Error occurred while %s.\n", messagex);
118     }
119 }
120
121 /* return whether stdout is a DM pad . Called by term.c */
122 int
123 gpr_isa_pad()
124 {
125     pad_$isa(1, &status);
126     return (status.all == status_$ok);
127 }
128
129
130 TERM_PUBLIC void
131 GPR_init()
132 {
133     gpr_$offset_t dm_bitmap_size;
134     gpr_$bitmap_desc_t dm_bitmap_desc;
135     pad_$window_desc_t window;
136     short font_id;
137     stream_$id_t stream_id;
138     static gpr_$rgb_plane_t hi_plane;
139     static gpr_$disp_char_t display_characteristics;
140     static float screen_size_r_width, screen_size_r_height;
141     static short int disp_len = sizeof(gpr_$disp_char_t);
142     static short int disp_len_returned;
143
144
145     /* open a pad to do graphics in */
146     window.top = 0;
147     window.left = 0;
148     /* 10 accounts for width of window border */
149     window.width = GPR_XMAX + 10;
150     /* 35 accounts for height of window border */
151     window.height = GPR_YMAX + 35;
152     pad_$create_window("", (short) 0, pad_$transcript, (short) 1, window, &stream_id, &status);
153     check("pad_$create_window");
154
155 /*  pad_$set_full_window(stream_id,(short) 1,&window, &status); */
156 /*  pad_$set_border (stream_id,(short) 1, true, &status); */
157     pad_$set_scale(stream_id, (short) 1, (short) 1, &status);
158     pad_$set_auto_close(stream_id, (short) 1, true, &status);
159
160     gpr_$inq_disp_characteristics(gpr_$direct, stream_id, disp_len, &display_characteristics, &disp_len_returned, &status);
161     check("in gpr_$inq_display_characteristics");
162     /*x_window_size in pixels */
163     screen_size_r_width = (float) display_characteristics.x_window_size;
164     /*y_window_size in pixels */
165     screen_size_r_height = (float) display_characteristics.y_window_size;
166     hi_plane = display_characteristics.n_planes - 1;
167     if (Debug)
168         printf("width=%f height=%f \n", screen_size_r_width, screen_size_r_height);
169
170     dm_bitmap_size.x_size = 1280;
171     dm_bitmap_size.y_size = 1024;
172     gpr_$init(gpr_$direct, stream_id, dm_bitmap_size, hi_plane, &dm_bitmap_desc, &status);
173     check("in gpr_$init");
174 /*
175   gpr_$set_obscured_opt(gpr_$pop_if_obs, &status);
176   check("in gpr_$set_obscured_opt");
177 */
178     gpr_$set_auto_refresh(true, &status);
179     check("in gpr_$set_auto_refresh");
180
181     /* load a font and make it current */
182     gpr_$load_font_file("f7x13", 5, &font_id, &status);
183     check("in gpr_$load_font_file");
184     gpr_$set_text_font(font_id, &status);
185     check("in gpr_$set_text_font");
186
187     /* set up color values */
188     gpr_$set_draw_value((gpr_$pixel_value_t) 7, &status);       /* white */
189     check("in gpr_set_draw_value");
190     gpr_$set_text_background_value((gpr_$pixel_value_t) (-1), &status); /* trans */
191     check("in gpr_$set_text_background_value");
192     gpr_$set_text_value((gpr_$pixel_value_t) 7, &status);       /* white */
193     check("in gpr_$set_text_value");
194 }
195
196
197 TERM_PUBLIC void
198 GPR_graphics()
199 {
200     gpr_$coordinate_t locx, locy, marker_size;
201
202     (void) gpr_$acquire_display(&status);
203     check("in gpr_$acquire display");
204     gpr_$clear((gpr_$pixel_value_t) 0, &status);        /* black */
205     check("in gpr_$clear");
206
207     if (Debug) {
208         marker_size = (short) 10;
209
210         locx = (short) 5;
211         locy = (short) 5;
212         gpr_$set_draw_value((gpr_$pixel_value_t) 2, &status);   /* white */
213         gpr_$move((locx - marker_size / 2), locy, &status);
214         gpr_$line((locx + marker_size / 2), locy, &status);
215         gpr_$move(locx, (locy + marker_size / 2), &status);
216         gpr_$line(locx, (locy - marker_size / 2), &status);
217
218         locx = (short) (GPR_XMAX - 1 - 5);
219         locy = (short) 5;
220         gpr_$set_draw_value((gpr_$pixel_value_t) 3, &status);   /* white */
221         gpr_$move((locx - marker_size / 2), locy, &status);
222         gpr_$line((locx + marker_size / 2), locy, &status);
223         gpr_$move(locx, (locy + marker_size / 2), &status);
224         gpr_$line(locx, (locy - marker_size / 2), &status);
225
226         locx = (short) 5;
227         locy = (short) (GPR_YMAX - 1 - 5);
228         gpr_$set_draw_value((gpr_$pixel_value_t) 4, &status);   /* white */
229         gpr_$move((locx - marker_size / 2), locy, &status);
230         gpr_$line((locx + marker_size / 2), locy, &status);
231         gpr_$move(locx, (locy + marker_size / 2), &status);
232         gpr_$line(locx, (locy - marker_size / 2), &status);
233
234         locx = (short) (GPR_XMAX - 1 - 5);
235         locy = (short) (GPR_YMAX - 1 - 5);
236         gpr_$set_draw_value((gpr_$pixel_value_t) 5, &status);   /* white */
237         gpr_$move((locx - marker_size / 2), locy, &status);
238         gpr_$line((locx + marker_size / 2), locy, &status);
239         gpr_$move(locx, (locy + marker_size / 2), &status);
240         gpr_$line(locx, (locy - marker_size / 2), &status);
241
242         gpr_$set_draw_value((gpr_$pixel_value_t) 7, &status);   /* white */
243         check("in gpr_$set_draw_value");
244     }                           /* end if(Debug) */
245 }
246
247
248 TERM_PUBLIC void
249 GPR_text()
250 {
251     gpr_$release_display(&status);
252     check("gpr_$release_display");
253 }
254
255
256 void
257 GPRold_linetype(int linetype)
258 {
259     static gpr_$line_pattern_t patterns[2 + 5] =
260     {
261         {0xFFFF},               /* solid              1111111111111111 */
262         {0x3FFF},               /* very long dashed   0011111111111111 */
263         {0xFFFF},               /* solid              1111111111111111 */
264         {0x5555},               /* dotted             0101010101010101 */
265         {0x3333},               /* short dashed       0011001100110011 */
266         {0xB5AD},               /* dot short-dashed   1011010110101101 */
267         {0x3FFF}                /* very long dashed   0011111111111111 */
268     };
269     if (linetype >= 5)
270         linetype %= 5;
271     gpr_$set_line_pattern((short) 1, patterns[linetype + 2], (short) 16, &status);
272     check("in gpr_$set_line_pattern");
273 }
274
275 TERM_PUBLIC void
276 GPR_linetype(int linetype)
277 {
278     static gpr_$line_pattern_t patterns[2 + 7] =
279     {
280         {0xFFFF},               /* solid              1111111111111111 */
281         {0x1111},               /* long-spaced dotted 0001000100010001 */
282         {0xFFFF},               /* solid              1111111111111111 */
283         {0x5555},               /* dotted             0101010101010101 */
284         {0x3333},               /* short  dashed      0011001100110011 */
285         {0x7777},               /* medium dashed      0111011101110111 */
286         {0x3F3F},               /* long   dashed      0011111100111111 */
287         {0x0F0F},               /* long-spaced dashed 0000111100001111 */
288         {0x5F5F}                /* dot dashed         0101111101011111 */
289     };
290     if (linetype >= 7)
291         linetype %= 7;
292     gpr_$set_line_pattern((short) 1, patterns[linetype + 2], (short) 16, &status);
293     check("in gpr_$set_line_pattern");
294
295 /*
296   gpr_$set_draw_value((gpr_$pixel_value_t)(linetype + 1), &status);
297   check("in gpr_$set_draw_value");
298 */
299 }
300
301
302 TERM_PUBLIC void
303 GPR_move(unsigned int x, unsigned int y)
304 {
305     gpr_$move((short) x, (short) (GPR_YMAX - 1 - y), &status);
306     check("in gpr_$move");
307 }
308
309
310 TERM_PUBLIC void
311 GPR_vector(unsigned int x, unsigned int y)
312 {
313     gpr_$line((short) x, (short) (GPR_YMAX - 1 - y), &status);
314     check("in gpr_$line");
315 }
316
317
318 TERM_PUBLIC void
319 GPR_put_text(unsigned int x, unsigned int y, const char str[])
320 {
321     gpr_$coordinate_t xgpr, ygpr;
322     gpr_$offset_t str_size_in_pixels;
323     short int str_len;
324
325     gpr_$coordinate_t locx, locy, marker_size;
326
327     if (Debug) {
328         locx = (short) x;
329         locy = (short) (GPR_YMAX - 1 - y);
330         marker_size = (short) 20;
331         gpr_$set_draw_value((gpr_$pixel_value_t) 1, &status);   /* white */
332         gpr_$move((locx - marker_size / 2), locy, &status);
333         gpr_$line((locx + marker_size / 2), locy, &status);
334         gpr_$move(locx, (locy + marker_size / 2), &status);
335         gpr_$line(locx, (locy - marker_size / 2), &status);
336         gpr_$set_draw_value((gpr_$pixel_value_t) 7, &status);   /* white */
337     }
338     xgpr = (short) x;
339     ygpr = (short) (GPR_YMAX - 1 - y);
340     gpr_$set_text_path(gpr_path, &status);
341     check("gpr_$set_text_path");
342
343     str_len = (short) strlen(str);
344     /* Calculate how much space (in pixels) the string requires */
345     gpr_$inq_text_extent(str, str_len, &str_size_in_pixels, &status);
346     check("in gpr_$inq_text_extent");
347
348     switch (gpr_justify) {
349     case LEFT:
350         {
351             switch (gpr_path) {
352             case gpr_$up:       /* vertical */
353                 {
354                     if (Debug)
355                         printf("LEFT and up , str=%s\n", str);
356                     break;
357                 }
358             case gpr_$right:    /* horizontal */
359                 {
360                     ygpr = ygpr + str_size_in_pixels.y_size / 2;
361                     if (Debug)
362                         printf("LEFT and right, str=%s \n", str);
363                     break;
364                 }
365             }
366             break;
367         }
368
369     case CENTRE:
370         {
371             switch (gpr_path) {
372             case gpr_$up:       /* vertical */
373                 {
374                     xgpr = xgpr + str_size_in_pixels.x_size / 2;
375                     ygpr = ygpr + str_size_in_pixels.y_size / 2;
376                     if (Debug)
377                         printf("CENTRE and up, str=%s \n", str);
378                     break;
379                 }
380             case gpr_$right:    /* horizontal */
381                 {
382                     xgpr = xgpr - str_size_in_pixels.x_size / 2;
383                     ygpr = ygpr + str_size_in_pixels.y_size / 2;
384                     if (Debug)
385                         printf("CENTRE and right, str=%s \n", str);
386                     break;
387                 }
388             }
389             break;
390         }
391     case RIGHT:
392         {
393             switch (gpr_path) {
394             case gpr_$up:       /* vertical */
395                 {
396                     ygpr = ygpr + str_size_in_pixels.y_size;
397                     if (Debug)
398                         printf("RIGHT and up, str=%s \n", str);
399                     break;
400                 }
401             case gpr_$right:    /* horizontal */
402                 {
403                     xgpr = xgpr - str_size_in_pixels.x_size;
404                     ygpr = ygpr + str_size_in_pixels.y_size / 2;
405                     if (Debug)
406                         printf("RIGHT and right, str=%s \n", str);
407                     break;
408                 }
409             }
410             break;
411         }
412     }
413
414     gpr_$move(xgpr, ygpr, &status);
415     check("in gpr_$move");
416     gpr_$text(str, str_len, &status);
417     check("in gpr_$text");
418 }
419
420 TERM_PUBLIC int
421 GPR_text_angle(int ang)
422 {
423     if (gpr_ang != ang) {
424         gpr_ang = ang;
425         gpr_path = (gpr_ang ? gpr_$up : gpr_$right);
426     }
427     return (TRUE);
428 }
429
430 TERM_PUBLIC int
431 GPR_justify_text(enum JUSTIFY mode)
432 {
433     gpr_justify = mode;
434     return (TRUE);
435 }
436
437 TERM_PUBLIC void
438 GPR_reset()
439 {
440     gpr_$terminate(false, &status);
441     check("in gpr_$terminate");
442 }
443
444 #endif /* TERM_BODY */
445
446 #ifdef TERM_TABLE
447
448 TERM_TABLE_START(gpr_driver)
449     "gpr", "Apollo Graphics Primitive Resource, fixed-size window",
450     GPR_XMAX, GPR_YMAX, GPR_VCHAR, GPR_HCHAR,
451     GPR_VTIC, GPR_HTIC, options_null, GPR_init, GPR_reset,
452     GPR_text, null_scale, GPR_graphics, GPR_move, GPR_vector,
453     GPR_linetype, GPR_put_text, GPR_text_angle,
454     GPR_justify_text, line_and_point, do_arrow, set_font_null
455 TERM_TABLE_END(gpr_driver)
456
457 #undef LAST_TERM
458 #define LAST_TERM gpr_driver
459
460 #endif /* TERM_TABLE */
461 #endif /* TERM_PROTO_ONLY */
462
463 #ifdef TERM_HELP
464 START_HELP(gpr)
465 "1 gpr",
466 "?commands set terminal gpr",
467 "?set terminal gpr",
468 "?set term gpr",
469 "?terminal gpr",
470 "?term gpr",
471 "?gpr",
472 " The `gpr` terminal driver supports the Apollo Graphics Primitive Resource",
473 " for a fixed-size window.  It has no options.",
474 "",
475 " If a variable window size is desired, use the `apollo` terminal instead."
476 END_HELP(gpr)
477 #endif /* TERM_HELP */