File: zdevice.c

package info (click to toggle)
gs 3.33-7
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 7,436 kB
  • ctags: 15,511
  • sloc: ansic: 92,150; asm: 684; sh: 486; makefile: 91
file content (285 lines) | stat: -rw-r--r-- 7,858 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* Copyright (C) 1989, 1995 Aladdin Enterprises.  All rights reserved.
  
  This file is part of GNU Ghostscript.
  
  GNU Ghostscript is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility to
  anyone for the consequences of using it or for whether it serves any
  particular purpose or works at all, unless he says so in writing.  Refer
  to the GNU Ghostscript General Public License for full details.
  
*/

/* zdevice.c */
/* Device-related operators */
#include "ghost.h"
#include "errors.h"
#include "oper.h"
#include "ialloc.h"
#include "idict.h"
#include "igstate.h"
#include "iname.h"
#include "interp.h"
#include "iparam.h"
#include "ivmspace.h"
#include "gsmatrix.h"
#include "gsstate.h"
#include "gxdevice.h"
#include "store.h"

/* <device> copydevice <newdevice> */
private int
zcopydevice(register os_ptr op)
{	gx_device *new_dev;
	int code;
	check_type(*op, t_device);
	code = gs_copydevice(&new_dev, op->value.pdevice, imemory);
	if ( code < 0 ) return code;
	new_dev->memory = imemory;
	make_tav(op, t_device, icurrent_space, pdevice, new_dev);
	return 0;
}

/* <device> <y> <string> copyscanlines <substring> */
private int
zcopyscanlines(register os_ptr op)
{	os_ptr op1 = op - 1;
	os_ptr op2 = op - 2;
	gx_device *dev;
	int code;
	uint bytes_copied;
	check_type(*op2, t_device);
	dev = op2->value.pdevice;
	check_type(*op1, t_integer);
	if ( op1->value.intval < 0 || op1->value.intval > dev->height )
		return_error(e_rangecheck);
	check_write_type(*op, t_string);
	code = gs_copyscanlines(dev, (int)op1->value.intval,
		op->value.bytes, r_size(op), NULL, &bytes_copied);
	if ( code < 0 )
		return_error(e_rangecheck);	/* not a memory device */
	*op2 = *op;
	r_set_size(op2, bytes_copied);
	pop(2);
	return 0;
}

/* - currentdevice <device> */
private int
zcurrentdevice(register os_ptr op)
{	gx_device *dev = gs_currentdevice(igs);
	gs_ref_memory_t *mem = (gs_ref_memory_t *)dev->memory;
	push(1);
	make_tav(op, t_device,
		 (mem == 0 ? avm_foreign : imemory_space(mem)),
		 pdevice, dev);
	return 0;
}

/* - flushpage - */
int
zflushpage(register os_ptr op)
{	return gs_flushpage(igs);
}

/* <int> .getdevice <device> */
private int
zgetdevice(register os_ptr op)
{	gx_device *dev;
	check_type(*op, t_integer);
	if ( op->value.intval != (int)(op->value.intval) )
		return_error(e_rangecheck);	/* won't fit in an int */
	dev = gs_getdevice((int)(op->value.intval));
	if ( dev == 0 )		/* index out of range */
		return_error(e_rangecheck);
	make_tav(op, t_device, avm_foreign, pdevice, dev);
	return 0;
}

/* <device> <key_dict|null> .getdeviceparams <mark> <name> <value> ... */
private int
zgetdeviceparams(os_ptr op)
{	ref rkeys;
	gx_device *dev;
	stack_param_list list;
	int code;
	ref *pmark;
	check_type(op[-1], t_device);
	rkeys = *op;
	dev = op[-1].value.pdevice;
	pop(1);
	stack_param_list_write(&list, &o_stack, &rkeys);
	code = gs_getdeviceparams(dev, (gs_param_list *)&list);
	if ( code < 0 )
	  {	/* We have to put back the top argument. */
		if ( list.count > 0 )
		  ref_stack_pop(&o_stack, list.count * 2 - 1);
		else
		  ref_stack_push(&o_stack, 1);
		*osp = rkeys;
		return code;
	  }
	pmark = ref_stack_index(&o_stack, list.count * 2);
	make_mark(pmark);
	return 0;
}

/* <matrix> <width> <height> <palette> makeimagedevice <device> */
private int
zmakeimagedevice(register os_ptr op)
{	gs_matrix imat;
	gx_device *new_dev;
	const byte *colors;
	int colors_size;
	int code;
	check_int_leu(op[-2], max_uint >> 1);	/* width */
	check_int_leu(op[-1], max_uint >> 1);	/* height */
	if ( r_has_type(op, t_null) )	/* true color */
	   {	colors = 0;
		colors_size = -24;	/* 24-bit true color */
	   }
	else
	   {	check_type(*op, t_string);	/* palette */
		if ( r_size(op) > 3*256 )
			return_error(e_rangecheck);
		colors = op->value.bytes;
		colors_size = r_size(op);
	   }
	if ( (code = read_matrix(op - 3, &imat)) < 0 )
		return code;
	/* Everything OK, create device */
	code = gs_makeimagedevice(&new_dev, &imat,
				  (int)op[-2].value.intval,
				  (int)op[-1].value.intval,
				  colors, colors_size, imemory);
	if ( code == 0 )
	{	new_dev->memory = imemory;
		make_tav(op - 3, t_device, imemory_space(iimemory),
			 pdevice, new_dev);
		pop(3);
	}
	return code;
}

