/*
*
* 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.
*
*/
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 */
/*
*
* 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 */
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 */
/*
*
* 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;
/*
*
* 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);
/*
*
* 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 */
/*
*
* 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.
*
*/
x = cursor.x; /* where the cursor is right now */
y = cursor.y;
switch ( c ) {
case DEL:
return;
case BS:
if ((x -= charwidth[tekfont]) < margin)
x = TEKXMAX - charwidth[tekfont];
break;
case NL:
y -= charheight[tekfont];
break;
case CR:
x = margin;
break;
case VT:
if ((y += charheight[tekfont]) >= TEKYMAX)
y = 0;
break;
case HT:
case ' ':
default:
if ( characters++ == 0 )
fprintf(fp_out, "%d %d (", cursor.x, cursor.y);
switch ( c ) {
case '(':
case ')':
case '\\':
putc('\\', fp_out);
default:
putc(c, fp_out);
} /* End switch */
x += charwidth[tekfont];
move(x, y);
break;
} /* End switch */
if (x >= TEKXMAX) {
x = margin;
y -= charheight[tekfont];
} /* End if */
if (y < 0) {
y = TEKYMAX - charheight[tekfont];
x -= margin;
margin = (TEKXMAX/2) - margin;
if ((x += margin) > TEKXMAX)
x -= margin;
} /* End if */
int c; /* next character */
int b; /* for figuring out loy */
int x, y; /* next point in the vector */
static int hix, hiy; /* upper */
static int lox, loy; /* and lower part of the address */
static int extra; /* for extended addressing */
/*
*
* Handles things when we're in GRAPH, POINT, or SPECIALPOINT mode.
*
*/
if ((c = nextchar()) < 040) {
control(c);
return;
} /* End if */
if ((c & 0140) == 040) { /* new hiy */
hiy = c & 037;
do
if (((c = nextchar()) < 040) && ((c = control(c)) == OUTMODED))
return;
while (c == 0);
} /* End if */
if ((c & 0140) == 0140) { /* new loy */
b = c & 037;
do
if (((c = nextchar()) < 040) && ((c = control(c)) == OUTMODED))
return;
while (c == 0);
if ((c & 0140) == 0140) { /* no, it was extra */
extra = b;
loy = c & 037;
do
if (((c = nextchar()) < 040) && ((c = control(c)) == OUTMODED))
return;
while (c == 0);
} else loy = b;
} /* End if */
if ((c & 0140) == 040) { /* new hix */
hix = c & 037;
do
if (((c = nextchar()) < 040) && ((c = control(c)) == OUTMODED))
return;
while (c == 0);
} /* End if */
lox = c & 037; /* this should be lox */
if (extra & 020)
margin = TEKXMAX/2;
x = (hix<<7) | (lox<<2) | (extra & 03);
y = (hiy<<7) | (loy<<2) | ((extra & 014)>>2);
if ( points > 100 ) { /* don't put too much on the stack */
draw();
points = 1;
} /* End if */
/*
*
* Special point mode permits gray scaling by varying the size of the stored
* point, which is controlled by an intensity character that preceeds each point
* address.
*
*/
if ( dispmode == SPECIALPOINT ) {
if ( (c = nextchar()) < 040 || c > 0175 )
return(control(c));
fprintf(fp_out, "%d %d i\n", intensity[c - ' '], c & 0100);
} /* End if */
int c; /* for the next few characters */
int x, y; /* cursor position when we're done */
/*
*
* Handles incremental plot mode. It's entered after the RS control code and is
* used to mark points relative to our current position. It's typically followed
* by one or two bytes that set the pen state and are used to increment the
* current position.
*
*/
/*
*
* Checks character c and does special things, like mode changes, that depend
* not only on the character, but also on the current state. If the mode changed
* becuase of c, OUTMODED is returned to the caller. In all other cases the
* return value is c or 0, if c doesn't make sense in the current mode.
*
*/
switch ( c ) {
case BEL:
return(0);
case BS:
case HT:
case VT:
return(dispmode == ALPHA ? c : 0);
case CR:
if ( dispmode != ALPHA ) {
setmode(ALPHA);
ungetc(c, fp_in);
return(OUTMODED);
} else return(c);
case FS:
if ( (dispmode == ALPHA) || (dispmode == GRAPH) ) {
setmode(POINT);
return(OUTMODED);
} /* End if */
return(0);
case GS:
if ( (dispmode == ALPHA) || (dispmode == GRAPH) ) {
setmode(GRAPH);
return(OUTMODED);
} /* End if */
return(0);
case NL:
ungetc(CR, fp_in);
return(dispmode == ALPHA ? c : 0);
case RS:
if ( dispmode != GIN ) {
setmode(INCREMENTAL);
return(OUTMODED);
} /* End if */
return(0);
case US:
if ( dispmode == ALPHA )
return(0);
setmode(ALPHA);
return(OUTMODED);
/*
*
* Usually called when we've finished the last page and want to get ready for the
* next one. Also used at the beginning and end of each input file, so we have to
* be careful about exactly what's done.
*
*/
setmode(dispmode); /* end any outstanding text or graphics */
if ( fp_out == stdout ) /* count the last page */
printed++;
/*
*
* Reads the next character from the current input file and returns it to the
* caller. When we're finished with the file dispmode is set to EXIT and OUTMODED
* is returned to the caller.
*
*/
if ( (ch = getc(fp_in)) == EOF ) {
setmode(EXIT);
ch = OUTMODED;
} /* End if */