2 * PPD command interpreter for CUPS.
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 1993-2007 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
12 * Include necessary headers...
15 #include <cups/raster-private.h>
16 #include <cups/ppd-private.h>
17 #include "debug-internal.h"
21 * Stack values for the PostScript mini-interpreter...
43 CUPS_PS_SETPAGEDEVICE
,
50 _cups_ps_type_t type
; /* Object type */
53 int boolean
; /* Boolean value */
54 char name
[64]; /* Name value */
55 double number
; /* Number value */
56 char other
[64]; /* Other operator */
57 char string
[64]; /* Sring value */
63 int num_objs
, /* Number of objects on stack */
64 alloc_objs
; /* Number of allocated objects */
65 _cups_ps_obj_t
*objs
; /* Objects in stack */
73 static int cleartomark_stack(_cups_ps_stack_t
*st
);
74 static int copy_stack(_cups_ps_stack_t
*st
, int count
);
75 static void delete_stack(_cups_ps_stack_t
*st
);
76 static void error_object(_cups_ps_obj_t
*obj
);
77 static void error_stack(_cups_ps_stack_t
*st
, const char *title
);
78 static _cups_ps_obj_t
*index_stack(_cups_ps_stack_t
*st
, int n
);
79 static _cups_ps_stack_t
*new_stack(void);
80 static _cups_ps_obj_t
*pop_stack(_cups_ps_stack_t
*st
);
81 static _cups_ps_obj_t
*push_stack(_cups_ps_stack_t
*st
,
83 static int roll_stack(_cups_ps_stack_t
*st
, int c
, int s
);
84 static _cups_ps_obj_t
*scan_ps(_cups_ps_stack_t
*st
, char **ptr
);
85 static int setpagedevice(_cups_ps_stack_t
*st
,
86 cups_page_header2_t
*h
,
89 static void DEBUG_object(const char *prefix
, _cups_ps_obj_t
*obj
);
90 static void DEBUG_stack(const char *prefix
, _cups_ps_stack_t
*st
);
95 * '_cupsRasterInterpretPPD()' - Interpret PPD commands to create a page header.
97 * This function is used by raster image processing (RIP) filters like
98 * cgpdftoraster and imagetoraster when writing CUPS raster data for a page.
99 * It is not used by raster printer driver filters which only read CUPS
103 * @code cupsRasterInterpretPPD@ does not mark the options in the PPD using
104 * the "num_options" and "options" arguments. Instead, mark the options with
105 * @code cupsMarkOptions@ and @code ppdMarkOption@ prior to calling it -
106 * this allows for per-page options without manipulating the options array.
108 * The "func" argument specifies an optional callback function that is
109 * called prior to the computation of the final raster data. The function
110 * can make changes to the @link cups_page_header2_t@ data as needed to use a
111 * supported raster format and then returns 0 on success and -1 if the
112 * requested attributes cannot be supported.
115 * @code cupsRasterInterpretPPD@ supports a subset of the PostScript language.
116 * Currently only the @code [@, @code ]@, @code <<@, @code >>@, @code {@,
117 * @code }@, @code cleartomark@, @code copy@, @code dup@, @code index@,
118 * @code pop@, @code roll@, @code setpagedevice@, and @code stopped@ operators
121 * @since CUPS 1.2/macOS 10.5@
124 int /* O - 0 on success, -1 on failure */
125 _cupsRasterInterpretPPD(
126 cups_page_header2_t
*h
, /* O - Page header to create */
127 ppd_file_t
*ppd
, /* I - PPD file */
128 int num_options
, /* I - Number of options */
129 cups_option_t
*options
, /* I - Options */
130 cups_interpret_cb_t func
) /* I - Optional page header callback (@code NULL@ for none) */
132 int status
; /* Cummulative status */
133 char *code
; /* Code to run */
134 const char *val
; /* Option value */
135 ppd_size_t
*size
; /* Current size */
136 float left
, /* Left position */
137 bottom
, /* Bottom position */
138 right
, /* Right position */
139 top
, /* Top position */
140 temp1
, temp2
; /* Temporary variables for swapping */
141 int preferred_bits
; /* Preferred bits per color */
145 * Range check input...
148 _cupsRasterClearError();
152 _cupsRasterAddError("Page header cannot be NULL!\n");
157 * Reset the page header to the defaults...
160 memset(h
, 0, sizeof(cups_page_header2_t
));
163 h
->PageSize
[0] = 612;
164 h
->PageSize
[1] = 792;
165 h
->HWResolution
[0] = 100;
166 h
->HWResolution
[1] = 100;
167 h
->cupsBitsPerColor
= 1;
168 h
->cupsColorOrder
= CUPS_ORDER_CHUNKED
;
169 h
->cupsColorSpace
= CUPS_CSPACE_K
;
170 h
->cupsBorderlessScalingFactor
= 1.0f
;
171 h
->cupsPageSize
[0] = 612.0f
;
172 h
->cupsPageSize
[1] = 792.0f
;
173 h
->cupsImagingBBox
[0] = 0.0f
;
174 h
->cupsImagingBBox
[1] = 0.0f
;
175 h
->cupsImagingBBox
[2] = 612.0f
;
176 h
->cupsImagingBBox
[3] = 792.0f
;
178 strlcpy(h
->cupsPageSizeName
, "Letter", sizeof(h
->cupsPageSizeName
));
182 * cupsInteger0 is also used for the total page count on macOS; set an
183 * uncommon default value so we can tell if the driver is using cupsInteger0.
186 h
->cupsInteger
[0] = 0x80000000;
187 #endif /* __APPLE__ */
190 * Apply patches and options to the page header...
199 * Apply any patch code (used to override the defaults...)
203 status
|= _cupsRasterExecPS(h
, &preferred_bits
, ppd
->patches
);
206 * Then apply printer options in the proper order...
209 if ((code
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, 0.0)) != NULL
)
211 status
|= _cupsRasterExecPS(h
, &preferred_bits
, code
);
215 if ((code
= ppdEmitString(ppd
, PPD_ORDER_ANY
, 0.0)) != NULL
)
217 status
|= _cupsRasterExecPS(h
, &preferred_bits
, code
);
221 if ((code
= ppdEmitString(ppd
, PPD_ORDER_PROLOG
, 0.0)) != NULL
)
223 status
|= _cupsRasterExecPS(h
, &preferred_bits
, code
);
227 if ((code
= ppdEmitString(ppd
, PPD_ORDER_PAGE
, 0.0)) != NULL
)
229 status
|= _cupsRasterExecPS(h
, &preferred_bits
, code
);
235 * Allow option override for page scaling...
238 if ((val
= cupsGetOption("cupsBorderlessScalingFactor", num_options
,
241 double sc
= atof(val
); /* Scale factor */
243 if (sc
>= 0.1 && sc
<= 2.0)
244 h
->cupsBorderlessScalingFactor
= (float)sc
;
248 * Get the margins for the current size...
251 if ((size
= ppdPageSize(ppd
, NULL
)) != NULL
)
254 * Use the margins from the PPD file...
258 bottom
= size
->bottom
;
262 strlcpy(h
->cupsPageSizeName
, size
->name
, sizeof(h
->cupsPageSizeName
));
264 h
->cupsPageSize
[0] = size
->width
;
265 h
->cupsPageSize
[1] = size
->length
;
270 * Use the default margins...
280 * Handle orientation...
283 switch (h
->Orientation
)
290 case CUPS_ORIENT_90
:
291 temp1
= h
->cupsPageSize
[0];
292 h
->cupsPageSize
[0] = h
->cupsPageSize
[1];
293 h
->cupsPageSize
[1] = temp1
;
297 left
= h
->cupsPageSize
[0] - top
;
298 right
= h
->cupsPageSize
[0] - bottom
;
299 bottom
= h
->cupsPageSize
[1] - temp1
;
300 top
= h
->cupsPageSize
[1] - temp2
;
303 case CUPS_ORIENT_180
:
306 left
= h
->cupsPageSize
[0] - right
;
307 right
= h
->cupsPageSize
[0] - temp1
;
308 bottom
= h
->cupsPageSize
[1] - top
;
309 top
= h
->cupsPageSize
[1] - temp2
;
312 case CUPS_ORIENT_270
:
313 temp1
= h
->cupsPageSize
[0];
314 h
->cupsPageSize
[0] = h
->cupsPageSize
[1];
315 h
->cupsPageSize
[1] = temp1
;
321 bottom
= h
->cupsPageSize
[1] - temp2
;
322 top
= h
->cupsPageSize
[1] - temp1
;
340 h
->PageSize
[0] = (unsigned)(h
->cupsPageSize
[0] *
341 h
->cupsBorderlessScalingFactor
);
342 h
->PageSize
[1] = (unsigned)(h
->cupsPageSize
[1] *
343 h
->cupsBorderlessScalingFactor
);
344 h
->Margins
[0] = (unsigned)(left
*
345 h
->cupsBorderlessScalingFactor
);
346 h
->Margins
[1] = (unsigned)(bottom
*
347 h
->cupsBorderlessScalingFactor
);
348 h
->ImagingBoundingBox
[0] = (unsigned)(left
*
349 h
->cupsBorderlessScalingFactor
);
350 h
->ImagingBoundingBox
[1] = (unsigned)(bottom
*
351 h
->cupsBorderlessScalingFactor
);
352 h
->ImagingBoundingBox
[2] = (unsigned)(right
*
353 h
->cupsBorderlessScalingFactor
);
354 h
->ImagingBoundingBox
[3] = (unsigned)(top
*
355 h
->cupsBorderlessScalingFactor
);
356 h
->cupsImagingBBox
[0] = (float)left
;
357 h
->cupsImagingBBox
[1] = (float)bottom
;
358 h
->cupsImagingBBox
[2] = (float)right
;
359 h
->cupsImagingBBox
[3] = (float)top
;
362 * Use the callback to validate the page header...
365 if (func
&& (*func
)(h
, preferred_bits
))
367 _cupsRasterAddError("Page header callback returned error.\n");
372 * Check parameters...
375 if (!h
->HWResolution
[0] || !h
->HWResolution
[1] ||
376 !h
->PageSize
[0] || !h
->PageSize
[1] ||
377 (h
->cupsBitsPerColor
!= 1 && h
->cupsBitsPerColor
!= 2 &&
378 h
->cupsBitsPerColor
!= 4 && h
->cupsBitsPerColor
!= 8 &&
379 h
->cupsBitsPerColor
!= 16) ||
380 h
->cupsBorderlessScalingFactor
< 0.1 ||
381 h
->cupsBorderlessScalingFactor
> 2.0)
383 _cupsRasterAddError("Page header uses unsupported values.\n");
388 * Compute the bitmap parameters...
391 h
->cupsWidth
= (unsigned)((right
- left
) * h
->cupsBorderlessScalingFactor
*
392 h
->HWResolution
[0] / 72.0f
+ 0.5f
);
393 h
->cupsHeight
= (unsigned)((top
- bottom
) * h
->cupsBorderlessScalingFactor
*
394 h
->HWResolution
[1] / 72.0f
+ 0.5f
);
396 switch (h
->cupsColorSpace
)
400 case CUPS_CSPACE_WHITE
:
401 case CUPS_CSPACE_GOLD
:
402 case CUPS_CSPACE_SILVER
:
403 case CUPS_CSPACE_SW
:
404 h
->cupsNumColors
= 1;
405 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
;
410 * Ensure that colorimetric colorspaces use at least 8 bits per
414 if (h
->cupsColorSpace
>= CUPS_CSPACE_CIEXYZ
&&
415 h
->cupsBitsPerColor
< 8)
416 h
->cupsBitsPerColor
= 8;
419 * Figure out the number of bits per pixel...
422 if (h
->cupsColorOrder
== CUPS_ORDER_CHUNKED
)
424 if (h
->cupsBitsPerColor
>= 8)
425 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
* 3;
427 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
* 4;
430 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
;
432 h
->cupsNumColors
= 3;
435 case CUPS_CSPACE_KCMYcm
:
436 if (h
->cupsBitsPerColor
== 1)
438 if (h
->cupsColorOrder
== CUPS_ORDER_CHUNKED
)
439 h
->cupsBitsPerPixel
= 8;
441 h
->cupsBitsPerPixel
= 1;
443 h
->cupsNumColors
= 6;
448 * Fall through to CMYK code...
451 case CUPS_CSPACE_RGBA
:
452 case CUPS_CSPACE_RGBW
:
453 case CUPS_CSPACE_CMYK
:
454 case CUPS_CSPACE_YMCK
:
455 case CUPS_CSPACE_KCMY
:
456 case CUPS_CSPACE_GMCK
:
457 case CUPS_CSPACE_GMCS
:
458 if (h
->cupsColorOrder
== CUPS_ORDER_CHUNKED
)
459 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
* 4;
461 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
;
463 h
->cupsNumColors
= 4;
466 case CUPS_CSPACE_DEVICE1
:
467 case CUPS_CSPACE_DEVICE2
:
468 case CUPS_CSPACE_DEVICE3
:
469 case CUPS_CSPACE_DEVICE4
:
470 case CUPS_CSPACE_DEVICE5
:
471 case CUPS_CSPACE_DEVICE6
:
472 case CUPS_CSPACE_DEVICE7
:
473 case CUPS_CSPACE_DEVICE8
:
474 case CUPS_CSPACE_DEVICE9
:
475 case CUPS_CSPACE_DEVICEA
:
476 case CUPS_CSPACE_DEVICEB
:
477 case CUPS_CSPACE_DEVICEC
:
478 case CUPS_CSPACE_DEVICED
:
479 case CUPS_CSPACE_DEVICEE
:
480 case CUPS_CSPACE_DEVICEF
:
481 h
->cupsNumColors
= h
->cupsColorSpace
- CUPS_CSPACE_DEVICE1
+ 1;
483 if (h
->cupsColorOrder
== CUPS_ORDER_CHUNKED
)
484 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
* h
->cupsNumColors
;
486 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
;
490 h
->cupsBytesPerLine
= (h
->cupsBitsPerPixel
* h
->cupsWidth
+ 7) / 8;
492 if (h
->cupsColorOrder
== CUPS_ORDER_BANDED
)
493 h
->cupsBytesPerLine
*= h
->cupsNumColors
;
500 * '_cupsRasterExecPS()' - Execute PostScript code to initialize a page header.
503 int /* O - 0 on success, -1 on error */
505 cups_page_header2_t
*h
, /* O - Page header */
506 int *preferred_bits
,/* O - Preferred bits per color */
507 const char *code
) /* I - PS code to execute */
509 int error
= 0; /* Error condition? */
510 _cups_ps_stack_t
*st
; /* PostScript value stack */
511 _cups_ps_obj_t
*obj
; /* Object from top of stack */
512 char *codecopy
, /* Copy of code */
513 *codeptr
; /* Pointer into copy of code */
516 DEBUG_printf(("_cupsRasterExecPS(h=%p, preferred_bits=%p, code=\"%s\")\n",
517 h
, preferred_bits
, code
));
520 * Copy the PostScript code and create a stack...
523 if ((codecopy
= strdup(code
)) == NULL
)
525 _cupsRasterAddError("Unable to duplicate code string.\n");
529 if ((st
= new_stack()) == NULL
)
531 _cupsRasterAddError("Unable to create stack.\n");
537 * Parse the PS string until we run out of data...
542 while ((obj
= scan_ps(st
, &codeptr
)) != NULL
)
545 DEBUG_printf(("_cupsRasterExecPS: Stack (%d objects)", st
->num_objs
));
546 DEBUG_object("_cupsRasterExecPS", obj
);
552 /* Do nothing for regular values */
555 case CUPS_PS_CLEARTOMARK
:
558 if (cleartomark_stack(st
))
559 _cupsRasterAddError("cleartomark: Stack underflow.\n");
562 DEBUG_puts("1_cupsRasterExecPS: dup");
563 DEBUG_stack("_cupsRasterExecPS", st
);
569 if ((obj
= pop_stack(st
)) != NULL
)
571 copy_stack(st
, (int)obj
->value
.number
);
574 DEBUG_puts("_cupsRasterExecPS: copy");
575 DEBUG_stack("_cupsRasterExecPS", st
);
585 DEBUG_puts("_cupsRasterExecPS: dup");
586 DEBUG_stack("_cupsRasterExecPS", st
);
592 if ((obj
= pop_stack(st
)) != NULL
)
594 index_stack(st
, (int)obj
->value
.number
);
597 DEBUG_puts("_cupsRasterExecPS: index");
598 DEBUG_stack("_cupsRasterExecPS", st
);
608 DEBUG_puts("_cupsRasterExecPS: pop");
609 DEBUG_stack("_cupsRasterExecPS", st
);
615 if ((obj
= pop_stack(st
)) != NULL
)
620 c
= (int)obj
->value
.number
;
622 if ((obj
= pop_stack(st
)) != NULL
)
624 roll_stack(st
, (int)obj
->value
.number
, c
);
627 DEBUG_puts("_cupsRasterExecPS: roll");
628 DEBUG_stack("_cupsRasterExecPS", st
);
634 case CUPS_PS_SETPAGEDEVICE
:
636 setpagedevice(st
, h
, preferred_bits
);
639 DEBUG_puts("_cupsRasterExecPS: setpagedevice");
640 DEBUG_stack("_cupsRasterExecPS", st
);
644 case CUPS_PS_START_PROC
:
645 case CUPS_PS_END_PROC
:
646 case CUPS_PS_STOPPED
:
651 _cupsRasterAddError("Unknown operator \"%s\".\n", obj
->value
.other
);
653 DEBUG_printf(("_cupsRasterExecPS: Unknown operator \"%s\".", obj
->value
.other
));
667 if (st
->num_objs
> 0)
669 error_stack(st
, "Stack not empty:");
672 DEBUG_puts("_cupsRasterExecPS: Stack not empty");
673 DEBUG_stack("_cupsRasterExecPS", st
);
692 * 'cleartomark_stack()' - Clear to the last mark ([) on the stack.
695 static int /* O - 0 on success, -1 on error */
696 cleartomark_stack(_cups_ps_stack_t
*st
) /* I - Stack */
698 _cups_ps_obj_t
*obj
; /* Current object on stack */
701 while ((obj
= pop_stack(st
)) != NULL
)
702 if (obj
->type
== CUPS_PS_START_ARRAY
)
705 return (obj
? 0 : -1);
710 * 'copy_stack()' - Copy the top N stack objects.
713 static int /* O - 0 on success, -1 on error */
714 copy_stack(_cups_ps_stack_t
*st
, /* I - Stack */
715 int c
) /* I - Number of objects to copy */
725 if ((n
= st
->num_objs
- c
) < 0)
730 if (!push_stack(st
, st
->objs
+ n
))
742 * 'delete_stack()' - Free memory used by a stack.
746 delete_stack(_cups_ps_stack_t
*st
) /* I - Stack */
754 * 'error_object()' - Add an object's value to the current error message.
758 error_object(_cups_ps_obj_t
*obj
) /* I - Object to add */
763 _cupsRasterAddError(" /%s", obj
->value
.name
);
766 case CUPS_PS_NUMBER
:
767 _cupsRasterAddError(" %g", obj
->value
.number
);
770 case CUPS_PS_STRING
:
771 _cupsRasterAddError(" (%s)", obj
->value
.string
);
774 case CUPS_PS_BOOLEAN
:
775 if (obj
->value
.boolean
)
776 _cupsRasterAddError(" true");
778 _cupsRasterAddError(" false");
782 _cupsRasterAddError(" null");
785 case CUPS_PS_START_ARRAY
:
786 _cupsRasterAddError(" [");
789 case CUPS_PS_END_ARRAY
:
790 _cupsRasterAddError(" ]");
793 case CUPS_PS_START_DICT
:
794 _cupsRasterAddError(" <<");
797 case CUPS_PS_END_DICT
:
798 _cupsRasterAddError(" >>");
801 case CUPS_PS_START_PROC
:
802 _cupsRasterAddError(" {");
805 case CUPS_PS_END_PROC
:
806 _cupsRasterAddError(" }");
810 _cupsRasterAddError(" --copy--");
813 case CUPS_PS_CLEARTOMARK
:
814 _cupsRasterAddError(" --cleartomark--");
818 _cupsRasterAddError(" --dup--");
822 _cupsRasterAddError(" --index--");
826 _cupsRasterAddError(" --pop--");
830 _cupsRasterAddError(" --roll--");
833 case CUPS_PS_SETPAGEDEVICE
:
834 _cupsRasterAddError(" --setpagedevice--");
837 case CUPS_PS_STOPPED
:
838 _cupsRasterAddError(" --stopped--");
842 _cupsRasterAddError(" --%s--", obj
->value
.other
);
849 * 'error_stack()' - Add a stack to the current error message...
853 error_stack(_cups_ps_stack_t
*st
, /* I - Stack */
854 const char *title
) /* I - Title string */
856 int c
; /* Looping var */
857 _cups_ps_obj_t
*obj
; /* Current object on stack */
860 _cupsRasterAddError("%s", title
);
862 for (obj
= st
->objs
, c
= st
->num_objs
; c
> 0; c
--, obj
++)
865 _cupsRasterAddError("\n");
870 * 'index_stack()' - Copy the Nth value on the stack.
873 static _cups_ps_obj_t
* /* O - New object */
874 index_stack(_cups_ps_stack_t
*st
, /* I - Stack */
875 int n
) /* I - Object index */
877 if (n
< 0 || (n
= st
->num_objs
- n
- 1) < 0)
880 return (push_stack(st
, st
->objs
+ n
));
885 * 'new_stack()' - Create a new stack.
888 static _cups_ps_stack_t
* /* O - New stack */
891 _cups_ps_stack_t
*st
; /* New stack */
894 if ((st
= calloc(1, sizeof(_cups_ps_stack_t
))) == NULL
)
899 if ((st
->objs
= calloc(32, sizeof(_cups_ps_obj_t
))) == NULL
)
910 * 'pop_stock()' - Pop the top object off the stack.
913 static _cups_ps_obj_t
* /* O - Object */
914 pop_stack(_cups_ps_stack_t
*st
) /* I - Stack */
916 if (st
->num_objs
> 0)
920 return (st
->objs
+ st
->num_objs
);
928 * 'push_stack()' - Push an object on the stack.
931 static _cups_ps_obj_t
* /* O - New object */
932 push_stack(_cups_ps_stack_t
*st
, /* I - Stack */
933 _cups_ps_obj_t
*obj
) /* I - Object */
935 _cups_ps_obj_t
*temp
; /* New object */
938 if (st
->num_objs
>= st
->alloc_objs
)
942 st
->alloc_objs
+= 32;
944 if ((temp
= realloc(st
->objs
, (size_t)st
->alloc_objs
*
945 sizeof(_cups_ps_obj_t
))) == NULL
)
949 memset(temp
+ st
->num_objs
, 0, 32 * sizeof(_cups_ps_obj_t
));
952 temp
= st
->objs
+ st
->num_objs
;
955 memcpy(temp
, obj
, sizeof(_cups_ps_obj_t
));
962 * 'roll_stack()' - Rotate stack objects.
965 static int /* O - 0 on success, -1 on error */
966 roll_stack(_cups_ps_stack_t
*st
, /* I - Stack */
967 int c
, /* I - Number of objects */
968 int s
) /* I - Amount to shift */
970 _cups_ps_obj_t
*temp
; /* Temporary array of objects */
971 int n
; /* Index into array */
974 DEBUG_printf(("3roll_stack(st=%p, s=%d, c=%d)", st
, s
, c
));
977 * Range check input...
985 if ((n
= st
->num_objs
- c
) < 0)
994 * Copy N objects and move things around...
1005 if ((temp
= calloc((size_t)s
, sizeof(_cups_ps_obj_t
))) == NULL
)
1008 memcpy(temp
, st
->objs
+ n
, (size_t)s
* sizeof(_cups_ps_obj_t
));
1009 memmove(st
->objs
+ n
, st
->objs
+ n
+ s
, (size_t)(c
- s
) * sizeof(_cups_ps_obj_t
));
1010 memcpy(st
->objs
+ n
+ c
- s
, temp
, (size_t)s
* sizeof(_cups_ps_obj_t
));
1018 if ((temp
= calloc((size_t)s
, sizeof(_cups_ps_obj_t
))) == NULL
)
1021 memcpy(temp
, st
->objs
+ n
+ c
- s
, (size_t)s
* sizeof(_cups_ps_obj_t
));
1022 memmove(st
->objs
+ n
+ s
, st
->objs
+ n
, (size_t)(c
- s
) * sizeof(_cups_ps_obj_t
));
1023 memcpy(st
->objs
+ n
, temp
, (size_t)s
* sizeof(_cups_ps_obj_t
));
1033 * 'scan_ps()' - Scan a string for the next PS object.
1036 static _cups_ps_obj_t
* /* O - New object or NULL on EOF */
1037 scan_ps(_cups_ps_stack_t
*st
, /* I - Stack */
1038 char **ptr
) /* IO - String pointer */
1040 _cups_ps_obj_t obj
; /* Current object */
1041 char *start
, /* Start of object */
1042 *cur
, /* Current position */
1043 *valptr
, /* Pointer into value string */
1044 *valend
; /* End of value string */
1045 int parens
; /* Parenthesis nesting level */
1049 * Skip leading whitespace...
1052 for (cur
= *ptr
; *cur
; cur
++)
1057 * Comment, skip to end of line...
1060 for (cur
++; *cur
&& *cur
!= '\n' && *cur
!= '\r'; cur
++);
1065 else if (!isspace(*cur
& 255))
1077 * See what we have...
1080 memset(&obj
, 0, sizeof(obj
));
1084 case '(' : /* (string) */
1085 obj
.type
= CUPS_PS_STRING
;
1088 for (cur
++, parens
= 1, valptr
= obj
.value
.string
,
1089 valend
= obj
.value
.string
+ sizeof(obj
.value
.string
) - 1;
1093 if (*cur
== ')' && parens
== 1)
1098 else if (*cur
== ')')
1101 if (valptr
>= valend
)
1111 * Decode escaped character...
1118 else if (*cur
== 'f')
1120 else if (*cur
== 'n')
1122 else if (*cur
== 'r')
1124 else if (*cur
== 't')
1126 else if (*cur
>= '0' && *cur
<= '7')
1128 int ch
= *cur
- '0';
1130 if (cur
[1] >= '0' && cur
[1] <= '7')
1133 ch
= (ch
<< 3) + *cur
- '0';
1136 if (cur
[1] >= '0' && cur
[1] <= '7')
1139 ch
= (ch
<< 3) + *cur
- '0';
1142 *valptr
++ = (char)ch
;
1144 else if (*cur
== '\r')
1149 else if (*cur
!= '\n')
1166 case '[' : /* Start array */
1167 obj
.type
= CUPS_PS_START_ARRAY
;
1171 case ']' : /* End array */
1172 obj
.type
= CUPS_PS_END_ARRAY
;
1176 case '<' : /* Start dictionary or hex string */
1179 obj
.type
= CUPS_PS_START_DICT
;
1184 obj
.type
= CUPS_PS_STRING
;
1187 for (cur
++, valptr
= obj
.value
.string
,
1188 valend
= obj
.value
.string
+ sizeof(obj
.value
.string
) - 1;
1192 int ch
; /* Current character */
1198 else if (valptr
>= valend
|| !isxdigit(*cur
& 255))
1204 if (*cur
>= '0' && *cur
<= '9')
1205 ch
= (*cur
- '0') << 4;
1207 ch
= (tolower(*cur
) - 'a' + 10) << 4;
1209 if (isxdigit(cur
[1] & 255))
1213 if (*cur
>= '0' && *cur
<= '9')
1216 ch
|= tolower(*cur
) - 'a' + 10;
1219 *valptr
++ = (char)ch
;
1232 case '>' : /* End dictionary? */
1235 obj
.type
= CUPS_PS_END_DICT
;
1240 obj
.type
= CUPS_PS_OTHER
;
1241 obj
.value
.other
[0] = *cur
;
1247 case '{' : /* Start procedure */
1248 obj
.type
= CUPS_PS_START_PROC
;
1252 case '}' : /* End procedure */
1253 obj
.type
= CUPS_PS_END_PROC
;
1257 case '-' : /* Possible number */
1259 if (!isdigit(cur
[1] & 255) && cur
[1] != '.')
1261 obj
.type
= CUPS_PS_OTHER
;
1262 obj
.value
.other
[0] = *cur
;
1268 case '0' : /* Number */
1279 obj
.type
= CUPS_PS_NUMBER
;
1282 for (cur
++; *cur
; cur
++)
1283 if (!isdigit(*cur
& 255))
1289 * Integer with radix...
1292 obj
.value
.number
= strtol(cur
+ 1, &cur
, atoi(start
));
1295 else if (strchr(".Ee()<>[]{}/%", *cur
) || isspace(*cur
& 255))
1298 * Integer or real number...
1301 obj
.value
.number
= _cupsStrScand(start
, &cur
, localeconv());
1307 default : /* Operator/variable name */
1312 obj
.type
= CUPS_PS_NAME
;
1313 valptr
= obj
.value
.name
;
1314 valend
= obj
.value
.name
+ sizeof(obj
.value
.name
) - 1;
1319 obj
.type
= CUPS_PS_OTHER
;
1320 valptr
= obj
.value
.other
;
1321 valend
= obj
.value
.other
+ sizeof(obj
.value
.other
) - 1;
1326 if (strchr("()<>[]{}/%", *cur
) || isspace(*cur
& 255))
1328 else if (valptr
< valend
)
1337 if (obj
.type
== CUPS_PS_OTHER
)
1339 if (!strcmp(obj
.value
.other
, "true"))
1341 obj
.type
= CUPS_PS_BOOLEAN
;
1342 obj
.value
.boolean
= 1;
1344 else if (!strcmp(obj
.value
.other
, "false"))
1346 obj
.type
= CUPS_PS_BOOLEAN
;
1347 obj
.value
.boolean
= 0;
1349 else if (!strcmp(obj
.value
.other
, "null"))
1350 obj
.type
= CUPS_PS_NULL
;
1351 else if (!strcmp(obj
.value
.other
, "cleartomark"))
1352 obj
.type
= CUPS_PS_CLEARTOMARK
;
1353 else if (!strcmp(obj
.value
.other
, "copy"))
1354 obj
.type
= CUPS_PS_COPY
;
1355 else if (!strcmp(obj
.value
.other
, "dup"))
1356 obj
.type
= CUPS_PS_DUP
;
1357 else if (!strcmp(obj
.value
.other
, "index"))
1358 obj
.type
= CUPS_PS_INDEX
;
1359 else if (!strcmp(obj
.value
.other
, "pop"))
1360 obj
.type
= CUPS_PS_POP
;
1361 else if (!strcmp(obj
.value
.other
, "roll"))
1362 obj
.type
= CUPS_PS_ROLL
;
1363 else if (!strcmp(obj
.value
.other
, "setpagedevice"))
1364 obj
.type
= CUPS_PS_SETPAGEDEVICE
;
1365 else if (!strcmp(obj
.value
.other
, "stopped"))
1366 obj
.type
= CUPS_PS_STOPPED
;
1372 * Save the current position in the string and return the new object...
1377 return (push_stack(st
, &obj
));
1382 * 'setpagedevice()' - Simulate the PostScript setpagedevice operator.
1385 static int /* O - 0 on success, -1 on error */
1387 _cups_ps_stack_t
*st
, /* I - Stack */
1388 cups_page_header2_t
*h
, /* O - Page header */
1389 int *preferred_bits
)/* O - Preferred bits per color */
1391 int i
; /* Index into array */
1392 _cups_ps_obj_t
*obj
, /* Current object */
1393 *end
; /* End of dictionary */
1394 const char *name
; /* Attribute name */
1398 * Make sure we have a dictionary on the stack...
1401 if (st
->num_objs
== 0)
1404 obj
= end
= st
->objs
+ st
->num_objs
- 1;
1406 if (obj
->type
!= CUPS_PS_END_DICT
)
1411 while (obj
> st
->objs
)
1413 if (obj
->type
== CUPS_PS_START_DICT
)
1423 * Found the start of the dictionary, empty the stack to this point...
1426 st
->num_objs
= (int)(obj
- st
->objs
);
1429 * Now pull /name and value pairs from the dictionary...
1432 DEBUG_puts("3setpagedevice: Dictionary:");
1434 for (obj
++; obj
< end
; obj
++)
1440 if (obj
->type
!= CUPS_PS_NAME
)
1443 name
= obj
->value
.name
;
1447 DEBUG_printf(("4setpagedevice: /%s ", name
));
1448 DEBUG_object("setpagedevice", obj
);
1452 * Then grab the value...
1455 if (!strcmp(name
, "MediaClass") && obj
->type
== CUPS_PS_STRING
)
1456 strlcpy(h
->MediaClass
, obj
->value
.string
, sizeof(h
->MediaClass
));
1457 else if (!strcmp(name
, "MediaColor") && obj
->type
== CUPS_PS_STRING
)
1458 strlcpy(h
->MediaColor
, obj
->value
.string
, sizeof(h
->MediaColor
));
1459 else if (!strcmp(name
, "MediaType") && obj
->type
== CUPS_PS_STRING
)
1460 strlcpy(h
->MediaType
, obj
->value
.string
, sizeof(h
->MediaType
));
1461 else if (!strcmp(name
, "OutputType") && obj
->type
== CUPS_PS_STRING
)
1462 strlcpy(h
->OutputType
, obj
->value
.string
, sizeof(h
->OutputType
));
1463 else if (!strcmp(name
, "AdvanceDistance") && obj
->type
== CUPS_PS_NUMBER
)
1464 h
->AdvanceDistance
= (unsigned)obj
->value
.number
;
1465 else if (!strcmp(name
, "AdvanceMedia") && obj
->type
== CUPS_PS_NUMBER
)
1466 h
->AdvanceMedia
= (unsigned)obj
->value
.number
;
1467 else if (!strcmp(name
, "Collate") && obj
->type
== CUPS_PS_BOOLEAN
)
1468 h
->Collate
= (unsigned)obj
->value
.boolean
;
1469 else if (!strcmp(name
, "CutMedia") && obj
->type
== CUPS_PS_NUMBER
)
1470 h
->CutMedia
= (cups_cut_t
)(unsigned)obj
->value
.number
;
1471 else if (!strcmp(name
, "Duplex") && obj
->type
== CUPS_PS_BOOLEAN
)
1472 h
->Duplex
= (unsigned)obj
->value
.boolean
;
1473 else if (!strcmp(name
, "HWResolution") && obj
->type
== CUPS_PS_START_ARRAY
)
1475 if (obj
[1].type
== CUPS_PS_NUMBER
&& obj
[2].type
== CUPS_PS_NUMBER
&&
1476 obj
[3].type
== CUPS_PS_END_ARRAY
)
1478 h
->HWResolution
[0] = (unsigned)obj
[1].value
.number
;
1479 h
->HWResolution
[1] = (unsigned)obj
[2].value
.number
;
1485 else if (!strcmp(name
, "InsertSheet") && obj
->type
== CUPS_PS_BOOLEAN
)
1486 h
->InsertSheet
= (unsigned)obj
->value
.boolean
;
1487 else if (!strcmp(name
, "Jog") && obj
->type
== CUPS_PS_NUMBER
)
1488 h
->Jog
= (unsigned)obj
->value
.number
;
1489 else if (!strcmp(name
, "LeadingEdge") && obj
->type
== CUPS_PS_NUMBER
)
1490 h
->LeadingEdge
= (unsigned)obj
->value
.number
;
1491 else if (!strcmp(name
, "ManualFeed") && obj
->type
== CUPS_PS_BOOLEAN
)
1492 h
->ManualFeed
= (unsigned)obj
->value
.boolean
;
1493 else if ((!strcmp(name
, "cupsMediaPosition") ||
1494 !strcmp(name
, "MediaPosition")) && obj
->type
== CUPS_PS_NUMBER
)
1497 * cupsMediaPosition is supported for backwards compatibility only.
1498 * We added it back in the Ghostscript 5.50 days to work around a
1499 * bug in Ghostscript WRT handling of MediaPosition and setpagedevice.
1501 * All new development should set MediaPosition...
1504 h
->MediaPosition
= (unsigned)obj
->value
.number
;
1506 else if (!strcmp(name
, "MediaWeight") && obj
->type
== CUPS_PS_NUMBER
)
1507 h
->MediaWeight
= (unsigned)obj
->value
.number
;
1508 else if (!strcmp(name
, "MirrorPrint") && obj
->type
== CUPS_PS_BOOLEAN
)
1509 h
->MirrorPrint
= (unsigned)obj
->value
.boolean
;
1510 else if (!strcmp(name
, "NegativePrint") && obj
->type
== CUPS_PS_BOOLEAN
)
1511 h
->NegativePrint
= (unsigned)obj
->value
.boolean
;
1512 else if (!strcmp(name
, "NumCopies") && obj
->type
== CUPS_PS_NUMBER
)
1513 h
->NumCopies
= (unsigned)obj
->value
.number
;
1514 else if (!strcmp(name
, "Orientation") && obj
->type
== CUPS_PS_NUMBER
)
1515 h
->Orientation
= (unsigned)obj
->value
.number
;
1516 else if (!strcmp(name
, "OutputFaceUp") && obj
->type
== CUPS_PS_BOOLEAN
)
1517 h
->OutputFaceUp
= (unsigned)obj
->value
.boolean
;
1518 else if (!strcmp(name
, "PageSize") && obj
->type
== CUPS_PS_START_ARRAY
)
1520 if (obj
[1].type
== CUPS_PS_NUMBER
&& obj
[2].type
== CUPS_PS_NUMBER
&&
1521 obj
[3].type
== CUPS_PS_END_ARRAY
)
1523 h
->cupsPageSize
[0] = (float)obj
[1].value
.number
;
1524 h
->cupsPageSize
[1] = (float)obj
[2].value
.number
;
1526 h
->PageSize
[0] = (unsigned)obj
[1].value
.number
;
1527 h
->PageSize
[1] = (unsigned)obj
[2].value
.number
;
1534 else if (!strcmp(name
, "Separations") && obj
->type
== CUPS_PS_BOOLEAN
)
1535 h
->Separations
= (unsigned)obj
->value
.boolean
;
1536 else if (!strcmp(name
, "TraySwitch") && obj
->type
== CUPS_PS_BOOLEAN
)
1537 h
->TraySwitch
= (unsigned)obj
->value
.boolean
;
1538 else if (!strcmp(name
, "Tumble") && obj
->type
== CUPS_PS_BOOLEAN
)
1539 h
->Tumble
= (unsigned)obj
->value
.boolean
;
1540 else if (!strcmp(name
, "cupsMediaType") && obj
->type
== CUPS_PS_NUMBER
)
1541 h
->cupsMediaType
= (unsigned)obj
->value
.number
;
1542 else if (!strcmp(name
, "cupsBitsPerColor") && obj
->type
== CUPS_PS_NUMBER
)
1543 h
->cupsBitsPerColor
= (unsigned)obj
->value
.number
;
1544 else if (!strcmp(name
, "cupsPreferredBitsPerColor") &&
1545 obj
->type
== CUPS_PS_NUMBER
)
1546 *preferred_bits
= (int)obj
->value
.number
;
1547 else if (!strcmp(name
, "cupsColorOrder") && obj
->type
== CUPS_PS_NUMBER
)
1548 h
->cupsColorOrder
= (cups_order_t
)(unsigned)obj
->value
.number
;
1549 else if (!strcmp(name
, "cupsColorSpace") && obj
->type
== CUPS_PS_NUMBER
)
1550 h
->cupsColorSpace
= (cups_cspace_t
)(unsigned)obj
->value
.number
;
1551 else if (!strcmp(name
, "cupsCompression") && obj
->type
== CUPS_PS_NUMBER
)
1552 h
->cupsCompression
= (unsigned)obj
->value
.number
;
1553 else if (!strcmp(name
, "cupsRowCount") && obj
->type
== CUPS_PS_NUMBER
)
1554 h
->cupsRowCount
= (unsigned)obj
->value
.number
;
1555 else if (!strcmp(name
, "cupsRowFeed") && obj
->type
== CUPS_PS_NUMBER
)
1556 h
->cupsRowFeed
= (unsigned)obj
->value
.number
;
1557 else if (!strcmp(name
, "cupsRowStep") && obj
->type
== CUPS_PS_NUMBER
)
1558 h
->cupsRowStep
= (unsigned)obj
->value
.number
;
1559 else if (!strcmp(name
, "cupsBorderlessScalingFactor") &&
1560 obj
->type
== CUPS_PS_NUMBER
)
1561 h
->cupsBorderlessScalingFactor
= (float)obj
->value
.number
;
1562 else if (!strncmp(name
, "cupsInteger", 11) && obj
->type
== CUPS_PS_NUMBER
)
1564 if ((i
= atoi(name
+ 11)) < 0 || i
> 15)
1567 h
->cupsInteger
[i
] = (unsigned)obj
->value
.number
;
1569 else if (!strncmp(name
, "cupsReal", 8) && obj
->type
== CUPS_PS_NUMBER
)
1571 if ((i
= atoi(name
+ 8)) < 0 || i
> 15)
1574 h
->cupsReal
[i
] = (float)obj
->value
.number
;
1576 else if (!strncmp(name
, "cupsString", 10) && obj
->type
== CUPS_PS_STRING
)
1578 if ((i
= atoi(name
+ 10)) < 0 || i
> 15)
1581 strlcpy(h
->cupsString
[i
], obj
->value
.string
, sizeof(h
->cupsString
[i
]));
1583 else if (!strcmp(name
, "cupsMarkerType") && obj
->type
== CUPS_PS_STRING
)
1584 strlcpy(h
->cupsMarkerType
, obj
->value
.string
, sizeof(h
->cupsMarkerType
));
1585 else if (!strcmp(name
, "cupsPageSizeName") && obj
->type
== CUPS_PS_STRING
)
1586 strlcpy(h
->cupsPageSizeName
, obj
->value
.string
,
1587 sizeof(h
->cupsPageSizeName
));
1588 else if (!strcmp(name
, "cupsRenderingIntent") &&
1589 obj
->type
== CUPS_PS_STRING
)
1590 strlcpy(h
->cupsRenderingIntent
, obj
->value
.string
,
1591 sizeof(h
->cupsRenderingIntent
));
1595 * Ignore unknown name+value...
1598 DEBUG_printf(("4setpagedevice: Unknown name (\"%s\") or value...\n", name
));
1600 while (obj
[1].type
!= CUPS_PS_NAME
&& obj
< end
)
1611 * 'DEBUG_object()' - Print an object's value...
1615 DEBUG_object(const char *prefix
, /* I - Prefix string */
1616 _cups_ps_obj_t
*obj
) /* I - Object to print */
1621 DEBUG_printf(("4%s: /%s\n", prefix
, obj
->value
.name
));
1624 case CUPS_PS_NUMBER
:
1625 DEBUG_printf(("4%s: %g\n", prefix
, obj
->value
.number
));
1628 case CUPS_PS_STRING
:
1629 DEBUG_printf(("4%s: (%s)\n", prefix
, obj
->value
.string
));
1632 case CUPS_PS_BOOLEAN
:
1633 if (obj
->value
.boolean
)
1634 DEBUG_printf(("4%s: true", prefix
));
1636 DEBUG_printf(("4%s: false", prefix
));
1640 DEBUG_printf(("4%s: null", prefix
));
1643 case CUPS_PS_START_ARRAY
:
1644 DEBUG_printf(("4%s: [", prefix
));
1647 case CUPS_PS_END_ARRAY
:
1648 DEBUG_printf(("4%s: ]", prefix
));
1651 case CUPS_PS_START_DICT
:
1652 DEBUG_printf(("4%s: <<", prefix
));
1655 case CUPS_PS_END_DICT
:
1656 DEBUG_printf(("4%s: >>", prefix
));
1659 case CUPS_PS_START_PROC
:
1660 DEBUG_printf(("4%s: {", prefix
));
1663 case CUPS_PS_END_PROC
:
1664 DEBUG_printf(("4%s: }", prefix
));
1667 case CUPS_PS_CLEARTOMARK
:
1668 DEBUG_printf(("4%s: --cleartomark--", prefix
));
1672 DEBUG_printf(("4%s: --copy--", prefix
));
1676 DEBUG_printf(("4%s: --dup--", prefix
));
1679 case CUPS_PS_INDEX
:
1680 DEBUG_printf(("4%s: --index--", prefix
));
1684 DEBUG_printf(("4%s: --pop--", prefix
));
1688 DEBUG_printf(("4%s: --roll--", prefix
));
1691 case CUPS_PS_SETPAGEDEVICE
:
1692 DEBUG_printf(("4%s: --setpagedevice--", prefix
));
1695 case CUPS_PS_STOPPED
:
1696 DEBUG_printf(("4%s: --stopped--", prefix
));
1699 case CUPS_PS_OTHER
:
1700 DEBUG_printf(("4%s: --%s--", prefix
, obj
->value
.other
));
1707 * 'DEBUG_stack()' - Print a stack...
1711 DEBUG_stack(const char *prefix
, /* I - Prefix string */
1712 _cups_ps_stack_t
*st
) /* I - Stack */
1714 int c
; /* Looping var */
1715 _cups_ps_obj_t
*obj
; /* Current object on stack */
1718 for (obj
= st
->objs
, c
= st
->num_objs
; c
> 0; c
--, obj
++)
1719 DEBUG_object(prefix
, obj
);