[comp.lang.postscript] PS to EPS

msteen@westc.UUCP (Martien F. van Steenbergen) (10/17/88)

Is there a tool which converts ordinary PostScript to Encapsulated PostScript,
including the bit image in PostScript comment? Perhaps the PS interpreter does
the job, I don't konw. If it doesn't, is there someone out there who added this
feature to the PS interpreter?

I need this conversion in order to be able to "preview" EPS files in FrameMaker
and prehaps some other applications.

Thanks in advance,

	Martien.

scmi@mtuxo.att.com (02341-S.MITTAL) (04/27/89)

I would like to insert drawings generated by Macdraw into Ventura 
documents. I can save the Macdraw file as a PS file using the "cmd-F" key
combination while printing the file. However, Ventura only takes EPS files.
Is there a way I can convert PS to EPS (and vice versa). I would appreciate
any information on the subject.

Subhash Mittal
(201) 957-4687

samurai@cs.mcgill.ca (Darcy BROCKBANK) (06/21/91)

In article <1991Jun20.050319.22444@ux1.cso.uiuc.edu> J-Beauchamp@uiuc.edu writes:
>
>I would like to be able to paste individual graphs into WriteNow.  Is there a 
>way to accomplish this in general?  I suspect that I need the .eps format to 
>do this.  Is there a way to convert a .ps file into a .eps file?
>
>Thanks for your help!
>
>Jim Beauchamp  j-beauchamp@uiuc.edu
>    

Since several people have asked for this, I am posting a program (read hack)
that I wrote that will make some necessary adjustments. Note that this is a
hack, and may do some unnecessary stuff, and might miss on a few. However
I have found that the results always work out fine, and that is what is 
important ;-).

Usage: ps2eps [-v] <filename> 

takes <filename> as a '.ps' file and creates filename with '.eps'.

Will read and write from stdin/stdout, but only at the same time.

If you make any changes/improvements e-mail me a copy!

- db

Oh yeah. You might want to edit the %%BoundingBox: line on the created
eps file to customize the image size.

-----------------------




#include <stdio.h>
#include <string.h>
#include <signal.h>

/* EDIT ME !!!!!!!!!!!! */
#define DEFAULT_X 612.0
#define DEFAULT_Y 792.0

#define TRUE 1
#define FALSE 0

int verbose;
char rem[60];

/* This program was written by Darcy Brockbank, and is free to be */
/* copied, modified, or thrown in the trash as long as this notice*/
/* remains intact. THERE IS NO GUARANTEE as far as the functioning*/
/* and results of using this program are concerned. I tested it on*/
/* a Sun4 and a NeXT Cube and it worked fine. For the most part   */
/* this is a cheap hack that allows a nearly conforming PS file to*/
/* be transformed into an EPS file. It was intended to allow one  */
/* page graphics in PS to be converted into EPS so they could be  */
/* included in documents. If you have the urge to improve this, I */
/* encourage you to mail me the results at samurai@cs.mcgill,ca.  */



/* BUG!!! Do not read/write from stdin/stdout with the -v option  */
/* remarks will be placed into the output stream. THis is fixed   */
/* easily enough 1) make remarks PS comments. 2) send remarks to  */
/* stderr instead of stdout.					  */




/*-------------------------------------------------------------------*/

quit(code)
	int code;
{
	/*
	** This routine will be called when certain
	** signals are caught. Do the cleanup before
	** exiting.
	*/
	system(rem);
	fprintf(stderr,"\n>>> Caught a signal %d. Goodbye.\n",code);
	exit(-1);
}


