File: gsdparam.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 (542 lines) | stat: -rw-r--r-- 16,462 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/* Copyright (C) 1993, 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.
  
*/

/* gsdparam.c */
/* Default device parameters for Ghostscript library */
#include "memory_.h"			/* for memcpy */
#include "gx.h"
#include "gserrors.h"
#include "gsparam.h"
#include "gxdevice.h"
#include "gxfixed.h"

/* ================ Getting parameters ================ */

/* Forward references */
private bool param_HWColorMap(P2(gx_device *, byte *));

/* Get the device parameters. */
int
gs_getdeviceparams(gx_device *dev, gs_param_list *plist)
{	gx_device_set_procs(dev);
	fill_dev_proc(dev, get_params, gx_default_get_params);
	fill_dev_proc(dev, get_page_device, gx_default_get_page_device);
	return (*dev_proc(dev, get_params))(dev, plist);
}

/* Standard ProcessColorModel values. */
static const char *pcmsa[] = {
  "", "DeviceGray", "", "DeviceRGB", "DeviceCMYK"
};

/* Get standard parameters. */
int
gx_default_get_params(gx_device *dev, gs_param_list *plist)
{
	int code;

	/* Standard page device parameters: */

	gs_param_string dns, pcms;
	gs_param_float_array psa, ibba, hwra, ma;
#define set_param_array(a, d, s)\
  (a.data = d, a.size = s, a.persistent = false);

	/* Non-standard parameters: */

	int colors = dev->color_info.num_components;
	int depth = dev->color_info.depth;
	int GrayValues = dev->color_info.max_gray + 1;
	bool IsPageDevice = (*dev_proc(dev, get_page_device))(dev) != 0;
	int HWSize[2];
	  gs_param_int_array hwsa;
	gs_param_float_array hwma;

	/* Fill in page device parameters. */

	param_string_from_string(dns, dev->dname);
	{	const char *cms = pcmsa[colors];
		/* We might have an uninitialized device with */
		/* color_info.num_components = 0.... */
		if ( *cms != 0 )
		  param_string_from_string(pcms, cms);
		else
		  pcms.data = 0;
	}
	set_param_array(hwra, dev->HWResolution, 2);
	set_param_array(psa, dev->PageSize, 2);
	set_param_array(ibba, dev->ImagingBBox, 4);
	set_param_array(ma, dev->Margins, 2);

	/* Fill in non-standard parameters. */

	HWSize[0] = dev->width;
	HWSize[1] = dev->height;
	set_param_array(hwsa, HWSize, 2);
	set_param_array(hwma, dev->HWMargins, 4);

	/* Transmit the values. */

	if (
			/* Standard parameters */

	     (code = param_write_name(plist, "OutputDevice", &dns)) < 0 ||
	     (code = (pcms.data == 0 ? 0 :
		      param_write_name(plist, "ProcessColorModel", &pcms))) < 0 ||
	     (code = param_write_float_array(plist, "HWResolution", &hwra)) < 0 ||
	     (code = param_write_float_array(plist, "PageSize", &psa)) < 0 ||
	     (code = (dev->ImagingBBox_set ?
		      param_write_float_array(plist, "ImagingBBox", &ibba) :
		      param_write_null(plist, "ImagingBBox"))) < 0 ||
	     (code = param_write_float_array(plist, "Margins", &ma)) < 0 ||
	     (code = (dev->Orientation_set > 0 ?
		      param_write_int(plist, "Orientation", &dev->Orientation) :
		      dev->Orientation_set == 0 ?
		      param_write_null(plist, "Orientation") :
		      0)) < 0 ||
	     (code = (dev->Duplex_set > 0 ?
		      param_write_bool(plist, "Duplex", &dev->Duplex) :
		      dev->Duplex_set == 0 ?
		      param_write_null(plist, "Duplex") :
		      0)) < 0 ||

			/* Non-standard parameters */

	     (code = param_write_int_array(plist, "HWSize", &hwsa)) < 0 ||
	     (code = param_write_float_array(plist, ".HWMargins", &hwma)) < 0 ||
	     (code = param_write_string(plist, "Name", &dns)) < 0 ||
	     (code = param_write_int(plist, "Colors", &colors)) < 0 ||
	     (code = param_write_int(plist, "BitsPerPixel", &depth)) < 0 ||
	     (code = param_write_int(plist, "GrayValues", &GrayValues)) < 0 ||
	     (code = param_write_long(plist, "PageCount", &dev->PageCount)) < 0 ||
	     (code = param_write_bool(plist, ".IsPageDevice", &IsPageDevice)) < 0

	   )
		return code;

	/* Fill in color information. */

	if ( colors > 1 )
	{	int RGBValues = dev->color_info.max_color + 1;
		long ColorValues = 1L << depth;
		if ( (code = param_write_int(plist, "RedValues", &RGBValues)) < 0 ||
		     (code = param_write_int(plist, "GreenValues", &RGBValues)) < 0 ||
		     (code = param_write_int(plist, "BlueValues", &RGBValues)) < 0 ||
		     (code = param_write_long(plist, "ColorValues", &ColorValues)) < 0
		   )
			return code;
	}

	if ( param_requested(plist, "HWColorMap") )
	{	byte palette[3 << 8];
		if ( param_HWColorMap(dev, palette) )
		  {	gs_param_string hwcms;
			hwcms.data = palette, hwcms.size = colors << depth,
			  hwcms.persistent = false;
			if ( (code = param_write_string(plist, "HWColorMap", &hwcms)) < 0 )
			  return code;
		  }
	}

	return 0;
}

