1199 lines · 26.2 KB · C
RawDownload
/* * * posttek - PostScript translator for tektronix 4014 files * * A program that can be used to translate tektronix 4014 files into PostScript. * Most of the code was borrowed from the tektronix 4014 emulator that was written * for DMDs. Things have been cleaned up some, but there's still plently that * could be done. * * The PostScript prologue is copied from *prologue before any of the input files * are translated. The program expects that the following PostScript procedures * are defined in that file: * *	setup * *	  mark ... setup - * *	    Handles special initialization stuff that depends on how the program *	    was called. Expects to find a mark followed by key/value pairs on the *	    stack. The def operator is applied to each pair up to the mark, then *	    the default state is set up. * *	pagesetup * *	  page pagesetup - * *	    Does whatever is needed to set things up for the next page. Expects *	    to find the current page number on the stack. * *	v * *	  mark dx1 dy1 ... dxn dyn x y v mark * *	    Draws the vector described by the numbers on the stack. The top two *	    numbers are the starting point. The rest are relative displacements *	    from the preceeding point. Must make sure we don't put too much on *	    the stack! * *	t * *	  x y string t - * *	    Prints the string that's on the top of the stack starting at point *	    (x, y). * *	p * *	  x y p - * *	    Marks the point (x, y) with a circle whose radius varies with the *	    current intensity setting. * *	i * *	  percent focus i - * *	    Changes the size of the circle used to mark individual points to *	    percent of maximum for focused mode (focus=1) or defocused mode *	    (focus=0). The implementation leaves much to be desired! * *	l * *	  mark array l mark * *	    Set the line drawing mode according to the description given in array. *	    The arrays that describe the different line styles are declared in *	    STYLES (file posttek.h). The array really belongs in the prologue! * *	w * *	  n w - * *	    Adjusts the line width for vector drawing. Used to select normal (n=0) *	    or defocused (n=1) mode. * *	f * *	  size f - * *	    Changes the size of the font that's used to print characters in alpha *	    mode. size is the tektronix character width and is used to choose an *	    appropriate point size in the current font. * *	done * *	  done * *	    Makes sure the last page is printed. Only needed when we're printing *	    more than one page on each sheet of paper. * * The default line width is zero, which forces lines to be one pixel wide. That * works well on 'write to black' engines but won't be right for 'write to white' * engines. The line width can be changed using the -w option, or you can change * the initialization of linewidth in the prologue. * * Many default values, like the magnification and orientation, are defined in  * the prologue, which is where they belong. If they're changed (by options), an * appropriate definition is made after the prologue is added to the output file. * The -P option passes arbitrary PostScript through to the output file. Among * other things it can be used to set (or change) values that can't be accessed by * other options. * */ #include <stdio.h>#include <signal.h>#include <sys/types.h>#include <fcntl.h>  #include "comments.h"			/* PostScript file structuring comments */#include "gen.h"			/* general purpose definitions */#include "path.h"			/* for the prologue */#include "ext.h"			/* external variable definitions */#include "posttek.h"			/* control codes and other definitions */ char	*optnames = "a:c:f:m:n:o:p:w:x:y:A:C:E:J:L:P:R:DI"; char	*prologue = POSTTEK;		/* default PostScript prologue */char	*formfile = FORMFILE;		/* stuff for multiple pages per sheet */ int	formsperpage = 1;		/* page images on each piece of paper */int	copies = 1;			/* and this many copies of each sheet */ int	charheight[] = CHARHEIGHT;	/* height */int	charwidth[] = CHARWIDTH;	/* and width arrays for tek characters */int	tekfont = TEKFONT;		/* index into charheight[] and charwidth[] */ char	intensity[] = INTENSITY;	/* special point intensity array */char	*styles[] = STYLES;		/* description of line styles */int	linestyle = 0;			/* index into styles[] */int	linetype = 0;			/* 0 for normal, 1 for defocused */ int	dispmode = ALPHA;		/* current tektronix state */int	points = 0;			/* points making up the current vector */int	characters = 0;			/* characters waiting to be printed */int	pen = UP;			/* just for point plotting */int	margin = 0;			/* left edge - ALPHA state */ Point	cursor;				/* should be current cursor position */ Fontmap	fontmap[] = FONTMAP;		/* for translating font names */char	*fontname = "Courier";		/* use this PostScript font */ int	page = 0;			/* page we're working on */int	printed = 0;			/* printed this many pages */ FILE	*fp_in;				/* read from this file */FILE	*fp_out = stdout;		/* and write stuff here */FILE	*fp_acct = NULL;		/* for accounting data */ /*****************************************************************************/ main(agc, agv)     int		agc;    char	*agv[]; { /* * * A simple program that can be used to translate tektronix 4014 files into * PostScript. Most of the code was taken from the DMD tektronix 4014 emulator, * although things have been cleaned up some. * */     argv = agv;				/* so everyone can use them */    argc = agc;     prog_name = argv[0];		/* just for error messages */     init_signals();			/* sets up interrupt handling */    header();				/* PostScript header comments */    options();				/* handle the command line options */    setup();				/* for PostScript */    arguments();			/* followed by each input file */    done();				/* print the last page etc. */    account();				/* job accounting data */     exit(x_stat);			/* nothing could be wrong */ }   /* End of main */ /*****************************************************************************/ init_signals() { /* * * Make sure we handle interrupts. * */     if ( signal(SIGINT, interrupt) == SIG_IGN )  {	signal(SIGINT, SIG_IGN);	signal(SIGQUIT, SIG_IGN);	signal(SIGHUP, SIG_IGN);    } else {	signal(SIGHUP, interrupt);	signal(SIGQUIT, interrupt);    }   /* End else */     signal(SIGTERM, interrupt); }   /* End of init_signals */ /*****************************************************************************/ header() {     int		ch;			/* return value from getopt() */    int		old_optind = optind;	/* for restoring optind - should be 1 */ /* * * Scans the option list looking for things, like the prologue file, that we need * right away but could be changed from the default. Doing things this way is an * attempt to conform to Adobe's latest file structuring conventions. In particular * they now say there should be nothing executed in the prologue, and they have * added two new comments that delimit global initialization calls. Once we know * where things really are we write out the job header, follow it by the prologue, * and then add the ENDPROLOG and BEGINSETUP comments. * */     while ( (ch = getopt(argc, argv, optnames)) != EOF )	if ( ch == 'L' )	    prologue = optarg;	else if ( ch == '?' )	    error(FATAL, "");     optind = old_optind;		/* get ready for option scanning */     fprintf(stdout, "%s", CONFORMING);    fprintf(stdout, "%s %s\n", VERSION, PROGRAMVERSION);    fprintf(stdout, "%s %s\n", DOCUMENTFONTS, ATEND);    fprintf(stdout, "%s %s\n", PAGES, ATEND);    fprintf(stdout, "%s", ENDCOMMENTS);     if ( cat(prologue) == FALSE )	error(FATAL, "can't read %s", prologue);     fprintf(stdout, "%s", ENDPROLOG);    fprintf(stdout, "%s", BEGINSETUP);    fprintf(stdout, "mark\n"); }   /* End of header */ /*****************************************************************************/ options() {     int		ch;			/* value returned by getopt() */ /* * * Reads and processes the command line options. Added the -P option so arbitrary * PostScript code can be passed through. Expect it could be useful for changing * definitions in the prologue for which options have not been defined. * */     while ( (ch = getopt(argc, argv, optnames)) != EOF )  {	switch ( ch )  {	    case 'a':			/* aspect ratio */		    fprintf(stdout, "/aspectratio %s def\n", optarg);		    break; 	    case 'c':			/* copies */		    copies = atoi(optarg);		    fprintf(stdout, "/#copies %s store\n", optarg);		    break; 	    case 'f':			/* use this PostScript font */		    fontname = get_font(optarg);		    fprintf(stdout, "/font /%s def\n", fontname);		    break; 	    case 'm':			/* magnification */		    fprintf(stdout, "/magnification %s def\n", optarg);		    break; 	    case 'n':			/* forms per page */		    formsperpage = atoi(optarg);		    fprintf(stdout, "%s %s\n", FORMSPERPAGE, optarg);		    fprintf(stdout, "/formsperpage %s def\n", optarg);		    break; 	    case 'o':			/* output page list */		    out_list(optarg);		    break; 	    case 'p':			/* landscape or portrait mode */		    if ( *optarg == 'l' )			fprintf(stdout, "/landscape true def\n");		    else fprintf(stdout, "/landscape false def\n");		    break; 	    case 'w':			/* line width */		    fprintf(stdout, "/linewidth %s def\n", optarg);		    break; 	    case 'x':			/* shift horizontally */		    fprintf(stdout, "/xoffset %s def\n", optarg);		    break; 	    case 'y':			/* and vertically on the page */		    fprintf(stdout, "/yoffset %s def\n", optarg);		    break; 	    case 'A':			/* force job accounting */	    case 'J':		    if ( (fp_acct = fopen(optarg, "a")) == NULL )			error(FATAL, "can't open accounting file %s", optarg);		    break; 	    case 'C':			/* copy file straight to output */		    if ( cat(optarg) == FALSE )			error(FATAL, "can't read %s", optarg);		    break; 	    case 'E':			/* text font encoding */		    fontencoding = optarg;		    break; 	    case 'L':			/* PostScript prologue file */		    prologue = optarg;		    break; 	    case 'P':			/* PostScript pass through */		    fprintf(stdout, "%s\n", optarg);		    break; 	    case 'R':			/* special global or page level request */		    saverequest(optarg);		    break; 	    case 'D':			/* debug flag */		    debug = ON;		    break; 	    case 'I':			/* ignore FATAL errors */		    ignore = ON;		    break; 	    case '?':			/* don't know the option */		    error(FATAL, "");		    break; 	    default:			/* don't know what to do for ch */		    error(FATAL, "missing case for option %c", ch);		    break;	}   /* End switch */    }	/* End while */     argc -= optind;    argv += optind; }   /* End of options */ /*****************************************************************************/ char *get_font(name)     char	*name;			/* name the user asked for */ {     int		i;			/* for looking through fontmap[] */ /* * * Called from options() to map a user's font name into a legal PostScript name. * If the lookup fails *name is returned to the caller. That should let you choose * any PostScript font. * */     for ( i = 0; fontmap[i].name != NULL; i++ )	if ( strcmp(name, fontmap[i].name) == 0 )	    return(fontmap[i].val);     return(name); }   /* End of get_font */ /*****************************************************************************/ setup() { /* * * Handles things that must be done after the options are read but before the * input files are processed. * */     writerequest(0, stdout);		/* global requests eg. manual feed */    setencoding(fontencoding);    fprintf(stdout, "setup\n");     if ( formsperpage > 1 )  {	if ( cat(formfile) == FALSE )	    error(FATAL, "can't read %s", formfile);	fprintf(stdout, "%d setupforms\n", formsperpage);    }	/* End if */     fprintf(stdout, "%s", ENDSETUP); }   /* End of setup */ /*****************************************************************************/ arguments() { /* * * Makes sure all the non-option command line arguments are processed. If we get * here and there aren't any arguments left, or if '-' is one of the input files * we'll process stdin. * */     if ( argc < 1 )	statemachine(fp_in = stdin);    else  {				/* at least one argument is left */	while ( argc > 0 )  {	    if ( strcmp(*argv, "-") == 0 )		fp_in = stdin;	    else if ( (fp_in = fopen(*argv, "r")) == NULL )		error(FATAL, "can't open %s", *argv);	    statemachine(fp_in);	    if ( fp_in != stdin )		fclose(fp_in);	    argc--;	    argv++;	}   /* End while */    }   /* End else */ }   /* End of arguments */ /*****************************************************************************/ done() { /* * * Finished with all the input files, so mark the end of the pages with a TRAILER * comment, make sure the last page prints, and add things like the PAGES comment * that can only be determined after all the input files have been read. * */     fprintf(stdout, "%s", TRAILER);    fprintf(stdout, "done\n");    fprintf(stdout, "%s %s\n", DOCUMENTFONTS, fontname);    fprintf(stdout, "%s %d\n", PAGES, printed); }   /* End of done */ /*****************************************************************************/ account() { /* * * Writes an accounting record to *fp_acct provided it's not NULL. Accounting * is requested using the -A or -J options. * */     if ( fp_acct != NULL )	fprintf(fp_acct, " print %d\n copies %d\n", printed, copies); }   /* End of account */ /*****************************************************************************/ statemachine(fp)     FILE	*fp;			/* used to set fp_in */ { /*

Lines 1–500 of 1199.