zdps1.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /* Copyright (C) 1990, 1996, 1997, 1998, 1999, 2000 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: zdps1.c,v 1.3 2000/12/02 20:39:37 lpd Exp $ */
  16. /* Level 2 / Display PostScript graphics extensions */
  17. #include "ghost.h"
  18. #include "oper.h"
  19. #include "gsmatrix.h"
  20. #include "gspath.h"
  21. #include "gspath2.h"
  22. #include "gsstate.h"
  23. #include "ialloc.h"
  24. #include "igstate.h"
  25. #include "ivmspace.h"
  26. #include "store.h"
  27. #include "stream.h"
  28. #include "ibnum.h"
  29. /* Forward references */
  30. private int gstate_unshare(P1(i_ctx_t *));
  31. /* Structure descriptors */
  32. public_st_igstate_obj();
  33. /* Extend the `copy' operator to deal with gstates. */
  34. /* This is done with a hack -- we know that gstates are the only */
  35. /* t_astruct subtype that implements copy. */
  36. private int
  37. z1copy(i_ctx_t *i_ctx_p)
  38. {
  39. os_ptr op = osp;
  40. int code = zcopy(i_ctx_p);
  41. if (code >= 0)
  42. return code;
  43. if (!r_has_type(op, t_astruct))
  44. return code;
  45. return zcopy_gstate(i_ctx_p);
  46. }
  47. /* ------ Graphics state ------ */
  48. /* <bool> setstrokeadjust - */
  49. private int
  50. zsetstrokeadjust(i_ctx_t *i_ctx_p)
  51. {
  52. os_ptr op = osp;
  53. check_type(*op, t_boolean);
  54. gs_setstrokeadjust(igs, op->value.boolval);
  55. pop(1);
  56. return 0;
  57. }
  58. /* - currentstrokeadjust <bool> */
  59. private int
  60. zcurrentstrokeadjust(i_ctx_t *i_ctx_p)
  61. {
  62. os_ptr op = osp;
  63. push(1);
  64. make_bool(op, gs_currentstrokeadjust(igs));
  65. return 0;
  66. }
  67. /* ------ Graphics state objects ------ */
  68. /*
  69. * Check to make sure that all the elements of a graphics state
  70. * can be stored in the given allocation space.
  71. */
  72. /* ****** DOESN'T CHECK THE NON-REFS. ****** */
  73. private int
  74. gstate_check_space(i_ctx_t *i_ctx_p, int_gstate *isp, uint space)
  75. {
  76. /*
  77. * ****** WORKAROUND ALERT ******
  78. * This code doesn't check the space of the non-refs, or copy their
  79. * contents, so it can create dangling references from global VM to
  80. * local VM. Because of this, we simply disallow writing into gstates
  81. * in global VM (including creating them in the first place) if the
  82. * save level is greater than 0.
  83. * ****** WORKAROUND ALERT ******
  84. */
  85. #if 1 /* ****** WORKAROUND ****** */
  86. if (space != avm_local && imemory_save_level(iimemory) > 0)
  87. return_error(e_invalidaccess);
  88. #endif /* ****** END ****** */
  89. #define gsref_check(p) store_check_space(space, p)
  90. int_gstate_map_refs(isp, gsref_check);
  91. #undef gsref_check
  92. return 0;
  93. }
  94. /* - gstate <gstate> */
  95. int
  96. zgstate(i_ctx_t *i_ctx_p)
  97. {
  98. os_ptr op = osp;
  99. int code = gstate_check_space(i_ctx_p, istate, icurrent_space);
  100. igstate_obj *pigo;
  101. gs_state *pnew;
  102. int_gstate *isp;
  103. if (code < 0)
  104. return code;
  105. pigo = ialloc_struct(igstate_obj, &st_igstate_obj, "gstate");
  106. if (pigo == 0)
  107. return_error(e_VMerror);
  108. pnew = gs_state_copy(igs, imemory);
  109. if (pnew == 0) {
  110. ifree_object(pigo, "gstate");
  111. return_error(e_VMerror);
  112. }
  113. isp = gs_int_gstate(pnew);
  114. int_gstate_map_refs(isp, ref_mark_new);
  115. push(1);
  116. /*
  117. * Since igstate_obj isn't a ref, but only contains a ref, save won't
  118. * clear its l_new bit automatically, and restore won't set it
  119. * automatically; we have to make sure this ref is on the changes chain.
  120. */
  121. make_iastruct(op, a_all, pigo);
  122. make_null(&pigo->gstate);
  123. ref_save(op, &pigo->gstate, "gstate");
  124. make_istruct_new(&pigo->gstate, 0, pnew);
  125. return 0;
  126. }
  127. /* copy for gstates */
  128. int
  129. zcopy_gstate(i_ctx_t *i_ctx_p)
  130. {
  131. os_ptr op = osp;
  132. os_ptr op1 = op - 1;
  133. gs_state *pgs;
  134. gs_state *pgs1;
  135. int_gstate *pistate;
  136. gs_memory_t *mem;
  137. int code;
  138. check_stype(*op, st_igstate_obj);
  139. check_stype(*op1, st_igstate_obj);
  140. check_write(*op);
  141. code = gstate_unshare(i_ctx_p);
  142. if (code < 0)
  143. return code;
  144. pgs = igstate_ptr(op);
  145. pgs1 = igstate_ptr(op1);
  146. pistate = gs_int_gstate(pgs);
  147. code = gstate_check_space(i_ctx_p, gs_int_gstate(pgs1), r_space(op));
  148. if (code < 0)
  149. return code;
  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. code = gs_copygstate(pgs, pgs1);
  155. gs_state_swap_memory(pgs, mem);
  156. if (code < 0)
  157. return code;
  158. int_gstate_map_refs(pistate, ref_mark_new);
  159. *op1 = *op;
  160. pop(1);
  161. return 0;
  162. }
  163. /* <gstate> currentgstate <gstate> */
  164. int
  165. zcurrentgstate(i_ctx_t *i_ctx_p)
  166. {
  167. os_ptr op = osp;
  168. gs_state *pgs;
  169. int_gstate *pistate;
  170. int code;
  171. gs_memory_t *mem;
  172. check_stype(*op, st_igstate_obj);
  173. check_write(*op);
  174. code = gstate_unshare(i_ctx_p);
  175. if (code < 0)
  176. return code;
  177. pgs = igstate_ptr(op);
  178. pistate = gs_int_gstate(pgs);
  179. code = gstate_check_space(i_ctx_p, istate, r_space(op));
  180. if (code < 0)
  181. return code;
  182. #define gsref_save(p) ref_save(op, p, "currentgstate")
  183. int_gstate_map_refs(pistate, gsref_save);
  184. #undef gsref_save
  185. mem = gs_state_swap_memory(pgs, imemory);
  186. code = gs_currentgstate(pgs, igs);
  187. gs_state_swap_memory(pgs, mem);
  188. if (code < 0)
  189. return code;
  190. int_gstate_map_refs(pistate, ref_mark_new);
  191. return 0;
  192. }
  193. /* <gstate> setgstate - */
  194. int
  195. zsetgstate(i_ctx_t *i_ctx_p)
  196. {
  197. os_ptr op = osp;
  198. int code;
  199. check_stype(*op, st_igstate_obj);
  200. check_read(*op);
  201. code = gs_setgstate(igs, igstate_ptr(op));
  202. if (code < 0)
  203. return code;
  204. pop(1);
  205. return 0;
  206. }
  207. /* ------ Rectangles ------- */
  208. /*
  209. * We preallocate a short list for rectangles, because
  210. * the rectangle operators usually will involve very few rectangles.
  211. */
  212. #define MAX_LOCAL_RECTS 5
  213. typedef struct local_rects_s {
  214. gs_rect *pr;
  215. uint count;
  216. gs_rect rl[MAX_LOCAL_RECTS];
  217. } local_rects_t;
  218. /* Forward references */
  219. private int rect_get(P3(local_rects_t *, os_ptr, gs_memory_t *));
  220. private void rect_release(P2(local_rects_t *, gs_memory_t *));
  221. /* <x> <y> <width> <height> .rectappend - */
  222. /* <numarray|numstring> .rectappend - */
  223. private int
  224. zrectappend(i_ctx_t *i_ctx_p)
  225. {
  226. os_ptr op = osp;
  227. local_rects_t lr;
  228. int npop = rect_get(&lr, op, imemory);
  229. int code;
  230. if (npop < 0)
  231. return npop;
  232. code = gs_rectappend(igs, lr.pr, lr.count);
  233. rect_release(&lr, imemory);
  234. if (code < 0)
  235. return code;
  236. pop(npop);
  237. return 0;
  238. }
  239. /* <x> <y> <width> <height> rectclip - */
  240. /* <numarray|numstring> rectclip - */
  241. private int
  242. zrectclip(i_ctx_t *i_ctx_p)
  243. {
  244. os_ptr op = osp;
  245. local_rects_t lr;
  246. int npop = rect_get(&lr, op, imemory);
  247. int code;
  248. if (npop < 0)
  249. return npop;
  250. code = gs_rectclip(igs, lr.pr, lr.count);
  251. rect_release(&lr, imemory);
  252. if (code < 0)
  253. return code;
  254. pop(npop);
  255. return 0;
  256. }
  257. /* <x> <y> <width> <height> rectfill - */
  258. /* <numarray|numstring> rectfill - */
  259. private int
  260. zrectfill(i_ctx_t *i_ctx_p)
  261. {
  262. os_ptr op = osp;
  263. local_rects_t lr;
  264. int npop = rect_get(&lr, op, imemory);
  265. int code;
  266. if (npop < 0)
  267. return npop;
  268. code = gs_rectfill(igs, lr.pr, lr.count);
  269. rect_release(&lr, imemory);
  270. if (code < 0)
  271. return code;
  272. pop(npop);
  273. return 0;
  274. }
  275. /* <x> <y> <width> <height> rectstroke - */
  276. /* <numarray|numstring> rectstroke - */
  277. private int
  278. zrectstroke(i_ctx_t *i_ctx_p)
  279. {
  280. os_ptr op = osp;
  281. gs_matrix mat;
  282. local_rects_t lr;
  283. int npop, code;
  284. if (read_matrix(op, &mat) >= 0) {
  285. /* Concatenate the matrix to the CTM just before stroking the path. */
  286. npop = rect_get(&lr, op - 1, imemory);
  287. if (npop < 0)
  288. return npop;
  289. code = gs_rectstroke(igs, lr.pr, lr.count, &mat);
  290. npop++;
  291. } else {
  292. /* No matrix. */
  293. npop = rect_get(&lr, op, imemory);
  294. if (npop < 0)
  295. return npop;
  296. code = gs_rectstroke(igs, lr.pr, lr.count, (gs_matrix *) 0);
  297. }
  298. rect_release(&lr, imemory);
  299. if (code < 0)
  300. return code;
  301. pop(npop);
  302. return 0;
  303. }
  304. /* --- Internal routines --- */
  305. /* Get rectangles from the stack. */
  306. /* Return the number of elements to pop (>0) if OK, <0 if error. */
  307. private int
  308. rect_get(local_rects_t * plr, os_ptr op, gs_memory_t *mem)
  309. {
  310. int format, code;
  311. uint n, count;
  312. gs_rect *pr;
  313. double rv[4];
  314. switch (r_type(op)) {
  315. case t_array:
  316. case t_mixedarray:
  317. case t_shortarray:
  318. case t_string:
  319. code = num_array_format(op);
  320. if (code < 0)
  321. return code;
  322. format = code;
  323. count = num_array_size(op, format);
  324. if (count % 4)
  325. return_error(e_rangecheck);
  326. count /= 4;
  327. break;
  328. default: /* better be 4 numbers */
  329. code = num_params(op, 4, rv);
  330. if (code < 0)
  331. return code;
  332. plr->pr = plr->rl;
  333. plr->count = 1;
  334. plr->rl[0].q.x = (plr->rl[0].p.x = rv[0]) + rv[2];
  335. plr->rl[0].q.y = (plr->rl[0].p.y = rv[1]) + rv[3];
  336. return 4;
  337. }
  338. plr->count = count;
  339. if (count <= MAX_LOCAL_RECTS)
  340. pr = plr->rl;
  341. else {
  342. pr = (gs_rect *)gs_alloc_byte_array(mem, count, sizeof(gs_rect),
  343. "rect_get");
  344. if (pr == 0)
  345. return_error(e_VMerror);
  346. }
  347. plr->pr = pr;
  348. for (n = 0; n < count; n++, pr++) {
  349. ref rnum;
  350. int i;
  351. for (i = 0; i < 4; i++) {
  352. code = num_array_get((const ref *)op, format,
  353. (n << 2) + i, &rnum);
  354. switch (code) {
  355. case t_integer:
  356. rv[i] = rnum.value.intval;
  357. break;
  358. case t_real:
  359. rv[i] = rnum.value.realval;
  360. break;
  361. default: /* code < 0 */
  362. return code;
  363. }
  364. }
  365. pr->q.x = (pr->p.x = rv[0]) + rv[2];
  366. pr->q.y = (pr->p.y = rv[1]) + rv[3];
  367. }
  368. return 1;
  369. }
  370. /* Release the rectangle list if needed. */
  371. private void
  372. rect_release(local_rects_t * plr, gs_memory_t *mem)
  373. {
  374. if (plr->pr != plr->rl)
  375. gs_free_object(mem, plr->pr, "rect_release");
  376. }
  377. /* ------ Graphics state ------ */
  378. /* <llx> <lly> <urx> <ury> setbbox - */
  379. int
  380. zsetbbox(i_ctx_t *i_ctx_p)
  381. {
  382. os_ptr op = osp;
  383. double box[4];
  384. int code = num_params(op, 4, box);
  385. if (code < 0)
  386. return code;
  387. if ((code = gs_setbbox(igs, box[0], box[1], box[2], box[3])) < 0)
  388. return code;
  389. pop(4);
  390. return 0;
  391. }
  392. /* ------ Initialization procedure ------ */
  393. const op_def zdps1_l2_op_defs[] =
  394. {
  395. op_def_begin_level2(),
  396. /* Graphics state */
  397. {"0currentstrokeadjust", zcurrentstrokeadjust},
  398. {"1setstrokeadjust", zsetstrokeadjust},
  399. /* Graphics state objects */
  400. {"1copy", z1copy},
  401. {"1currentgstate", zcurrentgstate},
  402. {"0gstate", zgstate},
  403. {"1setgstate", zsetgstate},
  404. /* Rectangles */
  405. {"1.rectappend", zrectappend},
  406. {"1rectclip", zrectclip},
  407. {"1rectfill", zrectfill},
  408. {"1rectstroke", zrectstroke},
  409. /* Graphics state components */
  410. {"4setbbox", zsetbbox},
  411. op_def_end(0)
  412. };
  413. /* ------ Internal routines ------ */
  414. /* Ensure that a gstate is not shared with an outer save level. */
  415. /* *op is of type t_astruct(igstate_obj). */
  416. private int
  417. gstate_unshare(i_ctx_t *i_ctx_p)
  418. {
  419. os_ptr op = osp;
  420. ref *pgsref = &r_ptr(op, igstate_obj)->gstate;
  421. gs_state *pgs = r_ptr(pgsref, gs_state);
  422. gs_state *pnew;
  423. int_gstate *isp;
  424. if (!ref_must_save(pgsref))
  425. return 0;
  426. /* Copy the gstate. */
  427. pnew = gs_gstate(pgs);
  428. if (pnew == 0)
  429. return_error(e_VMerror);
  430. isp = gs_int_gstate(pnew);
  431. int_gstate_map_refs(isp, ref_mark_new);
  432. ref_do_save(op, pgsref, "gstate_unshare");
  433. make_istruct_new(pgsref, 0, pnew);
  434. return 0;
  435. }