]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/emit.c
fed81fc48ae09c5e947b33fadd1268fc294d2969
[thirdparty/cups.git] / cups / emit.c
1 /*
2 * "$Id: emit.c 7493 2008-04-24 00:10:34Z mike $"
3 *
4 * PPD code emission routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
8 *
9 * These coded instructions, statements, and computer programs are the
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/".
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 *
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.
26 * ppdEmitAfterOrder() - Emit a subset of the code for marked options to a
27 * file.
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.
31 * ppdEmitString() - Get a string containing the code for marked options.
32 * ppd_handle_media() - Handle media selection...
33 */
34
35 /*
36 * Include necessary headers...
37 */
38
39 #include "ppd.h"
40 #include <stdlib.h>
41 #include "string.h"
42 #include <errno.h>
43 #include "debug.h"
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
56 static void ppd_handle_media(ppd_file_t *ppd);
57
58
59 /*
60 * Local globals...
61 */
62
63 static 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.
71 *
72 * The choices array should be freed using @code free@ when you are
73 * finished with it.
74 */
75
76 int /* O - Number of options marked */
77 ppdCollect(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 */
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 *
89 * The choices array should be freed using @code free@ when you are
90 * finished with it.
91 *
92 * @since CUPS 1.2@
93 */
94
95 int /* O - Number of options marked */
96 ppdCollect2(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 */
100 {
101 ppd_choice_t *c; /* Current choice */
102 ppd_section_t csection; /* Current section */
103 float corder; /* Current OrderDependency value */
104 int count; /* Number of choices collected */
105 ppd_choice_t **collect; /* Collected choices */
106 float *orders; /* Collected order values */
107
108
109 DEBUG_printf(("ppdCollect2(ppd=%p, section=%d, min_order=%f, choices=%p)\n",
110 ppd, section, min_order, choices));
111
112 if (!ppd || !choices)
113 {
114 if (choices)
115 *choices = NULL;
116
117 return (0);
118 }
119
120 /*
121 * Allocate memory for up to N selected choices...
122 */
123
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 }
138
139 /*
140 * Loop through all options and add choices as needed...
141 */
142
143 for (c = (ppd_choice_t *)cupsArrayFirst(ppd->marked);
144 c;
145 c = (ppd_choice_t *)cupsArrayNext(ppd->marked))
146 {
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 */
172
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 }
198 }
199
200 /*
201 * If we have more than 1 marked choice, sort them...
202 */
203
204 if (count > 1)
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);
222
223 DEBUG_printf(("ppdCollect2: %d marked choices...\n", count));
224
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
248 int /* O - 0 on success, -1 on failure */
249 ppdEmit(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 */
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
268 int /* O - 0 on success, -1 on failure */
269 ppdEmitAfterOrder(
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 */
273 int limit, /* I - Non-zero to use min_order */
274 float min_order) /* I - Lowest OrderDependency */
275 {
276 char *buffer; /* Option code */
277 int status; /* Return status */
278
279
280 /*
281 * Range check input...
282 */
283
284 if (!ppd || !fp)
285 return (-1);
286
287 /*
288 * Get the string...
289 */
290
291 buffer = ppdEmitString(ppd, section, min_order);
292
293 /*
294 * Write it as needed and return...
295 */
296
297 if (buffer)
298 {
299 status = fputs(buffer, fp) < 0 ? -1 : 0;
300
301 free(buffer);
302 }
303 else
304 status = 0;
305
306 return (status);
307 }
308
309
310 /*
311 * 'ppdEmitFd()' - Emit code for marked options to a file.
312 */
313
314 int /* O - 0 on success, -1 on failure */
315 ppdEmitFd(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 {
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 */
324
325
326 /*
327 * Range check input...
328 */
329
330 if (!ppd || fd < 0)
331 return (-1);
332
333 /*
334 * Get the string...
335 */
336
337 buffer = ppdEmitString(ppd, section, 0.0);
338
339 /*
340 * Write it as needed and return...
341 */
342
343 if (buffer)
344 {
345 buflength = strlen(buffer);
346 bufptr = buffer;
347 bytes = 0;
348
349 while (buflength > 0)
350 {
351 #ifdef WIN32
352 if ((bytes = (ssize_t)write(fd, bufptr, (unsigned)buflength)) < 0)
353 #else
354 if ((bytes = write(fd, bufptr, buflength)) < 0)
355 #endif /* WIN32 */
356 {
357 if (errno == EAGAIN || errno == EINTR)
358 continue;
359
360 break;
361 }
362
363 buflength -= bytes;
364 bufptr += bytes;
365 }
366
367 status = bytes < 0 ? -1 : 0;
368
369 free(buffer);
370 }
371 else
372 status = 0;
373
374 return (status);
375 }
376
377
378 /*
379 * 'ppdEmitJCL()' - Emit code for JCL options to a file.
380 */
381
382 int /* O - 0 on success, -1 on failure */
383 ppdEmitJCL(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
397 if (!ppd || !ppd->jcl_begin || !ppd->jcl_ps)
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
415 ppd_attr_t *charset; /* PJL charset */
416 ppd_attr_t *display; /* PJL display command */
417
418
419 if ((charset = ppdFindAttr(ppd, "cupsPJLCharset", NULL)) != NULL)
420 {
421 if (!charset->value || strcasecmp(charset->value, "UTF-8"))
422 charset = NULL;
423 }
424
425 if ((display = ppdFindAttr(ppd, "cupsPJLDisplay", NULL)) != NULL)
426 {
427 if (!display->value)
428 display = NULL;
429 }
430
431 fputs("\033%-12345X@PJL\n", fp);
432 for (ptr = ppd->jcl_begin + 9; *ptr;)
433 if (!strncmp(ptr, "@PJL JOB", 8))
434 {
435 /*
436 * Skip job command...
437 */
438
439 for (;*ptr; ptr ++)
440 if (*ptr == '\n')
441 break;
442
443 if (*ptr)
444 ptr ++;
445 }
446 else
447 {
448 /*
449 * Copy line...
450 */
451
452 for (;*ptr; ptr ++)
453 {
454 putc(*ptr, fp);
455 if (*ptr == '\n')
456 break;
457 }
458
459 if (*ptr)
460 ptr ++;
461 }
462
463 /*
464 * Eliminate any path info from the job title...
465 */
466
467 if ((ptr = strrchr(title, '/')) != NULL)
468 title = ptr + 1;
469
470 /*
471 * Replace double quotes with single quotes and 8-bit characters with
472 * question marks so that the title does not cause a PJL syntax error.
473 */
474
475 strlcpy(temp, title, sizeof(temp));
476
477 for (ptr = temp; *ptr; ptr ++)
478 if (*ptr == '\"')
479 *ptr = '\'';
480 else if (charset && (*ptr & 128))
481 *ptr = '?';
482
483 /*
484 * Send PJL JOB and PJL RDYMSG commands before we enter PostScript mode...
485 */
486
487 if (display && strcmp(display->value, "job"))
488 {
489 fprintf(fp, "@PJL JOB NAME = \"%s\"\n", temp);
490
491 if (display && !strcmp(display->value, "rdymsg"))
492 fprintf(fp, "@PJL RDYMSG DISPLAY = \"%d %s %s\"\n", job_id, user, temp);
493 }
494 else
495 fprintf(fp, "@PJL JOB NAME = \"%s\" DISPLAY = \"%d %s %s\"\n", temp,
496 job_id, user, temp);
497 }
498 else
499 fputs(ppd->jcl_begin, fp);
500
501 ppdEmit(ppd, fp, PPD_ORDER_JCL);
502 fputs(ppd->jcl_ps, fp);
503
504 return (0);
505 }
506
507
508 /*
509 * 'ppdEmitJCLEnd()' - Emit JCLEnd code to a file.
510 *
511 * @since CUPS 1.2@
512 */
513
514 int /* O - 0 on success, -1 on failure */
515 ppdEmitJCLEnd(ppd_file_t *ppd, /* I - PPD file record */
516 FILE *fp) /* I - File to write to */
517 {
518 /*
519 * Range check the input...
520 */
521
522 if (!ppd)
523 return (0);
524
525 if (!ppd->jcl_end)
526 {
527 if (ppd->num_filters == 0)
528 putc(0x04, fp);
529
530 return (0);
531 }
532
533 /*
534 * See if the printer supports HP PJL...
535 */
536
537 if (!strncmp(ppd->jcl_end, "\033%-12345X@", 10))
538 {
539 /*
540 * This printer uses HP PJL commands for output; filter the output
541 * so that we only have a single "@PJL JOB" command in the header...
542 *
543 * To avoid bugs in the PJL implementation of certain vendors' products
544 * (Xerox in particular), we add a dummy "@PJL" command at the beginning
545 * of the PJL commands to initialize PJL processing.
546 */
547
548 fputs("\033%-12345X@PJL\n", fp);
549 fputs("@PJL RDYMSG DISPLAY = \"READY\"\n", fp);
550 fputs(ppd->jcl_end + 9, fp);
551 }
552 else
553 fputs(ppd->jcl_end, fp);
554
555 return (0);
556 }
557
558
559 /*
560 * 'ppdEmitString()' - Get a string containing the code for marked options.
561 *
562 * When "min_order" is greater than zero, this function only includes options
563 * whose OrderDependency value is greater than or equal to "min_order".
564 * Otherwise, all options in the specified section are included in the
565 * returned string.
566 *
567 * The return string is allocated on the heap and should be freed using
568 * @code free@ when you are done with it.
569 *
570 * @since CUPS 1.2@
571 */
572
573 char * /* O - String containing option code or @code NULL@ if there is no option code */
574 ppdEmitString(ppd_file_t *ppd, /* I - PPD file record */
575 ppd_section_t section, /* I - Section to write */
576 float min_order) /* I - Lowest OrderDependency */
577 {
578 int i, j, /* Looping vars */
579 count; /* Number of choices */
580 ppd_choice_t **choices; /* Choices */
581 ppd_size_t *size; /* Custom page size */
582 ppd_coption_t *coption; /* Custom option */
583 ppd_cparam_t *cparam; /* Custom parameter */
584 size_t bufsize; /* Size of string buffer needed */
585 char *buffer, /* String buffer */
586 *bufptr, /* Pointer into buffer */
587 *bufend; /* End of buffer */
588 struct lconv *loc; /* Locale data */
589
590
591 DEBUG_printf(("ppdEmitString(ppd=%p, section=%d, min_order=%f)\n",
592 ppd, section, min_order));
593
594 /*
595 * Range check input...
596 */
597
598 if (!ppd)
599 return (NULL);
600
601 /*
602 * Use PageSize or PageRegion as required...
603 */
604
605 ppd_handle_media(ppd);
606
607 /*
608 * Collect the options we need to emit...
609 */
610
611 if ((count = ppdCollect2(ppd, section, min_order, &choices)) == 0)
612 return (NULL);
613
614 /*
615 * Count the number of bytes that are required to hold all of the
616 * option code...
617 */
618
619 for (i = 0, bufsize = 1; i < count; i ++)
620 {
621 if (section == PPD_ORDER_JCL)
622 {
623 if (!strcasecmp(choices[i]->choice, "Custom") &&
624 (coption = ppdFindCustomOption(ppd, choices[i]->option->keyword))
625 != NULL)
626 {
627 /*
628 * Add space to account for custom parameter substitution...
629 */
630
631 for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params);
632 cparam;
633 cparam = (ppd_cparam_t *)cupsArrayNext(coption->params))
634 {
635 switch (cparam->type)
636 {
637 case PPD_CUSTOM_CURVE :
638 case PPD_CUSTOM_INVCURVE :
639 case PPD_CUSTOM_POINTS :
640 case PPD_CUSTOM_REAL :
641 case PPD_CUSTOM_INT :
642 bufsize += 10;
643 break;
644
645 case PPD_CUSTOM_PASSCODE :
646 case PPD_CUSTOM_PASSWORD :
647 case PPD_CUSTOM_STRING :
648 bufsize += strlen(cparam->current.custom_string);
649 break;
650 }
651 }
652 }
653 }
654 else if (section != PPD_ORDER_EXIT)
655 {
656 bufsize += 3; /* [{\n */
657
658 if ((!strcasecmp(choices[i]->option->keyword, "PageSize") ||
659 !strcasecmp(choices[i]->option->keyword, "PageRegion")) &&
660 !strcasecmp(choices[i]->choice, "Custom"))
661 {
662 DEBUG_puts("ppdEmitString: Custom size set!");
663
664 bufsize += 37; /* %%BeginFeature: *CustomPageSize True\n */
665 bufsize += 50; /* Five 9-digit numbers + newline */
666 }
667 else if (!strcasecmp(choices[i]->choice, "Custom") &&
668 (coption = ppdFindCustomOption(ppd,
669 choices[i]->option->keyword))
670 != NULL)
671 {
672 bufsize += 17 + strlen(choices[i]->option->keyword) + 6;
673 /* %%BeginFeature: *keyword True\n */
674
675
676 for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params);
677 cparam;
678 cparam = (ppd_cparam_t *)cupsArrayNext(coption->params))
679 {
680 switch (cparam->type)
681 {
682 case PPD_CUSTOM_CURVE :
683 case PPD_CUSTOM_INVCURVE :
684 case PPD_CUSTOM_POINTS :
685 case PPD_CUSTOM_REAL :
686 case PPD_CUSTOM_INT :
687 bufsize += 10;
688 break;
689
690 case PPD_CUSTOM_PASSCODE :
691 case PPD_CUSTOM_PASSWORD :
692 case PPD_CUSTOM_STRING :
693 bufsize += 3 + 4 * strlen(cparam->current.custom_string);
694 break;
695 }
696 }
697 }
698 else
699 bufsize += 17 + strlen(choices[i]->option->keyword) + 1 +
700 strlen(choices[i]->choice) + 1;
701 /* %%BeginFeature: *keyword choice\n */
702
703 bufsize += 13; /* %%EndFeature\n */
704 bufsize += 22; /* } stopped cleartomark\n */
705 }
706
707 if (choices[i]->code)
708 bufsize += strlen(choices[i]->code) + 1;
709 else
710 bufsize += strlen(ppd_custom_code);
711 }
712
713 /*
714 * Allocate memory...
715 */
716
717 DEBUG_printf(("ppdEmitString: Allocating %d bytes for string...\n",
718 (int)bufsize));
719
720 if ((buffer = calloc(1, bufsize)) == NULL)
721 {
722 free(choices);
723 return (NULL);
724 }
725
726 bufend = buffer + bufsize - 1;
727 loc = localeconv();
728
729 /*
730 * Copy the option code to the buffer...
731 */
732
733 for (i = 0, bufptr = buffer; i < count; i ++, bufptr += strlen(bufptr))
734 if (section == PPD_ORDER_JCL)
735 {
736 if (!strcasecmp(choices[i]->choice, "Custom") &&
737 (coption = ppdFindCustomOption(ppd, choices[i]->option->keyword))
738 != NULL)
739 {
740 /*
741 * Handle substitutions in custom JCL options...
742 */
743
744 char *cptr; /* Pointer into code */
745 int pnum; /* Parameter number */
746
747
748 for (cptr = choices[i]->code; *cptr && bufptr < bufend;)
749 {
750 if (*cptr == '\\')
751 {
752 cptr ++;
753
754 if (isdigit(*cptr & 255))
755 {
756 /*
757 * Substitute parameter...
758 */
759
760 pnum = *cptr++ - '0';
761 while (isalnum(*cptr & 255))
762 pnum = pnum * 10 + *cptr - '0';
763
764 for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params);
765 cparam;
766 cparam = (ppd_cparam_t *)cupsArrayNext(coption->params))
767 if (cparam->order == pnum)
768 break;
769
770 if (cparam)
771 {
772 switch (cparam->type)
773 {
774 case PPD_CUSTOM_CURVE :
775 case PPD_CUSTOM_INVCURVE :
776 case PPD_CUSTOM_POINTS :
777 case PPD_CUSTOM_REAL :
778 bufptr = _cupsStrFormatd(bufptr, bufend,
779 cparam->current.custom_real,
780 loc);
781 break;
782
783 case PPD_CUSTOM_INT :
784 snprintf(bufptr, bufend - bufptr, "%d",
785 cparam->current.custom_int);
786 bufptr += strlen(bufptr);
787 break;
788
789 case PPD_CUSTOM_PASSCODE :
790 case PPD_CUSTOM_PASSWORD :
791 case PPD_CUSTOM_STRING :
792 strlcpy(bufptr, cparam->current.custom_string,
793 bufend - bufptr);
794 bufptr += strlen(bufptr);
795 break;
796 }
797 }
798 }
799 else if (*cptr)
800 *bufptr++ = *cptr++;
801 }
802 else
803 *bufptr++ = *cptr++;
804 }
805 }
806 else
807 {
808 /*
809 * Otherwise just copy the option code directly...
810 */
811
812 strlcpy(bufptr, choices[i]->code, bufend - bufptr + 1);
813 bufptr += strlen(bufptr);
814 }
815 }
816 else if (section != PPD_ORDER_EXIT)
817 {
818 /*
819 * Add wrapper commands to prevent printer errors for unsupported
820 * options...
821 */
822
823 strlcpy(bufptr, "[{\n", bufend - bufptr + 1);
824 bufptr += 3;
825
826 /*
827 * Send DSC comments with option...
828 */
829
830 DEBUG_printf(("Adding code for %s=%s...\n", choices[i]->option->keyword,
831 choices[i]->choice));
832
833 if ((!strcasecmp(choices[i]->option->keyword, "PageSize") ||
834 !strcasecmp(choices[i]->option->keyword, "PageRegion")) &&
835 !strcasecmp(choices[i]->choice, "Custom"))
836 {
837 /*
838 * Variable size; write out standard size options, using the
839 * parameter positions defined in the PPD file...
840 */
841
842 ppd_attr_t *attr; /* PPD attribute */
843 int pos, /* Position of custom value */
844 orientation; /* Orientation to use */
845 float values[5]; /* Values for custom command */
846
847
848 strlcpy(bufptr, "%%BeginFeature: *CustomPageSize True\n",
849 bufend - bufptr + 1);
850 bufptr += 37;
851
852 size = ppdPageSize(ppd, "Custom");
853
854 memset(values, 0, sizeof(values));
855
856 if ((attr = ppdFindAttr(ppd, "ParamCustomPageSize", "Width")) != NULL)
857 {
858 pos = atoi(attr->value) - 1;
859
860 if (pos < 0 || pos > 4)
861 pos = 0;
862 }
863 else
864 pos = 0;
865
866 values[pos] = size->width;
867
868 if ((attr = ppdFindAttr(ppd, "ParamCustomPageSize", "Height")) != NULL)
869 {
870 pos = atoi(attr->value) - 1;
871
872 if (pos < 0 || pos > 4)
873 pos = 1;
874 }
875 else
876 pos = 1;
877
878 values[pos] = size->length;
879
880 /*
881 * According to the Adobe PPD specification, an orientation of 1
882 * will produce a print that comes out upside-down with the X
883 * axis perpendicular to the direction of feed, which is exactly
884 * what we want to be consistent with non-PS printers.
885 *
886 * We could also use an orientation of 3 to produce output that
887 * comes out rightside-up (this is the default for many large format
888 * printer PPDs), however for consistency we will stick with the
889 * value 1.
890 *
891 * If we wanted to get fancy, we could use orientations of 0 or
892 * 2 and swap the width and length, however we don't want to get
893 * fancy, we just want it to work consistently.
894 *
895 * The orientation value is range limited by the Orientation
896 * parameter definition, so certain non-PS printer drivers that
897 * only support an Orientation of 0 will get the value 0 as
898 * expected.
899 */
900
901 orientation = 1;
902
903 if ((attr = ppdFindAttr(ppd, "ParamCustomPageSize",
904 "Orientation")) != NULL)
905 {
906 int min_orient, max_orient; /* Minimum and maximum orientations */
907
908
909 if (sscanf(attr->value, "%d%*s%d%d", &pos, &min_orient,
910 &max_orient) != 3)
911 pos = 4;
912 else
913 {
914 pos --;
915
916 if (pos < 0 || pos > 4)
917 pos = 4;
918
919 if (orientation > max_orient)
920 orientation = max_orient;
921 else if (orientation < min_orient)
922 orientation = min_orient;
923 }
924 }
925 else
926 pos = 4;
927
928 values[pos] = (float)orientation;
929
930 for (pos = 0; pos < 5; pos ++)
931 {
932 bufptr = _cupsStrFormatd(bufptr, bufend, values[pos], loc);
933 *bufptr++ = '\n';
934 }
935
936 if (!choices[i]->code)
937 {
938 /*
939 * This can happen with certain buggy PPD files that don't include
940 * a CustomPageSize command sequence... We just use a generic
941 * Level 2 command sequence...
942 */
943
944 strlcpy(bufptr, ppd_custom_code, bufend - bufptr + 1);
945 bufptr += strlen(bufptr);
946 }
947 }
948 else if (!strcasecmp(choices[i]->choice, "Custom") &&
949 (coption = ppdFindCustomOption(ppd, choices[i]->option->keyword))
950 != NULL)
951 {
952 /*
953 * Custom option...
954 */
955
956 const char *s; /* Pointer into string value */
957
958
959 snprintf(bufptr, bufend - bufptr + 1,
960 "%%%%BeginFeature: *Custom%s True\n", coption->keyword);
961 bufptr += strlen(bufptr);
962
963 for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params);
964 cparam;
965 cparam = (ppd_cparam_t *)cupsArrayNext(coption->params))
966 {
967 switch (cparam->type)
968 {
969 case PPD_CUSTOM_CURVE :
970 case PPD_CUSTOM_INVCURVE :
971 case PPD_CUSTOM_POINTS :
972 case PPD_CUSTOM_REAL :
973 bufptr = _cupsStrFormatd(bufptr, bufend,
974 cparam->current.custom_real, loc);
975 *bufptr++ = '\n';
976 break;
977
978 case PPD_CUSTOM_INT :
979 snprintf(bufptr, bufend - bufptr + 1, "%d\n",
980 cparam->current.custom_int);
981 bufptr += strlen(bufptr);
982 break;
983
984 case PPD_CUSTOM_PASSCODE :
985 case PPD_CUSTOM_PASSWORD :
986 case PPD_CUSTOM_STRING :
987 *bufptr++ = '(';
988
989 for (s = cparam->current.custom_string; *s; s ++)
990 if (*s < ' ' || *s == '(' || *s == ')' || *s >= 127)
991 {
992 snprintf(bufptr, bufend - bufptr + 1, "\\%03o", *s & 255);
993 bufptr += strlen(bufptr);
994 }
995 else
996 *bufptr++ = *s;
997
998 *bufptr++ = ')';
999 *bufptr++ = '\n';
1000 break;
1001 }
1002 }
1003 }
1004 else
1005 {
1006 snprintf(bufptr, bufend - bufptr + 1, "%%%%BeginFeature: *%s %s\n",
1007 choices[i]->option->keyword, choices[i]->choice);
1008 bufptr += strlen(bufptr);
1009 }
1010
1011 if (choices[i]->code && choices[i]->code[0])
1012 {
1013 j = (int)strlen(choices[i]->code);
1014 memcpy(bufptr, choices[i]->code, j);
1015 bufptr += j;
1016
1017 if (choices[i]->code[j - 1] != '\n')
1018 *bufptr++ = '\n';
1019 }
1020
1021 strlcpy(bufptr, "%%EndFeature\n"
1022 "} stopped cleartomark\n", bufend - bufptr + 1);
1023 bufptr += strlen(bufptr);
1024
1025 DEBUG_printf(("ppdEmitString: Offset in string is %d...\n",
1026 (int)(bufptr - buffer)));
1027 }
1028 else
1029 {
1030 strlcpy(bufptr, choices[i]->code, bufend - bufptr + 1);
1031 bufptr += strlen(bufptr);
1032 }
1033
1034 /*
1035 * Nul-terminate, free, and return...
1036 */
1037
1038 *bufptr = '\0';
1039
1040 free(choices);
1041
1042 return (buffer);
1043 }
1044
1045
1046 /*
1047 * 'ppd_handle_media()' - Handle media selection...
1048 */
1049
1050 static void
1051 ppd_handle_media(ppd_file_t *ppd)
1052 {
1053 ppd_choice_t *manual_feed, /* ManualFeed choice, if any */
1054 *input_slot, /* InputSlot choice, if any */
1055 *page; /* PageSize/PageRegion */
1056 ppd_size_t *size; /* Current media size */
1057 ppd_attr_t *rpr; /* RequiresPageRegion value */
1058
1059
1060 /*
1061 * This function determines if the user has selected a media source
1062 * via the InputSlot or ManualFeed options; if so, it marks the
1063 * PageRegion option corresponding to the current media size.
1064 * Otherwise it marks the PageSize option.
1065 */
1066
1067 if ((size = ppdPageSize(ppd, NULL)) == NULL)
1068 return;
1069
1070 manual_feed = ppdFindMarkedChoice(ppd, "ManualFeed");
1071 input_slot = ppdFindMarkedChoice(ppd, "InputSlot");
1072
1073 if (input_slot != NULL)
1074 rpr = ppdFindAttr(ppd, "RequiresPageRegion", input_slot->choice);
1075 else
1076 rpr = NULL;
1077
1078 if (!rpr)
1079 rpr = ppdFindAttr(ppd, "RequiresPageRegion", "All");
1080
1081 if (!strcasecmp(size->name, "Custom") || (!manual_feed && !input_slot) ||
1082 !((manual_feed && !strcasecmp(manual_feed->choice, "True")) ||
1083 (input_slot && input_slot->code && input_slot->code[0])))
1084 {
1085 /*
1086 * Manual feed was not selected and/or the input slot selection does
1087 * not contain any PostScript code. Use the PageSize option...
1088 */
1089
1090 ppdMarkOption(ppd, "PageSize", size->name);
1091 }
1092 else
1093 {
1094 /*
1095 * Manual feed was selected and/or the input slot selection contains
1096 * PostScript code. Use the PageRegion option...
1097 */
1098
1099 ppdMarkOption(ppd, "PageRegion", size->name);
1100
1101 /*
1102 * RequiresPageRegion does not apply to manual feed so we need to
1103 * check that we are not doing manual feed before unmarking PageRegion.
1104 */
1105
1106 if (!(manual_feed && !strcasecmp(manual_feed->choice, "True")) &&
1107 ((rpr && rpr->value && !strcmp(rpr->value, "False")) ||
1108 (!rpr && !ppd->num_filters)))
1109 {
1110 /*
1111 * Either the PPD file specifies no PageRegion code or the PPD file
1112 * not for a CUPS raster driver and thus defaults to no PageRegion
1113 * code... Unmark the PageRegion choice so that we don't output the
1114 * code...
1115 */
1116
1117 page = ppdFindMarkedChoice(ppd, "PageRegion");
1118
1119 if (page)
1120 page->marked = 0;
1121 }
1122 }
1123 }
1124
1125
1126 /*
1127 * End of "$Id: emit.c 7493 2008-04-24 00:10:34Z mike $".
1128 */