File: printline.c

package info (click to toggle)
jgraph 83-23
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, stretch
  • size: 652 kB
  • ctags: 446
  • sloc: ansic: 4,596; makefile: 146; sh: 106; awk: 104
file content (316 lines) | stat: -rw-r--r-- 7,456 bytes parent folder | download | duplicates (3)
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
/* 
 * $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 <assert.h>

#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;

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

void grestore(void)
{
  Fontlist l;

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

void setfont(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((List)l);
      free_node((List)l, (List)Jgraph_fonts);
    }
  } else {
    ins = 1;
  }
  if (ins) {
    l = (Fontlist) get_node((List)Jgraph_fonts);
    l->level = Jgraph_gsave_level;
    l->s = s;
    l->f = f;
    insert((List)l, (List)Jgraph_fonts);
    printf("/%s findfont %f scalefont setfont\n", f, s);
  }
}
  
void setfill(float x, float y, char t, float *f, char p, float 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");
}

void setgray(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] );
    }
}

void printline(float x1, float y1, float x2, float 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);
} 

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

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

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

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

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

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

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

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

void end_poly(float x, float y, char ftype, float *fill, char pattern, float 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 */

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

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

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

void printline_c(float x1, float y1, float x2, float 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');
}

void print_label(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_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");
}

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

void setlinestyle(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;
  }
}