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