]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/emit.c
2 * "$Id: emit.c 4785 2005-10-13 19:39:05Z mike $"
4 * PPD code emission routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2005 by Easy Software Products, all rights reserved.
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
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * PostScript is a trademark of Adobe Systems, Inc.
26 * This file is subject to the Apple OS-Developed Software exception.
30 * ppdCollect() - Collect all marked options that reside in the
32 * ppdEmit() - Emit code for marked options to a file.
33 * ppdEmitFd() - Emit code for marked options to a file.
34 * ppdEmitJCL() - Emit code for JCL options to a file.
35 * ppdEmitJCLEnd() - Emit JCLEnd code to a file.
36 * ppd_handle_media() - Handle media selection...
37 * ppd_sort() - Sort options by ordering numbers...
41 * Include necessary headers...
48 #if defined(WIN32) || defined(__EMX__)
52 #endif /* WIN32 || __EMX__ */
59 static void ppd_handle_media(ppd_file_t
*ppd
);
60 static int ppd_sort(ppd_choice_t
**c1
, ppd_choice_t
**c2
);
67 static const char ppd_custom_code
[] =
69 "<</PageSize[5 -2 roll]/ImagingBBox null>>setpagedevice\n";
73 * 'ppdCollect()' - Collect all marked options that reside in the specified
77 int /* O - Number of options marked */
78 ppdCollect(ppd_file_t
*ppd
, /* I - PPD file data */
79 ppd_section_t section
, /* I - Section to collect */
80 ppd_choice_t
***choices
) /* O - Pointers to choices */
82 int i
, j
, k
, m
; /* Looping vars */
83 ppd_group_t
*g
, /* Current group */
84 *sg
; /* Current sub-group */
85 ppd_option_t
*o
; /* Current option */
86 ppd_choice_t
*c
; /* Current choice */
87 int count
; /* Number of choices collected */
88 ppd_choice_t
**collect
; /* Collected choices */
95 * Allocate memory for up to 1000 selected choices...
99 collect
= calloc(sizeof(ppd_choice_t
*), 1000);
102 * Loop through all options and add choices as needed...
105 for (i
= ppd
->num_groups
, g
= ppd
->groups
; i
> 0; i
--, g
++)
107 for (j
= g
->num_options
, o
= g
->options
; j
> 0; j
--, o
++)
108 if (o
->section
== section
)
109 for (k
= o
->num_choices
, c
= o
->choices
; k
> 0; k
--, c
++)
110 if (c
->marked
&& count
< 1000)
116 for (j
= g
->num_subgroups
, sg
= g
->subgroups
; j
> 0; j
--, sg
++)
117 for (k
= sg
->num_options
, o
= sg
->options
; k
> 0; k
--, o
++)
118 if (o
->section
== section
)
119 for (m
= o
->num_choices
, c
= o
->choices
; m
> 0; m
--, c
++)
120 if (c
->marked
&& count
< 1000)
128 * If we have more than 1 marked choice, sort them...
132 qsort(collect
, count
, sizeof(ppd_choice_t
*),
133 (int (*)(const void *, const void *))ppd_sort
);
136 * Return the array and number of choices; if 0, free the array since
155 * 'ppdEmit()' - Emit code for marked options to a file.
158 int /* O - 0 on success, -1 on failure */
159 ppdEmit(ppd_file_t
*ppd
, /* I - PPD file record */
160 FILE *fp
, /* I - File to write to */
161 ppd_section_t section
) /* I - Section to write */
163 int i
, /* Looping var */
164 count
; /* Number of choices */
165 ppd_choice_t
**choices
; /* Choices */
166 ppd_size_t
*size
; /* Custom page size */
170 * Use PageSize or PageRegion as required...
173 ppd_handle_media(ppd
);
176 * Collect the options we need to emit and emit them!
179 if ((count
= ppdCollect(ppd
, section
, &choices
)) == 0)
182 for (i
= 0; i
< count
; i
++)
183 if (section
!= PPD_ORDER_EXIT
&& section
!= PPD_ORDER_JCL
)
186 * Send wrapper commands to prevent printer errors for unsupported
190 if (fputs("[{\n", fp
) < 0)
197 * Send DSC comments with option...
200 if ((strcasecmp(((ppd_option_t
*)choices
[i
]->option
)->keyword
, "PageSize") == 0 ||
201 strcasecmp(((ppd_option_t
*)choices
[i
]->option
)->keyword
, "PageRegion") == 0) &&
202 strcasecmp(choices
[i
]->choice
, "Custom") == 0)
205 * Variable size; write out standard size options, using the
206 * parameter positions defined in the PPD file...
209 ppd_attr_t
*attr
; /* PPD attribute */
210 int pos
, /* Position of custom value */
211 values
[5], /* Values for custom command */
212 orientation
; /* Orientation to use */
215 fputs("%%BeginFeature: *CustomPageSize True\n", fp
);
217 size
= ppdPageSize(ppd
, "Custom");
219 memset(values
, 0, sizeof(values
));
221 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize", "Width")) != NULL
)
223 pos
= atoi(attr
->value
) - 1;
225 if (pos
< 0 || pos
> 4)
231 values
[pos
] = (int)size
->width
;
233 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize", "Height")) != NULL
)
235 pos
= atoi(attr
->value
) - 1;
237 if (pos
< 0 || pos
> 4)
243 values
[pos
] = (int)size
->length
;
246 * According to the Adobe PPD specification, an orientation of 1
247 * will produce a print that comes out upside-down with the X
248 * axis perpendicular to the direction of feed, which is exactly
249 * what we want to be consistent with non-PS printers.
251 * We could also use an orientation of 3 to produce output that
252 * comes out rightside-up (this is the default for many large format
253 * printer PPDs), however for consistency we will stick with the
256 * If we wanted to get fancy, we could use orientations of 0 or
257 * 2 and swap the width and length, however we don't want to get
258 * fancy, we just want it to work consistently.
260 * The orientation value is range limited by the Orientation
261 * parameter definition, so certain non-PS printer drivers that
262 * only support an Orientation of 0 will get the value 0 as
268 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize",
269 "Orientation")) != NULL
)
271 int min_orient
, max_orient
; /* Minimum and maximum orientations */
274 if (sscanf(attr
->value
, "%d%*s%d%d", &pos
, &min_orient
,
281 if (pos
< 0 || pos
> 4)
284 if (orientation
> max_orient
)
285 orientation
= max_orient
;
286 else if (orientation
< min_orient
)
287 orientation
= min_orient
;
293 values
[pos
] = orientation
;
295 fprintf(fp
, "%d %d %d %d %d\n", values
[0], values
[1],
296 values
[2], values
[3], values
[4]);
298 if (choices
[i
]->code
== NULL
)
301 * This can happen with certain buggy PPD files that don't include
302 * a CustomPageSize command sequence... We just use a generic
303 * Level 2 command sequence...
306 fputs(ppd_custom_code
, fp
);
309 else if (fprintf(fp
, "%%%%BeginFeature: *%s %s\n",
310 ((ppd_option_t
*)choices
[i
]->option
)->keyword
,
311 choices
[i
]->choice
) < 0)
317 if (choices
[i
]->code
!= NULL
&& choices
[i
]->code
[0] != '\0')
319 if (fputs(choices
[i
]->code
, fp
) < 0)
325 if (choices
[i
]->code
[strlen(choices
[i
]->code
) - 1] != '\n')
329 if (fputs("%%EndFeature\n", fp
) < 0)
335 if (fputs("} stopped cleartomark\n", fp
) < 0)
341 else if (fputs(choices
[i
]->code
, fp
) < 0)
353 * 'ppdEmitFd()' - Emit code for marked options to a file.
356 int /* O - 0 on success, -1 on failure */
357 ppdEmitFd(ppd_file_t
*ppd
, /* I - PPD file record */
358 int fd
, /* I - File to write to */
359 ppd_section_t section
) /* I - Section to write */
361 int i
, /* Looping var */
362 count
, /* Number of choices */
363 custom_size
; /* Non-zero if this option is a custom size */
364 ppd_choice_t
**choices
; /* Choices */
365 ppd_size_t
*size
; /* Custom page size */
366 char buf
[1024]; /* Output buffer for feature */
370 * Use PageSize or PageRegion as required...
373 ppd_handle_media(ppd
);
376 * Collect the options we need to emit and emit them!
379 if ((count
= ppdCollect(ppd
, section
, &choices
)) == 0)
382 for (i
= 0; i
< count
; i
++)
383 if (section
!= PPD_ORDER_EXIT
&& section
!= PPD_ORDER_JCL
)
386 * Send wrapper commands to prevent printer errors for unsupported
390 if (write(fd
, "[{\n", 3) < 1)
397 * Send DSC comments with option...
400 if ((strcasecmp(((ppd_option_t
*)choices
[i
]->option
)->keyword
, "PageSize") == 0 ||
401 strcasecmp(((ppd_option_t
*)choices
[i
]->option
)->keyword
, "PageRegion") == 0) &&
402 strcasecmp(choices
[i
]->choice
, "Custom") == 0)
406 strcpy(buf
, "%%BeginFeature: *CustomPageSize True\n");
412 snprintf(buf
, sizeof(buf
), "%%%%BeginFeature: *%s %s\n",
413 ((ppd_option_t
*)choices
[i
]->option
)->keyword
,
417 if (write(fd
, buf
, strlen(buf
)) < 1)
426 * Variable size; write out standard size options, using the
427 * parameter positions defined in the PPD file...
430 ppd_attr_t
*attr
; /* PPD attribute */
431 int pos
, /* Position of custom value */
432 values
[5], /* Values for custom command */
433 orientation
; /* Orientation to use */
436 size
= ppdPageSize(ppd
, "Custom");
438 memset(values
, 0, sizeof(values
));
440 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize", "Width")) != NULL
)
442 pos
= atoi(attr
->value
) - 1;
444 if (pos
< 0 || pos
> 4)
450 values
[pos
] = (int)size
->width
;
452 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize", "Height")) != NULL
)
454 pos
= atoi(attr
->value
) - 1;
456 if (pos
< 0 || pos
> 4)
462 values
[pos
] = (int)size
->length
;
464 if (size
->width
< size
->length
)
469 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize",
470 "Orientation")) != NULL
)
472 int min_orient
, max_orient
; /* Minimum and maximum orientations */
475 if (sscanf(attr
->value
, "%d%*s%d%d", &pos
, &min_orient
,
482 if (pos
< 0 || pos
> 4)
485 if (orientation
> max_orient
)
486 orientation
= max_orient
;
487 else if (orientation
< min_orient
)
488 orientation
= min_orient
;
494 values
[pos
] = orientation
;
496 snprintf(buf
, sizeof(buf
), "%d %d %d %d %d\n", values
[0], values
[1],
497 values
[2], values
[3], values
[4]);
499 if (write(fd
, buf
, strlen(buf
)) < 1)
505 if (choices
[i
]->code
== NULL
)
508 * This can happen with certain buggy PPD files that don't include
509 * a CustomPageSize command sequence... We just use a generic
510 * Level 2 command sequence...
513 if (write(fd
, ppd_custom_code
, strlen(ppd_custom_code
)) < 1)
521 if (choices
[i
]->code
!= NULL
&& choices
[i
]->code
[0] != '\0')
523 if (write(fd
, choices
[i
]->code
, strlen(choices
[i
]->code
)) < 1)
530 if (write(fd
, "%%EndFeature\n", 13) < 1)
536 if (write(fd
, "} stopped cleartomark\n", 22) < 1)
542 else if (write(fd
, choices
[i
]->code
, strlen(choices
[i
]->code
)) < 1)
554 * 'ppdEmitJCL()' - Emit code for JCL options to a file.
557 int /* O - 0 on success, -1 on failure */
558 ppdEmitJCL(ppd_file_t
*ppd
, /* I - PPD file record */
559 FILE *fp
, /* I - File to write to */
560 int job_id
, /* I - Job ID */
561 const char *user
, /* I - Username */
562 const char *title
) /* I - Title */
564 char *ptr
; /* Pointer into JCL string */
565 char temp
[81]; /* Local title string */
569 * Range check the input...
572 if (ppd
== NULL
|| ppd
->jcl_begin
== NULL
|| ppd
->jcl_ps
== NULL
)
576 * See if the printer supports HP PJL...
579 if (!strncmp(ppd
->jcl_begin
, "\033%-12345X@", 10))
582 * This printer uses HP PJL commands for output; filter the output
583 * so that we only have a single "@PJL JOB" command in the header...
585 * To avoid bugs in the PJL implementation of certain vendors' products
586 * (Xerox in particular), we add a dummy "@PJL" command at the beginning
587 * of the PJL commands to initialize PJL processing.
590 fputs("\033%-12345X@PJL\n", fp
);
591 for (ptr
= ppd
->jcl_begin
+ 9; *ptr
;)
592 if (!strncmp(ptr
, "@PJL JOB", 8))
595 * Skip job command...
623 * Eliminate any path info from the job title...
626 if ((ptr
= strrchr(title
, '/')) != NULL
)
630 * Replace double quotes with single quotes so that the title
631 * does not cause a PJL syntax error.
634 strlcpy(temp
, title
, sizeof(temp
));
636 for (ptr
= temp
; *ptr
; ptr
++)
641 * Send PJL JOB and PJL RDYMSG commands before we enter PostScript mode...
644 fprintf(fp
, "@PJL JOB NAME = \"%s\" DISPLAY = \"%d %s %s\"\n", temp
,
646 fprintf(fp
, "@PJL RDYMSG DISPLAY = \"%d %s %s\"\n", job_id
, user
, temp
);
649 fputs(ppd
->jcl_begin
, fp
);
651 ppdEmit(ppd
, fp
, PPD_ORDER_JCL
);
652 fputs(ppd
->jcl_ps
, fp
);
659 * 'ppdEmitJCLEnd()' - Emit JCLEnd code to a file.
664 int /* O - 0 on success, -1 on failure */
665 ppdEmitJCLEnd(ppd_file_t
*ppd
, /* I - PPD file record */
666 FILE *fp
) /* I - File to write to */
668 ppd_attr_t
*attr
; /* PPD attributes */
672 * Range check the input...
678 if (ppd
->jcl_end
== NULL
)
680 if (ppd
->num_filters
== 0)
683 if ((attr
= ppdFindAttr(ppd
, "cupsProtocol", NULL
)) != NULL
&&
684 attr
->value
!= NULL
&& !strcasecmp(attr
->value
, "TBCP"))
685 fputs("\033%-12345X", stdout
);
691 * See if the printer supports HP PJL...
694 if (!strncmp(ppd
->jcl_end
, "\033%-12345X@", 10))
697 * This printer uses HP PJL commands for output; filter the output
698 * so that we only have a single "@PJL JOB" command in the header...
700 * To avoid bugs in the PJL implementation of certain vendors' products
701 * (Xerox in particular), we add a dummy "@PJL" command at the beginning
702 * of the PJL commands to initialize PJL processing.
705 fputs("\033%-12345X@PJL\n", fp
);
706 fputs("@PJL RDYMSG DISPLAY = \"READY\"\n", fp
);
707 fputs(ppd
->jcl_end
+ 9, fp
);
710 fputs(ppd
->jcl_end
, fp
);
717 * 'ppd_handle_media()' - Handle media selection...
721 ppd_handle_media(ppd_file_t
*ppd
)
723 ppd_choice_t
*manual_feed
, /* ManualFeed choice, if any */
724 *input_slot
, /* InputSlot choice, if any */
725 *page
; /* PageSize/PageRegion */
726 ppd_size_t
*size
; /* Current media size */
727 ppd_attr_t
*rpr
; /* RequiresPageRegion value */
731 * This function determines if the user has selected a media source
732 * via the InputSlot or ManualFeed options; if so, it marks the
733 * PageRegion option corresponding to the current media size.
734 * Otherwise it marks the PageSize option.
737 if ((size
= ppdPageSize(ppd
, NULL
)) == NULL
)
740 manual_feed
= ppdFindMarkedChoice(ppd
, "ManualFeed");
741 input_slot
= ppdFindMarkedChoice(ppd
, "InputSlot");
743 if (input_slot
!= NULL
)
744 rpr
= ppdFindAttr(ppd
, "RequiresPageRegion", input_slot
->choice
);
749 rpr
= ppdFindAttr(ppd
, "RequiresPageRegion", "All");
751 if (strcasecmp(size
->name
, "Custom") == 0 ||
752 (manual_feed
== NULL
&& input_slot
== NULL
) ||
753 (manual_feed
!= NULL
&& strcasecmp(manual_feed
->choice
, "False") == 0) ||
754 (input_slot
!= NULL
&& (input_slot
->code
== NULL
|| !input_slot
->code
[0])))
757 * Manual feed was not selected and/or the input slot selection does
758 * not contain any PostScript code. Use the PageSize option...
761 ppdMarkOption(ppd
, "PageSize", size
->name
);
766 * Manual feed was selected and/or the input slot selection contains
767 * PostScript code. Use the PageRegion option...
770 ppdMarkOption(ppd
, "PageRegion", size
->name
);
773 * RequiresPageRegion does not apply to manual feed so we need to
774 * check that we are not doing manual feed before unmarking PageRegion.
777 if (!(manual_feed
&& !strcasecmp(manual_feed
->choice
, "True")) &&
778 ((rpr
&& rpr
->value
&& !strcmp(rpr
->value
, "False")) ||
779 (!rpr
&& !ppd
->num_filters
)))
782 * Either the PPD file specifies no PageRegion code or the PPD file
783 * not for a CUPS raster driver and thus defaults to no PageRegion
784 * code... Unmark the PageRegion choice so that we don't output the
788 page
= ppdFindMarkedChoice(ppd
, "PageRegion");
798 * 'ppd_sort()' - Sort options by ordering numbers...
801 static int /* O - -1 if c1 < c2, 0 if equal, 1 otherwise */
802 ppd_sort(ppd_choice_t
**c1
, /* I - First choice */
803 ppd_choice_t
**c2
) /* I - Second choice */
805 if (((ppd_option_t
*)(*c1
)->option
)->order
< ((ppd_option_t
*)(*c2
)->option
)->order
)
807 else if (((ppd_option_t
*)(*c1
)->option
)->order
> ((ppd_option_t
*)(*c2
)->option
)->order
)
815 * End of "$Id: emit.c 4785 2005-10-13 19:39:05Z mike $".