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