2 * PPD cache implementation for CUPS.
4 * Copyright © 2022-2025 by OpenPrinting.
5 * Copyright © 2010-2021 by Apple Inc.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
11 #include "cups-private.h"
12 #include "ppd-private.h"
13 #include "debug-internal.h"
19 * Macro to test for two almost-equal PWG measurements.
22 #define _PWG_EQUIVALENT(x, y) (abs((x)-(y)) < 2)
29 static int cups_connect(http_t
**http
, const char *url
, char *resource
, size_t ressize
);
30 static cups_lang_t
*cups_get_strings(http_t
**http
, const char *printer_uri
, const char *language
);
31 static int cups_get_url(http_t
**http
, const char *url
, const char *suffix
, char *name
, size_t namesize
);
32 static const char *ppd_get_string(cups_lang_t
*base
, cups_lang_t
*printer
, const char *msgid
, char *buffer
, size_t bufsize
);
33 static void ppd_get_strings(ppd_file_t
*ppd
, cups_lang_t
*langs
, const char *option
, ppd_choice_t
*choice
, const char *pwg_msgid
);
34 static const char *ppd_inputslot_for_keyword(_ppd_cache_t
*pc
, const char *keyword
);
35 static void ppd_put_strings(cups_file_t
*fp
, cups_lang_t
*langs
, const char *ppd_option
, const char *ppd_choice
, const char *pwg_msgid
);
36 static void pwg_add_finishing(cups_array_t
*finishings
, ipp_finishings_t
template, const char *name
, const char *value
);
37 static int pwg_compare_finishings(_pwg_finishings_t
*a
, _pwg_finishings_t
*b
, void *data
);
38 static int pwg_compare_sizes(cups_size_t
*a
, cups_size_t
*b
, void *data
);
39 static cups_size_t
*pwg_copy_size(cups_size_t
*size
, void *data
);
40 static void pwg_free_finishings(_pwg_finishings_t
*f
, void *data
);
41 static void pwg_ppdize_name(const char *ipp
, char *name
, size_t namesize
);
42 static void pwg_ppdize_resolution(ipp_attribute_t
*attr
, int element
, int *xres
, int *yres
, char *name
, size_t namesize
);
43 static void pwg_unppdize_name(const char *ppd
, char *name
, size_t namesize
,
44 const char *dashchars
);
48 * '_cupsConvertOptions()' - Convert printer options to standard IPP attributes.
50 * This functions converts PPD and CUPS-specific options to their standard IPP
51 * attributes and values and adds them to the specified IPP request.
54 int /* O - New number of copies */
56 ipp_t
*request
, /* I - IPP request */
57 ppd_file_t
*ppd
, /* I - PPD file */
58 _ppd_cache_t
*pc
, /* I - PPD cache info */
59 ipp_attribute_t
*media_col_sup
, /* I - media-col-supported values */
60 ipp_attribute_t
*doc_handling_sup
, /* I - multiple-document-handling-supported values */
61 ipp_attribute_t
*print_color_mode_sup
,
62 /* I - Printer supports print-color-mode */
63 const char *user
, /* I - User info */
64 const char *format
, /* I - document-format value */
65 int copies
, /* I - Number of copies */
66 int num_options
, /* I - Number of options */
67 cups_option_t
*options
) /* I - Options */
69 int i
; /* Looping var */
70 const char *keyword
, /* PWG keyword */
71 *password
; /* Password string */
72 pwg_size_t
*size
; /* PWG media size */
73 ipp_t
*media_col
, /* media-col value */
74 *media_size
; /* media-size value */
75 const char *media_source
, /* media-source value */
76 *media_type
, /* media-type value */
77 *collate_str
, /* multiple-document-handling value */
78 *color_attr_name
, /* Supported color attribute */
79 *mandatory
, /* Mandatory attributes */
80 *finishing_template
; /* Finishing template */
81 int num_finishings
= 0, /* Number of finishing values */
82 finishings
[10]; /* Finishing enum values */
83 ppd_choice_t
*choice
; /* Marked choice */
84 int finishings_copies
= copies
,
85 /* Number of copies for finishings */
86 job_pages
= 0, /* job-pages value */
87 number_up
= 1; /* number-up value */
88 const char *value
; /* Option value */
92 * Send standard IPP attributes...
95 if (pc
->password
&& (password
= cupsGetOption("job-password", num_options
, options
)) != NULL
&& ippGetOperation(request
) != IPP_OP_VALIDATE_JOB
)
97 ipp_attribute_t
*attr
= NULL
; /* job-password attribute */
99 if ((keyword
= cupsGetOption("job-password-encryption", num_options
, options
)) == NULL
)
102 if (!strcmp(keyword
, "none"))
105 * Add plain-text job-password...
108 attr
= ippAddOctetString(request
, IPP_TAG_OPERATION
, "job-password", password
, (int)strlen(password
));
113 * Add hashed job-password...
116 unsigned char hash
[64]; /* Hash of password */
117 ssize_t hashlen
; /* Length of hash */
119 if ((hashlen
= cupsHashData(keyword
, password
, strlen(password
), hash
, sizeof(hash
))) > 0)
120 attr
= ippAddOctetString(request
, IPP_TAG_OPERATION
, "job-password", hash
, (int)hashlen
);
124 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "job-password-encryption", NULL
, keyword
);
129 if ((keyword
= cupsGetOption("job-account-id", num_options
, options
)) == NULL
)
130 keyword
= cupsGetOption("job-billing", num_options
, options
);
133 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-account-id", NULL
, keyword
);
136 if (pc
->accounting_user_id
)
138 if ((keyword
= cupsGetOption("job-accounting-user-id", num_options
, options
)) == NULL
)
142 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_NAME
, "job-accounting-user-id", NULL
, keyword
);
145 for (mandatory
= (const char *)cupsArrayFirst(pc
->mandatory
); mandatory
; mandatory
= (const char *)cupsArrayNext(pc
->mandatory
))
147 if (strcmp(mandatory
, "copies") &&
148 strcmp(mandatory
, "destination-uris") &&
149 strcmp(mandatory
, "finishings") &&
150 strcmp(mandatory
, "finishings-col") &&
151 strcmp(mandatory
, "finishing-template") &&
152 strcmp(mandatory
, "job-account-id") &&
153 strcmp(mandatory
, "job-accounting-user-id") &&
154 strcmp(mandatory
, "job-password") &&
155 strcmp(mandatory
, "job-password-encryption") &&
156 strcmp(mandatory
, "media") &&
157 strncmp(mandatory
, "media-col", 9) &&
158 strcmp(mandatory
, "multiple-document-handling") &&
159 strcmp(mandatory
, "output-bin") &&
160 strcmp(mandatory
, "print-color-mode") &&
161 strcmp(mandatory
, "print-quality") &&
162 strcmp(mandatory
, "sides") &&
163 (keyword
= cupsGetOption(mandatory
, num_options
, options
)) != NULL
)
165 _ipp_option_t
*opt
= _ippFindOption(mandatory
);
167 ipp_tag_t value_tag
= opt
? opt
->value_tag
: IPP_TAG_NAME
;
172 case IPP_TAG_INTEGER
:
174 ippAddInteger(request
, IPP_TAG_JOB
, value_tag
, mandatory
, atoi(keyword
));
176 case IPP_TAG_BOOLEAN
:
177 ippAddBoolean(request
, IPP_TAG_JOB
, mandatory
, !_cups_strcasecmp(keyword
, "true"));
181 int lower
, upper
; /* Range */
183 if (sscanf(keyword
, "%d-%d", &lower
, &upper
) != 2)
184 lower
= upper
= atoi(keyword
);
186 ippAddRange(request
, IPP_TAG_JOB
, mandatory
, lower
, upper
);
189 case IPP_TAG_STRING
:
190 ippAddOctetString(request
, IPP_TAG_JOB
, mandatory
, keyword
, (int)strlen(keyword
));
193 if (!strcmp(mandatory
, "print-color-mode") && !strcmp(keyword
, "monochrome"))
195 if (ippContainsString(print_color_mode_sup
, "auto-monochrome"))
196 keyword
= "auto-monochrome";
197 else if (ippContainsString(print_color_mode_sup
, "process-monochrome") && !ippContainsString(print_color_mode_sup
, "monochrome"))
198 keyword
= "process-monochrome";
201 ippAddString(request
, IPP_TAG_JOB
, value_tag
, mandatory
, NULL
, keyword
);
207 if ((keyword
= cupsGetOption("PageSize", num_options
, options
)) == NULL
)
208 keyword
= cupsGetOption("media", num_options
, options
);
210 media_source
= _ppdCacheGetSource(pc
, cupsGetOption("InputSlot", num_options
, options
));
211 media_type
= _ppdCacheGetType(pc
, cupsGetOption("MediaType", num_options
, options
));
212 size
= _ppdCacheGetSize(pc
, keyword
, /*ppd_size*/NULL
);
214 if (media_col_sup
&& (size
|| media_source
|| media_type
))
217 * Add a media-col value...
220 media_col
= ippNew();
224 media_size
= ippNew();
225 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
226 "x-dimension", size
->width
);
227 ippAddInteger(media_size
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
,
228 "y-dimension", size
->length
);
230 ippAddCollection(media_col
, IPP_TAG_ZERO
, "media-size", media_size
);
231 ippDelete(media_size
);
234 for (i
= 0; i
< media_col_sup
->num_values
; i
++)
236 if (size
&& !strcmp(media_col_sup
->values
[i
].string
.text
, "media-left-margin"))
237 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
, "media-left-margin", size
->left
);
238 else if (size
&& !strcmp(media_col_sup
->values
[i
].string
.text
, "media-bottom-margin"))
239 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
, "media-bottom-margin", size
->bottom
);
240 else if (size
&& !strcmp(media_col_sup
->values
[i
].string
.text
, "media-right-margin"))
241 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
, "media-right-margin", size
->right
);
242 else if (size
&& !strcmp(media_col_sup
->values
[i
].string
.text
, "media-top-margin"))
243 ippAddInteger(media_col
, IPP_TAG_ZERO
, IPP_TAG_INTEGER
, "media-top-margin", size
->top
);
244 else if (media_source
&& !strcmp(media_col_sup
->values
[i
].string
.text
, "media-source"))
245 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
, "media-source", NULL
, media_source
);
246 else if (media_type
&& !strcmp(media_col_sup
->values
[i
].string
.text
, "media-type"))
247 ippAddString(media_col
, IPP_TAG_ZERO
, IPP_TAG_KEYWORD
, "media-type", NULL
, media_type
);
250 ippAddCollection(request
, IPP_TAG_JOB
, "media-col", media_col
);
251 ippDelete(media_col
);
254 if ((keyword
= cupsGetOption("output-bin", num_options
, options
)) == NULL
)
256 if ((choice
= ppdFindMarkedChoice(ppd
, "OutputBin")) != NULL
)
257 keyword
= _ppdCacheGetBin(pc
, choice
->choice
);
261 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "output-bin", NULL
, keyword
);
263 color_attr_name
= print_color_mode_sup
? "print-color-mode" : "output-mode";
266 * If we use PPD with standardized PPD option for color support - ColorModel,
267 * prefer it to don't break color/grayscale support for PPDs, either classic
268 * or the ones generated from IPP Get-Printer-Attributes response.
271 if ((keyword
= cupsGetOption("ColorModel", num_options
, options
)) == NULL
)
274 * No ColorModel in options...
277 if ((choice
= ppdFindMarkedChoice(ppd
, "ColorModel")) != NULL
)
280 * ColorModel is taken from PPD as its default option.
283 if (!strcmp(choice
->choice
, "Gray") || !strcmp(choice
->choice
, "FastGray") || !strcmp(choice
->choice
, "DeviceGray"))
284 keyword
= "monochrome";
290 * print-color-mode is a default option since 2.4.1, use it as a fallback if there is no
291 * ColorModel in options or PPD...
293 keyword
= cupsGetOption("print-color-mode", num_options
, options
);
298 * ColorModel found in options...
301 if (!strcmp(keyword
, "Gray") || !strcmp(keyword
, "FastGray") || !strcmp(keyword
, "DeviceGray"))
302 keyword
= "monochrome";
307 if (keyword
&& !strcmp(keyword
, "monochrome"))
309 if (ippContainsString(print_color_mode_sup
, "auto-monochrome"))
310 keyword
= "auto-monochrome";
311 else if (ippContainsString(print_color_mode_sup
, "process-monochrome") && !ippContainsString(print_color_mode_sup
, "monochrome"))
312 keyword
= "process-monochrome";
316 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, color_attr_name
, NULL
, keyword
);
318 if ((keyword
= cupsGetOption("print-quality", num_options
, options
)) != NULL
)
319 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality", atoi(keyword
));
320 else if ((choice
= ppdFindMarkedChoice(ppd
, "cupsPrintQuality")) != NULL
)
322 if (!_cups_strcasecmp(choice
->choice
, "draft"))
323 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality", IPP_QUALITY_DRAFT
);
324 else if (!_cups_strcasecmp(choice
->choice
, "normal"))
325 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality", IPP_QUALITY_NORMAL
);
326 else if (!_cups_strcasecmp(choice
->choice
, "high"))
327 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "print-quality", IPP_QUALITY_HIGH
);
330 if ((keyword
= cupsGetOption("sides", num_options
, options
)) != NULL
)
331 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides", NULL
, keyword
);
332 else if (pc
->sides_option
&& (choice
= ppdFindMarkedChoice(ppd
, pc
->sides_option
)) != NULL
)
334 if (pc
->sides_1sided
&& !_cups_strcasecmp(choice
->choice
, pc
->sides_1sided
))
335 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides", NULL
, "one-sided");
336 else if (pc
->sides_2sided_long
&& !_cups_strcasecmp(choice
->choice
, pc
->sides_2sided_long
))
337 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides", NULL
, "two-sided-long-edge");
338 else if (pc
->sides_2sided_short
&& !_cups_strcasecmp(choice
->choice
, pc
->sides_2sided_short
))
339 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "sides", NULL
, "two-sided-short-edge");
346 if ((keyword
= cupsGetOption("multiple-document-handling", num_options
, options
)) != NULL
)
348 if (strstr(keyword
, "uncollated"))
353 else if ((keyword
= cupsGetOption("collate", num_options
, options
)) == NULL
)
358 if (!_cups_strcasecmp(format
, "image/gif") ||
359 !_cups_strcasecmp(format
, "image/jp2") ||
360 !_cups_strcasecmp(format
, "image/jpeg") ||
361 !_cups_strcasecmp(format
, "image/png") ||
362 !_cups_strcasecmp(format
, "image/tiff") ||
363 !_cups_strncasecmp(format
, "image/x-", 8))
366 * Collation makes no sense for single page image formats...
371 else if (!_cups_strncasecmp(format
, "image/", 6) ||
372 !_cups_strcasecmp(format
, "application/vnd.cups-raster"))
375 * Multi-page image formats will have copies applied by the upstream
383 if (doc_handling_sup
)
385 if (!_cups_strcasecmp(keyword
, "true"))
386 collate_str
= "separate-documents-collated-copies";
388 collate_str
= "separate-documents-uncollated-copies";
390 for (i
= 0; i
< doc_handling_sup
->num_values
; i
++)
392 if (!strcmp(doc_handling_sup
->values
[i
].string
.text
, collate_str
))
394 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "multiple-document-handling", NULL
, collate_str
);
399 if (i
>= doc_handling_sup
->num_values
)
404 * Map finishing options...
407 if (copies
!= finishings_copies
)
409 // Figure out the proper job-pages-per-set value...
410 if ((value
= cupsGetOption("job-pages", num_options
, options
)) == NULL
)
411 value
= cupsGetOption("com.apple.print.PrintSettings.PMTotalBeginPages..n.", num_options
, options
);
415 if ((job_pages
= atoi(value
)) < 1)
419 // Adjust for number-up
420 if ((value
= cupsGetOption("number-up", num_options
, options
)) != NULL
)
422 if ((number_up
= atoi(value
)) < 1)
426 job_pages
= (job_pages
+ number_up
- 1) / number_up
;
428 // When duplex printing, raster data will include an extra (blank) page to
429 // make the total number of pages even. Make sure this is reflected in the
431 if ((job_pages
& 1) && (keyword
= cupsGetOption("sides", num_options
, options
)) != NULL
&& strcmp(keyword
, "one-sided"))
435 if ((finishing_template
= cupsGetOption("cupsFinishingTemplate", num_options
, options
)) == NULL
)
436 finishing_template
= cupsGetOption("finishing-template", num_options
, options
);
438 if (finishing_template
&& strcmp(finishing_template
, "none"))
440 ipp_t
*fin_col
= ippNew(); /* finishings-col value */
442 ippAddString(fin_col
, IPP_TAG_JOB
, IPP_TAG_KEYWORD
, "finishing-template", NULL
, finishing_template
);
443 ippAddCollection(request
, IPP_TAG_JOB
, "finishings-col", fin_col
);
446 if (copies
!= finishings_copies
&& job_pages
> 0)
449 * Send job-pages-per-set attribute to apply finishings correctly...
452 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-pages-per-set", job_pages
);
457 num_finishings
= _ppdCacheGetFinishingValues(ppd
, pc
, (int)(sizeof(finishings
) / sizeof(finishings
[0])), finishings
);
458 if (num_finishings
> 0)
460 ippAddIntegers(request
, IPP_TAG_JOB
, IPP_TAG_ENUM
, "finishings", num_finishings
, finishings
);
462 if (copies
!= finishings_copies
&& job_pages
> 0)
465 * Send job-pages-per-set attribute to apply finishings correctly...
468 ippAddInteger(request
, IPP_TAG_JOB
, IPP_TAG_INTEGER
, "job-pages-per-set", job_pages
);
478 * '_ppdCacheCreateWithFile()' - Create PPD cache and mapping data from a
481 * Use the @link _ppdCacheWriteFile@ function to write PWG mapping data to a
485 _ppd_cache_t
* /* O - PPD cache and mapping data */
486 _ppdCacheCreateWithFile(
487 const char *filename
, /* I - File to read */
488 ipp_t
**attrs
) /* IO - IPP attributes, if any */
490 cups_file_t
*fp
; /* File */
491 _ppd_cache_t
*pc
; /* PWG mapping data */
492 pwg_size_t
*size
; /* Current size */
493 pwg_map_t
*map
; /* Current map */
494 _pwg_finishings_t
*finishings
; /* Current finishings option */
495 int linenum
, /* Current line number */
496 num_bins
, /* Number of bins in file */
497 num_sizes
, /* Number of sizes in file */
498 num_sources
, /* Number of sources in file */
499 num_types
; /* Number of types in file */
500 char line
[2048], /* Current line */
501 *value
, /* Pointer to value in line */
502 *valueptr
, /* Pointer into value */
503 pwg_keyword
[128], /* PWG keyword */
504 ppd_keyword
[PPD_MAX_NAME
];
506 _pwg_print_color_mode_t print_color_mode
;
507 /* Print color mode for preset */
508 _pwg_print_quality_t print_quality
; /* Print quality for preset */
511 DEBUG_printf("_ppdCacheCreateWithFile(filename=\"%s\")", filename
);
514 * Range check input...
522 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
530 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
532 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
537 * Read the first line and make sure it has "#CUPS-PPD-CACHE-version" in it...
540 if (!cupsFileGets(fp
, line
, sizeof(line
)))
542 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
543 DEBUG_puts("_ppdCacheCreateWithFile: Unable to read first line.");
548 if (strncmp(line
, "#CUPS-PPD-CACHE-", 16))
550 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
551 DEBUG_printf("_ppdCacheCreateWithFile: Wrong first line \"%s\".", line
);
556 if (atoi(line
+ 16) != _PPD_CACHE_VERSION
)
558 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Out of date PPD cache file."), 1);
559 DEBUG_printf("_ppdCacheCreateWithFile: Cache file has version %s, expected %d.", line
+ 16, _PPD_CACHE_VERSION
);
565 * Allocate the mapping data structure...
568 if ((pc
= calloc(1, sizeof(_ppd_cache_t
))) == NULL
)
570 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
571 DEBUG_puts("_ppdCacheCreateWithFile: Unable to allocate _ppd_cache_t.");
575 pc
->max_copies
= 9999;
587 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
589 DEBUG_printf("_ppdCacheCreateWithFile: line=\"%s\", value=\"%s\", linenum=%d", line
, value
, linenum
);
593 DEBUG_printf("_ppdCacheCreateWithFile: Missing value on line %d.", linenum
);
594 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
597 else if (!_cups_strcasecmp(line
, "Filter"))
600 pc
->filters
= cupsArrayNew3(NULL
, NULL
, NULL
, 0, (cups_acopy_func_t
)_cupsArrayStrdup
, _cupsArrayFree
);
602 cupsArrayAdd(pc
->filters
, value
);
604 else if (!_cups_strcasecmp(line
, "PreFilter"))
607 pc
->prefilters
= cupsArrayNew3(NULL
, NULL
, NULL
, 0, (cups_acopy_func_t
)_cupsArrayStrdup
, _cupsArrayFree
);
609 cupsArrayAdd(pc
->prefilters
, value
);
611 else if (!_cups_strcasecmp(line
, "Product"))
613 pc
->product
= strdup(value
);
615 else if (!_cups_strcasecmp(line
, "SingleFile"))
617 pc
->single_file
= !_cups_strcasecmp(value
, "true");
619 else if (!_cups_strcasecmp(line
, "IPP"))
621 off_t pos
= cupsFileTell(fp
), /* Position in file */
622 length
= strtol(value
, NULL
, 10);
623 /* Length of IPP attributes */
627 DEBUG_puts("_ppdCacheCreateWithFile: IPP listed multiple times.");
628 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
631 else if (length
<= 0)
633 DEBUG_puts("_ppdCacheCreateWithFile: Bad IPP length.");
634 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
641 * Read IPP attributes into the provided variable...
646 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
,
647 *attrs
) != IPP_STATE_DATA
)
649 DEBUG_puts("_ppdCacheCreateWithFile: Bad IPP data.");
650 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
657 * Skip the IPP data entirely...
660 cupsFileSeek(fp
, pos
+ length
);
663 if (cupsFileTell(fp
) != (pos
+ length
))
665 DEBUG_puts("_ppdCacheCreateWithFile: Bad IPP data.");
666 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
670 else if (!_cups_strcasecmp(line
, "NumBins"))
674 DEBUG_puts("_ppdCacheCreateWithFile: NumBins listed multiple times.");
675 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
679 if ((num_bins
= atoi(value
)) <= 0 || num_bins
> 65536)
681 DEBUG_printf("_ppdCacheCreateWithFile: Bad NumBins value %d on line %d.", num_sizes
, linenum
);
682 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
686 if ((pc
->bins
= calloc((size_t)num_bins
, sizeof(pwg_map_t
))) == NULL
)
688 DEBUG_printf("_ppdCacheCreateWithFile: Unable to allocate %d bins.", num_sizes
);
689 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
693 else if (!_cups_strcasecmp(line
, "Bin"))
695 if (sscanf(value
, "%127s%40s", pwg_keyword
, ppd_keyword
) != 2)
697 DEBUG_printf("_ppdCacheCreateWithFile: Bad Bin on line %d.", linenum
);
698 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
702 if (pc
->num_bins
>= num_bins
)
704 DEBUG_printf("_ppdCacheCreateWithFile: Too many Bin's on line %d.", linenum
);
705 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
709 map
= pc
->bins
+ pc
->num_bins
;
710 map
->pwg
= strdup(pwg_keyword
);
711 map
->ppd
= strdup(ppd_keyword
);
715 else if (!_cups_strcasecmp(line
, "NumSizes"))
719 DEBUG_puts("_ppdCacheCreateWithFile: NumSizes listed multiple times.");
720 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
724 if ((num_sizes
= atoi(value
)) < 0 || num_sizes
> 65536)
726 DEBUG_printf("_ppdCacheCreateWithFile: Bad NumSizes value %d on line %d.", num_sizes
, linenum
);
727 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
733 if ((pc
->sizes
= calloc((size_t)num_sizes
, sizeof(pwg_size_t
))) == NULL
)
735 DEBUG_printf("_ppdCacheCreateWithFile: Unable to allocate %d sizes.", num_sizes
);
736 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
741 else if (!_cups_strcasecmp(line
, "Size"))
743 if (pc
->num_sizes
>= num_sizes
)
745 DEBUG_printf("_ppdCacheCreateWithFile: Too many Size's on line %d.", linenum
);
746 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
750 size
= pc
->sizes
+ pc
->num_sizes
;
752 if (sscanf(value
, "%127s%40s%d%d%d%d%d%d", pwg_keyword
, ppd_keyword
,
753 &(size
->width
), &(size
->length
), &(size
->left
),
754 &(size
->bottom
), &(size
->right
), &(size
->top
)) != 8)
756 DEBUG_printf("_ppdCacheCreateWithFile: Bad Size on line %d.", linenum
);
757 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
761 size
->map
.pwg
= strdup(pwg_keyword
);
762 size
->map
.ppd
= strdup(ppd_keyword
);
766 else if (!_cups_strcasecmp(line
, "CustomSize"))
768 if (pc
->custom_max_width
> 0)
770 DEBUG_printf("_ppdCacheCreateWithFile: Too many CustomSize's on line %d.", linenum
);
771 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
775 if (sscanf(value
, "%d%d%d%d%d%d%d%d", &(pc
->custom_max_width
),
776 &(pc
->custom_max_length
), &(pc
->custom_min_width
),
777 &(pc
->custom_min_length
), &(pc
->custom_size
.left
),
778 &(pc
->custom_size
.bottom
), &(pc
->custom_size
.right
),
779 &(pc
->custom_size
.top
)) != 8)
781 DEBUG_printf("_ppdCacheCreateWithFile: Bad CustomSize on line %d.", linenum
);
782 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
786 pwgFormatSizeName(pwg_keyword
, sizeof(pwg_keyword
), "custom", "max",
787 pc
->custom_max_width
, pc
->custom_max_length
, NULL
);
788 pc
->custom_max_keyword
= strdup(pwg_keyword
);
790 pwgFormatSizeName(pwg_keyword
, sizeof(pwg_keyword
), "custom", "min",
791 pc
->custom_min_width
, pc
->custom_min_length
, NULL
);
792 pc
->custom_min_keyword
= strdup(pwg_keyword
);
794 else if (!_cups_strcasecmp(line
, "SourceOption"))
796 pc
->source_option
= strdup(value
);
798 else if (!_cups_strcasecmp(line
, "NumSources"))
802 DEBUG_puts("_ppdCacheCreateWithFile: NumSources listed multiple "
804 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
808 if ((num_sources
= atoi(value
)) <= 0 || num_sources
> 65536)
810 DEBUG_printf("_ppdCacheCreateWithFile: Bad NumSources value %d on line %d.", num_sources
, linenum
);
811 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
815 if ((pc
->sources
= calloc((size_t)num_sources
, sizeof(pwg_map_t
))) == NULL
)
817 DEBUG_printf("_ppdCacheCreateWithFile: Unable to allocate %d sources.", num_sources
);
818 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
822 else if (!_cups_strcasecmp(line
, "Source"))
824 if (sscanf(value
, "%127s%40s", pwg_keyword
, ppd_keyword
) != 2)
826 DEBUG_printf("_ppdCacheCreateWithFile: Bad Source on line %d.", linenum
);
827 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
831 if (pc
->num_sources
>= num_sources
)
833 DEBUG_printf("_ppdCacheCreateWithFile: Too many Source's on line %d.", linenum
);
834 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
838 map
= pc
->sources
+ pc
->num_sources
;
839 map
->pwg
= strdup(pwg_keyword
);
840 map
->ppd
= strdup(ppd_keyword
);
844 else if (!_cups_strcasecmp(line
, "NumTypes"))
848 DEBUG_puts("_ppdCacheCreateWithFile: NumTypes listed multiple times.");
849 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
853 if ((num_types
= atoi(value
)) <= 0 || num_types
> 65536)
855 DEBUG_printf("_ppdCacheCreateWithFile: Bad NumTypes value %d on line %d.", num_types
, linenum
);
856 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
860 if ((pc
->types
= calloc((size_t)num_types
, sizeof(pwg_map_t
))) == NULL
)
862 DEBUG_printf("_ppdCacheCreateWithFile: Unable to allocate %d types.", num_types
);
863 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
867 else if (!_cups_strcasecmp(line
, "Type"))
869 if (sscanf(value
, "%127s%40s", pwg_keyword
, ppd_keyword
) != 2)
871 DEBUG_printf("_ppdCacheCreateWithFile: Bad Type on line %d.", linenum
);
872 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
876 if (pc
->num_types
>= num_types
)
878 DEBUG_printf("_ppdCacheCreateWithFile: Too many Type's on line %d.", linenum
);
879 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
883 map
= pc
->types
+ pc
->num_types
;
884 map
->pwg
= strdup(pwg_keyword
);
885 map
->ppd
= strdup(ppd_keyword
);
889 else if (!_cups_strcasecmp(line
, "Preset"))
892 * Preset output-mode print-quality name=value ...
895 print_color_mode
= (_pwg_print_color_mode_t
)strtol(value
, &valueptr
, 10);
896 print_quality
= (_pwg_print_quality_t
)strtol(valueptr
, &valueptr
, 10);
898 if (print_color_mode
< _PWG_PRINT_COLOR_MODE_MONOCHROME
||
899 print_color_mode
>= _PWG_PRINT_COLOR_MODE_MAX
||
900 print_quality
< _PWG_PRINT_QUALITY_DRAFT
||
901 print_quality
>= _PWG_PRINT_QUALITY_MAX
||
902 valueptr
== value
|| !*valueptr
)
904 DEBUG_printf("_ppdCacheCreateWithFile: Bad Preset on line %d.", linenum
);
905 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
909 pc
->num_presets
[print_color_mode
][print_quality
] =
910 cupsParseOptions(valueptr
, 0,
911 pc
->presets
[print_color_mode
] + print_quality
);
913 else if (!_cups_strcasecmp(line
, "SidesOption"))
914 pc
->sides_option
= strdup(value
);
915 else if (!_cups_strcasecmp(line
, "Sides1Sided"))
916 pc
->sides_1sided
= strdup(value
);
917 else if (!_cups_strcasecmp(line
, "Sides2SidedLong"))
918 pc
->sides_2sided_long
= strdup(value
);
919 else if (!_cups_strcasecmp(line
, "Sides2SidedShort"))
920 pc
->sides_2sided_short
= strdup(value
);
921 else if (!_cups_strcasecmp(line
, "Finishings"))
925 cupsArrayNew3((cups_array_func_t
)pwg_compare_finishings
,
927 (cups_afree_func_t
)pwg_free_finishings
);
929 if ((finishings
= calloc(1, sizeof(_pwg_finishings_t
))) == NULL
)
932 finishings
->value
= (ipp_finishings_t
)strtol(value
, &valueptr
, 10);
933 finishings
->num_options
= cupsParseOptions(valueptr
, 0,
934 &(finishings
->options
));
936 cupsArrayAdd(pc
->finishings
, finishings
);
938 else if (!_cups_strcasecmp(line
, "FinishingTemplate"))
941 pc
->templates
= cupsArrayNew3((cups_array_func_t
)_cupsArrayStrcmp
, NULL
, NULL
, 0, (cups_acopy_func_t
)_cupsArrayStrdup
, _cupsArrayFree
);
943 cupsArrayAdd(pc
->templates
, value
);
945 else if (!_cups_strcasecmp(line
, "MaxCopies"))
946 pc
->max_copies
= atoi(value
);
947 else if (!_cups_strcasecmp(line
, "ChargeInfoURI"))
948 pc
->charge_info_uri
= strdup(value
);
949 else if (!_cups_strcasecmp(line
, "JobAccountId"))
950 pc
->account_id
= !_cups_strcasecmp(value
, "true");
951 else if (!_cups_strcasecmp(line
, "JobAccountingUserId"))
952 pc
->accounting_user_id
= !_cups_strcasecmp(value
, "true");
953 else if (!_cups_strcasecmp(line
, "JobPassword"))
954 pc
->password
= strdup(value
);
955 else if (!_cups_strcasecmp(line
, "Mandatory"))
958 cupsArrayAddStrings(pc
->mandatory
, value
, ' ');
960 pc
->mandatory
= cupsArrayNewStrings(value
, ' ');
962 else if (!_cups_strcasecmp(line
, "SupportFile"))
964 if (!pc
->support_files
)
965 pc
->support_files
= cupsArrayNew3(NULL
, NULL
, NULL
, 0, (cups_acopy_func_t
)_cupsArrayStrdup
, _cupsArrayFree
);
967 cupsArrayAdd(pc
->support_files
, value
);
971 DEBUG_printf("_ppdCacheCreateWithFile: Unknown %s on line %d.", line
, linenum
);
975 if (pc
->num_sizes
< num_sizes
)
977 DEBUG_printf("_ppdCacheCreateWithFile: Not enough sizes (%d < %d).", pc
->num_sizes
, num_sizes
);
978 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
982 if (pc
->num_sources
< num_sources
)
984 DEBUG_printf("_ppdCacheCreateWithFile: Not enough sources (%d < %d).", pc
->num_sources
, num_sources
);
985 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
989 if (pc
->num_types
< num_types
)
991 DEBUG_printf("_ppdCacheCreateWithFile: Not enough types (%d < %d).", pc
->num_types
, num_types
);
992 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
1001 * If we get here the file was bad - free any data and return...
1007 _ppdCacheDestroy(pc
);
1020 * '_ppdCacheCreateWithPPD()' - Create PWG mapping data from a PPD file.
1023 _ppd_cache_t
* /* O - PPD cache and mapping data */
1024 _ppdCacheCreateWithPPD(
1025 cups_lang_t
*langs
, /* I - Languages to load */
1026 ppd_file_t
*ppd
) /* I - PPD file */
1028 int i
, j
, k
; /* Looping vars */
1029 _ppd_cache_t
*pc
; /* PWG mapping data */
1030 ppd_option_t
*input_slot
, /* InputSlot option */
1031 *media_type
, /* MediaType option */
1032 *output_bin
, /* OutputBin option */
1033 *color_model
, /* ColorModel option */
1034 *duplex
, /* Duplex option */
1035 *ppd_option
; /* Other PPD option */
1036 ppd_choice_t
*choice
; /* Current InputSlot/MediaType */
1037 pwg_map_t
*map
; /* Current source/type map */
1038 ppd_attr_t
*ppd_attr
; /* Current PPD preset attribute */
1039 int num_options
; /* Number of preset options and props */
1040 cups_option_t
*options
; /* Preset options and properties */
1041 ppd_size_t
*ppd_size
; /* Current PPD size */
1042 pwg_size_t
*pwg_size
; /* Current PWG size */
1043 char pwg_keyword
[3 + PPD_MAX_NAME
+ 1 + 12 + 1 + 12 + 3],
1044 /* PWG keyword string */
1045 ppd_name
[PPD_MAX_NAME
];
1046 /* Normalized PPD name */
1047 const char *pwg_name
; /* Standard PWG media name */
1048 pwg_media_t
*pwg_media
, /* PWG media data */
1049 pwg_mediatemp
; /* PWG media data buffer */
1050 _pwg_print_color_mode_t pwg_print_color_mode
;
1051 /* print-color-mode index */
1052 _pwg_print_quality_t pwg_print_quality
;
1053 /* print-quality index */
1054 int similar
; /* Are the old and new size similar? */
1055 pwg_size_t
*old_size
; /* Current old size */
1056 int old_imageable
, /* Old imageable length in 2540ths */
1057 old_borderless
, /* Old borderless state */
1058 old_known_pwg
; /* Old PWG name is well-known */
1059 int new_width
, /* New width in 2540ths */
1060 new_length
, /* New length in 2540ths */
1061 new_left
, /* New left margin in 2540ths */
1062 new_bottom
, /* New bottom margin in 2540ths */
1063 new_right
, /* New right margin in 2540ths */
1064 new_top
, /* New top margin in 2540ths */
1065 new_imageable
, /* New imageable length in 2540ths */
1066 new_borderless
, /* New borderless state */
1067 new_known_pwg
; /* New PWG name is well-known */
1068 pwg_size_t
*new_size
; /* New size to add, if any */
1069 const char *filter
; /* Current filter */
1070 _pwg_finishings_t
*finishings
; /* Current finishings value */
1071 char msg_id
[256]; /* Message identifier */
1072 cups_lang_t
*lang
; /* Current language */
1073 _cups_message_t msg
; /* Message */
1076 DEBUG_printf("_ppdCacheCreateWithPPD(ppd=%p)", (void *)ppd
);
1079 * Range check input...
1086 * Allocate memory...
1089 if ((pc
= calloc(1, sizeof(_ppd_cache_t
))) == NULL
)
1091 DEBUG_puts("_ppdCacheCreateWithPPD: Unable to allocate _ppd_cache_t.");
1096 * Copy and convert size data...
1099 if (ppd
->num_sizes
> 0)
1101 if ((pc
->sizes
= calloc((size_t)ppd
->num_sizes
, sizeof(pwg_size_t
))) == NULL
)
1103 DEBUG_printf("_ppdCacheCreateWithPPD: Unable to allocate %d pwg_size_t's.", ppd
->num_sizes
);
1107 for (i
= ppd
->num_sizes
, pwg_size
= pc
->sizes
, ppd_size
= ppd
->sizes
;
1112 * Don't copy over custom size...
1115 if (!_cups_strcasecmp(ppd_size
->name
, "Custom"))
1119 * Convert the PPD size name to the corresponding PWG keyword name.
1122 if ((pwg_media
= pwgMediaForSize(PWG_FROM_POINTS(ppd_size
->width
), PWG_FROM_POINTS(ppd_size
->length
))) != NULL
)
1125 * Standard name, do we have conflicts?
1128 for (j
= 0; j
< pc
->num_sizes
; j
++)
1129 if (!strcmp(pc
->sizes
[j
].map
.pwg
, pwg_media
->pwg
))
1139 * Standard name and no conflicts, use it!
1142 pwg_name
= pwg_media
->pwg
;
1148 * Not a standard name; convert it to a PWG vendor name of the form:
1150 * pp_lowerppd_WIDTHxHEIGHTuu
1153 pwg_name
= pwg_keyword
;
1156 pwg_unppdize_name(ppd_size
->name
, ppd_name
, sizeof(ppd_name
), "_.");
1157 pwgFormatSizeName(pwg_keyword
, sizeof(pwg_keyword
), NULL
, ppd_name
,
1158 PWG_FROM_POINTS(ppd_size
->width
),
1159 PWG_FROM_POINTS(ppd_size
->length
), NULL
);
1163 * If we have a similar paper with non-zero margins then we only want to
1164 * keep it if it has a larger imageable area length. The NULL check is for
1165 * dimensions that are <= 0...
1168 if ((pwg_media
= _pwgMediaNearSize(&pwg_mediatemp
, /*keyword*/NULL
, /*keysize*/0, /*ppdname*/NULL
, /*ppdsize*/0, PWG_FROM_POINTS(ppd_size
->width
), PWG_FROM_POINTS(ppd_size
->length
), /*epsilon*/0)) == NULL
)
1171 new_width
= pwg_media
->width
;
1172 new_length
= pwg_media
->length
;
1173 new_left
= PWG_FROM_POINTS(ppd_size
->left
);
1174 new_bottom
= PWG_FROM_POINTS(ppd_size
->bottom
);
1175 new_right
= PWG_FROM_POINTS(ppd_size
->width
- ppd_size
->right
);
1176 new_top
= PWG_FROM_POINTS(ppd_size
->length
- ppd_size
->top
);
1177 new_imageable
= new_length
- new_top
- new_bottom
;
1178 new_borderless
= new_bottom
== 0 && new_top
== 0 &&
1179 new_left
== 0 && new_right
== 0;
1181 for (k
= pc
->num_sizes
, similar
= 0, old_size
= pc
->sizes
, new_size
= NULL
;
1185 old_imageable
= old_size
->length
- old_size
->top
- old_size
->bottom
;
1186 old_borderless
= old_size
->left
== 0 && old_size
->bottom
== 0 &&
1187 old_size
->right
== 0 && old_size
->top
== 0;
1188 old_known_pwg
= strncmp(old_size
->map
.pwg
, "oe_", 3) &&
1189 strncmp(old_size
->map
.pwg
, "om_", 3);
1191 similar
= old_borderless
== new_borderless
&&
1192 _PWG_EQUIVALENT(old_size
->width
, new_width
) &&
1193 _PWG_EQUIVALENT(old_size
->length
, new_length
);
1196 (new_known_pwg
|| (!old_known_pwg
&& new_imageable
> old_imageable
)))
1199 * The new paper has a larger imageable area so it could replace
1200 * the older paper. Regardless of the imageable area, we always
1201 * prefer the size with a well-known PWG name.
1204 new_size
= old_size
;
1205 free(old_size
->map
.ppd
);
1206 free(old_size
->map
.pwg
);
1213 * The paper was unique enough to deserve its own entry so add it to the
1217 new_size
= pwg_size
++;
1227 new_size
->map
.ppd
= strdup(ppd_size
->name
);
1228 new_size
->map
.pwg
= strdup(pwg_name
);
1229 new_size
->width
= new_width
;
1230 new_size
->length
= new_length
;
1231 new_size
->left
= new_left
;
1232 new_size
->bottom
= new_bottom
;
1233 new_size
->right
= new_right
;
1234 new_size
->top
= new_top
;
1239 if (ppd
->variable_sizes
)
1242 * Generate custom size data...
1245 pwgFormatSizeName(pwg_keyword
, sizeof(pwg_keyword
), "custom", "max",
1246 PWG_FROM_POINTS(ppd
->custom_max
[0]),
1247 PWG_FROM_POINTS(ppd
->custom_max
[1]), NULL
);
1248 pc
->custom_max_keyword
= strdup(pwg_keyword
);
1249 pc
->custom_max_width
= PWG_FROM_POINTS(ppd
->custom_max
[0]);
1250 pc
->custom_max_length
= PWG_FROM_POINTS(ppd
->custom_max
[1]);
1252 pwgFormatSizeName(pwg_keyword
, sizeof(pwg_keyword
), "custom", "min",
1253 PWG_FROM_POINTS(ppd
->custom_min
[0]),
1254 PWG_FROM_POINTS(ppd
->custom_min
[1]), NULL
);
1255 pc
->custom_min_keyword
= strdup(pwg_keyword
);
1256 pc
->custom_min_width
= PWG_FROM_POINTS(ppd
->custom_min
[0]);
1257 pc
->custom_min_length
= PWG_FROM_POINTS(ppd
->custom_min
[1]);
1259 pc
->custom_size
.left
= PWG_FROM_POINTS(ppd
->custom_margins
[0]);
1260 pc
->custom_size
.bottom
= PWG_FROM_POINTS(ppd
->custom_margins
[1]);
1261 pc
->custom_size
.right
= PWG_FROM_POINTS(ppd
->custom_margins
[2]);
1262 pc
->custom_size
.top
= PWG_FROM_POINTS(ppd
->custom_margins
[3]);
1266 * Copy and convert InputSlot data...
1269 if ((input_slot
= ppdFindOption(ppd
, "InputSlot")) == NULL
)
1270 input_slot
= ppdFindOption(ppd
, "HPPaperSource");
1274 pc
->source_option
= strdup(input_slot
->keyword
);
1276 if ((pc
->sources
= calloc((size_t)input_slot
->num_choices
, sizeof(pwg_map_t
))) == NULL
)
1278 DEBUG_printf("_ppdCacheCreateWithPPD: Unable to allocate %d pwg_map_t's for InputSlot.", input_slot
->num_choices
);
1282 pc
->num_sources
= input_slot
->num_choices
;
1284 for (i
= input_slot
->num_choices
, choice
= input_slot
->choices
,
1287 i
--, choice
++, map
++)
1289 if (!_cups_strncasecmp(choice
->choice
, "Auto", 4) ||
1290 !_cups_strncasecmp(choice
->text
, "Auto", 4) ||
1291 !_cups_strcasecmp(choice
->choice
, "Default") ||
1292 !_cups_strcasecmp(choice
->text
, "Default"))
1294 else if (!_cups_strcasecmp(choice
->choice
, "Cassette"))
1296 else if (!_cups_strcasecmp(choice
->choice
, "PhotoTray"))
1298 else if (!_cups_strcasecmp(choice
->choice
, "CDTray"))
1300 else if (!_cups_strncasecmp(choice
->choice
, "Multipurpose", 12) ||
1301 !_cups_strcasecmp(choice
->choice
, "MP") ||
1302 !_cups_strcasecmp(choice
->choice
, "MPTray"))
1303 pwg_name
= "by-pass-tray";
1304 else if (!_cups_strcasecmp(choice
->choice
, "LargeCapacity"))
1305 pwg_name
= "large-capacity";
1306 else if (!_cups_strncasecmp(choice
->choice
, "Lower", 5))
1307 pwg_name
= "bottom";
1308 else if (!_cups_strncasecmp(choice
->choice
, "Middle", 6))
1309 pwg_name
= "middle";
1310 else if (!_cups_strncasecmp(choice
->choice
, "Upper", 5))
1312 else if (!_cups_strncasecmp(choice
->choice
, "Side", 4))
1314 else if (!_cups_strcasecmp(choice
->choice
, "Roll"))
1315 pwg_name
= "main-roll";
1316 else if (!_cups_strcasecmp(choice
->choice
, "0"))
1317 pwg_name
= "tray-1";
1318 else if (!_cups_strcasecmp(choice
->choice
, "1"))
1319 pwg_name
= "tray-2";
1320 else if (!_cups_strcasecmp(choice
->choice
, "2"))
1321 pwg_name
= "tray-3";
1322 else if (!_cups_strcasecmp(choice
->choice
, "3"))
1323 pwg_name
= "tray-4";
1324 else if (!_cups_strcasecmp(choice
->choice
, "4"))
1325 pwg_name
= "tray-5";
1326 else if (!_cups_strcasecmp(choice
->choice
, "5"))
1327 pwg_name
= "tray-6";
1328 else if (!_cups_strcasecmp(choice
->choice
, "6"))
1329 pwg_name
= "tray-7";
1330 else if (!_cups_strcasecmp(choice
->choice
, "7"))
1331 pwg_name
= "tray-8";
1332 else if (!_cups_strcasecmp(choice
->choice
, "8"))
1333 pwg_name
= "tray-9";
1334 else if (!_cups_strcasecmp(choice
->choice
, "9"))
1335 pwg_name
= "tray-10";
1339 * Convert PPD name to lowercase...
1342 pwg_name
= pwg_keyword
;
1343 pwg_unppdize_name(choice
->choice
, pwg_keyword
, sizeof(pwg_keyword
),
1347 map
->pwg
= strdup(pwg_name
);
1348 map
->ppd
= strdup(choice
->choice
);
1351 * Add localized text for PWG keyword to message catalog...
1354 snprintf(msg_id
, sizeof(msg_id
), "media-source.%s", pwg_name
);
1355 ppd_get_strings(ppd
, langs
, "InputSlot", choice
, msg_id
);
1360 * Copy and convert MediaType data...
1363 if ((media_type
= ppdFindOption(ppd
, "MediaType")) != NULL
)
1367 const char *ppd_name
; /* PPD MediaType name or prefix to match */
1368 int match_length
; /* Length of prefix, or -1 to match entire string */
1369 const char *pwg_name
; /* Registered PWG media-type name to use */
1370 } standard_types
[] = {
1371 {"Auto", 4, "auto"},
1372 {"Any", -1, "auto"},
1373 {"Default", -1, "auto"},
1374 {"Card", 4, "cardstock"},
1375 {"Env", 3, "envelope"},
1376 {"Gloss", 5, "photographic-glossy"},
1377 {"HighGloss", -1, "photographic-high-gloss"},
1378 {"Matte", -1, "photographic-matte"},
1379 {"Plain", 5, "stationery"},
1380 {"Coated", 6, "stationery-coated"},
1381 {"Inkjet", -1, "stationery-inkjet"},
1382 {"Letterhead", -1, "stationery-letterhead"},
1383 {"Preprint", 8, "stationery-preprinted"},
1384 {"Recycled", -1, "stationery-recycled"},
1385 {"Transparen", 10, "transparency"},
1387 const int num_standard_types
= (int)(sizeof(standard_types
) / sizeof(standard_types
[0]));
1388 /* Length of the standard_types array */
1389 int match_counts
[sizeof(standard_types
) / sizeof(standard_types
[0])] = {0};
1390 /* Number of matches for each standard type */
1392 if ((pc
->types
= calloc((size_t)media_type
->num_choices
, sizeof(pwg_map_t
))) == NULL
)
1394 DEBUG_printf("_ppdCacheCreateWithPPD: Unable to allocate %d pwg_map_t's for MediaType.", media_type
->num_choices
);
1398 pc
->num_types
= media_type
->num_choices
;
1400 for (i
= media_type
->num_choices
, choice
= media_type
->choices
,
1403 i
--, choice
++, map
++)
1407 for (j
= 0; j
< num_standard_types
; j
++)
1409 if (standard_types
[j
].match_length
<= 0)
1411 if (!_cups_strcasecmp(choice
->choice
, standard_types
[j
].ppd_name
))
1413 pwg_name
= standard_types
[j
].pwg_name
;
1417 else if (!_cups_strncasecmp(choice
->choice
, standard_types
[j
].ppd_name
, (size_t)standard_types
[j
].match_length
))
1419 pwg_name
= standard_types
[j
].pwg_name
;
1427 * Convert PPD name to lowercase...
1430 pwg_name
= pwg_keyword
;
1431 pwg_unppdize_name(choice
->choice
, pwg_keyword
, sizeof(pwg_keyword
),
1435 map
->pwg
= strdup(pwg_name
);
1436 map
->ppd
= strdup(choice
->choice
);
1440 * Since three PPD name patterns can map to "auto", their match counts
1441 * should each be the count of all three combined.
1444 i
= match_counts
[0] + match_counts
[1] + match_counts
[2];
1445 match_counts
[0] = match_counts
[1] = match_counts
[2] = i
;
1447 for (i
= 0, choice
= media_type
->choices
, map
= pc
->types
;
1448 i
< media_type
->num_choices
;
1449 i
++, choice
++, map
++)
1452 * If there are two matches for any standard PWG media type, don't give
1453 * the PWG name to either one.
1456 for (j
= 0; j
< num_standard_types
; j
++)
1458 if (match_counts
[j
] > 1 && !strcmp(map
->pwg
, standard_types
[j
].pwg_name
))
1461 pwg_unppdize_name(choice
->choice
, pwg_keyword
, sizeof(pwg_keyword
), "_");
1462 map
->pwg
= strdup(pwg_keyword
);
1467 * Add localized text for PWG keyword to message catalog...
1470 snprintf(msg_id
, sizeof(msg_id
), "media-type.%s", map
->pwg
);
1471 ppd_get_strings(ppd
, langs
, "MediaType", choice
, msg_id
);
1476 * Copy and convert OutputBin data...
1479 if ((output_bin
= ppdFindOption(ppd
, "OutputBin")) != NULL
)
1481 if ((pc
->bins
= calloc((size_t)output_bin
->num_choices
, sizeof(pwg_map_t
))) == NULL
)
1483 DEBUG_printf("_ppdCacheCreateWithPPD: Unable to allocate %d pwg_map_t's for OutputBin.", output_bin
->num_choices
);
1487 pc
->num_bins
= output_bin
->num_choices
;
1489 for (i
= output_bin
->num_choices
, choice
= output_bin
->choices
,
1492 i
--, choice
++, map
++)
1494 pwg_unppdize_name(choice
->choice
, pwg_keyword
, sizeof(pwg_keyword
), "_");
1496 map
->pwg
= strdup(pwg_keyword
);
1497 map
->ppd
= strdup(choice
->choice
);
1500 * Add localized text for PWG keyword to message catalog...
1503 snprintf(msg_id
, sizeof(msg_id
), "output-bin.%s", pwg_keyword
);
1504 ppd_get_strings(ppd
, langs
, "OutputBin", choice
, msg_id
);
1508 if ((ppd_attr
= ppdFindAttr(ppd
, "APPrinterPreset", NULL
)) != NULL
)
1511 * Copy and convert APPrinterPreset (output-mode + print-quality) data...
1514 const char *quality
, /* com.apple.print.preset.quality value */
1515 *output_mode
, /* com.apple.print.preset.output-mode value */
1516 *color_model_val
, /* ColorModel choice */
1517 *graphicsType
, /* com.apple.print.preset.graphicsType value */
1518 *media_front_coating
; /* com.apple.print.preset.media-front-coating value */
1524 * Add localized text for PWG keyword to message catalog...
1527 snprintf(msg_id
, sizeof(msg_id
), "preset-name.%s", ppd_attr
->spec
);
1528 pwg_add_message(pc
->strings
, msg_id
, ppd_attr
->text
);
1532 * Get the options for this preset...
1535 num_options
= _ppdParseOptions(ppd_attr
->value
, 0, &options
,
1538 if ((quality
= cupsGetOption("com.apple.print.preset.quality",
1539 num_options
, options
)) != NULL
)
1542 * Get the print-quality for this preset...
1545 if (!strcmp(quality
, "low"))
1546 pwg_print_quality
= _PWG_PRINT_QUALITY_DRAFT
;
1547 else if (!strcmp(quality
, "high"))
1548 pwg_print_quality
= _PWG_PRINT_QUALITY_HIGH
;
1550 pwg_print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
1553 * Ignore graphicsType "Photo" presets that are not high quality.
1556 graphicsType
= cupsGetOption("com.apple.print.preset.graphicsType",
1557 num_options
, options
);
1559 if (pwg_print_quality
!= _PWG_PRINT_QUALITY_HIGH
&& graphicsType
&&
1560 !strcmp(graphicsType
, "Photo"))
1564 * Ignore presets for normal and draft quality where the coating
1565 * isn't "none" or "autodetect".
1568 media_front_coating
= cupsGetOption(
1569 "com.apple.print.preset.media-front-coating",
1570 num_options
, options
);
1572 if (pwg_print_quality
!= _PWG_PRINT_QUALITY_HIGH
&&
1573 media_front_coating
&&
1574 strcmp(media_front_coating
, "none") &&
1575 strcmp(media_front_coating
, "autodetect"))
1579 * Get the output mode for this preset...
1582 output_mode
= cupsGetOption("com.apple.print.preset.output-mode",
1583 num_options
, options
);
1584 color_model_val
= cupsGetOption("ColorModel", num_options
, options
);
1588 if (!strcmp(output_mode
, "monochrome"))
1589 pwg_print_color_mode
= _PWG_PRINT_COLOR_MODE_MONOCHROME
;
1591 pwg_print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
1593 else if (color_model_val
)
1595 if (!_cups_strcasecmp(color_model_val
, "Gray"))
1596 pwg_print_color_mode
= _PWG_PRINT_COLOR_MODE_MONOCHROME
;
1598 pwg_print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
1601 pwg_print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
1604 * Save the options for this combination as needed...
1607 if (!pc
->num_presets
[pwg_print_color_mode
][pwg_print_quality
])
1608 pc
->num_presets
[pwg_print_color_mode
][pwg_print_quality
] =
1609 _ppdParseOptions(ppd_attr
->value
, 0,
1610 pc
->presets
[pwg_print_color_mode
] +
1611 pwg_print_quality
, _PPD_PARSE_OPTIONS
);
1614 cupsFreeOptions(num_options
, options
);
1616 while ((ppd_attr
= ppdFindNextAttr(ppd
, "APPrinterPreset", NULL
)) != NULL
);
1619 if (!pc
->num_presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
][_PWG_PRINT_QUALITY_DRAFT
] &&
1620 !pc
->num_presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
][_PWG_PRINT_QUALITY_NORMAL
] &&
1621 !pc
->num_presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
][_PWG_PRINT_QUALITY_HIGH
])
1624 * Try adding some common color options to create grayscale presets. These
1625 * are listed in order of popularity...
1628 const char *color_option
= NULL
, /* Color control option */
1629 *gray_choice
= NULL
; /* Choice to select grayscale */
1631 if ((color_model
= ppdFindOption(ppd
, "ColorModel")) != NULL
&&
1632 ppdFindChoice(color_model
, "Gray"))
1634 color_option
= "ColorModel";
1635 gray_choice
= "Gray";
1637 else if ((color_model
= ppdFindOption(ppd
, "HPColorMode")) != NULL
&&
1638 ppdFindChoice(color_model
, "grayscale"))
1640 color_option
= "HPColorMode";
1641 gray_choice
= "grayscale";
1643 else if ((color_model
= ppdFindOption(ppd
, "BRMonoColor")) != NULL
&&
1644 ppdFindChoice(color_model
, "Mono"))
1646 color_option
= "BRMonoColor";
1647 gray_choice
= "Mono";
1649 else if ((color_model
= ppdFindOption(ppd
, "CNIJSGrayScale")) != NULL
&&
1650 ppdFindChoice(color_model
, "1"))
1652 color_option
= "CNIJSGrayScale";
1655 else if ((color_model
= ppdFindOption(ppd
, "HPColorAsGray")) != NULL
&&
1656 ppdFindChoice(color_model
, "True"))
1658 color_option
= "HPColorAsGray";
1659 gray_choice
= "True";
1662 if (color_option
&& gray_choice
)
1665 * Copy and convert ColorModel (output-mode) data...
1668 cups_option_t
*coption
, /* Color option */
1669 *moption
; /* Monochrome option */
1671 for (pwg_print_quality
= _PWG_PRINT_QUALITY_DRAFT
;
1672 pwg_print_quality
< _PWG_PRINT_QUALITY_MAX
;
1673 pwg_print_quality
++)
1675 if (pc
->num_presets
[_PWG_PRINT_COLOR_MODE_COLOR
][pwg_print_quality
])
1678 * Copy the color options...
1681 num_options
= pc
->num_presets
[_PWG_PRINT_COLOR_MODE_COLOR
]
1682 [pwg_print_quality
];
1683 options
= calloc((size_t)num_options
, sizeof(cups_option_t
));
1687 for (i
= num_options
, moption
= options
,
1688 coption
= pc
->presets
[_PWG_PRINT_COLOR_MODE_COLOR
]
1689 [pwg_print_quality
];
1691 i
--, moption
++, coption
++)
1693 moption
->name
= _cupsStrRetain(coption
->name
);
1694 moption
->value
= _cupsStrRetain(coption
->value
);
1697 pc
->num_presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
][pwg_print_quality
] =
1699 pc
->presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
][pwg_print_quality
] =
1703 else if (pwg_print_quality
!= _PWG_PRINT_QUALITY_NORMAL
)
1707 * Add the grayscale option to the preset...
1710 pc
->num_presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
][pwg_print_quality
] =
1711 cupsAddOption(color_option
, gray_choice
,
1712 pc
->num_presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
]
1713 [pwg_print_quality
],
1714 pc
->presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
] +
1721 * Copy and convert Duplex (sides) data...
1724 if ((duplex
= ppdFindOption(ppd
, "Duplex")) == NULL
)
1725 if ((duplex
= ppdFindOption(ppd
, "JCLDuplex")) == NULL
)
1726 if ((duplex
= ppdFindOption(ppd
, "EFDuplex")) == NULL
)
1727 if ((duplex
= ppdFindOption(ppd
, "EFDuplexing")) == NULL
)
1728 duplex
= ppdFindOption(ppd
, "KD03Duplex");
1732 pc
->sides_option
= strdup(duplex
->keyword
);
1734 for (i
= duplex
->num_choices
, choice
= duplex
->choices
;
1738 if ((!_cups_strcasecmp(choice
->choice
, "None") ||
1739 !_cups_strcasecmp(choice
->choice
, "False")) && !pc
->sides_1sided
)
1740 pc
->sides_1sided
= strdup(choice
->choice
);
1741 else if ((!_cups_strcasecmp(choice
->choice
, "DuplexNoTumble") ||
1742 !_cups_strcasecmp(choice
->choice
, "LongEdge") ||
1743 !_cups_strcasecmp(choice
->choice
, "Top")) && !pc
->sides_2sided_long
)
1744 pc
->sides_2sided_long
= strdup(choice
->choice
);
1745 else if ((!_cups_strcasecmp(choice
->choice
, "DuplexTumble") ||
1746 !_cups_strcasecmp(choice
->choice
, "ShortEdge") ||
1747 !_cups_strcasecmp(choice
->choice
, "Bottom")) &&
1748 !pc
->sides_2sided_short
)
1749 pc
->sides_2sided_short
= strdup(choice
->choice
);
1754 * Copy filters and pre-filters...
1757 pc
->filters
= cupsArrayNew3(NULL
, NULL
, NULL
, 0, (cups_acopy_func_t
)_cupsArrayStrdup
, _cupsArrayFree
);
1759 cupsArrayAdd(pc
->filters
,
1760 "application/vnd.cups-raw application/octet-stream 0 -");
1762 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsFilter2", NULL
)) != NULL
)
1766 cupsArrayAdd(pc
->filters
, ppd_attr
->value
);
1768 while ((ppd_attr
= ppdFindNextAttr(ppd
, "cupsFilter2", NULL
)) != NULL
);
1770 else if (ppd
->num_filters
> 0)
1772 for (i
= 0; i
< ppd
->num_filters
; i
++)
1773 cupsArrayAdd(pc
->filters
, ppd
->filters
[i
]);
1776 cupsArrayAdd(pc
->filters
, "application/vnd.cups-postscript 0 -");
1779 * See if we have a command filter...
1782 for (filter
= (const char *)cupsArrayFirst(pc
->filters
);
1784 filter
= (const char *)cupsArrayNext(pc
->filters
))
1785 if (!_cups_strncasecmp(filter
, "application/vnd.cups-command", 28) &&
1786 _cups_isspace(filter
[28]))
1790 ((ppd_attr
= ppdFindAttr(ppd
, "cupsCommands", NULL
)) == NULL
||
1791 _cups_strcasecmp(ppd_attr
->value
, "none")))
1794 * No command filter and no cupsCommands keyword telling us not to use one.
1795 * See if this is a PostScript printer, and if so add a PostScript command
1799 for (filter
= (const char *)cupsArrayFirst(pc
->filters
);
1801 filter
= (const char *)cupsArrayNext(pc
->filters
))
1802 if (!_cups_strncasecmp(filter
, "application/vnd.cups-postscript", 31) &&
1803 _cups_isspace(filter
[31]))
1807 cupsArrayAdd(pc
->filters
,
1808 "application/vnd.cups-command application/postscript 100 "
1812 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsPreFilter", NULL
)) != NULL
)
1814 pc
->prefilters
= cupsArrayNew3(NULL
, NULL
, NULL
, 0, (cups_acopy_func_t
)_cupsArrayStrdup
, _cupsArrayFree
);
1818 cupsArrayAdd(pc
->prefilters
, ppd_attr
->value
);
1820 while ((ppd_attr
= ppdFindNextAttr(ppd
, "cupsPreFilter", NULL
)) != NULL
);
1823 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsSingleFile", NULL
)) != NULL
)
1824 pc
->single_file
= !_cups_strcasecmp(ppd_attr
->value
, "true");
1827 * Copy the product string, if any...
1831 pc
->product
= strdup(ppd
->product
);
1834 * Copy finishings mapping data...
1837 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsIPPFinishings", NULL
)) != NULL
)
1840 * Have proper vendor mapping of IPP finishings values to PPD options...
1843 pc
->finishings
= cupsArrayNew3((cups_array_func_t
)pwg_compare_finishings
,
1844 NULL
, NULL
, 0, NULL
,
1845 (cups_afree_func_t
)pwg_free_finishings
);
1849 if ((finishings
= calloc(1, sizeof(_pwg_finishings_t
))) == NULL
)
1852 finishings
->value
= (ipp_finishings_t
)atoi(ppd_attr
->spec
);
1853 finishings
->num_options
= _ppdParseOptions(ppd_attr
->value
, 0,
1854 &(finishings
->options
),
1855 _PPD_PARSE_OPTIONS
);
1857 cupsArrayAdd(pc
->finishings
, finishings
);
1859 while ((ppd_attr
= ppdFindNextAttr(ppd
, "cupsIPPFinishings",
1865 * No IPP mapping data, try to map common/standard PPD keywords...
1868 pc
->finishings
= cupsArrayNew3((cups_array_func_t
)pwg_compare_finishings
, NULL
, NULL
, 0, NULL
, (cups_afree_func_t
)pwg_free_finishings
);
1870 if ((ppd_option
= ppdFindOption(ppd
, "StapleLocation")) != NULL
)
1873 * Add staple finishings...
1876 if (ppdFindChoice(ppd_option
, "SinglePortrait"))
1877 pwg_add_finishing(pc
->finishings
, IPP_FINISHINGS_STAPLE_TOP_LEFT
, "StapleLocation", "SinglePortrait");
1878 if (ppdFindChoice(ppd_option
, "UpperLeft")) /* Ricoh extension */
1879 pwg_add_finishing(pc
->finishings
, IPP_FINISHINGS_STAPLE_TOP_LEFT
, "StapleLocation", "UpperLeft");
1880 if (ppdFindChoice(ppd_option
, "UpperRight")) /* Ricoh extension */
1881 pwg_add_finishing(pc
->finishings
, IPP_FINISHINGS_STAPLE_TOP_RIGHT
, "StapleLocation", "UpperRight");
1882 if (ppdFindChoice(ppd_option
, "SingleLandscape"))
1883 pwg_add_finishing(pc
->finishings
, IPP_FINISHINGS_STAPLE_BOTTOM_LEFT
, "StapleLocation", "SingleLandscape");
1884 if (ppdFindChoice(ppd_option
, "DualLandscape"))
1885 pwg_add_finishing(pc
->finishings
, IPP_FINISHINGS_STAPLE_DUAL_LEFT
, "StapleLocation", "DualLandscape");
1888 if ((ppd_option
= ppdFindOption(ppd
, "RIPunch")) != NULL
)
1891 * Add (Ricoh) punch finishings...
1894 if (ppdFindChoice(ppd_option
, "Left2"))
1895 pwg_add_finishing(pc
->finishings
, IPP_FINISHINGS_PUNCH_DUAL_LEFT
, "RIPunch", "Left2");
1896 if (ppdFindChoice(ppd_option
, "Left3"))
1897 pwg_add_finishing(pc
->finishings
, IPP_FINISHINGS_PUNCH_TRIPLE_LEFT
, "RIPunch", "Left3");
1898 if (ppdFindChoice(ppd_option
, "Left4"))
1899 pwg_add_finishing(pc
->finishings
, IPP_FINISHINGS_PUNCH_QUAD_LEFT
, "RIPunch", "Left4");
1900 if (ppdFindChoice(ppd_option
, "Right2"))
1901 pwg_add_finishing(pc
->finishings
, IPP_FINISHINGS_PUNCH_DUAL_RIGHT
, "RIPunch", "Right2");
1902 if (ppdFindChoice(ppd_option
, "Right3"))
1903 pwg_add_finishing(pc
->finishings
, IPP_FINISHINGS_PUNCH_TRIPLE_RIGHT
, "RIPunch", "Right3");
1904 if (ppdFindChoice(ppd_option
, "Right4"))
1905 pwg_add_finishing(pc
->finishings
, IPP_FINISHINGS_PUNCH_QUAD_RIGHT
, "RIPunch", "Right4");
1906 if (ppdFindChoice(ppd_option
, "Upper2"))
1907 pwg_add_finishing(pc
->finishings
, IPP_FINISHINGS_PUNCH_DUAL_TOP
, "RIPunch", "Upper2");
1908 if (ppdFindChoice(ppd_option
, "Upper3"))
1909 pwg_add_finishing(pc
->finishings
, IPP_FINISHINGS_PUNCH_TRIPLE_TOP
, "RIPunch", "Upper3");
1910 if (ppdFindChoice(ppd_option
, "Upper4"))
1911 pwg_add_finishing(pc
->finishings
, IPP_FINISHINGS_PUNCH_QUAD_TOP
, "RIPunch", "Upper4");
1914 if ((ppd_option
= ppdFindOption(ppd
, "BindEdge")) != NULL
)
1917 * Add bind finishings...
1920 if (ppdFindChoice(ppd_option
, "Left"))
1921 pwg_add_finishing(pc
->finishings
, IPP_FINISHINGS_BIND_LEFT
, "BindEdge", "Left");
1922 if (ppdFindChoice(ppd_option
, "Right"))
1923 pwg_add_finishing(pc
->finishings
, IPP_FINISHINGS_BIND_RIGHT
, "BindEdge", "Right");
1924 if (ppdFindChoice(ppd_option
, "Top"))
1925 pwg_add_finishing(pc
->finishings
, IPP_FINISHINGS_BIND_TOP
, "BindEdge", "Top");
1926 if (ppdFindChoice(ppd_option
, "Bottom"))
1927 pwg_add_finishing(pc
->finishings
, IPP_FINISHINGS_BIND_BOTTOM
, "BindEdge", "Bottom");
1930 if ((ppd_option
= ppdFindOption(ppd
, "FoldType")) != NULL
)
1933 * Add (Adobe) fold finishings...
1936 if (ppdFindChoice(ppd_option
, "ZFold"))
1937 pwg_add_finishing(pc
->finishings
, IPP_FINISHINGS_FOLD_Z
, "FoldType", "ZFold");
1938 if (ppdFindChoice(ppd_option
, "Saddle"))
1939 pwg_add_finishing(pc
->finishings
, IPP_FINISHINGS_FOLD_HALF
, "FoldType", "Saddle");
1940 if (ppdFindChoice(ppd_option
, "DoubleGate"))
1941 pwg_add_finishing(pc
->finishings
, IPP_FINISHINGS_FOLD_DOUBLE_GATE
, "FoldType", "DoubleGate");
1942 if (ppdFindChoice(ppd_option
, "LeftGate"))
1943 pwg_add_finishing(pc
->finishings
, IPP_FINISHINGS_FOLD_LEFT_GATE
, "FoldType", "LeftGate");
1944 if (ppdFindChoice(ppd_option
, "RightGate"))
1945 pwg_add_finishing(pc
->finishings
, IPP_FINISHINGS_FOLD_RIGHT_GATE
, "FoldType", "RightGate");
1946 if (ppdFindChoice(ppd_option
, "Letter"))
1947 pwg_add_finishing(pc
->finishings
, IPP_FINISHINGS_FOLD_LETTER
, "FoldType", "Letter");
1948 if (ppdFindChoice(ppd_option
, "XFold"))
1949 pwg_add_finishing(pc
->finishings
, IPP_FINISHINGS_FOLD_POSTER
, "FoldType", "XFold");
1952 if ((ppd_option
= ppdFindOption(ppd
, "RIFoldType")) != NULL
)
1955 * Add (Ricoh) fold finishings...
1958 if (ppdFindChoice(ppd_option
, "OutsideTwoFold"))
1959 pwg_add_finishing(pc
->finishings
, IPP_FINISHINGS_FOLD_LETTER
, "RIFoldType", "OutsideTwoFold");
1962 if (cupsArrayCount(pc
->finishings
) == 0)
1964 cupsArrayDelete(pc
->finishings
);
1965 pc
->finishings
= NULL
;
1969 if ((ppd_option
= ppdFindOption(ppd
, "cupsFinishingTemplate")) != NULL
)
1971 pc
->templates
= cupsArrayNew3((cups_array_func_t
)_cupsArrayStrcmp
, NULL
, NULL
, 0, (cups_acopy_func_t
)_cupsArrayStrdup
, _cupsArrayFree
);
1973 for (choice
= ppd_option
->choices
, i
= ppd_option
->num_choices
; i
> 0; choice
++, i
--)
1975 cupsArrayAdd(pc
->templates
, (void *)choice
->choice
);
1978 * Add localized text for PWG keyword to message catalog...
1981 snprintf(msg_id
, sizeof(msg_id
), "finishing-template.%s", choice
->choice
);
1984 for (lang
= langs
; lang
; lang
= lang
->next
)
1986 // See if the string is already localized...
1987 if (cupsArrayFind(lang
->strings
, &msg
))
1990 // Otherwise add the text...
1991 if (!strcmp(lang
->language
, "en"))
1994 msg
.str
= choice
->text
;
1998 // Other languauge...
1999 snprintf(ppd_name
, sizeof(ppd_name
), "%s.cupsFinishingTemplate", lang
->language
);
2000 if ((ppd_attr
= ppdFindAttr(ppd
, ppd_name
, choice
->choice
)) != NULL
)
2001 msg
.str
= ppd_attr
->text
;
2006 cupsArrayAdd(lang
->strings
, &msg
);
2015 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsMaxCopies", NULL
)) != NULL
)
2016 pc
->max_copies
= atoi(ppd_attr
->value
);
2017 else if (ppd
->manual_copies
)
2020 pc
->max_copies
= 9999;
2023 * cupsChargeInfoURI, cupsJobAccountId, cupsJobAccountingUserId,
2024 * cupsJobPassword, and cupsMandatory.
2027 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsChargeInfoURI", NULL
)) != NULL
)
2028 pc
->charge_info_uri
= strdup(ppd_attr
->value
);
2030 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsJobAccountId", NULL
)) != NULL
)
2031 pc
->account_id
= !_cups_strcasecmp(ppd_attr
->value
, "true");
2033 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsJobAccountingUserId", NULL
)) != NULL
)
2034 pc
->accounting_user_id
= !_cups_strcasecmp(ppd_attr
->value
, "true");
2036 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsJobPassword", NULL
)) != NULL
)
2037 pc
->password
= strdup(ppd_attr
->value
);
2039 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsMandatory", NULL
)) != NULL
)
2040 pc
->mandatory
= cupsArrayNewStrings(ppd_attr
->value
, ' ');
2046 pc
->support_files
= cupsArrayNew3(NULL
, NULL
, NULL
, 0, (cups_acopy_func_t
)_cupsArrayStrdup
, _cupsArrayFree
);
2048 for (ppd_attr
= ppdFindAttr(ppd
, "cupsICCProfile", NULL
);
2050 ppd_attr
= ppdFindNextAttr(ppd
, "cupsICCProfile", NULL
))
2051 cupsArrayAdd(pc
->support_files
, ppd_attr
->value
);
2053 #ifdef HAVE_APPLICATIONSERVICES_H
2054 if ((ppd_attr
= ppdFindAttr(ppd
, "APPrinterIconPath", NULL
)) != NULL
)
2055 cupsArrayAdd(pc
->support_files
, ppd_attr
->value
);
2059 * Return the cache data...
2065 * If we get here we need to destroy the PWG mapping data and return NULL...
2070 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Out of memory."), 1);
2071 _ppdCacheDestroy(pc
);
2078 * '_ppdCacheDestroy()' - Free all memory used for PWG mapping data.
2082 _ppdCacheDestroy(_ppd_cache_t
*pc
) /* I - PPD cache and mapping data */
2084 int i
; /* Looping var */
2085 pwg_map_t
*map
; /* Current map */
2086 pwg_size_t
*size
; /* Current size */
2090 * Range check input...
2097 * Free memory as needed...
2102 for (i
= pc
->num_bins
, map
= pc
->bins
; i
> 0; i
--, map
++)
2113 for (i
= pc
->num_sizes
, size
= pc
->sizes
; i
> 0; i
--, size
++)
2115 free(size
->map
.pwg
);
2116 free(size
->map
.ppd
);
2122 free(pc
->source_option
);
2126 for (i
= pc
->num_sources
, map
= pc
->sources
; i
> 0; i
--, map
++)
2137 for (i
= pc
->num_types
, map
= pc
->types
; i
> 0; i
--, map
++)
2146 free(pc
->custom_max_keyword
);
2147 free(pc
->custom_min_keyword
);
2150 cupsArrayDelete(pc
->filters
);
2151 cupsArrayDelete(pc
->prefilters
);
2152 cupsArrayDelete(pc
->finishings
);
2154 free(pc
->charge_info_uri
);
2157 free(pc
->sides_option
);
2158 free(pc
->sides_1sided
);
2159 free(pc
->sides_2sided_long
);
2160 free(pc
->sides_2sided_short
);
2162 cupsArrayDelete(pc
->mandatory
);
2164 cupsArrayDelete(pc
->support_files
);
2171 * '_ppdCacheGetBin()' - Get the PWG output-bin keyword associated with a PPD
2175 const char * /* O - output-bin or NULL */
2177 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2178 const char *output_bin
) /* I - PPD OutputBin string */
2180 int i
; /* Looping var */
2184 * Range check input...
2187 if (!pc
|| !output_bin
)
2191 * Look up the OutputBin string...
2195 for (i
= 0; i
< pc
->num_bins
; i
++)
2196 if (!_cups_strcasecmp(output_bin
, pc
->bins
[i
].ppd
) || !_cups_strcasecmp(output_bin
, pc
->bins
[i
].pwg
))
2197 return (pc
->bins
[i
].pwg
);
2204 * '_ppdCacheGetFinishingOptions()' - Get PPD finishing options for the given
2205 * IPP finishings value(s).
2208 int /* O - New number of options */
2209 _ppdCacheGetFinishingOptions(
2210 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2211 ipp_t
*job
, /* I - Job attributes or NULL */
2212 ipp_finishings_t value
, /* I - IPP finishings value of IPP_FINISHINGS_NONE */
2213 int num_options
, /* I - Number of options */
2214 cups_option_t
**options
) /* IO - Options */
2216 int i
; /* Looping var */
2217 _pwg_finishings_t
*f
, /* PWG finishings options */
2218 key
; /* Search key */
2219 ipp_attribute_t
*attr
; /* Finishings attribute */
2220 cups_option_t
*option
; /* Current finishings option */
2224 * Range check input...
2227 if (!pc
|| cupsArrayCount(pc
->finishings
) == 0 || !options
||
2228 (!job
&& value
== IPP_FINISHINGS_NONE
))
2229 return (num_options
);
2232 * Apply finishing options...
2235 if (job
&& (attr
= ippFindAttribute(job
, "finishings", IPP_TAG_ENUM
)) != NULL
)
2237 int num_values
= ippGetCount(attr
); /* Number of values */
2239 for (i
= 0; i
< num_values
; i
++)
2241 key
.value
= (ipp_finishings_t
)ippGetInteger(attr
, i
);
2243 if ((f
= cupsArrayFind(pc
->finishings
, &key
)) != NULL
)
2245 int j
; /* Another looping var */
2247 for (j
= f
->num_options
, option
= f
->options
; j
> 0; j
--, option
++)
2248 num_options
= cupsAddOption(option
->name
, option
->value
,
2249 num_options
, options
);
2253 else if (value
!= IPP_FINISHINGS_NONE
)
2257 if ((f
= cupsArrayFind(pc
->finishings
, &key
)) != NULL
)
2259 int j
; /* Another looping var */
2261 for (j
= f
->num_options
, option
= f
->options
; j
> 0; j
--, option
++)
2262 num_options
= cupsAddOption(option
->name
, option
->value
,
2263 num_options
, options
);
2267 return (num_options
);
2272 * '_ppdCacheGetFinishingValues()' - Get IPP finishings value(s) from the given
2276 int /* O - Number of finishings values */
2277 _ppdCacheGetFinishingValues(
2278 ppd_file_t
*ppd
, /* I - Marked PPD file */
2279 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2280 int max_values
, /* I - Maximum number of finishings values */
2281 int *values
) /* O - Finishings values */
2283 int i
, /* Looping var */
2284 num_values
= 0; /* Number of values */
2285 _pwg_finishings_t
*f
; /* Current finishings option */
2286 cups_option_t
*option
; /* Current option */
2287 ppd_choice_t
*choice
; /* Marked PPD choice */
2291 * Range check input...
2294 DEBUG_printf("_ppdCacheGetFinishingValues(ppd=%p, pc=%p, max_values=%d, values=%p)", (void *)ppd
, (void *)pc
, max_values
, (void *)values
);
2296 if (!ppd
|| !pc
|| max_values
< 1 || !values
)
2298 DEBUG_puts("_ppdCacheGetFinishingValues: Bad arguments, returning 0.");
2301 else if (!pc
->finishings
)
2303 DEBUG_puts("_ppdCacheGetFinishingValues: No finishings support, returning 0.");
2308 * Go through the finishings options and see what is set...
2311 for (f
= (_pwg_finishings_t
*)cupsArrayFirst(pc
->finishings
);
2313 f
= (_pwg_finishings_t
*)cupsArrayNext(pc
->finishings
))
2315 DEBUG_printf("_ppdCacheGetFinishingValues: Checking %d (%s)", (int)f
->value
, ippEnumString("finishings", (int)f
->value
));
2317 for (i
= f
->num_options
, option
= f
->options
; i
> 0; i
--, option
++)
2319 DEBUG_printf("_ppdCacheGetFinishingValues: %s=%s?", option
->name
, option
->value
);
2321 if ((choice
= ppdFindMarkedChoice(ppd
, option
->name
)) == NULL
|| _cups_strcasecmp(option
->value
, choice
->choice
))
2323 DEBUG_puts("_ppdCacheGetFinishingValues: NO");
2330 DEBUG_printf("_ppdCacheGetFinishingValues: Adding %d (%s)", (int)f
->value
, ippEnumString("finishings", (int)f
->value
));
2332 values
[num_values
++] = (int)f
->value
;
2334 if (num_values
>= max_values
)
2339 if (num_values
== 0)
2342 * Always have at least "finishings" = 'none'...
2345 DEBUG_puts("_ppdCacheGetFinishingValues: Adding 3 (none).");
2346 values
[0] = IPP_FINISHINGS_NONE
;
2350 DEBUG_printf("_ppdCacheGetFinishingValues: Returning %d.", num_values
);
2352 return (num_values
);
2357 * 'ppd_inputslot_for_keyword()' - Return the PPD InputSlot associated
2358 * a keyword string, or NULL if no mapping
2361 static const char * /* O - PPD InputSlot or NULL */
2362 ppd_inputslot_for_keyword(
2363 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2364 const char *keyword
) /* I - Keyword string */
2366 int i
; /* Looping var */
2368 if (!pc
|| !keyword
)
2371 for (i
= 0; i
< pc
->num_sources
; i
++)
2372 if (!_cups_strcasecmp(keyword
, pc
->sources
[i
].pwg
) || !_cups_strcasecmp(keyword
, pc
->sources
[i
].ppd
))
2373 return (pc
->sources
[i
].ppd
);
2380 * '_ppdCacheGetInputSlot()' - Get the PPD InputSlot associated with the job
2381 * attributes or a keyword string.
2384 const char * /* O - PPD InputSlot or NULL */
2385 _ppdCacheGetInputSlot(
2386 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2387 ipp_t
*job
, /* I - Job attributes or NULL */
2388 const char *keyword
) /* I - Keyword string or NULL */
2391 * Range check input...
2394 if (!pc
|| pc
->num_sources
== 0 || (!job
&& !keyword
))
2397 if (job
&& !keyword
)
2400 * Lookup the media-col attribute and any media-source found there...
2403 ipp_attribute_t
*media_col
, /* media-col attribute */
2404 *media_source
; /* media-source attribute */
2405 pwg_size_t size
; /* Dimensional size */
2406 int margins_set
; /* Were the margins set? */
2408 media_col
= ippFindAttribute(job
, "media-col", IPP_TAG_BEGIN_COLLECTION
);
2410 (media_source
= ippFindAttribute(ippGetCollection(media_col
, 0),
2412 IPP_TAG_KEYWORD
)) != NULL
)
2415 * Use the media-source value from media-col...
2418 keyword
= ippGetString(media_source
, 0, NULL
);
2420 else if (pwgInitSize(&size
, job
, &margins_set
))
2423 * For media <= 5x7, try to ask for automatic selection so the printer can
2424 * pick the photo tray. If auto isn't available, fall back to explicitly
2425 * asking for the photo tray.
2428 if (size
.width
<= (5 * 2540) && size
.length
<= (7 * 2540)) {
2430 if ((match
= ppd_inputslot_for_keyword(pc
, "auto")) != NULL
)
2437 return (ppd_inputslot_for_keyword(pc
, keyword
));
2442 * '_ppdCacheGetMediaType()' - Get the PPD MediaType associated with the job
2443 * attributes or a keyword string.
2446 const char * /* O - PPD MediaType or NULL */
2447 _ppdCacheGetMediaType(
2448 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2449 ipp_t
*job
, /* I - Job attributes or NULL */
2450 const char *keyword
) /* I - Keyword string or NULL */
2453 * Range check input...
2456 if (!pc
|| pc
->num_types
== 0 || (!job
&& !keyword
))
2459 if (job
&& !keyword
)
2462 * Lookup the media-col attribute and any media-source found there...
2465 ipp_attribute_t
*media_col
, /* media-col attribute */
2466 *media_type
; /* media-type attribute */
2468 media_col
= ippFindAttribute(job
, "media-col", IPP_TAG_BEGIN_COLLECTION
);
2471 if ((media_type
= ippFindAttribute(media_col
->values
[0].collection
,
2473 IPP_TAG_KEYWORD
)) == NULL
)
2474 media_type
= ippFindAttribute(media_col
->values
[0].collection
,
2475 "media-type", IPP_TAG_NAME
);
2478 keyword
= media_type
->values
[0].string
.text
;
2484 int i
; /* Looping var */
2486 for (i
= 0; i
< pc
->num_types
; i
++)
2487 if (!_cups_strcasecmp(keyword
, pc
->types
[i
].pwg
) || !_cups_strcasecmp(keyword
, pc
->types
[i
].ppd
))
2488 return (pc
->types
[i
].ppd
);
2496 * '_ppdCacheGetOutputBin()' - Get the PPD OutputBin associated with the keyword
2500 const char * /* O - PPD OutputBin or NULL */
2501 _ppdCacheGetOutputBin(
2502 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2503 const char *output_bin
) /* I - Keyword string */
2505 int i
; /* Looping var */
2509 * Range check input...
2512 if (!pc
|| !output_bin
)
2516 * Look up the OutputBin string...
2520 for (i
= 0; i
< pc
->num_bins
; i
++)
2521 if (!_cups_strcasecmp(output_bin
, pc
->bins
[i
].pwg
) || !_cups_strcasecmp(output_bin
, pc
->bins
[i
].ppd
))
2522 return (pc
->bins
[i
].ppd
);
2529 * '_ppdCacheGetPageSize()' - Get the PPD PageSize associated with the job
2530 * attributes or a keyword string.
2533 const char * /* O - PPD PageSize or NULL */
2534 _ppdCacheGetPageSize(
2535 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2536 ipp_t
*job
, /* I - Job attributes or NULL */
2537 const char *keyword
, /* I - Keyword string or NULL */
2538 int *exact
) /* O - 1 if exact match, 0 otherwise */
2540 int i
; /* Looping var */
2541 pwg_size_t
*size
, /* Current size */
2542 *closest
, /* Closest size */
2543 jobsize
; /* Size data from job */
2544 int margins_set
, /* Were the margins set? */
2545 dwidth
, /* Difference in width */
2546 dlength
, /* Difference in length */
2547 dleft
, /* Difference in left margins */
2548 dright
, /* Difference in right margins */
2549 dbottom
, /* Difference in bottom margins */
2550 dtop
, /* Difference in top margins */
2551 dmin
, /* Minimum difference */
2552 dclosest
; /* Closest difference */
2553 const char *ppd_name
; /* PPD media name */
2556 DEBUG_printf("_ppdCacheGetPageSize(pc=%p, job=%p, keyword=\"%s\", exact=%p)", (void *)pc
, (void *)job
, keyword
, (void *)exact
);
2559 * Range check input...
2562 if (!pc
|| (!job
&& !keyword
))
2573 * Try getting the PPD media name from the job attributes...
2576 ipp_attribute_t
*attr
; /* Job attribute */
2578 if ((attr
= ippFindAttribute(job
, "PageSize", IPP_TAG_ZERO
)) == NULL
)
2579 if ((attr
= ippFindAttribute(job
, "PageRegion", IPP_TAG_ZERO
)) == NULL
)
2580 attr
= ippFindAttribute(job
, "media", IPP_TAG_ZERO
);
2584 DEBUG_printf("1_ppdCacheGetPageSize: Found attribute %s (%s)", attr
->name
, ippTagString(attr
->value_tag
));
2586 DEBUG_puts("1_ppdCacheGetPageSize: Did not find media attribute.");
2589 if (attr
&& (attr
->value_tag
== IPP_TAG_NAME
||
2590 attr
->value_tag
== IPP_TAG_KEYWORD
))
2591 ppd_name
= attr
->values
[0].string
.text
;
2594 DEBUG_printf("1_ppdCacheGetPageSize: ppd_name=\"%s\"", ppd_name
);
2599 * Try looking up the named PPD size first...
2602 for (i
= pc
->num_sizes
, size
= pc
->sizes
; i
> 0; i
--, size
++)
2604 DEBUG_printf("2_ppdCacheGetPageSize: size[%d]=[\"%s\" \"%s\"]", (int)(size
- pc
->sizes
), size
->map
.pwg
, size
->map
.ppd
);
2606 if (!_cups_strcasecmp(ppd_name
, size
->map
.ppd
) ||
2607 !_cups_strcasecmp(ppd_name
, size
->map
.pwg
))
2612 DEBUG_printf("1_ppdCacheGetPageSize: Returning \"%s\"", ppd_name
);
2614 return (size
->map
.ppd
);
2619 if (job
&& !keyword
)
2622 * Get the size using media-col or media, with the preference being
2626 if (!pwgInitSize(&jobsize
, job
, &margins_set
))
2632 * Get the size using a media keyword...
2635 pwg_media_t
*media
; /* Media definition */
2638 if ((media
= pwgMediaForPWG(keyword
)) == NULL
)
2639 if ((media
= pwgMediaForLegacy(keyword
)) == NULL
)
2640 if ((media
= pwgMediaForPPD(keyword
)) == NULL
)
2643 jobsize
.width
= media
->width
;
2644 jobsize
.length
= media
->length
;
2649 * Now that we have the dimensions and possibly the margins, look at the
2650 * available sizes and find the match...
2654 dclosest
= 999999999;
2656 if (!ppd_name
|| _cups_strncasecmp(ppd_name
, "Custom.", 7) ||
2657 _cups_strncasecmp(ppd_name
, "custom_", 7))
2659 for (i
= pc
->num_sizes
, size
= pc
->sizes
; i
> 0; i
--, size
++)
2662 * Adobe uses a size matching algorithm with an epsilon of 5 points, which
2663 * is just about 176/2540ths...
2666 dwidth
= size
->width
- jobsize
.width
;
2667 dlength
= size
->length
- jobsize
.length
;
2669 if (dwidth
<= -176 || dwidth
>= 176 || dlength
<= -176 || dlength
>= 176)
2675 * Use a tighter epsilon of 1 point (35/2540ths) for margins...
2678 dleft
= size
->left
- jobsize
.left
;
2679 dright
= size
->right
- jobsize
.right
;
2680 dtop
= size
->top
- jobsize
.top
;
2681 dbottom
= size
->bottom
- jobsize
.bottom
;
2683 if (dleft
<= -35 || dleft
>= 35 || dright
<= -35 || dright
>= 35 ||
2684 dtop
<= -35 || dtop
>= 35 || dbottom
<= -35 || dbottom
>= 35)
2686 dleft
= dleft
< 0 ? -dleft
: dleft
;
2687 dright
= dright
< 0 ? -dright
: dright
;
2688 dbottom
= dbottom
< 0 ? -dbottom
: dbottom
;
2689 dtop
= dtop
< 0 ? -dtop
: dtop
;
2690 dmin
= dleft
+ dright
+ dbottom
+ dtop
;
2692 if (dmin
< dclosest
)
2705 DEBUG_printf("1_ppdCacheGetPageSize: Returning \"%s\"", size
->map
.ppd
);
2707 return (size
->map
.ppd
);
2713 DEBUG_printf("1_ppdCacheGetPageSize: Returning \"%s\" (closest)", closest
->map
.ppd
);
2715 return (closest
->map
.ppd
);
2719 * If we get here we need to check for custom page size support...
2722 if (jobsize
.width
>= pc
->custom_min_width
&&
2723 jobsize
.width
<= pc
->custom_max_width
&&
2724 jobsize
.length
>= pc
->custom_min_length
&&
2725 jobsize
.length
<= pc
->custom_max_length
)
2728 * In range, format as Custom.WWWWxLLLL (points).
2731 snprintf(pc
->custom_ppd_size
, sizeof(pc
->custom_ppd_size
), "Custom.%dx%d",
2732 (int)PWG_TO_POINTS(jobsize
.width
), (int)PWG_TO_POINTS(jobsize
.length
));
2734 if (margins_set
&& exact
)
2736 dleft
= pc
->custom_size
.left
- jobsize
.left
;
2737 dright
= pc
->custom_size
.right
- jobsize
.right
;
2738 dtop
= pc
->custom_size
.top
- jobsize
.top
;
2739 dbottom
= pc
->custom_size
.bottom
- jobsize
.bottom
;
2741 if (dleft
> -35 && dleft
< 35 && dright
> -35 && dright
< 35 &&
2742 dtop
> -35 && dtop
< 35 && dbottom
> -35 && dbottom
< 35)
2748 DEBUG_printf("1_ppdCacheGetPageSize: Returning \"%s\" (custom)", pc
->custom_ppd_size
);
2750 return (pc
->custom_ppd_size
);
2754 * No custom page size support or the size is out of range - return NULL.
2757 DEBUG_puts("1_ppdCacheGetPageSize: Returning NULL");
2764 * '_ppdCacheGetSize()' - Get the PWG size associated with a PPD PageSize.
2767 pwg_size_t
* /* O - PWG size or NULL */
2769 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2770 const char *page_size
, /* I - PPD PageSize */
2771 ppd_size_t
*ppd_size
) /* I - PPD page size information */
2773 int i
; /* Looping var */
2774 pwg_media_t
*media
; /* Media */
2775 pwg_size_t
*size
; /* Current size */
2779 * Range check input...
2782 if (!pc
|| !page_size
)
2785 if (!_cups_strcasecmp(page_size
, "Custom") || !_cups_strncasecmp(page_size
, "Custom.", 7))
2788 * Custom size; size name can be one of the following:
2790 * Custom.WIDTHxLENGTHin - Size in inches
2791 * Custom.WIDTHxLENGTHft - Size in feet
2792 * Custom.WIDTHxLENGTHcm - Size in centimeters
2793 * Custom.WIDTHxLENGTHmm - Size in millimeters
2794 * Custom.WIDTHxLENGTHm - Size in meters
2795 * Custom.WIDTHxLENGTH[pt] - Size in points
2798 double w
, l
; /* Width and length of page */
2799 char *ptr
; /* Pointer into PageSize */
2800 struct lconv
*loc
; /* Locale data */
2805 w
= (float)_cupsStrScand(page_size
+ 7, &ptr
, loc
);
2806 if (!ptr
|| *ptr
!= 'x')
2809 l
= (float)_cupsStrScand(ptr
+ 1, &ptr
, loc
);
2813 if (!_cups_strcasecmp(ptr
, "in"))
2818 else if (!_cups_strcasecmp(ptr
, "ft"))
2823 else if (!_cups_strcasecmp(ptr
, "mm"))
2828 else if (!_cups_strcasecmp(ptr
, "cm"))
2833 else if (!_cups_strcasecmp(ptr
, "m"))
2846 w
= ppd_size
->width
* 2540.0 / 72.0;
2847 l
= ppd_size
->length
* 2540.0 / 72.0;
2851 // No custom size information...
2855 pc
->custom_size
.map
.ppd
= (char *)page_size
;
2856 pc
->custom_size
.width
= (int)w
;
2857 pc
->custom_size
.length
= (int)l
;
2859 if ((media
= pwgMediaForSize((int)w
, (int)l
)) != NULL
)
2860 pc
->custom_size
.map
.pwg
= (char *)media
->pwg
;
2862 return (&(pc
->custom_size
));
2866 * Not a custom size - look it up...
2869 for (i
= pc
->num_sizes
, size
= pc
->sizes
; i
> 0; i
--, size
++)
2871 if (!_cups_strcasecmp(page_size
, size
->map
.ppd
) ||
2872 !_cups_strcasecmp(page_size
, size
->map
.pwg
))
2877 * Look up standard sizes...
2880 if ((media
= pwgMediaForPPD(page_size
)) == NULL
)
2882 if ((media
= pwgMediaForLegacy(page_size
)) == NULL
)
2883 media
= pwgMediaForPWG(page_size
);
2888 pc
->custom_size
.map
.ppd
= (char *)page_size
;
2889 pc
->custom_size
.map
.pwg
= (char *)media
->pwg
;
2890 pc
->custom_size
.width
= media
->width
;
2891 pc
->custom_size
.length
= media
->length
;
2893 return (&(pc
->custom_size
));
2901 * '_ppdCacheGetSource()' - Get the PWG media-source associated with a PPD
2905 const char * /* O - PWG media-source keyword */
2907 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2908 const char *input_slot
) /* I - PPD InputSlot */
2910 int i
; /* Looping var */
2911 pwg_map_t
*source
; /* Current source */
2915 * Range check input...
2918 if (!pc
|| !input_slot
)
2921 for (i
= pc
->num_sources
, source
= pc
->sources
; i
> 0; i
--, source
++)
2922 if (!_cups_strcasecmp(input_slot
, source
->ppd
) || !_cups_strcasecmp(input_slot
, source
->pwg
))
2923 return (source
->pwg
);
2930 * '_ppdCacheGetType()' - Get the PWG media-type associated with a PPD
2934 const char * /* O - PWG media-type keyword */
2936 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2937 const char *media_type
) /* I - PPD MediaType */
2939 int i
; /* Looping var */
2940 pwg_map_t
*type
; /* Current type */
2944 * Range check input...
2947 if (!pc
|| !media_type
)
2950 for (i
= pc
->num_types
, type
= pc
->types
; i
> 0; i
--, type
++)
2951 if (!_cups_strcasecmp(media_type
, type
->ppd
) || !_cups_strcasecmp(media_type
, type
->pwg
))
2959 * '_ppdCacheWriteFile()' - Write PWG mapping data to a file.
2962 int /* O - 1 on success, 0 on failure */
2964 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2965 const char *filename
, /* I - File to write */
2966 ipp_t
*attrs
) /* I - Attributes to write, if any */
2968 int i
, j
, k
; /* Looping vars */
2969 cups_file_t
*fp
; /* Output file */
2970 pwg_size_t
*size
; /* Current size */
2971 pwg_map_t
*map
; /* Current map */
2972 _pwg_finishings_t
*f
; /* Current finishing option */
2973 cups_option_t
*option
; /* Current option */
2974 const char *value
; /* String value */
2975 char newfile
[1024]; /* New filename */
2979 * Range check input...
2982 if (!pc
|| !filename
)
2984 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
2989 * Open the file and write with compression...
2992 snprintf(newfile
, sizeof(newfile
), "%s.N", filename
);
2993 if ((fp
= cupsFileOpen(newfile
, "w9")) == NULL
)
2995 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
3000 * Standard header...
3003 cupsFilePrintf(fp
, "#CUPS-PPD-CACHE-%d\n", _PPD_CACHE_VERSION
);
3009 if (pc
->num_bins
> 0)
3011 cupsFilePrintf(fp
, "NumBins %d\n", pc
->num_bins
);
3012 for (i
= pc
->num_bins
, map
= pc
->bins
; i
> 0; i
--, map
++)
3013 cupsFilePrintf(fp
, "Bin %s %s\n", map
->pwg
, map
->ppd
);
3020 cupsFilePrintf(fp
, "NumSizes %d\n", pc
->num_sizes
);
3021 for (i
= pc
->num_sizes
, size
= pc
->sizes
; i
> 0; i
--, size
++)
3022 cupsFilePrintf(fp
, "Size %s %s %d %d %d %d %d %d\n", size
->map
.pwg
,
3023 size
->map
.ppd
, size
->width
, size
->length
, size
->left
,
3024 size
->bottom
, size
->right
, size
->top
);
3025 if (pc
->custom_max_width
> 0)
3026 cupsFilePrintf(fp
, "CustomSize %d %d %d %d %d %d %d %d\n",
3027 pc
->custom_max_width
, pc
->custom_max_length
,
3028 pc
->custom_min_width
, pc
->custom_min_length
,
3029 pc
->custom_size
.left
, pc
->custom_size
.bottom
,
3030 pc
->custom_size
.right
, pc
->custom_size
.top
);
3036 if (pc
->source_option
)
3037 cupsFilePrintf(fp
, "SourceOption %s\n", pc
->source_option
);
3039 if (pc
->num_sources
> 0)
3041 cupsFilePrintf(fp
, "NumSources %d\n", pc
->num_sources
);
3042 for (i
= pc
->num_sources
, map
= pc
->sources
; i
> 0; i
--, map
++)
3043 cupsFilePrintf(fp
, "Source %s %s\n", map
->pwg
, map
->ppd
);
3050 if (pc
->num_types
> 0)
3052 cupsFilePrintf(fp
, "NumTypes %d\n", pc
->num_types
);
3053 for (i
= pc
->num_types
, map
= pc
->types
; i
> 0; i
--, map
++)
3054 cupsFilePrintf(fp
, "Type %s %s\n", map
->pwg
, map
->ppd
);
3061 for (i
= _PWG_PRINT_COLOR_MODE_MONOCHROME
; i
< _PWG_PRINT_COLOR_MODE_MAX
; i
++)
3063 for (j
= _PWG_PRINT_QUALITY_DRAFT
; j
< _PWG_PRINT_QUALITY_MAX
; j
++)
3065 if (pc
->num_presets
[i
][j
])
3067 cupsFilePrintf(fp
, "Preset %d %d", i
, j
);
3068 for (k
= pc
->num_presets
[i
][j
], option
= pc
->presets
[i
][j
];
3071 cupsFilePrintf(fp
, " %s=%s", option
->name
, option
->value
);
3072 cupsFilePutChar(fp
, '\n');
3081 if (pc
->sides_option
)
3082 cupsFilePrintf(fp
, "SidesOption %s\n", pc
->sides_option
);
3084 if (pc
->sides_1sided
)
3085 cupsFilePrintf(fp
, "Sides1Sided %s\n", pc
->sides_1sided
);
3087 if (pc
->sides_2sided_long
)
3088 cupsFilePrintf(fp
, "Sides2SidedLong %s\n", pc
->sides_2sided_long
);
3090 if (pc
->sides_2sided_short
)
3091 cupsFilePrintf(fp
, "Sides2SidedShort %s\n", pc
->sides_2sided_short
);
3094 * Product, cupsFilter, cupsFilter2, and cupsPreFilter...
3098 cupsFilePutConf(fp
, "Product", pc
->product
);
3100 for (value
= (const char *)cupsArrayFirst(pc
->filters
);
3102 value
= (const char *)cupsArrayNext(pc
->filters
))
3103 cupsFilePutConf(fp
, "Filter", value
);
3105 for (value
= (const char *)cupsArrayFirst(pc
->prefilters
);
3107 value
= (const char *)cupsArrayNext(pc
->prefilters
))
3108 cupsFilePutConf(fp
, "PreFilter", value
);
3110 cupsFilePrintf(fp
, "SingleFile %s\n", pc
->single_file
? "true" : "false");
3113 * Finishing options...
3116 for (f
= (_pwg_finishings_t
*)cupsArrayFirst(pc
->finishings
);
3118 f
= (_pwg_finishings_t
*)cupsArrayNext(pc
->finishings
))
3120 cupsFilePrintf(fp
, "Finishings %d", f
->value
);
3121 for (i
= f
->num_options
, option
= f
->options
; i
> 0; i
--, option
++)
3122 cupsFilePrintf(fp
, " %s=%s", option
->name
, option
->value
);
3123 cupsFilePutChar(fp
, '\n');
3126 for (value
= (const char *)cupsArrayFirst(pc
->templates
); value
; value
= (const char *)cupsArrayNext(pc
->templates
))
3127 cupsFilePutConf(fp
, "FinishingTemplate", value
);
3133 cupsFilePrintf(fp
, "MaxCopies %d\n", pc
->max_copies
);
3136 * Accounting/quota/PIN/managed printing values...
3139 if (pc
->charge_info_uri
)
3140 cupsFilePutConf(fp
, "ChargeInfoURI", pc
->charge_info_uri
);
3142 cupsFilePrintf(fp
, "JobAccountId %s\n", pc
->account_id
? "true" : "false");
3143 cupsFilePrintf(fp
, "JobAccountingUserId %s\n",
3144 pc
->accounting_user_id
? "true" : "false");
3147 cupsFilePutConf(fp
, "JobPassword", pc
->password
);
3149 for (value
= (char *)cupsArrayFirst(pc
->mandatory
);
3151 value
= (char *)cupsArrayNext(pc
->mandatory
))
3152 cupsFilePutConf(fp
, "Mandatory", value
);
3158 for (value
= (char *)cupsArrayFirst(pc
->support_files
);
3160 value
= (char *)cupsArrayNext(pc
->support_files
))
3161 cupsFilePutConf(fp
, "SupportFile", value
);
3164 * IPP attributes, if any...
3169 cupsFilePrintf(fp
, "IPP " CUPS_LLFMT
"\n", CUPS_LLCAST
ippLength(attrs
));
3171 attrs
->state
= IPP_STATE_IDLE
;
3172 ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
, attrs
);
3176 * Close and return...
3179 if (cupsFileClose(fp
))
3186 return (!rename(newfile
, filename
));
3191 * '_ppdCreateFromIPP()' - Create a PPD file describing the capabilities
3192 * of an IPP printer.
3195 char * /* O - PPD filename or `NULL` on error */
3196 _ppdCreateFromIPP(char *buffer
, /* I - Filename buffer */
3197 size_t bufsize
, /* I - Size of filename buffer */
3198 ipp_t
*supported
) /* I - Get-Printer-Attributes response */
3200 const char *printer_uri
; /* Printer URI */
3201 http_t
*http
= NULL
; /* HTTP connection */
3202 cups_lang_t
*base_lang
, /* Base English language */
3203 *lang
, /* Current language information */
3204 *langs
= NULL
; /* Language (strings) files */
3205 const char *prefix
; /* Prefix string */
3206 cups_file_t
*fp
; /* PPD file */
3207 cups_array_t
*sizes
; /* Media sizes supported by printer */
3208 cups_size_t
*size
; /* Current media size */
3209 ipp_attribute_t
*attr
, /* xxx-supported */
3210 *lang_supp
, /* printer-strings-languages-supported */
3211 *defattr
, /* xxx-default */
3212 *quality
, /* print-quality-supported */
3213 *x_dim
, *y_dim
; /* Media dimensions */
3214 ipp_t
*media_col
, /* Media collection */
3215 *media_size
; /* Media size collection */
3216 char make
[256], /* Make and model */
3217 *mptr
, /* Pointer into make and model */
3218 ppdname
[PPD_MAX_NAME
],
3220 ppdtext
[PPD_MAX_TEXT
];
3221 /* PPD English text */
3222 const char *model
; /* Model name */
3223 int i
, j
, /* Looping vars */
3224 count
, /* Number of values */
3225 bottom
, /* Largest bottom margin */
3226 left
, /* Largest left margin */
3227 right
, /* Largest right margin */
3228 top
, /* Largest top margin */
3229 max_length
= 0, /* Maximum custom size */
3231 min_length
= INT_MAX
,
3232 /* Minimum custom size */
3233 min_width
= INT_MAX
,
3234 is_apple
= 0, /* Does the printer support Apple raster? */
3235 is_pdf
= 0, /* Does the printer support PDF? */
3236 is_pwg
= 0; /* Does the printer support PWG Raster? */
3237 pwg_media_t
*pwg
; /* PWG media size */
3238 int xres
, yres
; /* Resolution values */
3239 int resolutions
[1000];
3240 /* Array of resolution indices */
3241 int have_qdraft
= 0,/* Have draft quality? */
3242 have_qhigh
= 0; /* Have high quality? */
3243 char msgid
[256]; /* PWG message identifier (attr.value) */
3244 const char *keyword
; /* Keyword value */
3245 struct lconv
*loc
= localeconv();
3247 cups_array_t
*fin_options
= NULL
;
3248 /* Finishing options */
3252 * Range check input...
3258 if (!buffer
|| bufsize
< 1)
3260 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
3266 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No IPP attributes."), 1);
3270 if ((printer_uri
= ippGetString(ippFindAttribute(supported
, "printer-uri-supported", IPP_TAG_URI
), 0, NULL
)) == NULL
)
3272 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No printer-uri-supported attribute."), 1);
3277 * Open a temporary file for the PPD...
3280 if ((fp
= cupsCreateTempFile("ippeve", ".ppd", buffer
, bufsize
)) == NULL
)
3282 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
3287 * Get a sanitized make and model...
3290 if ((attr
= ippFindAttribute(supported
, "printer-make-and-model", IPP_TAG_TEXT
)) != NULL
&& ippValidateAttribute(attr
))
3293 * Sanitize the model name to only contain PPD-safe characters.
3296 cupsCopyString(make
, ippGetString(attr
, 0, NULL
), sizeof(make
));
3298 for (mptr
= make
; *mptr
; mptr
++)
3300 if (*mptr
< ' ' || *mptr
>= 127 || *mptr
== '\"')
3303 * Truncate the make and model on the first bad character...
3314 * Strip trailing whitespace...
3327 * Use a default make and model if nothing remains...
3330 cupsCopyString(make
, "Unknown", sizeof(make
));
3336 * Use a default make and model...
3339 cupsCopyString(make
, "Unknown", sizeof(make
));
3342 if (!_cups_strncasecmp(make
, "Hewlett Packard ", 16) || !_cups_strncasecmp(make
, "Hewlett-Packard ", 16))
3345 * Normalize HP printer make and model...
3349 cupsCopyString(make
, "HP", sizeof(make
));
3351 if (!_cups_strncasecmp(model
, "HP ", 3))
3354 else if ((mptr
= strchr(make
, ' ')) != NULL
)
3357 * Separate "MAKE MODEL"...
3360 while (*mptr
&& *mptr
== ' ')
3368 * No separate model name...
3375 * Standard stuff for PPD file...
3378 cupsFilePuts(fp
, "*PPD-Adobe: \"4.3\"\n");
3379 cupsFilePuts(fp
, "*FormatVersion: \"4.3\"\n");
3380 cupsFilePrintf(fp
, "*FileVersion: \"%d.%d\"\n", CUPS_VERSION_MAJOR
, CUPS_VERSION_MINOR
);
3381 cupsFilePuts(fp
, "*LanguageVersion: English\n");
3382 cupsFilePuts(fp
, "*LanguageEncoding: ISOLatin1\n");
3383 cupsFilePuts(fp
, "*PSVersion: \"(3010.000) 0\"\n");
3384 cupsFilePuts(fp
, "*LanguageLevel: \"3\"\n");
3385 cupsFilePuts(fp
, "*FileSystem: False\n");
3386 cupsFilePuts(fp
, "*PCFileName: \"ippeve.ppd\"\n");
3387 cupsFilePrintf(fp
, "*Manufacturer: \"%s\"\n", make
);
3388 cupsFilePrintf(fp
, "*ModelName: \"%s\"\n", model
);
3389 cupsFilePrintf(fp
, "*Product: \"(%s)\"\n", model
);
3390 cupsFilePrintf(fp
, "*NickName: \"%s - IPP Everywhere\"\n", model
);
3391 cupsFilePrintf(fp
, "*ShortNickName: \"%s - IPP Everywhere\"\n", model
);
3393 if (ippGetBoolean(ippFindAttribute(supported
, "color-supported", IPP_TAG_BOOLEAN
), 0))
3394 cupsFilePuts(fp
, "*ColorDevice: True\n");
3396 cupsFilePuts(fp
, "*ColorDevice: False\n");
3398 cupsFilePrintf(fp
, "*cupsVersion: %d.%d\n", CUPS_VERSION_MAJOR
, CUPS_VERSION_MINOR
);
3400 cupsFilePrintf(fp
, "*APAirPrint: True\n");
3402 cupsFilePuts(fp
, "*cupsSNMPSupplies: False\n");
3404 if ((lang_supp
= ippFindAttribute(supported
, "printer-strings-languages-supported", IPP_TAG_LANGUAGE
)) != NULL
)
3407 * List supported languages...
3410 for (i
= 0, count
= ippGetCount(lang_supp
), prefix
= "*cupsLanguages: \""; i
< count
; i
++)
3412 keyword
= ippGetString(lang_supp
, i
, NULL
);
3414 if ((lang
= cups_get_strings(&http
, printer_uri
, keyword
)) != NULL
)
3423 cupsFilePrintf(fp
, "%s%s", prefix
, keyword
);
3428 if (!strcmp(prefix
, " "))
3429 cupsFilePuts(fp
, "\"\n");
3438 // Find the English localization...
3439 for (lang
= langs
; lang
; lang
= lang
->next
)
3441 if (!strcmp(lang
->language
, "en"))
3445 base_lang
= cupsLangGet("en");
3447 if ((attr
= ippFindAttribute(supported
, "printer-more-info", IPP_TAG_URI
)) != NULL
&& ippValidateAttribute(attr
))
3448 cupsFilePrintf(fp
, "*APSupplies: \"%s\"\n", ippGetString(attr
, 0, NULL
));
3450 if ((attr
= ippFindAttribute(supported
, "printer-charge-info-uri", IPP_TAG_URI
)) != NULL
&& ippValidateAttribute(attr
))
3451 cupsFilePrintf(fp
, "*cupsChargeInfoURI: \"%s\"\n", ippGetString(attr
, 0, NULL
));
3457 if (ippGetBoolean(ippFindAttribute(supported
, "job-account-id-supported", IPP_TAG_BOOLEAN
), 0))
3458 cupsFilePuts(fp
, "*cupsJobAccountId: True\n");
3460 if (ippGetBoolean(ippFindAttribute(supported
, "job-accounting-user-id-supported", IPP_TAG_BOOLEAN
), 0))
3461 cupsFilePuts(fp
, "*cupsJobAccountingUserId: True\n");
3463 if ((attr
= ippFindAttribute(supported
, "printer-privacy-policy-uri", IPP_TAG_URI
)) != NULL
&& ippValidateAttribute(attr
))
3464 cupsFilePrintf(fp
, "*cupsPrivacyURI: \"%s\"\n", ippGetString(attr
, 0, NULL
));
3466 if ((attr
= ippFindAttribute(supported
, "printer-mandatory-job-attributes", IPP_TAG_KEYWORD
)) != NULL
&& ippValidateAttribute(attr
))
3468 for (i
= 0, count
= ippGetCount(attr
), prefix
= "*cupsMandatory: \""; i
< count
; i
++)
3470 keyword
= ippGetString(attr
, i
, NULL
);
3472 if (strcmp(keyword
, "attributes-charset") && strcmp(keyword
, "attributes-natural-language") && strcmp(keyword
, "printer-uri"))
3474 cupsFilePrintf(fp
, "%s%s", prefix
, keyword
);
3479 if (!strcmp(prefix
, " "))
3480 cupsFilePuts(fp
, "\"\n");
3483 if ((attr
= ippFindAttribute(supported
, "printer-requested-job-attributes", IPP_TAG_KEYWORD
)) != NULL
&& ippValidateAttribute(attr
))
3485 for (i
= 0, count
= ippGetCount(attr
), prefix
= "*cupsRequested: \""; i
< count
; i
++)
3487 keyword
= ippGetString(attr
, i
, NULL
);
3489 if (strcmp(keyword
, "attributes-charset") && strcmp(keyword
, "attributes-natural-language") && strcmp(keyword
, "printer-uri"))
3491 cupsFilePrintf(fp
, "%s%s", prefix
, keyword
);
3496 if (!strcmp(prefix
, " "))
3497 cupsFilePuts(fp
, "\"\n");
3501 * Password/PIN printing...
3504 if ((attr
= ippFindAttribute(supported
, "job-password-supported", IPP_TAG_INTEGER
)) != NULL
)
3506 char pattern
[33]; /* Password pattern */
3507 int maxlen
= ippGetInteger(attr
, 0);
3508 /* Maximum length */
3509 const char *repertoire
= ippGetString(ippFindAttribute(supported
, "job-password-repertoire-configured", IPP_TAG_KEYWORD
), 0, NULL
);
3510 /* Type of password */
3512 if (maxlen
> (int)(sizeof(pattern
) - 1))
3513 maxlen
= (int)sizeof(pattern
) - 1;
3515 if (!repertoire
|| !strcmp(repertoire
, "iana_us-ascii_digits"))
3516 memset(pattern
, '1', (size_t)maxlen
);
3517 else if (!strcmp(repertoire
, "iana_us-ascii_letters"))
3518 memset(pattern
, 'A', (size_t)maxlen
);
3519 else if (!strcmp(repertoire
, "iana_us-ascii_complex"))
3520 memset(pattern
, 'C', (size_t)maxlen
);
3521 else if (!strcmp(repertoire
, "iana_us-ascii_any"))
3522 memset(pattern
, '.', (size_t)maxlen
);
3523 else if (!strcmp(repertoire
, "iana_utf-8_digits"))
3524 memset(pattern
, 'N', (size_t)maxlen
);
3525 else if (!strcmp(repertoire
, "iana_utf-8_letters"))
3526 memset(pattern
, 'U', (size_t)maxlen
);
3528 memset(pattern
, '*', (size_t)maxlen
);
3530 pattern
[maxlen
] = '\0';
3532 cupsFilePrintf(fp
, "*cupsJobPassword: \"%s\"\n", pattern
);
3539 if ((attr
= ippFindAttribute(supported
, "document-format-supported", IPP_TAG_MIMETYPE
)) != NULL
)
3541 is_apple
= ippContainsString(attr
, "image/urf") && (ippFindAttribute(supported
, "urf-supported", IPP_TAG_KEYWORD
) != NULL
);
3542 is_pdf
= ippContainsString(attr
, "application/pdf");
3543 is_pwg
= ippContainsString(attr
, "image/pwg-raster") && !is_apple
&&
3544 (ippFindAttribute(supported
, "pwg-raster-document-resolution-supported", IPP_TAG_RESOLUTION
) != NULL
) &&
3545 (ippFindAttribute(supported
, "pwg-raster-document-type-supported", IPP_TAG_KEYWORD
) != NULL
);
3547 if (ippContainsString(attr
, "image/jpeg"))
3548 cupsFilePuts(fp
, "*cupsFilter2: \"image/jpeg image/jpeg 0 -\"\n");
3549 if (ippContainsString(attr
, "image/png"))
3550 cupsFilePuts(fp
, "*cupsFilter2: \"image/png image/png 0 -\"\n");
3554 * Don't locally filter PDF content when printing to a CUPS shared
3555 * printer, otherwise the options will be applied twice...
3558 if (ippContainsString(attr
, "application/vnd.cups-pdf"))
3559 cupsFilePuts(fp
, "*cupsFilter2: \"application/pdf application/pdf 0 -\"\n");
3561 cupsFilePuts(fp
, "*cupsFilter2: \"application/vnd.cups-pdf application/pdf 10 -\"\n");
3564 cupsFilePuts(fp
, "*cupsManualCopies: True\n");
3566 cupsFilePuts(fp
, "*cupsFilter2: \"image/urf image/urf 100 -\"\n");
3568 cupsFilePuts(fp
, "*cupsFilter2: \"image/pwg-raster image/pwg-raster 100 -\"\n");
3571 if (!is_apple
&& !is_pdf
&& !is_pwg
)
3575 * PageSize/PageRegion/ImageableArea/PaperDimension
3578 if ((attr
= ippFindAttribute(supported
, "media-bottom-margin-supported", IPP_TAG_INTEGER
)) != NULL
)
3580 for (i
= 1, bottom
= ippGetInteger(attr
, 0), count
= ippGetCount(attr
); i
< count
; i
++)
3581 if (ippGetInteger(attr
, i
) > bottom
)
3582 bottom
= ippGetInteger(attr
, i
);
3587 if ((attr
= ippFindAttribute(supported
, "media-left-margin-supported", IPP_TAG_INTEGER
)) != NULL
)
3589 for (i
= 1, left
= ippGetInteger(attr
, 0), count
= ippGetCount(attr
); i
< count
; i
++)
3590 if (ippGetInteger(attr
, i
) > left
)
3591 left
= ippGetInteger(attr
, i
);
3596 if ((attr
= ippFindAttribute(supported
, "media-right-margin-supported", IPP_TAG_INTEGER
)) != NULL
)
3598 for (i
= 1, right
= ippGetInteger(attr
, 0), count
= ippGetCount(attr
); i
< count
; i
++)
3599 if (ippGetInteger(attr
, i
) > right
)
3600 right
= ippGetInteger(attr
, i
);
3605 if ((attr
= ippFindAttribute(supported
, "media-top-margin-supported", IPP_TAG_INTEGER
)) != NULL
)
3607 for (i
= 1, top
= ippGetInteger(attr
, 0), count
= ippGetCount(attr
); i
< count
; i
++)
3608 if (ippGetInteger(attr
, i
) > top
)
3609 top
= ippGetInteger(attr
, i
);
3614 if ((defattr
= ippFindAttribute(supported
, "media-col-default", IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
3616 if ((attr
= ippFindAttribute(ippGetCollection(defattr
, 0), "media-size", IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
3618 media_size
= ippGetCollection(attr
, 0);
3619 x_dim
= ippFindAttribute(media_size
, "x-dimension", IPP_TAG_INTEGER
);
3620 y_dim
= ippFindAttribute(media_size
, "y-dimension", IPP_TAG_INTEGER
);
3622 if (x_dim
&& y_dim
&& (pwg
= pwgMediaForSize(ippGetInteger(x_dim
, 0), ippGetInteger(y_dim
, 0))) != NULL
)
3623 cupsCopyString(ppdname
, pwg
->ppd
, sizeof(ppdname
));
3625 cupsCopyString(ppdname
, "Unknown", sizeof(ppdname
));
3628 cupsCopyString(ppdname
, "Unknown", sizeof(ppdname
));
3630 else if ((pwg
= pwgMediaForPWG(ippGetString(ippFindAttribute(supported
, "media-default", IPP_TAG_ZERO
), 0, NULL
))) != NULL
)
3631 cupsCopyString(ppdname
, pwg
->ppd
, sizeof(ppdname
));
3633 cupsCopyString(ppdname
, "Unknown", sizeof(ppdname
));
3635 sizes
= cupsArrayNew3((cups_array_func_t
)pwg_compare_sizes
, NULL
, NULL
, 0, (cups_acopy_func_t
)pwg_copy_size
, _cupsArrayFree
);
3637 if ((attr
= ippFindAttribute(supported
, "media-col-database", IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
3639 for (i
= 0, count
= ippGetCount(attr
); i
< count
; i
++)
3641 cups_size_t temp
; /* Current size */
3642 ipp_attribute_t
*margin
; /* media-xxx-margin attribute */
3644 media_col
= ippGetCollection(attr
, i
);
3645 media_size
= ippGetCollection(ippFindAttribute(media_col
, "media-size", IPP_TAG_BEGIN_COLLECTION
), 0);
3646 x_dim
= ippFindAttribute(media_size
, "x-dimension", IPP_TAG_ZERO
);
3647 y_dim
= ippFindAttribute(media_size
, "y-dimension", IPP_TAG_ZERO
);
3648 pwg
= pwgMediaForSize(ippGetInteger(x_dim
, 0), ippGetInteger(y_dim
, 0));
3652 temp
.width
= pwg
->width
;
3653 temp
.length
= pwg
->length
;
3655 if ((margin
= ippFindAttribute(media_col
, "media-bottom-margin", IPP_TAG_INTEGER
)) != NULL
)
3656 temp
.bottom
= ippGetInteger(margin
, 0);
3658 temp
.bottom
= bottom
;
3660 if ((margin
= ippFindAttribute(media_col
, "media-left-margin", IPP_TAG_INTEGER
)) != NULL
)
3661 temp
.left
= ippGetInteger(margin
, 0);
3665 if ((margin
= ippFindAttribute(media_col
, "media-right-margin", IPP_TAG_INTEGER
)) != NULL
)
3666 temp
.right
= ippGetInteger(margin
, 0);
3670 if ((margin
= ippFindAttribute(media_col
, "media-top-margin", IPP_TAG_INTEGER
)) != NULL
)
3671 temp
.top
= ippGetInteger(margin
, 0);
3675 if (temp
.bottom
== 0 && temp
.left
== 0 && temp
.right
== 0 && temp
.top
== 0)
3676 snprintf(temp
.media
, sizeof(temp
.media
), "%s.Borderless", pwg
->ppd
);
3678 cupsCopyString(temp
.media
, pwg
->ppd
, sizeof(temp
.media
));
3680 if (!cupsArrayFind(sizes
, &temp
))
3681 cupsArrayAdd(sizes
, &temp
);
3683 else if (ippGetValueTag(x_dim
) == IPP_TAG_RANGE
|| ippGetValueTag(y_dim
) == IPP_TAG_RANGE
)
3686 * Custom size - record the min/max values...
3689 int lower
, upper
; /* Range values */
3691 if (ippGetValueTag(x_dim
) == IPP_TAG_RANGE
)
3692 lower
= ippGetRange(x_dim
, 0, &upper
);
3694 lower
= upper
= ippGetInteger(x_dim
, 0);
3696 if (lower
< min_width
)
3698 if (upper
> max_width
)
3701 if (ippGetValueTag(y_dim
) == IPP_TAG_RANGE
)
3702 lower
= ippGetRange(y_dim
, 0, &upper
);
3704 lower
= upper
= ippGetInteger(y_dim
, 0);
3706 if (lower
< min_length
)
3708 if (upper
> max_length
)
3713 if ((max_width
== 0 || max_length
== 0) && (attr
= ippFindAttribute(supported
, "media-size-supported", IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
3716 * Some printers don't list custom size support in media-col-database...
3719 for (i
= 0, count
= ippGetCount(attr
); i
< count
; i
++)
3721 media_size
= ippGetCollection(attr
, i
);
3722 x_dim
= ippFindAttribute(media_size
, "x-dimension", IPP_TAG_ZERO
);
3723 y_dim
= ippFindAttribute(media_size
, "y-dimension", IPP_TAG_ZERO
);
3725 if (ippGetValueTag(x_dim
) == IPP_TAG_RANGE
|| ippGetValueTag(y_dim
) == IPP_TAG_RANGE
)
3728 * Custom size - record the min/max values...
3731 int lower
, upper
; /* Range values */
3733 if (ippGetValueTag(x_dim
) == IPP_TAG_RANGE
)
3734 lower
= ippGetRange(x_dim
, 0, &upper
);
3736 lower
= upper
= ippGetInteger(x_dim
, 0);
3738 if (lower
< min_width
)
3740 if (upper
> max_width
)
3743 if (ippGetValueTag(y_dim
) == IPP_TAG_RANGE
)
3744 lower
= ippGetRange(y_dim
, 0, &upper
);
3746 lower
= upper
= ippGetInteger(y_dim
, 0);
3748 if (lower
< min_length
)
3750 if (upper
> max_length
)
3756 else if ((attr
= ippFindAttribute(supported
, "media-size-supported", IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
3758 for (i
= 0, count
= ippGetCount(attr
); i
< count
; i
++)
3760 cups_size_t temp
; /* Current size */
3762 media_size
= ippGetCollection(attr
, i
);
3763 x_dim
= ippFindAttribute(media_size
, "x-dimension", IPP_TAG_ZERO
);
3764 y_dim
= ippFindAttribute(media_size
, "y-dimension", IPP_TAG_ZERO
);
3765 pwg
= pwgMediaForSize(ippGetInteger(x_dim
, 0), ippGetInteger(y_dim
, 0));
3769 temp
.width
= pwg
->width
;
3770 temp
.length
= pwg
->length
;
3771 temp
.bottom
= bottom
;
3776 if (temp
.bottom
== 0 && temp
.left
== 0 && temp
.right
== 0 && temp
.top
== 0)
3777 snprintf(temp
.media
, sizeof(temp
.media
), "%s.Borderless", pwg
->ppd
);
3779 cupsCopyString(temp
.media
, pwg
->ppd
, sizeof(temp
.media
));
3781 if (!cupsArrayFind(sizes
, &temp
))
3782 cupsArrayAdd(sizes
, &temp
);
3784 else if (ippGetValueTag(x_dim
) == IPP_TAG_RANGE
|| ippGetValueTag(y_dim
) == IPP_TAG_RANGE
)
3787 * Custom size - record the min/max values...
3790 int lower
, upper
; /* Range values */
3792 if (ippGetValueTag(x_dim
) == IPP_TAG_RANGE
)
3793 lower
= ippGetRange(x_dim
, 0, &upper
);
3795 lower
= upper
= ippGetInteger(x_dim
, 0);
3797 if (lower
< min_width
)
3799 if (upper
> max_width
)
3802 if (ippGetValueTag(y_dim
) == IPP_TAG_RANGE
)
3803 lower
= ippGetRange(y_dim
, 0, &upper
);
3805 lower
= upper
= ippGetInteger(y_dim
, 0);
3807 if (lower
< min_length
)
3809 if (upper
> max_length
)
3814 else if ((attr
= ippFindAttribute(supported
, "media-supported", IPP_TAG_ZERO
)) != NULL
)
3816 for (i
= 0, count
= ippGetCount(attr
); i
< count
; i
++)
3818 const char *pwg_size
= ippGetString(attr
, i
, NULL
);
3820 cups_size_t temp
; /* Current size */
3822 if ((pwg
= pwgMediaForPWG(pwg_size
)) != NULL
)
3824 if (strstr(pwg_size
, "_max_") || strstr(pwg_size
, "_max."))
3826 if (pwg
->width
> max_width
)
3827 max_width
= pwg
->width
;
3828 if (pwg
->length
> max_length
)
3829 max_length
= pwg
->length
;
3831 else if (strstr(pwg_size
, "_min_") || strstr(pwg_size
, "_min."))
3833 if (pwg
->width
< min_width
)
3834 min_width
= pwg
->width
;
3835 if (pwg
->length
< min_length
)
3836 min_length
= pwg
->length
;
3840 temp
.width
= pwg
->width
;
3841 temp
.length
= pwg
->length
;
3842 temp
.bottom
= bottom
;
3847 if (temp
.bottom
== 0 && temp
.left
== 0 && temp
.right
== 0 && temp
.top
== 0)
3848 snprintf(temp
.media
, sizeof(temp
.media
), "%s.Borderless", pwg
->ppd
);
3850 cupsCopyString(temp
.media
, pwg
->ppd
, sizeof(temp
.media
));
3852 if (!cupsArrayFind(sizes
, &temp
))
3853 cupsArrayAdd(sizes
, &temp
);
3859 if (cupsArrayCount(sizes
) > 0)
3862 * List all of the standard sizes...
3865 char tleft
[256], /* Left string */
3866 tbottom
[256], /* Bottom string */
3867 tright
[256], /* Right string */
3868 ttop
[256], /* Top string */
3869 twidth
[256], /* Width string */
3870 tlength
[256]; /* Length string */
3872 cupsFilePrintf(fp
, "*OpenUI *PageSize: PickOne\n"
3873 "*OrderDependency: 10 AnySetup *PageSize\n"
3874 "*DefaultPageSize: %s\n", ppdname
);
3875 for (size
= (cups_size_t
*)cupsArrayFirst(sizes
); size
; size
= (cups_size_t
*)cupsArrayNext(sizes
))
3877 _cupsStrFormatd(twidth
, twidth
+ sizeof(twidth
), size
->width
* 72.0 / 2540.0, loc
);
3878 _cupsStrFormatd(tlength
, tlength
+ sizeof(tlength
), size
->length
* 72.0 / 2540.0, loc
);
3880 cupsFilePrintf(fp
, "*PageSize %s: \"<</PageSize[%s %s]>>setpagedevice\"\n", size
->media
, twidth
, tlength
);
3882 cupsFilePuts(fp
, "*CloseUI: *PageSize\n");
3884 cupsFilePrintf(fp
, "*OpenUI *PageRegion: PickOne\n"
3885 "*OrderDependency: 10 AnySetup *PageRegion\n"
3886 "*DefaultPageRegion: %s\n", ppdname
);
3887 for (size
= (cups_size_t
*)cupsArrayFirst(sizes
); size
; size
= (cups_size_t
*)cupsArrayNext(sizes
))
3889 _cupsStrFormatd(twidth
, twidth
+ sizeof(twidth
), size
->width
* 72.0 / 2540.0, loc
);
3890 _cupsStrFormatd(tlength
, tlength
+ sizeof(tlength
), size
->length
* 72.0 / 2540.0, loc
);
3892 cupsFilePrintf(fp
, "*PageRegion %s: \"<</PageSize[%s %s]>>setpagedevice\"\n", size
->media
, twidth
, tlength
);
3894 cupsFilePuts(fp
, "*CloseUI: *PageRegion\n");
3896 cupsFilePrintf(fp
, "*DefaultImageableArea: %s\n"
3897 "*DefaultPaperDimension: %s\n", ppdname
, ppdname
);
3899 for (size
= (cups_size_t
*)cupsArrayFirst(sizes
); size
; size
= (cups_size_t
*)cupsArrayNext(sizes
))
3901 _cupsStrFormatd(tleft
, tleft
+ sizeof(tleft
), size
->left
* 72.0 / 2540.0, loc
);
3902 _cupsStrFormatd(tbottom
, tbottom
+ sizeof(tbottom
), size
->bottom
* 72.0 / 2540.0, loc
);
3903 _cupsStrFormatd(tright
, tright
+ sizeof(tright
), (size
->width
- size
->right
) * 72.0 / 2540.0, loc
);
3904 _cupsStrFormatd(ttop
, ttop
+ sizeof(ttop
), (size
->length
- size
->top
) * 72.0 / 2540.0, loc
);
3905 _cupsStrFormatd(twidth
, twidth
+ sizeof(twidth
), size
->width
* 72.0 / 2540.0, loc
);
3906 _cupsStrFormatd(tlength
, tlength
+ sizeof(tlength
), size
->length
* 72.0 / 2540.0, loc
);
3908 cupsFilePrintf(fp
, "*ImageableArea %s: \"%s %s %s %s\"\n", size
->media
, tleft
, tbottom
, tright
, ttop
);
3909 cupsFilePrintf(fp
, "*PaperDimension %s: \"%s %s\"\n", size
->media
, twidth
, tlength
);
3912 cupsArrayDelete(sizes
);
3915 * Custom size support...
3918 if (max_width
> 0 && min_width
< INT_MAX
&& max_length
> 0 && min_length
< INT_MAX
)
3920 char tmax
[256], tmin
[256]; /* Min/max values */
3922 _cupsStrFormatd(tleft
, tleft
+ sizeof(tleft
), left
* 72.0 / 2540.0, loc
);
3923 _cupsStrFormatd(tbottom
, tbottom
+ sizeof(tbottom
), bottom
* 72.0 / 2540.0, loc
);
3924 _cupsStrFormatd(tright
, tright
+ sizeof(tright
), right
* 72.0 / 2540.0, loc
);
3925 _cupsStrFormatd(ttop
, ttop
+ sizeof(ttop
), top
* 72.0 / 2540.0, loc
);
3927 cupsFilePrintf(fp
, "*HWMargins: \"%s %s %s %s\"\n", tleft
, tbottom
, tright
, ttop
);
3929 _cupsStrFormatd(tmax
, tmax
+ sizeof(tmax
), max_width
* 72.0 / 2540.0, loc
);
3930 _cupsStrFormatd(tmin
, tmin
+ sizeof(tmin
), min_width
* 72.0 / 2540.0, loc
);
3931 cupsFilePrintf(fp
, "*ParamCustomPageSize Width: 1 points %s %s\n", tmin
, tmax
);
3933 _cupsStrFormatd(tmax
, tmax
+ sizeof(tmax
), max_length
* 72.0 / 2540.0, loc
);
3934 _cupsStrFormatd(tmin
, tmin
+ sizeof(tmin
), min_length
* 72.0 / 2540.0, loc
);
3935 cupsFilePrintf(fp
, "*ParamCustomPageSize Height: 2 points %s %s\n", tmin
, tmax
);
3937 cupsFilePuts(fp
, "*ParamCustomPageSize WidthOffset: 3 points 0 0\n");
3938 cupsFilePuts(fp
, "*ParamCustomPageSize HeightOffset: 4 points 0 0\n");
3939 cupsFilePuts(fp
, "*ParamCustomPageSize Orientation: 5 int 0 3\n");
3940 cupsFilePuts(fp
, "*CustomPageSize True: \"pop pop pop <</PageSize[5 -2 roll]/ImagingBBox null>>setpagedevice\"\n");
3945 cupsArrayDelete(sizes
);
3953 if ((attr
= ippFindAttribute(ippGetCollection(defattr
, 0), "media-source", IPP_TAG_ZERO
)) != NULL
)
3954 pwg_ppdize_name(ippGetString(attr
, 0, NULL
), ppdname
, sizeof(ppdname
));
3958 if ((attr
= ippFindAttribute(supported
, "media-source-supported", IPP_TAG_ZERO
)) != NULL
&& (count
= ippGetCount(attr
)) > 1)
3960 int have_default
= ppdname
[0] != '\0';
3961 /* Do we have a default InputSlot? */
3962 static const char * const sources
[] =
3963 { /* Standard "media-source" strings */
4016 cupsFilePuts(fp
, "*OpenUI *InputSlot: PickOne\n"
4017 "*OrderDependency: 10 AnySetup *InputSlot\n");
4019 cupsFilePrintf(fp
, "*DefaultInputSlot: %s\n", ppdname
);
4021 for (i
= 0; i
< count
; i
++)
4023 keyword
= ippGetString(attr
, i
, NULL
);
4025 pwg_ppdize_name(keyword
, ppdname
, sizeof(ppdname
));
4027 if (i
== 0 && !have_default
)
4028 cupsFilePrintf(fp
, "*DefaultInputSlot: %s\n", ppdname
);
4030 for (j
= 0; j
< (int)(sizeof(sources
) / sizeof(sources
[0])); j
++)
4032 if (!strcmp(sources
[j
], keyword
))
4034 snprintf(msgid
, sizeof(msgid
), "media-source.%s", keyword
);
4036 cupsFilePrintf(fp
, "*InputSlot %s/%s: \"<</MediaPosition %d>>setpagedevice\"\n", ppdname
, ppd_get_string(base_lang
, lang
, msgid
, ppdtext
, sizeof(ppdtext
)), j
);
4037 ppd_put_strings(fp
, langs
, "InputSlot", ppdname
, msgid
);
4042 cupsFilePuts(fp
, "*CloseUI: *InputSlot\n");
4049 if ((attr
= ippFindAttribute(ippGetCollection(defattr
, 0), "media-type", IPP_TAG_ZERO
)) != NULL
)
4050 pwg_ppdize_name(ippGetString(attr
, 0, NULL
), ppdname
, sizeof(ppdname
));
4052 cupsCopyString(ppdname
, "Unknown", sizeof(ppdname
));
4054 if ((attr
= ippFindAttribute(supported
, "media-type-supported", IPP_TAG_ZERO
)) != NULL
&& (count
= ippGetCount(attr
)) > 1)
4056 cupsFilePrintf(fp
, "*OpenUI *MediaType: PickOne\n"
4057 "*OrderDependency: 10 AnySetup *MediaType\n"
4058 "*DefaultMediaType: %s\n", ppdname
);
4059 for (i
= 0; i
< count
; i
++)
4061 keyword
= ippGetString(attr
, i
, NULL
);
4063 pwg_ppdize_name(keyword
, ppdname
, sizeof(ppdname
));
4065 snprintf(msgid
, sizeof(msgid
), "media-type.%s", keyword
);
4067 cupsFilePrintf(fp
, "*MediaType %s/%s: \"<</MediaType(%s)>>setpagedevice\"\n", ppdname
, ppd_get_string(base_lang
, lang
, msgid
, ppdtext
, sizeof(ppdtext
)), keyword
);
4068 ppd_put_strings(fp
, langs
, "MediaType", ppdname
, msgid
);
4070 cupsFilePuts(fp
, "*CloseUI: *MediaType\n");
4074 * cupsPrintQuality and DefaultResolution...
4077 quality
= ippFindAttribute(supported
, "print-quality-supported", IPP_TAG_ENUM
);
4079 if ((attr
= ippFindAttribute(supported
, "urf-supported", IPP_TAG_KEYWORD
)) != NULL
)
4081 int lowdpi
= 0, hidpi
= 0; /* Lower and higher resolution */
4083 for (i
= 0, count
= ippGetCount(attr
); i
< count
; i
++)
4085 const char *rs
= ippGetString(attr
, i
, NULL
);
4088 if (_cups_strncasecmp(rs
, "RS", 2))
4091 lowdpi
= atoi(rs
+ 2);
4092 if ((rs
= strrchr(rs
, '-')) != NULL
)
4093 hidpi
= atoi(rs
+ 1);
4102 * Invalid "urf-supported" value...
4110 * Generate print qualities based on low and high DPIs...
4113 cupsFilePrintf(fp
, "*DefaultResolution: %ddpi\n", lowdpi
);
4115 cupsFilePuts(fp
, "*OpenUI *cupsPrintQuality: PickOne\n"
4116 "*OrderDependency: 10 AnySetup *cupsPrintQuality\n"
4117 "*DefaultcupsPrintQuality: Normal\n");
4118 if ((lowdpi
& 1) == 0)
4120 cupsFilePrintf(fp
, "*cupsPrintQuality Draft: \"<</HWResolution[%d %d]>>setpagedevice\"\n", lowdpi
, lowdpi
/ 2);
4123 else if (ippContainsInteger(quality
, IPP_QUALITY_DRAFT
))
4125 cupsFilePrintf(fp
, "*cupsPrintQuality Draft: \"<</HWResolution[%d %d]>>setpagedevice\"\n*", lowdpi
, lowdpi
);
4129 cupsFilePrintf(fp
, "*cupsPrintQuality Normal: \"<</HWResolution[%d %d]>>setpagedevice\"\n", lowdpi
, lowdpi
);
4131 if (hidpi
> lowdpi
|| ippContainsInteger(quality
, IPP_QUALITY_HIGH
))
4133 cupsFilePrintf(fp
, "*cupsPrintQuality High: \"<</HWResolution[%d %d]>>setpagedevice\"\n", hidpi
, hidpi
);
4137 cupsFilePuts(fp
, "*CloseUI: *cupsPrintQuality\n");
4140 else if ((attr
= ippFindAttribute(supported
, "pwg-raster-document-resolution-supported", IPP_TAG_RESOLUTION
)) != NULL
)
4143 * Make a sorted list of resolutions.
4146 count
= ippGetCount(attr
);
4147 if (count
> (int)(sizeof(resolutions
) / sizeof(resolutions
[0])))
4148 count
= (int)(sizeof(resolutions
) / sizeof(resolutions
[0]));
4150 resolutions
[0] = 0; /* Not in loop to silence Clang static analyzer... */
4151 for (i
= 1; i
< count
; i
++)
4154 for (i
= 0; i
< (count
- 1); i
++)
4156 for (j
= i
+ 1; j
< count
; j
++)
4158 int ix
, iy
, /* First X and Y resolution */
4159 jx
, jy
, /* Second X and Y resolution */
4160 temp
; /* Swap variable */
4161 ipp_res_t units
; /* Resolution units */
4163 ix
= ippGetResolution(attr
, resolutions
[i
], &iy
, &units
);
4164 jx
= ippGetResolution(attr
, resolutions
[j
], &jy
, &units
);
4166 if (ix
> jx
|| (ix
== jx
&& iy
> jy
))
4169 * Swap these two resolutions...
4172 temp
= resolutions
[i
];
4173 resolutions
[i
] = resolutions
[j
];
4174 resolutions
[j
] = temp
;
4180 * Generate print quality options...
4183 pwg_ppdize_resolution(attr
, resolutions
[count
/ 2], &xres
, &yres
, ppdname
, sizeof(ppdname
));
4184 cupsFilePrintf(fp
, "*DefaultResolution: %s\n", ppdname
);
4186 cupsFilePuts(fp
, "*OpenUI *cupsPrintQuality: PickOne\n"
4187 "*OrderDependency: 10 AnySetup *cupsPrintQuality\n"
4188 "*DefaultcupsPrintQuality: Normal\n");
4189 if (count
> 2 || ippContainsInteger(quality
, IPP_QUALITY_DRAFT
))
4191 pwg_ppdize_resolution(attr
, resolutions
[0], &xres
, &yres
, NULL
, 0);
4192 cupsFilePrintf(fp
, "*cupsPrintQuality Draft: \"<</HWResolution[%d %d]>>setpagedevice\"\n", xres
, yres
);
4196 pwg_ppdize_resolution(attr
, resolutions
[count
/ 2], &xres
, &yres
, NULL
, 0);
4197 cupsFilePrintf(fp
, "*cupsPrintQuality Normal: \"<</HWResolution[%d %d]>>setpagedevice\"\n", xres
, yres
);
4199 if (count
> 1 || ippContainsInteger(quality
, IPP_QUALITY_HIGH
))
4201 pwg_ppdize_resolution(attr
, resolutions
[count
- 1], &xres
, &yres
, NULL
, 0);
4202 cupsFilePrintf(fp
, "*cupsPrintQuality High: \"<</HWResolution[%d %d]>>setpagedevice\"\n", xres
, yres
);
4206 cupsFilePuts(fp
, "*CloseUI: *cupsPrintQuality\n");
4208 else if (is_apple
|| is_pwg
)
4214 if ((attr
= ippFindAttribute(supported
, "printer-resolution-default", IPP_TAG_RESOLUTION
)) != NULL
)
4216 pwg_ppdize_resolution(attr
, 0, &xres
, &yres
, ppdname
, sizeof(ppdname
));
4221 cupsCopyString(ppdname
, "300dpi", sizeof(ppdname
));
4224 cupsFilePrintf(fp
, "*DefaultResolution: %s\n", ppdname
);
4226 cupsFilePuts(fp
, "*OpenUI *cupsPrintQuality: PickOne\n"
4227 "*OrderDependency: 10 AnySetup *cupsPrintQuality\n"
4228 "*DefaultcupsPrintQuality: Normal\n");
4229 if (ippContainsInteger(quality
, IPP_QUALITY_DRAFT
))
4231 cupsFilePrintf(fp
, "*cupsPrintQuality Draft: \"<</HWResolution[%d %d]>>setpagedevice\"\n*", xres
, yres
);
4235 cupsFilePrintf(fp
, "*cupsPrintQuality Normal: \"<</HWResolution[%d %d]>>setpagedevice\"\n", xres
, yres
);
4237 if (ippContainsInteger(quality
, IPP_QUALITY_HIGH
))
4239 cupsFilePrintf(fp
, "*cupsPrintQuality High: \"<</HWResolution[%d %d]>>setpagedevice\"\n", xres
, yres
);
4242 cupsFilePuts(fp
, "*CloseUI: *cupsPrintQuality\n");
4249 if ((defattr
= ippFindAttribute(supported
, "print-color-mode-default", IPP_TAG_KEYWORD
)) == NULL
)
4250 defattr
= ippFindAttribute(supported
, "output-mode-default", IPP_TAG_KEYWORD
);
4252 if ((attr
= ippFindAttribute(supported
, "urf-supported", IPP_TAG_KEYWORD
)) == NULL
)
4253 if ((attr
= ippFindAttribute(supported
, "pwg-raster-document-type-supported", IPP_TAG_KEYWORD
)) == NULL
)
4254 if ((attr
= ippFindAttribute(supported
, "print-color-mode-supported", IPP_TAG_KEYWORD
)) == NULL
)
4255 attr
= ippFindAttribute(supported
, "output-mode-supported", IPP_TAG_KEYWORD
);
4259 int wrote_color
= 0;
4260 const char *default_color
= NULL
; /* Default */
4262 if ((keyword
= ippGetString(defattr
, 0, NULL
)) != NULL
&&
4263 strcmp(keyword
, "auto"))
4265 if (!strcmp(keyword
, "bi-level"))
4266 default_color
= "FastGray";
4267 else if (!strcmp(keyword
, "monochrome") || !strcmp(keyword
, "auto-monochrome"))
4268 default_color
= "Gray";
4270 default_color
= "RGB";
4273 cupsFilePrintf(fp
, "*%% ColorModel from %s\n", ippGetName(attr
));
4275 for (i
= 0, count
= ippGetCount(attr
); i
< count
; i
++)
4277 keyword
= ippGetString(attr
, i
, NULL
);
4279 #define PRINTF_COLORMODEL if (!wrote_color) { cupsFilePuts(fp, "*OpenUI *ColorModel: PickOne\n*OrderDependency: 10 AnySetup *ColorModel\n"); wrote_color = 1; }
4280 #define PRINTF_COLOROPTION(name,text,cspace,bpp) cupsFilePrintf(fp, "*ColorModel %s/%s: \"<</cupsColorSpace %d/cupsBitsPerColor %d/cupsColorOrder 0/cupsCompression 0>>setpagedevice\"\n", name, text, cspace, bpp);
4282 if (!strcasecmp(keyword
, "black_1") || !strcmp(keyword
, "bi-level") || !strcmp(keyword
, "process-bi-level"))
4286 PRINTF_COLOROPTION("FastGray", _("Fast Grayscale"), CUPS_CSPACE_K
, 1)
4289 default_color
= "FastGray";
4291 else if (!strcasecmp(keyword
, "sgray_8") || !strcmp(keyword
, "W8") || !strcmp(keyword
, "monochrome") || !strcmp(keyword
, "process-monochrome"))
4295 PRINTF_COLOROPTION("Gray", _("Grayscale"), CUPS_CSPACE_SW
, 8)
4297 if (!default_color
|| (!defattr
&& !strcmp(default_color
, "FastGray")))
4298 default_color
= "Gray";
4300 else if (!strcasecmp(keyword
, "sgray_16") || !strcmp(keyword
, "W8-16"))
4304 if (!strcmp(keyword
, "W8-16"))
4306 PRINTF_COLOROPTION("Gray", _("Grayscale"), CUPS_CSPACE_SW
, 8)
4308 if (!default_color
|| (!defattr
&& !strcmp(default_color
, "FastGray")))
4309 default_color
= "Gray";
4312 PRINTF_COLOROPTION("Gray16", _("Deep Gray"), CUPS_CSPACE_SW
, 16)
4314 else if (!strcasecmp(keyword
, "srgb_8") || !strncmp(keyword
, "SRGB24", 6) || !strcmp(keyword
, "color"))
4318 PRINTF_COLOROPTION("RGB", _("Color"), CUPS_CSPACE_SRGB
, 8)
4321 default_color
= "RGB";
4323 // Apparently some printers only advertise color support, so make sure
4324 // we also do grayscale for these printers...
4325 if (!ippContainsString(attr
, "sgray_8") && !ippContainsString(attr
, "black_1") && !ippContainsString(attr
, "black_8") && !ippContainsString(attr
, "W8") && !ippContainsString(attr
, "W8-16"))
4326 PRINTF_COLOROPTION("Gray", _("GrayScale"), CUPS_CSPACE_SW
, 8)
4328 else if (!strcasecmp(keyword
, "adobe-rgb_16") || !strcmp(keyword
, "ADOBERGB48") || !strcmp(keyword
, "ADOBERGB24-48"))
4332 PRINTF_COLOROPTION("AdobeRGB", _("Deep Color"), CUPS_CSPACE_ADOBERGB
, 16)
4335 default_color
= "AdobeRGB";
4337 else if ((!strcasecmp(keyword
, "adobe-rgb_8") && !ippContainsString(attr
, "adobe-rgb_16")) || !strcmp(keyword
, "ADOBERGB24"))
4341 PRINTF_COLOROPTION("AdobeRGB", _("Deep Color"), CUPS_CSPACE_ADOBERGB
, 8)
4344 default_color
= "AdobeRGB";
4346 else if ((!strcasecmp(keyword
, "black_8") && !ippContainsString(attr
, "black_16")) || !strcmp(keyword
, "DEVW8"))
4350 PRINTF_COLOROPTION("DeviceGray", _("Device Gray"), CUPS_CSPACE_W
, 8)
4352 else if (!strcasecmp(keyword
, "black_16") || !strcmp(keyword
, "DEVW16") || !strcmp(keyword
, "DEVW8-16"))
4356 PRINTF_COLOROPTION("DeviceGray", _("Device Gray"), CUPS_CSPACE_W
, 16)
4358 else if ((!strcasecmp(keyword
, "cmyk_8") && !ippContainsString(attr
, "cmyk_16")) || !strcmp(keyword
, "DEVCMYK32"))
4362 PRINTF_COLOROPTION("CMYK", _("Device CMYK"), CUPS_CSPACE_CMYK
, 8)
4364 else if (!strcasecmp(keyword
, "cmyk_16") || !strcmp(keyword
, "DEVCMYK32-64") || !strcmp(keyword
, "DEVCMYK64"))
4368 PRINTF_COLOROPTION("CMYK", _("Device CMYK"), CUPS_CSPACE_CMYK
, 16)
4370 else if ((!strcasecmp(keyword
, "rgb_8") && ippContainsString(attr
, "rgb_16")) || !strcmp(keyword
, "DEVRGB24"))
4374 PRINTF_COLOROPTION("DeviceRGB", _("Device RGB"), CUPS_CSPACE_RGB
, 8)
4376 else if (!strcasecmp(keyword
, "rgb_16") || !strcmp(keyword
, "DEVRGB24-48") || !strcmp(keyword
, "DEVRGB48"))
4380 PRINTF_COLOROPTION("DeviceRGB", _("Device RGB"), CUPS_CSPACE_RGB
, 16)
4385 cupsFilePrintf(fp
, "*DefaultColorModel: %s\n", default_color
);
4387 cupsFilePuts(fp
, "*CloseUI: *ColorModel\n");
4391 // Standard presets for color mode and quality...
4394 "*APPrinterPreset Gray_with_Paper_Auto-Detect_-_Draft/Draft B&W: \"\n"
4395 " *cupsPrintQuality Draft *ColorModel Gray\n"
4396 " com.apple.print.preset.graphicsType General\n"
4397 " com.apple.print.preset.quality low\n"
4398 " com.apple.print.preset.media-front-coating autodetect\n"
4399 " com.apple.print.preset.output-mode monochrome\"\n"
4402 "*APPrinterPreset Gray_with_Paper_Auto-Detect/Black and White: \"\n"
4403 " *cupsPrintQuality Normal *ColorModel Gray\n"
4404 " com.apple.print.preset.graphicsType General\n"
4405 " com.apple.print.preset.quality mid\n"
4406 " com.apple.print.preset.media-front-coating autodetect\n"
4407 " com.apple.print.preset.output-mode monochrome\"\n"
4409 if (strcmp(default_color
, "Gray"))
4411 "*APPrinterPreset Color_with_Paper_Auto-Detect/Color: \"\n"
4412 " *cupsPrintQuality Normal *ColorModel RGB\n"
4413 " com.apple.print.preset.graphicsType General\n"
4414 " com.apple.print.preset.quality mid\n"
4415 " com.apple.print.preset.media-front-coating autodetect\n"
4416 " com.apple.print.preset.output-mode color\"\n"
4418 if (!strcmp(default_color
, "AdobeRGB") || have_qhigh
)
4420 "*APPrinterPreset Photo_with_Paper_Auto-Detect/Photo: \"\n"
4421 " *cupsPrintQuality %s *ColorModel %s\n"
4422 " com.apple.print.preset.graphicsType Photo\n"
4423 " com.apple.print.preset.quality %s\n"
4424 " com.apple.print.preset.media-front-coating autodetect\n"
4425 " com.apple.print.preset.output-mode color\"\n"
4426 "*End\n", have_qhigh
? "High" : "Normal", default_color
, have_qhigh
? "high" : "mid");
4434 if ((attr
= ippFindAttribute(supported
, "sides-supported", IPP_TAG_KEYWORD
)) != NULL
&& ippContainsString(attr
, "two-sided-long-edge"))
4436 cupsFilePuts(fp
, "*OpenUI *Duplex: PickOne\n"
4437 "*OrderDependency: 10 AnySetup *Duplex\n"
4438 "*DefaultDuplex: None\n"
4439 "*Duplex None: \"<</Duplex false>>setpagedevice\"\n"
4440 "*Duplex DuplexNoTumble: \"<</Duplex true/Tumble false>>setpagedevice\"\n"
4441 "*Duplex DuplexTumble: \"<</Duplex true/Tumble true>>setpagedevice\"\n"
4442 "*CloseUI: *Duplex\n");
4444 if ((attr
= ippFindAttribute(supported
, "urf-supported", IPP_TAG_KEYWORD
)) != NULL
)
4446 for (i
= 0, count
= ippGetCount(attr
); i
< count
; i
++)
4448 const char *dm
= ippGetString(attr
, i
, NULL
);
4451 if (!_cups_strcasecmp(dm
, "DM1"))
4453 cupsFilePuts(fp
, "*cupsBackSide: Normal\n");
4456 else if (!_cups_strcasecmp(dm
, "DM2"))
4458 cupsFilePuts(fp
, "*cupsBackSide: Flipped\n");
4461 else if (!_cups_strcasecmp(dm
, "DM3"))
4463 cupsFilePuts(fp
, "*cupsBackSide: Rotated\n");
4466 else if (!_cups_strcasecmp(dm
, "DM4"))
4468 cupsFilePuts(fp
, "*cupsBackSide: ManualTumble\n");
4473 else if ((attr
= ippFindAttribute(supported
, "pwg-raster-document-sheet-back", IPP_TAG_KEYWORD
)) != NULL
)
4475 keyword
= ippGetString(attr
, 0, NULL
);
4477 if (!strcmp(keyword
, "flipped"))
4478 cupsFilePuts(fp
, "*cupsBackSide: Flipped\n");
4479 else if (!strcmp(keyword
, "manual-tumble"))
4480 cupsFilePuts(fp
, "*cupsBackSide: ManualTumble\n");
4481 else if (!strcmp(keyword
, "normal"))
4482 cupsFilePuts(fp
, "*cupsBackSide: Normal\n");
4484 cupsFilePuts(fp
, "*cupsBackSide: Rotated\n");
4492 if ((attr
= ippFindAttribute(supported
, "output-bin-default", IPP_TAG_ZERO
)) != NULL
)
4493 pwg_ppdize_name(ippGetString(attr
, 0, NULL
), ppdname
, sizeof(ppdname
));
4495 cupsCopyString(ppdname
, "Unknown", sizeof(ppdname
));
4497 if ((attr
= ippFindAttribute(supported
, "output-bin-supported", IPP_TAG_ZERO
)) != NULL
&& (count
= ippGetCount(attr
)) > 0)
4499 ipp_attribute_t
*trays
= ippFindAttribute(supported
, "printer-output-tray", IPP_TAG_STRING
);
4500 /* printer-output-tray attribute, if any */
4501 const char *tray_ptr
; /* printer-output-tray value */
4502 int tray_len
; /* Len of printer-output-tray value */
4503 char tray
[IPP_MAX_OCTETSTRING
];
4504 /* printer-output-tray string value */
4506 cupsFilePrintf(fp
, "*OpenUI *OutputBin: PickOne\n"
4507 "*OrderDependency: 10 AnySetup *OutputBin\n"
4508 "*DefaultOutputBin: %s\n", ppdname
);
4509 if (!strcmp(ppdname
, "FaceUp"))
4510 cupsFilePuts(fp
, "*DefaultOutputOrder: Reverse\n");
4512 cupsFilePuts(fp
, "*DefaultOutputOrder: Normal\n");
4514 for (i
= 0; i
< count
; i
++)
4516 keyword
= ippGetString(attr
, i
, NULL
);
4518 pwg_ppdize_name(keyword
, ppdname
, sizeof(ppdname
));
4520 snprintf(msgid
, sizeof(msgid
), "output-bin.%s", keyword
);
4522 cupsFilePrintf(fp
, "*OutputBin %s/%s: \"\"\n", ppdname
, ppd_get_string(base_lang
, lang
, msgid
, ppdtext
, sizeof(ppdtext
)));
4523 ppd_put_strings(fp
, langs
, "OutputBin", ppdname
, msgid
);
4525 if ((tray_ptr
= ippGetOctetString(trays
, i
, &tray_len
)) != NULL
)
4527 if (tray_len
>= (int)sizeof(tray
))
4528 tray_len
= (int)sizeof(tray
) - 1;
4530 memcpy(tray
, tray_ptr
, (size_t)tray_len
);
4531 tray
[tray_len
] = '\0';
4533 if (strstr(tray
, "stackingorder=lastToFirst;"))
4534 cupsFilePrintf(fp
, "*PageStackOrder %s: Reverse\n", ppdname
);
4536 cupsFilePrintf(fp
, "*PageStackOrder %s: Normal\n", ppdname
);
4538 else if (!strcmp(ppdname
, "FaceUp"))
4539 cupsFilePrintf(fp
, "*PageStackOrder %s: Reverse\n", ppdname
);
4541 cupsFilePrintf(fp
, "*PageStackOrder %s: Normal\n", ppdname
);
4543 cupsFilePuts(fp
, "*CloseUI: *OutputBin\n");
4547 * Finishing options...
4550 if ((attr
= ippFindAttribute(supported
, "finishings-supported", IPP_TAG_ENUM
)) != NULL
)
4552 int value
; /* Enum value */
4553 const char *ppd_keyword
; /* PPD keyword for enum */
4554 cups_array_t
*names
; /* Names we've added */
4555 static const char * const base_keywords
[] =
4556 { /* Base STD 92 keywords */
4558 "SingleAuto", /* staple */
4559 "SingleAuto", /* punch */
4561 "BindAuto", /* bind */
4562 "SaddleStitch", /* saddle-stitch */
4563 "EdgeStitchAuto", /* edge-stitch */
4567 NULL
, /* booklet-maker */
4568 NULL
, /* jog-offset */
4573 count
= ippGetCount(attr
);
4574 names
= cupsArrayNew3((cups_array_func_t
)_cupsArrayStrcmp
, NULL
, NULL
, 0, (cups_acopy_func_t
)_cupsArrayStrdup
, _cupsArrayFree
);
4575 fin_options
= cupsArrayNew((cups_array_func_t
)_cupsArrayStrcmp
, NULL
);
4578 * Staple/Bind/Stitch
4581 for (i
= 0; i
< count
; i
++)
4583 value
= ippGetInteger(attr
, i
);
4584 keyword
= ippEnumString("finishings", value
);
4586 if (!strncmp(keyword
, "staple-", 7) || !strncmp(keyword
, "bind-", 5) || !strncmp(keyword
, "edge-stitch-", 12) || !strcmp(keyword
, "saddle-stitch") || !strcmp(keyword
, "staple") || !strcmp(keyword
, "bind"))
4592 static const char * const staple_keywords
[] =
4593 { /* StapleLocation keywords */
4595 "SingleRevLandscape",
4597 "SingleRevPortrait",
4598 "EdgeStitchPortrait",
4599 "EdgeStitchLandscape",
4600 "EdgeStitchRevPortrait",
4601 "EdgeStitchRevLandscape",
4608 "TripleRevPortrait",
4609 "TripleRevLandscape"
4611 static const char * const bind_keywords
[] =
4612 { /* StapleLocation binding keywords */
4619 cupsArrayAdd(fin_options
, "*StapleLocation");
4621 cupsFilePrintf(fp
, "*OpenUI *StapleLocation/%s: PickOne\n", _cupsLangString(base_lang
, "finishings.4"));
4622 cupsFilePuts(fp
, "*OrderDependency: 10 AnySetup *StapleLocation\n");
4623 ppd_put_strings(fp
, langs
, "Translation", "StapleLocation", "finishings.4");
4624 cupsFilePuts(fp
, "*DefaultStapleLocation: None\n");
4625 cupsFilePrintf(fp
, "*StapleLocation None/%s: \"\"\n", _cupsLangString(base_lang
, "finishings.3"));
4626 ppd_put_strings(fp
, langs
, "StapleLocation", "None", "finishings.3");
4628 for (; i
< count
; i
++)
4630 value
= ippGetInteger(attr
, i
);
4631 keyword
= ippEnumString("finishings", value
);
4633 if (strncmp(keyword
, "staple-", 7) && strncmp(keyword
, "bind-", 5) && strncmp(keyword
, "edge-stitch-", 12) && strcmp(keyword
, "saddle-stitch") && strcmp(keyword
, "staple") && strcmp(keyword
, "bind"))
4636 if (cupsArrayFind(names
, (char *)keyword
))
4637 continue; /* Already did this finishing template */
4639 cupsArrayAdd(names
, (char *)keyword
);
4641 snprintf(msgid
, sizeof(msgid
), "finishings.%d", value
);
4643 if (value
>= IPP_FINISHINGS_NONE
&& value
<= IPP_FINISHINGS_LAMINATE
)
4644 ppd_keyword
= base_keywords
[value
- IPP_FINISHINGS_NONE
];
4645 else if (value
>= IPP_FINISHINGS_STAPLE_TOP_LEFT
&& value
<= IPP_FINISHINGS_STAPLE_TRIPLE_BOTTOM
)
4646 ppd_keyword
= staple_keywords
[value
- IPP_FINISHINGS_STAPLE_TOP_LEFT
];
4647 else if (value
>= IPP_FINISHINGS_BIND_LEFT
&& value
<= IPP_FINISHINGS_BIND_BOTTOM
)
4648 ppd_keyword
= bind_keywords
[value
- IPP_FINISHINGS_BIND_LEFT
];
4655 cupsFilePrintf(fp
, "*StapleLocation %s/%s: \"\"\n", ppd_keyword
, _cupsLangString(base_lang
, msgid
));
4656 ppd_put_strings(fp
, langs
, "StapleLocation", ppd_keyword
, msgid
);
4657 cupsFilePrintf(fp
, "*cupsIPPFinishings %d/%s: \"*StapleLocation %s\"\n", value
, keyword
, ppd_keyword
);
4660 cupsFilePuts(fp
, "*CloseUI: *StapleLocation\n");
4667 for (i
= 0; i
< count
; i
++)
4669 value
= ippGetInteger(attr
, i
);
4670 keyword
= ippEnumString("finishings", value
);
4672 if (!strncmp(keyword
, "cups-fold-", 10) || !strcmp(keyword
, "fold") || !strncmp(keyword
, "fold-", 5))
4678 static const char * const fold_keywords
[] =
4679 { /* FoldType keywords */
4694 cupsArrayAdd(fin_options
, "*FoldType");
4696 cupsFilePrintf(fp
, "*OpenUI *FoldType/%s: PickOne\n", _cupsLangString(base_lang
, "finishings.10"));
4697 cupsFilePuts(fp
, "*OrderDependency: 10 AnySetup *FoldType\n");
4698 ppd_put_strings(fp
, langs
, "Translation", "FoldType", "finishings.10");
4699 cupsFilePuts(fp
, "*DefaultFoldType: None\n");
4700 cupsFilePrintf(fp
, "*FoldType None/%s: \"\"\n", _cupsLangString(base_lang
, "finishings.3"));
4701 ppd_put_strings(fp
, langs
, "FoldType", "None", "finishings.3");
4703 for (; i
< count
; i
++)
4705 value
= ippGetInteger(attr
, i
);
4706 keyword
= ippEnumString("finishings", value
);
4708 if (!strncmp(keyword
, "cups-fold-", 10))
4710 else if (strcmp(keyword
, "fold") && strncmp(keyword
, "fold-", 5))
4713 if (cupsArrayFind(names
, (char *)keyword
))
4714 continue; /* Already did this finishing template */
4716 cupsArrayAdd(names
, (char *)keyword
);
4718 snprintf(msgid
, sizeof(msgid
), "finishings.%d", value
);
4720 if (value
>= IPP_FINISHINGS_NONE
&& value
<= IPP_FINISHINGS_LAMINATE
)
4721 ppd_keyword
= base_keywords
[value
- IPP_FINISHINGS_NONE
];
4722 else if (value
>= IPP_FINISHINGS_FOLD_ACCORDION
&& value
<= IPP_FINISHINGS_FOLD_ENGINEERING_Z
)
4723 ppd_keyword
= fold_keywords
[value
- IPP_FINISHINGS_FOLD_ACCORDION
];
4724 else if (value
>= IPP_FINISHINGS_CUPS_FOLD_ACCORDION
&& value
<= IPP_FINISHINGS_CUPS_FOLD_Z
)
4725 ppd_keyword
= fold_keywords
[value
- IPP_FINISHINGS_CUPS_FOLD_ACCORDION
];
4732 cupsFilePrintf(fp
, "*FoldType %s/%s: \"\"\n", ppd_keyword
, _cupsLangString(base_lang
, msgid
));
4733 ppd_put_strings(fp
, langs
, "FoldType", ppd_keyword
, msgid
);
4734 cupsFilePrintf(fp
, "*cupsIPPFinishings %d/%s: \"*FoldType %s\"\n", value
, keyword
, ppd_keyword
);
4737 cupsFilePuts(fp
, "*CloseUI: *FoldType\n");
4744 for (i
= 0; i
< count
; i
++)
4746 value
= ippGetInteger(attr
, i
);
4747 keyword
= ippEnumString("finishings", value
);
4749 if (!strcmp(keyword
, "punch") || !strncmp(keyword
, "cups-punch-", 11) || !strncmp(keyword
, "punch-", 6))
4755 static const char * const punch_keywords
[] =
4756 { /* PunchMedia keywords */
4758 "SingleRevLandscape",
4760 "SingleRevPortrait",
4767 "TripleRevPortrait",
4768 "TripleRevLandscape",
4774 "MultipleLandscape",
4775 "MultipleRevPortrait",
4776 "MultipleRevLandscape"
4779 cupsArrayAdd(fin_options
, "*PunchMedia");
4781 cupsFilePrintf(fp
, "*OpenUI *PunchMedia/%s: PickOne\n", _cupsLangString(base_lang
, "finishings.5"));
4782 cupsFilePuts(fp
, "*OrderDependency: 10 AnySetup *PunchMedia\n");
4783 ppd_put_strings(fp
, langs
, "Translation", "PunchMedia", "finishings.5");
4784 cupsFilePuts(fp
, "*DefaultPunchMedia: None\n");
4785 cupsFilePrintf(fp
, "*PunchMedia None/%s: \"\"\n", _cupsLangString(base_lang
, "finishings.3"));
4786 ppd_put_strings(fp
, langs
, "PunchMedia", "None", "finishings.3");
4788 for (i
= 0; i
< count
; i
++)
4790 value
= ippGetInteger(attr
, i
);
4791 keyword
= ippEnumString("finishings", value
);
4793 if (!strncmp(keyword
, "cups-punch-", 11))
4795 else if (strcmp(keyword
, "punch") && strncmp(keyword
, "punch-", 6))
4798 if (cupsArrayFind(names
, (char *)keyword
))
4799 continue; /* Already did this finishing template */
4801 cupsArrayAdd(names
, (char *)keyword
);
4803 snprintf(msgid
, sizeof(msgid
), "finishings.%d", value
);
4805 if (value
>= IPP_FINISHINGS_NONE
&& value
<= IPP_FINISHINGS_LAMINATE
)
4806 ppd_keyword
= base_keywords
[value
- IPP_FINISHINGS_NONE
];
4807 else if (value
>= IPP_FINISHINGS_PUNCH_TOP_LEFT
&& value
<= IPP_FINISHINGS_PUNCH_MULTIPLE_BOTTOM
)
4808 ppd_keyword
= punch_keywords
[value
- IPP_FINISHINGS_PUNCH_TOP_LEFT
];
4809 else if (value
>= IPP_FINISHINGS_CUPS_PUNCH_TOP_LEFT
&& value
<= IPP_FINISHINGS_CUPS_PUNCH_QUAD_BOTTOM
)
4810 ppd_keyword
= punch_keywords
[value
- IPP_FINISHINGS_CUPS_PUNCH_TOP_LEFT
];
4817 cupsFilePrintf(fp
, "*PunchMedia %s/%s: \"\"\n", ppd_keyword
, _cupsLangString(base_lang
, msgid
));
4818 ppd_put_strings(fp
, langs
, "PunchMedia", ppd_keyword
, msgid
);
4819 cupsFilePrintf(fp
, "*cupsIPPFinishings %d/%s: \"*PunchMedia %s\"\n", value
, keyword
, ppd_keyword
);
4822 cupsFilePuts(fp
, "*CloseUI: *PunchMedia\n");
4829 if (ippContainsInteger(attr
, IPP_FINISHINGS_BOOKLET_MAKER
))
4831 cupsArrayAdd(fin_options
, "*Booklet");
4833 cupsFilePrintf(fp
, "*OpenUI *Booklet/%s: Boolean\n", _cupsLangString(base_lang
, "finishings.13"));
4834 cupsFilePuts(fp
, "*OrderDependency: 10 AnySetup *Booklet\n");
4835 ppd_put_strings(fp
, langs
, "Translation", "StapleLocation", "finishings.13");
4836 cupsFilePuts(fp
, "*DefaultBooklet: False\n");
4837 cupsFilePuts(fp
, "*Booklet False: \"\"\n");
4838 cupsFilePuts(fp
, "*Booklet True: \"\"\n");
4839 cupsFilePrintf(fp
, "*cupsIPPFinishings %d/booklet-maker: \"*Booklet True\"\n", IPP_FINISHINGS_BOOKLET_MAKER
);
4840 cupsFilePuts(fp
, "*CloseUI: *Booklet\n");
4847 for (i
= 0; i
< count
; i
++)
4849 value
= ippGetInteger(attr
, i
);
4850 keyword
= ippEnumString("finishings", value
);
4852 if (!strcmp(keyword
, "trim") || !strncmp(keyword
, "trim-", 5))
4858 static const char * const trim_keywords
[] =
4859 { /* CutMedia keywords */
4866 cupsArrayAdd(fin_options
, "*CutMedia");
4868 cupsFilePrintf(fp
, "*OpenUI *CutMedia/%s: PickOne\n", _cupsLangString(base_lang
, "finishings.11"));
4869 cupsFilePuts(fp
, "*OrderDependency: 10 AnySetup *CutMedia\n");
4870 ppd_put_strings(fp
, langs
, "Translation", "CutMedia", "finishings.11");
4871 cupsFilePuts(fp
, "*DefaultCutMedia: None\n");
4872 cupsFilePrintf(fp
, "*CutMedia None/%s: \"\"\n", _cupsLangString(base_lang
, "finishings.3"));
4873 ppd_put_strings(fp
, langs
, "CutMedia", "None", "finishings.3");
4875 for (i
= 0; i
< count
; i
++)
4877 value
= ippGetInteger(attr
, i
);
4878 keyword
= ippEnumString("finishings", value
);
4880 if (strcmp(keyword
, "trim") && strncmp(keyword
, "trim-", 5))
4883 if (cupsArrayFind(names
, (char *)keyword
))
4884 continue; /* Already did this finishing template */
4886 cupsArrayAdd(names
, (char *)keyword
);
4888 snprintf(msgid
, sizeof(msgid
), "finishings.%d", value
);
4890 if (value
== IPP_FINISHINGS_TRIM
)
4891 ppd_keyword
= "Auto";
4893 ppd_keyword
= trim_keywords
[value
- IPP_FINISHINGS_TRIM_AFTER_PAGES
];
4895 cupsFilePrintf(fp
, "*CutMedia %s/%s: \"\"\n", ppd_keyword
, _cupsLangString(base_lang
, msgid
));
4896 ppd_put_strings(fp
, langs
, "CutMedia", ppd_keyword
, msgid
);
4897 cupsFilePrintf(fp
, "*cupsIPPFinishings %d/%s: \"*CutMedia %s\"\n", value
, keyword
, ppd_keyword
);
4900 cupsFilePuts(fp
, "*CloseUI: *CutMedia\n");
4903 cupsArrayDelete(names
);
4906 if ((attr
= ippFindAttribute(supported
, "finishings-col-database", IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
4908 ipp_t
*finishing_col
; /* Current finishing collection */
4909 ipp_attribute_t
*finishing_attr
; /* Current finishing member attribute */
4910 cups_array_t
*templates
; /* Finishing templates */
4912 cupsFilePrintf(fp
, "*OpenUI *cupsFinishingTemplate/%s: PickOne\n", ppd_get_string(base_lang
, lang
, "finishing-template", ppdtext
, sizeof(ppdtext
)));
4913 cupsFilePuts(fp
, "*OrderDependency: 10 AnySetup *cupsFinishingTemplate\n");
4914 ppd_put_strings(fp
, langs
, "Translation", "cupsFinishingTemplate", "finishing-template");
4915 cupsFilePuts(fp
, "*DefaultcupsFinishingTemplate: none\n");
4916 cupsFilePrintf(fp
, "*cupsFinishingTemplate none/%s: \"\"\n", _cupsLangString(lang
, "finishings.3"));
4917 ppd_put_strings(fp
, langs
, "cupsFinishingTemplate", "none", "finishings.3");
4919 templates
= cupsArrayNew((cups_array_func_t
)_cupsArrayStrcmp
, NULL
);
4920 count
= ippGetCount(attr
);
4922 for (i
= 0; i
< count
; i
++)
4924 finishing_col
= ippGetCollection(attr
, i
);
4925 keyword
= ippGetString(ippFindAttribute(finishing_col
, "finishing-template", IPP_TAG_ZERO
), 0, NULL
);
4927 if (!keyword
|| cupsArrayFind(templates
, (void *)keyword
))
4930 if (!strcmp(keyword
, "none"))
4933 cupsArrayAdd(templates
, (void *)keyword
);
4935 pwg_ppdize_name(keyword
, ppdname
, sizeof(ppdname
));
4937 snprintf(msgid
, sizeof(msgid
), "finishing-template.%s", keyword
);
4939 cupsFilePrintf(fp
, "*cupsFinishingTemplate %s/%s: \"\n", ppdname
, ppd_get_string(base_lang
, lang
, msgid
, ppdtext
, sizeof(ppdtext
)));
4940 for (finishing_attr
= ippFirstAttribute(finishing_col
); finishing_attr
; finishing_attr
= ippNextAttribute(finishing_col
))
4942 if (ippGetValueTag(finishing_attr
) == IPP_TAG_BEGIN_COLLECTION
)
4944 const char *name
= ippGetName(finishing_attr
);
4945 /* Member attribute name */
4947 if (strcmp(name
, "media-size"))
4948 cupsFilePrintf(fp
, "%% %s\n", name
);
4951 cupsFilePuts(fp
, "\"\n");
4952 ppd_put_strings(fp
, langs
, "cupsFinishingTemplate", ppdname
, msgid
);
4953 cupsFilePuts(fp
, "*End\n");
4956 cupsFilePuts(fp
, "*CloseUI: *cupsFinishingTemplate\n");
4958 if (cupsArrayCount(fin_options
))
4960 const char *fin_option
; /* Current finishing option */
4962 cupsFilePuts(fp
, "*cupsUIConstraint finishing-template: \"*cupsFinishingTemplate");
4963 for (fin_option
= (const char *)cupsArrayFirst(fin_options
); fin_option
; fin_option
= (const char *)cupsArrayNext(fin_options
))
4964 cupsFilePrintf(fp
, " %s", fin_option
);
4965 cupsFilePuts(fp
, "\"\n");
4967 cupsFilePuts(fp
, "*cupsUIResolver finishing-template: \"*cupsFinishingTemplate none");
4968 for (fin_option
= (const char *)cupsArrayFirst(fin_options
); fin_option
; fin_option
= (const char *)cupsArrayNext(fin_options
))
4969 cupsFilePrintf(fp
, " %s None", fin_option
);
4970 cupsFilePuts(fp
, "\"\n");
4973 cupsArrayDelete(templates
);
4976 cupsArrayDelete(fin_options
);
4982 if ((attr
= ippFindAttribute(supported
, "job-presets-supported", IPP_TAG_BEGIN_COLLECTION
)) != NULL
)
4984 for (i
= 0, count
= ippGetCount(attr
); i
< count
; i
++)
4986 ipp_t
*preset
= ippGetCollection(attr
, i
);
4987 /* Preset collection */
4988 const char *preset_name
= ippGetString(ippFindAttribute(preset
, "preset-name", IPP_TAG_ZERO
), 0, NULL
);
4990 ipp_attribute_t
*member
; /* Member attribute in preset */
4991 const char *member_name
; /* Member attribute name */
4992 char member_value
[256]; /* Member attribute value */
4994 if (!preset
|| !preset_name
)
4997 pwg_ppdize_name(preset_name
, ppdname
, sizeof(ppdname
));
4999 cupsFilePrintf(fp
, "*APPrinterPreset %s: \"\n", ppdname
);
5000 for (member
= ippFirstAttribute(preset
); member
; member
= ippNextAttribute(preset
))
5002 member_name
= ippGetName(member
);
5004 if (!member_name
|| !strcmp(member_name
, "preset-name"))
5007 if (!strcmp(member_name
, "finishings"))
5009 for (i
= 0, count
= ippGetCount(member
); i
< count
; i
++)
5011 const char *option
= NULL
; /* PPD option name */
5013 keyword
= ippEnumString("finishings", ippGetInteger(member
, i
));
5015 if (!strcmp(keyword
, "booklet-maker"))
5020 else if (!strncmp(keyword
, "fold-", 5))
5021 option
= "FoldType";
5022 else if (!strncmp(keyword
, "punch-", 6))
5023 option
= "PunchMedia";
5024 else if (!strncmp(keyword
, "bind-", 5) || !strncmp(keyword
, "edge-stitch-", 12) || !strcmp(keyword
, "saddle-stitch") || !strncmp(keyword
, "staple-", 7) || !strcmp(keyword
, "staple") || !strcmp(keyword
, "bind"))
5025 option
= "StapleLocation";
5027 if (option
&& keyword
)
5028 cupsFilePrintf(fp
, "*%s %s\n", option
, keyword
);
5031 else if (!strcmp(member_name
, "finishings-col"))
5033 ipp_t
*fin_col
; /* finishings-col value */
5035 for (i
= 0, count
= ippGetCount(member
); i
< count
; i
++)
5037 fin_col
= ippGetCollection(member
, i
);
5039 if ((keyword
= ippGetString(ippFindAttribute(fin_col
, "finishing-template", IPP_TAG_ZERO
), 0, NULL
)) != NULL
)
5041 pwg_ppdize_name(keyword
, ppdname
, sizeof(ppdname
));
5042 cupsFilePrintf(fp
, "*cupsFinishingTemplate %s\n", ppdname
);
5046 else if (!strcmp(member_name
, "media"))
5049 * Map media to PageSize...
5052 if ((pwg
= pwgMediaForPWG(ippGetString(member
, 0, NULL
))) != NULL
&& pwg
->ppd
)
5053 cupsFilePrintf(fp
, "*PageSize %s\n", pwg
->ppd
);
5055 else if (!strcmp(member_name
, "media-col"))
5057 media_col
= ippGetCollection(member
, 0);
5059 if ((media_size
= ippGetCollection(ippFindAttribute(media_col
, "media-size", IPP_TAG_BEGIN_COLLECTION
), 0)) != NULL
)
5061 x_dim
= ippFindAttribute(media_size
, "x-dimension", IPP_TAG_INTEGER
);
5062 y_dim
= ippFindAttribute(media_size
, "y-dimension", IPP_TAG_INTEGER
);
5063 if ((pwg
= pwgMediaForSize(ippGetInteger(x_dim
, 0), ippGetInteger(y_dim
, 0))) != NULL
&& pwg
->ppd
)
5064 cupsFilePrintf(fp
, "*PageSize %s\n", pwg
->ppd
);
5067 if ((keyword
= ippGetString(ippFindAttribute(media_col
, "media-source", IPP_TAG_ZERO
), 0, NULL
)) != NULL
)
5069 pwg_ppdize_name(keyword
, ppdname
, sizeof(ppdname
));
5070 cupsFilePrintf(fp
, "*InputSlot %s\n", ppdname
);
5073 if ((keyword
= ippGetString(ippFindAttribute(media_col
, "media-type", IPP_TAG_ZERO
), 0, NULL
)) != NULL
)
5075 pwg_ppdize_name(keyword
, ppdname
, sizeof(ppdname
));
5076 cupsFilePrintf(fp
, "*MediaType %s\n", ppdname
);
5079 else if (!strcmp(member_name
, "print-quality"))
5082 * Map print-quality to cupsPrintQuality...
5085 int qval
= ippGetInteger(member
, 0);
5086 /* print-quality value */
5087 static const char * const qualities
[] = { "Draft", "Normal", "High" };
5088 /* cupsPrintQuality values */
5090 if (qval
>= IPP_QUALITY_DRAFT
&& qval
<= IPP_QUALITY_HIGH
)
5091 cupsFilePrintf(fp
, "*cupsPrintQuality %s\n", qualities
[qval
- IPP_QUALITY_DRAFT
]);
5093 else if (!strcmp(member_name
, "output-bin"))
5095 pwg_ppdize_name(ippGetString(member
, 0, NULL
), ppdname
, sizeof(ppdname
));
5096 cupsFilePrintf(fp
, "*OutputBin %s\n", ppdname
);
5098 else if (!strcmp(member_name
, "sides"))
5100 keyword
= ippGetString(member
, 0, NULL
);
5101 if (keyword
&& !strcmp(keyword
, "one-sided"))
5102 cupsFilePuts(fp
, "*Duplex None\n");
5103 else if (keyword
&& !strcmp(keyword
, "two-sided-long-edge"))
5104 cupsFilePuts(fp
, "*Duplex DuplexNoTumble\n");
5105 else if (keyword
&& !strcmp(keyword
, "two-sided-short-edge"))
5106 cupsFilePuts(fp
, "*Duplex DuplexTumble\n");
5111 * Add attribute name and value as-is...
5114 ippAttributeString(member
, member_value
, sizeof(member_value
));
5115 cupsFilePrintf(fp
, "*%s %s\n", member_name
, member_value
);
5119 cupsFilePuts(fp
, "\"\n*End\n");
5121 pwg_ppdize_name(preset_name
, ppdname
, sizeof(ppdname
));
5122 snprintf(msgid
, sizeof(msgid
), "preset-name.%s", preset_name
);
5123 ppd_put_strings(fp
, langs
, "APPrinterPreset", ppdname
, msgid
);
5128 * Add cupsSingleFile to support multiple files printing on printers
5129 * which don't support multiple files in its firmware...
5131 * Adding the keyword degrades printing performance (there is 1-2 seconds
5132 * pause between files).
5135 cupsFilePuts(fp
, "*cupsSingleFile: true\n");
5138 * Add localized printer-state-reasons keywords as needed...
5141 for (lang
= langs
; lang
; lang
= lang
->next
)
5143 _cups_message_t
*msg
; // Current message
5145 for (msg
= (_cups_message_t
*)cupsArrayGetFirst(lang
->strings
); msg
; msg
= (_cups_message_t
*)cupsArrayGetNext(lang
->strings
))
5147 if (!strncmp(msg
->msg
, "printer-state-reasons.", 22))
5149 if (!strcmp(lang
->language
, "en"))
5150 cupsFilePrintf(fp
, "*cupsIPPReason %s/%s: \"\"\n", msg
->msg
+ 22, msg
->str
);
5152 cupsFilePrintf(fp
, "*%s.cupsIPPReason %s/%s: \"\"\n", lang
->language
, msg
->msg
+ 22, msg
->str
);
5158 * Close up and return...
5163 for (lang
= langs
; lang
;)
5165 cups_lang_t
*next
= lang
->next
;
5167 cupsArrayDelete(lang
->strings
);
5176 * If we get here then there was a problem creating the PPD...
5183 for (lang
= langs
; lang
;)
5185 cups_lang_t
*next
= lang
->next
;
5187 cupsArrayDelete(lang
->strings
);
5196 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Printer does not support required IPP attributes or document formats."), 1);
5203 * '_pwgInputSlotForSource()' - Get the InputSlot name for the given PWG
5207 const char * /* O - InputSlot name */
5208 _pwgInputSlotForSource(
5209 const char *media_source
, /* I - PWG media-source */
5210 char *name
, /* I - Name buffer */
5211 size_t namesize
) /* I - Size of name buffer */
5214 * Range check input...
5217 if (!media_source
|| !name
|| namesize
< PPD_MAX_NAME
)
5220 if (_cups_strcasecmp(media_source
, "main"))
5221 cupsCopyString(name
, "Cassette", namesize
);
5222 else if (_cups_strcasecmp(media_source
, "alternate"))
5223 cupsCopyString(name
, "Multipurpose", namesize
);
5224 else if (_cups_strcasecmp(media_source
, "large-capacity"))
5225 cupsCopyString(name
, "LargeCapacity", namesize
);
5226 else if (_cups_strcasecmp(media_source
, "bottom"))
5227 cupsCopyString(name
, "Lower", namesize
);
5228 else if (_cups_strcasecmp(media_source
, "middle"))
5229 cupsCopyString(name
, "Middle", namesize
);
5230 else if (_cups_strcasecmp(media_source
, "top"))
5231 cupsCopyString(name
, "Upper", namesize
);
5232 else if (_cups_strcasecmp(media_source
, "rear"))
5233 cupsCopyString(name
, "Rear", namesize
);
5234 else if (_cups_strcasecmp(media_source
, "side"))
5235 cupsCopyString(name
, "Side", namesize
);
5236 else if (_cups_strcasecmp(media_source
, "envelope"))
5237 cupsCopyString(name
, "Envelope", namesize
);
5238 else if (_cups_strcasecmp(media_source
, "main-roll"))
5239 cupsCopyString(name
, "Roll", namesize
);
5240 else if (_cups_strcasecmp(media_source
, "alternate-roll"))
5241 cupsCopyString(name
, "Roll2", namesize
);
5243 pwg_ppdize_name(media_source
, name
, namesize
);
5250 * '_pwgMediaTypeForType()' - Get the MediaType name for the given PWG
5254 const char * /* O - MediaType name */
5255 _pwgMediaTypeForType(
5256 const char *media_type
, /* I - PWG media-type */
5257 char *name
, /* I - Name buffer */
5258 size_t namesize
) /* I - Size of name buffer */
5261 * Range check input...
5264 if (!media_type
|| !name
|| namesize
< PPD_MAX_NAME
)
5267 if (_cups_strcasecmp(media_type
, "auto"))
5268 cupsCopyString(name
, "Auto", namesize
);
5269 else if (_cups_strcasecmp(media_type
, "cardstock"))
5270 cupsCopyString(name
, "Cardstock", namesize
);
5271 else if (_cups_strcasecmp(media_type
, "envelope"))
5272 cupsCopyString(name
, "Envelope", namesize
);
5273 else if (_cups_strcasecmp(media_type
, "photographic-glossy"))
5274 cupsCopyString(name
, "Glossy", namesize
);
5275 else if (_cups_strcasecmp(media_type
, "photographic-high-gloss"))
5276 cupsCopyString(name
, "HighGloss", namesize
);
5277 else if (_cups_strcasecmp(media_type
, "photographic-matte"))
5278 cupsCopyString(name
, "Matte", namesize
);
5279 else if (_cups_strcasecmp(media_type
, "stationery"))
5280 cupsCopyString(name
, "Plain", namesize
);
5281 else if (_cups_strcasecmp(media_type
, "stationery-coated"))
5282 cupsCopyString(name
, "Coated", namesize
);
5283 else if (_cups_strcasecmp(media_type
, "stationery-inkjet"))
5284 cupsCopyString(name
, "Inkjet", namesize
);
5285 else if (_cups_strcasecmp(media_type
, "stationery-letterhead"))
5286 cupsCopyString(name
, "Letterhead", namesize
);
5287 else if (_cups_strcasecmp(media_type
, "stationery-preprinted"))
5288 cupsCopyString(name
, "Preprinted", namesize
);
5289 else if (_cups_strcasecmp(media_type
, "transparency"))
5290 cupsCopyString(name
, "Transparency", namesize
);
5292 pwg_ppdize_name(media_type
, name
, namesize
);
5299 * '_pwgPageSizeForMedia()' - Get the PageSize name for the given media.
5302 const char * /* O - PageSize name */
5303 _pwgPageSizeForMedia(
5304 pwg_media_t
*media
, /* I - Media */
5305 char *name
, /* I - PageSize name buffer */
5306 size_t namesize
) /* I - Size of name buffer */
5308 const char *sizeptr
, /* Pointer to size in PWG name */
5309 *dimptr
; /* Pointer to dimensions in PWG name */
5313 * Range check input...
5316 if (!media
|| !name
|| namesize
< PPD_MAX_NAME
)
5320 * Copy or generate a PageSize name...
5326 * Use a standard Adobe name...
5329 cupsCopyString(name
, media
->ppd
, namesize
);
5331 else if (!media
->pwg
|| !strncmp(media
->pwg
, "custom_", 7) ||
5332 (sizeptr
= strchr(media
->pwg
, '_')) == NULL
||
5333 (dimptr
= strchr(sizeptr
+ 1, '_')) == NULL
||
5334 (size_t)(dimptr
- sizeptr
) > namesize
)
5337 * Use a name of the form "wNNNhNNN"...
5340 snprintf(name
, namesize
, "w%dh%d", (int)PWG_TO_POINTS(media
->width
),
5341 (int)PWG_TO_POINTS(media
->length
));
5346 * Copy the size name from class_sizename_dimensions...
5349 memcpy(name
, sizeptr
+ 1, (size_t)(dimptr
- sizeptr
- 1));
5350 name
[dimptr
- sizeptr
- 1] = '\0';
5358 * 'cups_connect()' - Connect to a URL and get the resource path.
5361 static int /* O - 1 on success, 0 on failure */
5362 cups_connect(http_t
**http
, /* IO - Current HTTP connection */
5363 const char *url
, /* I - URL to connect */
5364 char *resource
, /* I - Resource path buffer */
5365 size_t ressize
) /* I - Size of resource path buffer */
5367 char scheme
[32], /* URL scheme */
5368 userpass
[256], /* URL username:password */
5369 host
[256], /* URL host */
5370 curhost
[256]; /* Current host */
5371 int port
; /* URL port */
5372 http_encryption_t encryption
; /* Type of encryption to use */
5375 // Separate the URI...
5376 if (httpSeparateURI(HTTP_URI_CODING_ALL
, url
, scheme
, sizeof(scheme
), userpass
, sizeof(userpass
), host
, sizeof(host
), &port
, resource
, ressize
) < HTTP_URI_STATUS_OK
)
5379 // Use encryption as needed..
5380 if (port
== 443 || !strcmp(scheme
, "https") || !strcmp(scheme
, "ipps"))
5381 encryption
= HTTP_ENCRYPTION_ALWAYS
;
5383 encryption
= HTTP_ENCRYPTION_IF_REQUESTED
;
5385 if (!*http
|| strcasecmp(host
, httpGetHostname(*http
, curhost
, sizeof(curhost
))) || httpAddrPort(httpGetAddress(*http
)) != port
|| httpIsEncrypted(*http
) != (encryption
== HTTP_ENCRYPTION_ALWAYS
))
5388 *http
= httpConnect2(host
, port
, NULL
, AF_UNSPEC
, encryption
, 1, 5000, NULL
);
5391 return (*http
!= NULL
);
5396 * 'cups_get_strings()' - Get the strings for the specified language.
5399 static cups_lang_t
* /* O - Language data */
5401 http_t
**http
, /* IO - Current HTTP connection */
5402 const char *printer_uri
, /* I - Printer URI */
5403 const char *language
) /* I - Language name */
5405 cups_lang_t
*lang
= NULL
; /* Language data */
5406 char resource
[256]; /* Resource path for printer */
5407 ipp_t
*request
, /* IPP request */
5408 *response
= NULL
; /* IPP response */
5409 const char *strings_uri
; /* "printer-strings-uri" value */
5410 char strings_file
[1024] = "";/* Strings filename */
5411 static int request_id
= 1000; // Request ID
5415 * Connect to the printer...
5418 if (!cups_connect(http
, printer_uri
, resource
, sizeof(resource
)))
5422 * Get the URL for the corresponding language strings...
5426 ippSetOperation(request
, IPP_OP_GET_PRINTER_ATTRIBUTES
);
5427 ippSetRequestId(request
, request_id
++);
5428 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
, "attributes-charset", /*language*/NULL
, "utf-8");
5429 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
, "attributes-natural-language", /*language*/NULL
, language
);
5430 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", /*language*/NULL
, printer_uri
);
5432 response
= cupsDoRequest(*http
, request
, resource
);
5434 if ((strings_uri
= ippGetString(ippFindAttribute(response
, "printer-strings-uri", IPP_TAG_URI
), 0, NULL
)) != NULL
)
5437 * Download the strings file...
5440 if (!cups_get_url(http
, strings_uri
, ".strings", strings_file
, sizeof(strings_file
)))
5444 * Allocate memory...
5447 if ((lang
= (cups_lang_t
*)calloc(1, sizeof(cups_lang_t
))) == NULL
)
5451 * Load the strings...
5454 cupsCopyString(lang
->language
, language
, sizeof(lang
->language
));
5455 lang
->strings
= _cupsMessageLoad(NULL
, strings_file
, _CUPS_MESSAGE_STRINGS
);
5456 unlink(strings_file
);
5461 ipp_attribute_t
*attr
;
5462 ipp_tag_t group_tag
= IPP_TAG_ZERO
, value_tag
;
5466 puts("No printer-strings-uri in response.");
5467 printf("Get-Printer-Attributes: %s\n", ippErrorString(ippGetStatusCode(response
)));
5468 for (attr
= ippGetFirstAttribute(response
); attr
; attr
= ippGetNextAttribute(response
))
5470 if (ippGetGroupTag(attr
) != group_tag
)
5472 group_tag
= ippGetGroupTag(attr
);
5473 puts(ippTagString(group_tag
));
5476 name
= ippGetName(attr
);
5477 value_tag
= ippGetValueTag(attr
);
5479 ippAttributeString(attr
, value
, sizeof(value
));
5481 printf(" %s %s = %s\n", name
, ippTagString(value_tag
), value
);
5488 ippDelete(response
);
5495 * 'cups_get_url()' - Get a copy of the file at the given URL.
5498 static int /* O - 1 on success, 0 on failure */
5499 cups_get_url(http_t
**http
, /* IO - Current HTTP connection */
5500 const char *url
, /* I - URL to get */
5501 const char *suffix
, /* I - Filename suffix */
5502 char *name
, /* I - Temporary filename */
5503 size_t namesize
) /* I - Size of temporary filename buffer */
5505 char resource
[256]; /* URL resource */
5506 http_status_t status
; /* Status of GET request */
5507 int fd
; /* Temporary file */
5510 if (!cups_connect(http
, url
, resource
, sizeof(resource
)))
5513 if ((fd
= cupsCreateTempFd("ippeve", suffix
, name
, namesize
)) < 0)
5516 status
= cupsGetFd(*http
, resource
, fd
);
5520 if (status
!= HTTP_STATUS_OK
)
5532 * 'ppd_get_string()' - Get a localized string.
5534 * This function looks up the msgid first in the CUPS English localization
5535 * catalog, then the printer's English localization, and finally falls back on
5536 * converting the keyword from "word-like-this" to "Words Like This".
5539 static const char * // O - Localized text
5540 ppd_get_string(cups_lang_t
*base
, // I - Base (CUPS) localization
5541 cups_lang_t
*printer
, // I - Printer localization
5542 const char *msgid
, // I - Message ID
5543 char *buffer
, // I - Temporary string buffer
5544 size_t bufsize
) // I - Size of temporary string buffer
5546 const char *text
, // Localized text
5547 *msgptr
; // Pointer into message ID
5548 char *bufptr
; // Pointer into string buffer
5551 if ((text
= _cupsLangString(base
, msgid
)) == msgid
)
5553 if ((text
= _cupsLangString(printer
, msgid
)) == msgid
)
5555 if ((msgptr
= strrchr(msgid
, '.')) != NULL
)
5563 cupsCopyString(buffer
, msgptr
, bufsize
);
5564 buffer
[0] = (char)toupper(buffer
[0] & 255);
5565 for (bufptr
= buffer
+ 1; *bufptr
; bufptr
++)
5570 bufptr
[1] = (char)toupper(bufptr
[1] & 255);
5583 // 'ppd_get_strings()' - Get the strings for a given option and choice.
5588 ppd_file_t
*ppd
, // I - PPD file
5589 cups_lang_t
*langs
, // I - Languages
5590 const char *option
, // I - PPD option keyword
5591 ppd_choice_t
*choice
, // I - PPD choice
5592 const char *pwg_msgid
) // I - PWG message ID
5594 cups_lang_t
*lang
; // Current language
5595 char ppd_name
[PPD_MAX_NAME
]; // PPD main keyword
5596 ppd_attr_t
*ppd_attr
; // PPD attribute
5597 _cups_message_t msg
; // Message
5600 msg
.msg
= (char *)pwg_msgid
;
5602 for (lang
= langs
; lang
; lang
= lang
->next
)
5604 // See if the string is already localized...
5605 if (cupsArrayFind(lang
->strings
, &msg
))
5608 // Otherwise add the text...
5609 if (!strcmp(lang
->language
, "en"))
5612 msg
.str
= choice
->text
;
5616 // Other languauge...
5617 snprintf(ppd_name
, sizeof(ppd_name
), "%s.%s", lang
->language
, option
);
5618 if ((ppd_attr
= ppdFindAttr(ppd
, ppd_name
, choice
->choice
)) != NULL
)
5619 msg
.str
= ppd_attr
->text
;
5624 cupsArrayAdd(lang
->strings
, &msg
);
5630 * 'ppd_put_strings()' - Write localization attributes to a PPD file.
5634 ppd_put_strings(cups_file_t
*fp
, /* I - PPD file */
5635 cups_lang_t
*langs
, /* I - Languages */
5636 const char *ppd_option
,/* I - PPD option */
5637 const char *ppd_choice
,/* I - PPD choice */
5638 const char *pwg_msgid
) /* I - PWG message ID */
5640 cups_lang_t
*lang
; /* Current language */
5641 const char *text
; /* Localized text */
5644 for (lang
= langs
; lang
; lang
= lang
->next
)
5646 if (strcmp(lang
->language
, "en") && (text
= _cupsLangString(lang
, pwg_msgid
)) != pwg_msgid
)
5648 // Add the first line of localized text...
5649 cupsFilePrintf(fp
, "*%s.%s %s/", lang
->language
, ppd_option
, ppd_choice
);
5650 while (*text
&& *text
!= '\n')
5652 // Escape ":" and "<"...
5653 if (*text
== ':' || *text
== '<')
5654 cupsFilePrintf(fp
, "<%02X>", *text
);
5656 cupsFilePutChar(fp
, *text
);
5660 cupsFilePuts(fp
, ": \"\"\n");
5667 * 'pwg_add_finishing()' - Add a finishings value.
5672 cups_array_t
*finishings
, /* I - Finishings array */
5673 ipp_finishings_t
template, /* I - Finishing template */
5674 const char *name
, /* I - PPD option */
5675 const char *value
) /* I - PPD choice */
5677 _pwg_finishings_t
*f
; /* New finishings value */
5680 if ((f
= (_pwg_finishings_t
*)calloc(1, sizeof(_pwg_finishings_t
))) != NULL
)
5682 f
->value
= template;
5683 f
->num_options
= cupsAddOption(name
, value
, 0, &f
->options
);
5685 cupsArrayAdd(finishings
, f
);
5691 * 'pwg_compare_finishings()' - Compare two finishings values.
5694 static int /* O - Result of comparison */
5695 pwg_compare_finishings(
5696 _pwg_finishings_t
*a
, /* I - First finishings value */
5697 _pwg_finishings_t
*b
, /* I - Second finishings value */
5698 void *data
) /* Unused */
5701 return ((int)b
->value
- (int)a
->value
);
5706 * 'pwg_compare_sizes()' - Compare two media sizes...
5709 static int /* O - Result of comparison */
5710 pwg_compare_sizes(cups_size_t
*a
, /* I - First media size */
5711 cups_size_t
*b
, /* I - Second media size */
5712 void *data
) /* Unused */
5715 return (strcmp(a
->media
, b
->media
));
5720 * 'pwg_copy_size()' - Copy a media size.
5723 static cups_size_t
* /* O - New media size */
5724 pwg_copy_size(cups_size_t
*size
, /* I - Media size to copy */
5725 void *data
) /* Unused*/
5727 cups_size_t
*newsize
= (cups_size_t
*)calloc(1, sizeof(cups_size_t
));
5728 /* New media size */
5731 memcpy(newsize
, size
, sizeof(cups_size_t
));
5738 * 'pwg_free_finishings()' - Free a finishings value.
5742 pwg_free_finishings(_pwg_finishings_t
*f
, /* I - Finishings value */
5743 void *data
) /* Unused */
5746 cupsFreeOptions(f
->num_options
, f
->options
);
5752 * 'pwg_ppdize_name()' - Convert an IPP keyword to a PPD keyword.
5756 pwg_ppdize_name(const char *ipp
, /* I - IPP keyword */
5757 char *name
, /* I - Name buffer */
5758 size_t namesize
) /* I - Size of name buffer */
5760 char *ptr
, /* Pointer into name buffer */
5761 *end
; /* End of name buffer */
5764 if (!ipp
|| !_cups_isalnum(*ipp
))
5770 *name
= (char)toupper(*ipp
++);
5772 for (ptr
= name
+ 1, end
= name
+ namesize
- 1; *ipp
&& ptr
< end
;)
5774 if (*ipp
== '-' && _cups_isalnum(ipp
[1]))
5777 *ptr
++ = (char)toupper(*ipp
++ & 255);
5779 else if (*ipp
== '_' || *ipp
== '.' || *ipp
== '-' || _cups_isalnum(*ipp
))
5794 * 'pwg_ppdize_resolution()' - Convert PWG resolution values to PPD values.
5798 pwg_ppdize_resolution(
5799 ipp_attribute_t
*attr
, /* I - Attribute to convert */
5800 int element
, /* I - Element to convert */
5801 int *xres
, /* O - X resolution in DPI */
5802 int *yres
, /* O - Y resolution in DPI */
5803 char *name
, /* I - Name buffer */
5804 size_t namesize
) /* I - Size of name buffer */
5806 ipp_res_t units
; /* Units for resolution */
5809 *xres
= ippGetResolution(attr
, element
, yres
, &units
);
5811 if (units
== IPP_RES_PER_CM
)
5813 *xres
= (int)(*xres
* 2.54);
5814 *yres
= (int)(*yres
* 2.54);
5817 if (name
&& namesize
> 4)
5820 snprintf(name
, namesize
, "%ddpi", *xres
);
5822 snprintf(name
, namesize
, "%dx%ddpi", *xres
, *yres
);
5828 * 'pwg_unppdize_name()' - Convert a PPD keyword to a lowercase IPP keyword.
5832 pwg_unppdize_name(const char *ppd
, /* I - PPD keyword */
5833 char *name
, /* I - Name buffer */
5834 size_t namesize
, /* I - Size of name buffer */
5835 const char *dashchars
)/* I - Characters to be replaced by dashes */
5837 char *ptr
, /* Pointer into name buffer */
5838 *end
; /* End of name buffer */
5839 int nodash
= 1; /* Next char in IPP name cannot be a
5840 dash (first char or after a dash) */
5843 if (_cups_islower(*ppd
))
5846 * Already lowercase name, use as-is?
5849 const char *ppdptr
; /* Pointer into PPD keyword */
5851 for (ppdptr
= ppd
+ 1; *ppdptr
; ppdptr
++)
5852 if (_cups_isupper(*ppdptr
) || strchr(dashchars
, *ppdptr
) ||
5853 (*ppdptr
== '-' && *(ppdptr
- 1) == '-') ||
5854 (*ppdptr
== '-' && *(ppdptr
+ 1) == '\0'))
5859 cupsCopyString(name
, ppd
, namesize
);
5864 for (ptr
= name
, end
= name
+ namesize
- 1; *ppd
&& ptr
< end
; ppd
++)
5866 if (_cups_isalnum(*ppd
) || *ppd
== '.' || *ppd
== '_')
5868 *ptr
++ = (char)tolower(*ppd
& 255);
5871 else if (*ppd
== '-' || strchr(dashchars
, *ppd
))
5882 if (!_cups_isupper(*ppd
) && _cups_isalnum(*ppd
) &&
5883 _cups_isupper(ppd
[1]) && ptr
< end
)
5888 else if (!isdigit(*ppd
& 255) && isdigit(ppd
[1] & 255))
5896 /* Remove trailing dashes */
5897 while (ptr
> name
&& *(ptr
- 1) == '-')