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