]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/ppd-cache.c
Add support for finishings-col (Issue #5180)
[thirdparty/cups.git] / cups / ppd-cache.c
CommitLineData
f14324a7 1/*
7e86f2f6 2 * PPD cache implementation for CUPS.
f14324a7 3 *
149eecf6 4 * Copyright 2010-2017 by Apple Inc.
f14324a7 5 *
e3101897 6 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
f14324a7
MS
7 */
8
9/*
10 * Include necessary headers...
11 */
12
13#include "cups-private.h"
f787e1e3 14#include "ppd-private.h"
f14324a7
MS
15#include <math.h>
16
17
18/*
19 * Macro to test for two almost-equal PWG measurements.
20 */
21
22#define _PWG_EQUIVALENT(x, y) (abs((x)-(y)) < 2)
23
24
25/*
26 * Local functions...
27 */
28
89550f3f 29static int cups_get_url(http_t **http, const char *url, char *name, size_t namesize);
1fbd0cab 30static void pwg_add_finishing(cups_array_t *finishings, ipp_finishings_t template, const char *name, const char *value);
dcb445bc
MS
31static int pwg_compare_finishings(_pwg_finishings_t *a,
32 _pwg_finishings_t *b);
33static void pwg_free_finishings(_pwg_finishings_t *f);
f14324a7 34static void pwg_ppdize_name(const char *ipp, char *name, size_t namesize);
d9fc71e4 35static void pwg_ppdize_resolution(ipp_attribute_t *attr, int element, int *xres, int *yres, char *name, size_t namesize);
c1420c87
MS
36static void pwg_unppdize_name(const char *ppd, char *name, size_t namesize,
37 const char *dashchars);
f14324a7
MS
38
39
f099325e
MS
40/*
41 * '_cupsConvertOptions()' - Convert printer options to standard IPP attributes.
42 *
43 * This functions converts PPD and CUPS-specific options to their standard IPP
44 * attributes and values and adds them to the specified IPP request.
45 */
46
a740a849
MS
47int /* O - New number of copies */
48_cupsConvertOptions(
49 ipp_t *request, /* I - IPP request */
50 ppd_file_t *ppd, /* I - PPD file */
51 _ppd_cache_t *pc, /* I - PPD cache info */
52 ipp_attribute_t *media_col_sup, /* I - media-col-supported values */
53 ipp_attribute_t *doc_handling_sup, /* I - multiple-document-handling-supported values */
54 ipp_attribute_t *print_color_mode_sup,
55 /* I - Printer supports print-color-mode */
56 const char *user, /* I - User info */
57 const char *format, /* I - document-format value */
58 int copies, /* I - Number of copies */
59 int num_options, /* I - Number of options */
60 cups_option_t *options) /* I - Options */
f099325e
MS
61{
62 int i; /* Looping var */
5ae9fbb3
MS
63 const char *keyword, /* PWG keyword */
64 *password; /* Password string */
f099325e
MS
65 pwg_size_t *size; /* PWG media size */
66 ipp_t *media_col, /* media-col value */
67 *media_size; /* media-size value */
68 const char *media_source, /* media-source value */
69 *media_type, /* media-type value */
70 *collate_str, /* multiple-document-handling value */
71 *color_attr_name, /* Supported color attribute */
4f63d6cd
MS
72 *mandatory, /* Mandatory attributes */
73 *finishing_template; /* Finishing template */
f099325e
MS
74 int num_finishings = 0, /* Number of finishing values */
75 finishings[10]; /* Finishing enum values */
76 ppd_choice_t *choice; /* Marked choice */
49f495c3
MS
77 int finishings_copies = copies;
78 /* Number of copies for finishings */
f099325e
MS
79
80
81 /*
82 * Send standard IPP attributes...
83 */
84
5ae9fbb3 85 if (pc->password && (password = cupsGetOption("job-password", num_options, options)) != NULL && ippGetOperation(request) != IPP_OP_VALIDATE_JOB)
f099325e 86 {
5ae9fbb3 87 ipp_attribute_t *attr = NULL; /* job-password attribute */
f099325e
MS
88
89 if ((keyword = cupsGetOption("job-password-encryption", num_options, options)) == NULL)
90 keyword = "none";
91
5ae9fbb3
MS
92 if (!strcmp(keyword, "none"))
93 {
94 /*
95 * Add plain-text job-password...
96 */
97
98 attr = ippAddOctetString(request, IPP_TAG_OPERATION, "job-password", password, (int)strlen(password));
99 }
100 else
101 {
102 /*
103 * Add hashed job-password...
104 */
105
106 unsigned char hash[64]; /* Hash of password */
107 ssize_t hashlen; /* Length of hash */
108
109 if ((hashlen = cupsHashData(keyword, password, strlen(password), hash, sizeof(hash))) > 0)
110 attr = ippAddOctetString(request, IPP_TAG_OPERATION, "job-password", hash, (int)hashlen);
111 }
112
113 if (attr)
114 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "job-password-encryption", NULL, keyword);
f099325e
MS
115 }
116
117 if (pc->account_id)
118 {
119 if ((keyword = cupsGetOption("job-account-id", num_options, options)) == NULL)
120 keyword = cupsGetOption("job-billing", num_options, options);
121
122 if (keyword)
123 ippAddString(request, IPP_TAG_JOB, IPP_TAG_NAME, "job-account-id", NULL, keyword);
124 }
125
126 if (pc->accounting_user_id)
127 {
128 if ((keyword = cupsGetOption("job-accounting-user-id", num_options, options)) == NULL)
129 keyword = user;
130
131 if (keyword)
132 ippAddString(request, IPP_TAG_JOB, IPP_TAG_NAME, "job-accounting-user-id", NULL, keyword);
133 }
134
135 for (mandatory = (const char *)cupsArrayFirst(pc->mandatory); mandatory; mandatory = (const char *)cupsArrayNext(pc->mandatory))
136 {
137 if (strcmp(mandatory, "copies") &&
138 strcmp(mandatory, "destination-uris") &&
139 strcmp(mandatory, "finishings") &&
4f63d6cd
MS
140 strcmp(mandatory, "finishings-col") &&
141 strcmp(mandatory, "finishing-template") &&
f099325e
MS
142 strcmp(mandatory, "job-account-id") &&
143 strcmp(mandatory, "job-accounting-user-id") &&
144 strcmp(mandatory, "job-password") &&
145 strcmp(mandatory, "job-password-encryption") &&
146 strcmp(mandatory, "media") &&
147 strncmp(mandatory, "media-col", 9) &&
148 strcmp(mandatory, "multiple-document-handling") &&
149 strcmp(mandatory, "output-bin") &&
150 strcmp(mandatory, "print-color-mode") &&
151 strcmp(mandatory, "print-quality") &&
152 strcmp(mandatory, "sides") &&
153 (keyword = cupsGetOption(mandatory, num_options, options)) != NULL)
154 {
155 _ipp_option_t *opt = _ippFindOption(mandatory);
156 /* Option type */
157 ipp_tag_t value_tag = opt ? opt->value_tag : IPP_TAG_NAME;
158 /* Value type */
159
160 switch (value_tag)
161 {
162 case IPP_TAG_INTEGER :
163 case IPP_TAG_ENUM :
164 ippAddInteger(request, IPP_TAG_JOB, value_tag, mandatory, atoi(keyword));
165 break;
166 case IPP_TAG_BOOLEAN :
167 ippAddBoolean(request, IPP_TAG_JOB, mandatory, !_cups_strcasecmp(keyword, "true"));
168 break;
169 case IPP_TAG_RANGE :
170 {
171 int lower, upper; /* Range */
172
173 if (sscanf(keyword, "%d-%d", &lower, &upper) != 2)
174 lower = upper = atoi(keyword);
175
176 ippAddRange(request, IPP_TAG_JOB, mandatory, lower, upper);
177 }
178 break;
179 case IPP_TAG_STRING :
180 ippAddOctetString(request, IPP_TAG_JOB, mandatory, keyword, (int)strlen(keyword));
181 break;
182 default :
183 if (!strcmp(mandatory, "print-color-mode") && !strcmp(keyword, "monochrome"))
184 {
185 if (ippContainsString(print_color_mode_sup, "auto-monochrome"))
186 keyword = "auto-monochrome";
187 else if (ippContainsString(print_color_mode_sup, "process-monochrome") && !ippContainsString(print_color_mode_sup, "monochrome"))
188 keyword = "process-monochrome";
189 }
190
191 ippAddString(request, IPP_TAG_JOB, value_tag, mandatory, NULL, keyword);
192 break;
193 }
194 }
195 }
196
197 if ((keyword = cupsGetOption("PageSize", num_options, options)) == NULL)
198 keyword = cupsGetOption("media", num_options, options);
199
40c80860
MS
200 media_source = _ppdCacheGetSource(pc, cupsGetOption("InputSlot", num_options, options));
201 media_type = _ppdCacheGetType(pc, cupsGetOption("MediaType", num_options, options));
202 size = _ppdCacheGetSize(pc, keyword);
203
204 if (size || media_source || media_type)
f099325e
MS
205 {
206 /*
207 * Add a media-col value...
208 */
209
f099325e 210 media_col = ippNew();
f099325e 211
40c80860
MS
212 if (size)
213 {
214 media_size = ippNew();
215 ippAddInteger(media_size, IPP_TAG_ZERO, IPP_TAG_INTEGER,
216 "x-dimension", size->width);
217 ippAddInteger(media_size, IPP_TAG_ZERO, IPP_TAG_INTEGER,
218 "y-dimension", size->length);
219
220 ippAddCollection(media_col, IPP_TAG_ZERO, "media-size", media_size);
221 }
f099325e
MS
222
223 for (i = 0; i < media_col_sup->num_values; i ++)
224 {
40c80860 225 if (size && !strcmp(media_col_sup->values[i].string.text, "media-left-margin"))
f099325e 226 ippAddInteger(media_col, IPP_TAG_ZERO, IPP_TAG_INTEGER, "media-left-margin", size->left);
40c80860 227 else if (size && !strcmp(media_col_sup->values[i].string.text, "media-bottom-margin"))
f099325e 228 ippAddInteger(media_col, IPP_TAG_ZERO, IPP_TAG_INTEGER, "media-bottom-margin", size->bottom);
40c80860 229 else if (size && !strcmp(media_col_sup->values[i].string.text, "media-right-margin"))
f099325e 230 ippAddInteger(media_col, IPP_TAG_ZERO, IPP_TAG_INTEGER, "media-right-margin", size->right);
40c80860 231 else if (size && !strcmp(media_col_sup->values[i].string.text, "media-top-margin"))
f099325e 232 ippAddInteger(media_col, IPP_TAG_ZERO, IPP_TAG_INTEGER, "media-top-margin", size->top);
40c80860 233 else if (media_source && !strcmp(media_col_sup->values[i].string.text, "media-source"))
f099325e 234 ippAddString(media_col, IPP_TAG_ZERO, IPP_TAG_KEYWORD, "media-source", NULL, media_source);
40c80860 235 else if (media_type && !strcmp(media_col_sup->values[i].string.text, "media-type"))
f099325e
MS
236 ippAddString(media_col, IPP_TAG_ZERO, IPP_TAG_KEYWORD, "media-type", NULL, media_type);
237 }
238
239 ippAddCollection(request, IPP_TAG_JOB, "media-col", media_col);
240 }
241
242 if ((keyword = cupsGetOption("output-bin", num_options, options)) == NULL)
243 {
244 if ((choice = ppdFindMarkedChoice(ppd, "OutputBin")) != NULL)
245 keyword = _ppdCacheGetBin(pc, choice->choice);
246 }
247
248 if (keyword)
249 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "output-bin", NULL, keyword);
250
251 color_attr_name = print_color_mode_sup ? "print-color-mode" : "output-mode";
252
253 if ((keyword = cupsGetOption("print-color-mode", num_options, options)) == NULL)
254 {
255 if ((choice = ppdFindMarkedChoice(ppd, "ColorModel")) != NULL)
256 {
257 if (!_cups_strcasecmp(choice->choice, "Gray"))
258 keyword = "monochrome";
259 else
260 keyword = "color";
261 }
262 }
263
264 if (keyword && !strcmp(keyword, "monochrome"))
265 {
266 if (ippContainsString(print_color_mode_sup, "auto-monochrome"))
267 keyword = "auto-monochrome";
268 else if (ippContainsString(print_color_mode_sup, "process-monochrome") && !ippContainsString(print_color_mode_sup, "monochrome"))
269 keyword = "process-monochrome";
270 }
271
272 if (keyword)
273 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, color_attr_name, NULL, keyword);
274
275 if ((keyword = cupsGetOption("print-quality", num_options, options)) != NULL)
276 ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_ENUM, "print-quality", atoi(keyword));
277 else if ((choice = ppdFindMarkedChoice(ppd, "cupsPrintQuality")) != NULL)
278 {
279 if (!_cups_strcasecmp(choice->choice, "draft"))
280 ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_ENUM, "print-quality", IPP_QUALITY_DRAFT);
281 else if (!_cups_strcasecmp(choice->choice, "normal"))
282 ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_ENUM, "print-quality", IPP_QUALITY_NORMAL);
283 else if (!_cups_strcasecmp(choice->choice, "high"))
284 ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_ENUM, "print-quality", IPP_QUALITY_HIGH);
285 }
286
287 if ((keyword = cupsGetOption("sides", num_options, options)) != NULL)
288 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "sides", NULL, keyword);
289 else if (pc->sides_option && (choice = ppdFindMarkedChoice(ppd, pc->sides_option)) != NULL)
290 {
291 if (!_cups_strcasecmp(choice->choice, pc->sides_1sided))
292 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "sides", NULL, "one-sided");
293 else if (!_cups_strcasecmp(choice->choice, pc->sides_2sided_long))
294 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "sides", NULL, "two-sided-long-edge");
295 if (!_cups_strcasecmp(choice->choice, pc->sides_2sided_short))
296 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "sides", NULL, "two-sided-short-edge");
297 }
298
299 /*
300 * Copies...
301 */
302
303 if ((keyword = cupsGetOption("multiple-document-handling", num_options, options)) != NULL)
304 {
305 if (strstr(keyword, "uncollated"))
306 keyword = "false";
307 else
308 keyword = "true";
309 }
310 else if ((keyword = cupsGetOption("collate", num_options, options)) == NULL)
311 keyword = "true";
312
313 if (format)
314 {
315 if (!_cups_strcasecmp(format, "image/gif") ||
316 !_cups_strcasecmp(format, "image/jp2") ||
317 !_cups_strcasecmp(format, "image/jpeg") ||
318 !_cups_strcasecmp(format, "image/png") ||
319 !_cups_strcasecmp(format, "image/tiff") ||
320 !_cups_strncasecmp(format, "image/x-", 8))
321 {
322 /*
323 * Collation makes no sense for single page image formats...
324 */
325
326 keyword = "false";
327 }
328 else if (!_cups_strncasecmp(format, "image/", 6) ||
329 !_cups_strcasecmp(format, "application/vnd.cups-raster"))
330 {
331 /*
332 * Multi-page image formats will have copies applied by the upstream
333 * filters...
334 */
335
336 copies = 1;
337 }
338 }
339
340 if (doc_handling_sup)
341 {
342 if (!_cups_strcasecmp(keyword, "true"))
343 collate_str = "separate-documents-collated-copies";
344 else
345 collate_str = "separate-documents-uncollated-copies";
346
347 for (i = 0; i < doc_handling_sup->num_values; i ++)
348 {
349 if (!strcmp(doc_handling_sup->values[i].string.text, collate_str))
350 {
351 ippAddString(request, IPP_TAG_JOB, IPP_TAG_KEYWORD, "multiple-document-handling", NULL, collate_str);
352 break;
353 }
354 }
355
356 if (i >= doc_handling_sup->num_values)
357 copies = 1;
358 }
359
360 /*
361 * Map finishing options...
362 */
363
4f63d6cd
MS
364 if ((finishing_template = cupsGetOption("cupsFinishingTemplate", num_options, options)) == NULL)
365 finishing_template = cupsGetOption("finishing-template", num_options, options);
366
367 if (finishing_template)
f099325e 368 {
4f63d6cd
MS
369 ipp_t *fin_col = ippNew(); /* finishings-col value */
370
371 ippAddString(fin_col, IPP_TAG_JOB, IPP_TAG_KEYWORD, "finishing-template", NULL, finishing_template);
372 ippAddCollection(request, IPP_TAG_JOB, "finishings-col", fin_col);
373 ippDelete(fin_col);
f099325e 374
49f495c3 375 if (copies != finishings_copies && (keyword = cupsGetOption("job-impressions", num_options, options)) != NULL)
f099325e
MS
376 {
377 /*
378 * Send job-pages-per-set attribute to apply finishings correctly...
379 */
380
49f495c3 381 ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-pages-per-set", atoi(keyword) / finishings_copies);
f099325e
MS
382 }
383 }
4f63d6cd
MS
384 else
385 {
386 num_finishings = _ppdCacheGetFinishingValues(pc, num_options, options, (int)(sizeof(finishings) / sizeof(finishings[0])), finishings);
387 if (num_finishings > 0)
388 {
389 ippAddIntegers(request, IPP_TAG_JOB, IPP_TAG_ENUM, "finishings", num_finishings, finishings);
390
391 if (copies != finishings_copies && (keyword = cupsGetOption("job-impressions", num_options, options)) != NULL)
392 {
393 /*
394 * Send job-pages-per-set attribute to apply finishings correctly...
395 */
396
397 ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-pages-per-set", atoi(keyword) / finishings_copies);
398 }
399 }
400 }
f099325e
MS
401
402 return (copies);
403}
404
405
f14324a7
MS
406/*
407 * '_ppdCacheCreateWithFile()' - Create PPD cache and mapping data from a
408 * written file.
409 *
410 * Use the @link _ppdCacheWriteFile@ function to write PWG mapping data to a
411 * file.
412 */
413
414_ppd_cache_t * /* O - PPD cache and mapping data */
415_ppdCacheCreateWithFile(
416 const char *filename, /* I - File to read */
417 ipp_t **attrs) /* IO - IPP attributes, if any */
418{
419 cups_file_t *fp; /* File */
420 _ppd_cache_t *pc; /* PWG mapping data */
6961465f
MS
421 pwg_size_t *size; /* Current size */
422 pwg_map_t *map; /* Current map */
dcb445bc 423 _pwg_finishings_t *finishings; /* Current finishings option */
f14324a7
MS
424 int linenum, /* Current line number */
425 num_bins, /* Number of bins in file */
426 num_sizes, /* Number of sizes in file */
427 num_sources, /* Number of sources in file */
428 num_types; /* Number of types in file */
429 char line[2048], /* Current line */
430 *value, /* Pointer to value in line */
431 *valueptr, /* Pointer into value */
432 pwg_keyword[128], /* PWG keyword */
433 ppd_keyword[PPD_MAX_NAME];
434 /* PPD keyword */
435 _pwg_print_color_mode_t print_color_mode;
436 /* Print color mode for preset */
437 _pwg_print_quality_t print_quality; /* Print quality for preset */
438
439
440 DEBUG_printf(("_ppdCacheCreateWithFile(filename=\"%s\")", filename));
441
442 /*
443 * Range check input...
444 */
445
446 if (attrs)
447 *attrs = NULL;
448
449 if (!filename)
450 {
cb7f98ee 451 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
f14324a7
MS
452 return (NULL);
453 }
454
455 /*
456 * Open the file...
457 */
458
459 if ((fp = cupsFileOpen(filename, "r")) == NULL)
460 {
cb7f98ee 461 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
f14324a7
MS
462 return (NULL);
463 }
464
465 /*
466 * Read the first line and make sure it has "#CUPS-PPD-CACHE-version" in it...
467 */
468
469 if (!cupsFileGets(fp, line, sizeof(line)))
470 {
cb7f98ee 471 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
f14324a7
MS
472 DEBUG_puts("_ppdCacheCreateWithFile: Unable to read first line.");
473 cupsFileClose(fp);
474 return (NULL);
475 }
476
22c9029b 477 if (strncmp(line, "#CUPS-PPD-CACHE-", 16))
f14324a7 478 {
cb7f98ee 479 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
f14324a7
MS
480 DEBUG_printf(("_ppdCacheCreateWithFile: Wrong first line \"%s\".", line));
481 cupsFileClose(fp);
482 return (NULL);
483 }
484
22c9029b
MS
485 if (atoi(line + 16) != _PPD_CACHE_VERSION)
486 {
cb7f98ee 487 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Out of date PPD cache file."), 1);
22c9029b
MS
488 DEBUG_printf(("_ppdCacheCreateWithFile: Cache file has version %s, "
489 "expected %d.", line + 16, _PPD_CACHE_VERSION));
490 cupsFileClose(fp);
491 return (NULL);
492 }
493
f14324a7
MS
494 /*
495 * Allocate the mapping data structure...
496 */
497
498 if ((pc = calloc(1, sizeof(_ppd_cache_t))) == NULL)
499 {
cb7f98ee 500 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
f14324a7
MS
501 DEBUG_puts("_ppdCacheCreateWithFile: Unable to allocate _ppd_cache_t.");
502 goto create_error;
503 }
504
3e7fe0ca
MS
505 pc->max_copies = 9999;
506
f14324a7
MS
507 /*
508 * Read the file...
509 */
510
511 linenum = 0;
512 num_bins = 0;
513 num_sizes = 0;
514 num_sources = 0;
515 num_types = 0;
516
517 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
518 {
519 DEBUG_printf(("_ppdCacheCreateWithFile: line=\"%s\", value=\"%s\", "
520 "linenum=%d", line, value, linenum));
521
522 if (!value)
523 {
524 DEBUG_printf(("_ppdCacheCreateWithFile: Missing value on line %d.",
525 linenum));
cb7f98ee 526 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
f14324a7
MS
527 goto create_error;
528 }
88f9aafc 529 else if (!_cups_strcasecmp(line, "Filter"))
f14324a7
MS
530 {
531 if (!pc->filters)
532 pc->filters = cupsArrayNew3(NULL, NULL, NULL, 0,
533 (cups_acopy_func_t)_cupsStrAlloc,
534 (cups_afree_func_t)_cupsStrFree);
535
536 cupsArrayAdd(pc->filters, value);
537 }
88f9aafc 538 else if (!_cups_strcasecmp(line, "PreFilter"))
f14324a7
MS
539 {
540 if (!pc->prefilters)
541 pc->prefilters = cupsArrayNew3(NULL, NULL, NULL, 0,
542 (cups_acopy_func_t)_cupsStrAlloc,
543 (cups_afree_func_t)_cupsStrFree);
544
545 cupsArrayAdd(pc->prefilters, value);
546 }
88f9aafc 547 else if (!_cups_strcasecmp(line, "Product"))
f14324a7
MS
548 {
549 pc->product = _cupsStrAlloc(value);
550 }
88f9aafc 551 else if (!_cups_strcasecmp(line, "SingleFile"))
82f97232 552 {
88f9aafc 553 pc->single_file = !_cups_strcasecmp(value, "true");
82f97232 554 }
88f9aafc 555 else if (!_cups_strcasecmp(line, "IPP"))
f14324a7
MS
556 {
557 off_t pos = cupsFileTell(fp), /* Position in file */
558 length = strtol(value, NULL, 10);
559 /* Length of IPP attributes */
560
561 if (attrs && *attrs)
562 {
563 DEBUG_puts("_ppdCacheCreateWithFile: IPP listed multiple times.");
cb7f98ee 564 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
f14324a7
MS
565 goto create_error;
566 }
567 else if (length <= 0)
568 {
569 DEBUG_puts("_ppdCacheCreateWithFile: Bad IPP length.");
cb7f98ee 570 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
f14324a7
MS
571 goto create_error;
572 }
573
574 if (attrs)
575 {
576 /*
577 * Read IPP attributes into the provided variable...
578 */
579
580 *attrs = ippNew();
581
582 if (ippReadIO(fp, (ipp_iocb_t)cupsFileRead, 1, NULL,
cb7f98ee 583 *attrs) != IPP_STATE_DATA)
f14324a7
MS
584 {
585 DEBUG_puts("_ppdCacheCreateWithFile: Bad IPP data.");
cb7f98ee 586 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
f14324a7
MS
587 goto create_error;
588 }
589 }
590 else
591 {
592 /*
593 * Skip the IPP data entirely...
594 */
595
596 cupsFileSeek(fp, pos + length);
597 }
598
599 if (cupsFileTell(fp) != (pos + length))
600 {
601 DEBUG_puts("_ppdCacheCreateWithFile: Bad IPP data.");
cb7f98ee 602 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
f14324a7
MS
603 goto create_error;
604 }
605 }
88f9aafc 606 else if (!_cups_strcasecmp(line, "NumBins"))
f14324a7
MS
607 {
608 if (num_bins > 0)
609 {
610 DEBUG_puts("_ppdCacheCreateWithFile: NumBins listed multiple times.");
cb7f98ee 611 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
f14324a7
MS
612 goto create_error;
613 }
614
615 if ((num_bins = atoi(value)) <= 0 || num_bins > 65536)
616 {
617 DEBUG_printf(("_ppdCacheCreateWithFile: Bad NumBins value %d on line "
618 "%d.", num_sizes, linenum));
cb7f98ee 619 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
f14324a7
MS
620 goto create_error;
621 }
622
7e86f2f6 623 if ((pc->bins = calloc((size_t)num_bins, sizeof(pwg_map_t))) == NULL)
f14324a7
MS
624 {
625 DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d bins.",
626 num_sizes));
cb7f98ee 627 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
f14324a7
MS
628 goto create_error;
629 }
630 }
88f9aafc 631 else if (!_cups_strcasecmp(line, "Bin"))
f14324a7
MS
632 {
633 if (sscanf(value, "%127s%40s", pwg_keyword, ppd_keyword) != 2)
634 {
635 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Bin on line %d.", linenum));
cb7f98ee 636 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
f14324a7
MS
637 goto create_error;
638 }
639
640 if (pc->num_bins >= num_bins)
641 {
642 DEBUG_printf(("_ppdCacheCreateWithFile: Too many Bin's on line %d.",
643 linenum));
cb7f98ee 644 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
f14324a7
MS
645 goto create_error;
646 }
647
648 map = pc->bins + pc->num_bins;
649 map->pwg = _cupsStrAlloc(pwg_keyword);
650 map->ppd = _cupsStrAlloc(ppd_keyword);
651
652 pc->num_bins ++;
653 }
88f9aafc 654 else if (!_cups_strcasecmp(line, "NumSizes"))
f14324a7
MS
655 {
656 if (num_sizes > 0)
657 {
658 DEBUG_puts("_ppdCacheCreateWithFile: NumSizes listed multiple times.");
cb7f98ee 659 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
f14324a7
MS
660 goto create_error;
661 }
662
5a9febac 663 if ((num_sizes = atoi(value)) < 0 || num_sizes > 65536)
f14324a7
MS
664 {
665 DEBUG_printf(("_ppdCacheCreateWithFile: Bad NumSizes value %d on line "
666 "%d.", num_sizes, linenum));
cb7f98ee 667 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
f14324a7
MS
668 goto create_error;
669 }
670
5a9febac 671 if (num_sizes > 0)
f14324a7 672 {
7e86f2f6 673 if ((pc->sizes = calloc((size_t)num_sizes, sizeof(pwg_size_t))) == NULL)
5a9febac
MS
674 {
675 DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d sizes.",
676 num_sizes));
cb7f98ee 677 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
5a9febac
MS
678 goto create_error;
679 }
f14324a7
MS
680 }
681 }
88f9aafc 682 else if (!_cups_strcasecmp(line, "Size"))
f14324a7
MS
683 {
684 if (pc->num_sizes >= num_sizes)
685 {
686 DEBUG_printf(("_ppdCacheCreateWithFile: Too many Size's on line %d.",
687 linenum));
cb7f98ee 688 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
f14324a7
MS
689 goto create_error;
690 }
691
692 size = pc->sizes + pc->num_sizes;
693
694 if (sscanf(value, "%127s%40s%d%d%d%d%d%d", pwg_keyword, ppd_keyword,
695 &(size->width), &(size->length), &(size->left),
696 &(size->bottom), &(size->right), &(size->top)) != 8)
697 {
698 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Size on line %d.",
699 linenum));
cb7f98ee 700 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
f14324a7
MS
701 goto create_error;
702 }
703
704 size->map.pwg = _cupsStrAlloc(pwg_keyword);
705 size->map.ppd = _cupsStrAlloc(ppd_keyword);
706
707 pc->num_sizes ++;
708 }
88f9aafc 709 else if (!_cups_strcasecmp(line, "CustomSize"))
f14324a7
MS
710 {
711 if (pc->custom_max_width > 0)
712 {
713 DEBUG_printf(("_ppdCacheCreateWithFile: Too many CustomSize's on line "
714 "%d.", linenum));
cb7f98ee 715 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
f14324a7
MS
716 goto create_error;
717 }
718
719 if (sscanf(value, "%d%d%d%d%d%d%d%d", &(pc->custom_max_width),
720 &(pc->custom_max_length), &(pc->custom_min_width),
721 &(pc->custom_min_length), &(pc->custom_size.left),
722 &(pc->custom_size.bottom), &(pc->custom_size.right),
723 &(pc->custom_size.top)) != 8)
724 {
725 DEBUG_printf(("_ppdCacheCreateWithFile: Bad CustomSize on line %d.",
726 linenum));
cb7f98ee 727 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
f14324a7
MS
728 goto create_error;
729 }
730
6961465f
MS
731 pwgFormatSizeName(pwg_keyword, sizeof(pwg_keyword), "custom", "max",
732 pc->custom_max_width, pc->custom_max_length, NULL);
f14324a7
MS
733 pc->custom_max_keyword = _cupsStrAlloc(pwg_keyword);
734
6961465f
MS
735 pwgFormatSizeName(pwg_keyword, sizeof(pwg_keyword), "custom", "min",
736 pc->custom_min_width, pc->custom_min_length, NULL);
f14324a7
MS
737 pc->custom_min_keyword = _cupsStrAlloc(pwg_keyword);
738 }
88f9aafc 739 else if (!_cups_strcasecmp(line, "SourceOption"))
f14324a7
MS
740 {
741 pc->source_option = _cupsStrAlloc(value);
742 }
88f9aafc 743 else if (!_cups_strcasecmp(line, "NumSources"))
f14324a7
MS
744 {
745 if (num_sources > 0)
746 {
747 DEBUG_puts("_ppdCacheCreateWithFile: NumSources listed multiple "
748 "times.");
cb7f98ee 749 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
f14324a7
MS
750 goto create_error;
751 }
752
753 if ((num_sources = atoi(value)) <= 0 || num_sources > 65536)
754 {
755 DEBUG_printf(("_ppdCacheCreateWithFile: Bad NumSources value %d on "
756 "line %d.", num_sources, linenum));
cb7f98ee 757 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
f14324a7
MS
758 goto create_error;
759 }
760
7e86f2f6 761 if ((pc->sources = calloc((size_t)num_sources, sizeof(pwg_map_t))) == NULL)
f14324a7
MS
762 {
763 DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d sources.",
764 num_sources));
cb7f98ee 765 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
f14324a7
MS
766 goto create_error;
767 }
768 }
88f9aafc 769 else if (!_cups_strcasecmp(line, "Source"))
f14324a7
MS
770 {
771 if (sscanf(value, "%127s%40s", pwg_keyword, ppd_keyword) != 2)
772 {
773 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Source on line %d.",
774 linenum));
cb7f98ee 775 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
f14324a7
MS
776 goto create_error;
777 }
778
779 if (pc->num_sources >= num_sources)
780 {
781 DEBUG_printf(("_ppdCacheCreateWithFile: Too many Source's on line %d.",
782 linenum));
cb7f98ee 783 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
f14324a7
MS
784 goto create_error;
785 }
786
787 map = pc->sources + pc->num_sources;
788 map->pwg = _cupsStrAlloc(pwg_keyword);
789 map->ppd = _cupsStrAlloc(ppd_keyword);
790
791 pc->num_sources ++;
792 }
88f9aafc 793 else if (!_cups_strcasecmp(line, "NumTypes"))
f14324a7
MS
794 {
795 if (num_types > 0)
796 {
797 DEBUG_puts("_ppdCacheCreateWithFile: NumTypes listed multiple times.");
cb7f98ee 798 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
f14324a7
MS
799 goto create_error;
800 }
801
802 if ((num_types = atoi(value)) <= 0 || num_types > 65536)
803 {
804 DEBUG_printf(("_ppdCacheCreateWithFile: Bad NumTypes value %d on "
805 "line %d.", num_types, linenum));
cb7f98ee 806 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
f14324a7
MS
807 goto create_error;
808 }
809
7e86f2f6 810 if ((pc->types = calloc((size_t)num_types, sizeof(pwg_map_t))) == NULL)
f14324a7
MS
811 {
812 DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d types.",
813 num_types));
cb7f98ee 814 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
f14324a7
MS
815 goto create_error;
816 }
817 }
88f9aafc 818 else if (!_cups_strcasecmp(line, "Type"))
f14324a7
MS
819 {
820 if (sscanf(value, "%127s%40s", pwg_keyword, ppd_keyword) != 2)
821 {
822 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Type on line %d.",
823 linenum));
cb7f98ee 824 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
f14324a7
MS
825 goto create_error;
826 }
827
828 if (pc->num_types >= num_types)
829 {
830 DEBUG_printf(("_ppdCacheCreateWithFile: Too many Type's on line %d.",
831 linenum));
cb7f98ee 832 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
f14324a7
MS
833 goto create_error;
834 }
835
836 map = pc->types + pc->num_types;
837 map->pwg = _cupsStrAlloc(pwg_keyword);
838 map->ppd = _cupsStrAlloc(ppd_keyword);
839
840 pc->num_types ++;
841 }
88f9aafc 842 else if (!_cups_strcasecmp(line, "Preset"))
f14324a7
MS
843 {
844 /*
845 * Preset output-mode print-quality name=value ...
846 */
847
848 print_color_mode = (_pwg_print_color_mode_t)strtol(value, &valueptr, 10);
849 print_quality = (_pwg_print_quality_t)strtol(valueptr, &valueptr, 10);
850
851 if (print_color_mode < _PWG_PRINT_COLOR_MODE_MONOCHROME ||
852 print_color_mode >= _PWG_PRINT_COLOR_MODE_MAX ||
853 print_quality < _PWG_PRINT_QUALITY_DRAFT ||
854 print_quality >= _PWG_PRINT_QUALITY_MAX ||
855 valueptr == value || !*valueptr)
856 {
857 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Preset on line %d.",
858 linenum));
cb7f98ee 859 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
f14324a7
MS
860 goto create_error;
861 }
862
863 pc->num_presets[print_color_mode][print_quality] =
864 cupsParseOptions(valueptr, 0,
865 pc->presets[print_color_mode] + print_quality);
866 }
88f9aafc 867 else if (!_cups_strcasecmp(line, "SidesOption"))
f14324a7 868 pc->sides_option = _cupsStrAlloc(value);
88f9aafc 869 else if (!_cups_strcasecmp(line, "Sides1Sided"))
f14324a7 870 pc->sides_1sided = _cupsStrAlloc(value);
88f9aafc 871 else if (!_cups_strcasecmp(line, "Sides2SidedLong"))
f14324a7 872 pc->sides_2sided_long = _cupsStrAlloc(value);
88f9aafc 873 else if (!_cups_strcasecmp(line, "Sides2SidedShort"))
f14324a7 874 pc->sides_2sided_short = _cupsStrAlloc(value);
dcb445bc
MS
875 else if (!_cups_strcasecmp(line, "Finishings"))
876 {
877 if (!pc->finishings)
878 pc->finishings =
879 cupsArrayNew3((cups_array_func_t)pwg_compare_finishings,
880 NULL, NULL, 0, NULL,
881 (cups_afree_func_t)pwg_free_finishings);
882
883 if ((finishings = calloc(1, sizeof(_pwg_finishings_t))) == NULL)
884 goto create_error;
885
7e86f2f6 886 finishings->value = (ipp_finishings_t)strtol(value, &valueptr, 10);
dcb445bc
MS
887 finishings->num_options = cupsParseOptions(valueptr, 0,
888 &(finishings->options));
889
890 cupsArrayAdd(pc->finishings, finishings);
891 }
4f63d6cd
MS
892 else if (!_cups_strcasecmp(line, "FinishingTemplate"))
893 {
894 if (!pc->templates)
895 pc->templates = cupsArrayNew3((cups_array_func_t)strcmp, NULL, NULL, 0, (cups_acopy_func_t)_cupsStrAlloc, (cups_afree_func_t)_cupsStrFree);
896
897 cupsArrayAdd(pc->templates, value);
898 }
3e7fe0ca
MS
899 else if (!_cups_strcasecmp(line, "MaxCopies"))
900 pc->max_copies = atoi(value);
a469f8a5
MS
901 else if (!_cups_strcasecmp(line, "ChargeInfoURI"))
902 pc->charge_info_uri = _cupsStrAlloc(value);
5a9febac
MS
903 else if (!_cups_strcasecmp(line, "JobAccountId"))
904 pc->account_id = !_cups_strcasecmp(value, "true");
905 else if (!_cups_strcasecmp(line, "JobAccountingUserId"))
906 pc->accounting_user_id = !_cups_strcasecmp(value, "true");
907 else if (!_cups_strcasecmp(line, "JobPassword"))
908 pc->password = _cupsStrAlloc(value);
909 else if (!_cups_strcasecmp(line, "Mandatory"))
910 {
911 if (pc->mandatory)
912 _cupsArrayAddStrings(pc->mandatory, value, ' ');
913 else
914 pc->mandatory = _cupsArrayNewStrings(value, ' ');
915 }
89550f3f
MS
916 else if (!_cups_strcasecmp(line, "StringsURI"))
917 pc->strings_uri = _cupsStrAlloc(value);
c1420c87
MS
918 else if (!_cups_strcasecmp(line, "SupportFile"))
919 {
920 if (!pc->support_files)
921 pc->support_files = cupsArrayNew3(NULL, NULL, NULL, 0,
922 (cups_acopy_func_t)_cupsStrAlloc,
923 (cups_afree_func_t)_cupsStrFree);
924
925 cupsArrayAdd(pc->support_files, value);
926 }
f14324a7
MS
927 else
928 {
929 DEBUG_printf(("_ppdCacheCreateWithFile: Unknown %s on line %d.", line,
930 linenum));
f14324a7
MS
931 }
932 }
933
934 if (pc->num_sizes < num_sizes)
935 {
936 DEBUG_printf(("_ppdCacheCreateWithFile: Not enough sizes (%d < %d).",
937 pc->num_sizes, num_sizes));
cb7f98ee 938 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
f14324a7
MS
939 goto create_error;
940 }
941
942 if (pc->num_sources < num_sources)
943 {
944 DEBUG_printf(("_ppdCacheCreateWithFile: Not enough sources (%d < %d).",
945 pc->num_sources, num_sources));
cb7f98ee 946 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
f14324a7
MS
947 goto create_error;
948 }
949
950 if (pc->num_types < num_types)
951 {
952 DEBUG_printf(("_ppdCacheCreateWithFile: Not enough types (%d < %d).",
953 pc->num_types, num_types));
cb7f98ee 954 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad PPD cache file."), 1);
f14324a7
MS
955 goto create_error;
956 }
957
958 cupsFileClose(fp);
959
960 return (pc);
961
962 /*
963 * If we get here the file was bad - free any data and return...
964 */
965
966 create_error:
967
968 cupsFileClose(fp);
969 _ppdCacheDestroy(pc);
970
971 if (attrs)
22c9029b 972 {
f14324a7 973 ippDelete(*attrs);
22c9029b
MS
974 *attrs = NULL;
975 }
f14324a7
MS
976
977 return (NULL);
978}
979
980
981/*
982 * '_ppdCacheCreateWithPPD()' - Create PWG mapping data from a PPD file.
983 */
984
985_ppd_cache_t * /* O - PPD cache and mapping data */
986_ppdCacheCreateWithPPD(ppd_file_t *ppd) /* I - PPD file */
987{
988 int i, j, k; /* Looping vars */
989 _ppd_cache_t *pc; /* PWG mapping data */
990 ppd_option_t *input_slot, /* InputSlot option */
991 *media_type, /* MediaType option */
992 *output_bin, /* OutputBin option */
993 *color_model, /* ColorModel option */
4f63d6cd
MS
994 *duplex, /* Duplex option */
995 *ppd_option; /* Other PPD option */
f14324a7 996 ppd_choice_t *choice; /* Current InputSlot/MediaType */
6961465f 997 pwg_map_t *map; /* Current source/type map */
f14324a7
MS
998 ppd_attr_t *ppd_attr; /* Current PPD preset attribute */
999 int num_options; /* Number of preset options and props */
1000 cups_option_t *options; /* Preset options and properties */
1001 ppd_size_t *ppd_size; /* Current PPD size */
6961465f 1002 pwg_size_t *pwg_size; /* Current PWG size */
f14324a7
MS
1003 char pwg_keyword[3 + PPD_MAX_NAME + 1 + 12 + 1 + 12 + 3],
1004 /* PWG keyword string */
1005 ppd_name[PPD_MAX_NAME];
1006 /* Normalized PPD name */
1007 const char *pwg_name; /* Standard PWG media name */
6961465f 1008 pwg_media_t *pwg_media; /* PWG media data */
f14324a7
MS
1009 _pwg_print_color_mode_t pwg_print_color_mode;
1010 /* print-color-mode index */
1011 _pwg_print_quality_t pwg_print_quality;
1012 /* print-quality index */
1013 int similar; /* Are the old and new size similar? */
6961465f 1014 pwg_size_t *old_size; /* Current old size */
f14324a7
MS
1015 int old_imageable, /* Old imageable length in 2540ths */
1016 old_borderless, /* Old borderless state */
1017 old_known_pwg; /* Old PWG name is well-known */
1018 int new_width, /* New width in 2540ths */
1019 new_length, /* New length in 2540ths */
1020 new_left, /* New left margin in 2540ths */
1021 new_bottom, /* New bottom margin in 2540ths */
1022 new_right, /* New right margin in 2540ths */
1023 new_top, /* New top margin in 2540ths */
1024 new_imageable, /* New imageable length in 2540ths */
1025 new_borderless, /* New borderless state */
1026 new_known_pwg; /* New PWG name is well-known */
6961465f 1027 pwg_size_t *new_size; /* New size to add, if any */
f14324a7 1028 const char *filter; /* Current filter */
dcb445bc 1029 _pwg_finishings_t *finishings; /* Current finishings value */
f14324a7
MS
1030
1031
1032 DEBUG_printf(("_ppdCacheCreateWithPPD(ppd=%p)", ppd));
1033
1034 /*
1035 * Range check input...
1036 */
1037
1038 if (!ppd)
1039 return (NULL);
1040
1041 /*
1042 * Allocate memory...
1043 */
1044
1045 if ((pc = calloc(1, sizeof(_ppd_cache_t))) == NULL)
1046 {
1047 DEBUG_puts("_ppdCacheCreateWithPPD: Unable to allocate _ppd_cache_t.");
1048 goto create_error;
1049 }
1050
1051 /*
1052 * Copy and convert size data...
1053 */
1054
5a9febac 1055 if (ppd->num_sizes > 0)
f14324a7 1056 {
7e86f2f6 1057 if ((pc->sizes = calloc((size_t)ppd->num_sizes, sizeof(pwg_size_t))) == NULL)
f14324a7 1058 {
5a9febac 1059 DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d "
6961465f 1060 "pwg_size_t's.", ppd->num_sizes));
5a9febac 1061 goto create_error;
f14324a7
MS
1062 }
1063
5a9febac
MS
1064 for (i = ppd->num_sizes, pwg_size = pc->sizes, ppd_size = ppd->sizes;
1065 i > 0;
1066 i --, ppd_size ++)
f14324a7
MS
1067 {
1068 /*
5a9febac 1069 * Don't copy over custom size...
f14324a7
MS
1070 */
1071
5a9febac
MS
1072 if (!_cups_strcasecmp(ppd_size->name, "Custom"))
1073 continue;
1074
f14324a7 1075 /*
5a9febac 1076 * Convert the PPD size name to the corresponding PWG keyword name.
f14324a7
MS
1077 */
1078
6961465f 1079 if ((pwg_media = pwgMediaForPPD(ppd_size->name)) != NULL)
5a9febac
MS
1080 {
1081 /*
1082 * Standard name, do we have conflicts?
1083 */
f14324a7 1084
5a9febac
MS
1085 for (j = 0; j < pc->num_sizes; j ++)
1086 if (!strcmp(pc->sizes[j].map.pwg, pwg_media->pwg))
1087 {
1088 pwg_media = NULL;
1089 break;
1090 }
1091 }
f14324a7 1092
5a9febac
MS
1093 if (pwg_media)
1094 {
1095 /*
1096 * Standard name and no conflicts, use it!
1097 */
f14324a7 1098
5a9febac
MS
1099 pwg_name = pwg_media->pwg;
1100 new_known_pwg = 1;
1101 }
1102 else
f14324a7
MS
1103 {
1104 /*
5a9febac
MS
1105 * Not a standard name; convert it to a PWG vendor name of the form:
1106 *
1107 * pp_lowerppd_WIDTHxHEIGHTuu
f14324a7
MS
1108 */
1109
5a9febac
MS
1110 pwg_name = pwg_keyword;
1111 new_known_pwg = 0;
1112
c1420c87 1113 pwg_unppdize_name(ppd_size->name, ppd_name, sizeof(ppd_name), "_.");
6961465f
MS
1114 pwgFormatSizeName(pwg_keyword, sizeof(pwg_keyword), NULL, ppd_name,
1115 PWG_FROM_POINTS(ppd_size->width),
1116 PWG_FROM_POINTS(ppd_size->length), NULL);
f14324a7 1117 }
f14324a7 1118
f14324a7 1119 /*
5a9febac
MS
1120 * If we have a similar paper with non-zero margins then we only want to
1121 * keep it if it has a larger imageable area length. The NULL check is for
1122 * dimensions that are <= 0...
f14324a7
MS
1123 */
1124
c3ebc4c6
MS
1125 if ((pwg_media = _pwgMediaNearSize(PWG_FROM_POINTS(ppd_size->width),
1126 PWG_FROM_POINTS(ppd_size->length),
1127 0)) == NULL)
5a9febac 1128 continue;
f14324a7 1129
5a9febac
MS
1130 new_width = pwg_media->width;
1131 new_length = pwg_media->length;
6961465f
MS
1132 new_left = PWG_FROM_POINTS(ppd_size->left);
1133 new_bottom = PWG_FROM_POINTS(ppd_size->bottom);
1134 new_right = PWG_FROM_POINTS(ppd_size->width - ppd_size->right);
1135 new_top = PWG_FROM_POINTS(ppd_size->length - ppd_size->top);
5a9febac
MS
1136 new_imageable = new_length - new_top - new_bottom;
1137 new_borderless = new_bottom == 0 && new_top == 0 &&
1138 new_left == 0 && new_right == 0;
1139
1140 for (k = pc->num_sizes, similar = 0, old_size = pc->sizes, new_size = NULL;
1141 k > 0 && !similar;
1142 k --, old_size ++)
1143 {
1144 old_imageable = old_size->length - old_size->top - old_size->bottom;
1145 old_borderless = old_size->left == 0 && old_size->bottom == 0 &&
1146 old_size->right == 0 && old_size->top == 0;
1147 old_known_pwg = strncmp(old_size->map.pwg, "oe_", 3) &&
1148 strncmp(old_size->map.pwg, "om_", 3);
1149
1150 similar = old_borderless == new_borderless &&
1151 _PWG_EQUIVALENT(old_size->width, new_width) &&
1152 _PWG_EQUIVALENT(old_size->length, new_length);
1153
1154 if (similar &&
1155 (new_known_pwg || (!old_known_pwg && new_imageable > old_imageable)))
1156 {
1157 /*
1158 * The new paper has a larger imageable area so it could replace
1159 * the older paper. Regardless of the imageable area, we always
1160 * prefer the size with a well-known PWG name.
1161 */
1162
1163 new_size = old_size;
1164 _cupsStrFree(old_size->map.ppd);
1165 _cupsStrFree(old_size->map.pwg);
1166 }
1167 }
1168
1169 if (!similar)
1170 {
1171 /*
1172 * The paper was unique enough to deserve its own entry so add it to the
1173 * end.
1174 */
1175
1176 new_size = pwg_size ++;
1177 pc->num_sizes ++;
1178 }
1179
1180 if (new_size)
1181 {
1182 /*
1183 * Save this size...
1184 */
f14324a7 1185
5a9febac
MS
1186 new_size->map.ppd = _cupsStrAlloc(ppd_size->name);
1187 new_size->map.pwg = _cupsStrAlloc(pwg_name);
1188 new_size->width = new_width;
1189 new_size->length = new_length;
1190 new_size->left = new_left;
1191 new_size->bottom = new_bottom;
1192 new_size->right = new_right;
1193 new_size->top = new_top;
1194 }
f14324a7
MS
1195 }
1196 }
1197
1198 if (ppd->variable_sizes)
1199 {
1200 /*
1201 * Generate custom size data...
1202 */
1203
6961465f
MS
1204 pwgFormatSizeName(pwg_keyword, sizeof(pwg_keyword), "custom", "max",
1205 PWG_FROM_POINTS(ppd->custom_max[0]),
1206 PWG_FROM_POINTS(ppd->custom_max[1]), NULL);
f14324a7 1207 pc->custom_max_keyword = _cupsStrAlloc(pwg_keyword);
6961465f
MS
1208 pc->custom_max_width = PWG_FROM_POINTS(ppd->custom_max[0]);
1209 pc->custom_max_length = PWG_FROM_POINTS(ppd->custom_max[1]);
f14324a7 1210
6961465f
MS
1211 pwgFormatSizeName(pwg_keyword, sizeof(pwg_keyword), "custom", "min",
1212 PWG_FROM_POINTS(ppd->custom_min[0]),
1213 PWG_FROM_POINTS(ppd->custom_min[1]), NULL);
f14324a7 1214 pc->custom_min_keyword = _cupsStrAlloc(pwg_keyword);
6961465f
MS
1215 pc->custom_min_width = PWG_FROM_POINTS(ppd->custom_min[0]);
1216 pc->custom_min_length = PWG_FROM_POINTS(ppd->custom_min[1]);
f14324a7 1217
6961465f
MS
1218 pc->custom_size.left = PWG_FROM_POINTS(ppd->custom_margins[0]);
1219 pc->custom_size.bottom = PWG_FROM_POINTS(ppd->custom_margins[1]);
1220 pc->custom_size.right = PWG_FROM_POINTS(ppd->custom_margins[2]);
1221 pc->custom_size.top = PWG_FROM_POINTS(ppd->custom_margins[3]);
f14324a7
MS
1222 }
1223
1224 /*
1225 * Copy and convert InputSlot data...
1226 */
1227
1228 if ((input_slot = ppdFindOption(ppd, "InputSlot")) == NULL)
1229 input_slot = ppdFindOption(ppd, "HPPaperSource");
1230
1231 if (input_slot)
1232 {
1233 pc->source_option = _cupsStrAlloc(input_slot->keyword);
1234
7e86f2f6 1235 if ((pc->sources = calloc((size_t)input_slot->num_choices, sizeof(pwg_map_t))) == NULL)
f14324a7
MS
1236 {
1237 DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d "
6961465f 1238 "pwg_map_t's for InputSlot.", input_slot->num_choices));
f14324a7
MS
1239 goto create_error;
1240 }
1241
1242 pc->num_sources = input_slot->num_choices;
1243
1244 for (i = input_slot->num_choices, choice = input_slot->choices,
1245 map = pc->sources;
1246 i > 0;
1247 i --, choice ++, map ++)
1248 {
88f9aafc
MS
1249 if (!_cups_strncasecmp(choice->choice, "Auto", 4) ||
1250 !_cups_strcasecmp(choice->choice, "Default"))
f14324a7 1251 pwg_name = "auto";
88f9aafc 1252 else if (!_cups_strcasecmp(choice->choice, "Cassette"))
f14324a7 1253 pwg_name = "main";
88f9aafc 1254 else if (!_cups_strcasecmp(choice->choice, "PhotoTray"))
f14324a7 1255 pwg_name = "photo";
88f9aafc 1256 else if (!_cups_strcasecmp(choice->choice, "CDTray"))
f14324a7 1257 pwg_name = "disc";
88f9aafc
MS
1258 else if (!_cups_strncasecmp(choice->choice, "Multipurpose", 12) ||
1259 !_cups_strcasecmp(choice->choice, "MP") ||
1260 !_cups_strcasecmp(choice->choice, "MPTray"))
12f89d24 1261 pwg_name = "by-pass-tray";
88f9aafc 1262 else if (!_cups_strcasecmp(choice->choice, "LargeCapacity"))
f14324a7 1263 pwg_name = "large-capacity";
88f9aafc 1264 else if (!_cups_strncasecmp(choice->choice, "Lower", 5))
f14324a7 1265 pwg_name = "bottom";
88f9aafc 1266 else if (!_cups_strncasecmp(choice->choice, "Middle", 6))
f14324a7 1267 pwg_name = "middle";
88f9aafc 1268 else if (!_cups_strncasecmp(choice->choice, "Upper", 5))
f14324a7 1269 pwg_name = "top";
88f9aafc 1270 else if (!_cups_strncasecmp(choice->choice, "Side", 4))
f14324a7 1271 pwg_name = "side";
a4845881 1272 else if (!_cups_strcasecmp(choice->choice, "Roll"))
f14324a7 1273 pwg_name = "main-roll";
f14324a7
MS
1274 else
1275 {
1276 /*
1277 * Convert PPD name to lowercase...
1278 */
1279
1280 pwg_name = pwg_keyword;
c1420c87
MS
1281 pwg_unppdize_name(choice->choice, pwg_keyword, sizeof(pwg_keyword),
1282 "_");
f14324a7
MS
1283 }
1284
1285 map->pwg = _cupsStrAlloc(pwg_name);
1286 map->ppd = _cupsStrAlloc(choice->choice);
1287 }
1288 }
1289
1290 /*
1291 * Copy and convert MediaType data...
1292 */
1293
1294 if ((media_type = ppdFindOption(ppd, "MediaType")) != NULL)
1295 {
7e86f2f6 1296 if ((pc->types = calloc((size_t)media_type->num_choices, sizeof(pwg_map_t))) == NULL)
f14324a7
MS
1297 {
1298 DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d "
6961465f 1299 "pwg_map_t's for MediaType.", media_type->num_choices));
f14324a7
MS
1300 goto create_error;
1301 }
1302
1303 pc->num_types = media_type->num_choices;
1304
1305 for (i = media_type->num_choices, choice = media_type->choices,
1306 map = pc->types;
1307 i > 0;
1308 i --, choice ++, map ++)
1309 {
88f9aafc
MS
1310 if (!_cups_strncasecmp(choice->choice, "Auto", 4) ||
1311 !_cups_strcasecmp(choice->choice, "Any") ||
1312 !_cups_strcasecmp(choice->choice, "Default"))
f14324a7 1313 pwg_name = "auto";
88f9aafc 1314 else if (!_cups_strncasecmp(choice->choice, "Card", 4))
f14324a7 1315 pwg_name = "cardstock";
88f9aafc 1316 else if (!_cups_strncasecmp(choice->choice, "Env", 3))
f14324a7 1317 pwg_name = "envelope";
88f9aafc 1318 else if (!_cups_strncasecmp(choice->choice, "Gloss", 5))
f14324a7 1319 pwg_name = "photographic-glossy";
88f9aafc 1320 else if (!_cups_strcasecmp(choice->choice, "HighGloss"))
f14324a7 1321 pwg_name = "photographic-high-gloss";
88f9aafc 1322 else if (!_cups_strcasecmp(choice->choice, "Matte"))
f14324a7 1323 pwg_name = "photographic-matte";
88f9aafc 1324 else if (!_cups_strncasecmp(choice->choice, "Plain", 5))
f14324a7 1325 pwg_name = "stationery";
88f9aafc 1326 else if (!_cups_strncasecmp(choice->choice, "Coated", 6))
f14324a7 1327 pwg_name = "stationery-coated";
88f9aafc 1328 else if (!_cups_strcasecmp(choice->choice, "Inkjet"))
f14324a7 1329 pwg_name = "stationery-inkjet";
88f9aafc 1330 else if (!_cups_strcasecmp(choice->choice, "Letterhead"))
f14324a7 1331 pwg_name = "stationery-letterhead";
88f9aafc 1332 else if (!_cups_strncasecmp(choice->choice, "Preprint", 8))
f14324a7 1333 pwg_name = "stationery-preprinted";
a4845881
MS
1334 else if (!_cups_strcasecmp(choice->choice, "Recycled"))
1335 pwg_name = "stationery-recycled";
88f9aafc 1336 else if (!_cups_strncasecmp(choice->choice, "Transparen", 10))
f14324a7
MS
1337 pwg_name = "transparency";
1338 else
1339 {
1340 /*
1341 * Convert PPD name to lowercase...
1342 */
1343
1344 pwg_name = pwg_keyword;
c1420c87
MS
1345 pwg_unppdize_name(choice->choice, pwg_keyword, sizeof(pwg_keyword),
1346 "_");
f14324a7
MS
1347 }
1348
1349 map->pwg = _cupsStrAlloc(pwg_name);
1350 map->ppd = _cupsStrAlloc(choice->choice);
1351 }
1352 }
1353
f14324a7
MS
1354 /*
1355 * Copy and convert OutputBin data...
1356 */
1357
1358 if ((output_bin = ppdFindOption(ppd, "OutputBin")) != NULL)
1359 {
7e86f2f6 1360 if ((pc->bins = calloc((size_t)output_bin->num_choices, sizeof(pwg_map_t))) == NULL)
f14324a7
MS
1361 {
1362 DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d "
6961465f 1363 "pwg_map_t's for OutputBin.", output_bin->num_choices));
f14324a7
MS
1364 goto create_error;
1365 }
1366
1367 pc->num_bins = output_bin->num_choices;
1368
1369 for (i = output_bin->num_choices, choice = output_bin->choices,
1370 map = pc->bins;
1371 i > 0;
1372 i --, choice ++, map ++)
1373 {
c1420c87 1374 pwg_unppdize_name(choice->choice, pwg_keyword, sizeof(pwg_keyword), "_");
f14324a7
MS
1375
1376 map->pwg = _cupsStrAlloc(pwg_keyword);
1377 map->ppd = _cupsStrAlloc(choice->choice);
1378 }
1379 }
1380
1381 if ((ppd_attr = ppdFindAttr(ppd, "APPrinterPreset", NULL)) != NULL)
1382 {
1383 /*
1384 * Copy and convert APPrinterPreset (output-mode + print-quality) data...
1385 */
1386
1387 const char *quality, /* com.apple.print.preset.quality value */
1388 *output_mode, /* com.apple.print.preset.output-mode value */
1389 *color_model_val, /* ColorModel choice */
1390 *graphicsType, /* com.apple.print.preset.graphicsType value */
1391 *media_front_coating; /* com.apple.print.preset.media-front-coating value */
1392
1393 do
1394 {
1395 num_options = _ppdParseOptions(ppd_attr->value, 0, &options,
1396 _PPD_PARSE_ALL);
1397
1398 if ((quality = cupsGetOption("com.apple.print.preset.quality",
1399 num_options, options)) != NULL)
1400 {
1401 /*
1402 * Get the print-quality for this preset...
1403 */
1404
1405 if (!strcmp(quality, "low"))
1406 pwg_print_quality = _PWG_PRINT_QUALITY_DRAFT;
1407 else if (!strcmp(quality, "high"))
1408 pwg_print_quality = _PWG_PRINT_QUALITY_HIGH;
1409 else
1410 pwg_print_quality = _PWG_PRINT_QUALITY_NORMAL;
1411
1412 /*
1413 * Ignore graphicsType "Photo" presets that are not high quality.
1414 */
1415
1416 graphicsType = cupsGetOption("com.apple.print.preset.graphicsType",
1417 num_options, options);
1418
1419 if (pwg_print_quality != _PWG_PRINT_QUALITY_HIGH && graphicsType &&
1420 !strcmp(graphicsType, "Photo"))
1421 continue;
1422
1423 /*
1424 * Ignore presets for normal and draft quality where the coating
1425 * isn't "none" or "autodetect".
1426 */
1427
1428 media_front_coating = cupsGetOption(
1429 "com.apple.print.preset.media-front-coating",
1430 num_options, options);
1431
1432 if (pwg_print_quality != _PWG_PRINT_QUALITY_HIGH &&
1433 media_front_coating &&
1434 strcmp(media_front_coating, "none") &&
1435 strcmp(media_front_coating, "autodetect"))
1436 continue;
1437
1438 /*
1439 * Get the output mode for this preset...
1440 */
1441
1442 output_mode = cupsGetOption("com.apple.print.preset.output-mode",
1443 num_options, options);
1444 color_model_val = cupsGetOption("ColorModel", num_options, options);
1445
1446 if (output_mode)
1447 {
1448 if (!strcmp(output_mode, "monochrome"))
1449 pwg_print_color_mode = _PWG_PRINT_COLOR_MODE_MONOCHROME;
1450 else
1451 pwg_print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR;
1452 }
1453 else if (color_model_val)
1454 {
88f9aafc 1455 if (!_cups_strcasecmp(color_model_val, "Gray"))
f14324a7
MS
1456 pwg_print_color_mode = _PWG_PRINT_COLOR_MODE_MONOCHROME;
1457 else
1458 pwg_print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR;
1459 }
1460 else
1461 pwg_print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR;
1462
1463 /*
1464 * Save the options for this combination as needed...
1465 */
1466
1467 if (!pc->num_presets[pwg_print_color_mode][pwg_print_quality])
1468 pc->num_presets[pwg_print_color_mode][pwg_print_quality] =
1469 _ppdParseOptions(ppd_attr->value, 0,
1470 pc->presets[pwg_print_color_mode] +
1471 pwg_print_quality, _PPD_PARSE_OPTIONS);
1472 }
1473
1474 cupsFreeOptions(num_options, options);
1475 }
1476 while ((ppd_attr = ppdFindNextAttr(ppd, "APPrinterPreset", NULL)) != NULL);
1477 }
1478
1479 if (!pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][_PWG_PRINT_QUALITY_DRAFT] &&
1480 !pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][_PWG_PRINT_QUALITY_NORMAL] &&
1481 !pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][_PWG_PRINT_QUALITY_HIGH])
1482 {
1483 /*
1484 * Try adding some common color options to create grayscale presets. These
1485 * are listed in order of popularity...
1486 */
1487
1488 const char *color_option = NULL, /* Color control option */
1489 *gray_choice = NULL; /* Choice to select grayscale */
1490
1491 if ((color_model = ppdFindOption(ppd, "ColorModel")) != NULL &&
1492 ppdFindChoice(color_model, "Gray"))
1493 {
1494 color_option = "ColorModel";
1495 gray_choice = "Gray";
1496 }
1497 else if ((color_model = ppdFindOption(ppd, "HPColorMode")) != NULL &&
1498 ppdFindChoice(color_model, "grayscale"))
1499 {
1500 color_option = "HPColorMode";
1501 gray_choice = "grayscale";
1502 }
1503 else if ((color_model = ppdFindOption(ppd, "BRMonoColor")) != NULL &&
1504 ppdFindChoice(color_model, "Mono"))
1505 {
1506 color_option = "BRMonoColor";
1507 gray_choice = "Mono";
1508 }
1509 else if ((color_model = ppdFindOption(ppd, "CNIJSGrayScale")) != NULL &&
1510 ppdFindChoice(color_model, "1"))
1511 {
1512 color_option = "CNIJSGrayScale";
1513 gray_choice = "1";
1514 }
1515 else if ((color_model = ppdFindOption(ppd, "HPColorAsGray")) != NULL &&
1516 ppdFindChoice(color_model, "True"))
1517 {
1518 color_option = "HPColorAsGray";
1519 gray_choice = "True";
1520 }
1521
1522 if (color_option && gray_choice)
1523 {
1524 /*
1525 * Copy and convert ColorModel (output-mode) data...
1526 */
1527
1528 cups_option_t *coption, /* Color option */
1529 *moption; /* Monochrome option */
1530
1531 for (pwg_print_quality = _PWG_PRINT_QUALITY_DRAFT;
1532 pwg_print_quality < _PWG_PRINT_QUALITY_MAX;
1533 pwg_print_quality ++)
1534 {
1535 if (pc->num_presets[_PWG_PRINT_COLOR_MODE_COLOR][pwg_print_quality])
1536 {
1537 /*
1538 * Copy the color options...
1539 */
1540
1541 num_options = pc->num_presets[_PWG_PRINT_COLOR_MODE_COLOR]
1542 [pwg_print_quality];
7e86f2f6 1543 options = calloc(sizeof(cups_option_t), (size_t)num_options);
f14324a7
MS
1544
1545 if (options)
1546 {
1547 for (i = num_options, moption = options,
1548 coption = pc->presets[_PWG_PRINT_COLOR_MODE_COLOR]
1549 [pwg_print_quality];
1550 i > 0;
1551 i --, moption ++, coption ++)
1552 {
1553 moption->name = _cupsStrRetain(coption->name);
1554 moption->value = _cupsStrRetain(coption->value);
1555 }
1556
1557 pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][pwg_print_quality] =
1558 num_options;
1559 pc->presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][pwg_print_quality] =
1560 options;
1561 }
1562 }
1563 else if (pwg_print_quality != _PWG_PRINT_QUALITY_NORMAL)
1564 continue;
1565
1566 /*
1567 * Add the grayscale option to the preset...
1568 */
1569
1570 pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][pwg_print_quality] =
1571 cupsAddOption(color_option, gray_choice,
1572 pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME]
1573 [pwg_print_quality],
1574 pc->presets[_PWG_PRINT_COLOR_MODE_MONOCHROME] +
1575 pwg_print_quality);
1576 }
1577 }
1578 }
1579
1580 /*
1581 * Copy and convert Duplex (sides) data...
1582 */
1583
1584 if ((duplex = ppdFindOption(ppd, "Duplex")) == NULL)
1585 if ((duplex = ppdFindOption(ppd, "JCLDuplex")) == NULL)
1586 if ((duplex = ppdFindOption(ppd, "EFDuplex")) == NULL)
1587 if ((duplex = ppdFindOption(ppd, "EFDuplexing")) == NULL)
1588 duplex = ppdFindOption(ppd, "KD03Duplex");
1589
1590 if (duplex)
1591 {
1592 pc->sides_option = _cupsStrAlloc(duplex->keyword);
1593
1594 for (i = duplex->num_choices, choice = duplex->choices;
1595 i > 0;
1596 i --, choice ++)
1597 {
88f9aafc
MS
1598 if ((!_cups_strcasecmp(choice->choice, "None") ||
1599 !_cups_strcasecmp(choice->choice, "False")) && !pc->sides_1sided)
f14324a7 1600 pc->sides_1sided = _cupsStrAlloc(choice->choice);
88f9aafc
MS
1601 else if ((!_cups_strcasecmp(choice->choice, "DuplexNoTumble") ||
1602 !_cups_strcasecmp(choice->choice, "LongEdge") ||
1603 !_cups_strcasecmp(choice->choice, "Top")) && !pc->sides_2sided_long)
f14324a7 1604 pc->sides_2sided_long = _cupsStrAlloc(choice->choice);
88f9aafc
MS
1605 else if ((!_cups_strcasecmp(choice->choice, "DuplexTumble") ||
1606 !_cups_strcasecmp(choice->choice, "ShortEdge") ||
1607 !_cups_strcasecmp(choice->choice, "Bottom")) &&
f14324a7
MS
1608 !pc->sides_2sided_short)
1609 pc->sides_2sided_short = _cupsStrAlloc(choice->choice);
1610 }
1611 }
1612
1613 /*
1614 * Copy filters and pre-filters...
1615 */
1616
1617 pc->filters = cupsArrayNew3(NULL, NULL, NULL, 0,
1618 (cups_acopy_func_t)_cupsStrAlloc,
1619 (cups_afree_func_t)_cupsStrFree);
1620
1621 cupsArrayAdd(pc->filters,
1622 "application/vnd.cups-raw application/octet-stream 0 -");
1623
1624 if ((ppd_attr = ppdFindAttr(ppd, "cupsFilter2", NULL)) != NULL)
1625 {
1626 do
1627 {
1628 cupsArrayAdd(pc->filters, ppd_attr->value);
1629 }
1630 while ((ppd_attr = ppdFindNextAttr(ppd, "cupsFilter2", NULL)) != NULL);
1631 }
1632 else if (ppd->num_filters > 0)
1633 {
1634 for (i = 0; i < ppd->num_filters; i ++)
1635 cupsArrayAdd(pc->filters, ppd->filters[i]);
1636 }
1637 else
1638 cupsArrayAdd(pc->filters, "application/vnd.cups-postscript 0 -");
1639
1640 /*
1641 * See if we have a command filter...
1642 */
1643
1644 for (filter = (const char *)cupsArrayFirst(pc->filters);
1645 filter;
1646 filter = (const char *)cupsArrayNext(pc->filters))
88f9aafc 1647 if (!_cups_strncasecmp(filter, "application/vnd.cups-command", 28) &&
f14324a7
MS
1648 _cups_isspace(filter[28]))
1649 break;
1650
1651 if (!filter &&
1652 ((ppd_attr = ppdFindAttr(ppd, "cupsCommands", NULL)) == NULL ||
88f9aafc 1653 _cups_strcasecmp(ppd_attr->value, "none")))
f14324a7
MS
1654 {
1655 /*
1656 * No command filter and no cupsCommands keyword telling us not to use one.
1657 * See if this is a PostScript printer, and if so add a PostScript command
1658 * filter...
1659 */
1660
1661 for (filter = (const char *)cupsArrayFirst(pc->filters);
1662 filter;
1663 filter = (const char *)cupsArrayNext(pc->filters))
88f9aafc 1664 if (!_cups_strncasecmp(filter, "application/vnd.cups-postscript", 31) &&
f14324a7
MS
1665 _cups_isspace(filter[31]))
1666 break;
1667
1668 if (filter)
1669 cupsArrayAdd(pc->filters,
a2326b5b
MS
1670 "application/vnd.cups-command application/postscript 100 "
1671 "commandtops");
f14324a7
MS
1672 }
1673
1674 if ((ppd_attr = ppdFindAttr(ppd, "cupsPreFilter", NULL)) != NULL)
1675 {
1676 pc->prefilters = cupsArrayNew3(NULL, NULL, NULL, 0,
1677 (cups_acopy_func_t)_cupsStrAlloc,
1678 (cups_afree_func_t)_cupsStrFree);
1679
1680 do
1681 {
1682 cupsArrayAdd(pc->prefilters, ppd_attr->value);
1683 }
1684 while ((ppd_attr = ppdFindNextAttr(ppd, "cupsPreFilter", NULL)) != NULL);
1685 }
1686
82f97232 1687 if ((ppd_attr = ppdFindAttr(ppd, "cupsSingleFile", NULL)) != NULL)
88f9aafc 1688 pc->single_file = !_cups_strcasecmp(ppd_attr->value, "true");
82f97232 1689
f14324a7
MS
1690 /*
1691 * Copy the product string, if any...
1692 */
1693
1694 if (ppd->product)
1695 pc->product = _cupsStrAlloc(ppd->product);
1696
dcb445bc
MS
1697 /*
1698 * Copy finishings mapping data...
1699 */
1700
1701 if ((ppd_attr = ppdFindAttr(ppd, "cupsIPPFinishings", NULL)) != NULL)
1702 {
1fbd0cab
MS
1703 /*
1704 * Have proper vendor mapping of IPP finishings values to PPD options...
1705 */
1706
dcb445bc
MS
1707 pc->finishings = cupsArrayNew3((cups_array_func_t)pwg_compare_finishings,
1708 NULL, NULL, 0, NULL,
1709 (cups_afree_func_t)pwg_free_finishings);
1710
1711 do
1712 {
1713 if ((finishings = calloc(1, sizeof(_pwg_finishings_t))) == NULL)
1714 goto create_error;
1715
7e86f2f6 1716 finishings->value = (ipp_finishings_t)atoi(ppd_attr->spec);
dcb445bc
MS
1717 finishings->num_options = _ppdParseOptions(ppd_attr->value, 0,
1718 &(finishings->options),
1719 _PPD_PARSE_OPTIONS);
1720
1721 cupsArrayAdd(pc->finishings, finishings);
1722 }
1723 while ((ppd_attr = ppdFindNextAttr(ppd, "cupsIPPFinishings",
1724 NULL)) != NULL);
1725 }
1fbd0cab
MS
1726 else
1727 {
1728 /*
1729 * No IPP mapping data, try to map common/standard PPD keywords...
1730 */
1731
1fbd0cab
MS
1732 pc->finishings = cupsArrayNew3((cups_array_func_t)pwg_compare_finishings, NULL, NULL, 0, NULL, (cups_afree_func_t)pwg_free_finishings);
1733
1734 if ((ppd_option = ppdFindOption(ppd, "StapleLocation")) != NULL)
1735 {
1736 /*
1737 * Add staple finishings...
1738 */
1739
1740 if (ppdFindChoice(ppd_option, "SinglePortrait"))
1741 pwg_add_finishing(pc->finishings, IPP_FINISHINGS_STAPLE_TOP_LEFT, "StapleLocation", "SinglePortrait");
1742 if (ppdFindChoice(ppd_option, "UpperLeft")) /* Ricoh extension */
1743 pwg_add_finishing(pc->finishings, IPP_FINISHINGS_STAPLE_TOP_LEFT, "StapleLocation", "UpperLeft");
1744 if (ppdFindChoice(ppd_option, "UpperRight")) /* Ricoh extension */
1745 pwg_add_finishing(pc->finishings, IPP_FINISHINGS_STAPLE_TOP_RIGHT, "StapleLocation", "UpperRight");
1746 if (ppdFindChoice(ppd_option, "SingleLandscape"))
1747 pwg_add_finishing(pc->finishings, IPP_FINISHINGS_STAPLE_BOTTOM_LEFT, "StapleLocation", "SingleLandscape");
1748 if (ppdFindChoice(ppd_option, "DualLandscape"))
1749 pwg_add_finishing(pc->finishings, IPP_FINISHINGS_STAPLE_DUAL_LEFT, "StapleLocation", "DualLandscape");
1750 }
1751
1752 if ((ppd_option = ppdFindOption(ppd, "RIPunch")) != NULL)
1753 {
1754 /*
1755 * Add (Ricoh) punch finishings...
1756 */
1757
1758 if (ppdFindChoice(ppd_option, "Left2"))
1759 pwg_add_finishing(pc->finishings, IPP_FINISHINGS_PUNCH_DUAL_LEFT, "RIPunch", "Left2");
1760 if (ppdFindChoice(ppd_option, "Left3"))
1761 pwg_add_finishing(pc->finishings, IPP_FINISHINGS_PUNCH_TRIPLE_LEFT, "RIPunch", "Left3");
1762 if (ppdFindChoice(ppd_option, "Left4"))
1763 pwg_add_finishing(pc->finishings, IPP_FINISHINGS_PUNCH_QUAD_LEFT, "RIPunch", "Left4");
1764 if (ppdFindChoice(ppd_option, "Right2"))
1765 pwg_add_finishing(pc->finishings, IPP_FINISHINGS_PUNCH_DUAL_RIGHT, "RIPunch", "Right2");
1766 if (ppdFindChoice(ppd_option, "Right3"))
1767 pwg_add_finishing(pc->finishings, IPP_FINISHINGS_PUNCH_TRIPLE_RIGHT, "RIPunch", "Right3");
1768 if (ppdFindChoice(ppd_option, "Right4"))
1769 pwg_add_finishing(pc->finishings, IPP_FINISHINGS_PUNCH_QUAD_RIGHT, "RIPunch", "Right4");
1770 if (ppdFindChoice(ppd_option, "Upper2"))
1771 pwg_add_finishing(pc->finishings, IPP_FINISHINGS_PUNCH_DUAL_TOP, "RIPunch", "Upper2");
1772 if (ppdFindChoice(ppd_option, "Upper3"))
1773 pwg_add_finishing(pc->finishings, IPP_FINISHINGS_PUNCH_TRIPLE_TOP, "RIPunch", "Upper3");
1774 if (ppdFindChoice(ppd_option, "Upper4"))
1775 pwg_add_finishing(pc->finishings, IPP_FINISHINGS_PUNCH_QUAD_TOP, "RIPunch", "Upper4");
1776 }
1777
1778 if ((ppd_option = ppdFindOption(ppd, "BindEdge")) != NULL)
1779 {
1780 /*
1781 * Add bind finishings...
1782 */
1783
1784 if (ppdFindChoice(ppd_option, "Left"))
1785 pwg_add_finishing(pc->finishings, IPP_FINISHINGS_BIND_LEFT, "BindEdge", "Left");
1786 if (ppdFindChoice(ppd_option, "Right"))
1787 pwg_add_finishing(pc->finishings, IPP_FINISHINGS_BIND_RIGHT, "BindEdge", "Right");
1788 if (ppdFindChoice(ppd_option, "Top"))
1789 pwg_add_finishing(pc->finishings, IPP_FINISHINGS_BIND_TOP, "BindEdge", "Top");
1790 if (ppdFindChoice(ppd_option, "Bottom"))
1791 pwg_add_finishing(pc->finishings, IPP_FINISHINGS_BIND_BOTTOM, "BindEdge", "Bottom");
1792 }
1793
1794 if ((ppd_option = ppdFindOption(ppd, "FoldType")) != NULL)
1795 {
1796 /*
1797 * Add (Adobe) fold finishings...
1798 */
1799
1800 if (ppdFindChoice(ppd_option, "ZFold"))
1801 pwg_add_finishing(pc->finishings, IPP_FINISHINGS_FOLD_Z, "FoldType", "ZFold");
1802 if (ppdFindChoice(ppd_option, "Saddle"))
1803 pwg_add_finishing(pc->finishings, IPP_FINISHINGS_FOLD_HALF, "FoldType", "Saddle");
1804 if (ppdFindChoice(ppd_option, "DoubleGate"))
1805 pwg_add_finishing(pc->finishings, IPP_FINISHINGS_FOLD_DOUBLE_GATE, "FoldType", "DoubleGate");
1806 if (ppdFindChoice(ppd_option, "LeftGate"))
1807 pwg_add_finishing(pc->finishings, IPP_FINISHINGS_FOLD_LEFT_GATE, "FoldType", "LeftGate");
1808 if (ppdFindChoice(ppd_option, "RightGate"))
1809 pwg_add_finishing(pc->finishings, IPP_FINISHINGS_FOLD_RIGHT_GATE, "FoldType", "RightGate");
1810 if (ppdFindChoice(ppd_option, "Letter"))
1811 pwg_add_finishing(pc->finishings, IPP_FINISHINGS_FOLD_LETTER, "FoldType", "Letter");
1812 if (ppdFindChoice(ppd_option, "XFold"))
1813 pwg_add_finishing(pc->finishings, IPP_FINISHINGS_FOLD_POSTER, "FoldType", "XFold");
1814 }
1815
1816 if ((ppd_option = ppdFindOption(ppd, "RIFoldType")) != NULL)
1817 {
1818 /*
1819 * Add (Ricoh) fold finishings...
1820 */
1821
1822 if (ppdFindChoice(ppd_option, "OutsideTwoFold"))
1823 pwg_add_finishing(pc->finishings, IPP_FINISHINGS_FOLD_LETTER, "RIFoldType", "OutsideTwoFold");
1824 }
1825
1826 if (cupsArrayCount(pc->finishings) == 0)
1827 {
1828 cupsArrayDelete(pc->finishings);
1829 pc->finishings = NULL;
1830 }
1831 }
dcb445bc 1832
4f63d6cd
MS
1833 if ((ppd_option = ppdFindOption(ppd, "cupsFinishingTemplate")) != NULL)
1834 {
1835 pc->templates = cupsArrayNew3((cups_array_func_t)strcmp, NULL, NULL, 0, (cups_acopy_func_t)_cupsStrAlloc, (cups_afree_func_t)_cupsStrFree);
1836
1837 for (choice = ppd_option->choices, i = ppd_option->num_choices; i > 0; choice ++, i --)
1838 cupsArrayAdd(pc->templates, (void *)choice->choice);
1839 }
1840
3e7fe0ca
MS
1841 /*
1842 * Max copies...
1843 */
1844
1845 if ((ppd_attr = ppdFindAttr(ppd, "cupsMaxCopies", NULL)) != NULL)
1846 pc->max_copies = atoi(ppd_attr->value);
1847 else if (ppd->manual_copies)
1848 pc->max_copies = 1;
1849 else
1850 pc->max_copies = 9999;
1851
5a9febac 1852 /*
a469f8a5
MS
1853 * cupsChargeInfoURI, cupsJobAccountId, cupsJobAccountingUserId,
1854 * cupsJobPassword, and cupsMandatory.
5a9febac
MS
1855 */
1856
a469f8a5
MS
1857 if ((ppd_attr = ppdFindAttr(ppd, "cupsChargeInfoURI", NULL)) != NULL)
1858 pc->charge_info_uri = _cupsStrAlloc(ppd_attr->value);
1859
5a9febac
MS
1860 if ((ppd_attr = ppdFindAttr(ppd, "cupsJobAccountId", NULL)) != NULL)
1861 pc->account_id = !_cups_strcasecmp(ppd_attr->value, "true");
1862
1863 if ((ppd_attr = ppdFindAttr(ppd, "cupsJobAccountingUserId", NULL)) != NULL)
1864 pc->accounting_user_id = !_cups_strcasecmp(ppd_attr->value, "true");
1865
1866 if ((ppd_attr = ppdFindAttr(ppd, "cupsJobPassword", NULL)) != NULL)
1867 pc->password = _cupsStrAlloc(ppd_attr->value);
1868
1869 if ((ppd_attr = ppdFindAttr(ppd, "cupsMandatory", NULL)) != NULL)
1870 pc->mandatory = _cupsArrayNewStrings(ppd_attr->value, ' ');
1871
89550f3f
MS
1872 /*
1873 * Strings (remote) file...
1874 */
1875
1876 if ((ppd_attr = ppdFindAttr(ppd, "cupsStringsURI", NULL)) != NULL)
1877 pc->strings_uri = _cupsStrAlloc(ppd_attr->value);
1878
c1420c87
MS
1879 /*
1880 * Support files...
1881 */
1882
1883 pc->support_files = cupsArrayNew3(NULL, NULL, NULL, 0,
1884 (cups_acopy_func_t)_cupsStrAlloc,
1885 (cups_afree_func_t)_cupsStrFree);
1886
1887 for (ppd_attr = ppdFindAttr(ppd, "cupsICCProfile", NULL);
1888 ppd_attr;
1889 ppd_attr = ppdFindNextAttr(ppd, "cupsICCProfile", NULL))
1890 cupsArrayAdd(pc->support_files, ppd_attr->value);
1891
1892 if ((ppd_attr = ppdFindAttr(ppd, "APPrinterIconPath", NULL)) != NULL)
1893 cupsArrayAdd(pc->support_files, ppd_attr->value);
1894
f14324a7
MS
1895 /*
1896 * Return the cache data...
1897 */
1898
1899 return (pc);
1900
1901 /*
1902 * If we get here we need to destroy the PWG mapping data and return NULL...
1903 */
1904
1905 create_error:
1906
cb7f98ee 1907 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Out of memory."), 1);
f14324a7
MS
1908 _ppdCacheDestroy(pc);
1909
1910 return (NULL);
1911}
1912
1913
1914/*
1915 * '_ppdCacheDestroy()' - Free all memory used for PWG mapping data.
1916 */
1917
1918void
1919_ppdCacheDestroy(_ppd_cache_t *pc) /* I - PPD cache and mapping data */
1920{
1921 int i; /* Looping var */
6961465f
MS
1922 pwg_map_t *map; /* Current map */
1923 pwg_size_t *size; /* Current size */
f14324a7
MS
1924
1925
1926 /*
1927 * Range check input...
1928 */
1929
1930 if (!pc)
1931 return;
1932
1933 /*
1934 * Free memory as needed...
1935 */
1936
1937 if (pc->bins)
1938 {
1939 for (i = pc->num_bins, map = pc->bins; i > 0; i --, map ++)
1940 {
1941 _cupsStrFree(map->pwg);
1942 _cupsStrFree(map->ppd);
1943 }
1944
1945 free(pc->bins);
1946 }
1947
1948 if (pc->sizes)
1949 {
1950 for (i = pc->num_sizes, size = pc->sizes; i > 0; i --, size ++)
1951 {
1952 _cupsStrFree(size->map.pwg);
1953 _cupsStrFree(size->map.ppd);
1954 }
1955
1956 free(pc->sizes);
1957 }
1958
1959 if (pc->source_option)
1960 _cupsStrFree(pc->source_option);
1961
1962 if (pc->sources)
1963 {
1964 for (i = pc->num_sources, map = pc->sources; i > 0; i --, map ++)
1965 {
1966 _cupsStrFree(map->pwg);
1967 _cupsStrFree(map->ppd);
1968 }
1969
1970 free(pc->sources);
1971 }
1972
1973 if (pc->types)
1974 {
1975 for (i = pc->num_types, map = pc->types; i > 0; i --, map ++)
1976 {
1977 _cupsStrFree(map->pwg);
1978 _cupsStrFree(map->ppd);
1979 }
1980
1981 free(pc->types);
1982 }
1983
1984 if (pc->custom_max_keyword)
1985 _cupsStrFree(pc->custom_max_keyword);
1986
1987 if (pc->custom_min_keyword)
1988 _cupsStrFree(pc->custom_min_keyword);
1989
1990 _cupsStrFree(pc->product);
1991 cupsArrayDelete(pc->filters);
1992 cupsArrayDelete(pc->prefilters);
dcb445bc 1993 cupsArrayDelete(pc->finishings);
f14324a7 1994
a469f8a5 1995 _cupsStrFree(pc->charge_info_uri);
5a9febac
MS
1996 _cupsStrFree(pc->password);
1997
1998 cupsArrayDelete(pc->mandatory);
1999
c1420c87
MS
2000 cupsArrayDelete(pc->support_files);
2001
f14324a7
MS
2002 free(pc);
2003}
2004
2005
2006/*
2007 * '_ppdCacheGetBin()' - Get the PWG output-bin keyword associated with a PPD
2008 * OutputBin.
2009 */
2010
2011const char * /* O - output-bin or NULL */
2012_ppdCacheGetBin(
2013 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
2014 const char *output_bin) /* I - PPD OutputBin string */
2015{
2016 int i; /* Looping var */
2017
2018
2019 /*
2020 * Range check input...
2021 */
2022
2023 if (!pc || !output_bin)
2024 return (NULL);
2025
2026 /*
2027 * Look up the OutputBin string...
2028 */
2029
2030
2031 for (i = 0; i < pc->num_bins; i ++)
88f9aafc 2032 if (!_cups_strcasecmp(output_bin, pc->bins[i].ppd))
f14324a7
MS
2033 return (pc->bins[i].pwg);
2034
2035 return (NULL);
2036}
2037
2038
dcb445bc
MS
2039/*
2040 * '_ppdCacheGetFinishingOptions()' - Get PPD finishing options for the given
2041 * IPP finishings value(s).
2042 */
2043
2044int /* O - New number of options */
2045_ppdCacheGetFinishingOptions(
cb7f98ee
MS
2046 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
2047 ipp_t *job, /* I - Job attributes or NULL */
2048 ipp_finishings_t value, /* I - IPP finishings value of IPP_FINISHINGS_NONE */
2049 int num_options, /* I - Number of options */
2050 cups_option_t **options) /* IO - Options */
dcb445bc
MS
2051{
2052 int i; /* Looping var */
2053 _pwg_finishings_t *f, /* PWG finishings options */
2054 key; /* Search key */
2055 ipp_attribute_t *attr; /* Finishings attribute */
2056 cups_option_t *option; /* Current finishings option */
2057
2058
2059 /*
2060 * Range check input...
2061 */
2062
2063 if (!pc || cupsArrayCount(pc->finishings) == 0 || !options ||
2064 (!job && value == IPP_FINISHINGS_NONE))
2065 return (num_options);
2066
2067 /*
2068 * Apply finishing options...
2069 */
2070
2071 if (job && (attr = ippFindAttribute(job, "finishings", IPP_TAG_ENUM)) != NULL)
2072 {
2073 int num_values = ippGetCount(attr); /* Number of values */
2074
2075 for (i = 0; i < num_values; i ++)
2076 {
7e86f2f6 2077 key.value = (ipp_finishings_t)ippGetInteger(attr, i);
dcb445bc
MS
2078
2079 if ((f = cupsArrayFind(pc->finishings, &key)) != NULL)
2080 {
2081 int j; /* Another looping var */
2082
2083 for (j = f->num_options, option = f->options; j > 0; j --, option ++)
2084 num_options = cupsAddOption(option->name, option->value,
2085 num_options, options);
2086 }
2087 }
2088 }
2089 else if (value != IPP_FINISHINGS_NONE)
2090 {
2091 key.value = value;
2092
2093 if ((f = cupsArrayFind(pc->finishings, &key)) != NULL)
2094 {
2095 int j; /* Another looping var */
2096
2097 for (j = f->num_options, option = f->options; j > 0; j --, option ++)
2098 num_options = cupsAddOption(option->name, option->value,
2099 num_options, options);
2100 }
2101 }
2102
2103 return (num_options);
2104}
2105
2106
2107/*
2108 * '_ppdCacheGetFinishingValues()' - Get IPP finishings value(s) from the given
2109 * PPD options.
2110 */
2111
2112int /* O - Number of finishings values */
2113_ppdCacheGetFinishingValues(
2114 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
2115 int num_options, /* I - Number of options */
2116 cups_option_t *options, /* I - Options */
2117 int max_values, /* I - Maximum number of finishings values */
2118 int *values) /* O - Finishings values */
2119{
2120 int i, /* Looping var */
2121 num_values = 0; /* Number of values */
2122 _pwg_finishings_t *f; /* Current finishings option */
2123 cups_option_t *option; /* Current option */
2124 const char *val; /* Value for option */
2125
2126
2127 /*
2128 * Range check input...
2129 */
2130
b2b9911d
MS
2131 DEBUG_printf(("_ppdCacheGetFinishingValues(pc=%p, num_options=%d, options=%p, max_values=%d, values=%p)", pc, num_options, options, max_values, values));
2132
e44bdfe7 2133 if (!pc || max_values < 1 || !values)
b2b9911d
MS
2134 {
2135 DEBUG_puts("_ppdCacheGetFinishingValues: Bad arguments, returning 0.");
dcb445bc 2136 return (0);
b2b9911d 2137 }
e44bdfe7
MS
2138 else if (!pc->finishings)
2139 {
2140 DEBUG_puts("_ppdCacheGetFinishingValues: No finishings support, returning 0.");
2141 return (0);
2142 }
dcb445bc
MS
2143
2144 /*
2145 * Go through the finishings options and see what is set...
2146 */
2147
2148 for (f = (_pwg_finishings_t *)cupsArrayFirst(pc->finishings);
2149 f;
2150 f = (_pwg_finishings_t *)cupsArrayNext(pc->finishings))
2151 {
b2b9911d
MS
2152 DEBUG_printf(("_ppdCacheGetFinishingValues: Checking %d (%s)", f->value, ippEnumString("finishings", f->value)));
2153
dcb445bc 2154 for (i = f->num_options, option = f->options; i > 0; i --, option ++)
b2b9911d
MS
2155 {
2156 DEBUG_printf(("_ppdCacheGetFinishingValues: %s=%s?", option->name, option->value));
2157
dcb445bc
MS
2158 if ((val = cupsGetOption(option->name, num_options, options)) == NULL ||
2159 _cups_strcasecmp(option->value, val))
b2b9911d
MS
2160 {
2161 DEBUG_puts("_ppdCacheGetFinishingValues: NO");
dcb445bc 2162 break;
b2b9911d
MS
2163 }
2164 }
dcb445bc
MS
2165
2166 if (i == 0)
2167 {
e44bdfe7 2168 DEBUG_printf(("_ppdCacheGetFinishingValues: Adding %d (%s)", f->value, ippEnumString("finishings", f->value)));
b2b9911d 2169
dcb445bc
MS
2170 values[num_values ++] = f->value;
2171
2172 if (num_values >= max_values)
2173 break;
2174 }
2175 }
2176
e44bdfe7
MS
2177 if (num_values == 0)
2178 {
2179 /*
2180 * Always have at least "finishings" = 'none'...
2181 */
2182
2183 DEBUG_puts("_ppdCacheGetFinishingValues: Adding 3 (none).");
2184 values[0] = IPP_FINISHINGS_NONE;
2185 num_values ++;
2186 }
2187
b2b9911d
MS
2188 DEBUG_printf(("_ppdCacheGetFinishingValues: Returning %d.", num_values));
2189
dcb445bc
MS
2190 return (num_values);
2191}
2192
2193
f14324a7
MS
2194/*
2195 * '_ppdCacheGetInputSlot()' - Get the PPD InputSlot associated with the job
2196 * attributes or a keyword string.
2197 */
2198
2199const char * /* O - PPD InputSlot or NULL */
2200_ppdCacheGetInputSlot(
2201 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
2202 ipp_t *job, /* I - Job attributes or NULL */
2203 const char *keyword) /* I - Keyword string or NULL */
2204{
2205 /*
2206 * Range check input...
2207 */
2208
2209 if (!pc || pc->num_sources == 0 || (!job && !keyword))
2210 return (NULL);
2211
2212 if (job && !keyword)
2213 {
2214 /*
2215 * Lookup the media-col attribute and any media-source found there...
2216 */
2217
2218 ipp_attribute_t *media_col, /* media-col attribute */
2219 *media_source; /* media-source attribute */
6961465f 2220 pwg_size_t size; /* Dimensional size */
f14324a7
MS
2221 int margins_set; /* Were the margins set? */
2222
2223 media_col = ippFindAttribute(job, "media-col", IPP_TAG_BEGIN_COLLECTION);
2224 if (media_col &&
dcb445bc 2225 (media_source = ippFindAttribute(ippGetCollection(media_col, 0),
f14324a7
MS
2226 "media-source",
2227 IPP_TAG_KEYWORD)) != NULL)
2228 {
2229 /*
2230 * Use the media-source value from media-col...
2231 */
2232
dcb445bc 2233 keyword = ippGetString(media_source, 0, NULL);
f14324a7 2234 }
6961465f 2235 else if (pwgInitSize(&size, job, &margins_set))
f14324a7
MS
2236 {
2237 /*
2238 * For media <= 5x7, look for a photo tray...
2239 */
2240
2241 if (size.width <= (5 * 2540) && size.length <= (7 * 2540))
2242 keyword = "photo";
2243 }
2244 }
2245
2246 if (keyword)
2247 {
2248 int i; /* Looping var */
2249
2250 for (i = 0; i < pc->num_sources; i ++)
88f9aafc 2251 if (!_cups_strcasecmp(keyword, pc->sources[i].pwg))
f14324a7
MS
2252 return (pc->sources[i].ppd);
2253 }
2254
2255 return (NULL);
2256}
2257
2258
2259/*
2260 * '_ppdCacheGetMediaType()' - Get the PPD MediaType associated with the job
2261 * attributes or a keyword string.
2262 */
2263
2264const char * /* O - PPD MediaType or NULL */
2265_ppdCacheGetMediaType(
2266 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
2267 ipp_t *job, /* I - Job attributes or NULL */
2268 const char *keyword) /* I - Keyword string or NULL */
2269{
2270 /*
2271 * Range check input...
2272 */
2273
2274 if (!pc || pc->num_types == 0 || (!job && !keyword))
2275 return (NULL);
2276
2277 if (job && !keyword)
2278 {
2279 /*
2280 * Lookup the media-col attribute and any media-source found there...
2281 */
2282
2283 ipp_attribute_t *media_col, /* media-col attribute */
2284 *media_type; /* media-type attribute */
2285
2286 media_col = ippFindAttribute(job, "media-col", IPP_TAG_BEGIN_COLLECTION);
2287 if (media_col)
2288 {
2289 if ((media_type = ippFindAttribute(media_col->values[0].collection,
2290 "media-type",
2291 IPP_TAG_KEYWORD)) == NULL)
2292 media_type = ippFindAttribute(media_col->values[0].collection,
2293 "media-type", IPP_TAG_NAME);
2294
2295 if (media_type)
2296 keyword = media_type->values[0].string.text;
2297 }
2298 }
2299
2300 if (keyword)
2301 {
2302 int i; /* Looping var */
2303
2304 for (i = 0; i < pc->num_types; i ++)
88f9aafc 2305 if (!_cups_strcasecmp(keyword, pc->types[i].pwg))
f14324a7
MS
2306 return (pc->types[i].ppd);
2307 }
2308
2309 return (NULL);
2310}
2311
2312
2313/*
2314 * '_ppdCacheGetOutputBin()' - Get the PPD OutputBin associated with the keyword
2315 * string.
2316 */
2317
2318const char * /* O - PPD OutputBin or NULL */
2319_ppdCacheGetOutputBin(
2320 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
2321 const char *output_bin) /* I - Keyword string */
2322{
2323 int i; /* Looping var */
2324
2325
2326 /*
2327 * Range check input...
2328 */
2329
2330 if (!pc || !output_bin)
2331 return (NULL);
2332
2333 /*
2334 * Look up the OutputBin string...
2335 */
2336
2337
2338 for (i = 0; i < pc->num_bins; i ++)
88f9aafc 2339 if (!_cups_strcasecmp(output_bin, pc->bins[i].pwg))
f14324a7
MS
2340 return (pc->bins[i].ppd);
2341
2342 return (NULL);
2343}
2344
2345
2346/*
2347 * '_ppdCacheGetPageSize()' - Get the PPD PageSize associated with the job
2348 * attributes or a keyword string.
2349 */
2350
2351const char * /* O - PPD PageSize or NULL */
2352_ppdCacheGetPageSize(
2353 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
2354 ipp_t *job, /* I - Job attributes or NULL */
2355 const char *keyword, /* I - Keyword string or NULL */
2356 int *exact) /* O - 1 if exact match, 0 otherwise */
2357{
2358 int i; /* Looping var */
6961465f 2359 pwg_size_t *size, /* Current size */
f14324a7
MS
2360 *closest, /* Closest size */
2361 jobsize; /* Size data from job */
2362 int margins_set, /* Were the margins set? */
2363 dwidth, /* Difference in width */
2364 dlength, /* Difference in length */
2365 dleft, /* Difference in left margins */
2366 dright, /* Difference in right margins */
2367 dbottom, /* Difference in bottom margins */
2368 dtop, /* Difference in top margins */
2369 dmin, /* Minimum difference */
2370 dclosest; /* Closest difference */
2371 const char *ppd_name; /* PPD media name */
2372
2373
2374 DEBUG_printf(("_ppdCacheGetPageSize(pc=%p, job=%p, keyword=\"%s\", exact=%p)",
2375 pc, job, keyword, exact));
2376
2377 /*
2378 * Range check input...
2379 */
2380
2381 if (!pc || (!job && !keyword))
2382 return (NULL);
2383
2384 if (exact)
2385 *exact = 0;
2386
2387 ppd_name = keyword;
2388
2389 if (job)
2390 {
2391 /*
2392 * Try getting the PPD media name from the job attributes...
2393 */
2394
2395 ipp_attribute_t *attr; /* Job attribute */
2396
2397 if ((attr = ippFindAttribute(job, "PageSize", IPP_TAG_ZERO)) == NULL)
2398 if ((attr = ippFindAttribute(job, "PageRegion", IPP_TAG_ZERO)) == NULL)
2399 attr = ippFindAttribute(job, "media", IPP_TAG_ZERO);
2400
2401#ifdef DEBUG
2402 if (attr)
2403 DEBUG_printf(("1_ppdCacheGetPageSize: Found attribute %s (%s)",
2404 attr->name, ippTagString(attr->value_tag)));
2405 else
2406 DEBUG_puts("1_ppdCacheGetPageSize: Did not find media attribute.");
2407#endif /* DEBUG */
2408
2409 if (attr && (attr->value_tag == IPP_TAG_NAME ||
2410 attr->value_tag == IPP_TAG_KEYWORD))
2411 ppd_name = attr->values[0].string.text;
2412 }
2413
2414 DEBUG_printf(("1_ppdCacheGetPageSize: ppd_name=\"%s\"", ppd_name));
2415
2416 if (ppd_name)
2417 {
2418 /*
2419 * Try looking up the named PPD size first...
2420 */
2421
2422 for (i = pc->num_sizes, size = pc->sizes; i > 0; i --, size ++)
2423 {
2424 DEBUG_printf(("2_ppdCacheGetPageSize: size[%d]=[\"%s\" \"%s\"]",
2425 (int)(size - pc->sizes), size->map.pwg, size->map.ppd));
2426
88f9aafc
MS
2427 if (!_cups_strcasecmp(ppd_name, size->map.ppd) ||
2428 !_cups_strcasecmp(ppd_name, size->map.pwg))
f14324a7
MS
2429 {
2430 if (exact)
2431 *exact = 1;
2432
2433 DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\"", ppd_name));
2434
2435 return (size->map.ppd);
2436 }
2437 }
2438 }
2439
2440 if (job && !keyword)
2441 {
2442 /*
2443 * Get the size using media-col or media, with the preference being
2444 * media-col.
2445 */
2446
6961465f 2447 if (!pwgInitSize(&jobsize, job, &margins_set))
f14324a7
MS
2448 return (NULL);
2449 }
2450 else
2451 {
2452 /*
2453 * Get the size using a media keyword...
2454 */
2455
6961465f 2456 pwg_media_t *media; /* Media definition */
f14324a7
MS
2457
2458
6961465f
MS
2459 if ((media = pwgMediaForPWG(keyword)) == NULL)
2460 if ((media = pwgMediaForLegacy(keyword)) == NULL)
2461 if ((media = pwgMediaForPPD(keyword)) == NULL)
f14324a7
MS
2462 return (NULL);
2463
2464 jobsize.width = media->width;
2465 jobsize.length = media->length;
2466 margins_set = 0;
2467 }
2468
2469 /*
2470 * Now that we have the dimensions and possibly the margins, look at the
2471 * available sizes and find the match...
2472 */
2473
2474 closest = NULL;
2475 dclosest = 999999999;
2476
88f9aafc
MS
2477 if (!ppd_name || _cups_strncasecmp(ppd_name, "Custom.", 7) ||
2478 _cups_strncasecmp(ppd_name, "custom_", 7))
f14324a7
MS
2479 {
2480 for (i = pc->num_sizes, size = pc->sizes; i > 0; i --, size ++)
2481 {
2482 /*
2483 * Adobe uses a size matching algorithm with an epsilon of 5 points, which
2484 * is just about 176/2540ths...
2485 */
2486
2487 dwidth = size->width - jobsize.width;
2488 dlength = size->length - jobsize.length;
2489
2490 if (dwidth <= -176 || dwidth >= 176 || dlength <= -176 || dlength >= 176)
2491 continue;
2492
2493 if (margins_set)
2494 {
2495 /*
2496 * Use a tighter epsilon of 1 point (35/2540ths) for margins...
2497 */
2498
2499 dleft = size->left - jobsize.left;
2500 dright = size->right - jobsize.right;
2501 dtop = size->top - jobsize.top;
2502 dbottom = size->bottom - jobsize.bottom;
2503
2504 if (dleft <= -35 || dleft >= 35 || dright <= -35 || dright >= 35 ||
2505 dtop <= -35 || dtop >= 35 || dbottom <= -35 || dbottom >= 35)
2506 {
2507 dleft = dleft < 0 ? -dleft : dleft;
2508 dright = dright < 0 ? -dright : dright;
2509 dbottom = dbottom < 0 ? -dbottom : dbottom;
2510 dtop = dtop < 0 ? -dtop : dtop;
2511 dmin = dleft + dright + dbottom + dtop;
2512
2513 if (dmin < dclosest)
2514 {
2515 dclosest = dmin;
2516 closest = size;
2517 }
2518
2519 continue;
2520 }
2521 }
2522
2523 if (exact)
2524 *exact = 1;
2525
2526 DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\"", size->map.ppd));
2527
2528 return (size->map.ppd);
2529 }
2530 }
2531
2532 if (closest)
2533 {
2534 DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\" (closest)",
2535 closest->map.ppd));
2536
2537 return (closest->map.ppd);
2538 }
2539
2540 /*
2541 * If we get here we need to check for custom page size support...
2542 */
2543
2544 if (jobsize.width >= pc->custom_min_width &&
2545 jobsize.width <= pc->custom_max_width &&
2546 jobsize.length >= pc->custom_min_length &&
2547 jobsize.length <= pc->custom_max_length)
2548 {
2549 /*
2550 * In range, format as Custom.WWWWxLLLL (points).
2551 */
2552
2553 snprintf(pc->custom_ppd_size, sizeof(pc->custom_ppd_size), "Custom.%dx%d",
6961465f 2554 (int)PWG_TO_POINTS(jobsize.width), (int)PWG_TO_POINTS(jobsize.length));
f14324a7
MS
2555
2556 if (margins_set && exact)
2557 {
2558 dleft = pc->custom_size.left - jobsize.left;
2559 dright = pc->custom_size.right - jobsize.right;
2560 dtop = pc->custom_size.top - jobsize.top;
2561 dbottom = pc->custom_size.bottom - jobsize.bottom;
2562
2563 if (dleft > -35 && dleft < 35 && dright > -35 && dright < 35 &&
2564 dtop > -35 && dtop < 35 && dbottom > -35 && dbottom < 35)
2565 *exact = 1;
2566 }
2567 else if (exact)
2568 *exact = 1;
2569
2570 DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\" (custom)",
2571 pc->custom_ppd_size));
2572
2573 return (pc->custom_ppd_size);
2574 }
2575
2576 /*
2577 * No custom page size support or the size is out of range - return NULL.
2578 */
2579
2580 DEBUG_puts("1_ppdCacheGetPageSize: Returning NULL");
2581
2582 return (NULL);
2583}
2584
2585
2586/*
2587 * '_ppdCacheGetSize()' - Get the PWG size associated with a PPD PageSize.
2588 */
2589
6961465f 2590pwg_size_t * /* O - PWG size or NULL */
f14324a7
MS
2591_ppdCacheGetSize(
2592 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
2593 const char *page_size) /* I - PPD PageSize */
2594{
dcb445bc 2595 int i; /* Looping var */
6961465f
MS
2596 pwg_media_t *media; /* Media */
2597 pwg_size_t *size; /* Current size */
f14324a7
MS
2598
2599
2600 /*
2601 * Range check input...
2602 */
2603
2604 if (!pc || !page_size)
2605 return (NULL);
2606
88f9aafc 2607 if (!_cups_strncasecmp(page_size, "Custom.", 7))
f14324a7
MS
2608 {
2609 /*
2610 * Custom size; size name can be one of the following:
2611 *
2612 * Custom.WIDTHxLENGTHin - Size in inches
2613 * Custom.WIDTHxLENGTHft - Size in feet
2614 * Custom.WIDTHxLENGTHcm - Size in centimeters
2615 * Custom.WIDTHxLENGTHmm - Size in millimeters
2616 * Custom.WIDTHxLENGTHm - Size in meters
2617 * Custom.WIDTHxLENGTH[pt] - Size in points
2618 */
2619
2620 double w, l; /* Width and length of page */
2621 char *ptr; /* Pointer into PageSize */
2622 struct lconv *loc; /* Locale data */
2623
2624 loc = localeconv();
2625 w = (float)_cupsStrScand(page_size + 7, &ptr, loc);
2626 if (!ptr || *ptr != 'x')
2627 return (NULL);
2628
2629 l = (float)_cupsStrScand(ptr + 1, &ptr, loc);
2630 if (!ptr)
2631 return (NULL);
2632
88f9aafc 2633 if (!_cups_strcasecmp(ptr, "in"))
f14324a7
MS
2634 {
2635 w *= 2540.0;
2636 l *= 2540.0;
2637 }
88f9aafc 2638 else if (!_cups_strcasecmp(ptr, "ft"))
f14324a7
MS
2639 {
2640 w *= 12.0 * 2540.0;
2641 l *= 12.0 * 2540.0;
2642 }
88f9aafc 2643 else if (!_cups_strcasecmp(ptr, "mm"))
f14324a7
MS
2644 {
2645 w *= 100.0;
2646 l *= 100.0;
2647 }
88f9aafc 2648 else if (!_cups_strcasecmp(ptr, "cm"))
f14324a7
MS
2649 {
2650 w *= 1000.0;
2651 l *= 1000.0;
2652 }
88f9aafc 2653 else if (!_cups_strcasecmp(ptr, "m"))
f14324a7
MS
2654 {
2655 w *= 100000.0;
2656 l *= 100000.0;
2657 }
2658 else
2659 {
2660 w *= 2540.0 / 72.0;
2661 l *= 2540.0 / 72.0;
2662 }
2663
2664 pc->custom_size.width = (int)w;
2665 pc->custom_size.length = (int)l;
2666
2667 return (&(pc->custom_size));
2668 }
2669
2670 /*
2671 * Not a custom size - look it up...
2672 */
2673
2674 for (i = pc->num_sizes, size = pc->sizes; i > 0; i --, size ++)
dcb445bc
MS
2675 if (!_cups_strcasecmp(page_size, size->map.ppd) ||
2676 !_cups_strcasecmp(page_size, size->map.pwg))
f14324a7
MS
2677 return (size);
2678
dcb445bc
MS
2679 /*
2680 * Look up standard sizes...
2681 */
2682
6961465f
MS
2683 if ((media = pwgMediaForPPD(page_size)) == NULL)
2684 if ((media = pwgMediaForLegacy(page_size)) == NULL)
2685 media = pwgMediaForPWG(page_size);
dcb445bc
MS
2686
2687 if (media)
2688 {
2689 pc->custom_size.width = media->width;
2690 pc->custom_size.length = media->length;
2691
2692 return (&(pc->custom_size));
2693 }
2694
f14324a7
MS
2695 return (NULL);
2696}
2697
2698
2699/*
2700 * '_ppdCacheGetSource()' - Get the PWG media-source associated with a PPD
2701 * InputSlot.
2702 */
2703
2704const char * /* O - PWG media-source keyword */
2705_ppdCacheGetSource(
2706 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
2707 const char *input_slot) /* I - PPD InputSlot */
2708{
2709 int i; /* Looping var */
6961465f 2710 pwg_map_t *source; /* Current source */
f14324a7
MS
2711
2712
2713 /*
2714 * Range check input...
2715 */
2716
2717 if (!pc || !input_slot)
2718 return (NULL);
2719
2720 for (i = pc->num_sources, source = pc->sources; i > 0; i --, source ++)
88f9aafc 2721 if (!_cups_strcasecmp(input_slot, source->ppd))
f14324a7
MS
2722 return (source->pwg);
2723
2724 return (NULL);
2725}
2726
2727
2728/*
2729 * '_ppdCacheGetType()' - Get the PWG media-type associated with a PPD
2730 * MediaType.
2731 */
2732
2733const char * /* O - PWG media-type keyword */
2734_ppdCacheGetType(
2735 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
2736 const char *media_type) /* I - PPD MediaType */
2737{
2738 int i; /* Looping var */
6961465f 2739 pwg_map_t *type; /* Current type */
f14324a7
MS
2740
2741
2742 /*
2743 * Range check input...
2744 */
2745
2746 if (!pc || !media_type)
2747 return (NULL);
2748
2749 for (i = pc->num_types, type = pc->types; i > 0; i --, type ++)
88f9aafc 2750 if (!_cups_strcasecmp(media_type, type->ppd))
f14324a7
MS
2751 return (type->pwg);
2752
2753 return (NULL);
2754}
2755
2756
2757/*
2758 * '_ppdCacheWriteFile()' - Write PWG mapping data to a file.
2759 */
2760
2761int /* O - 1 on success, 0 on failure */
2762_ppdCacheWriteFile(
2763 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
2764 const char *filename, /* I - File to write */
2765 ipp_t *attrs) /* I - Attributes to write, if any */
2766{
dcb445bc
MS
2767 int i, j, k; /* Looping vars */
2768 cups_file_t *fp; /* Output file */
6961465f
MS
2769 pwg_size_t *size; /* Current size */
2770 pwg_map_t *map; /* Current map */
dcb445bc
MS
2771 _pwg_finishings_t *f; /* Current finishing option */
2772 cups_option_t *option; /* Current option */
4f63d6cd 2773 const char *value; /* String value */
dcb445bc 2774 char newfile[1024]; /* New filename */
f14324a7
MS
2775
2776
2777 /*
2778 * Range check input...
2779 */
2780
2781 if (!pc || !filename)
2782 {
cb7f98ee 2783 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
f14324a7
MS
2784 return (0);
2785 }
2786
2787 /*
2788 * Open the file and write with compression...
2789 */
2790
321d8d57
MS
2791 snprintf(newfile, sizeof(newfile), "%s.N", filename);
2792 if ((fp = cupsFileOpen(newfile, "w9")) == NULL)
f14324a7 2793 {
cb7f98ee 2794 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
f14324a7
MS
2795 return (0);
2796 }
2797
2798 /*
2799 * Standard header...
2800 */
2801
2802 cupsFilePrintf(fp, "#CUPS-PPD-CACHE-%d\n", _PPD_CACHE_VERSION);
2803
2804 /*
2805 * Output bins...
2806 */
2807
2808 if (pc->num_bins > 0)
2809 {
2810 cupsFilePrintf(fp, "NumBins %d\n", pc->num_bins);
2811 for (i = pc->num_bins, map = pc->bins; i > 0; i --, map ++)
2812 cupsFilePrintf(fp, "Bin %s %s\n", map->pwg, map->ppd);
2813 }
2814
2815 /*
2816 * Media sizes...
2817 */
2818
2819 cupsFilePrintf(fp, "NumSizes %d\n", pc->num_sizes);
2820 for (i = pc->num_sizes, size = pc->sizes; i > 0; i --, size ++)
2821 cupsFilePrintf(fp, "Size %s %s %d %d %d %d %d %d\n", size->map.pwg,
2822 size->map.ppd, size->width, size->length, size->left,
2823 size->bottom, size->right, size->top);
2824 if (pc->custom_max_width > 0)
2825 cupsFilePrintf(fp, "CustomSize %d %d %d %d %d %d %d %d\n",
2826 pc->custom_max_width, pc->custom_max_length,
2827 pc->custom_min_width, pc->custom_min_length,
2828 pc->custom_size.left, pc->custom_size.bottom,
2829 pc->custom_size.right, pc->custom_size.top);
2830
2831 /*
2832 * Media sources...
2833 */
2834
2835 if (pc->source_option)
2836 cupsFilePrintf(fp, "SourceOption %s\n", pc->source_option);
2837
2838 if (pc->num_sources > 0)
2839 {
2840 cupsFilePrintf(fp, "NumSources %d\n", pc->num_sources);
2841 for (i = pc->num_sources, map = pc->sources; i > 0; i --, map ++)
2842 cupsFilePrintf(fp, "Source %s %s\n", map->pwg, map->ppd);
2843 }
2844
2845 /*
2846 * Media types...
2847 */
2848
2849 if (pc->num_types > 0)
2850 {
2851 cupsFilePrintf(fp, "NumTypes %d\n", pc->num_types);
2852 for (i = pc->num_types, map = pc->types; i > 0; i --, map ++)
2853 cupsFilePrintf(fp, "Type %s %s\n", map->pwg, map->ppd);
2854 }
2855
2856 /*
2857 * Presets...
2858 */
2859
2860 for (i = _PWG_PRINT_COLOR_MODE_MONOCHROME; i < _PWG_PRINT_COLOR_MODE_MAX; i ++)
2861 for (j = _PWG_PRINT_QUALITY_DRAFT; j < _PWG_PRINT_QUALITY_MAX; j ++)
2862 if (pc->num_presets[i][j])
2863 {
2864 cupsFilePrintf(fp, "Preset %d %d", i, j);
2865 for (k = pc->num_presets[i][j], option = pc->presets[i][j];
2866 k > 0;
2867 k --, option ++)
2868 cupsFilePrintf(fp, " %s=%s", option->name, option->value);
2869 cupsFilePutChar(fp, '\n');
2870 }
2871
2872 /*
2873 * Duplex/sides...
2874 */
2875
2876 if (pc->sides_option)
2877 cupsFilePrintf(fp, "SidesOption %s\n", pc->sides_option);
2878
2879 if (pc->sides_1sided)
2880 cupsFilePrintf(fp, "Sides1Sided %s\n", pc->sides_1sided);
2881
2882 if (pc->sides_2sided_long)
2883 cupsFilePrintf(fp, "Sides2SidedLong %s\n", pc->sides_2sided_long);
2884
2885 if (pc->sides_2sided_short)
2886 cupsFilePrintf(fp, "Sides2SidedShort %s\n", pc->sides_2sided_short);
2887
2888 /*
2889 * Product, cupsFilter, cupsFilter2, and cupsPreFilter...
2890 */
2891
2892 if (pc->product)
2893 cupsFilePutConf(fp, "Product", pc->product);
2894
2895 for (value = (const char *)cupsArrayFirst(pc->filters);
2896 value;
2897 value = (const char *)cupsArrayNext(pc->filters))
2898 cupsFilePutConf(fp, "Filter", value);
2899
2900 for (value = (const char *)cupsArrayFirst(pc->prefilters);
2901 value;
2902 value = (const char *)cupsArrayNext(pc->prefilters))
2903 cupsFilePutConf(fp, "PreFilter", value);
2904
82f97232
MS
2905 cupsFilePrintf(fp, "SingleFile %s\n", pc->single_file ? "true" : "false");
2906
dcb445bc
MS
2907 /*
2908 * Finishing options...
2909 */
2910
2911 for (f = (_pwg_finishings_t *)cupsArrayFirst(pc->finishings);
2912 f;
2913 f = (_pwg_finishings_t *)cupsArrayNext(pc->finishings))
2914 {
2915 cupsFilePrintf(fp, "Finishings %d", f->value);
2916 for (i = f->num_options, option = f->options; i > 0; i --, option ++)
2917 cupsFilePrintf(fp, " %s=%s", option->name, option->value);
2918 cupsFilePutChar(fp, '\n');
2919 }
2920
4f63d6cd
MS
2921 for (value = (const char *)cupsArrayFirst(pc->templates); value; value = (const char *)cupsArrayNext(pc->templates))
2922 cupsFilePutConf(fp, "FinishingTemplate", value);
2923
3e7fe0ca
MS
2924 /*
2925 * Max copies...
2926 */
2927
2928 cupsFilePrintf(fp, "MaxCopies %d\n", pc->max_copies);
2929
5a9febac
MS
2930 /*
2931 * Accounting/quota/PIN/managed printing values...
2932 */
2933
a469f8a5
MS
2934 if (pc->charge_info_uri)
2935 cupsFilePutConf(fp, "ChargeInfoURI", pc->charge_info_uri);
2936
5a9febac
MS
2937 cupsFilePrintf(fp, "AccountId %s\n", pc->account_id ? "true" : "false");
2938 cupsFilePrintf(fp, "AccountingUserId %s\n",
2939 pc->accounting_user_id ? "true" : "false");
2940
2941 if (pc->password)
2942 cupsFilePutConf(fp, "Password", pc->password);
2943
2944 for (value = (char *)cupsArrayFirst(pc->mandatory);
2945 value;
2946 value = (char *)cupsArrayNext(pc->mandatory))
2947 cupsFilePutConf(fp, "Mandatory", value);
2948
89550f3f
MS
2949 /*
2950 * (Remote) strings file...
2951 */
2952
2953 if (pc->strings_uri)
2954 cupsFilePutConf(fp, "StringsURI", pc->strings_uri);
2955
c1420c87
MS
2956 /*
2957 * Support files...
2958 */
2959
2960 for (value = (char *)cupsArrayFirst(pc->support_files);
2961 value;
2962 value = (char *)cupsArrayNext(pc->support_files))
2963 cupsFilePutConf(fp, "SupportFile", value);
2964
f14324a7
MS
2965 /*
2966 * IPP attributes, if any...
2967 */
2968
2969 if (attrs)
2970 {
2971 cupsFilePrintf(fp, "IPP " CUPS_LLFMT "\n", CUPS_LLCAST ippLength(attrs));
2972
cb7f98ee 2973 attrs->state = IPP_STATE_IDLE;
f14324a7
MS
2974 ippWriteIO(fp, (ipp_iocb_t)cupsFileWrite, 1, NULL, attrs);
2975 }
2976
2977 /*
2978 * Close and return...
2979 */
2980
321d8d57
MS
2981 if (cupsFileClose(fp))
2982 {
2983 unlink(newfile);
2984 return (0);
2985 }
2986
2987 unlink(filename);
2988 return (!rename(newfile, filename));
f14324a7
MS
2989}
2990
2991
d9fc71e4
MS
2992/*
2993 * '_ppdCreateFromIPP()' - Create a PPD file describing the capabilities
2994 * of an IPP printer.
2995 */
2996
d4259b45 2997char * /* O - PPD filename or @code NULL@ on error */
d9fc71e4
MS
2998_ppdCreateFromIPP(char *buffer, /* I - Filename buffer */
2999 size_t bufsize, /* I - Size of filename buffer */
3000 ipp_t *response) /* I - Get-Printer-Attributes response */
3001{
3002 cups_file_t *fp; /* PPD file */
7fad1ee9 3003 cups_array_t *sizes; /* Media sizes we've added */
d9fc71e4
MS
3004 ipp_attribute_t *attr, /* xxx-supported */
3005 *defattr, /* xxx-default */
3b6c3c8e 3006 *quality, /* print-quality-supported */
d9fc71e4
MS
3007 *x_dim, *y_dim; /* Media dimensions */
3008 ipp_t *media_size; /* Media size collection */
3009 char make[256], /* Make and model */
3010 *model, /* Model name */
3011 ppdname[PPD_MAX_NAME];
3012 /* PPD keyword */
3013 int i, j, /* Looping vars */
3014 count, /* Number of values */
3015 bottom, /* Largest bottom margin */
3016 left, /* Largest left margin */
3017 right, /* Largest right margin */
d4259b45
MS
3018 top, /* Largest top margin */
3019 is_apple = 0, /* Does the printer support Apple raster? */
3020 is_pdf = 0, /* Does the printer support PDF? */
3021 is_pwg = 0; /* Does the printer support PWG Raster? */
d9fc71e4
MS
3022 pwg_media_t *pwg; /* PWG media size */
3023 int xres, yres; /* Resolution values */
fa76bc3d
MS
3024 int resolutions[1000];
3025 /* Array of resolution indices */
c0886523
MS
3026 cups_lang_t *lang = cupsLangDefault();
3027 /* Localization info */
89550f3f 3028 cups_array_t *strings = NULL;/* Printer strings file */
560634d3
MS
3029 struct lconv *loc = localeconv();
3030 /* Locale data */
27ee2c4d
MS
3031 cups_array_t *fin_options = NULL;
3032 /* Finishing options */
c0886523
MS
3033 static const char * const finishings[][2] =
3034 { /* Finishings strings */
3035 { "bale", _("Bale") },
3036 { "bind", _("Bind") },
3037 { "bind-bottom", _("Bind (Reverse Landscape)") },
3038 { "bind-left", _("Bind (Portrait)") },
3039 { "bind-right", _("Bind (Reverse Portrait)") },
3040 { "bind-top", _("Bind (Landscape)") },
3041 { "booklet-maker", _("Booklet Maker") },
3042 { "coat", _("Coat") },
3043 { "cover", _("Cover") },
3044 { "edge-stitch", _("Staple Edge") },
3045 { "edge-stitch-bottom", _("Staple Edge (Reverse Landscape)") },
3046 { "edge-stitch-left", _("Staple Edge (Portrait)") },
3047 { "edge-stitch-right", _("Staple Edge (Reverse Portrait)") },
3048 { "edge-stitch-top", _("Staple Edge (Landscape)") },
3049 { "fold", _("Fold") },
3050 { "fold-accordian", _("Accordian Fold") },
3051 { "fold-double-gate", _("Double Gate Fold") },
2a8afc20 3052 { "fold-engineering-z", _("Engineering Z Fold") },
c0886523
MS
3053 { "fold-gate", _("Gate Fold") },
3054 { "fold-half", _("Half Fold") },
3055 { "fold-half-z", _("Half Z Fold") },
3056 { "fold-left-gate", _("Left Gate Fold") },
3057 { "fold-letter", _("Letter Fold") },
3058 { "fold-parallel", _("Parallel Fold") },
3059 { "fold-poster", _("Poster Fold") },
3060 { "fold-right-gate", _("Right Gate Fold") },
3061 { "fold-z", _("Z Fold") },
3062 { "jog-offset", _("Jog") },
3063 { "laminate", _("Laminate") },
3064 { "punch", _("Punch") },
3065 { "punch-bottom-left", _("Single Punch (Reverse Landscape)") },
3066 { "punch-bottom-right", _("Single Punch (Reverse Portrait)") },
3067 { "punch-double-bottom", _("2-Hole Punch (Reverse Portrait)") },
3068 { "punch-double-left", _("2-Hole Punch (Reverse Landscape)") },
3069 { "punch-double-right", _("2-Hole Punch (Landscape)") },
3070 { "punch-double-top", _("2-Hole Punch (Portrait)") },
3071 { "punch-quad-bottom", _("4-Hole Punch (Reverse Landscape)") },
3072 { "punch-quad-left", _("4-Hole Punch (Portrait)") },
3073 { "punch-quad-right", _("4-Hole Punch (Reverse Portrait)") },
3074 { "punch-quad-top", _("4-Hole Punch (Landscape)") },
3075 { "punch-top-left", _("Single Punch (Portrait)") },
3076 { "punch-top-right", _("Single Punch (Landscape)") },
3077 { "punch-triple-bottom", _("3-Hole Punch (Reverse Landscape)") },
3078 { "punch-triple-left", _("3-Hole Punch (Portrait)") },
3079 { "punch-triple-right", _("3-Hole Punch (Reverse Portrait)") },
3080 { "punch-triple-top", _("3-Hole Punch (Landscape)") },
2a8afc20
MS
3081 { "punch-multiple-bottom", _("Multi-Hole Punch (Reverse Landscape)") },
3082 { "punch-multiple-left", _("Multi-Hole Punch (Portrait)") },
3083 { "punch-multiple-right", _("Multi-Hole Punch (Reverse Portrait)") },
3084 { "punch-multiple-top", _("Multi-Hole Punch (Landscape)") },
c0886523
MS
3085 { "saddle-stitch", _("Saddle Stitch") },
3086 { "staple", _("Staple") },
3087 { "staple-bottom-left", _("Single Staple (Reverse Landscape)") },
3088 { "staple-bottom-right", _("Single Staple (Reverse Portrait)") },
3089 { "staple-dual-bottom", _("Double Staple (Reverse Landscape)") },
3090 { "staple-dual-left", _("Double Staple (Portrait)") },
3091 { "staple-dual-right", _("Double Staple (Reverse Portrait)") },
3092 { "staple-dual-top", _("Double Staple (Landscape)") },
3093 { "staple-top-left", _("Single Staple (Portrait)") },
3094 { "staple-top-right", _("Single Staple (Landscape)") },
3095 { "staple-triple-bottom", _("Triple Staple (Reverse Landscape)") },
3096 { "staple-triple-left", _("Triple Staple (Portrait)") },
3097 { "staple-triple-right", _("Triple Staple (Reverse Portrait)") },
3098 { "staple-triple-top", _("Triple Staple (Landscape)") },
3099 { "trim", _("Cut Media") }
3100 };
d9fc71e4
MS
3101
3102
3103 /*
3104 * Range check input...
3105 */
3106
fffed089
MS
3107 if (buffer)
3108 *buffer = '\0';
3109
a946858f
MS
3110 if (!buffer || bufsize < 1)
3111 {
3112 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
d9fc71e4 3113 return (NULL);
a946858f
MS
3114 }
3115
3116 if (!response)
3117 {
3118 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No IPP attributes."), 1);
3119 return (NULL);
3120 }
d9fc71e4
MS
3121
3122 /*
3123 * Open a temporary file for the PPD...
3124 */
3125
3126 if ((fp = cupsTempFile2(buffer, (int)bufsize)) == NULL)
a946858f
MS
3127 {
3128 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(errno), 0);
d9fc71e4 3129 return (NULL);
a946858f 3130 }
d9fc71e4
MS
3131
3132 /*
3133 * Standard stuff for PPD file...
3134 */
3135
3136 cupsFilePuts(fp, "*PPD-Adobe: \"4.3\"\n");
3137 cupsFilePuts(fp, "*FormatVersion: \"4.3\"\n");
3138 cupsFilePrintf(fp, "*FileVersion: \"%d.%d\"\n", CUPS_VERSION_MAJOR, CUPS_VERSION_MINOR);
3139 cupsFilePuts(fp, "*LanguageVersion: English\n");
3140 cupsFilePuts(fp, "*LanguageEncoding: ISOLatin1\n");
3141 cupsFilePuts(fp, "*PSVersion: \"(3010.000) 0\"\n");
3142 cupsFilePuts(fp, "*LanguageLevel: \"3\"\n");
3143 cupsFilePuts(fp, "*FileSystem: False\n");
3144 cupsFilePuts(fp, "*PCFileName: \"ippeve.ppd\"\n");
3145
3146 if ((attr = ippFindAttribute(response, "printer-make-and-model", IPP_TAG_TEXT)) != NULL)
3147 strlcpy(make, ippGetString(attr, 0, NULL), sizeof(make));
3148 else
3149 strlcpy(make, "Unknown Printer", sizeof(make));
3150
3151 if (!_cups_strncasecmp(make, "Hewlett Packard ", 16) ||
3152 !_cups_strncasecmp(make, "Hewlett-Packard ", 16))
3153 {
3154 model = make + 16;
3155 strlcpy(make, "HP", sizeof(make));
3156 }
3157 else if ((model = strchr(make, ' ')) != NULL)
3158 *model++ = '\0';
3159 else
3160 model = make;
3161
3162 cupsFilePrintf(fp, "*Manufacturer: \"%s\"\n", make);
3163 cupsFilePrintf(fp, "*ModelName: \"%s\"\n", model);
3164 cupsFilePrintf(fp, "*Product: \"(%s)\"\n", model);
3165 cupsFilePrintf(fp, "*NickName: \"%s\"\n", model);
3166 cupsFilePrintf(fp, "*ShortNickName: \"%s\"\n", model);
3167
fb2d5470
MS
3168 if ((attr = ippFindAttribute(response, "color-supported", IPP_TAG_BOOLEAN)) != NULL && ippGetBoolean(attr, 0))
3169 cupsFilePuts(fp, "*ColorDevice: True\n");
3170 else
3171 cupsFilePuts(fp, "*ColorDevice: False\n");
3172
d9fc71e4
MS
3173 cupsFilePrintf(fp, "*cupsVersion: %d.%d\n", CUPS_VERSION_MAJOR, CUPS_VERSION_MINOR);
3174 cupsFilePuts(fp, "*cupsSNMPSupplies: False\n");
89550f3f
MS
3175 cupsFilePrintf(fp, "*cupsLanguages: \"%s\"\n", lang->language);
3176
3177 if ((attr = ippFindAttribute(response, "printer-more-info", IPP_TAG_URI)) != NULL)
3178 cupsFilePrintf(fp, "*APSupplies: \"%s\"\n", ippGetString(attr, 0, NULL));
3179
3180 if ((attr = ippFindAttribute(response, "printer-charge-info-uri", IPP_TAG_URI)) != NULL)
3181 cupsFilePrintf(fp, "*cupsChargeInfoURI: \"%s\"\n", ippGetString(attr, 0, NULL));
3182
3183 if ((attr = ippFindAttribute(response, "printer-strings-uri", IPP_TAG_URI)) != NULL)
3184 {
3185 http_t *http = NULL; /* Connection to printer */
3186 char stringsfile[1024]; /* Temporary strings file */
3187
3188 if (cups_get_url(&http, ippGetString(attr, 0, NULL), stringsfile, sizeof(stringsfile)))
3189 {
3190 cupsFilePrintf(fp, "*cupsStringsURI: \"%s\"\n", ippGetString(attr, 0, NULL));
3191
3192 strings = _cupsMessageLoad(stringsfile, _CUPS_MESSAGE_STRINGS | _CUPS_MESSAGE_UNQUOTE);
3193
3194 unlink(stringsfile);
3195 }
3196
3197 if (http)
3198 httpClose(http);
3199 }
d9fc71e4
MS
3200
3201 /*
3202 * Filters...
3203 */
3204
3205 if ((attr = ippFindAttribute(response, "document-format-supported", IPP_TAG_MIMETYPE)) != NULL)
3206 {
d4259b45
MS
3207 is_apple = ippContainsString(attr, "image/urf");
3208 is_pdf = ippContainsString(attr, "application/pdf");
3209 is_pwg = ippContainsString(attr, "image/pwg-raster");
3210
d9fc71e4
MS
3211 for (i = 0, count = ippGetCount(attr); i < count; i ++)
3212 {
3213 const char *format = ippGetString(attr, i, NULL);
3214 /* PDL */
3215
d4259b45 3216 /*
07428f6a 3217 * Write cupsFilter2 lines for supported formats...
d4259b45
MS
3218 */
3219
d9fc71e4
MS
3220 if (!_cups_strcasecmp(format, "application/pdf"))
3221 cupsFilePuts(fp, "*cupsFilter2: \"application/vnd.cups-pdf application/pdf 10 -\"\n");
1add2337
MS
3222 else if (!_cups_strcasecmp(format, "image/jpeg") || !_cups_strcasecmp(format, "image/png"))
3223 cupsFilePrintf(fp, "*cupsFilter2: \"%s %s 0 -\"\n", format, format);
3224 else if (!_cups_strcasecmp(format, "image/pwg-raster") || !_cups_strcasecmp(format, "image/urf"))
3225 cupsFilePrintf(fp, "*cupsFilter2: \"%s %s 100 -\"\n", format, format);
d9fc71e4
MS
3226 }
3227 }
3228
d4259b45
MS
3229 if (!is_apple && !is_pdf && !is_pwg)
3230 goto bad_ppd;
3231
d9fc71e4
MS
3232 /*
3233 * PageSize/PageRegion/ImageableArea/PaperDimension
3234 */
3235
3236 if ((attr = ippFindAttribute(response, "media-bottom-margin-supported", IPP_TAG_INTEGER)) != NULL)
3237 {
3238 for (i = 1, bottom = ippGetInteger(attr, 0), count = ippGetCount(attr); i < count; i ++)
3239 if (ippGetInteger(attr, i) > bottom)
3240 bottom = ippGetInteger(attr, i);
3241 }
3242 else
3243 bottom = 1270;
3244
3245 if ((attr = ippFindAttribute(response, "media-left-margin-supported", IPP_TAG_INTEGER)) != NULL)
3246 {
3247 for (i = 1, left = ippGetInteger(attr, 0), count = ippGetCount(attr); i < count; i ++)
3248 if (ippGetInteger(attr, i) > left)
3249 left = ippGetInteger(attr, i);
3250 }
3251 else
3252 left = 635;
3253
3254 if ((attr = ippFindAttribute(response, "media-right-margin-supported", IPP_TAG_INTEGER)) != NULL)
3255 {
3256 for (i = 1, right = ippGetInteger(attr, 0), count = ippGetCount(attr); i < count; i ++)
3257 if (ippGetInteger(attr, i) > right)
3258 right = ippGetInteger(attr, i);
3259 }
3260 else
3261 right = 635;
3262
3263 if ((attr = ippFindAttribute(response, "media-top-margin-supported", IPP_TAG_INTEGER)) != NULL)
3264 {
3265 for (i = 1, top = ippGetInteger(attr, 0), count = ippGetCount(attr); i < count; i ++)
3266 if (ippGetInteger(attr, i) > top)
3267 top = ippGetInteger(attr, i);
3268 }
3269 else
3270 top = 1270;
3271
3272 if ((defattr = ippFindAttribute(response, "media-col-default", IPP_TAG_BEGIN_COLLECTION)) != NULL)
3273 {
3274 if ((attr = ippFindAttribute(ippGetCollection(defattr, 0), "media-size", IPP_TAG_BEGIN_COLLECTION)) != NULL)
3275 {
3276 media_size = ippGetCollection(attr, 0);
3277 x_dim = ippFindAttribute(media_size, "x-dimension", IPP_TAG_INTEGER);
3278 y_dim = ippFindAttribute(media_size, "y-dimension", IPP_TAG_INTEGER);
3279
2a8afc20 3280 if (x_dim && y_dim && (pwg = pwgMediaForSize(ippGetInteger(x_dim, 0), ippGetInteger(y_dim, 0))) != NULL)
d9fc71e4 3281 strlcpy(ppdname, pwg->ppd, sizeof(ppdname));
d9fc71e4
MS
3282 else
3283 strlcpy(ppdname, "Unknown", sizeof(ppdname));
3284 }
3285 else
3286 strlcpy(ppdname, "Unknown", sizeof(ppdname));
3287 }
670172ea
MS
3288 else if ((pwg = pwgMediaForPWG(ippGetString(ippFindAttribute(response, "media-default", IPP_TAG_ZERO), 0, NULL))) != NULL)
3289 strlcpy(ppdname, pwg->ppd, sizeof(ppdname));
3290 else
3291 strlcpy(ppdname, "Unknown", sizeof(ppdname));
d9fc71e4 3292
670172ea
MS
3293 if ((attr = ippFindAttribute(response, "media-size-supported", IPP_TAG_BEGIN_COLLECTION)) == NULL)
3294 attr = ippFindAttribute(response, "media-supported", IPP_TAG_ZERO);
3295 if (attr)
d9fc71e4
MS
3296 {
3297 cupsFilePrintf(fp, "*OpenUI *PageSize: PickOne\n"
3298 "*OrderDependency: 10 AnySetup *PageSize\n"
3299 "*DefaultPageSize: %s\n", ppdname);
7fad1ee9
MS
3300
3301 sizes = cupsArrayNew3((cups_array_func_t)strcmp, NULL, NULL, 0, (cups_acopy_func_t)strdup, (cups_afree_func_t)free);
3302
d9fc71e4
MS
3303 for (i = 0, count = ippGetCount(attr); i < count; i ++)
3304 {
670172ea
MS
3305 if (ippGetValueTag(attr) == IPP_TAG_BEGIN_COLLECTION)
3306 {
3307 media_size = ippGetCollection(attr, i);
3308 x_dim = ippFindAttribute(media_size, "x-dimension", IPP_TAG_INTEGER);
3309 y_dim = ippFindAttribute(media_size, "y-dimension", IPP_TAG_INTEGER);
d9fc71e4 3310
670172ea
MS
3311 pwg = pwgMediaForSize(ippGetInteger(x_dim, 0), ippGetInteger(y_dim, 0));
3312 }
3313 else
3314 pwg = pwgMediaForPWG(ippGetString(attr, i, NULL));
3315
3316 if (pwg)
d9fc71e4 3317 {
560634d3
MS
3318 char twidth[256], /* Width string */
3319 tlength[256]; /* Length string */
3320
7fad1ee9
MS
3321 if (cupsArrayFind(sizes, (void *)pwg->ppd))
3322 {
3323 cupsFilePrintf(fp, "*%% warning: Duplicate size '%s' reported by printer.\n", pwg->ppd);
3324 continue;
3325 }
3326
3327 cupsArrayAdd(sizes, (void *)pwg->ppd);
3328
560634d3
MS
3329 _cupsStrFormatd(twidth, twidth + sizeof(twidth), pwg->width * 72.0 / 2540.0, loc);
3330 _cupsStrFormatd(tlength, tlength + sizeof(tlength), pwg->length * 72.0 / 2540.0, loc);
3331
3332 cupsFilePrintf(fp, "*PageSize %s: \"<</PageSize[%s %s]>>setpagedevice\"\n", pwg->ppd, twidth, tlength);
d9fc71e4
MS
3333 }
3334 }
3335 cupsFilePuts(fp, "*CloseUI: *PageSize\n");
3336
7fad1ee9
MS
3337 cupsArrayDelete(sizes);
3338 sizes = cupsArrayNew3((cups_array_func_t)strcmp, NULL, NULL, 0, (cups_acopy_func_t)strdup, (cups_afree_func_t)free);
3339
d9fc71e4
MS
3340 cupsFilePrintf(fp, "*OpenUI *PageRegion: PickOne\n"
3341 "*OrderDependency: 10 AnySetup *PageRegion\n"
3342 "*DefaultPageRegion: %s\n", ppdname);
3343 for (i = 0, count = ippGetCount(attr); i < count; i ++)
3344 {
3be8e026
MS
3345 if (ippGetValueTag(attr) == IPP_TAG_BEGIN_COLLECTION)
3346 {
3347 media_size = ippGetCollection(attr, i);
3348 x_dim = ippFindAttribute(media_size, "x-dimension", IPP_TAG_INTEGER);
3349 y_dim = ippFindAttribute(media_size, "y-dimension", IPP_TAG_INTEGER);
d9fc71e4 3350
3be8e026
MS
3351 pwg = pwgMediaForSize(ippGetInteger(x_dim, 0), ippGetInteger(y_dim, 0));
3352 }
3353 else
3354 pwg = pwgMediaForPWG(ippGetString(attr, i, NULL));
3355
3356 if (pwg)
d9fc71e4 3357 {
560634d3
MS
3358 char twidth[256], /* Width string */
3359 tlength[256]; /* Length string */
3360
7fad1ee9
MS
3361 if (cupsArrayFind(sizes, (void *)pwg->ppd))
3362 continue;
3363
3364 cupsArrayAdd(sizes, (void *)pwg->ppd);
3365
560634d3
MS
3366 _cupsStrFormatd(twidth, twidth + sizeof(twidth), pwg->width * 72.0 / 2540.0, loc);
3367 _cupsStrFormatd(tlength, tlength + sizeof(tlength), pwg->length * 72.0 / 2540.0, loc);
3368
d1db56d4 3369 cupsFilePrintf(fp, "*PageRegion %s: \"<</PageSize[%s %s]>>setpagedevice\"\n", pwg->ppd, twidth, tlength);
d9fc71e4
MS
3370 }
3371 }
3372 cupsFilePuts(fp, "*CloseUI: *PageRegion\n");
3373
7fad1ee9
MS
3374 cupsArrayDelete(sizes);
3375 sizes = cupsArrayNew3((cups_array_func_t)strcmp, NULL, NULL, 0, (cups_acopy_func_t)strdup, (cups_afree_func_t)free);
3376
d9fc71e4
MS
3377 cupsFilePrintf(fp, "*DefaultImageableArea: %s\n"
3378 "*DefaultPaperDimension: %s\n", ppdname, ppdname);
3379 for (i = 0, count = ippGetCount(attr); i < count; i ++)
3380 {
3be8e026
MS
3381 if (ippGetValueTag(attr) == IPP_TAG_BEGIN_COLLECTION)
3382 {
3383 media_size = ippGetCollection(attr, i);
3384 x_dim = ippFindAttribute(media_size, "x-dimension", IPP_TAG_INTEGER);
3385 y_dim = ippFindAttribute(media_size, "y-dimension", IPP_TAG_INTEGER);
d9fc71e4 3386
3be8e026
MS
3387 pwg = pwgMediaForSize(ippGetInteger(x_dim, 0), ippGetInteger(y_dim, 0));
3388 }
3389 else
3390 pwg = pwgMediaForPWG(ippGetString(attr, i, NULL));
3391
3392 if (pwg)
d9fc71e4 3393 {
560634d3
MS
3394 char tleft[256], /* Left string */
3395 tbottom[256], /* Bottom string */
3396 tright[256], /* Right string */
3397 ttop[256], /* Top string */
3398 twidth[256], /* Width string */
3399 tlength[256]; /* Length string */
3400
7fad1ee9
MS
3401 if (cupsArrayFind(sizes, (void *)pwg->ppd))
3402 continue;
3403
3404 cupsArrayAdd(sizes, (void *)pwg->ppd);
3405
560634d3
MS
3406 _cupsStrFormatd(tleft, tleft + sizeof(tleft), left * 72.0 / 2540.0, loc);
3407 _cupsStrFormatd(tbottom, tbottom + sizeof(tbottom), bottom * 72.0 / 2540.0, loc);
3408 _cupsStrFormatd(tright, tright + sizeof(tright), (pwg->width - right) * 72.0 / 2540.0, loc);
3409 _cupsStrFormatd(ttop, ttop + sizeof(ttop), (pwg->length - top) * 72.0 / 2540.0, loc);
3410 _cupsStrFormatd(twidth, twidth + sizeof(twidth), pwg->width * 72.0 / 2540.0, loc);
3411 _cupsStrFormatd(tlength, tlength + sizeof(tlength), pwg->length * 72.0 / 2540.0, loc);
3412
3413 cupsFilePrintf(fp, "*ImageableArea %s: \"%s %s %s %s\"\n", pwg->ppd, tleft, tbottom, tright, ttop);
3414 cupsFilePrintf(fp, "*PaperDimension %s: \"%s %s\"\n", pwg->ppd, twidth, tlength);
d9fc71e4
MS
3415 }
3416 }
7fad1ee9
MS
3417
3418 cupsArrayDelete(sizes);
d9fc71e4 3419 }
d4259b45
MS
3420 else
3421 goto bad_ppd;
d9fc71e4
MS
3422
3423 /*
3424 * InputSlot...
3425 */
3426
670172ea 3427 if ((attr = ippFindAttribute(ippGetCollection(defattr, 0), "media-source", IPP_TAG_ZERO)) != NULL)
d9fc71e4
MS
3428 pwg_ppdize_name(ippGetString(attr, 0, NULL), ppdname, sizeof(ppdname));
3429 else
3430 strlcpy(ppdname, "Unknown", sizeof(ppdname));
3431
670172ea 3432 if ((attr = ippFindAttribute(response, "media-source-supported", IPP_TAG_ZERO)) != NULL && (count = ippGetCount(attr)) > 1)
d9fc71e4
MS
3433 {
3434 static const char * const sources[][2] =
c3355394 3435 { /* "media-source" strings */
c0886523
MS
3436 { "Auto", _("Automatic") },
3437 { "Main", _("Main") },
3438 { "Alternate", _("Alternate") },
3439 { "LargeCapacity", _("Large Capacity") },
3440 { "Manual", _("Manual") },
3441 { "Envelope", _("Envelope") },
3442 { "Disc", _("Disc") },
3443 { "Photo", _("Photo") },
3444 { "Hagaki", _("Hagaki") },
3445 { "MainRoll", _("Main Roll") },
3446 { "AlternateRoll", _("Alternate Roll") },
3447 { "Top", _("Top") },
3448 { "Middle", _("Middle") },
3449 { "Bottom", _("Bottom") },
3450 { "Side", _("Side") },
3451 { "Left", _("Left") },
3452 { "Right", _("Right") },
3453 { "Center", _("Center") },
3454 { "Rear", _("Rear") },
3455 { "ByPassTray", _("Multipurpose") },
3456 { "Tray1", _("Tray 1") },
3457 { "Tray2", _("Tray 2") },
3458 { "Tray3", _("Tray 3") },
3459 { "Tray4", _("Tray 4") },
3460 { "Tray5", _("Tray 5") },
3461 { "Tray6", _("Tray 6") },
3462 { "Tray7", _("Tray 7") },
3463 { "Tray8", _("Tray 8") },
3464 { "Tray9", _("Tray 9") },
3465 { "Tray10", _("Tray 10") },
3466 { "Tray11", _("Tray 11") },
3467 { "Tray12", _("Tray 12") },
3468 { "Tray13", _("Tray 13") },
3469 { "Tray14", _("Tray 14") },
3470 { "Tray15", _("Tray 15") },
3471 { "Tray16", _("Tray 16") },
3472 { "Tray17", _("Tray 17") },
3473 { "Tray18", _("Tray 18") },
3474 { "Tray19", _("Tray 19") },
3475 { "Tray20", _("Tray 20") },
3476 { "Roll1", _("Roll 1") },
3477 { "Roll2", _("Roll 2") },
3478 { "Roll3", _("Roll 3") },
3479 { "Roll4", _("Roll 4") },
3480 { "Roll5", _("Roll 5") },
3481 { "Roll6", _("Roll 6") },
3482 { "Roll7", _("Roll 7") },
3483 { "Roll8", _("Roll 8") },
3484 { "Roll9", _("Roll 9") },
3485 { "Roll10", _("Roll 10") }
d9fc71e4
MS
3486 };
3487
3488 cupsFilePrintf(fp, "*OpenUI *InputSlot: PickOne\n"
3489 "*OrderDependency: 10 AnySetup *InputSlot\n"
3490 "*DefaultInputSlot: %s\n", ppdname);
3491 for (i = 0, count = ippGetCount(attr); i < count; i ++)
3492 {
3493 pwg_ppdize_name(ippGetString(attr, i, NULL), ppdname, sizeof(ppdname));
3494
3495 for (j = 0; j < (int)(sizeof(sources) / sizeof(sources[0])); j ++)
3496 if (!strcmp(sources[j][0], ppdname))
3497 {
c0886523 3498 cupsFilePrintf(fp, "*InputSlot %s/%s: \"<</MediaPosition %d>>setpagedevice\"\n", ppdname, _cupsLangString(lang, sources[j][1]), j);
d9fc71e4
MS
3499 break;
3500 }
3501 }
3502 cupsFilePuts(fp, "*CloseUI: *InputSlot\n");
3503 }
3504
3505 /*
3506 * MediaType...
3507 */
3508
670172ea 3509 if ((attr = ippFindAttribute(ippGetCollection(defattr, 0), "media-type", IPP_TAG_ZERO)) != NULL)
d9fc71e4
MS
3510 pwg_ppdize_name(ippGetString(attr, 0, NULL), ppdname, sizeof(ppdname));
3511 else
3512 strlcpy(ppdname, "Unknown", sizeof(ppdname));
3513
670172ea 3514 if ((attr = ippFindAttribute(response, "media-type-supported", IPP_TAG_ZERO)) != NULL && (count = ippGetCount(attr)) > 1)
d9fc71e4 3515 {
c3355394
MS
3516 static const char * const media_types[][2] =
3517 { /* "media-type" strings */
3518 { "aluminum", _("Aluminum") },
3519 { "auto", _("Automatic") },
3520 { "back-print-film", _("Back Print Film") },
3521 { "cardboard", _("Cardboard") },
3522 { "cardstock", _("Cardstock") },
3523 { "cd", _("CD") },
4886d674
MS
3524 { "com.hp.advanced-photo", _("Advanced Photo Paper") }, /* HP */
3525 { "com.hp.brochure-glossy", _("Glossy Brochure Paper") }, /* HP */
3526 { "com.hp.brochure-matte", _("Matte Brochure Paper") }, /* HP */
3527 { "com.hp.cover-matte", _("Matte Cover Paper") }, /* HP */
3528 { "com.hp.ecosmart-lite", _("Office Recycled Paper") }, /* HP */
3529 { "com.hp.everyday-glossy", _("Everyday Glossy Photo Paper") }, /* HP */
3530 { "com.hp.everyday-matte", _("Everyday Matte Paper") }, /* HP */
3531 { "com.hp.extra-heavy", _("Extra Heavyweight Paper") }, /* HP */
3532 { "com.hp.intermediate", _("Multipurpose Paper") }, /* HP */
3533 { "com.hp.mid-weight", _("Mid-Weight Paper") }, /* HP */
3534 { "com.hp.premium-inkjet", _("Premium Inkjet Paper") }, /* HP */
3535 { "com.hp.premium-photo", _("Premium Photo Glossy Paper") }, /* HP */
3536 { "com.hp.premium-presentation-matte", _("Premium Presentation Matte Paper") }, /* HP */
c3355394
MS
3537 { "continuous", _("Continuous") },
3538 { "continuous-long", _("Continuous Long") },
3539 { "continuous-short", _("Continuous Short") },
3540 { "disc", _("Optical Disc") },
3541 { "disc-glossy", _("Glossy Optical Disc") },
3542 { "disc-high-gloss", _("High Gloss Optical Disc") },
3543 { "disc-matte", _("Matte Optical Disc") },
3544 { "disc-satin", _("Satin Optical Disc") },
3545 { "disc-semi-gloss", _("Semi-Gloss Optical Disc") },
3546 { "double-wall", _("Double Wall Cardboard") },
3547 { "dry-film", _("Dry Film") },
3548 { "dvd", _("DVD") },
3549 { "embossing-foil", _("Embossing Foil") },
3550 { "end-board", _("End Board") },
3551 { "envelope", _("Envelope") },
3552 { "envelope-archival", _("Archival Envelope") },
3553 { "envelope-bond", _("Bond Envelope") },
3554 { "envelope-coated", _("Coated Envelope") },
3555 { "envelope-cotton", _("Cotton Envelope") },
3556 { "envelope-fine", _("Fine Envelope") },
3557 { "envelope-heavyweight", _("Heavyweight Envelope") },
3558 { "envelope-inkjet", _("Inkjet Envelope") },
3559 { "envelope-lightweight", _("Lightweight Envelope") },
3560 { "envelope-plain", _("Plain Envelope") },
3561 { "envelope-preprinted", _("Preprinted Envelope") },
3562 { "envelope-window", _("Windowed Envelope") },
3563 { "fabric", _("Fabric") },
3564 { "fabric-archival", _("Archival Fabric") },
3565 { "fabric-glossy", _("Glossy Fabric") },
3566 { "fabric-high-gloss", _("High Gloss Fabric") },
3567 { "fabric-matte", _("Matte Fabric") },
3568 { "fabric-semi-gloss", _("Semi-Gloss Fabric") },
3569 { "fabric-waterproof", _("Waterproof Fabric") },
3570 { "film", _("Film") },
3571 { "flexo-base", _("Flexo Base") },
3572 { "flexo-photo-polymer", _("Flexo Photo Polymer") },
3573 { "flute", _("Flute") },
3574 { "foil", _("Foil") },
3575 { "full-cut-tabs", _("Full Cut Tabs") },
3576 { "glass", _("Glass") },
3577 { "glass-colored", _("Glass Colored") },
3578 { "glass-opaque", _("Glass Opaque") },
3579 { "glass-surfaced", _("Glass Surfaced") },
3580 { "glass-textured", _("Glass Textured") },
3581 { "gravure-cylinder", _("Gravure Cylinder") },
3582 { "image-setter-paper", _("Image Setter Paper") },
3583 { "imaging-cylinder", _("Imaging Cylinder") },
4886d674
MS
3584 { "jp.co.canon_photo-paper-plus-glossy-ii", _("Photo Paper Plus Glossy II") }, /* Canon */
3585 { "jp.co.canon_photo-paper-pro-platinum", _("Photo Paper Pro Platinum") }, /* Canon */
3586 { "jp.co.canon-photo-paper-plus-glossy-ii", _("Photo Paper Plus Glossy II") }, /* Canon */
3587 { "jp.co.canon-photo-paper-pro-platinum", _("Photo Paper Pro Platinum") }, /* Canon */
c3355394
MS
3588 { "labels", _("Labels") },
3589 { "labels-colored", _("Colored Labels") },
3590 { "labels-glossy", _("Glossy Labels") },
3591 { "labels-high-gloss", _("High Gloss Labels") },
3592 { "labels-inkjet", _("Inkjet Labels") },
3593 { "labels-matte", _("Matte Labels") },
3594 { "labels-permanent", _("Permanent Labels") },
3595 { "labels-satin", _("Satin Labels") },
3596 { "labels-security", _("Security Labels") },
3597 { "labels-semi-gloss", _("Semi-Gloss Labels") },
3598 { "laminating-foil", _("Laminating Foil") },
3599 { "letterhead", _("Letterhead") },
3600 { "metal", _("Metal") },
3601 { "metal-glossy", _("Metal Glossy") },
3602 { "metal-high-gloss", _("Metal High Gloss") },
3603 { "metal-matte", _("Metal Matte") },
3604 { "metal-satin", _("Metal Satin") },
3605 { "metal-semi-gloss", _("Metal Semi Gloss") },
3606 { "mounting-tape", _("Mounting Tape") },
3607 { "multi-layer", _("Multi Layer") },
3608 { "multi-part-form", _("Multi Part Form") },
3609 { "other", _("Other") },
3610 { "paper", _("Paper") },
3ae6282d 3611 { "photo", _("Photo Paper") }, /* HP mis-spelling */
c3355394 3612 { "photographic", _("Photo Paper") },
16b0b411 3613 { "photographic-archival", _("Archival Photo Paper") },
c3355394
MS
3614 { "photographic-film", _("Photo Film") },
3615 { "photographic-glossy", _("Glossy Photo Paper") },
3616 { "photographic-high-gloss", _("High Gloss Photo Paper") },
3617 { "photographic-matte", _("Matte Photo Paper") },
3618 { "photographic-satin", _("Satin Photo Paper") },
3619 { "photographic-semi-gloss", _("Semi-Gloss Photo Paper") },
3620 { "plastic", _("Plastic") },
3621 { "plastic-archival", _("Plastic Archival") },
3622 { "plastic-colored", _("Plastic Colored") },
3623 { "plastic-glossy", _("Plastic Glossy") },
3624 { "plastic-high-gloss", _("Plastic High Gloss") },
3625 { "plastic-matte", _("Plastic Matte") },
3626 { "plastic-satin", _("Plastic Satin") },
3627 { "plastic-semi-gloss", _("Plastic Semi Gloss") },
3628 { "plate", _("Plate") },
3629 { "polyester", _("Polyester") },
3630 { "pre-cut-tabs", _("Pre Cut Tabs") },
3631 { "roll", _("Roll") },
3632 { "screen", _("Screen") },
3633 { "screen-paged", _("Screen Paged") },
3634 { "self-adhesive", _("Self Adhesive") },
3635 { "self-adhesive-film", _("Self Adhesive Film") },
3636 { "shrink-foil", _("Shrink Foil") },
3637 { "single-face", _("Single Face") },
3638 { "single-wall", _("Single Wall Cardboard") },
3639 { "sleeve", _("Sleeve") },
4886d674
MS
3640 { "stationery", _("Plain Paper") },
3641 { "stationery-archival", _("Archival Paper") },
c3355394 3642 { "stationery-coated", _("Coated Paper") },
4886d674 3643 { "stationery-cotton", _("Cotton Paper") },
c3355394
MS
3644 { "stationery-fine", _("Vellum Paper") },
3645 { "stationery-heavyweight", _("Heavyweight Paper") },
4886d674
MS
3646 { "stationery-heavyweight-coated", _("Heavyweight Coated Paper") },
3647 { "stationery-inkjet", _("Inkjet Paper") },
c3355394
MS
3648 { "stationery-letterhead", _("Letterhead") },
3649 { "stationery-lightweight", _("Lightweight Paper") },
3650 { "stationery-preprinted", _("Preprinted Paper") },
3651 { "stationery-prepunched", _("Punched Paper") },
3652 { "tab-stock", _("Tab Stock") },
3653 { "tractor", _("Tractor") },
3654 { "transfer", _("Transfer") },
3655 { "transparency", _("Transparency") },
3656 { "triple-wall", _("Triple Wall Cardboard") },
3657 { "wet-film", _("Wet Film") }
d9fc71e4
MS
3658 };
3659
3660 cupsFilePrintf(fp, "*OpenUI *MediaType: PickOne\n"
3661 "*OrderDependency: 10 AnySetup *MediaType\n"
3662 "*DefaultMediaType: %s\n", ppdname);
3ae6282d 3663 for (i = 0; i < count; i ++)
d9fc71e4 3664 {
3ae6282d 3665 const char *keyword = ippGetString(attr, i, NULL);
d9fc71e4 3666
3ae6282d 3667 pwg_ppdize_name(keyword, ppdname, sizeof(ppdname));
d9fc71e4 3668
3ae6282d 3669 for (j = 0; j < (int)(sizeof(media_types) / sizeof(media_types[0])); j ++)
4886d674 3670 if (!strcmp(keyword, media_types[j][0]))
3ae6282d
MS
3671 break;
3672
3673 if (j < (int)(sizeof(media_types) / sizeof(media_types[0])))
3674 cupsFilePrintf(fp, "*MediaType %s/%s: \"<</MediaType(%s)>>setpagedevice\"\n", ppdname, _cupsLangString(lang, media_types[j][1]), ppdname);
3675 else
89550f3f
MS
3676 {
3677 char msg[256]; /* Message key */
3678 const char *str; /* Localized string */
3679
3680 snprintf(msg, sizeof(msg), "media-type.%s", keyword);
3681 if ((str = _cupsMessageLookup(strings, msg)) == msg)
3682 str = keyword;
3683
3684 cupsFilePrintf(fp, "*MediaType %s/%s: \"<</MediaType(%s)>>setpagedevice\"\n", ppdname, str, ppdname);
3685 }
d9fc71e4
MS
3686 }
3687 cupsFilePuts(fp, "*CloseUI: *MediaType\n");
3688 }
3689
3690 /*
3691 * ColorModel...
3692 */
3693
fa76bc3d
MS
3694 if ((attr = ippFindAttribute(response, "urf-supported", IPP_TAG_KEYWORD)) == NULL)
3695 if ((attr = ippFindAttribute(response, "pwg-raster-document-type-supported", IPP_TAG_KEYWORD)) == NULL)
670172ea
MS
3696 if ((attr = ippFindAttribute(response, "print-color-mode-supported", IPP_TAG_KEYWORD)) == NULL)
3697 attr = ippFindAttribute(response, "output-mode-supported", IPP_TAG_KEYWORD);
d9fc71e4
MS
3698
3699 if (attr)
3700 {
3701 const char *default_color = NULL; /* Default */
3702
d9fc71e4
MS
3703 for (i = 0, count = ippGetCount(attr); i < count; i ++)
3704 {
3705 const char *keyword = ippGetString(attr, i, NULL);
3706 /* Keyword for color/bit depth */
3707
d2817c9f 3708 if (!strcasecmp(keyword, "black_1") || !strcmp(keyword, "bi-level") || !strcmp(keyword, "process-bi-level"))
d9fc71e4 3709 {
5e7464ec 3710 if (!default_color)
c0886523
MS
3711 cupsFilePrintf(fp, "*OpenUI *ColorModel/%s: PickOne\n"
3712 "*OrderDependency: 10 AnySetup *ColorModel\n", _cupsLangString(lang, _("Color Mode")));
5e7464ec 3713
c0886523 3714 cupsFilePrintf(fp, "*ColorModel FastGray/%s: \"<</cupsColorSpace 3/cupsBitsPerColor 1/cupsColorOrder 0/cupsCompression 0>>setpagedevice\"\n", _cupsLangString(lang, _("Fast Grayscale")));
d9fc71e4
MS
3715
3716 if (!default_color)
3717 default_color = "FastGray";
3718 }
d2817c9f 3719 else if (!strcasecmp(keyword, "sgray_8") || !strcmp(keyword, "W8") || !strcmp(keyword, "monochrome") || !strcmp(keyword, "process-monochrome"))
d9fc71e4 3720 {
5e7464ec 3721 if (!default_color)
c0886523
MS
3722 cupsFilePrintf(fp, "*OpenUI *ColorModel/%s: PickOne\n"
3723 "*OrderDependency: 10 AnySetup *ColorModel\n", _cupsLangString(lang, _("Color Mode")));
5e7464ec 3724
c0886523 3725 cupsFilePrintf(fp, "*ColorModel Gray/%s: \"<</cupsColorSpace 18/cupsBitsPerColor 8/cupsColorOrder 0/cupsCompression 0>>setpagedevice\"\n", _cupsLangString(lang, _("Grayscale")));
d9fc71e4
MS
3726
3727 if (!default_color || !strcmp(default_color, "FastGray"))
3728 default_color = "Gray";
3729 }
d2817c9f 3730 else if (!strcasecmp(keyword, "srgb_8") || !strcmp(keyword, "SRGB24") || !strcmp(keyword, "color"))
d9fc71e4 3731 {
5e7464ec 3732 if (!default_color)
c0886523
MS
3733 cupsFilePrintf(fp, "*OpenUI *ColorModel/%s: PickOne\n"
3734 "*OrderDependency: 10 AnySetup *ColorModel\n", _cupsLangString(lang, _("Color Mode")));
5e7464ec 3735
c0886523 3736 cupsFilePrintf(fp, "*ColorModel RGB/%s: \"<</cupsColorSpace 19/cupsBitsPerColor 8/cupsColorOrder 0/cupsCompression 0>>setpagedevice\"\n", _cupsLangString(lang, _("Color")));
d9fc71e4
MS
3737
3738 default_color = "RGB";
3739 }
d2817c9f 3740 else if (!strcasecmp(keyword, "adobe-rgb_16") || !strcmp(keyword, "ADOBERGB48"))
2a8afc20
MS
3741 {
3742 if (!default_color)
3743 cupsFilePrintf(fp, "*OpenUI *ColorModel/%s: PickOne\n"
3744 "*OrderDependency: 10 AnySetup *ColorModel\n", _cupsLangString(lang, _("Color Mode")));
3745
3746 cupsFilePrintf(fp, "*ColorModel AdobeRGB/%s: \"<</cupsColorSpace 20/cupsBitsPerColor 16/cupsColorOrder 0/cupsCompression 0>>setpagedevice\"\n", _cupsLangString(lang, _("Deep Color")));
3747
d4259b45
MS
3748 if (!default_color)
3749 default_color = "AdobeRGB";
2a8afc20 3750 }
d9fc71e4
MS
3751 }
3752
3753 if (default_color)
5e7464ec 3754 {
d9fc71e4 3755 cupsFilePrintf(fp, "*DefaultColorModel: %s\n", default_color);
5e7464ec
MS
3756 cupsFilePuts(fp, "*CloseUI: *ColorModel\n");
3757 }
d9fc71e4
MS
3758 }
3759
3760 /*
3761 * Duplex...
3762 */
3763
3764 if ((attr = ippFindAttribute(response, "sides-supported", IPP_TAG_KEYWORD)) != NULL && ippContainsString(attr, "two-sided-long-edge"))
3765 {
c0886523
MS
3766 cupsFilePrintf(fp, "*OpenUI *Duplex/%s: PickOne\n"
3767 "*OrderDependency: 10 AnySetup *Duplex\n"
3768 "*DefaultDuplex: None\n"
3769 "*Duplex None/%s: \"<</Duplex false>>setpagedevice\"\n"
3770 "*Duplex DuplexNoTumble/%s: \"<</Duplex true/Tumble false>>setpagedevice\"\n"
3771 "*Duplex DuplexTumble/%s: \"<</Duplex true/Tumble true>>setpagedevice\"\n"
3772 "*CloseUI: *Duplex\n", _cupsLangString(lang, _("2-Sided Printing")), _cupsLangString(lang, _("Off (1-Sided)")), _cupsLangString(lang, _("Long-Edge (Portrait)")), _cupsLangString(lang, _("Short-Edge (Landscape)")));
d9fc71e4 3773
fa76bc3d
MS
3774 if ((attr = ippFindAttribute(response, "urf-supported", IPP_TAG_KEYWORD)) != NULL)
3775 {
3776 for (i = 0, count = ippGetCount(attr); i < count; i ++)
3777 {
3778 const char *dm = ippGetString(attr, i, NULL);
3779 /* DM value */
3780
3781 if (!_cups_strcasecmp(dm, "DM1"))
3782 {
3783 cupsFilePuts(fp, "*cupsBackSide: Normal\n");
3784 break;
3785 }
3786 else if (!_cups_strcasecmp(dm, "DM2"))
3787 {
3788 cupsFilePuts(fp, "*cupsBackSide: Flipped\n");
3789 break;
3790 }
3791 else if (!_cups_strcasecmp(dm, "DM3"))
3792 {
3793 cupsFilePuts(fp, "*cupsBackSide: Rotated\n");
3794 break;
3795 }
3796 else if (!_cups_strcasecmp(dm, "DM4"))
3797 {
3798 cupsFilePuts(fp, "*cupsBackSide: ManualTumble\n");
3799 break;
3800 }
3801 }
3802 }
3803 else if ((attr = ippFindAttribute(response, "pwg-raster-document-sheet-back", IPP_TAG_KEYWORD)) != NULL)
d9fc71e4
MS
3804 {
3805 const char *keyword = ippGetString(attr, 0, NULL);
3806 /* Keyword value */
3807
3808 if (!strcmp(keyword, "flipped"))
3809 cupsFilePuts(fp, "*cupsBackSide: Flipped\n");
3810 else if (!strcmp(keyword, "manual-tumble"))
3811 cupsFilePuts(fp, "*cupsBackSide: ManualTumble\n");
3812 else if (!strcmp(keyword, "normal"))
3813 cupsFilePuts(fp, "*cupsBackSide: Normal\n");
3814 else
3815 cupsFilePuts(fp, "*cupsBackSide: Rotated\n");
3816 }
3817 }
3818
1999164d
MS
3819 /*
3820 * Output bin...
3821 */
3822
3823 if ((attr = ippFindAttribute(response, "output-bin-default", IPP_TAG_ZERO)) != NULL)
3824 pwg_ppdize_name(ippGetString(attr, 0, NULL), ppdname, sizeof(ppdname));
3825 else
3826 strlcpy(ppdname, "Unknown", sizeof(ppdname));
3827
3942a9c8 3828 if ((attr = ippFindAttribute(response, "output-bin-supported", IPP_TAG_ZERO)) != NULL && (count = ippGetCount(attr)) > 1)
1999164d
MS
3829 {
3830 static const char * const output_bins[][2] =
3831 { /* "output-bin" strings */
3832 { "auto", _("Automatic") },
3833 { "bottom", _("Bottom Tray") },
3834 { "center", _("Center Tray") },
3835 { "face-down", _("Face Down") },
3836 { "face-up", _("Face Up") },
3837 { "large-capacity", _("Large Capacity Tray") },
3838 { "left", _("Left Tray") },
3839 { "mailbox-1", _("Mailbox 1") },
3840 { "mailbox-2", _("Mailbox 2") },
3841 { "mailbox-3", _("Mailbox 3") },
3842 { "mailbox-4", _("Mailbox 4") },
3843 { "mailbox-5", _("Mailbox 5") },
3844 { "mailbox-6", _("Mailbox 6") },
3845 { "mailbox-7", _("Mailbox 7") },
3846 { "mailbox-8", _("Mailbox 8") },
3847 { "mailbox-9", _("Mailbox 9") },
3848 { "mailbox-10", _("Mailbox 10") },
3849 { "middle", _("Middle") },
3850 { "my-mailbox", _("My Mailbox") },
3851 { "rear", _("Rear Tray") },
3852 { "right", _("Right Tray") },
3853 { "side", _("Side Tray") },
3854 { "stacker-1", _("Stacker 1") },
3855 { "stacker-2", _("Stacker 2") },
3856 { "stacker-3", _("Stacker 3") },
3857 { "stacker-4", _("Stacker 4") },
3858 { "stacker-5", _("Stacker 5") },
3859 { "stacker-6", _("Stacker 6") },
3860 { "stacker-7", _("Stacker 7") },
3861 { "stacker-8", _("Stacker 8") },
3862 { "stacker-9", _("Stacker 9") },
3863 { "stacker-10", _("Stacker 10") },
3864 { "top", _("Top Tray") },
3865 { "tray-1", _("Tray 1") },
3866 { "tray-2", _("Tray 2") },
3867 { "tray-3", _("Tray 3") },
3868 { "tray-4", _("Tray 4") },
3869 { "tray-5", _("Tray 5") },
3870 { "tray-6", _("Tray 6") },
3871 { "tray-7", _("Tray 7") },
3872 { "tray-8", _("Tray 8") },
3873 { "tray-9", _("Tray 9") },
3874 { "tray-10", _("Tray 10") }
3875 };
3876
3877 cupsFilePrintf(fp, "*OpenUI *OutputBin: PickOne\n"
3878 "*OrderDependency: 10 AnySetup *OutputBin\n"
3879 "*DefaultOutputBin: %s\n", ppdname);
3880 for (i = 0; i < (int)(sizeof(output_bins) / sizeof(output_bins[0])); i ++)
3881 {
3882 if (!ippContainsString(attr, output_bins[i][0]))
3883 continue;
3884
3885 pwg_ppdize_name(output_bins[i][0], ppdname, sizeof(ppdname));
3886
3887 cupsFilePrintf(fp, "*OutputBin %s/%s: \"\"\n", ppdname, _cupsLangString(lang, output_bins[i][1]));
3888 }
3889 cupsFilePuts(fp, "*CloseUI: *OutputBin\n");
3890 }
3891
c0886523
MS
3892 /*
3893 * Finishing options...
3894 */
3895
a740a849 3896 if ((attr = ippFindAttribute(response, "finishings-supported", IPP_TAG_ENUM)) != NULL)
c0886523 3897 {
c0886523 3898 const char *name; /* String name */
a740a849 3899 int value; /* Enum value */
30ea7759 3900 cups_array_t *names; /* Names we've added */
c0886523 3901
27ee2c4d
MS
3902 count = ippGetCount(attr);
3903 names = cupsArrayNew3((cups_array_func_t)strcmp, NULL, NULL, 0, (cups_acopy_func_t)strdup, (cups_afree_func_t)free);
3904 fin_options = cupsArrayNew((cups_array_func_t)strcmp, NULL);
c0886523 3905
a740a849
MS
3906 /*
3907 * Staple/Bind/Stitch
3908 */
c0886523
MS
3909
3910 for (i = 0; i < count; i ++)
3911 {
a740a849
MS
3912 value = ippGetInteger(attr, i);
3913 name = ippEnumString("finishings", value);
c0886523 3914
a740a849
MS
3915 if (!strncmp(name, "staple-", 7) || !strncmp(name, "bind-", 5) || !strncmp(name, "edge-stitch-", 12) || !strcmp(name, "saddle-stitch"))
3916 break;
3917 }
30ea7759 3918
a740a849
MS
3919 if (i < count)
3920 {
27ee2c4d
MS
3921 cupsArrayAdd(fin_options, "*StapleLocation");
3922
a740a849
MS
3923 cupsFilePrintf(fp, "*OpenUI *StapleLocation/%s: PickOne\n", _cupsLangString(lang, _("Staple")));
3924 cupsFilePuts(fp, "*OrderDependency: 10 AnySetup *StapleLocation\n");
3925 cupsFilePuts(fp, "*DefaultStapleLocation: None\n");
3926 cupsFilePrintf(fp, "*StapleLocation None/%s: \"\"\n", _cupsLangString(lang, _("None")));
30ea7759 3927
a740a849 3928 for (; i < count; i ++)
c0886523 3929 {
a740a849
MS
3930 value = ippGetInteger(attr, i);
3931 name = ippEnumString("finishings", value);
c0886523 3932
a740a849
MS
3933 if (strncmp(name, "staple-", 7) && strncmp(name, "bind-", 5) && strncmp(name, "edge-stitch-", 12) && strcmp(name, "saddle-stitch"))
3934 continue;
c0886523 3935
a740a849
MS
3936 if (cupsArrayFind(names, (char *)name))
3937 continue; /* Already did this finishing template */
3938
3939 cupsArrayAdd(names, (char *)name);
3940
3941 for (j = 0; j < (int)(sizeof(finishings) / sizeof(finishings[0])); j ++)
3942 {
3943 if (!strcmp(finishings[j][0], name))
3944 {
3945 cupsFilePrintf(fp, "*StapleLocation %s/%s: \"\"\n", name, _cupsLangString(lang, finishings[j][1]));
3946 cupsFilePrintf(fp, "*cupsIPPFinishings %d/%s: \"*StapleLocation %s\"\n", value, name, name);
3947 break;
3948 }
3949 }
c0886523 3950 }
a740a849
MS
3951
3952 cupsFilePuts(fp, "*CloseUI: *StapleLocation\n");
c0886523
MS
3953 }
3954
a740a849
MS
3955 /*
3956 * Fold
3957 */
30ea7759 3958
a740a849
MS
3959 for (i = 0; i < count; i ++)
3960 {
3961 value = ippGetInteger(attr, i);
3962 name = ippEnumString("finishings", value);
c0886523 3963
a740a849
MS
3964 if (!strncmp(name, "fold-", 5))
3965 break;
3966 }
3967
3968 if (i < count)
3969 {
27ee2c4d
MS
3970 cupsArrayAdd(fin_options, "*FoldType");
3971
a740a849
MS
3972 cupsFilePrintf(fp, "*OpenUI *FoldType/%s: PickOne\n", _cupsLangString(lang, _("Fold")));
3973 cupsFilePuts(fp, "*OrderDependency: 10 AnySetup *FoldType\n");
3974 cupsFilePuts(fp, "*DefaultFoldType: None\n");
3975 cupsFilePrintf(fp, "*FoldType None/%s: \"\"\n", _cupsLangString(lang, _("None")));
3976
3977 for (; i < count; i ++)
3978 {
3979 value = ippGetInteger(attr, i);
3980 name = ippEnumString("finishings", value);
3981
3982 if (strncmp(name, "fold-", 5))
3983 continue;
c0886523 3984
a740a849
MS
3985 if (cupsArrayFind(names, (char *)name))
3986 continue; /* Already did this finishing template */
3987
3988 cupsArrayAdd(names, (char *)name);
3989
3990 for (j = 0; j < (int)(sizeof(finishings) / sizeof(finishings[0])); j ++)
3991 {
3992 if (!strcmp(finishings[j][0], name))
3993 {
3994 cupsFilePrintf(fp, "*FoldType %s/%s: \"\"\n", name, _cupsLangString(lang, finishings[j][1]));
3995 cupsFilePrintf(fp, "*cupsIPPFinishings %d/%s: \"*FoldType %s\"\n", value, name, name);
3996 break;
3997 }
3998 }
3999 }
4000
4001 cupsFilePuts(fp, "*CloseUI: *FoldType\n");
4002 }
4003
4004 /*
4005 * Punch
4006 */
c0886523
MS
4007
4008 for (i = 0; i < count; i ++)
4009 {
a740a849
MS
4010 value = ippGetInteger(attr, i);
4011 name = ippEnumString("finishings", value);
4012
4013 if (!strncmp(name, "punch-", 6))
4014 break;
4015 }
4016
4017 if (i < count)
4018 {
27ee2c4d
MS
4019 cupsArrayAdd(fin_options, "*PunchMedia");
4020
a740a849
MS
4021 cupsFilePrintf(fp, "*OpenUI *PunchMedia/%s: PickOne\n", _cupsLangString(lang, _("Punch")));
4022 cupsFilePuts(fp, "*OrderDependency: 10 AnySetup *PunchMedia\n");
4023 cupsFilePuts(fp, "*DefaultPunchMedia: None\n");
4024 cupsFilePrintf(fp, "*PunchMedia None/%s: \"\"\n", _cupsLangString(lang, _("None")));
c0886523 4025
a740a849 4026 for (i = 0; i < count; i ++)
c0886523 4027 {
a740a849
MS
4028 value = ippGetInteger(attr, i);
4029 name = ippEnumString("finishings", value);
4030
4031 if (strncmp(name, "punch-", 6))
4032 continue;
4033
4034 if (cupsArrayFind(names, (char *)name))
4035 continue; /* Already did this finishing template */
4036
4037 cupsArrayAdd(names, (char *)name);
4038
4039 for (j = 0; j < (int)(sizeof(finishings) / sizeof(finishings[0])); j ++)
4040 {
4041 if (!strcmp(finishings[j][0], name))
4042 {
4043 cupsFilePrintf(fp, "*PunchMedia %s/%s: \"\"\n", name, _cupsLangString(lang, finishings[j][1]));
4044 cupsFilePrintf(fp, "*cupsIPPFinishings %d/%s: \"*PunchMedia %s\"\n", value, name, name);
4045 break;
4046 }
4047 }
c0886523 4048 }
a740a849
MS
4049
4050 cupsFilePuts(fp, "*CloseUI: *PunchMedia\n");
4051 }
4052
4053 /*
4054 * Booklet
4055 */
4056
4057 if (ippContainsInteger(attr, IPP_FINISHINGS_BOOKLET_MAKER))
4058 {
27ee2c4d
MS
4059 cupsArrayAdd(fin_options, "*Booklet");
4060
a740a849
MS
4061 cupsFilePrintf(fp, "*OpenUI *Booklet/%s: Boolean\n", _cupsLangString(lang, _("Booklet")));
4062 cupsFilePuts(fp, "*OrderDependency: 10 AnySetup *Booklet\n");
4063 cupsFilePuts(fp, "*DefaultBooklet: False\n");
4064 cupsFilePuts(fp, "*Booklet False: \"\"\n");
4065 cupsFilePuts(fp, "*Booklet True: \"\"\n");
4066 cupsFilePrintf(fp, "*cupsIPPFinishings %d/booklet-maker: \"*Booklet True\"\n", IPP_FINISHINGS_BOOKLET_MAKER);
4067 cupsFilePuts(fp, "*CloseUI: *Booklet\n");
c0886523
MS
4068 }
4069
a740a849 4070 cupsArrayDelete(names);
c0886523
MS
4071 }
4072
27ee2c4d
MS
4073 if ((attr = ippFindAttribute(response, "finishings-col-database", IPP_TAG_BEGIN_COLLECTION)) != NULL)
4074 {
4075 ipp_t *finishing_col; /* Current finishing collection */
4076 const char *template, /* Current finishing template */
4077 *loctemplate; /* Localized template name */
4078 cups_array_t *templates; /* Finishing templates */
4079
4080 cupsFilePrintf(fp, "*OpenUI *cupsFinishingTemplate/%s: PickOne\n", _cupsLangString(lang, _("Finishing Preset")));
4081 cupsFilePuts(fp, "*OrderDependency: 10 AnySetup *cupsFinishingTemplate\n");
4f63d6cd
MS
4082 cupsFilePuts(fp, "*DefaultcupsFinishingTemplate: none\n");
4083 cupsFilePrintf(fp, "*cupsFinishingTemplate none/%s: \"\"\n", _cupsLangString(lang, _("None")));
27ee2c4d
MS
4084
4085 templates = cupsArrayNew((cups_array_func_t)strcmp, NULL);
4086 count = ippGetCount(attr);
4087
4088 for (i = 0; i < count; i ++)
4089 {
4090 finishing_col = ippGetCollection(attr, i);
4091 template = ippGetString(ippFindAttribute(finishing_col, "finishing_template", IPP_TAG_ZERO), 0, NULL);
4092
4093 if (!template || cupsArrayFind(templates, (void *)template))
4094 continue;
4095
4096 if (strncmp(template, "fold-", 5) && (strstr(template, "-bottom") || strstr(template, "-left") || strstr(template, "-right") || strstr(template, "-top")))
4097 continue;
4098
4099 cupsArrayAdd(templates, (void *)template);
4100
4101 for (j = 0, loctemplate = NULL; j < (int)(sizeof(finishings) / sizeof(finishings[0])); j ++)
4102 {
4103 if (!strcmp(finishings[j][0], template))
4104 {
4105 loctemplate = _cupsLangString(lang, finishings[j][1]);
4106 break;
4107 }
4108 }
4109
4110 if (!loctemplate)
4111 {
4112 char msg[256]; /* Message key */
4113
4114 snprintf(msg, sizeof(msg), "finishing-template.%s", template);
4115 if ((loctemplate = _cupsMessageLookup(strings, msg)) == msg)
4116 loctemplate = template;
4117 }
4118
4119 cupsFilePrintf(fp, "*cupsFinishingTemplate %s/%s: \"\"\n", template, loctemplate);
4120 }
4121
4122 cupsFilePuts(fp, "*CloseUI: *cupsFinishingTemplate\n");
4123
4124 if (cupsArrayCount(fin_options))
4125 {
4126 const char *fin_option; /* Current finishing option */
4127
4128 cupsFilePuts(fp, "*cupsUIConstraint finisings: \"*cupsFinishingTemplate");
4129 for (fin_option = (const char *)cupsArrayFirst(fin_options); fin_option; fin_option = (const char *)cupsArrayNext(fin_options))
4130 cupsFilePrintf(fp, " %s", fin_option);
4131 cupsFilePuts(fp, "\"\n");
4132
4133 cupsFilePuts(fp, "*cupsUIResolver finisings: \"*cupsFinishingTemplate None");
4134 for (fin_option = (const char *)cupsArrayFirst(fin_options); fin_option; fin_option = (const char *)cupsArrayNext(fin_options))
4135 cupsFilePrintf(fp, " %s None", fin_option);
4136 cupsFilePuts(fp, "\"\n");
4137 }
4138
4139 cupsArrayDelete(templates);
4140 }
4141
4142 cupsArrayDelete(fin_options);
4143
d9fc71e4
MS
4144 /*
4145 * cupsPrintQuality and DefaultResolution...
4146 */
4147
3b6c3c8e
MS
4148 quality = ippFindAttribute(response, "print-quality-supported", IPP_TAG_ENUM);
4149
fa76bc3d 4150 if ((attr = ippFindAttribute(response, "urf-supported", IPP_TAG_KEYWORD)) != NULL)
d9fc71e4 4151 {
fa76bc3d 4152 int lowdpi = 0, hidpi = 0; /* Lower and higher resolution */
fb2d5470
MS
4153
4154 for (i = 0, count = ippGetCount(attr); i < count; i ++)
4155 {
4156 const char *rs = ippGetString(attr, i, NULL);
fa76bc3d 4157 /* RS value */
fb2d5470
MS
4158
4159 if (_cups_strncasecmp(rs, "RS", 2))
4160 continue;
4161
4162 lowdpi = atoi(rs + 2);
4163 if ((rs = strrchr(rs, '-')) != NULL)
4164 hidpi = atoi(rs + 1);
4165 else
4166 hidpi = lowdpi;
4167 break;
4168 }
4169
4170 if (lowdpi == 0)
4171 {
4172 /*
4173 * Invalid "urf-supported" value...
4174 */
4175
d4259b45 4176 goto bad_ppd;
fb2d5470
MS
4177 }
4178 else
4179 {
4180 /*
4181 * Generate print qualities based on low and high DPIs...
4182 */
4183
4184 cupsFilePrintf(fp, "*DefaultResolution: %ddpi\n", lowdpi);
4185
c0886523 4186 cupsFilePrintf(fp, "*OpenUI *cupsPrintQuality/%s: PickOne\n"
fa76bc3d
MS
4187 "*OrderDependency: 10 AnySetup *cupsPrintQuality\n"
4188 "*DefaultcupsPrintQuality: Normal\n", _cupsLangString(lang, _("Print Quality")));
fb2d5470 4189 if ((lowdpi & 1) == 0)
fa76bc3d 4190 cupsFilePrintf(fp, "*cupsPrintQuality Draft/%s: \"<</HWResolution[%d %d]>>setpagedevice\"\n", _cupsLangString(lang, _("Draft")), lowdpi, lowdpi / 2);
3b6c3c8e 4191 else if (ippContainsInteger(quality, IPP_QUALITY_DRAFT))
fa76bc3d 4192 cupsFilePrintf(fp, "*cupsPrintQuality Draft/%s: \"<</HWResolution[%d %d]>>setpagedevice\"\n", _cupsLangString(lang, _("Draft")), lowdpi, lowdpi);
c0886523 4193 cupsFilePrintf(fp, "*cupsPrintQuality Normal/%s: \"<</HWResolution[%d %d]>>setpagedevice\"\n", _cupsLangString(lang, _("Normal")), lowdpi, lowdpi);
3b6c3c8e 4194 if (hidpi > lowdpi || ippContainsInteger(quality, IPP_QUALITY_HIGH))
fa76bc3d 4195 cupsFilePrintf(fp, "*cupsPrintQuality High/%s: \"<</HWResolution[%d %d]>>setpagedevice\"\n", _cupsLangString(lang, _("High")), hidpi, hidpi);
fb2d5470
MS
4196 cupsFilePuts(fp, "*CloseUI: *cupsPrintQuality\n");
4197 }
4198 }
fa76bc3d
MS
4199 else if ((attr = ippFindAttribute(response, "pwg-raster-document-resolution-supported", IPP_TAG_RESOLUTION)) != NULL)
4200 {
4201 /*
4202 * Make a sorted list of resolutions.
4203 */
4204
4205 count = ippGetCount(attr);
4206 if (count > (int)(sizeof(resolutions) / sizeof(resolutions[0])))
4207 count = (int)(sizeof(resolutions) / sizeof(resolutions[0]));
4208
25140c63
MS
4209 resolutions[0] = 0; /* Not in loop to silence Clang static analyzer... */
4210 for (i = 1; i < count; i ++)
fa76bc3d
MS
4211 resolutions[i] = i;
4212
4213 for (i = 0; i < (count - 1); i ++)
4214 {
91d748b9 4215 for (j = i + 1; j < count; j ++)
fa76bc3d
MS
4216 {
4217 int ix, iy, /* First X and Y resolution */
4218 jx, jy, /* Second X and Y resolution */
4219 temp; /* Swap variable */
4220 ipp_res_t units; /* Resolution units */
4221
4222 ix = ippGetResolution(attr, resolutions[i], &iy, &units);
4223 jx = ippGetResolution(attr, resolutions[j], &jy, &units);
4224
4225 if (ix > jx || (ix == jx && iy > jy))
4226 {
4227 /*
4228 * Swap these two resolutions...
4229 */
4230
4231 temp = resolutions[i];
4232 resolutions[i] = resolutions[j];
4233 resolutions[j] = temp;
4234 }
4235 }
4236 }
4237
4238 /*
4239 * Generate print quality options...
4240 */
4241
4242 pwg_ppdize_resolution(attr, resolutions[count / 2], &xres, &yres, ppdname, sizeof(ppdname));
4243 cupsFilePrintf(fp, "*DefaultResolution: %s\n", ppdname);
4244
4245 cupsFilePrintf(fp, "*OpenUI *cupsPrintQuality/%s: PickOne\n"
4246 "*OrderDependency: 10 AnySetup *cupsPrintQuality\n"
4247 "*DefaultcupsPrintQuality: Normal\n", _cupsLangString(lang, _("Print Quality")));
4248 if (count > 2 || ippContainsInteger(quality, IPP_QUALITY_DRAFT))
4249 {
4250 pwg_ppdize_resolution(attr, resolutions[0], &xres, &yres, NULL, 0);
4251 cupsFilePrintf(fp, "*cupsPrintQuality Draft/%s: \"<</HWResolution[%d %d]>>setpagedevice\"\n", _cupsLangString(lang, _("Draft")), xres, yres);
4252 }
4253 pwg_ppdize_resolution(attr, resolutions[count / 2], &xres, &yres, NULL, 0);
4254 cupsFilePrintf(fp, "*cupsPrintQuality Normal/%s: \"<</HWResolution[%d %d]>>setpagedevice\"\n", _cupsLangString(lang, _("Normal")), xres, yres);
4255 if (count > 1 || ippContainsInteger(quality, IPP_QUALITY_HIGH))
4256 {
4257 pwg_ppdize_resolution(attr, resolutions[count - 1], &xres, &yres, NULL, 0);
4258 cupsFilePrintf(fp, "*cupsPrintQuality High/%s: \"<</HWResolution[%d %d]>>setpagedevice\"\n", _cupsLangString(lang, _("High")), xres, yres);
4259 }
4260
4261 cupsFilePuts(fp, "*CloseUI: *cupsPrintQuality\n");
4262 }
d4259b45
MS
4263 else if (is_apple || is_pwg)
4264 goto bad_ppd;
3b6c3c8e 4265 else
d9fc71e4 4266 {
3b6c3c8e
MS
4267 if ((attr = ippFindAttribute(response, "printer-resolution-default", IPP_TAG_RESOLUTION)) != NULL)
4268 {
4269 pwg_ppdize_resolution(attr, 0, &xres, &yres, ppdname, sizeof(ppdname));
4270 }
4271 else
4272 {
4273 xres = yres = 300;
4274 strlcpy(ppdname, "300dpi", sizeof(ppdname));
4275 }
4276
d9fc71e4 4277 cupsFilePrintf(fp, "*DefaultResolution: %s\n", ppdname);
3b6c3c8e
MS
4278
4279 cupsFilePrintf(fp, "*OpenUI *cupsPrintQuality/%s: PickOne\n"
4280 "*OrderDependency: 10 AnySetup *cupsPrintQuality\n"
4281 "*DefaultcupsPrintQuality: Normal\n", _cupsLangString(lang, _("Print Quality")));
4282 if (ippContainsInteger(quality, IPP_QUALITY_DRAFT))
4283 cupsFilePrintf(fp, "*cupsPrintQuality Draft/%s: \"<</HWResolution[%d %d]>>setpagedevice\"\n", _cupsLangString(lang, _("Draft")), xres, yres);
4284 cupsFilePrintf(fp, "*cupsPrintQuality Normal/%s: \"<</HWResolution[%d %d]>>setpagedevice\"\n", _cupsLangString(lang, _("Normal")), xres, yres);
4285 if (ippContainsInteger(quality, IPP_QUALITY_HIGH))
4286 cupsFilePrintf(fp, "*cupsPrintQuality High/%s: \"<</HWResolution[%d %d]>>setpagedevice\"\n", _cupsLangString(lang, _("High")), xres, yres);
4287 cupsFilePuts(fp, "*CloseUI: *cupsPrintQuality\n");
d9fc71e4 4288 }
d9fc71e4
MS
4289
4290 /*
4291 * Close up and return...
4292 */
4293
4294 cupsFileClose(fp);
4295
4296 return (buffer);
d4259b45
MS
4297
4298 /*
4299 * If we get here then there was a problem creating the PPD...
4300 */
4301
4302 bad_ppd:
4303
4304 cupsFileClose(fp);
4305 unlink(buffer);
4306 *buffer = '\0';
4307
a946858f
MS
4308 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Printer does not support required IPP attributes or document formats."), 1);
4309
d4259b45 4310 return (NULL);
d9fc71e4
MS
4311}
4312
4313
f14324a7
MS
4314/*
4315 * '_pwgInputSlotForSource()' - Get the InputSlot name for the given PWG
4316 * media-source.
4317 */
4318
4319const char * /* O - InputSlot name */
4320_pwgInputSlotForSource(
4321 const char *media_source, /* I - PWG media-source */
4322 char *name, /* I - Name buffer */
4323 size_t namesize) /* I - Size of name buffer */
4324{
4325 /*
4326 * Range check input...
4327 */
4328
4329 if (!media_source || !name || namesize < PPD_MAX_NAME)
4330 return (NULL);
4331
88f9aafc 4332 if (_cups_strcasecmp(media_source, "main"))
f14324a7 4333 strlcpy(name, "Cassette", namesize);
88f9aafc 4334 else if (_cups_strcasecmp(media_source, "alternate"))
f14324a7 4335 strlcpy(name, "Multipurpose", namesize);
88f9aafc 4336 else if (_cups_strcasecmp(media_source, "large-capacity"))
f14324a7 4337 strlcpy(name, "LargeCapacity", namesize);
88f9aafc 4338 else if (_cups_strcasecmp(media_source, "bottom"))
f14324a7 4339 strlcpy(name, "Lower", namesize);
88f9aafc 4340 else if (_cups_strcasecmp(media_source, "middle"))
f14324a7 4341 strlcpy(name, "Middle", namesize);
88f9aafc 4342 else if (_cups_strcasecmp(media_source, "top"))
f14324a7 4343 strlcpy(name, "Upper", namesize);
88f9aafc 4344 else if (_cups_strcasecmp(media_source, "rear"))
f14324a7 4345 strlcpy(name, "Rear", namesize);
88f9aafc 4346 else if (_cups_strcasecmp(media_source, "side"))
f14324a7 4347 strlcpy(name, "Side", namesize);
88f9aafc 4348 else if (_cups_strcasecmp(media_source, "envelope"))
f14324a7 4349 strlcpy(name, "Envelope", namesize);
88f9aafc 4350 else if (_cups_strcasecmp(media_source, "main-roll"))
f14324a7 4351 strlcpy(name, "Roll", namesize);
88f9aafc 4352 else if (_cups_strcasecmp(media_source, "alternate-roll"))
f14324a7
MS
4353 strlcpy(name, "Roll2", namesize);
4354 else
4355 pwg_ppdize_name(media_source, name, namesize);
4356
4357 return (name);
4358}
4359
4360
4361/*
4362 * '_pwgMediaTypeForType()' - Get the MediaType name for the given PWG
4363 * media-type.
4364 */
4365
4366const char * /* O - MediaType name */
4367_pwgMediaTypeForType(
4368 const char *media_type, /* I - PWG media-type */
4369 char *name, /* I - Name buffer */
4370 size_t namesize) /* I - Size of name buffer */
4371{
4372 /*
4373 * Range check input...
4374 */
4375
4376 if (!media_type || !name || namesize < PPD_MAX_NAME)
4377 return (NULL);
4378
88f9aafc 4379 if (_cups_strcasecmp(media_type, "auto"))
f14324a7 4380 strlcpy(name, "Auto", namesize);
88f9aafc 4381 else if (_cups_strcasecmp(media_type, "cardstock"))
f14324a7 4382 strlcpy(name, "Cardstock", namesize);
88f9aafc 4383 else if (_cups_strcasecmp(media_type, "envelope"))
f14324a7 4384 strlcpy(name, "Envelope", namesize);
88f9aafc 4385 else if (_cups_strcasecmp(media_type, "photographic-glossy"))
f14324a7 4386 strlcpy(name, "Glossy", namesize);
88f9aafc 4387 else if (_cups_strcasecmp(media_type, "photographic-high-gloss"))
f14324a7 4388 strlcpy(name, "HighGloss", namesize);
88f9aafc 4389 else if (_cups_strcasecmp(media_type, "photographic-matte"))
f14324a7 4390 strlcpy(name, "Matte", namesize);
88f9aafc 4391 else if (_cups_strcasecmp(media_type, "stationery"))
f14324a7 4392 strlcpy(name, "Plain", namesize);
88f9aafc 4393 else if (_cups_strcasecmp(media_type, "stationery-coated"))
f14324a7 4394 strlcpy(name, "Coated", namesize);
88f9aafc 4395 else if (_cups_strcasecmp(media_type, "stationery-inkjet"))
f14324a7 4396 strlcpy(name, "Inkjet", namesize);
88f9aafc 4397 else if (_cups_strcasecmp(media_type, "stationery-letterhead"))
f14324a7 4398 strlcpy(name, "Letterhead", namesize);
88f9aafc 4399 else if (_cups_strcasecmp(media_type, "stationery-preprinted"))
f14324a7 4400 strlcpy(name, "Preprinted", namesize);
88f9aafc 4401 else if (_cups_strcasecmp(media_type, "transparency"))
f14324a7
MS
4402 strlcpy(name, "Transparency", namesize);
4403 else
4404 pwg_ppdize_name(media_type, name, namesize);
4405
4406 return (name);
4407}
4408
4409
4410/*
4411 * '_pwgPageSizeForMedia()' - Get the PageSize name for the given media.
4412 */
4413
4414const char * /* O - PageSize name */
4415_pwgPageSizeForMedia(
6961465f 4416 pwg_media_t *media, /* I - Media */
f14324a7
MS
4417 char *name, /* I - PageSize name buffer */
4418 size_t namesize) /* I - Size of name buffer */
4419{
4420 const char *sizeptr, /* Pointer to size in PWG name */
4421 *dimptr; /* Pointer to dimensions in PWG name */
4422
4423
4424 /*
4425 * Range check input...
4426 */
4427
4428 if (!media || !name || namesize < PPD_MAX_NAME)
4429 return (NULL);
4430
4431 /*
4432 * Copy or generate a PageSize name...
4433 */
4434
4435 if (media->ppd)
4436 {
4437 /*
4438 * Use a standard Adobe name...
4439 */
4440
4441 strlcpy(name, media->ppd, namesize);
4442 }
4443 else if (!media->pwg || !strncmp(media->pwg, "custom_", 7) ||
4444 (sizeptr = strchr(media->pwg, '_')) == NULL ||
4445 (dimptr = strchr(sizeptr + 1, '_')) == NULL ||
4446 (size_t)(dimptr - sizeptr) > namesize)
4447 {
4448 /*
4449 * Use a name of the form "wNNNhNNN"...
4450 */
4451
6961465f
MS
4452 snprintf(name, namesize, "w%dh%d", (int)PWG_TO_POINTS(media->width),
4453 (int)PWG_TO_POINTS(media->length));
f14324a7
MS
4454 }
4455 else
4456 {
4457 /*
4458 * Copy the size name from class_sizename_dimensions...
4459 */
4460
07623986 4461 memcpy(name, sizeptr + 1, (size_t)(dimptr - sizeptr - 1));
f14324a7
MS
4462 name[dimptr - sizeptr - 1] = '\0';
4463 }
4464
4465 return (name);
4466}
4467
4468
89550f3f
MS
4469/*
4470 * 'cups_get_url()' - Get a copy of the file at the given URL.
4471 */
4472
4473static int /* O - 1 on success, 0 on failure */
4474cups_get_url(http_t **http, /* IO - Current HTTP connection */
4475 const char *url, /* I - URL to get */
4476 char *name, /* I - Temporary filename */
4477 size_t namesize) /* I - Size of temporary filename buffer */
4478{
4479 char scheme[32], /* URL scheme */
4480 userpass[256], /* URL username:password */
4481 host[256], /* URL host */
4482 curhost[256], /* Current host */
4483 resource[256]; /* URL resource */
4484 int port; /* URL port */
4485 http_encryption_t encryption; /* Type of encryption to use */
4486 http_status_t status; /* Status of GET request */
4487 int fd; /* Temporary file */
4488
4489
4490 if (httpSeparateURI(HTTP_URI_CODING_ALL, url, scheme, sizeof(scheme), userpass, sizeof(userpass), host, sizeof(host), &port, resource, sizeof(resource)) < HTTP_URI_STATUS_OK)
4491 return (0);
4492
4493 if (port == 443 || !strcmp(scheme, "https"))
4494 encryption = HTTP_ENCRYPTION_ALWAYS;
4495 else
4496 encryption = HTTP_ENCRYPTION_IF_REQUESTED;
4497
4498 if (!*http || strcasecmp(host, httpGetHostname(*http, curhost, sizeof(curhost))) || httpAddrPort(httpGetAddress(*http)) != port)
4499 {
4500 httpClose(*http);
4501 *http = httpConnect2(host, port, NULL, AF_UNSPEC, encryption, 1, 5000, NULL);
4502 }
4503
4504 if (!*http)
4505 return (0);
4506
4507 if ((fd = cupsTempFd(name, (int)namesize)) < 0)
4508 return (0);
4509
4510 status = cupsGetFd(*http, resource, fd);
4511
4512 close(fd);
4513
4514 if (status != HTTP_STATUS_OK)
4515 {
4516 unlink(name);
4517 *name = '\0';
4518 return (0);
4519 }
4520
4521 return (1);
4522}
4523
4524
1fbd0cab
MS
4525/*
4526 * 'pwg_add_finishing()' - Add a finishings value.
4527 */
4528
4529static void
4530pwg_add_finishing(
4531 cups_array_t *finishings, /* I - Finishings array */
4532 ipp_finishings_t template, /* I - Finishing template */
4533 const char *name, /* I - PPD option */
4534 const char *value) /* I - PPD choice */
4535{
4536 _pwg_finishings_t *f; /* New finishings value */
4537
4538
4539 if ((f = (_pwg_finishings_t *)calloc(1, sizeof(_pwg_finishings_t))) != NULL)
4540 {
4541 f->value = template;
4542 f->num_options = cupsAddOption(name, value, 0, &f->options);
4543
4544 cupsArrayAdd(finishings, f);
4545 }
4546}
4547
4548
dcb445bc
MS
4549/*
4550 * 'pwg_compare_finishings()' - Compare two finishings values.
4551 */
4552
1fbd0cab 4553static int /* O - Result of comparison */
dcb445bc
MS
4554pwg_compare_finishings(
4555 _pwg_finishings_t *a, /* I - First finishings value */
4556 _pwg_finishings_t *b) /* I - Second finishings value */
4557{
7d5824d6 4558 return ((int)b->value - (int)a->value);
dcb445bc
MS
4559}
4560
4561
4562/*
4563 * 'pwg_free_finishings()' - Free a finishings value.
4564 */
4565
4566static void
4567pwg_free_finishings(
4568 _pwg_finishings_t *f) /* I - Finishings value */
4569{
4570 cupsFreeOptions(f->num_options, f->options);
4571 free(f);
4572}
4573
4574
f14324a7
MS
4575/*
4576 * 'pwg_ppdize_name()' - Convert an IPP keyword to a PPD keyword.
4577 */
4578
4579static void
4580pwg_ppdize_name(const char *ipp, /* I - IPP keyword */
4581 char *name, /* I - Name buffer */
4582 size_t namesize) /* I - Size of name buffer */
4583{
4584 char *ptr, /* Pointer into name buffer */
4585 *end; /* End of name buffer */
4586
4587
670172ea
MS
4588 if (!ipp)
4589 {
4590 *name = '\0';
4591 return;
4592 }
4593
7e86f2f6 4594 *name = (char)toupper(*ipp++);
f14324a7
MS
4595
4596 for (ptr = name + 1, end = name + namesize - 1; *ipp && ptr < end;)
4597 {
2a8afc20 4598 if (*ipp == '-' && _cups_isalnum(ipp[1]))
f14324a7
MS
4599 {
4600 ipp ++;
7e86f2f6 4601 *ptr++ = (char)toupper(*ipp++ & 255);
f14324a7
MS
4602 }
4603 else
4604 *ptr++ = *ipp++;
4605 }
4606
4607 *ptr = '\0';
4608}
4609
4610
d9fc71e4
MS
4611/*
4612 * 'pwg_ppdize_resolution()' - Convert PWG resolution values to PPD values.
4613 */
4614
4615static void
4616pwg_ppdize_resolution(
4617 ipp_attribute_t *attr, /* I - Attribute to convert */
4618 int element, /* I - Element to convert */
4619 int *xres, /* O - X resolution in DPI */
4620 int *yres, /* O - Y resolution in DPI */
4621 char *name, /* I - Name buffer */
4622 size_t namesize) /* I - Size of name buffer */
4623{
4624 ipp_res_t units; /* Units for resolution */
4625
4626
4627 *xres = ippGetResolution(attr, element, yres, &units);
4628
4629 if (units == IPP_RES_PER_CM)
4630 {
4631 *xres = (int)(*xres * 2.54);
4632 *yres = (int)(*yres * 2.54);
4633 }
4634
4635 if (name && namesize > 4)
4636 {
4637 if (*xres == *yres)
4638 snprintf(name, namesize, "%ddpi", *xres);
4639 else
4640 snprintf(name, namesize, "%dx%ddpi", *xres, *yres);
4641 }
4642}
4643
4644
f14324a7
MS
4645/*
4646 * 'pwg_unppdize_name()' - Convert a PPD keyword to a lowercase IPP keyword.
4647 */
4648
4649static void
4650pwg_unppdize_name(const char *ppd, /* I - PPD keyword */
4651 char *name, /* I - Name buffer */
c1420c87
MS
4652 size_t namesize, /* I - Size of name buffer */
4653 const char *dashchars)/* I - Characters to be replaced by dashes */
f14324a7
MS
4654{
4655 char *ptr, /* Pointer into name buffer */
4656 *end; /* End of name buffer */
4657
4658
79d7d84a
MS
4659 if (_cups_islower(*ppd))
4660 {
4661 /*
4662 * Already lowercase name, use as-is?
4663 */
4664
4665 const char *ppdptr; /* Pointer into PPD keyword */
4666
4667 for (ppdptr = ppd + 1; *ppdptr; ppdptr ++)
4668 if (_cups_isupper(*ppdptr) || strchr(dashchars, *ppdptr))
4669 break;
4670
4671 if (!*ppdptr)
4672 {
4673 strlcpy(name, ppd, namesize);
4674 return;
4675 }
4676 }
4677
f14324a7
MS
4678 for (ptr = name, end = name + namesize - 1; *ppd && ptr < end; ppd ++)
4679 {
4680 if (_cups_isalnum(*ppd) || *ppd == '-')
7e86f2f6 4681 *ptr++ = (char)tolower(*ppd & 255);
c1420c87 4682 else if (strchr(dashchars, *ppd))
f14324a7 4683 *ptr++ = '-';
c1420c87
MS
4684 else
4685 *ptr++ = *ppd;
f14324a7
MS
4686
4687 if (!_cups_isupper(*ppd) && _cups_isalnum(*ppd) &&
4688 _cups_isupper(ppd[1]) && ptr < end)
4689 *ptr++ = '-';
d489df6a
MS
4690 else if (!isdigit(*ppd & 255) && isdigit(ppd[1] & 255))
4691 *ptr++ = '-';
f14324a7
MS
4692 }
4693
4694 *ptr = '\0';
4695}