1 #include <math.h>
2 #include <string.h>
3 #include <grass/glocale.h>
4 #include "local_proto.h"
5
6 /* test if file is really EPS file and find bbox
7 * returns 1 if OK
8 * 0 on error
9 */
eps_bbox(char * eps,double * llx,double * lly,double * urx,double * ury)10 int eps_bbox(char *eps, double *llx, double *lly, double *urx, double *ury)
11 {
12 char buf[201];
13 FILE *fp;
14 int v1, v2, v3, v4;
15
16 /* test if file is really eps and find bbox */
17 if ((fp = fopen(eps, "r")) == NULL) {
18 G_warning(_("Can't open eps file <%s>"), eps);
19 return (0);
20 }
21 /* test if first row contains '%!PS-Adobe-m.n EPSF-m.n' string */
22 fgets(buf, 200, fp);
23 if (sscanf(buf, "%%!PS-Adobe-%d.%d EPSF-%d.%d", &v1, &v2, &v3, &v4) < 4) {
24 fprintf(stderr, "file <%s> is not in EPS format\n", eps);
25 fclose(fp);
26 return (0);
27 }
28 /* looking for bbox */
29 while (fgets(buf, 200, fp) != NULL) {
30 if (sscanf
31 (buf, "%%%%BoundingBox: %lf %lf %lf %lf", llx, lly, urx,
32 ury) == 4) {
33 fclose(fp);
34 return (1);
35 }
36 }
37 G_warning(_("Bounding box in eps file <%s> was not found"), eps);
38 fclose(fp);
39 return (0);
40 }
41
42 /* calculate translation for EPS file
43 * rotate is in degrees
44 */
eps_trans(double llx,double lly,double urx,double ury,double x,double y,double scale,double rotate,double * xt,double * yt)45 int eps_trans(double llx, double lly, double urx, double ury,
46 double x, double y, double scale, double rotate,
47 double *xt, double *yt)
48 {
49 double xc, yc, angle;
50
51 xc = (llx + urx) / 2;
52 yc = (lly + ury) / 2;
53
54 angle = M_PI * rotate / 180;
55 *xt = x + scale * (yc * sin(angle) - xc * cos(angle));
56 *yt = y - scale * (yc * cos(angle) + xc * sin(angle));
57
58 return (1);
59 }
60
61 /* save EPS file into PS file for later use */
eps_save(FILE * fp,char * epsf,char * name)62 int eps_save(FILE * fp, char *epsf, char *name)
63 {
64 char buf[1024];
65 FILE *epsfp;
66
67 if ((epsfp = fopen(epsf, "r")) == NULL)
68 return (0);
69
70 fprintf(fp, "\n/%s {\n", name);
71 while (fgets(buf, 1024, epsfp) != NULL)
72 fprintf(fp, "%s", buf);
73 fprintf(fp, "} def\n");
74 fclose(epsfp);
75
76 return (1);
77 }
78
79 /* draw EPS file saved by eps_save */
eps_draw_saved(char * name,double x,double y,double scale,double rotate)80 int eps_draw_saved(char *name, double x, double y, double scale,
81 double rotate)
82 {
83 fprintf(PS.fp, "\nBeginEPSF\n");
84 fprintf(PS.fp, "%.5f %.5f translate\n", x, y);
85 fprintf(PS.fp, "%.5f rotate\n", rotate);
86 fprintf(PS.fp, "%.5f %.5f scale\n", scale, scale);
87 fprintf(PS.fp, "%%BeginDocument: %s\n", name);
88
89 fprintf(PS.fp, "%s\n", name);
90
91 fprintf(PS.fp, "%%EndDocument\n");
92 fprintf(PS.fp, "EndEPSF\n");
93
94 return (1);
95 }
96
97
98 /* write EPS file into PS file */
eps_draw(FILE * fp,char * eps,double x,double y,double scale,double rotate)99 int eps_draw(FILE * fp, char *eps, double x, double y, double scale,
100 double rotate)
101 {
102 char buf[1024];
103 FILE *epsfp;
104
105 if ((epsfp = fopen(eps, "r")) == NULL)
106 return (0);
107
108 fprintf(PS.fp, "\nBeginEPSF\n");
109 fprintf(PS.fp, "%.5f %.5f translate\n", x, y);
110 fprintf(PS.fp, "%.5f rotate\n", rotate);
111 fprintf(PS.fp, "%.5f %.5f scale\n", scale, scale);
112 fprintf(PS.fp, "%%BeginDocument: %s\n", eps);
113
114 while (fgets(buf, 1024, epsfp) != NULL)
115 fprintf(fp, "%s", buf);
116
117 fprintf(PS.fp, "%%EndDocument\n");
118 fprintf(PS.fp, "EndEPSF\n");
119 fclose(epsfp);
120
121 return (1);
122 }
123
124 /* save EPS patter file into PS file for later use */
125 /* For pattern we have to remove header comments */
pat_save(FILE * fp,char * epsf,char * name)126 int pat_save(FILE * fp, char *epsf, char *name)
127 {
128 char buf[1024];
129 FILE *epsfp;
130
131 if ((epsfp = fopen(epsf, "r")) == NULL)
132 return (0);
133
134 fprintf(fp, "\n/%s {\n", name);
135 while (fgets(buf, 1024, epsfp) != NULL) {
136 if (strncmp(buf, "%!PS-Adobe", 10) == 0 ||
137 strncmp(buf, "%%BoundingBox", 13) == 0)
138 continue;
139 fprintf(fp, "%s", buf);
140 }
141 fprintf(fp, "} def\n");
142 fclose(epsfp);
143
144 return (1);
145 }
146