jobs(input)
    char *input;
{
    FILE *outfile,*infile; 

    char buffer[256],newbuf[256],dfile[40],file[40];
    char *word1,*word2;
    int linecount,pagesOK,boundingboxOK,atend,fixed,EndComments;
    
    EndComments=boundingboxOK=pagesOK=FALSE;
    
    

    if (input!=NULL){
    	sprintf(file,"%s",input);
    	sprintf((file+strlen(file)-3),".eps");
    	sprintf(rem,"rm -f %s",file);
    	if (((infile=fopen(input,"r"))==NULL)) {
   	    	 fprintf(stderr,"Cannot open input file \"%s\" for reading.\n",input);
       		 system(rem);
       		 exit(1);
    	}
    	if (((outfile=fopen(file,"w"))==NULL)) {
		fprintf(stderr,"Cannot open output file \"%s\" for writing.\n",file);
		system(rem);
		exit(1);
	}
    }
    else {
	infile=stdin;
	outfile=stdout;
    }
    linecount=0;

        for(fgets(buffer,sizeof(buffer),infile);
		(!(feof(infile)));
		fgets(buffer,sizeof(buffer),infile)){
     		linecount++;
	    	if (buffer[0]=='%' && buffer[1]=='!'){
	        if (verbose){
	         	fprintf(stdout,"**\nRedefining program descriptor: %s   as: %%!PS-Adobe-2.0 ESPF-2.0\n",buffer);
		 }
		 sprintf(buffer,"%%!PS-Adobe-2.0 ESPF-2.0\n");
	     }
	     
	    if (buffer[0]=='%' && buffer[1]=='%'){
		strcpy(newbuf,buffer);
                word1=strtok(newbuf," :\n");
		word2=strtok('\0'," \n\t");
		if((word1!=NULL) && (strcmp(word1,"%%PageSize")!=0) && (strcmp(word1,"%%PageBoundingBox")!=0) && (strcmp(word1,"%%Feature")!=0)) {
		    if (strcmp(word1,"%%Page")==0){
           	         if (verbose){
			     fprintf(stdout,"**\n I think that there are multiple pages in the document.\n EPSF files may only have one page.\n I am doing my best to correct the problem, but it may be beyond my abilities.\n");
		             fprintf(stdout,"**\nStripping: %s   on line %d\n",buffer,linecount);
			 }
			 sprintf(buffer,"%%\n");
		    }
			
		    if (strcmp(word1,"%%BoundingBox")==0) {
		        boundingboxOK=TRUE; 
	         	if (verbose){
			    fprintf(stdout,"**\nFound a %%%%BoundingBox definition on line %d\n",linecount);
			}	
			if (strcmp(word2,"(atend)")==0){
			    atend=TRUE;
		            boundingboxOK=FALSE;
		            if (verbose){
				fprintf(stdout,"**\n%%%%BoundingBox values deferred to Trailer\n");
			    }
			}
		    }
		    if  (strcmp(word1,"%%Pages")==0){
		        pagesOK=TRUE;
		        if (verbose){
			    fprintf(stdout,"**\nRedefining: %s   as: %%%%Pages: 0 1  on line %d\n",buffer,linecount);
			}
			sprintf(buffer,"%%%%Pages: 0 1\n");
		    }
		    if  (strcmp(word1,"%%EndComments")==0){
		         if (verbose){
		            fprintf(stdout,"**\n Came to end of comments on line %d.\n",linecount);
			}
			EndComments=TRUE;
		    }
		 }else{
 	            if (verbose){
		        fprintf(stdout,"**\nStripping unneccesary definition: %s   on line %d.\n",buffer,linecount);
		    }
		    sprintf(buffer,"%%\n");
		 }
		 if (EndComments) {
		    if (!(pagesOK)) {
		         if (verbose){
			    fprintf(stdout,"**\nI did not find a valid %%%%Pages declaration.\nI am adding %%%%Pages 0 1 on line %d.",linecount);
                         } 
			 linecount++;
			 fprintf(outfile,"%%%%Pages: 0 1\n");
		    }
		    if (!(boundingboxOK) && !(atend)) {
		         if (verbose){
			     fprintf(stdout,"**\nI did not find a valid %%%%BoundingBox declaration.\nI am adding one on line %d.\n",linecount);
			     fprintf(stdout,"Note that there are four REAL number values following the  %%%%BoundingBox.\nYou may wish to edit these to change the size of your graphic window.\n");
			     fprintf(stdout,"I am defaulting to a %fx%f point window.\n",DEFAULT_X, DEFAULT_Y);
			 }
			 linecount++;
			 fprintf(outfile,"%%%%BoundingBox: 0 0 %f %f\n", DEFAULT_X, DEFAULT_Y);
			 boundingboxOK=TRUE;
		    }
		}
             }
	     else { 
	     	if ((!(EndComments))&& (!(fixed))&&(buffer[0]!='%')){
		    if (verbose){
			fprintf(stdout,"**\nThere was a badly formed header in the document that will not allow proper\nparsing of the EPS special comments.\nI am inserting a default header section at line %d.\nYou may wish to edit these lines.\n",linecount);
		    }
		    if (!(boundingboxOK) && !(atend)) {
			if (verbose){
			    fprintf(stdout,"Inserted %%%%BoundingBox: 0 0 %f %f\n", DEFAULT_X, DEFAULT_Y);
			}
			fprintf(outfile,"%%%%BoundingBox: 0 0 %f %f\n", DEFAULT_X, DEFAULT_Y);
			linecount++;
			boundingboxOK=TRUE;
			}
			if (!(pagesOK)) {
			    if (verbose){
				fprintf(stdout,"Inserted %%%%Pages: 0 1\n");
			    }
			    fprintf(outfile,"%%%%Pages: 0 1\n");
				linecount++;
				pagesOK=TRUE;
			    }
			    if (verbose){
			    	fprintf(stdout,"Inserted %%%%EndComments\n");
			    }
			    fprintf(outfile,"%%%%EndComments\n");
			    EndComments=TRUE;
			    fixed=TRUE;
		        }
		    } 
	            fprintf(outfile,"%s",buffer);
	    } 
	    if (!(boundingboxOK)) {
		    if (verbose){
			fprintf(stdout,"**\nI did not find a valid %%%%BoundingBox declaration.\nI am adding one on line %d.\n",linecount);
			fprintf(stdout,"Note that there are four REAL number values following the %%%%BoundingBox.\nYou may wish to edit these to change the size of your graphic window.\n");
			fprintf(stdout,"I am defaulting to a %fx%f point window.\n", DEFAULT_X, DEFAULT_Y);
       		    }
		    linecount++;
		    fprintf(outfile,"%%%%Trailer\n%%%%BoundingBox: 0 0 %f %f\n", DEFAULT_X, DEFAULT_Y);
	    }
	    fclose(infile);
	    fclose(outfile);
}
		    
/*------------------------------------------------------------------------_*/

main(argc,argv)
    char *argv[];
    int argc;
{
    char c,*optarg;
    int optind,error;
    /*
    ** Intercept termination signals.
    */
    signal(SIGINT, quit);
    signal(SIGTERM, quit);
    signal(SIGQUIT, quit);
    signal(SIGBUS, quit);
    signal(SIGSEGV, quit); 
    
        verbose=FALSE; error=FALSE;

        while ((c = getopt(argc, argv, "v")) != EOF)
        switch (c) {
          case 'v': 
		verbose=TRUE;
                optind++;
                break;
          case '?':
          default:
                error=TRUE;
                break;
     }
     if (verbose){
     	fprintf(stdout,"**\nWelcome to ps2eps 1.1.\nUse this program to convert PS files into portable EPS files.\nThe converted file has the same name, but the EPS extension.\nSend comments to samurai@cs.mcgill.ca.\n");
     }
     if (error || argc>optind+2 ) {
          fprintf(stderr, "Usage: ps2eps [-v(erbose)] <infile.ps>\n");
          exit(2);
     }
     if (argc==1+optind) {
     	jobs(NULL);
     }
     else {
     	jobs(argv[argc-1]);
     }
}