]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/emit.c
Merge changes from CUPS 1.4svn-r7485.
[thirdparty/cups.git] / cups / emit.c
CommitLineData
ef416fc2 1/*
bc44d920 2 * "$Id: emit.c 6649 2007-07-11 21:46:42Z mike $"
ef416fc2 3 *
4 * PPD code emission routines for the Common UNIX Printing System (CUPS).
5 *
91c84a35 6 * Copyright 2007-2008 by Apple Inc.
b86bc4cf 7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
ef416fc2 8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 14 *
15 * PostScript is a trademark of Adobe Systems, Inc.
16 *
17 * This file is subject to the Apple OS-Developed Software exception.
18 *
19 * Contents:
20 *
fa73b229 21 * ppdCollect() - Collect all marked options that reside in the
22 * specified section.
23 * ppdCollect2() - Collect all marked options that reside in the
24 * specified section and minimum order.
25 * ppdEmit() - Emit code for marked options to a file.
757d2cad 26 * ppdEmitAfterOrder() - Emit a subset of the code for marked options to a
27 * file.
fa73b229 28 * ppdEmitFd() - Emit code for marked options to a file.
29 * ppdEmitJCL() - Emit code for JCL options to a file.
30 * ppdEmitJCLEnd() - Emit JCLEnd code to a file.
757d2cad 31 * ppdEmitString() - Get a string containing the code for marked options.
fa73b229 32 * ppd_handle_media() - Handle media selection...
ef416fc2 33 */
34
35/*
36 * Include necessary headers...
37 */
38
39#include "ppd.h"
40#include <stdlib.h>
41#include "string.h"
757d2cad 42#include <errno.h>
8ca02f3c 43#include "debug.h"
ef416fc2 44
45#if defined(WIN32) || defined(__EMX__)
46# include <io.h>
47#else
48# include <unistd.h>
49#endif /* WIN32 || __EMX__ */
50
51
52/*
53 * Local functions...
54 */
55
56static void ppd_handle_media(ppd_file_t *ppd);
ef416fc2 57
58
59/*
60 * Local globals...
61 */
62
63static const char ppd_custom_code[] =
64 "pop pop pop\n"
65 "<</PageSize[5 -2 roll]/ImagingBBox null>>setpagedevice\n";
66
67
68/*
69 * 'ppdCollect()' - Collect all marked options that reside in the specified
70 * section.
5a738aea
MS
71 *
72 * The choices array should be freed using @code free@ when you are
73 * finished with it.
ef416fc2 74 */
75
76int /* O - Number of options marked */
77ppdCollect(ppd_file_t *ppd, /* I - PPD file data */
78 ppd_section_t section, /* I - Section to collect */
79 ppd_choice_t ***choices) /* O - Pointers to choices */
fa73b229 80{
81 return (ppdCollect2(ppd, section, 0.0, choices));
82}
83
84
85/*
86 * 'ppdCollect2()' - Collect all marked options that reside in the
87 * specified section and minimum order.
88 *
5a738aea
MS
89 * The choices array should be freed using @code free@ when you are
90 * finished with it.
91 *
fa73b229 92 * @since CUPS 1.2@
93 */
94
95int /* O - Number of options marked */
96ppdCollect2(ppd_file_t *ppd, /* I - PPD file data */
97 ppd_section_t section, /* I - Section to collect */
98 float min_order, /* I - Minimum OrderDependency value */
99 ppd_choice_t ***choices) /* O - Pointers to choices */
ef416fc2 100{
ef416fc2 101 ppd_choice_t *c; /* Current choice */
0a682745
MS
102 ppd_section_t csection; /* Current section */
103 float corder; /* Current OrderDependency value */
ef416fc2 104 int count; /* Number of choices collected */
105 ppd_choice_t **collect; /* Collected choices */
0a682745 106 float *orders; /* Collected order values */
ef416fc2 107
108
8ca02f3c 109 DEBUG_printf(("ppdCollect2(ppd=%p, section=%d, min_order=%f, choices=%p)\n",
110 ppd, section, min_order, choices));
111
91c84a35
MS
112 if (!ppd || !choices)
113 {
114 if (choices)
115 *choices = NULL;
116
ef416fc2 117 return (0);
91c84a35 118 }
ef416fc2 119
120 /*
0a682745 121 * Allocate memory for up to N selected choices...
ef416fc2 122 */
123
91c84a35
MS
124 count = 0;
125 if ((collect = calloc(sizeof(ppd_choice_t *),
126 cupsArrayCount(ppd->marked))) == NULL)
127 {
128 *choices = NULL;
129 return (0);
130 }
131
132 if ((orders = calloc(sizeof(float), cupsArrayCount(ppd->marked))) == NULL)
133 {
134 *choices = NULL;
135 free(collect);
136 return (0);
137 }
ef416fc2 138
139 /*
140 * Loop through all options and add choices as needed...
141 */
142
0a682745
MS
143 for (c = (ppd_choice_t *)cupsArrayFirst(ppd->marked);
144 c;
145 c = (ppd_choice_t *)cupsArrayNext(ppd->marked))
ef416fc2 146 {
0a682745
MS
147 csection = c->option->section;
148 corder = c->option->order;
149
150 if (!strcmp(c->choice, "Custom"))
151 {
152 ppd_attr_t *attr; /* NonUIOrderDependency value */
153 float aorder; /* Order value */
154 char asection[17], /* Section name */
155 amain[PPD_MAX_NAME + 1],
156 aoption[PPD_MAX_NAME];
157 /* *CustomFoo and True */
158
159
160 for (attr = ppdFindAttr(ppd, "NonUIOrderDependency", NULL);
161 attr;
162 attr = ppdFindNextAttr(ppd, "NonUIOrderDependency", NULL))
163 if (attr->value &&
164 sscanf(attr->value, "%f%16s%41s%40s", &aorder, asection, amain,
165 aoption) == 4 &&
166 !strncmp(amain, "*Custom", 7) &&
167 !strcmp(amain + 7, c->option->keyword) && !strcmp(aoption, "True"))
168 {
169 /*
170 * Use this NonUIOrderDependency...
171 */
ef416fc2 172
0a682745
MS
173 corder = aorder;
174
175 if (!strcmp(asection, "DocumentSetup"))
176 csection = PPD_ORDER_DOCUMENT;
177 else if (!strcmp(asection, "ExitServer"))
178 csection = PPD_ORDER_EXIT;
179 else if (!strcmp(asection, "JCLSetup"))
180 csection = PPD_ORDER_JCL;
181 else if (!strcmp(asection, "PageSetup"))
182 csection = PPD_ORDER_PAGE;
183 else if (!strcmp(asection, "Prolog"))
184 csection = PPD_ORDER_PROLOG;
185 else
186 csection = PPD_ORDER_ANY;
187
188 break;
189 }
190 }
191
192 if (csection == section && corder >= min_order)
193 {
194 collect[count] = c;
195 orders[count] = corder;
196 count ++;
197 }
ef416fc2 198 }
199
200 /*
201 * If we have more than 1 marked choice, sort them...
202 */
203
204 if (count > 1)
0a682745
MS
205 {
206 int i, j; /* Looping vars */
207
208 for (i = 0; i < (count - 1); i ++)
209 for (j = i + 1; j < count; j ++)
210 if (orders[i] > orders[j])
211 {
212 c = collect[i];
213 corder = orders[i];
214 collect[i] = collect[j];
215 orders[i] = orders[j];
216 collect[j] = c;
217 orders[j] = corder;
218 }
219 }
220
221 free(orders);
ef416fc2 222
8ca02f3c 223 DEBUG_printf(("ppdCollect2: %d marked choices...\n", count));
224
ef416fc2 225 /*
226 * Return the array and number of choices; if 0, free the array since
227 * it isn't needed.
228 */
229
230 if (count > 0)
231 {
232 *choices = collect;
233 return (count);
234 }
235 else
236 {
237 *choices = NULL;
238 free(collect);
239 return (0);
240 }
241}
242
243
244/*
245 * 'ppdEmit()' - Emit code for marked options to a file.
246 */
247
248int /* O - 0 on success, -1 on failure */
249ppdEmit(ppd_file_t *ppd, /* I - PPD file record */
250 FILE *fp, /* I - File to write to */
251 ppd_section_t section) /* I - Section to write */
fa73b229 252{
253 return (ppdEmitAfterOrder(ppd, fp, section, 0, 0.0));
254}
255
256
257/*
258 * 'ppdEmitAfterOrder()' - Emit a subset of the code for marked options to a file.
259 *
260 * When "limit" is non-zero, this function only emits options whose
261 * OrderDependency value is greater than or equal to "min_order".
262 *
263 * When "limit" is zero, this function is identical to ppdEmit().
264 *
265 * @since CUPS 1.2@
266 */
267
268int /* O - 0 on success, -1 on failure */
269ppdEmitAfterOrder(
270 ppd_file_t *ppd, /* I - PPD file record */
271 FILE *fp, /* I - File to write to */
272 ppd_section_t section, /* I - Section to write */
757d2cad 273 int limit, /* I - Non-zero to use min_order */
274 float min_order) /* I - Lowest OrderDependency */
ef416fc2 275{
757d2cad 276 char *buffer; /* Option code */
277 int status; /* Return status */
ef416fc2 278
279
280 /*
757d2cad 281 * Range check input...
ef416fc2 282 */
283
757d2cad 284 if (!ppd || !fp)
285 return (-1);
ef416fc2 286
287 /*
757d2cad 288 * Get the string...
ef416fc2 289 */
290
757d2cad 291 buffer = ppdEmitString(ppd, section, min_order);
ef416fc2 292
757d2cad 293 /*
294 * Write it as needed and return...
295 */
ef416fc2 296
757d2cad 297 if (buffer)
298 {
299 status = fputs(buffer, fp) < 0 ? -1 : 0;
ef416fc2 300
757d2cad 301 free(buffer);
302 }
303 else
304 status = 0;
ef416fc2 305
757d2cad 306 return (status);
ef416fc2 307}
308
309
310/*
311 * 'ppdEmitFd()' - Emit code for marked options to a file.
312 */
313
314int /* O - 0 on success, -1 on failure */
315ppdEmitFd(ppd_file_t *ppd, /* I - PPD file record */
316 int fd, /* I - File to write to */
317 ppd_section_t section) /* I - Section to write */
318{
757d2cad 319 char *buffer, /* Option code */
320 *bufptr; /* Pointer into code */
321 size_t buflength; /* Length of option code */
322 ssize_t bytes; /* Bytes written */
323 int status; /* Return status */
ef416fc2 324
325
326 /*
757d2cad 327 * Range check input...
ef416fc2 328 */
329
757d2cad 330 if (!ppd || fd < 0)
331 return (-1);
ef416fc2 332
333 /*
757d2cad 334 * Get the string...
ef416fc2 335 */
336
757d2cad 337 buffer = ppdEmitString(ppd, section, 0.0);
ef416fc2 338
757d2cad 339 /*
340 * Write it as needed and return...
341 */
ef416fc2 342
757d2cad 343 if (buffer)
344 {
345 buflength = strlen(buffer);
346 bufptr = buffer;
347 bytes = 0;
ef416fc2 348
757d2cad 349 while (buflength > 0)
350 {
b86bc4cf 351#ifdef WIN32
352 if ((bytes = (ssize_t)write(fd, bufptr, (unsigned)buflength)) < 0)
353#else
757d2cad 354 if ((bytes = write(fd, bufptr, buflength)) < 0)
b86bc4cf 355#endif /* WIN32 */
ef416fc2 356 {
757d2cad 357 if (errno == EAGAIN || errno == EINTR)
358 continue;
ef416fc2 359
757d2cad 360 break;
ef416fc2 361 }
757d2cad 362
363 buflength -= bytes;
364 bufptr += bytes;
ef416fc2 365 }
366
757d2cad 367 status = bytes < 0 ? -1 : 0;
368
369 free(buffer);
370 }
371 else
372 status = 0;
373
374 return (status);
ef416fc2 375}
376
377
378/*
379 * 'ppdEmitJCL()' - Emit code for JCL options to a file.
380 */
381
382int /* O - 0 on success, -1 on failure */
383ppdEmitJCL(ppd_file_t *ppd, /* I - PPD file record */
384 FILE *fp, /* I - File to write to */
385 int job_id, /* I - Job ID */
386 const char *user, /* I - Username */
387 const char *title) /* I - Title */
388{
389 char *ptr; /* Pointer into JCL string */
390 char temp[81]; /* Local title string */
391
392
393 /*
394 * Range check the input...
395 */
396
757d2cad 397 if (!ppd || !ppd->jcl_begin || !ppd->jcl_ps)
ef416fc2 398 return (0);
399
400 /*
401 * See if the printer supports HP PJL...
402 */
403
404 if (!strncmp(ppd->jcl_begin, "\033%-12345X@", 10))
405 {
406 /*
407 * This printer uses HP PJL commands for output; filter the output
408 * so that we only have a single "@PJL JOB" command in the header...
409 *
410 * To avoid bugs in the PJL implementation of certain vendors' products
411 * (Xerox in particular), we add a dummy "@PJL" command at the beginning
412 * of the PJL commands to initialize PJL processing.
413 */
414
f7deaa1a 415 ppd_attr_t *charset; /* PJL charset */
416
417
418 if ((charset = ppdFindAttr(ppd, "cupsPJLCharset", NULL)) != NULL)
419 {
420 if (!charset->value || strcasecmp(charset->value, "UTF-8"))
421 charset = NULL;
422 }
423
ef416fc2 424 fputs("\033%-12345X@PJL\n", fp);
425 for (ptr = ppd->jcl_begin + 9; *ptr;)
426 if (!strncmp(ptr, "@PJL JOB", 8))
427 {
428 /*
429 * Skip job command...
430 */
431
432 for (;*ptr; ptr ++)
433 if (*ptr == '\n')
434 break;
435
436 if (*ptr)
437 ptr ++;
438 }
439 else
440 {
441 /*
442 * Copy line...
443 */
444
445 for (;*ptr; ptr ++)
446 {
447 putc(*ptr, fp);
448 if (*ptr == '\n')
449 break;
450 }
451
452 if (*ptr)
453 ptr ++;
454 }
455
456 /*
457 * Eliminate any path info from the job title...
458 */
459
460 if ((ptr = strrchr(title, '/')) != NULL)
461 title = ptr + 1;
462
463 /*
07725fee 464 * Replace double quotes with single quotes and 8-bit characters with
465 * question marks so that the title does not cause a PJL syntax error.
ef416fc2 466 */
467
468 strlcpy(temp, title, sizeof(temp));
469
470 for (ptr = temp; *ptr; ptr ++)
471 if (*ptr == '\"')
472 *ptr = '\'';
f7deaa1a 473 else if (charset && (*ptr & 128))
07725fee 474 *ptr = '?';
ef416fc2 475
476 /*
477 * Send PJL JOB and PJL RDYMSG commands before we enter PostScript mode...
478 */
479
480 fprintf(fp, "@PJL JOB NAME = \"%s\" DISPLAY = \"%d %s %s\"\n", temp,
481 job_id, user, temp);
482 fprintf(fp, "@PJL RDYMSG DISPLAY = \"%d %s %s\"\n", job_id, user, temp);
483 }
484 else
485 fputs(ppd->jcl_begin, fp);
486
487 ppdEmit(ppd, fp, PPD_ORDER_JCL);
488 fputs(ppd->jcl_ps, fp);
489
490 return (0);
491}
492
493
494/*
495 * 'ppdEmitJCLEnd()' - Emit JCLEnd code to a file.
496 *
497 * @since CUPS 1.2@
498 */
499
500int /* O - 0 on success, -1 on failure */
501ppdEmitJCLEnd(ppd_file_t *ppd, /* I - PPD file record */
502 FILE *fp) /* I - File to write to */
503{
ef416fc2 504 /*
505 * Range check the input...
506 */
507
757d2cad 508 if (!ppd)
ef416fc2 509 return (0);
510
757d2cad 511 if (!ppd->jcl_end)
ef416fc2 512 {
513 if (ppd->num_filters == 0)
757d2cad 514 putc(0x04, fp);
ef416fc2 515
516 return (0);
517 }
518
519 /*
520 * See if the printer supports HP PJL...
521 */
522
523 if (!strncmp(ppd->jcl_end, "\033%-12345X@", 10))
524 {
525 /*
526 * This printer uses HP PJL commands for output; filter the output
527 * so that we only have a single "@PJL JOB" command in the header...
528 *
529 * To avoid bugs in the PJL implementation of certain vendors' products
530 * (Xerox in particular), we add a dummy "@PJL" command at the beginning
531 * of the PJL commands to initialize PJL processing.
532 */
533
534 fputs("\033%-12345X@PJL\n", fp);
535 fputs("@PJL RDYMSG DISPLAY = \"READY\"\n", fp);
536 fputs(ppd->jcl_end + 9, fp);
537 }
538 else
539 fputs(ppd->jcl_end, fp);
540
541 return (0);
542}
543
544
757d2cad 545/*
546 * 'ppdEmitString()' - Get a string containing the code for marked options.
547 *
548 * When "min_order" is greater than zero, this function only includes options
549 * whose OrderDependency value is greater than or equal to "min_order".
550 * Otherwise, all options in the specified section are included in the
551 * returned string.
552 *
553 * The return string is allocated on the heap and should be freed using
5a738aea 554 * @code free@ when you are done with it.
757d2cad 555 *
556 * @since CUPS 1.2@
557 */
558
5a738aea 559char * /* O - String containing option code or @code NULL@ if there is no option code */
757d2cad 560ppdEmitString(ppd_file_t *ppd, /* I - PPD file record */
561 ppd_section_t section, /* I - Section to write */
562 float min_order) /* I - Lowest OrderDependency */
563{
564 int i, j, /* Looping vars */
565 count; /* Number of choices */
566 ppd_choice_t **choices; /* Choices */
567 ppd_size_t *size; /* Custom page size */
568 ppd_coption_t *coption; /* Custom option */
569 ppd_cparam_t *cparam; /* Custom parameter */
570 size_t bufsize; /* Size of string buffer needed */
571 char *buffer, /* String buffer */
572 *bufptr, /* Pointer into buffer */
573 *bufend; /* End of buffer */
574 struct lconv *loc; /* Locale data */
575
576
8ca02f3c 577 DEBUG_printf(("ppdEmitString(ppd=%p, section=%d, min_order=%f)\n",
578 ppd, section, min_order));
579
757d2cad 580 /*
581 * Range check input...
582 */
583
584 if (!ppd)
585 return (NULL);
586
587 /*
588 * Use PageSize or PageRegion as required...
589 */
590
591 ppd_handle_media(ppd);
592
593 /*
594 * Collect the options we need to emit...
595 */
596
597 if ((count = ppdCollect2(ppd, section, min_order, &choices)) == 0)
598 return (NULL);
599
600 /*
601 * Count the number of bytes that are required to hold all of the
602 * option code...
603 */
604
605 for (i = 0, bufsize = 1; i < count; i ++)
606 {
607 if (section != PPD_ORDER_EXIT && section != PPD_ORDER_JCL)
608 {
609 bufsize += 3; /* [{\n */
610
611 if ((!strcasecmp(choices[i]->option->keyword, "PageSize") ||
612 !strcasecmp(choices[i]->option->keyword, "PageRegion")) &&
613 !strcasecmp(choices[i]->choice, "Custom"))
614 {
8ca02f3c 615 DEBUG_puts("ppdEmitString: Custom size set!");
616
e1d6a774 617 bufsize += 37; /* %%BeginFeature: *CustomPageSize True\n */
757d2cad 618 bufsize += 50; /* Five 9-digit numbers + newline */
619 }
620 else if (!strcasecmp(choices[i]->choice, "Custom") &&
621 (coption = ppdFindCustomOption(ppd,
622 choices[i]->option->keyword))
623 != NULL)
624 {
e1d6a774 625 bufsize += 17 + strlen(choices[i]->option->keyword) + 6;
626 /* %%BeginFeature: *keyword True\n */
757d2cad 627
628
629 for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params);
630 cparam;
631 cparam = (ppd_cparam_t *)cupsArrayNext(coption->params))
632 {
633 switch (cparam->type)
634 {
635 case PPD_CUSTOM_CURVE :
636 case PPD_CUSTOM_INVCURVE :
637 case PPD_CUSTOM_POINTS :
638 case PPD_CUSTOM_REAL :
639 case PPD_CUSTOM_INT :
640 bufsize += 10;
641 break;
642
643 case PPD_CUSTOM_PASSCODE :
644 case PPD_CUSTOM_PASSWORD :
645 case PPD_CUSTOM_STRING :
646 bufsize += 3 + 4 * strlen(cparam->current.custom_string);
647 break;
648 }
649 }
650 }
651 else
e1d6a774 652 bufsize += 17 + strlen(choices[i]->option->keyword) + 1 +
653 strlen(choices[i]->choice) + 1;
654 /* %%BeginFeature: *keyword choice\n */
757d2cad 655
656 bufsize += 13; /* %%EndFeature\n */
657 bufsize += 22; /* } stopped cleartomark\n */
658 }
659
660 if (choices[i]->code)
e1d6a774 661 bufsize += strlen(choices[i]->code) + 1;
757d2cad 662 else
663 bufsize += strlen(ppd_custom_code);
664 }
665
666 /*
667 * Allocate memory...
668 */
669
ae71f5de
MS
670 DEBUG_printf(("ppdEmitString: Allocating %d bytes for string...\n",
671 (int)bufsize));
8ca02f3c 672
757d2cad 673 if ((buffer = calloc(1, bufsize)) == NULL)
674 {
675 free(choices);
676 return (NULL);
677 }
678
679 bufend = buffer + bufsize - 1;
680 loc = localeconv();
681
682 /*
683 * Copy the option code to the buffer...
684 */
685
686 for (i = 0, bufptr = buffer; i < count; i ++, bufptr += strlen(bufptr))
687 if (section != PPD_ORDER_EXIT && section != PPD_ORDER_JCL)
688 {
689 /*
690 * Add wrapper commands to prevent printer errors for unsupported
691 * options...
692 */
693
694 strlcpy(bufptr, "[{\n", bufend - bufptr + 1);
695 bufptr += 3;
696
697 /*
698 * Send DSC comments with option...
699 */
700
8ca02f3c 701 DEBUG_printf(("Adding code for %s=%s...\n", choices[i]->option->keyword,
702 choices[i]->choice));
703
757d2cad 704 if ((!strcasecmp(choices[i]->option->keyword, "PageSize") ||
705 !strcasecmp(choices[i]->option->keyword, "PageRegion")) &&
706 !strcasecmp(choices[i]->choice, "Custom"))
707 {
708 /*
709 * Variable size; write out standard size options, using the
710 * parameter positions defined in the PPD file...
711 */
712
713 ppd_attr_t *attr; /* PPD attribute */
714 int pos, /* Position of custom value */
715 orientation; /* Orientation to use */
716 float values[5]; /* Values for custom command */
717
718
719 strlcpy(bufptr, "%%BeginFeature: *CustomPageSize True\n",
720 bufend - bufptr + 1);
721 bufptr += 37;
722
723 size = ppdPageSize(ppd, "Custom");
724
725 memset(values, 0, sizeof(values));
726
727 if ((attr = ppdFindAttr(ppd, "ParamCustomPageSize", "Width")) != NULL)
728 {
729 pos = atoi(attr->value) - 1;
730
731 if (pos < 0 || pos > 4)
732 pos = 0;
733 }
734 else
735 pos = 0;
736
737 values[pos] = size->width;
738
739 if ((attr = ppdFindAttr(ppd, "ParamCustomPageSize", "Height")) != NULL)
740 {
741 pos = atoi(attr->value) - 1;
742
743 if (pos < 0 || pos > 4)
744 pos = 1;
745 }
746 else
747 pos = 1;
748
749 values[pos] = size->length;
750
751 /*
752 * According to the Adobe PPD specification, an orientation of 1
753 * will produce a print that comes out upside-down with the X
754 * axis perpendicular to the direction of feed, which is exactly
755 * what we want to be consistent with non-PS printers.
756 *
757 * We could also use an orientation of 3 to produce output that
758 * comes out rightside-up (this is the default for many large format
759 * printer PPDs), however for consistency we will stick with the
760 * value 1.
761 *
762 * If we wanted to get fancy, we could use orientations of 0 or
763 * 2 and swap the width and length, however we don't want to get
764 * fancy, we just want it to work consistently.
765 *
766 * The orientation value is range limited by the Orientation
767 * parameter definition, so certain non-PS printer drivers that
768 * only support an Orientation of 0 will get the value 0 as
769 * expected.
770 */
771
772 orientation = 1;
773
774 if ((attr = ppdFindAttr(ppd, "ParamCustomPageSize",
775 "Orientation")) != NULL)
776 {
777 int min_orient, max_orient; /* Minimum and maximum orientations */
778
779
780 if (sscanf(attr->value, "%d%*s%d%d", &pos, &min_orient,
781 &max_orient) != 3)
782 pos = 4;
783 else
784 {
785 pos --;
786
787 if (pos < 0 || pos > 4)
788 pos = 4;
789
790 if (orientation > max_orient)
791 orientation = max_orient;
792 else if (orientation < min_orient)
793 orientation = min_orient;
794 }
795 }
796 else
797 pos = 4;
798
b86bc4cf 799 values[pos] = (float)orientation;
757d2cad 800
801 for (pos = 0; pos < 5; pos ++)
802 {
803 bufptr = _cupsStrFormatd(bufptr, bufend, values[pos], loc);
804 *bufptr++ = '\n';
805 }
806
807 if (!choices[i]->code)
808 {
809 /*
810 * This can happen with certain buggy PPD files that don't include
811 * a CustomPageSize command sequence... We just use a generic
812 * Level 2 command sequence...
813 */
814
815 strlcpy(bufptr, ppd_custom_code, bufend - bufptr + 1);
816 bufptr += strlen(bufptr);
817 }
818 }
819 else if (!strcasecmp(choices[i]->choice, "Custom") &&
820 (coption = ppdFindCustomOption(ppd,
821 choices[i]->option->keyword))
822 != NULL)
823 {
824 /*
825 * Custom option...
826 */
827
828 const char *s; /* Pointer into string value */
829
830
831 snprintf(bufptr, bufend - bufptr + 1,
832 "%%%%BeginFeature: *Custom%s True\n", coption->keyword);
833 bufptr += strlen(bufptr);
834
835 for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params);
836 cparam;
837 cparam = (ppd_cparam_t *)cupsArrayNext(coption->params))
838 {
839 switch (cparam->type)
840 {
841 case PPD_CUSTOM_CURVE :
842 case PPD_CUSTOM_INVCURVE :
843 case PPD_CUSTOM_POINTS :
844 case PPD_CUSTOM_REAL :
845 bufptr = _cupsStrFormatd(bufptr, bufend,
846 cparam->current.custom_real, loc);
847 *bufptr++ = '\n';
848 break;
849
850 case PPD_CUSTOM_INT :
851 snprintf(bufptr, bufend - bufptr + 1, "%d\n",
852 cparam->current.custom_int);
853 bufptr += strlen(bufptr);
854 break;
855
856 case PPD_CUSTOM_PASSCODE :
857 case PPD_CUSTOM_PASSWORD :
858 case PPD_CUSTOM_STRING :
859 *bufptr++ = '(';
860
861 for (s = cparam->current.custom_string; *s; s ++)
862 if (*s < ' ' || *s == '(' || *s == ')' || *s >= 127)
863 {
864 snprintf(bufptr, bufend - bufptr + 1, "\\%03o", *s & 255);
865 bufptr += strlen(bufptr);
866 }
867 else
868 *bufptr++ = *s;
869
870 *bufptr++ = ')';
871 *bufptr++ = '\n';
872 break;
873 }
874 }
875 }
876 else
877 {
878 snprintf(bufptr, bufend - bufptr + 1, "%%%%BeginFeature: *%s %s\n",
879 choices[i]->option->keyword, choices[i]->choice);
880 bufptr += strlen(bufptr);
881 }
882
883 if (choices[i]->code && choices[i]->code[0])
884 {
b86bc4cf 885 j = (int)strlen(choices[i]->code);
757d2cad 886 memcpy(bufptr, choices[i]->code, j);
887 bufptr += j;
888
889 if (choices[i]->code[j - 1] != '\n')
890 *bufptr++ = '\n';
891 }
892
893 strlcpy(bufptr, "%%EndFeature\n"
894 "} stopped cleartomark\n", bufend - bufptr + 1);
895 bufptr += strlen(bufptr);
8ca02f3c 896
897 DEBUG_printf(("ppdEmitString: Offset in string is %d...\n",
ae71f5de 898 (int)(bufptr - buffer)));
757d2cad 899 }
900 else
901 {
902 strlcpy(bufptr, choices[i]->code, bufend - bufptr + 1);
903 bufptr += strlen(bufptr);
904 }
905
906 /*
907 * Nul-terminate, free, and return...
908 */
909
910 *bufptr = '\0';
911
912 free(choices);
913
914 return (buffer);
915}
916
917
ef416fc2 918/*
919 * 'ppd_handle_media()' - Handle media selection...
920 */
921
922static void
923ppd_handle_media(ppd_file_t *ppd)
924{
925 ppd_choice_t *manual_feed, /* ManualFeed choice, if any */
926 *input_slot, /* InputSlot choice, if any */
927 *page; /* PageSize/PageRegion */
928 ppd_size_t *size; /* Current media size */
929 ppd_attr_t *rpr; /* RequiresPageRegion value */
930
931
932 /*
933 * This function determines if the user has selected a media source
934 * via the InputSlot or ManualFeed options; if so, it marks the
935 * PageRegion option corresponding to the current media size.
936 * Otherwise it marks the PageSize option.
937 */
938
939 if ((size = ppdPageSize(ppd, NULL)) == NULL)
940 return;
941
942 manual_feed = ppdFindMarkedChoice(ppd, "ManualFeed");
943 input_slot = ppdFindMarkedChoice(ppd, "InputSlot");
944
945 if (input_slot != NULL)
946 rpr = ppdFindAttr(ppd, "RequiresPageRegion", input_slot->choice);
947 else
948 rpr = NULL;
949
950 if (!rpr)
951 rpr = ppdFindAttr(ppd, "RequiresPageRegion", "All");
952
fa73b229 953 if (!strcasecmp(size->name, "Custom") || (!manual_feed && !input_slot) ||
954 !((manual_feed && !strcasecmp(manual_feed->choice, "True")) ||
955 (input_slot && input_slot->code && input_slot->code[0])))
ef416fc2 956 {
957 /*
958 * Manual feed was not selected and/or the input slot selection does
959 * not contain any PostScript code. Use the PageSize option...
960 */
961
962 ppdMarkOption(ppd, "PageSize", size->name);
963 }
964 else
965 {
966 /*
967 * Manual feed was selected and/or the input slot selection contains
968 * PostScript code. Use the PageRegion option...
969 */
970
971 ppdMarkOption(ppd, "PageRegion", size->name);
972
973 /*
974 * RequiresPageRegion does not apply to manual feed so we need to
975 * check that we are not doing manual feed before unmarking PageRegion.
976 */
977
978 if (!(manual_feed && !strcasecmp(manual_feed->choice, "True")) &&
979 ((rpr && rpr->value && !strcmp(rpr->value, "False")) ||
980 (!rpr && !ppd->num_filters)))
981 {
982 /*
983 * Either the PPD file specifies no PageRegion code or the PPD file
984 * not for a CUPS raster driver and thus defaults to no PageRegion
985 * code... Unmark the PageRegion choice so that we don't output the
986 * code...
987 */
988
989 page = ppdFindMarkedChoice(ppd, "PageRegion");
990
991 if (page)
992 page->marked = 0;
993 }
994 }
995}
996
997
ef416fc2 998/*
bc44d920 999 * End of "$Id: emit.c 6649 2007-07-11 21:46:42Z mike $".
ef416fc2 1000 */