2 * "$Id: interpret.c 6281 2007-02-14 16:32:42Z mike $"
4 * PPD command interpreter for the Common UNIX Printing System (CUPS).
6 * Copyright 1993-2007 by Easy Software Products.
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 * This file is subject to the Apple OS-Developed Software exception.
28 * cupsRasterInterpretPPD() - Interpret PPD commands to create a page header.
29 * _cupsRasterExecPS() - Execute PostScript code to initialize a page
31 * cleartomark_stack() - Clear to the last mark ([) on the stack.
32 * copy_stack() - Copy the top N stack objects.
33 * delete_stack() - Free memory used by a stack.
34 * error_object() - Add an object's value to the current error
36 * error_stack() - Add a stack to the current error message.
37 * index_stack() - Copy the Nth value on the stack.
38 * new_stack() - Create a new stack.
39 * pop_stock() - Pop the top object off the stack.
40 * push_stack() - Push an object on the stack.
41 * roll_stack() - Rotate stack objects.
42 * scan_ps() - Scan a string for the next PS object.
43 * setpagedevice() - Simulate the PostScript setpagedevice operator.
44 * DEBUG_object() - Print an object value.
45 * DEBUG_stack() - Print a stack.
49 * Include necessary headers...
52 #include <cups/string.h>
53 #include "image-private.h"
58 * Stack values for the PostScript mini-interpreter...
80 CUPS_PS_SETPAGEDEVICE
,
87 _cups_ps_type_t type
; /* Object type */
90 int boolean
; /* Boolean value */
91 char name
[64]; /* Name value */
92 double number
; /* Number value */
93 char other
[64]; /* Other operator */
94 char string
[64]; /* Sring value */
100 int num_objs
, /* Number of objects on stack */
101 alloc_objs
; /* Number of allocated objects */
102 _cups_ps_obj_t
*objs
; /* Objects in stack */
110 static int cleartomark_stack(_cups_ps_stack_t
*st
);
111 static int copy_stack(_cups_ps_stack_t
*st
, int count
);
112 static void delete_stack(_cups_ps_stack_t
*st
);
113 static void error_object(_cups_ps_obj_t
*obj
);
114 static void error_stack(_cups_ps_stack_t
*st
, const char *title
);
115 static _cups_ps_obj_t
*index_stack(_cups_ps_stack_t
*st
, int n
);
116 static _cups_ps_stack_t
*new_stack(void);
117 static _cups_ps_obj_t
*pop_stack(_cups_ps_stack_t
*st
);
118 static _cups_ps_obj_t
*push_stack(_cups_ps_stack_t
*st
,
119 _cups_ps_obj_t
*obj
);
120 static int roll_stack(_cups_ps_stack_t
*st
, int c
, int s
);
121 static _cups_ps_obj_t
*scan_ps(_cups_ps_stack_t
*st
, char **ptr
);
122 static int setpagedevice(_cups_ps_stack_t
*st
,
123 cups_page_header2_t
*h
,
124 int *preferred_bits
);
126 static void DEBUG_object(_cups_ps_obj_t
*obj
);
127 static void DEBUG_stack(_cups_ps_stack_t
*st
);
132 * 'cupsRasterInterpretPPD()' - Interpret PPD commands to create a page header.
134 * This function does not mark the options in the PPD using the "num_options"
135 * and "options" arguments. Instead, mark the options prior to calling
136 * cupsRasterInterpretPPD() - this allows you to do per-page options
137 * without manipulating the options array.
139 * The "func" argument specifies an optional callback function that is
140 * called prior to the computation of the final raster data. The function
141 * can make changes to the cups_page_header2_t data as needed to use a
142 * supported raster format and then returns 0 on success and -1 if the
143 * requested attributes cannot be supported.
145 * cupsRasterInterpretPPD() supports a subset of the PostScript language.
146 * Currently only the [, ], <<, >>, {, }, cleartomark, copy, dup, index,
147 * pop, roll, setpagedevice, and stopped operators are supported.
152 int /* O - 0 on success, -1 on failure */
153 cupsRasterInterpretPPD(
154 cups_page_header2_t
*h
, /* O - Page header */
155 ppd_file_t
*ppd
, /* I - PPD file */
156 int num_options
, /* I - Number of options */
157 cups_option_t
*options
, /* I - Options */
158 cups_interpret_cb_t func
) /* I - Optional page header callback */
160 int status
; /* Cummulative status */
161 char *code
; /* Code to run */
162 const char *val
; /* Option value */
163 ppd_size_t
*size
; /* Current size */
164 float left
, /* Left position */
165 bottom
, /* Bottom position */
166 right
, /* Right position */
167 top
; /* Top position */
168 int preferred_bits
; /* Preferred bits per color */
172 * Range check input...
175 _cupsRasterClearError();
179 _cupsRasterAddError("Page header cannot be NULL!\n");
184 * Reset the page header to the defaults...
187 memset(h
, 0, sizeof(cups_page_header2_t
));
190 h
->PageSize
[0] = 612;
191 h
->PageSize
[1] = 792;
192 h
->HWResolution
[0] = 100;
193 h
->HWResolution
[1] = 100;
194 h
->cupsBitsPerColor
= 1;
195 h
->cupsColorOrder
= CUPS_ORDER_CHUNKED
;
196 h
->cupsColorSpace
= CUPS_CSPACE_K
;
197 h
->cupsBorderlessScalingFactor
= 1.0f
;
198 h
->cupsPageSize
[0] = 612.0f
;
199 h
->cupsPageSize
[1] = 792.0f
;
200 h
->cupsImagingBBox
[0] = 0.0f
;
201 h
->cupsImagingBBox
[1] = 0.0f
;
202 h
->cupsImagingBBox
[2] = 612.0f
;
203 h
->cupsImagingBBox
[3] = 792.0f
;
205 strcpy(h
->cupsPageSizeName
, "Letter");
208 * Apply patches and options to the page header...
217 * Apply any patch code (used to override the defaults...)
221 status
|= _cupsRasterExecPS(h
, &preferred_bits
, ppd
->patches
);
224 * Then apply printer options in the proper order...
227 if ((code
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, 0.0)) != NULL
)
229 status
|= _cupsRasterExecPS(h
, &preferred_bits
, code
);
233 if ((code
= ppdEmitString(ppd
, PPD_ORDER_ANY
, 0.0)) != NULL
)
235 status
|= _cupsRasterExecPS(h
, &preferred_bits
, code
);
239 if ((code
= ppdEmitString(ppd
, PPD_ORDER_PROLOG
, 0.0)) != NULL
)
241 status
|= _cupsRasterExecPS(h
, &preferred_bits
, code
);
245 if ((code
= ppdEmitString(ppd
, PPD_ORDER_PAGE
, 0.0)) != NULL
)
247 status
|= _cupsRasterExecPS(h
, &preferred_bits
, code
);
253 * Allow option override for page scaling...
256 if ((val
= cupsGetOption("cupsBorderlessScalingFactor", num_options
,
259 float sc
= atof(val
);
261 if (sc
>= 0.9 && sc
<= 1.1)
262 h
->cupsBorderlessScalingFactor
= sc
;
266 * Get the margins for the current size...
269 if ((size
= ppdPageSize(ppd
, NULL
)) != NULL
)
272 * Use the margins from the PPD file...
276 bottom
= size
->bottom
;
280 strlcpy(h
->cupsPageSizeName
, size
->name
, sizeof(h
->cupsPageSizeName
));
282 h
->cupsPageSize
[0] = size
->width
;
283 h
->cupsPageSize
[1] = size
->length
;
288 * Use the default margins...
297 h
->PageSize
[0] = h
->cupsPageSize
[0] *
298 h
->cupsBorderlessScalingFactor
;
299 h
->PageSize
[1] = h
->cupsPageSize
[1] *
300 h
->cupsBorderlessScalingFactor
;
301 h
->Margins
[0] = left
* h
->cupsBorderlessScalingFactor
;
302 h
->Margins
[1] = bottom
* h
->cupsBorderlessScalingFactor
;
303 h
->ImagingBoundingBox
[0] = left
* h
->cupsBorderlessScalingFactor
;
304 h
->ImagingBoundingBox
[1] = bottom
* h
->cupsBorderlessScalingFactor
;
305 h
->ImagingBoundingBox
[2] = right
* h
->cupsBorderlessScalingFactor
;
306 h
->ImagingBoundingBox
[3] = top
* h
->cupsBorderlessScalingFactor
;
307 h
->cupsImagingBBox
[0] = left
;
308 h
->cupsImagingBBox
[1] = bottom
;
309 h
->cupsImagingBBox
[2] = right
;
310 h
->cupsImagingBBox
[3] = top
;
313 * Use the callback to validate the page header...
316 if (func
&& (*func
)(h
, preferred_bits
))
318 _cupsRasterAddError("Page header callback returned error.\n");
323 * Check parameters...
326 if (!h
->HWResolution
[0] || !h
->HWResolution
[1] ||
327 !h
->PageSize
[0] || !h
->PageSize
[1] ||
328 (h
->cupsBitsPerColor
!= 1 && h
->cupsBitsPerColor
!= 2 &&
329 h
->cupsBitsPerColor
!= 4 && h
->cupsBitsPerColor
!= 8 &&
330 h
->cupsBitsPerColor
!= 16) ||
331 h
->cupsBorderlessScalingFactor
< 0.5 ||
332 h
->cupsBorderlessScalingFactor
> 2.0)
334 _cupsRasterAddError("Page header uses unsupported values.\n");
339 * Compute the bitmap parameters...
342 h
->cupsWidth
= (int)((right
- left
) * h
->cupsBorderlessScalingFactor
*
343 h
->HWResolution
[0] / 72.0f
+ 0.5f
);
344 h
->cupsHeight
= (int)((top
- bottom
) * h
->cupsBorderlessScalingFactor
*
345 h
->HWResolution
[1] / 72.0f
+ 0.5f
);
347 switch (h
->cupsColorSpace
)
351 case CUPS_CSPACE_WHITE
:
352 case CUPS_CSPACE_GOLD
:
353 case CUPS_CSPACE_SILVER
:
354 h
->cupsNumColors
= 1;
355 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
;
360 * Ensure that colorimetric colorspaces use at least 8 bits per
364 if (h
->cupsColorSpace
>= CUPS_CSPACE_CIEXYZ
&&
365 h
->cupsBitsPerColor
< 8)
366 h
->cupsBitsPerColor
= 8;
369 * Figure out the number of bits per pixel...
372 if (h
->cupsColorOrder
== CUPS_ORDER_CHUNKED
)
374 if (h
->cupsBitsPerColor
>= 8)
375 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
* 3;
377 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
* 4;
380 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
;
382 h
->cupsNumColors
= 3;
385 case CUPS_CSPACE_KCMYcm
:
386 if (h
->cupsBitsPerColor
== 1)
388 if (h
->cupsColorOrder
== CUPS_ORDER_CHUNKED
)
389 h
->cupsBitsPerPixel
= 8;
391 h
->cupsBitsPerPixel
= 1;
393 h
->cupsNumColors
= 6;
398 * Fall through to CMYK code...
401 case CUPS_CSPACE_RGBA
:
402 case CUPS_CSPACE_RGBW
:
403 case CUPS_CSPACE_CMYK
:
404 case CUPS_CSPACE_YMCK
:
405 case CUPS_CSPACE_KCMY
:
406 case CUPS_CSPACE_GMCK
:
407 case CUPS_CSPACE_GMCS
:
408 if (h
->cupsColorOrder
== CUPS_ORDER_CHUNKED
)
409 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
* 4;
411 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
;
413 h
->cupsNumColors
= 4;
417 h
->cupsBytesPerLine
= (h
->cupsBitsPerPixel
* h
->cupsWidth
+ 7) / 8;
419 if (h
->cupsColorOrder
== CUPS_ORDER_BANDED
)
420 h
->cupsBytesPerLine
*= h
->cupsNumColors
;
427 * '_cupsRasterExecPS()' - Execute PostScript code to initialize a page header.
430 int /* O - 0 on success, -1 on error */
432 cups_page_header2_t
*h
, /* O - Page header */
433 int *preferred_bits
,/* O - Preferred bits per color */
434 const char *code
) /* I - PS code to execute */
436 _cups_ps_stack_t
*st
; /* PostScript value stack */
437 _cups_ps_obj_t
*obj
; /* Object from top of stack */
438 char *codecopy
, /* Copy of code */
439 *codeptr
; /* Pointer into copy of code */
442 DEBUG_printf(("_cupsRasterExecPS(h=%p, preferred_bits=%p, code=\"%s\")\n",
443 h
, preferred_bits
, code
? code
: "(null)"));
446 * Copy the PostScript code and create a stack...
449 if ((codecopy
= strdup(code
)) == NULL
)
451 _cupsRasterAddError("Unable to duplicate code string.\n");
455 if ((st
= new_stack()) == NULL
)
457 _cupsRasterAddError("Unable to create stack.\n");
463 * Parse the PS string until we run out of data...
468 while ((obj
= scan_ps(st
, &codeptr
)) != NULL
)
471 printf(" (%d): ", st
->num_objs
);
479 /* Do nothing for regular values */
482 case CUPS_PS_CLEARTOMARK
:
485 if (cleartomark_stack(st
))
486 _cupsRasterAddError("cleartomark: Stack underflow!\n");
489 fputs(" dup: ", stdout
);
496 if ((obj
= pop_stack(st
)) != NULL
)
498 copy_stack(st
, (int)obj
->value
.number
);
501 fputs(" copy: ", stdout
);
512 fputs(" dup: ", stdout
);
519 if ((obj
= pop_stack(st
)) != NULL
)
521 index_stack(st
, (int)obj
->value
.number
);
524 fputs(" index: ", stdout
);
535 fputs(" pop: ", stdout
);
542 if ((obj
= pop_stack(st
)) != NULL
)
547 c
= (int)obj
->value
.number
;
549 if ((obj
= pop_stack(st
)) != NULL
)
551 roll_stack(st
, (int)obj
->value
.number
, c
);
554 fputs(" roll:", stdout
);
561 case CUPS_PS_SETPAGEDEVICE
:
563 setpagedevice(st
, h
, preferred_bits
);
566 fputs(" setpagedevice: ", stdout
);
571 case CUPS_PS_START_PROC
:
572 case CUPS_PS_END_PROC
:
573 case CUPS_PS_STOPPED
:
578 _cupsRasterAddError("Unknown operator \"%s\"!\n", obj
->value
.other
);
579 DEBUG_printf((" Unknown operator \"%s\"!\n", obj
->value
.other
));
583 if (obj
->type
== CUPS_PS_OTHER
)
593 if (st
->num_objs
> 0)
595 error_stack(st
, "Stack not empty:");
598 fputs(" Stack not empty:", stdout
);
618 * 'cleartomark_stack()' - Clear to the last mark ([) on the stack.
621 static int /* O - 0 on success, -1 on error */
622 cleartomark_stack(_cups_ps_stack_t
*st
) /* I - Stack */
624 _cups_ps_obj_t
*obj
; /* Current object on stack */
627 while ((obj
= pop_stack(st
)) != NULL
)
628 if (obj
->type
== CUPS_PS_START_ARRAY
)
631 return (obj
? 0 : -1);
636 * 'copy_stack()' - Copy the top N stack objects.
639 static int /* O - 0 on success, -1 on error */
640 copy_stack(_cups_ps_stack_t
*st
, /* I - Stack */
641 int c
) /* I - Number of objects to copy */
651 if ((n
= st
->num_objs
- c
) < 0)
656 if (!push_stack(st
, st
->objs
+ n
))
668 * 'delete_stack()' - Free memory used by a stack.
672 delete_stack(_cups_ps_stack_t
*st
) /* I - Stack */
680 * 'error_object()' - Add an object's value to the current error message.
684 error_object(_cups_ps_obj_t
*obj
) /* I - Object to add */
689 _cupsRasterAddError(" /%s", obj
->value
.name
);
692 case CUPS_PS_NUMBER
:
693 _cupsRasterAddError(" %g", obj
->value
.number
);
696 case CUPS_PS_STRING
:
697 _cupsRasterAddError(" (%s)", obj
->value
.string
);
700 case CUPS_PS_BOOLEAN
:
701 if (obj
->value
.boolean
)
702 _cupsRasterAddError(" true");
704 _cupsRasterAddError(" false");
708 _cupsRasterAddError(" null");
711 case CUPS_PS_START_ARRAY
:
712 _cupsRasterAddError(" [");
715 case CUPS_PS_END_ARRAY
:
716 _cupsRasterAddError(" ]");
719 case CUPS_PS_START_DICT
:
720 _cupsRasterAddError(" <<");
723 case CUPS_PS_END_DICT
:
724 _cupsRasterAddError(" >>");
727 case CUPS_PS_START_PROC
:
728 _cupsRasterAddError(" {");
731 case CUPS_PS_END_PROC
:
732 _cupsRasterAddError(" }");
736 _cupsRasterAddError(" --copy--");
739 case CUPS_PS_CLEARTOMARK
:
740 _cupsRasterAddError(" --cleartomark--");
744 _cupsRasterAddError(" --dup--");
748 _cupsRasterAddError(" --index--");
752 _cupsRasterAddError(" --pop--");
756 _cupsRasterAddError(" --roll--");
759 case CUPS_PS_SETPAGEDEVICE
:
760 _cupsRasterAddError(" --setpagedevice--");
763 case CUPS_PS_STOPPED
:
764 _cupsRasterAddError(" --stopped--");
768 _cupsRasterAddError(" --%s--", obj
->value
.other
);
775 * 'error_stack()' - Add a stack to the current error message...
779 error_stack(_cups_ps_stack_t
*st
, /* I - Stack */
780 const char *title
) /* I - Title string */
782 int c
; /* Looping var */
783 _cups_ps_obj_t
*obj
; /* Current object on stack */
786 _cupsRasterAddError(title
);
788 for (obj
= st
->objs
, c
= st
->num_objs
; c
> 0; c
--, obj
++)
791 _cupsRasterAddError("\n");
796 * 'index_stack()' - Copy the Nth value on the stack.
799 static _cups_ps_obj_t
* /* O - New object */
800 index_stack(_cups_ps_stack_t
*st
, /* I - Stack */
801 int n
) /* I - Object index */
803 if (n
< 0 || (n
= st
->num_objs
- n
- 1) < 0)
806 return (push_stack(st
, st
->objs
+ n
));
811 * 'new_stack()' - Create a new stack.
814 static _cups_ps_stack_t
* /* O - New stack */
817 _cups_ps_stack_t
*st
; /* New stack */
820 if ((st
= calloc(1, sizeof(_cups_ps_stack_t
))) == NULL
)
825 if ((st
->objs
= calloc(32, sizeof(_cups_ps_obj_t
))) == NULL
)
836 * 'pop_stock()' - Pop the top object off the stack.
839 static _cups_ps_obj_t
* /* O - Object */
840 pop_stack(_cups_ps_stack_t
*st
) /* I - Stack */
842 if (st
->num_objs
> 0)
846 return (st
->objs
+ st
->num_objs
);
854 * 'push_stack()' - Push an object on the stack.
857 static _cups_ps_obj_t
* /* O - New object */
858 push_stack(_cups_ps_stack_t
*st
, /* I - Stack */
859 _cups_ps_obj_t
*obj
) /* I - Object */
861 _cups_ps_obj_t
*temp
; /* New object */
864 if (st
->num_objs
>= st
->alloc_objs
)
868 st
->alloc_objs
+= 32;
870 if ((temp
= realloc(st
->objs
, st
->alloc_objs
*
871 sizeof(_cups_ps_obj_t
))) == NULL
)
875 memset(temp
+ st
->num_objs
, 0, 32 * sizeof(_cups_ps_obj_t
));
878 temp
= st
->objs
+ st
->num_objs
;
881 memcpy(temp
, obj
, sizeof(_cups_ps_obj_t
));
888 * 'roll_stack()' - Rotate stack objects.
891 static int /* O - 0 on success, -1 on error */
892 roll_stack(_cups_ps_stack_t
*st
, /* I - Stack */
893 int c
, /* I - Number of objects */
894 int s
) /* I - Amount to shift */
896 _cups_ps_obj_t
*temp
; /* Temporary array of objects */
897 int n
; /* Index into array */
900 DEBUG_printf((" roll_stack(st=%p, s=%d, c=%d)\n", st
, s
, c
));
903 * Range check input...
911 if ((n
= st
->num_objs
- c
) < 0)
920 * Copy N objects and move things around...
931 if ((temp
= calloc(s
, sizeof(_cups_ps_obj_t
))) == NULL
)
934 memcpy(temp
, st
->objs
+ n
, s
* sizeof(_cups_ps_obj_t
));
935 memmove(st
->objs
+ n
, st
->objs
+ n
+ s
, (c
- s
) * sizeof(_cups_ps_obj_t
));
936 memcpy(st
->objs
+ n
+ c
- s
, temp
, s
* sizeof(_cups_ps_obj_t
));
944 if ((temp
= calloc(s
, sizeof(_cups_ps_obj_t
))) == NULL
)
947 memcpy(temp
, st
->objs
+ n
+ c
- s
, s
* sizeof(_cups_ps_obj_t
));
948 memmove(st
->objs
+ n
+ s
, st
->objs
+ n
,
949 (c
- s
) * sizeof(_cups_ps_obj_t
));
950 memcpy(st
->objs
+ n
, temp
, s
* sizeof(_cups_ps_obj_t
));
960 * 'scan_ps()' - Scan a string for the next PS object.
963 static _cups_ps_obj_t
* /* O - New object or NULL on EOF */
964 scan_ps(_cups_ps_stack_t
*st
, /* I - Stack */
965 char **ptr
) /* IO - String pointer */
967 _cups_ps_obj_t obj
; /* Current object */
968 char *start
, /* Start of object */
969 *cur
, /* Current position */
970 *valptr
, /* Pointer into value string */
971 *valend
; /* End of value string */
972 int parens
; /* Parenthesis nesting level */
976 * Skip leading whitespace...
979 for (cur
= *ptr
; *cur
; cur
++)
984 * Comment, skip to end of line...
987 for (cur
++; *cur
&& *cur
!= '\n' && *cur
!= '\r'; cur
++);
989 else if (!isspace(*cur
& 255))
1001 * See what we have...
1004 memset(&obj
, 0, sizeof(obj
));
1008 case '(' : /* (string) */
1009 obj
.type
= CUPS_PS_STRING
;
1012 for (cur
++, parens
= 1, valptr
= obj
.value
.string
,
1013 valend
= obj
.value
.string
+ sizeof(obj
.value
.string
) - 1;
1017 if (*cur
== ')' && parens
== 1)
1022 else if (*cur
== ')')
1025 if (valptr
>= valend
)
1035 * Decode escaped character...
1042 else if (*cur
== 'f')
1044 else if (*cur
== 'n')
1046 else if (*cur
== 'r')
1048 else if (*cur
== 't')
1050 else if (*cur
>= '0' && *cur
<= '7')
1052 int ch
= *cur
- '0';
1054 if (cur
[1] >= '0' && cur
[1] <= '7')
1057 ch
= (ch
<< 3) + *cur
- '0';
1060 if (cur
[1] >= '0' && cur
[1] <= '7')
1063 ch
= (ch
<< 3) + *cur
- '0';
1068 else if (*cur
== '\r')
1073 else if (*cur
!= '\n')
1090 case '[' : /* Start array */
1091 obj
.type
= CUPS_PS_START_ARRAY
;
1095 case ']' : /* End array */
1096 obj
.type
= CUPS_PS_END_ARRAY
;
1100 case '<' : /* Start dictionary or hex string */
1103 obj
.type
= CUPS_PS_START_DICT
;
1108 obj
.type
= CUPS_PS_STRING
;
1111 for (cur
++, valptr
= obj
.value
.string
,
1112 valend
= obj
.value
.string
+ sizeof(obj
.value
.string
) - 1;
1116 int ch
; /* Current character */
1122 else if (valptr
>= valend
|| !isxdigit(*cur
& 255))
1128 if (*cur
>= '0' && *cur
<= '9')
1129 ch
= (*cur
- '0') << 4;
1131 ch
= (tolower(*cur
) - 'a' + 10) << 4;
1133 if (isxdigit(cur
[1] & 255))
1137 if (*cur
>= '0' && *cur
<= '9')
1140 ch
|= tolower(*cur
) - 'a' + 10;
1156 case '>' : /* End dictionary? */
1159 obj
.type
= CUPS_PS_END_DICT
;
1164 obj
.type
= CUPS_PS_OTHER
;
1165 obj
.value
.other
[0] = *cur
;
1171 case '{' : /* Start procedure */
1172 obj
.type
= CUPS_PS_START_PROC
;
1176 case '}' : /* End procedure */
1177 obj
.type
= CUPS_PS_END_PROC
;
1181 case '-' : /* Possible number */
1183 if (!isdigit(cur
[1] & 255) && cur
[1] != '.')
1185 obj
.type
= CUPS_PS_OTHER
;
1186 obj
.value
.other
[0] = *cur
;
1192 case '0' : /* Number */
1203 obj
.type
= CUPS_PS_NUMBER
;
1206 for (cur
++; *cur
; cur
++)
1207 if (!isdigit(*cur
& 255))
1213 * Integer with radix...
1216 obj
.value
.number
= strtol(cur
+ 1, &cur
, atoi(start
));
1219 else if (strchr(".Ee()<>[]{}/%", *cur
) || isspace(*cur
& 255))
1222 * Integer or real number...
1225 obj
.value
.number
= _cupsStrScand(start
, &cur
, localeconv());
1231 default : /* Operator/variable name */
1236 obj
.type
= CUPS_PS_NAME
;
1237 valptr
= obj
.value
.name
;
1238 valend
= obj
.value
.name
+ sizeof(obj
.value
.name
) - 1;
1243 obj
.type
= CUPS_PS_OTHER
;
1244 valptr
= obj
.value
.other
;
1245 valend
= obj
.value
.other
+ sizeof(obj
.value
.other
) - 1;
1250 if (strchr("()<>[]{}/%", *cur
) || isspace(*cur
& 255))
1252 else if (valptr
< valend
)
1261 if (obj
.type
== CUPS_PS_OTHER
)
1263 if (!strcmp(obj
.value
.other
, "true"))
1265 obj
.type
= CUPS_PS_BOOLEAN
;
1266 obj
.value
.boolean
= 1;
1268 else if (!strcmp(obj
.value
.other
, "false"))
1270 obj
.type
= CUPS_PS_BOOLEAN
;
1271 obj
.value
.boolean
= 0;
1273 else if (!strcmp(obj
.value
.other
, "null"))
1274 obj
.type
= CUPS_PS_NULL
;
1275 else if (!strcmp(obj
.value
.other
, "cleartomark"))
1276 obj
.type
= CUPS_PS_CLEARTOMARK
;
1277 else if (!strcmp(obj
.value
.other
, "copy"))
1278 obj
.type
= CUPS_PS_COPY
;
1279 else if (!strcmp(obj
.value
.other
, "dup"))
1280 obj
.type
= CUPS_PS_DUP
;
1281 else if (!strcmp(obj
.value
.other
, "index"))
1282 obj
.type
= CUPS_PS_INDEX
;
1283 else if (!strcmp(obj
.value
.other
, "pop"))
1284 obj
.type
= CUPS_PS_POP
;
1285 else if (!strcmp(obj
.value
.other
, "roll"))
1286 obj
.type
= CUPS_PS_ROLL
;
1287 else if (!strcmp(obj
.value
.other
, "setpagedevice"))
1288 obj
.type
= CUPS_PS_SETPAGEDEVICE
;
1289 else if (!strcmp(obj
.value
.other
, "stopped"))
1290 obj
.type
= CUPS_PS_STOPPED
;
1296 * Save the current position in the string and return the new object...
1301 return (push_stack(st
, &obj
));
1306 * 'setpagedevice()' - Simulate the PostScript setpagedevice operator.
1309 static int /* O - 0 on success, -1 on error */
1311 _cups_ps_stack_t
*st
, /* I - Stack */
1312 cups_page_header2_t
*h
, /* O - Page header */
1313 int *preferred_bits
)/* O - Preferred bits per color */
1315 int i
; /* Index into array */
1316 _cups_ps_obj_t
*obj
, /* Current object */
1317 *end
; /* End of dictionary */
1318 const char *name
; /* Attribute name */
1322 * Make sure we have a dictionary on the stack...
1325 if (st
->num_objs
== 0)
1328 obj
= end
= st
->objs
+ st
->num_objs
- 1;
1330 if (obj
->type
!= CUPS_PS_END_DICT
)
1335 while (obj
> st
->objs
)
1337 if (obj
->type
== CUPS_PS_START_DICT
)
1347 * Found the start of the dictionary, empty the stack to this point...
1350 st
->num_objs
= obj
- st
->objs
;
1353 * Now pull /name and value pairs from the dictionary...
1356 DEBUG_puts(" Dictionary:");
1358 for (obj
++; obj
< end
; obj
++)
1364 if (obj
->type
!= CUPS_PS_NAME
)
1367 name
= obj
->value
.name
;
1371 printf(" /%s ", name
);
1377 * Then grab the value...
1380 if (!strcmp(name
, "MediaClass") && obj
->type
== CUPS_PS_STRING
)
1381 strlcpy(h
->MediaClass
, obj
->value
.string
, sizeof(h
->MediaClass
));
1382 else if (!strcmp(name
, "MediaColor") && obj
->type
== CUPS_PS_STRING
)
1383 strlcpy(h
->MediaColor
, obj
->value
.string
, sizeof(h
->MediaColor
));
1384 else if (!strcmp(name
, "MediaType") && obj
->type
== CUPS_PS_STRING
)
1385 strlcpy(h
->MediaType
, obj
->value
.string
, sizeof(h
->MediaType
));
1386 else if (!strcmp(name
, "OutputType") && obj
->type
== CUPS_PS_STRING
)
1387 strlcpy(h
->OutputType
, obj
->value
.string
, sizeof(h
->OutputType
));
1388 else if (!strcmp(name
, "AdvanceDistance") && obj
->type
== CUPS_PS_NUMBER
)
1389 h
->AdvanceDistance
= (unsigned)obj
->value
.number
;
1390 else if (!strcmp(name
, "AdvanceMedia") && obj
->type
== CUPS_PS_NUMBER
)
1391 h
->AdvanceMedia
= (unsigned)obj
->value
.number
;
1392 else if (!strcmp(name
, "Collate") && obj
->type
== CUPS_PS_BOOLEAN
)
1393 h
->Collate
= (unsigned)obj
->value
.boolean
;
1394 else if (!strcmp(name
, "CutMedia") && obj
->type
== CUPS_PS_NUMBER
)
1395 h
->CutMedia
= (cups_cut_t
)(unsigned)obj
->value
.number
;
1396 else if (!strcmp(name
, "Duplex") && obj
->type
== CUPS_PS_BOOLEAN
)
1397 h
->Duplex
= (unsigned)obj
->value
.boolean
;
1398 else if (!strcmp(name
, "HWResolution") && obj
->type
== CUPS_PS_START_ARRAY
)
1400 if (obj
[1].type
== CUPS_PS_NUMBER
&& obj
[2].type
== CUPS_PS_NUMBER
&&
1401 obj
[3].type
== CUPS_PS_END_ARRAY
)
1403 h
->HWResolution
[0] = (unsigned)obj
[1].value
.number
;
1404 h
->HWResolution
[1] = (unsigned)obj
[2].value
.number
;
1410 else if (!strcmp(name
, "InsertSheet") && obj
->type
== CUPS_PS_BOOLEAN
)
1411 h
->InsertSheet
= (unsigned)obj
->value
.boolean
;
1412 else if (!strcmp(name
, "Jog") && obj
->type
== CUPS_PS_NUMBER
)
1413 h
->Jog
= (unsigned)obj
->value
.number
;
1414 else if (!strcmp(name
, "LeadingEdge") && obj
->type
== CUPS_PS_NUMBER
)
1415 h
->LeadingEdge
= (unsigned)obj
->value
.number
;
1416 else if (!strcmp(name
, "ManualFeed") && obj
->type
== CUPS_PS_BOOLEAN
)
1417 h
->ManualFeed
= (unsigned)obj
->value
.boolean
;
1418 else if ((!strcmp(name
, "cupsMediaPosition") || /* Compatibility */
1419 !strcmp(name
, "MediaPosition")) && obj
->type
== CUPS_PS_NUMBER
)
1420 h
->MediaPosition
= (unsigned)obj
->value
.number
;
1421 else if (!strcmp(name
, "MediaWeight") && obj
->type
== CUPS_PS_NUMBER
)
1422 h
->MediaWeight
= (unsigned)obj
->value
.number
;
1423 else if (!strcmp(name
, "MirrorPrint") && obj
->type
== CUPS_PS_BOOLEAN
)
1424 h
->MirrorPrint
= (unsigned)obj
->value
.boolean
;
1425 else if (!strcmp(name
, "NegativePrint") && obj
->type
== CUPS_PS_BOOLEAN
)
1426 h
->NegativePrint
= (unsigned)obj
->value
.boolean
;
1427 else if (!strcmp(name
, "NumCopies") && obj
->type
== CUPS_PS_NUMBER
)
1428 h
->NumCopies
= (unsigned)obj
->value
.number
;
1429 else if (!strcmp(name
, "Orientation") && obj
->type
== CUPS_PS_NUMBER
)
1430 h
->Orientation
= (unsigned)obj
->value
.number
;
1431 else if (!strcmp(name
, "OutputFaceUp") && obj
->type
== CUPS_PS_BOOLEAN
)
1432 h
->OutputFaceUp
= (unsigned)obj
->value
.boolean
;
1433 else if (!strcmp(name
, "PageSize") && obj
->type
== CUPS_PS_START_ARRAY
)
1435 if (obj
[1].type
== CUPS_PS_NUMBER
&& obj
[2].type
== CUPS_PS_NUMBER
&&
1436 obj
[3].type
== CUPS_PS_END_ARRAY
)
1438 h
->cupsPageSize
[0] = obj
[1].value
.number
;
1439 h
->cupsPageSize
[1] = obj
[2].value
.number
;
1441 h
->PageSize
[0] = (unsigned)obj
[1].value
.number
;
1442 h
->PageSize
[1] = (unsigned)obj
[2].value
.number
;
1449 else if (!strcmp(name
, "Separations") && obj
->type
== CUPS_PS_BOOLEAN
)
1450 h
->Separations
= (unsigned)obj
->value
.boolean
;
1451 else if (!strcmp(name
, "TraySwitch") && obj
->type
== CUPS_PS_BOOLEAN
)
1452 h
->TraySwitch
= (unsigned)obj
->value
.boolean
;
1453 else if (!strcmp(name
, "Tumble") && obj
->type
== CUPS_PS_BOOLEAN
)
1454 h
->Tumble
= (unsigned)obj
->value
.boolean
;
1455 else if (!strcmp(name
, "cupsMediaType") && obj
->type
== CUPS_PS_NUMBER
)
1456 h
->cupsMediaType
= (unsigned)obj
->value
.number
;
1457 else if (!strcmp(name
, "cupsBitsPerColor") && obj
->type
== CUPS_PS_NUMBER
)
1458 h
->cupsBitsPerColor
= (unsigned)obj
->value
.number
;
1459 else if (!strcmp(name
, "cupsPreferredBitsPerColor") &&
1460 obj
->type
== CUPS_PS_NUMBER
)
1461 *preferred_bits
= (int)obj
->value
.number
;
1462 else if (!strcmp(name
, "cupsColorOrder") && obj
->type
== CUPS_PS_NUMBER
)
1463 h
->cupsColorOrder
= (cups_order_t
)(unsigned)obj
->value
.number
;
1464 else if (!strcmp(name
, "cupsColorSpace") && obj
->type
== CUPS_PS_NUMBER
)
1465 h
->cupsColorSpace
= (cups_cspace_t
)(unsigned)obj
->value
.number
;
1466 else if (!strcmp(name
, "cupsCompression") && obj
->type
== CUPS_PS_NUMBER
)
1467 h
->cupsCompression
= (unsigned)obj
->value
.number
;
1468 else if (!strcmp(name
, "cupsRowCount") && obj
->type
== CUPS_PS_NUMBER
)
1469 h
->cupsRowCount
= (unsigned)obj
->value
.number
;
1470 else if (!strcmp(name
, "cupsRowFeed") && obj
->type
== CUPS_PS_NUMBER
)
1471 h
->cupsRowFeed
= (unsigned)obj
->value
.number
;
1472 else if (!strcmp(name
, "cupsRowStep") && obj
->type
== CUPS_PS_NUMBER
)
1473 h
->cupsRowStep
= (unsigned)obj
->value
.number
;
1474 else if (!strcmp(name
, "cupsBorderlessScalingFactor") &&
1475 obj
->type
== CUPS_PS_NUMBER
)
1476 h
->cupsBorderlessScalingFactor
= obj
->value
.number
;
1477 else if (!strncmp(name
, "cupsInteger", 11) && obj
->type
== CUPS_PS_NUMBER
)
1479 if ((i
= atoi(name
+ 11)) < 0 || i
> 15)
1482 h
->cupsInteger
[i
] = (unsigned)obj
->value
.number
;
1484 else if (!strncmp(name
, "cupsReal", 8) && obj
->type
== CUPS_PS_NUMBER
)
1486 if ((i
= atoi(name
+ 8)) < 0 || i
> 15)
1489 h
->cupsReal
[i
] = obj
->value
.number
;
1491 else if (!strncmp(name
, "cupsString", 10) && obj
->type
== CUPS_PS_STRING
)
1493 if ((i
= atoi(name
+ 10)) < 0 || i
> 15)
1496 strlcpy(h
->cupsString
[i
], obj
->value
.string
, sizeof(h
->cupsString
[i
]));
1498 else if (!strcmp(name
, "cupsMarkerType") && obj
->type
== CUPS_PS_STRING
)
1499 strlcpy(h
->cupsMarkerType
, obj
->value
.string
, sizeof(h
->cupsMarkerType
));
1500 else if (!strcmp(name
, "cupsPageSizeName") && obj
->type
== CUPS_PS_STRING
)
1501 strlcpy(h
->cupsPageSizeName
, obj
->value
.string
,
1502 sizeof(h
->cupsPageSizeName
));
1503 else if (!strcmp(name
, "cupsRenderingIntent") &&
1504 obj
->type
== CUPS_PS_STRING
)
1505 strlcpy(h
->cupsRenderingIntent
, obj
->value
.string
,
1506 sizeof(h
->cupsRenderingIntent
));
1510 * Ignore unknown name+value...
1513 DEBUG_printf((" Unknown name (\"%s\") or value...\n", name
));
1515 while (obj
[1].type
!= CUPS_PS_NAME
&& obj
< end
)
1526 * 'DEBUG_object()' - Print an object's value...
1530 DEBUG_object(_cups_ps_obj_t
*obj
) /* I - Object to print */
1535 printf("/%s", obj
->value
.name
);
1538 case CUPS_PS_NUMBER
:
1539 printf("%g", obj
->value
.number
);
1542 case CUPS_PS_STRING
:
1543 printf("(%s)", obj
->value
.string
);
1546 case CUPS_PS_BOOLEAN
:
1547 if (obj
->value
.boolean
)
1548 fputs("true", stdout
);
1550 fputs("false", stdout
);
1554 fputs("null", stdout
);
1557 case CUPS_PS_START_ARRAY
:
1561 case CUPS_PS_END_ARRAY
:
1565 case CUPS_PS_START_DICT
:
1566 fputs("<<", stdout
);
1569 case CUPS_PS_END_DICT
:
1570 fputs(">>", stdout
);
1573 case CUPS_PS_START_PROC
:
1577 case CUPS_PS_END_PROC
:
1581 case CUPS_PS_CLEARTOMARK
:
1582 fputs("--cleartomark--", stdout
);
1586 fputs("--copy--", stdout
);
1590 fputs("--dup--", stdout
);
1593 case CUPS_PS_INDEX
:
1594 fputs("--index--", stdout
);
1598 fputs("--pop--", stdout
);
1602 fputs("--roll--", stdout
);
1605 case CUPS_PS_SETPAGEDEVICE
:
1606 fputs("--setpagedevice--", stdout
);
1609 case CUPS_PS_STOPPED
:
1610 fputs("--stopped--", stdout
);
1613 case CUPS_PS_OTHER
:
1614 printf("--%s--", obj
->value
.other
);
1621 * 'DEBUG_stack()' - Print a stack...
1625 DEBUG_stack(_cups_ps_stack_t
*st
) /* I - Stack */
1627 int c
; /* Looping var */
1628 _cups_ps_obj_t
*obj
; /* Current object on stack */
1631 for (obj
= st
->objs
, c
= st
->num_objs
; c
> 0; c
--, obj
++)
1643 * End of "$Id: interpret.c 6281 2007-02-14 16:32:42Z mike $".