File: eps.c

package info (click to toggle)
grass 6.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 40,044 kB
  • ctags: 31,303
  • sloc: ansic: 321,125; tcl: 25,676; sh: 11,176; cpp: 10,098; makefile: 5,025; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (140 lines) | stat: -rw-r--r-- 3,665 bytes parent folder | download | duplicates (3)
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
#include <math.h>
#include <string.h>
#include "ps_info.h"
#include "local_proto.h"

/*  test if file is realy EPS file and find bbox 
 *  returns  1 if OK
 *           0 on error               
*/ 
int eps_bbox (char *eps, double *llx, double *lly, double *urx, double *ury)
{
    char buf[201];
    FILE *fp;
    int  v1,v2,v3,v4;

    /* test if file is realy eps and find bbox */
    if ((fp = fopen(eps, "r")) == NULL)
    { 
	fprintf (stderr,"can't open eps file <%s>\n", eps);
	return (0);
    }
    /* test if first row contains '%!PS-Adobe-m.n EPSF-m.n' string */
    fgets(buf, 200, fp);
    if (sscanf(buf, "%%!PS-Adobe-%d.%d EPSF-%d.%d", &v1, &v2, &v3, &v4 ) < 4)
    {
	fprintf (stderr,"file <%s> is not in EPS format\n", eps);
	fclose (fp);
	return (0);		
    }
    /* looking for bbox */
    while ( fgets(buf, 200, fp) != NULL )
    {	
	if (sscanf(buf, "%%%%BoundingBox: %lf %lf %lf %lf", llx, lly, urx, ury) == 4)
	{
	    fclose (fp);
	    return (1);
	}    	
    }
    fprintf (stderr,"Bounding box in eps file <%s> was not found\n", eps);	    
    fclose (fp);	
    return (0);
}

/*  calculate translation for EPS file 
 * rotate is in degrees
 */ 
int eps_trans (double llx, double lly, double urx, double ury, 
              double x, double y, double scale, double rotate, 
	      double *xt, double *yt)
{
    double xc, yc, angle;
    
    xc = (llx + urx) / 2;
    yc = (lly + ury) / 2;    

    angle = M_PI * rotate / 180;
    *xt = x + scale * ( yc * sin (angle) - xc * cos (angle));
    *yt = y - scale * ( yc * cos (angle) + xc * sin (angle));    

    return (1);    
}

/* save EPS file into PS file for later use */
int eps_save (FILE *fp, char *epsf, char *name)
{
    char buf[1024];
    FILE *epsfp;

    if ((epsfp = fopen(epsf, "r")) == NULL) return (0);

    fprintf(fp, "\n/%s {\n", name);
    while (fgets(buf, 1024, epsfp) != NULL) fprintf(fp, "%s", buf);
    fprintf(fp, "} def\n");
    fclose(epsfp); 

    return (1);
}

/* draw EPS file saved by eps_save */
int eps_draw_saved (FILE *fp, char *name, double x, double y, double scale, double rotate)
{
    fprintf(PS.fp, "\nBeginEPSF\n");
    fprintf(PS.fp, "%.5f %.5f translate\n", x, y);    
    fprintf(PS.fp, "%.5f rotate\n", rotate);    
    fprintf(PS.fp, "%.5f %.5f scale\n", scale, scale);
    fprintf(PS.fp, "%%BeginDocument: %s\n", name);

    fprintf(PS.fp, "%s\n", name);

    fprintf(PS.fp, "%%EndDocument\n");
    fprintf(PS.fp, "EndEPSF\n"); 

    return (1);
}


/* write EPS file into PS file */
int eps_draw (FILE *fp, char *eps, double x, double y, double scale, double rotate)
{
    char buf[1024];
    FILE *epsfp;

    if ((epsfp = fopen(eps, "r")) == NULL) return (0);

    fprintf(PS.fp, "\nBeginEPSF\n");
    fprintf(PS.fp, "%.5f %.5f translate\n", x, y);    
    fprintf(PS.fp, "%.5f rotate\n", rotate);    
    fprintf(PS.fp, "%.5f %.5f scale\n", scale, scale);
    fprintf(PS.fp, "%%BeginDocument: %s\n", eps);

    while (fgets(buf, 1024, epsfp) != NULL) fprintf(fp, "%s", buf);

    fprintf(PS.fp, "%%EndDocument\n");
    fprintf(PS.fp, "EndEPSF\n"); 
    fclose(epsfp); 

    return (1);
}

/* save EPS patter file into PS file for later use */
/* For pattern we have to remove header comments */
int pat_save (FILE *fp, char *epsf, char *name)
{
    char buf[1024];
    FILE *epsfp;

    if ((epsfp = fopen(epsf, "r")) == NULL) return (0);

    fprintf(fp, "\n/%s {\n", name);
    while (fgets(buf, 1024, epsfp) != NULL) {
	if ( strncmp ( buf, "%!PS-Adobe", 10 ) == 0 ||
	     strncmp ( buf, "%%BoundingBox", 13 ) == 0 ) continue;
       	fprintf(fp, "%s", buf);
    }
    fprintf(fp, "} def\n");
    fclose(epsfp); 

    return (1);
}