00001 #include <stdio.h>
00002 #include <time.h>
00003 #include <math.h>
00004 #include <string.h>
00005
00006
00007
00008
00009
00010
00011
00012
00013 void ps_move( int , int ) ;
00014 void ps_line( int , int , int , int ) ;
00015 void ps_cont( int , int ) ;
00016 void ps_point( int , int ) ;
00017 void ps_label( char * ) ;
00018 void ps_arc( int , int , int , int , int , int ) ;
00019 void ps_circle( int , int , int ) ;
00020 void ps_erase( void ) ;
00021 void ps_linemod( char * ) ;
00022 void ps_space( int , int , int , int ) ;
00023 int ps_openpl( char * ) ;
00024 void ps_closepl( void ) ;
00025 void ps_setrgb( float , float , float ) ;
00026 void ps_setwidth( float ) ;
00027 void ps_rect( int,int,int,int ) ;
00028
00029
00030
00031 void zzpsco_( float *, float *, float *) ;
00032 void zzpsop_( char *cfl , int ncfl ) ;
00033 void zzpsli_( int *, int *, int *, int *) ;
00034 void zzpsfr_( void ) ;
00035 void zzpscl_( void ) ;
00036
00037
00038
00039 void ps_maybe_stroke( void ) ;
00040 void ps_stroke( void ) ;
00041 int ps_setfont( void ) ;
00042 void ps_prolog( void ) ;
00043 void ps_epilog( void ) ;
00044 void ps_clear( void ) ;
00045
00046
00047
00048 static int font;
00049 static int error;
00050 static int npages=0;
00051 static int cx,cy;
00052 static int ttcur=0;
00053 static int atcur=0;
00054 static int inpath=0;
00055 static int plot=0;
00056 static double scal=1.0;
00057 static int prolog_not_output = 1 ;
00058 static FILE *psfile = NULL ;
00059 static int psfile_ispipe = 0 ;
00060
00061
00062
00063 void ps_maybe_stroke( void )
00064 { if (inpath>100) ps_stroke(); }
00065
00066 void ps_stroke( void )
00067 { fprintf( psfile , "S\n") ; atcur=inpath=0 ; }
00068
00069 void ps_move( int ix , int iy )
00070 {
00071 if( atcur && cx == ix && cy == iy ) return ;
00072 cx = ix ;
00073 cy = iy ;
00074 ttcur=atcur=0;
00075 }
00076
00077 void ps_line( int ix1 , int iy1 , int ix2 , int iy2 )
00078 { ps_move( ix1 , iy1 ) ;
00079 ps_cont( ix2 , iy2 ) ;
00080 }
00081
00082 void zzpsli_( int *ix1 , int *iy1 , int *ix2 , int *iy2 )
00083 { ps_line( *ix1,*iy1 , *ix2,*iy2 ) ; }
00084
00085 void ps_cont( int ix , int iy )
00086 { if (!inpath) fprintf( psfile , "NP ");
00087 if (!atcur) fprintf( psfile , "%d %d M\n",cx,cy);
00088 ps_move( ix , iy ) ;
00089 fprintf( psfile , "%d %d N\n",cx,cy);
00090 ttcur=0;
00091 atcur=plot=1;
00092 inpath++;
00093 ps_maybe_stroke() ;
00094 }
00095
00096 void ps_rect( int x1,int y1 , int x2,int y2 )
00097 {
00098 if( inpath ) ps_stroke() ;
00099 fprintf( psfile , "NP ");
00100 fprintf( psfile , "%d %d M ",x1,y1);
00101 fprintf( psfile , "%d %d N ",x2,y1);
00102 fprintf( psfile , "%d %d N ",x2,y2);
00103 fprintf( psfile , "%d %d N ",x1,y2);
00104 #if 0
00105 fprintf( psfile , "%d %d N ",x1,y1);
00106 #endif
00107 fprintf( psfile , "F S\n") ;
00108 }
00109
00110 void ps_point( int ix , int iy )
00111 { if (inpath) ps_stroke() ;
00112 ps_move( ix , iy ) ;
00113 fprintf( psfile , "%d %d %c\n",cx,cy,'P');
00114 ttcur=atcur=inpath=0;
00115 plot=1;
00116 }
00117
00118 void ps_label( char * s )
00119 { int is ;
00120 char c ;
00121
00122 if (inpath) ps_stroke() ;
00123 if (!ttcur) fprintf( psfile , "%d %d M\n",cx,cy);
00124 if (!font) font=ps_setfont();
00125 fprintf( psfile , "(");
00126 for( is=0,c=s[is] ; (c!='\0')&&(c!='\n') ; ++is,c=s[is] )
00127 {
00128 if (c=='(' || c==')' || c=='\\')putchar('\\');
00129 putchar(c);
00130 }
00131 fprintf( psfile , ") T\n");
00132 ttcur=plot=1;
00133 atcur=inpath=0;
00134 }
00135
00136 #ifndef M_PI
00137 #define M_PI 3.14159265358979323846
00138 #endif
00139
00140 void ps_arc( int x , int y , int x1 , int y1 , int x2 , int y2 )
00141 { double dx , dy ;
00142
00143 if (inpath) ps_stroke() ;
00144 dx=x1-x;
00145 dy=y1-y;
00146 fprintf( psfile , "%d %d %f ", x, y, sqrt(dx*dx+dy*dy) );
00147 fprintf( psfile , "%f ", (double)(atan2(dy,dx)/M_PI)*180.0 );
00148 dx=x2-x;
00149 dy=y2-y;
00150 fprintf( psfile , "%f ", (double)(atan2(dy,dx)/M_PI)*180.0 );
00151 plot=1;
00152 atcur=inpath=0;
00153 }
00154
00155 void ps_circle( int x , int y , int r )
00156 { fprintf( psfile , "%d %d %d C\n",x,y,r);
00157 plot=1;
00158 }
00159
00160 void ps_erase( void )
00161 { ps_clear() ; }
00162
00163 void zzpsfr_( void )
00164 { ps_erase() ; }
00165
00166 void ps_linemod( char * s)
00167 { double pt ;
00168 pt = 1.0 / scal ;
00169
00170 if (inpath) ps_stroke() ;
00171
00172 if (strncmp(s,"solid",5) == 0) {
00173 fprintf( psfile , "[] 0 setdash\n") ;
00174 } else if( strncmp(s,"dotted",6) == 0 ) {
00175 fprintf( psfile , "[ %f %f ] 0 setdash\n" , 2.0*pt , 3.0*pt ) ;
00176 } else if( strncmp(s,"dotdashed",9) == 0 ) {
00177 fprintf( psfile , "[ %f %f %f %f ] 0 setdash\n" ,
00178 2.0*pt , 3.0*pt , 6.0*pt , 3.0*pt ) ;
00179 } else if( strncmp(s,"shortdashed",11) == 0 ) {
00180 fprintf( psfile , "[ %f %f ] 0 setdash\n" , 6.0*pt , 3.0*pt ) ;
00181 } else if( strncmp(s,"longdashed",10) == 0 ) {
00182 fprintf( psfile , "[ %f %f ] 0 setdash\n" , 9.0*pt , 4.5*pt ) ;
00183 } else {
00184 fprintf(stderr,
00185 "plotps: linestyle '%s' not implemented.\n",s);
00186 fprintf( psfile , "[] 0 setdash\n") ;
00187 }
00188 }
00189
00190 void ps_space( int ix1 , int iy1 , int ix2 , int iy2 )
00191 { if( prolog_not_output ) ps_prolog() ;
00192 if (inpath) ps_stroke() ;
00193 fprintf( psfile , "initgraphics\n");
00194 fprintf( psfile , "1 setlinewidth\n");
00195 fprintf( psfile , "66 72 translate\n");
00196 scal=480.0/(ix2-ix1);
00197 fprintf( psfile , "%f %f scale\n",scal,480.0/(iy2-iy1));
00198 if (ix1 || iy1) fprintf( psfile , "%d %d translate\n",-ix1, -iy1);
00199 ps_linemod( "solid" ) ;
00200 atcur=inpath=font=0;
00201 }
00202
00203 void ps_setwidth( float www )
00204 { if( inpath ) ps_stroke() ;
00205 fprintf( psfile , "%f setlinewidth\n" , www ) ;
00206 }
00207
00208 void ps_setrgb( float rrr , float ggg , float bbb )
00209 { if( inpath ) ps_stroke() ;
00210 fprintf( psfile , "%f %f %f setrgbcolor\n" , rrr,ggg,bbb ) ;
00211 }
00212
00213 void zzpsco_( float *rrr , float *ggg , float *bbb )
00214 { ps_setrgb( *rrr,*ggg,*bbb ) ; }
00215
00216 int ps_setfont( void )
00217 {
00218 fprintf( psfile , "%f SF\n",12.0/scal);
00219 return 1;
00220 }
00221
00222 int ps_openpl( char *fname )
00223 {
00224 if( strcmp(fname,"-") == 0 ){
00225 psfile = stdout ;
00226 psfile_ispipe = 0 ;
00227 } else if( fname[0] != '|' ){
00228 psfile = fopen( fname , "w" ) ;
00229 psfile_ispipe = 0 ;
00230 } else {
00231 psfile = popen( fname+1 , "w" ) ;
00232 psfile_ispipe = 1 ;
00233 }
00234 if( psfile == NULL ) return 0 ;
00235 ps_prolog();
00236 return 1 ;
00237 }
00238
00239 void zzpsop_( char *cfl , int ncfl )
00240 { int i ;
00241 char ccc[128] ;
00242
00243 for( i=0 ; (i < 127) && (i < ncfl) && (cfl[i] != ' ') ; i++ ) {
00244 ccc[i] = cfl[i] ;
00245 }
00246 ccc[i] = '\0' ;
00247 ps_openpl( ccc ) ; if( psfile == NULL ) return ;
00248 ps_space( 0,0,4096,4096 ) ;
00249 }
00250
00251 void ps_closepl( void )
00252 { ps_epilog();
00253
00254 if( psfile == stdout ){
00255 fflush(psfile) ;
00256 } else {
00257 if( ! psfile_ispipe ) fclose(psfile) ;
00258 else pclose(psfile) ;
00259 }
00260
00261 psfile = NULL ; psfile_ispipe = 0 ;
00262 }
00263
00264 static char *prolog_text[] = {
00265 "%%BoundingBox: 36 36 540 690",
00266 "%%Title: plotps output",
00267 "%%Creator: plotps 1.0 (RWCox)",
00268 "%%Pages: (atend)",
00269 "%%DocumentFonts: Times-Roman",
00270 "%%EndComments",
00271 "/S{stroke}bind def",
00272 "/F{fill}bind def" ,
00273 "/NP{newpath}bind def",
00274 "/M{moveto}bind def",
00275 "/N{lineto}bind def",
00276 "/A{NP arc S}bind def",
00277 "/C{0 360 A}bind def",
00278 "/P{1 0 360 A}bind def",
00279 "/T{show}bind def",
00280 "/CL{showpage}bind def",
00281 "/SF{/Times-Roman findfont exch scalefont setfont}bind def",
00282 "%%EndProlog",
00283 NULL
00284 };
00285
00286 void ps_prolog( void )
00287 {
00288 time_t tt = time(NULL);
00289 char **p;
00290
00291 fprintf( psfile , "%%!PS-Adobe-2.0 EPSF-2.0\n%%%%CreationDate: %s", ctime(&tt));
00292 for (p = prolog_text; *p; p++)
00293 fprintf(psfile,"%s\n" , *p);
00294 font=0;
00295 prolog_not_output = 0 ;
00296 }
00297
00298 void ps_clear( void )
00299 { if (inpath) ps_stroke() ;
00300 if (plot)
00301 {
00302 fprintf( psfile , "CL\n");
00303 npages++;
00304 atcur=inpath=ttcur=plot=0;
00305 }
00306 }
00307
00308 #undef USE_EOT
00309
00310 void ps_epilog( void )
00311 { ps_clear();
00312 #ifdef USE_EOT
00313 fprintf( psfile , "%%%%Trailer\n%%%%Pages: %d\n%c", npages,4);
00314 #else
00315 fprintf( psfile , "%%%%Trailer\n%%%%Pages: %d\n", npages);
00316 #endif
00317 }
00318
00319 void zzpscl_( void )
00320 { ps_closepl() ; }