2 * "$Id: interpret.c 7852 2008-08-21 04:19:45Z mike $"
4 * PPD command interpreter for CUPS.
6 * Copyright 2007-2011 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/raster-private.h>
47 * Stack values for the PostScript mini-interpreter...
69 CUPS_PS_SETPAGEDEVICE
,
76 _cups_ps_type_t type
; /* Object type */
79 int boolean
; /* Boolean value */
80 char name
[64]; /* Name value */
81 double number
; /* Number value */
82 char other
[64]; /* Other operator */
83 char string
[64]; /* Sring value */
89 int num_objs
, /* Number of objects on stack */
90 alloc_objs
; /* Number of allocated objects */
91 _cups_ps_obj_t
*objs
; /* Objects in stack */
99 static int cleartomark_stack(_cups_ps_stack_t
*st
);
100 static int copy_stack(_cups_ps_stack_t
*st
, int count
);
101 static void delete_stack(_cups_ps_stack_t
*st
);
102 static void error_object(_cups_ps_obj_t
*obj
);
103 static void error_stack(_cups_ps_stack_t
*st
, const char *title
);
104 static _cups_ps_obj_t
*index_stack(_cups_ps_stack_t
*st
, int n
);
105 static _cups_ps_stack_t
*new_stack(void);
106 static _cups_ps_obj_t
*pop_stack(_cups_ps_stack_t
*st
);
107 static _cups_ps_obj_t
*push_stack(_cups_ps_stack_t
*st
,
108 _cups_ps_obj_t
*obj
);
109 static int roll_stack(_cups_ps_stack_t
*st
, int c
, int s
);
110 static _cups_ps_obj_t
*scan_ps(_cups_ps_stack_t
*st
, char **ptr
);
111 static int setpagedevice(_cups_ps_stack_t
*st
,
112 cups_page_header2_t
*h
,
113 int *preferred_bits
);
115 static void DEBUG_object(_cups_ps_obj_t
*obj
);
116 static void DEBUG_stack(_cups_ps_stack_t
*st
);
121 * 'cupsRasterInterpretPPD()' - Interpret PPD commands to create a page header.
123 * This function is used by raster image processing (RIP) filters like
124 * cgpdftoraster and imagetoraster when writing CUPS raster data for a page.
125 * It is not used by raster printer driver filters which only read CUPS
129 * @code cupsRasterInterpretPPD@ does not mark the options in the PPD using
130 * the "num_options" and "options" arguments. Instead, mark the options with
131 * @code cupsMarkOptions@ and @code ppdMarkOption@ prior to calling it -
132 * this allows for per-page options without manipulating the options array.
134 * The "func" argument specifies an optional callback function that is
135 * called prior to the computation of the final raster data. The function
136 * can make changes to the @link cups_page_header2_t@ data as needed to use a
137 * supported raster format and then returns 0 on success and -1 if the
138 * requested attributes cannot be supported.
141 * @code cupsRasterInterpretPPD@ supports a subset of the PostScript language.
142 * Currently only the @code [@, @code ]@, @code <<@, @code >>@, @code {@,
143 * @code }@, @code cleartomark@, @code copy@, @code dup@, @code index@,
144 * @code pop@, @code roll@, @code setpagedevice@, and @code stopped@ operators
147 * @since CUPS 1.2/Mac OS X 10.5@
150 int /* O - 0 on success, -1 on failure */
151 cupsRasterInterpretPPD(
152 cups_page_header2_t
*h
, /* O - Page header to create */
153 ppd_file_t
*ppd
, /* I - PPD file */
154 int num_options
, /* I - Number of options */
155 cups_option_t
*options
, /* I - Options */
156 cups_interpret_cb_t func
) /* I - Optional page header callback (@code NULL@ for none) */
158 int status
; /* Cummulative status */
159 char *code
; /* Code to run */
160 const char *val
; /* Option value */
161 ppd_size_t
*size
; /* Current size */
162 float left
, /* Left position */
163 bottom
, /* Bottom position */
164 right
, /* Right position */
165 top
; /* Top position */
166 int preferred_bits
; /* Preferred bits per color */
170 * Range check input...
173 _cupsRasterClearError();
177 _cupsRasterAddError("Page header cannot be NULL!\n");
182 * Reset the page header to the defaults...
185 memset(h
, 0, sizeof(cups_page_header2_t
));
188 h
->PageSize
[0] = 612;
189 h
->PageSize
[1] = 792;
190 h
->HWResolution
[0] = 100;
191 h
->HWResolution
[1] = 100;
192 h
->cupsBitsPerColor
= 1;
193 h
->cupsColorOrder
= CUPS_ORDER_CHUNKED
;
194 h
->cupsColorSpace
= CUPS_CSPACE_K
;
195 h
->cupsBorderlessScalingFactor
= 1.0f
;
196 h
->cupsPageSize
[0] = 612.0f
;
197 h
->cupsPageSize
[1] = 792.0f
;
198 h
->cupsImagingBBox
[0] = 0.0f
;
199 h
->cupsImagingBBox
[1] = 0.0f
;
200 h
->cupsImagingBBox
[2] = 612.0f
;
201 h
->cupsImagingBBox
[3] = 792.0f
;
203 strcpy(h
->cupsPageSizeName
, "Letter");
207 * cupsInteger0 is also used for the total page count on Mac OS X; set an
208 * uncommon default value so we can tell if the driver is using cupsInteger0.
211 h
->cupsInteger
[0] = 0x80000000;
212 #endif /* __APPLE__ */
215 * Apply patches and options to the page header...
224 * Apply any patch code (used to override the defaults...)
228 status
|= _cupsRasterExecPS(h
, &preferred_bits
, ppd
->patches
);
231 * Then apply printer options in the proper order...
234 if ((code
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, 0.0)) != NULL
)
236 status
|= _cupsRasterExecPS(h
, &preferred_bits
, code
);
240 if ((code
= ppdEmitString(ppd
, PPD_ORDER_ANY
, 0.0)) != NULL
)
242 status
|= _cupsRasterExecPS(h
, &preferred_bits
, code
);
246 if ((code
= ppdEmitString(ppd
, PPD_ORDER_PROLOG
, 0.0)) != NULL
)
248 status
|= _cupsRasterExecPS(h
, &preferred_bits
, code
);
252 if ((code
= ppdEmitString(ppd
, PPD_ORDER_PAGE
, 0.0)) != NULL
)
254 status
|= _cupsRasterExecPS(h
, &preferred_bits
, code
);
260 * Allow option override for page scaling...
263 if ((val
= cupsGetOption("cupsBorderlessScalingFactor", num_options
,
266 double sc
= atof(val
); /* Scale factor */
268 if (sc
>= 0.1 && sc
<= 2.0)
269 h
->cupsBorderlessScalingFactor
= (float)sc
;
273 * Get the margins for the current size...
276 if ((size
= ppdPageSize(ppd
, NULL
)) != NULL
)
279 * Use the margins from the PPD file...
283 bottom
= size
->bottom
;
287 strlcpy(h
->cupsPageSizeName
, size
->name
, sizeof(h
->cupsPageSizeName
));
289 h
->cupsPageSize
[0] = size
->width
;
290 h
->cupsPageSize
[1] = size
->length
;
295 * Use the default margins...
304 h
->PageSize
[0] = (unsigned)(h
->cupsPageSize
[0] *
305 h
->cupsBorderlessScalingFactor
);
306 h
->PageSize
[1] = (unsigned)(h
->cupsPageSize
[1] *
307 h
->cupsBorderlessScalingFactor
);
308 h
->Margins
[0] = (unsigned)(left
*
309 h
->cupsBorderlessScalingFactor
);
310 h
->Margins
[1] = (unsigned)(bottom
*
311 h
->cupsBorderlessScalingFactor
);
312 h
->ImagingBoundingBox
[0] = (unsigned)(left
*
313 h
->cupsBorderlessScalingFactor
);
314 h
->ImagingBoundingBox
[1] = (unsigned)(bottom
*
315 h
->cupsBorderlessScalingFactor
);
316 h
->ImagingBoundingBox
[2] = (unsigned)(right
*
317 h
->cupsBorderlessScalingFactor
);
318 h
->ImagingBoundingBox
[3] = (unsigned)(top
*
319 h
->cupsBorderlessScalingFactor
);
320 h
->cupsImagingBBox
[0] = (float)left
;
321 h
->cupsImagingBBox
[1] = (float)bottom
;
322 h
->cupsImagingBBox
[2] = (float)right
;
323 h
->cupsImagingBBox
[3] = (float)top
;
326 * Use the callback to validate the page header...
329 if (func
&& (*func
)(h
, preferred_bits
))
331 _cupsRasterAddError("Page header callback returned error.\n");
336 * Check parameters...
339 if (!h
->HWResolution
[0] || !h
->HWResolution
[1] ||
340 !h
->PageSize
[0] || !h
->PageSize
[1] ||
341 (h
->cupsBitsPerColor
!= 1 && h
->cupsBitsPerColor
!= 2 &&
342 h
->cupsBitsPerColor
!= 4 && h
->cupsBitsPerColor
!= 8 &&
343 h
->cupsBitsPerColor
!= 16) ||
344 h
->cupsBorderlessScalingFactor
< 0.1 ||
345 h
->cupsBorderlessScalingFactor
> 2.0)
347 _cupsRasterAddError("Page header uses unsupported values.\n");
352 * Compute the bitmap parameters...
355 h
->cupsWidth
= (int)((right
- left
) * h
->cupsBorderlessScalingFactor
*
356 h
->HWResolution
[0] / 72.0f
+ 0.5f
);
357 h
->cupsHeight
= (int)((top
- bottom
) * h
->cupsBorderlessScalingFactor
*
358 h
->HWResolution
[1] / 72.0f
+ 0.5f
);
360 switch (h
->cupsColorSpace
)
364 case CUPS_CSPACE_WHITE
:
365 case CUPS_CSPACE_GOLD
:
366 case CUPS_CSPACE_SILVER
:
367 case CUPS_CSPACE_SW
:
368 h
->cupsNumColors
= 1;
369 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
;
374 * Ensure that colorimetric colorspaces use at least 8 bits per
378 if (h
->cupsColorSpace
>= CUPS_CSPACE_CIEXYZ
&&
379 h
->cupsBitsPerColor
< 8)
380 h
->cupsBitsPerColor
= 8;
383 * Figure out the number of bits per pixel...
386 if (h
->cupsColorOrder
== CUPS_ORDER_CHUNKED
)
388 if (h
->cupsBitsPerColor
>= 8)
389 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
* 3;
391 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
* 4;
394 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
;
396 h
->cupsNumColors
= 3;
399 case CUPS_CSPACE_KCMYcm
:
400 if (h
->cupsBitsPerColor
== 1)
402 if (h
->cupsColorOrder
== CUPS_ORDER_CHUNKED
)
403 h
->cupsBitsPerPixel
= 8;
405 h
->cupsBitsPerPixel
= 1;
407 h
->cupsNumColors
= 6;
412 * Fall through to CMYK code...
415 case CUPS_CSPACE_RGBA
:
416 case CUPS_CSPACE_RGBW
:
417 case CUPS_CSPACE_CMYK
:
418 case CUPS_CSPACE_YMCK
:
419 case CUPS_CSPACE_KCMY
:
420 case CUPS_CSPACE_GMCK
:
421 case CUPS_CSPACE_GMCS
:
422 if (h
->cupsColorOrder
== CUPS_ORDER_CHUNKED
)
423 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
* 4;
425 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
;
427 h
->cupsNumColors
= 4;
430 case CUPS_CSPACE_DEVICE1
:
431 case CUPS_CSPACE_DEVICE2
:
432 case CUPS_CSPACE_DEVICE3
:
433 case CUPS_CSPACE_DEVICE4
:
434 case CUPS_CSPACE_DEVICE5
:
435 case CUPS_CSPACE_DEVICE6
:
436 case CUPS_CSPACE_DEVICE7
:
437 case CUPS_CSPACE_DEVICE8
:
438 case CUPS_CSPACE_DEVICE9
:
439 case CUPS_CSPACE_DEVICEA
:
440 case CUPS_CSPACE_DEVICEB
:
441 case CUPS_CSPACE_DEVICEC
:
442 case CUPS_CSPACE_DEVICED
:
443 case CUPS_CSPACE_DEVICEE
:
444 case CUPS_CSPACE_DEVICEF
:
445 h
->cupsNumColors
= h
->cupsColorSpace
- CUPS_CSPACE_DEVICE1
+ 1;
447 if (h
->cupsColorOrder
== CUPS_ORDER_CHUNKED
)
448 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
* h
->cupsNumColors
;
450 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
;
454 h
->cupsBytesPerLine
= (h
->cupsBitsPerPixel
* h
->cupsWidth
+ 7) / 8;
456 if (h
->cupsColorOrder
== CUPS_ORDER_BANDED
)
457 h
->cupsBytesPerLine
*= h
->cupsNumColors
;
464 * '_cupsRasterExecPS()' - Execute PostScript code to initialize a page header.
467 int /* O - 0 on success, -1 on error */
469 cups_page_header2_t
*h
, /* O - Page header */
470 int *preferred_bits
,/* O - Preferred bits per color */
471 const char *code
) /* I - PS code to execute */
473 _cups_ps_stack_t
*st
; /* PostScript value stack */
474 _cups_ps_obj_t
*obj
; /* Object from top of stack */
475 char *codecopy
, /* Copy of code */
476 *codeptr
; /* Pointer into copy of code */
479 DEBUG_printf(("_cupsRasterExecPS(h=%p, preferred_bits=%p, code=\"%s\")\n",
480 h
, preferred_bits
, code
? code
: "(null)"));
483 * Copy the PostScript code and create a stack...
486 if ((codecopy
= strdup(code
)) == NULL
)
488 _cupsRasterAddError("Unable to duplicate code string.\n");
492 if ((st
= new_stack()) == NULL
)
494 _cupsRasterAddError("Unable to create stack.\n");
500 * Parse the PS string until we run out of data...
505 while ((obj
= scan_ps(st
, &codeptr
)) != NULL
)
508 DEBUG_printf(("_cupsRasterExecPS: Stack (%d objects)\n", st
->num_objs
));
515 /* Do nothing for regular values */
518 case CUPS_PS_CLEARTOMARK
:
521 if (cleartomark_stack(st
))
522 _cupsRasterAddError("cleartomark: Stack underflow!\n");
525 DEBUG_puts(" dup: ");
532 if ((obj
= pop_stack(st
)) != NULL
)
534 copy_stack(st
, (int)obj
->value
.number
);
537 DEBUG_puts("_cupsRasterExecPS: copy");
548 DEBUG_puts("_cupsRasterExecPS: dup");
555 if ((obj
= pop_stack(st
)) != NULL
)
557 index_stack(st
, (int)obj
->value
.number
);
560 DEBUG_puts("_cupsRasterExecPS: index");
571 DEBUG_puts("_cupsRasterExecPS: pop");
578 if ((obj
= pop_stack(st
)) != NULL
)
583 c
= (int)obj
->value
.number
;
585 if ((obj
= pop_stack(st
)) != NULL
)
587 roll_stack(st
, (int)obj
->value
.number
, c
);
590 DEBUG_puts("_cupsRasterExecPS: roll");
597 case CUPS_PS_SETPAGEDEVICE
:
599 setpagedevice(st
, h
, preferred_bits
);
602 DEBUG_puts("_cupsRasterExecPS: setpagedevice");
607 case CUPS_PS_START_PROC
:
608 case CUPS_PS_END_PROC
:
609 case CUPS_PS_STOPPED
:
614 _cupsRasterAddError("Unknown operator \"%s\"!\n", obj
->value
.other
);
615 DEBUG_printf(("_cupsRasterExecPS: Unknown operator \"%s\"!\n",
620 if (obj
&& obj
->type
== CUPS_PS_OTHER
)
630 if (st
->num_objs
> 0)
632 error_stack(st
, "Stack not empty:");
635 DEBUG_puts("_cupsRasterExecPS: Stack not empty:");
655 * 'cleartomark_stack()' - Clear to the last mark ([) on the stack.
658 static int /* O - 0 on success, -1 on error */
659 cleartomark_stack(_cups_ps_stack_t
*st
) /* I - Stack */
661 _cups_ps_obj_t
*obj
; /* Current object on stack */
664 while ((obj
= pop_stack(st
)) != NULL
)
665 if (obj
->type
== CUPS_PS_START_ARRAY
)
668 return (obj
? 0 : -1);
673 * 'copy_stack()' - Copy the top N stack objects.
676 static int /* O - 0 on success, -1 on error */
677 copy_stack(_cups_ps_stack_t
*st
, /* I - Stack */
678 int c
) /* I - Number of objects to copy */
688 if ((n
= st
->num_objs
- c
) < 0)
693 if (!push_stack(st
, st
->objs
+ n
))
705 * 'delete_stack()' - Free memory used by a stack.
709 delete_stack(_cups_ps_stack_t
*st
) /* I - Stack */
717 * 'error_object()' - Add an object's value to the current error message.
721 error_object(_cups_ps_obj_t
*obj
) /* I - Object to add */
726 _cupsRasterAddError(" /%s", obj
->value
.name
);
729 case CUPS_PS_NUMBER
:
730 _cupsRasterAddError(" %g", obj
->value
.number
);
733 case CUPS_PS_STRING
:
734 _cupsRasterAddError(" (%s)", obj
->value
.string
);
737 case CUPS_PS_BOOLEAN
:
738 if (obj
->value
.boolean
)
739 _cupsRasterAddError(" true");
741 _cupsRasterAddError(" false");
745 _cupsRasterAddError(" null");
748 case CUPS_PS_START_ARRAY
:
749 _cupsRasterAddError(" [");
752 case CUPS_PS_END_ARRAY
:
753 _cupsRasterAddError(" ]");
756 case CUPS_PS_START_DICT
:
757 _cupsRasterAddError(" <<");
760 case CUPS_PS_END_DICT
:
761 _cupsRasterAddError(" >>");
764 case CUPS_PS_START_PROC
:
765 _cupsRasterAddError(" {");
768 case CUPS_PS_END_PROC
:
769 _cupsRasterAddError(" }");
773 _cupsRasterAddError(" --copy--");
776 case CUPS_PS_CLEARTOMARK
:
777 _cupsRasterAddError(" --cleartomark--");
781 _cupsRasterAddError(" --dup--");
785 _cupsRasterAddError(" --index--");
789 _cupsRasterAddError(" --pop--");
793 _cupsRasterAddError(" --roll--");
796 case CUPS_PS_SETPAGEDEVICE
:
797 _cupsRasterAddError(" --setpagedevice--");
800 case CUPS_PS_STOPPED
:
801 _cupsRasterAddError(" --stopped--");
805 _cupsRasterAddError(" --%s--", obj
->value
.other
);
812 * 'error_stack()' - Add a stack to the current error message...
816 error_stack(_cups_ps_stack_t
*st
, /* I - Stack */
817 const char *title
) /* I - Title string */
819 int c
; /* Looping var */
820 _cups_ps_obj_t
*obj
; /* Current object on stack */
823 _cupsRasterAddError("%s", title
);
825 for (obj
= st
->objs
, c
= st
->num_objs
; c
> 0; c
--, obj
++)
828 _cupsRasterAddError("\n");
833 * 'index_stack()' - Copy the Nth value on the stack.
836 static _cups_ps_obj_t
* /* O - New object */
837 index_stack(_cups_ps_stack_t
*st
, /* I - Stack */
838 int n
) /* I - Object index */
840 if (n
< 0 || (n
= st
->num_objs
- n
- 1) < 0)
843 return (push_stack(st
, st
->objs
+ n
));
848 * 'new_stack()' - Create a new stack.
851 static _cups_ps_stack_t
* /* O - New stack */
854 _cups_ps_stack_t
*st
; /* New stack */
857 if ((st
= calloc(1, sizeof(_cups_ps_stack_t
))) == NULL
)
862 if ((st
->objs
= calloc(32, sizeof(_cups_ps_obj_t
))) == NULL
)
873 * 'pop_stock()' - Pop the top object off the stack.
876 static _cups_ps_obj_t
* /* O - Object */
877 pop_stack(_cups_ps_stack_t
*st
) /* I - Stack */
879 if (st
->num_objs
> 0)
883 return (st
->objs
+ st
->num_objs
);
891 * 'push_stack()' - Push an object on the stack.
894 static _cups_ps_obj_t
* /* O - New object */
895 push_stack(_cups_ps_stack_t
*st
, /* I - Stack */
896 _cups_ps_obj_t
*obj
) /* I - Object */
898 _cups_ps_obj_t
*temp
; /* New object */
901 if (st
->num_objs
>= st
->alloc_objs
)
905 st
->alloc_objs
+= 32;
907 if ((temp
= realloc(st
->objs
, st
->alloc_objs
*
908 sizeof(_cups_ps_obj_t
))) == NULL
)
912 memset(temp
+ st
->num_objs
, 0, 32 * sizeof(_cups_ps_obj_t
));
915 temp
= st
->objs
+ st
->num_objs
;
918 memcpy(temp
, obj
, sizeof(_cups_ps_obj_t
));
925 * 'roll_stack()' - Rotate stack objects.
928 static int /* O - 0 on success, -1 on error */
929 roll_stack(_cups_ps_stack_t
*st
, /* I - Stack */
930 int c
, /* I - Number of objects */
931 int s
) /* I - Amount to shift */
933 _cups_ps_obj_t
*temp
; /* Temporary array of objects */
934 int n
; /* Index into array */
937 DEBUG_printf((" roll_stack(st=%p, s=%d, c=%d)\n", st
, s
, c
));
940 * Range check input...
948 if ((n
= st
->num_objs
- c
) < 0)
957 * Copy N objects and move things around...
968 if ((temp
= calloc(s
, sizeof(_cups_ps_obj_t
))) == NULL
)
971 memcpy(temp
, st
->objs
+ n
, s
* sizeof(_cups_ps_obj_t
));
972 memmove(st
->objs
+ n
, st
->objs
+ n
+ s
, (c
- s
) * sizeof(_cups_ps_obj_t
));
973 memcpy(st
->objs
+ n
+ c
- s
, temp
, s
* sizeof(_cups_ps_obj_t
));
981 if ((temp
= calloc(s
, sizeof(_cups_ps_obj_t
))) == NULL
)
984 memcpy(temp
, st
->objs
+ n
+ c
- s
, s
* sizeof(_cups_ps_obj_t
));
985 memmove(st
->objs
+ n
+ s
, st
->objs
+ n
,
986 (c
- s
) * sizeof(_cups_ps_obj_t
));
987 memcpy(st
->objs
+ n
, temp
, s
* sizeof(_cups_ps_obj_t
));
997 * 'scan_ps()' - Scan a string for the next PS object.
1000 static _cups_ps_obj_t
* /* O - New object or NULL on EOF */
1001 scan_ps(_cups_ps_stack_t
*st
, /* I - Stack */
1002 char **ptr
) /* IO - String pointer */
1004 _cups_ps_obj_t obj
; /* Current object */
1005 char *start
, /* Start of object */
1006 *cur
, /* Current position */
1007 *valptr
, /* Pointer into value string */
1008 *valend
; /* End of value string */
1009 int parens
; /* Parenthesis nesting level */
1013 * Skip leading whitespace...
1016 for (cur
= *ptr
; *cur
; cur
++)
1021 * Comment, skip to end of line...
1024 for (cur
++; *cur
&& *cur
!= '\n' && *cur
!= '\r'; cur
++);
1029 else if (!isspace(*cur
& 255))
1041 * See what we have...
1044 memset(&obj
, 0, sizeof(obj
));
1048 case '(' : /* (string) */
1049 obj
.type
= CUPS_PS_STRING
;
1052 for (cur
++, parens
= 1, valptr
= obj
.value
.string
,
1053 valend
= obj
.value
.string
+ sizeof(obj
.value
.string
) - 1;
1057 if (*cur
== ')' && parens
== 1)
1062 else if (*cur
== ')')
1065 if (valptr
>= valend
)
1075 * Decode escaped character...
1082 else if (*cur
== 'f')
1084 else if (*cur
== 'n')
1086 else if (*cur
== 'r')
1088 else if (*cur
== 't')
1090 else if (*cur
>= '0' && *cur
<= '7')
1092 int ch
= *cur
- '0';
1094 if (cur
[1] >= '0' && cur
[1] <= '7')
1097 ch
= (ch
<< 3) + *cur
- '0';
1100 if (cur
[1] >= '0' && cur
[1] <= '7')
1103 ch
= (ch
<< 3) + *cur
- '0';
1108 else if (*cur
== '\r')
1113 else if (*cur
!= '\n')
1130 case '[' : /* Start array */
1131 obj
.type
= CUPS_PS_START_ARRAY
;
1135 case ']' : /* End array */
1136 obj
.type
= CUPS_PS_END_ARRAY
;
1140 case '<' : /* Start dictionary or hex string */
1143 obj
.type
= CUPS_PS_START_DICT
;
1148 obj
.type
= CUPS_PS_STRING
;
1151 for (cur
++, valptr
= obj
.value
.string
,
1152 valend
= obj
.value
.string
+ sizeof(obj
.value
.string
) - 1;
1156 int ch
; /* Current character */
1162 else if (valptr
>= valend
|| !isxdigit(*cur
& 255))
1168 if (*cur
>= '0' && *cur
<= '9')
1169 ch
= (*cur
- '0') << 4;
1171 ch
= (tolower(*cur
) - 'a' + 10) << 4;
1173 if (isxdigit(cur
[1] & 255))
1177 if (*cur
>= '0' && *cur
<= '9')
1180 ch
|= tolower(*cur
) - 'a' + 10;
1196 case '>' : /* End dictionary? */
1199 obj
.type
= CUPS_PS_END_DICT
;
1204 obj
.type
= CUPS_PS_OTHER
;
1205 obj
.value
.other
[0] = *cur
;
1211 case '{' : /* Start procedure */
1212 obj
.type
= CUPS_PS_START_PROC
;
1216 case '}' : /* End procedure */
1217 obj
.type
= CUPS_PS_END_PROC
;
1221 case '-' : /* Possible number */
1223 if (!isdigit(cur
[1] & 255) && cur
[1] != '.')
1225 obj
.type
= CUPS_PS_OTHER
;
1226 obj
.value
.other
[0] = *cur
;
1232 case '0' : /* Number */
1243 obj
.type
= CUPS_PS_NUMBER
;
1246 for (cur
++; *cur
; cur
++)
1247 if (!isdigit(*cur
& 255))
1253 * Integer with radix...
1256 obj
.value
.number
= strtol(cur
+ 1, &cur
, atoi(start
));
1259 else if (strchr(".Ee()<>[]{}/%", *cur
) || isspace(*cur
& 255))
1262 * Integer or real number...
1265 obj
.value
.number
= _cupsStrScand(start
, &cur
, localeconv());
1271 default : /* Operator/variable name */
1276 obj
.type
= CUPS_PS_NAME
;
1277 valptr
= obj
.value
.name
;
1278 valend
= obj
.value
.name
+ sizeof(obj
.value
.name
) - 1;
1283 obj
.type
= CUPS_PS_OTHER
;
1284 valptr
= obj
.value
.other
;
1285 valend
= obj
.value
.other
+ sizeof(obj
.value
.other
) - 1;
1290 if (strchr("()<>[]{}/%", *cur
) || isspace(*cur
& 255))
1292 else if (valptr
< valend
)
1301 if (obj
.type
== CUPS_PS_OTHER
)
1303 if (!strcmp(obj
.value
.other
, "true"))
1305 obj
.type
= CUPS_PS_BOOLEAN
;
1306 obj
.value
.boolean
= 1;
1308 else if (!strcmp(obj
.value
.other
, "false"))
1310 obj
.type
= CUPS_PS_BOOLEAN
;
1311 obj
.value
.boolean
= 0;
1313 else if (!strcmp(obj
.value
.other
, "null"))
1314 obj
.type
= CUPS_PS_NULL
;
1315 else if (!strcmp(obj
.value
.other
, "cleartomark"))
1316 obj
.type
= CUPS_PS_CLEARTOMARK
;
1317 else if (!strcmp(obj
.value
.other
, "copy"))
1318 obj
.type
= CUPS_PS_COPY
;
1319 else if (!strcmp(obj
.value
.other
, "dup"))
1320 obj
.type
= CUPS_PS_DUP
;
1321 else if (!strcmp(obj
.value
.other
, "index"))
1322 obj
.type
= CUPS_PS_INDEX
;
1323 else if (!strcmp(obj
.value
.other
, "pop"))
1324 obj
.type
= CUPS_PS_POP
;
1325 else if (!strcmp(obj
.value
.other
, "roll"))
1326 obj
.type
= CUPS_PS_ROLL
;
1327 else if (!strcmp(obj
.value
.other
, "setpagedevice"))
1328 obj
.type
= CUPS_PS_SETPAGEDEVICE
;
1329 else if (!strcmp(obj
.value
.other
, "stopped"))
1330 obj
.type
= CUPS_PS_STOPPED
;
1336 * Save the current position in the string and return the new object...
1341 return (push_stack(st
, &obj
));
1346 * 'setpagedevice()' - Simulate the PostScript setpagedevice operator.
1349 static int /* O - 0 on success, -1 on error */
1351 _cups_ps_stack_t
*st
, /* I - Stack */
1352 cups_page_header2_t
*h
, /* O - Page header */
1353 int *preferred_bits
)/* O - Preferred bits per color */
1355 int i
; /* Index into array */
1356 _cups_ps_obj_t
*obj
, /* Current object */
1357 *end
; /* End of dictionary */
1358 const char *name
; /* Attribute name */
1362 * Make sure we have a dictionary on the stack...
1365 if (st
->num_objs
== 0)
1368 obj
= end
= st
->objs
+ st
->num_objs
- 1;
1370 if (obj
->type
!= CUPS_PS_END_DICT
)
1375 while (obj
> st
->objs
)
1377 if (obj
->type
== CUPS_PS_START_DICT
)
1387 * Found the start of the dictionary, empty the stack to this point...
1390 st
->num_objs
= (int)(obj
- st
->objs
);
1393 * Now pull /name and value pairs from the dictionary...
1396 DEBUG_puts("setpagedevice: Dictionary:");
1398 for (obj
++; obj
< end
; obj
++)
1404 if (obj
->type
!= CUPS_PS_NAME
)
1407 name
= obj
->value
.name
;
1411 DEBUG_printf(("setpagedevice: /%s ", name
));
1416 * Then grab the value...
1419 if (!strcmp(name
, "MediaClass") && obj
->type
== CUPS_PS_STRING
)
1420 strlcpy(h
->MediaClass
, obj
->value
.string
, sizeof(h
->MediaClass
));
1421 else if (!strcmp(name
, "MediaColor") && obj
->type
== CUPS_PS_STRING
)
1422 strlcpy(h
->MediaColor
, obj
->value
.string
, sizeof(h
->MediaColor
));
1423 else if (!strcmp(name
, "MediaType") && obj
->type
== CUPS_PS_STRING
)
1424 strlcpy(h
->MediaType
, obj
->value
.string
, sizeof(h
->MediaType
));
1425 else if (!strcmp(name
, "OutputType") && obj
->type
== CUPS_PS_STRING
)
1426 strlcpy(h
->OutputType
, obj
->value
.string
, sizeof(h
->OutputType
));
1427 else if (!strcmp(name
, "AdvanceDistance") && obj
->type
== CUPS_PS_NUMBER
)
1428 h
->AdvanceDistance
= (unsigned)obj
->value
.number
;
1429 else if (!strcmp(name
, "AdvanceMedia") && obj
->type
== CUPS_PS_NUMBER
)
1430 h
->AdvanceMedia
= (unsigned)obj
->value
.number
;
1431 else if (!strcmp(name
, "Collate") && obj
->type
== CUPS_PS_BOOLEAN
)
1432 h
->Collate
= (unsigned)obj
->value
.boolean
;
1433 else if (!strcmp(name
, "CutMedia") && obj
->type
== CUPS_PS_NUMBER
)
1434 h
->CutMedia
= (cups_cut_t
)(unsigned)obj
->value
.number
;
1435 else if (!strcmp(name
, "Duplex") && obj
->type
== CUPS_PS_BOOLEAN
)
1436 h
->Duplex
= (unsigned)obj
->value
.boolean
;
1437 else if (!strcmp(name
, "HWResolution") && 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
->HWResolution
[0] = (unsigned)obj
[1].value
.number
;
1443 h
->HWResolution
[1] = (unsigned)obj
[2].value
.number
;
1449 else if (!strcmp(name
, "InsertSheet") && obj
->type
== CUPS_PS_BOOLEAN
)
1450 h
->InsertSheet
= (unsigned)obj
->value
.boolean
;
1451 else if (!strcmp(name
, "Jog") && obj
->type
== CUPS_PS_NUMBER
)
1452 h
->Jog
= (unsigned)obj
->value
.number
;
1453 else if (!strcmp(name
, "LeadingEdge") && obj
->type
== CUPS_PS_NUMBER
)
1454 h
->LeadingEdge
= (unsigned)obj
->value
.number
;
1455 else if (!strcmp(name
, "ManualFeed") && obj
->type
== CUPS_PS_BOOLEAN
)
1456 h
->ManualFeed
= (unsigned)obj
->value
.boolean
;
1457 else if ((!strcmp(name
, "cupsMediaPosition") ||
1458 !strcmp(name
, "MediaPosition")) && obj
->type
== CUPS_PS_NUMBER
)
1461 * cupsMediaPosition is supported for backwards compatibility only.
1462 * We added it back in the Ghostscript 5.50 days to work around a
1463 * bug in Ghostscript WRT handling of MediaPosition and setpagedevice.
1465 * All new development should set MediaPosition...
1468 h
->MediaPosition
= (unsigned)obj
->value
.number
;
1470 else if (!strcmp(name
, "MediaWeight") && obj
->type
== CUPS_PS_NUMBER
)
1471 h
->MediaWeight
= (unsigned)obj
->value
.number
;
1472 else if (!strcmp(name
, "MirrorPrint") && obj
->type
== CUPS_PS_BOOLEAN
)
1473 h
->MirrorPrint
= (unsigned)obj
->value
.boolean
;
1474 else if (!strcmp(name
, "NegativePrint") && obj
->type
== CUPS_PS_BOOLEAN
)
1475 h
->NegativePrint
= (unsigned)obj
->value
.boolean
;
1476 else if (!strcmp(name
, "NumCopies") && obj
->type
== CUPS_PS_NUMBER
)
1477 h
->NumCopies
= (unsigned)obj
->value
.number
;
1478 else if (!strcmp(name
, "Orientation") && obj
->type
== CUPS_PS_NUMBER
)
1479 h
->Orientation
= (unsigned)obj
->value
.number
;
1480 else if (!strcmp(name
, "OutputFaceUp") && obj
->type
== CUPS_PS_BOOLEAN
)
1481 h
->OutputFaceUp
= (unsigned)obj
->value
.boolean
;
1482 else if (!strcmp(name
, "PageSize") && obj
->type
== CUPS_PS_START_ARRAY
)
1484 if (obj
[1].type
== CUPS_PS_NUMBER
&& obj
[2].type
== CUPS_PS_NUMBER
&&
1485 obj
[3].type
== CUPS_PS_END_ARRAY
)
1487 h
->cupsPageSize
[0] = (float)obj
[1].value
.number
;
1488 h
->cupsPageSize
[1] = (float)obj
[2].value
.number
;
1490 h
->PageSize
[0] = (unsigned)obj
[1].value
.number
;
1491 h
->PageSize
[1] = (unsigned)obj
[2].value
.number
;
1498 else if (!strcmp(name
, "Separations") && obj
->type
== CUPS_PS_BOOLEAN
)
1499 h
->Separations
= (unsigned)obj
->value
.boolean
;
1500 else if (!strcmp(name
, "TraySwitch") && obj
->type
== CUPS_PS_BOOLEAN
)
1501 h
->TraySwitch
= (unsigned)obj
->value
.boolean
;
1502 else if (!strcmp(name
, "Tumble") && obj
->type
== CUPS_PS_BOOLEAN
)
1503 h
->Tumble
= (unsigned)obj
->value
.boolean
;
1504 else if (!strcmp(name
, "cupsMediaType") && obj
->type
== CUPS_PS_NUMBER
)
1505 h
->cupsMediaType
= (unsigned)obj
->value
.number
;
1506 else if (!strcmp(name
, "cupsBitsPerColor") && obj
->type
== CUPS_PS_NUMBER
)
1507 h
->cupsBitsPerColor
= (unsigned)obj
->value
.number
;
1508 else if (!strcmp(name
, "cupsPreferredBitsPerColor") &&
1509 obj
->type
== CUPS_PS_NUMBER
)
1510 *preferred_bits
= (int)obj
->value
.number
;
1511 else if (!strcmp(name
, "cupsColorOrder") && obj
->type
== CUPS_PS_NUMBER
)
1512 h
->cupsColorOrder
= (cups_order_t
)(unsigned)obj
->value
.number
;
1513 else if (!strcmp(name
, "cupsColorSpace") && obj
->type
== CUPS_PS_NUMBER
)
1514 h
->cupsColorSpace
= (cups_cspace_t
)(unsigned)obj
->value
.number
;
1515 else if (!strcmp(name
, "cupsCompression") && obj
->type
== CUPS_PS_NUMBER
)
1516 h
->cupsCompression
= (unsigned)obj
->value
.number
;
1517 else if (!strcmp(name
, "cupsRowCount") && obj
->type
== CUPS_PS_NUMBER
)
1518 h
->cupsRowCount
= (unsigned)obj
->value
.number
;
1519 else if (!strcmp(name
, "cupsRowFeed") && obj
->type
== CUPS_PS_NUMBER
)
1520 h
->cupsRowFeed
= (unsigned)obj
->value
.number
;
1521 else if (!strcmp(name
, "cupsRowStep") && obj
->type
== CUPS_PS_NUMBER
)
1522 h
->cupsRowStep
= (unsigned)obj
->value
.number
;
1523 else if (!strcmp(name
, "cupsBorderlessScalingFactor") &&
1524 obj
->type
== CUPS_PS_NUMBER
)
1525 h
->cupsBorderlessScalingFactor
= (float)obj
->value
.number
;
1526 else if (!strncmp(name
, "cupsInteger", 11) && obj
->type
== CUPS_PS_NUMBER
)
1528 if ((i
= atoi(name
+ 11)) < 0 || i
> 15)
1531 h
->cupsInteger
[i
] = (unsigned)obj
->value
.number
;
1533 else if (!strncmp(name
, "cupsReal", 8) && obj
->type
== CUPS_PS_NUMBER
)
1535 if ((i
= atoi(name
+ 8)) < 0 || i
> 15)
1538 h
->cupsReal
[i
] = (float)obj
->value
.number
;
1540 else if (!strncmp(name
, "cupsString", 10) && obj
->type
== CUPS_PS_STRING
)
1542 if ((i
= atoi(name
+ 10)) < 0 || i
> 15)
1545 strlcpy(h
->cupsString
[i
], obj
->value
.string
, sizeof(h
->cupsString
[i
]));
1547 else if (!strcmp(name
, "cupsMarkerType") && obj
->type
== CUPS_PS_STRING
)
1548 strlcpy(h
->cupsMarkerType
, obj
->value
.string
, sizeof(h
->cupsMarkerType
));
1549 else if (!strcmp(name
, "cupsPageSizeName") && obj
->type
== CUPS_PS_STRING
)
1550 strlcpy(h
->cupsPageSizeName
, obj
->value
.string
,
1551 sizeof(h
->cupsPageSizeName
));
1552 else if (!strcmp(name
, "cupsRenderingIntent") &&
1553 obj
->type
== CUPS_PS_STRING
)
1554 strlcpy(h
->cupsRenderingIntent
, obj
->value
.string
,
1555 sizeof(h
->cupsRenderingIntent
));
1559 * Ignore unknown name+value...
1562 DEBUG_printf((" Unknown name (\"%s\") or value...\n", name
));
1564 while (obj
[1].type
!= CUPS_PS_NAME
&& obj
< end
)
1575 * 'DEBUG_object()' - Print an object's value...
1579 DEBUG_object(_cups_ps_obj_t
*obj
) /* I - Object to print */
1584 DEBUG_printf(("/%s\n", obj
->value
.name
));
1587 case CUPS_PS_NUMBER
:
1588 DEBUG_printf(("%g\n", obj
->value
.number
));
1591 case CUPS_PS_STRING
:
1592 DEBUG_printf(("(%s)\n", obj
->value
.string
));
1595 case CUPS_PS_BOOLEAN
:
1596 if (obj
->value
.boolean
)
1599 DEBUG_puts("false");
1606 case CUPS_PS_START_ARRAY
:
1610 case CUPS_PS_END_ARRAY
:
1614 case CUPS_PS_START_DICT
:
1618 case CUPS_PS_END_DICT
:
1622 case CUPS_PS_START_PROC
:
1626 case CUPS_PS_END_PROC
:
1630 case CUPS_PS_CLEARTOMARK
:
1631 DEBUG_puts("--cleartomark--");
1635 DEBUG_puts("--copy--");
1639 DEBUG_puts("--dup--");
1642 case CUPS_PS_INDEX
:
1643 DEBUG_puts("--index--");
1647 DEBUG_puts("--pop--");
1651 DEBUG_puts("--roll--");
1654 case CUPS_PS_SETPAGEDEVICE
:
1655 DEBUG_puts("--setpagedevice--");
1658 case CUPS_PS_STOPPED
:
1659 DEBUG_puts("--stopped--");
1662 case CUPS_PS_OTHER
:
1663 DEBUG_printf(("--%s--\n", obj
->value
.other
));
1670 * 'DEBUG_stack()' - Print a stack...
1674 DEBUG_stack(_cups_ps_stack_t
*st
) /* I - Stack */
1676 int c
; /* Looping var */
1677 _cups_ps_obj_t
*obj
; /* Current object on stack */
1680 for (obj
= st
->objs
, c
= st
->num_objs
; c
> 0; c
--, obj
++)
1687 * End of "$Id: interpret.c 7852 2008-08-21 04:19:45Z mike $".