]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/emit.c
Import CUPS trunk (1.4svn) r7116.
[thirdparty/cups.git] / cups / emit.c
index 6f4dc78de8ffdcc39600192baf93e5d1d4b9c81f..ccbc0259639181617e96208d168c6e3ae18fa023 100644 (file)
@@ -1,25 +1,16 @@
 /*
- * "$Id: emit.c 5257 2006-03-09 15:27:27Z mike $"
+ * "$Id: emit.c 6649 2007-07-11 21:46:42Z mike $"
  *
  *   PPD code emission routines for the Common UNIX Printing System (CUPS).
  *
- *   Copyright 1997-2006 by Easy Software Products, all rights reserved.
+ *   Copyright 2007 by Apple Inc.
+ *   Copyright 1997-2007 by Easy Software Products, all rights reserved.
  *
  *   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/".
  *
  *   PostScript is a trademark of Adobe Systems, Inc.
  *
@@ -39,7 +30,6 @@
  *   ppdEmitJCLEnd()     - Emit JCLEnd code to a file.
  *   ppdEmitString()     - Get a string containing the code for marked options.
  *   ppd_handle_media()  - Handle media selection...
- *   ppd_sort()          - Sort options by ordering numbers...
  */
 
 /*
@@ -50,6 +40,7 @@
 #include <stdlib.h>
 #include "string.h"
 #include <errno.h>
+#include "debug.h"
 
 #if defined(WIN32) || defined(__EMX__)
 #  include <io.h>
@@ -63,7 +54,6 @@
  */
 
 static void    ppd_handle_media(ppd_file_t *ppd);
