File: hpgl.c

package info (click to toggle)
xgraph 11.3.2-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 536 kB
  • ctags: 1,071
  • sloc: ansic: 6,118; makefile: 260
file content (312 lines) | stat: -rw-r--r-- 10,217 bytes parent folder | download
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
/*
 * HPGL Output
 *
 * Tom Quarles
 */

#define MAPX(state,x) ( (x) + P1X + state->clipminX ) 
#define MAPY(state,y) ( MAXY - (y) + P1Y - state->clipminY)

#include <stdlib.h>
#include <string.h>
#include "copyright.h"
#include "xgout.h"
#include "plotter.h"
#include <stdio.h>
#include <math.h>
#define MAX(a,b) ( ((a)>(b)) ? (a) : (b) )
#define MIN(a,b) ( ((a)<(b)) ? (a) : (b) )
/* char *malloc(); */

static void hpglText();
static void hpglSeg();
static void hpglDot();
static void hpglEnd();

static xgOut hpglInfo = {
    D_COLOR,    /* device characteristics */
    MAXX,   /* width */
    MAXY,   /* height */
    200,    /* border padding */
    0,      /* extra space around axis labels */
    250,    /* tick length - approx 1/4 inch */
    50,	    /* spacing above legend lables */
    0,      /* axis font width */
    0,      /* axis font height */
    0,      /* title font width */
    0,      /* title font height */
	1000000,/* maximum number of segments */

    hpglText,   /* text output function */
    hpglSeg,    /* segment  drawing function */
    hpglDot,    /* dot/marker drawing function */
    hpglEnd,    /* end of plot function */

    NULL,   /* userInfo */
};

typedef struct {
	double axis_w;
	double axis_h;
	double title_w;
	double title_h;
	FILE *plotterFile;
	int clipminX;
	int clipminY;
	int clipmaxX;
	int clipmaxY;
} mydata;

/*ARGSUSED*/
int
hpglInit(stream,width,height,title_family, title_size,
		axis_family, axis_size, flags, outInfo,errmsg)
    FILE *stream;	/* output stream */
	int width;		/* desired width of space in microns */
	int height;		/* desired height in microns */
	char *title_family;	/* name of font for titles */
	double title_size;	/* size of font for titles */
	char *axis_family;	/* name of font for axes */
	double axis_size;	/* size of font for axes */
        int flags;		/* predicate values (ignored) */
    xgOut *outInfo;	/* my structure */
	char errmsg[ERRBUFSIZE];	/* a place to complain to */
{
	mydata *myInfo;

	myInfo = (mydata*)malloc(sizeof(mydata));
	if(myInfo == NULL) return(0);
    *outInfo = hpglInfo;
	outInfo->area_w = MIN(MAXX,width/25);
	outInfo->area_h = MIN(MAXY,height/25);
	/* magic formulas:  input sizes are in points = 1/72 inch */
	/* my sizes are in cm */
	/* plotter units are in units of .025mm ~= 1/1016 inch */
	/* have to warn of height 1.5 times larger or get bitten by
	   plotter's internal padding */
	/* widths are (arbitrarily) selected to be 2/3 of the height */
	/*     (cancels with width factor) */
	myInfo->axis_w = axis_size * .666 * 2.54/72.;
	myInfo->axis_h = axis_size * 2.54/72.;
	myInfo->title_w = title_size * .666 * 2.54/72.;
	myInfo->title_h = title_size * 2.54/72.;

    outInfo->axis_pad = axis_size*1016.*1.5/72.;
    outInfo->axis_width = axis_size*1016.*1.5/72.;
    outInfo->axis_height = axis_size*1016.*.666/72.;
    outInfo->title_width = title_size*1016.*1.5/72.;
    outInfo->title_height = title_size*1016.*.666/72.;
    outInfo->user_state = (char *)myInfo;
	myInfo->plotterFile = stream;
    myInfo->clipminX = 0;
    myInfo->clipminY = 0;
    myInfo->clipmaxX = MAXX;
    myInfo->clipmaxY = MAXY;
	fprintf(myInfo->plotterFile,"PG;IN;\n");
	fprintf(myInfo->plotterFile,"DI1,0;\n");
    fprintf(myInfo->plotterFile,"IW%d,%d,%d,%d;\n",MAPX(myInfo,0),
			MAPY(myInfo,myInfo->clipmaxY-myInfo->clipminY),
            MAPX(myInfo,myInfo->clipmaxX-myInfo->clipminX),
			MAPY(myInfo,0));
    return(1);
}

