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