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