static void 
hpglText(userState,x,y,text,just,style)
    mydata *userState;    /* my state information  */
    int x,y;    /* coords of text origin */
    char *text; /* what to put there */
    int just;   /* how to justify */
    /* where the origin is relative to where the text should go
     * as a function of the various values of just 

    T_UPPERLEFT     T_TOP       T_UPPERRIGHT
    T_LEFT          T_CENTER    T_RIGHT
    T_LOWERLEFT     T_BOTTOM    T_LOWERRIGHT

    */
    int style;  /* T_AXIS = axis font, T_TITLE = title font */

{
    fprintf(userState->plotterFile,"PU;SP%d;",TEXTCOLOR);
    fprintf(userState->plotterFile,"PA%d,%d;",MAPX(userState,x),MAPY(userState,y));
    switch(style) {
        case T_AXIS:
            fprintf(userState->plotterFile,"SI%f,%f;",userState->axis_w,userState->axis_h);
            break;
        case T_TITLE:
            fprintf(userState->plotterFile,"SI%f,%f;",userState->title_w,userState->title_h);
            break;
        default:
            printf("bad text style %d in hpglText\n",style);
            exit(1);
            break;
    }
    switch(just) {
        case T_UPPERLEFT:
            fprintf(userState->plotterFile,"LO3;\n");
            break;
        case T_TOP:
            fprintf(userState->plotterFile,"LO6;\n");
            break;
        case T_UPPERRIGHT:
            fprintf(userState->plotterFile,"LO9;\n");
            break;
        case T_LEFT:
            fprintf(userState->plotterFile,"LO2;\n");
            break;
        case T_CENTER:
            fprintf(userState->plotterFile,"LO5;\n");
            break;
        case T_RIGHT:
            fprintf(userState->plotterFile,"LO8;\n");
            break;
        case T_LOWERLEFT:
            fprintf(userState->plotterFile,"LO1;\n");
            break;
        case T_BOTTOM:
            fprintf(userState->plotterFile,"LO4;\n");
            break;
        case T_LOWERRIGHT:
            fprintf(userState->plotterFile,"LO7;\n");
            break;
        default:
            printf("bad justification type %d in hpglText\n",just);
            exit(1);
            break;
    }
    fprintf(userState->plotterFile,"LB%s\03;",text);
}



static int penselect[8] = { PEN1, PEN2, PEN3, PEN4, PEN5, PEN6, PEN7, PEN8};
static int lineselect[8] = { LINE1, LINE2, LINE3, LINE4, LINE5, LINE6, 
        LINE7, LINE8};



