Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

open-axiom repository from github

24005 views
1
/*
2
Copyright (C) 1991-2002, The Numerical ALgorithms Group Ltd.
3
All rights reserved.
4
Copyright (C) 2007-2008, Gabriel Dos Reis.
5
All rights reserved.
6
7
Redistribution and use in source and binary forms, with or without
8
modification, are permitted provided that the following conditions are
9
met:
10
11
- Redistributions of source code must retain the above copyright
12
notice, this list of conditions and the following disclaimer.
13
14
- Redistributions in binary form must reproduce the above copyright
15
notice, this list of conditions and the following disclaimer in
16
the documentation and/or other materials provided with the
17
distribution.
18
19
- Neither the name of The Numerical ALgorithms Group Ltd. nor the
20
names of its contributors may be used to endorse or promote products
21
derived from this software without specific prior written permission.
22
23
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
26
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
27
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
*/
35
36
37
#define _GFUN_C
38
39
#include <stdio.h>
40
#include <unistd.h>
41
#include <stdlib.h>
42
#include <string.h>
43
44
#include "Gdraws0.h"
45
#include "G.h"
46
#include "hash.h"
47
48
#include "Gfun.H1"
49
#include "cfuns.h"
50
51
using namespace OpenAxiom;
52
53
54
/*
55
* Given 2 file pointers, this function copies file ifp to file ofp
56
*/
57
58
static void
59
filecopy(FILE * ifp, FILE * ofp)
60
{
61
62
int c;
63
64
while ((c = getc(ifp)) != EOF)
65
putc(c, ofp);
66
}
67
68
69
/*
70
* PSCreateFile generates the output file by using the order of defined
71
* variables; they are used to create the OUTPUT file. Essentially,
72
* PSCreateFile() loop through the index of 0 to psDrawNo and checks if the
73
* file/procedure is used. If so, the file is included into the output
74
* file.
75
*/
76
77
int
78
PSCreateFile(
79
int bWidth, /* border width of picture frame */
80
Window vw, Window tw, /* viewWindow, and titleWindow */
81
char *title) /* title of picture to be drawn in title bar */
82
{
83
FILE *ifp, *ofp, *fp; /* input, output and temp file pointer */
84
int i; /* index */
85
86
/* last things to add to the script file */
87
88
fp = fopen(psData[scriptps].filename, "a");
89
fprintf(fp, "\n grestore\t%% restore graphics state\n\n");
90
fclose(fp);
91
92
#if 0
93
/* Make frame drawing optional. */
94
95
Gdraws_drawFrame(bWidth, vw, tw, title);
96
#endif
97
98
/* put procedures and script together into OUTPUT.ps */
99
100
if ((ofp = fopen(psData[output].filename, "w")) == NULL) {
101
fprintf(stderr, "Cannot open %s to write.\n", psData[output].filename);
102
return (psError);
103
}
104
else {
105
i = 1;
106
while (i < psDrawNo) { /* loops through each file/procedure */
107
if (psData[i].flag) { /* if set, procedure/file is used */
108
if ((ifp = fopen(psData[i].filename, "r")) == NULL) {
109
if (i == GCdictps) { /* GC dictionaries */
110
fprintf(stderr, "Warning: missing GCdictionary.\n");
111
}
112
else {
113
fprintf(stderr, "Cannot open %s to read.\n",
114
psData[i].filename);
115
fclose(ofp);
116
return (psError);
117
}
118
}
119
else {
120
filecopy(ifp, ofp);
121
fclose(ifp);
122
}
123
}
124
i++;
125
}
126
}
127
128
/* remove script file in tmp */
129
130
unlink(psData[scriptps].filename);
131
132
#if 0
133
/* remove GCdict file in tmp */
134
unlink(psData[GCdictps].filename);
135
#endif
136
137
return (fclose(ofp));
138
}
139
140
141
142
/*
143
* This function draws the frame of the picture, which corresponds to the
144
* picture frame on the X display. In addition, it draws the title window as
145
* well as the title of the picture.
146
*/
147
148
int
149
Gdraws_drawFrame(
150
int borderW, /* border width */
151
Window viewWindow, Window titleWindow,
152
char *title) /* title of picture */
153
{
154
FILE *fp;
155
XWindowAttributes vwInfo, twInfo;
156
157
/* choose 2 and "frameDict" for frame dictionary: can be anything else */
158
159
PSCreateContext((GC)2, "frameDict", borderW, psButtCap, psMiterJoin,
160
psWhite, psBlack);
161
162
fp = fopen(psData[scriptps].filename, "a");
163
164
XGetWindowAttributes(dsply, viewWindow, &vwInfo);
165
166
/* draw title window */
167
168
XGetWindowAttributes(dsply, titleWindow, &twInfo);
169
fprintf(fp, "\t%s\t%d\t%d\t%d\t%d\ttitle\n", "frameDict",
170
twInfo.height - vwInfo.height, twInfo.width, 0, vwInfo.height);
171
172
/* draw viewport window */
173
174
fprintf(fp, "\t%s\tdrawFrame\n", "frameDict"); /* using Gdraws_setDimension() */
175
176
/* draw title text */
177
178
psData[drawIstrps].flag = yes;
179
fprintf(fp, "\t%s\tloadFont\n\t%d\t(%s) stringwidth pop sub 2 div\n",
180
"frameDict", twInfo.width, title);
181
fprintf(fp, "\t%d\t(%s)\t(%s)\tpsDrawIStr\n", 15, title, "title");
182
183
return (fclose(fp));
184
}
185
186
187
/* setDimension sets the dimension of the picture */
188
189
int
190
Gdraws_setDimension(
191
Window viewWindow,
192
Window titleWindow)
193
{
194
FILE *fp;
195
XWindowAttributes vwInfo, twInfo;
196
float pageWidth, pageHeight, width;
197
198
fp = fopen(psData[scriptps].filename, "w");
199
200
XGetWindowAttributes(dsply, titleWindow, &twInfo);
201
XGetWindowAttributes(dsply, viewWindow, &vwInfo);
202
pageWidth = 575.0;
203
pageHeight = 750.0;
204
205
#if 0
206
pageWidth = (float) (DisplayWidth(dsply, scrn) / DisplayWidthMM(dsply, scrn));
207
pageWidth *= 160.0;
208
pageHeight = (float) (DisplayHeight(dsply, scrn) / DisplayHeightMM(dsply, scrn));
209
pageHeight *= 210.0;
210
fprintf(stderr, "%f, %f\n", pageWidth, pageHeight);
211
#endif
212
213
fprintf(fp, "\n gsave\t%% save graphics state for clipping path\n\n");
214
if ((vwInfo.height > pageWidth) || (vwInfo.height > pageHeight)) {
215
width = (float) vwInfo.width;
216
if (vwInfo.height > pageWidth) {
217
width = pageWidth / width;
218
fprintf(fp, "\t%f\t%f", width, width);
219
}
220
else {
221
if (vwInfo.height > pageHeight)
222
fprintf(fp, "\t%f\t%f", width, pageHeight / width);
223
}
224
}
225
else {
226
fprintf(fp, "\t%f\t%f", 1.0, 1.0);
227
}
228
fprintf(fp, "\tscale\n\n");
229
230
fprintf(fp, "\t%d\t%d\t%d\tsetDim\n", twInfo.height - vwInfo.height,
231
vwInfo.height, vwInfo.width);
232
233
/* Write a Bounding Box for psfig etc. */
234
235
fprintf(fp, "%%%%BoundingBox: 0 0 %d %d\n", vwInfo.height, vwInfo.width);
236
237
fprintf(fp, "\tmaxX maxY\t0 0\trectangle\tclip\t%% set clip path\n\n");
238
return (fclose(fp));
239
}
240
/*
241
* GDrawImageString draws an image text string
242
*/
243
244
int
245
GDrawImageString(
246
GC gc, /* graphics context */
247
Window wid, /* window id */
248
int x, int y,
249
const char* string,
250
int length, int dFlag)
251
{
252
int s;
253
254
switch (dFlag) {
255
case Xoption:
256
s = XDrawImageString(dsply, wid, gc, x, y, string, length);
257
break;
258
case PSoption:
259
{
260
FILE *fp;
261
262
if ((fp = fopen(psData[scriptps].filename, "a")) == NULL) {
263
fprintf(stderr, "GDrawImageString cannot open %s\n",
264
psData[scriptps].filename);
265
return (psError);
266
}
267
268
psData[drawIstrps].flag = yes; /* set procedure flag */
269
fprintf(fp, "\t%s\t%d\t%d\t(%s)\t(%s)\tpsDrawIStr\n",
270
PSfindGC(gc), x, y, string, "window");
271
s = fclose(fp);
272
}
273
break;
274
default:
275
fprintf(stderr, "GdrawImagestring request (%d) not implemented yet.\n", dFlag);
276
return (psError);
277
}
278
return (s);
279
}
280
281
/* Draws an arc; see XDrawArc */
282
283
int
284
GDrawArc(
285
GC gc, /* graphics context */
286
Window wid, /* window id */
287
int x, int y,
288
unsigned int wdth, unsigned int hght,
289
int ang1, int ang2, int dFlag)
290
{
291
int s = 0;
292
293
switch (dFlag) {
294
case Xoption:
295
XDrawArc(dsply, wid, gc, x, y, wdth, hght, ang1, ang2);
296
break;
297
case PSoption:
298
{
299
FILE *fp;
300
301
if ((fp = fopen(psData[scriptps].filename, "a")) == NULL) {
302
fprintf(stderr, "GDrawArc cannot open %s\n",
303
psData[scriptps].filename);
304
return (psError);
305
}
306
307
psData[drawarcps].flag = yes;
308
fprintf(fp, "\t%s\t%d\t%d\t%d\t%d\t%d\t%d\tpsDrawArc\n",
309
PSfindGC(gc), x, y, hght, wdth, ang1 / 64, ang2 / 64);
310
s = fclose(fp);
311
}
312
break;
313
default:
314
fprintf(stderr, "Gdrawarc request (%d) not implemented yet.\n",
315
dFlag);
316
return (psError);
317
}
318
return (s);
319
}
320
/*
321
* GDrawLine draws a line, see XDrawLine
322
*/
323
324
int
325
GDrawLine(
326
GC gc, /* graphics context */
327
Window wid, /* window id */
328
int x0, int y0, int x1, int y1, int dFlag)
329
{
330
int s = 0;
331
332
switch (dFlag) {
333
case Xoption:
334
XDrawLine(dsply, wid, gc, x0, y0, x1, y1);
335
break;
336
case PSoption:
337
{
338
FILE *fp;
339
340
if ((fp = fopen(psData[scriptps].filename, "a")) == NULL) {
341
fprintf(stderr, "GDrawLine cannot open %s\n",
342
psData[scriptps].filename);
343
return (psError);
344
}
345
346
psData[drawlineps].flag = yes; /* sets procedure flag */
347
fprintf(fp, "\t%s\t%d\t%d\t%d\t%d\tpsDrawLine\n",
348
PSfindGC(gc), x1, y1, x0, y0);
349
s = fclose(fp);
350
}
351
break;
352
default:
353
fprintf(stderr, "Gdrawline request (%d) not implemented yet.\n",
354
dFlag);
355
return (psError);
356
}
357
return (s);
358
}
359
360
361
362
363
364
/*
365
* GDrawLines draws lines; see XDrawLines
366
*/
367
368
int
369
GDrawLines(
370
GC gc, /* graphics context */
371
Window wid, /* window id */
372
XPoint * points, /* points */
373
int numberOfPoints, int mode, int dFlag)
374
/* number of points, mode and display flag */
375
{
376
int s = 0;
377
378
switch (dFlag) {
379
case Xoption:
380
XDrawLines(dsply, wid, gc, points, numberOfPoints, mode);
381
break;
382
case PSoption:
383
{
384
FILE *fp; /* not dealing with modes yet */
385
int i = 0;
386
387
if ((fp = fopen(psData[scriptps].filename, "a")) == NULL) {
388
fprintf(stderr, "GDrawLines cannot open %s\n",
389
psData[scriptps].filename);
390
return (psError);
391
}
392
393
psData[drawlinesps].flag = yes; /* set procedure flag */
394
fprintf(fp, "\t%s\n", PSfindGC(gc));
395
i = numberOfPoints - 1;
396
while (i > 0)
397
{ fprintf(fp, "\t%d\t%d\n", points[i].x, points[i].y);
398
i = i-1;
399
}
400
401
fprintf(fp, "\t%d\t%d\t%d\tpsDrawLines\n",
402
numberOfPoints, points[i].x, points[i].y);
403
s = fclose(fp);
404
}
405
break;
406
default:
407
fprintf(stderr, "Gdrawlines request (%d) not implemented yet\n",
408
dFlag);
409
return (psError);
410
}
411
return (s);
412
}
413
414
/*
415
* GDrawPoint draws a point, see XDrawPoint
416
*/
417
418
int
419
GDrawPoint(
420
Window wid, /* window id */
421
GC gc, /* graphics context */
422
int x0, int y0, int dFlag)
423
{
424
int s = 0;
425
426
switch (dFlag) {
427
case Xoption:
428
XDrawPoint(dsply, wid, gc, x0, y0);
429
break;
430
case PSoption:
431
{
432
FILE *fp;
433
434
if ((fp = fopen(psData[scriptps].filename, "a")) == NULL) {
435
fprintf(stderr, "GDrawPoint cannot open %s\n",
436
psData[scriptps].filename);
437
return (psError);
438
}
439
440
psData[drawpointps].flag = yes; /* sets procedure flag */
441
fprintf(fp, "\t%s\t%d\t%d\t%d\t%d\tpsDrawPoint\n",
442
PSfindGC(gc), x0, y0, x0 + 1, y0 + 1);
443
s = fclose(fp);
444
}
445
break;
446
default:
447
fprintf(stderr, "Gdrawpoint request (%d) not implemented yet\n",
448
dFlag);
449
return (psError);
450
}
451
return (s);
452
}
453
454
/*
455
* GDrawRectangle draws a rectangle; see XDrawRectangle
456
*/
457
458
int
459
GDrawRectangle(
460
GC gc,
461
Window windowId,
462
short int x,short int y,short int width,short int height,
463
int dFlag)
464
{
465
int s = 0;
466
467
switch (dFlag) {
468
case Xoption:
469
XDrawRectangle(dsply, windowId, gc, x, y, width, height);
470
break;
471
case PSoption:
472
{
473
FILE *fp;
474
475
if ((fp = fopen(psData[scriptps].filename, "a")) == NULL) {
476
fprintf(stderr, "GDrawRect cannot open %s\n",
477
psData[scriptps].filename);
478
return (psError);
479
}
480
481
psData[drawrectps].flag = yes;
482
fprintf(fp, "\t%s\t%d\t%d\t%d\t%d\tpsDrawRect\n",
483
PSfindGC(gc), width, height, x, y);
484
s = fclose(fp);
485
}
486
break;
487
default:
488
fprintf(stderr, "Gdrawrect request (%d) not implemented yet\n",
489
dFlag);
490
return (psError);
491
}
492
return (s);
493
}
494
495
/*
496
* GDraw3DButtonOut draws a rectangle with 3D shading on rhs and bottom
497
*/
498
499
int
500
GDraw3DButtonOut(
501
GC gc,
502
Window windowId,
503
short int x,short int y,short int width,short int height,
504
int dFlag)
505
{
506
/* draw regular rectangle */
507
508
int s = GDrawRectangle(gc, windowId, x, y, width - 1, height - 1, dFlag);
509
510
/* add extra line down rhs */
511
512
GDrawLine(gc, windowId,
513
x + width, y + 1, x + width, y + height,
514
dFlag);
515
516
/* add extra line across bottom */
517
518
GDrawLine(gc, windowId,
519
x + 1, y + height, x + width, y + height,
520
dFlag);
521
522
return (s);
523
}
524
525
/*
526
* GDraw3DButtonIn draws a rectangle with 3D shading on lhs and top
527
*/
528
529
int
530
GDraw3DButtonIn(
531
GC gc,
532
Window windowId,
533
short int x,short int y,short int width,short int height,
534
int dFlag)
535
{
536
/* draw regular rectangle */
537
538
int s = GDrawRectangle(gc, windowId, x + 1, y + 1, width - 1, height - 1, dFlag);
539
540
/* add extra line down lhs */
541
542
GDrawLine(gc, windowId,
543
x, y, x, y + height - 1,
544
dFlag);
545
546
/* add extra line across top */
547
548
GDrawLine(gc, windowId,
549
x, y, x + width - 1, y,
550
dFlag);
551
552
return (s);
553
}
554
555
/*
556
* GDrawPushButton draws a push button whose appearance depends on "isOn."
557
*/
558
559
int
560
GDrawPushButton(
561
Display * display,
562
GC gc1, GC gc2, GC gc3,
563
Window windowId,
564
short int x,short int y,short int width,short int height,
565
int isOn, const char *text,
566
unsigned long buttonColor, unsigned long color,
567
int dFlag)
568
{
569
int len = strlen(text);
570
571
if (dFlag == Xoption)
572
XClearArea(display, windowId, x, y, width + 1, height + 1, False);
573
574
GSetForeground(gc1, (float) buttonColor, dFlag);
575
576
if (isOn)
577
GDraw3DButtonIn(gc1, windowId, x, y, width, height, dFlag);
578
else
579
GDraw3DButtonOut(gc1, windowId, x, y, width, height, dFlag);
580
581
GSetForeground(gc2, (float) color, dFlag);
582
583
return GDrawString(gc2, windowId,
584
x + (isOn ? 2 : 0) + centerX(gc3, text, len, width),
585
y + (isOn ? 2 : 0) + centerY(gc3, height),
586
text, len, dFlag);
587
}
588
589
590
591
/*
592
* Draws a string; see XDrawString
593
*/
594
595
int
596
GDrawString(
597
GC gc, /* graphics context */
598
Window wid, /* window id */
599
int x, int y,
600
const char* string, /* string to be drawn */
601
int length, int dFlag)
602
{
603
int s;
604
605
switch (dFlag) {
606
case Xoption:
607
s = XDrawString(dsply, wid, gc, x, y, string, length);
608
break;
609
case PSoption:
610
{
611
FILE *fp;
612
613
if ((fp = fopen(psData[scriptps].filename, "a")) == NULL) {
614
fprintf(stderr, "GDrawString cannot open %s\n",
615
psData[scriptps].filename);
616
return (psError);
617
}
618
619
psData[drawstrps].flag = yes; /* sets procedure flag */
620
fprintf(fp, "\t%s\t(%s)\t%d\t%d\tpsDrawStr\n",
621
PSfindGC(gc), string, x, y);
622
623
s = fclose(fp);
624
}
625
break;
626
default:
627
fprintf(stderr, "Gdrawstring request (%d) not implemented yet\n",
628
dFlag);
629
return (psError);
630
}
631
return (s);
632
}
633
634
/*
635
* Draws and fills an arc with foreground color; see XFillArc
636
*/
637
638
int
639
GFillArc(
640
GC gc, /* graphics context */
641
Window wid, /* window id */
642
int x, int y,
643
unsigned int wdth, unsigned int hght,
644
int ang1, int ang2, int dFlag)
645
{
646
int s = 0;
647
648
switch (dFlag) {
649
case Xoption: /* angle: times 64 already */
650
XFillArc(dsply, wid, gc, x, y, wdth, hght, ang1, ang2);
651
break;
652
case PSoption:
653
{
654
FILE *fp;
655
656
if ((fp = fopen(psData[scriptps].filename, "a")) == NULL) {
657
fprintf(stderr, "GFillArc cannot open %s\n",
658
psData[scriptps].filename);
659
return (psError);
660
}
661
662
psData[fillarcps].flag = yes; /* sets procedure flag */
663
fprintf(fp, "\t%s\t%d %d\t%d %d\t%d %d\t%d %d\tpsFillArc\n",
664
PSfindGC(gc), x, y, hght, wdth, ang1 / 64, ang2 / 64,
665
x + wdth / 2, y + hght / 2);
666
s = fclose(fp);
667
}
668
break;
669
default:
670
fprintf(stderr, "GFillArc request (%d) not implemented yet\n",
671
dFlag);
672
return (psError);
673
}
674
return (s);
675
}
676
677
/*
678
* Initializes the path and files to be used.
679
*/
680
681
int
682
PSGlobalInit(void)
683
{ /* This needs to be called only once each
684
* session. */
685
char *tmp;
686
687
/* path-independent global file name */
688
psData[GCdictps].flag = yes;
689
tmp = tempnam(NULL, "axPS");
690
sprintf(psData[GCdictps].filename, "%s", tmp);
691
free(tmp);
692
psData[setupps].flag = yes;
693
psData[scriptps].flag = yes;/* new script file name */
694
psData[endps].flag = yes;
695
696
/* path specific file names */
697
698
if ((envAXIOM = oa_getenv("DEVE")) != NULL) { /* get env var AXIOM */
699
700
psData[headerps].flag = yes;
701
sprintf(psData[headerps].filename, "%s%s", envAXIOM, "/Gdraws/PS/header.ps");
702
sprintf(psData[drawps].filename, "%s%s", envAXIOM, "/Gdraws/PS/draw.ps");
703
sprintf(psData[drawarcps].filename, "%s%s", envAXIOM,
704
"/Gdraws/PS/drawarc.ps");
705
sprintf(psData[drawfilledps].filename, "%s%s", envAXIOM,
706
"/Gdraws/PS/drwfilled.ps");
707
sprintf(psData[drawcolorps].filename, "%s%s", envAXIOM,
708
"/Gdraws/PS/drawcolor.ps");
709
sprintf(psData[fillpolyps].filename, "%s%s", envAXIOM,
710
"/Gdraws/PS/fillpoly.ps");
711
sprintf(psData[colorpolyps].filename, "%s%s", envAXIOM,
712
"/Gdraws/PS/colorpoly.ps");
713
sprintf(psData[fillwolps].filename, "%s%s", envAXIOM,
714
"/Gdraws/PS/fillwol.ps");
715
sprintf(psData[colorwolps].filename, "%s%s", envAXIOM,
716
"/Gdraws/PS/colorwol.ps");
717
sprintf(psData[drawpointps].filename, "%s%s", envAXIOM,
718
"/Gdraws/PS/drawpoint.ps");
719
sprintf(psData[drawlineps].filename, "%s%s", envAXIOM,
720
"/Gdraws/PS/drawline.ps");
721
sprintf(psData[drawlinesps].filename, "%s%s", envAXIOM,
722
"/Gdraws/PS/drawlines.ps");
723
sprintf(psData[drawrectps].filename, "%s%s", envAXIOM,
724
"/Gdraws/PS/drawrect.ps");
725
sprintf(psData[drawstrps].filename, "%s%s", envAXIOM,
726
"/Gdraws/PS/drawstr.ps");
727
sprintf(psData[drawIstrps].filename, "%s%s", envAXIOM,
728
"/Gdraws/PS/drawIstr.ps");
729
sprintf(psData[fillarcps].filename, "%s%s", envAXIOM,
730
"/Gdraws/PS/fillarc.ps");
731
sprintf(psData[setupps].filename, "%s%s", envAXIOM, "/Gdraws/PS/setup.ps");
732
sprintf(psData[endps].filename, "%s%s", envAXIOM, "/Gdraws/PS/end.ps");
733
}
734
else if ((envAXIOM = oa_getenv("AXIOM")) != NULL) {
735
psData[headerps].flag = yes;
736
sprintf(psData[headerps].filename, "%s%s", envAXIOM,
737
"/lib/graph/header.ps");
738
sprintf(psData[drawps].filename, "%s%s", envAXIOM,
739
"/lib/graph/draw.ps");
740
sprintf(psData[drawarcps].filename, "%s%s", envAXIOM,
741
"/lib/graph/drawarc.ps");
742
sprintf(psData[drawfilledps].filename, "%s%s", envAXIOM,
743
"/lib/graph/drwfilled.ps");
744
sprintf(psData[drawcolorps].filename, "%s%s", envAXIOM,
745
"/lib/graph/drawcolor.ps");
746
sprintf(psData[fillpolyps].filename, "%s%s", envAXIOM,
747
"/lib/graph/fillpoly.ps");
748
sprintf(psData[colorpolyps].filename, "%s%s", envAXIOM,
749
"/lib/graph/colorpoly.ps");
750
sprintf(psData[fillwolps].filename, "%s%s", envAXIOM,
751
"/lib/graph/fillwol.ps");
752
sprintf(psData[colorwolps].filename, "%s%s", envAXIOM,
753
"/lib/graph/colorwol.ps");
754
sprintf(psData[drawpointps].filename, "%s%s", envAXIOM,
755
"/lib/graph/drawpoint.ps");
756
sprintf(psData[drawlineps].filename, "%s%s", envAXIOM,
757
"/lib/graph/drawline.ps");
758
sprintf(psData[drawlinesps].filename, "%s%s", envAXIOM,
759
"/lib/graph/drawlines.ps");
760
sprintf(psData[drawrectps].filename, "%s%s", envAXIOM,
761
"/lib/graph/drawrect.ps");
762
sprintf(psData[drawstrps].filename, "%s%s", envAXIOM,
763
"/lib/graph/drawstr.ps");
764
sprintf(psData[drawIstrps].filename, "%s%s", envAXIOM,
765
"/lib/graph/drawIstr.ps");
766
sprintf(psData[fillarcps].filename, "%s%s", envAXIOM,
767
"/lib/graph/fillarc.ps");
768
sprintf(psData[setupps].filename, "%s%s", envAXIOM,
769
"/lib/graph/setup.ps");
770
sprintf(psData[endps].filename, "%s%s", envAXIOM,
771
"/lib/graph/end.ps");
772
}
773
else {
774
fprintf(stderr, " need environment variable AXIOM or DEVE; process aborted\n");
775
return (psError);
776
}
777
778
return (psInit = yes);
779
}
780
781
782
/*
783
* This needs to be called for every postscript file generated. It
784
* initializes the procedure flags.
785
*/
786
787
int
788
PSInit(Window vw, Window tw)
789
{
790
if (!psInit) {
791
/* must have PSGlobalInit() called before this */
792
fprintf(stderr, "Error: need initialization for ps data files: call PSGlobalInit().\n");
793
return (psError);
794
}
795
796
sprintf(psData[output].filename, "%s", PSfilename); /* output file name */
797
798
psData[drawps].flag = no; /* ps procedures flags */
799
psData[drawarcps].flag = no;
800
psData[drawfilledps].flag = no;
801
psData[drawcolorps].flag = no;
802
psData[fillpolyps].flag = no;
803
psData[fillwolps].flag = no;
804
psData[colorpolyps].flag = no;
805
psData[colorwolps].flag = no;
806
psData[drawpointps].flag = no;
807
psData[drawlineps].flag = no;
808
psData[drawlinesps].flag = no;
809
psData[drawrectps].flag = no;
810
psData[drawstrps].flag = no;
811
psData[drawIstrps].flag = no;
812
psData[fillarcps].flag = no;
813
814
sprintf(psData[scriptps].filename, "%s", tmpnam(NULL)); /* script file */
815
return (Gdraws_setDimension(vw, tw));
816
}
817
818
/*
819
* This procedure sets the line attributes; notice that lineWidth is not set
820
* for PS, this is because lineWidth of 0, the thinest line on the ps device,
821
* is device-dependent, thus, we'll leave it and use default. Also lineStyle
822
* is solid in ps by default, thus we don't need to set it. We'll leave out
823
* line style here since we really never used anything other than line solid
824
* which is the default line style in postscript.
825
*/
826
827
int
828
PSCreateContext(
829
GC gc, /* graphics context */
830
const char *C_gc, /* GC name to be used as postscript variable */
831
int lineWidth, int capStyle, int joinStyle,
832
float bg, float fg)
833
{
834
FILE *fp;
835
GCptr newGC, curGC;
836
837
/* get memory for new GC cell */
838
839
if (!(newGC = (GCptr) malloc(sizeof(GCstruct)))) {
840
fprintf(stderr, "Ran out of memory(malloc) trying to create a ps GC.\n");
841
exit(-1);
842
}
843
844
/* attach newGC to chain */
845
846
if (GChead == NULL)
847
GChead = newGC;
848
else { /* attach newGC to end of linked list */
849
curGC = GChead;
850
while (curGC->next != NULL)
851
curGC = curGC->next;
852
curGC->next = newGC;
853
}
854
855
/* fill newGC with information */
856
857
newGC->GCint = gc;
858
sprintf(newGC->GCchar, "%s", C_gc);
859
newGC->next = NULL;
860
861
if ((fp = fopen(psData[GCdictps].filename, "a")) == NULL) {
862
fprintf(stderr, "PSCreateContext cannot open %s\n",
863
psData[GCdictps].filename);
864
return (psError);
865
}
866
867
fprintf(fp, "\t%d\t%d\t%d\n\t%f\t%f\t/%s\tmakeDict\n", joinStyle,
868
capStyle, lineWidth, bg, fg, C_gc);
869
870
return (fclose(fp));
871
}
872
873
/*
874
* Looks into GC linked list with gc (unsigned long) as index to find the
875
* character name.
876
*/
877
878
char *
879
PSfindGC(GC gc)
880
{
881
GCptr curGC;
882
883
curGC = GChead;
884
while ((curGC != NULL) && (curGC->GCint != gc))
885
curGC = curGC->next;
886
887
if (curGC == NULL) {
888
fprintf(stderr, "PSfindGC cannot find gc: %p.\n",gc);
889
return (NULL);
890
}
891
else
892
return (curGC->GCchar);
893
}
894
895
/*
896
* Sets foreground color; see XSetForeground
897
*/
898
899
int
900
GSetForeground(
901
GC gc, /* graphics context */
902
float color, /* foreground color to be set */
903
int dFlag) /* display flag: PS, X,... */
904
{
905
int s = 0;
906
907
switch (dFlag) {
908
case Xoption:
909
XSetForeground(dsply, gc, (unsigned long) color);
910
break;
911
case PSoption:
912
{
913
FILE *fp;
914
915
if ((fp = fopen(psData[scriptps].filename, "a")) == NULL) {
916
fprintf(stderr, "GSetForeground cannot open %s\n",
917
psData[scriptps].filename);
918
return (0);
919
}
920
fprintf(fp, "\t%s\t%f\tsetForeground\n", PSfindGC(gc), color);
921
s = fclose(fp);
922
break;
923
}
924
default:
925
fprintf(stderr, "GSetForeground request (%d) not implemented yet\n", dFlag);
926
return (0);
927
}
928
return (s);
929
}
930
931
/*
932
* Sets background color; see XSetBackground
933
*/
934
935
int
936
GSetBackground(
937
GC gc, /* graphics context */
938
float color, /* background color to be set */
939
int dFlag) /* display flag: PS, X,... */
940
{
941
int s = 0;
942
943
switch (dFlag) {
944
case Xoption:
945
XSetBackground(dsply, gc, (unsigned long) color);
946
break;
947
case PSoption:
948
{
949
FILE *fp;
950
951
if ((fp = fopen(psData[scriptps].filename, "a")) == NULL) {
952
fprintf(stderr, "GSetBackground cannot open %s\n",
953
psData[scriptps].filename);
954
return (0);
955
}
956
fprintf(fp, "\t%s\t%f\tsetBackground\n", PSfindGC(gc), color);
957
s = fclose(fp);
958
break;
959
}
960
default:
961
fprintf(stderr, "GSetBackground request (%d) not implemented yet\n", dFlag);
962
return (0);
963
}
964
return (s);
965
}
966
967
/*
968
* See XSetLineAttributes. Notice that we'll not setting line style for
969
* postscript. This is because solid is the ls in ps and in view3D and 2D,
970
* we really don't use anything else than solid.
971
*/
972
973
int
974
GSetLineAttributes(
975
GC gc,
976
int lineWidth, int lineStyle, int capStyle, int joinStyle,
977
int dFlag)
978
{
979
int s = 0;
980
981
switch (dFlag) {
982
case Xoption:
983
XSetLineAttributes(dsply, gc, lineWidth, lineStyle,
984
capStyle, joinStyle);
985
break;
986
case PSoption:
987
{
988
FILE *fp;
989
int psCap, psJoin;
990
991
switch (capStyle) {
992
case 0: /* CapNotLast is not implemented in ps */
993
case 1:
994
psCap = psButtCap;
995
break;
996
case 2:
997
psCap = psRoundCap;
998
break;
999
case 3:
1000
psCap = psPSqCap;
1001
break;
1002
default:
1003
fprintf(stderr, "cap style: %d unknown, using default.\n", capStyle);
1004
psCap = psButtCap;
1005
}
1006
1007
switch (joinStyle) {
1008
case 0:
1009
psJoin = psMiterJoin;
1010
break;
1011
case 1:
1012
psJoin = psRoundJoin;
1013
break;
1014
case 2:
1015
psJoin = psBevelJoin;
1016
break;
1017
default:
1018
fprintf(stderr, "join style: %d unknown, using default.\n", joinStyle);
1019
psJoin = psMiterJoin;
1020
}
1021
1022
/*
1023
* width of zero is machine-dependent and is not recom- mended,
1024
* we'll use 1 as the thinest line available if (lineWidth < 1)
1025
* lineWidth = 1;
1026
*/
1027
1028
if ((fp = fopen(psData[scriptps].filename, "a")) == NULL) {
1029
fprintf(stderr, "GSetLineAttributes cannot open %s\n",
1030
psData[scriptps].filename);
1031
return (0);
1032
}
1033
1034
fprintf(fp, "\t%d\t%d\t%d\t%s\tsetLineAttributes\n",
1035
lineWidth, psCap, psJoin, PSfindGC(gc));
1036
s = fclose(fp);
1037
}
1038
break;
1039
default:
1040
fprintf(stderr, "GSetLineAttributes request (%d) not implemented yet\n", dFlag);
1041
return (0);
1042
}
1043
return (s);
1044
}
1045
1046
/*
1047
* This procedure frees the data structure used for GC information, and also
1048
* unlinks the GC dictionary file.
1049
*/
1050
1051
int
1052
PSClose(void)
1053
{
1054
if (GChead != NULL) {
1055
1056
/* free memory used by GC struct */
1057
1058
GCptr curGC = GChead;
1059
1060
while (curGC != NULL) {
1061
GCptr freeGC = curGC;
1062
1063
curGC = curGC->next;
1064
free(freeGC);
1065
}
1066
}
1067
1068
/* remove GC dictionary file */
1069
1070
return (unlink(psData[GCdictps].filename));
1071
}
1072
1073
1074
1075
1076
int
1077
centerX (GC viewGCx, const char* theString,int strlength,int windowWidth)
1078
{
1079
XFontStruct *fontStruct;
1080
GContext con;
1081
int result;
1082
1083
1084
con=XGContextFromGC(viewGCx);
1085
fontStruct = XQueryFont(dsply,con);
1086
if(fontStruct == NULL) return(0);
1087
result = (windowWidth - XTextWidth(fontStruct,theString,strlength))/2 -
1088
fontStruct->min_bounds.lbearing;
1089
XFreeFontInfo(NULL,fontStruct,1);
1090
return(result);
1091
}
1092
1093
1094
int
1095
centerY (GC viewGCy,int windowHeight)
1096
{
1097
1098
XFontStruct *fontStruct;
1099
GContext con;
1100
int result;
1101
1102
con=XGContextFromGC(viewGCy);
1103
fontStruct = XQueryFont(dsply,con);
1104
if (fontStruct == NULL) return(0);
1105
result = (windowHeight -
1106
(fontStruct->max_bounds.ascent + fontStruct->max_bounds.descent))/2 +
1107
fontStruct->max_bounds.ascent;
1108
XFreeFontInfo(NULL,fontStruct,1);
1109
return(result);
1110
1111
}
1112
1113
1114
/*
1115
* PSColorPolygon draws and fills a polygon given data in XPoint; see
1116
* XFillPolygon
1117
*/
1118
1119
int
1120
PSColorPolygon(
1121
float r, float g, float b, /* red, green and blue color
1122
* components */
1123
XPoint * points, /* vertices of polygon */
1124
int numberOfPoints) /* number of points */
1125
{
1126
int i = 0;
1127
FILE *fp;
1128
1129
if ((fp = fopen(psData[scriptps].filename, "a")) == NULL) {
1130
fprintf(stderr, "PSColorPolygon cannot open %s\n",
1131
psData[scriptps].filename);
1132
return (psError);
1133
}
1134
1135
psData[colorpolyps].flag = yes; /* sets procedure flag */
1136
1137
fprintf(fp, "\t%f\t%f\t%f\tsetrgbcolor\n", r, g, b);
1138
1139
i = numberOfPoints - 1;
1140
while (i > 0)
1141
{ fprintf(fp, "\t%d\t%d\n", points[i].x, points[i].y);
1142
i = i-1;
1143
}
1144
1145
fprintf(fp, "\t%d\t%d\t%d\tpsColorPoly\n", numberOfPoints, points[i].x, points[i].y);
1146
1147
return (fclose(fp));
1148
}
1149
1150
1151
/*
1152
* PSColorwOutline draws and also outlines the colored polygon.
1153
*/
1154
1155
int
1156
PSColorwOutline(
1157
float r, float g, float b, /* red, green and blue color
1158
* components */
1159
XPoint * points, /* vertices of polygon */
1160
int numberOfPoints) /* number of points */
1161
{
1162
int i = 0;
1163
FILE *fp;
1164
1165
if ((fp = fopen(psData[scriptps].filename, "a")) == NULL) {
1166
fprintf(stderr, "PSDrawFOL cannot open %s\n", psData[scriptps].filename);
1167
return (psError);
1168
}
1169
1170
psData[colorwolps].flag = yes; /* sets procedure flag */
1171
1172
fprintf(fp, "\t%f\t%f\t%f\tsetrgbcolor\n", r, g, b);
1173
1174
i = numberOfPoints - 1;
1175
while (i > 0)
1176
{ fprintf(fp, "\t%d\t%d\n", points[i].x, points[i].y);
1177
i = i-1;
1178
}
1179
1180
fprintf(fp, "\t%d\t%d\t%d\tpsFillwOutline\n",
1181
numberOfPoints, points[i].x, points[i].y);
1182
1183
return (fclose(fp));
1184
}
1185
/*
1186
* This function does what XDraw would do, notice that only a subset of
1187
* attributes in GC is implemented -- adequate for our purpose now
1188
*/
1189
1190
int
1191
PSDrawColor(
1192
float r, float g, float b, /* red, green and blue color
1193
* components */
1194
XPoint *points, /* point list */
1195
int numberOfPoints) /* vertex count and display flag (X, PS,...) */
1196
{
1197
int i = 0;
1198
FILE *fp;
1199
1200
if ((fp = fopen(psData[scriptps].filename, "a")) == NULL) {
1201
fprintf(stderr, "GDraw cannot open %s\n",
1202
psData[scriptps].filename);
1203
return (psError);
1204
}
1205
1206
psData[drawcolorps].flag = yes; /* set procedure flag */
1207
1208
fprintf(fp, "\t%f\t%f\t%f\tsetrgbcolor\n", r, g, b);
1209
1210
i = numberOfPoints - 1;
1211
while (i > 0)
1212
{ fprintf(fp, "\t%d\t%d\n", points[i].x, points[i].y);
1213
i=i-1;
1214
}
1215
1216
fprintf(fp, "\t%d\t%d\t%d\tpsDrawColor\n", numberOfPoints, points[i].x, points[i].y);
1217
1218
return (fclose(fp));
1219
}
1220
/*
1221
* PSFillPolygon draws and fills a polygon given data in XPoint; see
1222
* XFillPolygon.
1223
*/
1224
1225
int
1226
PSFillPolygon(
1227
GC gc, /* graphics context */
1228
XPoint * points, /* vertices of polygon */
1229
int numberOfPoints) /* number of points */
1230
{
1231
int i = 0;
1232
FILE *fp;
1233
1234
if ((fp = fopen(psData[scriptps].filename, "a")) == NULL) {
1235
fprintf(stderr, "PSFillPolygon cannot open %s\n",
1236
psData[scriptps].filename);
1237
return (psError);
1238
}
1239
1240
psData[fillpolyps].flag = yes; /* sets procedure flag */
1241
fprintf(fp, "\t%s\n", PSfindGC(gc));
1242
1243
i = numberOfPoints - 1;
1244
while (i > 0)
1245
{ fprintf(fp, "\t%d\t%d\n", points[i].x, points[i].y);
1246
i = i-1;
1247
}
1248
1249
fprintf(fp, "\t%d\t%d\t%d\tpsFillPoly\n", numberOfPoints, points[i].x, points[i].y);
1250
1251
return (fclose(fp));
1252
}
1253
1254
/*
1255
* PSFillwOutline draws and also outlines the filled polygon.
1256
*/
1257
1258
int
1259
PSFillwOutline(
1260
GC gc, /* graphics context */
1261
XPoint * points, /* vertices of polygon */
1262
int numberOfPoints)
1263
/* number of points */
1264
{
1265
int i = 0;
1266
FILE *fp;
1267
1268
if ((fp = fopen(psData[scriptps].filename, "a")) == NULL) {
1269
fprintf(stderr, "PSDrawFOL cannot open %s\n", psData[scriptps].filename);
1270
return (psError);
1271
}
1272
1273
psData[fillwolps].flag = yes; /* sets procedure flag */
1274
fprintf(fp, "\t%s\n", PSfindGC(gc));
1275
1276
i = numberOfPoints - 1;
1277
while (i > 0)
1278
{ fprintf(fp, "\t%d\t%d\n", points[i].x, points[i].y);
1279
i = i-1;
1280
}
1281
1282
fprintf(fp, "\t%d\t%d\t%d\tpsFillwOutline\n",
1283
numberOfPoints, points[i].x, points[i].y);
1284
1285
return (fclose(fp));
1286
}
1287
1288
static int
1289
TrivEqual(Window s1,Window s2)
1290
{
1291
return ( s1 == s2);
1292
}
1293
1294
static int
1295
TrivHash_code(Window s,int size)
1296
{
1297
return (s % size);
1298
}
1299
1300
1301
HashTable *
1302
XCreateAssocTable(int size)
1303
{
1304
HashTable * table;
1305
table = (HashTable *) malloc(sizeof(HashTable));
1306
hash_init(table,size,(EqualFunction)TrivEqual,(HashcodeFunction)TrivHash_code);
1307
return table;
1308
}
1309
1310
void
1311
XMakeAssoc(Display * dsp, HashTable *table, Window w, int * p)
1312
{
1313
hash_insert(table,(char *) p, (char *) w);
1314
}
1315
1316
int *
1317
XLookUpAssoc(Display * dsp, HashTable *table,Window w)
1318
{
1319
return (int *) hash_find(table,(char *)w);
1320
}
1321
1322
void
1323
XDeleteAssoc(Display * dsp,HashTable * table, Window w)
1324
{
1325
hash_delete(table,(char *) w);
1326
}
1327
1328
1329
1330
1331
1332
1333