zdevice2.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* Copyright (C) 1993, 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: zdevice2.c,v 1.10 2005/10/04 06:30:02 ray Exp $ */
  14. /* Level 2 device operators */
  15. #include "math_.h"
  16. #include "memory_.h"
  17. #include "ghost.h"
  18. #include "oper.h"
  19. #include "dstack.h" /* for dict_find_name */
  20. #include "estack.h"
  21. #include "idict.h"
  22. #include "idparam.h"
  23. #include "igstate.h"
  24. #include "iname.h"
  25. #include "iutil.h"
  26. #include "store.h"
  27. #include "gxdevice.h"
  28. #include "gsstate.h"
  29. /* Exported for zfunc4.c */
  30. int z2copy(i_ctx_t *);
  31. /* Forward references */
  32. private int z2copy_gstate(i_ctx_t *);
  33. private int push_callout(i_ctx_t *, const char *);
  34. /* Extend the `copy' operator to deal with gstates. */
  35. /* This is done with a hack -- we know that gstates are the only */
  36. /* t_astruct subtype that implements copy. */
  37. /* We export this for recognition in FunctionType 4 functions. */
  38. int
  39. z2copy(i_ctx_t *i_ctx_p)
  40. {
  41. os_ptr op = osp;
  42. int code = zcopy(i_ctx_p);
  43. if (code >= 0)
  44. return code;
  45. if (!r_has_type(op, t_astruct))
  46. return code;
  47. return z2copy_gstate(i_ctx_p);
  48. }
  49. /* - .currentshowpagecount <count> true */
  50. /* - .currentshowpagecount false */
  51. private int
  52. zcurrentshowpagecount(i_ctx_t *i_ctx_p)
  53. {
  54. os_ptr op = osp;
  55. gx_device *dev = gs_currentdevice(igs);
  56. if ((*dev_proc(dev, get_page_device))(dev) == 0) {
  57. push(1);
  58. make_false(op);
  59. } else {
  60. push(2);
  61. make_int(op - 1, dev->ShowpageCount);
  62. make_true(op);
  63. }
  64. return 0;
  65. }
  66. /* - .currentpagedevice <dict> <bool> */
  67. private int
  68. zcurrentpagedevice(i_ctx_t *i_ctx_p)
  69. {
  70. os_ptr op = osp;
  71. gx_device *dev = gs_currentdevice(igs);
  72. push(2);
  73. if ((*dev_proc(dev, get_page_device))(dev) != 0) {
  74. op[-1] = istate->pagedevice;
  75. make_true(op);
  76. } else {
  77. make_null(op - 1);
  78. make_false(op);
  79. }
  80. return 0;
  81. }
  82. /* <local_dict|null> .setpagedevice - */
  83. private int
  84. zsetpagedevice(i_ctx_t *i_ctx_p)
  85. {
  86. os_ptr op = osp;
  87. int code;
  88. /******
  89. if ( igs->in_cachedevice )
  90. return_error(e_undefined);
  91. ******/
  92. if (r_has_type(op, t_dictionary)) {
  93. check_dict_read(*op);
  94. #if 0 /****************/
  95. /*
  96. * In order to avoid invalidaccess errors on setpagedevice,
  97. * the dictionary must be allocated in local VM.
  98. */
  99. if (!(r_is_local(op)))
  100. return_error(e_invalidaccess);
  101. #endif /****************/
  102. /* Make the dictionary read-only. */
  103. code = zreadonly(i_ctx_p);
  104. if (code < 0)
  105. return code;
  106. } else {
  107. check_type(*op, t_null);
  108. }
  109. istate->pagedevice = *op;
  110. pop(1);
  111. return 0;
  112. }
  113. /* Default Install/BeginPage/EndPage procedures */
  114. /* that just call the procedure in the device. */
  115. /* - .callinstall - */
  116. private int
  117. zcallinstall(i_ctx_t *i_ctx_p)
  118. {
  119. gx_device *dev = gs_currentdevice(igs);
  120. if ((dev = (*dev_proc(dev, get_page_device))(dev)) != 0) {
  121. int code = (*dev->page_procs.install) (dev, igs);
  122. if (code < 0)
  123. return code;
  124. }
  125. return 0;
  126. }
  127. /* <showpage_count> .callbeginpage - */
  128. private int
  129. zcallbeginpage(i_ctx_t *i_ctx_p)
  130. {
  131. os_ptr op = osp;
  132. gx_device *dev = gs_currentdevice(igs);
  133. check_type(*op, t_integer);
  134. if ((dev = (*dev_proc(dev, get_page_device))(dev)) != 0) {
  135. int code = (*dev->page_procs.begin_page)(dev, igs);
  136. if (code < 0)
  137. return code;
  138. }
  139. pop(1);
  140. return 0;
  141. }
  142. /* <showpage_count> <reason_int> .callendpage <flush_bool> */
  143. private int
  144. zcallendpage(i_ctx_t *i_ctx_p)
  145. {
  146. os_ptr op = osp;
  147. gx_device *dev = gs_currentdevice(igs);
  148. int code;
  149. check_type(op[-1], t_integer);
  150. check_type(*op, t_integer);
  151. if ((dev = (*dev_proc(dev, get_page_device))(dev)) != 0) {
  152. code = (*dev->page_procs.end_page)(dev, (int)op->value.intval, igs);
  153. if (code < 0)
  154. return code;
  155. if (code > 1)
  156. return_error(e_rangecheck);
  157. } else {
  158. code = (op->value.intval == 2 ? 0 : 1);
  159. }
  160. make_bool(op - 1, code);
  161. pop(1);
  162. return 0;
  163. }
  164. /* ------ Wrappers for operators that save the graphics state. ------ */
  165. /* When saving the state with the current device a page device, */
  166. /* we need to make sure that the page device dictionary exists */
  167. /* so that grestore can use it to reset the device parameters. */
  168. /* This may have significant performance consequences, but we don't see */
  169. /* any way around it. */
  170. /* Check whether we need to call out to create the page device dictionary. */
  171. private bool
  172. save_page_device(gs_state *pgs)
  173. {
  174. return
  175. (r_has_type(&gs_int_gstate(pgs)->pagedevice, t_null) &&
  176. (*dev_proc(gs_currentdevice(pgs), get_page_device))(gs_currentdevice(pgs)) != 0);
  177. }
  178. /* - gsave - */
  179. private int
  180. z2gsave(i_ctx_t *i_ctx_p)
  181. {
  182. if (!save_page_device(igs))
  183. return gs_gsave(igs);
  184. return push_callout(i_ctx_p, "%gsavepagedevice");
  185. }
  186. /* - save - */
  187. private int
  188. z2save(i_ctx_t *i_ctx_p)
  189. {
  190. if (!save_page_device(igs))
  191. return zsave(i_ctx_p);
  192. return push_callout(i_ctx_p, "%savepagedevice");
  193. }
  194. /* - gstate <gstate> */
  195. private int
  196. z2gstate(i_ctx_t *i_ctx_p)
  197. {
  198. if (!save_page_device(igs))
  199. return zgstate(i_ctx_p);
  200. return push_callout(i_ctx_p, "%gstatepagedevice");
  201. }
  202. /* <gstate1> <gstate2> copy <gstate2> */
  203. private int
  204. z2copy_gstate(i_ctx_t *i_ctx_p)
  205. {
  206. if (!save_page_device(igs))
  207. return zcopy_gstate(i_ctx_p);
  208. return push_callout(i_ctx_p, "%copygstatepagedevice");
  209. }
  210. /* <gstate> currentgstate <gstate> */
  211. private int
  212. z2currentgstate(i_ctx_t *i_ctx_p)
  213. {
  214. if (!save_page_device(igs))
  215. return zcurrentgstate(i_ctx_p);
  216. return push_callout(i_ctx_p, "%currentgstatepagedevice");
  217. }
  218. /* ------ Wrappers for operators that reset the graphics state. ------ */
  219. /* Check whether we need to call out to restore the page device. */
  220. private bool
  221. restore_page_device(const gs_state * pgs_old, const gs_state * pgs_new)
  222. {
  223. gx_device *dev_old = gs_currentdevice(pgs_old);
  224. gx_device *dev_new;
  225. gx_device *dev_t1;
  226. gx_device *dev_t2;
  227. bool samepagedevice = obj_eq(dev_old->memory, &gs_int_gstate(pgs_old)->pagedevice,
  228. &gs_int_gstate(pgs_new)->pagedevice);
  229. if ((dev_t1 = (*dev_proc(dev_old, get_page_device)) (dev_old)) == 0)
  230. return false;
  231. /* If we are going to putdeviceparams in a callout, we need to */
  232. /* unlock temporarily. The device will be re-locked as needed */
  233. /* by putdeviceparams from the pgs_old->pagedevice dict state. */
  234. if (!samepagedevice)
  235. dev_old->LockSafetyParams = false;
  236. dev_new = gs_currentdevice(pgs_new);
  237. if (dev_old != dev_new) {
  238. if ((dev_t2 = (*dev_proc(dev_new, get_page_device)) (dev_new)) == 0)
  239. return false;
  240. if (dev_t1 != dev_t2)
  241. return true;
  242. }
  243. /*
  244. * The current implementation of setpagedevice just sets new
  245. * parameters in the same device object, so we have to check
  246. * whether the page device dictionaries are the same.
  247. */
  248. return !samepagedevice;
  249. }
  250. /* - grestore - */
  251. private int
  252. z2grestore(i_ctx_t *i_ctx_p)
  253. {
  254. if (!restore_page_device(igs, gs_state_saved(igs)))
  255. return gs_grestore(igs);
  256. return push_callout(i_ctx_p, "%grestorepagedevice");
  257. }
  258. /* - grestoreall - */
  259. private int
  260. z2grestoreall(i_ctx_t *i_ctx_p)
  261. {
  262. for (;;) {
  263. if (!restore_page_device(igs, gs_state_saved(igs))) {
  264. bool done = !gs_state_saved(gs_state_saved(igs));
  265. gs_grestore(igs);
  266. if (done)
  267. break;
  268. } else
  269. return push_callout(i_ctx_p, "%grestoreallpagedevice");
  270. }
  271. return 0;
  272. }
  273. /* <save> restore - */
  274. private int
  275. z2restore(i_ctx_t *i_ctx_p)
  276. {
  277. while (gs_state_saved(gs_state_saved(igs))) {
  278. if (restore_page_device(igs, gs_state_saved(igs)))
  279. return push_callout(i_ctx_p, "%restore1pagedevice");
  280. gs_grestore(igs);
  281. }
  282. if (restore_page_device(igs, gs_state_saved(igs)))
  283. return push_callout(i_ctx_p, "%restorepagedevice");
  284. return zrestore(i_ctx_p);
  285. }
  286. /* <gstate> setgstate - */
  287. private int
  288. z2setgstate(i_ctx_t *i_ctx_p)
  289. {
  290. os_ptr op = osp;
  291. check_stype(*op, st_igstate_obj);
  292. if (!restore_page_device(igs, igstate_ptr(op)))
  293. return zsetgstate(i_ctx_p);
  294. return push_callout(i_ctx_p, "%setgstatepagedevice");
  295. }
  296. /* ------ Initialization procedure ------ */
  297. const op_def zdevice2_l2_op_defs[] =
  298. {
  299. op_def_begin_level2(),
  300. {"0.currentshowpagecount", zcurrentshowpagecount},
  301. {"0.currentpagedevice", zcurrentpagedevice},
  302. {"1.setpagedevice", zsetpagedevice},
  303. /* Note that the following replace prior definitions */
  304. /* in the indicated files: */
  305. {"1copy", z2copy}, /* zdps1.c */
  306. {"0gsave", z2gsave}, /* zgstate.c */
  307. {"0save", z2save}, /* zvmem.c */
  308. {"0gstate", z2gstate}, /* zdps1.c */
  309. {"1currentgstate", z2currentgstate}, /* zdps1.c */
  310. {"0grestore", z2grestore}, /* zgstate.c */
  311. {"0grestoreall", z2grestoreall}, /* zgstate.c */
  312. {"1restore", z2restore}, /* zvmem.c */
  313. {"1setgstate", z2setgstate}, /* zdps1.c */
  314. /* Default Install/BeginPage/EndPage procedures */
  315. /* that just call the procedure in the device. */
  316. {"0.callinstall", zcallinstall},
  317. {"1.callbeginpage", zcallbeginpage},
  318. {"2.callendpage", zcallendpage},
  319. op_def_end(0)
  320. };
  321. /* ------ Internal routines ------ */
  322. /* Call out to a PostScript procedure. */
  323. private int
  324. push_callout(i_ctx_t *i_ctx_p, const char *callout_name)
  325. {
  326. int code;
  327. check_estack(1);
  328. code = name_enter_string(imemory, callout_name, esp + 1);
  329. if (code < 0)
  330. return code;
  331. ++esp;
  332. r_set_attrs(esp, a_executable);
  333. return o_push_estack;
  334. }