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