]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/ppd-cache.c
Merge changes from CUPS 1.6svn-r10188, including changes for <rdar://problem/10127258...
[thirdparty/cups.git] / cups / ppd-cache.c
1 /*
2 * "$Id$"
3 *
4 * PPD cache implementation for CUPS.
5 *
6 * Copyright 2010-2011 by Apple Inc.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Apple Inc. and are protected by Federal copyright
10 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
11 * which should have been included with this file. If this file is
12 * file is missing or damaged, see the license at "http://www.cups.org/".
13 *
14 * This file is subject to the Apple OS-Developed Software exception.
15 *
16 * Contents:
17 *
18 * _ppdCacheCreateWithFile() - Create PPD cache and mapping data from a
19 * written file.
20 * _ppdCacheCreateWithPPD() - Create PWG mapping data from a PPD file.
21 * _ppdCacheDestroy() - Free all memory used for PWG mapping data.
22 * _ppdCacheGetBin() - Get the PWG output-bin keyword associated with
23 * a PPD OutputBin.
24 * _ppdCacheGetInputSlot() - Get the PPD InputSlot associated with the job
25 * attributes or a keyword string.
26 * _ppdCacheGetMediaType() - Get the PPD MediaType associated with the job
27 * attributes or a keyword string.
28 * _ppdCacheGetOutputBin() - Get the PPD OutputBin associated with the
29 * keyword string.
30 * _ppdCacheGetPageSize() - Get the PPD PageSize associated with the job
31 * attributes or a keyword string.
32 * _ppdCacheGetSize() - Get the PWG size associated with a PPD
33 * PageSize.
34 * _ppdCacheGetSource() - Get the PWG media-source associated with a PPD
35 * InputSlot.
36 * _ppdCacheGetType() - Get the PWG media-type associated with a PPD
37 * MediaType.
38 * _ppdCacheWriteFile() - Write PWG mapping data to a file.
39 * _pwgInputSlotForSource() - Get the InputSlot name for the given PWG
40 * media-source.
41 * _pwgMediaTypeForType() - Get the MediaType name for the given PWG
42 * media-type.
43 * _pwgPageSizeForMedia() - Get the PageSize name for the given media.
44 * pwg_ppdize_name() - Convert an IPP keyword to a PPD keyword.
45 * pwg_unppdize_name() - Convert a PPD keyword to a lowercase IPP
46 * keyword.
47 */
48
49 /*
50 * Include necessary headers...
51 */
52
53 #include "cups-private.h"
54 #include <math.h>
55
56
57 /*
58 * Macro to test for two almost-equal PWG measurements.
59 */
60
61 #define _PWG_EQUIVALENT(x, y) (abs((x)-(y)) < 2)
62
63
64 /*
65 * Local functions...
66 */
67
68 static int pwg_compare_finishings(_pwg_finishings_t *a,
69 _pwg_finishings_t *b);
70 static void pwg_free_finishings(_pwg_finishings_t *f);
71 static void pwg_ppdize_name(const char *ipp, char *name, size_t namesize);
72 static void pwg_unppdize_name(const char *ppd, char *name, size_t namesize);
73
74
75 /*
76 * '_ppdCacheCreateWithFile()' - Create PPD cache and mapping data from a
77 * written file.
78 *
79 * Use the @link _ppdCacheWriteFile@ function to write PWG mapping data to a
80 * file.
81 */
82
83 _ppd_cache_t * /* O - PPD cache and mapping data */
84 _ppdCacheCreateWithFile(
85 const char *filename, /* I - File to read */
86 ipp_t **attrs) /* IO - IPP attributes, if any */
87 {
88 cups_file_t *fp; /* File */
89 _ppd_cache_t *pc; /* PWG mapping data */
90 _pwg_size_t *size; /* Current size */
91 _pwg_map_t *map; /* Current map */
92 _pwg_finishings_t *finishings; /* Current finishings option */
93 int linenum, /* Current line number */
94 num_bins, /* Number of bins in file */
95 num_sizes, /* Number of sizes in file */
96 num_sources, /* Number of sources in file */
97 num_types; /* Number of types in file */
98 char line[2048], /* Current line */
99 *value, /* Pointer to value in line */
100 *valueptr, /* Pointer into value */
101 pwg_keyword[128], /* PWG keyword */
102 ppd_keyword[PPD_MAX_NAME];
103 /* PPD keyword */
104 _pwg_print_color_mode_t print_color_mode;
105 /* Print color mode for preset */
106 _pwg_print_quality_t print_quality; /* Print quality for preset */
107
108
109 DEBUG_printf(("_ppdCacheCreateWithFile(filename=\"%s\")", filename));
110
111 /*
112 * Range check input...
113 */
114
115 if (attrs)
116 *attrs = NULL;
117
118 if (!filename)
119 {
120 _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
121 return (NULL);
122 }
123
124 /*
125 * Open the file...
126 */
127
128 if ((fp = cupsFileOpen(filename, "r")) == NULL)
129 {
130 _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0);
131 return (NULL);
132 }
133
134 /*
135 * Read the first line and make sure it has "#CUPS-PPD-CACHE-version" in it...
136 */
137
138 if (!cupsFileGets(fp, line, sizeof(line)))
139 {
140 _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0);
141 DEBUG_puts("_ppdCacheCreateWithFile: Unable to read first line.");
142 cupsFileClose(fp);
143 return (NULL);
144 }
145
146 if (strncmp(line, "#CUPS-PPD-CACHE-", 16))
147 {
148 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
149 DEBUG_printf(("_ppdCacheCreateWithFile: Wrong first line \"%s\".", line));
150 cupsFileClose(fp);
151 return (NULL);
152 }
153
154 if (atoi(line + 16) != _PPD_CACHE_VERSION)
155 {
156 _cupsSetError(IPP_INTERNAL_ERROR, _("Out of date PPD cache file."), 1);
157 DEBUG_printf(("_ppdCacheCreateWithFile: Cache file has version %s, "
158 "expected %d.", line + 16, _PPD_CACHE_VERSION));
159 cupsFileClose(fp);
160 return (NULL);
161 }
162
163 /*
164 * Allocate the mapping data structure...
165 */
166
167 if ((pc = calloc(1, sizeof(_ppd_cache_t))) == NULL)
168 {
169 _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0);
170 DEBUG_puts("_ppdCacheCreateWithFile: Unable to allocate _ppd_cache_t.");
171 goto create_error;
172 }
173
174 /*
175 * Read the file...
176 */
177
178 linenum = 0;
179 num_bins = 0;
180 num_sizes = 0;
181 num_sources = 0;
182 num_types = 0;
183
184 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
185 {
186 DEBUG_printf(("_ppdCacheCreateWithFile: line=\"%s\", value=\"%s\", "
187 "linenum=%d", line, value, linenum));
188
189 if (!value)
190 {
191 DEBUG_printf(("_ppdCacheCreateWithFile: Missing value on line %d.",
192 linenum));
193 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
194 goto create_error;
195 }
196 else if (!_cups_strcasecmp(line, "Filter"))
197 {
198 if (!pc->filters)
199 pc->filters = cupsArrayNew3(NULL, NULL, NULL, 0,
200 (cups_acopy_func_t)_cupsStrAlloc,
201 (cups_afree_func_t)_cupsStrFree);
202
203 cupsArrayAdd(pc->filters, value);
204 }
205 else if (!_cups_strcasecmp(line, "PreFilter"))
206 {
207 if (!pc->prefilters)
208 pc->prefilters = cupsArrayNew3(NULL, NULL, NULL, 0,
209 (cups_acopy_func_t)_cupsStrAlloc,
210 (cups_afree_func_t)_cupsStrFree);
211
212 cupsArrayAdd(pc->prefilters, value);
213 }
214 else if (!_cups_strcasecmp(line, "Product"))
215 {
216 pc->product = _cupsStrAlloc(value);
217 }
218 else if (!_cups_strcasecmp(line, "SingleFile"))
219 {
220 pc->single_file = !_cups_strcasecmp(value, "true");
221 }
222 else if (!_cups_strcasecmp(line, "IPP"))
223 {
224 off_t pos = cupsFileTell(fp), /* Position in file */
225 length = strtol(value, NULL, 10);
226 /* Length of IPP attributes */
227
228 if (attrs && *attrs)
229 {
230 DEBUG_puts("_ppdCacheCreateWithFile: IPP listed multiple times.");
231 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
232 goto create_error;
233 }
234 else if (length <= 0)
235 {
236 DEBUG_puts("_ppdCacheCreateWithFile: Bad IPP length.");
237 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
238 goto create_error;
239 }
240
241 if (attrs)
242 {
243 /*
244 * Read IPP attributes into the provided variable...
245 */
246
247 *attrs = ippNew();
248
249 if (ippReadIO(fp, (ipp_iocb_t)cupsFileRead, 1, NULL,
250 *attrs) != IPP_DATA)
251 {
252 DEBUG_puts("_ppdCacheCreateWithFile: Bad IPP data.");
253 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
254 goto create_error;
255 }
256 }
257 else
258 {
259 /*
260 * Skip the IPP data entirely...
261 */
262
263 cupsFileSeek(fp, pos + length);
264 }
265
266 if (cupsFileTell(fp) != (pos + length))
267 {
268 DEBUG_puts("_ppdCacheCreateWithFile: Bad IPP data.");
269 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
270 goto create_error;
271 }
272 }
273 else if (!_cups_strcasecmp(line, "NumBins"))
274 {
275 if (num_bins > 0)
276 {
277 DEBUG_puts("_ppdCacheCreateWithFile: NumBins listed multiple times.");
278 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
279 goto create_error;
280 }
281
282 if ((num_bins = atoi(value)) <= 0 || num_bins > 65536)
283 {
284 DEBUG_printf(("_ppdCacheCreateWithFile: Bad NumBins value %d on line "
285 "%d.", num_sizes, linenum));
286 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
287 goto create_error;
288 }
289
290 if ((pc->bins = calloc(num_bins, sizeof(_pwg_map_t))) == NULL)
291 {
292 DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d bins.",
293 num_sizes));
294 _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0);
295 goto create_error;
296 }
297 }
298 else if (!_cups_strcasecmp(line, "Bin"))
299 {
300 if (sscanf(value, "%127s%40s", pwg_keyword, ppd_keyword) != 2)
301 {
302 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Bin on line %d.", linenum));
303 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
304 goto create_error;
305 }
306
307 if (pc->num_bins >= num_bins)
308 {
309 DEBUG_printf(("_ppdCacheCreateWithFile: Too many Bin's on line %d.",
310 linenum));
311 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
312 goto create_error;
313 }
314
315 map = pc->bins + pc->num_bins;
316 map->pwg = _cupsStrAlloc(pwg_keyword);
317 map->ppd = _cupsStrAlloc(ppd_keyword);
318
319 pc->num_bins ++;
320 }
321 else if (!_cups_strcasecmp(line, "NumSizes"))
322 {
323 if (num_sizes > 0)
324 {
325 DEBUG_puts("_ppdCacheCreateWithFile: NumSizes listed multiple times.");
326 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
327 goto create_error;
328 }
329
330 if ((num_sizes = atoi(value)) <= 0 || num_sizes > 65536)
331 {
332 DEBUG_printf(("_ppdCacheCreateWithFile: Bad NumSizes value %d on line "
333 "%d.", num_sizes, linenum));
334 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
335 goto create_error;
336 }
337
338 if ((pc->sizes = calloc(num_sizes, sizeof(_pwg_size_t))) == NULL)
339 {
340 DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d sizes.",
341 num_sizes));
342 _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0);
343 goto create_error;
344 }
345 }
346 else if (!_cups_strcasecmp(line, "Size"))
347 {
348 if (pc->num_sizes >= num_sizes)
349 {
350 DEBUG_printf(("_ppdCacheCreateWithFile: Too many Size's on line %d.",
351 linenum));
352 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
353 goto create_error;
354 }
355
356 size = pc->sizes + pc->num_sizes;
357
358 if (sscanf(value, "%127s%40s%d%d%d%d%d%d", pwg_keyword, ppd_keyword,
359 &(size->width), &(size->length), &(size->left),
360 &(size->bottom), &(size->right), &(size->top)) != 8)
361 {
362 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Size on line %d.",
363 linenum));
364 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
365 goto create_error;
366 }
367
368 size->map.pwg = _cupsStrAlloc(pwg_keyword);
369 size->map.ppd = _cupsStrAlloc(ppd_keyword);
370
371 pc->num_sizes ++;
372 }
373 else if (!_cups_strcasecmp(line, "CustomSize"))
374 {
375 if (pc->custom_max_width > 0)
376 {
377 DEBUG_printf(("_ppdCacheCreateWithFile: Too many CustomSize's on line "
378 "%d.", linenum));
379 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
380 goto create_error;
381 }
382
383 if (sscanf(value, "%d%d%d%d%d%d%d%d", &(pc->custom_max_width),
384 &(pc->custom_max_length), &(pc->custom_min_width),
385 &(pc->custom_min_length), &(pc->custom_size.left),
386 &(pc->custom_size.bottom), &(pc->custom_size.right),
387 &(pc->custom_size.top)) != 8)
388 {
389 DEBUG_printf(("_ppdCacheCreateWithFile: Bad CustomSize on line %d.",
390 linenum));
391 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
392 goto create_error;
393 }
394
395 _pwgGenerateSize(pwg_keyword, sizeof(pwg_keyword), "custom", "max",
396 pc->custom_max_width, pc->custom_max_length);
397 pc->custom_max_keyword = _cupsStrAlloc(pwg_keyword);
398
399 _pwgGenerateSize(pwg_keyword, sizeof(pwg_keyword), "custom", "min",
400 pc->custom_min_width, pc->custom_min_length);
401 pc->custom_min_keyword = _cupsStrAlloc(pwg_keyword);
402 }
403 else if (!_cups_strcasecmp(line, "SourceOption"))
404 {
405 pc->source_option = _cupsStrAlloc(value);
406 }
407 else if (!_cups_strcasecmp(line, "NumSources"))
408 {
409 if (num_sources > 0)
410 {
411 DEBUG_puts("_ppdCacheCreateWithFile: NumSources listed multiple "
412 "times.");
413 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
414 goto create_error;
415 }
416
417 if ((num_sources = atoi(value)) <= 0 || num_sources > 65536)
418 {
419 DEBUG_printf(("_ppdCacheCreateWithFile: Bad NumSources value %d on "
420 "line %d.", num_sources, linenum));
421 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
422 goto create_error;
423 }
424
425 if ((pc->sources = calloc(num_sources, sizeof(_pwg_map_t))) == NULL)
426 {
427 DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d sources.",
428 num_sources));
429 _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0);
430 goto create_error;
431 }
432 }
433 else if (!_cups_strcasecmp(line, "Source"))
434 {
435 if (sscanf(value, "%127s%40s", pwg_keyword, ppd_keyword) != 2)
436 {
437 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Source on line %d.",
438 linenum));
439 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
440 goto create_error;
441 }
442
443 if (pc->num_sources >= num_sources)
444 {
445 DEBUG_printf(("_ppdCacheCreateWithFile: Too many Source's on line %d.",
446 linenum));
447 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
448 goto create_error;
449 }
450
451 map = pc->sources + pc->num_sources;
452 map->pwg = _cupsStrAlloc(pwg_keyword);
453 map->ppd = _cupsStrAlloc(ppd_keyword);
454
455 pc->num_sources ++;
456 }
457 else if (!_cups_strcasecmp(line, "NumTypes"))
458 {
459 if (num_types > 0)
460 {
461 DEBUG_puts("_ppdCacheCreateWithFile: NumTypes listed multiple times.");
462 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
463 goto create_error;
464 }
465
466 if ((num_types = atoi(value)) <= 0 || num_types > 65536)
467 {
468 DEBUG_printf(("_ppdCacheCreateWithFile: Bad NumTypes value %d on "
469 "line %d.", num_types, linenum));
470 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
471 goto create_error;
472 }
473
474 if ((pc->types = calloc(num_types, sizeof(_pwg_map_t))) == NULL)
475 {
476 DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d types.",
477 num_types));
478 _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0);
479 goto create_error;
480 }
481 }
482 else if (!_cups_strcasecmp(line, "Type"))
483 {
484 if (sscanf(value, "%127s%40s", pwg_keyword, ppd_keyword) != 2)
485 {
486 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Type on line %d.",
487 linenum));
488 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
489 goto create_error;
490 }
491
492 if (pc->num_types >= num_types)
493 {
494 DEBUG_printf(("_ppdCacheCreateWithFile: Too many Type's on line %d.",
495 linenum));
496 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
497 goto create_error;
498 }
499
500 map = pc->types + pc->num_types;
501 map->pwg = _cupsStrAlloc(pwg_keyword);
502 map->ppd = _cupsStrAlloc(ppd_keyword);
503
504 pc->num_types ++;
505 }
506 else if (!_cups_strcasecmp(line, "Preset"))
507 {
508 /*
509 * Preset output-mode print-quality name=value ...
510 */
511
512 print_color_mode = (_pwg_print_color_mode_t)strtol(value, &valueptr, 10);
513 print_quality = (_pwg_print_quality_t)strtol(valueptr, &valueptr, 10);
514
515 if (print_color_mode < _PWG_PRINT_COLOR_MODE_MONOCHROME ||
516 print_color_mode >= _PWG_PRINT_COLOR_MODE_MAX ||
517 print_quality < _PWG_PRINT_QUALITY_DRAFT ||
518 print_quality >= _PWG_PRINT_QUALITY_MAX ||
519 valueptr == value || !*valueptr)
520 {
521 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Preset on line %d.",
522 linenum));
523 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
524 goto create_error;
525 }
526
527 pc->num_presets[print_color_mode][print_quality] =
528 cupsParseOptions(valueptr, 0,
529 pc->presets[print_color_mode] + print_quality);
530 }
531 else if (!_cups_strcasecmp(line, "SidesOption"))
532 pc->sides_option = _cupsStrAlloc(value);
533 else if (!_cups_strcasecmp(line, "Sides1Sided"))
534 pc->sides_1sided = _cupsStrAlloc(value);
535 else if (!_cups_strcasecmp(line, "Sides2SidedLong"))
536 pc->sides_2sided_long = _cupsStrAlloc(value);
537 else if (!_cups_strcasecmp(line, "Sides2SidedShort"))
538 pc->sides_2sided_short = _cupsStrAlloc(value);
539 else if (!_cups_strcasecmp(line, "Finishings"))
540 {
541 if (!pc->finishings)
542 pc->finishings =
543 cupsArrayNew3((cups_array_func_t)pwg_compare_finishings,
544 NULL, NULL, 0, NULL,
545 (cups_afree_func_t)pwg_free_finishings);
546
547 if ((finishings = calloc(1, sizeof(_pwg_finishings_t))) == NULL)
548 goto create_error;
549
550 finishings->value = strtol(value, &valueptr, 10);
551 finishings->num_options = cupsParseOptions(valueptr, 0,
552 &(finishings->options));
553
554 cupsArrayAdd(pc->finishings, finishings);
555 }
556 else
557 {
558 DEBUG_printf(("_ppdCacheCreateWithFile: Unknown %s on line %d.", line,
559 linenum));
560 }
561 }
562
563 if (pc->num_sizes < num_sizes)
564 {
565 DEBUG_printf(("_ppdCacheCreateWithFile: Not enough sizes (%d < %d).",
566 pc->num_sizes, num_sizes));
567 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
568 goto create_error;
569 }
570
571 if (pc->num_sources < num_sources)
572 {
573 DEBUG_printf(("_ppdCacheCreateWithFile: Not enough sources (%d < %d).",
574 pc->num_sources, num_sources));
575 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
576 goto create_error;
577 }
578
579 if (pc->num_types < num_types)
580 {
581 DEBUG_printf(("_ppdCacheCreateWithFile: Not enough types (%d < %d).",
582 pc->num_types, num_types));
583 _cupsSetError(IPP_INTERNAL_ERROR, _("Bad PPD cache file."), 1);
584 goto create_error;
585 }
586
587 cupsFileClose(fp);
588
589 return (pc);
590
591 /*
592 * If we get here the file was bad - free any data and return...
593 */
594
595 create_error:
596
597 cupsFileClose(fp);
598 _ppdCacheDestroy(pc);
599
600 if (attrs)
601 {
602 ippDelete(*attrs);
603 *attrs = NULL;
604 }
605
606 return (NULL);
607 }
608
609
610 /*
611 * '_ppdCacheCreateWithPPD()' - Create PWG mapping data from a PPD file.
612 */
613
614 _ppd_cache_t * /* O - PPD cache and mapping data */
615 _ppdCacheCreateWithPPD(ppd_file_t *ppd) /* I - PPD file */
616 {
617 int i, j, k; /* Looping vars */
618 _ppd_cache_t *pc; /* PWG mapping data */
619 ppd_option_t *input_slot, /* InputSlot option */
620 *media_type, /* MediaType option */
621 *output_bin, /* OutputBin option */
622 *color_model, /* ColorModel option */
623 *duplex; /* Duplex option */
624 ppd_choice_t *choice; /* Current InputSlot/MediaType */
625 _pwg_map_t *map; /* Current source/type map */
626 ppd_attr_t *ppd_attr; /* Current PPD preset attribute */
627 int num_options; /* Number of preset options and props */
628 cups_option_t *options; /* Preset options and properties */
629 ppd_size_t *ppd_size; /* Current PPD size */
630 _pwg_size_t *pwg_size; /* Current PWG size */
631 char pwg_keyword[3 + PPD_MAX_NAME + 1 + 12 + 1 + 12 + 3],
632 /* PWG keyword string */
633 ppd_name[PPD_MAX_NAME];
634 /* Normalized PPD name */
635 const char *pwg_name; /* Standard PWG media name */
636 _pwg_media_t *pwg_media; /* PWG media data */
637 _pwg_print_color_mode_t pwg_print_color_mode;
638 /* print-color-mode index */
639 _pwg_print_quality_t pwg_print_quality;
640 /* print-quality index */
641 int similar; /* Are the old and new size similar? */
642 _pwg_size_t *old_size; /* Current old size */
643 int old_imageable, /* Old imageable length in 2540ths */
644 old_borderless, /* Old borderless state */
645 old_known_pwg; /* Old PWG name is well-known */
646 int new_width, /* New width in 2540ths */
647 new_length, /* New length in 2540ths */
648 new_left, /* New left margin in 2540ths */
649 new_bottom, /* New bottom margin in 2540ths */
650 new_right, /* New right margin in 2540ths */
651 new_top, /* New top margin in 2540ths */
652 new_imageable, /* New imageable length in 2540ths */
653 new_borderless, /* New borderless state */
654 new_known_pwg; /* New PWG name is well-known */
655 _pwg_size_t *new_size; /* New size to add, if any */
656 const char *filter; /* Current filter */
657 _pwg_finishings_t *finishings; /* Current finishings value */
658
659
660 DEBUG_printf(("_ppdCacheCreateWithPPD(ppd=%p)", ppd));
661
662 /*
663 * Range check input...
664 */
665
666 if (!ppd)
667 return (NULL);
668
669 /*
670 * Allocate memory...
671 */
672
673 if ((pc = calloc(1, sizeof(_ppd_cache_t))) == NULL)
674 {
675 DEBUG_puts("_ppdCacheCreateWithPPD: Unable to allocate _ppd_cache_t.");
676 goto create_error;
677 }
678
679 /*
680 * Copy and convert size data...
681 */
682
683 if (ppd->num_sizes == 0)
684 {
685 DEBUG_puts("_ppdCacheCreateWithPPD: No page sizes in PPD.");
686 goto create_error;
687 }
688
689 if ((pc->sizes = calloc(ppd->num_sizes, sizeof(_pwg_size_t))) == NULL)
690 {
691 DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d "
692 "_pwg_size_t's.", ppd->num_sizes));
693 goto create_error;
694 }
695
696 for (i = ppd->num_sizes, pwg_size = pc->sizes, ppd_size = ppd->sizes;
697 i > 0;
698 i --, ppd_size ++)
699 {
700 /*
701 * Don't copy over custom size...
702 */
703
704 if (!_cups_strcasecmp(ppd_size->name, "Custom"))
705 continue;
706
707 /*
708 * Convert the PPD size name to the corresponding PWG keyword name.
709 */
710
711 if ((pwg_media = _pwgMediaForPPD(ppd_size->name)) != NULL)
712 {
713 /*
714 * Standard name, do we have conflicts?
715 */
716
717 for (j = 0; j < pc->num_sizes; j ++)
718 if (!strcmp(pc->sizes[j].map.pwg, pwg_media->pwg))
719 {
720 pwg_media = NULL;
721 break;
722 }
723 }
724
725 if (pwg_media)
726 {
727 /*
728 * Standard name and no conflicts, use it!
729 */
730
731 pwg_name = pwg_media->pwg;
732 new_known_pwg = 1;
733 }
734 else
735 {
736 /*
737 * Not a standard name; convert it to a PWG vendor name of the form:
738 *
739 * pp_lowerppd_WIDTHxHEIGHTuu
740 */
741
742 pwg_name = pwg_keyword;
743 new_known_pwg = 0;
744
745 pwg_unppdize_name(ppd_size->name, ppd_name, sizeof(ppd_name));
746 _pwgGenerateSize(pwg_keyword, sizeof(pwg_keyword), NULL, ppd_name,
747 _PWG_FROMPTS(ppd_size->width),
748 _PWG_FROMPTS(ppd_size->length));
749 }
750
751 /*
752 * If we have a similar paper with non-zero margins then we only
753 * want to keep it if it has a larger imageable area length.
754 */
755
756 pwg_media = _pwgMediaForSize(_PWG_FROMPTS(ppd_size->width),
757 _PWG_FROMPTS(ppd_size->length));
758 new_width = pwg_media->width;
759 new_length = pwg_media->length;
760 new_left = _PWG_FROMPTS(ppd_size->left);
761 new_bottom = _PWG_FROMPTS(ppd_size->bottom);
762 new_right = _PWG_FROMPTS(ppd_size->width - ppd_size->right);
763 new_top = _PWG_FROMPTS(ppd_size->length - ppd_size->top);
764 new_imageable = new_length - new_top - new_bottom;
765 new_borderless = new_bottom == 0 && new_top == 0 &&
766 new_left == 0 && new_right == 0;
767
768 for (k = pc->num_sizes, similar = 0, old_size = pc->sizes, new_size = NULL;
769 k > 0 && !similar;
770 k --, old_size ++)
771 {
772 old_imageable = old_size->length - old_size->top - old_size->bottom;
773 old_borderless = old_size->left == 0 && old_size->bottom == 0 &&
774 old_size->right == 0 && old_size->top == 0;
775 old_known_pwg = strncmp(old_size->map.pwg, "oe_", 3) &&
776 strncmp(old_size->map.pwg, "om_", 3);
777
778 similar = old_borderless == new_borderless &&
779 _PWG_EQUIVALENT(old_size->width, new_width) &&
780 _PWG_EQUIVALENT(old_size->length, new_length);
781
782 if (similar &&
783 (new_known_pwg || (!old_known_pwg && new_imageable > old_imageable)))
784 {
785 /*
786 * The new paper has a larger imageable area so it could replace
787 * the older paper. Regardless of the imageable area, we always
788 * prefer the size with a well-known PWG name.
789 */
790
791 new_size = old_size;
792 _cupsStrFree(old_size->map.ppd);
793 _cupsStrFree(old_size->map.pwg);
794 }
795 }
796
797 if (!similar)
798 {
799 /*
800 * The paper was unique enough to deserve its own entry so add it to the
801 * end.
802 */
803
804 new_size = pwg_size ++;
805 pc->num_sizes ++;
806 }
807
808 if (new_size)
809 {
810 /*
811 * Save this size...
812 */
813
814 new_size->map.ppd = _cupsStrAlloc(ppd_size->name);
815 new_size->map.pwg = _cupsStrAlloc(pwg_name);
816 new_size->width = new_width;
817 new_size->length = new_length;
818 new_size->left = new_left;
819 new_size->bottom = new_bottom;
820 new_size->right = new_right;
821 new_size->top = new_top;
822 }
823 }
824
825 if (ppd->variable_sizes)
826 {
827 /*
828 * Generate custom size data...
829 */
830
831 _pwgGenerateSize(pwg_keyword, sizeof(pwg_keyword), "custom", "max",
832 _PWG_FROMPTS(ppd->custom_max[0]),
833 _PWG_FROMPTS(ppd->custom_max[1]));
834 pc->custom_max_keyword = _cupsStrAlloc(pwg_keyword);
835 pc->custom_max_width = _PWG_FROMPTS(ppd->custom_max[0]);
836 pc->custom_max_length = _PWG_FROMPTS(ppd->custom_max[1]);
837
838 _pwgGenerateSize(pwg_keyword, sizeof(pwg_keyword), "custom", "min",
839 _PWG_FROMPTS(ppd->custom_min[0]),
840 _PWG_FROMPTS(ppd->custom_min[1]));
841 pc->custom_min_keyword = _cupsStrAlloc(pwg_keyword);
842 pc->custom_min_width = _PWG_FROMPTS(ppd->custom_min[0]);
843 pc->custom_min_length = _PWG_FROMPTS(ppd->custom_min[1]);
844
845 pc->custom_size.left = _PWG_FROMPTS(ppd->custom_margins[0]);
846 pc->custom_size.bottom = _PWG_FROMPTS(ppd->custom_margins[1]);
847 pc->custom_size.right = _PWG_FROMPTS(ppd->custom_margins[2]);
848 pc->custom_size.top = _PWG_FROMPTS(ppd->custom_margins[3]);
849 }
850
851 /*
852 * Copy and convert InputSlot data...
853 */
854
855 if ((input_slot = ppdFindOption(ppd, "InputSlot")) == NULL)
856 input_slot = ppdFindOption(ppd, "HPPaperSource");
857
858 if (input_slot)
859 {
860 pc->source_option = _cupsStrAlloc(input_slot->keyword);
861
862 if ((pc->sources = calloc(input_slot->num_choices,
863 sizeof(_pwg_map_t))) == NULL)
864 {
865 DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d "
866 "_pwg_map_t's for InputSlot.", input_slot->num_choices));
867 goto create_error;
868 }
869
870 pc->num_sources = input_slot->num_choices;
871
872 for (i = input_slot->num_choices, choice = input_slot->choices,
873 map = pc->sources;
874 i > 0;
875 i --, choice ++, map ++)
876 {
877 if (!_cups_strncasecmp(choice->choice, "Auto", 4) ||
878 !_cups_strcasecmp(choice->choice, "Default"))
879 pwg_name = "auto";
880 else if (!_cups_strcasecmp(choice->choice, "Cassette"))
881 pwg_name = "main";
882 else if (!_cups_strcasecmp(choice->choice, "PhotoTray"))
883 pwg_name = "photo";
884 else if (!_cups_strcasecmp(choice->choice, "CDTray"))
885 pwg_name = "disc";
886 else if (!_cups_strncasecmp(choice->choice, "Multipurpose", 12) ||
887 !_cups_strcasecmp(choice->choice, "MP") ||
888 !_cups_strcasecmp(choice->choice, "MPTray"))
889 pwg_name = "alternate";
890 else if (!_cups_strcasecmp(choice->choice, "LargeCapacity"))
891 pwg_name = "large-capacity";
892 else if (!_cups_strncasecmp(choice->choice, "Lower", 5))
893 pwg_name = "bottom";
894 else if (!_cups_strncasecmp(choice->choice, "Middle", 6))
895 pwg_name = "middle";
896 else if (!_cups_strncasecmp(choice->choice, "Upper", 5))
897 pwg_name = "top";
898 else if (!_cups_strncasecmp(choice->choice, "Side", 4))
899 pwg_name = "side";
900 else if (!_cups_strcasecmp(choice->choice, "Roll"))
901 pwg_name = "main-roll";
902 else
903 {
904 /*
905 * Convert PPD name to lowercase...
906 */
907
908 pwg_name = pwg_keyword;
909 pwg_unppdize_name(choice->choice, pwg_keyword, sizeof(pwg_keyword));
910 }
911
912 map->pwg = _cupsStrAlloc(pwg_name);
913 map->ppd = _cupsStrAlloc(choice->choice);
914 }
915 }
916
917 /*
918 * Copy and convert MediaType data...
919 */
920
921 if ((media_type = ppdFindOption(ppd, "MediaType")) != NULL)
922 {
923 if ((pc->types = calloc(media_type->num_choices,
924 sizeof(_pwg_map_t))) == NULL)
925 {
926 DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d "
927 "_pwg_map_t's for MediaType.", media_type->num_choices));
928 goto create_error;
929 }
930
931 pc->num_types = media_type->num_choices;
932
933 for (i = media_type->num_choices, choice = media_type->choices,
934 map = pc->types;
935 i > 0;
936 i --, choice ++, map ++)
937 {
938 if (!_cups_strncasecmp(choice->choice, "Auto", 4) ||
939 !_cups_strcasecmp(choice->choice, "Any") ||
940 !_cups_strcasecmp(choice->choice, "Default"))
941 pwg_name = "auto";
942 else if (!_cups_strncasecmp(choice->choice, "Card", 4))
943 pwg_name = "cardstock";
944 else if (!_cups_strncasecmp(choice->choice, "Env", 3))
945 pwg_name = "envelope";
946 else if (!_cups_strncasecmp(choice->choice, "Gloss", 5))
947 pwg_name = "photographic-glossy";
948 else if (!_cups_strcasecmp(choice->choice, "HighGloss"))
949 pwg_name = "photographic-high-gloss";
950 else if (!_cups_strcasecmp(choice->choice, "Matte"))
951 pwg_name = "photographic-matte";
952 else if (!_cups_strncasecmp(choice->choice, "Plain", 5))
953 pwg_name = "stationery";
954 else if (!_cups_strncasecmp(choice->choice, "Coated", 6))
955 pwg_name = "stationery-coated";
956 else if (!_cups_strcasecmp(choice->choice, "Inkjet"))
957 pwg_name = "stationery-inkjet";
958 else if (!_cups_strcasecmp(choice->choice, "Letterhead"))
959 pwg_name = "stationery-letterhead";
960 else if (!_cups_strncasecmp(choice->choice, "Preprint", 8))
961 pwg_name = "stationery-preprinted";
962 else if (!_cups_strcasecmp(choice->choice, "Recycled"))
963 pwg_name = "stationery-recycled";
964 else if (!_cups_strncasecmp(choice->choice, "Transparen", 10))
965 pwg_name = "transparency";
966 else
967 {
968 /*
969 * Convert PPD name to lowercase...
970 */
971
972 pwg_name = pwg_keyword;
973 pwg_unppdize_name(choice->choice, pwg_keyword, sizeof(pwg_keyword));
974 }
975
976 map->pwg = _cupsStrAlloc(pwg_name);
977 map->ppd = _cupsStrAlloc(choice->choice);
978 }
979 }
980
981 /*
982 * Copy and convert OutputBin data...
983 */
984
985 if ((output_bin = ppdFindOption(ppd, "OutputBin")) != NULL)
986 {
987 if ((pc->bins = calloc(output_bin->num_choices,
988 sizeof(_pwg_map_t))) == NULL)
989 {
990 DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d "
991 "_pwg_map_t's for OutputBin.", output_bin->num_choices));
992 goto create_error;
993 }
994
995 pc->num_bins = output_bin->num_choices;
996
997 for (i = output_bin->num_choices, choice = output_bin->choices,
998 map = pc->bins;
999 i > 0;
1000 i --, choice ++, map ++)
1001 {
1002 pwg_unppdize_name(choice->choice, pwg_keyword, sizeof(pwg_keyword));
1003
1004 map->pwg = _cupsStrAlloc(pwg_keyword);
1005 map->ppd = _cupsStrAlloc(choice->choice);
1006 }
1007 }
1008
1009 if ((ppd_attr = ppdFindAttr(ppd, "APPrinterPreset", NULL)) != NULL)
1010 {
1011 /*
1012 * Copy and convert APPrinterPreset (output-mode + print-quality) data...
1013 */
1014
1015 const char *quality, /* com.apple.print.preset.quality value */
1016 *output_mode, /* com.apple.print.preset.output-mode value */
1017 *color_model_val, /* ColorModel choice */
1018 *graphicsType, /* com.apple.print.preset.graphicsType value */
1019 *media_front_coating; /* com.apple.print.preset.media-front-coating value */
1020
1021 do
1022 {
1023 num_options = _ppdParseOptions(ppd_attr->value, 0, &options,
1024 _PPD_PARSE_ALL);
1025
1026 if ((quality = cupsGetOption("com.apple.print.preset.quality",
1027 num_options, options)) != NULL)
1028 {
1029 /*
1030 * Get the print-quality for this preset...
1031 */
1032
1033 if (!strcmp(quality, "low"))
1034 pwg_print_quality = _PWG_PRINT_QUALITY_DRAFT;
1035 else if (!strcmp(quality, "high"))
1036 pwg_print_quality = _PWG_PRINT_QUALITY_HIGH;
1037 else
1038 pwg_print_quality = _PWG_PRINT_QUALITY_NORMAL;
1039
1040 /*
1041 * Ignore graphicsType "Photo" presets that are not high quality.
1042 */
1043
1044 graphicsType = cupsGetOption("com.apple.print.preset.graphicsType",
1045 num_options, options);
1046
1047 if (pwg_print_quality != _PWG_PRINT_QUALITY_HIGH && graphicsType &&
1048 !strcmp(graphicsType, "Photo"))
1049 continue;
1050
1051 /*
1052 * Ignore presets for normal and draft quality where the coating
1053 * isn't "none" or "autodetect".
1054 */
1055
1056 media_front_coating = cupsGetOption(
1057 "com.apple.print.preset.media-front-coating",
1058 num_options, options);
1059
1060 if (pwg_print_quality != _PWG_PRINT_QUALITY_HIGH &&
1061 media_front_coating &&
1062 strcmp(media_front_coating, "none") &&
1063 strcmp(media_front_coating, "autodetect"))
1064 continue;
1065
1066 /*
1067 * Get the output mode for this preset...
1068 */
1069
1070 output_mode = cupsGetOption("com.apple.print.preset.output-mode",
1071 num_options, options);
1072 color_model_val = cupsGetOption("ColorModel", num_options, options);
1073
1074 if (output_mode)
1075 {
1076 if (!strcmp(output_mode, "monochrome"))
1077 pwg_print_color_mode = _PWG_PRINT_COLOR_MODE_MONOCHROME;
1078 else
1079 pwg_print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR;
1080 }
1081 else if (color_model_val)
1082 {
1083 if (!_cups_strcasecmp(color_model_val, "Gray"))
1084 pwg_print_color_mode = _PWG_PRINT_COLOR_MODE_MONOCHROME;
1085 else
1086 pwg_print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR;
1087 }
1088 else
1089 pwg_print_color_mode = _PWG_PRINT_COLOR_MODE_COLOR;
1090
1091 /*
1092 * Save the options for this combination as needed...
1093 */
1094
1095 if (!pc->num_presets[pwg_print_color_mode][pwg_print_quality])
1096 pc->num_presets[pwg_print_color_mode][pwg_print_quality] =
1097 _ppdParseOptions(ppd_attr->value, 0,
1098 pc->presets[pwg_print_color_mode] +
1099 pwg_print_quality, _PPD_PARSE_OPTIONS);
1100 }
1101
1102 cupsFreeOptions(num_options, options);
1103 }
1104 while ((ppd_attr = ppdFindNextAttr(ppd, "APPrinterPreset", NULL)) != NULL);
1105 }
1106
1107 if (!pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][_PWG_PRINT_QUALITY_DRAFT] &&
1108 !pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][_PWG_PRINT_QUALITY_NORMAL] &&
1109 !pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][_PWG_PRINT_QUALITY_HIGH])
1110 {
1111 /*
1112 * Try adding some common color options to create grayscale presets. These
1113 * are listed in order of popularity...
1114 */
1115
1116 const char *color_option = NULL, /* Color control option */
1117 *gray_choice = NULL; /* Choice to select grayscale */
1118
1119 if ((color_model = ppdFindOption(ppd, "ColorModel")) != NULL &&
1120 ppdFindChoice(color_model, "Gray"))
1121 {
1122 color_option = "ColorModel";
1123 gray_choice = "Gray";
1124 }
1125 else if ((color_model = ppdFindOption(ppd, "HPColorMode")) != NULL &&
1126 ppdFindChoice(color_model, "grayscale"))
1127 {
1128 color_option = "HPColorMode";
1129 gray_choice = "grayscale";
1130 }
1131 else if ((color_model = ppdFindOption(ppd, "BRMonoColor")) != NULL &&
1132 ppdFindChoice(color_model, "Mono"))
1133 {
1134 color_option = "BRMonoColor";
1135 gray_choice = "Mono";
1136 }
1137 else if ((color_model = ppdFindOption(ppd, "CNIJSGrayScale")) != NULL &&
1138 ppdFindChoice(color_model, "1"))
1139 {
1140 color_option = "CNIJSGrayScale";
1141 gray_choice = "1";
1142 }
1143 else if ((color_model = ppdFindOption(ppd, "HPColorAsGray")) != NULL &&
1144 ppdFindChoice(color_model, "True"))
1145 {
1146 color_option = "HPColorAsGray";
1147 gray_choice = "True";
1148 }
1149
1150 if (color_option && gray_choice)
1151 {
1152 /*
1153 * Copy and convert ColorModel (output-mode) data...
1154 */
1155
1156 cups_option_t *coption, /* Color option */
1157 *moption; /* Monochrome option */
1158
1159 for (pwg_print_quality = _PWG_PRINT_QUALITY_DRAFT;
1160 pwg_print_quality < _PWG_PRINT_QUALITY_MAX;
1161 pwg_print_quality ++)
1162 {
1163 if (pc->num_presets[_PWG_PRINT_COLOR_MODE_COLOR][pwg_print_quality])
1164 {
1165 /*
1166 * Copy the color options...
1167 */
1168
1169 num_options = pc->num_presets[_PWG_PRINT_COLOR_MODE_COLOR]
1170 [pwg_print_quality];
1171 options = calloc(sizeof(cups_option_t), num_options);
1172
1173 if (options)
1174 {
1175 for (i = num_options, moption = options,
1176 coption = pc->presets[_PWG_PRINT_COLOR_MODE_COLOR]
1177 [pwg_print_quality];
1178 i > 0;
1179 i --, moption ++, coption ++)
1180 {
1181 moption->name = _cupsStrRetain(coption->name);
1182 moption->value = _cupsStrRetain(coption->value);
1183 }
1184
1185 pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][pwg_print_quality] =
1186 num_options;
1187 pc->presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][pwg_print_quality] =
1188 options;
1189 }
1190 }
1191 else if (pwg_print_quality != _PWG_PRINT_QUALITY_NORMAL)
1192 continue;
1193
1194 /*
1195 * Add the grayscale option to the preset...
1196 */
1197
1198 pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME][pwg_print_quality] =
1199 cupsAddOption(color_option, gray_choice,
1200 pc->num_presets[_PWG_PRINT_COLOR_MODE_MONOCHROME]
1201 [pwg_print_quality],
1202 pc->presets[_PWG_PRINT_COLOR_MODE_MONOCHROME] +
1203 pwg_print_quality);
1204 }
1205 }
1206 }
1207
1208 /*
1209 * Copy and convert Duplex (sides) data...
1210 */
1211
1212 if ((duplex = ppdFindOption(ppd, "Duplex")) == NULL)
1213 if ((duplex = ppdFindOption(ppd, "JCLDuplex")) == NULL)
1214 if ((duplex = ppdFindOption(ppd, "EFDuplex")) == NULL)
1215 if ((duplex = ppdFindOption(ppd, "EFDuplexing")) == NULL)
1216 duplex = ppdFindOption(ppd, "KD03Duplex");
1217
1218 if (duplex)
1219 {
1220 pc->sides_option = _cupsStrAlloc(duplex->keyword);
1221
1222 for (i = duplex->num_choices, choice = duplex->choices;
1223 i > 0;
1224 i --, choice ++)
1225 {
1226 if ((!_cups_strcasecmp(choice->choice, "None") ||
1227 !_cups_strcasecmp(choice->choice, "False")) && !pc->sides_1sided)
1228 pc->sides_1sided = _cupsStrAlloc(choice->choice);
1229 else if ((!_cups_strcasecmp(choice->choice, "DuplexNoTumble") ||
1230 !_cups_strcasecmp(choice->choice, "LongEdge") ||
1231 !_cups_strcasecmp(choice->choice, "Top")) && !pc->sides_2sided_long)
1232 pc->sides_2sided_long = _cupsStrAlloc(choice->choice);
1233 else if ((!_cups_strcasecmp(choice->choice, "DuplexTumble") ||
1234 !_cups_strcasecmp(choice->choice, "ShortEdge") ||
1235 !_cups_strcasecmp(choice->choice, "Bottom")) &&
1236 !pc->sides_2sided_short)
1237 pc->sides_2sided_short = _cupsStrAlloc(choice->choice);
1238 }
1239 }
1240
1241 /*
1242 * Copy filters and pre-filters...
1243 */
1244
1245 pc->filters = cupsArrayNew3(NULL, NULL, NULL, 0,
1246 (cups_acopy_func_t)_cupsStrAlloc,
1247 (cups_afree_func_t)_cupsStrFree);
1248
1249 cupsArrayAdd(pc->filters,
1250 "application/vnd.cups-raw application/octet-stream 0 -");
1251
1252 if ((ppd_attr = ppdFindAttr(ppd, "cupsFilter2", NULL)) != NULL)
1253 {
1254 do
1255 {
1256 cupsArrayAdd(pc->filters, ppd_attr->value);
1257 }
1258 while ((ppd_attr = ppdFindNextAttr(ppd, "cupsFilter2", NULL)) != NULL);
1259 }
1260 else if (ppd->num_filters > 0)
1261 {
1262 for (i = 0; i < ppd->num_filters; i ++)
1263 cupsArrayAdd(pc->filters, ppd->filters[i]);
1264 }
1265 else
1266 cupsArrayAdd(pc->filters, "application/vnd.cups-postscript 0 -");
1267
1268 /*
1269 * See if we have a command filter...
1270 */
1271
1272 for (filter = (const char *)cupsArrayFirst(pc->filters);
1273 filter;
1274 filter = (const char *)cupsArrayNext(pc->filters))
1275 if (!_cups_strncasecmp(filter, "application/vnd.cups-command", 28) &&
1276 _cups_isspace(filter[28]))
1277 break;
1278
1279 if (!filter &&
1280 ((ppd_attr = ppdFindAttr(ppd, "cupsCommands", NULL)) == NULL ||
1281 _cups_strcasecmp(ppd_attr->value, "none")))
1282 {
1283 /*
1284 * No command filter and no cupsCommands keyword telling us not to use one.
1285 * See if this is a PostScript printer, and if so add a PostScript command
1286 * filter...
1287 */
1288
1289 for (filter = (const char *)cupsArrayFirst(pc->filters);
1290 filter;
1291 filter = (const char *)cupsArrayNext(pc->filters))
1292 if (!_cups_strncasecmp(filter, "application/vnd.cups-postscript", 31) &&
1293 _cups_isspace(filter[31]))
1294 break;
1295
1296 if (filter)
1297 cupsArrayAdd(pc->filters,
1298 "application/vnd.cups-command application/postscript 100 "
1299 "commandtops");
1300 }
1301
1302 if ((ppd_attr = ppdFindAttr(ppd, "cupsPreFilter", NULL)) != NULL)
1303 {
1304 pc->prefilters = cupsArrayNew3(NULL, NULL, NULL, 0,
1305 (cups_acopy_func_t)_cupsStrAlloc,
1306 (cups_afree_func_t)_cupsStrFree);
1307
1308 do
1309 {
1310 cupsArrayAdd(pc->prefilters, ppd_attr->value);
1311 }
1312 while ((ppd_attr = ppdFindNextAttr(ppd, "cupsPreFilter", NULL)) != NULL);
1313 }
1314
1315 if ((ppd_attr = ppdFindAttr(ppd, "cupsSingleFile", NULL)) != NULL)
1316 pc->single_file = !_cups_strcasecmp(ppd_attr->value, "true");
1317
1318 /*
1319 * Copy the product string, if any...
1320 */
1321
1322 if (ppd->product)
1323 pc->product = _cupsStrAlloc(ppd->product);
1324
1325 /*
1326 * Copy finishings mapping data...
1327 */
1328
1329 if ((ppd_attr = ppdFindAttr(ppd, "cupsIPPFinishings", NULL)) != NULL)
1330 {
1331 pc->finishings = cupsArrayNew3((cups_array_func_t)pwg_compare_finishings,
1332 NULL, NULL, 0, NULL,
1333 (cups_afree_func_t)pwg_free_finishings);
1334
1335 do
1336 {
1337 if ((finishings = calloc(1, sizeof(_pwg_finishings_t))) == NULL)
1338 goto create_error;
1339
1340 finishings->value = atoi(ppd_attr->spec);
1341 finishings->num_options = _ppdParseOptions(ppd_attr->value, 0,
1342 &(finishings->options),
1343 _PPD_PARSE_OPTIONS);
1344
1345 cupsArrayAdd(pc->finishings, finishings);
1346 }
1347 while ((ppd_attr = ppdFindNextAttr(ppd, "cupsIPPFinishings",
1348 NULL)) != NULL);
1349 }
1350
1351 /*
1352 * Return the cache data...
1353 */
1354
1355 return (pc);
1356
1357 /*
1358 * If we get here we need to destroy the PWG mapping data and return NULL...
1359 */
1360
1361 create_error:
1362
1363 _cupsSetError(IPP_INTERNAL_ERROR, _("Out of memory."), 1);
1364 _ppdCacheDestroy(pc);
1365
1366 return (NULL);
1367 }
1368
1369
1370 /*
1371 * '_ppdCacheDestroy()' - Free all memory used for PWG mapping data.
1372 */
1373
1374 void
1375 _ppdCacheDestroy(_ppd_cache_t *pc) /* I - PPD cache and mapping data */
1376 {
1377 int i; /* Looping var */
1378 _pwg_map_t *map; /* Current map */
1379 _pwg_size_t *size; /* Current size */
1380
1381
1382 /*
1383 * Range check input...
1384 */
1385
1386 if (!pc)
1387 return;
1388
1389 /*
1390 * Free memory as needed...
1391 */
1392
1393 if (pc->bins)
1394 {
1395 for (i = pc->num_bins, map = pc->bins; i > 0; i --, map ++)
1396 {
1397 _cupsStrFree(map->pwg);
1398 _cupsStrFree(map->ppd);
1399 }
1400
1401 free(pc->bins);
1402 }
1403
1404 if (pc->sizes)
1405 {
1406 for (i = pc->num_sizes, size = pc->sizes; i > 0; i --, size ++)
1407 {
1408 _cupsStrFree(size->map.pwg);
1409 _cupsStrFree(size->map.ppd);
1410 }
1411
1412 free(pc->sizes);
1413 }
1414
1415 if (pc->source_option)
1416 _cupsStrFree(pc->source_option);
1417
1418 if (pc->sources)
1419 {
1420 for (i = pc->num_sources, map = pc->sources; i > 0; i --, map ++)
1421 {
1422 _cupsStrFree(map->pwg);
1423 _cupsStrFree(map->ppd);
1424 }
1425
1426 free(pc->sources);
1427 }
1428
1429 if (pc->types)
1430 {
1431 for (i = pc->num_types, map = pc->types; i > 0; i --, map ++)
1432 {
1433 _cupsStrFree(map->pwg);
1434 _cupsStrFree(map->ppd);
1435 }
1436
1437 free(pc->types);
1438 }
1439
1440 if (pc->custom_max_keyword)
1441 _cupsStrFree(pc->custom_max_keyword);
1442
1443 if (pc->custom_min_keyword)
1444 _cupsStrFree(pc->custom_min_keyword);
1445
1446 _cupsStrFree(pc->product);
1447 cupsArrayDelete(pc->filters);
1448 cupsArrayDelete(pc->prefilters);
1449 cupsArrayDelete(pc->finishings);
1450
1451 free(pc);
1452 }
1453
1454
1455 /*
1456 * '_ppdCacheGetBin()' - Get the PWG output-bin keyword associated with a PPD
1457 * OutputBin.
1458 */
1459
1460 const char * /* O - output-bin or NULL */
1461 _ppdCacheGetBin(
1462 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
1463 const char *output_bin) /* I - PPD OutputBin string */
1464 {
1465 int i; /* Looping var */
1466
1467
1468 /*
1469 * Range check input...
1470 */
1471
1472 if (!pc || !output_bin)
1473 return (NULL);
1474
1475 /*
1476 * Look up the OutputBin string...
1477 */
1478
1479
1480 for (i = 0; i < pc->num_bins; i ++)
1481 if (!_cups_strcasecmp(output_bin, pc->bins[i].ppd))
1482 return (pc->bins[i].pwg);
1483
1484 return (NULL);
1485 }
1486
1487
1488 /*
1489 * '_ppdCacheGetFinishingOptions()' - Get PPD finishing options for the given
1490 * IPP finishings value(s).
1491 */
1492
1493 int /* O - New number of options */
1494 _ppdCacheGetFinishingOptions(
1495 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
1496 ipp_t *job, /* I - Job attributes or NULL */
1497 ipp_finish_t value, /* I - IPP finishings value of IPP_FINISHINGS_NONE */
1498 int num_options, /* I - Number of options */
1499 cups_option_t **options) /* IO - Options */
1500 {
1501 int i; /* Looping var */
1502 _pwg_finishings_t *f, /* PWG finishings options */
1503 key; /* Search key */
1504 ipp_attribute_t *attr; /* Finishings attribute */
1505 cups_option_t *option; /* Current finishings option */
1506
1507
1508 /*
1509 * Range check input...
1510 */
1511
1512 if (!pc || cupsArrayCount(pc->finishings) == 0 || !options ||
1513 (!job && value == IPP_FINISHINGS_NONE))
1514 return (num_options);
1515
1516 /*
1517 * Apply finishing options...
1518 */
1519
1520 if (job && (attr = ippFindAttribute(job, "finishings", IPP_TAG_ENUM)) != NULL)
1521 {
1522 int num_values = ippGetCount(attr); /* Number of values */
1523
1524 for (i = 0; i < num_values; i ++)
1525 {
1526 key.value = ippGetInteger(attr, i);
1527
1528 if ((f = cupsArrayFind(pc->finishings, &key)) != NULL)
1529 {
1530 int j; /* Another looping var */
1531
1532 for (j = f->num_options, option = f->options; j > 0; j --, option ++)
1533 num_options = cupsAddOption(option->name, option->value,
1534 num_options, options);
1535 }
1536 }
1537 }
1538 else if (value != IPP_FINISHINGS_NONE)
1539 {
1540 key.value = value;
1541
1542 if ((f = cupsArrayFind(pc->finishings, &key)) != NULL)
1543 {
1544 int j; /* Another looping var */
1545
1546 for (j = f->num_options, option = f->options; j > 0; j --, option ++)
1547 num_options = cupsAddOption(option->name, option->value,
1548 num_options, options);
1549 }
1550 }
1551
1552 return (num_options);
1553 }
1554
1555
1556 /*
1557 * '_ppdCacheGetFinishingValues()' - Get IPP finishings value(s) from the given
1558 * PPD options.
1559 */
1560
1561 int /* O - Number of finishings values */
1562 _ppdCacheGetFinishingValues(
1563 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
1564 int num_options, /* I - Number of options */
1565 cups_option_t *options, /* I - Options */
1566 int max_values, /* I - Maximum number of finishings values */
1567 int *values) /* O - Finishings values */
1568 {
1569 int i, /* Looping var */
1570 num_values = 0; /* Number of values */
1571 _pwg_finishings_t *f; /* Current finishings option */
1572 cups_option_t *option; /* Current option */
1573 const char *val; /* Value for option */
1574
1575
1576 /*
1577 * Range check input...
1578 */
1579
1580 if (!pc || !pc->finishings || num_options < 1 || max_values < 1 || !values)
1581 return (0);
1582
1583 /*
1584 * Go through the finishings options and see what is set...
1585 */
1586
1587 for (f = (_pwg_finishings_t *)cupsArrayFirst(pc->finishings);
1588 f;
1589 f = (_pwg_finishings_t *)cupsArrayNext(pc->finishings))
1590 {
1591 for (i = f->num_options, option = f->options; i > 0; i --, option ++)
1592 if ((val = cupsGetOption(option->name, num_options, options)) == NULL ||
1593 _cups_strcasecmp(option->value, val))
1594 break;
1595
1596 if (i == 0)
1597 {
1598 values[num_values ++] = f->value;
1599
1600 if (num_values >= max_values)
1601 break;
1602 }
1603 }
1604
1605 return (num_values);
1606 }
1607
1608
1609 /*
1610 * '_ppdCacheGetInputSlot()' - Get the PPD InputSlot associated with the job
1611 * attributes or a keyword string.
1612 */
1613
1614 const char * /* O - PPD InputSlot or NULL */
1615 _ppdCacheGetInputSlot(
1616 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
1617 ipp_t *job, /* I - Job attributes or NULL */
1618 const char *keyword) /* I - Keyword string or NULL */
1619 {
1620 /*
1621 * Range check input...
1622 */
1623
1624 if (!pc || pc->num_sources == 0 || (!job && !keyword))
1625 return (NULL);
1626
1627 if (job && !keyword)
1628 {
1629 /*
1630 * Lookup the media-col attribute and any media-source found there...
1631 */
1632
1633 ipp_attribute_t *media_col, /* media-col attribute */
1634 *media_source; /* media-source attribute */
1635 _pwg_size_t size; /* Dimensional size */
1636 int margins_set; /* Were the margins set? */
1637
1638 media_col = ippFindAttribute(job, "media-col", IPP_TAG_BEGIN_COLLECTION);
1639 if (media_col &&
1640 (media_source = ippFindAttribute(ippGetCollection(media_col, 0),
1641 "media-source",
1642 IPP_TAG_KEYWORD)) != NULL)
1643 {
1644 /*
1645 * Use the media-source value from media-col...
1646 */
1647
1648 keyword = ippGetString(media_source, 0, NULL);
1649 }
1650 else if (_pwgInitSize(&size, job, &margins_set))
1651 {
1652 /*
1653 * For media <= 5x7, look for a photo tray...
1654 */
1655
1656 if (size.width <= (5 * 2540) && size.length <= (7 * 2540))
1657 keyword = "photo";
1658 }
1659 }
1660
1661 if (keyword)
1662 {
1663 int i; /* Looping var */
1664
1665 for (i = 0; i < pc->num_sources; i ++)
1666 if (!_cups_strcasecmp(keyword, pc->sources[i].pwg))
1667 return (pc->sources[i].ppd);
1668 }
1669
1670 return (NULL);
1671 }
1672
1673
1674 /*
1675 * '_ppdCacheGetMediaType()' - Get the PPD MediaType associated with the job
1676 * attributes or a keyword string.
1677 */
1678
1679 const char * /* O - PPD MediaType or NULL */
1680 _ppdCacheGetMediaType(
1681 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
1682 ipp_t *job, /* I - Job attributes or NULL */
1683 const char *keyword) /* I - Keyword string or NULL */
1684 {
1685 /*
1686 * Range check input...
1687 */
1688
1689 if (!pc || pc->num_types == 0 || (!job && !keyword))
1690 return (NULL);
1691
1692 if (job && !keyword)
1693 {
1694 /*
1695 * Lookup the media-col attribute and any media-source found there...
1696 */
1697
1698 ipp_attribute_t *media_col, /* media-col attribute */
1699 *media_type; /* media-type attribute */
1700
1701 media_col = ippFindAttribute(job, "media-col", IPP_TAG_BEGIN_COLLECTION);
1702 if (media_col)
1703 {
1704 if ((media_type = ippFindAttribute(media_col->values[0].collection,
1705 "media-type",
1706 IPP_TAG_KEYWORD)) == NULL)
1707 media_type = ippFindAttribute(media_col->values[0].collection,
1708 "media-type", IPP_TAG_NAME);
1709
1710 if (media_type)
1711 keyword = media_type->values[0].string.text;
1712 }
1713 }
1714
1715 if (keyword)
1716 {
1717 int i; /* Looping var */
1718
1719 for (i = 0; i < pc->num_types; i ++)
1720 if (!_cups_strcasecmp(keyword, pc->types[i].pwg))
1721 return (pc->types[i].ppd);
1722 }
1723
1724 return (NULL);
1725 }
1726
1727
1728 /*
1729 * '_ppdCacheGetOutputBin()' - Get the PPD OutputBin associated with the keyword
1730 * string.
1731 */
1732
1733 const char * /* O - PPD OutputBin or NULL */
1734 _ppdCacheGetOutputBin(
1735 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
1736 const char *output_bin) /* I - Keyword string */
1737 {
1738 int i; /* Looping var */
1739
1740
1741 /*
1742 * Range check input...
1743 */
1744
1745 if (!pc || !output_bin)
1746 return (NULL);
1747
1748 /*
1749 * Look up the OutputBin string...
1750 */
1751
1752
1753 for (i = 0; i < pc->num_bins; i ++)
1754 if (!_cups_strcasecmp(output_bin, pc->bins[i].pwg))
1755 return (pc->bins[i].ppd);
1756
1757 return (NULL);
1758 }
1759
1760
1761 /*
1762 * '_ppdCacheGetPageSize()' - Get the PPD PageSize associated with the job
1763 * attributes or a keyword string.
1764 */
1765
1766 const char * /* O - PPD PageSize or NULL */
1767 _ppdCacheGetPageSize(
1768 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
1769 ipp_t *job, /* I - Job attributes or NULL */
1770 const char *keyword, /* I - Keyword string or NULL */
1771 int *exact) /* O - 1 if exact match, 0 otherwise */
1772 {
1773 int i; /* Looping var */
1774 _pwg_size_t *size, /* Current size */
1775 *closest, /* Closest size */
1776 jobsize; /* Size data from job */
1777 int margins_set, /* Were the margins set? */
1778 dwidth, /* Difference in width */
1779 dlength, /* Difference in length */
1780 dleft, /* Difference in left margins */
1781 dright, /* Difference in right margins */
1782 dbottom, /* Difference in bottom margins */
1783 dtop, /* Difference in top margins */
1784 dmin, /* Minimum difference */
1785 dclosest; /* Closest difference */
1786 const char *ppd_name; /* PPD media name */
1787
1788
1789 DEBUG_printf(("_ppdCacheGetPageSize(pc=%p, job=%p, keyword=\"%s\", exact=%p)",
1790 pc, job, keyword, exact));
1791
1792 /*
1793 * Range check input...
1794 */
1795
1796 if (!pc || (!job && !keyword))
1797 return (NULL);
1798
1799 if (exact)
1800 *exact = 0;
1801
1802 ppd_name = keyword;
1803
1804 if (job)
1805 {
1806 /*
1807 * Try getting the PPD media name from the job attributes...
1808 */
1809
1810 ipp_attribute_t *attr; /* Job attribute */
1811
1812 if ((attr = ippFindAttribute(job, "PageSize", IPP_TAG_ZERO)) == NULL)
1813 if ((attr = ippFindAttribute(job, "PageRegion", IPP_TAG_ZERO)) == NULL)
1814 attr = ippFindAttribute(job, "media", IPP_TAG_ZERO);
1815
1816 #ifdef DEBUG
1817 if (attr)
1818 DEBUG_printf(("1_ppdCacheGetPageSize: Found attribute %s (%s)",
1819 attr->name, ippTagString(attr->value_tag)));
1820 else
1821 DEBUG_puts("1_ppdCacheGetPageSize: Did not find media attribute.");
1822 #endif /* DEBUG */
1823
1824 if (attr && (attr->value_tag == IPP_TAG_NAME ||
1825 attr->value_tag == IPP_TAG_KEYWORD))
1826 ppd_name = attr->values[0].string.text;
1827 }
1828
1829 DEBUG_printf(("1_ppdCacheGetPageSize: ppd_name=\"%s\"", ppd_name));
1830
1831 if (ppd_name)
1832 {
1833 /*
1834 * Try looking up the named PPD size first...
1835 */
1836
1837 for (i = pc->num_sizes, size = pc->sizes; i > 0; i --, size ++)
1838 {
1839 DEBUG_printf(("2_ppdCacheGetPageSize: size[%d]=[\"%s\" \"%s\"]",
1840 (int)(size - pc->sizes), size->map.pwg, size->map.ppd));
1841
1842 if (!_cups_strcasecmp(ppd_name, size->map.ppd) ||
1843 !_cups_strcasecmp(ppd_name, size->map.pwg))
1844 {
1845 if (exact)
1846 *exact = 1;
1847
1848 DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\"", ppd_name));
1849
1850 return (size->map.ppd);
1851 }
1852 }
1853 }
1854
1855 if (job && !keyword)
1856 {
1857 /*
1858 * Get the size using media-col or media, with the preference being
1859 * media-col.
1860 */
1861
1862 if (!_pwgInitSize(&jobsize, job, &margins_set))
1863 return (NULL);
1864 }
1865 else
1866 {
1867 /*
1868 * Get the size using a media keyword...
1869 */
1870
1871 _pwg_media_t *media; /* Media definition */
1872
1873
1874 if ((media = _pwgMediaForPWG(keyword)) == NULL)
1875 if ((media = _pwgMediaForLegacy(keyword)) == NULL)
1876 if ((media = _pwgMediaForPPD(keyword)) == NULL)
1877 return (NULL);
1878
1879 jobsize.width = media->width;
1880 jobsize.length = media->length;
1881 margins_set = 0;
1882 }
1883
1884 /*
1885 * Now that we have the dimensions and possibly the margins, look at the
1886 * available sizes and find the match...
1887 */
1888
1889 closest = NULL;
1890 dclosest = 999999999;
1891
1892 if (!ppd_name || _cups_strncasecmp(ppd_name, "Custom.", 7) ||
1893 _cups_strncasecmp(ppd_name, "custom_", 7))
1894 {
1895 for (i = pc->num_sizes, size = pc->sizes; i > 0; i --, size ++)
1896 {
1897 /*
1898 * Adobe uses a size matching algorithm with an epsilon of 5 points, which
1899 * is just about 176/2540ths...
1900 */
1901
1902 dwidth = size->width - jobsize.width;
1903 dlength = size->length - jobsize.length;
1904
1905 if (dwidth <= -176 || dwidth >= 176 || dlength <= -176 || dlength >= 176)
1906 continue;
1907
1908 if (margins_set)
1909 {
1910 /*
1911 * Use a tighter epsilon of 1 point (35/2540ths) for margins...
1912 */
1913
1914 dleft = size->left - jobsize.left;
1915 dright = size->right - jobsize.right;
1916 dtop = size->top - jobsize.top;
1917 dbottom = size->bottom - jobsize.bottom;
1918
1919 if (dleft <= -35 || dleft >= 35 || dright <= -35 || dright >= 35 ||
1920 dtop <= -35 || dtop >= 35 || dbottom <= -35 || dbottom >= 35)
1921 {
1922 dleft = dleft < 0 ? -dleft : dleft;
1923 dright = dright < 0 ? -dright : dright;
1924 dbottom = dbottom < 0 ? -dbottom : dbottom;
1925 dtop = dtop < 0 ? -dtop : dtop;
1926 dmin = dleft + dright + dbottom + dtop;
1927
1928 if (dmin < dclosest)
1929 {
1930 dclosest = dmin;
1931 closest = size;
1932 }
1933
1934 continue;
1935 }
1936 }
1937
1938 if (exact)
1939 *exact = 1;
1940
1941 DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\"", size->map.ppd));
1942
1943 return (size->map.ppd);
1944 }
1945 }
1946
1947 if (closest)
1948 {
1949 DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\" (closest)",
1950 closest->map.ppd));
1951
1952 return (closest->map.ppd);
1953 }
1954
1955 /*
1956 * If we get here we need to check for custom page size support...
1957 */
1958
1959 if (jobsize.width >= pc->custom_min_width &&
1960 jobsize.width <= pc->custom_max_width &&
1961 jobsize.length >= pc->custom_min_length &&
1962 jobsize.length <= pc->custom_max_length)
1963 {
1964 /*
1965 * In range, format as Custom.WWWWxLLLL (points).
1966 */
1967
1968 snprintf(pc->custom_ppd_size, sizeof(pc->custom_ppd_size), "Custom.%dx%d",
1969 (int)_PWG_TOPTS(jobsize.width), (int)_PWG_TOPTS(jobsize.length));
1970
1971 if (margins_set && exact)
1972 {
1973 dleft = pc->custom_size.left - jobsize.left;
1974 dright = pc->custom_size.right - jobsize.right;
1975 dtop = pc->custom_size.top - jobsize.top;
1976 dbottom = pc->custom_size.bottom - jobsize.bottom;
1977
1978 if (dleft > -35 && dleft < 35 && dright > -35 && dright < 35 &&
1979 dtop > -35 && dtop < 35 && dbottom > -35 && dbottom < 35)
1980 *exact = 1;
1981 }
1982 else if (exact)
1983 *exact = 1;
1984
1985 DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\" (custom)",
1986 pc->custom_ppd_size));
1987
1988 return (pc->custom_ppd_size);
1989 }
1990
1991 /*
1992 * No custom page size support or the size is out of range - return NULL.
1993 */
1994
1995 DEBUG_puts("1_ppdCacheGetPageSize: Returning NULL");
1996
1997 return (NULL);
1998 }
1999
2000
2001 /*
2002 * '_ppdCacheGetSize()' - Get the PWG size associated with a PPD PageSize.
2003 */
2004
2005 _pwg_size_t * /* O - PWG size or NULL */
2006 _ppdCacheGetSize(
2007 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
2008 const char *page_size) /* I - PPD PageSize */
2009 {
2010 int i; /* Looping var */
2011 _pwg_media_t *media; /* Media */
2012 _pwg_size_t *size; /* Current size */
2013
2014
2015 /*
2016 * Range check input...
2017 */
2018
2019 if (!pc || !page_size)
2020 return (NULL);
2021
2022 if (!_cups_strncasecmp(page_size, "Custom.", 7))
2023 {
2024 /*
2025 * Custom size; size name can be one of the following:
2026 *
2027 * Custom.WIDTHxLENGTHin - Size in inches
2028 * Custom.WIDTHxLENGTHft - Size in feet
2029 * Custom.WIDTHxLENGTHcm - Size in centimeters
2030 * Custom.WIDTHxLENGTHmm - Size in millimeters
2031 * Custom.WIDTHxLENGTHm - Size in meters
2032 * Custom.WIDTHxLENGTH[pt] - Size in points
2033 */
2034
2035 double w, l; /* Width and length of page */
2036 char *ptr; /* Pointer into PageSize */
2037 struct lconv *loc; /* Locale data */
2038
2039 loc = localeconv();
2040 w = (float)_cupsStrScand(page_size + 7, &ptr, loc);
2041 if (!ptr || *ptr != 'x')
2042 return (NULL);
2043
2044 l = (float)_cupsStrScand(ptr + 1, &ptr, loc);
2045 if (!ptr)
2046 return (NULL);
2047
2048 if (!_cups_strcasecmp(ptr, "in"))
2049 {
2050 w *= 2540.0;
2051 l *= 2540.0;
2052 }
2053 else if (!_cups_strcasecmp(ptr, "ft"))
2054 {
2055 w *= 12.0 * 2540.0;
2056 l *= 12.0 * 2540.0;
2057 }
2058 else if (!_cups_strcasecmp(ptr, "mm"))
2059 {
2060 w *= 100.0;
2061 l *= 100.0;
2062 }
2063 else if (!_cups_strcasecmp(ptr, "cm"))
2064 {
2065 w *= 1000.0;
2066 l *= 1000.0;
2067 }
2068 else if (!_cups_strcasecmp(ptr, "m"))
2069 {
2070 w *= 100000.0;
2071 l *= 100000.0;
2072 }
2073 else
2074 {
2075 w *= 2540.0 / 72.0;
2076 l *= 2540.0 / 72.0;
2077 }
2078
2079 pc->custom_size.width = (int)w;
2080 pc->custom_size.length = (int)l;
2081
2082 return (&(pc->custom_size));
2083 }
2084
2085 /*
2086 * Not a custom size - look it up...
2087 */
2088
2089 for (i = pc->num_sizes, size = pc->sizes; i > 0; i --, size ++)
2090 if (!_cups_strcasecmp(page_size, size->map.ppd) ||
2091 !_cups_strcasecmp(page_size, size->map.pwg))
2092 return (size);
2093
2094 /*
2095 * Look up standard sizes...
2096 */
2097
2098 if ((media = _pwgMediaForPPD(page_size)) == NULL)
2099 if ((media = _pwgMediaForLegacy(page_size)) == NULL)
2100 media = _pwgMediaForPWG(page_size);
2101
2102 if (media)
2103 {
2104 pc->custom_size.width = media->width;
2105 pc->custom_size.length = media->length;
2106
2107 return (&(pc->custom_size));
2108 }
2109
2110 return (NULL);
2111 }
2112
2113
2114 /*
2115 * '_ppdCacheGetSource()' - Get the PWG media-source associated with a PPD
2116 * InputSlot.
2117 */
2118
2119 const char * /* O - PWG media-source keyword */
2120 _ppdCacheGetSource(
2121 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
2122 const char *input_slot) /* I - PPD InputSlot */
2123 {
2124 int i; /* Looping var */
2125 _pwg_map_t *source; /* Current source */
2126
2127
2128 /*
2129 * Range check input...
2130 */
2131
2132 if (!pc || !input_slot)
2133 return (NULL);
2134
2135 for (i = pc->num_sources, source = pc->sources; i > 0; i --, source ++)
2136 if (!_cups_strcasecmp(input_slot, source->ppd))
2137 return (source->pwg);
2138
2139 return (NULL);
2140 }
2141
2142
2143 /*
2144 * '_ppdCacheGetType()' - Get the PWG media-type associated with a PPD
2145 * MediaType.
2146 */
2147
2148 const char * /* O - PWG media-type keyword */
2149 _ppdCacheGetType(
2150 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
2151 const char *media_type) /* I - PPD MediaType */
2152 {
2153 int i; /* Looping var */
2154 _pwg_map_t *type; /* Current type */
2155
2156
2157 /*
2158 * Range check input...
2159 */
2160
2161 if (!pc || !media_type)
2162 return (NULL);
2163
2164 for (i = pc->num_types, type = pc->types; i > 0; i --, type ++)
2165 if (!_cups_strcasecmp(media_type, type->ppd))
2166 return (type->pwg);
2167
2168 return (NULL);
2169 }
2170
2171
2172 /*
2173 * '_ppdCacheWriteFile()' - Write PWG mapping data to a file.
2174 */
2175
2176 int /* O - 1 on success, 0 on failure */
2177 _ppdCacheWriteFile(
2178 _ppd_cache_t *pc, /* I - PPD cache and mapping data */
2179 const char *filename, /* I - File to write */
2180 ipp_t *attrs) /* I - Attributes to write, if any */
2181 {
2182 int i, j, k; /* Looping vars */
2183 cups_file_t *fp; /* Output file */
2184 _pwg_size_t *size; /* Current size */
2185 _pwg_map_t *map; /* Current map */
2186 _pwg_finishings_t *f; /* Current finishing option */
2187 cups_option_t *option; /* Current option */
2188 const char *value; /* Filter/pre-filter value */
2189 char newfile[1024]; /* New filename */
2190
2191
2192 /*
2193 * Range check input...
2194 */
2195
2196 if (!pc || !filename)
2197 {
2198 _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
2199 return (0);
2200 }
2201
2202 /*
2203 * Open the file and write with compression...
2204 */
2205
2206 snprintf(newfile, sizeof(newfile), "%s.N", filename);
2207 if ((fp = cupsFileOpen(newfile, "w9")) == NULL)
2208 {
2209 _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno), 0);
2210 return (0);
2211 }
2212
2213 /*
2214 * Standard header...
2215 */
2216
2217 cupsFilePrintf(fp, "#CUPS-PPD-CACHE-%d\n", _PPD_CACHE_VERSION);
2218
2219 /*
2220 * Output bins...
2221 */
2222
2223 if (pc->num_bins > 0)
2224 {
2225 cupsFilePrintf(fp, "NumBins %d\n", pc->num_bins);
2226 for (i = pc->num_bins, map = pc->bins; i > 0; i --, map ++)
2227 cupsFilePrintf(fp, "Bin %s %s\n", map->pwg, map->ppd);
2228 }
2229
2230 /*
2231 * Media sizes...
2232 */
2233
2234 cupsFilePrintf(fp, "NumSizes %d\n", pc->num_sizes);
2235 for (i = pc->num_sizes, size = pc->sizes; i > 0; i --, size ++)
2236 cupsFilePrintf(fp, "Size %s %s %d %d %d %d %d %d\n", size->map.pwg,
2237 size->map.ppd, size->width, size->length, size->left,
2238 size->bottom, size->right, size->top);
2239 if (pc->custom_max_width > 0)
2240 cupsFilePrintf(fp, "CustomSize %d %d %d %d %d %d %d %d\n",
2241 pc->custom_max_width, pc->custom_max_length,
2242 pc->custom_min_width, pc->custom_min_length,
2243 pc->custom_size.left, pc->custom_size.bottom,
2244 pc->custom_size.right, pc->custom_size.top);
2245
2246 /*
2247 * Media sources...
2248 */
2249
2250 if (pc->source_option)
2251 cupsFilePrintf(fp, "SourceOption %s\n", pc->source_option);
2252
2253 if (pc->num_sources > 0)
2254 {
2255 cupsFilePrintf(fp, "NumSources %d\n", pc->num_sources);
2256 for (i = pc->num_sources, map = pc->sources; i > 0; i --, map ++)
2257 cupsFilePrintf(fp, "Source %s %s\n", map->pwg, map->ppd);
2258 }
2259
2260 /*
2261 * Media types...
2262 */
2263
2264 if (pc->num_types > 0)
2265 {
2266 cupsFilePrintf(fp, "NumTypes %d\n", pc->num_types);
2267 for (i = pc->num_types, map = pc->types; i > 0; i --, map ++)
2268 cupsFilePrintf(fp, "Type %s %s\n", map->pwg, map->ppd);
2269 }
2270
2271 /*
2272 * Presets...
2273 */
2274
2275 for (i = _PWG_PRINT_COLOR_MODE_MONOCHROME; i < _PWG_PRINT_COLOR_MODE_MAX; i ++)
2276 for (j = _PWG_PRINT_QUALITY_DRAFT; j < _PWG_PRINT_QUALITY_MAX; j ++)
2277 if (pc->num_presets[i][j])
2278 {
2279 cupsFilePrintf(fp, "Preset %d %d", i, j);
2280 for (k = pc->num_presets[i][j], option = pc->presets[i][j];
2281 k > 0;
2282 k --, option ++)
2283 cupsFilePrintf(fp, " %s=%s", option->name, option->value);
2284 cupsFilePutChar(fp, '\n');
2285 }
2286
2287 /*
2288 * Duplex/sides...
2289 */
2290
2291 if (pc->sides_option)
2292 cupsFilePrintf(fp, "SidesOption %s\n", pc->sides_option);
2293
2294 if (pc->sides_1sided)
2295 cupsFilePrintf(fp, "Sides1Sided %s\n", pc->sides_1sided);
2296
2297 if (pc->sides_2sided_long)
2298 cupsFilePrintf(fp, "Sides2SidedLong %s\n", pc->sides_2sided_long);
2299
2300 if (pc->sides_2sided_short)
2301 cupsFilePrintf(fp, "Sides2SidedShort %s\n", pc->sides_2sided_short);
2302
2303 /*
2304 * Product, cupsFilter, cupsFilter2, and cupsPreFilter...
2305 */
2306
2307 if (pc->product)
2308 cupsFilePutConf(fp, "Product", pc->product);
2309
2310 for (value = (const char *)cupsArrayFirst(pc->filters);
2311 value;
2312 value = (const char *)cupsArrayNext(pc->filters))
2313 cupsFilePutConf(fp, "Filter", value);
2314
2315 for (value = (const char *)cupsArrayFirst(pc->prefilters);
2316 value;
2317 value = (const char *)cupsArrayNext(pc->prefilters))
2318 cupsFilePutConf(fp, "PreFilter", value);
2319
2320 cupsFilePrintf(fp, "SingleFile %s\n", pc->single_file ? "true" : "false");
2321
2322 /*
2323 * Finishing options...
2324 */
2325
2326 for (f = (_pwg_finishings_t *)cupsArrayFirst(pc->finishings);
2327 f;
2328 f = (_pwg_finishings_t *)cupsArrayNext(pc->finishings))
2329 {
2330 cupsFilePrintf(fp, "Finishings %d", f->value);
2331 for (i = f->num_options, option = f->options; i > 0; i --, option ++)
2332 cupsFilePrintf(fp, " %s=%s", option->name, option->value);
2333 cupsFilePutChar(fp, '\n');
2334 }
2335
2336 /*
2337 * IPP attributes, if any...
2338 */
2339
2340 if (attrs)
2341 {
2342 cupsFilePrintf(fp, "IPP " CUPS_LLFMT "\n", CUPS_LLCAST ippLength(attrs));
2343
2344 attrs->state = IPP_IDLE;
2345 ippWriteIO(fp, (ipp_iocb_t)cupsFileWrite, 1, NULL, attrs);
2346 }
2347
2348 /*
2349 * Close and return...
2350 */
2351
2352 if (cupsFileClose(fp))
2353 {
2354 unlink(newfile);
2355 return (0);
2356 }
2357
2358 unlink(filename);
2359 return (!rename(newfile, filename));
2360 }
2361
2362
2363 /*
2364 * '_pwgInputSlotForSource()' - Get the InputSlot name for the given PWG
2365 * media-source.
2366 */
2367
2368 const char * /* O - InputSlot name */
2369 _pwgInputSlotForSource(
2370 const char *media_source, /* I - PWG media-source */
2371 char *name, /* I - Name buffer */
2372 size_t namesize) /* I - Size of name buffer */
2373 {
2374 /*
2375 * Range check input...
2376 */
2377
2378 if (!media_source || !name || namesize < PPD_MAX_NAME)
2379 return (NULL);
2380
2381 if (_cups_strcasecmp(media_source, "main"))
2382 strlcpy(name, "Cassette", namesize);
2383 else if (_cups_strcasecmp(media_source, "alternate"))
2384 strlcpy(name, "Multipurpose", namesize);
2385 else if (_cups_strcasecmp(media_source, "large-capacity"))
2386 strlcpy(name, "LargeCapacity", namesize);
2387 else if (_cups_strcasecmp(media_source, "bottom"))
2388 strlcpy(name, "Lower", namesize);
2389 else if (_cups_strcasecmp(media_source, "middle"))
2390 strlcpy(name, "Middle", namesize);
2391 else if (_cups_strcasecmp(media_source, "top"))
2392 strlcpy(name, "Upper", namesize);
2393 else if (_cups_strcasecmp(media_source, "rear"))
2394 strlcpy(name, "Rear", namesize);
2395 else if (_cups_strcasecmp(media_source, "side"))
2396 strlcpy(name, "Side", namesize);
2397 else if (_cups_strcasecmp(media_source, "envelope"))
2398 strlcpy(name, "Envelope", namesize);
2399 else if (_cups_strcasecmp(media_source, "main-roll"))
2400 strlcpy(name, "Roll", namesize);
2401 else if (_cups_strcasecmp(media_source, "alternate-roll"))
2402 strlcpy(name, "Roll2", namesize);
2403 else
2404 pwg_ppdize_name(media_source, name, namesize);
2405
2406 return (name);
2407 }
2408
2409
2410 /*
2411 * '_pwgMediaTypeForType()' - Get the MediaType name for the given PWG
2412 * media-type.
2413 */
2414
2415 const char * /* O - MediaType name */
2416 _pwgMediaTypeForType(
2417 const char *media_type, /* I - PWG media-type */
2418 char *name, /* I - Name buffer */
2419 size_t namesize) /* I - Size of name buffer */
2420 {
2421 /*
2422 * Range check input...
2423 */
2424
2425 if (!media_type || !name || namesize < PPD_MAX_NAME)
2426 return (NULL);
2427
2428 if (_cups_strcasecmp(media_type, "auto"))
2429 strlcpy(name, "Auto", namesize);
2430 else if (_cups_strcasecmp(media_type, "cardstock"))
2431 strlcpy(name, "Cardstock", namesize);
2432 else if (_cups_strcasecmp(media_type, "envelope"))
2433 strlcpy(name, "Envelope", namesize);
2434 else if (_cups_strcasecmp(media_type, "photographic-glossy"))
2435 strlcpy(name, "Glossy", namesize);
2436 else if (_cups_strcasecmp(media_type, "photographic-high-gloss"))
2437 strlcpy(name, "HighGloss", namesize);
2438 else if (_cups_strcasecmp(media_type, "photographic-matte"))
2439 strlcpy(name, "Matte", namesize);
2440 else if (_cups_strcasecmp(media_type, "stationery"))
2441 strlcpy(name, "Plain", namesize);
2442 else if (_cups_strcasecmp(media_type, "stationery-coated"))
2443 strlcpy(name, "Coated", namesize);
2444 else if (_cups_strcasecmp(media_type, "stationery-inkjet"))
2445 strlcpy(name, "Inkjet", namesize);
2446 else if (_cups_strcasecmp(media_type, "stationery-letterhead"))
2447 strlcpy(name, "Letterhead", namesize);
2448 else if (_cups_strcasecmp(media_type, "stationery-preprinted"))
2449 strlcpy(name, "Preprinted", namesize);
2450 else if (_cups_strcasecmp(media_type, "transparency"))
2451 strlcpy(name, "Transparency", namesize);
2452 else
2453 pwg_ppdize_name(media_type, name, namesize);
2454
2455 return (name);
2456 }
2457
2458
2459 /*
2460 * '_pwgPageSizeForMedia()' - Get the PageSize name for the given media.
2461 */
2462
2463 const char * /* O - PageSize name */
2464 _pwgPageSizeForMedia(
2465 _pwg_media_t *media, /* I - Media */
2466 char *name, /* I - PageSize name buffer */
2467 size_t namesize) /* I - Size of name buffer */
2468 {
2469 const char *sizeptr, /* Pointer to size in PWG name */
2470 *dimptr; /* Pointer to dimensions in PWG name */
2471
2472
2473 /*
2474 * Range check input...
2475 */
2476
2477 if (!media || !name || namesize < PPD_MAX_NAME)
2478 return (NULL);
2479
2480 /*
2481 * Copy or generate a PageSize name...
2482 */
2483
2484 if (media->ppd)
2485 {
2486 /*
2487 * Use a standard Adobe name...
2488 */
2489
2490 strlcpy(name, media->ppd, namesize);
2491 }
2492 else if (!media->pwg || !strncmp(media->pwg, "custom_", 7) ||
2493 (sizeptr = strchr(media->pwg, '_')) == NULL ||
2494 (dimptr = strchr(sizeptr + 1, '_')) == NULL ||
2495 (size_t)(dimptr - sizeptr) > namesize)
2496 {
2497 /*
2498 * Use a name of the form "wNNNhNNN"...
2499 */
2500
2501 snprintf(name, namesize, "w%dh%d", (int)_PWG_TOPTS(media->width),
2502 (int)_PWG_TOPTS(media->length));
2503 }
2504 else
2505 {
2506 /*
2507 * Copy the size name from class_sizename_dimensions...
2508 */
2509
2510 memcpy(name, sizeptr + 1, dimptr - sizeptr - 1);
2511 name[dimptr - sizeptr - 1] = '\0';
2512 }
2513
2514 return (name);
2515 }
2516
2517
2518 /*
2519 * 'pwg_compare_finishings()' - Compare two finishings values.
2520 */
2521
2522 static int /* O- Result of comparison */
2523 pwg_compare_finishings(
2524 _pwg_finishings_t *a, /* I - First finishings value */
2525 _pwg_finishings_t *b) /* I - Second finishings value */
2526 {
2527 return (b->value - a->value);
2528 }
2529
2530
2531 /*
2532 * 'pwg_free_finishings()' - Free a finishings value.
2533 */
2534
2535 static void
2536 pwg_free_finishings(
2537 _pwg_finishings_t *f) /* I - Finishings value */
2538 {
2539 cupsFreeOptions(f->num_options, f->options);
2540 free(f);
2541 }
2542
2543
2544 /*
2545 * 'pwg_ppdize_name()' - Convert an IPP keyword to a PPD keyword.
2546 */
2547
2548 static void
2549 pwg_ppdize_name(const char *ipp, /* I - IPP keyword */
2550 char *name, /* I - Name buffer */
2551 size_t namesize) /* I - Size of name buffer */
2552 {
2553 char *ptr, /* Pointer into name buffer */
2554 *end; /* End of name buffer */
2555
2556
2557 *name = toupper(*ipp++);
2558
2559 for (ptr = name + 1, end = name + namesize - 1; *ipp && ptr < end;)
2560 {
2561 if (*ipp == '-' && _cups_isalpha(ipp[1]))
2562 {
2563 ipp ++;
2564 *ptr++ = toupper(*ipp++ & 255);
2565 }
2566 else
2567 *ptr++ = *ipp++;
2568 }
2569
2570 *ptr = '\0';
2571 }
2572
2573
2574 /*
2575 * 'pwg_unppdize_name()' - Convert a PPD keyword to a lowercase IPP keyword.
2576 */
2577
2578 static void
2579 pwg_unppdize_name(const char *ppd, /* I - PPD keyword */
2580 char *name, /* I - Name buffer */
2581 size_t namesize) /* I - Size of name buffer */
2582 {
2583 char *ptr, /* Pointer into name buffer */
2584 *end; /* End of name buffer */
2585
2586
2587 for (ptr = name, end = name + namesize - 1; *ppd && ptr < end; ppd ++)
2588 {
2589 if (_cups_isalnum(*ppd) || *ppd == '-')
2590 *ptr++ = tolower(*ppd & 255);
2591 else if (*ppd == '_' || *ppd == '.')
2592 *ptr++ = '-';
2593
2594 if (!_cups_isupper(*ppd) && _cups_isalnum(*ppd) &&
2595 _cups_isupper(ppd[1]) && ptr < end)
2596 *ptr++ = '-';
2597 }
2598
2599 *ptr = '\0';
2600 }
2601
2602
2603 /*
2604 * End of "$Id$".
2605 */