1 /* Copyright (C) 2001-2006 Artifex Software, Inc.
2    All Rights Reserved.
3 
4    This software is provided AS-IS with no warranty, either express or
5    implied.
6 
7    This software is distributed under license and may not be copied, modified
8    or distributed except as expressly authorized under the terms of that
9    license.  Refer to licensing information at http://www.artifex.com/
10    or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
11    San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
12 */
13 
14 /* $Id: zdps.c 9778 2009-06-05 05:55:54Z alexcher $ */
15 /* Display PostScript extensions */
16 #include "ghost.h"
17 #include "oper.h"
18 #include "gsstate.h"
19 #include "gsdps.h"
20 #include "gsimage.h"
21 #include "gsiparm2.h"
22 #include "gxalloc.h"		/* for names_array in allocator */
23 #include "gxfixed.h"		/* for gxpath.h */
24 #include "gxpath.h"
25 #include "btoken.h"		/* for user_names_p */
26 #include "iddict.h"
27 #include "idparam.h"
28 #include "igstate.h"
29 #include "iimage2.h"
30 #include "iname.h"
31 #include "store.h"
32 
33 /* Import the procedure for constructing user paths. */
34 extern int make_upath(i_ctx_t *, ref *, const gs_state *, gx_path *, bool);
35 
36 /* ------ Graphics state ------ */
37 
38 /* <screen_index> <x> <y> .setscreenphase - */
39 static int
zsetscreenphase(i_ctx_t * i_ctx_p)40 zsetscreenphase(i_ctx_t *i_ctx_p)
41 {
42     os_ptr op = osp;
43     int code;
44     int x, y;
45 
46     check_type(op[-2], t_integer);
47     check_type(op[-1], t_integer);
48     check_type(*op, t_integer);
49     x = op[-1].value.intval;
50     y = op->value.intval;
51     if (op[-2].value.intval < -1 ||
52 	op[-2].value.intval >= gs_color_select_count
53 	)
54 	return_error(e_rangecheck);
55     code = gs_setscreenphase(igs, x, y,
56 			     (gs_color_select_t) op[-2].value.intval);
57     if (code >= 0)
58 	pop(3);
59     return code;
60 }
61 
62 /* <screen_index> .currentscreenphase <x> <y> */
63 static int
zcurrentscreenphase(i_ctx_t * i_ctx_p)64 zcurrentscreenphase(i_ctx_t *i_ctx_p)
65 {
66     os_ptr op = osp;
67     gs_int_point phase;
68     int code;
69 
70     check_type(*op, t_integer);
71     if (op->value.intval < -1 ||
72 	op->value.intval >= gs_color_select_count
73 	)
74 	return_error(e_rangecheck);
75     code = gs_currentscreenphase(igs, &phase,
76 				 (gs_color_select_t)op->value.intval);
77     if (code < 0)
78 	return code;
79     push(1);
80     make_int(op - 1, phase.x);
81     make_int(op, phase.y);
82     return 0;
83 }
84 
85 /* ------ Device-source images ------ */
86 
87 /* <dict> .image2 - */
88 static int
zimage2(i_ctx_t * i_ctx_p)89 zimage2(i_ctx_t *i_ctx_p)
90 {
91     os_ptr op = osp;
92     int code;
93 
94     check_type(*op, t_dictionary);
95     check_dict_read(*op);
96     {
97 	gs_image2_t image;
98 	ref *pDataSource;
99 
100 	gs_image2_t_init(&image);
101 	if ((code = dict_matrix_param(imemory, op, "ImageMatrix",
102 				      &image.ImageMatrix)) < 0 ||
103 	    (code = dict_find_string(op, "DataSource", &pDataSource)) < 0 ||
104 	    (code = dict_float_param(op, "XOrigin", 0.0,
105 				     &image.XOrigin)) != 0 ||
106 	    (code = dict_float_param(op, "YOrigin", 0.0,
107 				     &image.YOrigin)) != 0 ||
108 	    (code = dict_float_param(op, "Width", 0.0,
109 				     &image.Width)) != 0 ||
110 	    image.Width <= 0 ||
111 	    (code = dict_float_param(op, "Height", 0.0,
112 				     &image.Height)) != 0 ||
113 	    image.Height <= 0 ||
114 	    (code = dict_bool_param(op, "PixelCopy", false,
115 				    &image.PixelCopy)) < 0
116 	    )
117 	    return (code < 0 ? code : gs_note_error(e_rangecheck));
118 	check_stype(*pDataSource, st_igstate_obj);
119 	image.DataSource = igstate_ptr(pDataSource);
120 	{
121 	    ref *ignoref;
122 
123 	    if (dict_find_string(op, "UnpaintedPath", &ignoref) > 0) {
124 		check_dict_write(*op);
125 		image.UnpaintedPath = gx_path_alloc(imemory,
126 						    ".image2 UnpaintedPath");
127 		if (image.UnpaintedPath == 0)
128 		    return_error(e_VMerror);
129 	    } else
130 		image.UnpaintedPath = 0;
131 	}
132 	code = process_non_source_image(i_ctx_p,
133 					(const gs_image_common_t *)&image,
134 					".image2");
135 	if (image.UnpaintedPath) {
136 	    ref rupath;
137 
138 	    if (code < 0)
139 		return code;
140 	    if (gx_path_is_null(image.UnpaintedPath))
141 		make_null(&rupath);
142 	    else
143 		code = make_upath(i_ctx_p, &rupath, igs, image.UnpaintedPath,
144 				  false);
145 	    gx_path_free(image.UnpaintedPath, ".image2 UnpaintedPath");
146 	    if (code < 0)
147 		return code;
148 	    code = idict_put_string(op, "UnpaintedPath", &rupath);
149 	}
150     }
151     if (code >= 0)
152 	pop(1);
153     return code;
154 }
155 
156 /* ------ View clipping ------ */
157 
158 /* - viewclip - */
159 static int
zviewclip(i_ctx_t * i_ctx_p)160 zviewclip(i_ctx_t *i_ctx_p)
161 {
162     return gs_viewclip(igs);
163 }
164 
165 /* - eoviewclip - */
166 static int
zeoviewclip(i_ctx_t * i_ctx_p)167 zeoviewclip(i_ctx_t *i_ctx_p)
168 {
169     return gs_eoviewclip(igs);
170 }
171 
172 /* - initviewclip - */
173 static int
zinitviewclip(i_ctx_t * i_ctx_p)174 zinitviewclip(i_ctx_t *i_ctx_p)
175 {
176     return gs_initviewclip(igs);
177 }
178 
179 /* - viewclippath - */
180 static int
zviewclippath(i_ctx_t * i_ctx_p)181 zviewclippath(i_ctx_t *i_ctx_p)
182 {
183     return gs_viewclippath(igs);
184 }
185 
186 /* ------ User names ------ */
187 
188 /* <index> <name> defineusername - */
189 static int
zdefineusername(i_ctx_t * i_ctx_p)190 zdefineusername(i_ctx_t *i_ctx_p)
191 {
192     os_ptr op = osp;
193     ref uname;
194 
195     check_int_ltu(op[-1], max_array_size);
196     check_type(*op, t_name);
197     if (user_names_p == 0) {
198 	int code = create_names_array(&user_names_p, imemory_local,
199 				      "defineusername");
200 
201 	if (code < 0)
202 	    return code;
203     }
204     if (array_get(imemory, user_names_p,
205 		  op[-1].value.intval, &uname) >= 0) {
206 	switch (r_type(&uname)) {
207 	    case t_null:
208 		break;
209 	    case t_name:
210 		if (name_eq(&uname, op))
211 		    goto ret;
212 		/* falls through */
213 	    default:
214 		return_error(e_invalidaccess);
215 	}
216     } else {			/* Expand the array. */
217 	ref new_array;
218 	uint old_size = r_size(user_names_p);
219 	uint new_size = (uint) op[-1].value.intval + 1;
220 
221 	if (new_size < 100)
222 	    new_size = 100;
223 	else if (new_size > max_array_size / 2)
224 	    new_size = max_array_size;
225 	else if (new_size >> 1 < old_size)
226 	    new_size = (old_size > max_array_size / 2 ? max_array_size :
227 			old_size << 1);
228 	else
229 	    new_size <<= 1;
230 	/*
231 	 * The user name array is allocated in stable local VM,
232 	 * because it must be immune to save/restore.
233 	 */
234 	{
235 	    gs_ref_memory_t *slmem =
236 		(gs_ref_memory_t *)gs_memory_stable(imemory_local);
237 	    int code;
238 
239 	    code = gs_alloc_ref_array(slmem, &new_array, a_all, new_size,
240 				      "defineusername(new)");
241 	    if (code < 0)
242 		return code;
243 	    refcpy_to_new(new_array.value.refs, user_names_p->value.refs,
244 			  old_size, idmemory);
245 	    refset_null(new_array.value.refs + old_size,
246 			new_size - old_size);
247 	    if (old_size)
248 		gs_free_ref_array(slmem, user_names_p, "defineusername(old)");
249 	}
250 	ref_assign(user_names_p, &new_array);
251     }
252     ref_assign(user_names_p->value.refs + op[-1].value.intval, op);
253   ret:
254     pop(2);
255     return 0;
256 }
257 
258 /* ------ Initialization procedure ------ */
259 
260 const op_def zdps_op_defs[] =
261 {
262 		/* Graphics state */
263     {"1.currentscreenphase", zcurrentscreenphase},
264     {"3.setscreenphase", zsetscreenphase},
265 		/* Device-source images */
266     {"1.image2", zimage2},
267 		/* View clipping */
268     {"0eoviewclip", zeoviewclip},
269     {"0initviewclip", zinitviewclip},
270     {"0viewclip", zviewclip},
271     {"0viewclippath", zviewclippath},
272 		/* User names */
273     {"2defineusername", zdefineusername},
274     op_def_end(0)
275 };
276