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