]> git.ipfire.org Git - thirdparty/cups.git/commitdiff
Update PPD cache private API to support getting custom size names (Issue #1238)
authorMichael R Sweet <msweet@msweet.org>
Tue, 27 May 2025 18:55:47 +0000 (14:55 -0400)
committerMichael R Sweet <msweet@msweet.org>
Tue, 27 May 2025 18:55:47 +0000 (14:55 -0400)
cups/ppd-cache.c
cups/ppd-private.h
cups/testppd.c
filter/rastertopwg.c
scheduler/printers.c

index 61e63ae1c891e98a1878439f478ab9168fc815d2..eb7b411b5e5b195f40f2811a171edba746d897b2 100644 (file)
@@ -209,7 +209,7 @@ _cupsConvertOptions(
 
   media_source = _ppdCacheGetSource(pc, cupsGetOption("InputSlot", num_options, options));
   media_type   = _ppdCacheGetType(pc, cupsGetOption("MediaType", num_options, options));
-  size         = _ppdCacheGetSize(pc, keyword);
+  size         = _ppdCacheGetSize(pc, keyword, /*ppd_size*/NULL);
 
   if (media_col_sup && (size || media_source || media_type))
   {
@@ -2760,7 +2760,8 @@ _ppdCacheGetPageSize(
 pwg_size_t *                           /* O - PWG size or NULL */
 _ppdCacheGetSize(
     _ppd_cache_t *pc,                  /* I - PPD cache and mapping data */
-    const char   *page_size)           /* I - PPD PageSize */
+    const char   *page_size,           /* I - PPD PageSize */
+    ppd_size_t   *ppd_size)            /* I - PPD page size information */
 {
   int          i;                      /* Looping var */
   pwg_media_t  *media;                 /* Media */
@@ -2774,7 +2775,7 @@ _ppdCacheGetSize(
   if (!pc || !page_size)
     return (NULL);
 
-  if (!_cups_strncasecmp(page_size, "Custom.", 7))
+  if (!_cups_strcasecmp(page_size, "Custom") || !_cups_strncasecmp(page_size, "Custom.", 7))
   {
    /*
     * Custom size; size name can be one of the following:
@@ -2791,48 +2792,65 @@ _ppdCacheGetSize(
     char               *ptr;           /* Pointer into PageSize */
     struct lconv       *loc;           /* Locale data */
 
-    loc = localeconv();
-    w   = (float)_cupsStrScand(page_size + 7, &ptr, loc);
-    if (!ptr || *ptr != 'x')
-      return (NULL);
+    if (page_size[6])
+    {
+      loc = localeconv();
+      w   = (float)_cupsStrScand(page_size + 7, &ptr, loc);
+      if (!ptr || *ptr != 'x')
+       return (NULL);
 
-    l = (float)_cupsStrScand(ptr + 1, &ptr, loc);
-    if (!ptr)
-      return (NULL);
+      l = (float)_cupsStrScand(ptr + 1, &ptr, loc);
+      if (!ptr)
+       return (NULL);
 
-    if (!_cups_strcasecmp(ptr, "in"))
-    {
-      w *= 2540.0;
-      l *= 2540.0;
-    }
-    else if (!_cups_strcasecmp(ptr, "ft"))
-    {
-      w *= 12.0 * 2540.0;
-      l *= 12.0 * 2540.0;
-    }
-    else if (!_cups_strcasecmp(ptr, "mm"))
-    {
-      w *= 100.0;
-      l *= 100.0;
-    }
-    else if (!_cups_strcasecmp(ptr, "cm"))
-    {
-      w *= 1000.0;
-      l *= 1000.0;
+      if (!_cups_strcasecmp(ptr, "in"))
+      {
+       w *= 2540.0;
+       l *= 2540.0;
+      }
+      else if (!_cups_strcasecmp(ptr, "ft"))
+      {
+       w *= 12.0 * 2540.0;
+       l *= 12.0 * 2540.0;
+      }
+      else if (!_cups_strcasecmp(ptr, "mm"))
+      {
+       w *= 100.0;
+       l *= 100.0;
+      }
+      else if (!_cups_strcasecmp(ptr, "cm"))
+      {
+       w *= 1000.0;
+       l *= 1000.0;
+      }
+      else if (!_cups_strcasecmp(ptr, "m"))
+      {
+       w *= 100000.0;
+       l *= 100000.0;
+      }
+      else
+      {
+       w *= 2540.0 / 72.0;
+       l *= 2540.0 / 72.0;
+      }
     }
-    else if (!_cups_strcasecmp(ptr, "m"))
+    else if (ppd_size)
     {
-      w *= 100000.0;
-      l *= 100000.0;
+      w = ppd_size->width * 2540.0 / 72.0;
+      l = ppd_size->length * 2540.0 / 72.0;
     }
     else
     {
-      w *= 2540.0 / 72.0;
-      l *= 2540.0 / 72.0;
+      // No custom size information...
+      return (NULL);
     }
 
-    pc->custom_size.width  = (int)w;
-    pc->custom_size.length = (int)l;
+    pc->custom_size.map.ppd = (char *)page_size;
+    pc->custom_size.width   = (int)w;
+    pc->custom_size.length  = (int)l;
+
+    if ((media = pwgMediaForSize((int)w, (int)l)) != NULL)
+      pc->custom_size.map.pwg = (char *)media->pwg;
 
     return (&(pc->custom_size));
   }
@@ -2842,22 +2860,28 @@ _ppdCacheGetSize(
   */
 
   for (i = pc->num_sizes, size = pc->sizes; i > 0; i --, size ++)
+  {
     if (!_cups_strcasecmp(page_size, size->map.ppd) ||
         !_cups_strcasecmp(page_size, size->map.pwg))
       return (size);
+  }
 
  /*
   * Look up standard sizes...
   */
 
   if ((media = pwgMediaForPPD(page_size)) == NULL)
+  {
     if ((media = pwgMediaForLegacy(page_size)) == NULL)
       media = pwgMediaForPWG(page_size);
+  }
 
   if (media)
   {
-    pc->custom_size.width  = media->width;
-    pc->custom_size.length = media->length;
+    pc->custom_size.map.ppd = (char *)page_size;
+    pc->custom_size.map.pwg = (char *)media->pwg;
+    pc->custom_size.width   = media->width;
+    pc->custom_size.length  = media->length;
 
     return (&(pc->custom_size));
   }
index 3f88fb46fdface94a112c1c6c1cde0a0d60e31fb..2a0e7783594bfc1d74cf3b38ff5dbd883f16798c 100644 (file)
@@ -158,7 +158,7 @@ extern const char   *_ppdCacheGetInputSlot(_ppd_cache_t *pc, ipp_t *job, const cha
 extern const char      *_ppdCacheGetMediaType(_ppd_cache_t *pc, ipp_t *job, const char *keyword) _CUPS_PRIVATE;
 extern const char      *_ppdCacheGetOutputBin(_ppd_cache_t *pc, const char *keyword) _CUPS_PRIVATE;
 extern const char      *_ppdCacheGetPageSize(_ppd_cache_t *pc, ipp_t *job, const char *keyword, int *exact) _CUPS_PRIVATE;
-extern pwg_size_t      *_ppdCacheGetSize(_ppd_cache_t *pc, const char *page_size) _CUPS_PRIVATE;
+extern pwg_size_t      *_ppdCacheGetSize(_ppd_cache_t *pc, const char *page_size, ppd_size_t *ppd_size) _CUPS_PRIVATE;
 extern const char      *_ppdCacheGetSource(_ppd_cache_t *pc, const char *input_slot) _CUPS_PRIVATE;
 extern const char      *_ppdCacheGetType(_ppd_cache_t *pc, const char *media_type) _CUPS_PRIVATE;
 extern int             _ppdCacheWriteFile(_ppd_cache_t *pc, const char *filename, ipp_t *attrs) _CUPS_PRIVATE;
index 9cac71508f968297f450cc3c3b9a345bf93315ab..9da66cb748b223acd505e0997f650d183c0009e7 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * PPD test program for CUPS.
  *
- * Copyright © 2020-2024 by OpenPrinting.
+ * Copyright © 2020-2025 by OpenPrinting.
  * Copyright © 2007-2018 by Apple Inc.
  * Copyright © 1997-2006 by Easy Software Products.
  *
@@ -9,10 +9,6 @@
  * information.
  */
 
-/*
- * Include necessary headers...
- */
-
 #undef _CUPS_NO_DEPRECATED
 #include "cups-private.h"
 #include "ppd-private.h"
@@ -1208,6 +1204,7 @@ main(int  argc,                           /* I - Number of command-line arguments */
       ppd_option_t     *option;        /* Option */
       ppd_coption_t    *coption;       /* Custom option */
       ppd_cparam_t     *cparam;        /* Custom parameter */
+      ppd_size_t       *size;          /* Default paper size */
       ppd_const_t      *c;             /* UIConstraints */
       char             lang[255],      /* LANG environment variable */
                        lc_all[255],    /* LC_ALL environment variable */
@@ -1359,12 +1356,23 @@ main(int  argc,                         /* I - Number of command-line arguments */
 
       puts("\nPPD Cache:");
       if ((pc = _ppdCacheCreateWithPPD(NULL, ppd)) == NULL)
+      {
         printf("    Unable to create: %s\n", cupsGetErrorString());
+      }
       else
       {
         _ppdCacheWriteFile(pc, "t.cache", NULL);
         puts("    Wrote t.cache.");
       }
+
+      if ((size = ppdPageSize(ppd, NULL)) != NULL)
+      {
+        pwg_size_t     *pwg;           /* PWG media size */
+
+       pwg = _ppdCacheGetSize(pc, size->name, size);
+
+        printf("    media-default: %s\n", pwg ? pwg->map.pwg : "unknown");
+      }
     }
 
     if (!strncmp(argv[1], "-d", 2))
index bf6743046e034dbdfd5be86d71c87c46c1a64340..9c50f71a74966e43f8594fd97aa77435329a90c6 100644 (file)
@@ -1,17 +1,13 @@
 /*
  * CUPS raster to PWG raster format filter for CUPS.
  *
- * Copyright © 2020-2024 by OpenPrinting.
+ * Copyright © 2020-2025 by OpenPrinting.
  * Copyright © 2011, 2014-2017 Apple Inc.
  *
  * Licensed under Apache License v2.0.  See the file "LICENSE" for more
  * information.
  */
 
-/*
- * Include necessary headers...
- */
-
 #include <cups/cups-private.h>
 #include <cups/ppd-private.h>
 #include <cups/raster.h>
@@ -278,7 +274,7 @@ main(int  argc,                             /* I - Number of command-line args */
       }
     }
 
-    if (inheader.cupsPageSizeName[0] && (pwg_size = _ppdCacheGetSize(cache, inheader.cupsPageSizeName)) != NULL && pwg_size->map.pwg)
+    if (inheader.cupsPageSizeName[0] && (pwg_size = _ppdCacheGetSize(cache, inheader.cupsPageSizeName, /*ppd_size*/NULL)) != NULL && pwg_size->map.pwg)
     {
       cupsCopyString(outheader.cupsPageSizeName, pwg_size->map.pwg,
              sizeof(outheader.cupsPageSizeName));
index 31d66322f1aab372a68d11ba419d4c50f3e8a456..6b78e0d3253fc92d1e5af0a7d4fe02cf355e0fcb 100644 (file)
@@ -4297,7 +4297,7 @@ load_ppd(cupsd_printer_t *p)              /* I - Printer */
       */
 
       if ((size = ppdPageSize(ppd, NULL)) != NULL)
-        pwgsize = _ppdCacheGetSize(p->pc, size->name);
+       pwgsize = _ppdCacheGetSize(p->pc, size->name, size);
       else
         pwgsize = NULL;