1: /******************************************************************************
2: *
3: * parser -- parses device independent troff commands and calls
4: * appropriate action routines
5: *
6: * John Mellor-Crummey (Xerox Corp)
7: *
8: * Copyright 1985 Xerox Corporation
9: *
10: ******************************************************************************/
11:
12:
13: #include <stdio.h>
14: #include <ctype.h>
15:
16: #include "deviceinfo.h"
17: #include "defs.h" /* constant and macro definitions */
18: #include "externs.h" /* declarations for global variables */
19:
20:
21: /*-----------------------------------------------------------------------------
22: * ditroffToIpress -- parses the input file calling routines to perform
23: * requested operations
24: *---------------------------------------------------------------------------*/
25: ditroffToIpress(fptr)
26: register FILE *fptr;
27: {
28: char buffer[BUFFERSIZE];
29: int c1,c2;
30: int cmd;
31: int flag = 1;
32:
33: while((cmd = getc(fptr)) != EOF)
34: {
35: switch(cmd)
36: {
37: case cmdPointSize:
38: if (flag = (fscanf(fptr,"%d",&c1) == 1))
39: setPointSize(internalSize(c1)+1);
40: break;
41: case cmdFont:
42: if (flag = (fscanf(fptr,"%d",&c1) == 1))
43: setFont(fontNumber(c1));
44: break;
45: case cmdChar:
46: if (flag = ((cmd = getc(fptr)) != EOF))
47: outputChar((unsigned int) cmd);
48: break;
49: case cmdSpecChar:
50: if (flag = (fscanf(fptr,"%s",buffer) == 1))
51: outputString(buffer);
52: break;
53: case cmdAbsHoriz:
54: if (flag = (fscanf(fptr,"%d",&c1) == 1))
55: hMov(c1);
56: break;
57: case cmdRelHoriz:
58: if (flag = (fscanf(fptr,"%d",&c1) == 1))
59: hInc(c1);
60: break;
61: case cmdAbsVert:
62: if (flag = (fscanf(fptr,"%d",&c1) == 1))
63: vMov(c1);
64: break;
65: case cmdRelVert:
66: if (flag = (fscanf(fptr,"%d",&c1) == 1))
67: vInc(c1);
68: break;
69: case cmdEol:
70: /* ignore arguments, call routine for newline */
71: flag = (fscanf(fptr,"%d %d",&c1,&c2) == 2);
72: newLine();
73: break;
74: case cmdWordSep:
75: break;
76: case cmdNewPage:
77: if (flag = (fscanf(fptr,"%d",&c1) == 1))
78: newpage(c1);
79: break;
80: case cmdPushEnv:
81: pushCurrentEnv();
82: break;
83: case cmdPopEnv:
84: popSavedEnv();
85: break;
86: case cmdCharString:
87: (void) fgets(buffer,BUFFERSIZE,fptr);
88: /* no op -- this command causes no conversion
89: * not expected in ditroff output
90: */
91: break;
92: case :
93: skipToNextLine(fptr);
94: break;
95: case cmdDraw:
96: drawCommand(fptr);
97: skipToNextLine(fptr);
98: break;
99: case cmdDevice:
100: deviceCommand(fptr);
101: skipToNextLine(fptr);
102: break;
103: case '\n':
104: linenumber++;
105: break;
106: default:
107: if(isdigit(cmd))
108: {
109: /* command is 2 digit horizontal displacement
110: * followed by a char
111: */
112: if (flag = ((c1 = getc(fptr)) != EOF))
113: {
114: hInc(c1 - '0' + 10 * (cmd - '0'));
115: if (flag = ((c1 = getc(fptr)) != EOF))
116: outputChar((unsigned int) c1);
117: }
118: break;
119: }
120: else if (white(cmd) || (cmd == '\0'))
121: break; /* handle trash in stream */
122: reportError(QUIT,"unrecognized character in input %o %c", (char *) cmd, (char *) cmd);
123: }
124: if (!flag)
125: reportError(QUIT,"error in input for ditroff command %c", (char *) cmd);
126:
127: }
128: }
129:
130:
131: /*-----------------------------------------------------------------------------
132: * drawCommand -- process ditroff draw commands
133: *---------------------------------------------------------------------------*/
134: drawCommand(fptr)
135: register FILE *fptr;
136: {
137: int c1,c2,c3,c4;
138: int cmd,flag;
139: char buffer[BUFFERSIZE];
140:
141: if (flag = ((cmd = getc(fptr)) != EOF))
142: {
143: switch(cmd)
144: {
145: case drawLine: /* draw from current x,y to c1,c2 */
146: /* read termination point */
147: if (flag = (fscanf(fptr,"%d %d",&c1,&c2) == 2))
148: drawline(c1,c2);
149: break;
150: case drawCircle: /* draw circle of diameter c1 with current x,y */
151: /* leftmost position on circle */
152: /* read circle diameter */
153: if (flag = (fscanf(fptr,"%d",&c1) == 1))
154: {
155: c2 = (c1 + 1) / 2;
156: if(!outputflag) /* won't be printed anyway */
157: break;
158: gobj_size(hor_pos,ver_pos - c2,hor_pos + c1,ver_pos + c2);
159: bitmapDrawCircle(c1);
160: }
161: break;
162: case drawEllipse: /* draw ellipse with axes c1,c2 with current x,y */
163: /* leftmost point on ellipse */
164: /* read height and width */
165: if (flag = (fscanf(fptr,"%d %d",&c1,&c2) == 2))
166: {
167: c3 = (c2 + 1) / 2;
168: if(!outputflag) /* won't be printed anyway */
169: break;
170: gobj_size(hor_pos,ver_pos - c3,hor_pos + c1,ver_pos + c3);
171: bitmapDrawEllipse(c1,c2);
172: }
173: break;
174: case drawArc: /* draw counter-clockwise arc with current x,y */
175: /* as start, c1,c2 represent relative x,y */
176: /* offset to center, c3,c4 represent relative */
177: /* x,y offset from center to ending point on arc */
178: /* read relative coords to current point: xc yc xt yt */
179: if (flag = (fscanf(fptr,"%d %d %d %d",&c1,&c2,&c3,&c4) == 4))
180: {
181: if(!outputflag) /* won't be printed anyway */
182: break;
183: g_sizearc(hor_pos,ver_pos,c1,c2,c3,c4);
184: bitmapDrawArc(c1,c2,c3,c4);
185: }
186: break;
187: case drawWigglyLine:
188: /* read the rest of the command line containing a
189: * list of x,y pairs into a buffer
190: */
191: (void) fgets(buffer,BUFFERSIZE,fptr);
192:
193: if(!outputflag) /* won't be printed anyway */
194: break;
195: g_sizeWigglyLine(buffer);
196: bitmapDrawWigglyLine(buffer);
197: break;
198: default:
199: flag = 0;
200: break;
201: }
202: }
203: if (!flag)
204: reportError(QUIT,"ill formed draw command");
205: }
206:
207:
208: /*-----------------------------------------------------------------------------
209: * deviceCommand -- process ditroff device commands
210: *---------------------------------------------------------------------------*/
211: deviceCommand(fptr)
212: register FILE *fptr;
213: {
214: int flag,n;
215: int c1;
216: char st[20];
217: char buffer[80];
218:
219: if (flag = (fscanf(fptr,"%s",st) == 1))
220: {
221: switch(*st)
222: {
223: case deviceInit:
224: initDevice();
225: readDeviceInitInfo();
226: break;
227: case deviceName:
228: flag = (fscanf(fptr,"%s",devicename) == 1);
229: break;
230: case deviceResolution:
231: /* expect only resolution in spots per inch */
232: if (flag = (fscanf(fptr,"%d",&spotsPerInch) == 1))
233: setScale(spotsPerInch);
234: break;
235: case devicePause:
236: /* ignore device pauses */
237: break;
238: case deviceStop:
239: resetDevice();
240: break;
241: case deviceTrailer:
242: /* a no op */
243: break;
244: case deviceFont:
245: if (flag = (fscanf(fptr,"%d %s",&n,buffer) == 2))
246: loadFont(n,buffer);
247: break;
248: case deviceHeight:
249: /* a no op */
250: if (flag = (fscanf(fptr,"%d",&c1) == 1))
251: break;
252: case deviceSlant:
253: /* a no op */
254: if (flag = (fscanf(fptr,"%d",&c1) == 1))
255: break;
256: }
257: }
258: if (!flag)
259: reportError(QUIT,"ill formed device command");
260: }
261:
262:
263: /*-----------------------------------------------------------------------------
264: * skipToNextLine -- eat any trailing junk and newline
265: *---------------------------------------------------------------------------*/
266: skipToNextLine(fptr)
267: FILE *fptr;
268: {
269: int n;
270:
271: while ((n = getc(fptr)) != '\n')
272: if (n == EOF) return;
273: linenumber++;
274: }