summaryrefslogtreecommitdiff
path: root/filter/interpret.c
blob: 6f8f754afc6a09976c75ebf2897ddea360af95d0 (plain)
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*
 * "$Id: interpret.c 4982 2006-01-25 21:44:44Z mike $"
 *
 *   PPD command interpreter for the Common UNIX Printing System (CUPS).
 *
 *   Copyright 1993-2006 by Easy Software Products.
 *
 *   These coded instructions, statements, and computer programs are the
 *   property of Easy Software Products and are protected by Federal
 *   copyright law.  Distribution and use rights are outlined in the file
 *   "LICENSE.txt" which should have been included with this file.  If this
 *   file is missing or damaged please contact Easy Software Products
 *   at:
 *
 *       Attn: CUPS Licensing Information
 *       Easy Software Products
 *       44141 Airport View Drive, Suite 204
 *       Hollywood, Maryland 20636 USA
 *
 *       Voice: (301) 373-9600
 *       EMail: cups-info@cups.org
 *         WWW: http://www.cups.org
 *
 *   This file is subject to the Apple OS-Developed Software exception.
 *
 * Contents:
 *
 *   cupsRasterInterpretPPD() - Interpret PPD commands to create a page header.
 *   exec_code()              - Execute PostScript setpagedevice commands as
 *                              appropriate.
 */

/*
 * Include necessary headers...
 */

#include <cups/ppd.h>
#include <cups/string.h>
#include "raster.h"
#include <stdlib.h>


/*
 * Value types for PS code...
 */

#define CUPS_TYPE_NUMBER	0	/* Integer or real number */
#define CUPS_TYPE_NAME		1	/* Name */
#define CUPS_TYPE_STRING	2	/* String */
#define CUPS_TYPE_ARRAY		3	/* Array of integers */


/*
 * Local functions...
 */

static int	exec_code(cups_page_header2_t *header, const char *code);


/*
 * 'cupsRasterInterpretPPD()' - Interpret PPD commands to create a page header.
 *
 * @since CUPS 1.2@
 */

