|
|
1.1 root 1: /* Copyright (c) 1989 AT&T */
2: /* All Rights Reserved */
3:
4: /* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T */
5: /* The copyright notice above does not evidence any */
6: /* actual or intended publication of such source code. */
7:
8: /* @(#)picasso:ps_include.c 1.0 */
9:
10: /*
11: *
12: * Picture inclusion code for PostScript printers, taken from Rich Drechsler
13: *
14: */
15:
16: #include "picasso.h"
17: #include "ps_include.h"
18:
19: /* in SVR4, the C-preprocessor will not accept the tricky definitions of lq
20: and rq. However, we should be able to do it using the following
21:
22: define var(x) fprintf(fout, "/%s %g def\n", #x, x)
23: */
24: #define lq(x) "x
25: #define rq(x) x"
26: #define quote(x) rq(lq(x))
27: #define var(x) fprintf(fout, "/%s %g def\n", quote(x), x)
28: #define has(word) (strncmp(buf, word, strlen(word)) == 0)
29: #define grab(n) ((Section *)(nglobal \
30: ? realloc((char *)global, n*sizeof(Section)) \
31: : calloc(n, sizeof(Section))))
32:
33: char buf[512];
34: typedef struct {long start, end;} Section;
35:
36: puteqn(x, y, type, neqn)
37: double x, y;
38: int type, neqn;
39: {
40: extern FILE *eqnfp, *outfp;
41: extern float eqn_move;
42: double ax, ay;
43:
44: ax = 0;
45: ay = 0;
46: if (type & LJUST|RJUST|ABOVE|BELOW) {
47: /* modify ax,ay if text_bounds can get the bounding box */
48: }
49: if (eqnfp != NULL)
50: ps_include(eqnfp, outfp, neqn, x, y + eqn_move, 0., 0., ax, ay/*,0.*/);
51: }
52:
53: ps_include(fin, fout, page_no, cx, cy, sx, sy, ax, ay /*, rot */)
54:
55: FILE *fin, *fout; /* input and output files */
56: int page_no; /* physical page number from *fin */
57: double cx, cy; /* center of the picture and */
58: double sx, sy; /* its size - in current coordinates */
59: double ax, ay; /* left-right, up-down adjustment */
60: /* double rot; /* rotation - in clockwise degrees */
61: {
62: int foundpage = 0; /* found the page when non zero */
63: int foundpbox = 0; /* found the page bounding box */
64: int nglobal = 0; /* number of global defs so far */
65: int maxglobal = 0; /* and the number we've got room for */
66: Section prolog, setup, /* major divisions in document */
67: page, trailer;
68: Section *global; /* offsets for all global definitions */
69: double llx, lly; /* lower left and upper right corners */
70: double urx, ury; /* (default -- printer pt -- coords) */
71: int i = 0; /* found page number */
72: /*
73: *
74: * Reads a PostScript file (*fin), and uses structuring comments to locate the
75: * prologue, trailer, global definitions, and the requested page. After the
76: * file is scanned, the special ps_include PostScript definitions are copied to
77: * *fout, followed by the prologue, global definitions, the requested page, and
78: * the trailer. Before returning the initial environment (saved in PS_head) is
79: * restored.
80: *
81: * By default we assume the picture is 8.5 by 11 inches, but the BoundingBox
82: * comment, if found, takes precedence.
83: *
84: */
85:
86: llx = lly = 0; /* default BoundingBox - 8.5x11 inches */
87: urx = 72 * 8.5;
88: ury = 72 * 11.0;
89:
90: /* section boundaries and bounding box */
91:
92: prolog.start = prolog.end = 0;
93: setup.start = setup.end = 0;
94: page.start = page.end = 0;
95: trailer.start = 0;
96: fseek(fin, 0L, 0);
97:
98: while ( fgets(buf, sizeof(buf), fin) != NULL ) {
99: if (!has("%%"))
100: continue;
101: else if (has("%%PageBoundingBox: (atend)")
102: || has("%%BoundingBox: (atend)"))
103: continue;
104: else if (has("%%Page: ")) {
105: if (!foundpage)
106: page.start = ftell(fin);
107: sscanf(buf, "%*s %*s %d", &i);
108: if (i == page_no)
109: foundpage = 1;
110: else if (foundpage && page.end <= page.start)
111: page.end = ftell(fin);
112: } else if (has("%%EndPage: ")) {
113: sscanf(buf, "%*s %*s %d", &i);
114: if (i == page_no) {
115: foundpage = 1;
116: page.end = ftell(fin);
117: }
118: if (!foundpage)
119: page.start = ftell(fin);
120: } else if (has("%%PageBoundingBox: ")) {
121: if (i > page_no & !foundpbox) {
122: foundpbox = 1;
123: llx = lly = urx = ury = 0;
124: } else if (i == page_no) {
125: sscanf(buf, "%*s %lf %lf %lf %lf",
126: &llx, &lly, &urx, &ury);
127: foundpbox = 1;
128: }
129: } else if (has("%%BoundingBox: ")) {
130: if (!foundpbox)
131: sscanf(buf,"%*s %lf %lf %lf %lf",
132: &llx, &lly, &urx, &ury);
133: } else if (has("%%EndProlog"))
134: prolog.end = setup.start = page.start = ftell(fin);
135: else if (has("%%BeginSetup")
136: || has("%%BeginDocumentSetup")) {
137:
138: if (!setup.start) setup.start = ftell(fin);
139: } else if (has("%%EndSetup")
140: || has("%%EndDocumentSetup"))
141: setup.end = page.start = ftell(fin);
142: else if (has("%%Trailer"))
143: trailer.start = ftell(fin);
144: else if (has("%%BeginGlobal"))
145: if (page.end <= page.start) {
146: if (nglobal >= maxglobal) {
147: maxglobal += 20;
148: global = grab(maxglobal);
149: }
150: global[nglobal].start = ftell(fin);
151: }
152: else if (has("%%EndGlobal"))
153: if (page.end <= page.start)
154: global[nglobal++].end = ftell(fin);
155: }
156: if (urx == llx && ury == lly)
157: return;
158: fseek(fin, 0L, 2);
159: if (trailer.start == 0)
160: trailer.start = ftell(fin);
161: trailer.end = ftell(fin);
162:
163: if (page.end <= page.start)
164: page.end = trailer.start;
165:
166: ps_print(fout, PS_head);
167: if (sx == 0)
168: sx = (urx-llx)/pgscale;
169: if (sy == 0)
170: sy = (ury-lly)/pgscale;
171: track_bounds(cx-sx/2, cy-sy/2, cx+sy/2, cy+sy/2);
172:
173: fprintf(fout, "/llx %g def\n", llx);
174: fprintf(fout, "/lly %g def\n", lly);
175: fprintf(fout, "/urx %g def\n", urx);
176: fprintf(fout, "/ury %g def\n", ury);
177: fprintf(fout, "/cx %g def\n", cx);
178: fprintf(fout, "/cy %g def\n", cy);
179: fprintf(fout, "/sx %g def\n", sx);
180: fprintf(fout, "/sy %g def\n", sy);
181: fprintf(fout, "/ax %g def\n", ax);
182: fprintf(fout, "/ay %g def\n", ay);
183:
184: ps_print(fout, PS_setup);
185: ps_copy(fin, fout, &prolog);
186: fprintf(fout,"/useclippath false def\n");
187: ps_copy(fin, fout, &setup);
188: for(i = 0; i < nglobal; i++)
189: ps_copy(fin, fout, &global[i]);
190: ps_copy(fin, fout, &page);
191: ps_copy(fin, fout, &trailer);
192: ps_print(fout, PS_tail);
193: if(nglobal)
194: free(global);
195: }
196:
197: pic_include(fin, fout, page_no, o)
198: FILE *fin, *fout; /* input and output files */
199: int page_no; /* physical page number from *fin */
200: obj *o; /* has bounds and transformation */
201: {
202: int foundpage = 0; /* found the page when non zero */
203: int foundpbox = 0; /* found the page bounding box */
204: int nglobal = 0; /* number of global defs so far */
205: int maxglobal = 0; /* and the number we've got room for */
206: Section prolog, setup, /* major divisions in document */
207: page, trailer;
208: Section *global; /* offsets for all global definitions */
209: double cx, cy;
210: double sx, sy;
211: double llx, lly; /* lower left and upper right corners */
212: double urx, ury; /* (default -- printer pt -- coords) */
213: int i = 0; /* found page number */
214: /*
215: *
216: * Reads a PostScript file (*fin), and uses structuring comments to locate the
217: * prologue, trailer, global definitions, and the requested page. After the
218: * file is scanned, the special ps_include PostScript definitions are copied to
219: * *fout, followed by the prologue, global definitions, the requested page, and
220: * the trailer. Before returning the initial environment (saved in PS_head) is
221: * restored.
222: *
223: * By default we assume the picture is 8.5 by 11 inches, but the BoundingBox
224: * comment, if found, takes precedence.
225: *
226: */
227:
228: llx = lly = 0; /* default BoundingBox - 8.5x11 inches */
229: urx = 72 * 8.5;
230: ury = 72 * 11.0;
231: cx = o->o_x;
232: cy = o->o_y;
233: sx = o->o_wid;
234: sy = o->o_ht;
235:
236: /* section boundaries and bounding box */
237:
238: prolog.start = prolog.end = 0;
239: setup.start = setup.end = 0;
240: page.start = page.end = 0;
241: trailer.start = 0;
242: fseek(fin, 0L, 0);
243:
244: while ( fgets(buf, sizeof(buf), fin) != NULL ) {
245: if (!has("%%"))
246: continue;
247: else if (has("%%PageBoundingBox: (atend)")
248: || has("%%BoundingBox: (atend)"))
249: continue;
250: else if (has("%%Page: ")) {
251: if (!foundpage)
252: page.start = ftell(fin);
253: sscanf(buf, "%*s %*s %d", &i);
254: if (i == page_no)
255: foundpage = 1;
256: else if (foundpage && page.end <= page.start)
257: page.end = ftell(fin);
258: } else if (has("%%EndPage: ")) {
259: sscanf(buf, "%*s %*s %d", &i);
260: if (i == page_no) {
261: foundpage = 1;
262: page.end = ftell(fin);
263: }
264: if (!foundpage)
265: page.start = ftell(fin);
266: } else if (has("%%PageBoundingBox: ")) {
267: if (i > page_no & !foundpbox) {
268: foundpbox = 1;
269: llx = lly = urx = ury = 0;
270: } else if (i == page_no) {
271: sscanf(buf, "%*s %lf %lf %lf %lf",
272: &llx, &lly, &urx, &ury);
273: foundpbox = 1;
274: }
275: } else if (has("%%BoundingBox: ")) {
276: if (!foundpbox)
277: sscanf(buf,"%*s %lf %lf %lf %lf",
278: &llx, &lly, &urx, &ury);
279: } else if (has("%%EndProlog"))
280: prolog.end = setup.start = page.start = ftell(fin);
281: else if (has("%%BeginSetup")
282: || has("%%BeginDocumentSetup")) {
283:
284: if (!setup.start) setup.start = ftell(fin);
285: } else if (has("%%EndSetup")
286: || has("%%EndDocumentSetup"))
287: setup.end = page.start = ftell(fin);
288: else if (has("%%Trailer"))
289: trailer.start = ftell(fin);
290: else if (has("%%BeginGlobal"))
291: if (page.end <= page.start) {
292: if (nglobal >= maxglobal) {
293: maxglobal += 20;
294: global = grab(maxglobal);
295: }
296: global[nglobal].start = ftell(fin);
297: }
298: else if (has("%%EndGlobal"))
299: if (page.end <= page.start)
300: global[nglobal++].end = ftell(fin);
301: }
302: if (urx == llx && ury == lly)
303: return;
304: fseek(fin, 0L, 2);
305: if (trailer.start == 0)
306: trailer.start = ftell(fin);
307: trailer.end = ftell(fin);
308:
309: if (page.end <= page.start)
310: page.end = trailer.start;
311:
312: ps_print(fout, PS_head);
313: if (sx == 0)
314: sx = (urx-llx)/pgscale;
315: if (sy == 0)
316: sy = (ury-lly)/pgscale;
317: track_bounds(cx-sx/2, cy-sy/2, cx+sy/2, cy+sy/2);
318:
319: fprintf(fout, "/llx %g def\n", llx);
320: fprintf(fout, "/lly %g def\n", lly);
321: fprintf(fout, "/urx %g def\n", urx);
322: fprintf(fout, "/ury %g def\n", ury);
323: fprintf(fout, "/cx %g def\n", cx);
324: fprintf(fout, "/cy %g def\n", cy);
325: fprintf(fout, "/sx %g def\n", sx);
326: fprintf(fout, "/sy %g def\n", sy);
327: fprintf(fout, "/B [ %g %g %g %g 0 0 ] def\n", o->o_mxx, o->o_myx,
328: o->o_mxy, o->o_myy);
329:
330: ps_print(fout, Pic_setup);
331: ps_copy(fin, fout, &prolog);
332: fprintf(fout,"/useclippath false def\n");
333: ps_copy(fin, fout, &setup);
334: for(i = 0; i < nglobal; i++)
335: ps_copy(fin, fout, &global[i]);
336: ps_copy(fin, fout, &page);
337: ps_copy(fin, fout, &trailer);
338: ps_print(fout, PS_tail);
339: if(nglobal)
340: free(global);
341: }
342:
343: static
344: ps_print(fout, s)
345: FILE *fout;
346: char **s;
347: {
348: while (*s)
349: fprintf(fout, "%s\n", *s++);
350: }
351:
352: static
353: ps_copy(fin, fout, s)
354: FILE *fin, *fout;
355: Section *s;
356: {
357: if (s->end <= s->start)
358: return;
359: fseek(fin, s->start, 0);
360: while (ftell(fin) < s->end && fgets(buf, sizeof(buf), fin) != NULL)
361: if (buf[0] != '%')
362: fprintf(fout, "%s", buf);
363: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.