File: printline.c

package info (click to toggle)
jgraph 83-20
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 628 kB
  • ctags: 445
  • sloc: ansic: 4,728; makefile: 186; sh: 106; awk: 104
file content (351 lines) | stat: -rw-r--r-- 7,492 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/* 
 * $Source: /tmp_mnt/n/fs/grad1/jsp/src/jgraph/RCS/printline.c,v $
 * $Revision: 8.3 $
 * $Date: 92/11/30 11:42:31 $
 * $Author: jsp $
 */

#include "jgraph.h"
#include <stdio.h>
#include <assert.h> //by pzn@debian.org

#define LINEWIDTHFACTOR 0.700
#define MAX(a, b) ((a > b) ? (a) : (b))

typedef struct fontlist {
  struct fontlist *flink;
  struct fontlist *blink;
  int level;
  float s;
  char *f;
} *Fontlist;

static Fontlist Jgraph_fonts;
static int Jgraph_gsave_level = -100;
static int Jgraph_comment;

gsave()
{
  if (Jgraph_gsave_level == -100) {
    Jgraph_gsave_level = 0;
    Jgraph_fonts = (Fontlist) make_list(sizeof(struct fontlist));
  } 
  Jgraph_gsave_level++;
  printf(" gsave ");
}

grestore()
{
  Fontlist l;

  if (last(Jgraph_fonts) != nil(Jgraph_fonts)) {
    l = last(Jgraph_fonts);
    if (l->level == Jgraph_gsave_level) {
      delete_item(l);
      free_node(l, Jgraph_fonts);
    }
  }
  Jgraph_gsave_level--;
  printf(" grestore ");
}

setfont(f, s)
char *f;
float s;
{
  Fontlist l;
  int ins;

  if (last(Jgraph_fonts) != nil(Jgraph_fonts)) {
    l = last(Jgraph_fonts);
    ins = (strcmp(l->f, f) != 0 || s != l->s);
    if (ins) {
      delete_item(l);
      free_node(l, Jgraph_fonts);
    }
  } else {
    ins = 1;
  }
  if (ins) {
    l = (Fontlist) get_node(Jgraph_fonts);
    l->level = Jgraph_gsave_level;
    l->s = s;
    l->f = f;
    insert(l, Jgraph_fonts);
    printf("/%s findfont %f scalefont setfont\n", f, s);
  }
}
  
setfill( x, y, t, f, p, a)
char t, p ;
float x, y;
float f[], a ;
{
/*   fprintf(stderr, "Hello?  %c %f %c %f\n", t, f[0], p, a); */
  if (t == 'g' && f[0] < 0.0) return;
  printf("gsave ");

  if ( t == 'g' )  {
    if( f[0] >= 0.0 ) printf("%f setgray ", f[0] );
  } else if ( t == 'c' )  {
    printf("%f %f %f setrgbcolor ", f[0], f[1], f[2] );
  }

  if (p == 's') {
    printf(" fill");
  } else if (p == '/') {
    printf(" 6.1 10 %f %f %f 1 JSTR", a, x, y);
  } else if (p == 'e') {
    printf(" 6.1 10 %f %f %f 0 JSTR", a, x, y);
  }
  printf(" grestore\n");
}

setgray( t, f)
char t ;
float f[] ;
{
    if ( t == 'g' )  {
       if( f[0] >= 0.0 ) printf("%f setgray\n", f[0] );
    } else if ( t == 'c' )  {
       printf("%f %f %f setrgbcolor\n", f[0], f[1], f[2] );
    }
}

printline(x1, y1,x2, y2, orientation)
float x1, y1, x2, y2;
char orientation;
{
  if (orientation == 'x') 
    printf("newpath %f %f moveto %f %f lineto stroke\n", x1, y1, x2, y2);
  else
    printf("newpath %f %f moveto %f %f lineto stroke\n", y1, x1, y2, x2);
  fflush(stdout);
} 

print_ebar(x1, y1, x2, ms, orientation)
float x1, y1, x2, ms;
char orientation;
{
  printline(x1, y1, x2, y1, orientation);
  printline(x2, y1-ms, x2, y1+ms, orientation);
}

start_line(x1, y1, c)
float x1, y1;
Curve c;
{
  setlinewidth(c->linethick);
  setlinestyle(c->linetype, c->gen_linetype);
  printf("%f %f moveto ", x1, y1);
}

cont_line(x1, y1)
float x1, y1;
{
  printf("  %f %f lineto\n", x1, y1);
}

end_line()
{
  printf("stroke\n");
  setlinewidth(1.0);
  setlinestyle('s', (Flist) 0);
  fflush(stdout);

}

bezier_control(x1, y1)
float x1, y1;
{
  printf("  %f %f ", x1, y1);
}

bezier_end(x1, y1)
float x1, y1;
{
  printf("  %f %f curveto\n", x1, y1);
}


start_poly(x1, y1)
float x1, y1;
{
  printf(" newpath %f %f moveto", x1, y1);
}

cont_poly(x1, y1)
float x1, y1;
{
  printf("  %f %f lineto\n", x1, y1);
}