static void 
hpglSeg(userState,ns,segs,width,style,lappr,color)
    mydata *userState;    /* my state information (not used) */
    int ns;         /* number of segments */
    XSegment *segs; /* X array of segments */
    int width;      /* width of lines in pixels */
    int style;      /* L_VAR = dotted, L_AXIS = grid, L_ZERO = axis*/
    int lappr;      /* line style */
    int color;      /* line color */
{
    int i;

    if (style == L_ZERO) {
        fprintf(userState->plotterFile,"SP%d;",PENAXIS); /* select correct pen */
        fprintf(userState->plotterFile,"LT;"); /* solid line style */
    } else if (style == L_AXIS) {
        fprintf(userState->plotterFile,"SP%d;",PENGRID); /* select correct pen */
        fprintf(userState->plotterFile,"LT;"); /* solid line style */
    } else if (style == L_VAR) {
        if( (color < 0) || (color >7) ) {
            printf("out of range line color %d in hpglLine\n",color);
            exit(1);
        }
        fprintf(userState->plotterFile,"SP%d;",penselect[color]); /* select correct pen */
        if( (lappr < 0) || (lappr >7) ) {
            printf("out of range line style %d in hpglLine\n",lappr);
            exit(1);
        }
        if(lappr == 0) {
            fprintf(userState->plotterFile,"LT;");/*select solid line type*/
        } else {
            fprintf(userState->plotterFile,"LT%d;",lineselect[lappr]);/*select line type*/
        }
    } else {
        printf("unknown style %d in hpglLine\n",style);
        exit(1);
    }
    for(i=0;i<ns;i++) {
		if(!i || ( (segs[i].x1!=segs[i-1].x2) || (segs[i].y1!=segs[i-1].y2) ) ){
            /* MOVE */
            fprintf(userState->plotterFile,"PU;PA%d,%d;\n",MAPX(userState,segs[i].x1),
                    MAPY(userState,segs[i].y1));
        }
		/* DRAW */
		if(width <= 1) {
			fprintf(userState->plotterFile,"PD;PA%d,%d;\n",MAPX(userState,segs[i].x2),
					MAPY(userState,segs[i].y2));
		} else { /* ugly - wide lines -> rectangles */
			double frac;
			int lx,ly;
			int urx,ury,ulx,uly,llx,lly,lrx,lry;

			frac = (width/2)/sqrt((double)
					((segs[i].x1-segs[i].x2)*
					(segs[i].x1-segs[i].x2))+
					((segs[i].y1-segs[i].y2)*
					(segs[i].y1-segs[i].y2)) );
			lx = frac * (segs[i].y2 - segs[i].y1);
			ly = -frac * (segs[i].x2 - segs[i].x1);
			urx = segs[i].x2 +lx;
			ury = segs[i].y2 +ly;
			ulx = segs[i].x2 -lx;
			uly = segs[i].y2 -ly;
			llx = segs[i].x1 -lx;
			lly = segs[i].y1 -ly;
			lrx = segs[i].x1 +lx;
			lry = segs[i].y1 +ly;
			fprintf(userState->plotterFile,"PU;PA%d,%d;",MAPX(userState,llx),
					MAPY(userState,lly));
			fprintf(userState->plotterFile,"PM0;");
			fprintf(userState->plotterFile,"PD,PA%d,%d;PA%d,%d;PA%d,%d;\n",
				MAPX(userState,lrx),MAPY(userState,lry),
				MAPX(userState,urx),MAPY(userState,ury),
				MAPX(userState,ulx),MAPY(userState,uly) );
			fprintf(userState->plotterFile,"PM2;FP;EP;");
		}
    }
	fprintf(userState->plotterFile,"PU;");
}

static char *markselect[8] = { MARK1, MARK2, MARK3, MARK4, MARK5, MARK6, 
        MARK7, MARK8};

static void 
hpglDot(userState,x,y,style,type,color)
    mydata *userState;    /* my state information (not used) */
    int x,y;    /* coord of dot */
    int style;  /* type of dot */
    int type;   /* dot style variation */
    int color;  /* color of dot */
{
    /* move to given coord */
    fprintf(userState->plotterFile,"PU;PA%d,%d;\n",MAPX(userState,x), MAPY(userState,y));
    if( (color<0) || (color>7) ) {
        printf("unknown color %d in hpglDot\n",color);
        exit(1);
    }
    fprintf(userState->plotterFile,"SP%d;",penselect[color]);
    if(style == P_PIXEL) {
        fprintf(userState->plotterFile,"PD;PU;\n");
    } else if (style == P_DOT) {
        fprintf(userState->plotterFile,"LT;PM0;CI40;PM2;FT;EP;\n");
    } else if (style == P_MARK) {
        if( (type<0) || (type>7) ) {
            printf("unknown marker type %d in hpglDot\n",type);
            exit(1);
        }
        /*fprintf(userState->plotterFile,"LT;CA5;LO4;SI0.1;LB%s\03;\n",markselect[type]);*/
		fprintf(userState->plotterFile,"LT;CS5;LO4;SI0.15;SM%s;PR0,0;SM;CS;\n",markselect[type]);
    } else {
        printf("unknown marker style %d in hpglDot\n",style);
        exit(1);
    }
}

static void 
hpglEnd(userState)
    mydata *userState;    /* my state information (not used) */

{
	fprintf(userState->plotterFile,"SP;PG;IN;\n");
    fflush(userState->plotterFile);
    return;
}