home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / ghostscr / z2path.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-31  |  5.4 KB  |  212 lines

  1. /* Copyright (C) 1990 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. /* z2path.c */
  21. /* Level 2 user path and rectangle operators */
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "state.h"
  26. #include "stream.h"
  27. #include "i2num.h"
  28.  
  29. /* .rectappend */
  30. int
  31. zrectappend(register ref *op)
  32. {    int npop, code;
  33.     stream st;
  34.     float pq[4];
  35.     int i;
  36.     ref rnum;
  37.     switch ( r_type(op) )
  38.        {
  39.     case t_array: case t_string:
  40.         code = sread_num_array(&st, op);
  41.         if ( code < 0 ) return code;
  42.         npop = 1;
  43.         break;
  44.     default:            /* better be 4 numbers */
  45.         sread_string(&st, (byte *)(op - 3), sizeof(ref) * 4);
  46.         st.num_format = num_array;
  47.         npop = 4;
  48.         break;
  49.        }
  50.     while ( 1 )
  51.        {    for ( i = 0; i < 4; i++ )
  52.            {    switch ( code = sget_encoded_number(&st, &rnum) )
  53.                {
  54.             case t_integer:
  55.                 pq[i] = rnum.value.intval;
  56.                 break;
  57.             case t_real:
  58.                 pq[i] = rnum.value.realval;
  59.                 break;
  60.             case t_null:
  61.                 if ( i != 0 ) return e_syntaxerror;
  62.                 pop(npop);
  63.                 return 0;
  64.             default:    /* code < 0 */
  65.                 return code;
  66.                }
  67.            }
  68.         /* Force the vertices into counter-clockwise order, */
  69.         /* by requiring width >= 0, height >= 0. */
  70.         if ( pq[2] < 0 ) pq[0] += pq[2], pq[2] = -pq[2];
  71.         if ( pq[3] < 0 ) pq[1] += pq[3], pq[3] = -pq[3];
  72.         if ( (code = gs_moveto(igs, pq[0], pq[1])) < 0 ||
  73.              (code = gs_rlineto(igs, pq[2], 0.0)) < 0 ||
  74.              (code = gs_rlineto(igs, 0.0, pq[3])) < 0 ||
  75.              (code = gs_rlineto(igs, -pq[2], 0.0)) < 0 ||
  76.              (code = gs_closepath(igs)) < 0
  77.            )
  78.             return code;
  79.        }
  80. }
  81.  
  82. /* User path operator codes */
  83. typedef enum {
  84.   upath_setbbox = 0,
  85.   upath_moveto = 1,
  86.   upath_rmoveto = 2,
  87.   upath_lineto = 3,
  88.   upath_rlineto = 4,
  89.   upath_curveto = 5,
  90.   upath_rcurveto = 6,
  91.   upath_arc = 7,
  92.   upath_arcn = 8,
  93.   upath_arct = 9,
  94.   upath_closepath = 10,
  95.   upath_ucache = 11
  96. } upath_op;
  97. #define upath_op_max 11
  98. #define upath_repeat 32
  99. static byte up_nargs[upath_op_max + 1] =
  100.    { 4, 2, 2, 2, 2, 6, 6, 5, 5, 5, 0, 0 };
  101. #define zp(proc) extern int proc(P1(ref *));
  102.     zp(zsetbbox); zp(zmoveto); zp(zrmoveto);
  103.     zp(zlineto); zp(zrlineto); zp(zcurveto); zp(zrcurveto);
  104.     zp(zarc); zp(zarcn); zp(zarct); zp(zclosepath); zp(zucache);
  105. #undef zp
  106. static int (*up_ops[upath_op_max + 1])(P1(ref *)) =
  107.    {    zsetbbox, zmoveto, zrmoveto, zlineto, zrlineto,
  108.     zcurveto, zrcurveto, zarc, zarcn, zarct,
  109.     zclosepath, zucache
  110.    };
  111.  
  112. /* uappend */
  113. int
  114. zuappend(register ref *op)
  115. {    /****** ROUND tx AND ty ******/
  116.     /****** ALLOW PACKED ARRAY ******/
  117.     check_array(*op);
  118.     if ( op->size == 2 && r_type(op->value.refs + 1) == t_string )
  119.        {    /* 1st element is operators, 2nd is operands */
  120.         stream st;
  121.         int code;
  122.         int repcount = 1;
  123.         byte *opp;
  124.         uint ocount;
  125.         code = sread_num_array(&st, op->value.refs);
  126.         if ( code < 0 ) return code;
  127.         opp = op->value.refs[1].value.bytes;
  128.         ocount = op->value.refs[1].size;
  129.         while ( ocount-- )
  130.            {    byte opx = *opp++;
  131.             if ( opx > 32 )
  132.                 repcount = opx - 32;
  133.             else if ( opx > upath_op_max )
  134.                 return e_typecheck;
  135.             else        /* operator */
  136.                {    do
  137.                    {    byte opargs = up_nargs[opx];
  138.                     while ( opargs-- )
  139.                        {    push(1);
  140.                         code = sget_encoded_number(&st, op);
  141.                         switch ( code )
  142.                            {
  143.                         case t_integer: case t_real:
  144.                             /***** SET TYPE *****/
  145.                             break;
  146.                         default:
  147.                             return e_typecheck;
  148.                            }
  149.                        }
  150.                     code = (*up_ops[opx])(op);
  151.                     if ( code < 0 ) return code;
  152.                     op = osp;    /* resync */
  153.                    }
  154.                 while ( --repcount );
  155.                 repcount = 1;
  156.                }
  157.            }
  158.        }
  159.     else
  160.         {    /* Ordinary executable array */
  161.         ref *opp = op->value.refs;
  162.         uint ocount = op->size;
  163.         int argcount = 0;
  164.         int (*oproc)(P1(ref *));
  165.         int opx, code;
  166.         for ( ; ocount--; opp++ )
  167.          switch ( r_type(opp) )
  168.            {
  169.         case t_integer: case t_real:
  170.             argcount++;
  171.             push(1);
  172.             *op = *opp;
  173.             break;
  174.         case t_name:
  175.             if ( !r_has_attrs(op, a_executable) )
  176.                 return e_typecheck;
  177.             /****** LOOK UP IN systemdict ******/
  178.             /****** MUST BE AN OPERATOR -> oproc ******/
  179.             goto xop;
  180.         case t_operator:
  181.             if ( !r_has_attrs(op, a_executable) )
  182.                 return e_typecheck;
  183.             oproc = op->value.opproc;
  184. xop:            for ( opx = 0; opx <= upath_op_max; opx++ )
  185.                 if ( oproc == up_ops[opx] ) break;
  186.             if ( opx > upath_op_max || argcount != up_nargs[opx] )
  187.                 return e_typecheck;
  188.             code = (*oproc)(op);
  189.             if ( code < 0 ) return code;
  190.             op = osp;    /* resync ostack pointer */
  191.             argcount = 0;
  192.             break;
  193.         default:
  194.             return e_typecheck;
  195.            }
  196.        }
  197.     pop(1);
  198.     return 0;
  199. }
  200.  
  201. /* ------ Initialization procedure ------ */
  202.  
  203. void
  204. z2path_op_init()
  205. {    static op_def my_defs[] = {
  206.         {"1.rectappend", zrectappend},
  207.         {"1uappend", zuappend},
  208.         op_def_end
  209.     };
  210.     z_op_init(my_defs);
  211. }
  212.