end_poly(x, y, ftype, fill, pattern, parg)
float x, y;
char  ftype, pattern ;
float fill[], parg;
{
  printf("closepath ");
  setfill( x, y, ftype, fill, pattern, parg );
  printf("stroke\n");
  fflush(stdout);
}

/* Ellipse at 0, 0 -- assumes that you've already translated to x, y */

printellipse(x, y, radius1, radius2, ftype, fill, pattern, parg)
char ftype, pattern;
float x, y, radius1, radius2, fill[], parg;
{
  printf("newpath %f %f JDE\n", radius1, radius2);
  setfill( x, y, ftype, fill, pattern, parg );
  printf("stroke\n");
  fflush(stdout);
}

set_comment(c)
int c;
{
  Jgraph_comment = c;
}

comment(s)
char *s;
{
  if (Jgraph_comment) printf("%% %s\n", s);
}

printline_c(x1, y1, x2, y2, g)
float x1, y1, x2, y2;
Graph g;
{
  printline(ctop(x1, g->x_axis), ctop(y1, g->y_axis),
            ctop(x2, g->x_axis), ctop(y2, g->y_axis), 'x');
}

print_label(l)
Label l;
{
  int f, i, nlines;
  float fnl;
  char *s;
  unsigned char *s_7bit; // added by pzn@debian.org

  if (l->label == CNULL) return;

  nlines = 0;
  for (i = 0; l->label[i] != '\0'; i++) {
    if (l->label[i] == '\n') {
      l->label[i] = '\0';
      nlines++;
    }
  }
  fnl = (float) nlines;

  setfont(l->font, l->fontsize);
  printf("gsave %f %f translate %f rotate\n", l->x, l->y, l->rotate);
  if (l->graytype == 'g') {
    printf("  %f setgray\n", l->gray[0]);
  } else if (l->graytype == 'c') {
    printf("  %f %f %f setrgbcolor\n", l->gray[0], l->gray[1], 
           l->gray[2]);
  }

  if (l->vj == 'b') {
    printf("0 %f translate ", fnl * (l->fontsize + l->linesep) * FCPI / FPPI);
  } else if (l->vj == 'c') {
    if (nlines % 2 == 0) {
      printf("0 %f translate ", 
             (fnl/2.0*(l->fontsize + l->linesep) - l->fontsize/2.0)
              * FCPI / FPPI);
    } else {
      printf("0 %f translate ", 
             ((fnl-1.0)/2.0*(l->fontsize + l->linesep) + l->linesep/2.0)
              * FCPI / FPPI);
    }
  } else {
    printf("0 %f translate ", -l->fontsize * FCPI / FPPI);
  }

  s = l->label;
  
  for (i = 0; i <= nlines; i++) {
    // BEGIN added by pzn@debian.org
    // converts 8bit ascii chars to octal value;
    // 7bit are not converted
    {
       int i, j=0, len;
       len=strlen(s);
       s_7bit=malloc(len*4+1);
       assert(s_7bit!=NULL);
       for (i=0; i<=len; i++) {
	 if((unsigned char)s[i]<128) {
	   //char is ascii 7bit
	   s_7bit[j]=s[i];
	   j++;
	 } else {
	   //char must be converted to octal
           sprintf(s_7bit+j,"\\%03o",(unsigned char)s[i]);
	   j+=4;
	 }
       }
       s_7bit[j-1]=0;
    }
    //printf("(%s) dup stringwidth pop ", s); //disabled by pzn
    printf("(%s) dup stringwidth pop ", s_7bit); free(s_7bit);
    // END added by pzn
    if (l->hj == 'c') {
      printf("2 div neg 0 moveto\n");
    } else if (l->hj == 'r') {
      printf("neg 0 moveto\n");
    } else {
      printf("pop 0 0 moveto\n");
    }
    /* I would put string blanking in here if I had the time... */
 
    if (i != nlines) {
      f = strlen(s);
      s[f] = '\n';
      s = &(s[f+1]);
      printf("show 0 %f translate\n", 
              - (l->fontsize + l->linesep) * FCPI / FPPI);
    } else {
      printf("show\n");
    }
  }
  printf("grestore\n");
}

setlinewidth(size)
float size;
{
  printf("%f setlinewidth ", size * LINEWIDTHFACTOR);
}

setlinestyle(style, glist)
char style;
Flist glist;
{
  Flist fl;

  switch(style) {
    case '0': printf(" [0 2] setdash\n"); break;
    case 's': printf(" [] 0 setdash\n"); break;
    case '.': printf(" [1 3.200000] 0 setdash\n"); break;
    case '-': printf(" [4.00000] 0 setdash\n"); break;
    case 'l': printf(" [7 2] 0 setdash\n"); break;
    case 'd': printf(" [5 3 1 3] 0 setdash\n"); break;
    case 'D': printf(" [5 3 1 2 1 3] 0 setdash\n"); break;
    case '2': printf(" [5 3 5 3 1 2 1 3] 0 setdash\n"); break;
    case 'g': 
      printf(" [");
      for (fl = first(glist); fl != nil(glist); fl = next(fl))
        printf("%f ", fl->f);
      printf("] 0 setdash\n");
      break;
    default: fprintf(stderr, "Error: Unknown line type: %c\n", style);
             exit(1);
             break;
  }
}