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