Skip to content

Commit 46c58ec

Browse files
committed
Update PPD cache private API to support getting custom size names (Issue #1238)
1 parent b037e19 commit 46c58ec

5 files changed

Lines changed: 79 additions & 51 deletions

File tree

cups/ppd-cache.c

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ _cupsConvertOptions(
209209

210210
media_source = _ppdCacheGetSource(pc, cupsGetOption("InputSlot", num_options, options));
211211
media_type = _ppdCacheGetType(pc, cupsGetOption("MediaType", num_options, options));
212-
size = _ppdCacheGetSize(pc, keyword);
212+
size = _ppdCacheGetSize(pc, keyword, /*ppd_size*/NULL);
213213

214214
if (media_col_sup && (size || media_source || media_type))
215215
{
@@ -2760,7 +2760,8 @@ _ppdCacheGetPageSize(
27602760
pwg_size_t * /* O - PWG size or NULL */
27612761
_ppdCacheGetSize(
27622762
_ppd_cache_t *pc, /* I - PPD cache and mapping data */
2763-
const char *page_size) /* I - PPD PageSize */
2763+
const char *page_size, /* I - PPD PageSize */
2764+
ppd_size_t *ppd_size) /* I - PPD page size information */
27642765
{
27652766
int i; /* Looping var */
27662767
pwg_media_t *media; /* Media */
@@ -2774,7 +2775,7 @@ _ppdCacheGetSize(
27742775
if (!pc || !page_size)
27752776
return (NULL);
27762777

2777-
if (!_cups_strncasecmp(page_size, "Custom.", 7))
2778+
if (!_cups_strcasecmp(page_size, "Custom") || !_cups_strncasecmp(page_size, "Custom.", 7))
27782779
{
27792780
/*
27802781
* Custom size; size name can be one of the following:
@@ -2791,48 +2792,65 @@ _ppdCacheGetSize(
27912792
char *ptr; /* Pointer into PageSize */
27922793
struct lconv *loc; /* Locale data */
27932794

2794-
loc = localeconv();
2795-
w = (float)_cupsStrScand(page_size + 7, &ptr, loc);
2796-
if (!ptr || *ptr != 'x')
2797-
return (NULL);
2795+
if (page_size[6])
2796+
{
2797+
loc = localeconv();
2798+
w = (float)_cupsStrScand(page_size + 7, &ptr, loc);
2799+
if (!ptr || *ptr != 'x')
2800+
return (NULL);
27982801

2799-
l = (float)_cupsStrScand(ptr + 1, &ptr, loc);
2800-
if (!ptr)
2801-
return (NULL);
2802+
l = (float)_cupsStrScand(ptr + 1, &ptr, loc);
2803+
if (!ptr)
2804+
return (NULL);
28022805

2803-
if (!_cups_strcasecmp(ptr, "in"))
2804-
{
2805-
w *= 2540.0;
2806-
l *= 2540.0;
2807-
}
2808-
else if (!_cups_strcasecmp(ptr, "ft"))
2809-
{
2810-
w *= 12.0 * 2540.0;
2811-
l *= 12.0 * 2540.0;
2812-
}
2813-
else if (!_cups_strcasecmp(ptr, "mm"))
2814-
{
2815-
w *= 100.0;
2816-
l *= 100.0;
2817-
}
2818-
else if (!_cups_strcasecmp(ptr, "cm"))
2819-
{
2820-
w *= 1000.0;
2821-
l *= 1000.0;
2806+
if (!_cups_strcasecmp(ptr, "in"))
2807+
{
2808+
w *= 2540.0;
2809+
l *= 2540.0;
2810+
}
2811+
else if (!_cups_strcasecmp(ptr, "ft"))
2812+
{
2813+
w *= 12.0 * 2540.0;
2814+
l *= 12.0 * 2540.0;
2815+
}
2816+
else if (!_cups_strcasecmp(ptr, "mm"))
2817+
{
2818+
w *= 100.0;
2819+
l *= 100.0;
2820+
}
2821+
else if (!_cups_strcasecmp(ptr, "cm"))
2822+
{
2823+
w *= 1000.0;
2824+
l *= 1000.0;
2825+
}
2826+
else if (!_cups_strcasecmp(ptr, "m"))
2827+
{
2828+
w *= 100000.0;
2829+
l *= 100000.0;
2830+
}
2831+
else
2832+
{
2833+
w *= 2540.0 / 72.0;
2834+
l *= 2540.0 / 72.0;
2835+
}
28222836
}
2823-
else if (!_cups_strcasecmp(ptr, "m"))
2837+
else if (ppd_size)
28242838
{
2825-
w *= 100000.0;
2826-
l *= 100000.0;
2839+
w = ppd_size->width * 2540.0 / 72.0;
2840+
l = ppd_size->length * 2540.0 / 72.0;
28272841
}
28282842
else
28292843
{
2830-
w *= 2540.0 / 72.0;
2831-
l *= 2540.0 / 72.0;
2844+
// No custom size information...
2845+
return (NULL);
28322846
}
28332847

2834-
pc->custom_size.width = (int)w;
2835-
pc->custom_size.length = (int)l;
2848+
pc->custom_size.map.ppd = (char *)page_size;
2849+
pc->custom_size.width = (int)w;
2850+
pc->custom_size.length = (int)l;
2851+
2852+
if ((media = pwgMediaForSize((int)w, (int)l)) != NULL)
2853+
pc->custom_size.map.pwg = (char *)media->pwg;
28362854

28372855
return (&(pc->custom_size));
28382856
}
@@ -2842,22 +2860,28 @@ _ppdCacheGetSize(
28422860
*/
28432861

28442862
for (i = pc->num_sizes, size = pc->sizes; i > 0; i --, size ++)
2863+
{
28452864
if (!_cups_strcasecmp(page_size, size->map.ppd) ||
28462865
!_cups_strcasecmp(page_size, size->map.pwg))
28472866
return (size);
2867+
}
28482868

28492869
/*
28502870
* Look up standard sizes...
28512871
*/
28522872

28532873
if ((media = pwgMediaForPPD(page_size)) == NULL)
2874+
{
28542875
if ((media = pwgMediaForLegacy(page_size)) == NULL)
28552876
media = pwgMediaForPWG(page_size);
2877+
}
28562878

28572879
if (media)
28582880
{
2859-
pc->custom_size.width = media->width;
2860-
pc->custom_size.length = media->length;
2881+
pc->custom_size.map.ppd = (char *)page_size;
2882+
pc->custom_size.map.pwg = (char *)media->pwg;
2883+
pc->custom_size.width = media->width;
2884+
pc->custom_size.length = media->length;
28612885

28622886
return (&(pc->custom_size));
28632887
}

cups/ppd-private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ extern const char *_ppdCacheGetInputSlot(_ppd_cache_t *pc, ipp_t *job, const cha
158158
extern const char *_ppdCacheGetMediaType(_ppd_cache_t *pc, ipp_t *job, const char *keyword) _CUPS_PRIVATE;
159159
extern const char *_ppdCacheGetOutputBin(_ppd_cache_t *pc, const char *keyword) _CUPS_PRIVATE;
160160
extern const char *_ppdCacheGetPageSize(_ppd_cache_t *pc, ipp_t *job, const char *keyword, int *exact) _CUPS_PRIVATE;
161-
extern pwg_size_t *_ppdCacheGetSize(_ppd_cache_t *pc, const char *page_size) _CUPS_PRIVATE;
161+
extern pwg_size_t *_ppdCacheGetSize(_ppd_cache_t *pc, const char *page_size, ppd_size_t *ppd_size) _CUPS_PRIVATE;
162162
extern const char *_ppdCacheGetSource(_ppd_cache_t *pc, const char *input_slot) _CUPS_PRIVATE;
163163
extern const char *_ppdCacheGetType(_ppd_cache_t *pc, const char *media_type) _CUPS_PRIVATE;
164164
extern int _ppdCacheWriteFile(_ppd_cache_t *pc, const char *filename, ipp_t *attrs) _CUPS_PRIVATE;

cups/testppd.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
/*
22
* PPD test program for CUPS.
33
*
4-
* Copyright © 2020-2024 by OpenPrinting.
4+
* Copyright © 2020-2025 by OpenPrinting.
55
* Copyright © 2007-2018 by Apple Inc.
66
* Copyright © 1997-2006 by Easy Software Products.
77
*
88
* Licensed under Apache License v2.0. See the file "LICENSE" for more
99
* information.
1010
*/
1111

12-
/*
13-
* Include necessary headers...
14-
*/
15-
1612
#undef _CUPS_NO_DEPRECATED
1713
#include "cups-private.h"
1814
#include "ppd-private.h"
@@ -1208,6 +1204,7 @@ main(int argc, /* I - Number of command-line arguments */
12081204
ppd_option_t *option; /* Option */
12091205
ppd_coption_t *coption; /* Custom option */
12101206
ppd_cparam_t *cparam; /* Custom parameter */
1207+
ppd_size_t *size; /* Default paper size */
12111208
ppd_const_t *c; /* UIConstraints */
12121209
char lang[255], /* LANG environment variable */
12131210
lc_all[255], /* LC_ALL environment variable */
@@ -1359,12 +1356,23 @@ main(int argc, /* I - Number of command-line arguments */
13591356

13601357
puts("\nPPD Cache:");
13611358
if ((pc = _ppdCacheCreateWithPPD(NULL, ppd)) == NULL)
1359+
{
13621360
printf(" Unable to create: %s\n", cupsGetErrorString());
1361+
}
13631362
else
13641363
{
13651364
_ppdCacheWriteFile(pc, "t.cache", NULL);
13661365
puts(" Wrote t.cache.");
13671366
}
1367+
1368+
if ((size = ppdPageSize(ppd, NULL)) != NULL)
1369+
{
1370+
pwg_size_t *pwg; /* PWG media size */
1371+
1372+
pwg = _ppdCacheGetSize(pc, size->name, size);
1373+
1374+
printf(" media-default: %s\n", pwg ? pwg->map.pwg : "unknown");
1375+
}
13681376
}
13691377

13701378
if (!strncmp(argv[1], "-d", 2))

filter/rastertopwg.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
/*
22
* CUPS raster to PWG raster format filter for CUPS.
33
*
4-
* Copyright © 2020-2024 by OpenPrinting.
4+
* Copyright © 2020-2025 by OpenPrinting.
55
* Copyright © 2011, 2014-2017 Apple Inc.
66
*
77
* Licensed under Apache License v2.0. See the file "LICENSE" for more
88
* information.
99
*/
1010

11-
/*
12-
* Include necessary headers...
13-
*/
14-
1511
#include <cups/cups-private.h>
1612
#include <cups/ppd-private.h>
1713
#include <cups/raster.h>
@@ -278,7 +274,7 @@ main(int argc, /* I - Number of command-line args */
278274
}
279275
}
280276

281-
if (inheader.cupsPageSizeName[0] && (pwg_size = _ppdCacheGetSize(cache, inheader.cupsPageSizeName)) != NULL && pwg_size->map.pwg)
277+
if (inheader.cupsPageSizeName[0] && (pwg_size = _ppdCacheGetSize(cache, inheader.cupsPageSizeName, /*ppd_size*/NULL)) != NULL && pwg_size->map.pwg)
282278
{
283279
cupsCopyString(outheader.cupsPageSizeName, pwg_size->map.pwg,
284280
sizeof(outheader.cupsPageSizeName));

scheduler/printers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4297,7 +4297,7 @@ load_ppd(cupsd_printer_t *p) /* I - Printer */
42974297
*/
42984298

42994299
if ((size = ppdPageSize(ppd, NULL)) != NULL)
4300-
pwgsize = _ppdCacheGetSize(p->pc, size->name);
4300+
pwgsize = _ppdCacheGetSize(p->pc, size->name, size);
43014301
else
43024302
pwgsize = NULL;
43034303

0 commit comments

Comments
 (0)