home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / ZDEVICE.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  9KB  |  298 lines

  1. /* Copyright (C) 1989, 1992, 1993, 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* zdevice.c */
  20. /* Device-related operators */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "ialloc.h"
  25. #include "idict.h"
  26. #include "igstate.h"
  27. #include "iname.h"
  28. #include "interp.h"
  29. #include "iparam.h"
  30. #include "gsmatrix.h"
  31. #include "gsstate.h"
  32. #include "gxdevice.h"
  33. #include "store.h"
  34.  
  35. /* <device> copydevice <newdevice> */
  36. int
  37. zcopydevice(register os_ptr op)
  38. {    gx_device *new_dev;
  39.     int code;
  40.     check_type(*op, t_device);
  41.     code = gs_copydevice(&new_dev, op->value.pdevice, imemory);
  42.     if ( code < 0 ) return code;
  43.     new_dev->memory = imemory;
  44.     make_tav(op, t_device, ialloc_local_attr(idmemory), pdevice, new_dev);
  45.     return 0;
  46. }
  47.  
  48. /* <device> <y> <string> copyscanlines <substring> */
  49. int
  50. zcopyscanlines(register os_ptr op)
  51. {    os_ptr op1 = op - 1;
  52.     os_ptr op2 = op - 2;
  53.     gx_device *dev;
  54.     int code;
  55.     uint bytes_copied;
  56.     check_type(*op2, t_device);
  57.     dev = op2->value.pdevice;
  58.     check_type(*op1, t_integer);
  59.     if ( op1->value.intval < 0 || op1->value.intval > dev->height )
  60.         return_error(e_rangecheck);
  61.     check_write_type(*op, t_string);
  62.     code = gs_copyscanlines(dev, (int)op1->value.intval,
  63.         op->value.bytes, r_size(op), NULL, &bytes_copied);
  64.     if ( code < 0 )
  65.         return_error(e_typecheck);    /* not a memory device */
  66.     *op2 = *op;
  67.     r_set_size(op2, bytes_copied);
  68.     pop(2);
  69.     return 0;
  70. }
  71.  
  72. /* - currentdevice <device> */
  73. int
  74. zcurrentdevice(register os_ptr op)
  75. {    gx_device *dev = gs_currentdevice(igs);
  76.     gs_ref_memory_t *mem = (gs_ref_memory_t *)dev->memory;
  77.     push(1);
  78.     make_tav(op, t_device,
  79.          (mem == 0 ? 0 : imemory_local_attr(mem)),
  80.          pdevice, dev);
  81.     return 0;
  82. }
  83.  
  84. /* <device> <matrix> deviceinitialmatrix <matrix> */
  85. int
  86. zdeviceinitialmatrix(register os_ptr op)
  87. {    gs_matrix mat;
  88.     int code;
  89.     check_type(op[-1], t_device);
  90.     gs_deviceinitialmatrix(op[-1].value.pdevice, &mat);
  91.     code = write_matrix(op, &mat);
  92.     if ( code < 0 )
  93.         return code;
  94.     op[-1] = *op;
  95.     pop(1);
  96.     return 0;
  97. }
  98.  
  99. /* - flushpage - */
  100. int
  101. zflushpage(register os_ptr op)
  102. {    return gs_flushpage(igs);
  103. }
  104.  
  105. /* <int> .getdevice <device> */
  106. int
  107. zgetdevice(register os_ptr op)
  108. {    gx_device *dev;
  109.     check_type(*op, t_integer);
  110.     if ( op->value.intval != (int)(op->value.intval) )
  111.         return_error(e_rangecheck);    /* won't fit in an int */
  112.     dev = gs_getdevice((int)(op->value.intval));
  113.     if ( dev == 0 )        /* index out of range */
  114.         return_error(e_rangecheck);
  115.     make_tav(op, t_device, a_foreign, pdevice, dev);
  116.     return 0;
  117. }
  118.  
  119. /* <device> <key_dict|null> .getdeviceparams <mark> <name> <value> ... */
  120. int
  121. zgetdeviceparams(os_ptr op)
  122. {    ref rkeys;
  123.     gx_device *dev;
  124.     stack_param_list list;
  125.     int code;
  126.     ref *pmark;
  127.     check_type(op[-1], t_device);
  128.     rkeys = *op;
  129.     dev = op[-1].value.pdevice;
  130.     pop(1);
  131.     stack_param_list_write(&list, &o_stack, &rkeys);
  132.     code = gs_getdeviceparams(dev, (gs_param_list *)&list);
  133.     if ( code < 0 )
  134.       {    /* We have to put back the top argument. */
  135.         if ( list.count > 0 )
  136.           ref_stack_pop(&o_stack, list.count * 2 - 1);
  137.         else
  138.           ref_stack_push(&o_stack, 1);
  139.         *osp = rkeys;
  140.         return code;
  141.       }
  142.     pmark = ref_stack_index(&o_stack, list.count * 2);
  143.     make_mark(pmark);
  144.     return 0;
  145. }
  146.  
  147. /* <matrix> <width> <height> <palette> makeimagedevice <device> */
  148. int
  149. zmakeimagedevice(register os_ptr op)
  150. {    gs_matrix imat;
  151.     gx_device *new_dev;
  152.     const byte *colors;
  153.     int colors_size;
  154.     int code;
  155.     check_int_leu(op[-2], max_uint >> 1);    /* width */
  156.     check_int_leu(op[-1], max_uint >> 1);    /* height */
  157.     if ( r_has_type(op, t_null) )    /* true color */
  158.        {    colors = 0;
  159.         colors_size = -24;    /* 24-bit true color */
  160.        }
  161.     else
  162.        {    check_type(*op, t_string);    /* palette */
  163.         if ( r_size(op) > 3*256 )
  164.             return_error(e_rangecheck);
  165.         colors = op->value.bytes;
  166.         colors_size = r_size(op);
  167.        }
  168.     if ( (code = read_matrix(op - 3, &imat)) < 0 )
  169.         return code;
  170.     /* Everything OK, create device */
  171.     code = gs_makeimagedevice(&new_dev, &imat,
  172.                   (int)op[-2].value.intval,
  173.                   (int)op[-1].value.intval,
  174.                   colors, colors_size, imemory);
  175.     if ( code == 0 )
  176.     {    new_dev->memory = imemory;
  177.         make_tav(op - 3, t_device, imemory_local_attr(iimemory),
  178.              pdevice, new_dev);
  179.         pop(3);
  180.     }
  181.     return code;
  182. }
  183.  
  184. /* - nulldevice - */
  185. int
  186. znulldevice(register os_ptr op)
  187. {    gs_nulldevice(igs);
  188.     return 0;
  189. }
  190.  
  191. /* <num_copies> <flush_bool> .outputpage - */
  192. int
  193. zoutputpage(register os_ptr op)
  194. {    int code;
  195.     check_type(op[-1], t_integer);
  196.     check_type(*op, t_boolean);
  197.     code = gs_output_page(igs, (int)op[-1].value.intval, op->value.boolval);
  198.     if ( code < 0 ) return code;
  199.     pop(2);
  200.     return 0;
  201. }
  202.  
  203. /* <device> <policy_dict|null> <mark> <name> <value> ... .putdeviceparams */
  204. /*   (on success) <device> <eraseflag> */
  205. /*   (on failure) <device> <policy_dict|null> <mark> <name1> <error1> ... */
  206. /* For a name that simply was not recognized, the error will be /undefined. */
  207. int
  208. zputdeviceparams(os_ptr op)
  209. {    uint count = ref_stack_counttomark(&o_stack);
  210.     ref *pdev;
  211.     ref *ppolicy;
  212.     gx_device *dev;
  213.     stack_param_list list;
  214.     int code;
  215.     int old_width, old_height;
  216.     int i, dest;
  217.     int ecode = 0;
  218.     if ( count == 0 )
  219.         return_error(e_unmatchedmark);
  220.     ppolicy = ref_stack_index(&o_stack, count);
  221.     pdev = ref_stack_index(&o_stack, count + 1);
  222.     if ( pdev == 0 )
  223.         return_error(e_stackunderflow);
  224.     check_type(*pdev, t_device);
  225.     dev = pdev->value.pdevice;
  226.     code = stack_param_list_read(&list, &o_stack, 0, ppolicy);
  227.     if ( code < 0 )
  228.         return code;
  229.     old_width = dev->width;
  230.     old_height = dev->height;
  231.     code = gs_putdeviceparams(dev, (gs_param_list *)&list);
  232.     /* Check for names that were undefined or caused errors. */
  233.     for ( dest = count - 2, i = 0; i < count >> 1; i++ )
  234.       if ( list.results[i] != 1 )
  235.         {    ecode = list.results[i];
  236.         if ( ecode == 0 )
  237.           ecode = e_undefined;
  238.         *ref_stack_index(&o_stack, dest) =
  239.           *ref_stack_index(&o_stack, count - (i << 1) - 2);
  240.         gs_errorname(ecode, ref_stack_index(&o_stack, dest - 1));
  241.         dest -= 2;
  242.         }
  243.     if ( ecode != 0 )
  244.       {    /* There were errors reported. */
  245.         ref_stack_pop(&o_stack, dest + 1);
  246.         return 0;
  247.       }
  248.     if ( code > 0 || (code == 0 && (dev->width != old_width || dev->height != old_height)) )
  249.     {    /* The device was open and is now closed, */
  250.         /* or its dimensions have changed. */
  251.         /* If it was the current device, */
  252.         /* call setdevice to reinstall it and erase the page. */
  253.         /****** DOESN'T FIND ALL THE GSTATES THAT REFERENCE THE DEVICE. ******/
  254.         if ( gs_currentdevice(igs) == dev )
  255.         {    bool was_open = dev->is_open;
  256.             code = gs_setdevice_no_erase(igs, dev);
  257.             /* If the device wasn't closed, */
  258.             /* setdevice won't erase the page. */
  259.             if ( was_open && code >= 0 )
  260.                 code = 1;
  261.         }
  262.     }
  263.     if ( code < 0 )
  264.         return code;
  265.     ref_stack_pop(&o_stack, count);
  266.     make_bool(osp, code);
  267.     return 0;
  268. }
  269.  
  270. /* <device> .setdevice <eraseflag> */
  271. int
  272. zsetdevice(register os_ptr op)
  273. {    int code;
  274.     check_type(*op, t_device);
  275.     code = gs_setdevice_no_erase(igs, op->value.pdevice);
  276.     if ( code >= 0 )
  277.         make_bool(op, 1);
  278.     return code;
  279. }
  280.  
  281. /* ------ Initialization procedure ------ */
  282.  
  283. op_def zdevice_op_defs[] = {
  284.     {"1copydevice", zcopydevice},
  285.     {"3copyscanlines", zcopyscanlines},
  286.     {"0currentdevice", zcurrentdevice},
  287.     {"2deviceinitialmatrix", zdeviceinitialmatrix},
  288.     {"0flushpage", zflushpage},
  289.     {"1.getdevice", zgetdevice},
  290.     {"2.getdeviceparams", zgetdeviceparams},
  291.     {"4makeimagedevice", zmakeimagedevice},
  292.     {"0nulldevice", znulldevice},
  293.     {"2.outputpage", zoutputpage},
  294.     {"3.putdeviceparams", zputdeviceparams},
  295.     {"1.setdevice", zsetdevice},
  296.     op_def_end(0)
  297. };
  298.