2 * PPD command interpreter for CUPS.
4 * Copyright 2007-2015 by Apple Inc.
5 * Copyright 1993-2007 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
20 #include <cups/raster-private.h>
25 * Stack values for the PostScript mini-interpreter...
47 CUPS_PS_SETPAGEDEVICE
,
54 _cups_ps_type_t type
; /* Object type */
57 int boolean
; /* Boolean value */
58 char name
[64]; /* Name value */
59 double number
; /* Number value */
60 char other
[64]; /* Other operator */
61 char string
[64]; /* Sring value */
67 int num_objs
, /* Number of objects on stack */
68 alloc_objs
; /* Number of allocated objects */
69 _cups_ps_obj_t
*objs
; /* Objects in stack */
77 static int cleartomark_stack(_cups_ps_stack_t
*st
);
78 static int copy_stack(_cups_ps_stack_t
*st
, int count
);
79 static void delete_stack(_cups_ps_stack_t
*st
);
80 static void error_object(_cups_ps_obj_t
*obj
);
81 static void error_stack(_cups_ps_stack_t
*st
, const char *title
);
82 static _cups_ps_obj_t
*index_stack(_cups_ps_stack_t
*st
, int n
);
83 static _cups_ps_stack_t
*new_stack(void);
84 static _cups_ps_obj_t
*pop_stack(_cups_ps_stack_t
*st
);
85 static _cups_ps_obj_t
*push_stack(_cups_ps_stack_t
*st
,
87 static int roll_stack(_cups_ps_stack_t
*st
, int c
, int s
);
88 static _cups_ps_obj_t
*scan_ps(_cups_ps_stack_t
*st
, char **ptr
);
89 static int setpagedevice(_cups_ps_stack_t
*st
,
90 cups_page_header2_t
*h
,
93 static void DEBUG_object(const char *prefix
, _cups_ps_obj_t
*obj
);
94 static void DEBUG_stack(const char *prefix
, _cups_ps_stack_t
*st
);
99 * 'cupsRasterInterpretPPD()' - Interpret PPD commands to create a page header.
101 * This function is used by raster image processing (RIP) filters like
102 * cgpdftoraster and imagetoraster when writing CUPS raster data for a page.
103 * It is not used by raster printer driver filters which only read CUPS
107 * @code cupsRasterInterpretPPD@ does not mark the options in the PPD using
108 * the "num_options" and "options" arguments. Instead, mark the options with
109 * @code cupsMarkOptions@ and @code ppdMarkOption@ prior to calling it -
110 * this allows for per-page options without manipulating the options array.
112 * The "func" argument specifies an optional callback function that is
113 * called prior to the computation of the final raster data. The function
114 * can make changes to the @link cups_page_header2_t@ data as needed to use a
115 * supported raster format and then returns 0 on success and -1 if the
116 * requested attributes cannot be supported.
119 * @code cupsRasterInterpretPPD@ supports a subset of the PostScript language.
120 * Currently only the @code [@, @code ]@, @code <<@, @code >>@, @code {@,
121 * @code }@, @code cleartomark@, @code copy@, @code dup@, @code index@,
122 * @code pop@, @code roll@, @code setpagedevice@, and @code stopped@ operators
125 * @since CUPS 1.2/macOS 10.5@
128 int /* O - 0 on success, -1 on failure */
129 cupsRasterInterpretPPD(
130 cups_page_header2_t
*h
, /* O - Page header to create */
131 ppd_file_t
*ppd
, /* I - PPD file */
132 int num_options
, /* I - Number of options */
133 cups_option_t
*options
, /* I - Options */
134 cups_interpret_cb_t func
) /* I - Optional page header callback (@code NULL@ for none) */
136 int status
; /* Cummulative status */
137 char *code
; /* Code to run */
138 const char *val
; /* Option value */
139 ppd_size_t
*size
; /* Current size */
140 float left
, /* Left position */
141 bottom
, /* Bottom position */
142 right
, /* Right position */
143 top
, /* Top position */
144 temp1
, temp2
; /* Temporary variables for swapping */
145 int preferred_bits
; /* Preferred bits per color */
149 * Range check input...
152 _cupsRasterClearError();
156 _cupsRasterAddError("Page header cannot be NULL!\n");
161 * Reset the page header to the defaults...
164 memset(h
, 0, sizeof(cups_page_header2_t
));
167 h
->PageSize
[0] = 612;
168 h
->PageSize
[1] = 792;
169 h
->HWResolution
[0] = 100;
170 h
->HWResolution
[1] = 100;
171 h
->cupsBitsPerColor
= 1;
172 h
->cupsColorOrder
= CUPS_ORDER_CHUNKED
;
173 h
->cupsColorSpace
= CUPS_CSPACE_K
;
174 h
->cupsBorderlessScalingFactor
= 1.0f
;
175 h
->cupsPageSize
[0] = 612.0f
;
176 h
->cupsPageSize
[1] = 792.0f
;
177 h
->cupsImagingBBox
[0] = 0.0f
;
178 h
->cupsImagingBBox
[1] = 0.0f
;
179 h
->cupsImagingBBox
[2] = 612.0f
;
180 h
->cupsImagingBBox
[3] = 792.0f
;
182 strlcpy(h
->cupsPageSizeName
, "Letter", sizeof(h
->cupsPageSizeName
));
186 * cupsInteger0 is also used for the total page count on macOS; set an
187 * uncommon default value so we can tell if the driver is using cupsInteger0.
190 h
->cupsInteger
[0] = 0x80000000;
191 #endif /* __APPLE__ */
194 * Apply patches and options to the page header...
203 * Apply any patch code (used to override the defaults...)
207 status
|= _cupsRasterExecPS(h
, &preferred_bits
, ppd
->patches
);
210 * Then apply printer options in the proper order...
213 if ((code
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, 0.0)) != NULL
)
215 status
|= _cupsRasterExecPS(h
, &preferred_bits
, code
);
219 if ((code
= ppdEmitString(ppd
, PPD_ORDER_ANY
, 0.0)) != NULL
)
221 status
|= _cupsRasterExecPS(h
, &preferred_bits
, code
);
225 if ((code
= ppdEmitString(ppd
, PPD_ORDER_PROLOG
, 0.0)) != NULL
)
227 status
|= _cupsRasterExecPS(h
, &preferred_bits
, code
);
231 if ((code
= ppdEmitString(ppd
, PPD_ORDER_PAGE
, 0.0)) != NULL
)
233 status
|= _cupsRasterExecPS(h
, &preferred_bits
, code
);
239 * Allow option override for page scaling...
242 if ((val
= cupsGetOption("cupsBorderlessScalingFactor", num_options
,
245 double sc
= atof(val
); /* Scale factor */
247 if (sc
>= 0.1 && sc
<= 2.0)
248 h
->cupsBorderlessScalingFactor
= (float)sc
;
252 * Get the margins for the current size...
255 if ((size
= ppdPageSize(ppd
, NULL
)) != NULL
)
258 * Use the margins from the PPD file...
262 bottom
= size
->bottom
;
266 strlcpy(h
->cupsPageSizeName
, size
->name
, sizeof(h
->cupsPageSizeName
));
268 h
->cupsPageSize
[0] = size
->width
;
269 h
->cupsPageSize
[1] = size
->length
;
274 * Use the default margins...
284 * Handle orientation...
287 switch (h
->Orientation
)
294 case CUPS_ORIENT_90
:
295 temp1
= h
->cupsPageSize
[0];
296 h
->cupsPageSize
[0] = h
->cupsPageSize
[1];
297 h
->cupsPageSize
[1] = temp1
;
301 left
= h
->cupsPageSize
[0] - top
;
302 right
= h
->cupsPageSize
[0] - bottom
;
303 bottom
= h
->cupsPageSize
[1] - temp1
;
304 top
= h
->cupsPageSize
[1] - temp2
;
307 case CUPS_ORIENT_180
:
310 left
= h
->cupsPageSize
[0] - right
;
311 right
= h
->cupsPageSize
[0] - temp1
;
312 bottom
= h
->cupsPageSize
[1] - top
;
313 top
= h
->cupsPageSize
[1] - temp2
;
316 case CUPS_ORIENT_270
:
317 temp1
= h
->cupsPageSize
[0];
318 h
->cupsPageSize
[0] = h
->cupsPageSize
[1];
319 h
->cupsPageSize
[1] = temp1
;
325 bottom
= h
->cupsPageSize
[1] - temp2
;
326 top
= h
->cupsPageSize
[1] - temp1
;
344 h
->PageSize
[0] = (unsigned)(h
->cupsPageSize
[0] *
345 h
->cupsBorderlessScalingFactor
);
346 h
->PageSize
[1] = (unsigned)(h
->cupsPageSize
[1] *
347 h
->cupsBorderlessScalingFactor
);
348 h
->Margins
[0] = (unsigned)(left
*
349 h
->cupsBorderlessScalingFactor
);
350 h
->Margins
[1] = (unsigned)(bottom
*
351 h
->cupsBorderlessScalingFactor
);
352 h
->ImagingBoundingBox
[0] = (unsigned)(left
*
353 h
->cupsBorderlessScalingFactor
);
354 h
->ImagingBoundingBox
[1] = (unsigned)(bottom
*
355 h
->cupsBorderlessScalingFactor
);
356 h
->ImagingBoundingBox
[2] = (unsigned)(right
*
357 h
->cupsBorderlessScalingFactor
);
358 h
->ImagingBoundingBox
[3] = (unsigned)(top
*
359 h
->cupsBorderlessScalingFactor
);
360 h
->cupsImagingBBox
[0] = (float)left
;
361 h
->cupsImagingBBox
[1] = (float)bottom
;
362 h
->cupsImagingBBox
[2] = (float)right
;
363 h
->cupsImagingBBox
[3] = (float)top
;
366 * Use the callback to validate the page header...
369 if (func
&& (*func
)(h
, preferred_bits
))
371 _cupsRasterAddError("Page header callback returned error.\n");
376 * Check parameters...
379 if (!h
->HWResolution
[0] || !h
->HWResolution
[1] ||
380 !h
->PageSize
[0] || !h
->PageSize
[1] ||
381 (h
->cupsBitsPerColor
!= 1 && h
->cupsBitsPerColor
!= 2 &&
382 h
->cupsBitsPerColor
!= 4 && h
->cupsBitsPerColor
!= 8 &&
383 h
->cupsBitsPerColor
!= 16) ||
384 h
->cupsBorderlessScalingFactor
< 0.1 ||
385 h
->cupsBorderlessScalingFactor
> 2.0)
387 _cupsRasterAddError("Page header uses unsupported values.\n");
392 * Compute the bitmap parameters...
395 h
->cupsWidth
= (unsigned)((right
- left
) * h
->cupsBorderlessScalingFactor
*
396 h
->HWResolution
[0] / 72.0f
+ 0.5f
);
397 h
->cupsHeight
= (unsigned)((top
- bottom
) * h
->cupsBorderlessScalingFactor
*
398 h
->HWResolution
[1] / 72.0f
+ 0.5f
);
400 switch (h
->cupsColorSpace
)
404 case CUPS_CSPACE_WHITE
:
405 case CUPS_CSPACE_GOLD
:
406 case CUPS_CSPACE_SILVER
:
407 case CUPS_CSPACE_SW
:
408 h
->cupsNumColors
= 1;
409 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
;
414 * Ensure that colorimetric colorspaces use at least 8 bits per
418 if (h
->cupsColorSpace
>= CUPS_CSPACE_CIEXYZ
&&
419 h
->cupsBitsPerColor
< 8)
420 h
->cupsBitsPerColor
= 8;
423 * Figure out the number of bits per pixel...
426 if (h
->cupsColorOrder
== CUPS_ORDER_CHUNKED
)
428 if (h
->cupsBitsPerColor
>= 8)
429 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
* 3;
431 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
* 4;
434 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
;
436 h
->cupsNumColors
= 3;
439 case CUPS_CSPACE_KCMYcm
:
440 if (h
->cupsBitsPerColor
== 1)
442 if (h
->cupsColorOrder
== CUPS_ORDER_CHUNKED
)
443 h
->cupsBitsPerPixel
= 8;
445 h
->cupsBitsPerPixel
= 1;
447 h
->cupsNumColors
= 6;
452 * Fall through to CMYK code...
455 case CUPS_CSPACE_RGBA
:
456 case CUPS_CSPACE_RGBW
:
457 case CUPS_CSPACE_CMYK
:
458 case CUPS_CSPACE_YMCK
:
459 case CUPS_CSPACE_KCMY
:
460 case CUPS_CSPACE_GMCK
:
461 case CUPS_CSPACE_GMCS
:
462 if (h
->cupsColorOrder
== CUPS_ORDER_CHUNKED
)
463 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
* 4;
465 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
;
467 h
->cupsNumColors
= 4;
470 case CUPS_CSPACE_DEVICE1
:
471 case CUPS_CSPACE_DEVICE2
:
472 case CUPS_CSPACE_DEVICE3
:
473 case CUPS_CSPACE_DEVICE4
:
474 case CUPS_CSPACE_DEVICE5
:
475 case CUPS_CSPACE_DEVICE6
:
476 case CUPS_CSPACE_DEVICE7
:
477 case CUPS_CSPACE_DEVICE8
:
478 case CUPS_CSPACE_DEVICE9
:
479 case CUPS_CSPACE_DEVICEA
:
480 case CUPS_CSPACE_DEVICEB
:
481 case CUPS_CSPACE_DEVICEC
:
482 case CUPS_CSPACE_DEVICED
:
483 case CUPS_CSPACE_DEVICEE
:
484 case CUPS_CSPACE_DEVICEF
:
485 h
->cupsNumColors
= h
->cupsColorSpace
- CUPS_CSPACE_DEVICE1
+ 1;
487 if (h
->cupsColorOrder
== CUPS_ORDER_CHUNKED
)
488 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
* h
->cupsNumColors
;
490 h
->cupsBitsPerPixel
= h
->cupsBitsPerColor
;
494 h
->cupsBytesPerLine
= (h
->cupsBitsPerPixel
* h
->cupsWidth
+ 7) / 8;
496 if (h
->cupsColorOrder
== CUPS_ORDER_BANDED
)
497 h
->cupsBytesPerLine
*= h
->cupsNumColors
;
504 * '_cupsRasterExecPS()' - Execute PostScript code to initialize a page header.
507 int /* O - 0 on success, -1 on error */
509 cups_page_header2_t
*h
, /* O - Page header */
510 int *preferred_bits
,/* O - Preferred bits per color */
511 const char *code
) /* I - PS code to execute */
513 int error
= 0; /* Error condition? */
514 _cups_ps_stack_t
*st
; /* PostScript value stack */
515 _cups_ps_obj_t
*obj
; /* Object from top of stack */
516 char *codecopy
, /* Copy of code */
517 *codeptr
; /* Pointer into copy of code */
520 DEBUG_printf(("_cupsRasterExecPS(h=%p, preferred_bits=%p, code=\"%s\")\n",
521 h
, preferred_bits
, code
));
524 * Copy the PostScript code and create a stack...
527 if ((codecopy
= strdup(code
)) == NULL
)
529 _cupsRasterAddError("Unable to duplicate code string.\n");
533 if ((st
= new_stack()) == NULL
)
535 _cupsRasterAddError("Unable to create stack.\n");
541 * Parse the PS string until we run out of data...
546 while ((obj
= scan_ps(st
, &codeptr
)) != NULL
)
549 DEBUG_printf(("_cupsRasterExecPS: Stack (%d objects)", st
->num_objs
));
550 DEBUG_object("_cupsRasterExecPS", obj
);
556 /* Do nothing for regular values */
559 case CUPS_PS_CLEARTOMARK
:
562 if (cleartomark_stack(st
))
563 _cupsRasterAddError("cleartomark: Stack underflow.\n");
566 DEBUG_puts("1_cupsRasterExecPS: dup");
567 DEBUG_stack("_cupsRasterExecPS", st
);
573 if ((obj
= pop_stack(st
)) != NULL
)
575 copy_stack(st
, (int)obj
->value
.number
);
578 DEBUG_puts("_cupsRasterExecPS: copy");
579 DEBUG_stack("_cupsRasterExecPS", st
);
589 DEBUG_puts("_cupsRasterExecPS: dup");
590 DEBUG_stack("_cupsRasterExecPS", st
);
596 if ((obj
= pop_stack(st
)) != NULL
)
598 index_stack(st
, (int)obj
->value
.number
);
601 DEBUG_puts("_cupsRasterExecPS: index");
602 DEBUG_stack("_cupsRasterExecPS", st
);
612 DEBUG_puts("_cupsRasterExecPS: pop");
613 DEBUG_stack("_cupsRasterExecPS", st
);
619 if ((obj
= pop_stack(st
)) != NULL
)
624 c
= (int)obj
->value
.number
;
626 if ((obj
= pop_stack(st
)) != NULL
)
628 roll_stack(st
, (int)obj
->value
.number
, c
);
631 DEBUG_puts("_cupsRasterExecPS: roll");
632 DEBUG_stack("_cupsRasterExecPS", st
);
638 case CUPS_PS_SETPAGEDEVICE
:
640 setpagedevice(st
, h
, preferred_bits
);
643 DEBUG_puts("_cupsRasterExecPS: setpagedevice");
644 DEBUG_stack("_cupsRasterExecPS", st
);
648 case CUPS_PS_START_PROC
:
649 case CUPS_PS_END_PROC
:
650 case CUPS_PS_STOPPED
:
655 _cupsRasterAddError("Unknown operator \"%s\".\n", obj
->value
.other
);
657 DEBUG_printf(("_cupsRasterExecPS: Unknown operator \"%s\".", obj
->value
.other
));
671 if (st
->num_objs
> 0)
673 error_stack(st
, "Stack not empty:");
676 DEBUG_puts("_cupsRasterExecPS: Stack not empty");
677 DEBUG_stack("_cupsRasterExecPS", st
);
696 * 'cleartomark_stack()' - Clear to the last mark ([) on the stack.
699 static int /* O - 0 on success, -1 on error */
700 cleartomark_stack(_cups_ps_stack_t
*st
) /* I - Stack */
702 _cups_ps_obj_t
*obj
; /* Current object on stack */
705 while ((obj
= pop_stack(st
)) != NULL
)
706 if (obj
->type
== CUPS_PS_START_ARRAY
)
709 return (obj
? 0 : -1);
714 * 'copy_stack()' - Copy the top N stack objects.
717 static int /* O - 0 on success, -1 on error */
718 copy_stack(_cups_ps_stack_t
*st
, /* I - Stack */
719 int c
) /* I - Number of objects to copy */
729 if ((n
= st
->num_objs
- c
) < 0)
734 if (!push_stack(st
, st
->objs
+ n
))
746 * 'delete_stack()' - Free memory used by a stack.
750 delete_stack(_cups_ps_stack_t
*st
) /* I - Stack */
758 * 'error_object()' - Add an object's value to the current error message.
762 error_object(_cups_ps_obj_t
*obj
) /* I - Object to add */
767 _cupsRasterAddError(" /%s", obj
->value
.name
);
770 case CUPS_PS_NUMBER
:
771 _cupsRasterAddError(" %g", obj
->value
.number
);
774 case CUPS_PS_STRING
:
775 _cupsRasterAddError(" (%s)", obj
->value
.string
);
778 case CUPS_PS_BOOLEAN
:
779 if (obj
->value
.boolean
)
780 _cupsRasterAddError(" true");
782 _cupsRasterAddError(" false");
786 _cupsRasterAddError(" null");
789 case CUPS_PS_START_ARRAY
:
790 _cupsRasterAddError(" [");
793 case CUPS_PS_END_ARRAY
:
794 _cupsRasterAddError(" ]");
797 case CUPS_PS_START_DICT
:
798 _cupsRasterAddError(" <<");
801 case CUPS_PS_END_DICT
:
802 _cupsRasterAddError(" >>");
805 case CUPS_PS_START_PROC
:
806 _cupsRasterAddError(" {");
809 case CUPS_PS_END_PROC
:
810 _cupsRasterAddError(" }");
814 _cupsRasterAddError(" --copy--");
817 case CUPS_PS_CLEARTOMARK
:
818 _cupsRasterAddError(" --cleartomark--");
822 _cupsRasterAddError(" --dup--");
826 _cupsRasterAddError(" --index--");
830 _cupsRasterAddError(" --pop--");
834 _cupsRasterAddError(" --roll--");
837 case CUPS_PS_SETPAGEDEVICE
:
838 _cupsRasterAddError(" --setpagedevice--");
841 case CUPS_PS_STOPPED
:
842 _cupsRasterAddError(" --stopped--");
846 _cupsRasterAddError(" --%s--", obj
->value
.other
);
853 * 'error_stack()' - Add a stack to the current error message...
857 error_stack(_cups_ps_stack_t
*st
, /* I - Stack */
858 const char *title
) /* I - Title string */
860 int c
; /* Looping var */
861 _cups_ps_obj_t
*obj
; /* Current object on stack */
864 _cupsRasterAddError("%s", title
);
866 for (obj
= st
->objs
, c
= st
->num_objs
; c
> 0; c
--, obj
++)
869 _cupsRasterAddError("\n");
874 * 'index_stack()' - Copy the Nth value on the stack.
877 static _cups_ps_obj_t
* /* O - New object */
878 index_stack(_cups_ps_stack_t
*st
, /* I - Stack */
879 int n
) /* I - Object index */
881 if (n
< 0 || (n
= st
->num_objs
- n
- 1) < 0)
884 return (push_stack(st
, st
->objs
+ n
));
889 * 'new_stack()' - Create a new stack.
892 static _cups_ps_stack_t
* /* O - New stack */
895 _cups_ps_stack_t
*st
; /* New stack */
898 if ((st
= calloc(1, sizeof(_cups_ps_stack_t
))) == NULL
)
903 if ((st
->objs
= calloc(32, sizeof(_cups_ps_obj_t
))) == NULL
)
914 * 'pop_stock()' - Pop the top object off the stack.
917 static _cups_ps_obj_t
* /* O - Object */
918 pop_stack(_cups_ps_stack_t
*st
) /* I - Stack */
920 if (st
->num_objs
> 0)
924 return (st
->objs
+ st
->num_objs
);
932 * 'push_stack()' - Push an object on the stack.
935 static _cups_ps_obj_t
* /* O - New object */
936 push_stack(_cups_ps_stack_t
*st
, /* I - Stack */
937 _cups_ps_obj_t
*obj
) /* I - Object */
939 _cups_ps_obj_t
*temp
; /* New object */
942 if (st
->num_objs
>= st
->alloc_objs
)
946 st
->alloc_objs
+= 32;
948 if ((temp
= realloc(st
->objs
, (size_t)st
->alloc_objs
*
949 sizeof(_cups_ps_obj_t
))) == NULL
)
953 memset(temp
+ st
->num_objs
, 0, 32 * sizeof(_cups_ps_obj_t
));
956 temp
= st
->objs
+ st
->num_objs
;
959 memcpy(temp
, obj
, sizeof(_cups_ps_obj_t
));
966 * 'roll_stack()' - Rotate stack objects.
969 static int /* O - 0 on success, -1 on error */
970 roll_stack(_cups_ps_stack_t
*st
, /* I - Stack */
971 int c
, /* I - Number of objects */
972 int s
) /* I - Amount to shift */
974 _cups_ps_obj_t
*temp
; /* Temporary array of objects */
975 int n
; /* Index into array */
978 DEBUG_printf(("3roll_stack(st=%p, s=%d, c=%d)", st
, s
, c
));
981 * Range check input...
989 if ((n
= st
->num_objs
- c
) < 0)
998 * Copy N objects and move things around...
1009 if ((temp
= calloc((size_t)s
, sizeof(_cups_ps_obj_t
))) == NULL
)
1012 memcpy(temp
, st
->objs
+ n
, (size_t)s
* sizeof(_cups_ps_obj_t
));
1013 memmove(st
->objs
+ n
, st
->objs
+ n
+ s
, (size_t)(c
- s
) * sizeof(_cups_ps_obj_t
));
1014 memcpy(st
->objs
+ n
+ c
- s
, temp
, (size_t)s
* sizeof(_cups_ps_obj_t
));
1022 if ((temp
= calloc((size_t)s
, sizeof(_cups_ps_obj_t
))) == NULL
)
1025 memcpy(temp
, st
->objs
+ n
+ c
- s
, (size_t)s
* sizeof(_cups_ps_obj_t
));
1026 memmove(st
->objs
+ n
+ s
, st
->objs
+ n
, (size_t)(c
- s
) * sizeof(_cups_ps_obj_t
));
1027 memcpy(st
->objs
+ n
, temp
, (size_t)s
* sizeof(_cups_ps_obj_t
));
1037 * 'scan_ps()' - Scan a string for the next PS object.
1040 static _cups_ps_obj_t
* /* O - New object or NULL on EOF */
1041 scan_ps(_cups_ps_stack_t
*st
, /* I - Stack */
1042 char **ptr
) /* IO - String pointer */
1044 _cups_ps_obj_t obj
; /* Current object */
1045 char *start
, /* Start of object */
1046 *cur
, /* Current position */
1047 *valptr
, /* Pointer into value string */
1048 *valend
; /* End of value string */
1049 int parens
; /* Parenthesis nesting level */
1053 * Skip leading whitespace...
1056 for (cur
= *ptr
; *cur
; cur
++)
1061 * Comment, skip to end of line...
1064 for (cur
++; *cur
&& *cur
!= '\n' && *cur
!= '\r'; cur
++);
1069 else if (!isspace(*cur
& 255))
1081 * See what we have...
1084 memset(&obj
, 0, sizeof(obj
));
1088 case '(' : /* (string) */
1089 obj
.type
= CUPS_PS_STRING
;
1092 for (cur
++, parens
= 1, valptr
= obj
.value
.string
,
1093 valend
= obj
.value
.string
+ sizeof(obj
.value
.string
) - 1;
1097 if (*cur
== ')' && parens
== 1)
1102 else if (*cur
== ')')
1105 if (valptr
>= valend
)
1115 * Decode escaped character...
1122 else if (*cur
== 'f')
1124 else if (*cur
== 'n')
1126 else if (*cur
== 'r')
1128 else if (*cur
== 't')
1130 else if (*cur
>= '0' && *cur
<= '7')
1132 int ch
= *cur
- '0';
1134 if (cur
[1] >= '0' && cur
[1] <= '7')
1137 ch
= (ch
<< 3) + *cur
- '0';
1140 if (cur
[1] >= '0' && cur
[1] <= '7')
1143 ch
= (ch
<< 3) + *cur
- '0';
1146 *valptr
++ = (char)ch
;
1148 else if (*cur
== '\r')
1153 else if (*cur
!= '\n')
1170 case '[' : /* Start array */
1171 obj
.type
= CUPS_PS_START_ARRAY
;
1175 case ']' : /* End array */
1176 obj
.type
= CUPS_PS_END_ARRAY
;
1180 case '<' : /* Start dictionary or hex string */
1183 obj
.type
= CUPS_PS_START_DICT
;
1188 obj
.type
= CUPS_PS_STRING
;
1191 for (cur
++, valptr
= obj
.value
.string
,
1192 valend
= obj
.value
.string
+ sizeof(obj
.value
.string
) - 1;
1196 int ch
; /* Current character */
1202 else if (valptr
>= valend
|| !isxdigit(*cur
& 255))
1208 if (*cur
>= '0' && *cur
<= '9')
1209 ch
= (*cur
- '0') << 4;
1211 ch
= (tolower(*cur
) - 'a' + 10) << 4;
1213 if (isxdigit(cur
[1] & 255))
1217 if (*cur
>= '0' && *cur
<= '9')
1220 ch
|= tolower(*cur
) - 'a' + 10;
1223 *valptr
++ = (char)ch
;
1236 case '>' : /* End dictionary? */
1239 obj
.type
= CUPS_PS_END_DICT
;
1244 obj
.type
= CUPS_PS_OTHER
;
1245 obj
.value
.other
[0] = *cur
;
1251 case '{' : /* Start procedure */
1252 obj
.type
= CUPS_PS_START_PROC
;
1256 case '}' : /* End procedure */
1257 obj
.type
= CUPS_PS_END_PROC
;
1261 case '-' : /* Possible number */
1263 if (!isdigit(cur
[1] & 255) && cur
[1] != '.')
1265 obj
.type
= CUPS_PS_OTHER
;
1266 obj
.value
.other
[0] = *cur
;
1272 case '0' : /* Number */
1283 obj
.type
= CUPS_PS_NUMBER
;
1286 for (cur
++; *cur
; cur
++)
1287 if (!isdigit(*cur
& 255))
1293 * Integer with radix...
1296 obj
.value
.number
= strtol(cur
+ 1, &cur
, atoi(start
));
1299 else if (strchr(".Ee()<>[]{}/%", *cur
) || isspace(*cur
& 255))
1302 * Integer or real number...
1305 obj
.value
.number
= _cupsStrScand(start
, &cur
, localeconv());
1311 default : /* Operator/variable name */
1316 obj
.type
= CUPS_PS_NAME
;
1317 valptr
= obj
.value
.name
;
1318 valend
= obj
.value
.name
+ sizeof(obj
.value
.name
) - 1;
1323 obj
.type
= CUPS_PS_OTHER
;
1324 valptr
= obj
.value
.other
;
1325 valend
= obj
.value
.other
+ sizeof(obj
.value
.other
) - 1;
1330 if (strchr("()<>[]{}/%", *cur
) || isspace(*cur
& 255))
1332 else if (valptr
< valend
)
1341 if (obj
.type
== CUPS_PS_OTHER
)
1343 if (!strcmp(obj
.value
.other
, "true"))
1345 obj
.type
= CUPS_PS_BOOLEAN
;
1346 obj
.value
.boolean
= 1;
1348 else if (!strcmp(obj
.value
.other
, "false"))
1350 obj
.type
= CUPS_PS_BOOLEAN
;
1351 obj
.value
.boolean
= 0;
1353 else if (!strcmp(obj
.value
.other
, "null"))
1354 obj
.type
= CUPS_PS_NULL
;
1355 else if (!strcmp(obj
.value
.other
, "cleartomark"))
1356 obj
.type
= CUPS_PS_CLEARTOMARK
;
1357 else if (!strcmp(obj
.value
.other
, "copy"))
1358 obj
.type
= CUPS_PS_COPY
;
1359 else if (!strcmp(obj
.value
.other
, "dup"))
1360 obj
.type
= CUPS_PS_DUP
;
1361 else if (!strcmp(obj
.value
.other
, "index"))
1362 obj
.type
= CUPS_PS_INDEX
;
1363 else if (!strcmp(obj
.value
.other
, "pop"))
1364 obj
.type
= CUPS_PS_POP
;
1365 else if (!strcmp(obj
.value
.other
, "roll"))
1366 obj
.type
= CUPS_PS_ROLL
;
1367 else if (!strcmp(obj
.value
.other
, "setpagedevice"))
1368 obj
.type
= CUPS_PS_SETPAGEDEVICE
;
1369 else if (!strcmp(obj
.value
.other
, "stopped"))
1370 obj
.type
= CUPS_PS_STOPPED
;
1376 * Save the current position in the string and return the new object...
1381 return (push_stack(st
, &obj
));
1386 * 'setpagedevice()' - Simulate the PostScript setpagedevice operator.
1389 static int /* O - 0 on success, -1 on error */
1391 _cups_ps_stack_t
*st
, /* I - Stack */
1392 cups_page_header2_t
*h
, /* O - Page header */
1393 int *preferred_bits
)/* O - Preferred bits per color */
1395 int i
; /* Index into array */
1396 _cups_ps_obj_t
*obj
, /* Current object */
1397 *end
; /* End of dictionary */
1398 const char *name
; /* Attribute name */
1402 * Make sure we have a dictionary on the stack...
1405 if (st
->num_objs
== 0)
1408 obj
= end
= st
->objs
+ st
->num_objs
- 1;
1410 if (obj
->type
!= CUPS_PS_END_DICT
)
1415 while (obj
> st
->objs
)
1417 if (obj
->type
== CUPS_PS_START_DICT
)
1427 * Found the start of the dictionary, empty the stack to this point...
1430 st
->num_objs
= (int)(obj
- st
->objs
);
1433 * Now pull /name and value pairs from the dictionary...
1436 DEBUG_puts("3setpagedevice: Dictionary:");
1438 for (obj
++; obj
< end
; obj
++)
1444 if (obj
->type
!= CUPS_PS_NAME
)
1447 name
= obj
->value
.name
;
1451 DEBUG_printf(("4setpagedevice: /%s ", name
));
1452 DEBUG_object("setpagedevice", obj
);
1456 * Then grab the value...
1459 if (!strcmp(name
, "MediaClass") && obj
->type
== CUPS_PS_STRING
)
1460 strlcpy(h
->MediaClass
, obj
->value
.string
, sizeof(h
->MediaClass
));
1461 else if (!strcmp(name
, "MediaColor") && obj
->type
== CUPS_PS_STRING
)
1462 strlcpy(h
->MediaColor
, obj
->value
.string
, sizeof(h
->MediaColor
));
1463 else if (!strcmp(name
, "MediaType") && obj
->type
== CUPS_PS_STRING
)
1464 strlcpy(h
->MediaType
, obj
->value
.string
, sizeof(h
->MediaType
));
1465 else if (!strcmp(name
, "OutputType") && obj
->type
== CUPS_PS_STRING
)
1466 strlcpy(h
->OutputType
, obj
->value
.string
, sizeof(h
->OutputType
));
1467 else if (!strcmp(name
, "AdvanceDistance") && obj
->type
== CUPS_PS_NUMBER
)
1468 h
->AdvanceDistance
= (unsigned)obj
->value
.number
;
1469 else if (!strcmp(name
, "AdvanceMedia") && obj
->type
== CUPS_PS_NUMBER
)
1470 h
->AdvanceMedia
= (unsigned)obj
->value
.number
;
1471 else if (!strcmp(name
, "Collate") && obj
->type
== CUPS_PS_BOOLEAN
)
1472 h
->Collate
= (unsigned)obj
->value
.boolean
;
1473 else if (!strcmp(name
, "CutMedia") && obj
->type
== CUPS_PS_NUMBER
)
1474 h
->CutMedia
= (cups_cut_t
)(unsigned)obj
->value
.number
;
1475 else if (!strcmp(name
, "Duplex") && obj
->type
== CUPS_PS_BOOLEAN
)
1476 h
->Duplex
= (unsigned)obj
->value
.boolean
;
1477 else if (!strcmp(name
, "HWResolution") && obj
->type
== CUPS_PS_START_ARRAY
)
1479 if (obj
[1].type
== CUPS_PS_NUMBER
&& obj
[2].type
== CUPS_PS_NUMBER
&&
1480 obj
[3].type
== CUPS_PS_END_ARRAY
)
1482 h
->HWResolution
[0] = (unsigned)obj
[1].value
.number
;
1483 h
->HWResolution
[1] = (unsigned)obj
[2].value
.number
;
1489 else if (!strcmp(name
, "InsertSheet") && obj
->type
== CUPS_PS_BOOLEAN
)
1490 h
->InsertSheet
= (unsigned)obj
->value
.boolean
;
1491 else if (!strcmp(name
, "Jog") && obj
->type
== CUPS_PS_NUMBER
)
1492 h
->Jog
= (unsigned)obj
->value
.number
;
1493 else if (!strcmp(name
, "LeadingEdge") && obj
->type
== CUPS_PS_NUMBER
)
1494 h
->LeadingEdge
= (unsigned)obj
->value
.number
;
1495 else if (!strcmp(name
, "ManualFeed") && obj
->type
== CUPS_PS_BOOLEAN
)
1496 h
->ManualFeed
= (unsigned)obj
->value
.boolean
;
1497 else if ((!strcmp(name
, "cupsMediaPosition") ||
1498 !strcmp(name
, "MediaPosition")) && obj
->type
== CUPS_PS_NUMBER
)
1501 * cupsMediaPosition is supported for backwards compatibility only.
1502 * We added it back in the Ghostscript 5.50 days to work around a
1503 * bug in Ghostscript WRT handling of MediaPosition and setpagedevice.
1505 * All new development should set MediaPosition...
1508 h
->MediaPosition
= (unsigned)obj
->value
.number
;
1510 else if (!strcmp(name
, "MediaWeight") && obj
->type
== CUPS_PS_NUMBER
)
1511 h
->MediaWeight
= (unsigned)obj
->value
.number
;
1512 else if (!strcmp(name
, "MirrorPrint") && obj
->type
== CUPS_PS_BOOLEAN
)
1513 h
->MirrorPrint
= (unsigned)obj
->value
.boolean
;
1514 else if (!strcmp(name
, "NegativePrint") && obj
->type
== CUPS_PS_BOOLEAN
)
1515 h
->NegativePrint
= (unsigned)obj
->value
.boolean
;
1516 else if (!strcmp(name
, "NumCopies") && obj
->type
== CUPS_PS_NUMBER
)
1517 h
->NumCopies
= (unsigned)obj
->value
.number
;
1518 else if (!strcmp(name
, "Orientation") && obj
->type
== CUPS_PS_NUMBER
)
1519 h
->Orientation
= (unsigned)obj
->value
.number
;
1520 else if (!strcmp(name
, "OutputFaceUp") && obj
->type
== CUPS_PS_BOOLEAN
)
1521 h
->OutputFaceUp
= (unsigned)obj
->value
.boolean
;
1522 else if (!strcmp(name
, "PageSize") && obj
->type
== CUPS_PS_START_ARRAY
)
1524 if (obj
[1].type
== CUPS_PS_NUMBER
&& obj
[2].type
== CUPS_PS_NUMBER
&&
1525 obj
[3].type
== CUPS_PS_END_ARRAY
)
1527 h
->cupsPageSize
[0] = (float)obj
[1].value
.number
;
1528 h
->cupsPageSize
[1] = (float)obj
[2].value
.number
;
1530 h
->PageSize
[0] = (unsigned)obj
[1].value
.number
;
1531 h
->PageSize
[1] = (unsigned)obj
[2].value
.number
;
1538 else if (!strcmp(name
, "Separations") && obj
->type
== CUPS_PS_BOOLEAN
)
1539 h
->Separations
= (unsigned)obj
->value
.boolean
;
1540 else if (!strcmp(name
, "TraySwitch") && obj
->type
== CUPS_PS_BOOLEAN
)
1541 h
->TraySwitch
= (unsigned)obj
->value
.boolean
;
1542 else if (!strcmp(name
, "Tumble") && obj
->type
== CUPS_PS_BOOLEAN
)
1543 h
->Tumble
= (unsigned)obj
->value
.boolean
;
1544 else if (!strcmp(name
, "cupsMediaType") && obj
->type
== CUPS_PS_NUMBER
)
1545 h
->cupsMediaType
= (unsigned)obj
->value
.number
;
1546 else if (!strcmp(name
, "cupsBitsPerColor") && obj
->type
== CUPS_PS_NUMBER
)
1547 h
->cupsBitsPerColor
= (unsigned)obj
->value
.number
;
1548 else if (!strcmp(name
, "cupsPreferredBitsPerColor") &&
1549 obj
->type
== CUPS_PS_NUMBER
)
1550 *preferred_bits
= (int)obj
->value
.number
;
1551 else if (!strcmp(name
, "cupsColorOrder") && obj
->type
== CUPS_PS_NUMBER
)
1552 h
->cupsColorOrder
= (cups_order_t
)(unsigned)obj
->value
.number
;
1553 else if (!strcmp(name
, "cupsColorSpace") && obj
->type
== CUPS_PS_NUMBER
)
1554 h
->cupsColorSpace
= (cups_cspace_t
)(unsigned)obj
->value
.number
;
1555 else if (!strcmp(name
, "cupsCompression") && obj
->type
== CUPS_PS_NUMBER
)
1556 h
->cupsCompression
= (unsigned)obj
->value
.number
;
1557 else if (!strcmp(name
, "cupsRowCount") && obj
->type
== CUPS_PS_NUMBER
)
1558 h
->cupsRowCount
= (unsigned)obj
->value
.number
;
1559 else if (!strcmp(name
, "cupsRowFeed") && obj
->type
== CUPS_PS_NUMBER
)
1560 h
->cupsRowFeed
= (unsigned)obj
->value
.number
;
1561 else if (!strcmp(name
, "cupsRowStep") && obj
->type
== CUPS_PS_NUMBER
)
1562 h
->cupsRowStep
= (unsigned)obj
->value
.number
;
1563 else if (!strcmp(name
, "cupsBorderlessScalingFactor") &&
1564 obj
->type
== CUPS_PS_NUMBER
)
1565 h
->cupsBorderlessScalingFactor
= (float)obj
->value
.number
;
1566 else if (!strncmp(name
, "cupsInteger", 11) && obj
->type
== CUPS_PS_NUMBER
)
1568 if ((i
= atoi(name
+ 11)) < 0 || i
> 15)
1571 h
->cupsInteger
[i
] = (unsigned)obj
->value
.number
;
1573 else if (!strncmp(name
, "cupsReal", 8) && obj
->type
== CUPS_PS_NUMBER
)
1575 if ((i
= atoi(name
+ 8)) < 0 || i
> 15)
1578 h
->cupsReal
[i
] = (float)obj
->value
.number
;
1580 else if (!strncmp(name
, "cupsString", 10) && obj
->type
== CUPS_PS_STRING
)
1582 if ((i
= atoi(name
+ 10)) < 0 || i
> 15)
1585 strlcpy(h
->cupsString
[i
], obj
->value
.string
, sizeof(h
->cupsString
[i
]));
1587 else if (!strcmp(name
, "cupsMarkerType") && obj
->type
== CUPS_PS_STRING
)
1588 strlcpy(h
->cupsMarkerType
, obj
->value
.string
, sizeof(h
->cupsMarkerType
));
1589 else if (!strcmp(name
, "cupsPageSizeName") && obj
->type
== CUPS_PS_STRING
)
1590 strlcpy(h
->cupsPageSizeName
, obj
->value
.string
,
1591 sizeof(h
->cupsPageSizeName
));
1592 else if (!strcmp(name
, "cupsRenderingIntent") &&
1593 obj
->type
== CUPS_PS_STRING
)
1594 strlcpy(h
->cupsRenderingIntent
, obj
->value
.string
,
1595 sizeof(h
->cupsRenderingIntent
));
1599 * Ignore unknown name+value...
1602 DEBUG_printf(("4setpagedevice: Unknown name (\"%s\") or value...\n", name
));
1604 while (obj
[1].type
!= CUPS_PS_NAME
&& obj
< end
)
1615 * 'DEBUG_object()' - Print an object's value...
1619 DEBUG_object(const char *prefix
, /* I - Prefix string */
1620 _cups_ps_obj_t
*obj
) /* I - Object to print */
1625 DEBUG_printf(("4%s: /%s\n", prefix
, obj
->value
.name
));
1628 case CUPS_PS_NUMBER
:
1629 DEBUG_printf(("4%s: %g\n", prefix
, obj
->value
.number
));
1632 case CUPS_PS_STRING
:
1633 DEBUG_printf(("4%s: (%s)\n", prefix
, obj
->value
.string
));
1636 case CUPS_PS_BOOLEAN
:
1637 if (obj
->value
.boolean
)
1638 DEBUG_printf(("4%s: true", prefix
));
1640 DEBUG_printf(("4%s: false", prefix
));
1644 DEBUG_printf(("4%s: null", prefix
));
1647 case CUPS_PS_START_ARRAY
:
1648 DEBUG_printf(("4%s: [", prefix
));
1651 case CUPS_PS_END_ARRAY
:
1652 DEBUG_printf(("4%s: ]", prefix
));
1655 case CUPS_PS_START_DICT
:
1656 DEBUG_printf(("4%s: <<", prefix
));
1659 case CUPS_PS_END_DICT
:
1660 DEBUG_printf(("4%s: >>", prefix
));
1663 case CUPS_PS_START_PROC
:
1664 DEBUG_printf(("4%s: {", prefix
));
1667 case CUPS_PS_END_PROC
:
1668 DEBUG_printf(("4%s: }", prefix
));
1671 case CUPS_PS_CLEARTOMARK
:
1672 DEBUG_printf(("4%s: --cleartomark--", prefix
));
1676 DEBUG_printf(("4%s: --copy--", prefix
));
1680 DEBUG_printf(("4%s: --dup--", prefix
));
1683 case CUPS_PS_INDEX
:
1684 DEBUG_printf(("4%s: --index--", prefix
));
1688 DEBUG_printf(("4%s: --pop--", prefix
));
1692 DEBUG_printf(("4%s: --roll--", prefix
));
1695 case CUPS_PS_SETPAGEDEVICE
:
1696 DEBUG_printf(("4%s: --setpagedevice--", prefix
));
1699 case CUPS_PS_STOPPED
:
1700 DEBUG_printf(("4%s: --stopped--", prefix
));
1703 case CUPS_PS_OTHER
:
1704 DEBUG_printf(("4%s: --%s--", prefix
, obj
->value
.other
));
1711 * 'DEBUG_stack()' - Print a stack...
1715 DEBUG_stack(const char *prefix
, /* I - Prefix string */
1716 _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
++)
1723 DEBUG_object(prefix
, obj
);