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