2 * "$Id: interpret.c 6649 2007-07-11 21:46:42Z mike $"
4 * PPD command interpreter for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1993-2007 by Easy Software Products.
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/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * cupsRasterInterpretPPD() - Interpret PPD commands to create a page header.
20 * _cupsRasterExecPS() - Execute PostScript code to initialize a page
22 * cleartomark_stack() - Clear to the last mark ([) on the stack.
23 * copy_stack() - Copy the top N stack objects.
24 * delete_stack() - Free memory used by a stack.
25 * error_object() - Add an object's value to the current error
27 * error_stack() - Add a stack to the current error message.
28 * index_stack() - Copy the Nth value on the stack.
29 * new_stack() - Create a new stack.
30 * pop_stock() - Pop the top object off the stack.
31 * push_stack() - Push an object on the stack.
32 * roll_stack() - Rotate stack objects.
33 * scan_ps() - Scan a string for the next PS object.
34 * setpagedevice() - Simulate the PostScript setpagedevice operator.
35 * DEBUG_object() - Print an object value.
36 * DEBUG_stack() - Print a stack.
40 * Include necessary headers...
43 #include <cups/string.h>
44 #include "image-private.h"
49 * Stack values for the PostScript mini-interpreter...
71 CUPS_PS_SETPAGEDEVICE
,
78 _cups_ps_type_t type
; /* Object type */
81 int boolean
; /* Boolean value */
82 char name
[64]; /* Name value */
83 double number
; /* Number value */
84 char other
[64]; /* Other operator */
85 char string
[64]; /* Sring value */
91 int num_objs
, /* Number of objects on stack */
92 alloc_objs
; /* Number of allocated objects */
93 _cups_ps_obj_t
*objs
; /* Objects in stack */
101 static int cleartomark_stack(_cups_ps_stack_t
*st
);
102 static int copy_stack(_cups_ps_stack_t
*st
, int count
);
103 static void delete_stack(_cups_ps_stack_t
*st
);
104 static void error_object(_cups_ps_obj_t
*obj
);
105 static void error_stack(_cups_ps_stack_t
*st
, const char *title
);
106 static _cups_ps_obj_t
*index_stack(_cups_ps_stack_t
*st
, int n
);
107 static _cups_ps_stack_t
*new_stack(void);
108 static _cups_ps_obj_t
*pop_stack(_cups_ps_stack_t
*st
);
109 static _cups_ps_obj_t
*push_stack(_cups_ps_stack_t
*st
,
110 _cups_ps_obj_t
*obj
);
111 static int roll_stack(_cups_ps_stack_t
*st
, int c
, int s
);
112 static _cups_ps_obj_t
*scan_ps(_cups_ps_stack_t
*st
, char **ptr
);
113 static int setpagedevice(_cups_ps_stack_t
*st
,
114 cups_page_header2_t
*h
,
115 int *preferred_bits
);
117 static void DEBUG_object(_cups_ps_obj_t
*obj
);
118 static void DEBUG_stack(_cups_ps_stack_t
*st
);
123 * 'cupsRasterInterpretPPD()' - Interpret PPD commands to create a page header.
125 * This function does not mark the options in the PPD using the "num_options"
126 * and "options" arguments. Instead, mark the options with
127 * @code cupsMarkOptions@ and @code ppdMarkOption@ prior to calling
128 * @code cupsRasterInterpretPPD@ - this allows you to do per-page options
129 * without manipulating the options array.
131 * The "func" argument specifies an optional callback function that is
132 * called prior to the computation of the final raster data. The function
133 * can make changes to the @link cups_page_header2_t@ data as needed to use a
134 * supported raster format and then returns 0 on success and -1 if the
135 * requested attributes cannot be supported.
137 * @code cupsRasterInterpretPPD@ supports a subset of the PostScript language.
138 * Currently only the @code [@, @code ]@, @code <<@, @code >>@, @code {@,
139 * @code }@, @code cleartomark@, @code copy@, @code dup@, @code index@,
140 * @code pop@, @code roll@, @code setpagedevice@, and @code stopped@ operators
146 int /* O - 0 on success, -1 on failure */
147 cupsRasterInterpretPPD(
148 cups_page_header2_t
*h
, /* O - Page header */
149 ppd_file_t
*ppd
, /* I - PPD file */
150 int num_options
, /* I - Number of options */
151 cups_option_t
*options
, /* I - Options */
152 cups_interpret_cb_t func
) /* I - Optional page header callback */
154 int status
; /* Cummulative status */
155 char *code
; /* Code to run */
156 const char *val
; /* Option value */
157 ppd_size_t
*size
; /* Current size */
158 float left
, /* Left position */
159 bottom
, /* Bottom position */
160 right
, /* Right position */
161 top
; /* Top position */
162 int preferred_bits
; /* Preferred bits per color */
166 * Range check input...
169 _cupsRasterClearError();
173 _cupsRasterAddError("Page header cannot be NULL!\n");
178 * Reset the page header to the defaults...
181 memset(h
, 0, sizeof(cups_page_header2_t
));
184 h
->PageSize
[0] = 612;
185 h
->PageSize
[1] = 792;
186 h
->HWResolution
[0] = 100;
187 h
->HWResolution
[1] = 100;
188 h
->cupsBitsPerColor
= 1;
189 h
->cupsColorOrder
= CUPS_ORDER_CHUNKED
;
190 h
->cupsColorSpace
= CUPS_CSPACE_K
;
191 h
->cupsBorderlessScalingFactor
= 1.0f
;
192 h
->cupsPageSize
[0] = 612.0f
;
193 h
->cupsPageSize
[1] = 792.0f
;
194 h
->cupsImagingBBox
[0] = 0.0f
;
195 h
->cupsImagingBBox
[1] = 0.0f
;
196 h
->cupsImagingBBox
[2] = 612.0f
;
197 h
->cupsImagingBBox
[3] = 792.0f
;
199 strcpy(h
->cupsPageSizeName
, "Letter");
202 * Apply patches and options to the page header...
211 * Apply any patch code (used to override the defaults...)
215 status
|= _cupsRasterExecPS(h
, &preferred_bits
, ppd
->patches
);
218 * Then apply printer options in the proper order...
221 if ((code
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, 0.0)) != NULL
)
223 status
|= _cupsRasterExecPS(h
, &preferred_bits
, code
);
227 if ((code
= ppdEmitString(ppd
, PPD_ORDER_ANY
, 0.0)) != NULL
)
229 status
|= _cupsRasterExecPS(h
, &preferred_bits
, code
);
233 if ((code
= ppdEmitString(ppd
, PPD_ORDER_PROLOG
, 0.0)) != NULL
)
235 status
|= _cupsRasterExecPS(h
, &preferred_bits
, code
);
239 if ((code
= ppdEmitString(ppd
, PPD_ORDER_PAGE
, 0.0)) != NULL
)
241 status
|= _cupsRasterExecPS(h
, &preferred_bits
, code
);
247 * Allow option override for page scaling...
250 if ((val
= cupsGetOption("cupsBorderlessScalingFactor", num_options
,
253 float sc
= atof(val
);
255 if (sc
>= 0.5 && sc
<= 2.0)
256 h
->cupsBorderlessScalingFactor
= sc
;
260 * Get the margins for the current size...
263 if ((size
= ppdPageSize(ppd
, NULL
)) != NULL
)
266 * Use the margins from the PPD file...
270 bottom
= size
->bottom
;
274 strlcpy(h
->cupsPageSizeName
, size
->name
, sizeof(h
->cupsPageSizeName
));
276 h
->cupsPageSize
[0] = size
->width
;
277 h
->cupsPageSize
[1] = size
->length
;
282 * Use the default margins...
291 h
->PageSize
[0] = h
->cupsPageSize
[0] *
292 h
->cupsBorderlessScalingFactor
;
293 h
->PageSize
[1] = h
->cupsPageSize
[1] *
294 h
->cupsBorderlessScalingFactor
;
295 h
->Margins
[0] = left
* h
->cupsBorderlessScalingFactor
;
296 h
->Margins
[1] = bottom
* h
->cupsBorderlessScalingFactor
;
297 h
->ImagingBoundingBox
[0] = left
* h
->cupsBorderlessScalingFactor
;
298 h
->ImagingBoundingBox
[1] = bottom
* h
->cupsBorderlessScalingFactor
;
299 h
->ImagingBoundingBox
[2] = right
* h
->cupsBorderlessScalingFactor
;
300 h
->ImagingBoundingBox
[3] = top
* h
->cupsBorderlessScalingFactor
;
301 h
->cupsImagingBBox
[0] = left
;
302 h
->cupsImagingBBox
[1] = bottom
;
303 h
->cupsImagingBBox
[2] = right
;
304 h
->cupsImagingBBox
[3] = top
;
307 * Use the callback to validate the page header...
310 if (func
&& (*func
)(h
, preferred_bits
))
312 _cupsRasterAddError("Page header callback returned error.\n");
317 * Check parameters...
320 if (!h
->HWResolution
[0] || !h
->HWResolution
[1] ||
321 !h
->PageSize
[0] || !h
->PageSize
[1] ||
322 (h
->cupsBitsPerColor
!= 1 && h
->cupsBitsPerColor
!= 2 &&
323 h
->cupsBitsPerColor
!= 4 && h
->cupsBitsPerColor
!= 8 &&
324 h
->cupsBitsPerColor
!= 16) ||
325 h
->cupsBorderlessScalingFactor
< 0.5 ||
326 h
->cupsBorderlessScalingFactor
> 2.0)
328 _cupsRasterAddError("Page header uses unsupported values.\n");
333 * Compute the bitmap parameters...
336 h
->cupsWidth
= (int)((right
- left
) * h
->cupsBorderlessScalingFactor
*
337 h
->HWResolution
[0] / 72.0f
+ 0.5f
);
338 h
->cupsHeight
= (int)((top
- bottom
) * h
->cupsBorderlessScalingFactor
*
339 h
->HWResolution
[1] / 72.0f
+ 0.5f
);
341 switch (h
->cupsColorSpace
)
345 case CUPS_CSPACE_WHITE
:
346 case CUPS_CSPACE_GOLD
:
347 case CUPS_CSPACE_SILVER
:
348 h
->cupsNumColors
= 1;
349 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
;
354 * Ensure that colorimetric colorspaces use at least 8 bits per
358 if (h
->cupsColorSpace
>= CUPS_CSPACE_CIEXYZ
&&
359 h
->cupsBitsPerColor
< 8)
360 h
->cupsBitsPerColor
= 8;
363 * Figure out the number of bits per pixel...
366 if (h
->cupsColorOrder
== CUPS_ORDER_CHUNKED
)
368 if (h
->cupsBitsPerColor
>= 8)
369 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
* 3;
371 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
* 4;
374 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
;
376 h
->cupsNumColors
= 3;
379 case CUPS_CSPACE_KCMYcm
:
380 if (h
->cupsBitsPerColor
== 1)
382 if (h
->cupsColorOrder
== CUPS_ORDER_CHUNKED
)
383 h
->cupsBitsPerPixel
= 8;
385 h
->cupsBitsPerPixel
= 1;
387 h
->cupsNumColors
= 6;
392 * Fall through to CMYK code...
395 case CUPS_CSPACE_RGBA
:
396 case CUPS_CSPACE_RGBW
:
397 case CUPS_CSPACE_CMYK
:
398 case CUPS_CSPACE_YMCK
:
399 case CUPS_CSPACE_KCMY
:
400 case CUPS_CSPACE_GMCK
:
401 case CUPS_CSPACE_GMCS
:
402 if (h
->cupsColorOrder
== CUPS_ORDER_CHUNKED
)
403 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
* 4;
405 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
;
407 h
->cupsNumColors
= 4;
411 h
->cupsBytesPerLine
= (h
->cupsBitsPerPixel
* h
->cupsWidth
+ 7) / 8;
413 if (h
->cupsColorOrder
== CUPS_ORDER_BANDED
)
414 h
->cupsBytesPerLine
*= h
->cupsNumColors
;
421 * '_cupsRasterExecPS()' - Execute PostScript code to initialize a page header.
424 int /* O - 0 on success, -1 on error */
426 cups_page_header2_t
*h
, /* O - Page header */
427 int *preferred_bits
,/* O - Preferred bits per color */
428 const char *code
) /* I - PS code to execute */
430 _cups_ps_stack_t
*st
; /* PostScript value stack */
431 _cups_ps_obj_t
*obj
; /* Object from top of stack */
432 char *codecopy
, /* Copy of code */
433 *codeptr
; /* Pointer into copy of code */
436 DEBUG_printf(("_cupsRasterExecPS(h=%p, preferred_bits=%p, code=\"%s\")\n",
437 h
, preferred_bits
, code
? code
: "(null)"));
440 * Copy the PostScript code and create a stack...
443 if ((codecopy
= strdup(code
)) == NULL
)
445 _cupsRasterAddError("Unable to duplicate code string.\n");
449 if ((st
= new_stack()) == NULL
)
451 _cupsRasterAddError("Unable to create stack.\n");
457 * Parse the PS string until we run out of data...
462 while ((obj
= scan_ps(st
, &codeptr
)) != NULL
)
465 printf(" (%d): ", st
->num_objs
);
473 /* Do nothing for regular values */
476 case CUPS_PS_CLEARTOMARK
:
479 if (cleartomark_stack(st
))
480 _cupsRasterAddError("cleartomark: Stack underflow!\n");
483 fputs(" dup: ", stdout
);
490 if ((obj
= pop_stack(st
)) != NULL
)
492 copy_stack(st
, (int)obj
->value
.number
);
495 fputs(" copy: ", stdout
);
506 fputs(" dup: ", stdout
);
513 if ((obj
= pop_stack(st
)) != NULL
)
515 index_stack(st
, (int)obj
->value
.number
);
518 fputs(" index: ", stdout
);
529 fputs(" pop: ", stdout
);
536 if ((obj
= pop_stack(st
)) != NULL
)
541 c
= (int)obj
->value
.number
;
543 if ((obj
= pop_stack(st
)) != NULL
)
545 roll_stack(st
, (int)obj
->value
.number
, c
);
548 fputs(" roll:", stdout
);
555 case CUPS_PS_SETPAGEDEVICE
:
557 setpagedevice(st
, h
, preferred_bits
);
560 fputs(" setpagedevice: ", stdout
);
565 case CUPS_PS_START_PROC
:
566 case CUPS_PS_END_PROC
:
567 case CUPS_PS_STOPPED
:
572 _cupsRasterAddError("Unknown operator \"%s\"!\n", obj
->value
.other
);
573 DEBUG_printf((" Unknown operator \"%s\"!\n", obj
->value
.other
));
577 if (obj
&& obj
->type
== CUPS_PS_OTHER
)
587 if (st
->num_objs
> 0)
589 error_stack(st
, "Stack not empty:");
592 fputs(" Stack not empty:", stdout
);
612 * 'cleartomark_stack()' - Clear to the last mark ([) on the stack.
615 static int /* O - 0 on success, -1 on error */
616 cleartomark_stack(_cups_ps_stack_t
*st
) /* I - Stack */
618 _cups_ps_obj_t
*obj
; /* Current object on stack */
621 while ((obj
= pop_stack(st
)) != NULL
)
622 if (obj
->type
== CUPS_PS_START_ARRAY
)
625 return (obj
? 0 : -1);
630 * 'copy_stack()' - Copy the top N stack objects.
633 static int /* O - 0 on success, -1 on error */
634 copy_stack(_cups_ps_stack_t
*st
, /* I - Stack */
635 int c
) /* I - Number of objects to copy */
645 if ((n
= st
->num_objs
- c
) < 0)
650 if (!push_stack(st
, st
->objs
+ n
))
662 * 'delete_stack()' - Free memory used by a stack.
666 delete_stack(_cups_ps_stack_t
*st
) /* I - Stack */
674 * 'error_object()' - Add an object's value to the current error message.
678 error_object(_cups_ps_obj_t
*obj
) /* I - Object to add */
683 _cupsRasterAddError(" /%s", obj
->value
.name
);
686 case CUPS_PS_NUMBER
:
687 _cupsRasterAddError(" %g", obj
->value
.number
);
690 case CUPS_PS_STRING
:
691 _cupsRasterAddError(" (%s)", obj
->value
.string
);
694 case CUPS_PS_BOOLEAN
:
695 if (obj
->value
.boolean
)
696 _cupsRasterAddError(" true");
698 _cupsRasterAddError(" false");
702 _cupsRasterAddError(" null");
705 case CUPS_PS_START_ARRAY
:
706 _cupsRasterAddError(" [");
709 case CUPS_PS_END_ARRAY
:
710 _cupsRasterAddError(" ]");
713 case CUPS_PS_START_DICT
:
714 _cupsRasterAddError(" <<");
717 case CUPS_PS_END_DICT
:
718 _cupsRasterAddError(" >>");
721 case CUPS_PS_START_PROC
:
722 _cupsRasterAddError(" {");
725 case CUPS_PS_END_PROC
:
726 _cupsRasterAddError(" }");
730 _cupsRasterAddError(" --copy--");
733 case CUPS_PS_CLEARTOMARK
:
734 _cupsRasterAddError(" --cleartomark--");
738 _cupsRasterAddError(" --dup--");
742 _cupsRasterAddError(" --index--");
746 _cupsRasterAddError(" --pop--");
750 _cupsRasterAddError(" --roll--");
753 case CUPS_PS_SETPAGEDEVICE
:
754 _cupsRasterAddError(" --setpagedevice--");
757 case CUPS_PS_STOPPED
:
758 _cupsRasterAddError(" --stopped--");
762 _cupsRasterAddError(" --%s--", obj
->value
.other
);
769 * 'error_stack()' - Add a stack to the current error message...
773 error_stack(_cups_ps_stack_t
*st
, /* I - Stack */
774 const char *title
) /* I - Title string */
776 int c
; /* Looping var */
777 _cups_ps_obj_t
*obj
; /* Current object on stack */
780 _cupsRasterAddError(title
);
782 for (obj
= st
->objs
, c
= st
->num_objs
; c
> 0; c
--, obj
++)
785 _cupsRasterAddError("\n");
790 * 'index_stack()' - Copy the Nth value on the stack.
793 static _cups_ps_obj_t
* /* O - New object */
794 index_stack(_cups_ps_stack_t
*st
, /* I - Stack */
795 int n
) /* I - Object index */
797 if (n
< 0 || (n
= st
->num_objs
- n
- 1) < 0)
800 return (push_stack(st
, st
->objs
+ n
));
805 * 'new_stack()' - Create a new stack.
808 static _cups_ps_stack_t
* /* O - New stack */
811 _cups_ps_stack_t
*st
; /* New stack */
814 if ((st
= calloc(1, sizeof(_cups_ps_stack_t
))) == NULL
)
819 if ((st
->objs
= calloc(32, sizeof(_cups_ps_obj_t
))) == NULL
)
830 * 'pop_stock()' - Pop the top object off the stack.
833 static _cups_ps_obj_t
* /* O - Object */
834 pop_stack(_cups_ps_stack_t
*st
) /* I - Stack */
836 if (st
->num_objs
> 0)
840 return (st
->objs
+ st
->num_objs
);
848 * 'push_stack()' - Push an object on the stack.
851 static _cups_ps_obj_t
* /* O - New object */
852 push_stack(_cups_ps_stack_t
*st
, /* I - Stack */
853 _cups_ps_obj_t
*obj
) /* I - Object */
855 _cups_ps_obj_t
*temp
; /* New object */
858 if (st
->num_objs
>= st
->alloc_objs
)
862 st
->alloc_objs
+= 32;
864 if ((temp
= realloc(st
->objs
, st
->alloc_objs
*
865 sizeof(_cups_ps_obj_t
))) == NULL
)
869 memset(temp
+ st
->num_objs
, 0, 32 * sizeof(_cups_ps_obj_t
));
872 temp
= st
->objs
+ st
->num_objs
;
875 memcpy(temp
, obj
, sizeof(_cups_ps_obj_t
));
882 * 'roll_stack()' - Rotate stack objects.
885 static int /* O - 0 on success, -1 on error */
886 roll_stack(_cups_ps_stack_t
*st
, /* I - Stack */
887 int c
, /* I - Number of objects */
888 int s
) /* I - Amount to shift */
890 _cups_ps_obj_t
*temp
; /* Temporary array of objects */
891 int n
; /* Index into array */
894 DEBUG_printf((" roll_stack(st=%p, s=%d, c=%d)\n", st
, s
, c
));
897 * Range check input...
905 if ((n
= st
->num_objs
- c
) < 0)
914 * Copy N objects and move things around...
925 if ((temp
= calloc(s
, sizeof(_cups_ps_obj_t
))) == NULL
)
928 memcpy(temp
, st
->objs
+ n
, s
* sizeof(_cups_ps_obj_t
));
929 memmove(st
->objs
+ n
, st
->objs
+ n
+ s
, (c
- s
) * sizeof(_cups_ps_obj_t
));
930 memcpy(st
->objs
+ n
+ c
- s
, temp
, s
* sizeof(_cups_ps_obj_t
));
938 if ((temp
= calloc(s
, sizeof(_cups_ps_obj_t
))) == NULL
)
941 memcpy(temp
, st
->objs
+ n
+ c
- s
, s
* sizeof(_cups_ps_obj_t
));
942 memmove(st
->objs
+ n
+ s
, st
->objs
+ n
,
943 (c
- s
) * sizeof(_cups_ps_obj_t
));
944 memcpy(st
->objs
+ n
, temp
, s
* sizeof(_cups_ps_obj_t
));
954 * 'scan_ps()' - Scan a string for the next PS object.
957 static _cups_ps_obj_t
* /* O - New object or NULL on EOF */
958 scan_ps(_cups_ps_stack_t
*st
, /* I - Stack */
959 char **ptr
) /* IO - String pointer */
961 _cups_ps_obj_t obj
; /* Current object */
962 char *start
, /* Start of object */
963 *cur
, /* Current position */
964 *valptr
, /* Pointer into value string */
965 *valend
; /* End of value string */
966 int parens
; /* Parenthesis nesting level */
970 * Skip leading whitespace...
973 for (cur
= *ptr
; *cur
; cur
++)
978 * Comment, skip to end of line...
981 for (cur
++; *cur
&& *cur
!= '\n' && *cur
!= '\r'; cur
++);
983 else if (!isspace(*cur
& 255))
995 * See what we have...
998 memset(&obj
, 0, sizeof(obj
));
1002 case '(' : /* (string) */
1003 obj
.type
= CUPS_PS_STRING
;
1006 for (cur
++, parens
= 1, valptr
= obj
.value
.string
,
1007 valend
= obj
.value
.string
+ sizeof(obj
.value
.string
) - 1;
1011 if (*cur
== ')' && parens
== 1)
1016 else if (*cur
== ')')
1019 if (valptr
>= valend
)
1029 * Decode escaped character...
1036 else if (*cur
== 'f')
1038 else if (*cur
== 'n')
1040 else if (*cur
== 'r')
1042 else if (*cur
== 't')
1044 else if (*cur
>= '0' && *cur
<= '7')
1046 int ch
= *cur
- '0';
1048 if (cur
[1] >= '0' && cur
[1] <= '7')
1051 ch
= (ch
<< 3) + *cur
- '0';
1054 if (cur
[1] >= '0' && cur
[1] <= '7')
1057 ch
= (ch
<< 3) + *cur
- '0';
1062 else if (*cur
== '\r')
1067 else if (*cur
!= '\n')
1084 case '[' : /* Start array */
1085 obj
.type
= CUPS_PS_START_ARRAY
;
1089 case ']' : /* End array */
1090 obj
.type
= CUPS_PS_END_ARRAY
;
1094 case '<' : /* Start dictionary or hex string */
1097 obj
.type
= CUPS_PS_START_DICT
;
1102 obj
.type
= CUPS_PS_STRING
;
1105 for (cur
++, valptr
= obj
.value
.string
,
1106 valend
= obj
.value
.string
+ sizeof(obj
.value
.string
) - 1;
1110 int ch
; /* Current character */
1116 else if (valptr
>= valend
|| !isxdigit(*cur
& 255))
1122 if (*cur
>= '0' && *cur
<= '9')
1123 ch
= (*cur
- '0') << 4;
1125 ch
= (tolower(*cur
) - 'a' + 10) << 4;
1127 if (isxdigit(cur
[1] & 255))
1131 if (*cur
>= '0' && *cur
<= '9')
1134 ch
|= tolower(*cur
) - 'a' + 10;
1150 case '>' : /* End dictionary? */
1153 obj
.type
= CUPS_PS_END_DICT
;
1158 obj
.type
= CUPS_PS_OTHER
;
1159 obj
.value
.other
[0] = *cur
;
1165 case '{' : /* Start procedure */
1166 obj
.type
= CUPS_PS_START_PROC
;
1170 case '}' : /* End procedure */
1171 obj
.type
= CUPS_PS_END_PROC
;
1175 case '-' : /* Possible number */
1177 if (!isdigit(cur
[1] & 255) && cur
[1] != '.')
1179 obj
.type
= CUPS_PS_OTHER
;
1180 obj
.value
.other
[0] = *cur
;
1186 case '0' : /* Number */
1197 obj
.type
= CUPS_PS_NUMBER
;
1200 for (cur
++; *cur
; cur
++)
1201 if (!isdigit(*cur
& 255))
1207 * Integer with radix...
1210 obj
.value
.number
= strtol(cur
+ 1, &cur
, atoi(start
));
1213 else if (strchr(".Ee()<>[]{}/%", *cur
) || isspace(*cur
& 255))
1216 * Integer or real number...
1219 obj
.value
.number
= _cupsStrScand(start
, &cur
, localeconv());
1225 default : /* Operator/variable name */
1230 obj
.type
= CUPS_PS_NAME
;
1231 valptr
= obj
.value
.name
;
1232 valend
= obj
.value
.name
+ sizeof(obj
.value
.name
) - 1;
1237 obj
.type
= CUPS_PS_OTHER
;
1238 valptr
= obj
.value
.other
;
1239 valend
= obj
.value
.other
+ sizeof(obj
.value
.other
) - 1;
1244 if (strchr("()<>[]{}/%", *cur
) || isspace(*cur
& 255))
1246 else if (valptr
< valend
)
1255 if (obj
.type
== CUPS_PS_OTHER
)
1257 if (!strcmp(obj
.value
.other
, "true"))
1259 obj
.type
= CUPS_PS_BOOLEAN
;
1260 obj
.value
.boolean
= 1;
1262 else if (!strcmp(obj
.value
.other
, "false"))
1264 obj
.type
= CUPS_PS_BOOLEAN
;
1265 obj
.value
.boolean
= 0;
1267 else if (!strcmp(obj
.value
.other
, "null"))
1268 obj
.type
= CUPS_PS_NULL
;
1269 else if (!strcmp(obj
.value
.other
, "cleartomark"))
1270 obj
.type
= CUPS_PS_CLEARTOMARK
;
1271 else if (!strcmp(obj
.value
.other
, "copy"))
1272 obj
.type
= CUPS_PS_COPY
;
1273 else if (!strcmp(obj
.value
.other
, "dup"))
1274 obj
.type
= CUPS_PS_DUP
;
1275 else if (!strcmp(obj
.value
.other
, "index"))
1276 obj
.type
= CUPS_PS_INDEX
;
1277 else if (!strcmp(obj
.value
.other
, "pop"))
1278 obj
.type
= CUPS_PS_POP
;
1279 else if (!strcmp(obj
.value
.other
, "roll"))
1280 obj
.type
= CUPS_PS_ROLL
;
1281 else if (!strcmp(obj
.value
.other
, "setpagedevice"))
1282 obj
.type
= CUPS_PS_SETPAGEDEVICE
;
1283 else if (!strcmp(obj
.value
.other
, "stopped"))
1284 obj
.type
= CUPS_PS_STOPPED
;
1290 * Save the current position in the string and return the new object...
1295 return (push_stack(st
, &obj
));
1300 * 'setpagedevice()' - Simulate the PostScript setpagedevice operator.
1303 static int /* O - 0 on success, -1 on error */
1305 _cups_ps_stack_t
*st
, /* I - Stack */
1306 cups_page_header2_t
*h
, /* O - Page header */
1307 int *preferred_bits
)/* O - Preferred bits per color */
1309 int i
; /* Index into array */
1310 _cups_ps_obj_t
*obj
, /* Current object */
1311 *end
; /* End of dictionary */
1312 const char *name
; /* Attribute name */
1316 * Make sure we have a dictionary on the stack...
1319 if (st
->num_objs
== 0)
1322 obj
= end
= st
->objs
+ st
->num_objs
- 1;
1324 if (obj
->type
!= CUPS_PS_END_DICT
)
1329 while (obj
> st
->objs
)
1331 if (obj
->type
== CUPS_PS_START_DICT
)
1341 * Found the start of the dictionary, empty the stack to this point...
1344 st
->num_objs
= obj
- st
->objs
;
1347 * Now pull /name and value pairs from the dictionary...
1350 DEBUG_puts(" Dictionary:");
1352 for (obj
++; obj
< end
; obj
++)
1358 if (obj
->type
!= CUPS_PS_NAME
)
1361 name
= obj
->value
.name
;
1365 printf(" /%s ", name
);
1371 * Then grab the value...
1374 if (!strcmp(name
, "MediaClass") && obj
->type
== CUPS_PS_STRING
)
1375 strlcpy(h
->MediaClass
, obj
->value
.string
, sizeof(h
->MediaClass
));
1376 else if (!strcmp(name
, "MediaColor") && obj
->type
== CUPS_PS_STRING
)
1377 strlcpy(h
->MediaColor
, obj
->value
.string
, sizeof(h
->MediaColor
));
1378 else if (!strcmp(name
, "MediaType") && obj
->type
== CUPS_PS_STRING
)
1379 strlcpy(h
->MediaType
, obj
->value
.string
, sizeof(h
->MediaType
));
1380 else if (!strcmp(name
, "OutputType") && obj
->type
== CUPS_PS_STRING
)
1381 strlcpy(h
->OutputType
, obj
->value
.string
, sizeof(h
->OutputType
));
1382 else if (!strcmp(name
, "AdvanceDistance") && obj
->type
== CUPS_PS_NUMBER
)
1383 h
->AdvanceDistance
= (unsigned)obj
->value
.number
;
1384 else if (!strcmp(name
, "AdvanceMedia") && obj
->type
== CUPS_PS_NUMBER
)
1385 h
->AdvanceMedia
= (unsigned)obj
->value
.number
;
1386 else if (!strcmp(name
, "Collate") && obj
->type
== CUPS_PS_BOOLEAN
)
1387 h
->Collate
= (unsigned)obj
->value
.boolean
;
1388 else if (!strcmp(name
, "CutMedia") && obj
->type
== CUPS_PS_NUMBER
)
1389 h
->CutMedia
= (cups_cut_t
)(unsigned)obj
->value
.number
;
1390 else if (!strcmp(name
, "Duplex") && obj
->type
== CUPS_PS_BOOLEAN
)
1391 h
->Duplex
= (unsigned)obj
->value
.boolean
;
1392 else if (!strcmp(name
, "HWResolution") && obj
->type
== CUPS_PS_START_ARRAY
)
1394 if (obj
[1].type
== CUPS_PS_NUMBER
&& obj
[2].type
== CUPS_PS_NUMBER
&&
1395 obj
[3].type
== CUPS_PS_END_ARRAY
)
1397 h
->HWResolution
[0] = (unsigned)obj
[1].value
.number
;
1398 h
->HWResolution
[1] = (unsigned)obj
[2].value
.number
;
1404 else if (!strcmp(name
, "InsertSheet") && obj
->type
== CUPS_PS_BOOLEAN
)
1405 h
->InsertSheet
= (unsigned)obj
->value
.boolean
;
1406 else if (!strcmp(name
, "Jog") && obj
->type
== CUPS_PS_NUMBER
)
1407 h
->Jog
= (unsigned)obj
->value
.number
;
1408 else if (!strcmp(name
, "LeadingEdge") && obj
->type
== CUPS_PS_NUMBER
)
1409 h
->LeadingEdge
= (unsigned)obj
->value
.number
;
1410 else if (!strcmp(name
, "ManualFeed") && obj
->type
== CUPS_PS_BOOLEAN
)
1411 h
->ManualFeed
= (unsigned)obj
->value
.boolean
;
1412 else if ((!strcmp(name
, "cupsMediaPosition") ||
1413 !strcmp(name
, "MediaPosition")) && obj
->type
== CUPS_PS_NUMBER
)
1416 * cupsMediaPosition is supported for backwards compatibility only.
1417 * We added it back in the Ghostscript 5.50 days to work around a
1418 * bug in Ghostscript WRT handling of MediaPosition and setpagedevice.
1420 * All new development should set MediaPosition...
1423 h
->MediaPosition
= (unsigned)obj
->value
.number
;
1425 else if (!strcmp(name
, "MediaWeight") && obj
->type
== CUPS_PS_NUMBER
)
1426 h
->MediaWeight
= (unsigned)obj
->value
.number
;
1427 else if (!strcmp(name
, "MirrorPrint") && obj
->type
== CUPS_PS_BOOLEAN
)
1428 h
->MirrorPrint
= (unsigned)obj
->value
.boolean
;
1429 else if (!strcmp(name
, "NegativePrint") && obj
->type
== CUPS_PS_BOOLEAN
)
1430 h
->NegativePrint
= (unsigned)obj
->value
.boolean
;
1431 else if (!strcmp(name
, "NumCopies") && obj
->type
== CUPS_PS_NUMBER
)
1432 h
->NumCopies
= (unsigned)obj
->value
.number
;
1433 else if (!strcmp(name
, "Orientation") && obj
->type
== CUPS_PS_NUMBER
)
1434 h
->Orientation
= (unsigned)obj
->value
.number
;
1435 else if (!strcmp(name
, "OutputFaceUp") && obj
->type
== CUPS_PS_BOOLEAN
)
1436 h
->OutputFaceUp
= (unsigned)obj
->value
.boolean
;
1437 else if (!strcmp(name
, "PageSize") && obj
->type
== CUPS_PS_START_ARRAY
)
1439 if (obj
[1].type
== CUPS_PS_NUMBER
&& obj
[2].type
== CUPS_PS_NUMBER
&&
1440 obj
[3].type
== CUPS_PS_END_ARRAY
)
1442 h
->cupsPageSize
[0] = obj
[1].value
.number
;
1443 h
->cupsPageSize
[1] = obj
[2].value
.number
;
1445 h
->PageSize
[0] = (unsigned)obj
[1].value
.number
;
1446 h
->PageSize
[1] = (unsigned)obj
[2].value
.number
;
1453 else if (!strcmp(name
, "Separations") && obj
->type
== CUPS_PS_BOOLEAN
)
1454 h
->Separations
= (unsigned)obj
->value
.boolean
;
1455 else if (!strcmp(name
, "TraySwitch") && obj
->type
== CUPS_PS_BOOLEAN
)
1456 h
->TraySwitch
= (unsigned)obj
->value
.boolean
;
1457 else if (!strcmp(name
, "Tumble") && obj
->type
== CUPS_PS_BOOLEAN
)
1458 h
->Tumble
= (unsigned)obj
->value
.boolean
;
1459 else if (!strcmp(name
, "cupsMediaType") && obj
->type
== CUPS_PS_NUMBER
)
1460 h
->cupsMediaType
= (unsigned)obj
->value
.number
;
1461 else if (!strcmp(name
, "cupsBitsPerColor") && obj
->type
== CUPS_PS_NUMBER
)
1462 h
->cupsBitsPerColor
= (unsigned)obj
->value
.number
;
1463 else if (!strcmp(name
, "cupsPreferredBitsPerColor") &&
1464 obj
->type
== CUPS_PS_NUMBER
)
1465 *preferred_bits
= (int)obj
->value
.number
;
1466 else if (!strcmp(name
, "cupsColorOrder") && obj
->type
== CUPS_PS_NUMBER
)
1467 h
->cupsColorOrder
= (cups_order_t
)(unsigned)obj
->value
.number
;
1468 else if (!strcmp(name
, "cupsColorSpace") && obj
->type
== CUPS_PS_NUMBER
)
1469 h
->cupsColorSpace
= (cups_cspace_t
)(unsigned)obj
->value
.number
;
1470 else if (!strcmp(name
, "cupsCompression") && obj
->type
== CUPS_PS_NUMBER
)
1471 h
->cupsCompression
= (unsigned)obj
->value
.number
;
1472 else if (!strcmp(name
, "cupsRowCount") && obj
->type
== CUPS_PS_NUMBER
)
1473 h
->cupsRowCount
= (unsigned)obj
->value
.number
;
1474 else if (!strcmp(name
, "cupsRowFeed") && obj
->type
== CUPS_PS_NUMBER
)
1475 h
->cupsRowFeed
= (unsigned)obj
->value
.number
;
1476 else if (!strcmp(name
, "cupsRowStep") && obj
->type
== CUPS_PS_NUMBER
)
1477 h
->cupsRowStep
= (unsigned)obj
->value
.number
;
1478 else if (!strcmp(name
, "cupsBorderlessScalingFactor") &&
1479 obj
->type
== CUPS_PS_NUMBER
)
1480 h
->cupsBorderlessScalingFactor
= obj
->value
.number
;
1481 else if (!strncmp(name
, "cupsInteger", 11) && obj
->type
== CUPS_PS_NUMBER
)
1483 if ((i
= atoi(name
+ 11)) < 0 || i
> 15)
1486 h
->cupsInteger
[i
] = (unsigned)obj
->value
.number
;
1488 else if (!strncmp(name
, "cupsReal", 8) && obj
->type
== CUPS_PS_NUMBER
)
1490 if ((i
= atoi(name
+ 8)) < 0 || i
> 15)
1493 h
->cupsReal
[i
] = obj
->value
.number
;
1495 else if (!strncmp(name
, "cupsString", 10) && obj
->type
== CUPS_PS_STRING
)
1497 if ((i
= atoi(name
+ 10)) < 0 || i
> 15)
1500 strlcpy(h
->cupsString
[i
], obj
->value
.string
, sizeof(h
->cupsString
[i
]));
1502 else if (!strcmp(name
, "cupsMarkerType") && obj
->type
== CUPS_PS_STRING
)
1503 strlcpy(h
->cupsMarkerType
, obj
->value
.string
, sizeof(h
->cupsMarkerType
));
1504 else if (!strcmp(name
, "cupsPageSizeName") && obj
->type
== CUPS_PS_STRING
)
1505 strlcpy(h
->cupsPageSizeName
, obj
->value
.string
,
1506 sizeof(h
->cupsPageSizeName
));
1507 else if (!strcmp(name
, "cupsRenderingIntent") &&
1508 obj
->type
== CUPS_PS_STRING
)
1509 strlcpy(h
->cupsRenderingIntent
, obj
->value
.string
,
1510 sizeof(h
->cupsRenderingIntent
));
1514 * Ignore unknown name+value...
1517 DEBUG_printf((" Unknown name (\"%s\") or value...\n", name
));
1519 while (obj
[1].type
!= CUPS_PS_NAME
&& obj
< end
)
1530 * 'DEBUG_object()' - Print an object's value...
1534 DEBUG_object(_cups_ps_obj_t
*obj
) /* I - Object to print */
1539 printf("/%s", obj
->value
.name
);
1542 case CUPS_PS_NUMBER
:
1543 printf("%g", obj
->value
.number
);
1546 case CUPS_PS_STRING
:
1547 printf("(%s)", obj
->value
.string
);
1550 case CUPS_PS_BOOLEAN
:
1551 if (obj
->value
.boolean
)
1552 fputs("true", stdout
);
1554 fputs("false", stdout
);
1558 fputs("null", stdout
);
1561 case CUPS_PS_START_ARRAY
:
1565 case CUPS_PS_END_ARRAY
:
1569 case CUPS_PS_START_DICT
:
1570 fputs("<<", stdout
);
1573 case CUPS_PS_END_DICT
:
1574 fputs(">>", stdout
);
1577 case CUPS_PS_START_PROC
:
1581 case CUPS_PS_END_PROC
:
1585 case CUPS_PS_CLEARTOMARK
:
1586 fputs("--cleartomark--", stdout
);
1590 fputs("--copy--", stdout
);
1594 fputs("--dup--", stdout
);
1597 case CUPS_PS_INDEX
:
1598 fputs("--index--", stdout
);
1602 fputs("--pop--", stdout
);
1606 fputs("--roll--", stdout
);
1609 case CUPS_PS_SETPAGEDEVICE
:
1610 fputs("--setpagedevice--", stdout
);
1613 case CUPS_PS_STOPPED
:
1614 fputs("--stopped--", stdout
);
1617 case CUPS_PS_OTHER
:
1618 printf("--%s--", obj
->value
.other
);
1625 * 'DEBUG_stack()' - Print a stack...
1629 DEBUG_stack(_cups_ps_stack_t
*st
) /* I - Stack */
1631 int c
; /* Looping var */
1632 _cups_ps_obj_t
*obj
; /* Current object on stack */
1635 for (obj
= st
->objs
, c
= st
->num_objs
; c
> 0; c
--, obj
++)
1647 * End of "$Id: interpret.c 6649 2007-07-11 21:46:42Z mike $".