File: idparam.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 (324 lines) | stat: -rw-r--r-- 9,418 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* Copyright (C) 1992, 1993, 1994 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.
  
*/

/* idparam.c */
/* Utilities for getting parameters out of dictionaries. */
#include "memory_.h"
#include "string_.h"		/* for strlen */
#include "ghost.h"
#include "errors.h"
#include "gsmatrix.h"		/* for dict_matrix_param */
#include "gsuid.h"
#include "idict.h"
#include "idparam.h"		/* interface definition */
#include "ilevel.h"
#include "imemory.h"		/* for iutil.h */
#include "iname.h"
#include "iutil.h"
#include "oper.h"		/* for check_proc */
#include "store.h"		/* for making empty proc */

/* Get a Boolean parameter from a dictionary. */
/* Return 0 if found, 1 if defaulted, <0 if wrong type. */
int
dict_bool_param(const ref *pdict, const char _ds *kstr,
  bool defaultval, bool *pvalue)
{	ref *pdval;
	if ( pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0 )
	{	*pvalue = defaultval;
		return 1;
	}
	if ( !r_has_type(pdval, t_boolean) )
		return_error(e_typecheck);
	*pvalue = pdval->value.boolval;
	return 0;
}		

/* Get an integer parameter from a dictionary. */
/* Return 0 if found, 1 if defaulted, <0 if invalid. */
/* Note that the default value may be out of range, in which case */
/* a missing value will return e_rangecheck rather than 1. */
int
dict_int_param(const ref *pdict, const char _ds *kstr, int minval, int maxval,
  int defaultval, int *pvalue)
{	ref *pdval;
	int code;
	long ival;
	if ( pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0 )
	{	ival = defaultval;
		code = 1;
	}
	else
	{	switch ( r_type(pdval) )
		{
		case t_integer:
			ival = pdval->value.intval;
			break;
		case t_real:
			/* Allow an integral real, because Fontographer */
			/* (which violates the Adobe specs in other ways */
			/* as well) sometimes generates output that */
			/* needs this. */
			if ( pdval->value.realval < minval || pdval->value.realval > maxval )
				return_error(e_rangecheck);
			ival = (long)pdval->value.realval;
			if ( ival != pdval->value.realval )
				return_error(e_rangecheck);
			break;
		default:
			return_error(e_typecheck);
		}
		code = 0;
	}
	if ( ival < minval || ival > maxval )
		return_error(e_rangecheck);
	*pvalue = (int)ival;
	return code;
}		

/* Get an unsigned integer parameter from a dictionary. */
/* Return 0 if found, 1 if defaulted, <0 if invalid. */
/* Note that the default value may be out of range, in which case */
/* a missing value will return e_rangecheck rather than 1. */
int
dict_uint_param(const ref *pdict, const char _ds *kstr,
  uint minval, uint maxval, uint defaultval, uint *pvalue)
{	ref *pdval;
	int code;
	uint ival;
	if ( pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0 )
	{	ival = defaultval;
		code = 1;
	}
	else
	{	check_type_only(*pdval, t_integer);
		if ( pdval->value.intval != (uint)pdval->value.intval )
			return_error(e_rangecheck);
		ival = (uint)pdval->value.intval;
		code = 0;
	}
	if ( ival < minval || ival > maxval )
		return_error(e_rangecheck);
	*pvalue = ival;
	return code;
}		

/* Get a float parameter from a dictionary. */
/* Return 0 if found, 1 if defaulted, <0 if wrong type. */
int
dict_float_param(const ref *pdict, const char _ds *kstr,
  floatp defaultval, float *pvalue)
{	ref *pdval;
	if ( pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0 )
	{	*pvalue = defaultval;
		return 1;
	}
	switch ( r_type(pdval) )
	{
	case t_integer: *pvalue = pdval->value.intval; return 0;
	case t_real: *pvalue = pdval->value.realval; return 0;
	}
	return_error(e_typecheck);
}		

/* Get an integer array from a dictionary. */
/* Return the element count if OK, 0 if missing, <0 if invalid. */
int
dict_int_array_param(const ref *pdict, const char _ds *kstr,
  uint maxlen, int *ivec)
{	ref *pdval;
	const ref *pa;
	int *pi = ivec;
	uint size;
	int i;
	if ( pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0 )
		return 0;
	if ( !r_has_type(pdval, t_array) )
		return_error(e_typecheck);
	size = r_size(pdval);
	if ( size > maxlen )
		return_error(e_limitcheck);
	pa = pdval->value.const_refs;
	for ( i = 0; i < size; i++, pa++, pi++ )
	   {	/* See dict_int_param above for why we allow reals here. */
		switch ( r_type(pa) )
		{
		case t_integer:
			if ( pa->value.intval != (int)pa->value.intval )
				return_error(e_rangecheck);
			*pi = (int)pa->value.intval;
			break;
		case t_real:
			if ( pa->value.realval < min_int ||
			     pa->value.realval > max_int ||
			     pa->value.realval != (int)pa->value.realval
			   )
				return_error(e_rangecheck);
			*pi = (int)pa->value.realval;
			break;
		default:
			return_error(e_typecheck);
		}
	   }
	return size;
}		

