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