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

  1. /* Copyright (C) 1990, 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. /* zdps1.c */
  20. /* Display PostScript graphics extensions */
  21. #include "ghost.h"
  22. #include "errors.h"
  23. #include "oper.h"
  24. #include "gsmatrix.h"
  25. #include "gspath.h"
  26. #include "gspath2.h"
  27. #include "gsstate.h"
  28. #include "gsstruct.h"            /* for st_gstate in igstate.h */
  29. #include "ialloc.h"
  30. #include "igstate.h"
  31. #include "ivmspace.h"
  32. #include "store.h"
  33. #include "stream.h"
  34. #include "ibnum.h"
  35.  
  36. /* Imported data */
  37. extern op_proc_p zcopy_procs[t_next_index];
  38.  
  39. /* Forward references */
  40. private int zcopy_gstate(P1(os_ptr));
  41.  
  42. /* Initialize by adding an entry for gstates to the `copy' operator. */
  43. /* This is done with a hack -- we know that gstates are the only */
  44. /* t_astruct subtype that implements copy. */
  45. private void
  46. zdps1_init(void)
  47. {    zcopy_procs[t_astruct] = zcopy_gstate;
  48. }
  49.  
  50. /* ------ Graphics state ------ */
  51.  
  52. /* <bool> setstrokeadjust - */
  53. int
  54. zsetstrokeadjust(register os_ptr op)
  55. {    check_type(*op, t_boolean);
  56.     gs_setstrokeadjust(igs, op->value.boolval);
  57.     pop(1);
  58.     return 0;
  59. }
  60.  
  61. /* - currentstrokeadjust <bool> */
  62. int
  63. zcurrentstrokeadjust(register os_ptr op)
  64. {    push(1);
  65.     make_bool(op, gs_currentstrokeadjust(igs));
  66.     return 0;
  67. }
  68.  
  69. /* <x> <y> sethalftonephase - */
  70. int
  71. zsethalftonephase(register os_ptr op)
  72. {    int code;
  73.     long x, y;
  74.     check_type(op[-1], t_integer);
  75.     check_type(*op, t_integer);
  76.     x = op[-1].value.intval;
  77.     y = op->value.intval;
  78.     if ( x != (int)x || y != (int)y )
  79.         return_error(e_rangecheck);
  80.     code = gs_sethalftonephase(igs, (int)x, (int)y);
  81.     if ( code >= 0 ) pop(2);
  82.     return code;
  83. }
  84.  
  85. /* - currenthalftonephase <x> <y> */
  86. int
  87. zcurrenthalftonephase(register os_ptr op)
  88. {    gs_int_point phase;
  89.     gs_currenthalftonephase(igs, &phase);
  90.     push(2);
  91.     make_int(op - 1, phase.x);
  92.     make_int(op, phase.y);
  93.     return 0;
  94. }
  95.  
  96. /* ------ Graphics state objects ------ */
  97.  
  98. /* Check to make sure that all the elements of a graphics state */
  99. /* are global.  ****** DOESN'T CHECK THE NON-REFS. ****** */
  100. private int
  101. gstate_check_global(int_gstate *isp)
  102. {
  103. #define gsref_check(p) check_global(*(p))
  104.     int_gstate_map_refs(isp, gsref_check);
  105. #undef gsref_check
  106.     return 0;
  107. }
  108.  
  109. /* - gstate <gstate> */
  110. int
  111. zgstate(register os_ptr op)
  112. {    gs_state *pnew;
  113.     int_gstate *isp;
  114.     gs_memory_t *mem;
  115.     if ( !ialloc_is_local(idmemory) )
  116.     {    int code = gstate_check_global(istate);
  117.         if ( code < 0 ) return code;
  118.     }
  119.     mem = gs_state_swap_memory(igs, imemory);
  120.     pnew = gs_gstate(igs);
  121.     gs_state_swap_memory(igs, mem);
  122.     if ( pnew == 0 )
  123.         return_error(e_VMerror);
  124.     isp = gs_int_gstate(pnew);
  125.     int_gstate_map_refs(isp, ref_mark_new);
  126.     push(1);
  127.     make_istruct(op, a_all, pnew);
  128.     return 0;
  129. }
  130.  
  131. /* copy for gstates */
  132. private int
  133. zcopy_gstate(register os_ptr op)
  134. {    os_ptr op1 = op - 1;
  135.     gs_state *pgs;
  136.     gs_state *pgs1;
  137.     int_gstate *pistate;
  138.     gs_memory_t *mem;
  139.     int code;
  140.     check_stype(*op, st_gstate);
  141.     check_stype(*op1, st_gstate);
  142.     check_write(*op);
  143.     pgs = r_ptr(op, gs_state);
  144.     pgs1 = r_ptr(op1, gs_state);
  145.     pistate = gs_int_gstate(pgs);
  146.     if ( !r_is_global(op) )
  147.     {    code = gstate_check_global(gs_int_gstate(pgs1));
  148.         if ( code < 0 ) return code;
  149.     }
  150. #define gsref_save(p) ref_save(op, p, "copygstate")
  151.     int_gstate_map_refs(pistate, gsref_save);
  152. #undef gsref_save
  153.     mem = gs_state_swap_memory(pgs, imemory);
  154.     /****** DOESN'T GET FULLY UNDONE BY RESTORE ******/
  155.     code = gs_copygstate(pgs, pgs1);
  156.     gs_state_swap_memory(pgs, mem);
  157.     if ( code < 0 )
  158.       return code;
  159.     int_gstate_map_refs(pistate, ref_mark_new);
  160.     *op1 = *op;
  161.     pop(1);
  162.     return 0;
  163. }
  164.  
  165. /* <gstate> currentgstate <gstate> */
  166. int
  167. zcurrentgstate(register os_ptr op)
  168. {    gs_state *pgs;
  169.     int_gstate *pistate;
  170.     int code;
  171.     gs_memory_t *mem;
  172.     check_stype(*op, st_gstate);
  173.     check_write(*op);
  174.     pgs = r_ptr(op, gs_state);
  175.     pistate = gs_int_gstate(pgs);
  176.     if ( !r_is_global(op) )
  177.     {    code = gstate_check_global(istate);
  178.         if ( code < 0 ) return code;
  179.     }
  180. #define gsref_save(p) ref_save(op, p, "currentgstate")
  181.     int_gstate_map_refs(pistate, gsref_save);
  182. #undef gsref_save
  183.     mem = gs_state_swap_memory(pgs, imemory);
  184.     /****** DOESN'T GET FULLY UNDONE BY RESTORE ******/
  185.     code = gs_currentgstate(pgs, igs);
  186.     gs_state_swap_memory(pgs, mem);
  187.     if ( code < 0 )
  188.       return code;
  189.     int_gstate_map_refs(pistate, ref_mark_new);
  190.     return 0;
  191. }
  192.  
  193. /* <gstate> setgstate - */
  194. int
  195. zsetgstate(register os_ptr op)
  196. {    int code;
  197.     check_stype(*op, st_gstate);
  198.     check_read(*op);
  199.     code = gs_setgstate(igs, r_ptr(op, gs_state));
  200.     if ( code < 0 ) return code;
  201.     pop(1);
  202.     return 0;
  203. }
  204.  
  205. /* ------ Rectangles ------- */
  206.  
  207. /* We preallocate a short list for rectangles, because */
  208. /* the rectangle operators usually will involve very few rectangles. */
  209. #define max_local_rect 5
  210. typedef struct local_rects_s {
  211.     gs_rect *pr;
  212.     uint count;
  213.     gs_rect rl[max_local_rect];
  214. } local_rects;
  215.  
  216. /* Forward references */
  217. private int rect_get(P2(local_rects *, os_ptr));
  218. private void rect_release(P1(local_rects *));
  219.  
  220. /* <x> <y> <width> <height> rectappend - */
  221. /* <numarray|numstring> rectappend - */
  222. int
  223. zrectappend(os_ptr op)
  224. {    local_rects lr;
  225.     int npop = rect_get(&lr, op);
  226.     int code;
  227.     if ( npop < 0 ) return npop;
  228.     code = gs_rectappend(igs, lr.pr, lr.count);
  229.     rect_release(&lr);
  230.     if ( code < 0 ) return code;
  231.     pop(npop);
  232.     return 0;
  233. }
  234.  
  235. /* <x> <y> <width> <height> rectclip - */
  236. /* <numarray|numstring> rectclip - */
  237. int
  238. zrectclip(os_ptr op)
  239. {    local_rects lr;
  240.     int npop = rect_get(&lr, op);
  241.     int code;
  242.     if ( npop < 0 ) return npop;
  243.     code = gs_rectclip(igs, lr.pr, lr.count);
  244.     rect_release(&lr);
  245.     if ( code < 0 ) return code;
  246.     pop(npop);
  247.     return 0;
  248. }
  249.  
  250. /* <x> <y> <width> <height> rectfill - */
  251. /* <numarray|numstring> rectfill - */
  252. int
  253. zrectfill(os_ptr op)
  254. {    local_rects lr;
  255.     int npop = rect_get(&lr, op);
  256.     int code;
  257.     if ( npop < 0 ) return npop;
  258.     code = gs_rectfill(igs, lr.pr, lr.count);
  259.     rect_release(&lr);
  260.     if ( code < 0 ) return code;
  261.     pop(npop);
  262.     return 0;
  263. }
  264.  
  265. /* <x> <y> <width> <height> rectstroke - */
  266. /* <numarray|numstring> rectstroke - */
  267. int
  268. zrectstroke(os_ptr op)
  269. {    gs_matrix mat;
  270.     local_rects lr;
  271.     int npop, code;
  272.     if ( read_matrix(op, &mat) >= 0 )
  273.        {    /* Concatenate the matrix to the CTM just before */
  274.         /* stroking the path. */
  275.         npop = rect_get(&lr, op - 1);
  276.         if ( npop < 0 ) return npop;
  277.         code = gs_rectstroke(igs, lr.pr, lr.count, &mat);
  278.         npop++;
  279.        }
  280.     else
  281.        {    /* No matrix. */
  282.         npop = rect_get(&lr, op);
  283.         if ( npop < 0 ) return npop;
  284.         code = gs_rectstroke(igs, lr.pr, lr.count, (gs_matrix *)0);
  285.        }
  286.     rect_release(&lr);
  287.     if ( code < 0 ) return code;
  288.     pop(npop);
  289.     return 0;
  290. }
  291.  
  292. /* --- Internal routines --- */
  293.  
  294. /* Get rectangles from the stack. */
  295. /* Return the number of elements to pop (>0) if OK, <0 if error. */
  296. private int
  297. rect_get(local_rects *plr, os_ptr op)
  298. {    int format, code, npop;
  299.     stream st;
  300.     uint n, count;
  301.     gs_rect *pr;
  302.     switch ( r_type(op) )
  303.        {
  304.     case t_array:
  305.     case t_string:
  306.         code = sread_num_array(&st, op);
  307.         if ( code < 0 ) return code;
  308.         format = code;
  309.         count = scount_num_stream(&st, format);
  310.         if ( count % 4 )
  311.             return_error(e_typecheck);
  312.         count /= 4, npop = 1;
  313.         break;
  314.     default:            /* better be 4 numbers */
  315.         sread_string(&st, (byte *)(op - 3), sizeof(ref) * 4);
  316.         format = num_array;
  317.         count = 1, npop = 4;
  318.         break;
  319.        }
  320.     plr->count = count;
  321.     if ( count <= max_local_rect )
  322.         pr = plr->rl;
  323.     else
  324.     {    pr = (gs_rect *)ialloc_byte_array(count, sizeof(gs_rect),
  325.                           "rect_get");
  326.         if ( pr == 0 )
  327.             return_error(e_VMerror);
  328.     }
  329.     plr->pr = pr;
  330.     for ( n = 0; n < count; n++, pr++ )
  331.        {    ref rnum;
  332.         float rv[4];
  333.         int i;
  334.         for ( i = 0; i < 4; i++ )
  335.            {    switch ( code = sget_encoded_number(&st, format, &rnum) )
  336.                {
  337.             case t_integer:
  338.                 rv[i] = rnum.value.intval;
  339.                 break;
  340.             case t_real:
  341.                 rv[i] = rnum.value.realval;
  342.                 break;
  343.             default:    /* code < 0 */
  344.                 return code;
  345.                }
  346.            }
  347.         pr->q.x = (pr->p.x = rv[0]) + rv[2];
  348.         pr->q.y = (pr->p.y = rv[1]) + rv[3];
  349.        }
  350.     return npop;
  351. }
  352.  
  353. /* Release the rectangle list if needed. */
  354. private void
  355. rect_release(local_rects *plr)
  356. {    if ( plr->pr != plr->rl )
  357.         ifree_object(plr->pr, "rect_release");
  358. }
  359.  
  360. /* ------ Graphics state ------ */
  361.  
  362. /* <llx> <lly> <urx> <ury> setbbox - */
  363. int
  364. zsetbbox(register os_ptr op)
  365. {    float box[4];
  366.     int code = num_params(op, 4, box);
  367.     if ( code < 0 ) return code;
  368.     if ( (code = gs_setbbox(igs, box[0], box[1], box[2], box[3])) < 0 )
  369.         return code;
  370.     pop(4);
  371.     return 0;
  372. }
  373.  
  374. /* ------ Initialization procedure ------ */
  375.  
  376. op_def zdps1_l2_op_defs[] = {
  377.         op_def_begin_level2(),
  378.         /* Graphics state */
  379.     {"0currenthalftonephase", zcurrenthalftonephase},
  380.     {"0currentstrokeadjust", zcurrentstrokeadjust},
  381.     {"2sethalftonephase", zsethalftonephase},
  382.     {"1setstrokeadjust", zsetstrokeadjust},
  383.         /* Graphics state objects */
  384.     {"1currentgstate", zcurrentgstate},
  385.     {"0gstate", zgstate},
  386.     {"1setgstate", zsetgstate},
  387.         /* Rectangles */
  388.     {"1rectappend", zrectappend},
  389.     {"1rectclip", zrectclip},
  390.     {"1rectfill", zrectfill},
  391.     {"1rectstroke", zrectstroke},
  392.         /* Graphics state components */
  393.     {"4setbbox", zsetbbox},
  394.     op_def_end(zdps1_init)
  395. };
  396.