4 * PPD cache implementation for CUPS.
6 * Copyright 2010-2014 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 * Include necessary headers...
21 #include "cups-private.h"
26 * Macro to test for two almost-equal PWG measurements.
29 #define _PWG_EQUIVALENT(x, y) (abs((x)-(y)) < 2)
36 static int pwg_compare_finishings(_pwg_finishings_t
*a
,
37 _pwg_finishings_t
*b
);
38 static void pwg_free_finishings(_pwg_finishings_t
*f
);
39 static void pwg_ppdize_name(const char *ipp
, char *name
, size_t namesize
);
40 static void pwg_unppdize_name(const char *ppd
, char *name
, size_t namesize
,
41 const char *dashchars
);
45 * '_ppdCacheCreateWithFile()' - Create PPD cache and mapping data from a
48 * Use the @link _ppdCacheWriteFile@ function to write PWG mapping data to a
52 _ppd_cache_t
* /* O - PPD cache and mapping data */
53 _ppdCacheCreateWithFile(
54 const char *filename
, /* I - File to read */
55 ipp_t
**attrs
) /* IO - IPP attributes, if any */
57 cups_file_t
*fp
; /* File */
58 _ppd_cache_t
*pc
; /* PWG mapping data */
59 pwg_size_t
*size
; /* Current size */
60 pwg_map_t
*map
; /* Current map */
61 _pwg_finishings_t
*finishings
; /* Current finishings option */
62 int linenum
, /* Current line number */
63 num_bins
, /* Number of bins in file */
64 num_sizes
, /* Number of sizes in file */
65 num_sources
, /* Number of sources in file */
66 num_types
; /* Number of types in file */
67 char line
[2048], /* Current line */
68 *value
, /* Pointer to value in line */
69 *valueptr
, /* Pointer into value */
70 pwg_keyword
[128], /* PWG keyword */
71 ppd_keyword
[PPD_MAX_NAME
];
73 _pwg_print_color_mode_t print_color_mode
;
74 /* Print color mode for preset */
75 _pwg_print_quality_t print_quality
; /* Print quality for preset */
78 DEBUG_printf(("_ppdCacheCreateWithFile(filename=\"%s\")", filename
));
81 * Range check input...
89 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
97 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
99 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
104 * Read the first line and make sure it has "#CUPS-PPD-CACHE-version" in it...
107 if (!cupsFileGets(fp
, line
, sizeof(line
)))
109 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
110 DEBUG_puts("_ppdCacheCreateWithFile: Unable to read first line.");
115 if (strncmp(line
, "#CUPS-PPD-CACHE-", 16))
117 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
118 DEBUG_printf(("_ppdCacheCreateWithFile: Wrong first line \"%s\".", line
));
123 if (atoi(line
+ 16) != _PPD_CACHE_VERSION
)
125 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Out of date PPD cache file."), 1);
126 DEBUG_printf(("_ppdCacheCreateWithFile: Cache file has version %s, "
127 "expected %d.", line
+ 16, _PPD_CACHE_VERSION
));
133 * Allocate the mapping data structure...
136 if ((pc
= calloc(1, sizeof(_ppd_cache_t
))) == NULL
)
138 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
139 DEBUG_puts("_ppdCacheCreateWithFile: Unable to allocate _ppd_cache_t.");
143 pc
->max_copies
= 9999;
155 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
157 DEBUG_printf(("_ppdCacheCreateWithFile: line=\"%s\", value=\"%s\", "
158 "linenum=%d", line
, value
, linenum
));
162 DEBUG_printf(("_ppdCacheCreateWithFile: Missing value on line %d.",
164 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
167 else if (!_cups_strcasecmp(line
, "Filter"))
170 pc
->filters
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
171 (cups_acopy_func_t
)_cupsStrAlloc
,
172 (cups_afree_func_t
)_cupsStrFree
);
174 cupsArrayAdd(pc
->filters
, value
);
176 else if (!_cups_strcasecmp(line
, "PreFilter"))
179 pc
->prefilters
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
180 (cups_acopy_func_t
)_cupsStrAlloc
,
181 (cups_afree_func_t
)_cupsStrFree
);
183 cupsArrayAdd(pc
->prefilters
, value
);
185 else if (!_cups_strcasecmp(line
, "Product"))
187 pc
->product
= _cupsStrAlloc(value
);
189 else if (!_cups_strcasecmp(line
, "SingleFile"))
191 pc
->single_file
= !_cups_strcasecmp(value
, "true");
193 else if (!_cups_strcasecmp(line
, "IPP"))
195 off_t pos
= cupsFileTell(fp
), /* Position in file */
196 length
= strtol(value
, NULL
, 10);
197 /* Length of IPP attributes */
201 DEBUG_puts("_ppdCacheCreateWithFile: IPP listed multiple times.");
202 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
205 else if (length
<= 0)
207 DEBUG_puts("_ppdCacheCreateWithFile: Bad IPP length.");
208 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
215 * Read IPP attributes into the provided variable...
220 if (ippReadIO(fp
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
,
221 *attrs
) != IPP_STATE_DATA
)
223 DEBUG_puts("_ppdCacheCreateWithFile: Bad IPP data.");
224 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
231 * Skip the IPP data entirely...
234 cupsFileSeek(fp
, pos
+ length
);
237 if (cupsFileTell(fp
) != (pos
+ length
))
239 DEBUG_puts("_ppdCacheCreateWithFile: Bad IPP data.");
240 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
244 else if (!_cups_strcasecmp(line
, "NumBins"))
248 DEBUG_puts("_ppdCacheCreateWithFile: NumBins listed multiple times.");
249 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
253 if ((num_bins
= atoi(value
)) <= 0 || num_bins
> 65536)
255 DEBUG_printf(("_ppdCacheCreateWithFile: Bad NumBins value %d on line "
256 "%d.", num_sizes
, linenum
));
257 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
261 if ((pc
->bins
= calloc((size_t)num_bins
, sizeof(pwg_map_t
))) == NULL
)
263 DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d bins.",
265 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
269 else if (!_cups_strcasecmp(line
, "Bin"))
271 if (sscanf(value
, "%127s%40s", pwg_keyword
, ppd_keyword
) != 2)
273 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Bin on line %d.", linenum
));
274 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
278 if (pc
->num_bins
>= num_bins
)
280 DEBUG_printf(("_ppdCacheCreateWithFile: Too many Bin's on line %d.",
282 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
286 map
= pc
->bins
+ pc
->num_bins
;
287 map
->pwg
= _cupsStrAlloc(pwg_keyword
);
288 map
->ppd
= _cupsStrAlloc(ppd_keyword
);
292 else if (!_cups_strcasecmp(line
, "NumSizes"))
296 DEBUG_puts("_ppdCacheCreateWithFile: NumSizes listed multiple times.");
297 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
301 if ((num_sizes
= atoi(value
)) < 0 || num_sizes
> 65536)
303 DEBUG_printf(("_ppdCacheCreateWithFile: Bad NumSizes value %d on line "
304 "%d.", num_sizes
, linenum
));
305 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
311 if ((pc
->sizes
= calloc((size_t)num_sizes
, sizeof(pwg_size_t
))) == NULL
)
313 DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d sizes.",
315 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
320 else if (!_cups_strcasecmp(line
, "Size"))
322 if (pc
->num_sizes
>= num_sizes
)
324 DEBUG_printf(("_ppdCacheCreateWithFile: Too many Size's on line %d.",
326 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
330 size
= pc
->sizes
+ pc
->num_sizes
;
332 if (sscanf(value
, "%127s%40s%d%d%d%d%d%d", pwg_keyword
, ppd_keyword
,
333 &(size
->width
), &(size
->length
), &(size
->left
),
334 &(size
->bottom
), &(size
->right
), &(size
->top
)) != 8)
336 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Size on line %d.",
338 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
342 size
->map
.pwg
= _cupsStrAlloc(pwg_keyword
);
343 size
->map
.ppd
= _cupsStrAlloc(ppd_keyword
);
347 else if (!_cups_strcasecmp(line
, "CustomSize"))
349 if (pc
->custom_max_width
> 0)
351 DEBUG_printf(("_ppdCacheCreateWithFile: Too many CustomSize's on line "
353 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
357 if (sscanf(value
, "%d%d%d%d%d%d%d%d", &(pc
->custom_max_width
),
358 &(pc
->custom_max_length
), &(pc
->custom_min_width
),
359 &(pc
->custom_min_length
), &(pc
->custom_size
.left
),
360 &(pc
->custom_size
.bottom
), &(pc
->custom_size
.right
),
361 &(pc
->custom_size
.top
)) != 8)
363 DEBUG_printf(("_ppdCacheCreateWithFile: Bad CustomSize on line %d.",
365 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
369 pwgFormatSizeName(pwg_keyword
, sizeof(pwg_keyword
), "custom", "max",
370 pc
->custom_max_width
, pc
->custom_max_length
, NULL
);
371 pc
->custom_max_keyword
= _cupsStrAlloc(pwg_keyword
);
373 pwgFormatSizeName(pwg_keyword
, sizeof(pwg_keyword
), "custom", "min",
374 pc
->custom_min_width
, pc
->custom_min_length
, NULL
);
375 pc
->custom_min_keyword
= _cupsStrAlloc(pwg_keyword
);
377 else if (!_cups_strcasecmp(line
, "SourceOption"))
379 pc
->source_option
= _cupsStrAlloc(value
);
381 else if (!_cups_strcasecmp(line
, "NumSources"))
385 DEBUG_puts("_ppdCacheCreateWithFile: NumSources listed multiple "
387 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
391 if ((num_sources
= atoi(value
)) <= 0 || num_sources
> 65536)
393 DEBUG_printf(("_ppdCacheCreateWithFile: Bad NumSources value %d on "
394 "line %d.", num_sources
, linenum
));
395 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
399 if ((pc
->sources
= calloc((size_t)num_sources
, sizeof(pwg_map_t
))) == NULL
)
401 DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d sources.",
403 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
407 else if (!_cups_strcasecmp(line
, "Source"))
409 if (sscanf(value
, "%127s%40s", pwg_keyword
, ppd_keyword
) != 2)
411 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Source on line %d.",
413 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
417 if (pc
->num_sources
>= num_sources
)
419 DEBUG_printf(("_ppdCacheCreateWithFile: Too many Source's on line %d.",
421 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
425 map
= pc
->sources
+ pc
->num_sources
;
426 map
->pwg
= _cupsStrAlloc(pwg_keyword
);
427 map
->ppd
= _cupsStrAlloc(ppd_keyword
);
431 else if (!_cups_strcasecmp(line
, "NumTypes"))
435 DEBUG_puts("_ppdCacheCreateWithFile: NumTypes listed multiple times.");
436 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
440 if ((num_types
= atoi(value
)) <= 0 || num_types
> 65536)
442 DEBUG_printf(("_ppdCacheCreateWithFile: Bad NumTypes value %d on "
443 "line %d.", num_types
, linenum
));
444 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
448 if ((pc
->types
= calloc((size_t)num_types
, sizeof(pwg_map_t
))) == NULL
)
450 DEBUG_printf(("_ppdCacheCreateWithFile: Unable to allocate %d types.",
452 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
456 else if (!_cups_strcasecmp(line
, "Type"))
458 if (sscanf(value
, "%127s%40s", pwg_keyword
, ppd_keyword
) != 2)
460 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Type on line %d.",
462 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
466 if (pc
->num_types
>= num_types
)
468 DEBUG_printf(("_ppdCacheCreateWithFile: Too many Type's on line %d.",
470 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
474 map
= pc
->types
+ pc
->num_types
;
475 map
->pwg
= _cupsStrAlloc(pwg_keyword
);
476 map
->ppd
= _cupsStrAlloc(ppd_keyword
);
480 else if (!_cups_strcasecmp(line
, "Preset"))
483 * Preset output-mode print-quality name=value ...
486 print_color_mode
= (_pwg_print_color_mode_t
)strtol(value
, &valueptr
, 10);
487 print_quality
= (_pwg_print_quality_t
)strtol(valueptr
, &valueptr
, 10);
489 if (print_color_mode
< _PWG_PRINT_COLOR_MODE_MONOCHROME
||
490 print_color_mode
>= _PWG_PRINT_COLOR_MODE_MAX
||
491 print_quality
< _PWG_PRINT_QUALITY_DRAFT
||
492 print_quality
>= _PWG_PRINT_QUALITY_MAX
||
493 valueptr
== value
|| !*valueptr
)
495 DEBUG_printf(("_ppdCacheCreateWithFile: Bad Preset on line %d.",
497 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
501 pc
->num_presets
[print_color_mode
][print_quality
] =
502 cupsParseOptions(valueptr
, 0,
503 pc
->presets
[print_color_mode
] + print_quality
);
505 else if (!_cups_strcasecmp(line
, "SidesOption"))
506 pc
->sides_option
= _cupsStrAlloc(value
);
507 else if (!_cups_strcasecmp(line
, "Sides1Sided"))
508 pc
->sides_1sided
= _cupsStrAlloc(value
);
509 else if (!_cups_strcasecmp(line
, "Sides2SidedLong"))
510 pc
->sides_2sided_long
= _cupsStrAlloc(value
);
511 else if (!_cups_strcasecmp(line
, "Sides2SidedShort"))
512 pc
->sides_2sided_short
= _cupsStrAlloc(value
);
513 else if (!_cups_strcasecmp(line
, "Finishings"))
517 cupsArrayNew3((cups_array_func_t
)pwg_compare_finishings
,
519 (cups_afree_func_t
)pwg_free_finishings
);
521 if ((finishings
= calloc(1, sizeof(_pwg_finishings_t
))) == NULL
)
524 finishings
->value
= (ipp_finishings_t
)strtol(value
, &valueptr
, 10);
525 finishings
->num_options
= cupsParseOptions(valueptr
, 0,
526 &(finishings
->options
));
528 cupsArrayAdd(pc
->finishings
, finishings
);
530 else if (!_cups_strcasecmp(line
, "MaxCopies"))
531 pc
->max_copies
= atoi(value
);
532 else if (!_cups_strcasecmp(line
, "ChargeInfoURI"))
533 pc
->charge_info_uri
= _cupsStrAlloc(value
);
534 else if (!_cups_strcasecmp(line
, "JobAccountId"))
535 pc
->account_id
= !_cups_strcasecmp(value
, "true");
536 else if (!_cups_strcasecmp(line
, "JobAccountingUserId"))
537 pc
->accounting_user_id
= !_cups_strcasecmp(value
, "true");
538 else if (!_cups_strcasecmp(line
, "JobPassword"))
539 pc
->password
= _cupsStrAlloc(value
);
540 else if (!_cups_strcasecmp(line
, "Mandatory"))
543 _cupsArrayAddStrings(pc
->mandatory
, value
, ' ');
545 pc
->mandatory
= _cupsArrayNewStrings(value
, ' ');
547 else if (!_cups_strcasecmp(line
, "SupportFile"))
549 if (!pc
->support_files
)
550 pc
->support_files
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
551 (cups_acopy_func_t
)_cupsStrAlloc
,
552 (cups_afree_func_t
)_cupsStrFree
);
554 cupsArrayAdd(pc
->support_files
, value
);
558 DEBUG_printf(("_ppdCacheCreateWithFile: Unknown %s on line %d.", line
,
563 if (pc
->num_sizes
< num_sizes
)
565 DEBUG_printf(("_ppdCacheCreateWithFile: Not enough sizes (%d < %d).",
566 pc
->num_sizes
, num_sizes
));
567 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
571 if (pc
->num_sources
< num_sources
)
573 DEBUG_printf(("_ppdCacheCreateWithFile: Not enough sources (%d < %d).",
574 pc
->num_sources
, num_sources
));
575 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
579 if (pc
->num_types
< num_types
)
581 DEBUG_printf(("_ppdCacheCreateWithFile: Not enough types (%d < %d).",
582 pc
->num_types
, num_types
));
583 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad PPD cache file."), 1);
592 * If we get here the file was bad - free any data and return...
598 _ppdCacheDestroy(pc
);
611 * '_ppdCacheCreateWithPPD()' - Create PWG mapping data from a PPD file.
614 _ppd_cache_t
* /* O - PPD cache and mapping data */
615 _ppdCacheCreateWithPPD(ppd_file_t
*ppd
) /* I - PPD file */
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 */
660 DEBUG_printf(("_ppdCacheCreateWithPPD(ppd=%p)", ppd
));
663 * Range check input...
673 if ((pc
= calloc(1, sizeof(_ppd_cache_t
))) == NULL
)
675 DEBUG_puts("_ppdCacheCreateWithPPD: Unable to allocate _ppd_cache_t.");
680 * Copy and convert size data...
683 if (ppd
->num_sizes
> 0)
685 if ((pc
->sizes
= calloc((size_t)ppd
->num_sizes
, sizeof(pwg_size_t
))) == NULL
)
687 DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d "
688 "pwg_size_t's.", ppd
->num_sizes
));
692 for (i
= ppd
->num_sizes
, pwg_size
= pc
->sizes
, ppd_size
= ppd
->sizes
;
697 * Don't copy over custom size...
700 if (!_cups_strcasecmp(ppd_size
->name
, "Custom"))
704 * Convert the PPD size name to the corresponding PWG keyword name.
707 if ((pwg_media
= pwgMediaForPPD(ppd_size
->name
)) != NULL
)
710 * Standard name, do we have conflicts?
713 for (j
= 0; j
< pc
->num_sizes
; j
++)
714 if (!strcmp(pc
->sizes
[j
].map
.pwg
, pwg_media
->pwg
))
724 * Standard name and no conflicts, use it!
727 pwg_name
= pwg_media
->pwg
;
733 * Not a standard name; convert it to a PWG vendor name of the form:
735 * pp_lowerppd_WIDTHxHEIGHTuu
738 pwg_name
= pwg_keyword
;
741 pwg_unppdize_name(ppd_size
->name
, ppd_name
, sizeof(ppd_name
), "_.");
742 pwgFormatSizeName(pwg_keyword
, sizeof(pwg_keyword
), NULL
, ppd_name
,
743 PWG_FROM_POINTS(ppd_size
->width
),
744 PWG_FROM_POINTS(ppd_size
->length
), NULL
);
748 * If we have a similar paper with non-zero margins then we only want to
749 * keep it if it has a larger imageable area length. The NULL check is for
750 * dimensions that are <= 0...
753 if ((pwg_media
= pwgMediaForSize(PWG_FROM_POINTS(ppd_size
->width
),
754 PWG_FROM_POINTS(ppd_size
->length
))) == NULL
)
757 new_width
= pwg_media
->width
;
758 new_length
= pwg_media
->length
;
759 new_left
= PWG_FROM_POINTS(ppd_size
->left
);
760 new_bottom
= PWG_FROM_POINTS(ppd_size
->bottom
);
761 new_right
= PWG_FROM_POINTS(ppd_size
->width
- ppd_size
->right
);
762 new_top
= PWG_FROM_POINTS(ppd_size
->length
- ppd_size
->top
);
763 new_imageable
= new_length
- new_top
- new_bottom
;
764 new_borderless
= new_bottom
== 0 && new_top
== 0 &&
765 new_left
== 0 && new_right
== 0;
767 for (k
= pc
->num_sizes
, similar
= 0, old_size
= pc
->sizes
, new_size
= NULL
;
771 old_imageable
= old_size
->length
- old_size
->top
- old_size
->bottom
;
772 old_borderless
= old_size
->left
== 0 && old_size
->bottom
== 0 &&
773 old_size
->right
== 0 && old_size
->top
== 0;
774 old_known_pwg
= strncmp(old_size
->map
.pwg
, "oe_", 3) &&
775 strncmp(old_size
->map
.pwg
, "om_", 3);
777 similar
= old_borderless
== new_borderless
&&
778 _PWG_EQUIVALENT(old_size
->width
, new_width
) &&
779 _PWG_EQUIVALENT(old_size
->length
, new_length
);
782 (new_known_pwg
|| (!old_known_pwg
&& new_imageable
> old_imageable
)))
785 * The new paper has a larger imageable area so it could replace
786 * the older paper. Regardless of the imageable area, we always
787 * prefer the size with a well-known PWG name.
791 _cupsStrFree(old_size
->map
.ppd
);
792 _cupsStrFree(old_size
->map
.pwg
);
799 * The paper was unique enough to deserve its own entry so add it to the
803 new_size
= pwg_size
++;
813 new_size
->map
.ppd
= _cupsStrAlloc(ppd_size
->name
);
814 new_size
->map
.pwg
= _cupsStrAlloc(pwg_name
);
815 new_size
->width
= new_width
;
816 new_size
->length
= new_length
;
817 new_size
->left
= new_left
;
818 new_size
->bottom
= new_bottom
;
819 new_size
->right
= new_right
;
820 new_size
->top
= new_top
;
825 if (ppd
->variable_sizes
)
828 * Generate custom size data...
831 pwgFormatSizeName(pwg_keyword
, sizeof(pwg_keyword
), "custom", "max",
832 PWG_FROM_POINTS(ppd
->custom_max
[0]),
833 PWG_FROM_POINTS(ppd
->custom_max
[1]), NULL
);
834 pc
->custom_max_keyword
= _cupsStrAlloc(pwg_keyword
);
835 pc
->custom_max_width
= PWG_FROM_POINTS(ppd
->custom_max
[0]);
836 pc
->custom_max_length
= PWG_FROM_POINTS(ppd
->custom_max
[1]);
838 pwgFormatSizeName(pwg_keyword
, sizeof(pwg_keyword
), "custom", "min",
839 PWG_FROM_POINTS(ppd
->custom_min
[0]),
840 PWG_FROM_POINTS(ppd
->custom_min
[1]), NULL
);
841 pc
->custom_min_keyword
= _cupsStrAlloc(pwg_keyword
);
842 pc
->custom_min_width
= PWG_FROM_POINTS(ppd
->custom_min
[0]);
843 pc
->custom_min_length
= PWG_FROM_POINTS(ppd
->custom_min
[1]);
845 pc
->custom_size
.left
= PWG_FROM_POINTS(ppd
->custom_margins
[0]);
846 pc
->custom_size
.bottom
= PWG_FROM_POINTS(ppd
->custom_margins
[1]);
847 pc
->custom_size
.right
= PWG_FROM_POINTS(ppd
->custom_margins
[2]);
848 pc
->custom_size
.top
= PWG_FROM_POINTS(ppd
->custom_margins
[3]);
852 * Copy and convert InputSlot data...
855 if ((input_slot
= ppdFindOption(ppd
, "InputSlot")) == NULL
)
856 input_slot
= ppdFindOption(ppd
, "HPPaperSource");
860 pc
->source_option
= _cupsStrAlloc(input_slot
->keyword
);
862 if ((pc
->sources
= calloc((size_t)input_slot
->num_choices
, sizeof(pwg_map_t
))) == NULL
)
864 DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d "
865 "pwg_map_t's for InputSlot.", input_slot
->num_choices
));
869 pc
->num_sources
= input_slot
->num_choices
;
871 for (i
= input_slot
->num_choices
, choice
= input_slot
->choices
,
874 i
--, choice
++, map
++)
876 if (!_cups_strncasecmp(choice
->choice
, "Auto", 4) ||
877 !_cups_strcasecmp(choice
->choice
, "Default"))
879 else if (!_cups_strcasecmp(choice
->choice
, "Cassette"))
881 else if (!_cups_strcasecmp(choice
->choice
, "PhotoTray"))
883 else if (!_cups_strcasecmp(choice
->choice
, "CDTray"))
885 else if (!_cups_strncasecmp(choice
->choice
, "Multipurpose", 12) ||
886 !_cups_strcasecmp(choice
->choice
, "MP") ||
887 !_cups_strcasecmp(choice
->choice
, "MPTray"))
888 pwg_name
= "by-pass-tray";
889 else if (!_cups_strcasecmp(choice
->choice
, "LargeCapacity"))
890 pwg_name
= "large-capacity";
891 else if (!_cups_strncasecmp(choice
->choice
, "Lower", 5))
893 else if (!_cups_strncasecmp(choice
->choice
, "Middle", 6))
895 else if (!_cups_strncasecmp(choice
->choice
, "Upper", 5))
897 else if (!_cups_strncasecmp(choice
->choice
, "Side", 4))
899 else if (!_cups_strcasecmp(choice
->choice
, "Roll"))
900 pwg_name
= "main-roll";
904 * Convert PPD name to lowercase...
907 pwg_name
= pwg_keyword
;
908 pwg_unppdize_name(choice
->choice
, pwg_keyword
, sizeof(pwg_keyword
),
912 map
->pwg
= _cupsStrAlloc(pwg_name
);
913 map
->ppd
= _cupsStrAlloc(choice
->choice
);
918 * Copy and convert MediaType data...
921 if ((media_type
= ppdFindOption(ppd
, "MediaType")) != NULL
)
923 if ((pc
->types
= calloc((size_t)media_type
->num_choices
, sizeof(pwg_map_t
))) == NULL
)
925 DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d "
926 "pwg_map_t's for MediaType.", media_type
->num_choices
));
930 pc
->num_types
= media_type
->num_choices
;
932 for (i
= media_type
->num_choices
, choice
= media_type
->choices
,
935 i
--, choice
++, map
++)
937 if (!_cups_strncasecmp(choice
->choice
, "Auto", 4) ||
938 !_cups_strcasecmp(choice
->choice
, "Any") ||
939 !_cups_strcasecmp(choice
->choice
, "Default"))
941 else if (!_cups_strncasecmp(choice
->choice
, "Card", 4))
942 pwg_name
= "cardstock";
943 else if (!_cups_strncasecmp(choice
->choice
, "Env", 3))
944 pwg_name
= "envelope";
945 else if (!_cups_strncasecmp(choice
->choice
, "Gloss", 5))
946 pwg_name
= "photographic-glossy";
947 else if (!_cups_strcasecmp(choice
->choice
, "HighGloss"))
948 pwg_name
= "photographic-high-gloss";
949 else if (!_cups_strcasecmp(choice
->choice
, "Matte"))
950 pwg_name
= "photographic-matte";
951 else if (!_cups_strncasecmp(choice
->choice
, "Plain", 5))
952 pwg_name
= "stationery";
953 else if (!_cups_strncasecmp(choice
->choice
, "Coated", 6))
954 pwg_name
= "stationery-coated";
955 else if (!_cups_strcasecmp(choice
->choice
, "Inkjet"))
956 pwg_name
= "stationery-inkjet";
957 else if (!_cups_strcasecmp(choice
->choice
, "Letterhead"))
958 pwg_name
= "stationery-letterhead";
959 else if (!_cups_strncasecmp(choice
->choice
, "Preprint", 8))
960 pwg_name
= "stationery-preprinted";
961 else if (!_cups_strcasecmp(choice
->choice
, "Recycled"))
962 pwg_name
= "stationery-recycled";
963 else if (!_cups_strncasecmp(choice
->choice
, "Transparen", 10))
964 pwg_name
= "transparency";
968 * Convert PPD name to lowercase...
971 pwg_name
= pwg_keyword
;
972 pwg_unppdize_name(choice
->choice
, pwg_keyword
, sizeof(pwg_keyword
),
976 map
->pwg
= _cupsStrAlloc(pwg_name
);
977 map
->ppd
= _cupsStrAlloc(choice
->choice
);
982 * Copy and convert OutputBin data...
985 if ((output_bin
= ppdFindOption(ppd
, "OutputBin")) != NULL
)
987 if ((pc
->bins
= calloc((size_t)output_bin
->num_choices
, sizeof(pwg_map_t
))) == NULL
)
989 DEBUG_printf(("_ppdCacheCreateWithPPD: Unable to allocate %d "
990 "pwg_map_t's for OutputBin.", output_bin
->num_choices
));
994 pc
->num_bins
= output_bin
->num_choices
;
996 for (i
= output_bin
->num_choices
, choice
= output_bin
->choices
,
999 i
--, choice
++, map
++)
1001 pwg_unppdize_name(choice
->choice
, pwg_keyword
, sizeof(pwg_keyword
), "_");
1003 map
->pwg
= _cupsStrAlloc(pwg_keyword
);
1004 map
->ppd
= _cupsStrAlloc(choice
->choice
);
1008 if ((ppd_attr
= ppdFindAttr(ppd
, "APPrinterPreset", NULL
)) != NULL
)
1011 * Copy and convert APPrinterPreset (output-mode + print-quality) data...
1014 const char *quality
, /* com.apple.print.preset.quality value */
1015 *output_mode
, /* com.apple.print.preset.output-mode value */
1016 *color_model_val
, /* ColorModel choice */
1017 *graphicsType
, /* com.apple.print.preset.graphicsType value */
1018 *media_front_coating
; /* com.apple.print.preset.media-front-coating value */
1022 num_options
= _ppdParseOptions(ppd_attr
->value
, 0, &options
,
1025 if ((quality
= cupsGetOption("com.apple.print.preset.quality",
1026 num_options
, options
)) != NULL
)
1029 * Get the print-quality for this preset...
1032 if (!strcmp(quality
, "low"))
1033 pwg_print_quality
= _PWG_PRINT_QUALITY_DRAFT
;
1034 else if (!strcmp(quality
, "high"))
1035 pwg_print_quality
= _PWG_PRINT_QUALITY_HIGH
;
1037 pwg_print_quality
= _PWG_PRINT_QUALITY_NORMAL
;
1040 * Ignore graphicsType "Photo" presets that are not high quality.
1043 graphicsType
= cupsGetOption("com.apple.print.preset.graphicsType",
1044 num_options
, options
);
1046 if (pwg_print_quality
!= _PWG_PRINT_QUALITY_HIGH
&& graphicsType
&&
1047 !strcmp(graphicsType
, "Photo"))
1051 * Ignore presets for normal and draft quality where the coating
1052 * isn't "none" or "autodetect".
1055 media_front_coating
= cupsGetOption(
1056 "com.apple.print.preset.media-front-coating",
1057 num_options
, options
);
1059 if (pwg_print_quality
!= _PWG_PRINT_QUALITY_HIGH
&&
1060 media_front_coating
&&
1061 strcmp(media_front_coating
, "none") &&
1062 strcmp(media_front_coating
, "autodetect"))
1066 * Get the output mode for this preset...
1069 output_mode
= cupsGetOption("com.apple.print.preset.output-mode",
1070 num_options
, options
);
1071 color_model_val
= cupsGetOption("ColorModel", num_options
, options
);
1075 if (!strcmp(output_mode
, "monochrome"))
1076 pwg_print_color_mode
= _PWG_PRINT_COLOR_MODE_MONOCHROME
;
1078 pwg_print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
1080 else if (color_model_val
)
1082 if (!_cups_strcasecmp(color_model_val
, "Gray"))
1083 pwg_print_color_mode
= _PWG_PRINT_COLOR_MODE_MONOCHROME
;
1085 pwg_print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
1088 pwg_print_color_mode
= _PWG_PRINT_COLOR_MODE_COLOR
;
1091 * Save the options for this combination as needed...
1094 if (!pc
->num_presets
[pwg_print_color_mode
][pwg_print_quality
])
1095 pc
->num_presets
[pwg_print_color_mode
][pwg_print_quality
] =
1096 _ppdParseOptions(ppd_attr
->value
, 0,
1097 pc
->presets
[pwg_print_color_mode
] +
1098 pwg_print_quality
, _PPD_PARSE_OPTIONS
);
1101 cupsFreeOptions(num_options
, options
);
1103 while ((ppd_attr
= ppdFindNextAttr(ppd
, "APPrinterPreset", NULL
)) != NULL
);
1106 if (!pc
->num_presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
][_PWG_PRINT_QUALITY_DRAFT
] &&
1107 !pc
->num_presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
][_PWG_PRINT_QUALITY_NORMAL
] &&
1108 !pc
->num_presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
][_PWG_PRINT_QUALITY_HIGH
])
1111 * Try adding some common color options to create grayscale presets. These
1112 * are listed in order of popularity...
1115 const char *color_option
= NULL
, /* Color control option */
1116 *gray_choice
= NULL
; /* Choice to select grayscale */
1118 if ((color_model
= ppdFindOption(ppd
, "ColorModel")) != NULL
&&
1119 ppdFindChoice(color_model
, "Gray"))
1121 color_option
= "ColorModel";
1122 gray_choice
= "Gray";
1124 else if ((color_model
= ppdFindOption(ppd
, "HPColorMode")) != NULL
&&
1125 ppdFindChoice(color_model
, "grayscale"))
1127 color_option
= "HPColorMode";
1128 gray_choice
= "grayscale";
1130 else if ((color_model
= ppdFindOption(ppd
, "BRMonoColor")) != NULL
&&
1131 ppdFindChoice(color_model
, "Mono"))
1133 color_option
= "BRMonoColor";
1134 gray_choice
= "Mono";
1136 else if ((color_model
= ppdFindOption(ppd
, "CNIJSGrayScale")) != NULL
&&
1137 ppdFindChoice(color_model
, "1"))
1139 color_option
= "CNIJSGrayScale";
1142 else if ((color_model
= ppdFindOption(ppd
, "HPColorAsGray")) != NULL
&&
1143 ppdFindChoice(color_model
, "True"))
1145 color_option
= "HPColorAsGray";
1146 gray_choice
= "True";
1149 if (color_option
&& gray_choice
)
1152 * Copy and convert ColorModel (output-mode) data...
1155 cups_option_t
*coption
, /* Color option */
1156 *moption
; /* Monochrome option */
1158 for (pwg_print_quality
= _PWG_PRINT_QUALITY_DRAFT
;
1159 pwg_print_quality
< _PWG_PRINT_QUALITY_MAX
;
1160 pwg_print_quality
++)
1162 if (pc
->num_presets
[_PWG_PRINT_COLOR_MODE_COLOR
][pwg_print_quality
])
1165 * Copy the color options...
1168 num_options
= pc
->num_presets
[_PWG_PRINT_COLOR_MODE_COLOR
]
1169 [pwg_print_quality
];
1170 options
= calloc(sizeof(cups_option_t
), (size_t)num_options
);
1174 for (i
= num_options
, moption
= options
,
1175 coption
= pc
->presets
[_PWG_PRINT_COLOR_MODE_COLOR
]
1176 [pwg_print_quality
];
1178 i
--, moption
++, coption
++)
1180 moption
->name
= _cupsStrRetain(coption
->name
);
1181 moption
->value
= _cupsStrRetain(coption
->value
);
1184 pc
->num_presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
][pwg_print_quality
] =
1186 pc
->presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
][pwg_print_quality
] =
1190 else if (pwg_print_quality
!= _PWG_PRINT_QUALITY_NORMAL
)
1194 * Add the grayscale option to the preset...
1197 pc
->num_presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
][pwg_print_quality
] =
1198 cupsAddOption(color_option
, gray_choice
,
1199 pc
->num_presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
]
1200 [pwg_print_quality
],
1201 pc
->presets
[_PWG_PRINT_COLOR_MODE_MONOCHROME
] +
1208 * Copy and convert Duplex (sides) data...
1211 if ((duplex
= ppdFindOption(ppd
, "Duplex")) == NULL
)
1212 if ((duplex
= ppdFindOption(ppd
, "JCLDuplex")) == NULL
)
1213 if ((duplex
= ppdFindOption(ppd
, "EFDuplex")) == NULL
)
1214 if ((duplex
= ppdFindOption(ppd
, "EFDuplexing")) == NULL
)
1215 duplex
= ppdFindOption(ppd
, "KD03Duplex");
1219 pc
->sides_option
= _cupsStrAlloc(duplex
->keyword
);
1221 for (i
= duplex
->num_choices
, choice
= duplex
->choices
;
1225 if ((!_cups_strcasecmp(choice
->choice
, "None") ||
1226 !_cups_strcasecmp(choice
->choice
, "False")) && !pc
->sides_1sided
)
1227 pc
->sides_1sided
= _cupsStrAlloc(choice
->choice
);
1228 else if ((!_cups_strcasecmp(choice
->choice
, "DuplexNoTumble") ||
1229 !_cups_strcasecmp(choice
->choice
, "LongEdge") ||
1230 !_cups_strcasecmp(choice
->choice
, "Top")) && !pc
->sides_2sided_long
)
1231 pc
->sides_2sided_long
= _cupsStrAlloc(choice
->choice
);
1232 else if ((!_cups_strcasecmp(choice
->choice
, "DuplexTumble") ||
1233 !_cups_strcasecmp(choice
->choice
, "ShortEdge") ||
1234 !_cups_strcasecmp(choice
->choice
, "Bottom")) &&
1235 !pc
->sides_2sided_short
)
1236 pc
->sides_2sided_short
= _cupsStrAlloc(choice
->choice
);
1241 * Copy filters and pre-filters...
1244 pc
->filters
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
1245 (cups_acopy_func_t
)_cupsStrAlloc
,
1246 (cups_afree_func_t
)_cupsStrFree
);
1248 cupsArrayAdd(pc
->filters
,
1249 "application/vnd.cups-raw application/octet-stream 0 -");
1251 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsFilter2", NULL
)) != NULL
)
1255 cupsArrayAdd(pc
->filters
, ppd_attr
->value
);
1257 while ((ppd_attr
= ppdFindNextAttr(ppd
, "cupsFilter2", NULL
)) != NULL
);
1259 else if (ppd
->num_filters
> 0)
1261 for (i
= 0; i
< ppd
->num_filters
; i
++)
1262 cupsArrayAdd(pc
->filters
, ppd
->filters
[i
]);
1265 cupsArrayAdd(pc
->filters
, "application/vnd.cups-postscript 0 -");
1268 * See if we have a command filter...
1271 for (filter
= (const char *)cupsArrayFirst(pc
->filters
);
1273 filter
= (const char *)cupsArrayNext(pc
->filters
))
1274 if (!_cups_strncasecmp(filter
, "application/vnd.cups-command", 28) &&
1275 _cups_isspace(filter
[28]))
1279 ((ppd_attr
= ppdFindAttr(ppd
, "cupsCommands", NULL
)) == NULL
||
1280 _cups_strcasecmp(ppd_attr
->value
, "none")))
1283 * No command filter and no cupsCommands keyword telling us not to use one.
1284 * See if this is a PostScript printer, and if so add a PostScript command
1288 for (filter
= (const char *)cupsArrayFirst(pc
->filters
);
1290 filter
= (const char *)cupsArrayNext(pc
->filters
))
1291 if (!_cups_strncasecmp(filter
, "application/vnd.cups-postscript", 31) &&
1292 _cups_isspace(filter
[31]))
1296 cupsArrayAdd(pc
->filters
,
1297 "application/vnd.cups-command application/postscript 100 "
1301 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsPreFilter", NULL
)) != NULL
)
1303 pc
->prefilters
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
1304 (cups_acopy_func_t
)_cupsStrAlloc
,
1305 (cups_afree_func_t
)_cupsStrFree
);
1309 cupsArrayAdd(pc
->prefilters
, ppd_attr
->value
);
1311 while ((ppd_attr
= ppdFindNextAttr(ppd
, "cupsPreFilter", NULL
)) != NULL
);
1314 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsSingleFile", NULL
)) != NULL
)
1315 pc
->single_file
= !_cups_strcasecmp(ppd_attr
->value
, "true");
1318 * Copy the product string, if any...
1322 pc
->product
= _cupsStrAlloc(ppd
->product
);
1325 * Copy finishings mapping data...
1328 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsIPPFinishings", NULL
)) != NULL
)
1330 pc
->finishings
= cupsArrayNew3((cups_array_func_t
)pwg_compare_finishings
,
1331 NULL
, NULL
, 0, NULL
,
1332 (cups_afree_func_t
)pwg_free_finishings
);
1336 if ((finishings
= calloc(1, sizeof(_pwg_finishings_t
))) == NULL
)
1339 finishings
->value
= (ipp_finishings_t
)atoi(ppd_attr
->spec
);
1340 finishings
->num_options
= _ppdParseOptions(ppd_attr
->value
, 0,
1341 &(finishings
->options
),
1342 _PPD_PARSE_OPTIONS
);
1344 cupsArrayAdd(pc
->finishings
, finishings
);
1346 while ((ppd_attr
= ppdFindNextAttr(ppd
, "cupsIPPFinishings",
1354 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsMaxCopies", NULL
)) != NULL
)
1355 pc
->max_copies
= atoi(ppd_attr
->value
);
1356 else if (ppd
->manual_copies
)
1359 pc
->max_copies
= 9999;
1362 * cupsChargeInfoURI, cupsJobAccountId, cupsJobAccountingUserId,
1363 * cupsJobPassword, and cupsMandatory.
1366 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsChargeInfoURI", NULL
)) != NULL
)
1367 pc
->charge_info_uri
= _cupsStrAlloc(ppd_attr
->value
);
1369 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsJobAccountId", NULL
)) != NULL
)
1370 pc
->account_id
= !_cups_strcasecmp(ppd_attr
->value
, "true");
1372 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsJobAccountingUserId", NULL
)) != NULL
)
1373 pc
->accounting_user_id
= !_cups_strcasecmp(ppd_attr
->value
, "true");
1375 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsJobPassword", NULL
)) != NULL
)
1376 pc
->password
= _cupsStrAlloc(ppd_attr
->value
);
1378 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsMandatory", NULL
)) != NULL
)
1379 pc
->mandatory
= _cupsArrayNewStrings(ppd_attr
->value
, ' ');
1385 pc
->support_files
= cupsArrayNew3(NULL
, NULL
, NULL
, 0,
1386 (cups_acopy_func_t
)_cupsStrAlloc
,
1387 (cups_afree_func_t
)_cupsStrFree
);
1389 for (ppd_attr
= ppdFindAttr(ppd
, "cupsICCProfile", NULL
);
1391 ppd_attr
= ppdFindNextAttr(ppd
, "cupsICCProfile", NULL
))
1392 cupsArrayAdd(pc
->support_files
, ppd_attr
->value
);
1394 if ((ppd_attr
= ppdFindAttr(ppd
, "APPrinterIconPath", NULL
)) != NULL
)
1395 cupsArrayAdd(pc
->support_files
, ppd_attr
->value
);
1398 * Return the cache data...
1404 * If we get here we need to destroy the PWG mapping data and return NULL...
1409 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Out of memory."), 1);
1410 _ppdCacheDestroy(pc
);
1417 * '_ppdCacheDestroy()' - Free all memory used for PWG mapping data.
1421 _ppdCacheDestroy(_ppd_cache_t
*pc
) /* I - PPD cache and mapping data */
1423 int i
; /* Looping var */
1424 pwg_map_t
*map
; /* Current map */
1425 pwg_size_t
*size
; /* Current size */
1429 * Range check input...
1436 * Free memory as needed...
1441 for (i
= pc
->num_bins
, map
= pc
->bins
; i
> 0; i
--, map
++)
1443 _cupsStrFree(map
->pwg
);
1444 _cupsStrFree(map
->ppd
);
1452 for (i
= pc
->num_sizes
, size
= pc
->sizes
; i
> 0; i
--, size
++)
1454 _cupsStrFree(size
->map
.pwg
);
1455 _cupsStrFree(size
->map
.ppd
);
1461 if (pc
->source_option
)
1462 _cupsStrFree(pc
->source_option
);
1466 for (i
= pc
->num_sources
, map
= pc
->sources
; i
> 0; i
--, map
++)
1468 _cupsStrFree(map
->pwg
);
1469 _cupsStrFree(map
->ppd
);
1477 for (i
= pc
->num_types
, map
= pc
->types
; i
> 0; i
--, map
++)
1479 _cupsStrFree(map
->pwg
);
1480 _cupsStrFree(map
->ppd
);
1486 if (pc
->custom_max_keyword
)
1487 _cupsStrFree(pc
->custom_max_keyword
);
1489 if (pc
->custom_min_keyword
)
1490 _cupsStrFree(pc
->custom_min_keyword
);
1492 _cupsStrFree(pc
->product
);
1493 cupsArrayDelete(pc
->filters
);
1494 cupsArrayDelete(pc
->prefilters
);
1495 cupsArrayDelete(pc
->finishings
);
1497 _cupsStrFree(pc
->charge_info_uri
);
1498 _cupsStrFree(pc
->password
);
1500 cupsArrayDelete(pc
->mandatory
);
1502 cupsArrayDelete(pc
->support_files
);
1509 * '_ppdCacheGetBin()' - Get the PWG output-bin keyword associated with a PPD
1513 const char * /* O - output-bin or NULL */
1515 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
1516 const char *output_bin
) /* I - PPD OutputBin string */
1518 int i
; /* Looping var */
1522 * Range check input...
1525 if (!pc
|| !output_bin
)
1529 * Look up the OutputBin string...
1533 for (i
= 0; i
< pc
->num_bins
; i
++)
1534 if (!_cups_strcasecmp(output_bin
, pc
->bins
[i
].ppd
))
1535 return (pc
->bins
[i
].pwg
);
1542 * '_ppdCacheGetFinishingOptions()' - Get PPD finishing options for the given
1543 * IPP finishings value(s).
1546 int /* O - New number of options */
1547 _ppdCacheGetFinishingOptions(
1548 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
1549 ipp_t
*job
, /* I - Job attributes or NULL */
1550 ipp_finishings_t value
, /* I - IPP finishings value of IPP_FINISHINGS_NONE */
1551 int num_options
, /* I - Number of options */
1552 cups_option_t
**options
) /* IO - Options */
1554 int i
; /* Looping var */
1555 _pwg_finishings_t
*f
, /* PWG finishings options */
1556 key
; /* Search key */
1557 ipp_attribute_t
*attr
; /* Finishings attribute */
1558 cups_option_t
*option
; /* Current finishings option */
1562 * Range check input...
1565 if (!pc
|| cupsArrayCount(pc
->finishings
) == 0 || !options
||
1566 (!job
&& value
== IPP_FINISHINGS_NONE
))
1567 return (num_options
);
1570 * Apply finishing options...
1573 if (job
&& (attr
= ippFindAttribute(job
, "finishings", IPP_TAG_ENUM
)) != NULL
)
1575 int num_values
= ippGetCount(attr
); /* Number of values */
1577 for (i
= 0; i
< num_values
; i
++)
1579 key
.value
= (ipp_finishings_t
)ippGetInteger(attr
, i
);
1581 if ((f
= cupsArrayFind(pc
->finishings
, &key
)) != NULL
)
1583 int j
; /* Another looping var */
1585 for (j
= f
->num_options
, option
= f
->options
; j
> 0; j
--, option
++)
1586 num_options
= cupsAddOption(option
->name
, option
->value
,
1587 num_options
, options
);
1591 else if (value
!= IPP_FINISHINGS_NONE
)
1595 if ((f
= cupsArrayFind(pc
->finishings
, &key
)) != NULL
)
1597 int j
; /* Another looping var */
1599 for (j
= f
->num_options
, option
= f
->options
; j
> 0; j
--, option
++)
1600 num_options
= cupsAddOption(option
->name
, option
->value
,
1601 num_options
, options
);
1605 return (num_options
);
1610 * '_ppdCacheGetFinishingValues()' - Get IPP finishings value(s) from the given
1614 int /* O - Number of finishings values */
1615 _ppdCacheGetFinishingValues(
1616 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
1617 int num_options
, /* I - Number of options */
1618 cups_option_t
*options
, /* I - Options */
1619 int max_values
, /* I - Maximum number of finishings values */
1620 int *values
) /* O - Finishings values */
1622 int i
, /* Looping var */
1623 num_values
= 0; /* Number of values */
1624 _pwg_finishings_t
*f
; /* Current finishings option */
1625 cups_option_t
*option
; /* Current option */
1626 const char *val
; /* Value for option */
1630 * Range check input...
1633 DEBUG_printf(("_ppdCacheGetFinishingValues(pc=%p, num_options=%d, options=%p, max_values=%d, values=%p)", pc
, num_options
, options
, max_values
, values
));
1635 if (!pc
|| !pc
->finishings
|| num_options
< 1 || max_values
< 1 || !values
)
1637 DEBUG_puts("_ppdCacheGetFinishingValues: Bad arguments, returning 0.");
1642 * Go through the finishings options and see what is set...
1645 for (f
= (_pwg_finishings_t
*)cupsArrayFirst(pc
->finishings
);
1647 f
= (_pwg_finishings_t
*)cupsArrayNext(pc
->finishings
))
1649 DEBUG_printf(("_ppdCacheGetFinishingValues: Checking %d (%s)", f
->value
, ippEnumString("finishings", f
->value
)));
1651 for (i
= f
->num_options
, option
= f
->options
; i
> 0; i
--, option
++)
1653 DEBUG_printf(("_ppdCacheGetFinishingValues: %s=%s?", option
->name
, option
->value
));
1655 if ((val
= cupsGetOption(option
->name
, num_options
, options
)) == NULL
||
1656 _cups_strcasecmp(option
->value
, val
))
1658 DEBUG_puts("_ppdCacheGetFinishingValues: NO");
1665 DEBUG_printf(("_ppdCacheGetFinishingValues: Adding %d.", f
->value
));
1667 values
[num_values
++] = f
->value
;
1669 if (num_values
>= max_values
)
1674 DEBUG_printf(("_ppdCacheGetFinishingValues: Returning %d.", num_values
));
1676 return (num_values
);
1681 * '_ppdCacheGetInputSlot()' - Get the PPD InputSlot associated with the job
1682 * attributes or a keyword string.
1685 const char * /* O - PPD InputSlot or NULL */
1686 _ppdCacheGetInputSlot(
1687 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
1688 ipp_t
*job
, /* I - Job attributes or NULL */
1689 const char *keyword
) /* I - Keyword string or NULL */
1692 * Range check input...
1695 if (!pc
|| pc
->num_sources
== 0 || (!job
&& !keyword
))
1698 if (job
&& !keyword
)
1701 * Lookup the media-col attribute and any media-source found there...
1704 ipp_attribute_t
*media_col
, /* media-col attribute */
1705 *media_source
; /* media-source attribute */
1706 pwg_size_t size
; /* Dimensional size */
1707 int margins_set
; /* Were the margins set? */
1709 media_col
= ippFindAttribute(job
, "media-col", IPP_TAG_BEGIN_COLLECTION
);
1711 (media_source
= ippFindAttribute(ippGetCollection(media_col
, 0),
1713 IPP_TAG_KEYWORD
)) != NULL
)
1716 * Use the media-source value from media-col...
1719 keyword
= ippGetString(media_source
, 0, NULL
);
1721 else if (pwgInitSize(&size
, job
, &margins_set
))
1724 * For media <= 5x7, look for a photo tray...
1727 if (size
.width
<= (5 * 2540) && size
.length
<= (7 * 2540))
1734 int i
; /* Looping var */
1736 for (i
= 0; i
< pc
->num_sources
; i
++)
1737 if (!_cups_strcasecmp(keyword
, pc
->sources
[i
].pwg
))
1738 return (pc
->sources
[i
].ppd
);
1746 * '_ppdCacheGetMediaType()' - Get the PPD MediaType associated with the job
1747 * attributes or a keyword string.
1750 const char * /* O - PPD MediaType or NULL */
1751 _ppdCacheGetMediaType(
1752 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
1753 ipp_t
*job
, /* I - Job attributes or NULL */
1754 const char *keyword
) /* I - Keyword string or NULL */
1757 * Range check input...
1760 if (!pc
|| pc
->num_types
== 0 || (!job
&& !keyword
))
1763 if (job
&& !keyword
)
1766 * Lookup the media-col attribute and any media-source found there...
1769 ipp_attribute_t
*media_col
, /* media-col attribute */
1770 *media_type
; /* media-type attribute */
1772 media_col
= ippFindAttribute(job
, "media-col", IPP_TAG_BEGIN_COLLECTION
);
1775 if ((media_type
= ippFindAttribute(media_col
->values
[0].collection
,
1777 IPP_TAG_KEYWORD
)) == NULL
)
1778 media_type
= ippFindAttribute(media_col
->values
[0].collection
,
1779 "media-type", IPP_TAG_NAME
);
1782 keyword
= media_type
->values
[0].string
.text
;
1788 int i
; /* Looping var */
1790 for (i
= 0; i
< pc
->num_types
; i
++)
1791 if (!_cups_strcasecmp(keyword
, pc
->types
[i
].pwg
))
1792 return (pc
->types
[i
].ppd
);
1800 * '_ppdCacheGetOutputBin()' - Get the PPD OutputBin associated with the keyword
1804 const char * /* O - PPD OutputBin or NULL */
1805 _ppdCacheGetOutputBin(
1806 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
1807 const char *output_bin
) /* I - Keyword string */
1809 int i
; /* Looping var */
1813 * Range check input...
1816 if (!pc
|| !output_bin
)
1820 * Look up the OutputBin string...
1824 for (i
= 0; i
< pc
->num_bins
; i
++)
1825 if (!_cups_strcasecmp(output_bin
, pc
->bins
[i
].pwg
))
1826 return (pc
->bins
[i
].ppd
);
1833 * '_ppdCacheGetPageSize()' - Get the PPD PageSize associated with the job
1834 * attributes or a keyword string.
1837 const char * /* O - PPD PageSize or NULL */
1838 _ppdCacheGetPageSize(
1839 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
1840 ipp_t
*job
, /* I - Job attributes or NULL */
1841 const char *keyword
, /* I - Keyword string or NULL */
1842 int *exact
) /* O - 1 if exact match, 0 otherwise */
1844 int i
; /* Looping var */
1845 pwg_size_t
*size
, /* Current size */
1846 *closest
, /* Closest size */
1847 jobsize
; /* Size data from job */
1848 int margins_set
, /* Were the margins set? */
1849 dwidth
, /* Difference in width */
1850 dlength
, /* Difference in length */
1851 dleft
, /* Difference in left margins */
1852 dright
, /* Difference in right margins */
1853 dbottom
, /* Difference in bottom margins */
1854 dtop
, /* Difference in top margins */
1855 dmin
, /* Minimum difference */
1856 dclosest
; /* Closest difference */
1857 const char *ppd_name
; /* PPD media name */
1860 DEBUG_printf(("_ppdCacheGetPageSize(pc=%p, job=%p, keyword=\"%s\", exact=%p)",
1861 pc
, job
, keyword
, exact
));
1864 * Range check input...
1867 if (!pc
|| (!job
&& !keyword
))
1878 * Try getting the PPD media name from the job attributes...
1881 ipp_attribute_t
*attr
; /* Job attribute */
1883 if ((attr
= ippFindAttribute(job
, "PageSize", IPP_TAG_ZERO
)) == NULL
)
1884 if ((attr
= ippFindAttribute(job
, "PageRegion", IPP_TAG_ZERO
)) == NULL
)
1885 attr
= ippFindAttribute(job
, "media", IPP_TAG_ZERO
);
1889 DEBUG_printf(("1_ppdCacheGetPageSize: Found attribute %s (%s)",
1890 attr
->name
, ippTagString(attr
->value_tag
)));
1892 DEBUG_puts("1_ppdCacheGetPageSize: Did not find media attribute.");
1895 if (attr
&& (attr
->value_tag
== IPP_TAG_NAME
||
1896 attr
->value_tag
== IPP_TAG_KEYWORD
))
1897 ppd_name
= attr
->values
[0].string
.text
;
1900 DEBUG_printf(("1_ppdCacheGetPageSize: ppd_name=\"%s\"", ppd_name
));
1905 * Try looking up the named PPD size first...
1908 for (i
= pc
->num_sizes
, size
= pc
->sizes
; i
> 0; i
--, size
++)
1910 DEBUG_printf(("2_ppdCacheGetPageSize: size[%d]=[\"%s\" \"%s\"]",
1911 (int)(size
- pc
->sizes
), size
->map
.pwg
, size
->map
.ppd
));
1913 if (!_cups_strcasecmp(ppd_name
, size
->map
.ppd
) ||
1914 !_cups_strcasecmp(ppd_name
, size
->map
.pwg
))
1919 DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\"", ppd_name
));
1921 return (size
->map
.ppd
);
1926 if (job
&& !keyword
)
1929 * Get the size using media-col or media, with the preference being
1933 if (!pwgInitSize(&jobsize
, job
, &margins_set
))
1939 * Get the size using a media keyword...
1942 pwg_media_t
*media
; /* Media definition */
1945 if ((media
= pwgMediaForPWG(keyword
)) == NULL
)
1946 if ((media
= pwgMediaForLegacy(keyword
)) == NULL
)
1947 if ((media
= pwgMediaForPPD(keyword
)) == NULL
)
1950 jobsize
.width
= media
->width
;
1951 jobsize
.length
= media
->length
;
1956 * Now that we have the dimensions and possibly the margins, look at the
1957 * available sizes and find the match...
1961 dclosest
= 999999999;
1963 if (!ppd_name
|| _cups_strncasecmp(ppd_name
, "Custom.", 7) ||
1964 _cups_strncasecmp(ppd_name
, "custom_", 7))
1966 for (i
= pc
->num_sizes
, size
= pc
->sizes
; i
> 0; i
--, size
++)
1969 * Adobe uses a size matching algorithm with an epsilon of 5 points, which
1970 * is just about 176/2540ths...
1973 dwidth
= size
->width
- jobsize
.width
;
1974 dlength
= size
->length
- jobsize
.length
;
1976 if (dwidth
<= -176 || dwidth
>= 176 || dlength
<= -176 || dlength
>= 176)
1982 * Use a tighter epsilon of 1 point (35/2540ths) for margins...
1985 dleft
= size
->left
- jobsize
.left
;
1986 dright
= size
->right
- jobsize
.right
;
1987 dtop
= size
->top
- jobsize
.top
;
1988 dbottom
= size
->bottom
- jobsize
.bottom
;
1990 if (dleft
<= -35 || dleft
>= 35 || dright
<= -35 || dright
>= 35 ||
1991 dtop
<= -35 || dtop
>= 35 || dbottom
<= -35 || dbottom
>= 35)
1993 dleft
= dleft
< 0 ? -dleft
: dleft
;
1994 dright
= dright
< 0 ? -dright
: dright
;
1995 dbottom
= dbottom
< 0 ? -dbottom
: dbottom
;
1996 dtop
= dtop
< 0 ? -dtop
: dtop
;
1997 dmin
= dleft
+ dright
+ dbottom
+ dtop
;
1999 if (dmin
< dclosest
)
2012 DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\"", size
->map
.ppd
));
2014 return (size
->map
.ppd
);
2020 DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\" (closest)",
2023 return (closest
->map
.ppd
);
2027 * If we get here we need to check for custom page size support...
2030 if (jobsize
.width
>= pc
->custom_min_width
&&
2031 jobsize
.width
<= pc
->custom_max_width
&&
2032 jobsize
.length
>= pc
->custom_min_length
&&
2033 jobsize
.length
<= pc
->custom_max_length
)
2036 * In range, format as Custom.WWWWxLLLL (points).
2039 snprintf(pc
->custom_ppd_size
, sizeof(pc
->custom_ppd_size
), "Custom.%dx%d",
2040 (int)PWG_TO_POINTS(jobsize
.width
), (int)PWG_TO_POINTS(jobsize
.length
));
2042 if (margins_set
&& exact
)
2044 dleft
= pc
->custom_size
.left
- jobsize
.left
;
2045 dright
= pc
->custom_size
.right
- jobsize
.right
;
2046 dtop
= pc
->custom_size
.top
- jobsize
.top
;
2047 dbottom
= pc
->custom_size
.bottom
- jobsize
.bottom
;
2049 if (dleft
> -35 && dleft
< 35 && dright
> -35 && dright
< 35 &&
2050 dtop
> -35 && dtop
< 35 && dbottom
> -35 && dbottom
< 35)
2056 DEBUG_printf(("1_ppdCacheGetPageSize: Returning \"%s\" (custom)",
2057 pc
->custom_ppd_size
));
2059 return (pc
->custom_ppd_size
);
2063 * No custom page size support or the size is out of range - return NULL.
2066 DEBUG_puts("1_ppdCacheGetPageSize: Returning NULL");
2073 * '_ppdCacheGetSize()' - Get the PWG size associated with a PPD PageSize.
2076 pwg_size_t
* /* O - PWG size or NULL */
2078 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2079 const char *page_size
) /* I - PPD PageSize */
2081 int i
; /* Looping var */
2082 pwg_media_t
*media
; /* Media */
2083 pwg_size_t
*size
; /* Current size */
2087 * Range check input...
2090 if (!pc
|| !page_size
)
2093 if (!_cups_strncasecmp(page_size
, "Custom.", 7))
2096 * Custom size; size name can be one of the following:
2098 * Custom.WIDTHxLENGTHin - Size in inches
2099 * Custom.WIDTHxLENGTHft - Size in feet
2100 * Custom.WIDTHxLENGTHcm - Size in centimeters
2101 * Custom.WIDTHxLENGTHmm - Size in millimeters
2102 * Custom.WIDTHxLENGTHm - Size in meters
2103 * Custom.WIDTHxLENGTH[pt] - Size in points
2106 double w
, l
; /* Width and length of page */
2107 char *ptr
; /* Pointer into PageSize */
2108 struct lconv
*loc
; /* Locale data */
2111 w
= (float)_cupsStrScand(page_size
+ 7, &ptr
, loc
);
2112 if (!ptr
|| *ptr
!= 'x')
2115 l
= (float)_cupsStrScand(ptr
+ 1, &ptr
, loc
);
2119 if (!_cups_strcasecmp(ptr
, "in"))
2124 else if (!_cups_strcasecmp(ptr
, "ft"))
2129 else if (!_cups_strcasecmp(ptr
, "mm"))
2134 else if (!_cups_strcasecmp(ptr
, "cm"))
2139 else if (!_cups_strcasecmp(ptr
, "m"))
2150 pc
->custom_size
.width
= (int)w
;
2151 pc
->custom_size
.length
= (int)l
;
2153 return (&(pc
->custom_size
));
2157 * Not a custom size - look it up...
2160 for (i
= pc
->num_sizes
, size
= pc
->sizes
; i
> 0; i
--, size
++)
2161 if (!_cups_strcasecmp(page_size
, size
->map
.ppd
) ||
2162 !_cups_strcasecmp(page_size
, size
->map
.pwg
))
2166 * Look up standard sizes...
2169 if ((media
= pwgMediaForPPD(page_size
)) == NULL
)
2170 if ((media
= pwgMediaForLegacy(page_size
)) == NULL
)
2171 media
= pwgMediaForPWG(page_size
);
2175 pc
->custom_size
.width
= media
->width
;
2176 pc
->custom_size
.length
= media
->length
;
2178 return (&(pc
->custom_size
));
2186 * '_ppdCacheGetSource()' - Get the PWG media-source associated with a PPD
2190 const char * /* O - PWG media-source keyword */
2192 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2193 const char *input_slot
) /* I - PPD InputSlot */
2195 int i
; /* Looping var */
2196 pwg_map_t
*source
; /* Current source */
2200 * Range check input...
2203 if (!pc
|| !input_slot
)
2206 for (i
= pc
->num_sources
, source
= pc
->sources
; i
> 0; i
--, source
++)
2207 if (!_cups_strcasecmp(input_slot
, source
->ppd
))
2208 return (source
->pwg
);
2215 * '_ppdCacheGetType()' - Get the PWG media-type associated with a PPD
2219 const char * /* O - PWG media-type keyword */
2221 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2222 const char *media_type
) /* I - PPD MediaType */
2224 int i
; /* Looping var */
2225 pwg_map_t
*type
; /* Current type */
2229 * Range check input...
2232 if (!pc
|| !media_type
)
2235 for (i
= pc
->num_types
, type
= pc
->types
; i
> 0; i
--, type
++)
2236 if (!_cups_strcasecmp(media_type
, type
->ppd
))
2244 * '_ppdCacheWriteFile()' - Write PWG mapping data to a file.
2247 int /* O - 1 on success, 0 on failure */
2249 _ppd_cache_t
*pc
, /* I - PPD cache and mapping data */
2250 const char *filename
, /* I - File to write */
2251 ipp_t
*attrs
) /* I - Attributes to write, if any */
2253 int i
, j
, k
; /* Looping vars */
2254 cups_file_t
*fp
; /* Output file */
2255 pwg_size_t
*size
; /* Current size */
2256 pwg_map_t
*map
; /* Current map */
2257 _pwg_finishings_t
*f
; /* Current finishing option */
2258 cups_option_t
*option
; /* Current option */
2259 const char *value
; /* Filter/pre-filter value */
2260 char newfile
[1024]; /* New filename */
2264 * Range check input...
2267 if (!pc
|| !filename
)
2269 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(EINVAL
), 0);
2274 * Open the file and write with compression...
2277 snprintf(newfile
, sizeof(newfile
), "%s.N", filename
);
2278 if ((fp
= cupsFileOpen(newfile
, "w9")) == NULL
)
2280 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, strerror(errno
), 0);
2285 * Standard header...
2288 cupsFilePrintf(fp
, "#CUPS-PPD-CACHE-%d\n", _PPD_CACHE_VERSION
);
2294 if (pc
->num_bins
> 0)
2296 cupsFilePrintf(fp
, "NumBins %d\n", pc
->num_bins
);
2297 for (i
= pc
->num_bins
, map
= pc
->bins
; i
> 0; i
--, map
++)
2298 cupsFilePrintf(fp
, "Bin %s %s\n", map
->pwg
, map
->ppd
);
2305 cupsFilePrintf(fp
, "NumSizes %d\n", pc
->num_sizes
);
2306 for (i
= pc
->num_sizes
, size
= pc
->sizes
; i
> 0; i
--, size
++)
2307 cupsFilePrintf(fp
, "Size %s %s %d %d %d %d %d %d\n", size
->map
.pwg
,
2308 size
->map
.ppd
, size
->width
, size
->length
, size
->left
,
2309 size
->bottom
, size
->right
, size
->top
);
2310 if (pc
->custom_max_width
> 0)
2311 cupsFilePrintf(fp
, "CustomSize %d %d %d %d %d %d %d %d\n",
2312 pc
->custom_max_width
, pc
->custom_max_length
,
2313 pc
->custom_min_width
, pc
->custom_min_length
,
2314 pc
->custom_size
.left
, pc
->custom_size
.bottom
,
2315 pc
->custom_size
.right
, pc
->custom_size
.top
);
2321 if (pc
->source_option
)
2322 cupsFilePrintf(fp
, "SourceOption %s\n", pc
->source_option
);
2324 if (pc
->num_sources
> 0)
2326 cupsFilePrintf(fp
, "NumSources %d\n", pc
->num_sources
);
2327 for (i
= pc
->num_sources
, map
= pc
->sources
; i
> 0; i
--, map
++)
2328 cupsFilePrintf(fp
, "Source %s %s\n", map
->pwg
, map
->ppd
);
2335 if (pc
->num_types
> 0)
2337 cupsFilePrintf(fp
, "NumTypes %d\n", pc
->num_types
);
2338 for (i
= pc
->num_types
, map
= pc
->types
; i
> 0; i
--, map
++)
2339 cupsFilePrintf(fp
, "Type %s %s\n", map
->pwg
, map
->ppd
);
2346 for (i
= _PWG_PRINT_COLOR_MODE_MONOCHROME
; i
< _PWG_PRINT_COLOR_MODE_MAX
; i
++)
2347 for (j
= _PWG_PRINT_QUALITY_DRAFT
; j
< _PWG_PRINT_QUALITY_MAX
; j
++)
2348 if (pc
->num_presets
[i
][j
])
2350 cupsFilePrintf(fp
, "Preset %d %d", i
, j
);
2351 for (k
= pc
->num_presets
[i
][j
], option
= pc
->presets
[i
][j
];
2354 cupsFilePrintf(fp
, " %s=%s", option
->name
, option
->value
);
2355 cupsFilePutChar(fp
, '\n');
2362 if (pc
->sides_option
)
2363 cupsFilePrintf(fp
, "SidesOption %s\n", pc
->sides_option
);
2365 if (pc
->sides_1sided
)
2366 cupsFilePrintf(fp
, "Sides1Sided %s\n", pc
->sides_1sided
);
2368 if (pc
->sides_2sided_long
)
2369 cupsFilePrintf(fp
, "Sides2SidedLong %s\n", pc
->sides_2sided_long
);
2371 if (pc
->sides_2sided_short
)
2372 cupsFilePrintf(fp
, "Sides2SidedShort %s\n", pc
->sides_2sided_short
);
2375 * Product, cupsFilter, cupsFilter2, and cupsPreFilter...
2379 cupsFilePutConf(fp
, "Product", pc
->product
);
2381 for (value
= (const char *)cupsArrayFirst(pc
->filters
);
2383 value
= (const char *)cupsArrayNext(pc
->filters
))
2384 cupsFilePutConf(fp
, "Filter", value
);
2386 for (value
= (const char *)cupsArrayFirst(pc
->prefilters
);
2388 value
= (const char *)cupsArrayNext(pc
->prefilters
))
2389 cupsFilePutConf(fp
, "PreFilter", value
);
2391 cupsFilePrintf(fp
, "SingleFile %s\n", pc
->single_file
? "true" : "false");
2394 * Finishing options...
2397 for (f
= (_pwg_finishings_t
*)cupsArrayFirst(pc
->finishings
);
2399 f
= (_pwg_finishings_t
*)cupsArrayNext(pc
->finishings
))
2401 cupsFilePrintf(fp
, "Finishings %d", f
->value
);
2402 for (i
= f
->num_options
, option
= f
->options
; i
> 0; i
--, option
++)
2403 cupsFilePrintf(fp
, " %s=%s", option
->name
, option
->value
);
2404 cupsFilePutChar(fp
, '\n');
2411 cupsFilePrintf(fp
, "MaxCopies %d\n", pc
->max_copies
);
2414 * Accounting/quota/PIN/managed printing values...
2417 if (pc
->charge_info_uri
)
2418 cupsFilePutConf(fp
, "ChargeInfoURI", pc
->charge_info_uri
);
2420 cupsFilePrintf(fp
, "AccountId %s\n", pc
->account_id
? "true" : "false");
2421 cupsFilePrintf(fp
, "AccountingUserId %s\n",
2422 pc
->accounting_user_id
? "true" : "false");
2425 cupsFilePutConf(fp
, "Password", pc
->password
);
2427 for (value
= (char *)cupsArrayFirst(pc
->mandatory
);
2429 value
= (char *)cupsArrayNext(pc
->mandatory
))
2430 cupsFilePutConf(fp
, "Mandatory", value
);
2436 for (value
= (char *)cupsArrayFirst(pc
->support_files
);
2438 value
= (char *)cupsArrayNext(pc
->support_files
))
2439 cupsFilePutConf(fp
, "SupportFile", value
);
2442 * IPP attributes, if any...
2447 cupsFilePrintf(fp
, "IPP " CUPS_LLFMT
"\n", CUPS_LLCAST
ippLength(attrs
));
2449 attrs
->state
= IPP_STATE_IDLE
;
2450 ippWriteIO(fp
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
, attrs
);
2454 * Close and return...
2457 if (cupsFileClose(fp
))
2464 return (!rename(newfile
, filename
));
2469 * '_pwgInputSlotForSource()' - Get the InputSlot name for the given PWG
2473 const char * /* O - InputSlot name */
2474 _pwgInputSlotForSource(
2475 const char *media_source
, /* I - PWG media-source */
2476 char *name
, /* I - Name buffer */
2477 size_t namesize
) /* I - Size of name buffer */
2480 * Range check input...
2483 if (!media_source
|| !name
|| namesize
< PPD_MAX_NAME
)
2486 if (_cups_strcasecmp(media_source
, "main"))
2487 strlcpy(name
, "Cassette", namesize
);
2488 else if (_cups_strcasecmp(media_source
, "alternate"))
2489 strlcpy(name
, "Multipurpose", namesize
);
2490 else if (_cups_strcasecmp(media_source
, "large-capacity"))
2491 strlcpy(name
, "LargeCapacity", namesize
);
2492 else if (_cups_strcasecmp(media_source
, "bottom"))
2493 strlcpy(name
, "Lower", namesize
);
2494 else if (_cups_strcasecmp(media_source
, "middle"))
2495 strlcpy(name
, "Middle", namesize
);
2496 else if (_cups_strcasecmp(media_source
, "top"))
2497 strlcpy(name
, "Upper", namesize
);
2498 else if (_cups_strcasecmp(media_source
, "rear"))
2499 strlcpy(name
, "Rear", namesize
);
2500 else if (_cups_strcasecmp(media_source
, "side"))
2501 strlcpy(name
, "Side", namesize
);
2502 else if (_cups_strcasecmp(media_source
, "envelope"))
2503 strlcpy(name
, "Envelope", namesize
);
2504 else if (_cups_strcasecmp(media_source
, "main-roll"))
2505 strlcpy(name
, "Roll", namesize
);
2506 else if (_cups_strcasecmp(media_source
, "alternate-roll"))
2507 strlcpy(name
, "Roll2", namesize
);
2509 pwg_ppdize_name(media_source
, name
, namesize
);
2516 * '_pwgMediaTypeForType()' - Get the MediaType name for the given PWG
2520 const char * /* O - MediaType name */
2521 _pwgMediaTypeForType(
2522 const char *media_type
, /* I - PWG media-type */
2523 char *name
, /* I - Name buffer */
2524 size_t namesize
) /* I - Size of name buffer */
2527 * Range check input...
2530 if (!media_type
|| !name
|| namesize
< PPD_MAX_NAME
)
2533 if (_cups_strcasecmp(media_type
, "auto"))
2534 strlcpy(name
, "Auto", namesize
);
2535 else if (_cups_strcasecmp(media_type
, "cardstock"))
2536 strlcpy(name
, "Cardstock", namesize
);
2537 else if (_cups_strcasecmp(media_type
, "envelope"))
2538 strlcpy(name
, "Envelope", namesize
);
2539 else if (_cups_strcasecmp(media_type
, "photographic-glossy"))
2540 strlcpy(name
, "Glossy", namesize
);
2541 else if (_cups_strcasecmp(media_type
, "photographic-high-gloss"))
2542 strlcpy(name
, "HighGloss", namesize
);
2543 else if (_cups_strcasecmp(media_type
, "photographic-matte"))
2544 strlcpy(name
, "Matte", namesize
);
2545 else if (_cups_strcasecmp(media_type
, "stationery"))
2546 strlcpy(name
, "Plain", namesize
);
2547 else if (_cups_strcasecmp(media_type
, "stationery-coated"))
2548 strlcpy(name
, "Coated", namesize
);
2549 else if (_cups_strcasecmp(media_type
, "stationery-inkjet"))
2550 strlcpy(name
, "Inkjet", namesize
);
2551 else if (_cups_strcasecmp(media_type
, "stationery-letterhead"))
2552 strlcpy(name
, "Letterhead", namesize
);
2553 else if (_cups_strcasecmp(media_type
, "stationery-preprinted"))
2554 strlcpy(name
, "Preprinted", namesize
);
2555 else if (_cups_strcasecmp(media_type
, "transparency"))
2556 strlcpy(name
, "Transparency", namesize
);
2558 pwg_ppdize_name(media_type
, name
, namesize
);
2565 * '_pwgPageSizeForMedia()' - Get the PageSize name for the given media.
2568 const char * /* O - PageSize name */
2569 _pwgPageSizeForMedia(
2570 pwg_media_t
*media
, /* I - Media */
2571 char *name
, /* I - PageSize name buffer */
2572 size_t namesize
) /* I - Size of name buffer */
2574 const char *sizeptr
, /* Pointer to size in PWG name */
2575 *dimptr
; /* Pointer to dimensions in PWG name */
2579 * Range check input...
2582 if (!media
|| !name
|| namesize
< PPD_MAX_NAME
)
2586 * Copy or generate a PageSize name...
2592 * Use a standard Adobe name...
2595 strlcpy(name
, media
->ppd
, namesize
);
2597 else if (!media
->pwg
|| !strncmp(media
->pwg
, "custom_", 7) ||
2598 (sizeptr
= strchr(media
->pwg
, '_')) == NULL
||
2599 (dimptr
= strchr(sizeptr
+ 1, '_')) == NULL
||
2600 (size_t)(dimptr
- sizeptr
) > namesize
)
2603 * Use a name of the form "wNNNhNNN"...
2606 snprintf(name
, namesize
, "w%dh%d", (int)PWG_TO_POINTS(media
->width
),
2607 (int)PWG_TO_POINTS(media
->length
));
2612 * Copy the size name from class_sizename_dimensions...
2615 memcpy(name
, sizeptr
+ 1, dimptr
- sizeptr
- 1);
2616 name
[dimptr
- sizeptr
- 1] = '\0';
2624 * 'pwg_compare_finishings()' - Compare two finishings values.
2627 static int /* O- Result of comparison */
2628 pwg_compare_finishings(
2629 _pwg_finishings_t
*a
, /* I - First finishings value */
2630 _pwg_finishings_t
*b
) /* I - Second finishings value */
2632 return (b
->value
- a
->value
);
2637 * 'pwg_free_finishings()' - Free a finishings value.
2641 pwg_free_finishings(
2642 _pwg_finishings_t
*f
) /* I - Finishings value */
2644 cupsFreeOptions(f
->num_options
, f
->options
);
2650 * 'pwg_ppdize_name()' - Convert an IPP keyword to a PPD keyword.
2654 pwg_ppdize_name(const char *ipp
, /* I - IPP keyword */
2655 char *name
, /* I - Name buffer */
2656 size_t namesize
) /* I - Size of name buffer */
2658 char *ptr
, /* Pointer into name buffer */
2659 *end
; /* End of name buffer */
2662 *name
= (char)toupper(*ipp
++);
2664 for (ptr
= name
+ 1, end
= name
+ namesize
- 1; *ipp
&& ptr
< end
;)
2666 if (*ipp
== '-' && _cups_isalpha(ipp
[1]))
2669 *ptr
++ = (char)toupper(*ipp
++ & 255);
2680 * 'pwg_unppdize_name()' - Convert a PPD keyword to a lowercase IPP keyword.
2684 pwg_unppdize_name(const char *ppd
, /* I - PPD keyword */
2685 char *name
, /* I - Name buffer */
2686 size_t namesize
, /* I - Size of name buffer */
2687 const char *dashchars
)/* I - Characters to be replaced by dashes */
2689 char *ptr
, /* Pointer into name buffer */
2690 *end
; /* End of name buffer */
2693 for (ptr
= name
, end
= name
+ namesize
- 1; *ppd
&& ptr
< end
; ppd
++)
2695 if (_cups_isalnum(*ppd
) || *ppd
== '-')
2696 *ptr
++ = (char)tolower(*ppd
& 255);
2697 else if (strchr(dashchars
, *ppd
))
2702 if (!_cups_isupper(*ppd
) && _cups_isalnum(*ppd
) &&
2703 _cups_isupper(ppd
[1]) && ptr
< end
)