Blame


1 61f5c35c 2004-05-15 devnull /*
2 61f5c35c 2004-05-15 devnull *
3 61f5c35c 2004-05-15 devnull * PostScript picture inclusion routines. Support for managing in-line pictures
4 61f5c35c 2004-05-15 devnull * has been added, and works in combination with the simple picpack pre-processor
5 61f5c35c 2004-05-15 devnull * that's supplied with this package. An in-line picture begins with a special
6 61f5c35c 2004-05-15 devnull * device control command that looks like,
7 61f5c35c 2004-05-15 devnull *
8 61f5c35c 2004-05-15 devnull * x X InlinPicture name size
9 61f5c35c 2004-05-15 devnull *
10 61f5c35c 2004-05-15 devnull * where name is the pathname of the original picture file and size is the number
11 61f5c35c 2004-05-15 devnull * of bytes in the picture, which begins immediately on the next line. When dpost
12 61f5c35c 2004-05-15 devnull * encounters the InlinePicture device control command inlinepic() is called and
13 61f5c35c 2004-05-15 devnull * that routine appends the string name and the integer size to a temporary file
14 61f5c35c 2004-05-15 devnull * (fp_pic) and then adds the next size bytes read from the current input file to
15 61f5c35c 2004-05-15 devnull * file fp_pic. All in-line pictures are saved in fp_pic and located later using
16 61f5c35c 2004-05-15 devnull * the name string and picture file size that separate pictures saved in fp_pic.
17 61f5c35c 2004-05-15 devnull *
18 61f5c35c 2004-05-15 devnull * When a picture request (ie. an "x X PI" command) is encountered picopen() is
19 61f5c35c 2004-05-15 devnull * called and it first looks for the picture file in fp_pic. If it's found there
20 61f5c35c 2004-05-15 devnull * the entire picture (ie. size bytes) is copied from fp_pic to a new temp file
21 61f5c35c 2004-05-15 devnull * and that temp file is used as the picture file. If there's nothing in fp_pic
22 61f5c35c 2004-05-15 devnull * or if the lookup failed the original route is taken.
23 61f5c35c 2004-05-15 devnull *
24 61f5c35c 2004-05-15 devnull * Support for in-line pictures is an attempt to address requirements, expressed
25 61f5c35c 2004-05-15 devnull * by several organizations, of being able to store a document as a single file
26 61f5c35c 2004-05-15 devnull * (usually troff input) that can then be sent through dpost and ultimately to
27 61f5c35c 2004-05-15 devnull * a PostScript printer. The mechanism may help some users, but the are obvious
28 61f5c35c 2004-05-15 devnull * disadvantages to this approach, and the original mechanism is the recommended
29 61f5c35c 2004-05-15 devnull * approach! Perhaps the most important problem is that troff output, with in-line
30 61f5c35c 2004-05-15 devnull * pictures included, doesn't fit the device independent language accepted by
31 61f5c35c 2004-05-15 devnull * important post-processors (like proff) and that means you won't be able to
32 61f5c35c 2004-05-15 devnull * reliably preview a packed file on your 5620 (or whatever).
33 61f5c35c 2004-05-15 devnull *
34 61f5c35c 2004-05-15 devnull */
35 61f5c35c 2004-05-15 devnull
36 61f5c35c 2004-05-15 devnull #include <u.h>
37 61f5c35c 2004-05-15 devnull #include <libc.h>
38 61f5c35c 2004-05-15 devnull #include <bio.h>
39 61f5c35c 2004-05-15 devnull #include <stdio.h>
40 61f5c35c 2004-05-15 devnull #include "ext.h"
41 61f5c35c 2004-05-15 devnull #include "common.h"
42 61f5c35c 2004-05-15 devnull #include "tr2post.h"
43 61f5c35c 2004-05-15 devnull /* PostScript file structuring comments */
44 61f5c35c 2004-05-15 devnull #include "comments.h"
45 61f5c35c 2004-05-15 devnull /* general purpose definitions */
46 61f5c35c 2004-05-15 devnull /* #include "gen.h" */
47 61f5c35c 2004-05-15 devnull /* just for TEMPDIR definition */
48 61f5c35c 2004-05-15 devnull #include "path.h"
49 61f5c35c 2004-05-15 devnull /* external variable declarations */
50 61f5c35c 2004-05-15 devnull /* #include "ext.h" */
51 61f5c35c 2004-05-15 devnull
52 61f5c35c 2004-05-15 devnull Biobuf *bfp_pic = NULL;
53 e8fb1d3e 2004-05-17 devnull Biobuf *Bfp_pic;
54 e8fb1d3e 2004-05-17 devnull Biobuf *picopen(char *);
55 61f5c35c 2004-05-15 devnull
56 61f5c35c 2004-05-15 devnull #define MAXGETFIELDS 16
57 61f5c35c 2004-05-15 devnull char *fields[MAXGETFIELDS];
58 61f5c35c 2004-05-15 devnull int nfields;
59 61f5c35c 2004-05-15 devnull
60 61f5c35c 2004-05-15 devnull extern int devres, hpos, vpos;
61 61f5c35c 2004-05-15 devnull extern int picflag;
62 61f5c35c 2004-05-15 devnull
63 61f5c35c 2004-05-15 devnull /*****************************************************************************/
64 61f5c35c 2004-05-15 devnull
65 61f5c35c 2004-05-15 devnull void
66 e8fb1d3e 2004-05-17 devnull picture(Biobuf *inp, char *buf) {
67 61f5c35c 2004-05-15 devnull int poffset; /* page offset */
68 61f5c35c 2004-05-15 devnull int indent; /* indent */
69 61f5c35c 2004-05-15 devnull int length; /* line length */
70 61f5c35c 2004-05-15 devnull int totrap; /* distance to next trap */
71 61f5c35c 2004-05-15 devnull char name[100]; /* picture file and page string */
72 61f5c35c 2004-05-15 devnull char hwo[40], *p; /* height, width and offset strings */
73 61f5c35c 2004-05-15 devnull char flags[20]; /* miscellaneous stuff */
74 61f5c35c 2004-05-15 devnull int page = 1; /* page number pulled from name[] */
75 61f5c35c 2004-05-15 devnull double frame[4]; /* height, width, y, and x offsets from hwo[] */
76 61f5c35c 2004-05-15 devnull char units; /* scale indicator for frame dimensions */
77 61f5c35c 2004-05-15 devnull int whiteout = 0; /* white out the box? */
78 61f5c35c 2004-05-15 devnull int outline = 0; /* draw a box around the picture? */
79 61f5c35c 2004-05-15 devnull int scaleboth = 0; /* scale both dimensions? */
80 61f5c35c 2004-05-15 devnull double adjx = 0.5; /* left-right adjustment */
81 61f5c35c 2004-05-15 devnull double adjy = 0.5; /* top-bottom adjustment */
82 61f5c35c 2004-05-15 devnull double rot = 0; /* rotation in clockwise degrees */
83 e8fb1d3e 2004-05-17 devnull Biobuf *fp_in; /* for *name */
84 61f5c35c 2004-05-15 devnull int i; /* loop index */
85 61f5c35c 2004-05-15 devnull
86 61f5c35c 2004-05-15 devnull /*
87 61f5c35c 2004-05-15 devnull *
88 61f5c35c 2004-05-15 devnull * Called from devcntrl() after an 'x X PI' command is found. The syntax of that
89 61f5c35c 2004-05-15 devnull * command is:
90 61f5c35c 2004-05-15 devnull *
91 61f5c35c 2004-05-15 devnull * x X PI:args
92 61f5c35c 2004-05-15 devnull *
93 61f5c35c 2004-05-15 devnull * with args separated by colons and given by:
94 61f5c35c 2004-05-15 devnull *
95 61f5c35c 2004-05-15 devnull * poffset
96 61f5c35c 2004-05-15 devnull * indent
97 61f5c35c 2004-05-15 devnull * length
98 61f5c35c 2004-05-15 devnull * totrap
99 61f5c35c 2004-05-15 devnull * file[(page)]
100 61f5c35c 2004-05-15 devnull * height[,width[,yoffset[,xoffset]]]
101 61f5c35c 2004-05-15 devnull * [flags]
102 61f5c35c 2004-05-15 devnull *
103 61f5c35c 2004-05-15 devnull * poffset, indent, length, and totrap are given in machine units. height, width,
104 61f5c35c 2004-05-15 devnull * and offset refer to the picture frame in inches, unless they're followed by
105 61f5c35c 2004-05-15 devnull * the u scale indicator. flags is a string that provides a little bit of control
106 61f5c35c 2004-05-15 devnull * over the placement of the picture in the frame. Rotation of the picture, in
107 61f5c35c 2004-05-15 devnull * clockwise degrees, is set by the a flag. If it's not followed by an angle
108 61f5c35c 2004-05-15 devnull * the current rotation angle is incremented by 90 degrees, otherwise the angle
109 61f5c35c 2004-05-15 devnull * is set by the number that immediately follows the a.
110 61f5c35c 2004-05-15 devnull *
111 61f5c35c 2004-05-15 devnull */
112 61f5c35c 2004-05-15 devnull
113 61f5c35c 2004-05-15 devnull if (!picflag) /* skip it */
114 61f5c35c 2004-05-15 devnull return;
115 61f5c35c 2004-05-15 devnull endstring();
116 61f5c35c 2004-05-15 devnull
117 61f5c35c 2004-05-15 devnull flags[0] = '\0'; /* just to be safe */
118 61f5c35c 2004-05-15 devnull
119 61f5c35c 2004-05-15 devnull nfields = getfields(buf, fields, MAXGETFIELDS, 0, ":\n");
120 61f5c35c 2004-05-15 devnull if (nfields < 6) {
121 61f5c35c 2004-05-15 devnull error(WARNING, "too few arguments to specify picture");
122 61f5c35c 2004-05-15 devnull return;
123 61f5c35c 2004-05-15 devnull }
124 61f5c35c 2004-05-15 devnull poffset = atoi(fields[1]);
125 61f5c35c 2004-05-15 devnull indent = atoi(fields[2]);
126 61f5c35c 2004-05-15 devnull length = atoi(fields[3]);
127 61f5c35c 2004-05-15 devnull totrap = atoi(fields[4]);
128 61f5c35c 2004-05-15 devnull strncpy(name, fields[5], sizeof(name));
129 61f5c35c 2004-05-15 devnull strncpy(hwo, fields[6], sizeof(hwo));
130 61f5c35c 2004-05-15 devnull if (nfields >= 6)
131 61f5c35c 2004-05-15 devnull strncpy(flags, fields[7], sizeof(flags));
132 61f5c35c 2004-05-15 devnull
133 61f5c35c 2004-05-15 devnull nfields = getfields(buf, fields, MAXGETFIELDS, 0, "()");
134 61f5c35c 2004-05-15 devnull if (nfields == 2) {
135 61f5c35c 2004-05-15 devnull strncpy(name, fields[0], sizeof(name));
136 61f5c35c 2004-05-15 devnull page = atoi(fields[1]);
137 61f5c35c 2004-05-15 devnull }
138 61f5c35c 2004-05-15 devnull
139 61f5c35c 2004-05-15 devnull if ((fp_in = picopen(name)) == NULL) {
140 61f5c35c 2004-05-15 devnull error(WARNING, "can't open picture file %s\n", name);
141 61f5c35c 2004-05-15 devnull return;
142 61f5c35c 2004-05-15 devnull }
143 61f5c35c 2004-05-15 devnull
144 61f5c35c 2004-05-15 devnull frame[0] = frame[1] = -1; /* default frame height, width */
145 61f5c35c 2004-05-15 devnull frame[2] = frame[3] = 0; /* and y and x offsets */
146 61f5c35c 2004-05-15 devnull
147 61f5c35c 2004-05-15 devnull for (i = 0, p = hwo-1; i < 4 && p != NULL; i++, p = strchr(p, ','))
148 61f5c35c 2004-05-15 devnull if (sscanf(++p, "%lf%c", &frame[i], &units) == 2)
149 61f5c35c 2004-05-15 devnull if (units == 'i' || units == ',' || units == '\0')
150 61f5c35c 2004-05-15 devnull frame[i] *= devres;
151 61f5c35c 2004-05-15 devnull
152 61f5c35c 2004-05-15 devnull if (frame[0] <= 0) /* check what we got for height */
153 61f5c35c 2004-05-15 devnull frame[0] = totrap;
154 61f5c35c 2004-05-15 devnull
155 61f5c35c 2004-05-15 devnull if (frame[1] <= 0) /* and width - check too big?? */
156 61f5c35c 2004-05-15 devnull frame[1] = length - indent;
157 61f5c35c 2004-05-15 devnull
158 61f5c35c 2004-05-15 devnull frame[3] += poffset + indent; /* real x offset */
159 61f5c35c 2004-05-15 devnull
160 61f5c35c 2004-05-15 devnull for (i = 0; flags[i]; i++)
161 61f5c35c 2004-05-15 devnull switch (flags[i]) {
162 61f5c35c 2004-05-15 devnull case 'c': adjx = adjy = 0.5; break; /* move to the center */
163 61f5c35c 2004-05-15 devnull case 'l': adjx = 0; break; /* left */
164 61f5c35c 2004-05-15 devnull case 'r': adjx = 1; break; /* right */
165 61f5c35c 2004-05-15 devnull case 't': adjy = 1; break; /* top */
166 61f5c35c 2004-05-15 devnull case 'b': adjy = 0; break; /* or bottom justify */
167 61f5c35c 2004-05-15 devnull case 'o': outline = 1; break; /* outline the picture */
168 61f5c35c 2004-05-15 devnull case 'w': whiteout = 1; break; /* white out the box */
169 61f5c35c 2004-05-15 devnull case 's': scaleboth = 1; break; /* scale both dimensions */
170 61f5c35c 2004-05-15 devnull case 'a': if ( sscanf(&flags[i+1], "%lf", &rot) != 1 )
171 61f5c35c 2004-05-15 devnull rot += 90;
172 61f5c35c 2004-05-15 devnull }
173 61f5c35c 2004-05-15 devnull
174 61f5c35c 2004-05-15 devnull /* restore(); */
175 61f5c35c 2004-05-15 devnull endstring();
176 61f5c35c 2004-05-15 devnull Bprint(Bstdout, "cleartomark\n");
177 61f5c35c 2004-05-15 devnull Bprint(Bstdout, "saveobj restore\n");
178 61f5c35c 2004-05-15 devnull
179 61f5c35c 2004-05-15 devnull ps_include(fp_in, Bstdout, page, whiteout, outline, scaleboth,
180 61f5c35c 2004-05-15 devnull frame[3]+frame[1]/2, -vpos-frame[2]-frame[0]/2, frame[1], frame[0], adjx, adjy, -rot);
181 61f5c35c 2004-05-15 devnull /* save(); */
182 61f5c35c 2004-05-15 devnull Bprint(Bstdout, "/saveobj save def\n");
183 61f5c35c 2004-05-15 devnull Bprint(Bstdout, "mark\n");
184 61f5c35c 2004-05-15 devnull Bterm(fp_in);
185 61f5c35c 2004-05-15 devnull
186 61f5c35c 2004-05-15 devnull }
187 61f5c35c 2004-05-15 devnull
188 61f5c35c 2004-05-15 devnull /*
189 61f5c35c 2004-05-15 devnull *
190 61f5c35c 2004-05-15 devnull * Responsible for finding and opening the next picture file. If we've accumulated
191 61f5c35c 2004-05-15 devnull * any in-line pictures fp_pic won't be NULL and we'll look there first. If *path
192 61f5c35c 2004-05-15 devnull * is found in *fp_pic we create another temp file, open it for update, unlink it,
193 61f5c35c 2004-05-15 devnull * copy in the picture, seek back to the start of the new temp file, and return
194 61f5c35c 2004-05-15 devnull * the file pointer to the caller. If fp_pic is NULL or the lookup fails we just
195 61f5c35c 2004-05-15 devnull * open file *path and return the resulting file pointer to the caller.
196 61f5c35c 2004-05-15 devnull *
197 61f5c35c 2004-05-15 devnull */
198 e8fb1d3e 2004-05-17 devnull Biobuf *
199 61f5c35c 2004-05-15 devnull picopen(char *path) {
200 61f5c35c 2004-05-15 devnull /* char name[100]; /* pathnames */
201 61f5c35c 2004-05-15 devnull /* long pos; /* current position */
202 61f5c35c 2004-05-15 devnull /* long total; /* and sizes - from *fp_pic */
203 61f5c35c 2004-05-15 devnull Biobuf *bfp;
204 e8fb1d3e 2004-05-17 devnull Biobuf *Bfp; /* and pointer for the new temp file */
205 61f5c35c 2004-05-15 devnull
206 61f5c35c 2004-05-15 devnull
207 61f5c35c 2004-05-15 devnull if ((bfp = Bopen(path, OREAD)) == 0)
208 61f5c35c 2004-05-15 devnull error(FATAL, "can't open %s\n", path);
209 b855148c 2004-05-16 devnull Bfp = bfp; /* &(bfp->Biobufhdr); */
210 61f5c35c 2004-05-15 devnull return(Bfp);
211 61f5c35c 2004-05-15 devnull #ifdef UNDEF
212 61f5c35c 2004-05-15 devnull if (Bfp_pic != NULL) {
213 61f5c35c 2004-05-15 devnull Bseek(Bfp_pic, 0L, 0);
214 61f5c35c 2004-05-15 devnull while (Bgetfield(Bfp_pic, 's', name, 99)>0
215 61f5c35c 2004-05-15 devnull && Bgetfield(Bfp_pic, 'd', &total, 0)>0) {
216 61f5c35c 2004-05-15 devnull pos = Bseek(Bfp_pic, 0L, 1);
217 61f5c35c 2004-05-15 devnull if (strcmp(path, name) == 0) {
218 61f5c35c 2004-05-15 devnull if (tmpnam(pictmpname) == NULL)
219 61f5c35c 2004-05-15 devnull error(FATAL, "can't generate temp file name");
220 61f5c35c 2004-05-15 devnull if ( (bfp = Bopen(pictmpname, ORDWR)) == NULL )
221 61f5c35c 2004-05-15 devnull error(FATAL, "can't open %s", pictmpname);
222 b855148c 2004-05-16 devnull Bfp = &(bfp->Biobufhdr);
223 61f5c35c 2004-05-15 devnull piccopy(Bfp_pic, Bfp, total);
224 61f5c35c 2004-05-15 devnull Bseek(Bfp, 0L, 0);
225 61f5c35c 2004-05-15 devnull return(Bfp);
226 61f5c35c 2004-05-15 devnull }
227 61f5c35c 2004-05-15 devnull Bseek(Bfp_pic, total+pos, 0);
228 61f5c35c 2004-05-15 devnull }
229 61f5c35c 2004-05-15 devnull }
230 61f5c35c 2004-05-15 devnull if ((bfp = Bopen(path, OREAD)) == 0)
231 61f5c35c 2004-05-15 devnull Bfp = 0;
232 61f5c35c 2004-05-15 devnull else
233 b855148c 2004-05-16 devnull Bfp = &(bfp->Biobufhdr);
234 61f5c35c 2004-05-15 devnull return(Bfp);
235 61f5c35c 2004-05-15 devnull #endif
236 61f5c35c 2004-05-15 devnull }
237 61f5c35c 2004-05-15 devnull
238 61f5c35c 2004-05-15 devnull /*
239 61f5c35c 2004-05-15 devnull *
240 61f5c35c 2004-05-15 devnull * Adds an in-line picture file to the end of temporary file *Bfp_pic. All pictures
241 61f5c35c 2004-05-15 devnull * grabbed from the input file are saved in the same temp file. Each is preceeded
242 61f5c35c 2004-05-15 devnull * by a one line header that includes the original picture file pathname and the
243 61f5c35c 2004-05-15 devnull * size of the picture in bytes. The in-line picture file is opened for update,
244 61f5c35c 2004-05-15 devnull * left open, and unlinked so it disappears when we do.
245 61f5c35c 2004-05-15 devnull *
246 61f5c35c 2004-05-15 devnull */
247 61f5c35c 2004-05-15 devnull /* *fp; /* current input file */
248 61f5c35c 2004-05-15 devnull /* *buf; /* whatever followed "x X InlinePicture" */
249 61f5c35c 2004-05-15 devnull
250 61f5c35c 2004-05-15 devnull #ifdef UNDEF
251 61f5c35c 2004-05-15 devnull void
252 b855148c 2004-05-16 devnull inlinepic(Biobufhdr *Bfp, char *buf) {
253 61f5c35c 2004-05-15 devnull char name[100]; /* picture file pathname */
254 61f5c35c 2004-05-15 devnull long total; /* and size - both from *buf */
255 61f5c35c 2004-05-15 devnull
256 61f5c35c 2004-05-15 devnull
257 61f5c35c 2004-05-15 devnull if (Bfp_pic == NULL ) {
258 61f5c35c 2004-05-15 devnull tmpnam(pictmpname);
259 61f5c35c 2004-05-15 devnull if ((bfp_pic = Bopen(pictmpname, ORDWR)) == 0)
260 61f5c35c 2004-05-15 devnull error(FATAL, "can't open in-line picture file %s", ipictmpname);
261 61f5c35c 2004-05-15 devnull unlink(pictmpname);
262 61f5c35c 2004-05-15 devnull }
263 61f5c35c 2004-05-15 devnull
264 61f5c35c 2004-05-15 devnull if ( sscanf(buf, "%s %ld", name, &total) != 2 )
265 61f5c35c 2004-05-15 devnull error(FATAL, "in-line picture error");
266 61f5c35c 2004-05-15 devnull
267 61f5c35c 2004-05-15 devnull fseek(Bfp_pic, 0L, 2);
268 61f5c35c 2004-05-15 devnull fprintf(Bfp_pic, "%s %ld\n", name, total);
269 61f5c35c 2004-05-15 devnull getc(fp);
270 61f5c35c 2004-05-15 devnull fflush(fp_pic);
271 61f5c35c 2004-05-15 devnull piccopy(fp, fp_pic, total);
272 61f5c35c 2004-05-15 devnull ungetc('\n', fp);
273 61f5c35c 2004-05-15 devnull
274 61f5c35c 2004-05-15 devnull }
275 61f5c35c 2004-05-15 devnull #endif
276 61f5c35c 2004-05-15 devnull
277 61f5c35c 2004-05-15 devnull /*
278 61f5c35c 2004-05-15 devnull *
279 61f5c35c 2004-05-15 devnull * Copies total bytes from file fp_in to fp_out. Used to append picture files to
280 61f5c35c 2004-05-15 devnull * *fp_pic and then copy them to yet another temporary file immediately before
281 61f5c35c 2004-05-15 devnull * they're used (in picture()).
282 61f5c35c 2004-05-15 devnull *
283 61f5c35c 2004-05-15 devnull */
284 61f5c35c 2004-05-15 devnull /* *fp_in; input */
285 61f5c35c 2004-05-15 devnull /* *fp_out; and output file pointers */
286 61f5c35c 2004-05-15 devnull /* total; number of bytes to be copied */
287 61f5c35c 2004-05-15 devnull void
288 e8fb1d3e 2004-05-17 devnull piccopy(Biobuf *Bfp_in, Biobuf *Bfp_out, long total) {
289 61f5c35c 2004-05-15 devnull long i;
290 61f5c35c 2004-05-15 devnull
291 61f5c35c 2004-05-15 devnull for (i = 0; i < total; i++)
292 61f5c35c 2004-05-15 devnull if (Bputc(Bfp_out, Bgetc(Bfp_in)) < 0)
293 61f5c35c 2004-05-15 devnull error(FATAL, "error copying in-line picture file");
294 61f5c35c 2004-05-15 devnull Bflush(Bfp_out);
295 61f5c35c 2004-05-15 devnull }