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