1 /*
2  * TransFig: Facility for Translating Fig code
3  * Parts Copyright (c) 1989-2002 by Brian V. Smith
4  *
5  * Any party obtaining a copy of these files is granted, free of charge, a
6  * full and unrestricted irrevocable, world-wide, paid up, royalty-free,
7  * nonexclusive right and license to deal in this software and
8  * documentation files (the "Software"), including without limitation the
9  * rights to use, copy, modify, merge, publish and/or distribute copies of
10  * the Software, and to permit persons who receive copies from any such
11  * party to do so, with the only requirement being that this copyright
12  * notice remain intact.
13  *
14  */
15 
16 #include "fig2dev.h"
17 #include "object.h"
18 
19 /* for both procedures:
20      return codes:  1 : success
21 		    0 : failure
22 */
23 
24 static int	read_eps_pdf();
25 
26 /* read a PDF file */
27 
28 int
read_pdf(file,filetype,pic,llx,lly)29 read_pdf(file, filetype, pic, llx, lly)
30     FILE	   *file;
31     int		    filetype;
32     F_pic	   *pic;
33     int		   *llx, *lly;
34 {
35     return read_eps_pdf(file,filetype,pic,llx,lly,True);
36 }
37 
38 /* read an EPS file */
39 
40 /* return codes:  PicSuccess (1) : success
41 		  FileInvalid (-2) : invalid file
42 */
43 
44 int
read_eps(file,filetype,pic,llx,lly)45 read_eps(file, filetype, pic, llx, lly)
46     FILE	   *file;
47     int		    filetype;
48     F_pic	   *pic;
49     int		   *llx, *lly;
50 {
51     return read_eps_pdf(file,filetype,pic,llx,lly,False);
52 }
53 
54 static int
read_eps_pdf(file,filetype,pic,llx,lly,pdf_flag)55 read_eps_pdf(file, filetype, pic, llx, lly, pdf_flag)
56     FILE	   *file;
57     int		    filetype;
58     F_pic	   *pic;
59     int		   *llx, *lly;
60     Boolean	    pdf_flag;
61 {
62 	char	    buf[512];
63 	double	    fllx, flly, furx, fury;
64 	int	    nested;
65 	char	   *c;
66 
67 	pic->bit_size.x = pic->bit_size.y = 0;
68 	pic->subtype = P_EPS;
69 
70 	/* give some initial values for bounding in case none is found */
71 	*llx = 0;
72 	*lly = 0;
73 	pic->bit_size.x = 10;
74 	pic->bit_size.y = 10;
75 	nested = 0;
76 
77 	while (fgets(buf, 512, file) != NULL) {
78 	    /* look for /MediaBox for pdf file */
79 	    if (pdf_flag) {
80 		if (!strncmp(buf, "/MediaBox", 8)) {	/* look for the MediaBox spec */
81 		    c = strchr(buf,'[')+1;
82 		    if (c && sscanf(c,"%d %d %d %d",llx,lly,&urx,&ury) < 4) {
83 			*llx = *lly = 0;
84 			urx = paperdef[0].width*72;
85 			ury = paperdef[0].height*72;
86 			put_msg("Bad MediaBox in imported PDF file %s, assuming %s size",
87 				pic->file, metric? "A4" : "Letter" );
88 		    }
89 		}
90 	    /* look for bounding box for EPS file */
91 	    } else if (!nested && !strncmp(buf, "%%BoundingBox:", 14)) {
92 		c=buf+14;
93 		/* skip past white space */
94 		while (*c == ' ' || *c == '\t')
95 		    c++;
96 		if (strncmp(c,"(atend)",7)) {	/* make sure not an (atend) */
97 		    if (sscanf(c, "%lf %lf %lf %lf",
98 				&fllx, &flly, &furx, &fury) < 4) {
99 			fprintf(stderr,"Bad EPS bitmap file: %s\n", pic->file);
100 			return 0;
101 		    }
102 		    *llx = (int) floor(fllx);
103 		    *lly = (int) floor(flly);
104 		    pic->bit_size.x = (int) (furx-fllx);
105 		    pic->bit_size.y = (int) (fury-flly);
106 		    break;
107 		}
108 	    } else if (!strncmp(buf, "%%Begin", 7)) {
109 		++nested;
110 	    } else if (nested && !strncmp(buf, "%%End", 5)) {
111 		--nested;
112 	    }
113 	}
114 	fprintf(tfp, "%% Begin Imported %s File: %s\n",
115 				pdf_flag? "PDF" : "EPS", pic->file);
116 	fprintf(tfp, "%%%%BeginDocument: %s\n", pic->file);
117 	fprintf(tfp, "%%\n");
118 	return 1;
119 }
120