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 / ZDPS1.C < prev    next >
C/C++ Source or Header  |  1992-07-18  |  6KB  |  255 lines

  1. /* Copyright (C) 1990, 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. /* zdps1.c */
  21. /* Display PostScript graphics extensions */
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "gsmatrix.h"
  26. #include "gspath.h"
  27. #include "gsstate.h"
  28. #include "alloc.h"
  29. #include "state.h"
  30. #include "store.h"
  31. #include "stream.h"
  32. #include "bnum.h"
  33.  
  34. /* ------ Graphics states ------ */
  35.  
  36. /* gstate */
  37. int
  38. zgstate(register os_ptr op)
  39. {    gs_state *pnew = gs_gstate(igs);
  40.     int_gstate *isp;
  41.     if ( pnew == 0 ) return e_VMerror;
  42.     isp = (int_gstate *)gs_state_client_data(pnew);
  43.     int_gstate_map_refs(isp, ref_mark_new);
  44.     push(1);
  45.     make_tv(op, t_gstate, pgstate, pnew);
  46.     return 0;
  47. }
  48.  
  49. /* currentgstate */
  50. int
  51. zcurrentgstate(register os_ptr op)
  52. {    int code;
  53.     int_gstate *pistate;
  54.     check_type(*op, t_gstate);
  55.     pistate = (int_gstate *)gs_state_client_data(op->value.pgstate);
  56. #define gsref_save(p) ref_save(p, "currentgstate")
  57.     int_gstate_map_refs(pistate, gsref_save);
  58. #undef gsref_save
  59.     /****** DOESN'T GET FULLY UNDONE BY RESTORE ******/
  60.     code = gs_currentgstate(op->value.pgstate, igs);
  61.     if ( code < 0 ) return code;
  62.     int_gstate_map_refs(pistate, ref_mark_new);
  63.     return 0;
  64. }
  65.  
  66. /* setgstate */
  67. int
  68. zsetgstate(register os_ptr op)
  69. {    int code;
  70.     check_type(*op, t_gstate);
  71.     code = gs_setgstate(igs, op->value.pgstate);
  72.     if ( code < 0 ) return code;
  73.     pop(1);
  74.     return 0;
  75. }
  76.  
  77. /* ------ Rectangles ------- */
  78.  
  79. /* We preallocate a short list for rectangles, because */
  80. /* the rectangle operators usually will involve very few rectangles. */
  81. #define max_local_rect 5
  82. typedef struct local_rects_s {
  83.     gs_rect *pr;
  84.     uint count;
  85.     gs_rect rl[max_local_rect];
  86. } local_rects;
  87.  
  88. /* Forward references */
  89. private int rect_get(P2(local_rects *, os_ptr));
  90. private void rect_release(P1(local_rects *));
  91.  
  92. /* rectappend */
  93. int
  94. zrectappend(os_ptr op)
  95. {    local_rects lr;
  96.     int npop = rect_get(&lr, op);
  97.     int code;
  98.     if ( npop < 0 ) return npop;
  99.     code = gs_rectappend(igs, lr.pr, lr.count);
  100.     rect_release(&lr);
  101.     if ( code < 0 ) return code;
  102.     pop(npop);
  103.     return 0;
  104. }
  105.  
  106. /* rectclip */
  107. int
  108. zrectclip(os_ptr op)
  109. {    local_rects lr;
  110.     int npop = rect_get(&lr, op);
  111.     int code;
  112.     if ( npop < 0 ) return npop;
  113.     code = gs_rectclip(igs, lr.pr, lr.count);
  114.     rect_release(&lr);
  115.     if ( code < 0 ) return code;
  116.     pop(npop);
  117.     return 0;
  118. }
  119.  
  120. /* rectfill */
  121. int
  122. zrectfill(os_ptr op)
  123. {    local_rects lr;
  124.     int npop = rect_get(&lr, op);
  125.     int code;
  126.     if ( npop < 0 ) return npop;
  127.     code = gs_rectfill(igs, lr.pr, lr.count);
  128.     rect_release(&lr);
  129.     if ( code < 0 ) return code;
  130.     pop(npop);
  131.     return 0;
  132. }
  133.  
  134. /* rectstroke */
  135. int
  136. zrectstroke(os_ptr op)
  137. {    gs_matrix mat;
  138.     local_rects lr;
  139.     int npop, code;
  140.     if ( read_matrix(op, &mat) >= 0 )
  141.        {    /* Concatenate the matrix to the CTM just before */
  142.         /* stroking the path. */
  143.         npop = rect_get(&lr, op - 1);
  144.         if ( npop < 0 ) return npop;
  145.         code = gs_rectstroke(igs, lr.pr, lr.count, &mat);
  146.         npop++;
  147.        }
  148.     else
  149.        {    /* No matrix. */
  150.         npop = rect_get(&lr, op);
  151.         if ( npop < 0 ) return npop;
  152.         code = gs_rectstroke(igs, lr.pr, lr.count, (gs_matrix *)0);
  153.        }
  154.     rect_release(&lr);
  155.     if ( code < 0 ) return code;
  156.     pop(npop);
  157.     return 0;
  158. }
  159.  
  160. /* --- Internal routines --- */
  161.  
  162. /* Get rectangles from the stack. */
  163. /* Return the number of elements to pop (>0) if OK, <0 if error. */
  164. private int
  165. rect_get(local_rects *plr, os_ptr op)
  166. {    int code, npop;
  167.     stream st;
  168.     uint n, count;
  169.     gs_rect *pr;
  170.     switch ( r_type(op) )
  171.        {
  172.     case t_array:
  173.     case t_string:
  174.         code = sread_num_array(&st, op);
  175.         if ( code < 0 ) return code;
  176.         count = scount_num_stream(&st);
  177.         if ( count % 4 ) return e_typecheck;
  178.         count /= 4, npop = 1;
  179.         break;
  180.     default:            /* better be 4 numbers */
  181.         sread_string(&st, (byte *)(op - 3), sizeof(ref) * 4);
  182.         st.num_format = num_array;
  183.         count = 1, npop = 4;
  184.         break;
  185.        }
  186.     plr->count = count;
  187.     if ( count <= max_local_rect )
  188.         pr = plr->rl;
  189.     else
  190.        {    pr = (gs_rect *)alloc(count, sizeof(gs_rect), "rect_get");
  191.         if ( pr == 0 ) return e_VMerror;
  192.        }
  193.     plr->pr = pr;
  194.     for ( n = 0; n < count; n++, pr++ )
  195.        {    ref rnum;
  196.         float rv[4];
  197.         int i;
  198.         for ( i = 0; i < 4; i++ )
  199.            {    switch ( code = sget_encoded_number(&st, &rnum) )
  200.                {
  201.             case t_integer:
  202.                 rv[i] = rnum.value.intval;
  203.                 break;
  204.             case t_real:
  205.                 rv[i] = rnum.value.realval;
  206.                 break;
  207.             default:    /* code < 0 */
  208.                 return code;
  209.                }
  210.            }
  211.         pr->q.x = (pr->p.x = rv[0]) + rv[2];
  212.         pr->q.y = (pr->p.y = rv[1]) + rv[3];
  213.        }
  214.     return npop;
  215. }
  216.  
  217. /* Release the rectangle list if needed. */
  218. private void
  219. rect_release(local_rects *plr)
  220. {    if ( plr->pr != plr->rl )
  221.         alloc_free((char *)plr->pr, plr->count, sizeof(gs_rect),
  222.                "rect_release");
  223. }
  224.  
  225. /* ------ Graphics state ------ */
  226.  
  227. /* setbbox */
  228. int
  229. zsetbbox(register os_ptr op)
  230. {    float box[4];
  231.     int code = num_params(op, 4, box);
  232.     if ( code < 0 ) return code;
  233.     if ( (code = gs_setbbox(igs, box[0], box[1], box[2], box[3])) < 0 )
  234.         return code;
  235.     pop(4);
  236.     return 0;
  237. }
  238.  
  239. /* ------ Initialization procedure ------ */
  240.  
  241. op_def zdps1_op_defs[] = {
  242.         /* Graphics state objects */
  243.     {"0gstate", zgstate},
  244.     {"1currentgstate", zcurrentgstate},
  245.     {"1setgstate", zsetgstate},
  246.         /* Rectangles */
  247.     {"1rectappend", zrectappend},
  248.     {"1rectclip", zrectclip},
  249.     {"1rectfill", zrectfill},
  250.     {"1rectstroke", zrectstroke},
  251.         /* Graphics state components */
  252.     {"4setbbox", zsetbbox},
  253.     op_def_end(0)
  254. };
  255.