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