pictures.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. #include <u.h>
  36. #include <libc.h>
  37. #include <bio.h>
  38. #include <stdio.h>
  39. #include "ext.h"
  40. #include "common.h"
  41. #include "tr2post.h"
  42. /* PostScript file structuring comments */
  43. #include "comments.h"
  44. /* general purpose definitions */
  45. /* #include "gen.h" */
  46. /* just for TEMPDIR definition */
  47. #include "path.h"
  48. /* external variable declarations */
  49. /* #include "ext.h" */
  50. Biobuf *bfp_pic = NULL;
  51. Biobufhdr *Bfp_pic;
  52. Biobufhdr *picopen(char *);
  53. #define MAXGETFIELDS 16
  54. char *fields[MAXGETFIELDS];
  55. int nfields;
  56. extern int devres, hpos, vpos;
  57. extern int picflag;
  58. /*****************************************************************************/
  59. void
  60. picture(Biobufhdr *inp, char *buf) {
  61. int poffset; /* page offset */
  62. int indent; /* indent */
  63. int length; /* line length */
  64. int totrap; /* distance to next trap */
  65. char name[100]; /* picture file and page string */
  66. char hwo[40], *p; /* height, width and offset strings */
  67. char flags[20]; /* miscellaneous stuff */
  68. int page = 1; /* page number pulled from name[] */
  69. double frame[4]; /* height, width, y, and x offsets from hwo[] */
  70. char units; /* scale indicator for frame dimensions */
  71. int whiteout = 0; /* white out the box? */
  72. int outline = 0; /* draw a box around the picture? */
  73. int scaleboth = 0; /* scale both dimensions? */
  74. double adjx = 0.5; /* left-right adjustment */
  75. double adjy = 0.5; /* top-bottom adjustment */
  76. double rot = 0; /* rotation in clockwise degrees */
  77. Biobufhdr *fp_in; /* for *name */
  78. int i; /* loop index */
  79. /*
  80. *
  81. * Called from devcntrl() after an 'x X PI' command is found. The syntax of that
  82. * command is:
  83. *
  84. * x X PI:args
  85. *
  86. * with args separated by colons and given by:
  87. *
  88. * poffset
  89. * indent
  90. * length
  91. * totrap
  92. * file[(page)]
  93. * height[,width[,yoffset[,xoffset]]]
  94. * [flags]
  95. *
  96. * poffset, indent, length, and totrap are given in machine units. height, width,
  97. * and offset refer to the picture frame in inches, unless they're followed by
  98. * the u scale indicator. flags is a string that provides a little bit of control
  99. * over the placement of the picture in the frame. Rotation of the picture, in
  100. * clockwise degrees, is set by the a flag. If it's not followed by an angle
  101. * the current rotation angle is incremented by 90 degrees, otherwise the angle
  102. * is set by the number that immediately follows the a.
  103. *
  104. */
  105. if (!picflag) /* skip it */
  106. return;
  107. endstring();
  108. flags[0] = '\0'; /* just to be safe */
  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. nfields = getfields(buf, fields, MAXGETFIELDS, 0, "()");
  123. if (nfields == 2) {
  124. strncpy(name, fields[0], sizeof(name));
  125. page = atoi(fields[1]);
  126. }
  127. if ((fp_in = picopen(name)) == NULL) {
  128. error(WARNING, "can't open picture file %s\n", name);
  129. return;
  130. }
  131. frame[0] = frame[1] = -1; /* default frame height, width */
  132. frame[2] = frame[3] = 0; /* and y and x offsets */
  133. for (i = 0, p = hwo-1; i < 4 && p != NULL; i++, p = strchr(p, ','))
  134. if (sscanf(++p, "%lf%c", &frame[i], &units) == 2)
  135. if (units == 'i' || units == ',' || units == '\0')
  136. frame[i] *= devres;
  137. if (frame[0] <= 0) /* check what we got for height */
  138. frame[0] = totrap;
  139. if (frame[1] <= 0) /* and width - check too big?? */
  140. frame[1] = length - indent;
  141. frame[3] += poffset + indent; /* real x offset */
  142. for (i = 0; flags[i]; i++)
  143. switch (flags[i]) {
  144. case 'c': adjx = adjy = 0.5; break; /* move to the center */
  145. case 'l': adjx = 0; break; /* left */
  146. case 'r': adjx = 1; break; /* right */
  147. case 't': adjy = 1; break; /* top */
  148. case 'b': adjy = 0; break; /* or bottom justify */
  149. case 'o': outline = 1; break; /* outline the picture */
  150. case 'w': whiteout = 1; break; /* white out the box */
  151. case 's': scaleboth = 1; break; /* scale both dimensions */
  152. case 'a': if ( sscanf(&flags[i+1], "%lf", &rot) != 1 )
  153. rot += 90;
  154. }
  155. /* restore(); */
  156. endstring();
  157. Bprint(Bstdout, "cleartomark\n");
  158. Bprint(Bstdout, "saveobj restore\n");
  159. ps_include(fp_in, Bstdout, page, whiteout, outline, scaleboth,
  160. frame[3]+frame[1]/2, -vpos-frame[2]-frame[0]/2, frame[1], frame[0], adjx, adjy, -rot);
  161. /* save(); */
  162. Bprint(Bstdout, "/saveobj save def\n");
  163. Bprint(Bstdout, "mark\n");
  164. Bterm(fp_in);
  165. }
  166. /*
  167. *
  168. * Responsible for finding and opening the next picture file. If we've accumulated
  169. * any in-line pictures fp_pic won't be NULL and we'll look there first. If *path
  170. * is found in *fp_pic we create another temp file, open it for update, unlink it,
  171. * copy in the picture, seek back to the start of the new temp file, and return
  172. * the file pointer to the caller. If fp_pic is NULL or the lookup fails we just
  173. * open file *path and return the resulting file pointer to the caller.
  174. *
  175. */
  176. Biobufhdr *
  177. picopen(char *path) {
  178. /* char name[100]; /* pathnames */
  179. /* long pos; /* current position */
  180. /* long total; /* and sizes - from *fp_pic */
  181. Biobuf *bfp;
  182. Biobufhdr *Bfp; /* and pointer for the new temp file */
  183. if ((bfp = Bopen(path, OREAD)) == 0)
  184. error(FATAL, "can't open %s\n", path);
  185. Bfp = &(bfp->Biobufhdr);
  186. return(Bfp);
  187. #ifdef UNDEF
  188. if (Bfp_pic != NULL) {
  189. Bseek(Bfp_pic, 0L, 0);
  190. while (Bgetfield(Bfp_pic, 's', name, 99)>0
  191. && Bgetfield(Bfp_pic, 'd', &total, 0)>0) {
  192. pos = Bseek(Bfp_pic, 0L, 1);
  193. if (strcmp(path, name) == 0) {
  194. if (tmpnam(pictmpname) == NULL)
  195. error(FATAL, "can't generate temp file name");
  196. if ( (bfp = Bopen(pictmpname, ORDWR)) == NULL )
  197. error(FATAL, "can't open %s", pictmpname);
  198. Bfp = &(bfp->Biobufhdr);
  199. piccopy(Bfp_pic, Bfp, total);
  200. Bseek(Bfp, 0L, 0);
  201. return(Bfp);
  202. }
  203. Bseek(Bfp_pic, total+pos, 0);
  204. }
  205. }
  206. if ((bfp = Bopen(path, OREAD)) == 0)
  207. Bfp = 0;
  208. else
  209. Bfp = &(bfp->Biobufhdr);
  210. return(Bfp);
  211. #endif
  212. }
  213. /*
  214. *
  215. * Adds an in-line picture file to the end of temporary file *Bfp_pic. All pictures
  216. * grabbed from the input file are saved in the same temp file. Each is preceeded
  217. * by a one line header that includes the original picture file pathname and the
  218. * size of the picture in bytes. The in-line picture file is opened for update,
  219. * left open, and unlinked so it disappears when we do.
  220. *
  221. */
  222. /* *fp; /* current input file */
  223. /* *buf; /* whatever followed "x X InlinePicture" */
  224. #ifdef UNDEF
  225. void
  226. inlinepic(Biobufhdr *Bfp, char *buf) {
  227. char name[100]; /* picture file pathname */
  228. long total; /* and size - both from *buf */
  229. if (Bfp_pic == NULL ) {
  230. tmpnam(pictmpname);
  231. if ((bfp_pic = Bopen(pictmpname, ORDWR)) == 0)
  232. error(FATAL, "can't open in-line picture file %s", ipictmpname);
  233. unlink(pictmpname);
  234. }
  235. if ( sscanf(buf, "%s %ld", name, &total) != 2 )
  236. error(FATAL, "in-line picture error");
  237. fseek(Bfp_pic, 0L, 2);
  238. fprintf(Bfp_pic, "%s %ld\n", name, total);
  239. getc(fp);
  240. fflush(fp_pic);
  241. piccopy(fp, fp_pic, total);
  242. ungetc('\n', fp);
  243. }
  244. #endif
  245. /*
  246. *
  247. * Copies total bytes from file fp_in to fp_out. Used to append picture files to
  248. * *fp_pic and then copy them to yet another temporary file immediately before
  249. * they're used (in picture()).
  250. *
  251. */
  252. /* *fp_in; input */
  253. /* *fp_out; and output file pointers */
  254. /* total; number of bytes to be copied */
  255. void
  256. piccopy(Biobufhdr *Bfp_in, Biobufhdr *Bfp_out, long total) {
  257. long i;
  258. for (i = 0; i < total; i++)
  259. if (Bputc(Bfp_out, Bgetc(Bfp_in)) < 0)
  260. error(FATAL, "error copying in-line picture file");
  261. Bflush(Bfp_out);
  262. }