00001
00002
00003 #ifdef __STDC__
00004 # include <stdlib.h>
00005 # include <stdarg.h>
00006 #else
00007 # include <varargs.h>
00008 # include <stdlib.h>
00009 char *getenv();
00010 #endif
00011
00012 #include <string.h>
00013 #include <ctype.h>
00014 #include <stdio.h>
00015
00016 #include "../../modules/core/includes/machine.h"
00017 #include "util.h"
00018 #define Min(x,y) (((x)<(y))?(x):(y))
00019
00020 static int Sed (int,char *,FILE *,char *,char *,char *,char *,char *,char *);
00021 int ScilabPsToEps (char orientation,char *filein,char *fileout);
00022 static void dos2win32 (char *filename,char *filename1);
00023 static void ConvertName (char *filein,char *fileout);
00024 static void get_dims(char *file, int *w, int *h);
00025
00026
00027
00028
00029
00030
00031
00032 char * UsageStr[]={
00033 "\tfilename.ps : Postscript file produced by Scilab\n",
00034 "\torientation : p[ortrait] or l[andscape] \n",
00035 "fin"};
00036
00037 #define PATH_MAX 1024
00038
00039 static char file1[PATH_MAX],file2[PATH_MAX];
00040
00041 int main(int argc,char *argv[])
00042 {
00043 char orientation='p';
00044 if (argc >= 4 || argc <= 1)
00045 { int i=0;
00046 fprintf(stderr,"Usage : %s [-orientation] filename.ps \n",argv[0]);
00047 while (strcmp(UsageStr[i],"fin")!=0)
00048 fprintf(stderr,"%s",UsageStr[i]),i++;
00049 return(1);
00050 }
00051 switch ( argc )
00052 {
00053 case 2:
00054 if ( strncmp( argv[1] ,"-p",2) == 0 || strncmp( argv[1] ,"-l",2) == 0)
00055 {
00056 fprintf(stderr,"filename argument missing\n");
00057 exit(1);
00058 }
00059 orientation = 'p';
00060 strcpy(file1,argv[1]);
00061 break;
00062 case 3:
00063 strcpy(file1,argv[2]);
00064 if ( strncmp( argv[1] ,"-p",2) == 0)
00065 orientation = 'p';
00066 else if ( strncmp( argv[1] ,"-l",2) == 0)
00067 orientation = 'l';
00068 else
00069 fprintf(stderr,"Invalid first argument %s\n",argv[1]);
00070 break;
00071 }
00073 dos2win32(file1,file2);
00075 ScilabPsToEps(orientation,file2,file1);
00076 return(0);
00077 }
00078
00079 #ifdef __STDC__
00080
00081 void sciprint(char *fmt, ...)
00082 {
00083 va_list args;
00084 va_start(args,fmt);
00085 vfprintf(stdout, fmt, args );
00086 va_end(args);
00087 }
00088 #else
00089
00090
00091 void sciprint(va_alist) va_dcl
00092 {
00093 va_list ap;
00094 char *format;
00095 va_start(ap);
00096 format = va_arg(ap, char *);
00097 (void) vfprintf(stdout, format, ap );
00098 va_end(ap);
00099 }
00100 #endif
00101
00102 static void dos2win32(char *filename,char *filename1)
00103 {
00104 #ifdef CVT_PATH_BEG
00105 if ( filename[1] == ':' )
00106 {
00107 *filename1++ = '/';
00108 *filename1++ = '/';
00109 *filename1++ = *filename++;
00110 filename++;
00111 }
00112 #endif
00113 while ( *filename != '\0' )
00114 {
00115 *filename1++ = *filename++;
00116 if ( *(filename1-1) == '\\' ) *(filename1-1) = '/' ;
00117 }
00118 *filename1 = '\0';
00119 }
00120
00124
00125
00126
00127
00128
00129 static char entete[PATH_MAX];
00130
00131 int ScilabPsToEps(char orientation,char *filein,char *fileout)
00132 {
00133
00134 double ccm = 28.346457;
00135 double wp = ccm*210;
00136 double hp = ccm*297;
00137 double ws,hs,sc,marg= ccm*5;
00138
00139 int flag = 0,rep, w=0,h=0;
00140 FILE *fo;
00141 char *env;
00142 env = getenv("SCI");
00143 if (env == NULL)
00144 {
00145 sciprint("Environment variable SCI must be defined\r\n");
00146 return(1);
00147 }
00148 sprintf(entete,"%s/tools/printer/NperiPos.ps",env);
00149
00150 ConvertName(filein,fileout);
00151 if ( strcmp(fileout,filein) == 0 )
00152 {
00153 flag = 1;
00154 strcat(fileout,".temp");
00155 }
00156
00157 if ( (fo = fopen(fileout,"w"))== 0 )
00158 {
00159 sciprint(" Can't open file %s\r\n",fileout);
00160 exit(1);
00161 }
00162
00163
00164 get_dims(filein,&w,&h);
00165
00166 if ( w == 0 || h == 0 )
00167 {
00168 w= 6000; h = 4240;
00169 }
00170
00171
00172 fprintf(fo,"%%!PS-Adobe-2.0 EPSF-2.0\n");
00173 if ( orientation == 'p' )
00174 {
00175 ws = (wp-2*marg)/((double) w);
00176 hs = (hp-2*marg)/((double) h);
00177 sc = Min(ws,hs);
00178 ws = w*sc;
00179 hs = h*sc;
00180 fprintf(fo,"%%%%BoundingBox: %d %d %d %d \n",
00181 (int) ((wp - ws)/(2*10.0)),
00182 (int) ((hp - hs)/(2*10.0)),
00183 (int) (ws/10.0 + (wp - ws)/(2*10.0)),
00184 (int) (hs/10.0 + (hp - hs)/(2*10.0))
00185 );
00186 }
00187 else
00188 {
00189 ws = (hp-2*marg)/((double) w);
00190 hs = (wp-2*marg)/((double) h);
00191 sc = Min(ws,hs);
00192 ws = w*sc;
00193 hs = h*sc;
00194 fprintf(fo,"%%%%BoundingBox: %d %d %d %d \n",
00195 (int) ((wp - hs)/(2*10.0)),
00196 (int) ((hp - ws)/(2*10.0)),
00197 (int) (hs/10.0 + (wp - hs)/(2*10.0)) ,
00198 (int) (ws/10.0 + (hp - ws)/(2*10.0))
00199 );
00200
00201 }
00202
00203 Sed(0,entete,fo,"%!PS-ADOBE","%%",(char*) 0,(char *)0,(char*) 0,(char *)0);
00204
00205 if ( orientation == 'p' )
00206 {
00207 char cc[512];
00208 sprintf(cc,"[%f 20 div 0 0 %f 20 div neg %d 10 div %d 10 div] concat",
00209 sc,sc,(int) ((wp - ws)/(2)), (int) ((hp - hs)/(2) + hs));
00210 rep=Sed(1,filein,fo,"[0.5 10 div 0 0 0.5 10 div neg",
00211 cc,
00212 (char*) 0,(char *)0,(char*) 0,(char *)0);
00213 }
00214 else
00215 {
00216 char cc[512];
00217 sprintf(cc,"90 rotate 0 neg %d neg 10 div translate\n[%f 20 div 0 0 %f 20 div neg %d 10 div %d 10 div] concat",
00218 h + (int) ((wp - hs)/(2.0)) , sc,sc,(int) ((hp - ws)/2), h );
00219 rep=Sed(1,filein,fo,"[0.5 10 div 0 0 0.5 10 div neg",
00220 cc,
00221 (char*) 0,(char *)0,(char*) 0,(char *)0);
00222 }
00223
00224 fclose(fo);
00225
00226 if ( rep >= 1 )
00227 {
00228 if ( rep == 1)
00229 sciprint("input file doesn't need to be changed to epsf \r\n");
00230 remove(fileout);
00231 exit(0);
00232 }
00233
00234 if ( flag == 1)
00235 {
00237 fo = fopen(filein,"w");
00238 Sed(0,fileout,fo,(char*) 0,(char *)0,(char*) 0,(char *)0,(char*) 0,(char *)0);
00239 fclose(fo);
00240 }
00241 else
00242 {
00243 remove(filein);
00244 }
00245 return(0);
00246 }
00247
00248
00249 static void ConvertName(char *filein,char *fileout)
00250 {
00251 char *p=filein,*p1;
00252 p1= strrchr(p,'/');
00253 p = strrchr(p,'.');
00254
00255 if ( p != 0 )
00256 {
00257 *p = '\0';
00258 sprintf(fileout,"%s.eps",filein);
00259 *p = '.';
00260 }
00261 else
00262 sprintf(fileout,"%s.eps",filein);
00264 }
00265
00266
00267
00268
00269
00270
00271
00272 int Sed(int flag, char *file, FILE *fileo, char *strin1, char *strout1,
00273 char *strin2, char *strout2, char *strin3, char *strout3)
00274 {
00275 FILE *fd;
00276 static char *buff = NULL;
00277 static int buflen = 512;
00278 if ( buff == NULL)
00279 {
00280 buff = malloc(buflen*sizeof(char));
00281 if ( buff == NULL)
00282 {
00283 fprintf(stderr,"Running out of space \n");
00284 exit(1);
00285 }
00286 }
00287
00288 fd=fopen(file,"r");
00289 if (fd != 0)
00290 { int stop=0;
00291 while ( stop != 1)
00292 {
00293
00294 read_one_line (&buff,&stop,fd,&buflen);
00295 if ( flag == 1 )
00296 {
00297 if ( strncmp(buff,"%!PS-Adobe-2.0 EPSF-2.0",
00298 strlen("%!PS-Adobe-2.0 EPSF-2.0"))== 0)
00299 {
00300 fclose(fd);
00301 return(1);
00302 }
00303 }
00304 if ( strin1 != (char *) 0 && strncmp(buff,strin1,strlen(strin1))==0)
00305 fprintf(fileo,"%s\n",strout1);
00306 else
00307 {
00308 if ( strin2 != (char *) 0 && strncmp(buff,strin2,strlen(strin2))==0)
00309 fprintf(fileo,"%s\n",strout2);
00310 else
00311 {
00312 if ( strin3 != (char *) 0 && strncmp(buff,strin3,strlen(strin3))==0)
00313 fprintf(fileo,"%s\n",strout3);
00314 else
00315 fprintf(fileo,"%s",buff);
00316 }
00317 }
00318 }
00319 fclose(fd);
00320 }
00321 else
00322 {
00323 sciprint("file %s not found \r\n",file);
00324 return(2);
00325 }
00326 return(0);
00327 }
00328
00329
00330
00331
00332
00333
00334 static void get_dims(char *file, int *w, int *h)
00335 {
00336 FILE *fd;
00337 static char scipos_w[] = "%scipos_w=";
00338 static char scipos_h[] = "%scipos_h=";
00339 static char stop_s[] = "% Dessin en bas a gauche";
00340
00341 static char *buff = NULL;
00342 static int buflen = 512;
00343 if ( buff == NULL)
00344 {
00345 buff = malloc(buflen*sizeof(char));
00346 if ( buff == NULL)
00347 {
00348 fprintf(stderr,"Running out of space \n");
00349 exit(1);
00350 }
00351 }
00352
00353 fd=fopen(file,"r");
00354 if (fd != 0)
00355 {
00356 int stop=0;
00357 while ( stop != 1)
00358 {
00359 read_one_line (&buff,&stop,fd,&buflen);
00360 if ( strncmp(buff,scipos_w,strlen(scipos_w))==0)
00361 {
00362 sscanf(buff+strlen(scipos_w),"%d",w);
00363 }
00364 if ( strncmp(buff,scipos_h,strlen(scipos_h))==0)
00365 {
00366 sscanf(buff+strlen(scipos_h),"%d",h);
00367 }
00368 if ( strncmp(buff,stop_s,strlen(stop_s))==0 )
00369 break;
00370 }
00371 fclose(fd);
00372 }
00373 }
00374