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