]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - filter/pstops.c
Merge changes from CUPS 1.4svn-r8067 (tentative CUPS 1.4b1)
[thirdparty/cups.git] / filter / pstops.c
index 6cfca815d50f55eb7ced661bd44646948183be79..0e8ac48d05740b57a75effef8ba162b1ca121948 100644 (file)
@@ -1,55 +1,51 @@
 /*
- * "$Id: pstops.c 5886 2006-08-24 19:53:17Z mike $"
+ * "$Id: pstops.c 7977 2008-09-23 23:44:33Z mike $"
  *
  *   PostScript filter for the Common UNIX Printing System (CUPS).
  *
- *   Copyright 1993-2006 by Easy Software Products.
+ *   Copyright 2007-2008 by Apple Inc.
+ *   Copyright 1993-2007 by Easy Software Products.
  *
  *   These coded instructions, statements, and computer programs are the
- *   property of Easy Software Products and are protected by Federal
- *   copyright law.  Distribution and use rights are outlined in the file
- *   "LICENSE.txt" which should have been included with this file.  If this
- *   file is missing or damaged please contact Easy Software Products
- *   at:
- *
- *       Attn: CUPS Licensing Information
- *       Easy Software Products
- *       44141 Airport View Drive, Suite 204
- *       Hollywood, Maryland 20636 USA
- *
- *       Voice: (301) 373-9600
- *       EMail: cups-info@cups.org
- *         WWW: http://www.cups.org
+ *   property of Apple Inc. and are protected by Federal copyright
+ *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
+ *   which should have been included with this file.  If this file is
+ *   file is missing or damaged, see the license at "http://www.cups.org/".
  *
  *   This file is subject to the Apple OS-Developed Software exception.
  *
  * Contents:
  *
- *   main()               - Main entry...
- *   add_page()           - Add a page to the pages array...
+ *   main()               - Main entry.
+ *   add_page()           - Add a page to the pages array.
+ *   cancel_job()         - Flag the job as canceled.
  *   check_range()        - Check to see if the current page is selected for
- *   copy_bytes()         - Copy bytes from the input file to stdout...
- *   copy_comments()      - Copy all of the comments section...
- *   copy_dsc()           - Copy a DSC-conforming document...
- *   copy_non_dsc()       - Copy a document that does not conform to the DSC...
- *   copy_page()          - Copy a page description...
- *   copy_prolog()        - Copy the document prolog section...
- *   copy_setup()         - Copy the document setup section...
- *   copy_trailer()       - Copy the document trailer...
- *   do_prolog()          - Send the necessary document prolog commands...
- *   do_setup()           - Send the necessary document setup commands...
+ *                          printing.
+ *   copy_bytes()         - Copy bytes from the input file to stdout.
+ *   copy_comments()      - Copy all of the comments section.
+ *   copy_dsc()           - Copy a DSC-conforming document.
+ *   copy_non_dsc()       - Copy a document that does not conform to the DSC.
+ *   copy_page()          - Copy a page description.
+ *   copy_prolog()        - Copy the document prolog section.
+ *   copy_setup()         - Copy the document setup section.
+ *   copy_trailer()       - Copy the document trailer.
+ *   do_prolog()          - Send the necessary document prolog commands.
+ *   do_setup()           - Send the necessary document setup commands.
  *   doc_printf()         - Send a formatted string to stdout and/or the temp
  *                          file.
  *   doc_puts()           - Send a nul-terminated string to stdout and/or the
  *                          temp file.
  *   doc_write()          - Send data to stdout and/or the temp file.
- *   end_nup()            - End processing for N-up printing...
+ *   end_nup()            - End processing for N-up printing.
  *   include_feature()    - Include a printer option/feature command.
- *   parse_text()         - Parse a text value in a comment...
- *   set_pstops_options() - Set pstops options...
- *   skip_page()          - Skip past a page that won't be printed...
- *   start_nup()          - Start processing for N-up printing...
+ *   parse_text()         - Parse a text value in a comment.
+ *   set_pstops_options() - Set pstops options.
+ *   skip_page()          - Skip past a page that won't be printed.
+ *   start_nup()          - Start processing for N-up printing.
+ *   write_label_prolog() - Write the prolog with the classification and page
+ *                          label.
  *   write_labels()       - Write the actual page labels.
+ *   write_options()      - Write options provided via %%IncludeFeature.
  */
 
 /*
@@ -61,6 +57,8 @@
 #include <math.h>
 #include <cups/file.h>
 #include <cups/array.h>
+#include <cups/i18n.h>
+#include <signal.h>
 
 
 /*
@@ -154,35 +152,43 @@ typedef struct                            /**** Document information ****/
                                 ((p) % doc->number_up) != 0)
 
 
+/*
+ * Local globals...
+ */
+
+static int             JobCanceled = 0;/* Set to 1 on SIGTERM */
+
+
 /*
  * Local functions...
  */
 
 static pstops_page_t   *add_page(pstops_doc_t *doc, const char *label);
+static void            cancel_job(int sig);
 static int             check_range(pstops_doc_t *doc, int page);
 static void            copy_bytes(cups_file_t *fp, off_t offset,
                                   size_t length);
-static size_t          copy_comments(cups_file_t *fp, pstops_doc_t *doc,
-                                     char *line, size_t linelen,
-                                     size_t linesize);
+static ssize_t         copy_comments(cups_file_t *fp, pstops_doc_t *doc,
+                                     ppd_file_t *ppd, char *line,
+                                     ssize_t linelen, size_t linesize);
 static void            copy_dsc(cups_file_t *fp, pstops_doc_t *doc,
-                                ppd_file_t *ppd, char *line, size_t linelen,
+                                ppd_file_t *ppd, char *line, ssize_t linelen,
                                 size_t linesize);
 static void            copy_non_dsc(cups_file_t *fp, pstops_doc_t *doc,
                                     ppd_file_t *ppd, char *line,
-                                    size_t linelen, size_t linesize);
-static size_t          copy_page(cups_file_t *fp, pstops_doc_t *doc,
+                                    ssize_t linelen, size_t linesize);
+static ssize_t         copy_page(cups_file_t *fp, pstops_doc_t *doc,
                                  ppd_file_t *ppd, int number, char *line,
-                                 size_t linelen, size_t linesize);
-static size_t          copy_prolog(cups_file_t *fp, pstops_doc_t *doc,
+                                 ssize_t linelen, size_t linesize);
+static ssize_t         copy_prolog(cups_file_t *fp, pstops_doc_t *doc,
                                    ppd_file_t *ppd, char *line,
-                                   size_t linelen, size_t linesize);
-static size_t          copy_setup(cups_file_t *fp, pstops_doc_t *doc,
+                                   ssize_t linelen, size_t linesize);
+static ssize_t         copy_setup(cups_file_t *fp, pstops_doc_t *doc,
                                   ppd_file_t *ppd, char *line,
-                                  size_t linelen, size_t linesize);
-static size_t          copy_trailer(cups_file_t *fp, pstops_doc_t *doc,
+                                  ssize_t linelen, size_t linesize);
+static ssize_t         copy_trailer(cups_file_t *fp, pstops_doc_t *doc,
                                     ppd_file_t *ppd, int number, char *line,
-                                    size_t linelen, size_t linesize);
+                                    ssize_t linelen, size_t linesize);
 static void            do_prolog(pstops_doc_t *doc, ppd_file_t *ppd);
 static void            do_setup(pstops_doc_t *doc, ppd_file_t *ppd);
 static void            doc_printf(pstops_doc_t *doc, const char *format, ...)
