2 * ps.c -- Postscript scanning and copying routines.
3 * Copyright (C) 1992, 1998 Timothy O. Theisen
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 * Author: Tim Theisen Systems Programmer
20 * Internet: tim@cs.wisc.edu Department of Computer Sciences
21 * UUCP: uwvax!tim University of Wisconsin-Madison
22 * Phone: (608)262-0438 1210 West Dayton Street
23 * FAX: (608)262-9777 Madison, WI 53706
26 /* 18/3/98 Jake Hamby patch */
29 * 98/03/17: Jake Hamby (jehamby@lightside.com):
30 * Added support for compressed/gzipped Postscript and PDF files.
31 * Compressed files are gunzipped to a temporary file, and PDF files are
32 * scanned by calling Ghostscript to generate a fake DSC file.
33 * This is based on code from GV 3.5.8, which is available at:
34 * http://wwwthep.physik.uni-mainz.de/~plass/gv/
37 /* GV by Johannes Plass
38 * Department of Physics
39 * Johannes Gutenberg University
42 * <plass@thep.physik.uni-mainz.de>
56 #include <X11/Xos.h> /* #includes the appropriate <string.h> */
58 #include "gsdefaults.h"
64 /* length calculates string length at compile time */
65 /* can only be used with character constants */
66 #define length(a) (sizeof(a)-1)
67 #define iscomment(a, b) (strncmp(a, b, length(b)) == 0)
68 #define DSCcomment(a) (a[0] == '%' && a[1] == '%')
70 /* list of standard paper sizes from Adobe's PPD. */
72 /*--------------------------------------------------*/
73 /* Declarations for ps_io_*() routines. */
75 typedef struct FileDataStruct_ *FileData;
77 typedef struct FileDataStruct_ {
78 FILE *file; /* file */
79 int file_desc; /* file descriptor corresponding to file */
80 int filepos; /* file position corresponding to the start of the line */
81 char *buf; /* buffer */
82 int buf_size; /* size of buffer */
83 int buf_end; /* last char in buffer given as offset to buf */
84 int line_begin; /* start of the line given as offset to buf */
85 int line_end; /* end of the line given as offset to buf */
86 int line_len; /* length of line, i.e. (line_end-line_begin) */
87 char line_termchar; /* char exchanged for a '\0' at end of line */
88 int status; /* 0 = okay, 1 = failed */
91 static FileData ps_io_init (FILE *file);
92 static void ps_io_exit (FileData data);
93 static char *ps_io_fgetchars (FileData data, int offset);
95 static char *skipped_line = "% ps_io_fgetchars: skipped line";
96 static char *empty_string = "";
98 static char *readline (FileData fd, char **lineP, long *positionP, unsigned int *line_lenP);
99 static char *gettextline(char *line);
100 static char *get_next_text(char *line, char **next_char);
101 static int blank(char *line);
104 pages_new(struct page *pages, int current, int maxpages)
106 struct page *oldpages = pages;
108 pages = g_new0(struct page, maxpages);
110 pages = g_renew(struct page, oldpages, maxpages);
111 for(; current < maxpages; current++) {
112 memset(&(pages[current]), 0x00, sizeof(struct page));
113 pages[current].orientation = GTK_GS_ORIENTATION_NONE;
119 * psscan -- scan the PostScript file for document structuring comments.
121 * This scanner is designed to retrieve the information necessary for
122 * the ghostview previewer. It will scan files that conform to any
123 * version (1.0, 2.0, 2.1, or 3.0) of the document structuring conventions.
124 * It does not really care which version of comments the file contains.
125 * (The comments are largely upward compatible.) It will scan a number
126 * of non-conforming documents. (You could have part of the document
127 * conform to V2.0 and the rest conform to V3.0. It would be similar
128 * to the DC-2 1/2+, it would look funny but it can still fly.)
130 * This routine returns a pointer to the document structure.
131 * The structure contains the information relevant to previewing.
132 * These include EPSF flag (to tell if the file is a encapsulated figure),
133 * Page Size (for the Page Size), Bounding Box (to minimize backing
134 * pixmap size or determine window size for encapsulated PostScript),
135 * Orientation of Paper (for default transformation matrix), and
136 * Page Order. The Title, Creator, and CreationDate are also retrieved to
137 * help identify the document.
139 * The following comments are examined:
142 * Must start with %!PS-Adobe-. Version numbers ignored.
143 * Also allowed to be just %!PS, many files seem to have that.
145 * %!PS-Adobe-* [EPSF-*]
146 * %%BoundingBox: <int> <int> <int> <int>|(atend)
147 * %%Creator: <textline>
148 * %%CreationDate: <textline>
149 * %%Orientation: Portrait|Landscape|(atend)
150 * %%Pages: <uint> [<int>]|(atend)
151 * %%PageOrder: Ascend|Descend|Special|(atend)
152 * %%Title: <textline>
153 * %%DocumentMedia: <text> <real> <real> <real> <text> <text>
154 * %%DocumentPaperSizes: <text>
157 * Note: Either the 3.0 or 2.0 syntax for %%Pages is accepted.
158 * Also either the 2.0 %%DocumentPaperSizes or the 3.0
159 * %%DocumentMedia comments are accepted as well.
161 * The header section ends either explicitly with %%EndComments or
162 * implicitly with any line that does not begin with %X where X is
163 * a not whitespace character.
165 * If the file is encapsulated PostScript the optional Preview section
171 * This section explicitly begins and ends with the above comments.
173 * Next the Defaults section for version 3 page defaults:
176 * %%PageBoundingBox: <int> <int> <int> <int>
177 * %%PageOrientation: Portrait|Landscape
178 * %%PageMedia: <text>
181 * This section explicitly begins and ends with the above comments.
183 * The prolog section either explicitly starts with %%BeginProlog or
184 * implicitly with any nonblank line.
189 * The Prolog should end with %%EndProlog, however the proglog implicitly
190 * ends when %%BeginSetup, %%Page, %%Trailer or %%EOF are encountered.
192 * The Setup section is where the version 2 page defaults are found.
193 * This section either explicitly begins with %%BeginSetup or implicitly
194 * with any nonblank line after the Prolog.
197 * %%PageBoundingBox: <int> <int> <int> <int>
198 * %%PageOrientation: Portrait|Landscape
199 * %%PaperSize: <text>
202 * The Setup should end with %%EndSetup, however the setup implicitly
203 * ends when %%Page, %%Trailer or %%EOF are encountered.
205 * Next each page starts explicitly with %%Page and ends implicitly with
206 * %%Page or %%Trailer or %%EOF. The following comments are recognized:
208 * %%Page: <text> <uint>
209 * %%PageBoundingBox: <int> <int> <int> <int>|(atend)
210 * %%PageOrientation: Portrait|Landscape
211 * %%PageMedia: <text>
212 * %%PaperSize: <text>
214 * The tralier section start explicitly with %%Trailer and end with %%EOF.
215 * The following comment are examined with the proper (atend) notation
216 * was used in the header:
219 * %%BoundingBox: <int> <int> <int> <int>|(atend)
220 * %%Orientation: Portrait|Landscape|(atend)
221 * %%Pages: <uint> [<int>]|(atend)
222 * %%PageOrder: Ascend|Descend|Special|(atend)
226 * + A DC-3 received severe damage to one of its wings. The wing was a total
227 * loss. There was no replacement readily available, so the mechanic
228 * installed a wing from a DC-2.
234 psscan(FILE * file, int respect_eof, const gchar * fname)
236 struct document *doc;
238 int pages_set = NONE;
239 int page_order_set = NONE;
240 int orientation_set = NONE;
241 int page_bb_set = NONE;
242 int page_size_set = NONE;
243 int preread; /* flag which tells the readline isn't needed */
245 unsigned int maxpages = 0;
246 unsigned int nextpage = 1; /* Next expected page */
247 unsigned int thispage;
248 int ignore = 0; /* whether to ignore page ordinals */
251 char text[PSLINELENGTH]; /* Temporary storage for text */
252 long position; /* Position of the current line */
253 long beginsection; /* Position of the beginning of the section */
254 unsigned int line_len; /* Length of the current line */
255 unsigned int section_len; /* Place to accumulate the section length */
256 char *next_char; /* 1st char after text returned by get_next_text() */
259 GtkGSPaperSize *papersizes = gtk_gs_defaults_get_paper_sizes();
267 fd = ps_io_init(file);
268 if (!readline(fd, &line, &position, &line_len)) {
269 fprintf(stderr, "Warning: empty file.\n");
274 /* HP printer job language data follows. Some printer drivers add pjl
275 * commands to switch a pjl printer to postscript mode. If no PS header
276 * follows, this seems to be a real pjl file. */
277 if(iscomment(line, "\033%-12345X@PJL")) {
278 /* read until first DSC comment */
279 while(readline(fd, &line, &position, &line_len)
280 && (line[0] != '%')) ;
282 g_print("psscan error: input files seems to be a PJL file.\n");
287 /* Header comments */
289 /* Header should start with "%!PS-Adobe-", but some programms omit
290 * parts of this or add a ^D at the beginning. */
291 if(iscomment(line, "%!PS") || iscomment(line, "\004%!PS")) {
292 doc = g_new0(struct document, 1);
293 doc->default_page_orientation = GTK_GS_ORIENTATION_NONE;
294 doc->orientation = GTK_GS_ORIENTATION_NONE;
296 /* ignore possible leading ^D */
297 if(*line == '\004') {
302 /* Jake Hamby patch 18/3/98 */
305 sscanf(line, "%*s %256s", text);
306 /*doc->epsf = iscomment(text, "EPSF-"); */
307 doc->epsf = iscomment(text, "EPSF"); /* Hamby - This line changed */
308 doc->beginheader = position;
309 section_len = line_len;
312 /* There are postscript documents that do not have
313 %PS at the beginning, usually unstructured. We should GS decide
314 For instance, the tech reports at this university:
316 http://svrc.it.uq.edu.au/Bibliography/svrc-tr.html?94-45
318 add ugly PostScript before the actual document.
321 able to display them correctly as unstructured PS.
323 In a way, this makes sense, a program PostScript does not need
324 the !PS at the beginning.
326 doc = g_new0(struct document, 1);
327 doc->default_page_orientation = GTK_GS_ORIENTATION_NONE;
328 doc->orientation = GTK_GS_ORIENTATION_NONE;
333 while(preread || readline(fd, &line, &position, &line_len)) {
335 section_len += line_len;
338 iscomment(line + 1, "%EndComments") ||
339 line[1] == ' ' || line[1] == '\t' || line[1] == '\n' ||
343 else if(line[1] != '%') {
346 else if(doc->title == NULL && iscomment(line + 2, "Title:")) {
347 doc->title = gettextline(line + length("%%Title:"));
349 else if(doc->date == NULL && iscomment(line + 2, "CreationDate:")) {
350 doc->date = gettextline(line + length("%%CreationDate:"));
352 else if(doc->creator == NULL && iscomment(line + 2, "Creator:")) {
353 doc->creator = gettextline(line + length("%%Creator:"));
355 else if(bb_set == NONE && iscomment(line + 2, "BoundingBox:")) {
356 sscanf(line + length("%%BoundingBox:"), "%256s", text);
357 if(strcmp(text, "(atend)") == 0) {
361 if(sscanf(line + length("%%BoundingBox:"), "%d %d %d %d",
362 &(doc->boundingbox[LLX]),
363 &(doc->boundingbox[LLY]),
364 &(doc->boundingbox[URX]), &(doc->boundingbox[URY])) == 4)
367 float fllx, flly, furx, fury;
368 if(sscanf(line + length("%%BoundingBox:"), "%f %f %f %f",
369 &fllx, &flly, &furx, &fury) == 4) {
371 doc->boundingbox[LLX] = fllx;
372 doc->boundingbox[LLY] = flly;
373 doc->boundingbox[URX] = furx;
374 doc->boundingbox[URY] = fury;
375 if(fllx < doc->boundingbox[LLX])
376 doc->boundingbox[LLX]--;
377 if(flly < doc->boundingbox[LLY])
378 doc->boundingbox[LLY]--;
379 if(furx > doc->boundingbox[URX])
380 doc->boundingbox[URX]++;
381 if(fury > doc->boundingbox[URY])
382 doc->boundingbox[URY]++;
387 else if(orientation_set == NONE && iscomment(line + 2, "Orientation:")) {
388 sscanf(line + length("%%Orientation:"), "%256s", text);
389 if(strcmp(text, "(atend)") == 0) {
390 orientation_set = ATEND;
392 else if(strcmp(text, "Portrait") == 0) {
393 doc->orientation = GTK_GS_ORIENTATION_PORTRAIT;
396 else if(strcmp(text, "Landscape") == 0) {
397 doc->orientation = GTK_GS_ORIENTATION_LANDSCAPE;
400 else if(strcmp(text, "Seascape") == 0) {
401 doc->orientation = GTK_GS_ORIENTATION_SEASCAPE;
405 else if(page_order_set == NONE && iscomment(line + 2, "PageOrder:")) {
406 sscanf(line + length("%%PageOrder:"), "%256s", text);
407 if(strcmp(text, "(atend)") == 0) {
408 page_order_set = ATEND;
410 else if(strcmp(text, "Ascend") == 0) {
411 doc->pageorder = ASCEND;
414 else if(strcmp(text, "Descend") == 0) {
415 doc->pageorder = DESCEND;
418 else if(strcmp(text, "Special") == 0) {
419 doc->pageorder = SPECIAL;
423 else if(pages_set == NONE && iscomment(line + 2, "Pages:")) {
424 sscanf(line + length("%%Pages:"), "%256s", text);
425 if(strcmp(text, "(atend)") == 0) {
429 switch (sscanf(line + length("%%Pages:"), "%d %d", &maxpages, &i)) {
431 if(page_order_set == NONE) {
433 doc->pageorder = DESCEND;
437 doc->pageorder = SPECIAL;
441 doc->pageorder = ASCEND;
447 doc->pages = pages_new(NULL, 0, maxpages);
451 else if(doc->numsizes == NONE && iscomment(line + 2, "DocumentMedia:")) {
453 doc->size = g_new0(GtkGSPaperSize, 1);
455 get_next_text(line + length("%%DocumentMedia:"), &next_char);
456 if(doc->size[0].name != NULL) {
457 if(sscanf(next_char, "%f %f", &w, &h) == 2) {
458 doc->size[0].width = w + 0.5;
459 doc->size[0].height = h + 0.5;
461 if(doc->size[0].width != 0 && doc->size[0].height != 0)
464 g_free(doc->size[0].name);
467 while(readline(fd, &line, &position, &line_len) &&
468 DSCcomment(line) && iscomment(line + 2, "+")) {
469 section_len += line_len;
470 doc->size = g_renew(GtkGSPaperSize, doc->size, doc->numsizes + 1);
471 doc->size[doc->numsizes].name =
472 get_next_text(line + length("%%+"), &next_char);
473 if(doc->size[doc->numsizes].name != NULL) {
474 if(sscanf(next_char, "%f %f", &w, &h) == 2) {
475 doc->size[doc->numsizes].width = w + 0.5;
476 doc->size[doc->numsizes].height = h + 0.5;
478 if(doc->size[doc->numsizes].width != 0 &&
479 doc->size[doc->numsizes].height != 0)
482 g_free(doc->size[doc->numsizes].name);
485 section_len += line_len;
486 if(doc->numsizes != 0)
487 doc->default_page_size = doc->size;
489 else if(doc->numsizes == NONE && iscomment(line + 2, "DocumentPaperSizes:")) {
491 doc->size = g_new0(GtkGSPaperSize, 1);
493 get_next_text(line + length("%%DocumentPaperSizes:"), &next_char);
494 if(doc->size[0].name != NULL) {
495 doc->size[0].width = 0;
496 doc->size[0].height = 0;
497 for(dmp = papersizes; dmp->name != NULL; dmp++) {
498 /* Note: Paper size comment uses down cased paper size
499 * name. Case insensitive compares are only used for
500 * PaperSize comments.
502 if(strcasecmp(doc->size[0].name, dmp->name) == 0) {
503 g_free(doc->size[0].name);
504 doc->size[0].name = g_strdup(dmp->name);
505 doc->size[0].width = dmp->width;
506 doc->size[0].height = dmp->height;
510 if(doc->size[0].width != 0 && doc->size[0].height != 0)
513 g_free(doc->size[0].name);
515 while((cp = get_next_text(next_char, &next_char))) {
516 doc->size = g_renew(GtkGSPaperSize, doc->size, doc->numsizes + 1);
517 doc->size[doc->numsizes].name = cp;
518 doc->size[doc->numsizes].width = 0;
519 doc->size[doc->numsizes].height = 0;
520 for(dmp = papersizes; dmp->name != NULL; dmp++) {
521 /* Note: Paper size comment uses down cased paper size
522 * name. Case insensitive compares are only used for
523 * PaperSize comments.
525 if(strcasecmp(doc->size[doc->numsizes].name, dmp->name) == 0) {
526 g_free(doc->size[doc->numsizes].name);
527 doc->size[doc->numsizes].name = g_strdup(dmp->name);
528 doc->size[doc->numsizes].name = dmp->name;
529 doc->size[doc->numsizes].width = dmp->width;
530 doc->size[doc->numsizes].height = dmp->height;
534 if(doc->size[doc->numsizes].width != 0 &&
535 doc->size[doc->numsizes].height != 0)
538 g_free(doc->size[doc->numsizes].name);
541 while(readline(fd, &line, &position, &line_len) &&
542 DSCcomment(line) && iscomment(line + 2, "+")) {
543 section_len += line_len;
544 next_char = line + length("%%+");
545 while((cp = get_next_text(next_char, &next_char))) {
546 doc->size = g_renew(GtkGSPaperSize, doc->size, doc->numsizes + 1);
547 doc->size[doc->numsizes].name = cp;
548 doc->size[doc->numsizes].width = 0;
549 doc->size[doc->numsizes].height = 0;
550 for(dmp = papersizes; dmp->name != NULL; dmp++) {
551 /* Note: Paper size comment uses down cased paper size
552 * name. Case insensitive compares are only used for
553 * PaperSize comments.
555 if(strcasecmp(doc->size[doc->numsizes].name, dmp->name) == 0) {
556 doc->size[doc->numsizes].width = dmp->width;
557 doc->size[doc->numsizes].height = dmp->height;
561 if(doc->size[doc->numsizes].width != 0 &&
562 doc->size[doc->numsizes].height != 0)
565 g_free(doc->size[doc->numsizes].name);
568 section_len += line_len;
569 if(doc->numsizes != 0)
570 doc->default_page_size = doc->size;
574 if(DSCcomment(line) && iscomment(line + 2, "EndComments")) {
575 readline(fd, &line, &position, &line_len);
576 section_len += line_len;
578 doc->endheader = position;
579 doc->lenheader = section_len - line_len;
581 /* Optional Preview comments for encapsulated PostScript files */
583 beginsection = position;
584 section_len = line_len;
585 while(blank(line) && readline(fd, &line, &position, &line_len)) {
586 section_len += line_len;
589 if(doc->epsf && DSCcomment(line) && iscomment(line + 2, "BeginPreview")) {
590 doc->beginpreview = beginsection;
592 while(readline(fd, &line, &position, &line_len) &&
593 !(DSCcomment(line) && iscomment(line + 2, "EndPreview"))) {
594 section_len += line_len;
596 section_len += line_len;
597 readline(fd, &line, &position, &line_len);
598 section_len += line_len;
599 doc->endpreview = position;
600 doc->lenpreview = section_len - line_len;
603 /* Page Defaults for Version 3.0 files */
605 if(beginsection == 0) {
606 beginsection = position;
607 section_len = line_len;
609 while(blank(line) && readline(fd, &line, &position, &line_len)) {
610 section_len += line_len;
613 if(DSCcomment(line) && iscomment(line + 2, "BeginDefaults")) {
614 doc->begindefaults = beginsection;
616 while(readline(fd, &line, &position, &line_len) &&
617 !(DSCcomment(line) && iscomment(line + 2, "EndDefaults"))) {
618 section_len += line_len;
619 if(!DSCcomment(line)) {
622 else if(doc->default_page_orientation == GTK_GS_ORIENTATION_NONE &&
623 iscomment(line + 2, "PageOrientation:")) {
624 sscanf(line + length("%%PageOrientation:"), "%256s", text);
625 if(strcmp(text, "Portrait") == 0) {
626 doc->default_page_orientation = GTK_GS_ORIENTATION_PORTRAIT;
628 else if(strcmp(text, "Landscape") == 0) {
629 doc->default_page_orientation = GTK_GS_ORIENTATION_LANDSCAPE;
631 else if(strcmp(text, "Seascape") == 0) {
632 doc->default_page_orientation = GTK_GS_ORIENTATION_SEASCAPE;
635 else if(page_size_set == NONE && iscomment(line + 2, "PageMedia:")) {
636 cp = get_next_text(line + length("%%PageMedia:"), NULL);
637 for(dmp = doc->size, i = 0; i < doc->numsizes; i++, dmp++) {
638 if(strcmp(cp, dmp->name) == 0) {
639 doc->default_page_size = dmp;
646 else if(page_bb_set == NONE && iscomment(line + 2, "PageBoundingBox:")) {
647 if(sscanf(line + length("%%PageBoundingBox:"), "%d %d %d %d",
648 &(doc->default_page_boundingbox[LLX]),
649 &(doc->default_page_boundingbox[LLY]),
650 &(doc->default_page_boundingbox[URX]),
651 &(doc->default_page_boundingbox[URY])) == 4)
654 float fllx, flly, furx, fury;
656 (line + length("%%PageBoundingBox:"), "%f %f %f %f",
657 &fllx, &flly, &furx, &fury) == 4) {
659 doc->default_page_boundingbox[LLX] = fllx;
660 doc->default_page_boundingbox[LLY] = flly;
661 doc->default_page_boundingbox[URX] = furx;
662 doc->default_page_boundingbox[URY] = fury;
663 if(fllx < doc->default_page_boundingbox[LLX])
664 doc->default_page_boundingbox[LLX]--;
665 if(flly < doc->default_page_boundingbox[LLY])
666 doc->default_page_boundingbox[LLY]--;
667 if(furx > doc->default_page_boundingbox[URX])
668 doc->default_page_boundingbox[URX]++;
669 if(fury > doc->default_page_boundingbox[URY])
670 doc->default_page_boundingbox[URY]++;
675 section_len += line_len;
676 readline(fd, &line, &position, &line_len);
677 section_len += line_len;
678 doc->enddefaults = position;
679 doc->lendefaults = section_len - line_len;
682 /* Document Prolog */
684 if(beginsection == 0) {
685 beginsection = position;
686 section_len = line_len;
688 while(blank(line) && readline(fd, &line, &position, &line_len)) {
689 section_len += line_len;
692 if(!(DSCcomment(line) &&
693 (iscomment(line + 2, "BeginSetup") ||
694 iscomment(line + 2, "Page:") ||
695 iscomment(line + 2, "Trailer") || iscomment(line + 2, "EOF")))) {
696 doc->beginprolog = beginsection;
701 readline(fd, &line, &position, &line_len)) &&
702 !(DSCcomment(line) &&
703 (iscomment(line + 2, "EndProlog") ||
704 iscomment(line + 2, "BeginSetup") ||
705 iscomment(line + 2, "Page:") ||
706 iscomment(line + 2, "Trailer") || iscomment(line + 2, "EOF")))) {
708 section_len += line_len;
711 section_len += line_len;
712 if(DSCcomment(line) && iscomment(line + 2, "EndProlog")) {
713 readline(fd, &line, &position, &line_len);
714 section_len += line_len;
716 doc->endprolog = position;
717 doc->lenprolog = section_len - line_len;
720 /* Document Setup, Page Defaults found here for Version 2 files */
722 if(beginsection == 0) {
723 beginsection = position;
724 section_len = line_len;
726 while(blank(line) && readline(fd, &line, &position, &line_len)) {
727 section_len += line_len;
730 if(!(DSCcomment(line) &&
731 (iscomment(line + 2, "Page:") ||
732 iscomment(line + 2, "Trailer") ||
733 (respect_eof && iscomment(line + 2, "EOF"))))) {
734 doc->beginsetup = beginsection;
738 readline(fd, &line, &position, &line_len)) &&
739 !(DSCcomment(line) &&
740 (iscomment(line + 2, "EndSetup") ||
741 iscomment(line + 2, "Page:") ||
742 iscomment(line + 2, "Trailer") ||
743 (respect_eof && iscomment(line + 2, "EOF"))))) {
745 section_len += line_len;
747 if(!DSCcomment(line)) {
750 else if(doc->default_page_orientation == GTK_GS_ORIENTATION_NONE &&
751 iscomment(line + 2, "PageOrientation:")) {
752 sscanf(line + length("%%PageOrientation:"), "%256s", text);
753 if(strcmp(text, "Portrait") == 0) {
754 doc->default_page_orientation = GTK_GS_ORIENTATION_PORTRAIT;
756 else if(strcmp(text, "Landscape") == 0) {
757 doc->default_page_orientation = GTK_GS_ORIENTATION_LANDSCAPE;
759 else if(strcmp(text, "Seascape") == 0) {
760 doc->default_page_orientation = GTK_GS_ORIENTATION_SEASCAPE;
763 else if(page_size_set == NONE && iscomment(line + 2, "PaperSize:")) {
764 cp = get_next_text(line + length("%%PaperSize:"), NULL);
765 for(dmp = doc->size, i = 0; i < doc->numsizes; i++, dmp++) {
766 /* Note: Paper size comment uses down cased paper size
767 * name. Case insensitive compares are only used for
768 * PaperSize comments.
770 if(strcasecmp(cp, dmp->name) == 0) {
771 doc->default_page_size = dmp;
778 else if(page_bb_set == NONE && iscomment(line + 2, "PageBoundingBox:")) {
779 if(sscanf(line + length("%%PageBoundingBox:"), "%d %d %d %d",
780 &(doc->default_page_boundingbox[LLX]),
781 &(doc->default_page_boundingbox[LLY]),
782 &(doc->default_page_boundingbox[URX]),
783 &(doc->default_page_boundingbox[URY])) == 4)
786 float fllx, flly, furx, fury;
788 (line + length("%%PageBoundingBox:"), "%f %f %f %f",
789 &fllx, &flly, &furx, &fury) == 4) {
791 doc->default_page_boundingbox[LLX] = fllx;
792 doc->default_page_boundingbox[LLY] = flly;
793 doc->default_page_boundingbox[URX] = furx;
794 doc->default_page_boundingbox[URY] = fury;
795 if(fllx < doc->default_page_boundingbox[LLX])
796 doc->default_page_boundingbox[LLX]--;
797 if(flly < doc->default_page_boundingbox[LLY])
798 doc->default_page_boundingbox[LLY]--;
799 if(furx > doc->default_page_boundingbox[URX])
800 doc->default_page_boundingbox[URX]++;
801 if(fury > doc->default_page_boundingbox[URY])
802 doc->default_page_boundingbox[URY]++;
807 section_len += line_len;
808 if(DSCcomment(line) && iscomment(line + 2, "EndSetup")) {
809 readline(fd, &line, &position, &line_len);
810 section_len += line_len;
812 doc->endsetup = position;
813 doc->lensetup = section_len - line_len;
816 /* HACK: Mozilla 1.8 Workaround.
818 It seems that Mozilla 1.8 generates important postscript code
819 after the '%%EndProlog' and before the first page comment '%%Page: x y'.
820 See comment below also.
823 if(doc->beginprolog && !doc->beginsetup) {
824 doc->lenprolog += section_len - line_len;
825 doc->endprolog = position;
828 /* HACK: Windows NT Workaround
830 Mark Pfeifer (pfeiferm%ppddev@comet.cmis.abbott.com) noticed
831 about problems when viewing Windows NT 3.51 generated postscript
832 files with gv. He found that the relevant postscript files
833 show important postscript code after the '%%EndSetup' and before
834 the first page comment '%%Page: x y'.
836 if(doc->beginsetup) {
837 while(!(DSCcomment(line) &&
838 (iscomment(line + 2, "EndSetup") ||
839 (iscomment(line + 2, "Page:") ||
840 iscomment(line + 2, "Trailer") ||
841 (respect_eof && iscomment(line + 2, "EOF"))))) &&
842 (readline(fd, &line, &position, &line_len))) {
843 section_len += line_len;
844 doc->lensetup = section_len - line_len;
845 doc->endsetup = position;
849 /* Individual Pages */
851 if(beginsection == 0) {
852 beginsection = position;
853 section_len = line_len;
855 while(blank(line) && readline(fd, &line, &position, &line_len)) {
856 section_len += line_len;
861 while(DSCcomment(line) && iscomment(line + 2, "Page:")) {
864 doc->pages = pages_new(NULL, 0, maxpages);
866 label = get_next_text(line + length("%%Page:"), &next_char);
867 if(sscanf(next_char, "%d", &thispage) != 1)
870 ignore = thispage != 1;
872 if(!ignore && thispage != nextpage) {
878 if(doc->numpages == maxpages) {
880 doc->pages = pages_new(doc->pages, maxpages - 1, maxpages);
883 doc->pages[doc->numpages].label = label;
885 doc->pages[doc->numpages].begin = beginsection;
889 doc->pages[doc->numpages].begin = position;
890 section_len = line_len;
893 while(readline(fd, &line, &position, &line_len) &&
894 !(DSCcomment(line) &&
895 (iscomment(line + 2, "Page:") ||
896 iscomment(line + 2, "Trailer") ||
897 (respect_eof && iscomment(line + 2, "EOF"))))) {
898 section_len += line_len;
899 if(!DSCcomment(line)) {
902 else if(doc->pages[doc->numpages].orientation == NONE &&
903 iscomment(line + 2, "PageOrientation:")) {
904 sscanf(line + length("%%PageOrientation:"), "%256s", text);
905 if(strcmp(text, "Portrait") == 0) {
906 doc->pages[doc->numpages].orientation = GTK_GS_ORIENTATION_PORTRAIT;
908 else if(strcmp(text, "Landscape") == 0) {
909 doc->pages[doc->numpages].orientation = GTK_GS_ORIENTATION_LANDSCAPE;
911 else if(strcmp(text, "Seascape") == 0) {
912 doc->pages[doc->numpages].orientation = GTK_GS_ORIENTATION_SEASCAPE;
915 else if(doc->pages[doc->numpages].size == NULL &&
916 iscomment(line + 2, "PageMedia:")) {
917 cp = get_next_text(line + length("%%PageMedia:"), NULL);
918 for(dmp = doc->size, i = 0; i < doc->numsizes; i++, dmp++) {
919 if(strcmp(cp, dmp->name) == 0) {
920 doc->pages[doc->numpages].size = dmp;
926 else if(doc->pages[doc->numpages].size == NULL &&
927 iscomment(line + 2, "PaperSize:")) {
928 cp = get_next_text(line + length("%%PaperSize:"), NULL);
929 for(dmp = doc->size, i = 0; i < doc->numsizes; i++, dmp++) {
930 /* Note: Paper size comment uses down cased paper size
931 * name. Case insensitive compares are only used for
932 * PaperSize comments.
934 if(strcasecmp(cp, dmp->name) == 0) {
935 doc->pages[doc->numpages].size = dmp;
941 else if((page_bb_set == NONE || page_bb_set == ATEND) &&
942 iscomment(line + 2, "PageBoundingBox:")) {
943 sscanf(line + length("%%PageBoundingBox:"), "%256s", text);
944 if(strcmp(text, "(atend)") == 0) {
949 (line + length("%%PageBoundingBox:"), "%d %d %d %d",
950 &(doc->pages[doc->numpages].boundingbox[LLX]),
951 &(doc->pages[doc->numpages].boundingbox[LLY]),
952 &(doc->pages[doc->numpages].boundingbox[URX]),
953 &(doc->pages[doc->numpages].boundingbox[URY])) == 4) {
954 if(page_bb_set == NONE)
958 float fllx, flly, furx, fury;
959 if(sscanf(line + length("%%PageBoundingBox:"),
960 "%f %f %f %f", &fllx, &flly, &furx, &fury) == 4) {
961 if(page_bb_set == NONE)
963 doc->pages[doc->numpages].boundingbox[LLX] = fllx;
964 doc->pages[doc->numpages].boundingbox[LLY] = flly;
965 doc->pages[doc->numpages].boundingbox[URX] = furx;
966 doc->pages[doc->numpages].boundingbox[URY] = fury;
967 if(fllx < doc->pages[doc->numpages].boundingbox[LLX])
968 doc->pages[doc->numpages].boundingbox[LLX]--;
969 if(flly < doc->pages[doc->numpages].boundingbox[LLY])
970 doc->pages[doc->numpages].boundingbox[LLY]--;
971 if(furx > doc->pages[doc->numpages].boundingbox[URX])
972 doc->pages[doc->numpages].boundingbox[URX]++;
973 if(fury > doc->pages[doc->numpages].boundingbox[URY])
974 doc->pages[doc->numpages].boundingbox[URY]++;
980 section_len += line_len;
981 doc->pages[doc->numpages].end = position;
982 doc->pages[doc->numpages].len = section_len - line_len;
986 /* Document Trailer */
989 doc->begintrailer = beginsection;
993 doc->begintrailer = position;
994 section_len = line_len;
999 readline(fd, &line, &position, &line_len)) &&
1000 !(respect_eof && DSCcomment(line) && iscomment(line + 2, "EOF"))) {
1002 section_len += line_len;
1004 if(!DSCcomment(line)) {
1007 else if(iscomment(line + 2, "Page:")) {
1008 g_free(get_next_text(line + length("%%Page:"), &next_char));
1009 if(sscanf(next_char, "%d", &thispage) != 1)
1011 if(!ignore && thispage == nextpage) {
1012 if(doc->numpages > 0) {
1013 doc->pages[doc->numpages - 1].end = position;
1014 doc->pages[doc->numpages - 1].len += section_len - line_len;
1018 doc->endsetup = position;
1019 doc->endsetup += section_len - line_len;
1021 else if(doc->endprolog) {
1022 doc->endprolog = position;
1023 doc->endprolog += section_len - line_len;
1029 else if(!respect_eof && iscomment(line + 2, "Trailer")) {
1030 /* What we thought was the start of the trailer was really */
1031 /* the trailer of an EPS on the page. */
1032 /* Set the end of the page to this trailer and keep scanning. */
1033 if(doc->numpages > 0) {
1034 doc->pages[doc->numpages - 1].end = position;
1035 doc->pages[doc->numpages - 1].len += section_len - line_len;
1037 doc->begintrailer = position;
1038 section_len = line_len;
1040 else if(bb_set == ATEND && iscomment(line + 2, "BoundingBox:")) {
1041 if(sscanf(line + length("%%BoundingBox:"), "%d %d %d %d",
1042 &(doc->boundingbox[LLX]),
1043 &(doc->boundingbox[LLY]),
1044 &(doc->boundingbox[URX]), &(doc->boundingbox[URY])) != 4) {
1045 float fllx, flly, furx, fury;
1046 if(sscanf(line + length("%%BoundingBox:"), "%f %f %f %f",
1047 &fllx, &flly, &furx, &fury) == 4) {
1048 doc->boundingbox[LLX] = fllx;
1049 doc->boundingbox[LLY] = flly;
1050 doc->boundingbox[URX] = furx;
1051 doc->boundingbox[URY] = fury;
1052 if(fllx < doc->boundingbox[LLX])
1053 doc->boundingbox[LLX]--;
1054 if(flly < doc->boundingbox[LLY])
1055 doc->boundingbox[LLY]--;
1056 if(furx > doc->boundingbox[URX])
1057 doc->boundingbox[URX]++;
1058 if(fury > doc->boundingbox[URY])
1059 doc->boundingbox[URY]++;
1063 else if(orientation_set == ATEND && iscomment(line + 2, "Orientation:")) {
1064 sscanf(line + length("%%Orientation:"), "%256s", text);
1065 if(strcmp(text, "Portrait") == 0) {
1066 doc->orientation = GTK_GS_ORIENTATION_PORTRAIT;
1068 else if(strcmp(text, "Landscape") == 0) {
1069 doc->orientation = GTK_GS_ORIENTATION_LANDSCAPE;
1071 else if(strcmp(text, "Seascape") == 0) {
1072 doc->orientation = GTK_GS_ORIENTATION_SEASCAPE;
1075 else if(page_order_set == ATEND && iscomment(line + 2, "PageOrder:")) {
1076 sscanf(line + length("%%PageOrder:"), "%256s", text);
1077 if(strcmp(text, "Ascend") == 0) {
1078 doc->pageorder = ASCEND;
1080 else if(strcmp(text, "Descend") == 0) {
1081 doc->pageorder = DESCEND;
1083 else if(strcmp(text, "Special") == 0) {
1084 doc->pageorder = SPECIAL;
1087 else if(pages_set == ATEND && iscomment(line + 2, "Pages:")) {
1088 if(sscanf(line + length("%%Pages:"), "%*u %d", &i) == 1) {
1089 if(page_order_set == NONE) {
1091 doc->pageorder = DESCEND;
1093 doc->pageorder = SPECIAL;
1095 doc->pageorder = ASCEND;
1100 section_len += line_len;
1101 if(DSCcomment(line) && iscomment(line + 2, "EOF")) {
1102 readline(fd, &line, &position, &line_len);
1103 section_len += line_len;
1105 doc->endtrailer = position;
1106 doc->lentrailer = section_len - line_len;
1109 section_len = line_len;
1111 while(preread || readline(line, sizeof line, file, &position, &line_len)) {
1113 section_len += line_len;
1115 if(DSCcomment(line) && iscomment(line + 2, "Page:")) {
1116 g_free(get_next_text(line + length("%%Page:"), &next_char));
1117 if(sscanf(next_char, "%d", &thispage) != 1)
1119 if(!ignore && thispage == nextpage) {
1120 if(doc->numpages > 0) {
1121 doc->pages[doc->numpages - 1].end = position;
1122 doc->pages[doc->numpages - 1].len += doc->lentrailer +
1123 section_len - line_len;
1127 doc->endsetup = position;
1128 doc->endsetup += doc->lentrailer + section_len - line_len;
1130 else if(doc->endprolog) {
1131 doc->endprolog = position;
1132 doc->endprolog += doc->lentrailer + section_len - line_len;
1144 * psfree -- free dynamic storage associated with document structure.
1149 struct document *doc;
1155 printf("This document exists\n");
1157 for(i = 0; i < doc->numpages; i++) {
1158 if(doc->pages[i].label)
1159 g_free(doc->pages[i].label);
1161 for(i = 0; i < doc->numsizes; i++) {
1162 if(doc->size[i].name)
1163 g_free(doc->size[i].name);
1170 g_free(doc->creator);
1180 * gettextine -- skip over white space and return the rest of the line.
1181 * If the text begins with '(' return the text string
1182 * using get_next_text().
1186 gettextline(char *line)
1190 while(*line && (*line == ' ' || *line == '\t'))
1193 return get_next_text(line, NULL);
1196 if(strlen(line) == 0)
1199 cp = g_strdup(line);
1201 /* Remove end of line */
1202 if(cp[strlen(line) - 2] == '\r' && cp[strlen(line) - 1] == '\n')
1203 /* Handle DOS \r\n */
1204 cp[strlen(line) - 2] = '\0';
1205 else if(cp[strlen(line) - 1] == '\n' || cp[strlen(line) - 1] == '\r')
1206 /* Handle mac and unix */
1207 cp[strlen(line) - 1] = '\0';
1214 * get_next_text -- return the next text string on the line.
1215 * return NULL if nothing is present.
1219 get_next_text(line, next_char)
1223 char text[PSLINELENGTH]; /* Temporary storage for text */
1227 while(*line && (*line == ' ' || *line == '\t'))
1234 while(*line && !(*line == ')' && level == 0)) {
1236 if(*(line + 1) == 'n') {
1240 else if(*(line + 1) == 'r') {
1244 else if(*(line + 1) == 't') {
1248 else if(*(line + 1) == 'b') {
1252 else if(*(line + 1) == 'f') {
1256 else if(*(line + 1) == '\\') {
1260 else if(*(line + 1) == '(') {
1264 else if(*(line + 1) == ')') {
1268 else if(*(line + 1) >= '0' && *(line + 1) <= '9') {
1269 if(*(line + 2) >= '0' && *(line + 2) <= '9') {
1270 if(*(line + 3) >= '0' && *(line + 3) <= '9') {
1272 ((*(line + 1) - '0') * 8 + *(line + 2) -
1273 '0') * 8 + *(line + 3) - '0';
1277 *cp++ = (*(line + 1) - '0') * 8 + *(line + 2) - '0';
1282 *cp++ = *(line + 1) - '0';
1291 else if(*line == '(') {
1295 else if(*line == ')') {
1305 while(*line && !(*line == ' ' || *line == '\t' || *line == '\n'))
1311 if(!quoted && strlen(text) == 0)
1313 return g_strdup(text);
1317 * pscopy -- copy lines of Postscript from a section of one file
1319 * Automatically switch to binary copying whenever
1320 * %%BeginBinary/%%EndBinary or %%BeginData/%%EndData
1321 * comments are encountered.
1325 pscopy(from, to, begin, end)
1328 long begin; /* set negative to avoid initial seek */
1331 char line[PSLINELENGTH]; /* 255 characters + 1 newline + 1 NULL */
1332 char text[PSLINELENGTH]; /* Temporary storage for text */
1338 fseek(from, begin, SEEK_SET);
1339 while(ftell(from) < end) {
1340 fgets(line, sizeof line, from);
1341 gtk_gs_doc_sink_write(to, line, strlen(line));
1343 if(!(DSCcomment(line) && iscomment(line + 2, "Begin"))) {
1346 else if(iscomment(line + 7, "Data:")) {
1348 if(sscanf(line + length("%%BeginData:"), "%d %*s %256s", &num, text) >= 1) {
1349 if(strcmp(text, "Lines") == 0) {
1350 for(i = 0; i < num; i++) {
1351 fgets(line, sizeof(line), from);
1352 gtk_gs_doc_sink_write(to, line, strlen(line));
1356 while(num > BUFSIZ) {
1357 fread(buf, sizeof(char), BUFSIZ, from);
1358 gtk_gs_doc_sink_write(to, buf, BUFSIZ);
1361 fread(buf, sizeof(char), num, from);
1362 gtk_gs_doc_sink_write(to, buf, num);
1366 else if(iscomment(line + 7, "Binary:")) {
1367 if(sscanf(line + length("%%BeginBinary:"), "%d", &num) == 1) {
1368 while(num > BUFSIZ) {
1369 fread(buf, sizeof(char), BUFSIZ, from);
1370 gtk_gs_doc_sink_write(to, buf, BUFSIZ);
1373 fread(buf, sizeof(char), num, from);
1374 gtk_gs_doc_sink_write(to, buf, num);
1381 * pscopyuntil -- copy lines of Postscript from a section of one file
1382 * to another file until a particular comment is reached.
1383 * Automatically switch to binary copying whenever
1384 * %%BeginBinary/%%EndBinary or %%BeginData/%%EndData
1385 * comments are encountered.
1389 pscopyuntil(FILE * from, GtkGSDocSink * to, long begin, long end,
1390 const char *comment)
1392 char line[PSLINELENGTH]; /* 255 characters + 1 newline + 1 NULL */
1393 char text[PSLINELENGTH]; /* Temporary storage for text */
1400 comment_length = strlen(comment);
1404 fseek(from, begin, SEEK_SET);
1406 while(ftell(from) < end && !feof(from)) {
1407 fgets(line, sizeof line, from);
1409 /* iscomment cannot be used here,
1410 * because comment_length is not known at compile time. */
1411 if(comment != NULL && strncmp(line, comment, comment_length) == 0) {
1412 return g_strdup(line);
1414 gtk_gs_doc_sink_write(to, line, strlen(line));
1415 if(!(DSCcomment(line) && iscomment(line + 2, "Begin"))) {
1418 else if(iscomment(line + 7, "Data:")) {
1420 if(sscanf(line + length("%%BeginData:"), "%d %*s %256s", &num, text) >= 1) {
1421 if(strcmp(text, "Lines") == 0) {
1422 for(i = 0; i < num; i++) {
1423 fgets(line, sizeof line, from);
1424 gtk_gs_doc_sink_write(to, line, strlen(line));
1428 while(num > BUFSIZ) {
1429 fread(buf, sizeof(char), BUFSIZ, from);
1430 gtk_gs_doc_sink_write(to, buf, BUFSIZ);
1433 fread(buf, sizeof(char), num, from);
1434 gtk_gs_doc_sink_write(to, buf, num);
1438 else if(iscomment(line + 7, "Binary:")) {
1439 if(sscanf(line + length("%%BeginBinary:"), "%d", &num) == 1) {
1440 while(num > BUFSIZ) {
1441 fread(buf, sizeof(char), BUFSIZ, from);
1442 gtk_gs_doc_sink_write(to, buf, BUFSIZ);
1445 fread(buf, sizeof(char), num, from);
1446 gtk_gs_doc_sink_write(to, buf, num);
1454 * blank -- determine whether the line contains nothing but whitespace.
1462 while(*cp == ' ' || *cp == '\t')
1464 return *cp == '\n' || (*cp == '%' && (line[0] != '%' || line[1] != '%'));
1467 /*##########################################################*/
1469 /* Copy the headers, marked pages, and trailer to fp */
1470 /*##########################################################*/
1473 pscopydoc(GtkGSDocSink * dest,
1474 char *src_filename, struct document *d, gint * pagelist)
1477 char text[PSLINELENGTH];
1479 gboolean pages_written = FALSE;
1480 gboolean pages_atend = FALSE;
1486 src_file = fopen(src_filename, "r");
1489 for(i = 0; i < d->numpages; i++) {
1494 here = d->beginheader;
1496 while((comment = pscopyuntil(src_file, dest, here, d->endheader, "%%Pages:"))) {
1497 here = ftell(src_file);
1498 if(pages_written || pages_atend) {
1502 sscanf(comment + length("%%Pages:"), "%256s", text);
1503 if(strcmp(text, "(atend)") == 0) {
1504 gtk_gs_doc_sink_write(dest, comment, strlen(comment));
1508 switch (sscanf(comment + length("%%Pages:"), "%*d %d", &i)) {
1510 gtk_gs_doc_sink_printf(dest, "%%%%Pages: %d %d\n", pages, i);
1513 gtk_gs_doc_sink_printf(dest, "%%%%Pages: %d\n", pages);
1516 pages_written = TRUE;
1520 pscopyuntil(src_file, dest, d->beginpreview, d->endpreview, NULL);
1521 pscopyuntil(src_file, dest, d->begindefaults, d->enddefaults, NULL);
1522 pscopyuntil(src_file, dest, d->beginprolog, d->endprolog, NULL);
1523 pscopyuntil(src_file, dest, d->beginsetup, d->endsetup, NULL);
1525 for(i = 0; i < d->numpages; i++) {
1526 if(d->pageorder == DESCEND)
1527 j = (d->numpages - 1) - i;
1532 comment = pscopyuntil(src_file, dest,
1533 d->pages[i].begin, d->pages[i].end, "%%Page:");
1534 gtk_gs_doc_sink_printf(dest, "%%%%Page: %s %d\n",
1535 d->pages[i].label, page++);
1537 pscopyuntil(src_file, dest, -1, d->pages[i].end, NULL);
1541 here = d->begintrailer;
1542 while((comment = pscopyuntil(src_file, dest, here, d->endtrailer,
1544 here = ftell(src_file);
1549 switch (sscanf(comment + length("%%Pages:"), "%*d %d", &i)) {
1551 gtk_gs_doc_sink_printf(dest, "%%%%Pages: %d %d\n", pages, i);
1554 gtk_gs_doc_sink_printf(dest, "%%%%Pages: %d\n", pages);
1557 pages_written = TRUE;
1564 /*----------------------------------------------------------*/
1566 /*----------------------------------------------------------*/
1568 #define FD_FILE (fd->file)
1569 #define FD_FILE_DESC (fd->file_desc)
1570 #define FD_FILEPOS (fd->filepos)
1571 #define FD_LINE_BEGIN (fd->line_begin)
1572 #define FD_LINE_END (fd->line_end)
1573 #define FD_LINE_LEN (fd->line_len)
1574 #define FD_LINE_TERMCHAR (fd->line_termchar)
1575 #define FD_BUF (fd->buf)
1576 #define FD_BUF_END (fd->buf_end)
1577 #define FD_BUF_SIZE (fd->buf_size)
1578 #define FD_STATUS (fd->status)
1580 #define FD_STATUS_OKAY 0
1581 #define FD_STATUS_BUFTOOLARGE 1
1582 #define FD_STATUS_NOMORECHARS 2
1584 #define LINE_CHUNK_SIZE 4096
1585 #define MAX_PS_IO_FGETCHARS_BUF_SIZE 57344
1586 #define BREAK_PS_IO_FGETCHARS_BUF_SIZE 49152
1588 static FileData ps_io_init(file)
1592 size_t size = sizeof(FileDataStruct);
1594 fd = (FileData) g_malloc(size);
1595 memset((void*) fd ,0,(size_t)size);
1599 FD_FILE_DESC = fileno(file);
1600 FD_FILEPOS = ftell(file);
1601 FD_BUF_SIZE = (2*LINE_CHUNK_SIZE)+1;
1602 FD_BUF = g_malloc(FD_BUF_SIZE);
1607 /*----------------------------------------------------------*/
1609 /*----------------------------------------------------------*/
1619 /*----------------------------------------------------------*/
1621 /*----------------------------------------------------------*/
1624 ps_io_fseek(fd,offset)
1629 status=fseek(FD_FILE,(long)offset,SEEK_SET);
1630 FD_BUF_END = FD_LINE_BEGIN = FD_LINE_END = FD_LINE_LEN = 0;
1631 FD_FILEPOS = offset;
1632 FD_STATUS = FD_STATUS_OKAY;
1636 /*----------------------------------------------------------*/
1638 /*----------------------------------------------------------*/
1647 /*----------------------------------------------------------*/
1648 /* ps_io_fgetchars */
1649 /*----------------------------------------------------------*/
1651 #ifdef USE_MEMMOVE_CODE
1652 static void ps_memmove (d, s, l)
1657 if (s < d) for (s += l, d += l; l; --l) *--d = *--s;
1658 else if (s != d) for (; l; --l) *d++ = *s++;
1661 # define ps_memmove memmove
1664 static char * ps_io_fgetchars(fd,num)
1668 char *eol=NULL,*tmp;
1669 size_t size_of_char = sizeof(char);
1671 if (FD_STATUS != FD_STATUS_OKAY) {
1675 FD_BUF[FD_LINE_END] = FD_LINE_TERMCHAR; /* restoring char previously exchanged against '\0' */
1676 FD_LINE_BEGIN = FD_LINE_END;
1679 if (num<0) { /* reading whole line */
1680 if (FD_BUF_END-FD_LINE_END) {
1681 /* strpbrk is faster but fails on lines with embedded NULLs
1682 eol = strpbrk(FD_BUF+FD_LINE_END,"\n\r");
1684 tmp = FD_BUF + FD_BUF_END;
1685 eol = FD_BUF + FD_LINE_END;
1686 while (eol < tmp && *eol != '\n' && *eol != '\r') eol++;
1687 if (eol >= tmp) eol = NULL;
1689 if (*eol=='\r' && *(eol+1)=='\n') eol += 2;
1694 } else { /* reading specified num of chars */
1695 if (FD_BUF_END >= FD_LINE_BEGIN+num) {
1696 eol = FD_BUF+FD_LINE_BEGIN+num;
1701 if (FD_BUF_END - FD_LINE_BEGIN > BREAK_PS_IO_FGETCHARS_BUF_SIZE) {
1702 eol = FD_BUF + FD_BUF_END - 1;
1706 while (FD_BUF_SIZE < FD_BUF_END+LINE_CHUNK_SIZE+1) {
1707 if (FD_BUF_SIZE > MAX_PS_IO_FGETCHARS_BUF_SIZE) {
1708 /* we should never get here, since the line is broken
1709 artificially after BREAK_PS_IO_FGETCHARS_BUF_SIZE bytes. */
1710 fprintf(stderr, "gv: ps_io_fgetchars: Fatal Error: buffer became too large.\n");
1713 if (FD_LINE_BEGIN) {
1714 ps_memmove((void*)FD_BUF,(void*)(FD_BUF+FD_LINE_BEGIN),
1715 ((size_t)(FD_BUF_END-FD_LINE_BEGIN+1))*size_of_char);
1716 FD_BUF_END -= FD_LINE_BEGIN;
1719 FD_BUF_SIZE = FD_BUF_SIZE+LINE_CHUNK_SIZE+1;
1720 FD_BUF = g_realloc(FD_BUF,FD_BUF_SIZE);
1724 FD_LINE_END = FD_BUF_END;
1726 /* different existing VMS file formats require that we use read here ###jp###,10/12/96 */
1727 if (num<0) FD_BUF_END += read(FD_FILE_DESC,FD_BUF+FD_BUF_END,LINE_CHUNK_SIZE);
1728 else FD_BUF_END += fread(FD_BUF+FD_BUF_END,size_of_char,LINE_CHUNK_SIZE,FD_FILE);
1730 /* read() seems to fail sometimes (? ? ?) so we always use fread ###jp###,07/31/96*/
1731 FD_BUF_END += fread(FD_BUF+FD_BUF_END,size_of_char,LINE_CHUNK_SIZE,FD_FILE);
1734 FD_BUF[FD_BUF_END] = '\0';
1735 if (FD_BUF_END-FD_LINE_END == 0) {
1736 FD_STATUS = FD_STATUS_NOMORECHARS;
1742 FD_LINE_END = eol - FD_BUF;
1743 FD_LINE_LEN = FD_LINE_END - FD_LINE_BEGIN;
1744 FD_LINE_TERMCHAR = FD_BUF[FD_LINE_END];
1745 FD_BUF[FD_LINE_END] = '\0';
1746 #ifdef USE_FTELL_FOR_FILEPOS
1747 if (FD_LINE_END==FD_BUF_END) {
1749 For VMS we cannot assume that the record is FD_LINE_LEN bytes long
1750 on the disk. For stream_lf and stream_cr that is true, but not for
1751 other formats, since VAXC/DECC converts the formatting into a single \n.
1752 eg. variable format files have a 2-byte length and padding to an even
1753 number of characters. So, we use ftell for each record.
1754 This still will not work if we need to fseek to a \n or \r inside a
1755 variable record (ftell always returns the start of the record in this
1757 (Tim Adye, adye@v2.rl.ac.uk)
1759 FD_FILEPOS = ftell(FD_FILE);
1761 #endif /* USE_FTELL_FOR_FILEPOS */
1762 FD_FILEPOS += FD_LINE_LEN;
1764 return(FD_BUF+FD_LINE_BEGIN);
1767 /*----------------------------------------------------------*/
1770 Read the next line in the postscript file.
1771 Automatically skip over data (as indicated by
1772 %%BeginBinary/%%EndBinary or %%BeginData/%%EndData
1774 Also, skip over included documents (as indicated by
1775 %%BeginDocument/%%EndDocument comments.)
1777 /*----------------------------------------------------------*/
1779 static char *readline (fd, lineP, positionP, line_lenP)
1783 unsigned int *line_lenP;
1785 unsigned int nbytes=0;
1789 if (positionP) *positionP = FD_FILEPOS;
1790 line = ps_io_fgetchars(fd,-1);
1793 *lineP = empty_string;
1797 *line_lenP = FD_LINE_LEN;
1799 #define IS_COMMENT(comment) \
1800 (DSCcomment(line) && iscomment(line+2,(comment)))
1801 #define IS_BEGIN(comment) \
1802 (iscomment(line+7,(comment)))
1803 #define SKIP_WHILE(cond) \
1804 while (readline(fd, &line, NULL, &nbytes) && (cond)) *line_lenP += nbytes;\
1806 #define SKIP_UNTIL_1(comment) { \
1807 SKIP_WHILE((!IS_COMMENT(comment))) \
1809 #define SKIP_UNTIL_2(comment1,comment2) { \
1810 SKIP_WHILE((!IS_COMMENT(comment1) && !IS_COMMENT(comment2)))\
1813 if (!IS_COMMENT("Begin")) {} /* Do nothing */
1814 else if IS_BEGIN("Document:") SKIP_UNTIL_1("EndDocument")
1815 else if IS_BEGIN("Feature:") SKIP_UNTIL_1("EndFeature")
1816 #ifdef USE_ACROREAD_WORKAROUND
1817 else if IS_BEGIN("File") SKIP_UNTIL_2("EndFile","EOF")
1819 else if IS_BEGIN("File") SKIP_UNTIL_1("EndFile")
1821 else if IS_BEGIN("Font") SKIP_UNTIL_1("EndFont")
1822 else if IS_BEGIN("ProcSet") SKIP_UNTIL_1("EndProcSet")
1823 else if IS_BEGIN("Resource") SKIP_UNTIL_1("EndResource")
1824 else if IS_BEGIN("Data:") {
1827 if (FD_LINE_LEN > 100) FD_BUF[100] = '\0';
1829 if (sscanf(line+length("%%BeginData:"), "%d %*s %s", &num, text) >= 1) {
1830 if (strcmp(text, "Lines") == 0) {
1832 line = ps_io_fgetchars(fd,-1);
1833 if (line) *line_lenP += FD_LINE_LEN;
1837 int read_chunk_size = LINE_CHUNK_SIZE;
1839 if (num <= LINE_CHUNK_SIZE) read_chunk_size=num;
1840 line = ps_io_fgetchars(fd,read_chunk_size);
1841 if (line) *line_lenP += FD_LINE_LEN;
1842 num -= read_chunk_size;
1846 SKIP_UNTIL_1("EndData")
1848 else if IS_BEGIN("Binary:") {
1850 if (sscanf(line+length("%%BeginBinary:"), "%d", &num) == 1) {
1851 int read_chunk_size = LINE_CHUNK_SIZE;
1853 if (num <= LINE_CHUNK_SIZE) read_chunk_size=num;
1854 line = ps_io_fgetchars(fd,read_chunk_size);
1855 if (line) *line_lenP += FD_LINE_LEN;
1856 num -= read_chunk_size;
1858 SKIP_UNTIL_1("EndBinary")
1863 *line_lenP += nbytes;
1864 *lineP = skipped_line;
1866 *lineP = FD_BUF+FD_LINE_BEGIN;
1869 return(FD_BUF+FD_LINE_BEGIN);