/* - nulldevice - */
private int
znulldevice(register os_ptr op)
{	gs_nulldevice(igs);
	clear_pagedevice(istate);
	return 0;
}

/* <num_copies> <flush_bool> .outputpage - */
private int
zoutputpage(register os_ptr op)
{	int code;
	check_type(op[-1], t_integer);
	check_type(*op, t_boolean);
	code = gs_output_page(igs, (int)op[-1].value.intval, op->value.boolval);
	if ( code < 0 ) return code;
	pop(2);
	return 0;
}

/* <device> <policy_dict|null> <require_all> <mark> <name> <value> ... */
/*	.putdeviceparams */
/*   (on success) <device> <eraseflag> */
/*   (on failure) <device> <policy_dict|null> <require_all> <mark> */
/*	 <name1> <error1> ... */
/* For a key that simply was not recognized, if require_all is true, */
/* the result will be an /undefined error; if require_all is false, */
/* the key will be ignored. */
private int
zputdeviceparams(os_ptr op)
{	uint count = ref_stack_counttomark(&o_stack);
	ref *prequire_all;
	ref *ppolicy;
	ref *pdev;
	gx_device *dev;
	stack_param_list list;
	int code;
	int old_width, old_height;
	int i, dest;

	if ( count == 0 )
	  return_error(e_unmatchedmark);
	prequire_all = ref_stack_index(&o_stack, count);
	ppolicy = ref_stack_index(&o_stack, count + 1);
	pdev = ref_stack_index(&o_stack, count + 2);
	if ( pdev == 0 )
	  return_error(e_stackunderflow);
	check_type_only(*prequire_all, t_boolean);
	check_type_only(*pdev, t_device);
	dev = pdev->value.pdevice;
	code = stack_param_list_read(&list, &o_stack, 0, ppolicy,
				     prequire_all->value.boolval);
	if ( code < 0 )
	  return code;
	old_width = dev->width;
	old_height = dev->height;
	code = gs_putdeviceparams(dev, (gs_param_list *)&list);
	/* Check for names that were undefined or caused errors. */
	for ( dest = count - 2, i = 0; i < count >> 1; i++ )
	  if ( list.results[i] < 0 )
	    {	*ref_stack_index(&o_stack, dest) =
		  *ref_stack_index(&o_stack, count - (i << 1) - 2);
		gs_errorname(list.results[i],
			     ref_stack_index(&o_stack, dest - 1));
		dest -= 2;
	    }
	if ( code < 0 )
	  {	/* There were errors reported. */
		ref_stack_pop(&o_stack, dest + 1);
		return 0;
	  }
	if ( code > 0 || (code == 0 && (dev->width != old_width || dev->height != old_height)) )
	{	/* The device was open and is now closed, */
		/* or its dimensions have changed. */
		/* If it was the current device, */
		/* call setdevice to reinstall it and erase the page. */
		/****** DOESN'T FIND ALL THE GSTATES THAT REFERENCE THE DEVICE. ******/
		if ( gs_currentdevice(igs) == dev )
		{	bool was_open = dev->is_open;
			code = gs_setdevice_no_erase(igs, dev);
			/* If the device wasn't closed, */
			/* setdevice won't erase the page. */
			if ( was_open && code >= 0 )
			  code = 1;
		}
	}
	if ( code < 0 )
	  return code;
	ref_stack_pop(&o_stack, count + 1);
	make_bool(osp, code);
	clear_pagedevice(istate);
	return 0;
}

/* <device> .setdevice <eraseflag> */
int
zsetdevice(register os_ptr op)
{	int code;
	check_type(*op, t_device);
	code = gs_setdevice_no_erase(igs, op->value.pdevice);
	if ( code < 0 )
		return code;
	make_bool(op, 1);
	clear_pagedevice(istate);
	return code;
}

/* ------ Initialization procedure ------ */

BEGIN_OP_DEFS(zdevice_op_defs) {
	{"1copydevice", zcopydevice},
	{"3copyscanlines", zcopyscanlines},
	{"0currentdevice", zcurrentdevice},
	{"0flushpage", zflushpage},
	{"1.getdevice", zgetdevice},
	{"2.getdeviceparams", zgetdeviceparams},
	{"4makeimagedevice", zmakeimagedevice},
	{"0nulldevice", znulldevice},
	{"2.outputpage", zoutputpage},
	{"3.putdeviceparams", zputdeviceparams},
	{"1.setdevice", zsetdevice},
END_OP_DEFS(0) }