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