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