home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / ZPROPS.C < prev    next >
C/C++ Source or Header  |  1992-09-09  |  7KB  |  271 lines

  1. /* Copyright (C) 1991, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* zprops.c */
  21. /* Device property operators */
  22. #include "memory_.h"
  23. #include "ghost.h"
  24. #include "alloc.h"
  25. #include "dict.h"
  26. #include "errors.h"
  27. #include "oper.h"
  28. #include "name.h"
  29. #include "state.h"
  30. #include "store.h"
  31. #include "gsprops.h"
  32. #include "gsmatrix.h"            /* for gxdevice.h */
  33. #include "gxdevice.h"
  34. #include "gsstate.h"
  35.  
  36. /* Forward references */
  37. private int props_to_stack(P3(const gs_prop_item *, os_ptr, int));
  38. private int props_from_stack(P3(gs_prop_item *, const_os_ptr, int));
  39.  
  40. /* getdeviceprops */
  41. int
  42. zgetdeviceprops(os_ptr op)
  43. {    gx_device *dev;
  44.     gs_prop_item *plist;
  45.     int count;
  46.     int code;
  47.     check_type(*op, t_device);
  48.     dev = op->value.pdevice;
  49.     count = gs_getdeviceprops_size(dev);
  50.     plist = (gs_prop_item *)alloc(count, sizeof(gs_prop_item), "getdeviceprops");
  51.     if ( plist == 0 ) return_error(e_VMerror);
  52.     code = gs_getdeviceprops(dev, plist);
  53.     if ( code >= 0 ) code = props_to_stack(plist, op + 1, count);
  54.     alloc_free((char *)plist, count, sizeof(gs_prop_item), "getdeviceprops");
  55.     if ( code >= 0 )
  56.        {    make_mark(op);
  57.         push(code);
  58.         code = 0;
  59.        }
  60.     return code;
  61. }
  62.  
  63. /* putdeviceprops */
  64. int
  65. zputdeviceprops(os_ptr op)
  66. {    gx_device *dev;
  67.     gs_prop_item *plist;
  68.     os_ptr mp;
  69.     int count, acount = 0;
  70.     int code;
  71.     int old_width, old_height;
  72.     check_type(*op, t_device);
  73.     dev = op->value.pdevice;
  74.     old_width = dev->width;
  75.     old_height = dev->height;
  76.     for ( mp = op - 1; !r_has_type(mp, t_mark); mp-- )
  77.        {    if ( mp <= osbot ) return e_unmatchedmark;
  78.         switch ( r_type(mp) )
  79.            {
  80.         case t_array:
  81.         case t_mixedarray:
  82.         case t_shortarray:
  83.             acount += r_size(mp);
  84.            }
  85.        }
  86.     count = op - mp - 1;
  87.     if ( count & 1 ) return e_rangecheck;
  88.     count >>= 1;
  89.     plist = (gs_prop_item *)alloc(count + acount, sizeof(gs_prop_item), "putdeviceprops");
  90.     if ( plist == 0 ) return_error(e_VMerror);
  91.     code = props_from_stack(plist, mp + 1, count);
  92.     if ( code >= 0 )
  93.         code = gs_putdeviceprops(dev, plist, count + acount);
  94.     alloc_free((char *)plist, count + acount, sizeof(gs_prop_item), "putdeviceprops");
  95.     if ( code > 0 || dev->width != old_width || dev->height != old_height )
  96.     {    /* The device was open and is now closed, */
  97.         /* or its dimensions have changed. */
  98.         /* If it was the current device, */
  99.         /* call setdevice to reinstall it and erase the page. */
  100.         /****** DOESN'T FIND ALL THE GSTATES THAT REFERENCE THE DEVICE. ******/
  101.         if ( gs_currentdevice(igs) == dev )
  102.             code = gs_setdevice(igs, dev);
  103.     }
  104.     if ( code >= 0 )
  105.        {    *mp = *op;
  106.         osp = op = mp;
  107.         code = 0;
  108.        }
  109.     return code;
  110. }
  111.  
  112. /* ------ Initialization procedure ------ */
  113.  
  114. op_def zprops_op_defs[] = {
  115.     {"1getdeviceprops", zgetdeviceprops},
  116.     {"2putdeviceprops", zputdeviceprops},
  117.     op_def_end(0)
  118. };
  119.  
  120. /* ------ Internal routines ------ */
  121.  
  122. /* Get properties from a property list to the stack. */
  123. private int
  124. props_to_stack(const gs_prop_item *plist, os_ptr op0, int count)
  125. {    const gs_prop_item *pi;
  126.     os_ptr op;
  127.     int i;
  128.     int code;
  129.     for ( op = op0, pi = plist, i = count; i != 0; pi++, i-- )
  130.        {    ref value;
  131.         const char *nstr = pi->pname;
  132.         int nlen = pi->name_size;
  133.         if ( nstr == 0 ) continue;    /* no name, skip */
  134.         if ( ostop - op < 2 ) return e_stackoverflow;
  135.         if ( nlen < 0 ) nlen = strlen(nstr);
  136.         code = name_ref((const byte *)nstr, nlen, op, 0);
  137.         if ( code < 0 ) return code;
  138.         switch ( pi->type )
  139.            {
  140.         case (int)prt_int:
  141.             make_int(&value, pi->value.i);
  142.             break;
  143.         case (int)prt_float:
  144.             make_real(&value, pi->value.f);
  145.             break;
  146.         case (int)prt_bool:
  147.             make_bool(&value, pi->value.b);
  148.             break;
  149.         case (int)prt_string:
  150.            {    ushort size = pi->value.a.size;
  151.             char *str;
  152.             if ( size == (ushort)(-1) )
  153.                 size = strlen(pi->value.a.p.s);
  154.             str = alloc(size, 1, "props_to_stack(string)");
  155.             if ( str == 0 ) return e_VMerror;
  156.             memcpy(str, pi->value.a.p.s, size);
  157.             make_tasv(&value, t_string, a_all, size, bytes, (byte *)str);
  158.            }    break;
  159.         case (int)prt_int_array:
  160.         case (int)prt_float_array:
  161.            {    uint size = pi->value.a.size;
  162.             ref *arefs = alloc_refs(size, "props_to_stack(array)");
  163.             uint j;
  164.             gs_prop_item *pv = pi->value.a.p.v;
  165.             if ( arefs == 0 ) return e_VMerror;
  166.             make_tasv_new(&value, t_array, a_all, size, refs, arefs);
  167.             for ( j = 0; j < size; j++, arefs++, pv++ )
  168.               if ( pi->type == prt_int_array )
  169.                 make_int_new(arefs, pv->value.i);
  170.               else
  171.                 make_real_new(arefs, pv->value.f);
  172.            }    break;
  173.         default:
  174.             return e_typecheck;
  175.            }
  176.         ref_assign(op + 1, &value);
  177.         op += 2;
  178.        }
  179.     return op - op0;
  180. }
  181.  
  182. /* Set properties from the stack. */
  183. /* Returns the number of elements copied. */
  184. /* Entries with non-name keys are not copied; */
  185. /* entries with invalid values are copied with status = pv_typecheck. */
  186. private int
  187. props_from_stack(gs_prop_item *plist /* [count + acount] */, const_os_ptr op0,
  188.   int count)
  189. {    gs_prop_item *pi = plist;
  190.     gs_prop_item *pai = plist + count;
  191.     const_os_ptr op = op0 + 1;
  192.     for ( ; count; op += 2, count-- )
  193.        {    ref sref;
  194.         if ( !r_has_type(op - 1, t_name) ) return e_typecheck;
  195.         name_string_ref(op - 1, &sref);
  196.         pi->pname = (char *)sref.value.bytes;
  197.         pi->name_size = r_size(&sref);
  198.         pi->status = pv_set;
  199.         switch ( r_type(op) )
  200.            {
  201.         case t_null:
  202.             pi->type = prt_null;
  203.             break;
  204.         case t_integer:
  205.             pi->type = prt_int;
  206.             pi->value.i = op->value.intval;
  207.             break;
  208.         case t_real:
  209.             pi->type = prt_float;
  210.             pi->value.f = op->value.realval;
  211.             break;
  212.         case t_boolean:
  213.             pi->type = prt_bool;
  214.             pi->value.b = op->value.index;
  215.             break;
  216.         case t_name:
  217.             name_string_ref(op, &sref);
  218.             goto nst;
  219.         case t_string:
  220.             ref_assign(&sref, op);
  221.             pi->type = prt_string;
  222.             pi->value.a.p.s = (char *)op->value.bytes;
  223. nst:            pi->value.a.size = r_size(&sref);
  224.             break;
  225.         case t_array:
  226.         case t_mixedarray:
  227.         case t_shortarray:
  228.            {    uint size = r_size(op);
  229.             uint i;
  230.             gs_prop_item *pv;
  231.             gs_prop_type tv = prt_int;
  232.             pi->type = prt_int_array;
  233.             pi->value.a.p.v = pai;
  234.             pi->value.a.size = size;
  235. top:            pv = pai;
  236.             for ( i = 0; i < size; i++ )
  237.                {    ref rnum;
  238.                 array_get(op, (long)i, &rnum);
  239.                 pv->pname = 0;
  240.                 pv->type = tv;
  241.                 switch ( r_type(&rnum) )
  242.                    {
  243.                 case t_real:
  244.                     if ( tv == prt_int )
  245.                        {    tv = prt_float;
  246.                         pi->type = prt_float_array;
  247.                         goto top;
  248.                        }
  249.                     pv++->value.f = rnum.value.realval;
  250.                     break;
  251.                 case t_integer:
  252.                     if ( tv == prt_int )
  253.                       pv++->value.i = rnum.value.intval;
  254.                     else
  255.                       pv++->value.f = rnum.value.intval;
  256.                     break;
  257.                 default:
  258.                     pi->status = pv_typecheck;
  259.                    }
  260.                }
  261.  
  262.             pai = pv;
  263.            }    break;
  264.         default:
  265.             pi->status = pv_typecheck;
  266.            }
  267.         pi++;
  268.        }
  269.     return 0;
  270. }
  271.