4 * PPD cache implementation for CUPS.
6 * Copyright 2010-2013 by Apple Inc.
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/".
14 * This file is subject to the Apple OS-Developed Software exception.
18 * _ppdCacheCreateWithFile() - Create PPD cache and mapping data from a
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
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
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
34 * _ppdCacheGetSource() - Get the PWG media-source associated with a PPD
36 * _ppdCacheGetType() - Get the PWG media-type associated with a PPD
38 * _ppdCacheWriteFile() - Write PWG mapping data to a file.
39 * _pwgInputSlotForSource() - Get the InputSlot name for the given PWG
41 * _pwgMediaTypeForType() - Get the MediaType name for the given PWG
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
50 * Include necessary headers...
53 #include "cups-private.h"
58 * Macro to test for two almost-equal PWG measurements.
61 #define _PWG_EQUIVALENT(x, y) (abs((x)-(y)) < 2)
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 const char *dashchars
);
77 * '_ppdCacheCreateWithFile()' - Create PPD cache and mapping data from a
80 * Use the @link _ppdCacheWriteFile@ function to write PWG mapping data to a
84 _ppd_cache_t
* /* O - PPD cache and mapping data */
85 _ppdCacheCreateWithFile(
86 const char *filename
, /* I - File to read */
87 ipp_t
**attrs
) /* IO - IPP attributes, if any */
89 cups_file_t
*fp
; /* File */
90 _ppd_cache_t
*pc
; /* PWG mapping data */
91 pwg_size_t
*size
; /* Current size */
92 pwg_map_t
*map
; /* Current map */
93 _pwg_finishings_t
*finishings
; /* Current finishings option */
94 int linenum
, /* Current line number */
95 num_bins
, /* Number of bins in file */
96 num_sizes
, /* Number of sizes in file */
97 num_sources
, /* Number of sources in file */
98 num_types
; /* Number of types in file */
99 char line
[2048], /* Current line */
100 *value
, /* Pointer to value in line */
101 *valueptr
, /* Pointer into value */
102 pwg_keyword
[128], /* PWG keyword */
103 ppd_keyword
[PPD_MAX_NAME
];
105 _pwg_print_color_mode_t print_color_mode
;
106 /* Print color mode for preset */
107 _pwg_print_quality_t print_quality
; /* Print quality for preset */
110 DEBUG_printf(("_ppdCacheCreateWithFile(filename=\"%s\")", filename
));
113 * Range check input...
121 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
129 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
131 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
136 * Read the first line and make sure it has "#CUPS-PPD-CACHE-version" in it...
139 if (!cupsFileGets(fp
, line
, sizeof(line
)))
141 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
142 DEBUG_puts("_ppdCacheCreateWithFile: Unable to read first line.");
147 if (strncmp(line
, "#CUPS-PPD-CACHE-", 16))
149 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
150 DEBUG_printf(("_ppdCacheCreateWithFile: Wrong first line \"%s\".", line
));
155 if (atoi(line
+ 16) != _PPD_CACHE_VERSION
)
157 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Out of date PPD cache file."), 1);
158 DEBUG_printf(("_ppdCacheCreateWithFile: Cache file has version %s, "
159 "expected %d.", line
+ 16, _PPD_CACHE_VERSION
));
165 * Allocate the mapping data structure...
168 if ((pc
= calloc(1, sizeof(_ppd_cache_t
))) == NULL
)
170 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
171 DEBUG_puts("_ppdCacheCreateWithFile: Unable to allocate _ppd_cache_t.");
175 pc
->max_copies
= 9999;
187 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
189 DEBUG_printf(("_ppdCacheCreateWithFile: line=\"%s\", value=\"%s\", "
190 "linenum=%d", line
, value
, linenum
));
194 DEBUG_printf(("_ppdCacheCreateWithFile: Missing value on line %d.",
196 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
199 else if (!_cups_strcasecmp(line
, "Filter"))
202 pc
->filters
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
203 (cups_acopy_func_t
)_cupsStrAlloc
,
204 (cups_afree_func_t
)_cupsStrFree
);
206 cupsArrayAdd(pc
->filters
, value
);
208 else if (!_cups_strcasecmp(line
, "PreFilter"))
211 pc
->prefilters
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
212 (cups_acopy_func_t
)_cupsStrAlloc
,
213 (cups_afree_func_t
)_cupsStrFree
);
215 cupsArrayAdd(pc
->prefilters
, value
);
217 else if (!_cups_strcasecmp(line
, "Product"))
219 pc
->product
= _cupsStrAlloc(value
);
221 else if (!_cups_strcasecmp(line
, "SingleFile"))
223 pc
->single_file
= !_cups_strcasecmp(value
, "true");
225 else if (!_cups_strcasecmp(line
, "IPP"))
227 off_t pos
= cupsFileTell(fp
), /* Position in file */
228 length
= strtol(value
, NULL
, 10);
229 /* Length of IPP attributes */
233 DEBUG_puts("_ppdCacheCreateWithFile: IPP listed multiple times.");
234 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
237 else if (length
<= 0)
239 DEBUG_puts("_ppdCacheCreateWithFile: Bad IPP length.");
240 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
247 * Read IPP attributes into the provided variable...
252 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
,
253 *attrs
) != IPP_STATE_DATA
)
255 DEBUG_puts("_ppdCacheCreateWithFile: Bad IPP data.");
256 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
263 * Skip the IPP data entirely...
266 cupsFileSeek(fp
, pos
+ length
);
269 if (cupsFileTell(fp
) != (pos
+ length
))
271 DEBUG_puts("_ppdCacheCreateWithFile: Bad IPP data.");
272 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
276 else if (!_cups_strcasecmp(line
, "NumBins"))
280 DEBUG_puts("_ppdCacheCreateWithFile: NumBins listed multiple times.");
281 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
285 if ((num_bins
= atoi(value
)) <= 0 || num_bins
> 65536)
287 DEBUG_printf(("_ppdCacheCreateWithFile: Bad NumBins value %d on line "
288 "%d.", num_sizes
, linenum
));
289 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
293 if ((pc
->bins
= calloc(num_bins
, sizeof(pwg_map_t
))) == NULL
)
295 DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d bins.",
297 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
301 else if (!_cups_strcasecmp(line
, "Bin"))
303 if (sscanf(value
, "%127s%40s", pwg_keyword
, ppd_keyword
) != 2)
305 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Bin on line %d.", linenum
));
306 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
310 if (pc
->num_bins
>= num_bins
)
312 DEBUG_printf(("_ppdCacheCreateWithFile: Too many Bin's on line %d.",
314 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
318 map
= pc
->bins
+ pc
->num_bins
;
319 map
->pwg
= _cupsStrAlloc(pwg_keyword
);
320 map
->ppd
= _cupsStrAlloc(ppd_keyword
);
324 else if (!_cups_strcasecmp(line
, "NumSizes"))
328 DEBUG_puts("_ppdCacheCreateWithFile: NumSizes listed multiple times.");
329 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
333 if ((num_sizes
= atoi(value
)) < 0 || num_sizes
> 65536)
335 DEBUG_printf(("_ppdCacheCreateWithFile: Bad NumSizes value %d on line "
336 "%d.", num_sizes
, linenum
));
337 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
343 if ((pc
->sizes
= calloc(num_sizes
, sizeof(pwg_size_t
))) == NULL
)
345 DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d sizes.",
347 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
352 else if (!_cups_strcasecmp(line
, "Size"))
354 if (pc
->num_sizes
>= num_sizes
)
356 DEBUG_printf(("_ppdCacheCreateWithFile: Too many Size's on line %d.",
358 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
362 size
= pc
->sizes
+ pc
->num_sizes
;
364 if (sscanf(value
, "%127s%40s%d%d%d%d%d%d", pwg_keyword
, ppd_keyword
,
365 &(size
->width
), &(size
->length
), &(size
->left
),
366 &(size
->bottom
), &(size
->right
), &(size
->top
)) != 8)
368 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Size on line %d.",
370 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
374 size
->map
.pwg
= _cupsStrAlloc(pwg_keyword
);
375 size
->map
.ppd
= _cupsStrAlloc(ppd_keyword
);
379 else if (!_cups_strcasecmp(line
, "CustomSize"))
381 if (pc
->custom_max_width
> 0)
383 DEBUG_printf(("_ppdCacheCreateWithFile: Too many CustomSize's on line "
385 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
389 if (sscanf(value
, "%d%d%d%d%d%d%d%d", &(pc
->custom_max_width
),
390 &(pc
->custom_max_length
), &(pc
->custom_min_width
),
391 &(pc
->custom_min_length
), &(pc
->custom_size
.left
),
392 &(pc
->custom_size
.bottom
), &(pc
->custom_size
.right
),
393 &(pc
->custom_size
.top
)) != 8)
395 DEBUG_printf(("_ppdCacheCreateWithFile: Bad CustomSize on line %d.",
397 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
401 pwgFormatSizeName(pwg_keyword
, sizeof(pwg_keyword
), "custom", "max",
402 pc
->custom_max_width
, pc
->custom_max_length
, NULL
);
403 pc
->custom_max_keyword
= _cupsStrAlloc(pwg_keyword
);
405 pwgFormatSizeName(pwg_keyword
, sizeof(pwg_keyword
), "custom", "min",
406 pc
->custom_min_width
, pc
->custom_min_length
, NULL
);
407 pc
->custom_min_keyword
= _cupsStrAlloc(pwg_keyword
);
409 else if (!_cups_strcasecmp(line
, "SourceOption"))
411 pc
->source_option
= _cupsStrAlloc(value
);
413 else if (!_cups_strcasecmp(line
, "NumSources"))
417 DEBUG_puts("_ppdCacheCreateWithFile: NumSources listed multiple "
419 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
423 if ((num_sources
= atoi(value
)) <= 0 || num_sources
> 65536)
425 DEBUG_printf(("_ppdCacheCreateWithFile: Bad NumSources value %d on "
426 "line %d.", num_sources
, linenum
));
427 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
431 if ((pc
->sources
= calloc(num_sources
, sizeof(pwg_map_t
))) == NULL
)
433 DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d sources.",
435 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
439 else if (!_cups_strcasecmp(line
, "Source"))
441 if (sscanf(value
, "%127s%40s", pwg_keyword
, ppd_keyword
) != 2)
443 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Source on line %d.",
445 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
449 if (pc
->num_sources
>= num_sources
)
451 DEBUG_printf(("_ppdCacheCreateWithFile: Too many Source's on line %d.",
453 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
457 map
= pc
->sources
+ pc
->num_sources
;
458 map
->pwg
= _cupsStrAlloc(pwg_keyword
);
459 map
->ppd
= _cupsStrAlloc(ppd_keyword
);
463 else if (!_cups_strcasecmp(line
, "NumTypes"))
467 DEBUG_puts("_ppdCacheCreateWithFile: NumTypes listed multiple times.");
468 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
472 if ((num_types
= atoi(value
)) <= 0 || num_types
> 65536)
474 DEBUG_printf(("_ppdCacheCreateWithFile: Bad NumTypes value %d on "
475 "line %d.", num_types
, linenum
));
476 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
480 if ((pc
->types
= calloc(num_types
, sizeof(pwg_map_t
))) == NULL
)
482 DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d types.",
484 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
488 else if (!_cups_strcasecmp(line
, "Type"))
490 if (sscanf(value
, "%127s%40s", pwg_keyword
, ppd_keyword
) != 2)
492 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Type on line %d.",
494 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
498 if (pc
->num_types
>= num_types
)
500 DEBUG_printf(("_ppdCacheCreateWithFile: Too many Type's on line %d.",
502 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
506 map
= pc
->types
+ pc
->num_types
;
507 map
->pwg
= _cupsStrAlloc(pwg_keyword
);
508 map
->ppd
= _cupsStrAlloc(ppd_keyword
);
512 else if (!_cups_strcasecmp(line
, "Preset"))
515 * Preset output-mode print-quality name=value ...
518 print_color_mode
= (_pwg_print_color_mode_t
)strtol(value
, &valueptr
, 10);
519 print_quality
= (_pwg_print_quality_t
)strtol(valueptr
, &valueptr
, 10);
521 if (print_color_mode
< _PWG_PRINT_COLOR_MODE_MONOCHROME
||
522 print_color_mode
>= _PWG_PRINT_COLOR_MODE_MAX
||
523 print_quality
< _PWG_PRINT_QUALITY_DRAFT
||
524 print_quality
>= _PWG_PRINT_QUALITY_MAX
||
525 valueptr
== value
|| !*valueptr
)
527 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Preset on line %d.",
529 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
533 pc
->num_presets
[print_color_mode
][print_quality
] =
534 cupsParseOptions(valueptr
, 0,
535 pc
->presets
[print_color_mode
] + print_quality
);
537 else if (!_cups_strcasecmp(line
, "SidesOption"))
538 pc
->sides_option
= _cupsStrAlloc(value
);
539 else if (!_cups_strcasecmp(line
, "Sides1Sided"))
540 pc
->sides_1sided
= _cupsStrAlloc(value
);
541 else if (!_cups_strcasecmp(line
, "Sides2SidedLong"))
542 pc
->sides_2sided_long
= _cupsStrAlloc(value
);
543 else if (!_cups_strcasecmp(line
, "Sides2SidedShort"))
544 pc
->sides_2sided_short
= _cupsStrAlloc(value
);
545 else if (!_cups_strcasecmp(line
, "Finishings"))
549 cupsArrayNew3((cups_array_func_t
)pwg_compare_finishings
,
551 (cups_afree_func_t
)pwg_free_finishings
);
553 if ((finishings
= calloc(1, sizeof(_pwg_finishings_t
))) == NULL
)
556 finishings
->value
= strtol(value
, &valueptr
, 10);
557 finishings
->num_options
= cupsParseOptions(valueptr
, 0,
558 &(finishings
->options
));
560 cupsArrayAdd(pc
->finishings
, finishings
);
562 else if (!_cups_strcasecmp(line
, "MaxCopies"))
563 pc
->max_copies
= atoi(value
);
564 else if (!_cups_strcasecmp(line
, "ChargeInfoURI"))
565 pc
->charge_info_uri
= _cupsStrAlloc(value
);
566 else if (!_cups_strcasecmp(line
, "JobAccountId"))
567 pc
->account_id
= !_cups_strcasecmp(value
, "true");
568 else if (!_cups_strcasecmp(line
, "JobAccountingUserId"))
569 pc
->accounting_user_id
= !_cups_strcasecmp(value
, "true");
570 else if (!_cups_strcasecmp(line
, "JobPassword"))
571 pc
->password
= _cupsStrAlloc(value
);
572 else if (!_cups_strcasecmp(line
, "Mandatory"))
575 _cupsArrayAddStrings(pc
->mandatory
, value
, ' ');
577 pc
->mandatory
= _cupsArrayNewStrings(value
, ' ');
579 else if (!_cups_strcasecmp(line
, "SupportFile"))
581 if (!pc
->support_files
)
582 pc
->support_files
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
583 (cups_acopy_func_t
)_cupsStrAlloc
,
584 (cups_afree_func_t
)_cupsStrFree
);
586 cupsArrayAdd(pc
->support_files
, value
);
590 DEBUG_printf(("_ppdCacheCreateWithFile: Unknown %s on line %d.", line
,
595 if (pc
->num_sizes
< num_sizes
)
597 DEBUG_printf(("_ppdCacheCreateWithFile: Not enough sizes (%d < %d).",
598 pc
->num_sizes
, num_sizes
));
599 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
603 if (pc
->num_sources
< num_sources
)
605 DEBUG_printf(("_ppdCacheCreateWithFile: Not enough sources (%d < %d).",
606 pc
->num_sources
, num_sources
));
607 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
611 if (pc
->num_types
< num_types
)
613 DEBUG_printf(("_ppdCacheCreateWithFile: Not enough types (%d < %d).",
614 pc
->num_types
, num_types
));
615 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
624 * If we get here the file was bad - free any data and return...
630 _ppdCacheDestroy(pc
);
643 * '_ppdCacheCreateWithPPD()' - Create PWG mapping data from a PPD file.
646 _ppd_cache_t
* /* O - PPD cache and mapping data */
647 _ppdCacheCreateWithPPD(ppd_file_t
*ppd
) /* I - PPD file */
649 int i
, j
, k
; /* Looping vars */
650 _ppd_cache_t
*pc
; /* PWG mapping data */
651 ppd_option_t
*input_slot
, /* InputSlot option */
652 *media_type
, /* MediaType option */
653 *output_bin
, /* OutputBin option */
654 *color_model
, /* ColorModel option */
655 *duplex
; /* Duplex option */
656 ppd_choice_t
*choice
; /* Current InputSlot/MediaType */
657 pwg_map_t
*map
; /* Current source/type map */
658 ppd_attr_t
*ppd_attr
; /* Current PPD preset attribute */
659 int num_options
; /* Number of preset options and props */
660 cups_option_t
*options
; /* Preset options and properties */
661 ppd_size_t
*ppd_size
; /* Current PPD size */
662 pwg_size_t
*pwg_size
; /* Current PWG size */
663 char pwg_keyword
[3 + PPD_MAX_NAME
+ 1 + 12 + 1 + 12 + 3],
664 /* PWG keyword string */
665 ppd_name
[PPD_MAX_NAME
];
666 /* Normalized PPD name */
667 const char *pwg_name
; /* Standard PWG media name */
668 pwg_media_t
*pwg_media
; /* PWG media data */
669 _pwg_print_color_mode_t pwg_print_color_mode
;
670 /* print-color-mode index */
671 _pwg_print_quality_t pwg_print_quality
;
672 /* print-quality index */
673 int similar
; /* Are the old and new size similar? */
674 pwg_size_t
*old_size
; /* Current old size */
675 int old_imageable
, /* Old imageable length in 2540ths */
676 old_borderless
, /* Old borderless state */
677 old_known_pwg
; /* Old PWG name is well-known */
678 int new_width
, /* New width in 2540ths */
679 new_length
, /* New length in 2540ths */
680 new_left
, /* New left margin in 2540ths */
681 new_bottom
, /* New bottom margin in 2540ths */
682 new_right
, /* New right margin in 2540ths */
683 new_top
, /* New top margin in 2540ths */
684 new_imageable
, /* New imageable length in 2540ths */
685 new_borderless
, /* New borderless state */
686 new_known_pwg
; /* New PWG name is well-known */
687 pwg_size_t
*new_size
; /* New size to add, if any */
688 const char *filter
; /* Current filter */
689 _pwg_finishings_t
*finishings
; /* Current finishings value */
692 DEBUG_printf(("_ppdCacheCreateWithPPD(ppd=%p)", ppd
));
695 * Range check input...
705 if ((pc
= calloc(1, sizeof(_ppd_cache_t
))) == NULL
)
707 DEBUG_puts("_ppdCacheCreateWithPPD: Unable to allocate _ppd_cache_t.");
712 * Copy and convert size data...
715 if (ppd
->num_sizes
> 0)
717 if ((pc
->sizes
= calloc(ppd
->num_sizes
, sizeof(pwg_size_t
))) == NULL
)
719 DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d "
720 "pwg_size_t's.", ppd
->num_sizes
));
724 for (i
= ppd
->num_sizes
, pwg_size
= pc
->sizes
, ppd_size
= ppd
->sizes
;
729 * Don't copy over custom size...
732 if (!_cups_strcasecmp(ppd_size
->name
, "Custom"))
736 * Convert the PPD size name to the corresponding PWG keyword name.
739 if ((pwg_media
= pwgMediaForPPD(ppd_size
->name
)) != NULL
)
742 * Standard name, do we have conflicts?
745 for (j
= 0; j
< pc
->num_sizes
; j
++)
746 if (!strcmp(pc
->sizes
[j
].map
.pwg
, pwg_media
->pwg
))
756 * Standard name and no conflicts, use it!
759 pwg_name
= pwg_media
->pwg
;
765 * Not a standard name; convert it to a PWG vendor name of the form:
767 * pp_lowerppd_WIDTHxHEIGHTuu
770 pwg_name
= pwg_keyword
;
773 pwg_unppdize_name(ppd_size
->name
, ppd_name
, sizeof(ppd_name
), "_.");
774 pwgFormatSizeName(pwg_keyword
, sizeof(pwg_keyword
), NULL
, ppd_name
,
775 PWG_FROM_POINTS(ppd_size
->width
),
776 PWG_FROM_POINTS(ppd_size
->length
), NULL
);
780 * If we have a similar paper with non-zero margins then we only want to
781 * keep it if it has a larger imageable area length. The NULL check is for
782 * dimensions that are <= 0...
785 if ((pwg_media
= pwgMediaForSize(PWG_FROM_POINTS(ppd_size
->width
),
786 PWG_FROM_POINTS(ppd_size
->length
))) == NULL
)
789 new_width
= pwg_media
->width
;
790 new_length
= pwg_media
->length
;
791 new_left
= PWG_FROM_POINTS(ppd_size
->left
);
792 new_bottom
= PWG_FROM_POINTS(ppd_size
->bottom
);
793 new_right
= PWG_FROM_POINTS(ppd_size
->width
- ppd_size
->right
);
794 new_top
= PWG_FROM_POINTS(ppd_size
->length
- ppd_size
->top
);
795 new_imageable
= new_length
- new_top
- new_bottom
;
796 new_borderless
= new_bottom
== 0 && new_top
== 0 &&
797 new_left
== 0 && new_right
== 0;
799 for (k
= pc
->num_sizes
, similar
= 0, old_size
= pc
->sizes
, new_size
= NULL
;
803 old_imageable
= old_size
->length
- old_size
->top
- old_size
->bottom
;
804 old_borderless
= old_size
->left
== 0 && old_size
->bottom
== 0 &&
805 old_size
->right
== 0 && old_size
->top
== 0;
806 old_known_pwg
= strncmp(old_size
->map
.pwg
, "oe_", 3) &&
807 strncmp(old_size
->map
.pwg
, "om_", 3);
809 similar
= old_borderless
== new_borderless
&&
810 _PWG_EQUIVALENT(old_size
->width
, new_width
) &&
811 _PWG_EQUIVALENT(old_size
->length
, new_length
);
814 (new_known_pwg
|| (!old_known_pwg
&& new_imageable
> old_imageable
)))
817 * The new paper has a larger imageable area so it could replace
818 * the older paper. Regardless of the imageable area, we always
819 * prefer the size with a well-known PWG name.
823 _cupsStrFree(old_size
->map
.ppd
);
824 _cupsStrFree(old_size
->map
.pwg
);
831 * The paper was unique enough to deserve its own entry so add it to the
835 new_size
= pwg_size
++;
845 new_size
->map
.ppd
= _cupsStrAlloc(ppd_size
->name
);
846 new_size
->map
.pwg
= _cupsStrAlloc(pwg_name
);
847 new_size
->width
= new_width
;
848 new_size
->length
= new_length
;
849 new_size
->left
= new_left
;
850 new_size
->bottom
= new_bottom
;
851 new_size
->right
= new_right
;
852 new_size
->top
= new_top
;
857 if (ppd
->variable_sizes
)
860 * Generate custom size data...
863 pwgFormatSizeName(pwg_keyword
, sizeof(pwg_keyword
), "custom", "max",
864 PWG_FROM_POINTS(ppd
->custom_max
[0]),
865 PWG_FROM_POINTS(ppd
->custom_max
[1]), NULL
);
866 pc
->custom_max_keyword
= _cupsStrAlloc(pwg_keyword
);
867 pc
->custom_max_width
= PWG_FROM_POINTS(ppd
->custom_max
[0]);
868 pc
->custom_max_length
= PWG_FROM_POINTS(ppd
->custom_max
[1]);
870 pwgFormatSizeName(pwg_keyword
, sizeof(pwg_keyword
), "custom", "min",
871 PWG_FROM_POINTS(ppd
->custom_min
[0]),
872 PWG_FROM_POINTS(ppd
->custom_min
[1]), NULL
);
873 pc
->custom_min_keyword
= _cupsStrAlloc(pwg_keyword
);
874 pc
->custom_min_width
= PWG_FROM_POINTS(ppd
->custom_min
[0]);
875 pc
->custom_min_length
= PWG_FROM_POINTS(ppd
->custom_min
[1]);
877 pc
->custom_size
.left
= PWG_FROM_POINTS(ppd
->custom_margins
[0]);
878 pc
->custom_size
.bottom
= PWG_FROM_POINTS(ppd
->custom_margins
[1]);
879 pc
->custom_size
.right
= PWG_FROM_POINTS(ppd
->custom_margins
[2]);
880 pc
->custom_size
.top
= PWG_FROM_POINTS(ppd
->custom_margins
[3]);
884 * Copy and convert InputSlot data...
887 if ((input_slot
= ppdFindOption(ppd
, "InputSlot")) == NULL
)
888 input_slot
= ppdFindOption(ppd
, "HPPaperSource");
892 pc
->source_option
= _cupsStrAlloc(input_slot
->keyword
);
894 if ((pc
->sources
= calloc(input_slot
->num_choices
,
895 sizeof(pwg_map_t
))) == NULL
)
897 DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d "
898 "pwg_map_t's for InputSlot.", input_slot
->num_choices
));
902 pc
->num_sources
= input_slot
->num_choices
;
904 for (i
= input_slot
->num_choices
, choice
= input_slot
->choices
,
907 i
--, choice
++, map
++)
909 if (!_cups_strncasecmp(choice
->choice
, "Auto", 4) ||
910 !_cups_strcasecmp(choice
->choice
, "Default"))
912 else if (!_cups_strcasecmp(choice
->choice
, "Cassette"))
914 else if (!_cups_strcasecmp(choice
->choice
, "PhotoTray"))
916 else if (!_cups_strcasecmp(choice
->choice
, "CDTray"))
918 else if (!_cups_strncasecmp(choice
->choice
, "Multipurpose", 12) ||
919 !_cups_strcasecmp(choice
->choice
, "MP") ||
920 !_cups_strcasecmp(choice
->choice
, "MPTray"))
921 pwg_name
= "by-pass-tray";
922 else if (!_cups_strcasecmp(choice
->choice
, "LargeCapacity"))
923 pwg_name
= "large-capacity";
924 else if (!_cups_strncasecmp(choice
->choice
, "Lower", 5))
926 else if (!_cups_strncasecmp(choice
->choice
, "Middle", 6))
928 else if (!_cups_strncasecmp(choice
->choice
, "Upper", 5))
930 else if (!_cups_strncasecmp(choice
->choice
, "Side", 4))
932 else if (!_cups_strcasecmp(choice
->choice
, "Roll"))
933 pwg_name
= "main-roll";
937 * Convert PPD name to lowercase...
940 pwg_name
= pwg_keyword
;
941 pwg_unppdize_name(choice
->choice
, pwg_keyword
, sizeof(pwg_keyword
),
945 map
->pwg
= _cupsStrAlloc(pwg_name
);
946 map
->ppd
= _cupsStrAlloc(choice
->choice
);
951 * Copy and convert MediaType data...
954 if ((media_type
= ppdFindOption(ppd
, "MediaType")) != NULL
)
956 if ((pc
->types
= calloc(media_type
->num_choices
,
957 sizeof(pwg_map_t
))) == NULL
)
959 DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d "
960 "pwg_map_t's for MediaType.", media_type
->num_choices
));
964 pc
->num_types
= media_type
->num_choices
;
966 for (i
= media_type
->num_choices
, choice
= media_type
->choices
,
969 i
--, choice
++, map
++)
971 if (!_cups_strncasecmp(choice
->choice
, "Auto", 4) ||
972 !_cups_strcasecmp(choice
->choice
, "Any") ||
973 !_cups_strcasecmp(choice
->choice
, "Default"))
975 else if (!_cups_strncasecmp(choice
->choice
, "Card", 4))
976 pwg_name
= "cardstock";
977 else if (!_cups_strncasecmp(choice
->choice
, "Env", 3))
978 pwg_name
= "envelope";
979 else if (!_cups_strncasecmp(choice
->choice
, "Gloss", 5))
980 pwg_name
= "photographic-glossy";
981 else if (!_cups_strcasecmp(choice
->choice
, "HighGloss"))
982 pwg_name
= "photographic-high-gloss";
983 else if (!_cups_strcasecmp(choice
->choice
, "Matte"))
984 pwg_name
= "photographic-matte";
985 else if (!_cups_strncasecmp(choice
->choice
, "Plain", 5))
986 pwg_name
= "stationery";
987 else if (!_cups_strncasecmp(choice
->choice
, "Coated", 6))
988 pwg_name
= "stationery-coated";
989 else if (!_cups_strcasecmp(choice
->choice
, "Inkjet"))
990 pwg_name
= "stationery-inkjet";
991 else if (!_cups_strcasecmp(choice
->choice
, "Letterhead"))
992 pwg_name
= "stationery-letterhead";
993 else if (!_cups_strncasecmp(choice
->choice
, "Preprint", 8))
994 pwg_name
= "stationery-preprinted";
995 else if (!_cups_strcasecmp(choice
->choice
, "Recycled"))
996 pwg_name
= "stationery-recycled";
997 else if (!_cups_strncasecmp(choice
->choice
, "Transparen", 10))
998 pwg_name
= "transparency";
1002 * Convert PPD name to lowercase...
1005 pwg_name
= pwg_keyword
;
1006 pwg_unppdize_name(choice
->choice
, pwg_keyword
, sizeof(pwg_keyword
),
1010 map
->pwg
= _cupsStrAlloc(pwg_name
);
1011 map
->ppd
= _cupsStrAlloc(choice
->choice
);
1016 * Copy and convert OutputBin data...
1019 if ((output_bin
= ppdFindOption(ppd
, "OutputBin")) != NULL
)
1021 if ((pc
->bins
= calloc(output_bin
->num_choices
,
1022 sizeof(pwg_map_t
))) == NULL
)
1024 DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d "
1025 "pwg_map_t's for OutputBin.", output_bin
->num_choices
));
1029 pc
->num_bins
= output_bin
->num_choices
;
1031 for (i
= output_bin
->num_choices
, choice
= output_bin
->choices
,
1034 i
--, choice
++, map
++)
1036 pwg_unppdize_name(choice
->choice
, pwg_keyword
, sizeof(pwg_keyword
), "_");
1038 map
->pwg
= _cupsStrAlloc(pwg_keyword
);
1039 map
->ppd
= _cupsStrAlloc(choice
->choice
);
1043 if ((ppd_attr
= ppdFindAttr(ppd
, "APPrinterPreset", NULL
)) != NULL
)
1046 * Copy and convert APPrinterPreset (output-mode + print-quality) data...
1049 const char *quality
, /* com.apple.print.preset.quality value */
1050 *output_mode
, /* com.apple.print.preset.output-mode value */
1051 *color_model_val
, /* ColorModel choice */
1052 *graphicsType
, /* com.apple.print.preset.graphicsType value */
1053 *media_front_coating
; /* com.apple.print.preset.media-front-coating value */
1057 num_options
= _ppdParseOptions(ppd_attr
->value
, 0, &options
,
1060 if ((quality
= cupsGetOption("com.apple.print.preset.quality",
1061 num_options
, options
)) != NULL
)
1064 * Get the print-quality for this preset...
1067 if (!strcmp(quality
, "low"))
1068 pwg_print_quality
= _PWG_PRINT_QUALITY_DRAFT
;
1069 else if (!strcmp(quality
, "high"))
1070 pwg_print_quality
= _PWG_PRINT_QUALITY_HIGH
;
1072 pwg_print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
1075 * Ignore graphicsType "Photo" presets that are not high quality.
1078 graphicsType
= cupsGetOption("com.apple.print.preset.graphicsType",
1079 num_options
, options
);
1081 if (pwg_print_quality
!= _PWG_PRINT_QUALITY_HIGH
&& graphicsType
&&
1082 !strcmp(graphicsType
, "Photo"))
1086 * Ignore presets for normal and draft quality where the coating
1087 * isn't "none" or "autodetect".
1090 media_front_coating
= cupsGetOption(
1091 "com.apple.print.preset.media-front-coating",
1092 num_options
, options
);
1094 if (pwg_print_quality
!= _PWG_PRINT_QUALITY_HIGH
&&
1095 media_front_coating
&&
1096 strcmp(media_front_coating
, "none") &&
1097 strcmp(media_front_coating
, "autodetect"))
1101 * Get the output mode for this preset...
1104 output_mode
= cupsGetOption("com.apple.print.preset.output-mode",
1105 num_options
, options
);
1106 color_model_val
= cupsGetOption("ColorModel", num_options
, options
);
1110 if (!strcmp(output_mode
, "monochrome"))
1111 pwg_print_color_mode
= _PWG_PRINT_COLOR_MODE_MONOCHROME
;
1113 pwg_print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
1115 else if (color_model_val
)
1117 if (!_cups_strcasecmp(color_model_val
, "Gray"))
1118 pwg_print_color_mode
= _PWG_PRINT_COLOR_MODE_MONOCHROME
;
1120 pwg_print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
1123 pwg_print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
1126 * Save the options for this combination as needed...
1129 if (!pc
->num_presets
[pwg_print_color_mode
][pwg_print_quality
])
1130 pc
->num_presets
[pwg_print_color_mode
][pwg_print_quality
] =
1131 _ppdParseOptions(ppd_attr
->value
, 0,
1132 pc
->presets
[pwg_print_color_mode
] +
1133 pwg_print_quality
, _PPD_PARSE_OPTIONS
);
1136 cupsFreeOptions(num_options
, options
);
1138 while ((ppd_attr
= ppdFindNextAttr(ppd
, "APPrinterPreset", NULL
)) != NULL
);
1141 if (!pc
->num_presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
][_PWG_PRINT_QUALITY_DRAFT
] &&
1142 !pc
->num_presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
][_PWG_PRINT_QUALITY_NORMAL
] &&
1143 !pc
->num_presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
][_PWG_PRINT_QUALITY_HIGH
])
1146 * Try adding some common color options to create grayscale presets. These
1147 * are listed in order of popularity...
1150 const char *color_option
= NULL
, /* Color control option */
1151 *gray_choice
= NULL
; /* Choice to select grayscale */
1153 if ((color_model
= ppdFindOption(ppd
, "ColorModel")) != NULL
&&
1154 ppdFindChoice(color_model
, "Gray"))
1156 color_option
= "ColorModel";
1157 gray_choice
= "Gray";
1159 else if ((color_model
= ppdFindOption(ppd
, "HPColorMode")) != NULL
&&
1160 ppdFindChoice(color_model
, "grayscale"))
1162 color_option
= "HPColorMode";
1163 gray_choice
= "grayscale";
1165 else if ((color_model
= ppdFindOption(ppd
, "BRMonoColor")) != NULL
&&
1166 ppdFindChoice(color_model
, "Mono"))
1168 color_option
= "BRMonoColor";
1169 gray_choice
= "Mono";
1171 else if ((color_model
= ppdFindOption(ppd
, "CNIJSGrayScale")) != NULL
&&
1172 ppdFindChoice(color_model
, "1"))
1174 color_option
= "CNIJSGrayScale";
1177 else if ((color_model
= ppdFindOption(ppd
, "HPColorAsGray")) != NULL
&&
1178 ppdFindChoice(color_model
, "True"))
1180 color_option
= "HPColorAsGray";
1181 gray_choice
= "True";
1184 if (color_option
&& gray_choice
)
1187 * Copy and convert ColorModel (output-mode) data...
1190 cups_option_t
*coption
, /* Color option */
1191 *moption
; /* Monochrome option */
1193 for (pwg_print_quality
= _PWG_PRINT_QUALITY_DRAFT
;
1194 pwg_print_quality
< _PWG_PRINT_QUALITY_MAX
;
1195 pwg_print_quality
++)
1197 if (pc
->num_presets
[_PWG_PRINT_COLOR_MODE_COLOR
][pwg_print_quality
])
1200 * Copy the color options...
1203 num_options
= pc
->num_presets
[_PWG_PRINT_COLOR_MODE_COLOR
]
1204 [pwg_print_quality
];
1205 options
= calloc(sizeof(cups_option_t
), num_options
);
1209 for (i
= num_options
, moption
= options
,
1210 coption
= pc
->presets
[_PWG_PRINT_COLOR_MODE_COLOR
]
1211 [pwg_print_quality
];
1213 i
--, moption
++, coption
++)
1215 moption
->name
= _cupsStrRetain(coption
->name
);
1216 moption
->value
= _cupsStrRetain(coption
->value
);
1219 pc
->num_presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
][pwg_print_quality
] =
1221 pc
->presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
][pwg_print_quality
] =
1225 else if (pwg_print_quality
!= _PWG_PRINT_QUALITY_NORMAL
)
1229 * Add the grayscale option to the preset...
1232 pc
->num_presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
][pwg_print_quality
] =
1233 cupsAddOption(color_option
, gray_choice
,
1234 pc
->num_presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
]
1235 [pwg_print_quality
],
1236 pc
->presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
] +
1243 * Copy and convert Duplex (sides) data...
1246 if ((duplex
= ppdFindOption(ppd
, "Duplex")) == NULL
)
1247 if ((duplex
= ppdFindOption(ppd
, "JCLDuplex")) == NULL
)
1248 if ((duplex
= ppdFindOption(ppd
, "EFDuplex")) == NULL
)
1249 if ((duplex
= ppdFindOption(ppd
, "EFDuplexing")) == NULL
)
1250 duplex
= ppdFindOption(ppd
, "KD03Duplex");
1254 pc
->sides_option
= _cupsStrAlloc(duplex
->keyword
);
1256 for (i
= duplex
->num_choices
, choice
= duplex
->choices
;
1260 if ((!_cups_strcasecmp(choice
->choice
, "None") ||
1261 !_cups_strcasecmp(choice
->choice
, "False")) && !pc
->sides_1sided
)
1262 pc
->sides_1sided
= _cupsStrAlloc(choice
->choice
);
1263 else if ((!_cups_strcasecmp(choice
->choice
, "DuplexNoTumble") ||
1264 !_cups_strcasecmp(choice
->choice
, "LongEdge") ||
1265 !_cups_strcasecmp(choice
->choice
, "Top")) && !pc
->sides_2sided_long
)
1266 pc
->sides_2sided_long
= _cupsStrAlloc(choice
->choice
);
1267 else if ((!_cups_strcasecmp(choice
->choice
, "DuplexTumble") ||
1268 !_cups_strcasecmp(choice
->choice
, "ShortEdge") ||
1269 !_cups_strcasecmp(choice
->choice
, "Bottom")) &&
1270 !pc
->sides_2sided_short
)
1271 pc
->sides_2sided_short
= _cupsStrAlloc(choice
->choice
);
1276 * Copy filters and pre-filters...
1279 pc
->filters
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
1280 (cups_acopy_func_t
)_cupsStrAlloc
,
1281 (cups_afree_func_t
)_cupsStrFree
);
1283 cupsArrayAdd(pc
->filters
,
1284 "application/vnd.cups-raw application/octet-stream 0 -");
1286 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsFilter2", NULL
)) != NULL
)
1290 cupsArrayAdd(pc
->filters
, ppd_attr
->value
);
1292 while ((ppd_attr
= ppdFindNextAttr(ppd
, "cupsFilter2", NULL
)) != NULL
);
1294 else if (ppd
->num_filters
> 0)
1296 for (i
= 0; i
< ppd
->num_filters
; i
++)
1297 cupsArrayAdd(pc
->filters
, ppd
->filters
[i
]);
1300 cupsArrayAdd(pc
->filters
, "application/vnd.cups-postscript 0 -");
1303 * See if we have a command filter...
1306 for (filter
= (const char *)cupsArrayFirst(pc
->filters
);
1308 filter
= (const char *)cupsArrayNext(pc
->filters
))
1309 if (!_cups_strncasecmp(filter
, "application/vnd.cups-command", 28) &&
1310 _cups_isspace(filter
[28]))
1314 ((ppd_attr
= ppdFindAttr(ppd
, "cupsCommands", NULL
)) == NULL
||
1315 _cups_strcasecmp(ppd_attr
->value
, "none")))
1318 * No command filter and no cupsCommands keyword telling us not to use one.
1319 * See if this is a PostScript printer, and if so add a PostScript command
1323 for (filter
= (const char *)cupsArrayFirst(pc
->filters
);
1325 filter
= (const char *)cupsArrayNext(pc
->filters
))
1326 if (!_cups_strncasecmp(filter
, "application/vnd.cups-postscript", 31) &&
1327 _cups_isspace(filter
[31]))
1331 cupsArrayAdd(pc
->filters
,
1332 "application/vnd.cups-command application/postscript 100 "
1336 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsPreFilter", NULL
)) != NULL
)
1338 pc
->prefilters
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
1339 (cups_acopy_func_t
)_cupsStrAlloc
,
1340 (cups_afree_func_t
)_cupsStrFree
);
1344 cupsArrayAdd(pc
->prefilters
, ppd_attr
->value
);
1346 while ((ppd_attr
= ppdFindNextAttr(ppd
, "cupsPreFilter", NULL
)) != NULL
);
1349 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsSingleFile", NULL
)) != NULL
)
1350 pc
->single_file
= !_cups_strcasecmp(ppd_attr
->value
, "true");
1353 * Copy the product string, if any...
1357 pc
->product
= _cupsStrAlloc(ppd
->product
);
1360 * Copy finishings mapping data...
1363 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsIPPFinishings", NULL
)) != NULL
)
1365 pc
->finishings
= cupsArrayNew3((cups_array_func_t
)pwg_compare_finishings
,
1366 NULL
, NULL
, 0, NULL
,
1367 (cups_afree_func_t
)pwg_free_finishings
);
1371 if ((finishings
= calloc(1, sizeof(_pwg_finishings_t
))) == NULL
)
1374 finishings
->value
= atoi(ppd_attr
->spec
);
1375 finishings
->num_options
= _ppdParseOptions(ppd_attr
->value
, 0,
1376 &(finishings
->options
),
1377 _PPD_PARSE_OPTIONS
);
1379 cupsArrayAdd(pc
->finishings
, finishings
);
1381 while ((ppd_attr
= ppdFindNextAttr(ppd
, "cupsIPPFinishings",
1389 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsMaxCopies", NULL
)) != NULL
)
1390 pc
->max_copies
= atoi(ppd_attr
->value
);
1391 else if (ppd
->manual_copies
)
1394 pc
->max_copies
= 9999;
1397 * cupsChargeInfoURI, cupsJobAccountId, cupsJobAccountingUserId,
1398 * cupsJobPassword, and cupsMandatory.
1401 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsChargeInfoURI", NULL
)) != NULL
)
1402 pc
->charge_info_uri
= _cupsStrAlloc(ppd_attr
->value
);
1404 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsJobAccountId", NULL
)) != NULL
)
1405 pc
->account_id
= !_cups_strcasecmp(ppd_attr
->value
, "true");
1407 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsJobAccountingUserId", NULL
)) != NULL
)
1408 pc
->accounting_user_id
= !_cups_strcasecmp(ppd_attr
->value
, "true");
1410 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsJobPassword", NULL
)) != NULL
)
1411 pc
->password
= _cupsStrAlloc(ppd_attr
->value
);
1413 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsMandatory", NULL
)) != NULL
)
1414 pc
->mandatory
= _cupsArrayNewStrings(ppd_attr
->value
, ' ');
1420 pc
->support_files
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
1421 (cups_acopy_func_t
)_cupsStrAlloc
,
1422 (cups_afree_func_t
)_cupsStrFree
);
1424 for (ppd_attr
= ppdFindAttr(ppd
, "cupsICCProfile", NULL
);
1426 ppd_attr
= ppdFindNextAttr(ppd
, "cupsICCProfile", NULL
))
1427 cupsArrayAdd(pc
->support_files
, ppd_attr
->value
);
1429 if ((ppd_attr
= ppdFindAttr(ppd
, "APPrinterIconPath", NULL
)) != NULL
)
1430 cupsArrayAdd(pc
->support_files
, ppd_attr
->value
);
1433 * Return the cache data...
1439 * If we get here we need to destroy the PWG mapping data and return NULL...
1444 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Out of memory."), 1);
1445 _ppdCacheDestroy(pc
);
1452 * '_ppdCacheDestroy()' - Free all memory used for PWG mapping data.
1456 _ppdCacheDestroy(_ppd_cache_t
*pc
) /* I - PPD cache and mapping data */
1458 int i
; /* Looping var */
1459 pwg_map_t
*map
; /* Current map */
1460 pwg_size_t
*size
; /* Current size */
1464 * Range check input...
1471 * Free memory as needed...
1476 for (i
= pc
->num_bins
, map
= pc
->bins
; i
> 0; i
--, map
++)
1478 _cupsStrFree(map
->pwg
);
1479 _cupsStrFree(map
->ppd
);
1487 for (i
= pc
->num_sizes
, size
= pc
->sizes
; i
> 0; i
--, size
++)
1489 _cupsStrFree(size
->map
.pwg
);
1490 _cupsStrFree(size
->map
.ppd
);
1496 if (pc
->source_option
)
1497 _cupsStrFree(pc
->source_option
);
1501 for (i
= pc
->num_sources
, map
= pc
->sources
; i
> 0; i
--, map
++)
1503 _cupsStrFree(map
->pwg
);
1504 _cupsStrFree(map
->ppd
);
1512 for (i
= pc
->num_types
, map
= pc
->types
; i
> 0; i
--, map
++)
1514 _cupsStrFree(map
->pwg
);
1515 _cupsStrFree(map
->ppd
);
1521 if (pc
->custom_max_keyword
)
1522 _cupsStrFree(pc
->custom_max_keyword
);
1524 if (pc
->custom_min_keyword
)
1525 _cupsStrFree(pc
->custom_min_keyword
);
1527 _cupsStrFree(pc
->product
);
1528 cupsArrayDelete(pc
->filters
);
1529 cupsArrayDelete(pc
->prefilters
);
1530 cupsArrayDelete(pc
->finishings
);
1532 _cupsStrFree(pc
->charge_info_uri
);
1533 _cupsStrFree(pc
->password
);
1535 cupsArrayDelete(pc
->mandatory
);
1537 cupsArrayDelete(pc
->support_files
);
1544 * '_ppdCacheGetBin()' - Get the PWG output-bin keyword associated with a PPD
1548 const char * /* O - output-bin or NULL */
1550 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
1551 const char *output_bin
) /* I - PPD OutputBin string */
1553 int i
; /* Looping var */
1557 * Range check input...
1560 if (!pc
|| !output_bin
)
1564 * Look up the OutputBin string...
1568 for (i
= 0; i
< pc
->num_bins
; i
++)
1569 if (!_cups_strcasecmp(output_bin
, pc
->bins
[i
].ppd
))
1570 return (pc
->bins
[i
].pwg
);
1577 * '_ppdCacheGetFinishingOptions()' - Get PPD finishing options for the given
1578 * IPP finishings value(s).
1581 int /* O - New number of options */
1582 _ppdCacheGetFinishingOptions(
1583 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
1584 ipp_t
*job
, /* I - Job attributes or NULL */
1585 ipp_finishings_t value
, /* I - IPP finishings value of IPP_FINISHINGS_NONE */
1586 int num_options
, /* I - Number of options */
1587 cups_option_t
**options
) /* IO - Options */
1589 int i
; /* Looping var */
1590 _pwg_finishings_t
*f
, /* PWG finishings options */
1591 key
; /* Search key */
1592 ipp_attribute_t
*attr
; /* Finishings attribute */
1593 cups_option_t
*option
; /* Current finishings option */
1597 * Range check input...
1600 if (!pc
|| cupsArrayCount(pc
->finishings
) == 0 || !options
||
1601 (!job
&& value
== IPP_FINISHINGS_NONE
))
1602 return (num_options
);
1605 * Apply finishing options...
1608 if (job
&& (attr
= ippFindAttribute(job
, "finishings", IPP_TAG_ENUM
)) != NULL
)
1610 int num_values
= ippGetCount(attr
); /* Number of values */
1612 for (i
= 0; i
< num_values
; i
++)
1614 key
.value
= ippGetInteger(attr
, i
);
1616 if ((f
= cupsArrayFind(pc
->finishings
, &key
)) != NULL
)
1618 int j
; /* Another looping var */
1620 for (j
= f
->num_options
, option
= f
->options
; j
> 0; j
--, option
++)
1621 num_options
= cupsAddOption(option
->name
, option
->value
,
1622 num_options
, options
);
1626 else if (value
!= IPP_FINISHINGS_NONE
)
1630 if ((f
= cupsArrayFind(pc
->finishings
, &key
)) != NULL
)
1632 int j
; /* Another looping var */
1634 for (j
= f
->num_options
, option
= f
->options
; j
> 0; j
--, option
++)
1635 num_options
= cupsAddOption(option
->name
, option
->value
,
1636 num_options
, options
);
1640 return (num_options
);
1645 * '_ppdCacheGetFinishingValues()' - Get IPP finishings value(s) from the given
1649 int /* O - Number of finishings values */
1650 _ppdCacheGetFinishingValues(
1651 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
1652 int num_options
, /* I - Number of options */
1653 cups_option_t
*options
, /* I - Options */
1654 int max_values
, /* I - Maximum number of finishings values */
1655 int *values
) /* O - Finishings values */
1657 int i
, /* Looping var */
1658 num_values
= 0; /* Number of values */
1659 _pwg_finishings_t
*f
; /* Current finishings option */
1660 cups_option_t
*option
; /* Current option */
1661 const char *val
; /* Value for option */
1665 * Range check input...
1668 if (!pc
|| !pc
->finishings
|| num_options
< 1 || max_values
< 1 || !values
)
1672 * Go through the finishings options and see what is set...
1675 for (f
= (_pwg_finishings_t
*)cupsArrayFirst(pc
->finishings
);
1677 f
= (_pwg_finishings_t
*)cupsArrayNext(pc
->finishings
))
1679 for (i
= f
->num_options
, option
= f
->options
; i
> 0; i
--, option
++)
1680 if ((val
= cupsGetOption(option
->name
, num_options
, options
)) == NULL
||
1681 _cups_strcasecmp(option
->value
, val
))
1686 values
[num_values
++] = f
->value
;
1688 if (num_values
>= max_values
)
1693 return (num_values
);
1698 * '_ppdCacheGetInputSlot()' - Get the PPD InputSlot associated with the job
1699 * attributes or a keyword string.
1702 const char * /* O - PPD InputSlot or NULL */
1703 _ppdCacheGetInputSlot(
1704 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
1705 ipp_t
*job
, /* I - Job attributes or NULL */
1706 const char *keyword
) /* I - Keyword string or NULL */
1709 * Range check input...
1712 if (!pc
|| pc
->num_sources
== 0 || (!job
&& !keyword
))
1715 if (job
&& !keyword
)
1718 * Lookup the media-col attribute and any media-source found there...
1721 ipp_attribute_t
*media_col
, /* media-col attribute */
1722 *media_source
; /* media-source attribute */
1723 pwg_size_t size
; /* Dimensional size */
1724 int margins_set
; /* Were the margins set? */
1726 media_col
= ippFindAttribute(job
, "media-col", IPP_TAG_BEGIN_COLLECTION
);
1728 (media_source
= ippFindAttribute(ippGetCollection(media_col
, 0),
1730 IPP_TAG_KEYWORD
)) != NULL
)
1733 * Use the media-source value from media-col...
1736 keyword
= ippGetString(media_source
, 0, NULL
);
1738 else if (pwgInitSize(&size
, job
, &margins_set
))
1741 * For media <= 5x7, look for a photo tray...
1744 if (size
.width
<= (5 * 2540) && size
.length
<= (7 * 2540))
1751 int i
; /* Looping var */
1753 for (i
= 0; i
< pc
->num_sources
; i
++)
1754 if (!_cups_strcasecmp(keyword
, pc
->sources
[i
].pwg
))
1755 return (pc
->sources
[i
].ppd
);
1763 * '_ppdCacheGetMediaType()' - Get the PPD MediaType associated with the job
1764 * attributes or a keyword string.
1767 const char * /* O - PPD MediaType or NULL */
1768 _ppdCacheGetMediaType(
1769 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
1770 ipp_t
*job
, /* I - Job attributes or NULL */
1771 const char *keyword
) /* I - Keyword string or NULL */
1774 * Range check input...
1777 if (!pc
|| pc
->num_types
== 0 || (!job
&& !keyword
))
1780 if (job
&& !keyword
)
1783 * Lookup the media-col attribute and any media-source found there...
1786 ipp_attribute_t
*media_col
, /* media-col attribute */
1787 *media_type
; /* media-type attribute */
1789 media_col
= ippFindAttribute(job
, "media-col", IPP_TAG_BEGIN_COLLECTION
);
1792 if ((media_type
= ippFindAttribute(media_col
->values
[0].collection
,
1794 IPP_TAG_KEYWORD
)) == NULL
)
1795 media_type
= ippFindAttribute(media_col
->values
[0].collection
,
1796 "media-type", IPP_TAG_NAME
);
1799 keyword
= media_type
->values
[0].string
.text
;
1805 int i
; /* Looping var */
1807 for (i
= 0; i
< pc
->num_types
; i
++)
1808 if (!_cups_strcasecmp(keyword
, pc
->types
[i
].pwg
))
1809 return (pc
->types
[i
].ppd
);
1817 * '_ppdCacheGetOutputBin()' - Get the PPD OutputBin associated with the keyword
1821 const char * /* O - PPD OutputBin or NULL */
1822 _ppdCacheGetOutputBin(
1823 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
1824 const char *output_bin
) /* I - Keyword string */
1826 int i
; /* Looping var */
1830 * Range check input...
1833 if (!pc
|| !output_bin
)
1837 * Look up the OutputBin string...
1841 for (i
= 0; i
< pc
->num_bins
; i
++)
1842 if (!_cups_strcasecmp(output_bin
, pc
->bins
[i
].pwg
))
1843 return (pc
->bins
[i
].ppd
);
1850 * '_ppdCacheGetPageSize()' - Get the PPD PageSize associated with the job
1851 * attributes or a keyword string.
1854 const char * /* O - PPD PageSize or NULL */
1855 _ppdCacheGetPageSize(
1856 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
1857 ipp_t
*job
, /* I - Job attributes or NULL */
1858 const char *keyword
, /* I - Keyword string or NULL */
1859 int *exact
) /* O - 1 if exact match, 0 otherwise */
1861 int i
; /* Looping var */
1862 pwg_size_t
*size
, /* Current size */
1863 *closest
, /* Closest size */
1864 jobsize
; /* Size data from job */
1865 int margins_set
, /* Were the margins set? */
1866 dwidth
, /* Difference in width */
1867 dlength
, /* Difference in length */
1868 dleft
, /* Difference in left margins */
1869 dright
, /* Difference in right margins */
1870 dbottom
, /* Difference in bottom margins */
1871 dtop
, /* Difference in top margins */
1872 dmin
, /* Minimum difference */
1873 dclosest
; /* Closest difference */
1874 const char *ppd_name
; /* PPD media name */
1877 DEBUG_printf(("_ppdCacheGetPageSize(pc=%p, job=%p, keyword=\"%s\", exact=%p)",
1878 pc
, job
, keyword
, exact
));
1881 * Range check input...
1884 if (!pc
|| (!job
&& !keyword
))
1895 * Try getting the PPD media name from the job attributes...
1898 ipp_attribute_t
*attr
; /* Job attribute */
1900 if ((attr
= ippFindAttribute(job
, "PageSize", IPP_TAG_ZERO
)) == NULL
)
1901 if ((attr
= ippFindAttribute(job
, "PageRegion", IPP_TAG_ZERO
)) == NULL
)
1902 attr
= ippFindAttribute(job
, "media", IPP_TAG_ZERO
);
1906 DEBUG_printf(("1_ppdCacheGetPageSize: Found attribute %s (%s)",
1907 attr
->name
, ippTagString(attr
->value_tag
)));
1909 DEBUG_puts("1_ppdCacheGetPageSize: Did not find media attribute.");
1912 if (attr
&& (attr
->value_tag
== IPP_TAG_NAME
||
1913 attr
->value_tag
== IPP_TAG_KEYWORD
))
1914 ppd_name
= attr
->values
[0].string
.text
;
1917 DEBUG_printf(("1_ppdCacheGetPageSize: ppd_name=\"%s\"", ppd_name
));
1922 * Try looking up the named PPD size first...
1925 for (i
= pc
->num_sizes
, size
= pc
->sizes
; i
> 0; i
--, size
++)
1927 DEBUG_printf(("2_ppdCacheGetPageSize: size[%d]=[\"%s\" \"%s\"]",
1928 (int)(size
- pc
->sizes
), size
->map
.pwg
, size
->map
.ppd
));
1930 if (!_cups_strcasecmp(ppd_name
, size
->map
.ppd
) ||
1931 !_cups_strcasecmp(ppd_name
, size
->map
.pwg
))
1936 DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\"", ppd_name
));
1938 return (size
->map
.ppd
);
1943 if (job
&& !keyword
)
1946 * Get the size using media-col or media, with the preference being
1950 if (!pwgInitSize(&jobsize
, job
, &margins_set
))
1956 * Get the size using a media keyword...
1959 pwg_media_t
*media
; /* Media definition */
1962 if ((media
= pwgMediaForPWG(keyword
)) == NULL
)
1963 if ((media
= pwgMediaForLegacy(keyword
)) == NULL
)
1964 if ((media
= pwgMediaForPPD(keyword
)) == NULL
)
1967 jobsize
.width
= media
->width
;
1968 jobsize
.length
= media
->length
;
1973 * Now that we have the dimensions and possibly the margins, look at the
1974 * available sizes and find the match...
1978 dclosest
= 999999999;
1980 if (!ppd_name
|| _cups_strncasecmp(ppd_name
, "Custom.", 7) ||
1981 _cups_strncasecmp(ppd_name
, "custom_", 7))
1983 for (i
= pc
->num_sizes
, size
= pc
->sizes
; i
> 0; i
--, size
++)
1986 * Adobe uses a size matching algorithm with an epsilon of 5 points, which
1987 * is just about 176/2540ths...
1990 dwidth
= size
->width
- jobsize
.width
;
1991 dlength
= size
->length
- jobsize
.length
;
1993 if (dwidth
<= -176 || dwidth
>= 176 || dlength
<= -176 || dlength
>= 176)
1999 * Use a tighter epsilon of 1 point (35/2540ths) for margins...
2002 dleft
= size
->left
- jobsize
.left
;
2003 dright
= size
->right
- jobsize
.right
;
2004 dtop
= size
->top
- jobsize
.top
;
2005 dbottom
= size
->bottom
- jobsize
.bottom
;
2007 if (dleft
<= -35 || dleft
>= 35 || dright
<= -35 || dright
>= 35 ||
2008 dtop
<= -35 || dtop
>= 35 || dbottom
<= -35 || dbottom
>= 35)
2010 dleft
= dleft
< 0 ? -dleft
: dleft
;
2011 dright
= dright
< 0 ? -dright
: dright
;
2012 dbottom
= dbottom
< 0 ? -dbottom
: dbottom
;
2013 dtop
= dtop
< 0 ? -dtop
: dtop
;
2014 dmin
= dleft
+ dright
+ dbottom
+ dtop
;
2016 if (dmin
< dclosest
)
2029 DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\"", size
->map
.ppd
));
2031 return (size
->map
.ppd
);
2037 DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\" (closest)",
2040 return (closest
->map
.ppd
);
2044 * If we get here we need to check for custom page size support...
2047 if (jobsize
.width
>= pc
->custom_min_width
&&
2048 jobsize
.width
<= pc
->custom_max_width
&&
2049 jobsize
.length
>= pc
->custom_min_length
&&
2050 jobsize
.length
<= pc
->custom_max_length
)
2053 * In range, format as Custom.WWWWxLLLL (points).
2056 snprintf(pc
->custom_ppd_size
, sizeof(pc
->custom_ppd_size
), "Custom.%dx%d",
2057 (int)PWG_TO_POINTS(jobsize
.width
), (int)PWG_TO_POINTS(jobsize
.length
));
2059 if (margins_set
&& exact
)
2061 dleft
= pc
->custom_size
.left
- jobsize
.left
;
2062 dright
= pc
->custom_size
.right
- jobsize
.right
;
2063 dtop
= pc
->custom_size
.top
- jobsize
.top
;
2064 dbottom
= pc
->custom_size
.bottom
- jobsize
.bottom
;
2066 if (dleft
> -35 && dleft
< 35 && dright
> -35 && dright
< 35 &&
2067 dtop
> -35 && dtop
< 35 && dbottom
> -35 && dbottom
< 35)
2073 DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\" (custom)",
2074 pc
->custom_ppd_size
));
2076 return (pc
->custom_ppd_size
);
2080 * No custom page size support or the size is out of range - return NULL.
2083 DEBUG_puts("1_ppdCacheGetPageSize: Returning NULL");
2090 * '_ppdCacheGetSize()' - Get the PWG size associated with a PPD PageSize.
2093 pwg_size_t
* /* O - PWG size or NULL */
2095 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2096 const char *page_size
) /* I - PPD PageSize */
2098 int i
; /* Looping var */
2099 pwg_media_t
*media
; /* Media */
2100 pwg_size_t
*size
; /* Current size */
2104 * Range check input...
2107 if (!pc
|| !page_size
)
2110 if (!_cups_strncasecmp(page_size
, "Custom.", 7))
2113 * Custom size; size name can be one of the following:
2115 * Custom.WIDTHxLENGTHin - Size in inches
2116 * Custom.WIDTHxLENGTHft - Size in feet
2117 * Custom.WIDTHxLENGTHcm - Size in centimeters
2118 * Custom.WIDTHxLENGTHmm - Size in millimeters
2119 * Custom.WIDTHxLENGTHm - Size in meters
2120 * Custom.WIDTHxLENGTH[pt] - Size in points
2123 double w
, l
; /* Width and length of page */
2124 char *ptr
; /* Pointer into PageSize */
2125 struct lconv
*loc
; /* Locale data */
2128 w
= (float)_cupsStrScand(page_size
+ 7, &ptr
, loc
);
2129 if (!ptr
|| *ptr
!= 'x')
2132 l
= (float)_cupsStrScand(ptr
+ 1, &ptr
, loc
);
2136 if (!_cups_strcasecmp(ptr
, "in"))
2141 else if (!_cups_strcasecmp(ptr
, "ft"))
2146 else if (!_cups_strcasecmp(ptr
, "mm"))
2151 else if (!_cups_strcasecmp(ptr
, "cm"))
2156 else if (!_cups_strcasecmp(ptr
, "m"))
2167 pc
->custom_size
.width
= (int)w
;
2168 pc
->custom_size
.length
= (int)l
;
2170 return (&(pc
->custom_size
));
2174 * Not a custom size - look it up...
2177 for (i
= pc
->num_sizes
, size
= pc
->sizes
; i
> 0; i
--, size
++)
2178 if (!_cups_strcasecmp(page_size
, size
->map
.ppd
) ||
2179 !_cups_strcasecmp(page_size
, size
->map
.pwg
))
2183 * Look up standard sizes...
2186 if ((media
= pwgMediaForPPD(page_size
)) == NULL
)
2187 if ((media
= pwgMediaForLegacy(page_size
)) == NULL
)
2188 media
= pwgMediaForPWG(page_size
);
2192 pc
->custom_size
.width
= media
->width
;
2193 pc
->custom_size
.length
= media
->length
;
2195 return (&(pc
->custom_size
));
2203 * '_ppdCacheGetSource()' - Get the PWG media-source associated with a PPD
2207 const char * /* O - PWG media-source keyword */
2209 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2210 const char *input_slot
) /* I - PPD InputSlot */
2212 int i
; /* Looping var */
2213 pwg_map_t
*source
; /* Current source */
2217 * Range check input...
2220 if (!pc
|| !input_slot
)
2223 for (i
= pc
->num_sources
, source
= pc
->sources
; i
> 0; i
--, source
++)
2224 if (!_cups_strcasecmp(input_slot
, source
->ppd
))
2225 return (source
->pwg
);
2232 * '_ppdCacheGetType()' - Get the PWG media-type associated with a PPD
2236 const char * /* O - PWG media-type keyword */
2238 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2239 const char *media_type
) /* I - PPD MediaType */
2241 int i
; /* Looping var */
2242 pwg_map_t
*type
; /* Current type */
2246 * Range check input...
2249 if (!pc
|| !media_type
)
2252 for (i
= pc
->num_types
, type
= pc
->types
; i
> 0; i
--, type
++)
2253 if (!_cups_strcasecmp(media_type
, type
->ppd
))
2261 * '_ppdCacheWriteFile()' - Write PWG mapping data to a file.
2264 int /* O - 1 on success, 0 on failure */
2266 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2267 const char *filename
, /* I - File to write */
2268 ipp_t
*attrs
) /* I - Attributes to write, if any */
2270 int i
, j
, k
; /* Looping vars */
2271 cups_file_t
*fp
; /* Output file */
2272 pwg_size_t
*size
; /* Current size */
2273 pwg_map_t
*map
; /* Current map */
2274 _pwg_finishings_t
*f
; /* Current finishing option */
2275 cups_option_t
*option
; /* Current option */
2276 const char *value
; /* Filter/pre-filter value */
2277 char newfile
[1024]; /* New filename */
2281 * Range check input...
2284 if (!pc
|| !filename
)
2286 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
2291 * Open the file and write with compression...
2294 snprintf(newfile
, sizeof(newfile
), "%s.N", filename
);
2295 if ((fp
= cupsFileOpen(newfile
, "w9")) == NULL
)
2297 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
2302 * Standard header...
2305 cupsFilePrintf(fp
, "#CUPS-PPD-CACHE-%d\n", _PPD_CACHE_VERSION
);
2311 if (pc
->num_bins
> 0)
2313 cupsFilePrintf(fp
, "NumBins %d\n", pc
->num_bins
);
2314 for (i
= pc
->num_bins
, map
= pc
->bins
; i
> 0; i
--, map
++)
2315 cupsFilePrintf(fp
, "Bin %s %s\n", map
->pwg
, map
->ppd
);
2322 cupsFilePrintf(fp
, "NumSizes %d\n", pc
->num_sizes
);
2323 for (i
= pc
->num_sizes
, size
= pc
->sizes
; i
> 0; i
--, size
++)
2324 cupsFilePrintf(fp
, "Size %s %s %d %d %d %d %d %d\n", size
->map
.pwg
,
2325 size
->map
.ppd
, size
->width
, size
->length
, size
->left
,
2326 size
->bottom
, size
->right
, size
->top
);
2327 if (pc
->custom_max_width
> 0)
2328 cupsFilePrintf(fp
, "CustomSize %d %d %d %d %d %d %d %d\n",
2329 pc
->custom_max_width
, pc
->custom_max_length
,
2330 pc
->custom_min_width
, pc
->custom_min_length
,
2331 pc
->custom_size
.left
, pc
->custom_size
.bottom
,
2332 pc
->custom_size
.right
, pc
->custom_size
.top
);
2338 if (pc
->source_option
)
2339 cupsFilePrintf(fp
, "SourceOption %s\n", pc
->source_option
);
2341 if (pc
->num_sources
> 0)
2343 cupsFilePrintf(fp
, "NumSources %d\n", pc
->num_sources
);
2344 for (i
= pc
->num_sources
, map
= pc
->sources
; i
> 0; i
--, map
++)
2345 cupsFilePrintf(fp
, "Source %s %s\n", map
->pwg
, map
->ppd
);
2352 if (pc
->num_types
> 0)
2354 cupsFilePrintf(fp
, "NumTypes %d\n", pc
->num_types
);
2355 for (i
= pc
->num_types
, map
= pc
->types
; i
> 0; i
--, map
++)
2356 cupsFilePrintf(fp
, "Type %s %s\n", map
->pwg
, map
->ppd
);
2363 for (i
= _PWG_PRINT_COLOR_MODE_MONOCHROME
; i
< _PWG_PRINT_COLOR_MODE_MAX
; i
++)
2364 for (j
= _PWG_PRINT_QUALITY_DRAFT
; j
< _PWG_PRINT_QUALITY_MAX
; j
++)
2365 if (pc
->num_presets
[i
][j
])
2367 cupsFilePrintf(fp
, "Preset %d %d", i
, j
);
2368 for (k
= pc
->num_presets
[i
][j
], option
= pc
->presets
[i
][j
];
2371 cupsFilePrintf(fp
, " %s=%s", option
->name
, option
->value
);
2372 cupsFilePutChar(fp
, '\n');
2379 if (pc
->sides_option
)
2380 cupsFilePrintf(fp
, "SidesOption %s\n", pc
->sides_option
);
2382 if (pc
->sides_1sided
)
2383 cupsFilePrintf(fp
, "Sides1Sided %s\n", pc
->sides_1sided
);
2385 if (pc
->sides_2sided_long
)
2386 cupsFilePrintf(fp
, "Sides2SidedLong %s\n", pc
->sides_2sided_long
);
2388 if (pc
->sides_2sided_short
)
2389 cupsFilePrintf(fp
, "Sides2SidedShort %s\n", pc
->sides_2sided_short
);
2392 * Product, cupsFilter, cupsFilter2, and cupsPreFilter...
2396 cupsFilePutConf(fp
, "Product", pc
->product
);
2398 for (value
= (const char *)cupsArrayFirst(pc
->filters
);
2400 value
= (const char *)cupsArrayNext(pc
->filters
))
2401 cupsFilePutConf(fp
, "Filter", value
);
2403 for (value
= (const char *)cupsArrayFirst(pc
->prefilters
);
2405 value
= (const char *)cupsArrayNext(pc
->prefilters
))
2406 cupsFilePutConf(fp
, "PreFilter", value
);
2408 cupsFilePrintf(fp
, "SingleFile %s\n", pc
->single_file
? "true" : "false");
2411 * Finishing options...
2414 for (f
= (_pwg_finishings_t
*)cupsArrayFirst(pc
->finishings
);
2416 f
= (_pwg_finishings_t
*)cupsArrayNext(pc
->finishings
))
2418 cupsFilePrintf(fp
, "Finishings %d", f
->value
);
2419 for (i
= f
->num_options
, option
= f
->options
; i
> 0; i
--, option
++)
2420 cupsFilePrintf(fp
, " %s=%s", option
->name
, option
->value
);
2421 cupsFilePutChar(fp
, '\n');
2428 cupsFilePrintf(fp
, "MaxCopies %d\n", pc
->max_copies
);
2431 * Accounting/quota/PIN/managed printing values...
2434 if (pc
->charge_info_uri
)
2435 cupsFilePutConf(fp
, "ChargeInfoURI", pc
->charge_info_uri
);
2437 cupsFilePrintf(fp
, "AccountId %s\n", pc
->account_id
? "true" : "false");
2438 cupsFilePrintf(fp
, "AccountingUserId %s\n",
2439 pc
->accounting_user_id
? "true" : "false");
2442 cupsFilePutConf(fp
, "Password", pc
->password
);
2444 for (value
= (char *)cupsArrayFirst(pc
->mandatory
);
2446 value
= (char *)cupsArrayNext(pc
->mandatory
))
2447 cupsFilePutConf(fp
, "Mandatory", value
);
2453 for (value
= (char *)cupsArrayFirst(pc
->support_files
);
2455 value
= (char *)cupsArrayNext(pc
->support_files
))
2456 cupsFilePutConf(fp
, "SupportFile", value
);
2459 * IPP attributes, if any...
2464 cupsFilePrintf(fp
, "IPP " CUPS_LLFMT
"\n", CUPS_LLCAST
ippLength(attrs
));
2466 attrs
->state
= IPP_STATE_IDLE
;
2467 ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
, attrs
);
2471 * Close and return...
2474 if (cupsFileClose(fp
))
2481 return (!rename(newfile
, filename
));
2486 * '_pwgInputSlotForSource()' - Get the InputSlot name for the given PWG
2490 const char * /* O - InputSlot name */
2491 _pwgInputSlotForSource(
2492 const char *media_source
, /* I - PWG media-source */
2493 char *name
, /* I - Name buffer */
2494 size_t namesize
) /* I - Size of name buffer */
2497 * Range check input...
2500 if (!media_source
|| !name
|| namesize
< PPD_MAX_NAME
)
2503 if (_cups_strcasecmp(media_source
, "main"))
2504 strlcpy(name
, "Cassette", namesize
);
2505 else if (_cups_strcasecmp(media_source
, "alternate"))
2506 strlcpy(name
, "Multipurpose", namesize
);
2507 else if (_cups_strcasecmp(media_source
, "large-capacity"))
2508 strlcpy(name
, "LargeCapacity", namesize
);
2509 else if (_cups_strcasecmp(media_source
, "bottom"))
2510 strlcpy(name
, "Lower", namesize
);
2511 else if (_cups_strcasecmp(media_source
, "middle"))
2512 strlcpy(name
, "Middle", namesize
);
2513 else if (_cups_strcasecmp(media_source
, "top"))
2514 strlcpy(name
, "Upper", namesize
);
2515 else if (_cups_strcasecmp(media_source
, "rear"))
2516 strlcpy(name
, "Rear", namesize
);
2517 else if (_cups_strcasecmp(media_source
, "side"))
2518 strlcpy(name
, "Side", namesize
);
2519 else if (_cups_strcasecmp(media_source
, "envelope"))
2520 strlcpy(name
, "Envelope", namesize
);
2521 else if (_cups_strcasecmp(media_source
, "main-roll"))
2522 strlcpy(name
, "Roll", namesize
);
2523 else if (_cups_strcasecmp(media_source
, "alternate-roll"))
2524 strlcpy(name
, "Roll2", namesize
);
2526 pwg_ppdize_name(media_source
, name
, namesize
);
2533 * '_pwgMediaTypeForType()' - Get the MediaType name for the given PWG
2537 const char * /* O - MediaType name */
2538 _pwgMediaTypeForType(
2539 const char *media_type
, /* I - PWG media-type */
2540 char *name
, /* I - Name buffer */
2541 size_t namesize
) /* I - Size of name buffer */
2544 * Range check input...
2547 if (!media_type
|| !name
|| namesize
< PPD_MAX_NAME
)
2550 if (_cups_strcasecmp(media_type
, "auto"))
2551 strlcpy(name
, "Auto", namesize
);
2552 else if (_cups_strcasecmp(media_type
, "cardstock"))
2553 strlcpy(name
, "Cardstock", namesize
);
2554 else if (_cups_strcasecmp(media_type
, "envelope"))
2555 strlcpy(name
, "Envelope", namesize
);
2556 else if (_cups_strcasecmp(media_type
, "photographic-glossy"))
2557 strlcpy(name
, "Glossy", namesize
);
2558 else if (_cups_strcasecmp(media_type
, "photographic-high-gloss"))
2559 strlcpy(name
, "HighGloss", namesize
);
2560 else if (_cups_strcasecmp(media_type
, "photographic-matte"))
2561 strlcpy(name
, "Matte", namesize
);
2562 else if (_cups_strcasecmp(media_type
, "stationery"))
2563 strlcpy(name
, "Plain", namesize
);
2564 else if (_cups_strcasecmp(media_type
, "stationery-coated"))
2565 strlcpy(name
, "Coated", namesize
);
2566 else if (_cups_strcasecmp(media_type
, "stationery-inkjet"))
2567 strlcpy(name
, "Inkjet", namesize
);
2568 else if (_cups_strcasecmp(media_type
, "stationery-letterhead"))
2569 strlcpy(name
, "Letterhead", namesize
);
2570 else if (_cups_strcasecmp(media_type
, "stationery-preprinted"))
2571 strlcpy(name
, "Preprinted", namesize
);
2572 else if (_cups_strcasecmp(media_type
, "transparency"))
2573 strlcpy(name
, "Transparency", namesize
);
2575 pwg_ppdize_name(media_type
, name
, namesize
);
2582 * '_pwgPageSizeForMedia()' - Get the PageSize name for the given media.
2585 const char * /* O - PageSize name */
2586 _pwgPageSizeForMedia(
2587 pwg_media_t
*media
, /* I - Media */
2588 char *name
, /* I - PageSize name buffer */
2589 size_t namesize
) /* I - Size of name buffer */
2591 const char *sizeptr
, /* Pointer to size in PWG name */
2592 *dimptr
; /* Pointer to dimensions in PWG name */
2596 * Range check input...
2599 if (!media
|| !name
|| namesize
< PPD_MAX_NAME
)
2603 * Copy or generate a PageSize name...
2609 * Use a standard Adobe name...
2612 strlcpy(name
, media
->ppd
, namesize
);
2614 else if (!media
->pwg
|| !strncmp(media
->pwg
, "custom_", 7) ||
2615 (sizeptr
= strchr(media
->pwg
, '_')) == NULL
||
2616 (dimptr
= strchr(sizeptr
+ 1, '_')) == NULL
||
2617 (size_t)(dimptr
- sizeptr
) > namesize
)
2620 * Use a name of the form "wNNNhNNN"...
2623 snprintf(name
, namesize
, "w%dh%d", (int)PWG_TO_POINTS(media
->width
),
2624 (int)PWG_TO_POINTS(media
->length
));
2629 * Copy the size name from class_sizename_dimensions...
2632 memcpy(name
, sizeptr
+ 1, dimptr
- sizeptr
- 1);
2633 name
[dimptr
- sizeptr
- 1] = '\0';
2641 * 'pwg_compare_finishings()' - Compare two finishings values.
2644 static int /* O- Result of comparison */
2645 pwg_compare_finishings(
2646 _pwg_finishings_t
*a
, /* I - First finishings value */
2647 _pwg_finishings_t
*b
) /* I - Second finishings value */
2649 return (b
->value
- a
->value
);
2654 * 'pwg_free_finishings()' - Free a finishings value.
2658 pwg_free_finishings(
2659 _pwg_finishings_t
*f
) /* I - Finishings value */
2661 cupsFreeOptions(f
->num_options
, f
->options
);
2667 * 'pwg_ppdize_name()' - Convert an IPP keyword to a PPD keyword.
2671 pwg_ppdize_name(const char *ipp
, /* I - IPP keyword */
2672 char *name
, /* I - Name buffer */
2673 size_t namesize
) /* I - Size of name buffer */
2675 char *ptr
, /* Pointer into name buffer */
2676 *end
; /* End of name buffer */
2679 *name
= toupper(*ipp
++);
2681 for (ptr
= name
+ 1, end
= name
+ namesize
- 1; *ipp
&& ptr
< end
;)
2683 if (*ipp
== '-' && _cups_isalpha(ipp
[1]))
2686 *ptr
++ = toupper(*ipp
++ & 255);
2697 * 'pwg_unppdize_name()' - Convert a PPD keyword to a lowercase IPP keyword.
2701 pwg_unppdize_name(const char *ppd
, /* I - PPD keyword */
2702 char *name
, /* I - Name buffer */
2703 size_t namesize
, /* I - Size of name buffer */
2704 const char *dashchars
)/* I - Characters to be replaced by dashes */
2706 char *ptr
, /* Pointer into name buffer */
2707 *end
; /* End of name buffer */
2710 for (ptr
= name
, end
= name
+ namesize
- 1; *ppd
&& ptr
< end
; ppd
++)
2712 if (_cups_isalnum(*ppd
) || *ppd
== '-')
2713 *ptr
++ = tolower(*ppd
& 255);
2714 else if (strchr(dashchars
, *ppd
))
2719 if (!_cups_isupper(*ppd
) && _cups_isalnum(*ppd
) &&
2720 _cups_isupper(ppd
[1]) && ptr
< end
)