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