-static int     ppd_sort(ppd_choice_t **c1, ppd_choice_t **c2);
 
 
 /*
@@ -102,49 +92,87 @@ ppdCollect2(ppd_file_t    *ppd,            /* I - PPD file data */
            float         min_order,    /* I - Minimum OrderDependency value */
             ppd_choice_t  ***choices)  /* O - Pointers to choices */
 {
-  int          i, j, k, m;             /* Looping vars */
-  ppd_group_t  *g,                     /* Current group */
-               *sg;                    /* Current sub-group */
-  ppd_option_t *o;                     /* Current option */
   ppd_choice_t *c;                     /* Current choice */
+  ppd_section_t        csection;               /* Current section */
+  float                corder;                 /* Current OrderDependency value */
   int          count;                  /* Number of choices collected */
   ppd_choice_t **collect;              /* Collected choices */
+  float                *orders;                /* Collected order values */
+
 
+  DEBUG_printf(("ppdCollect2(ppd=%p, section=%d, min_order=%f, choices=%p)\n",
+                ppd, section, min_order, choices));
 
   if (ppd == NULL)
     return (0);
 
  /*
-  * Allocate memory for up to 1000 selected choices...
+  * Allocate memory for up to N selected choices...
   */
 
   count   = 0;
-  collect = calloc(sizeof(ppd_choice_t *), 1000);
+  collect = calloc(sizeof(ppd_choice_t *), cupsArrayCount(ppd->marked));
+  orders  = calloc(sizeof(float), cupsArrayCount(ppd->marked));
 
  /*
   * Loop through all options and add choices as needed...
   */
 
-  for (i = ppd->num_groups, g = ppd->groups; i > 0; i --, g ++)
+  for (c = (ppd_choice_t *)cupsArrayFirst(ppd->marked);
+       c;
+       c = (ppd_choice_t *)cupsArrayNext(ppd->marked))
   {
-    for (j = g->num_options, o = g->options; j > 0; j --, o ++)
-      if (o->section == section && o->order >= min_order)
-       for (k = o->num_choices, c = o->choices; k > 0; k --, c ++)
-         if (c->marked && count < 1000)
-         {
-            collect[count] = c;
-           count ++;
-         }
+    csection = c->option->section;
+    corder   = c->option->order;
+
+    if (!strcmp(c->choice, "Custom"))
+    {
+      ppd_attr_t       *attr;          /* NonUIOrderDependency value */
+      float            aorder;         /* Order value */
+      char             asection[17],   /* Section name */
+                       amain[PPD_MAX_NAME + 1],
+                       aoption[PPD_MAX_NAME];
+                                       /* *CustomFoo and True */
+
+
+      for (attr = ppdFindAttr(ppd, "NonUIOrderDependency", NULL);
+           attr;
+          attr = ppdFindNextAttr(ppd, "NonUIOrderDependency", NULL))
+        if (attr->value &&
+           sscanf(attr->value, "%f%16s%41s%40s", &aorder, asection, amain,
+                  aoption) == 4 &&
+           !strncmp(amain, "*Custom", 7) &&
+           !strcmp(amain + 7, c->option->keyword) && !strcmp(aoption, "True"))
+       {
+        /*
+         * Use this NonUIOrderDependency...
+         */
+
+          corder = aorder;
+
+         if (!strcmp(asection, "DocumentSetup"))
+           csection = PPD_ORDER_DOCUMENT;
+         else if (!strcmp(asection, "ExitServer"))
+           csection = PPD_ORDER_EXIT;
+         else if (!strcmp(asection, "JCLSetup"))
+           csection = PPD_ORDER_JCL;
+         else if (!strcmp(asection, "PageSetup"))
+           csection = PPD_ORDER_PAGE;
+         else if (!strcmp(asection, "Prolog"))
+           csection = PPD_ORDER_PROLOG;
+         else
+           csection = PPD_ORDER_ANY;
 
-    for (j = g->num_subgroups, sg = g->subgroups; j > 0; j --, sg ++)
-      for (k = sg->num_options, o = sg->options; k > 0; k --, o ++)
-       if (o->section == section && o->order >= min_order)
-         for (m = o->num_choices, c = o->choices; m > 0; m --, c ++)
-           if (c->marked && count < 1000)
-           {
-              collect[count] = c;
-             count ++;
-           }
+         break;
+       }
+    }
+
+    if (csection == section && corder >= min_order)
+    {
+      collect[count] = c;
+      orders[count]  = corder;
+      count ++;
+    }
   }
 
  /*
@@ -152,8 +180,25 @@ ppdCollect2(ppd_file_t    *ppd,            /* I - PPD file data */
   */
 
   if (count > 1)
-    qsort(collect, count, sizeof(ppd_choice_t *),
-          (int (*)(const void *, const void *))ppd_sort);
+  {
+    int i, j;                          /* Looping vars */
+
+    for (i = 0; i < (count - 1); i ++)
+      for (j = i + 1; j < count; j ++)
+        if (orders[i] > orders[j])
+       {
+         c          = collect[i];
+         corder     = orders[i];
+         collect[i] = collect[j];
+         orders[i]  = orders[j];
+         collect[j] = c;
+         orders[j]  = corder;
+       }
+  }
+
+  free(orders);
+
+  DEBUG_printf(("ppdCollect2: %d marked choices...\n", count));
 
  /*
   * Return the array and number of choices; if 0, free the array since
@@ -281,7 +326,11 @@ ppdEmitFd(ppd_file_t    *ppd,              /* I - PPD file record */
 
     while (buflength > 0)
     {
+#ifdef WIN32
+      if ((bytes = (ssize_t)write(fd, bufptr, (unsigned)buflength)) < 0)
+#else
       if ((bytes = write(fd, bufptr, buflength)) < 0)
+#endif /* WIN32 */
       {
         if (errno == EAGAIN || errno == EINTR)
          continue;
@@ -341,6 +390,15 @@ ppdEmitJCL(ppd_file_t *ppd,                /* I - PPD file record */
     * of the PJL commands to initialize PJL processing.
     */
 
+    ppd_attr_t *charset;               /* PJL charset */
+
+
+    if ((charset = ppdFindAttr(ppd, "cupsPJLCharset", NULL)) != NULL)
+    {
+      if (!charset->value || strcasecmp(charset->value, "UTF-8"))
+        charset = NULL;
+    }
+
     fputs("\033%-12345X@PJL\n", fp);
     for (ptr = ppd->jcl_begin + 9; *ptr;)
       if (!strncmp(ptr, "@PJL JOB", 8))
@@ -381,8 +439,8 @@ ppdEmitJCL(ppd_file_t *ppd,         /* I - PPD file record */
       title = ptr + 1;
 
    /*
-    * Replace double quotes with single quotes so that the title
-    * does not cause a PJL syntax error.
+    * Replace double quotes with single quotes and 8-bit characters with
+    * question marks so that the title does not cause a PJL syntax error.
     */
 
     strlcpy(temp, title, sizeof(temp));
@@ -390,6 +448,8 @@ ppdEmitJCL(ppd_file_t *ppd,         /* I - PPD file record */
     for (ptr = temp; *ptr; ptr ++)
       if (*ptr == '\"')
         *ptr = '\'';
+      else if (charset && (*ptr & 128))
+        *ptr = '?';
 
    /*
     * Send PJL JOB and PJL RDYMSG commands before we enter PostScript mode...
@@ -492,6 +552,9 @@ ppdEmitString(ppd_file_t    *ppd,   /* I - PPD file record */
   struct lconv *loc;                   /* Locale data */
 
 
+  DEBUG_printf(("ppdEmitString(ppd=%p, section=%d, min_order=%f)\n",
+                ppd, section, min_order));
+
  /*
   * Range check input...
   */
@@ -527,6 +590,8 @@ ppdEmitString(ppd_file_t    *ppd,   /* I - PPD file record */
            !strcasecmp(choices[i]->option->keyword, "PageRegion")) &&
           !strcasecmp(choices[i]->choice, "Custom"))
       {
+        DEBUG_puts("ppdEmitString: Custom size set!");
+
         bufsize += 37;                 /* %%BeginFeature: *CustomPageSize True\n */
         bufsize += 50;                 /* Five 9-digit numbers + newline */
       }
@@ -580,6 +645,8 @@ ppdEmitString(ppd_file_t    *ppd,   /* I - PPD file record */
   * Allocate memory...
   */
 
+  DEBUG_printf(("ppdEmitString: Allocating %d bytes for string...\n", bufsize));
+
   if ((buffer = calloc(1, bufsize)) == NULL)
   {
     free(choices);
@@ -608,6 +675,9 @@ ppdEmitString(ppd_file_t    *ppd,   /* I - PPD file record */
       * Send DSC comments with option...
       */
 
+      DEBUG_printf(("Adding code for %s=%s...\n", choices[i]->option->keyword,
+                    choices[i]->choice));
+
       if ((!strcasecmp(choices[i]->option->keyword, "PageSize") ||
            !strcasecmp(choices[i]->option->keyword, "PageRegion")) &&
           !strcasecmp(choices[i]->choice, "Custom"))
@@ -703,7 +773,7 @@ ppdEmitString(ppd_file_t    *ppd,   /* I - PPD file record */
        else
          pos = 4;
 
-       values[pos] = orientation;
+       values[pos] = (float)orientation;
 
         for (pos = 0; pos < 5; pos ++)
        {
@@ -789,7 +859,7 @@ ppdEmitString(ppd_file_t    *ppd,   /* I - PPD file record */
 
       if (choices[i]->code && choices[i]->code[0])
       {
-        j = strlen(choices[i]->code);
+        j = (int)strlen(choices[i]->code);
        memcpy(bufptr, choices[i]->code, j);
        bufptr += j;
 
@@ -800,6 +870,9 @@ ppdEmitString(ppd_file_t    *ppd,   /* I - PPD file record */
       strlcpy(bufptr, "%%EndFeature\n"
                      "} stopped cleartomark\n", bufend - bufptr + 1);
       bufptr += strlen(bufptr);
+
+      DEBUG_printf(("ppdEmitString: Offset in string is %d...\n",
+                    bufptr - buffer));
     }
     else
     {
@@ -900,22 +973,5 @@ ppd_handle_media(ppd_file_t *ppd)
 
 
 /*
- * 'ppd_sort()' - Sort options by ordering numbers...
- */
-
-static int                     /* O - -1 if c1 < c2, 0 if equal, 1 otherwise */
-ppd_sort(ppd_choice_t **c1,    /* I - First choice */
-         ppd_choice_t **c2)    /* I - Second choice */
-{
-  if ((*c1)->option->order < (*c2)->option->order)
-    return (-1);
-  else if ((*c1)->option->order > (*c2)->option->order)
-    return (1);
-  else
-    return (0);
-}
-
-
-/*
- * End of "$Id: emit.c 5257 2006-03-09 15:27:27Z mike $".
+ * End of "$Id: emit.c 6649 2007-07-11 21:46:42Z mike $".
  */