int					/* O - 0 on success, -1 on failure */
cupsRasterInterpretPPD(
    cups_page_header2_t *h,		/* O - Page header */
    ppd_file_t          *ppd)		/* I - PPD file */
{
  int		i;			/* Looping var */
  int		status;			/* Cummulative status */
  int		count;			/* Number of marked choices */
  ppd_choice_t	**choices;		/* List of marked choices */
  ppd_size_t	*size;			/* Current size */
  float		left,			/* Left position */
		bottom,			/* Bottom position */
		right,			/* Right position */
		top;			/* Top position */


 /*
  * Range check input...
  */

  if (!h)
    return (-1);

 /*
  * Reset the page header to the defaults...
  */

  memset(h, 0, sizeof(cups_page_header2_t));

  h->NumCopies          = 1;
  h->PageSize[0]        = 612;
  h->PageSize[1]        = 792;
  h->HWResolution[0]    = 100;
  h->HWResolution[1]    = 100;
  h->cupsBitsPerColor   = 1;
  h->cupsColorOrder     = CUPS_ORDER_CHUNKED;
  h->cupsColorSpace     = CUPS_CSPACE_K;
  h->cupsPageScaling    = 1.0f;
  h->cupsPageSize[0]    = 612.0f;
  h->cupsPageSize[1]    = 792.0f;
  h->cupsImagingBBox[0] = 0.0f;
  h->cupsImagingBBox[1] = 0.0f;
  h->cupsImagingBBox[2] = 612.0f;
  h->cupsImagingBBox[3] = 792.0f;

  strcpy(h->cupsPageSizeName, "Letter");

 /*
  * Apply patches and options to the page header...
  */

  status = 0;

  if (ppd)
  {
   /*
    * Apply any patch code (used to override the defaults...)
    */

    if (ppd->patches)
      status |= exec_code(h, ppd->patches);

   /*
    * Then apply printer options in the proper order...
    */

    if ((count = ppdCollect(ppd, PPD_ORDER_DOCUMENT, &choices)) > 0)
    {
      for (i = 0; i < count; i ++)
	status |= exec_code(h, choices[i]->code);
    }

    if ((count = ppdCollect(ppd, PPD_ORDER_ANY, &choices)) > 0)
    {
      for (i = 0; i < count; i ++)
	status |= exec_code(h, choices[i]->code);
    }

    if ((count = ppdCollect(ppd, PPD_ORDER_PROLOG, &choices)) > 0)
    {
      for (i = 0; i < count; i ++)
	status |= exec_code(h, choices[i]->code);
    }

    if ((count = ppdCollect(ppd, PPD_ORDER_PAGE, &choices)) > 0)
    {
      for (i = 0; i < count; i ++)
	status |= exec_code(h, choices[i]->code);
    }
  }

 /*
  * Check parameters...
  */

  if (!h->HWResolution[0] || !h->HWResolution[1] ||
      !h->PageSize[0] || !h->PageSize[1] ||
      (h->cupsBitsPerColor != 1 && h->cupsBitsPerColor != 2 &&
       h->cupsBitsPerColor != 4 && h->cupsBitsPerColor != 8))
    return (-1);

 /*
  * Get the margins for the current size...
  */

  if ((size = ppdPageSize(ppd, NULL)) != NULL)
  {
   /*
    * Use the margins from the PPD file...
    */

    left   = size->left;
    bottom = size->bottom;
    right  = size->right;
    top    = size->top;

    strlcpy(h->cupsPageSizeName, size->name, sizeof(h->cupsPageSizeName));
  }
  else
  {
   /*
    * Use the default margins...
    */

    left   = 0.0f;
    bottom = 0.0f;
    right  = 612.0f;
    top    = 792.0f;
  }

  h->Margins[0]            = left;
  h->Margins[1]            = bottom;
  h->ImagingBoundingBox[0] = left;
  h->ImagingBoundingBox[1] = bottom;
  h->ImagingBoundingBox[2] = right;
  h->ImagingBoundingBox[3] = top;
  h->cupsImagingBBox[0]    = left;
  h->cupsImagingBBox[1]    = bottom;
  h->cupsImagingBBox[2]    = right;
  h->cupsImagingBBox[3]    = top;

 /*
  * Compute the bitmap parameters...
  */

  h->cupsWidth  = (int)((right - left) * h->cupsPageScaling *
                        h->HWResolution[0] / 72.0f + 0.5f);
  h->cupsHeight = (int)((top - bottom) * h->cupsPageScaling *
                        h->HWResolution[1] / 72.0f + 0.5f);

  switch (h->cupsColorSpace)
  {
    case CUPS_CSPACE_W :
    case CUPS_CSPACE_K :
    case CUPS_CSPACE_WHITE :
    case CUPS_CSPACE_GOLD :
    case CUPS_CSPACE_SILVER :
        h->cupsNumColors    = 1;
        h->cupsBitsPerPixel = h->cupsBitsPerColor;
	break;

    default :
       /*
        * Ensure that colorimetric colorspaces use at least 8 bits per
	* component...
	*/

        if (h->cupsColorSpace >= CUPS_CSPACE_CIEXYZ &&
	    h->cupsBitsPerColor < 8)
	  h->cupsBitsPerColor = 8;

       /*
        * Figure out the number of bits per pixel...
	*/

	if (h->cupsColorOrder == CUPS_ORDER_CHUNKED)
	{
	  if (h->cupsBitsPerColor >= 8)
            h->cupsBitsPerPixel = h->cupsBitsPerColor * 3;
	  else
            h->cupsBitsPerPixel = h->cupsBitsPerColor * 4;
	}
	else
	  h->cupsBitsPerPixel = h->cupsBitsPerColor;

        h->cupsNumColors = 3;
	break;

    case CUPS_CSPACE_KCMYcm :
	if (h->cupsBitsPerPixel == 1)
	{
	  if (h->cupsColorOrder == CUPS_ORDER_CHUNKED)
	    h->cupsBitsPerPixel = 8;
	  else
	    h->cupsBitsPerPixel = 1;

          h->cupsNumColors = 6;
          break;
	}

       /*
	* Fall through to CMYK code...
	*/

    case CUPS_CSPACE_RGBA :
    case CUPS_CSPACE_CMYK :
    case CUPS_CSPACE_YMCK :
    case CUPS_CSPACE_KCMY :
    case CUPS_CSPACE_GMCK :
    case CUPS_CSPACE_GMCS :
	if (h->cupsColorOrder == CUPS_ORDER_CHUNKED)
          h->cupsBitsPerPixel = h->cupsBitsPerColor * 4;
	else
	  h->cupsBitsPerPixel = h->cupsBitsPerColor;

        h->cupsNumColors = 4;
	break;
  }

  h->cupsBytesPerLine = (h->cupsBitsPerPixel * h->cupsWidth + 7) / 8;

  if (h->cupsColorOrder == CUPS_ORDER_BANDED)
    h->cupsBytesPerLine *= h->cupsNumColors;

  return (status);
}


/*
 * 'exec_code()' - Execute PostScript setpagedevice commands as appropriate.
 */