@@ -201,15 +207,20 @@ static char               *parse_text(const char *start, char **end, char *buffer,
 static void            set_pstops_options(pstops_doc_t *doc, ppd_file_t *ppd,
                                           char *argv[], int num_options,
                                           cups_option_t *options);
-static size_t          skip_page(cups_file_t *fp, char *line, size_t linelen,
+static ssize_t         skip_page(cups_file_t *fp, char *line, ssize_t linelen,
                                  size_t linesize);
 static void            start_nup(pstops_doc_t *doc, int number,
                                  int show_border, const int *bounding_box);
+static void            write_label_prolog(pstops_doc_t *doc, const char *label,
+                                          float bottom, float top,
+                                          float width);
 static void            write_labels(pstops_doc_t *doc, int orient);
+static void            write_options(pstops_doc_t  *doc, ppd_file_t *ppd,
+                                     int num_options, cups_option_t *options);
 
 
 /*
- * 'main()' - Main entry...
+ * 'main()' - Main entry.
  */
 
 int                                    /* O - Exit status */
@@ -223,6 +234,9 @@ main(int  argc,                             /* I - Number of command-line args */
   cups_option_t        *options;               /* Print options */
   char         line[8192];             /* Line buffer */
   size_t       len;                    /* Length of line buffer */
+#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
+  struct sigaction action;             /* Actions for POSIX signals */
+#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
 
 
  /*
@@ -237,10 +251,27 @@ main(int  argc,                           /* I - Number of command-line args */
 
   if (argc < 6 || argc > 7)
   {
-    fputs("ERROR: pstops job-id user title copies options [file]\n", stderr);
+    fprintf(stderr, _("Usage: %s job-id user title copies options [file]\n"),
+            argv[0]);
     return (1);
   }
 
+ /*
+  * Register a signal handler to cleanly cancel a job.
+  */
+
+#ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
+  sigset(SIGTERM, cancel_job);
+#elif defined(HAVE_SIGACTION)
+  memset(&action, 0, sizeof(action));
+
+  sigemptyset(&action.sa_mask);
+  action.sa_handler = cancel_job;
+  sigaction(SIGTERM, &action, NULL);
+#else
+  signal(SIGTERM, cancel_job);
+#endif /* HAVE_SIGSET */
+
  /*
   * If we have 7 arguments, print the file named on the command-line.
   * Otherwise, send stdin instead...
@@ -256,7 +287,7 @@ main(int  argc,                             /* I - Number of command-line args */
 
     if ((fp = cupsFileOpen(argv[6], "r")) == NULL)
     {
-      fprintf(stderr, "ERROR: Unable to open print file \"%s\" - %s\n",
+      fprintf(stderr, _("ERROR: Unable to open file \"%s\" - %s\n"),
               argv[6], strerror(errno));
       return (1);
     }
@@ -268,7 +299,7 @@ main(int  argc,                             /* I - Number of command-line args */
 
   if ((len = cupsFileGetLine(fp, line, sizeof(line))) == 0)
   {
-    fputs("ERROR: Empty print file!\n", stderr);
+    fputs(_("ERROR: Empty print file!\n"), stderr);
     return (1);
   }
 
@@ -387,7 +418,7 @@ main(int  argc,                             /* I - Number of command-line args */
 
 
 /*
- * 'add_page()' - Add a page to the pages array...
+ * 'add_page()' - Add a page to the pages array.
  */
 
 static pstops_page_t *                 /* O - New page info object */
@@ -402,14 +433,14 @@ add_page(pstops_doc_t *doc,               /* I - Document information */
 
   if (!doc->pages)
   {
-    fprintf(stderr, "EMERG: Unable to allocate memory for pages array: %s\n",
+    fprintf(stderr, _("EMERG: Unable to allocate memory for pages array: %s\n"),
             strerror(errno));
     exit(1);
   }
 
   if ((pageinfo = calloc(1, sizeof(pstops_page_t))) == NULL)
   {
-    fprintf(stderr, "EMERG: Unable to allocate memory for page info: %s\n",
+    fprintf(stderr, _("EMERG: Unable to allocate memory for page info: %s\n"),
             strerror(errno));
     exit(1);
   }
@@ -425,6 +456,19 @@ add_page(pstops_doc_t *doc,                /* I - Document information */
 }
 
 
+/*
+ * 'cancel_job()' - Flag the job as canceled.
+ */
+
+static void
+cancel_job(int sig)                    /* I - Signal number (unused) */
+{
+  (void)sig;
+
+  JobCanceled = 1;
+}
+
+
 /*
  * 'check_range()' - Check to see if the current page is selected for
  *                   printing.
@@ -492,7 +536,7 @@ check_range(pstops_doc_t *doc,              /* I - Document information */
 
 
 /*
- * 'copy_bytes()' - Copy bytes from the input file to stdout...
+ * 'copy_bytes()' - Copy bytes from the input file to stdout.
  */
 
 static void
@@ -509,8 +553,12 @@ copy_bytes(cups_file_t *fp,                /* I - File to read from */
 
   if (cupsFileSeek(fp, offset) < 0)
   {
-    fprintf(stderr, "ERROR: Unable to seek to offset " CUPS_LLFMT
-                    " in file - %s\n",
+    fprintf(stderr,
+#ifdef HAVE_LONG_LONG
+            _("ERROR: Unable to seek to offset %lld in file - %s\n"),
+#else
+            _("ERROR: Unable to seek to offset %ld in file - %s\n"),
+#endif /* HAVE_LONG_LONG */
             CUPS_LLCAST offset, strerror(errno));
     return;
   }
@@ -533,17 +581,18 @@ copy_bytes(cups_file_t *fp,               /* I - File to read from */
 
 
 /*
- * 'copy_comments()' - Copy all of the comments section...
+ * 'copy_comments()' - Copy all of the comments section.
  *
  * This function expects "line" to be filled with a comment line.
  * On return, "line" will contain the next line in the file, if any.
  */
 
-static size_t                          /* O - Length of next line */
+static ssize_t                         /* O - Length of next line */
 copy_comments(cups_file_t  *fp,                /* I - File to read from */
               pstops_doc_t *doc,       /* I - Document info */
+             ppd_file_t   *ppd,        /* I - PPD file */
               char         *line,      /* I - Line buffer */
-             size_t       linelen,     /* I - Length of initial line */
+             ssize_t      linelen,     /* I - Length of initial line */
              size_t       linesize)    /* I - Size of line buffer */
 {
   int  saw_bounding_box,               /* Saw %%BoundingBox: comment? */
@@ -589,15 +638,58 @@ copy_comments(cups_file_t  *fp,           /* I - File to read from */
 
     if (!strncmp(line, "%%Pages:", 8))
     {
+      int      pages;                  /* Number of pages */
+
+
       if (saw_pages)
-        fputs("ERROR: Duplicate %%Pages: comment seen!\n", stderr);
+        fputs(_("ERROR: Duplicate %%Pages: comment seen!\n"), stderr);
 
       saw_pages = 1;
+
+      if (Duplex && (pages = atoi(line + 8)) > 0 && pages <= doc->number_up)
+      {
+       /*
+        * Since we will only be printing on a single page, disable duplexing.
+       */
+
+       Duplex           = 0;
+       doc->slow_duplex = 0;
+
+       if (cupsGetOption("sides", doc->num_options, doc->options))
+         doc->num_options = cupsAddOption("sides", "one-sided",
+                                          doc->num_options, &(doc->options));
+
+       if (cupsGetOption("Duplex", doc->num_options, doc->options))
+         doc->num_options = cupsAddOption("Duplex", "None",
+                                          doc->num_options, &(doc->options));
+
+       if (cupsGetOption("EFDuplex", doc->num_options, doc->options))
+         doc->num_options = cupsAddOption("EFDuplex", "None",
+                                          doc->num_options, &(doc->options));
+
+       if (cupsGetOption("EFDuplexing", doc->num_options, doc->options))
+         doc->num_options = cupsAddOption("EFDuplexing", "False",
+                                          doc->num_options, &(doc->options));
+
+       if (cupsGetOption("KD03Duplex", doc->num_options, doc->options))
+         doc->num_options = cupsAddOption("KD03Duplex", "None",
+                                          doc->num_options, &(doc->options));
+
+       if (cupsGetOption("JCLDuplex", doc->num_options, doc->options))
+         doc->num_options = cupsAddOption("JCLDuplex", "None",
+                                          doc->num_options, &(doc->options));
+
+       ppdMarkOption(ppd, "Duplex", "None");
+       ppdMarkOption(ppd, "EFDuplex", "None");
+       ppdMarkOption(ppd, "EFDuplexing", "False");
+       ppdMarkOption(ppd, "KD03Duplex", "None");
+       ppdMarkOption(ppd, "JCLDuplex", "None");
+      }
     }
     else if (!strncmp(line, "%%BoundingBox:", 14))
     {
       if (saw_bounding_box)
-        fputs("ERROR: Duplicate %%BoundingBox: comment seen!\n", stderr);
+        fputs(_("ERROR: Duplicate %%BoundingBox: comment seen!\n"), stderr);
       else if (strstr(line + 14, "(atend)"))
       {
        /*
@@ -608,7 +700,7 @@ copy_comments(cups_file_t  *fp,             /* I - File to read from */
                      doc->bounding_box + 1, doc->bounding_box + 2,
                      doc->bounding_box + 3) != 4)
       {
-       fputs("ERROR: Bad %%BoundingBox: comment seen!\n", stderr);
+       fputs(_("ERROR: Bad %%BoundingBox: comment seen!\n"), stderr);
 
        doc->bounding_box[0] = (int)PageLeft;
        doc->bounding_box[1] = (int)PageBottom;
@@ -621,12 +713,12 @@ copy_comments(cups_file_t  *fp,           /* I - File to read from */
     else if (!strncmp(line, "%%For:", 6))
     {
       saw_for = 1;
-      printf("%s\n", line);
+      doc_printf(doc, "%s\n", line);
     }
     else if (!strncmp(line, "%%Title:", 8))
     {
       saw_title = 1;
-      printf("%s\n", line);
+      doc_printf(doc, "%s\n", line);
     }
     else if (!strncmp(line, "%cupsRotation:", 14))
     {
@@ -653,23 +745,23 @@ copy_comments(cups_file_t  *fp,           /* I - File to read from */
       break;
     }
     else if (strncmp(line, "%!", 2) && strncmp(line, "%cups", 5))
-      printf("%s\n", line);
+      doc_printf(doc, "%s\n", line);
 
     if ((linelen = cupsFileGetLine(fp, line, linesize)) == 0)
       break;
   }
 
   if (!saw_bounding_box)
-    fputs("ERROR: No %%BoundingBox: comment in header!\n", stderr);
+    fputs(_("ERROR: No %%BoundingBox: comment in header!\n"), stderr);
 
   if (!saw_pages)
-    fputs("ERROR: No %%Pages: comment in header!\n", stderr);
+    fputs(_("ERROR: No %%Pages: comment in header!\n"), stderr);
 
   if (!saw_for)
-    printf("%%%%For: %s\n", doc->user);
+    WriteTextComment("For", doc->user);
 
   if (!saw_title)
-    printf("%%%%Title: %s\n", doc->title);
+    WriteTextComment("Title", doc->title);
 
   if (doc->copies != 1 && (!doc->collate || !doc->slow_collate))
   {
@@ -678,15 +770,15 @@ copy_comments(cups_file_t  *fp,           /* I - File to read from */
     * that are required...
     */
 
-    printf("%%%%Requirements: numcopies(%d)%s%s\n", doc->copies,
-           doc->collate ? " collate" : "",
-          Duplex ? " duplex" : "");
+    doc_printf(doc, "%%%%Requirements: numcopies(%d)%s%s\n", doc->copies,
+               doc->collate ? " collate" : "",
+              Duplex ? " duplex" : "");
 
    /*
     * Apple uses RBI comments for various non-PPD options...
     */
 
-    printf("%%RBINumCopies: %d\n", doc->copies);
+    doc_printf(doc, "%%RBINumCopies: %d\n", doc->copies);
   }
   else
   {
@@ -695,25 +787,25 @@ copy_comments(cups_file_t  *fp,           /* I - File to read from */
     */
 
     if (Duplex)
-      puts("%%Requirements: duplex");
+      doc_puts(doc, "%%Requirements: duplex\n");
 
    /*
     * Apple uses RBI comments for various non-PPD options...
     */
 
-    puts("%RBINumCopies: 1");
+    doc_puts(doc, "%RBINumCopies: 1\n");
   }
 
-  puts("%%Pages: (atend)");
-  puts("%%BoundingBox: (atend)");
-  puts("%%EndComments");
+  doc_puts(doc, "%%Pages: (atend)\n");
+  doc_puts(doc, "%%BoundingBox: (atend)\n");
+  doc_puts(doc, "%%EndComments\n");
 
   return (linelen);
 }
 
 
 /*
- * 'copy_dsc()' - Copy a DSC-conforming document...
+ * 'copy_dsc()' - Copy a DSC-conforming document.
  *
  * This function expects "line" to be filled with the %!PS-Adobe comment line.
  */
@@ -723,7 +815,7 @@ copy_dsc(cups_file_t  *fp,          /* I - File to read from */
          pstops_doc_t *doc,            /* I - Document info */
          ppd_file_t   *ppd,            /* I - PPD file */
         char         *line,            /* I - Line buffer */
-        size_t       linelen,          /* I - Length of initial line */
+        ssize_t      linelen,          /* I - Length of initial line */
         size_t       linesize)         /* I - Size of line buffer */
 {
   int          number;                 /* Page number */
@@ -745,7 +837,7 @@ copy_dsc(cups_file_t  *fp,          /* I - File to read from */
   */
 
   fprintf(stderr, "DEBUG: Before copy_comments - %s", line);
-  linelen = copy_comments(fp, doc, line, linelen, linesize);
+  linelen = copy_comments(fp, doc, ppd, line, linelen, linesize);
 
  /*
   * Now find the prolog section, if any...
@@ -767,7 +859,7 @@ copy_dsc(cups_file_t  *fp,          /* I - File to read from */
 
   while (strncmp(line, "%%Page:", 7) && strncmp(line, "%%Trailer", 9))
   {
-    fwrite(line, 1, linelen, stdout);
+    doc_write(doc, line, linelen);
 
     if ((linelen = cupsFileGetLine(fp, line, linesize)) == 0)
       break;
@@ -782,6 +874,9 @@ copy_dsc(cups_file_t  *fp,          /* I - File to read from */
   fprintf(stderr, "DEBUG: Before page loop - %s", line);
   while (!strncmp(line, "%%Page:", 7))
   {
+    if (JobCanceled)
+      break;
+
     number ++;
 
     if (check_range(doc, (number - 1) / doc->number_up + 1))
@@ -800,7 +895,8 @@ copy_dsc(cups_file_t  *fp,          /* I - File to read from */
   * Finish up the last page(s)...
   */
 
-  if (number && is_not_last_page(number) && cupsArrayLast(doc->pages))
+  if (number && is_not_last_page(number) && cupsArrayLast(doc->pages) &&
+      check_range(doc, (number - 1) / doc->number_up + 1))
   {
     pageinfo = (pstops_page_t *)cupsArrayLast(doc->pages);
 
@@ -841,7 +937,7 @@ copy_dsc(cups_file_t  *fp,          /* I - File to read from */
 
   number = doc->slow_order ? 0 : doc->page;
 
-  if (doc->temp)
+  if (doc->temp && !JobCanceled && cupsArrayCount(doc->pages) > 0)
   {
     int        copy;                           /* Current copy */
 
@@ -858,17 +954,77 @@ copy_dsc(cups_file_t  *fp,                /* I - File to read from */
     * Make the copies...
     */
 
-    for (copy = !doc->slow_order; copy < doc->copies; copy ++)
+    if (doc->slow_collate)
+      copy = !doc->slow_order;
+    else
+      copy = doc->copies - 1;
+
+    for (; copy < doc->copies; copy ++)
     {
+      if (JobCanceled)
+       break;
+
+     /*
+      * Send end-of-job stuff followed by any start-of-job stuff required
+      * for the JCL options...
+      */
+
+      if (number && doc->emit_jcl && ppd && ppd->jcl_end)
+      {
+       /*
+        * Send the trailer...
+       */
+
+        puts("%%Trailer");
+       printf("%%%%Pages: %d\n", cupsArrayCount(doc->pages));
+       if (doc->number_up > 1 || doc->fitplot)
+         printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
+                PageLeft, PageBottom, PageRight, PageTop);
+       else
+         printf("%%%%BoundingBox: %d %d %d %d\n",
+                doc->new_bounding_box[0], doc->new_bounding_box[1],
+                doc->new_bounding_box[2], doc->new_bounding_box[3]);
+        puts("%%EOF");
+
+       /*
+        * Start a new document...
+       */
+
+        ppdEmitJCLEnd(ppd, stdout);
+        ppdEmitJCL(ppd, stdout, doc->job_id, doc->user, doc->title);
+
+       puts("%!PS-Adobe-3.0");
+
+       number = 0;
+      }
+
+     /*
+      * Copy the prolog as needed...
+      */
+
+      if (!number)
+      {
+        pageinfo = (pstops_page_t *)cupsArrayFirst(doc->pages);
+       copy_bytes(doc->temp, 0, pageinfo->offset);
+      }
+
+     /*
+      * Then copy all of the pages...
+      */
+
       pageinfo = doc->slow_order ? (pstops_page_t *)cupsArrayLast(doc->pages) :
                                    (pstops_page_t *)cupsArrayFirst(doc->pages);
 
       while (pageinfo)
       {
+        if (JobCanceled)
+         break;
+
         number ++;
 
        if (!ppd || !ppd->num_filters)
-         fprintf(stderr, "PAGE: %d 1\n", number);
+         fprintf(stderr, "PAGE: %d %d\n", number,
+                 doc->slow_collate ? 1 : doc->copies);
 
        if (doc->number_up > 1)
        {
@@ -878,7 +1034,7 @@ copy_dsc(cups_file_t  *fp,         /* I - File to read from */
        }
        else
        {
-         printf("%%%%Page: %s %d\n", pageinfo->label, number);
+          printf("%%%%Page: %s %d\n", pageinfo->label, number);
          printf("%%%%PageBoundingBox: %d %d %d %d\n",
                 pageinfo->bounding_box[0], pageinfo->bounding_box[1],
                 pageinfo->bounding_box[2], pageinfo->bounding_box[3]);
@@ -903,12 +1059,13 @@ copy_dsc(cups_file_t  *fp,               /* I - File to read from */
   * Write/copy the trailer...
   */
 
-  linelen = copy_trailer(fp, doc, ppd, number, line, linelen, linesize);
+  if (!JobCanceled)
+    copy_trailer(fp, doc, ppd, number, line, linelen, linesize);
 }
 
 
 /*
- * 'copy_non_dsc()' - Copy a document that does not conform to the DSC...
+ * 'copy_non_dsc()' - Copy a document that does not conform to the DSC.
  *
  * This function expects "line" to be filled with the %! comment line.
  */
@@ -918,7 +1075,7 @@ copy_non_dsc(cups_file_t  *fp,             /* I - File to read from */
              pstops_doc_t *doc,                /* I - Document info */
              ppd_file_t   *ppd,                /* I - PPD file */
             char         *line,        /* I - Line buffer */
-            size_t       linelen,      /* I - Length of initial line */
+            ssize_t      linelen,      /* I - Length of initial line */
             size_t       linesize)     /* I - Size of line buffer */
 {
   int  copy;                           /* Current copy */
@@ -931,8 +1088,8 @@ copy_non_dsc(cups_file_t  *fp,             /* I - File to read from */
   * that may not print correctly...
   */
 
-  fputs("WARNING: This document does not conform to the Adobe Document "
-        "Structuring Conventions and may not print correctly!\n", stderr);
+  fputs(_("WARNING: This document does not conform to the Adobe Document "
+          "Structuring Conventions and may not print correctly!\n"), stderr);
 
  /*
   * Then write a standard DSC comment section...
@@ -946,8 +1103,8 @@ copy_non_dsc(cups_file_t  *fp,             /* I - File to read from */
   else
     puts("%%Pages: 1");
 
-  printf("%%%%For: %s\n", doc->user);
-  printf("%%%%Title: %s\n", doc->title);
+  WriteTextComment("For", doc->user);
+  WriteTextComment("Title", doc->title);
 
   if (doc->copies != 1 && (!doc->collate || !doc->slow_collate))
   {
@@ -1038,7 +1195,7 @@ copy_non_dsc(cups_file_t  *fp,            /* I - File to read from */
     puts("ESPshowpage");
   }
 
-  if (doc->temp)
+  if (doc->temp && !JobCanceled)
   {
    /*
     * Reopen the temporary file for reading...
@@ -1054,6 +1211,9 @@ copy_non_dsc(cups_file_t  *fp,            /* I - File to read from */
 
     for (copy = 1; copy < doc->copies; copy ++)
     {
+      if (JobCanceled)
+       break;
+
       if (!ppd || !ppd->num_filters)
        fputs("PAGE: 1 1\n", stderr);
 
@@ -1085,19 +1245,19 @@ copy_non_dsc(cups_file_t  *fp,          /* I - File to read from */
 
 
 /*
- * 'copy_page()' - Copy a page description...
+ * 'copy_page()' - Copy a page description.
  *
  * This function expects "line" to be filled with a %%Page comment line.
  * On return, "line" will contain the next line in the file, if any.
  */
 
-static size_t                          /* O - Length of next line */
+static ssize_t                         /* O - Length of next line */
 copy_page(cups_file_t  *fp,            /* I - File to read from */
           pstops_doc_t *doc,           /* I - Document info */
           ppd_file_t   *ppd,           /* I - PPD file */
          int          number,          /* I - Current page number */
          char         *line,           /* I - Line buffer */
-         size_t       linelen,         /* I - Length of initial line */
+         ssize_t      linelen,         /* I - Length of initial line */
          size_t       linesize)        /* I - Size of line buffer */
 {
   char         label[256],             /* Page label string */
@@ -1105,6 +1265,7 @@ copy_page(cups_file_t  *fp,               /* I - File to read from */
   int          level;                  /* Embedded document level */
   pstops_page_t        *pageinfo;              /* Page information */
   int          first_page;             /* First page on N-up output? */
+  int          has_page_setup;         /* Does the page have %%Begin/EndPageSetup? */
   int          bounding_box[4];        /* PageBoundingBox */
 
 
@@ -1116,13 +1277,13 @@ copy_page(cups_file_t  *fp,             /* I - File to read from */
 
   if (!parse_text(line + 7, &ptr, label, sizeof(label)))
   {
-    fputs("ERROR: Bad %%Page: comment in file!\n", stderr);
+    fputs(_("ERROR: Bad %%Page: comment in file!\n"), stderr);
     label[0] = '\0';
     number   = doc->page;
   }
   else if (strtol(ptr, &ptr, 10) == LONG_MAX || !isspace(*ptr & 255))
   {
-    fputs("ERROR: Bad %%Page: comment in file!\n", stderr);
+    fputs(_("ERROR: Bad %%Page: comment in file!\n"), stderr);
     number = doc->page;
   }
 
@@ -1150,7 +1311,9 @@ copy_page(cups_file_t  *fp,               /* I - File to read from */
       pageinfo->num_options = cupsAddOption("InputSlot", doc->ap_input_slot,
                                             pageinfo->num_options,
                                            &(pageinfo->options));
-      pageinfo->num_options = cupsAddOption("ManualFeed", doc->ap_manual_feed,
+      pageinfo->num_options = cupsAddOption("ManualFeed",
+                                            doc->ap_input_slot ? "False" :
+                                               doc->ap_manual_feed,
                                             pageinfo->num_options,
                                            &(pageinfo->options));
     }
@@ -1163,7 +1326,9 @@ copy_page(cups_file_t  *fp,               /* I - File to read from */
       pageinfo->num_options = cupsAddOption("InputSlot", doc->input_slot,
                                             pageinfo->num_options,
                                            &(pageinfo->options));
-      pageinfo->num_options = cupsAddOption("ManualFeed", doc->manual_feed,
+      pageinfo->num_options = cupsAddOption("ManualFeed",
+                                            doc->input_slot ? "False" :
+                                               doc->manual_feed,
                                             pageinfo->num_options,
                                            &(pageinfo->options));
     }
@@ -1188,10 +1353,52 @@ copy_page(cups_file_t  *fp,             /* I - File to read from */
                  bounding_box + 1, bounding_box + 2,
                 bounding_box + 3) != 4)
       {
-        fputs("ERROR: Bad %%PageBoundingBox: comment in file!\n", stderr);
+        fputs(_("ERROR: Bad %%PageBoundingBox: comment in file!\n"), stderr);
         memcpy(bounding_box, doc->bounding_box,
               sizeof(bounding_box));
       }
+      else if (doc->number_up == 1 && !doc->fitplot  && Orientation)
+      {
+        int    temp_bbox[4];           /* Temporary bounding box */
+
+
+        memcpy(temp_bbox, bounding_box, sizeof(temp_bbox));
+
+        fprintf(stderr, "DEBUG: Orientation = %d\n", Orientation);
+        fprintf(stderr, "DEBUG: original bounding_box = [ %d %d %d %d ]\n",
+               bounding_box[0], bounding_box[1],
+               bounding_box[2], bounding_box[3]);
+        fprintf(stderr, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
+               PageWidth, PageLength);
+
+        switch (Orientation)
+       {
+         case 1 : /* Landscape */
+             bounding_box[0] = PageLength - temp_bbox[3];
+             bounding_box[1] = temp_bbox[0];
+             bounding_box[2] = PageLength - temp_bbox[1];
+             bounding_box[3] = temp_bbox[2];
+              break;
+
+         case 2 : /* Reverse Portrait */
+             bounding_box[0] = PageWidth - temp_bbox[2];
+             bounding_box[1] = PageLength - temp_bbox[3];
+             bounding_box[2] = PageWidth - temp_bbox[0];
+             bounding_box[3] = PageLength - temp_bbox[1];
+              break;
+
+         case 3 : /* Reverse Landscape */
+             bounding_box[0] = temp_bbox[1];
+             bounding_box[1] = PageWidth - temp_bbox[2];
+             bounding_box[2] = temp_bbox[3];
+             bounding_box[3] = PageWidth - temp_bbox[0];
+              break;
+       }
+
+        fprintf(stderr, "DEBUG: updated bounding_box = [ %d %d %d %d ]\n",
+               bounding_box[0], bounding_box[1],
+               bounding_box[2], bounding_box[3]);
+      }
     }
 #if 0
     else if (!strncmp(line, "%%PageCustomColors:", 19) ||
@@ -1306,60 +1513,65 @@ copy_page(cups_file_t  *fp,             /* I - File to read from */
   */
 
   if (first_page)
-  {
-    char       *page_setup;            /* PageSetup commands to send */
-
-
     doc_puts(doc, "%%BeginPageSetup\n");
 
-    if (pageinfo->num_options > 0)
+  if ((has_page_setup = !strncmp(line, "%%BeginPageSetup", 16)) != 0)
+  {
+    int        feature = 0;                    /* In a Begin/EndFeature block? */
+
+    while ((linelen = cupsFileGetLine(fp, line, linesize)) > 0)
     {
-      int              i;              /* Looping var */
-      ppd_option_t     *option;        /* PPD option */
-      int              min_order;      /* Minimum OrderDependency value */
-      char             *doc_setup,     /* DocumentSetup commands to send */
-                       *any_setup;     /* AnySetup commands to send */
+      if (!strncmp(line, "%%EndPageSetup", 14))
+       break;
+      else if (!strncmp(line, "%%BeginFeature:", 15))
+      {
+       feature = 1;
 
+       if (doc->number_up > 1 || doc->fitplot)
+         continue;
+      }
+      else if (!strncmp(line, "%%EndFeature", 12))
+      {
+       feature = 0;
 
-     /*
-      * Yes, figure out the minimum OrderDependency value...
-      */
+       if (doc->number_up > 1 || doc->fitplot)
+         continue;
+      }
+      else if (!strncmp(line, "%%IncludeFeature:", 17))
+      {
+       pageinfo->num_options = include_feature(ppd, line,
+                                               pageinfo->num_options,
+                                               &(pageinfo->options));
+       continue;
+      }
+      else if (!strncmp(line, "%%Include", 9))
+       continue;
 
-      if ((option = ppdFindOption(ppd, "PageRegion")) != NULL)
-       min_order = option->order;
-      else
-       min_order = 999.0f;
+      if (line[0] != '%' && !feature)
+        break;
 
-      for (i = 0; i < pageinfo->num_options; i ++)
-       if ((option = ppdFindOption(ppd, pageinfo->options[i].name)) != NULL &&
-            option->order < min_order)
-          min_order = option->order;
+      if (!feature || (doc->number_up == 1 && !doc->fitplot))
+       doc_write(doc, line, linelen);
+    }
 
-     /*
-      * Mark and extract them...
-      */
+   /*
+    * Skip %%EndPageSetup...
+    */
 
-      cupsMarkOptions(ppd, pageinfo->num_options, pageinfo->options);
+    if (linelen > 0 && !strncmp(line, "%%EndPageSetup", 14))
+    {
+      linelen        = cupsFileGetLine(fp, line, linesize);
+      has_page_setup = 0;
+    }
+  }
 
-      doc_setup = ppdEmitString(ppd, PPD_ORDER_DOCUMENT, min_order);
-      any_setup = ppdEmitString(ppd, PPD_ORDER_ANY, min_order);
+  if (first_page)
+  {
+    char       *page_setup;            /* PageSetup commands to send */
 
-     /*
-      * Then send them out...
-      */
 
-      if (doc_setup)
-      {
-       doc_puts(doc, doc_setup);
-       free(doc_setup);
-      }
-
-      if (any_setup)
-      {
-       doc_puts(doc, any_setup);
-       free(any_setup);
-      }
-    }
+    if (pageinfo->num_options > 0)
+      write_options(doc, ppd, pageinfo->num_options, pageinfo->options);
 
    /*
     * Output commands for the current page...
@@ -1381,34 +1593,35 @@ copy_page(cups_file_t  *fp,             /* I - File to read from */
   start_nup(doc, number, 1, bounding_box);
 
  /*
-  * Copy page setup commands as needed...
+  * Finish the PageSetup section as needed...
   */
 
-  if (!strncmp(line, "%%BeginPageSetup", 16))
+  if (has_page_setup)
   {
     int        feature = 0;                    /* In a Begin/EndFeature block? */
 
+    doc_write(doc, line, linelen);
 
     while ((linelen = cupsFileGetLine(fp, line, linesize)) > 0)
     {
       if (!strncmp(line, "%%EndPageSetup", 14))
-        break;
+       break;
       else if (!strncmp(line, "%%BeginFeature:", 15))
       {
-        feature = 1;
+       feature = 1;
 
        if (doc->number_up > 1 || doc->fitplot)
          continue;
       }
-      else if (!strncmp(line, "%%EndFeature:", 13))
+      else if (!strncmp(line, "%%EndFeature", 12))
       {
-        feature = 0;
+       feature = 0;
 
        if (doc->number_up > 1 || doc->fitplot)
          continue;
       }
       else if (!strncmp(line, "%%Include", 9))
-        continue;
+       continue;
 
       if (!feature || (doc->number_up == 1 && !doc->fitplot))
        doc_write(doc, line, linelen);
@@ -1418,14 +1631,10 @@ copy_page(cups_file_t  *fp,             /* I - File to read from */
     * Skip %%EndPageSetup...
     */
 
-    if (linelen > 0)
+    if (linelen > 0 && !strncmp(line, "%%EndPageSetup", 14))
       linelen = cupsFileGetLine(fp, line, linesize);
   }
 
- /*
-  * Finish the PageSetup section as needed...
-  */
-
   if (first_page)
     doc_puts(doc, "%%EndPageSetup\n");
 
@@ -1508,18 +1717,18 @@ copy_page(cups_file_t  *fp,             /* I - File to read from */
 
 
 /*
- * 'copy_prolog()' - Copy the document prolog section...
+ * 'copy_prolog()' - Copy the document prolog section.
  *
  * This function expects "line" to be filled with a %%BeginProlog comment line.
  * On return, "line" will contain the next line in the file, if any.
  */
 
-static size_t                          /* O - Length of next line */
+static ssize_t                         /* O - Length of next line */
 copy_prolog(cups_file_t  *fp,          /* I - File to read from */
             pstops_doc_t *doc,         /* I - Document info */
             ppd_file_t   *ppd,         /* I - PPD file */
            char         *line,         /* I - Line buffer */
-           size_t       linelen,       /* I - Length of initial line */
+           ssize_t      linelen,       /* I - Length of initial line */
            size_t       linesize)      /* I - Size of line buffer */
 {
   while (strncmp(line, "%%BeginProlog", 13))
@@ -1527,13 +1736,13 @@ copy_prolog(cups_file_t  *fp,           /* I - File to read from */
     if (!strncmp(line, "%%BeginSetup", 12) || !strncmp(line, "%%Page:", 7))
       break;
 
-    fwrite(line, 1, linelen, stdout);
+    doc_write(doc, line, linelen);
 
     if ((linelen = cupsFileGetLine(fp, line, linesize)) == 0)
       break;
   }
 
-  puts("%%BeginProlog");
+  doc_puts(doc, "%%BeginProlog\n");
 
   do_prolog(doc, ppd);
 
@@ -1546,51 +1755,58 @@ copy_prolog(cups_file_t  *fp,           /* I - File to read from */
           !strncmp(line, "%%Page:", 7))
         break;
 
-      fwrite(line, 1, linelen, stdout);
+      doc_write(doc, line, linelen);
     }
 
     if (!strncmp(line, "%%EndProlog", 11))
       linelen = cupsFileGetLine(fp, line, linesize);
     else
-      fputs("ERROR: Missing %%EndProlog!\n", stderr);
+      fputs(_("ERROR: Missing %%EndProlog!\n"), stderr);
   }
 
-  puts("%%EndProlog");
+  doc_puts(doc, "%%EndProlog\n");
 
   return (linelen);
 }
 
 
 /*
- * 'copy_setup()' - Copy the document setup section...
+ * 'copy_setup()' - Copy the document setup section.
  *
  * This function expects "line" to be filled with a %%BeginSetup comment line.
  * On return, "line" will contain the next line in the file, if any.
  */
 
-static size_t                          /* O - Length of next line */
+static ssize_t                         /* O - Length of next line */
 copy_setup(cups_file_t  *fp,           /* I - File to read from */
            pstops_doc_t *doc,          /* I - Document info */
            ppd_file_t   *ppd,          /* I - PPD file */
           char         *line,          /* I - Line buffer */
-          size_t       linelen,        /* I - Length of initial line */
+          ssize_t      linelen,        /* I - Length of initial line */
           size_t       linesize)       /* I - Size of line buffer */
 {
+  int          num_options;            /* Number of options */
+  cups_option_t        *options;               /* Options */
+
+
   while (strncmp(line, "%%BeginSetup", 12))
   {
     if (!strncmp(line, "%%Page:", 7))
       break;
 
-    fwrite(line, 1, linelen, stdout);
+    doc_write(doc, line, linelen);
 
     if ((linelen = cupsFileGetLine(fp, line, linesize)) == 0)
       break;
   }
 
-  puts("%%BeginSetup");
+  doc_puts(doc, "%%BeginSetup\n");
   
   do_setup(doc, ppd);
 
+  num_options = 0;
+  options     = NULL;
+
   if (!strncmp(line, "%%BeginSetup", 12))
   {
     while (strncmp(line, "%%EndSetup", 10))
@@ -1604,11 +1820,10 @@ copy_setup(cups_file_t  *fp,            /* I - File to read from */
        */
 
         if (doc->number_up == 1 && !doc->fitplot)
-         doc->num_options = include_feature(ppd, line, doc->num_options,
-                                             &(doc->options));
+         num_options = include_feature(ppd, line, num_options, &options);
       }
       else if (strncmp(line, "%%BeginSetup", 12))
-        fwrite(line, 1, linelen, stdout);
+        doc_write(doc, line, linelen);
 
       if ((linelen = cupsFileGetLine(fp, line, linesize)) == 0)
        break;
@@ -1617,29 +1832,35 @@ copy_setup(cups_file_t  *fp,            /* I - File to read from */
     if (!strncmp(line, "%%EndSetup", 10))
       linelen = cupsFileGetLine(fp, line, linesize);
     else
-      fputs("ERROR: Missing %%EndSetup!\n", stderr);
+      fputs(_("ERROR: Missing %%EndSetup!\n"), stderr);
   }
 
-  puts("%%EndSetup");
+  if (num_options > 0)
+  {
+    write_options(doc, ppd, num_options, options);
+    cupsFreeOptions(num_options, options);
+  }
+
+  doc_puts(doc, "%%EndSetup\n");
 
   return (linelen);
 }
 
 
 /*
- * 'copy_trailer()' - Copy the document trailer...
+ * 'copy_trailer()' - Copy the document trailer.
  *
  * This function expects "line" to be filled with a %%Trailer comment line.
  * On return, "line" will contain the next line in the file, if any.
  */
 
-static size_t                          /* O - Length of next line */
+static ssize_t                         /* O - Length of next line */
 copy_trailer(cups_file_t  *fp,         /* I - File to read from */
              pstops_doc_t *doc,                /* I - Document info */
              ppd_file_t   *ppd,                /* I - PPD file */
             int          number,       /* I - Number of pages */
             char         *line,        /* I - Line buffer */
-            size_t       linelen,      /* I - Length of initial line */
+            ssize_t      linelen,      /* I - Length of initial line */
             size_t       linesize)     /* I - Size of line buffer */
 {
  /*
@@ -1676,25 +1897,32 @@ copy_trailer(cups_file_t  *fp,          /* I - File to read from */
 
 
 /*
- * 'do_prolog()' - Send the necessary document prolog commands...
+ * 'do_prolog()' - Send the necessary document prolog commands.
  */
 
 static void
 do_prolog(pstops_doc_t *doc,           /* I - Document information */
           ppd_file_t   *ppd)           /* I - PPD file */
 {
+  char *ps;                            /* PS commands */
+
+
  /*
   * Send the document prolog commands...
   */
 
   if (ppd && ppd->patches)
   {
-    puts("%%BeginFeature: *JobPatchFile 1");
-    puts(ppd->patches);
-    puts("%%EndFeature");
+    doc_puts(doc, "%%BeginFeature: *JobPatchFile 1\n");
+    doc_puts(doc, ppd->patches);
+    doc_puts(doc, "\n%%EndFeature\n");
   }
 
-  ppdEmit(ppd, stdout, PPD_ORDER_PROLOG);
+  if ((ps = ppdEmitString(ppd, PPD_ORDER_PROLOG, 0.0)) != NULL)
+  {
+    doc_puts(doc, ps);
+    free(ps);
+  }
 
  /*
   * Define ESPshowpage here so that applications that define their
@@ -1702,30 +1930,32 @@ do_prolog(pstops_doc_t *doc,            /* I - Document information */
   */
 
   if (doc->use_ESPshowpage)
-    puts("userdict/ESPshowpage/showpage load put\n"
-        "userdict/showpage{}put");
-
+    doc_puts(doc, "userdict/ESPshowpage/showpage load put\n"
+                 "userdict/showpage{}put\n");
 }
 
 
 /*
- * 'do_setup()' - Send the necessary document setup commands...
+ * 'do_setup()' - Send the necessary document setup commands.
  */
 
 static void
 do_setup(pstops_doc_t *doc,            /* I - Document information */
          ppd_file_t   *ppd)            /* I - PPD file */
 {
+  char *ps;                            /* PS commands */
+
+
  /*
   * Disable CTRL-D so that embedded files don't cause printing
   * errors...
   */
 
-  puts("% Disable CTRL-D as an end-of-file marker...");
-  puts("userdict dup(\\004)cvn{}put (\\004\\004)cvn{}put");
+  doc_puts(doc, "% Disable CTRL-D as an end-of-file marker...\n");
+  doc_puts(doc, "userdict dup(\\004)cvn{}put (\\004\\004)cvn{}put\n");
 
  /*
-  * Mark any options from %%IncludeFeature: comments...
+  * Mark job options...
   */
 
   cupsMarkOptions(ppd, doc->num_options, doc->options);
@@ -1734,8 +1964,17 @@ do_setup(pstops_doc_t *doc,              /* I - Document information */
   * Send all the printer-specific setup commands...
   */
 
-  ppdEmit(ppd, stdout, PPD_ORDER_DOCUMENT);
-  ppdEmit(ppd, stdout, PPD_ORDER_ANY);
+  if ((ps = ppdEmitString(ppd, PPD_ORDER_DOCUMENT, 0.0)) != NULL)
+  {
+    doc_puts(doc, ps);
+    free(ps);
+  }
+
+  if ((ps = ppdEmitString(ppd, PPD_ORDER_ANY, 0.0)) != NULL)
+  {
+    doc_puts(doc, ps);
+    free(ps);
+  }
 
  /*
   * Set the number of copies for the job...
@@ -1743,11 +1982,13 @@ do_setup(pstops_doc_t *doc,             /* I - Document information */
 
   if (doc->copies != 1 && (!doc->collate || !doc->slow_collate))
   {
-    printf("%%RBIBeginNonPPDFeature: *NumCopies %d\n", doc->copies);
-    printf("%d/languagelevel where{pop languagelevel 2 ge}{false}ifelse\n"
-           "{1 dict begin/NumCopies exch def currentdict end setpagedevice}\n"
-          "{userdict/#copies 3 -1 roll put}ifelse\n", doc->copies);
-    puts("%RBIEndNonPPDFeature");
+    doc_printf(doc, "%%RBIBeginNonPPDFeature: *NumCopies %d\n", doc->copies);
+    doc_printf(doc,
+               "%d/languagelevel where{pop languagelevel 2 ge}{false}ifelse\n"
+               "{1 dict begin/NumCopies exch def currentdict end "
+              "setpagedevice}\n"
+              "{userdict/#copies 3 -1 roll put}ifelse\n", doc->copies);
+    doc_puts(doc, "%RBIEndNonPPDFeature\n");
   }
 
  /*
@@ -1755,7 +1996,7 @@ do_setup(pstops_doc_t *doc,               /* I - Document information */
   */
 
   if (doc->number_up > 1)
-    puts("userdict/setpagedevice{pop}bind put");
+    doc_puts(doc, "userdict/setpagedevice{pop}bind put\n");
 
  /*
   * Changes to the transfer function must be made AFTER any
@@ -1763,15 +2004,31 @@ do_setup(pstops_doc_t *doc,             /* I - Document information */
   */
 
   if (doc->gamma != 1.0f || doc->brightness != 1.0f)
-    printf("{ neg 1 add dup 0 lt { pop 1 } { %.3f exp neg 1 add } "
-          "ifelse %.3f mul } bind settransfer\n", doc->gamma,
-          doc->brightness);
+    doc_printf(doc, "{ neg 1 add dup 0 lt { pop 1 } { %.3f exp neg 1 add } "
+                   "ifelse %.3f mul } bind settransfer\n",
+              doc->gamma, doc->brightness);
 
  /*
   * Make sure we have rectclip and rectstroke procedures of some sort...
   */
 
-  WriteCommon();
+  doc_puts(doc,
+           "% x y w h ESPrc - Clip to a rectangle.\n"
+          "userdict/ESPrc/rectclip where{pop/rectclip load}\n"
+          "{{newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
+          "neg 0 rlineto closepath clip newpath}bind}ifelse put\n");
+
+  doc_puts(doc,
+           "% x y w h ESPrf - Fill a rectangle.\n"
+          "userdict/ESPrf/rectfill where{pop/rectfill load}\n"
+          "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
+          "neg 0 rlineto closepath fill grestore}bind}ifelse put\n");
+
+  doc_puts(doc,
+           "% x y w h ESPrs - Stroke a rectangle.\n"
+          "userdict/ESPrs/rectstroke where{pop/rectstroke load}\n"
+          "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
+          "neg 0 rlineto closepath stroke grestore}bind}ifelse put\n");
 
  /*
   * Write the page and label prologs...
@@ -1785,13 +2042,14 @@ do_setup(pstops_doc_t *doc,             /* I - Document information */
     */
 
     if (Orientation & 1)
-      WriteLabelProlog(doc->page_label, PageBottom,
-                       PageWidth - PageLength + PageTop, PageLength);
+      write_label_prolog(doc, doc->page_label, PageBottom,
+                         PageWidth - PageLength + PageTop, PageLength);
     else
-      WriteLabelProlog(doc->page_label, PageLeft, PageRight, PageLength);
+      write_label_prolog(doc, doc->page_label, PageLeft, PageRight,
+                         PageLength);
   }
   else
-    WriteLabelProlog(doc->page_label, PageBottom, PageTop, PageWidth);
+    write_label_prolog(doc, doc->page_label, PageBottom, PageTop, PageWidth);
 }
 
 
@@ -1819,7 +2077,7 @@ doc_printf(pstops_doc_t *doc,             /* I - Document information */
   if (bytes > sizeof(buffer))
   {
     fprintf(stderr,
-            "ERROR: doc_printf overflow (%d bytes) detected, aborting!\n",
+            _("ERROR: doc_printf overflow (%d bytes) detected, aborting!\n"),
             (int)bytes);
     exit(1);
   }
@@ -1861,14 +2119,14 @@ doc_write(pstops_doc_t *doc,            /* I - Document information */
 
 
 /*
- * 'end_nup()' - End processing for N-up printing...
+ * 'end_nup()' - End processing for N-up printing.
  */
 
 static void
 end_nup(pstops_doc_t *doc,             /* I - Document information */
         int          number)           /* I - Page number */
 {
-  if (doc->mirror || Orientation || doc->number_up > 1)
+  if (doc->number_up > 1)
     doc_puts(doc, "userdict/ESPsave get restore\n");
 
   switch (doc->number_up)
@@ -1941,7 +2199,6 @@ include_feature(
   char         name[255],              /* Option name */
                value[255];             /* Option value */
   ppd_option_t *option;                /* Option in file */
-  ppd_choice_t *choice;                /* Choice */
 
 
  /*
@@ -1950,7 +2207,7 @@ include_feature(
 
   if (sscanf(line + 17, "%254s%254s", name, value) != 2)
   {
-    fputs("ERROR: Bad %%IncludeFeature: comment!\n", stderr);
+    fputs(_("ERROR: Bad %%IncludeFeature: comment!\n"), stderr);
     return (num_options);
   }
 
@@ -1960,21 +2217,21 @@ include_feature(
 
   if ((option = ppdFindOption(ppd, name + 1)) == NULL)
   {
-    fprintf(stderr, "WARNING: Unknown option \"%s\"!\n", name + 1);
+    fprintf(stderr, _("WARNING: Unknown option \"%s\"!\n"), name + 1);
     return (num_options);
   }
 
   if (option->section == PPD_ORDER_EXIT ||
       option->section == PPD_ORDER_JCL)
   {
-    fprintf(stderr, "WARNING: Option \"%s\" cannot be included via "
-                    "IncludeFeature!\n", name + 1);
+    fprintf(stderr, _("WARNING: Option \"%s\" cannot be included via "
+                      "IncludeFeature!\n"), name + 1);
     return (num_options);
   }
 
-  if ((choice = ppdFindChoice(option, value)) == NULL)
+  if (!ppdFindChoice(option, value))
   {
-    fprintf(stderr, "WARNING: Unknown choice \"%s\" for option \"%s\"!\n",
+    fprintf(stderr, _("WARNING: Unknown choice \"%s\" for option \"%s\"!\n"),
             value, name + 1);
     return (num_options);
   }
@@ -1988,7 +2245,7 @@ include_feature(
 
 
 /*
- * 'parse_text()' - Parse a text value in a comment...
+ * 'parse_text()' - Parse a text value in a comment.
  *
  * This function parses a DSC text value as defined on page 36 of the
  * DSC specification.  Text values are either surrounded by parenthesis
@@ -2077,7 +2334,7 @@ parse_text(const char *start,             /* I - Start of text value */
 
 
 /*
- * 'set_pstops_options()' - Set pstops options...
+ * 'set_pstops_options()' - Set pstops options.
  */
 
 static void
@@ -2147,8 +2404,8 @@ set_pstops_options(
 
     if (intval < 10 || intval > 1000)
     {
-      fprintf(stderr, "ERROR: Unsupported brightness value %s, using "
-                      "brightness=100!\n", val);
+      fprintf(stderr, _("ERROR: Unsupported brightness value %s, using "
+                        "brightness=100!\n"), val);
       doc->brightness = 1.0f;
     }
     else
@@ -2214,8 +2471,8 @@ set_pstops_options(
 
     if (intval < 1 || intval > 10000)
     {
-      fprintf(stderr, "ERROR: Unsupported gamma value %s, using "
-                      "gamma=1000!\n", val);
+      fprintf(stderr, _("ERROR: Unsupported gamma value %s, using "
+                        "gamma=1000!\n"), val);
       doc->gamma = 1.0f;
     }
     else
@@ -2238,9 +2495,16 @@ set_pstops_options(
   if ((choice = ppdFindMarkedChoice(ppd, "ManualFeed")) != NULL)
     doc->manual_feed = choice->choice;
 
-  if ((val = cupsGetOption("mirror", num_options, options)) != NULL &&
-      (!strcasecmp(val, "true") || !strcasecmp(val, "on") ||
-       !strcasecmp(val, "yes")))
+  if ((choice = ppdFindMarkedChoice(ppd, "MirrorPrint")) != NULL)
+  {
+    val = choice->choice;
+    choice->marked = 0;
+  }
+  else
+    val = cupsGetOption("mirror", num_options, options);
+
+  if (val && (!strcasecmp(val, "true") || !strcasecmp(val, "on") ||
+              !strcasecmp(val, "yes")))
     doc->mirror = 1;
 
  /*
@@ -2261,8 +2525,8 @@ set_pstops_options(
          break;
       default :
           fprintf(stderr,
-                 "ERROR: Unsupported number-up value %d, using number-up=1!\n",
-                 intval);
+                 _("ERROR: Unsupported number-up value %d, using "
+                   "number-up=1!\n"), intval);
           doc->number_up = 1;
          break;
     }
@@ -2294,8 +2558,8 @@ set_pstops_options(
       doc->number_up_layout = PSTOPS_LAYOUT_BTRL;
     else
     {
-      fprintf(stderr, "ERROR: Unsupported number-up-layout value %s, using "
-                      "number-up-layout=lrtb!\n", val);
+      fprintf(stderr, _("ERROR: Unsupported number-up-layout value %s, using "
+                        "number-up-layout=lrtb!\n"), val);
       doc->number_up_layout = PSTOPS_LAYOUT_LRTB;
     }
   }
@@ -2344,8 +2608,8 @@ set_pstops_options(
       doc->page_border = PSTOPS_BORDERDOUBLE2;
     else
     {
-      fprintf(stderr, "ERROR: Unsupported page-border value %s, using "
-                      "page-border=none!\n", val);
+      fprintf(stderr, _("ERROR: Unsupported page-border value %s, using "
+                        "page-border=none!\n"), val);
       doc->page_border = PSTOPS_BORDERNONE;
     }
   }
@@ -2407,7 +2671,7 @@ set_pstops_options(
       * turn the hardware collate option off...
       */
 
-      if ((option = ppdFindOption(ppd, "Option")) != NULL &&
+      if ((option = ppdFindOption(ppd, "Collate")) != NULL &&
           !option->conflicted)
        doc->slow_collate = 0;
       else
@@ -2422,7 +2686,10 @@ set_pstops_options(
   else
     doc->slow_order = 0;
 
-  if ((doc->slow_collate || doc->slow_order) && Duplex)
+  if (Duplex &&
+       (doc->slow_collate || doc->slow_order ||
+        ((attr = ppdFindAttr(ppd, "cupsEvenDuplex", NULL)) != NULL &&
+        attr->value && !strcasecmp(attr->value, "true"))))
     doc->slow_duplex = 1;
   else
     doc->slow_duplex = 0;
@@ -2436,7 +2703,7 @@ set_pstops_options(
     if ((doc->temp = cupsTempFile2(doc->tempfile,
                                    sizeof(doc->tempfile))) == NULL)
     {
-      fprintf(stderr, "ERROR: Unable to create temporary file: %s\n",
+      fprintf(stderr, _("ERROR: Unable to create temporary file: %s\n"),
               strerror(errno));
       exit(1);
     }
@@ -2462,13 +2729,13 @@ set_pstops_options(
 
 
 /*
- * 'skip_page()' - Skip past a page that won't be printed...
+ * 'skip_page()' - Skip past a page that won't be printed.
  */
 
-static size_t                          /* O - Length of next line */
+static ssize_t                         /* O - Length of next line */
 skip_page(cups_file_t *fp,             /* I - File to read from */
           char        *line,           /* I - Line buffer */
-         size_t      linelen,          /* I - Length of initial line */
+         ssize_t     linelen,          /* I - Length of initial line */
           size_t      linesize)                /* I - Size of line buffer */
 {
   int  level;                          /* Embedded document level */
@@ -2524,7 +2791,7 @@ skip_page(cups_file_t *fp,                /* I - File to read from */
 
 
 /*
- * 'start_nup()' - Start processing for N-up printing...
+ * 'start_nup()' - Start processing for N-up printing.
  */
 
 static void
@@ -2539,33 +2806,38 @@ start_nup(pstops_doc_t *doc,            /* I - Document information */
                tx, ty;                 /* Translation values for subpage */
   float                pagew,                  /* Printable width of page */
                pagel;                  /* Printable height of page */
-  int          bboxw,                  /* BoundingBox width */
+  int          bboxx,                  /* BoundingBox X origin */
+               bboxy,                  /* BoundingBox Y origin */
+               bboxw,                  /* BoundingBox width */
                bboxl;                  /* BoundingBox height */
+  float                margin = 0;             /* Current margin for border */
 
 
-  if (doc->mirror || Orientation || doc->number_up > 1)
+  if (doc->number_up > 1)
     doc_puts(doc, "userdict/ESPsave save put\n");
 
-  if (doc->mirror)
-    doc_printf(doc, "%.1f 0.0 translate -1 1 scale\n", PageWidth);
-
   pos   = (number - 1) % doc->number_up;
   pagew = PageRight - PageLeft;
   pagel = PageTop - PageBottom;
 
   if (doc->fitplot)
   {
+    bboxx = bounding_box[0];
+    bboxy = bounding_box[1];
     bboxw = bounding_box[2] - bounding_box[0];
     bboxl = bounding_box[3] - bounding_box[1];
   }
   else
   {
+    bboxx = 0;
+    bboxy = 0;
     bboxw = PageWidth;
     bboxl = PageLength;
   }
 
   fprintf(stderr, "DEBUG: pagew = %.1f, pagel = %.1f\n", pagew, pagel);
-  fprintf(stderr, "DEBUG: bboxw = %d, bboxl = %d\n", bboxw, bboxl);
+  fprintf(stderr, "DEBUG: bboxx = %d, bboxy = %d, bboxw = %d, bboxl = %d\n",
+          bboxx, bboxy, bboxw, bboxl);
   fprintf(stderr, "DEBUG: PageLeft = %.1f, PageRight = %.1f\n",
           PageLeft, PageRight);
   fprintf(stderr, "DEBUG: PageTop = %.1f, PageBottom = %.1f\n",
@@ -2592,6 +2864,9 @@ start_nup(pstops_doc_t *doc,              /* I - Document information */
   else if (doc->number_up > 1 || doc->fitplot)
     doc_printf(doc, "%.1f %.1f translate\n", PageLeft, PageBottom);
 
+  if (doc->mirror)
+    doc_printf(doc, "%.1f 0.0 translate -1 1 scale\n", PageWidth);
+
   switch (doc->number_up)
   {
     default :
@@ -2613,10 +2888,7 @@ start_nup(pstops_doc_t *doc,             /* I - Document information */
                     w / bboxw, l / bboxl);
        }
        else
-       {
           w = PageWidth;
-         l = PageLength;
-       }
        break;
 
     case 2 :
@@ -2883,8 +3155,7 @@ start_nup(pstops_doc_t *doc,              /* I - Document information */
   if (doc->page_border && show_border)
   {
     int                rects;                  /* Number of border rectangles */
-    float      fscale,                 /* Scaling value for points */
-               margin;                 /* Current margin for borders */
+    float      fscale;                 /* Scaling value for points */
 
 
     rects  = (doc->page_border & PSTOPS_BORDERDOUBLE) ? 2 : 1;
@@ -2907,10 +3178,10 @@ start_nup(pstops_doc_t *doc,            /* I - Document information */
     for (; rects > 0; rects --, margin += 2 * fscale)
       if (doc->number_up > 1)
        doc_printf(doc, "%.1f %.1f %.1f %.1f ESPrs\n",
-                  margin - 2.25 * fscale,
-                  margin - 2.25 * fscale,
-                  bboxw + 4.5 * fscale - 2 * margin,
-                  bboxl + 4.5 * fscale - 2 * margin);
+                  margin,
+                  margin,
+                  bboxw - 2 * margin,
+                  bboxl - 2 * margin);
       else
        doc_printf(doc, "%.1f %.1f %.1f %.1f ESPrs\n",
                   PageLeft + margin,
@@ -2928,25 +3199,143 @@ start_nup(pstops_doc_t *doc,           /* I - Document information */
   if (doc->fitplot)
   {
    /*
-    * Clip the page that follows to the bounding box of the page...
+    * Offset the page by its bounding box...
     */
 
     doc_printf(doc, "%d %d translate\n", -bounding_box[0],
                -bounding_box[1]);
-    doc_printf(doc, "%d %d %d %d ESPrc\n", bounding_box[0], bounding_box[1],
-               bboxw, bboxl);
   }
-  else if (doc->number_up > 1)
+
+  if (doc->fitplot || doc->number_up > 1)
   {
    /*
-    * Clip the page that follows to the default page size...
+    * Clip the page to the page's bounding box...
     */
 
-    doc_printf(doc, "0 0 %d %d ESPrc\n", bboxw, bboxl);
+    doc_printf(doc, "%.1f %.1f %.1f %.1f ESPrc\n",
+               bboxx + margin, bboxy + margin,
+               bboxw - 2 * margin, bboxl - 2 * margin);
   }
 }
 
 
+/*
+ * 'write_label_prolog()' - Write the prolog with the classification
+ *                          and page label.
+ */
+
+static void
+write_label_prolog(pstops_doc_t *doc,  /* I - Document info */
+                   const char   *label,        /* I - Page label */
+                  float        bottom, /* I - Bottom position in points */
+                  float        top,    /* I - Top position in points */
+                  float        width)  /* I - Width in points */
+{
+  const char   *classification;        /* CLASSIFICATION environment variable */
+  const char   *ptr;                   /* Temporary string pointer */
+
+
+ /*
+  * First get the current classification...
+  */
+
+  if ((classification = getenv("CLASSIFICATION")) == NULL)
+    classification = "";
+  if (strcmp(classification, "none") == 0)
+    classification = "";
+
+ /*
+  * If there is nothing to show, bind an empty 'write labels' procedure
+  * and return...
+  */
+
+  if (!classification[0] && (label == NULL || !label[0]))
+  {
+    doc_puts(doc, "userdict/ESPwl{}bind put\n");
+    return;
+  }
+
+ /*
+  * Set the classification + page label string...
+  */
+
+  doc_puts(doc, "userdict");
+  if (!strcmp(classification, "confidential"))
+    doc_puts(doc, "/ESPpl(CONFIDENTIAL");
+  else if (!strcmp(classification, "classified"))
+    doc_puts(doc, "/ESPpl(CLASSIFIED");
+  else if (!strcmp(classification, "secret"))
+    doc_puts(doc, "/ESPpl(SECRET");
+  else if (!strcmp(classification, "topsecret"))
+    doc_puts(doc, "/ESPpl(TOP SECRET");
+  else if (!strcmp(classification, "unclassified"))
+    doc_puts(doc, "/ESPpl(UNCLASSIFIED");
+  else
+  {
+    doc_puts(doc, "/ESPpl(");
+
+    for (ptr = classification; *ptr; ptr ++)
+    {
+      if (*ptr < 32 || *ptr > 126)
+        doc_printf(doc, "\\%03o", *ptr);
+      else if (*ptr == '_')
+        doc_puts(doc, " ");
+      else if (*ptr == '(' || *ptr == ')' || *ptr == '\\')
+       doc_printf(doc, "\\%c", *ptr);
+      else
+        doc_printf(doc, "%c", *ptr);
+    }
+  }
+
+  if (label)
+  {
+    if (classification[0])
+      doc_puts(doc, " - ");
+
+   /*
+    * Quote the label string as needed...
+    */
+
+    for (ptr = label; *ptr; ptr ++)
+    {
+      if (*ptr < 32 || *ptr > 126)
+        doc_printf(doc, "\\%03o", *ptr);
+      else if (*ptr == '(' || *ptr == ')' || *ptr == '\\')
+       doc_printf(doc, "\\%c", *ptr);
+      else
+        doc_printf(doc, "%c", *ptr);
+    }
+  }
+
+  doc_puts(doc, ")put\n");
+
+ /*
+  * Then get a 14 point Helvetica-Bold font...
+  */
+
+  doc_puts(doc, "userdict/ESPpf /Helvetica-Bold findfont 14 scalefont put\n");
+
+ /*
+  * Finally, the procedure to write the labels on the page...
+  */
+
+  doc_puts(doc, "userdict/ESPwl{\n");
+  doc_puts(doc, "  ESPpf setfont\n");
+  doc_printf(doc, "  ESPpl stringwidth pop dup 12 add exch -0.5 mul %.0f add\n",
+             width * 0.5f);
+  doc_puts(doc, "  1 setgray\n");
+  doc_printf(doc, "  dup 6 sub %.0f 3 index 20 ESPrf\n", bottom - 2.0);
+  doc_printf(doc, "  dup 6 sub %.0f 3 index 20 ESPrf\n", top - 18.0);
+  doc_puts(doc, "  0 setgray\n");
+  doc_printf(doc, "  dup 6 sub %.0f 3 index 20 ESPrs\n", bottom - 2.0);
+  doc_printf(doc, "  dup 6 sub %.0f 3 index 20 ESPrs\n", top - 18.0);
+  doc_printf(doc, "  dup %.0f moveto ESPpl show\n", bottom + 2.0);
+  doc_printf(doc, "  %.0f moveto ESPpl show\n", top - 14.0);
+  doc_puts(doc, "pop\n");
+  doc_puts(doc, "}bind put\n");
+}
+
+
 /*
  * 'write_labels()' - Write the actual page labels.
  *
@@ -2994,5 +3383,64 @@ write_labels(pstops_doc_t *doc,          /* I - Document information */
 
 
 /*
- * End of "$Id: pstops.c 5886 2006-08-24 19:53:17Z mike $".
+ * 'write_options()' - Write options provided via %%IncludeFeature.
+ */
+
+static void
+write_options(
+    pstops_doc_t  *doc,                /* I - Document */
+    ppd_file_t    *ppd,                /* I - PPD file */
+    int           num_options, /* I - Number of options */
+    cups_option_t *options)    /* I - Options */
+{
+  int          i;              /* Looping var */
+  ppd_option_t *option;        /* PPD option */
+  int          min_order;      /* Minimum OrderDependency value */
+  char         *doc_setup,     /* DocumentSetup commands to send */
+               *any_setup;     /* AnySetup commands to send */
+
+
+ /*
+  * Figure out the minimum OrderDependency value...
+  */
+
+  if ((option = ppdFindOption(ppd, "PageRegion")) != NULL)
+    min_order = option->order;
+  else
+    min_order = 999.0f;
+
+  for (i = 0; i < num_options; i ++)
+    if ((option = ppdFindOption(ppd, options[i].name)) != NULL &&
+       option->order < min_order)
+      min_order = option->order;
+
+ /*
+  * Mark and extract them...
+  */
+
+  cupsMarkOptions(ppd, num_options, options);
+
+  doc_setup = ppdEmitString(ppd, PPD_ORDER_DOCUMENT, min_order);
+  any_setup = ppdEmitString(ppd, PPD_ORDER_ANY, min_order);
+
+ /*
+  * Then send them out...
+  */
+
+  if (doc_setup)
+  {
+    doc_puts(doc, doc_setup);
+    free(doc_setup);
+  }
+
+  if (any_setup)
+  {
+    doc_puts(doc, any_setup);
+    free(any_setup);
+  }
+}
+
+
+/*
+ * End of "$Id: pstops.c 7977 2008-09-23 23:44:33Z mike $".
  */