/* Portions Copyright (C) 2001 artofcode LLC.
   Portions Copyright (C) 1996, 2001 Artifex Software Inc.
   Portions Copyright (C) 1988, 2000 Aladdin Enterprises.
   This software is based in part on the work of the Independent JPEG Group.
   All Rights Reserved.

   This software is distributed under license and may not be copied, modified
   or distributed except as expressly authorized under the terms of that
   license.  Refer to licensing information at http://www.artifex.com/ or
   contact Artifex Software, Inc., 101 Lucas Valley Road #110,
   San Rafael, CA  94903, (415)492-9861, for further information. */
/*$Id: pxvalue.h,v 1.3 2003/05/02 05:44:20 henrys Exp $ */

/* pxvalue.h */
/* Value definitions for PCL XL parser */

#ifndef pxvalue_INCLUDED
#  define pxvalue_INCLUDED

#include "gstypes.h"
#include "pxattr.h"

/* ---------------- Values on stack ---------------- */

/* Define masks for checking data types of attribute values. */
typedef enum {
  /* Structure */
  pxd_scalar = 1,
  pxd_xy = 2,
  pxd_box = 4,
  pxd_array = 8,
  pxd_structure = 0xf,
  /* Representation */
  pxd_ubyte = 0x10,
  pxd_uint16 = 0x20,
  pxd_uint32 = 0x40,
  pxd_sint16 = 0x80,
  pxd_sint32 = 0x100,
  pxd_any_int = pxd_ubyte | pxd_uint16 | pxd_uint32 | pxd_sint16 | pxd_sint32,
  pxd_real32 = 0x200,
  pxd_any_real = pxd_real32,
  pxd_representation = 0x3f0,
  /* Byte ordering for arrays */
  pxd_big_endian = 0x400,
  /* Heap allocation for arrays */
  pxd_on_heap = 0x800
} px_data_type_t;

/* Define a type for a 32-bit integer. */
#if arch_ints_are_short
typedef long integer;
#else
typedef int integer;
#endif
typedef float real;

/* Define a type for values on the stack. */
typedef struct px_array_s {
  uint size;		/* number of elements, not length in bytes */
  const byte *data;
} px_array_t;
typedef struct {
  px_data_type_t type;
  px_attribute_t attribute;
  union v_ {
    integer i;
    integer ia[4];		/* ia[0] aliases i */
    real r;
    real ra[4];			/* ra[0] aliases r */
    px_array_t array;
  } value;
} px_value_t;

#define real_value(pv, n)\
  ((pv)->type & pxd_any_real ? (pv)->value.ra[n] : (real)(pv)->value.ia[n])

#define set_box_value(x1, y1, x2, y2, pv)\
  do {\
    const px_value_t *pv_ = (pv);\
    if ( pv_->type & pxd_any_real )\
      x1 = pv_->value.ra[0], y1 = pv_->value.ra[1],\
	x2 = pv_->value.ra[2], y2 = pv_->value.ra[3];\
    else\
      x1 = pv_->value.ia[0], y1 = pv_->value.ia[1],\
	x2 = pv_->value.ia[2], y2 = pv_->value.ia[3];\
  } while ( 0 )

#define value_size(pv)\
  ((pv)->type & pxd_ubyte ? 1 : (pv)->type & (pxd_uint16 | pxd_sint16) ? 2 : 4)

#define array_value_size(pav)\
  ((pav)->value.array.size * value_size(pav))

/* ---------------- Procedures ---------------- */

/* Get numeric values from the input or an array. */
uint uint16at(const byte *p, bool big_endian);
int sint16at(const byte *p, bool big_endian);
integer uint32at(const byte *p, bool big_endian);
integer sint32at(const byte *p, bool big_endian);
real real32at(const byte *p, bool big_endian);

/* Get an element from an array. */
/* The caller does all index and type checking. */
integer integer_elt(const px_value_t *pav, uint index);
real real_elt(const px_value_t *pav, uint index);

#endif				/* pxvalue_INCLUDED */
