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

  1. /* Copyright (C) 1989, 1990, 1991, 1993 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. /* zpath2.c */
  20. /* Non-constructor path operators */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "oper.h"
  24. #include "errors.h"
  25. #include "estack.h"    /* for pathforall */
  26. #include "ialloc.h"
  27. #include "igstate.h"
  28. #include "gsstruct.h"
  29. #include "gspath.h"
  30. #include "store.h"
  31.  
  32. /* - flattenpath - */
  33. int
  34. zflattenpath(register os_ptr op)
  35. {    return gs_flattenpath(igs);
  36. }
  37.  
  38. /* - reversepath - */
  39. int
  40. zreversepath(register os_ptr op)
  41. {    return gs_reversepath(igs);
  42. }
  43.  
  44. /* - strokepath - */
  45. int
  46. zstrokepath(register os_ptr op)
  47. {    return gs_strokepath(igs);
  48. }
  49.  
  50. /* - clippath - */
  51. int
  52. zclippath(register os_ptr op)
  53. {    return gs_clippath(igs);
  54. }
  55.  
  56. /* - pathbbox <llx> <lly> <urx> <ury> */
  57. int
  58. zpathbbox(register os_ptr op)
  59. {    gs_rect box;
  60.     int code = gs_pathbbox(igs, &box);
  61.     if ( code < 0 ) return code;
  62.     push(4);
  63.     make_real(op - 3, box.p.x);
  64.     make_real(op - 2, box.p.y);
  65.     make_real(op - 1, box.q.x);
  66.     make_real(op, box.q.y);
  67.     return 0;
  68. }
  69.  
  70. /* <moveproc> <lineproc> <curveproc> <closeproc> pathforall - */
  71. private int path_continue(P1(os_ptr));
  72. private int path_cleanup(P1(os_ptr));
  73. int
  74. zpathforall(register os_ptr op)
  75. {    gs_path_enum *penum;
  76.     int code;
  77.     check_proc(op[-3]);
  78.     check_proc(op[-2]);
  79.     check_proc(op[-1]);
  80.     check_proc(*op);
  81.     check_estack(8);
  82.     if ( (penum = gs_path_enum_alloc(imemory, "pathforall")) == 0 )
  83.         return_error(e_VMerror);
  84.     code = gs_path_enum_init(penum, igs);
  85.     if ( code < 0 )
  86.     {    ifree_object(penum, "path_cleanup");
  87.         return code;
  88.     }
  89.     /* Push a mark, the four procedures, and the path enumerator, */
  90.     /* and then call the continuation procedure. */
  91.     push_mark_estack(es_for, path_cleanup);    /* iterator */
  92.     memcpy(esp + 1, op - 3, 4 * sizeof(ref));    /* 4 procs */
  93.     esp += 5;
  94.     make_istruct(esp, 0, penum);
  95.     pop(4);  op -= 4;
  96.     return path_continue(op);
  97. }
  98. /* Continuation procedure for pathforall */
  99. private void pf_push(P3(gs_point *, int, os_ptr));
  100. private int
  101. path_continue(register os_ptr op)
  102. {    gs_path_enum *penum = r_ptr(esp, gs_path_enum);
  103.     gs_point ppts[3];
  104.     int code;
  105.     /* Make sure we have room on the o-stack for the worst case */
  106.     /* before we enumerate the next path element. */
  107.     check_ostack(6);    /* 3 points for curveto */
  108.     code = gs_path_enum_next(penum, ppts);
  109.     switch ( code )
  110.       {
  111.     case 0:            /* all done */
  112.         { esp -= 6;
  113.           path_cleanup(op);
  114.           return o_pop_estack;
  115.         }
  116.     default:        /* error */
  117.         return code;
  118.     case gs_pe_moveto:
  119.         esp[2] = esp[-4];            /* moveto proc */
  120.         pf_push(ppts, 1, op);
  121.         break;
  122.     case gs_pe_lineto:
  123.         esp[2] = esp[-3];            /* lineto proc */
  124.         pf_push(ppts, 1, op);
  125.         break;
  126.     case gs_pe_curveto:
  127.         esp[2] = esp[-2];            /* curveto proc */
  128.         pf_push(ppts, 3, op);
  129.         break;
  130.     case gs_pe_closepath:
  131.         esp[2] = esp[-1];            /* closepath proc */
  132.         break;
  133.       }
  134.     push_op_estack(path_continue);
  135.     ++esp;        /* include pushed procedure */
  136.     return o_push_estack;
  137. }
  138. /* Internal procedure to push one or more points */
  139. private void
  140. pf_push(gs_point *ppts, int n, os_ptr op)
  141. {    while ( n-- )
  142.       { op += 2;
  143.         make_real(op - 1, ppts->x);
  144.         make_real(op, ppts->y);
  145.         ppts++;
  146.       }
  147.     osp = op;
  148. }
  149. /* Clean up after a pathforall */
  150. private int
  151. path_cleanup(os_ptr op)
  152. {    gs_path_enum *penum = r_ptr(esp + 6, gs_path_enum);
  153.     gs_path_enum_cleanup(penum);
  154.     ifree_object(penum, "path_cleanup");
  155.     return 0;
  156. }
  157.  
  158. /* - initclip - */
  159. int
  160. zinitclip(register os_ptr op)
  161. {    return gs_initclip(igs);
  162. }
  163.  
  164. /* - clip - */
  165. int
  166. zclip(register os_ptr op)
  167. {    return gs_clip(igs);
  168. }
  169.  
  170. /* - eoclip - */
  171. int
  172. zeoclip(register os_ptr op)
  173. {    return gs_eoclip(igs);
  174. }
  175.  
  176. /* ------ Initialization procedure ------ */
  177.  
  178. op_def zpath2_op_defs[] = {
  179.     {"0clip", zclip},
  180.     {"0clippath", zclippath},
  181.     {"0eoclip", zeoclip},
  182.     {"0flattenpath", zflattenpath},
  183.     {"0initclip", zinitclip},
  184.     {"0pathbbox", zpathbbox},
  185.     {"4pathforall", zpathforall},
  186.     {"0reversepath", zreversepath},
  187.     {"0strokepath", zstrokepath},
  188.         /* Internal operators */
  189.     {"0%path_continue", path_continue},
  190.     op_def_end(0)
  191. };
  192.