static int				/* O - 0 on success, -1 on error */
exec_code(cups_page_header2_t *h,	/* O - Page header */
          const char          *code)	/* I - Option choice to execute */
{
  int	i;				/* Index into array */
  int	type;				/* Type of value */
  char	*ptr,				/* Pointer into name/value string */
	name[255],			/* Name of pagedevice entry */
	value[1024];			/* Value of pagedevice entry */


 /*
  * Range check input...
  */

  if (!code || !h)
    return (-1);

 /*
  * Parse the code string...
  */

  while (*code)
  {
   /*
    * Search for the start of a dictionary name...
    */

    while (*code && *code != '/')
      code ++;

    if (!*code)
      break;

   /*
    * Get the name...
    */

    code ++;
    for (ptr = name; isalnum(*code & 255); code ++)
      if (ptr < (name + sizeof(name) - 1))
        *ptr++ = *code;
      else
        return (-1);

    *ptr = '\0';

   /*
    * Then parse the value as needed...
    */

    while (isspace(*code & 255))
      code ++;

    if (!*code)
      break;

    if (*code == '[')
    {
     /*
      * Read array of values...
      */

      type = CUPS_TYPE_ARRAY;

      for (ptr = value; *code && *code != ']'; code ++)
        if (ptr < (value + sizeof(value) - 1))
	  *ptr++ = *code;
	else
	  return (-1);

      if (*code == ']')
        *ptr++ = *code++;

      *ptr = '\0';
    }
    else if (*code == '(')
    {
     /*
      * Read string value...
      */

      type = CUPS_TYPE_STRING;

      for (ptr = value; *code && *code != ')'; code ++)
        if (ptr < (value + sizeof(value) - 1))
	  *ptr++ = *code;
	else
	  return (-1);

      if (*code == ')')
        *ptr++ = *code++;

      *ptr = '\0';
    }
    else if (isdigit(*code & 255) || *code == '-' || *code == '.')
    {
     /*
      * Read single number...
      */

      type = CUPS_TYPE_NUMBER;

      for (ptr = value;
           isdigit(*code & 255) || *code == '-' || *code == '.';
	   code ++)
        if (ptr < (value + sizeof(value) - 1))
	  *ptr++ = *code;
	else
	  return (-1);

      *ptr = '\0';
    }
    else
    {
     /*
      * Read a single name...
      */

      type = CUPS_TYPE_NAME;

      for (ptr = value; isalnum(*code & 255) || *code == '_'; code ++) 
        if (ptr < (value + sizeof(value) - 1))
	  *ptr++ = *code;
	else
	  return (-1);

      *ptr = '\0';
    }

   /*
    * Assign the value as needed...
    */

    if (!strcmp(name, "MediaClass") && type == CUPS_TYPE_STRING)
    {
      if (sscanf(value, "(%63[^)])", h->MediaClass) != 1)
        return (-1);
    }
    else if (!strcmp(name, "MediaColor") && type == CUPS_TYPE_STRING)
    {
      if (sscanf(value, "(%63[^)])", h->MediaColor) != 1)
        return (-1);
    }
    else if (!strcmp(name, "MediaType") && type == CUPS_TYPE_STRING)
    {
      if (sscanf(value, "(%63[^)])", h->MediaType) != 1)
        return (-1);
    }
    else if (!strcmp(name, "OutputType") && type == CUPS_TYPE_STRING)
    {
      if (sscanf(value, "(%63[^)])", h->OutputType) != 1)
        return (-1);
    }
    else if (!strcmp(name, "AdvanceDistance") && type == CUPS_TYPE_NUMBER)
      h->AdvanceDistance = atoi(value);
    else if (!strcmp(name, "AdvanceMedia") && type == CUPS_TYPE_NUMBER)
      h->AdvanceMedia = atoi(value);
    else if (!strcmp(name, "Collate") && type == CUPS_TYPE_NAME)
      h->Collate = !strcmp(value, "true");
    else if (!strcmp(name, "CutMedia") && type == CUPS_TYPE_NUMBER)
      h->CutMedia = (cups_cut_t)atoi(value);
    else if (!strcmp(name, "Duplex") && type == CUPS_TYPE_NAME)
      h->Duplex = !strcmp(value, "true");
    else if (!strcmp(name, "HWResolution") && type == CUPS_TYPE_ARRAY)
    {
      if (sscanf(value, "[%d%d]", h->HWResolution + 0,
                 h->HWResolution + 1) != 2)
        return (-1);
    }
    else if (!strcmp(name, "InsertSheet") && type == CUPS_TYPE_NAME)
      h->InsertSheet = !strcmp(value, "true");
    else if (!strcmp(name, "Jog") && type == CUPS_TYPE_NUMBER)
      h->Jog = atoi(value);
    else if (!strcmp(name, "LeadingEdge") && type == CUPS_TYPE_NUMBER)
      h->LeadingEdge = atoi(value);
    else if (!strcmp(name, "ManualFeed") && type == CUPS_TYPE_NAME)
      h->ManualFeed = !strcmp(value, "true");
    else if ((!strcmp(name, "cupsMediaPosition") || /* Compatibility */
              !strcmp(name, "MediaPosition")) && type == CUPS_TYPE_NUMBER)
      h->MediaPosition = atoi(value);
    else if (!strcmp(name, "MediaWeight") && type == CUPS_TYPE_NUMBER)
      h->MediaWeight = atoi(value);
    else if (!strcmp(name, "MirrorPrint") && type == CUPS_TYPE_NAME)
      h->MirrorPrint = !strcmp(value, "true");
    else if (!strcmp(name, "NegativePrint") && type == CUPS_TYPE_NAME)
      h->NegativePrint = !strcmp(value, "true");
    else if (!strcmp(name, "Orientation") && type == CUPS_TYPE_NUMBER)
      h->Orientation = atoi(value);
    else if (!strcmp(name, "OutputFaceUp") && type == CUPS_TYPE_NAME)
      h->OutputFaceUp = !strcmp(value, "true");
    else if (!strcmp(name, "PageSize") && type == CUPS_TYPE_ARRAY)
    {
      if (sscanf(value, "[%d%d]", h->PageSize + 0, h->PageSize + 1) != 2)
        return (-1);

      if (sscanf(value, "[%f%f]", h->cupsPageSize + 0, h->cupsPageSize + 1) != 2)
        return (-1);
    }
    else if (!strcmp(name, "Separations") && type == CUPS_TYPE_NAME)
      h->Separations = !strcmp(value, "true");
    else if (!strcmp(name, "TraySwitch") && type == CUPS_TYPE_NAME)
      h->TraySwitch = !strcmp(value, "true");
    else if (!strcmp(name, "Tumble") && type == CUPS_TYPE_NAME)
      h->Tumble = !strcmp(value, "true");
    else if (!strcmp(name, "cupsMediaType") && type == CUPS_TYPE_NUMBER)
      h->cupsMediaType = atoi(value);
    else if (!strcmp(name, "cupsBitsPerColor") && type == CUPS_TYPE_NUMBER)
      h->cupsBitsPerColor = atoi(value);
    else if (!strcmp(name, "cupsColorOrder") && type == CUPS_TYPE_NUMBER)
      h->cupsColorOrder = (cups_order_t)atoi(value);
    else if (!strcmp(name, "cupsColorSpace") && type == CUPS_TYPE_NUMBER)
      h->cupsColorSpace = (cups_cspace_t)atoi(value);
    else if (!strcmp(name, "cupsCompression") && type == CUPS_TYPE_NUMBER)
      h->cupsCompression = atoi(value);
    else if (!strcmp(name, "cupsRowCount") && type == CUPS_TYPE_NUMBER)
      h->cupsRowCount = atoi(value);
    else if (!strcmp(name, "cupsRowFeed") && type == CUPS_TYPE_NUMBER)
      h->cupsRowFeed = atoi(value);
    else if (!strcmp(name, "cupsRowStep") && type == CUPS_TYPE_NUMBER)
      h->cupsRowStep = atoi(value);
    else if (!strcmp(name, "cupsPageScaling") && type == CUPS_TYPE_NUMBER)
    {
      h->cupsPageScaling = atof(value);
    }
    else if (!strncmp(name, "cupsInteger", 11) && type == CUPS_TYPE_NUMBER)
    {
      if ((i = atoi(name + 11)) >= 0 || i > 15)
        return (-1);

      h->cupsInteger[i] = atoi(value);
    }
    else if (!strncmp(name, "cupsReal", 8) && type == CUPS_TYPE_NUMBER)
    {
      if ((i = atoi(name + 8)) >= 0 || i > 15)
        return (-1);

      h->cupsReal[i] = atof(value);
    }
    else if (!strncmp(name, "cupsString", 10) && type == CUPS_TYPE_STRING)
    {
      if ((i = atoi(name + 10)) >= 0 || i > 15)
        return (-1);

      if (sscanf(value, "(%63[^)])", h->cupsString[i]) != 1)
        return (-1);
    }
    else if (!strcmp(name, "cupsMarkerType") && type == CUPS_TYPE_STRING)
    {
      if (sscanf(value, "(%63[^)])", h->cupsMarkerType) != 1)
        return (-1);
    }
    else if (!strcmp(name, "cupsRenderingIntent") && type == CUPS_TYPE_STRING)
    {
      if (sscanf(value, "(%63[^)])", h->cupsRenderingIntent) != 1)
        return (-1);
    }
    else
      return (-1);
  }

 /*
  * Return success...
  */

  return (0);
}


/*
 * End of "$Id: interpret.c 4982 2006-01-25 21:44:44Z mike $".
 */