/* Get the color map for a device.  Return true if there is one. */
private bool
param_HWColorMap(gx_device *dev, byte *palette /* 3 << 8 */)
{	int depth = dev->color_info.depth;
	int colors = dev->color_info.num_components;
	
	if ( depth <= 8 && colors <= 3 )
	  {	byte *p = palette;
		gx_color_value rgb[3];
		gx_color_index i;
		fill_dev_proc(dev, map_color_rgb, gx_default_map_color_rgb);
		for ( i = 0; (i >> depth) == 0; i++ )
		  {	int j;
			(*dev_proc(dev, map_color_rgb))(dev, i, rgb);
			for ( j = 0; j < colors; j++ )
			  *p++ = gx_color_value_to_byte(rgb[j]);
		  }
		return true;
	  }
	return false;
}

/* ================ Putting parameters ================ */

/* Forward references */
private int param_check_bool(P4(gs_param_list *, gs_param_name, bool, bool));
private int param_check_long(P4(gs_param_list *, gs_param_name, long, bool));
#define param_check_int(plist, pname, ival, defined)\
  param_check_long(plist, pname, (long)(ival), defined)
private int param_check_bytes(P5(gs_param_list *, gs_param_name, const byte *, uint, bool));
#define param_check_string(plist, pname, str, defined)\
  param_check_bytes(plist, pname, (const byte *)str, strlen(str), defined)

/* Set the device parameters. */
/* If the device was open and the put_params procedure closed it, */
/* return 1; otherwise, return 0 or an error code as usual. */
int
gs_putdeviceparams(gx_device *dev, gs_param_list *plist)
{	bool was_open = dev->is_open;
	int code;
	fill_dev_proc(dev, put_params, gx_default_put_params);
	code = (*dev_proc(dev, put_params))(dev, plist);
	return (code < 0 ? code : was_open && !dev->is_open ? 1 : code);
}

