]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/emit.c
4 * PPD code emission routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 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 * 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 file.
36 * ppdEmitFd() - Emit code for marked options to a file.
37 * ppdEmitJCL() - Emit code for JCL options to a file.
38 * ppdEmitJCLEnd() - Emit JCLEnd code to a file.
39 * ppd_handle_media() - Handle media selection...
40 * ppd_sort() - Sort options by ordering numbers...
44 * Include necessary headers...
51 #if defined(WIN32) || defined(__EMX__)
55 #endif /* WIN32 || __EMX__ */
62 static void ppd_handle_media(ppd_file_t
*ppd
);
63 static int ppd_sort(ppd_choice_t
**c1
, ppd_choice_t
**c2
);
70 static const char ppd_custom_code
[] =
72 "<</PageSize[5 -2 roll]/ImagingBBox null>>setpagedevice\n";
76 * 'ppdCollect()' - Collect all marked options that reside in the specified
80 int /* O - Number of options marked */
81 ppdCollect(ppd_file_t
*ppd
, /* I - PPD file data */
82 ppd_section_t section
, /* I - Section to collect */
83 ppd_choice_t
***choices
) /* O - Pointers to choices */
85 return (ppdCollect2(ppd
, section
, 0.0, choices
));
90 * 'ppdCollect2()' - Collect all marked options that reside in the
91 * specified section and minimum order.
96 int /* O - Number of options marked */
97 ppdCollect2(ppd_file_t
*ppd
, /* I - PPD file data */
98 ppd_section_t section
, /* I - Section to collect */
99 float min_order
, /* I - Minimum OrderDependency value */
100 ppd_choice_t
***choices
) /* O - Pointers to choices */
102 int i
, j
, k
, m
; /* Looping vars */
103 ppd_group_t
*g
, /* Current group */
104 *sg
; /* Current sub-group */
105 ppd_option_t
*o
; /* Current option */
106 ppd_choice_t
*c
; /* Current choice */
107 int count
; /* Number of choices collected */
108 ppd_choice_t
**collect
; /* Collected choices */
115 * Allocate memory for up to 1000 selected choices...
119 collect
= calloc(sizeof(ppd_choice_t
*), 1000);
122 * Loop through all options and add choices as needed...
125 for (i
= ppd
->num_groups
, g
= ppd
->groups
; i
> 0; i
--, g
++)
127 for (j
= g
->num_options
, o
= g
->options
; j
> 0; j
--, o
++)
128 if (o
->section
== section
&& o
->order
>= min_order
)
129 for (k
= o
->num_choices
, c
= o
->choices
; k
> 0; k
--, c
++)
130 if (c
->marked
&& count
< 1000)
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)
148 * If we have more than 1 marked choice, sort them...
152 qsort(collect
, count
, sizeof(ppd_choice_t
*),
153 (int (*)(const void *, const void *))ppd_sort
);
156 * Return the array and number of choices; if 0, free the array since
175 * 'ppdEmit()' - Emit code for marked options to a file.
178 int /* O - 0 on success, -1 on failure */
179 ppdEmit(ppd_file_t
*ppd
, /* I - PPD file record */
180 FILE *fp
, /* I - File to write to */
181 ppd_section_t section
) /* I - Section to write */
183 return (ppdEmitAfterOrder(ppd
, fp
, section
, 0, 0.0));
188 * 'ppdEmitAfterOrder()' - Emit a subset of the code for marked options to a file.
190 * When "limit" is non-zero, this function only emits options whose
191 * OrderDependency value is greater than or equal to "min_order".
193 * When "limit" is zero, this function is identical to ppdEmit().
198 int /* O - 0 on success, -1 on failure */
200 ppd_file_t
*ppd
, /* I - PPD file record */
201 FILE *fp
, /* I - File to write to */
202 ppd_section_t section
, /* I - Section to write */
203 int limit
, /* I - Non-zero to use min_order, 0 to include all */
204 float min_order
) /* I - Lowest order dependency to include */
206 int i
, /* Looping var */
207 count
; /* Number of choices */
208 ppd_choice_t
**choices
; /* Choices */
209 ppd_size_t
*size
; /* Custom page size */
213 * Use PageSize or PageRegion as required...
216 ppd_handle_media(ppd
);
219 * Collect the options we need to emit and emit them!
222 if ((count
= ppdCollect2(ppd
, section
, min_order
, &choices
)) == 0)
225 for (i
= 0; i
< count
; i
++)
226 if (section
!= PPD_ORDER_EXIT
&& section
!= PPD_ORDER_JCL
)
229 * Send wrapper commands to prevent printer errors for unsupported
233 if (fputs("[{\n", fp
) < 0)
240 * Send DSC comments with option...
243 if ((!strcasecmp(choices
[i
]->option
->keyword
, "PageSize") ||
244 !strcasecmp(choices
[i
]->option
->keyword
, "PageRegion")) &&
245 !strcasecmp(choices
[i
]->choice
, "Custom"))
248 * Variable size; write out standard size options, using the
249 * parameter positions defined in the PPD file...
252 ppd_attr_t
*attr
; /* PPD attribute */
253 int pos
, /* Position of custom value */
254 orientation
; /* Orientation to use */
255 float values
[5]; /* Values for custom command */
256 int isfloat
[5]; /* Whether each value is float or int */
258 fputs("%%BeginFeature: *CustomPageSize True\n", fp
);
260 size
= ppdPageSize(ppd
, "Custom");
262 memset(values
, 0, sizeof(values
));
263 memset(isfloat
, 0, sizeof(isfloat
));
265 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize", "Width")) != NULL
)
267 pos
= atoi(attr
->value
) - 1;
269 if (pos
< 0 || pos
> 4)
275 values
[pos
] = size
->width
;
278 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize", "Height")) != NULL
)
280 pos
= atoi(attr
->value
) - 1;
282 if (pos
< 0 || pos
> 4)
288 values
[pos
] = size
->length
;
292 * According to the Adobe PPD specification, an orientation of 1
293 * will produce a print that comes out upside-down with the X
294 * axis perpendicular to the direction of feed, which is exactly
295 * what we want to be consistent with non-PS printers.
297 * We could also use an orientation of 3 to produce output that
298 * comes out rightside-up (this is the default for many large format
299 * printer PPDs), however for consistency we will stick with the
302 * If we wanted to get fancy, we could use orientations of 0 or
303 * 2 and swap the width and length, however we don't want to get
304 * fancy, we just want it to work consistently.
306 * The orientation value is range limited by the Orientation
307 * parameter definition, so certain non-PS printer drivers that
308 * only support an Orientation of 0 will get the value 0 as
314 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize",
315 "Orientation")) != NULL
)
317 int min_orient
, max_orient
; /* Minimum and maximum orientations */
320 if (sscanf(attr
->value
, "%d%*s%d%d", &pos
, &min_orient
,
327 if (pos
< 0 || pos
> 4)
330 if (orientation
> max_orient
)
331 orientation
= max_orient
;
332 else if (orientation
< min_orient
)
333 orientation
= min_orient
;
339 values
[pos
] = orientation
;
341 for (pos
= 0; pos
< 5; pos
++)
343 fprintf(fp
, "%.2f\n", values
[pos
]);
345 fprintf(fp
, "%.0f\n", values
[pos
]);
347 if (choices
[i
]->code
== NULL
)
350 * This can happen with certain buggy PPD files that don't include
351 * a CustomPageSize command sequence... We just use a generic
352 * Level 2 command sequence...
355 fputs(ppd_custom_code
, fp
);
358 else if (!strcasecmp(choices
[i
]->choice
, "Custom"))
364 ppd_coption_t
*coption
; /* Custom option */
365 ppd_cparam_t
*cparam
; /* Custom parameter */
366 const char *s
; /* Pointer into string value */
370 * TODO: Support custom options with more than 1 parameter...
373 if ((coption
= ppdFindCustomOption(ppd
, choices
[i
]->option
->keyword
))
375 (cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
)) != NULL
)
377 if (fprintf(fp
, "%%%%BeginFeature: *Custom%s True\n",
378 coption
->keyword
) < 0)
384 switch (cparam
->type
)
386 case PPD_CUSTOM_CURVE
:
387 case PPD_CUSTOM_INVCURVE
:
388 case PPD_CUSTOM_POINTS
:
389 case PPD_CUSTOM_REAL
:
390 if (fprintf(fp
, "%f\n", cparam
->current
.custom_real
) < 0)
397 case PPD_CUSTOM_INT
:
398 if (fprintf(fp
, "%d\n", cparam
->current
.custom_int
) < 0)
405 case PPD_CUSTOM_PASSCODE
:
406 case PPD_CUSTOM_PASSWORD
:
407 case PPD_CUSTOM_STRING
:
410 for (s
= cparam
->current
.custom_string
; *s
; s
++)
411 if (*s
< ' ' || *s
== '(' || *s
== ')' || *s
>= 127)
412 fprintf(fp
, "\\%03o", *s
& 255);
416 if (fputs(")\n", fp
) < 0)
425 else if (fprintf(fp
, "%%%%BeginFeature: *%s %s\n",
426 choices
[i
]->option
->keyword
,
427 choices
[i
]->choice
) < 0)
433 if (choices
[i
]->code
!= NULL
&& choices
[i
]->code
[0] != '\0')
435 if (fputs(choices
[i
]->code
, fp
) < 0)
441 if (choices
[i
]->code
[strlen(choices
[i
]->code
) - 1] != '\n')
445 if (fputs("%%EndFeature\n", fp
) < 0)
451 if (fputs("} stopped cleartomark\n", fp
) < 0)
457 else if (fputs(choices
[i
]->code
, fp
) < 0)
469 * 'ppdEmitFd()' - Emit code for marked options to a file.
472 int /* O - 0 on success, -1 on failure */
473 ppdEmitFd(ppd_file_t
*ppd
, /* I - PPD file record */
474 int fd
, /* I - File to write to */
475 ppd_section_t section
) /* I - Section to write */
477 int i
, /* Looping var */
478 count
, /* Number of choices */
479 custom_size
; /* Non-zero if this option is a custom size */
480 ppd_choice_t
**choices
; /* Choices */
481 ppd_size_t
*size
; /* Custom page size */
482 char buf
[1024]; /* Output buffer for feature */
486 * Use PageSize or PageRegion as required...
489 ppd_handle_media(ppd
);
492 * Collect the options we need to emit and emit them!
495 if ((count
= ppdCollect(ppd
, section
, &choices
)) == 0)
498 for (i
= 0; i
< count
; i
++)
499 if (section
!= PPD_ORDER_EXIT
&& section
!= PPD_ORDER_JCL
)
502 * Send wrapper commands to prevent printer errors for unsupported
506 if (write(fd
, "[{\n", 3) < 1)
513 * Send DSC comments with option...
516 if ((!strcasecmp(choices
[i
]->option
->keyword
, "PageSize") ||
517 !strcasecmp(choices
[i
]->option
->keyword
, "PageRegion")) &&
518 !strcasecmp(choices
[i
]->choice
, "Custom"))
522 strcpy(buf
, "%%BeginFeature: *CustomPageSize True\n");
528 snprintf(buf
, sizeof(buf
), "%%%%BeginFeature: *%s %s\n",
529 choices
[i
]->option
->keyword
, choices
[i
]->choice
);
532 if (write(fd
, buf
, strlen(buf
)) < 1)
541 * Variable size; write out standard size options, using the
542 * parameter positions defined in the PPD file...
545 ppd_attr_t
*attr
; /* PPD attribute */
546 int pos
, /* Position of custom value */
547 values
[5], /* Values for custom command */
548 orientation
; /* Orientation to use */
551 size
= ppdPageSize(ppd
, "Custom");
553 memset(values
, 0, sizeof(values
));
555 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize", "Width")) != NULL
)
557 pos
= atoi(attr
->value
) - 1;
559 if (pos
< 0 || pos
> 4)
565 values
[pos
] = (int)size
->width
;
567 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize", "Height")) != NULL
)
569 pos
= atoi(attr
->value
) - 1;
571 if (pos
< 0 || pos
> 4)
577 values
[pos
] = (int)size
->length
;
579 if (size
->width
< size
->length
)
584 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize",
585 "Orientation")) != NULL
)
587 int min_orient
, max_orient
; /* Minimum and maximum orientations */
590 if (sscanf(attr
->value
, "%d%*s%d%d", &pos
, &min_orient
,
597 if (pos
< 0 || pos
> 4)
600 if (orientation
> max_orient
)
601 orientation
= max_orient
;
602 else if (orientation
< min_orient
)
603 orientation
= min_orient
;
609 values
[pos
] = orientation
;
611 snprintf(buf
, sizeof(buf
), "%d %d %d %d %d\n", values
[0], values
[1],
612 values
[2], values
[3], values
[4]);
614 if (write(fd
, buf
, strlen(buf
)) < 1)
620 if (choices
[i
]->code
== NULL
)
623 * This can happen with certain buggy PPD files that don't include
624 * a CustomPageSize command sequence... We just use a generic
625 * Level 2 command sequence...
628 if (write(fd
, ppd_custom_code
, strlen(ppd_custom_code
)) < 1)
636 if (choices
[i
]->code
!= NULL
&& choices
[i
]->code
[0] != '\0')
638 if (write(fd
, choices
[i
]->code
, strlen(choices
[i
]->code
)) < 1)
645 if (write(fd
, "%%EndFeature\n", 13) < 1)
651 if (write(fd
, "} stopped cleartomark\n", 22) < 1)
657 else if (write(fd
, choices
[i
]->code
, strlen(choices
[i
]->code
)) < 1)
669 * 'ppdEmitJCL()' - Emit code for JCL options to a file.
672 int /* O - 0 on success, -1 on failure */
673 ppdEmitJCL(ppd_file_t
*ppd
, /* I - PPD file record */
674 FILE *fp
, /* I - File to write to */
675 int job_id
, /* I - Job ID */
676 const char *user
, /* I - Username */
677 const char *title
) /* I - Title */
679 char *ptr
; /* Pointer into JCL string */
680 char temp
[81]; /* Local title string */
684 * Range check the input...
687 if (ppd
== NULL
|| ppd
->jcl_begin
== NULL
|| ppd
->jcl_ps
== NULL
)
691 * See if the printer supports HP PJL...
694 if (!strncmp(ppd
->jcl_begin
, "\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 for (ptr
= ppd
->jcl_begin
+ 9; *ptr
;)
707 if (!strncmp(ptr
, "@PJL JOB", 8))
710 * Skip job command...
738 * Eliminate any path info from the job title...
741 if ((ptr
= strrchr(title
, '/')) != NULL
)
745 * Replace double quotes with single quotes so that the title
746 * does not cause a PJL syntax error.
749 strlcpy(temp
, title
, sizeof(temp
));
751 for (ptr
= temp
; *ptr
; ptr
++)
756 * Send PJL JOB and PJL RDYMSG commands before we enter PostScript mode...
759 fprintf(fp
, "@PJL JOB NAME = \"%s\" DISPLAY = \"%d %s %s\"\n", temp
,
761 fprintf(fp
, "@PJL RDYMSG DISPLAY = \"%d %s %s\"\n", job_id
, user
, temp
);
764 fputs(ppd
->jcl_begin
, fp
);
766 ppdEmit(ppd
, fp
, PPD_ORDER_JCL
);
767 fputs(ppd
->jcl_ps
, fp
);
774 * 'ppdEmitJCLEnd()' - Emit JCLEnd code to a file.
779 int /* O - 0 on success, -1 on failure */
780 ppdEmitJCLEnd(ppd_file_t
*ppd
, /* I - PPD file record */
781 FILE *fp
) /* I - File to write to */
783 ppd_attr_t
*attr
; /* PPD attributes */
787 * Range check the input...
793 if (ppd
->jcl_end
== NULL
)
795 if (ppd
->num_filters
== 0)
798 if ((attr
= ppdFindAttr(ppd
, "cupsProtocol", NULL
)) != NULL
&&
799 attr
->value
!= NULL
&& !strcasecmp(attr
->value
, "TBCP"))
800 fputs("\033%-12345X", stdout
);
806 * See if the printer supports HP PJL...
809 if (!strncmp(ppd
->jcl_end
, "\033%-12345X@", 10))
812 * This printer uses HP PJL commands for output; filter the output
813 * so that we only have a single "@PJL JOB" command in the header...
815 * To avoid bugs in the PJL implementation of certain vendors' products
816 * (Xerox in particular), we add a dummy "@PJL" command at the beginning
817 * of the PJL commands to initialize PJL processing.
820 fputs("\033%-12345X@PJL\n", fp
);
821 fputs("@PJL RDYMSG DISPLAY = \"READY\"\n", fp
);
822 fputs(ppd
->jcl_end
+ 9, fp
);
825 fputs(ppd
->jcl_end
, fp
);
832 * 'ppd_handle_media()' - Handle media selection...
836 ppd_handle_media(ppd_file_t
*ppd
)
838 ppd_choice_t
*manual_feed
, /* ManualFeed choice, if any */
839 *input_slot
, /* InputSlot choice, if any */
840 *page
; /* PageSize/PageRegion */
841 ppd_size_t
*size
; /* Current media size */
842 ppd_attr_t
*rpr
; /* RequiresPageRegion value */
846 * This function determines if the user has selected a media source
847 * via the InputSlot or ManualFeed options; if so, it marks the
848 * PageRegion option corresponding to the current media size.
849 * Otherwise it marks the PageSize option.
852 if ((size
= ppdPageSize(ppd
, NULL
)) == NULL
)
855 manual_feed
= ppdFindMarkedChoice(ppd
, "ManualFeed");
856 input_slot
= ppdFindMarkedChoice(ppd
, "InputSlot");
858 if (input_slot
!= NULL
)
859 rpr
= ppdFindAttr(ppd
, "RequiresPageRegion", input_slot
->choice
);
864 rpr
= ppdFindAttr(ppd
, "RequiresPageRegion", "All");
866 if (!strcasecmp(size
->name
, "Custom") || (!manual_feed
&& !input_slot
) ||
867 !((manual_feed
&& !strcasecmp(manual_feed
->choice
, "True")) ||
868 (input_slot
&& input_slot
->code
&& input_slot
->code
[0])))
871 * Manual feed was not selected and/or the input slot selection does
872 * not contain any PostScript code. Use the PageSize option...
875 ppdMarkOption(ppd
, "PageSize", size
->name
);
880 * Manual feed was selected and/or the input slot selection contains
881 * PostScript code. Use the PageRegion option...
884 ppdMarkOption(ppd
, "PageRegion", size
->name
);
887 * RequiresPageRegion does not apply to manual feed so we need to
888 * check that we are not doing manual feed before unmarking PageRegion.
891 if (!(manual_feed
&& !strcasecmp(manual_feed
->choice
, "True")) &&
892 ((rpr
&& rpr
->value
&& !strcmp(rpr
->value
, "False")) ||
893 (!rpr
&& !ppd
->num_filters
)))
896 * Either the PPD file specifies no PageRegion code or the PPD file
897 * not for a CUPS raster driver and thus defaults to no PageRegion
898 * code... Unmark the PageRegion choice so that we don't output the
902 page
= ppdFindMarkedChoice(ppd
, "PageRegion");
912 * 'ppd_sort()' - Sort options by ordering numbers...
915 static int /* O - -1 if c1 < c2, 0 if equal, 1 otherwise */
916 ppd_sort(ppd_choice_t
**c1
, /* I - First choice */
917 ppd_choice_t
**c2
) /* I - Second choice */
919 if ((*c1
)->option
->order
< (*c2
)->option
->order
)
921 else if ((*c1
)->option
->order
> (*c2
)->option
->order
)