1 | /*
|
---|
2 | * $Source: /tmp_mnt/n/fs/grad1/jsp/src/jgraph/RCS/draw.c,v $
|
---|
3 | * $Revision: 8.3 $
|
---|
4 | * $Date: 92/11/30 11:42:10 $
|
---|
5 | * $Author: jsp $
|
---|
6 | */
|
---|
7 |
|
---|
8 | #include "jgraph.h"
|
---|
9 | #include <stdio.h>
|
---|
10 | #include <math.h>
|
---|
11 | #include <stdlib.h> //by pzn@debian.org
|
---|
12 | #include <string.h> //by pzn@debian.org
|
---|
13 |
|
---|
14 | static char real_eof = EOF;
|
---|
15 |
|
---|
16 | float ctop(val, axis)
|
---|
17 | float val;
|
---|
18 | Axis axis;
|
---|
19 | {
|
---|
20 | if (axis->is_lg) {
|
---|
21 | if (val <= 0.0) {
|
---|
22 | error_header();
|
---|
23 | fprintf(stderr,
|
---|
24 | "Value of %f is at negative infinity with logrhythmic %c axis\n",
|
---|
25 | val, (axis->is_x) ? 'x' : 'y');
|
---|
26 | exit(1);
|
---|
27 | }
|
---|
28 | return (log(val) / axis->logfactor - axis->logmin) * axis->factor;
|
---|
29 | } else {
|
---|
30 | return (val - axis->min) * axis->factor;
|
---|
31 | }
|
---|
32 | }
|
---|
33 |
|
---|
34 | float disttop(val, axis)
|
---|
35 | float val;
|
---|
36 | Axis axis;
|
---|
37 | {
|
---|
38 | if (axis->is_lg) {
|
---|
39 | return FCPI * val;
|
---|
40 | } else {
|
---|
41 | return (val) * axis->factor;
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | float intop(val)
|
---|
46 | float val;
|
---|
47 | {
|
---|
48 | return FCPI * val;
|
---|
49 | }
|
---|
50 |
|
---|
51 | #define MAXIMUM(a,b) ((a > b) ? a : b)
|
---|
52 |
|
---|
53 | draw_axis(a, other)
|
---|
54 | Axis a, other;
|
---|
55 | {
|
---|
56 | char orientation;
|
---|
57 | Hash h;
|
---|
58 | String s;
|
---|
59 |
|
---|
60 | orientation = (a->is_x) ? 'x' : 'y';
|
---|
61 | setlinewidth(1.0);
|
---|
62 | comment("Drawing Axis");
|
---|
63 | if (a->grid_lines) {
|
---|
64 | comment("Drawing Grid lines");
|
---|
65 | gsave();
|
---|
66 | setgray(a->gr_graytype, a->gr_gray);
|
---|
67 | for (h = first(a->hash_lines); h != nil(a->hash_lines); h = next(h)) {
|
---|
68 | if (h->major) {
|
---|
69 | printline(h->loc, 0.0, h->loc, other->psize, orientation);
|
---|
70 | }
|
---|
71 | }
|
---|
72 | grestore();
|
---|
73 | }
|
---|
74 | if (a->mgrid_lines) {
|
---|
75 | comment("Drawing Minor Grid lines");
|
---|
76 | gsave();
|
---|
77 | setgray(a->mgr_graytype, a->mgr_gray);
|
---|
78 | for (h = first(a->hash_lines); h != nil(a->hash_lines); h = next(h)) {
|
---|
79 | if (!h->major) {
|
---|
80 | printline(h->loc, 0.0, h->loc, other->psize, orientation);
|
---|
81 | }
|
---|
82 | }
|
---|
83 | grestore();
|
---|
84 | }
|
---|
85 | gsave();
|
---|
86 | setgray(a->graytype, a->gray);
|
---|
87 | if (a->draw_axis_line) {
|
---|
88 | printline(0.0, a->draw_at, a->psize, a->draw_at, orientation);
|
---|
89 | }
|
---|
90 | if (a->draw_hash_marks) {
|
---|
91 | comment("Drawing Hash Marks");
|
---|
92 | for (h = first(a->hash_lines); h != nil(a->hash_lines); h = next(h)) {
|
---|
93 | printline(h->loc, a->draw_hash_marks_at, h->loc,
|
---|
94 | a->draw_hash_marks_at + (h->size * a->hash_scale),
|
---|
95 | orientation);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | if (a->draw_hash_labels) {
|
---|
99 | comment("Drawing Hash Labels");
|
---|
100 | for (s = first(a->hash_labels); s != nil(a->hash_labels); s = next(s)) {
|
---|
101 | a->hl->label = s->s->label;
|
---|
102 | if (a->is_x) {
|
---|
103 | a->hl->x = s->s->x;
|
---|
104 | } else {
|
---|
105 | a->hl->y = s->s->y;
|
---|
106 | }
|
---|
107 | draw_label(a->hl);
|
---|
108 | }
|
---|
109 | }
|
---|
110 | if (a->draw_axis_label) {
|
---|
111 | comment("Drawing Axis Label");
|
---|
112 | draw_label(a->label);
|
---|
113 | }
|
---|
114 | grestore();
|
---|
115 | printf("\n");
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | draw_label(l)
|
---|
120 | Label l;
|
---|
121 | {
|
---|
122 | if (l->label == CNULL) return;
|
---|
123 | comment(l->label);
|
---|
124 | print_label(l);
|
---|
125 | }
|
---|
126 |
|
---|
127 | set_clip(g)
|
---|
128 | Graph g;
|
---|
129 | {
|
---|
130 | comment("Setting Clip");
|
---|
131 | printf("newpath\n");
|
---|
132 | printf(" 0 0 moveto 0 %f lineto %f %f lineto %f 0 lineto\n",
|
---|
133 | g->y_axis->psize, g->x_axis->psize,
|
---|
134 | g->y_axis->psize, g->x_axis->psize);
|
---|
135 | printf(" closepath clip newpath\n");
|
---|
136 | }
|
---|
137 |
|
---|
138 | draw_curves(g)
|
---|
139 | Graph g;
|
---|
140 | {
|
---|
141 | Curve c;
|
---|
142 |
|
---|
143 | gsave();
|
---|
144 | printf("\n");
|
---|
145 | if (g->clip) set_clip(g);
|
---|
146 | for(c = first(g->curves); c != nil(g->curves); c = next(c)) {
|
---|
147 | draw_curve(c, g);
|
---|
148 | }
|
---|
149 | grestore();
|
---|
150 | printf("\n");
|
---|
151 | }
|
---|
152 |
|
---|
153 | draw_curve(c, g)
|
---|
154 | Curve c;
|
---|
155 | Graph g;
|
---|
156 | {
|
---|
157 | Point p, px, py;
|
---|
158 | int i, j;
|
---|
159 | float this_x, this_y, last_x, last_y, x, y;
|
---|
160 |
|
---|
161 | gsave();
|
---|
162 | setgray(c->graytype, c->gray);
|
---|
163 | if (c->clip) set_clip(g);
|
---|
164 | if (first(c->xepts) != nil(c->xepts) ||
|
---|
165 | first(c->yepts) != nil(c->yepts)) {
|
---|
166 | comment("Drawing Epts");
|
---|
167 | px = first(c->xepts);
|
---|
168 | py = first(c->yepts);
|
---|
169 | setlinewidth(c->linethick);
|
---|
170 | setlinestyle('s', (Flist)0);
|
---|
171 | for (p = first(c->pts); p != nil(c->pts); p = next(p)) {
|
---|
172 | if (p->e == 'x') {
|
---|
173 | x = ctop(p->x, g->x_axis);
|
---|
174 | y = ctop(p->y, g->y_axis);
|
---|
175 | print_ebar(x, y, ctop(px->x, g->x_axis), c->marksize[1]/2.0, 'x');
|
---|
176 | px = next(px);
|
---|
177 | print_ebar(x, y, ctop(px->x, g->x_axis), c->marksize[1]/2.0, 'x');
|
---|
178 | px = next(px);
|
---|
179 | } else if (p->e == 'y') {
|
---|
180 | x = ctop(p->x, g->x_axis);
|
---|
181 | y = ctop(p->y, g->y_axis);
|
---|
182 | print_ebar(y, x, ctop(py->y, g->y_axis), c->marksize[0]/2.0, 'y');
|
---|
183 | py = next(py);
|
---|
184 | print_ebar(y, x, ctop(py->y, g->y_axis), c->marksize[0]/2.0, 'y');
|
---|
185 | py = next(py);
|
---|
186 | }
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | comment("Drawing Curve");
|
---|
191 | if (c->linetype != '0' || c->poly) {
|
---|
192 | if (c->bezier) {
|
---|
193 | i = 0;
|
---|
194 | j = 0;
|
---|
195 | if (c->poly) printf("newpath ");
|
---|
196 | for (p = first(c->pts); p != nil(c->pts); p = next(p)) {
|
---|
197 | if (j == 0 && i == 0) {
|
---|
198 | start_line(ctop(p->x, g->x_axis), ctop(p->y, g->y_axis), c);
|
---|
199 | j++;
|
---|
200 | } else if (i != 0) {
|
---|
201 | bezier_control(ctop(p->x, g->x_axis), ctop(p->y, g->y_axis));
|
---|
202 | } else {
|
---|
203 | bezier_end(ctop(p->x, g->x_axis), ctop(p->y, g->y_axis));
|
---|
204 | j++;
|
---|
205 | }
|
---|
206 | if (!c->poly && j == 30 && i == 0) {
|
---|
207 | end_line();
|
---|
208 | p = prev(p);
|
---|
209 | j = 0;
|
---|
210 | i = 0;
|
---|
211 | } else i = (i + 1) % 3;
|
---|
212 | }
|
---|
213 | if (j != 0) {
|
---|
214 | if (c->poly) {
|
---|
215 | printf("closepath ");
|
---|
216 | setfill(0.0, 0.0, c->pfilltype, c->pfill, c->ppattern, c->pparg);
|
---|
217 | }
|
---|
218 | end_line();
|
---|
219 | }
|
---|
220 | } else {
|
---|
221 | i = 0;
|
---|
222 | if (c->poly) printf("newpath ");
|
---|
223 | for (p = first(c->pts);
|
---|
224 | p != nil(c->pts);
|
---|
225 | p = next(p)) {
|
---|
226 | if (i == 0) {
|
---|
227 | start_line(ctop(p->x, g->x_axis), ctop(p->y, g->y_axis), c);
|
---|
228 | } else {
|
---|
229 | cont_line(ctop(p->x, g->x_axis), ctop(p->y, g->y_axis));
|
---|
230 | }
|
---|
231 | if (!c->poly && i == 100 && next(p)) {
|
---|
232 | end_line();
|
---|
233 | p = prev(p);
|
---|
234 | i = 0;
|
---|
235 | } else i++;
|
---|
236 | }
|
---|
237 | if (i != 0) {
|
---|
238 | if (c->poly) {
|
---|
239 | printf("closepath ");
|
---|
240 | setfill(0.0, 0.0, c->pfilltype, c->pfill, c->ppattern, c->pparg);
|
---|
241 | }
|
---|
242 | end_line();
|
---|
243 | }
|
---|
244 | }
|
---|
245 | }
|
---|
246 | comment("Drawing Curve points");
|
---|
247 | i = 0;
|
---|
248 | for (p = first(c->pts);
|
---|
249 | p != nil(c->pts);
|
---|
250 | p = next(p)) {
|
---|
251 | this_x = ctop(p->x, g->x_axis);
|
---|
252 | this_y = ctop(p->y, g->y_axis);
|
---|
253 | if (!c->bezier || i == 0) draw_mark(this_x, this_y, c, g);
|
---|
254 | if (p != first(c->pts)) {
|
---|
255 | if (c->rarrows || (c->rarrow && p == last(c->pts))) {
|
---|
256 | if (!c->bezier || i == 0)
|
---|
257 | draw_arrow(this_x, this_y, last_x, last_y, c);
|
---|
258 | }
|
---|
259 | if (c->larrows || (c->larrow && prev(p) == first(c->pts))) {
|
---|
260 | if (!c->bezier || i == 1)
|
---|
261 | draw_arrow(last_x, last_y, this_x, this_y, c);
|
---|
262 | }
|
---|
263 | }
|
---|
264 | last_x = this_x;
|
---|
265 | last_y = this_y;
|
---|
266 | i = (i + 1) % 3;
|
---|
267 | }
|
---|
268 | grestore();
|
---|
269 | printf("\n");
|
---|
270 | }
|
---|
271 |
|
---|
272 | draw_mark(x, y, c, g)
|
---|
273 | float x, y;
|
---|
274 | Curve c;
|
---|
275 | Graph g;
|
---|
276 | {
|
---|
277 | Point p;
|
---|
278 | float ms0, ms1, scx, scy, trx, try;
|
---|
279 | int i, j;
|
---|
280 | FILE *f;
|
---|
281 | char ch;
|
---|
282 | int done, newline;
|
---|
283 | char inp[100];
|
---|
284 | int bb[4];
|
---|
285 |
|
---|
286 | if (c->marktype == 'n') return;
|
---|
287 | ms0 = c->marksize[0] / 2.0;
|
---|
288 | ms1 = c->marksize[1] / 2.0;
|
---|
289 |
|
---|
290 | gsave();
|
---|
291 | printf(" %f %f translate %f rotate\n", x, y, c->mrotate);
|
---|
292 |
|
---|
293 | switch (c->marktype) {
|
---|
294 | case 'n': break;
|
---|
295 | case 'E': if (c->eps == CNULL) break;
|
---|
296 | f = fopen(c->eps, "r");
|
---|
297 | if (f == NULL) {
|
---|
298 | fprintf(stderr, "Error: eps file %s couldn't be opened\n",
|
---|
299 | c->eps);
|
---|
300 | exit(1);
|
---|
301 | }
|
---|
302 | /* Get bbox */
|
---|
303 | newline = 1;
|
---|
304 | done = 0;
|
---|
305 | while(!done) {
|
---|
306 | while(!newline) {
|
---|
307 | ch = getc(f);
|
---|
308 | if (ch == real_eof) {
|
---|
309 | fprintf(stderr, "Error: Eps file '%s' has %s\n",
|
---|
310 | c->eps, "no bounding box");
|
---|
311 | exit(1);
|
---|
312 | }
|
---|
313 | newline = (ch == '\n');
|
---|
314 | }
|
---|
315 | fscanf(f, "%s", inp);
|
---|
316 | if (strcmp(inp, "%%BoundingBox:") == 0) done = 1;
|
---|
317 | }
|
---|
318 | for (i = 0; i < 4; i++) {
|
---|
319 | if (fscanf(f, "%d", &(bb[i])) == NULL) {
|
---|
320 | fprintf(stderr, "Error: Eps file '%s': eof in %s\n",
|
---|
321 | c->eps, "bounding box");
|
---|
322 | exit(1);
|
---|
323 | }
|
---|
324 | }
|
---|
325 | if (bb[2] - bb[0] == 0) {
|
---|
326 | scx = ms0;
|
---|
327 | trx = 0.0;
|
---|
328 | } else {
|
---|
329 | scx = ms0 * 2.0/(float)(bb[2] - bb[0]);
|
---|
330 | trx = -(float)(bb[2] - bb[0])/2.0 - bb[0];
|
---|
331 | }
|
---|
332 | if (bb[3] - bb[1] == 0) {
|
---|
333 | scy = ms1;
|
---|
334 | try = 0.0;
|
---|
335 | } else {
|
---|
336 | scy = ms1 * 2.0/(float)(bb[3] - bb[1]);
|
---|
337 | try = -(float)(bb[3] - bb[1])/2.0 - bb[1];
|
---|
338 | }
|
---|
339 | /* Don't scale if ms == 0 0 */
|
---|
340 | if (ms0 == 0.0 && ms1 == 0.0) {
|
---|
341 | scx = 1.0;
|
---|
342 | scy = 1.0;
|
---|
343 | }
|
---|
344 |
|
---|
345 | sprintf(inp, "Including eps file %s", c->eps);
|
---|
346 | comment(inp);
|
---|
347 | /* Use bbox to scale and translate */
|
---|
348 |
|
---|
349 | printf("%f %f scale %f %f translate\n", scx, scy, trx, try);
|
---|
350 | /* Include the rest of the file */
|
---|
351 | for (ch = getc(f); ch != real_eof; ch = getc(f)) putchar(ch);
|
---|
352 | putchar('\n');
|
---|
353 | fclose(f);
|
---|
354 | break;
|
---|
355 | case 'p': if (c->postscript == CNULL) break;
|
---|
356 | if (ms0 != 0.0 || ms1 != 0.0) {
|
---|
357 | printf("%f %f scale\n", ms0, ms1);
|
---|
358 | }
|
---|
359 | if (!c->postfile) {
|
---|
360 | printf("%s\n", c->postscript);
|
---|
361 | } else {
|
---|
362 | f = fopen(c->postscript, "r");
|
---|
363 | if (f == NULL) {
|
---|
364 | fprintf(stderr,
|
---|
365 | "Error: postscript file %s couldn't be opened\n",
|
---|
366 | c->postscript);
|
---|
367 | exit(1);
|
---|
368 | }
|
---|
369 | for (ch = getc(f); ch != real_eof; ch = getc(f)) putchar(ch);
|
---|
370 | putchar('\n');
|
---|
371 | fclose(f);
|
---|
372 | }
|
---|
373 | break;
|
---|
374 | case 'c': printline(-ms0, 0.0, ms0, 0.0, 'x');
|
---|
375 | printline(-ms1, 0.0, ms1, 0.0, 'y');
|
---|
376 | break;
|
---|
377 | case 'b': start_poly(-ms0, -ms1);
|
---|
378 | cont_poly(ms0, -ms1);
|
---|
379 | cont_poly(ms0, ms1);
|
---|
380 | cont_poly(-ms0, ms1);
|
---|
381 | end_poly(x, y, c->filltype, c->fill, c->pattern, c->parg);
|
---|
382 | break;
|
---|
383 | case 'd': start_poly(-ms0, 0.0);
|
---|
384 | cont_poly(0.0, -ms1);
|
---|
385 | cont_poly(ms0, 0.0);
|
---|
386 | cont_poly(0.0, ms1);
|
---|
387 | end_poly(x, y, c->filltype, c->fill, c->pattern, c->parg);
|
---|
388 | break;
|
---|
389 | case 'g': p = first(c->general_marks);
|
---|
390 | if (p == nil(c->general_marks)) break;
|
---|
391 | if (next(p) == nil(c->general_marks)) break;
|
---|
392 | start_poly(p->x*ms0, p->y*ms1);
|
---|
393 | for(p = next(p); p != nil(c->general_marks); p = next(p))
|
---|
394 | cont_poly(p->x*ms0, p->y*ms1);
|
---|
395 | end_poly(x, y, c->filltype, c->fill, c->pattern, c->parg);
|
---|
396 | break;
|
---|
397 | case 'G': i = 0;
|
---|
398 | for (p = first(c->general_marks);
|
---|
399 | p != nil(c->general_marks);
|
---|
400 | p = next(p)) {
|
---|
401 | if (i == 0) {
|
---|
402 | printf("%f %f moveto ", p->x*ms0, p->y*ms1);
|
---|
403 | } else {
|
---|
404 | printf("%f %f lineto\n", p->x*ms0, p->y*ms1);
|
---|
405 | }
|
---|
406 | if (i == 100) {
|
---|
407 | printf("stroke\n");
|
---|
408 | p = prev(p);
|
---|
409 | i = 0;
|
---|
410 | } else i++;
|
---|
411 | }
|
---|
412 | if (i != 0) printf("stroke\n");
|
---|
413 | break;
|
---|
414 | case 'B': i = 0;
|
---|
415 | j = 0;
|
---|
416 | for (p = first(c->general_marks);
|
---|
417 | p != nil(c->general_marks);
|
---|
418 | p = next(p)) {
|
---|
419 | if (j == 0 && i == 0) {
|
---|
420 | printf("%f %f moveto ", p->x*ms0, p->y*ms1);
|
---|
421 | j++;
|
---|
422 | } else if (i != 0) {
|
---|
423 | printf("%f %f ", p->x*ms0, p->y*ms1);
|
---|
424 | } else {
|
---|
425 | printf("%f %f curveto\n", p->x*ms0, p->y*ms1);
|
---|
426 | j++;
|
---|
427 | }
|
---|
428 | if (j == 30 && i == 0) {
|
---|
429 | printf(" stroke\n");
|
---|
430 | p = prev(p);
|
---|
431 | j = 0;
|
---|
432 | i = 0;
|
---|
433 | } else i = (i + 1) % 3;
|
---|
434 | }
|
---|
435 | if (j != 0) printf(" stroke\n");
|
---|
436 | if (! ((i == 1) || (i == 0 && j == 0))) {
|
---|
437 | fprintf(stderr, "Error: curve %d, %s\n", c->num,
|
---|
438 | "wrong number of points for bezier marktype\n");
|
---|
439 | exit(1);
|
---|
440 | }
|
---|
441 | break;
|
---|
442 |
|
---|
443 | case 'Z': i = 0;
|
---|
444 | j = 0;
|
---|
445 | for (p = first(c->general_marks);
|
---|
446 | p != nil(c->general_marks);
|
---|
447 | p = next(p)) {
|
---|
448 | if (i == 0 && j == 0) {
|
---|
449 | printf("newpath %f %f moveto ", p->x*ms0, p->y*ms1);
|
---|
450 | j++;
|
---|
451 | } else if (i != 0) {
|
---|
452 | printf("%f %f ", p->x*ms0, p->y*ms1);
|
---|
453 | } else {
|
---|
454 | printf("%f %f curveto\n", p->x*ms0, p->y*ms1);
|
---|
455 | }
|
---|
456 | i = (i + 1) % 3;
|
---|
457 | }
|
---|
458 | printf("closepath ");
|
---|
459 | setfill(x, y, c->filltype, c->fill, c->pattern, c->parg);
|
---|
460 | printf("stroke\n");
|
---|
461 |
|
---|
462 | if (i != 1) {
|
---|
463 | fprintf(stderr, "Error: curve %d, %s\n", c->num,
|
---|
464 | "wrong number of points for bezier marktype\n");
|
---|
465 | exit(1);
|
---|
466 | }
|
---|
467 | break;
|
---|
468 |
|
---|
469 | case 'x': printline(-ms0, -ms1, ms0, ms1, 'x');
|
---|
470 | printline(-ms0, ms1, ms0, -ms1, 'x');
|
---|
471 | break;
|
---|
472 | case 'o': printellipse(x, y, ms0, ms0,
|
---|
473 | c->filltype, c->fill, c->pattern, c->parg);
|
---|
474 | break;
|
---|
475 | case 'e': printellipse(x, y, ms0, ms1,
|
---|
476 | c->filltype, c->fill, c->pattern, c->parg);
|
---|
477 | break;
|
---|
478 | case 't': start_poly(ms0, -ms1);
|
---|
479 | cont_poly(0.0, ms1);
|
---|
480 | cont_poly(-ms0, -ms1);
|
---|
481 | end_poly(x, y, c->filltype, c->fill, c->pattern, c->parg);
|
---|
482 | break;
|
---|
483 | case 'X': start_poly(ms0, 0.0);
|
---|
484 | cont_poly(-ms0, 0.0);
|
---|
485 | cont_poly(-ms0, g->x_axis->draw_at - y);
|
---|
486 | cont_poly(ms0, g->x_axis->draw_at - y);
|
---|
487 | end_poly(x, y, c->filltype, c->fill, c->pattern, c->parg);
|
---|
488 | break;
|
---|
489 | case 'Y': start_poly(0.0, ms1);
|
---|
490 | cont_poly(0.0, -ms1);
|
---|
491 | cont_poly(g->y_axis->draw_at - x, -ms1);
|
---|
492 | cont_poly(g->y_axis->draw_at - x, ms1);
|
---|
493 | end_poly(x, y, c->filltype, c->fill, c->pattern, c->parg);
|
---|
494 | break;
|
---|
495 | case 'l': draw_label(c->lmark);
|
---|
496 | break;
|
---|
497 | default: error_header();
|
---|
498 | fprintf(stderr, "Unknown mark: %c\n", c->marktype);
|
---|
499 | break;
|
---|
500 | }
|
---|
501 | grestore();
|
---|
502 | }
|
---|
503 |
|
---|
504 | draw_arrow(x1, y1, x2, y2, c)
|
---|
505 | float x1, y1, x2, y2;
|
---|
506 | Curve c;
|
---|
507 | {
|
---|
508 | float dx, dy;
|
---|
509 | float ms0;
|
---|
510 | float theta, ct, st;
|
---|
511 |
|
---|
512 |
|
---|
513 | if (c->marktype == 'o') {
|
---|
514 | dx = x1 - x2;
|
---|
515 | dy = y1 - y2;
|
---|
516 | if (dx == 0.0 && dy == 0.0) return;
|
---|
517 |
|
---|
518 | ms0 = c->marksize[0] / 2.0;
|
---|
519 | if (dx == 0.0) theta = asin(1.0); else theta = atan(dy/dx);
|
---|
520 | if (theta < 0.0) theta = -theta;
|
---|
521 | ct = cos(theta)*ms0;
|
---|
522 | st = sin(theta)*ms0;
|
---|
523 | x1 = x1 + ct*(dx > 0.0 ? -1.0 : 1.0);
|
---|
524 | y1 = y1 + st*(dy > 0.0 ? -1.0 : 1.0);
|
---|
525 |
|
---|
526 | if ( ((x1 - x2 > 0) != (dx > 0)) ||
|
---|
527 | ((y1 - y2 > 0) != (dy > 0)) ) return;
|
---|
528 | }
|
---|
529 |
|
---|
530 | dx = x1 - x2;
|
---|
531 | dy = y1 - y2;
|
---|
532 | if (dx == 0.0 && dy == 0.0) return;
|
---|
533 |
|
---|
534 | gsave();
|
---|
535 | printf("%f %f translate %f %f atan rotate\n", x1, y1, dy, dx);
|
---|
536 | start_poly(0.0, 0.0);
|
---|
537 | cont_poly(-(c->asize[0]), (c->asize[1]));
|
---|
538 | cont_poly(-(c->asize[0]), -(c->asize[1]));
|
---|
539 | end_poly(0.0, 0.0, c->afilltype, c->afill, c->apattern, c->aparg);
|
---|
540 | grestore();
|
---|
541 | printf("\n");
|
---|
542 | }
|
---|
543 |
|
---|
544 | draw_legend(g)
|
---|
545 | Graph g;
|
---|
546 | {
|
---|
547 | Curve c;
|
---|
548 | Legend l;
|
---|
549 | float x, y;
|
---|
550 | char tmpmktype;
|
---|
551 |
|
---|
552 | l = g->legend;
|
---|
553 | comment("Drawing legend");
|
---|
554 | if (l->type == 'n' || l->anylines < 0) return;
|
---|
555 | gsave();
|
---|
556 | if (l->type == 'u') {
|
---|
557 | printf("%f %f translate %f rotate\n", l->l->x, l->l->y, l->l->rotate);
|
---|
558 | }
|
---|
559 | for (c = first(g->curves); c != nil(g->curves); c = next(c)) {
|
---|
560 | if (c->l->label != CNULL) {
|
---|
561 | gsave();
|
---|
562 | setgray(c->graytype, c->gray);
|
---|
563 | y = (c->l->ymax + c->l->ymin) / 2.0;
|
---|
564 | if (l->anylines) {
|
---|
565 | if (c->linetype != '0' && l->linelength != 0) {
|
---|
566 | if (l->type == 'c' && c->l->hj == 'r') {
|
---|
567 | x = c->l->x + l->midspace;
|
---|
568 | } else {
|
---|
569 | x = c->l->x - l->midspace - l->linelength;
|
---|
570 | }
|
---|
571 | start_line(x, y, c);
|
---|
572 | cont_line(x+l->linelength, y);
|
---|
573 | end_line();
|
---|
574 | }
|
---|
575 | tmpmktype = c->marktype;
|
---|
576 | c->marktype = 'n';
|
---|
577 | if (c->larrows || c->larrow) draw_arrow(x, y, x+l->linelength, y, c);
|
---|
578 | if (c->rarrows || c->rarrow) draw_arrow(x+l->linelength, y, x, y, c);
|
---|
579 | c->marktype = tmpmktype;
|
---|
580 | if (l->type == 'c' && c->l->hj == 'r') {
|
---|
581 | x = c->l->x + l->midspace + l->linelength / 2.0;
|
---|
582 | } else {
|
---|
583 | x = c->l->x - l->midspace - l->linelength / 2.0;
|
---|
584 | }
|
---|
585 | } else if (l->type == 'c' && c->l->hj == 'r') {
|
---|
586 | x = c->l->x + l->midspace;
|
---|
587 | } else {
|
---|
588 | x = c->l->x - l->midspace;
|
---|
589 | }
|
---|
590 | if (c->marktype == 'X' || c->marktype == 'Y') {
|
---|
591 | char old;
|
---|
592 | old = c->marktype;
|
---|
593 | c->marktype = 'b';
|
---|
594 | draw_mark(x, y, c, g);
|
---|
595 | c->marktype = old;
|
---|
596 | } else {
|
---|
597 | draw_mark(x, y, c, g);
|
---|
598 | }
|
---|
599 | grestore();
|
---|
600 | printf("\n");
|
---|
601 | draw_label(c->l);
|
---|
602 | }
|
---|
603 | }
|
---|
604 | grestore();
|
---|
605 | printf("\n");
|
---|
606 | }
|
---|
607 |
|
---|
608 | draw_strings(g)
|
---|
609 | Graph g;
|
---|
610 | {
|
---|
611 | String s;
|
---|
612 |
|
---|
613 | comment("Drawing strings");
|
---|
614 | for (s = first(g->strings); s != nil(g->strings); s = next(s))
|
---|
615 | draw_label(s->s);
|
---|
616 | }
|
---|
617 |
|
---|
618 | draw_graph(g)
|
---|
619 | Graph g;
|
---|
620 | {
|
---|
621 | comment("Drawing New Graph");
|
---|
622 | printf("%f %f translate\n", g->x_translate, g->y_translate);
|
---|
623 | if (g->border) {
|
---|
624 | printline(0.0, 0.0, 0.0, g->y_axis->psize, 'x');
|
---|
625 | printline(0.0, 0.0, 0.0, g->x_axis->psize, 'y');
|
---|
626 | printline(g->x_axis->psize, 0.0, g->x_axis->psize, g->y_axis->psize, 'x');
|
---|
627 | printline(g->y_axis->psize, 0.0, g->y_axis->psize, g->x_axis->psize, 'y');
|
---|
628 | }
|
---|
629 | draw_axis(g->x_axis, g->y_axis);
|
---|
630 | draw_axis(g->y_axis, g->x_axis);
|
---|
631 | draw_label(g->title);
|
---|
632 | draw_curves(g);
|
---|
633 | draw_legend(g);
|
---|
634 | draw_strings(g);
|
---|
635 | printf("%f %f translate\n", - g->x_translate, - g->y_translate);
|
---|
636 |
|
---|
637 | }
|
---|
638 |
|
---|
639 | draw_graphs(gs, pp, landscape)
|
---|
640 | Graphs gs;
|
---|
641 | int pp;
|
---|
642 | int landscape;
|
---|
643 | {
|
---|
644 | Graphs gs_p;
|
---|
645 | Graph g;
|
---|
646 | int page_counter=0;
|
---|
647 |
|
---|
648 | for (gs_p = first(gs); gs_p != nil(gs); gs_p = next(gs_p)) {
|
---|
649 | page_counter++;
|
---|
650 | draw_header(gs_p, pp, landscape);
|
---|
651 | for (g = first(gs_p->g); g != nil(gs_p->g); g = next(g)) {
|
---|
652 | draw_graph(g);
|
---|
653 | }
|
---|
654 | draw_footer(gs_p, pp);
|
---|
655 | }
|
---|
656 | if (pp) {
|
---|
657 | printf("\n%%%%Trailer\n");
|
---|
658 | printf("%%%%Pages: %i\n",page_counter);
|
---|
659 | printf("%%%%EOF\n");
|
---|
660 | }
|
---|
661 | }
|
---|
662 |
|
---|
663 | draw_header(gs, pp, landscape)
|
---|
664 | Graphs gs;
|
---|
665 | int pp;
|
---|
666 | int landscape;
|
---|
667 | {
|
---|
668 | FILE *f;
|
---|
669 | char c;
|
---|
670 |
|
---|
671 | if (gs->page == 1) {
|
---|
672 | if (pp) {
|
---|
673 | printf("%%!PS-Adobe-3.0\n");
|
---|
674 | printf("%%%%Pages: (atend)\n");
|
---|
675 | printf("%%%%Creator: jgraph\n");
|
---|
676 | printf("%%%%EndComments\n\n");
|
---|
677 | printf("%%%%BeginProlog\n");
|
---|
678 | printf("%%%%EndProlog\n");
|
---|
679 | printf("%%%%BeginSetup\n");
|
---|
680 | printf("%%%%EndSetup\n");
|
---|
681 | } else {
|
---|
682 | printf("%%!PS-Adobe-2.0 EPSF-1.2\n");
|
---|
683 | printf("%%%%Creator: jgraph\n");
|
---|
684 | }
|
---|
685 | }
|
---|
686 |
|
---|
687 | if (pp) {
|
---|
688 | printf("\n%%%%Page: %d %d\n", gs->page, gs->page);
|
---|
689 | printf("%%%%BeginPageSetup\n");
|
---|
690 | printf("%%%%EndPageSetup\n\n");
|
---|
691 | }
|
---|
692 |
|
---|
693 | if ( (gs->page>1) && !pp ) {
|
---|
694 | /* tring EPS with more than one page */
|
---|
695 | fprintf(stderr, "Error: 'newpage' token is not allowed"
|
---|
696 | " without '-P' command line option.\n");
|
---|
697 | exit(1);
|
---|
698 | }
|
---|
699 |
|
---|
700 | /* if (landscape) {
|
---|
701 | printf("%%%%BoundingBox: %d %d %d %d\n", gs->bb[1], gs->bb[0],
|
---|
702 | gs->bb[3], gs->bb[2]);
|
---|
703 | } else {
|
---|
704 | printf("%%%%BoundingBox: %d %d %d %d\n", gs->bb[0], gs->bb[1],
|
---|
705 | gs->bb[2], gs->bb[3]);
|
---|
706 | }
|
---|
707 | printf("%%%%EndComments\n"); */
|
---|
708 |
|
---|
709 | //BEGIN added by pzn@debian.org
|
---|
710 | { // expands the bounding box to fit characters with tilde, acute,...
|
---|
711 | // if user has set JGRAPH_BORDER enviroment variable
|
---|
712 | int expandborder=0;
|
---|
713 | char *s; \
|
---|
714 | s=(char *)getenv("JGRAPH_BORDER");
|
---|
715 | if (s!=NULL) {
|
---|
716 | expandborder=atoi(s);
|
---|
717 | }
|
---|
718 | if (!pp) {
|
---|
719 | if (landscape) {
|
---|
720 | printf("%%%%BoundingBox: %d %d %d %d\n", gs->bb[1]-expandborder,
|
---|
721 | gs->bb[0]-expandborder, gs->bb[3]+expandborder,
|
---|
722 | gs->bb[2]+expandborder);
|
---|
723 | } else {
|
---|
724 | printf("%%%%BoundingBox: %d %d %d %d\n", gs->bb[0]-expandborder,
|
---|
725 | gs->bb[1]-expandborder, gs->bb[2]+expandborder,
|
---|
726 | gs->bb[3]+expandborder);
|
---|
727 | }
|
---|
728 | printf("%%%%EndComments\n\n");
|
---|
729 | }
|
---|
730 | }
|
---|
731 |
|
---|
732 | if (gs->page==1)
|
---|
733 | //reencode fonts to ISOLatin1 or other
|
---|
734 | { //NOTE: this is a preliminary version. It will only work with
|
---|
735 | // the default fonts, that are Times-Roman and Times-Bold
|
---|
736 | // "export JGRAPH_ENCODING=ISOLatin1Encoding" will work in sh
|
---|
737 | char *s; \
|
---|
738 | s=(char *)getenv("JGRAPH_ENCODING");
|
---|
739 | if ((s!=NULL) && (strlen(s)>0)) {
|
---|
740 | comment("Setting font encoding by pzn@debian.org");
|
---|
741 | printf("/Times-Bold findfont\n");
|
---|
742 | printf("dup length dict begin\n");
|
---|
743 | printf(" {1 index /FID ne {def} {pop pop} ifelse} forall\n");
|
---|
744 | printf(" /Encoding %s def\n",s);
|
---|
745 | printf(" currentdict\n");
|
---|
746 | printf("end\n");
|
---|
747 | printf("/Times-Bold exch definefont pop\n");
|
---|
748 | printf("/Times-Roman findfont\n");
|
---|
749 | printf("dup length dict begin\n");
|
---|
750 | printf(" {1 index /FID ne {def} {pop pop} ifelse} forall\n");
|
---|
751 | printf(" /Encoding %s def\n",s);
|
---|
752 | printf(" currentdict\n");
|
---|
753 | printf("end\n");
|
---|
754 | printf("/Times-Roman exch definefont pop\n");
|
---|
755 | comment("End of font encoding");
|
---|
756 | printf("\n");
|
---|
757 | }
|
---|
758 | }
|
---|
759 | //END added by pzn
|
---|
760 |
|
---|
761 | if (landscape) {
|
---|
762 | printf("-90 rotate\n");
|
---|
763 | }
|
---|
764 | if (pp) {
|
---|
765 | if (landscape) {
|
---|
766 | printf("%f 0 translate\n", -(11.0 * FCPI));
|
---|
767 | printf("%f %f translate\n",
|
---|
768 | (((11.0 * FCPI) - (gs->bb[2] - gs->bb[0])) / 2.0) - gs->bb[0],
|
---|
769 | (((8.5 * FCPI) - (gs->bb[3] - gs->bb[1])) / 2.0) - gs->bb[1]);
|
---|
770 | } else {
|
---|
771 | printf("%f %f translate\n",
|
---|
772 | (((8.5 * FCPI) - (gs->bb[2] - gs->bb[0])) / 2.0) - gs->bb[0],
|
---|
773 | (((11.0 * FCPI) - (gs->bb[3] - gs->bb[1])) / 2.0) - gs->bb[1]);
|
---|
774 | }
|
---|
775 | } else if (landscape) {
|
---|
776 | printf("%f 0 translate\n", -gs->bb[2] - gs->bb[0]);
|
---|
777 | }
|
---|
778 | printf("1 setlinecap 1 setlinejoin\n");
|
---|
779 | printf("0.700 setlinewidth\n");
|
---|
780 | printf("0.00 setgray\n");
|
---|
781 |
|
---|
782 | printf("\n");
|
---|
783 | printf("/Jrnd { exch cvi exch cvi dup 3 1 roll idiv mul } def\n");
|
---|
784 |
|
---|
785 | printf("/JDEdict 8 dict def\n");
|
---|
786 | printf("JDEdict /mtrx matrix put\n");
|
---|
787 | printf("/JDE {\n");
|
---|
788 | printf(" JDEdict begin\n");
|
---|
789 | printf(" /yrad exch def\n");
|
---|
790 | printf(" /xrad exch def\n");
|
---|
791 | printf(" /savematrix mtrx currentmatrix def\n");
|
---|
792 | printf(" xrad yrad scale\n");
|
---|
793 | printf(" 0 0 1 0 360 arc\n");
|
---|
794 | printf(" savematrix setmatrix\n");
|
---|
795 | printf(" end\n");
|
---|
796 | printf("} def\n");
|
---|
797 |
|
---|
798 | printf("/JSTR {\n");
|
---|
799 | printf(" gsave 1 eq { gsave 1 setgray fill grestore } if\n");
|
---|
800 | printf(" exch neg exch neg translate \n");
|
---|
801 | printf(" clip \n");
|
---|
802 | printf(" rotate \n");
|
---|
803 | printf(" 4 dict begin\n");
|
---|
804 | printf(" pathbbox /&top exch def\n");
|
---|
805 | printf(" /&right exch def\n");
|
---|
806 | printf(" /&bottom exch def\n");
|
---|
807 | printf(" &right sub /&width exch def\n");
|
---|
808 | printf(" newpath\n");
|
---|
809 | printf(" currentlinewidth mul round dup \n");
|
---|
810 | printf(" &bottom exch Jrnd exch &top \n");
|
---|
811 | printf(" 4 -1 roll currentlinewidth mul setlinewidth \n");
|
---|
812 | printf(" { &right exch moveto &width 0 rlineto stroke } for \n");
|
---|
813 | printf(" end\n");
|
---|
814 | printf(" grestore\n");
|
---|
815 | printf(" newpath\n");
|
---|
816 | printf("} bind def\n");
|
---|
817 |
|
---|
818 | gsave();
|
---|
819 | setfont("Times-Roman", 9.00);
|
---|
820 | if (gs->preamble != CNULL) {
|
---|
821 | if (gs->prefile) {
|
---|
822 | f = fopen(gs->preamble, "r");
|
---|
823 | if (f == NULL) {
|
---|
824 | fprintf(stderr, "Error: preamble file %s couldn't be opened\n",
|
---|
825 | gs->preamble);
|
---|
826 | exit(1);
|
---|
827 | }
|
---|
828 | for (c = getc(f); c != real_eof; c = getc(f)) putchar(c);
|
---|
829 | putchar('\n');
|
---|
830 | fclose(f);
|
---|
831 | } else {
|
---|
832 | printf("%s\n", gs->preamble);
|
---|
833 | }
|
---|
834 | }
|
---|
835 | }
|
---|
836 |
|
---|
837 | draw_footer(gs, pp)
|
---|
838 | Graphs gs;
|
---|
839 | int pp;
|
---|
840 | {
|
---|
841 | FILE *f;
|
---|
842 | char c;
|
---|
843 |
|
---|
844 | if (gs->epilogue != CNULL) {
|
---|
845 | if (gs->epifile) {
|
---|
846 | f = fopen(gs->epilogue, "r");
|
---|
847 | if (f == NULL) {
|
---|
848 | fprintf(stderr, "Error: epilogue file %s couldn't be opened\n",
|
---|
849 | gs->epilogue);
|
---|
850 | exit(1);
|
---|
851 | }
|
---|
852 | for (c = getc(f); c != real_eof; c = getc(f)) putchar(c);
|
---|
853 | putchar('\n');
|
---|
854 | fclose(f);
|
---|
855 | } else {
|
---|
856 | printf("%s\n", gs->epilogue);
|
---|
857 | }
|
---|
858 | }
|
---|
859 | grestore();
|
---|
860 | if (pp) printf("showpage\n"); else printf("\n");
|
---|
861 |
|
---|
862 | if (pp) {
|
---|
863 | printf("%%%%PageTrailer\n");
|
---|
864 | }
|
---|
865 |
|
---|
866 | }
|
---|
867 |
|
---|