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