1 | /*
|
---|
2 | * $Source: /tmp_mnt/n/fs/grad1/jsp/src/jgraph/RCS/printline.c,v $
|
---|
3 | * $Revision: 8.3 $
|
---|
4 | * $Date: 92/11/30 11:42:31 $
|
---|
5 | * $Author: jsp $
|
---|
6 | */
|
---|
7 |
|
---|
8 | #include "jgraph.h"
|
---|
9 | #include <stdio.h>
|
---|
10 | #include <assert.h> //by pzn@debian.org
|
---|
11 |
|
---|
12 | #define LINEWIDTHFACTOR 0.700
|
---|
13 | #define MAX(a, b) ((a > b) ? (a) : (b))
|
---|
14 |
|
---|
15 | typedef struct fontlist {
|
---|
16 | struct fontlist *flink;
|
---|
17 | struct fontlist *blink;
|
---|
18 | int level;
|
---|
19 | float s;
|
---|
20 | char *f;
|
---|
21 | } *Fontlist;
|
---|
22 |
|
---|
23 | static Fontlist Jgraph_fonts;
|
---|
24 | static int Jgraph_gsave_level = -100;
|
---|
25 | static int Jgraph_comment;
|
---|
26 |
|
---|
27 | gsave()
|
---|
28 | {
|
---|
29 | if (Jgraph_gsave_level == -100) {
|
---|
30 | Jgraph_gsave_level = 0;
|
---|
31 | Jgraph_fonts = (Fontlist) make_list(sizeof(struct fontlist));
|
---|
32 | }
|
---|
33 | Jgraph_gsave_level++;
|
---|
34 | printf(" gsave ");
|
---|
35 | }
|
---|
36 |
|
---|
37 | grestore()
|
---|
38 | {
|
---|
39 | Fontlist l;
|
---|
40 |
|
---|
41 | if (last(Jgraph_fonts) != nil(Jgraph_fonts)) {
|
---|
42 | l = last(Jgraph_fonts);
|
---|
43 | if (l->level == Jgraph_gsave_level) {
|
---|
44 | delete_item(l);
|
---|
45 | free_node(l, Jgraph_fonts);
|
---|
46 | }
|
---|
47 | }
|
---|
48 | Jgraph_gsave_level--;
|
---|
49 | printf(" grestore ");
|
---|
50 | }
|
---|
51 |
|
---|
52 | setfont(f, s)
|
---|
53 | char *f;
|
---|
54 | float s;
|
---|
55 | {
|
---|
56 | Fontlist l;
|
---|
57 | int ins;
|
---|
58 |
|
---|
59 | if (last(Jgraph_fonts) != nil(Jgraph_fonts)) {
|
---|
60 | l = last(Jgraph_fonts);
|
---|
61 | ins = (strcmp(l->f, f) != 0 || s != l->s);
|
---|
62 | if (ins) {
|
---|
63 | delete_item(l);
|
---|
64 | free_node(l, Jgraph_fonts);
|
---|
65 | }
|
---|
66 | } else {
|
---|
67 | ins = 1;
|
---|
68 | }
|
---|
69 | if (ins) {
|
---|
70 | l = (Fontlist) get_node(Jgraph_fonts);
|
---|
71 | l->level = Jgraph_gsave_level;
|
---|
72 | l->s = s;
|
---|
73 | l->f = f;
|
---|
74 | insert(l, Jgraph_fonts);
|
---|
75 | printf("/%s findfont %f scalefont setfont\n", f, s);
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | setfill( x, y, t, f, p, a)
|
---|
80 | char t, p ;
|
---|
81 | float x, y;
|
---|
82 | float f[], a ;
|
---|
83 | {
|
---|
84 | /* fprintf(stderr, "Hello? %c %f %c %f\n", t, f[0], p, a); */
|
---|
85 | if (t == 'g' && f[0] < 0.0) return;
|
---|
86 | printf("gsave ");
|
---|
87 |
|
---|
88 | if ( t == 'g' ) {
|
---|
89 | if( f[0] >= 0.0 ) printf("%f setgray ", f[0] );
|
---|
90 | } else if ( t == 'c' ) {
|
---|
91 | printf("%f %f %f setrgbcolor ", f[0], f[1], f[2] );
|
---|
92 | }
|
---|
93 |
|
---|
94 | if (p == 's') {
|
---|
95 | printf(" fill");
|
---|
96 | } else if (p == '/') {
|
---|
97 | printf(" 6.1 10 %f %f %f 1 JSTR", a, x, y);
|
---|
98 | } else if (p == 'e') {
|
---|
99 | printf(" 6.1 10 %f %f %f 0 JSTR", a, x, y);
|
---|
100 | }
|
---|
101 | printf(" grestore\n");
|
---|
102 | }
|
---|
103 |
|
---|
104 | setgray( t, f)
|
---|
105 | char t ;
|
---|
106 | float f[] ;
|
---|
107 | {
|
---|
108 | if ( t == 'g' ) {
|
---|
109 | if( f[0] >= 0.0 ) printf("%f setgray\n", f[0] );
|
---|
110 | } else if ( t == 'c' ) {
|
---|
111 | printf("%f %f %f setrgbcolor\n", f[0], f[1], f[2] );
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | printline(x1, y1,x2, y2, orientation)
|
---|
116 | float x1, y1, x2, y2;
|
---|
117 | char orientation;
|
---|
118 | {
|
---|
119 | if (orientation == 'x')
|
---|
120 | printf("newpath %f %f moveto %f %f lineto stroke\n", x1, y1, x2, y2);
|
---|
121 | else
|
---|
122 | printf("newpath %f %f moveto %f %f lineto stroke\n", y1, x1, y2, x2);
|
---|
123 | fflush(stdout);
|
---|
124 | }
|
---|
125 |
|
---|
126 | print_ebar(x1, y1, x2, ms, orientation)
|
---|
127 | float x1, y1, x2, ms;
|
---|
128 | char orientation;
|
---|
129 | {
|
---|
130 | printline(x1, y1, x2, y1, orientation);
|
---|
131 | printline(x2, y1-ms, x2, y1+ms, orientation);
|
---|
132 | }
|
---|
133 |
|
---|
134 | start_line(x1, y1, c)
|
---|
135 | float x1, y1;
|
---|
136 | Curve c;
|
---|
137 | {
|
---|
138 | setlinewidth(c->linethick);
|
---|
139 | setlinestyle(c->linetype, c->gen_linetype);
|
---|
140 | printf("%f %f moveto ", x1, y1);
|
---|
141 | }
|
---|
142 |
|
---|
143 | cont_line(x1, y1)
|
---|
144 | float x1, y1;
|
---|
145 | {
|
---|
146 | printf(" %f %f lineto\n", x1, y1);
|
---|
147 | }
|
---|
148 |
|
---|
149 | end_line()
|
---|
150 | {
|
---|
151 | printf("stroke\n");
|
---|
152 | setlinewidth(1.0);
|
---|
153 | setlinestyle('s', (Flist) 0);
|
---|
154 | fflush(stdout);
|
---|
155 |
|
---|
156 | }
|
---|
157 |
|
---|
158 | bezier_control(x1, y1)
|
---|
159 | float x1, y1;
|
---|
160 | {
|
---|
161 | printf(" %f %f ", x1, y1);
|
---|
162 | }
|
---|
163 |
|
---|
164 | bezier_end(x1, y1)
|
---|
165 | float x1, y1;
|
---|
166 | {
|
---|
167 | printf(" %f %f curveto\n", x1, y1);
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | start_poly(x1, y1)
|
---|
172 | float x1, y1;
|
---|
173 | {
|
---|
174 | printf(" newpath %f %f moveto", x1, y1);
|
---|
175 | }
|
---|
176 |
|
---|
177 | cont_poly(x1, y1)
|
---|
178 | float x1, y1;
|
---|
179 | {
|
---|
180 | printf(" %f %f lineto\n", x1, y1);
|
---|
181 | }
|
---|
182 |
|
---|
183 | end_poly(x, y, ftype, fill, pattern, parg)
|
---|
184 | float x, y;
|
---|
185 | char ftype, pattern ;
|
---|
186 | float fill[], parg;
|
---|
187 | {
|
---|
188 | printf("closepath ");
|
---|
189 | setfill( x, y, ftype, fill, pattern, parg );
|
---|
190 | printf("stroke\n");
|
---|
191 | fflush(stdout);
|
---|
192 | }
|
---|
193 |
|
---|
194 | /* Ellipse at 0, 0 -- assumes that you've already translated to x, y */
|
---|
195 |
|
---|
196 | printellipse(x, y, radius1, radius2, ftype, fill, pattern, parg)
|
---|
197 | char ftype, pattern;
|
---|
198 | float x, y, radius1, radius2, fill[], parg;
|
---|
199 | {
|
---|
200 | printf("newpath %f %f JDE\n", radius1, radius2);
|
---|
201 | setfill( x, y, ftype, fill, pattern, parg );
|
---|
202 | printf("stroke\n");
|
---|
203 | fflush(stdout);
|
---|
204 | }
|
---|
205 |
|
---|
206 | set_comment(c)
|
---|
207 | int c;
|
---|
208 | {
|
---|
209 | Jgraph_comment = c;
|
---|
210 | }
|
---|
211 |
|
---|
212 | comment(s)
|
---|
213 | char *s;
|
---|
214 | {
|
---|
215 | if (Jgraph_comment) printf("%% %s\n", s);
|
---|
216 | }
|
---|
217 |
|
---|
218 | printline_c(x1, y1, x2, y2, g)
|
---|
219 | float x1, y1, x2, y2;
|
---|
220 | Graph g;
|
---|
221 | {
|
---|
222 | printline(ctop(x1, g->x_axis), ctop(y1, g->y_axis),
|
---|
223 | ctop(x2, g->x_axis), ctop(y2, g->y_axis), 'x');
|
---|
224 | }
|
---|
225 |
|
---|
226 | print_label(l)
|
---|
227 | Label l;
|
---|
228 | {
|
---|
229 | int f, i, nlines;
|
---|
230 | float fnl;
|
---|
231 | char *s;
|
---|
232 | unsigned char *s_7bit; // added by pzn@debian.org
|
---|
233 |
|
---|
234 | if (l->label == CNULL) return;
|
---|
235 |
|
---|
236 | nlines = 0;
|
---|
237 | for (i = 0; l->label[i] != '\0'; i++) {
|
---|
238 | if (l->label[i] == '\n') {
|
---|
239 | l->label[i] = '\0';
|
---|
240 | nlines++;
|
---|
241 | }
|
---|
242 | }
|
---|
243 | fnl = (float) nlines;
|
---|
244 |
|
---|
245 | setfont(l->font, l->fontsize);
|
---|
246 | printf("gsave %f %f translate %f rotate\n", l->x, l->y, l->rotate);
|
---|
247 | if (l->graytype == 'g') {
|
---|
248 | printf(" %f setgray\n", l->gray[0]);
|
---|
249 | } else if (l->graytype == 'c') {
|
---|
250 | printf(" %f %f %f setrgbcolor\n", l->gray[0], l->gray[1],
|
---|
251 | l->gray[2]);
|
---|
252 | }
|
---|
253 |
|
---|
254 | if (l->vj == 'b') {
|
---|
255 | printf("0 %f translate ", fnl * (l->fontsize + l->linesep) * FCPI / FPPI);
|
---|
256 | } else if (l->vj == 'c') {
|
---|
257 | if (nlines % 2 == 0) {
|
---|
258 | printf("0 %f translate ",
|
---|
259 | (fnl/2.0*(l->fontsize + l->linesep) - l->fontsize/2.0)
|
---|
260 | * FCPI / FPPI);
|
---|
261 | } else {
|
---|
262 | printf("0 %f translate ",
|
---|
263 | ((fnl-1.0)/2.0*(l->fontsize + l->linesep) + l->linesep/2.0)
|
---|
264 | * FCPI / FPPI);
|
---|
265 | }
|
---|
266 | } else {
|
---|
267 | printf("0 %f translate ", -l->fontsize * FCPI / FPPI);
|
---|
268 | }
|
---|
269 |
|
---|
270 | s = l->label;
|
---|
271 |
|
---|
272 | for (i = 0; i <= nlines; i++) {
|
---|
273 | // BEGIN added by pzn@debian.org
|
---|
274 | // converts 8bit ascii chars to octal value;
|
---|
275 | // 7bit are not converted
|
---|
276 | {
|
---|
277 | int i, j=0, len;
|
---|
278 | len=strlen(s);
|
---|
279 | s_7bit=malloc(len*4+1);
|
---|
280 | assert(s_7bit!=NULL);
|
---|
281 | for (i=0; i<=len; i++) {
|
---|
282 | if((unsigned char)s[i]<128) {
|
---|
283 | //char is ascii 7bit
|
---|
284 | s_7bit[j]=s[i];
|
---|
285 | j++;
|
---|
286 | } else {
|
---|
287 | //char must be converted to octal
|
---|
288 | sprintf(s_7bit+j,"\\%03o",(unsigned char)s[i]);
|
---|
289 | j+=4;
|
---|
290 | }
|
---|
291 | }
|
---|
292 | s_7bit[j-1]=0;
|
---|
293 | }
|
---|
294 | //printf("(%s) dup stringwidth pop ", s); //disabled by pzn
|
---|
295 | printf("(%s) dup stringwidth pop ", s_7bit); free(s_7bit);
|
---|
296 | // END added by pzn
|
---|
297 | if (l->hj == 'c') {
|
---|
298 | printf("2 div neg 0 moveto\n");
|
---|
299 | } else if (l->hj == 'r') {
|
---|
300 | printf("neg 0 moveto\n");
|
---|
301 | } else {
|
---|
302 | printf("pop 0 0 moveto\n");
|
---|
303 | }
|
---|
304 | /* I would put string blanking in here if I had the time... */
|
---|
305 |
|
---|
306 | if (i != nlines) {
|
---|
307 | f = strlen(s);
|
---|
308 | s[f] = '\n';
|
---|
309 | s = &(s[f+1]);
|
---|
310 | printf("show 0 %f translate\n",
|
---|
311 | - (l->fontsize + l->linesep) * FCPI / FPPI);
|
---|
312 | } else {
|
---|
313 | printf("show\n");
|
---|
314 | }
|
---|
315 | }
|
---|
316 | printf("grestore\n");
|
---|
317 | }
|
---|
318 |
|
---|
319 | setlinewidth(size)
|
---|
320 | float size;
|
---|
321 | {
|
---|
322 | printf("%f setlinewidth ", size * LINEWIDTHFACTOR);
|
---|
323 | }
|
---|
324 |
|
---|
325 | setlinestyle(style, glist)
|
---|
326 | char style;
|
---|
327 | Flist glist;
|
---|
328 | {
|
---|
329 | Flist fl;
|
---|
330 |
|
---|
331 | switch(style) {
|
---|
332 | case '0': printf(" [0 2] setdash\n"); break;
|
---|
333 | case 's': printf(" [] 0 setdash\n"); break;
|
---|
334 | case '.': printf(" [1 3.200000] 0 setdash\n"); break;
|
---|
335 | case '-': printf(" [4.00000] 0 setdash\n"); break;
|
---|
336 | case 'l': printf(" [7 2] 0 setdash\n"); break;
|
---|
337 | case 'd': printf(" [5 3 1 3] 0 setdash\n"); break;
|
---|
338 | case 'D': printf(" [5 3 1 2 1 3] 0 setdash\n"); break;
|
---|
339 | case '2': printf(" [5 3 5 3 1 2 1 3] 0 setdash\n"); break;
|
---|
340 | case 'g':
|
---|
341 | printf(" [");
|
---|
342 | for (fl = first(glist); fl != nil(glist); fl = next(fl))
|
---|
343 | printf("%f ", fl->f);
|
---|
344 | printf("] 0 setdash\n");
|
---|
345 | break;
|
---|
346 | default: fprintf(stderr, "Error: Unknown line type: %c\n", style);
|
---|
347 | exit(1);
|
---|
348 | break;
|
---|
349 | }
|
---|
350 | }
|
---|
351 |
|
---|