/* Set standard parameters. */
/* Note that setting the size, resolution, or (if allowed) orientation */
/*  closes the device. */
/* Window devices that don't want this to happen must temporarily */
/* set is_open to false before calling gx_default_put_params, */
/* and then taking appropriate action afterwards. */
int
gx_default_put_params(gx_device *dev, gs_param_list *plist)
{	int ecode = 0;
	int code;
	const char _ds *param_name;
	gs_param_float_array hwra;
	gs_param_int_array hwsa;
	gs_param_float_array psa;
	gs_param_float_array ma;
	gs_param_float_array hwma;
	gs_param_float_array ibba;
	bool ibbnull = false;
	int orientation;
	  int orientation_set = -1;
	bool duplex;
	  int duplex_set = -1;
	int colors = dev->color_info.num_components;
	int depth = dev->color_info.depth;
	int GrayValues = dev->color_info.max_gray + 1;
	int RGBValues = dev->color_info.max_color + 1;
	long ColorValues = 1L << depth;
	bool IsPageDevice = (*dev_proc(dev, get_page_device))(dev) != 0;
	gs_param_string cms;

#define BEGIN_ARRAY_PARAM(pread, pname, pa, psize, e)\
  switch ( code = pread(plist, (param_name = pname), &pa) )\
  {\
  case 0:\
	if ( pa.size != psize )\
	  ecode = gs_error_rangecheck;\
	else { 
/* The body of the processing code goes here. */
/* If it succeeds, it should do a 'break'; */
/* if it fails, it should set ecode and fall through. */
#define END_PARAM(pa, e)\
	}\
	goto e;\
  default:\
	ecode = code;\
e:	param_signal_error(plist, param_name, ecode);\
  case 1:\
	pa.data = 0;		/* mark as not filled */\
  }

	/*
	 * The HWResolution, HWSize, and PageSize parameters interact in
	 * the following way:
	 *	1. Setting HWResolution recomputes HWSize from PageSize.
	 *	2. Setting HWSize recomputes PageSize from HWResolution.
	 *	3. Setting PageSize recomputes HWSize from HWResolution.
	 * If more than one parameter is being set, we apply these rules
	 * in the order 1, 2, 3.  This does the right thing in the most
	 * common case of setting more than one parameter, namely,
	 * setting both HWResolution and HWSize.
	 */

	BEGIN_ARRAY_PARAM(param_read_float_array, "HWResolution", hwra, 2, hwre)
		if ( hwra.data[0] <= 0 || hwra.data[1] <= 0 )
		  ecode = gs_error_rangecheck;
		else
		  break;
	END_PARAM(hwra, hwre)

	BEGIN_ARRAY_PARAM(param_read_int_array, "HWSize", hwsa, 2, hwsa)
		if ( hwsa.data[0] <= 0 || hwsa.data[1] <= 0 )
		  ecode = gs_error_rangecheck;
#define max_coord (max_fixed / fixed_1)
#if max_coord < max_int
		else if ( hwsa.data[0] > max_coord || hwsa.data[1] > max_coord )
		  ecode = gs_error_limitcheck;
#endif
#undef max_coord
		else
		  break;
	END_PARAM(hwsa, hwse)

	BEGIN_ARRAY_PARAM(param_read_float_array, "PageSize", psa, 2, pse)
		const float *res =
		  (hwra.data == 0 ? dev->HWResolution : hwra.data);
		float width_new, height_new;
		if ( (width_new = psa.data[0] * res[0] / 72) <= 0 ||
		     (height_new = psa.data[1] * res[1] / 72) <= 0
		   )
		  ecode = gs_error_rangecheck;
#define max_coord (max_fixed / fixed_1)
#if max_coord < max_int
		else if ( width_new > max_coord || height_new > max_coord )
		  ecode = gs_error_limitcheck;
#endif
#undef max_coord
		else
		  break;
	END_PARAM(psa, pse)

	BEGIN_ARRAY_PARAM(param_read_float_array, "Margins", ma, 2, me)
		break;
	END_PARAM(ma, me)

	BEGIN_ARRAY_PARAM(param_read_float_array, ".HWMargins", hwma, 4, hwme)
		break;
	END_PARAM(hwma, hwme)

	switch ( code = param_read_float_array(plist, (param_name = "ImagingBBox"), &ibba) )
	{
	case 0:
		if ( ibba.size != 4 || ibba.data[0] < 0 || ibba.data[1] < 0 ||
		     ibba.data[2] < ibba.data[0] || ibba.data[3] < ibba.data[1]
		   )
		  ecode = gs_error_rangecheck;
		else
		  break;
		goto ibbe;
	default:
		if ( (code = param_read_null(plist, param_name)) == 0 )
		{	ibbnull = true;
			ibba.data = 0;
			break;
		}
		ecode = code;	/* can't be 1 */
ibbe:		param_signal_error(plist, param_name, ecode);
	case 1:
		ibba.data = 0;
		break;
	}

	if ( dev->Orientation_set >= 0 ) /* i.e., Orientation is supported */
	  switch ( code = param_read_int(plist, (param_name = "Orientation"),
					 &orientation) )
	    {
	    case 0:
		if ( orientation >= 0 && orientation <= 3 )
		  {	orientation_set = 1;
			break;
		  }
		ecode = gs_error_rangecheck;
		goto oe;
	    default:
		if ( (code = param_read_null(plist, param_name)) == 0 )
		{	orientation_set = 0;
			orientation = dev->Orientation;	/* preserve */
			break;
		}
		ecode = code;
oe:		param_signal_error(plist, param_name, ecode);
	    case 1:
		;
	    }

	if ( dev->Duplex_set >= 0 )	/* i.e., Duplex is supported */
	  switch ( code = param_read_bool(plist, (param_name = "Duplex"),
					  &duplex) )
	    {
	    case 0:
		duplex_set = 1;
		break;
	    default:
		if ( (code = param_read_null(plist, param_name)) == 0 )
		{	duplex_set = 0;
			break;
		}
		ecode = code;
		param_signal_error(plist, param_name, ecode);
	    case 1:
		;
	    }

	/* Now check nominally read-only parameters. */
	if ( (code = param_check_string(plist, "OutputDevice", dev->dname, true)) < 0 )
	  ecode = code;
	if ( (code = param_check_string(plist, "ProcessColorModel", pcmsa[colors], colors != 0)) < 0 )
	  ecode = code;
	if ( (code = param_check_string(plist, "Name", dev->dname, true)) < 0 )
	  ecode = code;
	if ( (code = param_check_int(plist, "Colors", colors, true)) < 0 )
	  ecode = code;
	if ( (code = param_check_int(plist, "BitsPerPixel", depth, true)) < 0 )
	  ecode = code;
	if ( (code = param_check_int(plist, "GrayValues", GrayValues, true)) < 0 )
	  ecode = code;
	if ( (code = param_check_long(plist, "PageCount", dev->PageCount, true)) < 0 )
	  ecode = code;
	if ( (code = param_check_int(plist, "RedValues", RGBValues, colors > 1)) < 0 )
	  ecode = code;
	if ( (code = param_check_int(plist, "GreenValues", RGBValues, colors > 1)) < 0 )
	  ecode = code;
	if ( (code = param_check_int(plist, "BlueValues", RGBValues, colors > 1)) < 0 )
	  ecode = code;
	if ( (code = param_check_long(plist, "ColorValues", ColorValues, colors > 1)) < 0 )
	  ecode = code;
	if ( (code = param_check_bool(plist, ".IsPageDevice", IsPageDevice, true)) < 0 )
	  ecode = code;
	if ( param_read_string(plist, "HWColorMap", &cms) != 1 )
	  {	byte palette[3 << 8];
		if ( param_HWColorMap(dev, palette) )
		  code = param_check_bytes(plist, "HWColorMap", palette,
					   colors << depth, true);
		else
		  code = param_check_bytes(plist, "HWColorMap", 0, 0, false);
		if ( code < 0 )
		  ecode = code;
	  }

	if ( ecode < 0 )
	  return ecode;
	code = param_commit(plist);
	if ( code < 0 )
	  return code;

	/* Now actually make the changes. */
	/* Changing resolution or page size requires closing the device, */
	/* but changing margins or ImagingBBox does not. */
	/* In order not to close and reopen the device unnecessarily, */
	/* we check for replacing the values with the same ones. */

	if ( hwra.data != 0 &&
	      (dev->HWResolution[0] != hwra.data[0] ||
	       dev->HWResolution[1] != hwra.data[1])
	   )
	  {	if ( dev->is_open )
		  gs_closedevice(dev);
		gx_device_set_resolution(dev, hwra.data[0], hwra.data[1]);
	  }
	if ( hwsa.data != 0 &&
	      (dev->width != hwsa.data[0] ||
	       dev->height != hwsa.data[1])
	   )
	  {	if ( dev->is_open )
		  gs_closedevice(dev);
		gx_device_set_width_height(dev, hwsa.data[0], hwsa.data[1]);
	  }
	if ( psa.data != 0 &&
	      (dev->PageSize[0] != psa.data[0] ||
	       dev->PageSize[1] != psa.data[1])
	   )
	  {	if ( dev->is_open )
		  gs_closedevice(dev);
		gx_device_set_page_size(dev, psa.data[0], psa.data[1]);
	  }
	if ( ma.data != 0 )
	  {	dev->Margins[0] = ma.data[0];
		dev->Margins[1] = ma.data[1];
	  }
	if ( hwma.data != 0 )
	  {	dev->HWMargins[0] = hwma.data[0];
		dev->HWMargins[1] = hwma.data[1];
		dev->HWMargins[2] = hwma.data[2];
		dev->HWMargins[3] = hwma.data[3];
	  }
	if ( ibba.data != 0 )
	  {	dev->ImagingBBox[0] = ibba.data[0];
		dev->ImagingBBox[1] = ibba.data[1];
		dev->ImagingBBox[2] = ibba.data[2];
		dev->ImagingBBox[3] = ibba.data[3];
		dev->ImagingBBox_set = true;
	  }
	else if ( ibbnull )
	  {	dev->ImagingBBox_set = false;
	  }
	if ( orientation_set >= 0 &&
	      dev->Orientation != orientation
	   )
	  {	dev->Orientation = orientation;
		dev->Orientation_set = orientation_set;
	  }
	if ( duplex_set >= 0 )
	  {	dev->Duplex = duplex;
		dev->Duplex_set = duplex_set;
	  }
	return 0;
}

