zpath1.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* Copyright (C) 1989, 1995, 1997, 1998, 1999 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: zpath1.c,v 1.5 2002/06/16 03:43:51 lpd Exp $ */
  14. /* PostScript Level 1 additional path operators */
  15. #include "memory_.h"
  16. #include "ghost.h"
  17. #include "oper.h"
  18. #include "oparc.h" /* for prototypes */
  19. #include "estack.h" /* for pathforall */
  20. #include "ialloc.h"
  21. #include "igstate.h"
  22. #include "gsstruct.h"
  23. #include "gspath.h"
  24. #include "store.h"
  25. /* Forward references */
  26. private int common_arc(i_ctx_t *,
  27. int (*)(gs_state *, floatp, floatp, floatp, floatp, floatp));
  28. private int common_arct(i_ctx_t *, float *);
  29. /* <x> <y> <r> <ang1> <ang2> arc - */
  30. int
  31. zarc(i_ctx_t *i_ctx_p)
  32. {
  33. return common_arc(i_ctx_p, gs_arc);
  34. }
  35. /* <x> <y> <r> <ang1> <ang2> arcn - */
  36. int
  37. zarcn(i_ctx_t *i_ctx_p)
  38. {
  39. return common_arc(i_ctx_p, gs_arcn);
  40. }
  41. /* Common code for arc[n] */
  42. private int
  43. common_arc(i_ctx_t *i_ctx_p,
  44. int (*aproc)(gs_state *, floatp, floatp, floatp, floatp, floatp))
  45. {
  46. os_ptr op = osp;
  47. double xyra[5]; /* x, y, r, ang1, ang2 */
  48. int code = num_params(op, 5, xyra);
  49. if (code < 0)
  50. return code;
  51. code = (*aproc)(igs, xyra[0], xyra[1], xyra[2], xyra[3], xyra[4]);
  52. if (code >= 0)
  53. pop(5);
  54. return code;
  55. }
  56. /* <x1> <y1> <x2> <y2> <r> arct - */
  57. int
  58. zarct(i_ctx_t *i_ctx_p)
  59. {
  60. int code = common_arct(i_ctx_p, (float *)0);
  61. if (code < 0)
  62. return code;
  63. pop(5);
  64. return 0;
  65. }
  66. /* <x1> <y1> <x2> <y2> <r> arcto <xt1> <yt1> <xt2> <yt2> */
  67. private int
  68. zarcto(i_ctx_t *i_ctx_p)
  69. {
  70. os_ptr op = osp;
  71. float tanxy[4]; /* xt1, yt1, xt2, yt2 */
  72. int code = common_arct(i_ctx_p, tanxy);
  73. if (code < 0)
  74. return code;
  75. make_real(op - 4, tanxy[0]);
  76. make_real(op - 3, tanxy[1]);
  77. make_real(op - 2, tanxy[2]);
  78. make_real(op - 1, tanxy[3]);
  79. pop(1);
  80. return 0;
  81. }
  82. /* Common code for arct[o] */
  83. private int
  84. common_arct(i_ctx_t *i_ctx_p, float *tanxy)
  85. {
  86. os_ptr op = osp;
  87. double args[5]; /* x1, y1, x2, y2, r */
  88. int code = num_params(op, 5, args);
  89. if (code < 0)
  90. return code;
  91. return gs_arcto(igs, args[0], args[1], args[2], args[3], args[4], tanxy);
  92. }
  93. /* - .dashpath - */
  94. private int
  95. zdashpath(i_ctx_t *i_ctx_p)
  96. {
  97. return gs_dashpath(igs);
  98. }
  99. /* - flattenpath - */
  100. private int
  101. zflattenpath(i_ctx_t *i_ctx_p)
  102. {
  103. return gs_flattenpath(igs);
  104. }
  105. /* - reversepath - */
  106. private int
  107. zreversepath(i_ctx_t *i_ctx_p)
  108. {
  109. return gs_reversepath(igs);
  110. }
  111. /* - strokepath - */
  112. private int
  113. zstrokepath(i_ctx_t *i_ctx_p)
  114. {
  115. return gs_strokepath(igs);
  116. }
  117. /* - clippath - */
  118. private int
  119. zclippath(i_ctx_t *i_ctx_p)
  120. {
  121. return gs_clippath(igs);
  122. }
  123. /* <bool> .pathbbox <llx> <lly> <urx> <ury> */
  124. private int
  125. zpathbbox(i_ctx_t *i_ctx_p)
  126. {
  127. os_ptr op = osp;
  128. gs_rect box;
  129. int code;
  130. check_type(*op, t_boolean);
  131. code = gs_upathbbox(igs, &box, op->value.boolval);
  132. if (code < 0)
  133. return code;
  134. push(3);
  135. make_real(op - 3, box.p.x);
  136. make_real(op - 2, box.p.y);
  137. make_real(op - 1, box.q.x);
  138. make_real(op, box.q.y);
  139. return 0;
  140. }
  141. /* <moveproc> <lineproc> <curveproc> <closeproc> pathforall - */
  142. private int path_continue(i_ctx_t *);
  143. private int path_cleanup(i_ctx_t *);
  144. private int
  145. zpathforall(i_ctx_t *i_ctx_p)
  146. {
  147. os_ptr op = osp;
  148. gs_path_enum *penum;
  149. int code;
  150. check_proc(op[-3]);
  151. check_proc(op[-2]);
  152. check_proc(op[-1]);
  153. check_proc(*op);
  154. check_estack(8);
  155. if ((penum = gs_path_enum_alloc(imemory, "pathforall")) == 0)
  156. return_error(e_VMerror);
  157. code = gs_path_enum_init(penum, igs);
  158. if (code < 0) {
  159. ifree_object(penum, "path_cleanup");
  160. return code;
  161. }
  162. /* Push a mark, the four procedures, and the path enumerator. */
  163. push_mark_estack(es_for, path_cleanup); /* iterator */
  164. memcpy(esp + 1, op - 3, 4 * sizeof(ref)); /* 4 procs */
  165. esp += 5;
  166. make_istruct(esp, 0, penum);
  167. push_op_estack(path_continue);
  168. pop(4);
  169. op -= 4;
  170. return o_push_estack;
  171. }
  172. /* Continuation procedure for pathforall */
  173. private void pf_push(i_ctx_t *, gs_point *, int);
  174. private int
  175. path_continue(i_ctx_t *i_ctx_p)
  176. {
  177. gs_path_enum *penum = r_ptr(esp, gs_path_enum);
  178. gs_point ppts[3];
  179. int code;
  180. /* Make sure we have room on the o-stack for the worst case */
  181. /* before we enumerate the next path element. */
  182. check_ostack(6); /* 3 points for curveto */
  183. code = gs_path_enum_next(penum, ppts);
  184. switch (code) {
  185. case 0: /* all done */
  186. esp -= 6;
  187. path_cleanup(i_ctx_p);
  188. return o_pop_estack;
  189. default: /* error */
  190. return code;
  191. case gs_pe_moveto:
  192. esp[2] = esp[-4]; /* moveto proc */
  193. pf_push(i_ctx_p, ppts, 1);
  194. break;
  195. case gs_pe_lineto:
  196. esp[2] = esp[-3]; /* lineto proc */
  197. pf_push(i_ctx_p, ppts, 1);
  198. break;
  199. case gs_pe_curveto:
  200. esp[2] = esp[-2]; /* curveto proc */
  201. pf_push(i_ctx_p, ppts, 3);
  202. break;
  203. case gs_pe_closepath:
  204. esp[2] = esp[-1]; /* closepath proc */
  205. break;
  206. }
  207. push_op_estack(path_continue);
  208. ++esp; /* include pushed procedure */
  209. return o_push_estack;
  210. }
  211. /* Internal procedure to push one or more points */
  212. private void
  213. pf_push(i_ctx_t *i_ctx_p, gs_point * ppts, int n)
  214. {
  215. os_ptr op = osp;
  216. while (n--) {
  217. op += 2;
  218. make_real(op - 1, ppts->x);
  219. make_real(op, ppts->y);
  220. ppts++;
  221. }
  222. osp = op;
  223. }
  224. /* Clean up after a pathforall */
  225. private int
  226. path_cleanup(i_ctx_t *i_ctx_p)
  227. {
  228. gs_path_enum *penum = r_ptr(esp + 6, gs_path_enum);
  229. gs_path_enum_cleanup(penum);
  230. ifree_object(penum, "path_cleanup");
  231. return 0;
  232. }
  233. /* ------ Initialization procedure ------ */
  234. const op_def zpath1_op_defs[] =
  235. {
  236. {"5arc", zarc},
  237. {"5arcn", zarcn},
  238. {"5arct", zarct},
  239. {"5arcto", zarcto},
  240. {"0clippath", zclippath},
  241. {"0.dashpath", zdashpath},
  242. {"0flattenpath", zflattenpath},
  243. {"4pathforall", zpathforall},
  244. {"0reversepath", zreversepath},
  245. {"0strokepath", zstrokepath},
  246. {"1.pathbbox", zpathbbox},
  247. /* Internal operators */
  248. {"0%path_continue", path_continue},
  249. op_def_end(0)
  250. };