gdevpsu.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /* Copyright (C) 2000 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: gdevpsu.c,v 1.19 2005/03/02 18:08:38 raph Exp $ */
  14. /* PostScript-writing utilities */
  15. #include "math_.h"
  16. #include "time_.h"
  17. #include "stat_.h"
  18. #include "unistd_.h"
  19. #include "gx.h"
  20. #include "gscdefs.h"
  21. #include "gxdevice.h"
  22. #include "gdevpsu.h"
  23. #include "spprint.h"
  24. #include "stream.h"
  25. #include "gserrors.h"
  26. /* ---------------- Low level ---------------- */
  27. /* Write a 0-terminated array of strings as lines. */
  28. int
  29. psw_print_lines(FILE *f, const char *const lines[])
  30. {
  31. int i;
  32. for (i = 0; lines[i] != 0; ++i) {
  33. if (fprintf(f, "%s\n", lines[i]) < 0)
  34. return_error(gs_error_ioerror);
  35. }
  36. return 0;
  37. }
  38. /* Write the ProcSet name. */
  39. private void
  40. psw_put_procset_name(stream *s, const gx_device *dev,
  41. const gx_device_pswrite_common_t *pdpc)
  42. {
  43. pprints1(s, "GS_%s", dev->dname);
  44. pprintd3(s, "_%d_%d_%d",
  45. (int)pdpc->LanguageLevel,
  46. (int)(pdpc->LanguageLevel * 10 + 0.5) % 10,
  47. pdpc->ProcSet_version);
  48. }
  49. private void
  50. psw_print_procset_name(FILE *f, const gx_device *dev,
  51. const gx_device_pswrite_common_t *pdpc)
  52. {
  53. byte buf[100]; /* arbitrary */
  54. stream s;
  55. s_init(&s, dev->memory);
  56. swrite_file(&s, f, buf, sizeof(buf));
  57. psw_put_procset_name(&s, dev, pdpc);
  58. sflush(&s);
  59. }
  60. /* Write a bounding box. */
  61. private void
  62. psw_print_bbox(FILE *f, const gs_rect *pbbox)
  63. {
  64. fprintf(f, "%%%%BoundingBox: %d %d %d %d\n",
  65. (int)floor(pbbox->p.x), (int)floor(pbbox->p.y),
  66. (int)ceil(pbbox->q.x), (int)ceil(pbbox->q.y));
  67. fprintf(f, "%%%%HiResBoundingBox: %f %f %f %f\n",
  68. pbbox->p.x, pbbox->p.y, pbbox->q.x, pbbox->q.y);
  69. }
  70. /* ---------------- File level ---------------- */
  71. private const char *const psw_ps_header[] = {
  72. "%!PS-Adobe-3.0",
  73. "%%Pages: (atend)",
  74. 0
  75. };
  76. private const char *const psw_eps_header[] = {
  77. "%!PS-Adobe-3.0 EPSF-3.0",
  78. 0
  79. };
  80. private const char *const psw_begin_prolog[] = {
  81. "%%EndComments",
  82. "%%BeginProlog",
  83. "% This copyright applies to everything between here and the %%EndProlog:",
  84. /* copyright */
  85. /* begin ProcSet */
  86. 0
  87. };
  88. /*
  89. * To achieve page independence, every page must in the general case
  90. * set page parameters. To preserve duplexing the page cannot set page
  91. * parameters. The following code checks the current page size and sets
  92. * it only if it is necessary.
  93. */
  94. private const char *const psw_ps_procset[] = {
  95. /* <w> <h> <sizename> setpagesize - */
  96. "/PageSize 2 array def"
  97. "/setpagesize" /* x y /a4 -> - */
  98. "{ PageSize aload pop " /* x y /a4 x0 y0 */
  99. "3 index eq exch", /* x y /a4 bool x0 */
  100. "4 index eq and" /* x y /a4 bool */
  101. "{ pop pop pop"
  102. "}"
  103. "{ PageSize dup 1", /* x y /a4 [ ] [ ] 1 */
  104. "5 -1 roll put 0 " /* x /a4 [ y] 0 */
  105. "4 -1 roll put " /* /a4 */
  106. "dup null eq {false} {dup where} ifelse"
  107. "{ exch get exec" /* - */
  108. "}",
  109. "{ pop" /* - */
  110. "/setpagedevice where",
  111. "{ pop 1 dict dup /PageSize PageSize put setpagedevice"
  112. "}",
  113. "{ /setpage where"
  114. "{ pop PageSize aload pop pageparams 3 {exch pop} repeat",
  115. "setpage"
  116. "}"
  117. "if"
  118. "}"
  119. "ifelse"
  120. "}"
  121. "ifelse"
  122. "}"
  123. "ifelse"
  124. "} bind def",
  125. 0
  126. };
  127. private const char *const psw_end_prolog[] = {
  128. "end readonly def",
  129. "%%EndResource", /* ProcSet */
  130. "/pagesave null def", /* establish binding */
  131. "%%EndProlog",
  132. 0
  133. };
  134. /* Return true when the file is seekable.
  135. * On Windows NT ftell() returns some non-EOF value when used on pipes.
  136. */
  137. private bool
  138. is_seekable(FILE *f)
  139. {
  140. struct stat buf;
  141. if(fstat(fileno(f), &buf))
  142. return 0;
  143. return S_ISREG(buf.st_mode);
  144. }
  145. /*
  146. * Write the file header, up through the BeginProlog. This must write to a
  147. * file, not a stream, because it may be called during finalization.
  148. */
  149. int
  150. psw_begin_file_header(FILE *f, const gx_device *dev, const gs_rect *pbbox,
  151. gx_device_pswrite_common_t *pdpc, bool ascii)
  152. {
  153. psw_print_lines(f, (pdpc->ProduceEPS ? psw_eps_header : psw_ps_header));
  154. if (pbbox) {
  155. psw_print_bbox(f, pbbox);
  156. pdpc->bbox_position = 0;
  157. } else if (!is_seekable(f)) { /* File is not seekable. */
  158. pdpc->bbox_position = -1;
  159. fputs("%%BoundingBox: (atend)\n", f);
  160. fputs("%%HiResBoundingBox: (atend)\n", f);
  161. } else { /* File is seekable, leave room to rewrite bbox. */
  162. pdpc->bbox_position = ftell(f);
  163. fputs("%...............................................................\n", f);
  164. fputs("%...............................................................\n", f);
  165. }
  166. fprintf(f, "%%%%Creator: %s %ld (%s)\n", gs_product, (long)gs_revision,
  167. dev->dname);
  168. {
  169. time_t t;
  170. struct tm tms;
  171. time(&t);
  172. tms = *localtime(&t);
  173. fprintf(f, "%%%%CreationDate: %d/%02d/%02d %02d:%02d:%02d\n",
  174. tms.tm_year + 1900, tms.tm_mon + 1, tms.tm_mday,
  175. tms.tm_hour, tms.tm_min, tms.tm_sec);
  176. }
  177. if (ascii)
  178. fputs("%%DocumentData: Clean7Bit\n", f);
  179. if (pdpc->LanguageLevel >= 2.0)
  180. fprintf(f, "%%%%LanguageLevel: %d\n", (int)pdpc->LanguageLevel);
  181. else if (pdpc->LanguageLevel == 1.5)
  182. fputs("%%Extensions: CMYK\n", f);
  183. psw_print_lines(f, psw_begin_prolog);
  184. fprintf(f, "%% %s\n", gs_copyright);
  185. fputs("%%BeginResource: procset ", f);
  186. fflush(f);
  187. psw_print_procset_name(f, dev, pdpc);
  188. fputs("\n/", f);
  189. fflush(f);
  190. psw_print_procset_name(f, dev, pdpc);
  191. fputs(" 80 dict dup begin\n", f);
  192. psw_print_lines(f, psw_ps_procset);
  193. fflush(f);
  194. if (ferror(f))
  195. return_error(gs_error_ioerror);
  196. return 0;
  197. }
  198. /*
  199. * End the file header.
  200. */
  201. int
  202. psw_end_file_header(FILE *f)
  203. {
  204. return psw_print_lines(f, psw_end_prolog);
  205. }
  206. /*
  207. * End the file.
  208. */
  209. int
  210. psw_end_file(FILE *f, const gx_device *dev,
  211. const gx_device_pswrite_common_t *pdpc, const gs_rect *pbbox,
  212. int /* should be long */ page_count)
  213. {
  214. if (f == NULL)
  215. return 0; /* clients should be more careful */
  216. fprintf(f, "%%%%Trailer\n%%%%Pages: %ld\n", (long)page_count);
  217. if (ferror(f))
  218. return_error(gs_error_ioerror);
  219. if (dev->PageCount > 0 && pdpc->bbox_position != 0) {
  220. if (pdpc->bbox_position >= 0) {
  221. long save_pos = ftell(f);
  222. fseek(f, pdpc->bbox_position, SEEK_SET);
  223. psw_print_bbox(f, pbbox);
  224. fputc('%', f);
  225. if (ferror(f))
  226. return_error(gs_error_ioerror);
  227. fseek(f, save_pos, SEEK_SET);
  228. } else
  229. psw_print_bbox(f, pbbox);
  230. }
  231. if (!pdpc->ProduceEPS)
  232. fputs("%%EOF\n", f);
  233. if (ferror(f))
  234. return_error(gs_error_ioerror);
  235. return 0;
  236. }
  237. /* ---------------- Page level ---------------- */
  238. /*
  239. * Write the page header.
  240. */
  241. int
  242. psw_write_page_header(stream *s, const gx_device *dev,
  243. const gx_device_pswrite_common_t *pdpc,
  244. bool do_scale, long page_ord, int dictsize)
  245. {
  246. long page = dev->PageCount + 1;
  247. pprintld2(s, "%%%%Page: %ld %ld\n%%%%BeginPageSetup\n", page, page_ord);
  248. /*
  249. * Adobe's documentation says that page setup must be placed outside the
  250. * save/restore that encapsulates the page contents, and that the
  251. * showpage must be placed after the restore. This means that to
  252. * achieve page independence, *every* page's setup code must include a
  253. * setpagedevice that sets *every* page device parameter that is changed
  254. * on *any* page. Currently, the only such parameter relevant to this
  255. * driver is page size, but there might be more in the future.
  256. */
  257. psw_put_procset_name(s, dev, pdpc);
  258. stream_puts(s, " begin\n");
  259. if (!pdpc->ProduceEPS) {
  260. int width = (int)(dev->width * 72.0 / dev->HWResolution[0] + 0.5);
  261. int height = (int)(dev->height * 72.0 / dev->HWResolution[1] + 0.5);
  262. typedef struct ps_ {
  263. const char *size_name;
  264. int width, height;
  265. } page_size;
  266. static const page_size sizes[] = {
  267. {"/11x17", 792, 1224},
  268. {"/a3", 842, 1190},
  269. {"/a4", 595, 842},
  270. {"/b5", 501, 709},
  271. {"/ledger", 1224, 792},
  272. {"/legal", 612, 1008},
  273. {"/letter", 612, 792},
  274. {"null", 0, 0}
  275. };
  276. const page_size *p = sizes;
  277. while (p->size_name[0] == '/' &&
  278. (p->width != width || p->height != height))
  279. ++p;
  280. pprintd2(s, "%d %d ", width, height);
  281. pprints1(s, "%s setpagesize\n", p->size_name);
  282. }
  283. pprintd1(s, "/pagesave save store %d dict begin\n", dictsize);
  284. if (do_scale)
  285. pprintg2(s, "%g %g scale\n",
  286. 72.0 / dev->HWResolution[0], 72.0 / dev->HWResolution[1]);
  287. stream_puts(s, "%%EndPageSetup\ngsave mark\n");
  288. if (s->end_status == ERRC)
  289. return_error(gs_error_ioerror);
  290. return 0;
  291. }
  292. /*
  293. * Write the page trailer. We do this directly to the file, rather than to
  294. * the stream, because we may have to do it during finalization.
  295. */
  296. int
  297. psw_write_page_trailer(FILE *f, int num_copies, int flush)
  298. {
  299. fprintf(f, "cleartomark end end pagesave restore\n");
  300. if (num_copies != 1)
  301. fprintf(f, "userdict /#copies %d put\n", num_copies);
  302. fprintf(f, " %s\n%%%%PageTrailer\n", (flush ? "showpage" : "copypage"));
  303. fflush(f);
  304. if (ferror(f))
  305. return_error(gs_error_ioerror);
  306. return 0;
  307. }