/* Check that a nominally read-only parameter is being set to */
/* its existing value. */
private int
param_check_bool(gs_param_list *plist, gs_param_name pname, bool value,
  bool defined)
{	int code;
	bool new_value;
	switch ( code = param_read_bool(plist, pname, &new_value) )
	  {
	  case 0:
		if ( defined && new_value == value )
		  break;
		code = gs_note_error(gs_error_rangecheck);
		goto e;
	  default:
		if ( param_read_null(plist, pname) == 0 )
		  return 1;
e:		param_signal_error(plist, pname, code);
	  case 1:
		;
	  }
	return code;
}
private int
param_check_long(gs_param_list *plist, gs_param_name pname, long value,
  bool defined)
{	int code;
	long new_value;
	switch ( code = param_read_long(plist, pname, &new_value) )
	  {
	  case 0:
		if ( defined && new_value == value )
		  break;
		code = gs_note_error(gs_error_rangecheck);
		goto e;
	  default:
		if ( param_read_null(plist, pname) == 0 )
		  return 1;
e:		param_signal_error(plist, pname, code);
	  case 1:
		;
	  }
	return code;
}
private int
param_check_bytes(gs_param_list *plist, gs_param_name pname, const byte *str,
  uint size, bool defined)
{	int code;
	gs_param_string new_value;
	switch ( code = param_read_string(plist, pname, &new_value) )
	  {
	  case 0:
		if ( defined && new_value.size == size &&
		     !memcmp((const char *)str, (const char *)new_value.data,
			     size)
		   )
		  break;
		code = gs_note_error(gs_error_rangecheck);
		goto e;
	  default:
		if ( param_read_null(plist, pname) == 0 )
		  return 1;
e:		param_signal_error(plist, pname, code);
	  case 1:
		;
	  }
	return code;
}