home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevpsu.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  7.8 KB  |  264 lines

  1. /* Copyright (C) 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevpsu.c,v 1.2 2000/09/19 19:00:22 lpd Exp $ */
  20. /* PostScript-writing utilities */
  21. #include "math_.h"
  22. #include "time_.h"
  23. #include "gx.h"
  24. #include "gscdefs.h"
  25. #include "gxdevice.h"
  26. #include "gdevpsu.h"
  27. #include "spprint.h"
  28. #include "stream.h"
  29.  
  30. /* ---------------- Low level ---------------- */
  31.  
  32. /* Write a 0-terminated array of strings as lines. */
  33. void
  34. psw_print_lines(FILE *f, const char *const lines[])
  35. {
  36.     int i;
  37.  
  38.     for (i = 0; lines[i] != 0; ++i)
  39.     fprintf(f, "%s\n", lines[i]);
  40. }
  41.  
  42. /* Write the ProcSet name. */
  43. private void
  44. psw_put_procset_name(stream *s, const gx_device *dev,
  45.              const gx_device_pswrite_common_t *pdpc)
  46. {
  47.     pprints1(s, "GS_%s", dev->dname);
  48.     pprintd3(s, "_%d_%d_%d",
  49.         (int)pdpc->LanguageLevel,
  50.         (int)(pdpc->LanguageLevel * 10 + 0.5) % 10,
  51.         pdpc->ProcSet_version);
  52. }
  53. private void
  54. psw_print_procset_name(FILE *f, const gx_device *dev,
  55.                const gx_device_pswrite_common_t *pdpc)
  56. {
  57.     byte buf[100];        /* arbitrary */
  58.     stream s;
  59.  
  60.     swrite_file(&s, f, buf, sizeof(buf));
  61.     psw_put_procset_name(&s, dev, pdpc);
  62.     sflush(&s);
  63. }
  64.  
  65. /* Write a bounding box. */
  66. private void
  67. psw_print_bbox(FILE *f, const gs_rect *pbbox)
  68. {
  69.     fprintf(f, "%%%%BoundingBox: %d %d %d %d\n",
  70.         (int)floor(pbbox->p.x), (int)floor(pbbox->p.y),
  71.         (int)ceil(pbbox->q.x), (int)ceil(pbbox->q.y));
  72.     fprintf(f, "%%%%HiResBoundingBox: %f %f %f %f\n",
  73.         pbbox->p.x, pbbox->p.y, pbbox->q.x, pbbox->q.y);
  74. }
  75.  
  76. /* ---------------- File level ---------------- */
  77.  
  78. private const char *const psw_ps_header[] = {
  79.     "%!PS-Adobe-3.0",
  80.     "%%Pages: (atend)",
  81.     0
  82. };
  83.  
  84. private const char *const psw_eps_header[] = {
  85.     "%!PS-Adobe-3.0 EPSF-3.0",
  86.     0
  87. };
  88.  
  89. private const char *const psw_begin_prolog[] = {
  90.     "%%EndComments",
  91.     "%%BeginProlog",
  92.     "% This copyright applies to everything between here and the %%EndProlog:",
  93.                 /* copyright */
  94.                 /* begin ProcSet */
  95.     0
  96. };
  97.  
  98. private const char *const psw_ps_procset[] = {
  99.     /* <w> <h> <sizename> setpagesize - */
  100.    "/setpagesize{1 index where{pop cvx exec pop pop}{pop /setpagedevice where",
  101.     "{pop 2 array astore 1 dict dup/PageSize 4 -1 roll put setpagedevice}",
  102.     "{pop/setpage where{pop pageparams 3{exch pop}repeat setpage}",
  103.     "{pop pop}ifelse}ifelse}ifelse}bind def",
  104.     0
  105. };
  106.  
  107. private const char *const psw_end_prolog[] = {
  108.     "end readonly def",
  109.     "%%EndResource",        /* ProcSet */
  110.     "/pagesave null def",    /* establish binding */
  111.     "%%EndProlog",
  112.     0
  113. };
  114.  
  115. /*
  116.  * Write the file header, up through the BeginProlog.  This must write to a
  117.  * file, not a stream, because it may be called during finalization.
  118.  */
  119. void
  120. psw_begin_file_header(FILE *f, const gx_device *dev, const gs_rect *pbbox,
  121.               gx_device_pswrite_common_t *pdpc, bool ascii)
  122. {
  123.     psw_print_lines(f, (pdpc->ProduceEPS ? psw_eps_header : psw_ps_header));
  124.     if (pbbox) {
  125.     psw_print_bbox(f, pbbox);
  126.     pdpc->bbox_position = 0;
  127.     } else if (ftell(f) < 0) {    /* File is not seekable. */
  128.     pdpc->bbox_position = -1;
  129.     fputs("%%BoundingBox: (atend)\n", f);
  130.     fputs("%%HiResBoundingBox: (atend)\n", f);
  131.     } else {        /* File is seekable, leave room to rewrite bbox. */
  132.     pdpc->bbox_position = ftell(f);
  133.     fputs("%...............................................................\n", f);
  134.     fputs("%...............................................................\n", f);
  135.     }
  136.     fprintf(f, "%%%%Creator: %s %ld (%s)\n", gs_product, (long)gs_revision,
  137.         dev->dname);
  138.     {
  139.     time_t t;
  140.     struct tm tms;
  141.  
  142.     time(&t);
  143.     tms = *localtime(&t);
  144.     fprintf(f, "%%%%CreationDate: %d/%02d/%02d %02d:%02d:%02d\n",
  145.         tms.tm_year + 1900, tms.tm_mon + 1, tms.tm_mday,
  146.         tms.tm_hour, tms.tm_min, tms.tm_sec);
  147.     }
  148.     if (ascii)
  149.     fputs("%%DocumentData: Clean7Bit\n", f);
  150.     if (pdpc->LanguageLevel >= 2.0)
  151.     fprintf(f, "%%%%LanguageLevel: %d\n", (int)pdpc->LanguageLevel);
  152.     else if (pdpc->LanguageLevel == 1.5)
  153.     fputs("%%Extensions: CMYK\n", f);
  154.     psw_print_lines(f, psw_begin_prolog);
  155.     fprintf(f, "%% %s\n", gs_copyright);
  156.     fputs("%%BeginResource: procset ", f);
  157.     psw_print_procset_name(f, dev, pdpc);
  158.     fputs("\n/", f);
  159.     psw_print_procset_name(f, dev, pdpc);
  160.     fputs(" 80 dict dup begin\n", f);
  161.     psw_print_lines(f, psw_ps_procset);
  162. }
  163.  
  164. /*
  165.  * End the file header.
  166.  */
  167. void
  168. psw_end_file_header(FILE *f)
  169. {
  170.     psw_print_lines(f, psw_end_prolog);
  171. }
  172.  
  173. /*
  174.  * End the file.
  175.  */
  176. void
  177. psw_end_file(FILE *f, const gx_device *dev,
  178.          const gx_device_pswrite_common_t *pdpc, const gs_rect *pbbox)
  179. {
  180.     fprintf(f, "%%%%Trailer\n%%%%Pages: %ld\n", dev->PageCount);
  181.     if (dev->PageCount > 0 && pdpc->bbox_position != 0) {
  182.     if (pdpc->bbox_position >= 0) {
  183.         long save_pos = ftell(f);
  184.  
  185.         fseek(f, pdpc->bbox_position, SEEK_SET);
  186.         psw_print_bbox(f, pbbox);
  187.         fputc('%', f);
  188.         fseek(f, save_pos, SEEK_SET);
  189.     } else
  190.         psw_print_bbox(f, pbbox);
  191.     }
  192.     if (!pdpc->ProduceEPS)
  193.     fputs("%%EOF\n", f);
  194. }
  195.  
  196. /* ---------------- Page level ---------------- */
  197.  
  198. /*
  199.  * Write the page header.
  200.  */
  201. void
  202. psw_write_page_header(stream *s, const gx_device *dev,
  203.               const gx_device_pswrite_common_t *pdpc,
  204.               bool do_scale)
  205. {
  206.     long page = dev->PageCount + 1;
  207.  
  208.     pprintld2(s, "%%%%Page: %ld %ld\n%%%%BeginPageSetup\n", page, page);
  209.     /*
  210.      * Adobe's documentation says that page setup must be placed outside the
  211.      * save/restore that encapsulates the page contents, and that the
  212.      * showpage must be placed after the restore.  This means that to
  213.      * achieve page independence, *every* page's setup code must include a
  214.      * setpagedevice that sets *every* page device parameter that is changed
  215.      * on *any* page.  Currently, the only such parameter relevant to this
  216.      * driver is page size, but there might be more in the future.
  217.      */
  218.     psw_put_procset_name(s, dev, pdpc);
  219.     pputs(s, " begin\n");
  220.     if (!pdpc->ProduceEPS) {
  221.     int width = (int)(dev->width * 72.0 / dev->HWResolution[0] + 0.5);
  222.     int height = (int)(dev->height * 72.0 / dev->HWResolution[1] + 0.5);
  223.     typedef struct ps_ {
  224.         const char *size_name;
  225.         int width, height;
  226.     } page_size;
  227.     static const page_size sizes[] = {
  228.         {"/11x17", 792, 1224},
  229.         {"/a3", 842, 1190},
  230.         {"/a4", 595, 842},
  231.         {"/b5", 501, 709},
  232.         {"/ledger", 1224, 792},
  233.         {"/legal", 612, 1008},
  234.         {"/letter", 612, 792},
  235.         {"null", 0, 0}
  236.     };
  237.     const page_size *p = sizes;
  238.  
  239.     while (p->size_name[0] == '/' &&
  240.            (p->width != width || p->height != height))
  241.         ++p;
  242.     pprintd2(s, "%d %d ", width, height);
  243.     pprints1(s, "%s setpagesize\n", p->size_name);
  244.     }
  245.     pputs(s, "/pagesave save store 100 dict begin\n");
  246.     if (do_scale)
  247.     pprintg2(s, "%g %g scale\n",
  248.          72.0 / dev->HWResolution[0], 72.0 / dev->HWResolution[1]);
  249.     pputs(s, "%%EndPageSetup\ngsave mark\n");
  250. }
  251.  
  252. /*
  253.  * Write the page trailer.  We do this directly to the file, rather than to
  254.  * the stream, because we may have to do it during finalization.
  255.  */
  256. void
  257. psw_write_page_trailer(FILE *f, int num_copies, int flush)
  258. {
  259.     if (num_copies != 1)
  260.     fprintf(f, "userdict /#copies %d put\n", num_copies);
  261.     fprintf(f, "cleartomark end end pagesave restore %s\n%%%%PageTrailer\n",
  262.         (flush ? "showpage" : "copypage"));
  263. }
  264.