/* Get a float array from a dictionary. */
/* Return the element count if OK, <0 if invalid. */
/* If the parameter is missing, then if defaultvec is NULL, return 0; */
/* if defaultvec is not NULL, copy it into fvec (maxlen elements) */
/* and return maxlen. */
int
dict_float_array_param(const ref *pdict, const char _ds *kstr,
  uint maxlen, float *fvec, float *defaultvec)
{	ref *pdval;
	uint size;
	int code;
	if ( pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0 )
	{	if ( defaultvec == NULL ) return 0;
		memcpy(fvec, defaultvec, maxlen * sizeof(float));
		return maxlen;
	}
	if ( !r_has_type(pdval, t_array) )
		return_error(e_typecheck);
	size = r_size(pdval);
	if ( size > maxlen )
		return_error(e_limitcheck);
	code = num_params(pdval->value.refs + size - 1, size, fvec);
	return (code >= 0 ? size : code);
}		

/*
 * Get a procedure from a dictionary.  If the key is missing,
 *	defaultval = false means substitute t__invalid;
 *	defaultval = true means substitute an empty procedure.
 * In either case, return 1.
 */
int
dict_proc_param(const ref *pdict, const char _ds *kstr, ref *pproc,
  bool defaultval)
{	ref *pdval;
	if ( pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0 )
	{	if ( defaultval )
		  make_empty_const_array(pproc, a_readonly + a_executable);
		else
		  make_t(pproc, t__invalid);
		return 1;
	}
	check_proc(*pdval);
	*pproc = *pdval;
	return 0;
}

/* Get a matrix from a dictionary. */
int
dict_matrix_param(const ref *pdict, const char _ds *kstr, gs_matrix *pmat)
{	ref *pdval;
	if ( pdict == 0 || dict_find_string(pdict, kstr, &pdval) <= 0 )
		return_error(e_typecheck);
	return read_matrix(pdval, pmat);
}

/* Get a UniqueID or XUID from a dictionary. */
/* Return 0 if UniqueID, 1 if XUID, <0 if error. */
/* If there is no uid, return default. */
int
dict_uid_param(const ref *pdict, gs_uid *puid, int defaultval,
  gs_memory_t *mem)
{	ref *puniqueid;
	if ( pdict == 0 )
	{	uid_set_invalid(puid);
		return defaultval;
	}
	/* In a Level 2 environment, check for XUID first. */
	if ( level2_enabled &&
	     dict_find_string(pdict, "XUID", &puniqueid) > 0
	   )
	{	long *xvalues;
		uint size, i;
		if ( !r_has_type(puniqueid, t_array) )
			return_error(e_typecheck);
		size = r_size(puniqueid);
		if ( size == 0 )
			return_error(e_rangecheck);
		xvalues = (long *)gs_alloc_byte_array(mem, size, sizeof(long),
						      "get XUID");
		if ( xvalues == 0 )
			return_error(e_VMerror);
		/* Get the values from the XUID array. */
		for ( i = 0; i < size; i++ )
		{	const ref *pvalue = puniqueid->value.const_refs + i;
			if ( !r_has_type(pvalue, t_integer) )
			{	gs_free_object(mem, xvalues, "get XUID");
				return_error(e_typecheck);
			}
			xvalues[i] = pvalue->value.intval;
		}
		uid_set_XUID(puid, xvalues, size);
		return 1;
	}
	/* If no UniqueID entry, set the UID to invalid, */
	/* because UniqueID need not be present in all fonts, */
	/* and if it is, the legal range is 0 to 2^24-1. */
	if ( dict_find_string(pdict, "UniqueID", &puniqueid) <= 0 )
	{	uid_set_invalid(puid);
		return defaultval;
	}
	else
	   {	if ( !r_has_type(puniqueid, t_integer) ||
		     puniqueid->value.intval < 0 ||
		     puniqueid->value.intval > 0xffffffL
		   )
			return_error(e_rangecheck);
		/* Apparently fonts created by Fontographer often have */
		/* a UniqueID of 0, contrary to Adobe's specifications. */
		/* Treat 0 as equivalent to -1 (no UniqueID). */
		if ( puniqueid->value.intval == 0 )
		{	uid_set_invalid(puid);
			return defaultval;
		}
		else
			uid_set_UniqueID(puid, puniqueid->value.intval);
	   }
	return 0;
}

/* Check that a UID in a dictionary is equal to an existing, valid UID. */
bool
dict_check_uid_param(const ref *pdict, const gs_uid *puid)
{	ref *puniqueid;
	if ( uid_is_XUID(puid) )
	  {	uint size = uid_XUID_size(puid);
		uint i;
		if ( dict_find_string(pdict, "XUID", &puniqueid) <= 0 )
		  return false;
		if ( !r_has_type(puniqueid, t_array) ||
		     r_size(puniqueid) != size
		   )
		  return false;
		for ( i = 0; i < size; i++ )
		  {	const ref *pvalue = puniqueid->value.const_refs + i;
			if ( !r_has_type(pvalue, t_integer) )
			  return false;
			if ( pvalue->value.intval != uid_XUID_values(puid)[i] )
			  return false;
		  }
		return true;
	  }
	else
	  {	if ( dict_find_string(pdict, "UniqueID", &puniqueid) <= 0 )
		  return false;
		return (r_has_type(puniqueid, t_integer) &&
			puniqueid->value.intval == puid->id);
	  }
}