1
/* Copyright (C) 2001-2006 Artifex Software, Inc.
4
This software is provided AS-IS with no warranty, either express or
7
This software is distributed under license and may not be copied, modified
8
or distributed except as expressly authorized under the terms of that
9
license. Refer to licensing information at http://www.artifex.com/
10
or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
11
San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
14
/* $Id: dscparse.h 8250 2007-09-25 13:31:24Z giles $*/
15
/* Interface for the DSC parser. */
17
#ifndef dscparse_INCLUDED
18
# define dscparse_INCLUDED
20
/* Some local types that may need modification */
22
typedef unsigned long GSDWORD; /* must be at least 32 bits */
23
typedef unsigned int GSWORD; /* must be at least 16 bits */
26
# define FALSE ((GSBOOL)0)
27
# define TRUE ((GSBOOL)(!FALSE))
30
/* DSC_OFFSET is an unsigned integer which holds the offset
31
* from the start of a file to a particular DSC comment,
32
* or the length of a file.
33
* Normally it is "unsigned long" which is commonly 32 bits.
34
* Change it if you need to handle larger files.
37
# define DSC_OFFSET unsigned long
39
#ifndef DSC_OFFSET_FORMAT
40
# define DSC_OFFSET_FORMAT "lu" /* for printf */
44
# define min(a,b) ((a) < (b) ? (a) : (b))
47
# define max(a,b) ((a) > (b) ? (a) : (b))
50
/* maximum legal length of lines in a DSC compliant file */
51
#define DSC_LINE_LENGTH 255
53
/* memory for strings is allocated in chunks of this length */
54
#define CDSC_STRING_CHUNK 4096
56
/* page array is allocated in chunks of this many pages */
57
#define CDSC_PAGE_CHUNK 128
59
/* buffer length for storing lines passed to dsc_scan_data() */
60
/* must be at least 2 * DSC_LINE_LENGTH */
61
/* We choose 8192 as twice the length passed to us by GSview */
62
#define CDSC_DATA_LENGTH 8192
64
/* Return codes from dsc_scan_data()
68
* -1 = error, usually insufficient memory.
70
* 10-99 = internal codes, should not be seen.
71
* 100-999 = identifier of last DSC comment processed.
74
typedef enum CDSC_RETURN_CODE_e {
75
CDSC_ERROR = -1, /* Fatal error, usually insufficient memory */
77
CDSC_OK = 0, /* OK, no DSC comment found */
78
CDSC_NOTDSC = 1, /* Not DSC, or DSC is being ignored */
81
CDSC_UNKNOWNDSC = 100, /* DSC comment not recognised */
84
CDSC_PSADOBE = 200, /* %!PS-Adobe- */
85
CDSC_BEGINCOMMENTS = 201, /* %%BeginComments */
86
CDSC_ENDCOMMENTS = 202, /* %%EndComments */
87
CDSC_PAGES = 203, /* %%Pages: */
88
CDSC_CREATOR = 204, /* %%Creator: */
89
CDSC_CREATIONDATE = 205, /* %%CreationDate: */
90
CDSC_TITLE = 206, /* %%Title: */
91
CDSC_FOR = 207, /* %%For: */
92
CDSC_LANGUAGELEVEL = 208, /* %%LanguageLevel: */
93
CDSC_BOUNDINGBOX = 209, /* %%BoundingBox: */
94
CDSC_ORIENTATION = 210, /* %%Orientation: */
95
CDSC_PAGEORDER = 211, /* %%PageOrder: */
96
CDSC_DOCUMENTMEDIA = 212, /* %%DocumentMedia: */
97
CDSC_DOCUMENTPAPERSIZES = 213, /* %%DocumentPaperSizes: */
98
CDSC_DOCUMENTPAPERFORMS = 214, /* %%DocumentPaperForms: */
99
CDSC_DOCUMENTPAPERCOLORS = 215, /* %%DocumentPaperColors: */
100
CDSC_DOCUMENTPAPERWEIGHTS = 216, /* %%DocumentPaperWeights: */
101
CDSC_DOCUMENTDATA = 217, /* %%DocumentData: */
102
CDSC_REQUIREMENTS = 218, /* IGNORED %%Requirements: */
103
CDSC_DOCUMENTNEEDEDFONTS = 219, /* IGNORED %%DocumentNeededFonts: */
104
CDSC_DOCUMENTSUPPLIEDFONTS = 220, /* IGNORED %%DocumentSuppliedFonts: */
105
CDSC_HIRESBOUNDINGBOX = 221, /* %%HiResBoundingBox: */
106
CDSC_CROPBOX = 222, /* %%CropBox: */
107
CDSC_PLATEFILE = 223, /* %%PlateFile: (DCS 2.0) */
108
CDSC_DOCUMENTPROCESSCOLORS = 224, /* %%DocumentProcessColors: */
109
CDSC_DOCUMENTCUSTOMCOLORS = 225, /* %%DocumentCustomColors: */
110
CDSC_CMYKCUSTOMCOLOR = 226, /* %%CMYKCustomColor: */
111
CDSC_RGBCUSTOMCOLOR = 227, /* %%RGBCustomColor: */
113
/* Preview section */
114
CDSC_BEGINPREVIEW = 301, /* %%BeginPreview */
115
CDSC_ENDPREVIEW = 302, /* %%EndPreview */
117
/* Defaults section */
118
CDSC_BEGINDEFAULTS = 401, /* %%BeginDefaults */
119
CDSC_ENDDEFAULTS = 402, /* %%EndDefaults */
120
/* also %%PageMedia, %%PageOrientation, %%PageBoundingBox */
123
CDSC_BEGINPROLOG = 501, /* %%BeginProlog */
124
CDSC_ENDPROLOG = 502, /* %%EndProlog */
125
CDSC_BEGINFONT = 503, /* IGNORED %%BeginFont */
126
CDSC_ENDFONT = 504, /* IGNORED %%EndFont */
127
CDSC_BEGINFEATURE = 505, /* IGNORED %%BeginFeature */
128
CDSC_ENDFEATURE = 506, /* IGNORED %%EndFeature */
129
CDSC_BEGINRESOURCE = 507, /* IGNORED %%BeginResource */
130
CDSC_ENDRESOURCE = 508, /* IGNORED %%EndResource */
131
CDSC_BEGINPROCSET = 509, /* IGNORED %%BeginProcSet */
132
CDSC_ENDPROCSET = 510, /* IGNORED %%EndProcSet */
135
CDSC_BEGINSETUP = 601, /* %%BeginSetup */
136
CDSC_ENDSETUP = 602, /* %%EndSetup */
137
CDSC_FEATURE = 603, /* IGNORED %%Feature: */
138
CDSC_PAPERCOLOR = 604, /* IGNORED %%PaperColor: */
139
CDSC_PAPERFORM = 605, /* IGNORED %%PaperForm: */
140
CDSC_PAPERWEIGHT = 606, /* IGNORED %%PaperWeight: */
141
CDSC_PAPERSIZE = 607, /* %%PaperSize: */
142
/* also %%Begin/EndFeature, %%Begin/EndResource */
145
CDSC_PAGE = 700, /* %%Page: */
146
CDSC_PAGETRAILER = 701, /* IGNORED %%PageTrailer */
147
CDSC_BEGINPAGESETUP = 702, /* IGNORED %%BeginPageSetup */
148
CDSC_ENDPAGESETUP = 703, /* IGNORED %%EndPageSetup */
149
CDSC_PAGEMEDIA = 704, /* %%PageMedia: */
150
/* also %%PaperColor, %%PaperForm, %%PaperWeight, %%PaperSize */
151
CDSC_PAGEORIENTATION = 705, /* %%PageOrientation: */
152
CDSC_PAGEBOUNDINGBOX = 706, /* %%PageBoundingBox: */
153
/* also %%Begin/EndFont, %%Begin/EndFeature */
154
/* also %%Begin/EndResource, %%Begin/EndProcSet */
155
CDSC_INCLUDEFONT = 707, /* IGNORED %%IncludeFont: */
156
CDSC_VIEWINGORIENTATION = 708, /* %%ViewingOrientation: */
157
CDSC_PAGECROPBOX = 709, /* %%PageCropBox: */
159
/* Trailer section */
160
CDSC_TRAILER = 800, /* %%Trailer */
161
/* also %%Pages, %%BoundingBox, %%Orientation, %%PageOrder, %%DocumentMedia */
162
/* %%Page is recognised as an error */
163
/* also %%DocumentNeededFonts, %%DocumentSuppliedFonts */
166
CDSC_EOF = 900 /* %%EOF */
170
/* stored in dsc->preview */
171
typedef enum CDSC_PREVIEW_TYPE_e {
179
/* stored in dsc->page_order */
180
typedef enum CDSC_PAGE_ORDER_e {
181
CDSC_ORDER_UNKNOWN = 0,
187
/* stored in dsc->page_orientation and dsc->page[pagenum-1].orientation */
188
typedef enum CDSC_ORIENTATION_ENUM_e {
189
CDSC_ORIENT_UNKNOWN = 0,
194
} CDSC_ORIENTATION_ENUM;
196
/* stored in dsc->document_data */
197
typedef enum CDSC_DOCUMENT_DATA_e {
198
CDSC_DATA_UNKNOWN = 0,
202
} CDSC_DOCUMENT_DATA ;
204
typedef struct CDSCBBOX_S {
211
typedef struct CDSCFBBOX_S {
218
typedef struct CDSCMEDIA_S {
220
float width; /* PostScript points */
222
float weight; /* GSM */
225
CDSCBBOX *mediabox; /* Used by GSview for PDF MediaBox */
228
#define CDSC_KNOWN_MEDIA 11
229
extern const CDSCMEDIA dsc_known_media[CDSC_KNOWN_MEDIA];
231
typedef struct CDSCCTM_S { /* used for %%ViewingOrientation */
240
typedef struct CDSCPAGE_S {
245
unsigned int orientation;
246
const CDSCMEDIA *media;
247
CDSCBBOX *bbox; /* PageBoundingBox, also used by GSview for PDF CropBox */
248
CDSCCTM *viewing_orientation;
249
/* Added 2001-10-19 */
250
CDSCFBBOX *crop_box; /* CropBox */
253
/* binary DOS EPS header */
254
typedef struct CDSCDOSEPS_S {
264
/* macbinary header */
265
typedef struct CDSCMACBIN_S {
266
GSDWORD data_begin; /* EPS */
268
GSDWORD resource_begin; /* PICT */
269
GSDWORD resource_length;
272
/* rather than allocated every string with malloc, we allocate
273
* chunks of 4k and place the (usually) short strings in these
276
typedef struct CDSCSTRING_S CDSCSTRING;
277
struct CDSCSTRING_S {
284
/* Desktop Color Separations - DCS 2.0 */
285
typedef struct CDCS2_S CDCS2;
288
char *filetype; /* Usually EPS */
289
/* For multiple file DCS, location and filename will be set */
290
char *location; /* Local or NULL */
292
/* For single file DCS, begin will be not equals to end */
295
/* We maintain the separations as a linked list */
299
typedef enum CDSC_COLOUR_TYPE_e {
300
CDSC_COLOUR_UNKNOWN=0,
301
CDSC_COLOUR_PROCESS=1, /* %%DocumentProcessColors: */
302
CDSC_COLOUR_CUSTOM=2 /* %%DocumentCustomColors: */
305
typedef enum CDSC_CUSTOM_COLOUR_e {
306
CDSC_CUSTOM_COLOUR_UNKNOWN=0,
307
CDSC_CUSTOM_COLOUR_RGB=1, /* %%RGBCustomColor: */
308
CDSC_CUSTOM_COLOUR_CMYK=2 /* %%CMYKCustomColor: */
309
} CDSC_CUSTOM_COLOUR;
311
typedef struct CDSCCOLOUR_S CDSCCOLOUR;
312
struct CDSCCOLOUR_S {
314
CDSC_COLOUR_TYPE type;
315
CDSC_CUSTOM_COLOUR custom;
316
/* If custom is CDSC_CUSTOM_COLOUR_RGB, the next three are correct */
320
/* If colourtype is CDSC_CUSTOM_COLOUR_CMYK, the next four are correct */
329
/* DSC error reporting */
331
typedef enum CDSC_MESSAGE_ERROR_e {
332
CDSC_MESSAGE_BBOX = 0,
333
CDSC_MESSAGE_EARLY_TRAILER = 1,
334
CDSC_MESSAGE_EARLY_EOF = 2,
335
CDSC_MESSAGE_PAGE_IN_TRAILER = 3,
336
CDSC_MESSAGE_PAGE_ORDINAL = 4,
337
CDSC_MESSAGE_PAGES_WRONG = 5,
338
CDSC_MESSAGE_EPS_NO_BBOX = 6,
339
CDSC_MESSAGE_EPS_PAGES = 7,
340
CDSC_MESSAGE_NO_MEDIA = 8,
341
CDSC_MESSAGE_ATEND = 9,
342
CDSC_MESSAGE_DUP_COMMENT = 10,
343
CDSC_MESSAGE_DUP_TRAILER = 11,
344
CDSC_MESSAGE_BEGIN_END = 12,
345
CDSC_MESSAGE_BAD_SECTION = 13,
346
CDSC_MESSAGE_LONG_LINE = 14,
347
CDSC_MESSAGE_INCORRECT_USAGE = 15
348
} CDSC_MESSAGE_ERROR;
351
typedef enum CDSC_MESSAGE_SEVERITY_e {
352
CDSC_ERROR_INFORM = 0, /* Not an error */
353
CDSC_ERROR_WARN = 1, /* Not a DSC error itself, */
354
CDSC_ERROR_ERROR = 2 /* DSC error */
355
} CDSC_MESSAGE_SEVERITY;
358
typedef enum CDSC_RESPONSE_e {
359
CDSC_RESPONSE_OK = 0,
360
CDSC_RESPONSE_CANCEL = 1,
361
CDSC_RESPONSE_IGNORE_ALL = 2
364
extern const char * const dsc_message[];
368
typedef struct CDSC_s CDSC;
374
GSBOOL dsc; /* TRUE if DSC comments found */
375
GSBOOL ctrld; /* TRUE if has CTRLD at start of stream */
376
GSBOOL pjl; /* TRUE if has HP PJL at start of stream */
377
GSBOOL epsf; /* TRUE if EPSF */
378
GSBOOL pdf; /* TRUE if Portable Document Format */
379
unsigned int preview; /* enum CDSC_PREVIEW_TYPE */
380
char *dsc_version; /* first line of file */
381
unsigned int language_level;
382
unsigned int document_data; /* Clean7Bit, Clean8Bit, Binary */
383
/* enum CDSC_DOCUMENT_DATA */
385
DSC_OFFSET begincomments;
386
DSC_OFFSET endcomments;
387
DSC_OFFSET beginpreview;
388
DSC_OFFSET endpreview;
389
DSC_OFFSET begindefaults;
390
DSC_OFFSET enddefaults;
391
DSC_OFFSET beginprolog;
392
DSC_OFFSET endprolog;
393
DSC_OFFSET beginsetup;
395
DSC_OFFSET begintrailer;
396
DSC_OFFSET endtrailer;
398
unsigned int page_count; /* number of %%Page: pages in document */
399
unsigned int page_pages; /* number of pages in document from %%Pages: */
400
unsigned int page_order; /* enum CDSC_PAGE_ORDER */
401
unsigned int page_orientation; /* the default page orientation */
402
/* enum CDSC_ORIENTATION */
403
CDSCCTM *viewing_orientation;
404
unsigned int media_count; /* number of media items */
405
CDSCMEDIA **media; /* the array of media */
406
const CDSCMEDIA *page_media;/* the default page media */
407
CDSCBBOX *bbox; /* the document bounding box */
408
CDSCBBOX *page_bbox; /* the default page bounding box */
409
CDSCDOSEPS *doseps; /* DOS binary header */
415
unsigned int max_error; /* highest error number that will be reported */
416
const int *severity; /* array of severity values, one per error */
420
void *caller_data; /* pointer to be provided when calling */
421
/* error and debug callbacks */
422
int id; /* last DSC comment found */
423
int scan_section; /* section currently being scanned */
424
/* enum CDSC_SECTION */
426
DSC_OFFSET doseps_end; /* ps_begin+ps_length, otherwise 0 */
427
unsigned int page_chunk_length; /* number of pages allocated */
428
DSC_OFFSET file_length; /* length of document */
429
/* If provided we try to recognise %%Trailer and %%EOF */
430
/* incorrectly embedded inside document. */
431
/* We will not parse DSC comments beyond this point. */
432
/* Can be left set to default value of 0 */
433
int skip_document; /* recursion level of %%BeginDocument: */
434
int skip_bytes; /* #bytes to ignore from BeginData: */
435
/* or DOSEPS preview section */
436
int skip_lines; /* #lines to ignore from BeginData: */
437
GSBOOL skip_pjl; /* TRUE if skip PJL until first PS comment */
438
int begin_font_count; /* recursion level of %%BeginFont */
439
int begin_feature_count; /* recursion level of %%BeginFeature */
440
int begin_resource_count; /* recursion level of %%BeginResource */
441
int begin_procset_count; /* recursion level of %%BeginProcSet */
443
/* buffer for input */
444
char data[CDSC_DATA_LENGTH];/* start of buffer */
445
unsigned int data_length; /* length of data in buffer */
446
unsigned int data_index; /* offset to next char in buffer */
447
DSC_OFFSET data_offset; /* offset from start of document */
448
/* to byte in data[0] */
449
GSBOOL eof; /* TRUE if there is no more data */
451
/* information about DSC line */
452
char *line; /* pointer to last read DSC line */
453
/* not null terminated */
454
unsigned int line_length; /* number of characters in line */
455
GSBOOL eol; /* TRUE if dsc_line contains EOL */
456
GSBOOL last_cr; /* TRUE if last line ended in \r */
457
/* check next time for \n */
458
unsigned int line_count; /* line number */
459
GSBOOL long_line; /* TRUE if found a line longer than 255 characters */
460
char last_line[256]; /* previous DSC line, used for %%+ */
462
/* more efficient string storage (for short strings) than malloc */
463
CDSCSTRING *string_head; /* linked list head */
464
CDSCSTRING *string; /* current list item */
466
/* memory allocation routines */
467
void *(*memalloc)(size_t size, void *closure_data);
468
void (*memfree)(void *ptr, void *closure_data);
469
void *mem_closure_data;
471
/* function for printing debug messages */
472
void (*debug_print_fn)(void *caller_data, const char *str);
474
/* function for reporting errors in DSC comments */
475
int (*dsc_error_fn)(void *caller_data, CDSC *dsc,
476
unsigned int explanation, const char *line, unsigned int line_len);
479
/* Added 2001-10-01 */
480
CDSCFBBOX *hires_bbox; /* the hires document bounding box */
481
CDSCFBBOX *crop_box; /* the size of the trimmed page */
482
CDCS2 *dcs2; /* Desktop Color Separations 2.0 */
483
CDSCCOLOUR *colours; /* Process and custom colours */
486
/* Added 2002-03-30 */
490
/* Added 2003-07-15 */
491
CDSCMACBIN *macbin; /* Mac Binary header */
495
/* Public functions */
497
/* Create and initialise DSC parser */
498
CDSC *dsc_init(void *caller_data);
500
CDSC *dsc_init_with_alloc(
502
void *(*memalloc)(size_t size, void *closure_data),
503
void (*memfree)(void *ptr, void *closure_data),
506
/* Free the DSC parser */
507
void dsc_free(CDSC *dsc);
509
/* Reference counting for dsc structure */
510
/* dsc_new is the same as dsc_init */
511
/* dsc_ref is called by dsc_new */
512
/* If dsc_unref decrements to 0, dsc_free will be called */
513
CDSC *dsc_new(void *caller_data);
514
int dsc_ref(CDSC *dsc);
515
int dsc_unref(CDSC *dsc);
517
/* Tell DSC parser how long document will be, to allow ignoring
518
* of early %%Trailer and %%EOF. This is optional.
520
void dsc_set_length(CDSC *dsc, DSC_OFFSET len);
522
/* Process a buffer containing DSC comments and PostScript */
523
int dsc_scan_data(CDSC *dsc, const char *data, int len);
525
/* All data has been processed, fixup any DSC errors */
526
int dsc_fixup(CDSC *dsc);
528
/* Install error query function */
529
void dsc_set_error_function(CDSC *dsc,
530
int (*dsc_error_fn)(void *caller_data, CDSC *dsc,
531
unsigned int explanation, const char *line, unsigned int line_len));
533
/* Install print function for debug messages */
534
void dsc_set_debug_function(CDSC *dsc,
535
void (*debug_fn)(void *caller_data, const char *str));
537
/* Print a message to debug output, if provided */
538
void dsc_debug_print(CDSC *dsc, const char *str);
540
/* Given a page number, find the filename for multi-file DCS 2.0 */
541
const char * dsc_find_platefile(CDSC *dsc, int page);
543
/* Compare two strings, case insensitive */
544
int dsc_stricmp(const char *s, const char *t);
546
/* should be internal only functions, but made available to
547
* GSview for handling PDF
549
int dsc_add_page(CDSC *dsc, int ordinal, char *label);
550
int dsc_add_media(CDSC *dsc, CDSCMEDIA *media);
551
int dsc_set_page_bbox(CDSC *dsc, unsigned int page_number,
552
int llx, int lly, int urx, int ury);
555
void dsc_display(CDSC *dsc, void (*dfn)(void *ptr, const char *str));
556
#endif /* dscparse_INCLUDED */