2 * "$Id: interpret.c 11848 2014-05-07 00:26:44Z msweet $"
4 * PPD command interpreter for CUPS.
6 * Copyright 2007-2014 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 * Include necessary headers...
22 #include <cups/raster-private.h>
26 * Stack values for the PostScript mini-interpreter...
48 CUPS_PS_SETPAGEDEVICE
,
55 _cups_ps_type_t type
; /* Object type */
58 int boolean
; /* Boolean value */
59 char name
[64]; /* Name value */
60 double number
; /* Number value */
61 char other
[64]; /* Other operator */
62 char string
[64]; /* Sring value */
68 int num_objs
, /* Number of objects on stack */
69 alloc_objs
; /* Number of allocated objects */
70 _cups_ps_obj_t
*objs
; /* Objects in stack */
78 static int cleartomark_stack(_cups_ps_stack_t
*st
);
79 static int copy_stack(_cups_ps_stack_t
*st
, int count
);
80 static void delete_stack(_cups_ps_stack_t
*st
);
81 static void error_object(_cups_ps_obj_t
*obj
);
82 static void error_stack(_cups_ps_stack_t
*st
, const char *title
);
83 static _cups_ps_obj_t
*index_stack(_cups_ps_stack_t
*st
, int n
);
84 static _cups_ps_stack_t
*new_stack(void);
85 static _cups_ps_obj_t
*pop_stack(_cups_ps_stack_t
*st
);
86 static _cups_ps_obj_t
*push_stack(_cups_ps_stack_t
*st
,
88 static int roll_stack(_cups_ps_stack_t
*st
, int c
, int s
);
89 static _cups_ps_obj_t
*scan_ps(_cups_ps_stack_t
*st
, char **ptr
);
90 static int setpagedevice(_cups_ps_stack_t
*st
,
91 cups_page_header2_t
*h
,
94 static void DEBUG_object(_cups_ps_obj_t
*obj
);
95 static void DEBUG_stack(_cups_ps_stack_t
*st
);
100 * 'cupsRasterInterpretPPD()' - Interpret PPD commands to create a page header.
102 * This function is used by raster image processing (RIP) filters like
103 * cgpdftoraster and imagetoraster when writing CUPS raster data for a page.
104 * It is not used by raster printer driver filters which only read CUPS
108 * @code cupsRasterInterpretPPD@ does not mark the options in the PPD using
109 * the "num_options" and "options" arguments. Instead, mark the options with
110 * @code cupsMarkOptions@ and @code ppdMarkOption@ prior to calling it -
111 * this allows for per-page options without manipulating the options array.
113 * The "func" argument specifies an optional callback function that is
114 * called prior to the computation of the final raster data. The function
115 * can make changes to the @link cups_page_header2_t@ data as needed to use a
116 * supported raster format and then returns 0 on success and -1 if the
117 * requested attributes cannot be supported.
120 * @code cupsRasterInterpretPPD@ supports a subset of the PostScript language.
121 * Currently only the @code [@, @code ]@, @code <<@, @code >>@, @code {@,
122 * @code }@, @code cleartomark@, @code copy@, @code dup@, @code index@,
123 * @code pop@, @code roll@, @code setpagedevice@, and @code stopped@ operators
126 * @since CUPS 1.2/OS X 10.5@
129 int /* O - 0 on success, -1 on failure */
130 cupsRasterInterpretPPD(
131 cups_page_header2_t
*h
, /* O - Page header to create */
132 ppd_file_t
*ppd
, /* I - PPD file */
133 int num_options
, /* I - Number of options */
134 cups_option_t
*options
, /* I - Options */
135 cups_interpret_cb_t func
) /* I - Optional page header callback (@code NULL@ for none) */
137 int status
; /* Cummulative status */
138 char *code
; /* Code to run */
139 const char *val
; /* Option value */
140 ppd_size_t
*size
; /* Current size */
141 float left
, /* Left position */
142 bottom
, /* Bottom position */
143 right
, /* Right position */
144 top
, /* Top position */
145 temp1
, temp2
; /* Temporary variables for swapping */
146 int preferred_bits
; /* Preferred bits per color */
150 * Range check input...
153 _cupsRasterClearError();
157 _cupsRasterAddError("Page header cannot be NULL!\n");
162 * Reset the page header to the defaults...
165 memset(h
, 0, sizeof(cups_page_header2_t
));
168 h
->PageSize
[0] = 612;
169 h
->PageSize
[1] = 792;
170 h
->HWResolution
[0] = 100;
171 h
->HWResolution
[1] = 100;
172 h
->cupsBitsPerColor
= 1;
173 h
->cupsColorOrder
= CUPS_ORDER_CHUNKED
;
174 h
->cupsColorSpace
= CUPS_CSPACE_K
;
175 h
->cupsBorderlessScalingFactor
= 1.0f
;
176 h
->cupsPageSize
[0] = 612.0f
;
177 h
->cupsPageSize
[1] = 792.0f
;
178 h
->cupsImagingBBox
[0] = 0.0f
;
179 h
->cupsImagingBBox
[1] = 0.0f
;
180 h
->cupsImagingBBox
[2] = 612.0f
;
181 h
->cupsImagingBBox
[3] = 792.0f
;
183 strlcpy(h
->cupsPageSizeName
, "Letter", sizeof(h
->cupsPageSizeName
));
187 * cupsInteger0 is also used for the total page count on OS X; set an
188 * uncommon default value so we can tell if the driver is using cupsInteger0.
191 h
->cupsInteger
[0] = 0x80000000;
192 #endif /* __APPLE__ */
195 * Apply patches and options to the page header...
204 * Apply any patch code (used to override the defaults...)
208 status
|= _cupsRasterExecPS(h
, &preferred_bits
, ppd
->patches
);
211 * Then apply printer options in the proper order...
214 if ((code
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, 0.0)) != NULL
)
216 status
|= _cupsRasterExecPS(h
, &preferred_bits
, code
);
220 if ((code
= ppdEmitString(ppd
, PPD_ORDER_ANY
, 0.0)) != NULL
)
222 status
|= _cupsRasterExecPS(h
, &preferred_bits
, code
);
226 if ((code
= ppdEmitString(ppd
, PPD_ORDER_PROLOG
, 0.0)) != NULL
)
228 status
|= _cupsRasterExecPS(h
, &preferred_bits
, code
);
232 if ((code
= ppdEmitString(ppd
, PPD_ORDER_PAGE
, 0.0)) != NULL
)
234 status
|= _cupsRasterExecPS(h
, &preferred_bits
, code
);
240 * Allow option override for page scaling...
243 if ((val
= cupsGetOption("cupsBorderlessScalingFactor", num_options
,
246 double sc
= atof(val
); /* Scale factor */
248 if (sc
>= 0.1 && sc
<= 2.0)
249 h
->cupsBorderlessScalingFactor
= (float)sc
;
253 * Get the margins for the current size...
256 if ((size
= ppdPageSize(ppd
, NULL
)) != NULL
)
259 * Use the margins from the PPD file...
263 bottom
= size
->bottom
;
267 strlcpy(h
->cupsPageSizeName
, size
->name
, sizeof(h
->cupsPageSizeName
));
269 h
->cupsPageSize
[0] = size
->width
;
270 h
->cupsPageSize
[1] = size
->length
;
275 * Use the default margins...
285 * Handle orientation...
288 switch (h
->Orientation
)
295 case CUPS_ORIENT_90
:
296 temp1
= h
->cupsPageSize
[0];
297 h
->cupsPageSize
[0] = h
->cupsPageSize
[1];
298 h
->cupsPageSize
[1] = temp1
;
302 left
= h
->cupsPageSize
[0] - top
;
303 right
= h
->cupsPageSize
[0] - bottom
;
304 bottom
= h
->cupsPageSize
[1] - temp1
;
305 top
= h
->cupsPageSize
[1] - temp2
;
308 case CUPS_ORIENT_180
:
311 left
= h
->cupsPageSize
[0] - right
;
312 right
= h
->cupsPageSize
[0] - temp1
;
313 bottom
= h
->cupsPageSize
[1] - top
;
314 top
= h
->cupsPageSize
[1] - temp2
;
317 case CUPS_ORIENT_270
:
318 temp1
= h
->cupsPageSize
[0];
319 h
->cupsPageSize
[0] = h
->cupsPageSize
[1];
320 h
->cupsPageSize
[1] = temp1
;
326 bottom
= h
->cupsPageSize
[1] - temp2
;
327 top
= h
->cupsPageSize
[1] - temp1
;
345 h
->PageSize
[0] = (unsigned)(h
->cupsPageSize
[0] *
346 h
->cupsBorderlessScalingFactor
);
347 h
->PageSize
[1] = (unsigned)(h
->cupsPageSize
[1] *
348 h
->cupsBorderlessScalingFactor
);
349 h
->Margins
[0] = (unsigned)(left
*
350 h
->cupsBorderlessScalingFactor
);
351 h
->Margins
[1] = (unsigned)(bottom
*
352 h
->cupsBorderlessScalingFactor
);
353 h
->ImagingBoundingBox
[0] = (unsigned)(left
*
354 h
->cupsBorderlessScalingFactor
);
355 h
->ImagingBoundingBox
[1] = (unsigned)(bottom
*
356 h
->cupsBorderlessScalingFactor
);
357 h
->ImagingBoundingBox
[2] = (unsigned)(right
*
358 h
->cupsBorderlessScalingFactor
);
359 h
->ImagingBoundingBox
[3] = (unsigned)(top
*
360 h
->cupsBorderlessScalingFactor
);
361 h
->cupsImagingBBox
[0] = (float)left
;
362 h
->cupsImagingBBox
[1] = (float)bottom
;
363 h
->cupsImagingBBox
[2] = (float)right
;
364 h
->cupsImagingBBox
[3] = (float)top
;
367 * Use the callback to validate the page header...
370 if (func
&& (*func
)(h
, preferred_bits
))
372 _cupsRasterAddError("Page header callback returned error.\n");
377 * Check parameters...
380 if (!h
->HWResolution
[0] || !h
->HWResolution
[1] ||
381 !h
->PageSize
[0] || !h
->PageSize
[1] ||
382 (h
->cupsBitsPerColor
!= 1 && h
->cupsBitsPerColor
!= 2 &&
383 h
->cupsBitsPerColor
!= 4 && h
->cupsBitsPerColor
!= 8 &&
384 h
->cupsBitsPerColor
!= 16) ||
385 h
->cupsBorderlessScalingFactor
< 0.1 ||
386 h
->cupsBorderlessScalingFactor
> 2.0)
388 _cupsRasterAddError("Page header uses unsupported values.\n");
393 * Compute the bitmap parameters...
396 h
->cupsWidth
= (unsigned)((right
- left
) * h
->cupsBorderlessScalingFactor
*
397 h
->HWResolution
[0] / 72.0f
+ 0.5f
);
398 h
->cupsHeight
= (unsigned)((top
- bottom
) * h
->cupsBorderlessScalingFactor
*
399 h
->HWResolution
[1] / 72.0f
+ 0.5f
);
401 switch (h
->cupsColorSpace
)
405 case CUPS_CSPACE_WHITE
:
406 case CUPS_CSPACE_GOLD
:
407 case CUPS_CSPACE_SILVER
:
408 case CUPS_CSPACE_SW
:
409 h
->cupsNumColors
= 1;
410 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
;
415 * Ensure that colorimetric colorspaces use at least 8 bits per
419 if (h
->cupsColorSpace
>= CUPS_CSPACE_CIEXYZ
&&
420 h
->cupsBitsPerColor
< 8)
421 h
->cupsBitsPerColor
= 8;
424 * Figure out the number of bits per pixel...
427 if (h
->cupsColorOrder
== CUPS_ORDER_CHUNKED
)
429 if (h
->cupsBitsPerColor
>= 8)
430 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
* 3;
432 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
* 4;
435 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
;
437 h
->cupsNumColors
= 3;
440 case CUPS_CSPACE_KCMYcm
:
441 if (h
->cupsBitsPerColor
== 1)
443 if (h
->cupsColorOrder
== CUPS_ORDER_CHUNKED
)
444 h
->cupsBitsPerPixel
= 8;
446 h
->cupsBitsPerPixel
= 1;
448 h
->cupsNumColors
= 6;
453 * Fall through to CMYK code...
456 case CUPS_CSPACE_RGBA
:
457 case CUPS_CSPACE_RGBW
:
458 case CUPS_CSPACE_CMYK
:
459 case CUPS_CSPACE_YMCK
:
460 case CUPS_CSPACE_KCMY
:
461 case CUPS_CSPACE_GMCK
:
462 case CUPS_CSPACE_GMCS
:
463 if (h
->cupsColorOrder
== CUPS_ORDER_CHUNKED
)
464 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
* 4;
466 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
;
468 h
->cupsNumColors
= 4;
471 case CUPS_CSPACE_DEVICE1
:
472 case CUPS_CSPACE_DEVICE2
:
473 case CUPS_CSPACE_DEVICE3
:
474 case CUPS_CSPACE_DEVICE4
:
475 case CUPS_CSPACE_DEVICE5
:
476 case CUPS_CSPACE_DEVICE6
:
477 case CUPS_CSPACE_DEVICE7
:
478 case CUPS_CSPACE_DEVICE8
:
479 case CUPS_CSPACE_DEVICE9
:
480 case CUPS_CSPACE_DEVICEA
:
481 case CUPS_CSPACE_DEVICEB
:
482 case CUPS_CSPACE_DEVICEC
:
483 case CUPS_CSPACE_DEVICED
:
484 case CUPS_CSPACE_DEVICEE
:
485 case CUPS_CSPACE_DEVICEF
:
486 h
->cupsNumColors
= h
->cupsColorSpace
- CUPS_CSPACE_DEVICE1
+ 1;
488 if (h
->cupsColorOrder
== CUPS_ORDER_CHUNKED
)
489 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
* h
->cupsNumColors
;
491 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
;
495 h
->cupsBytesPerLine
= (h
->cupsBitsPerPixel
* h
->cupsWidth
+ 7) / 8;
497 if (h
->cupsColorOrder
== CUPS_ORDER_BANDED
)
498 h
->cupsBytesPerLine
*= h
->cupsNumColors
;
505 * '_cupsRasterExecPS()' - Execute PostScript code to initialize a page header.
508 int /* O - 0 on success, -1 on error */
510 cups_page_header2_t
*h
, /* O - Page header */
511 int *preferred_bits
,/* O - Preferred bits per color */
512 const char *code
) /* I - PS code to execute */
514 int error
= 0; /* Error condition? */
515 _cups_ps_stack_t
*st
; /* PostScript value stack */
516 _cups_ps_obj_t
*obj
; /* Object from top of stack */
517 char *codecopy
, /* Copy of code */
518 *codeptr
; /* Pointer into copy of code */
521 DEBUG_printf(("_cupsRasterExecPS(h=%p, preferred_bits=%p, code=\"%s\")\n",
522 h
, preferred_bits
, code
));
525 * Copy the PostScript code and create a stack...
528 if ((codecopy
= strdup(code
)) == NULL
)
530 _cupsRasterAddError("Unable to duplicate code string.\n");
534 if ((st
= new_stack()) == NULL
)
536 _cupsRasterAddError("Unable to create stack.\n");
542 * Parse the PS string until we run out of data...
547 while ((obj
= scan_ps(st
, &codeptr
)) != NULL
)
550 DEBUG_printf(("_cupsRasterExecPS: Stack (%d objects)\n", st
->num_objs
));
557 /* Do nothing for regular values */
560 case CUPS_PS_CLEARTOMARK
:
563 if (cleartomark_stack(st
))
564 _cupsRasterAddError("cleartomark: Stack underflow!\n");
567 DEBUG_puts(" dup: ");
574 if ((obj
= pop_stack(st
)) != NULL
)
576 copy_stack(st
, (int)obj
->value
.number
);
579 DEBUG_puts("_cupsRasterExecPS: copy");
590 DEBUG_puts("_cupsRasterExecPS: dup");
597 if ((obj
= pop_stack(st
)) != NULL
)
599 index_stack(st
, (int)obj
->value
.number
);
602 DEBUG_puts("_cupsRasterExecPS: index");
613 DEBUG_puts("_cupsRasterExecPS: pop");
620 if ((obj
= pop_stack(st
)) != NULL
)
625 c
= (int)obj
->value
.number
;
627 if ((obj
= pop_stack(st
)) != NULL
)
629 roll_stack(st
, (int)obj
->value
.number
, c
);
632 DEBUG_puts("_cupsRasterExecPS: roll");
639 case CUPS_PS_SETPAGEDEVICE
:
641 setpagedevice(st
, h
, preferred_bits
);
644 DEBUG_puts("_cupsRasterExecPS: setpagedevice");
649 case CUPS_PS_START_PROC
:
650 case CUPS_PS_END_PROC
:
651 case CUPS_PS_STOPPED
:
656 _cupsRasterAddError("Unknown operator \"%s\"!\n", obj
->value
.other
);
658 DEBUG_printf(("_cupsRasterExecPS: Unknown operator \"%s\"!\n",
673 if (st
->num_objs
> 0)
675 error_stack(st
, "Stack not empty:");
678 DEBUG_puts("_cupsRasterExecPS: Stack not empty:");
698 * 'cleartomark_stack()' - Clear to the last mark ([) on the stack.
701 static int /* O - 0 on success, -1 on error */
702 cleartomark_stack(_cups_ps_stack_t
*st
) /* I - Stack */
704 _cups_ps_obj_t
*obj
; /* Current object on stack */
707 while ((obj
= pop_stack(st
)) != NULL
)
708 if (obj
->type
== CUPS_PS_START_ARRAY
)
711 return (obj
? 0 : -1);
716 * 'copy_stack()' - Copy the top N stack objects.
719 static int /* O - 0 on success, -1 on error */
720 copy_stack(_cups_ps_stack_t
*st
, /* I - Stack */
721 int c
) /* I - Number of objects to copy */
731 if ((n
= st
->num_objs
- c
) < 0)
736 if (!push_stack(st
, st
->objs
+ n
))
748 * 'delete_stack()' - Free memory used by a stack.
752 delete_stack(_cups_ps_stack_t
*st
) /* I - Stack */
760 * 'error_object()' - Add an object's value to the current error message.
764 error_object(_cups_ps_obj_t
*obj
) /* I - Object to add */
769 _cupsRasterAddError(" /%s", obj
->value
.name
);
772 case CUPS_PS_NUMBER
:
773 _cupsRasterAddError(" %g", obj
->value
.number
);
776 case CUPS_PS_STRING
:
777 _cupsRasterAddError(" (%s)", obj
->value
.string
);
780 case CUPS_PS_BOOLEAN
:
781 if (obj
->value
.boolean
)
782 _cupsRasterAddError(" true");
784 _cupsRasterAddError(" false");
788 _cupsRasterAddError(" null");
791 case CUPS_PS_START_ARRAY
:
792 _cupsRasterAddError(" [");
795 case CUPS_PS_END_ARRAY
:
796 _cupsRasterAddError(" ]");
799 case CUPS_PS_START_DICT
:
800 _cupsRasterAddError(" <<");
803 case CUPS_PS_END_DICT
:
804 _cupsRasterAddError(" >>");
807 case CUPS_PS_START_PROC
:
808 _cupsRasterAddError(" {");
811 case CUPS_PS_END_PROC
:
812 _cupsRasterAddError(" }");
816 _cupsRasterAddError(" --copy--");
819 case CUPS_PS_CLEARTOMARK
:
820 _cupsRasterAddError(" --cleartomark--");
824 _cupsRasterAddError(" --dup--");
828 _cupsRasterAddError(" --index--");
832 _cupsRasterAddError(" --pop--");
836 _cupsRasterAddError(" --roll--");
839 case CUPS_PS_SETPAGEDEVICE
:
840 _cupsRasterAddError(" --setpagedevice--");
843 case CUPS_PS_STOPPED
:
844 _cupsRasterAddError(" --stopped--");
848 _cupsRasterAddError(" --%s--", obj
->value
.other
);
855 * 'error_stack()' - Add a stack to the current error message...
859 error_stack(_cups_ps_stack_t
*st
, /* I - Stack */
860 const char *title
) /* I - Title string */
862 int c
; /* Looping var */
863 _cups_ps_obj_t
*obj
; /* Current object on stack */
866 _cupsRasterAddError("%s", title
);
868 for (obj
= st
->objs
, c
= st
->num_objs
; c
> 0; c
--, obj
++)
871 _cupsRasterAddError("\n");
876 * 'index_stack()' - Copy the Nth value on the stack.
879 static _cups_ps_obj_t
* /* O - New object */
880 index_stack(_cups_ps_stack_t
*st
, /* I - Stack */
881 int n
) /* I - Object index */
883 if (n
< 0 || (n
= st
->num_objs
- n
- 1) < 0)
886 return (push_stack(st
, st
->objs
+ n
));
891 * 'new_stack()' - Create a new stack.
894 static _cups_ps_stack_t
* /* O - New stack */
897 _cups_ps_stack_t
*st
; /* New stack */
900 if ((st
= calloc(1, sizeof(_cups_ps_stack_t
))) == NULL
)
905 if ((st
->objs
= calloc(32, sizeof(_cups_ps_obj_t
))) == NULL
)
916 * 'pop_stock()' - Pop the top object off the stack.
919 static _cups_ps_obj_t
* /* O - Object */
920 pop_stack(_cups_ps_stack_t
*st
) /* I - Stack */
922 if (st
->num_objs
> 0)
926 return (st
->objs
+ st
->num_objs
);
934 * 'push_stack()' - Push an object on the stack.
937 static _cups_ps_obj_t
* /* O - New object */
938 push_stack(_cups_ps_stack_t
*st
, /* I - Stack */
939 _cups_ps_obj_t
*obj
) /* I - Object */
941 _cups_ps_obj_t
*temp
; /* New object */
944 if (st
->num_objs
>= st
->alloc_objs
)
948 st
->alloc_objs
+= 32;
950 if ((temp
= realloc(st
->objs
, (size_t)st
->alloc_objs
*
951 sizeof(_cups_ps_obj_t
))) == NULL
)
955 memset(temp
+ st
->num_objs
, 0, 32 * sizeof(_cups_ps_obj_t
));
958 temp
= st
->objs
+ st
->num_objs
;
961 memcpy(temp
, obj
, sizeof(_cups_ps_obj_t
));
968 * 'roll_stack()' - Rotate stack objects.
971 static int /* O - 0 on success, -1 on error */
972 roll_stack(_cups_ps_stack_t
*st
, /* I - Stack */
973 int c
, /* I - Number of objects */
974 int s
) /* I - Amount to shift */
976 _cups_ps_obj_t
*temp
; /* Temporary array of objects */
977 int n
; /* Index into array */
980 DEBUG_printf((" roll_stack(st=%p, s=%d, c=%d)\n", st
, s
, c
));
983 * Range check input...
991 if ((n
= st
->num_objs
- c
) < 0)
1000 * Copy N objects and move things around...
1011 if ((temp
= calloc((size_t)s
, sizeof(_cups_ps_obj_t
))) == NULL
)
1014 memcpy(temp
, st
->objs
+ n
, (size_t)s
* sizeof(_cups_ps_obj_t
));
1015 memmove(st
->objs
+ n
, st
->objs
+ n
+ s
, (size_t)(c
- s
) * sizeof(_cups_ps_obj_t
));
1016 memcpy(st
->objs
+ n
+ c
- s
, temp
, (size_t)s
* sizeof(_cups_ps_obj_t
));
1024 if ((temp
= calloc((size_t)s
, sizeof(_cups_ps_obj_t
))) == NULL
)
1027 memcpy(temp
, st
->objs
+ n
+ c
- s
, (size_t)s
* sizeof(_cups_ps_obj_t
));
1028 memmove(st
->objs
+ n
+ s
, st
->objs
+ n
, (size_t)(c
- s
) * sizeof(_cups_ps_obj_t
));
1029 memcpy(st
->objs
+ n
, temp
, (size_t)s
* sizeof(_cups_ps_obj_t
));
1039 * 'scan_ps()' - Scan a string for the next PS object.
1042 static _cups_ps_obj_t
* /* O - New object or NULL on EOF */
1043 scan_ps(_cups_ps_stack_t
*st
, /* I - Stack */
1044 char **ptr
) /* IO - String pointer */
1046 _cups_ps_obj_t obj
; /* Current object */
1047 char *start
, /* Start of object */
1048 *cur
, /* Current position */
1049 *valptr
, /* Pointer into value string */
1050 *valend
; /* End of value string */
1051 int parens
; /* Parenthesis nesting level */
1055 * Skip leading whitespace...
1058 for (cur
= *ptr
; *cur
; cur
++)
1063 * Comment, skip to end of line...
1066 for (cur
++; *cur
&& *cur
!= '\n' && *cur
!= '\r'; cur
++);
1071 else if (!isspace(*cur
& 255))
1083 * See what we have...
1086 memset(&obj
, 0, sizeof(obj
));
1090 case '(' : /* (string) */
1091 obj
.type
= CUPS_PS_STRING
;
1094 for (cur
++, parens
= 1, valptr
= obj
.value
.string
,
1095 valend
= obj
.value
.string
+ sizeof(obj
.value
.string
) - 1;
1099 if (*cur
== ')' && parens
== 1)
1104 else if (*cur
== ')')
1107 if (valptr
>= valend
)
1117 * Decode escaped character...
1124 else if (*cur
== 'f')
1126 else if (*cur
== 'n')
1128 else if (*cur
== 'r')
1130 else if (*cur
== 't')
1132 else if (*cur
>= '0' && *cur
<= '7')
1134 int ch
= *cur
- '0';
1136 if (cur
[1] >= '0' && cur
[1] <= '7')
1139 ch
= (ch
<< 3) + *cur
- '0';
1142 if (cur
[1] >= '0' && cur
[1] <= '7')
1145 ch
= (ch
<< 3) + *cur
- '0';
1148 *valptr
++ = (char)ch
;
1150 else if (*cur
== '\r')
1155 else if (*cur
!= '\n')
1172 case '[' : /* Start array */
1173 obj
.type
= CUPS_PS_START_ARRAY
;
1177 case ']' : /* End array */
1178 obj
.type
= CUPS_PS_END_ARRAY
;
1182 case '<' : /* Start dictionary or hex string */
1185 obj
.type
= CUPS_PS_START_DICT
;
1190 obj
.type
= CUPS_PS_STRING
;
1193 for (cur
++, valptr
= obj
.value
.string
,
1194 valend
= obj
.value
.string
+ sizeof(obj
.value
.string
) - 1;
1198 int ch
; /* Current character */
1204 else if (valptr
>= valend
|| !isxdigit(*cur
& 255))
1210 if (*cur
>= '0' && *cur
<= '9')
1211 ch
= (*cur
- '0') << 4;
1213 ch
= (tolower(*cur
) - 'a' + 10) << 4;
1215 if (isxdigit(cur
[1] & 255))
1219 if (*cur
>= '0' && *cur
<= '9')
1222 ch
|= tolower(*cur
) - 'a' + 10;
1225 *valptr
++ = (char)ch
;
1238 case '>' : /* End dictionary? */
1241 obj
.type
= CUPS_PS_END_DICT
;
1246 obj
.type
= CUPS_PS_OTHER
;
1247 obj
.value
.other
[0] = *cur
;
1253 case '{' : /* Start procedure */
1254 obj
.type
= CUPS_PS_START_PROC
;
1258 case '}' : /* End procedure */
1259 obj
.type
= CUPS_PS_END_PROC
;
1263 case '-' : /* Possible number */
1265 if (!isdigit(cur
[1] & 255) && cur
[1] != '.')
1267 obj
.type
= CUPS_PS_OTHER
;
1268 obj
.value
.other
[0] = *cur
;
1274 case '0' : /* Number */
1285 obj
.type
= CUPS_PS_NUMBER
;
1288 for (cur
++; *cur
; cur
++)
1289 if (!isdigit(*cur
& 255))
1295 * Integer with radix...
1298 obj
.value
.number
= strtol(cur
+ 1, &cur
, atoi(start
));
1301 else if (strchr(".Ee()<>[]{}/%", *cur
) || isspace(*cur
& 255))
1304 * Integer or real number...
1307 obj
.value
.number
= _cupsStrScand(start
, &cur
, localeconv());
1313 default : /* Operator/variable name */
1318 obj
.type
= CUPS_PS_NAME
;
1319 valptr
= obj
.value
.name
;
1320 valend
= obj
.value
.name
+ sizeof(obj
.value
.name
) - 1;
1325 obj
.type
= CUPS_PS_OTHER
;
1326 valptr
= obj
.value
.other
;
1327 valend
= obj
.value
.other
+ sizeof(obj
.value
.other
) - 1;
1332 if (strchr("()<>[]{}/%", *cur
) || isspace(*cur
& 255))
1334 else if (valptr
< valend
)
1343 if (obj
.type
== CUPS_PS_OTHER
)
1345 if (!strcmp(obj
.value
.other
, "true"))
1347 obj
.type
= CUPS_PS_BOOLEAN
;
1348 obj
.value
.boolean
= 1;
1350 else if (!strcmp(obj
.value
.other
, "false"))
1352 obj
.type
= CUPS_PS_BOOLEAN
;
1353 obj
.value
.boolean
= 0;
1355 else if (!strcmp(obj
.value
.other
, "null"))
1356 obj
.type
= CUPS_PS_NULL
;
1357 else if (!strcmp(obj
.value
.other
, "cleartomark"))
1358 obj
.type
= CUPS_PS_CLEARTOMARK
;
1359 else if (!strcmp(obj
.value
.other
, "copy"))
1360 obj
.type
= CUPS_PS_COPY
;
1361 else if (!strcmp(obj
.value
.other
, "dup"))
1362 obj
.type
= CUPS_PS_DUP
;
1363 else if (!strcmp(obj
.value
.other
, "index"))
1364 obj
.type
= CUPS_PS_INDEX
;
1365 else if (!strcmp(obj
.value
.other
, "pop"))
1366 obj
.type
= CUPS_PS_POP
;
1367 else if (!strcmp(obj
.value
.other
, "roll"))
1368 obj
.type
= CUPS_PS_ROLL
;
1369 else if (!strcmp(obj
.value
.other
, "setpagedevice"))
1370 obj
.type
= CUPS_PS_SETPAGEDEVICE
;
1371 else if (!strcmp(obj
.value
.other
, "stopped"))
1372 obj
.type
= CUPS_PS_STOPPED
;
1378 * Save the current position in the string and return the new object...
1383 return (push_stack(st
, &obj
));
1388 * 'setpagedevice()' - Simulate the PostScript setpagedevice operator.
1391 static int /* O - 0 on success, -1 on error */
1393 _cups_ps_stack_t
*st
, /* I - Stack */
1394 cups_page_header2_t
*h
, /* O - Page header */
1395 int *preferred_bits
)/* O - Preferred bits per color */
1397 int i
; /* Index into array */
1398 _cups_ps_obj_t
*obj
, /* Current object */
1399 *end
; /* End of dictionary */
1400 const char *name
; /* Attribute name */
1404 * Make sure we have a dictionary on the stack...
1407 if (st
->num_objs
== 0)
1410 obj
= end
= st
->objs
+ st
->num_objs
- 1;
1412 if (obj
->type
!= CUPS_PS_END_DICT
)
1417 while (obj
> st
->objs
)
1419 if (obj
->type
== CUPS_PS_START_DICT
)
1429 * Found the start of the dictionary, empty the stack to this point...
1432 st
->num_objs
= (int)(obj
- st
->objs
);
1435 * Now pull /name and value pairs from the dictionary...
1438 DEBUG_puts("setpagedevice: Dictionary:");
1440 for (obj
++; obj
< end
; obj
++)
1446 if (obj
->type
!= CUPS_PS_NAME
)
1449 name
= obj
->value
.name
;
1453 DEBUG_printf(("setpagedevice: /%s ", name
));
1458 * Then grab the value...
1461 if (!strcmp(name
, "MediaClass") && obj
->type
== CUPS_PS_STRING
)
1462 strlcpy(h
->MediaClass
, obj
->value
.string
, sizeof(h
->MediaClass
));
1463 else if (!strcmp(name
, "MediaColor") && obj
->type
== CUPS_PS_STRING
)
1464 strlcpy(h
->MediaColor
, obj
->value
.string
, sizeof(h
->MediaColor
));
1465 else if (!strcmp(name
, "MediaType") && obj
->type
== CUPS_PS_STRING
)
1466 strlcpy(h
->MediaType
, obj
->value
.string
, sizeof(h
->MediaType
));
1467 else if (!strcmp(name
, "OutputType") && obj
->type
== CUPS_PS_STRING
)
1468 strlcpy(h
->OutputType
, obj
->value
.string
, sizeof(h
->OutputType
));
1469 else if (!strcmp(name
, "AdvanceDistance") && obj
->type
== CUPS_PS_NUMBER
)
1470 h
->AdvanceDistance
= (unsigned)obj
->value
.number
;
1471 else if (!strcmp(name
, "AdvanceMedia") && obj
->type
== CUPS_PS_NUMBER
)
1472 h
->AdvanceMedia
= (unsigned)obj
->value
.number
;
1473 else if (!strcmp(name
, "Collate") && obj
->type
== CUPS_PS_BOOLEAN
)
1474 h
->Collate
= (unsigned)obj
->value
.boolean
;
1475 else if (!strcmp(name
, "CutMedia") && obj
->type
== CUPS_PS_NUMBER
)
1476 h
->CutMedia
= (cups_cut_t
)(unsigned)obj
->value
.number
;
1477 else if (!strcmp(name
, "Duplex") && obj
->type
== CUPS_PS_BOOLEAN
)
1478 h
->Duplex
= (unsigned)obj
->value
.boolean
;
1479 else if (!strcmp(name
, "HWResolution") && obj
->type
== CUPS_PS_START_ARRAY
)
1481 if (obj
[1].type
== CUPS_PS_NUMBER
&& obj
[2].type
== CUPS_PS_NUMBER
&&
1482 obj
[3].type
== CUPS_PS_END_ARRAY
)
1484 h
->HWResolution
[0] = (unsigned)obj
[1].value
.number
;
1485 h
->HWResolution
[1] = (unsigned)obj
[2].value
.number
;
1491 else if (!strcmp(name
, "InsertSheet") && obj
->type
== CUPS_PS_BOOLEAN
)
1492 h
->InsertSheet
= (unsigned)obj
->value
.boolean
;
1493 else if (!strcmp(name
, "Jog") && obj
->type
== CUPS_PS_NUMBER
)
1494 h
->Jog
= (unsigned)obj
->value
.number
;
1495 else if (!strcmp(name
, "LeadingEdge") && obj
->type
== CUPS_PS_NUMBER
)
1496 h
->LeadingEdge
= (unsigned)obj
->value
.number
;
1497 else if (!strcmp(name
, "ManualFeed") && obj
->type
== CUPS_PS_BOOLEAN
)
1498 h
->ManualFeed
= (unsigned)obj
->value
.boolean
;
1499 else if ((!strcmp(name
, "cupsMediaPosition") ||
1500 !strcmp(name
, "MediaPosition")) && obj
->type
== CUPS_PS_NUMBER
)
1503 * cupsMediaPosition is supported for backwards compatibility only.
1504 * We added it back in the Ghostscript 5.50 days to work around a
1505 * bug in Ghostscript WRT handling of MediaPosition and setpagedevice.
1507 * All new development should set MediaPosition...
1510 h
->MediaPosition
= (unsigned)obj
->value
.number
;
1512 else if (!strcmp(name
, "MediaWeight") && obj
->type
== CUPS_PS_NUMBER
)
1513 h
->MediaWeight
= (unsigned)obj
->value
.number
;
1514 else if (!strcmp(name
, "MirrorPrint") && obj
->type
== CUPS_PS_BOOLEAN
)
1515 h
->MirrorPrint
= (unsigned)obj
->value
.boolean
;
1516 else if (!strcmp(name
, "NegativePrint") && obj
->type
== CUPS_PS_BOOLEAN
)
1517 h
->NegativePrint
= (unsigned)obj
->value
.boolean
;
1518 else if (!strcmp(name
, "NumCopies") && obj
->type
== CUPS_PS_NUMBER
)
1519 h
->NumCopies
= (unsigned)obj
->value
.number
;
1520 else if (!strcmp(name
, "Orientation") && obj
->type
== CUPS_PS_NUMBER
)
1521 h
->Orientation
= (unsigned)obj
->value
.number
;
1522 else if (!strcmp(name
, "OutputFaceUp") && obj
->type
== CUPS_PS_BOOLEAN
)
1523 h
->OutputFaceUp
= (unsigned)obj
->value
.boolean
;
1524 else if (!strcmp(name
, "PageSize") && obj
->type
== CUPS_PS_START_ARRAY
)
1526 if (obj
[1].type
== CUPS_PS_NUMBER
&& obj
[2].type
== CUPS_PS_NUMBER
&&
1527 obj
[3].type
== CUPS_PS_END_ARRAY
)
1529 h
->cupsPageSize
[0] = (float)obj
[1].value
.number
;
1530 h
->cupsPageSize
[1] = (float)obj
[2].value
.number
;
1532 h
->PageSize
[0] = (unsigned)obj
[1].value
.number
;
1533 h
->PageSize
[1] = (unsigned)obj
[2].value
.number
;
1540 else if (!strcmp(name
, "Separations") && obj
->type
== CUPS_PS_BOOLEAN
)
1541 h
->Separations
= (unsigned)obj
->value
.boolean
;
1542 else if (!strcmp(name
, "TraySwitch") && obj
->type
== CUPS_PS_BOOLEAN
)
1543 h
->TraySwitch
= (unsigned)obj
->value
.boolean
;
1544 else if (!strcmp(name
, "Tumble") && obj
->type
== CUPS_PS_BOOLEAN
)
1545 h
->Tumble
= (unsigned)obj
->value
.boolean
;
1546 else if (!strcmp(name
, "cupsMediaType") && obj
->type
== CUPS_PS_NUMBER
)
1547 h
->cupsMediaType
= (unsigned)obj
->value
.number
;
1548 else if (!strcmp(name
, "cupsBitsPerColor") && obj
->type
== CUPS_PS_NUMBER
)
1549 h
->cupsBitsPerColor
= (unsigned)obj
->value
.number
;
1550 else if (!strcmp(name
, "cupsPreferredBitsPerColor") &&
1551 obj
->type
== CUPS_PS_NUMBER
)
1552 *preferred_bits
= (int)obj
->value
.number
;
1553 else if (!strcmp(name
, "cupsColorOrder") && obj
->type
== CUPS_PS_NUMBER
)
1554 h
->cupsColorOrder
= (cups_order_t
)(unsigned)obj
->value
.number
;
1555 else if (!strcmp(name
, "cupsColorSpace") && obj
->type
== CUPS_PS_NUMBER
)
1556 h
->cupsColorSpace
= (cups_cspace_t
)(unsigned)obj
->value
.number
;
1557 else if (!strcmp(name
, "cupsCompression") && obj
->type
== CUPS_PS_NUMBER
)
1558 h
->cupsCompression
= (unsigned)obj
->value
.number
;
1559 else if (!strcmp(name
, "cupsRowCount") && obj
->type
== CUPS_PS_NUMBER
)
1560 h
->cupsRowCount
= (unsigned)obj
->value
.number
;
1561 else if (!strcmp(name
, "cupsRowFeed") && obj
->type
== CUPS_PS_NUMBER
)
1562 h
->cupsRowFeed
= (unsigned)obj
->value
.number
;
1563 else if (!strcmp(name
, "cupsRowStep") && obj
->type
== CUPS_PS_NUMBER
)
1564 h
->cupsRowStep
= (unsigned)obj
->value
.number
;
1565 else if (!strcmp(name
, "cupsBorderlessScalingFactor") &&
1566 obj
->type
== CUPS_PS_NUMBER
)
1567 h
->cupsBorderlessScalingFactor
= (float)obj
->value
.number
;
1568 else if (!strncmp(name
, "cupsInteger", 11) && obj
->type
== CUPS_PS_NUMBER
)
1570 if ((i
= atoi(name
+ 11)) < 0 || i
> 15)
1573 h
->cupsInteger
[i
] = (unsigned)obj
->value
.number
;
1575 else if (!strncmp(name
, "cupsReal", 8) && obj
->type
== CUPS_PS_NUMBER
)
1577 if ((i
= atoi(name
+ 8)) < 0 || i
> 15)
1580 h
->cupsReal
[i
] = (float)obj
->value
.number
;
1582 else if (!strncmp(name
, "cupsString", 10) && obj
->type
== CUPS_PS_STRING
)
1584 if ((i
= atoi(name
+ 10)) < 0 || i
> 15)
1587 strlcpy(h
->cupsString
[i
], obj
->value
.string
, sizeof(h
->cupsString
[i
]));
1589 else if (!strcmp(name
, "cupsMarkerType") && obj
->type
== CUPS_PS_STRING
)
1590 strlcpy(h
->cupsMarkerType
, obj
->value
.string
, sizeof(h
->cupsMarkerType
));
1591 else if (!strcmp(name
, "cupsPageSizeName") && obj
->type
== CUPS_PS_STRING
)
1592 strlcpy(h
->cupsPageSizeName
, obj
->value
.string
,
1593 sizeof(h
->cupsPageSizeName
));
1594 else if (!strcmp(name
, "cupsRenderingIntent") &&
1595 obj
->type
== CUPS_PS_STRING
)
1596 strlcpy(h
->cupsRenderingIntent
, obj
->value
.string
,
1597 sizeof(h
->cupsRenderingIntent
));
1601 * Ignore unknown name+value...
1604 DEBUG_printf((" Unknown name (\"%s\") or value...\n", name
));
1606 while (obj
[1].type
!= CUPS_PS_NAME
&& obj
< end
)
1617 * 'DEBUG_object()' - Print an object's value...
1621 DEBUG_object(_cups_ps_obj_t
*obj
) /* I - Object to print */
1626 DEBUG_printf(("/%s\n", obj
->value
.name
));
1629 case CUPS_PS_NUMBER
:
1630 DEBUG_printf(("%g\n", obj
->value
.number
));
1633 case CUPS_PS_STRING
:
1634 DEBUG_printf(("(%s)\n", obj
->value
.string
));
1637 case CUPS_PS_BOOLEAN
:
1638 if (obj
->value
.boolean
)
1641 DEBUG_puts("false");
1648 case CUPS_PS_START_ARRAY
:
1652 case CUPS_PS_END_ARRAY
:
1656 case CUPS_PS_START_DICT
:
1660 case CUPS_PS_END_DICT
:
1664 case CUPS_PS_START_PROC
:
1668 case CUPS_PS_END_PROC
:
1672 case CUPS_PS_CLEARTOMARK
:
1673 DEBUG_puts("--cleartomark--");
1677 DEBUG_puts("--copy--");
1681 DEBUG_puts("--dup--");
1684 case CUPS_PS_INDEX
:
1685 DEBUG_puts("--index--");
1689 DEBUG_puts("--pop--");
1693 DEBUG_puts("--roll--");
1696 case CUPS_PS_SETPAGEDEVICE
:
1697 DEBUG_puts("--setpagedevice--");
1700 case CUPS_PS_STOPPED
:
1701 DEBUG_puts("--stopped--");
1704 case CUPS_PS_OTHER
:
1705 DEBUG_printf(("--%s--\n", obj
->value
.other
));
1712 * 'DEBUG_stack()' - Print a stack...
1716 DEBUG_stack(_cups_ps_stack_t
*st
) /* I - Stack */
1718 int c
; /* Looping var */
1719 _cups_ps_obj_t
*obj
; /* Current object on stack */
1722 for (obj
= st
->objs
, c
= st
->num_objs
; c
> 0; c
--, obj
++)
1729 * End of "$Id: interpret.c 11848 2014-05-07 00:26:44Z msweet $".