root/trunk/Jgraph/printline.c

Revision 420, 7.3 kB (checked in by nicholas, 10 months ago)

Jgraph: my changes - ANSIfication, few minor bug fixes; works on OS X 10.5 now

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