DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - metrics - download
Index: T d

⟦b731129a1⟧ TextFile

    Length: 67668 (0x10854)
    Types: TextFile
    Names: »dvisun.c~«

Derivation

└─⟦52210d11f⟧ Bits:30007239 EUUGD2: TeX 3 1992-12
    └─⟦af5ba6c8e⟧ »unix3.0/DVIWARE.tar.Z« 
        └─⟦ca79c7339⟧ 
            └─⟦this⟧ »DVIware/crt-viewers/sunview/dvisun/dvisun.c~« 

TextFile

/**********************************************************************/
/************************  Global Definitions  ************************/
/**********************************************************************/

typedef int BOOLEAN;
/* NOTE: THIS DEFN OF NO_FILE IS DIFFERENT THAN THAT OF FONTS.C (DVIPAGE) */
#define NO_FILE                   ((FILE *)0)

#define NEW(A) ((A *) malloc(sizeof(A)))
#undef DEBUG                          /* for massive printing of input */
                                        /* trace information; select by -d */
                                        /* option after filename: */
                                        /* dvibit filename -d */
#ifdef DEBUG
int Debug = 1;
#else
int Debug = 0;
#endif

/*
#define ALLOW_INTERRUPT 1 
*/              /* to allow ^C for Debugging */
                                        /* undefine for normal catch-^C mode */

#define BINARYOPEN fopen                /* byte-oriented host version */

#define ARITHRSHIFT 1                   /* define if ">>" operator is a
                                           sign-propagating arithmetic
                                           right shift */

#define USEGLOBALMAG 1                  /* when defined, the dvi global
                                           magnification is applied */
/* P. A. MacKay, 15 November, 1989 */
/* #undef  USEGLOBALMAG	one of the wins in adding this patch is that
   globalmag will rarely if ever fail */

#define  DVIFORMAT         2
#define  FIRSTPKCHAR      0
#ifndef FONTAREA
#define  FONTAREA          "/usr/local/lib/tex/fonts/pk120"
#endif
/* P. A. MacKay, 15 November, 1989 */
#define  LASTPKCHAR     255  /* take full advantage of pk format */
#define  NPKCHARS       256

#define  PXLID          1001
#define  READ              4   /* for access() */
#define  RESOLUTION      120   /* is really 91.72192515 (on a bitgraph) */
/* P. A. MacKay, 15 November, 1989 */
#define  HI_FONT_FUZZ     9   /* see the explanation of why the fuzz   */
#define  LO_FONT_FUZZ     -9  /* values are so large. (at ReadFontDef) */

#define  hconvRESOLUTION 118
#define  vconvRESOLUTION 110
#define  STACKSIZE       100
#define  STRSIZE         257

/**********************************************************************/
/***********************  external definitions  ***********************/
/**********************************************************************/

#include "commands.h"
#include <sgtty.h>
#include <signal.h>
#include <stdio.h>
#include <sys/errno.h>
#include <suntool/tool_hs.h>
# include <ctype.h>
#ifdef  UCI
# include <strings.h>
#endif  UCI

#ifndef UCI
#define  MAXOPEN      _NFILE    /* limit on number of open PK files */
#else
#define MAXOPEN     10  /* Leave room for other files! */
#endif  UCI

extern struct pixrect *pr_open();
char *getenv();
char *index();
char *malloc();
char *rindex();
char *sprintf();
char *strcpy();

int dummyInt;
short dummyShort;
char dummyChar;
struct tool *tool = NULL;  /* filled in if in window system; clp 7/15/84 */

int     set_mode = PIX_COLOR(1) | PIX_SRC;      /* default modes */
int     clr_mode = PIX_COLOR(0) | PIX_SRC;
int     draw_mode = PIX_SRC | PIX_DST;
int     invert = 0;

int     borders = 1;

/*-->NoSignExtend*/
/**********************************************************************/
/***************************  NoSignExtend  ***************************/
/**********************************************************************/

int
NoSignExtend(fp, n)     /* return n byte quantity from file fp */
register FILE *fp;      /* file pointer    */
register int n;         /* number of bytes */

{
    register int x;     /* number being constructed */

    x = 0;
    while (n--)  {
        x <<= 8;
        x |= getc(fp);
    }
    return(x);
}
/*-->SignExtend*/
/**********************************************************************/
/****************************  SignExtend  ****************************/
/**********************************************************************/

int
SignExtend(fp, n)   /* return n byte quantity from file fp */
register FILE *fp;  /* file pointer    */
register int n;     /* number of bytes */

{
    int n1;         /* number of bytes      */
    register int x; /* number being constructed */

    x = getc(fp);   /* get first (high-order) byte */
    n1 = n--;
    while (n--)  {
        x <<= 8;
        x |= getc(fp);
    }

    /* NOTE: This code assumes that the right-shift is an arithmetic, rather
    than logical, shift which will propagate the sign bit right.   According
    to Kernighan and Ritchie, this is compiler dependent! */

    /* Also, assuming sizeof(int) == 4 is machine dependent (32-bit ints) */

    x<<=32-8*n1;
    x>>=32-8*n1;  /* sign extend */

#ifdef DEBUG
    if (Debug)
    {
        fprintf(stderr,"\tSignExtend(fp,%d)=%X\n",n1,x);
    }
#endif
    return(x);
}
/**********************************************************************/
/*************************  Global Procedures  ************************/
/**********************************************************************/

/* Note: Global procedures are declared here in alphabetical order, with
   those which do not return values typed "void".  Their bodies occur in
   alphabetical order following the main() procedure.  The names are
   kept unique in the first 6 characters for portability. */

void    AbortRun();
float   ActualFactor();
void    AllDone();
FILE*   BINARYOPEN();
void    Fatal();
void    FindPostAmblePtr();
void    GetBytes();
void    GetFontDef();
/*
#ifdef  ALLOW_INTERRUPT
void    GotInterrupt();
#endif
*/
void    InitTerm();
int     InputWaiting();
void    MoveDown();
void    MoveOver();
void    OpenFontFile();
int     PixRound();
int     ReadFontDef();
void    ReadPostAmble();
void    ResetTerm();
void    SetChar();
void    SetFntNum();
void    SetRule();
void    SkipFontDef();
void    Warning();
void    draw_borders();

/**********************************************************************/
/*************************  Global Variables  *************************/
/**********************************************************************/

int G_errenc = FALSE;      /* has an error been encountered?          */
char G_Logname[STRSIZE];   /* name of log file, if created            */
int G_interactive = TRUE;  /* is the program running interactively    */
                           /* (i.e., standard output not redirected)? */
int G_logging = 0;         /* Is a log file being created?            */
struct sgttyb G_intty;     /* information about stdin if interactive  */
FILE *G_logfp;             /* log file pointer (for errors)           */
char G_progname[STRSIZE];  /* program name                            */

struct char_entry {             /* character entry */
   unsigned short width, height;/* width and height in pixels */
   short xOffset, yOffset;      /* x offset and y offset in pixels */
   struct {
       int isloaded;
       union {
           int fileOffset;
           struct pixrect *pixrectptr;
       } address;
      int flags;
   } where;
   int tfmw;                    /* TFM width */
};

struct font_entry {  /* font entry */
   int k, c, s, d, a, l;
   char n[STRSIZE];     /* FNT_DEF command parameters  */
   int font_space;      /* computed from FNT_DEF s parameter        */
   int font_mag;        /* computed from FNT_DEF s and d parameters */
   char name[STRSIZE];  /* full name of PK file                    */
   FILE *font_file_id;  /* file identifier (0 if none)              */
   int magnification;   /* magnification read from PK file         */
   int designsize;      /* design size read from PK file           */
   struct char_entry ch[NPKCHARS];/* character information         */
   struct font_entry *next;
};

struct pixel_list
{
    FILE *pixel_file_id;        /* file identifier                       */
    int use_count;              /* count of "opens"                      */
#ifdef  UCI
    char name[STRSIZE];     /* Pathname of the font, for caching */
#endif  UCI
};

int hconv, vconv;               /* converts DVI units to pixels          */
int den;                        /* denominator specified in preamble     */
FILE *dvifp;                    /* DVI file pointer                      */
struct font_entry *fontptr;     /* font_entry pointer                    */

struct font_entry *hfontptr=NULL;/* font_entry pointer                   */
int h;                          /* current horizontal position           */
int hh;                         /* current horizontal position in pixels */
int v;                          /* current vertical position             */
int vv;                         /* current vertical position in pixels   */
int mag;                        /* magnification specified in preamble   */
int nopen;                      /* number of open PK files              */
int num;                        /* numerator specified in preamble       */
struct font_entry *pfontptr = NULL; /* previous font_entry pointer       */
struct pixel_list pixel_files[MAXOPEN+1];
                                /* list of open PK file identifiers     */
struct pixrect *screen, *display;

long postambleptr;              /* Pointer to the postamble              */
FILE *pkfp;                    /* PK file pointer                      */
char *PKpath;                  /* PK path name for search              */
struct sgttyb tty;              /* to see if program is running interactively */


int     XSIZE = 1152;   /* size of physical screen in X and Y dimen */
int     YSIZE = 900;

#define DOTS_PER_INCH   (80)            /* This stuff added by clp, 7/24/84 */

double  SFACT = 1.43;
double  PFACT = 1.47;

#define FACTOR(n)    ((int)((SFACT * n) + 0.5))  /* borders made 45% larger */
#define PAGEFACT(n)  ((int)((PFACT * n) + 0.5))    /* pages made 45% larger */

/* Actually, 82 dots/X-inch, 78 dots/Y-inch is closer to the truth. Similarly,
 *  the X_FACTOR is around 1.446 and Y_FACTOR around 1.435... (for Sun-1).
 */

#define XPAGESIZE  PAGEFACT(8.5 * DOTS_PER_INCH)  /* 986, 8 1/2 inches wide */
#define YPAGESIZE  PAGEFACT(11 * DOTS_PER_INCH)     /* 1276, 11 inches tall */

/* The numbers in comments at right below this line assume Sun-1 dimensions */

#define BORDER_BLACK    (3)
#define BORDER_WHITE    ((XSIZE - XPAGESIZE - 2 * BORDER_BLACK) / 2)  /* 16 */
#define BORDER          (BORDER_WHITE + BORDER_BLACK)                 /* 19 */

#ifndef UCI
#define XDEFAULT   (-(FACTOR(1.2 * DOTS_PER_INCH) + BORDER))  /* 144+19=163 */
#define YDEFAULT   (-(FACTOR(0.9 * DOTS_PER_INCH) + BORDER))  /* 104+19=123 */
#else
#define XDEFAULT   (-(FACTOR(1.0 * DOTS_PER_INCH) + BORDER))  /* 144+19=163 */
#define YDEFAULT   (-(FACTOR(1.0 * DOTS_PER_INCH) + BORDER))  /* 104+19=123 */
#endif  UCI

#define UD_MOVE    (YPAGESIZE - (YSIZE - BORDER))                    /* 495 */

int xdefault /* = XDEFAULT */, ydefault /* = YDEFAULT */;
int xscreen  /* = XDEFAULT */, yscreen  /* = YDEFAULT */;


/******************************** Unix only definitions ******************/

struct tchars tcb;           /* information about special terminal chars */

/*************************************************************************/


#ifdef  TPIC
extern void set_pen_size(), flush_path(), flush_dashed();
extern void add_path(), arc(), flush_spline(), whiten_last(), shade_last();

#define COMLEN  4

static void do_special(k)
int k;
{
    char *spstring, *cp, command[COMLEN];
    register int len;

    spstring = malloc((unsigned) (k+1));
    if (spstring == NULL) Fatal("Out of memory");
    len = 0;
    while (k--) spstring[len++] = NoSignExtend(dvifp, 1);
    spstring[len] = '\0';
    cp = spstring;
    while (isspace(*cp)) ++cp;
    len = 0;
    while (!isspace(*cp) && *cp && len < COMLEN-1) command[len++] = *cp++;
    command[len] = '\0';
    hh = PixRound(h, hconv);
    vv = PixRound(v, vconv);
    if (strcmp(command, "pn") == 0) set_pen_size(cp);
    else if (strcmp(command, "fp") == 0) flush_path();
    else if (strcmp(command, "da") == 0) flush_dashed(cp, 0);
    else if (strcmp(command, "dt") == 0) flush_dashed(cp, 1);
    else if (strcmp(command, "pa") == 0) add_path(cp);
    else if (strcmp(command, "ar") == 0) arc(cp);
    else if (strcmp(command, "sp") == 0) flush_spline();
    else if (strcmp(command, "sh") == 0) shade_last();
    else if (strcmp(command, "wh") == 0) whiten_last();
    /* This ignores any other specials which come along */
    free(spstring);
}
#endif  TPIC


/**********************************************************************/
/*******************************  main  *******************************/
/**********************************************************************/

main(argc, argv)
int argc;
char *argv[];

{
    struct stack_entry {  /* stack entry */
        int h, v, w, x, y, z;  /* what's on stack */
    };


    int argind;             /* argument index for flags              */
    int command;            /* current command                       */
    long cpagep;            /* current page pointer                  */
    char *filename; /* file name                             */
    char *basename; /* file name w/o extension               */
    register int i;         /* command parameter; loop index         */
    int k;                  /* temporary parameter                   */
    char n[STRSIZE];        /* command parameter                     */
    int PagesLeft;          /* Number of pages left to skip          */
    int PreLoad = TRUE;     /* preload the font descriptions?        */
    long ppagep;            /* previous page pointer                 */
    int SkipEOP = FALSE;    /* input is waiting from the terminal, skip to EOP */
    int SkipMode = FALSE;   /* in skip mode flag                     */
#ifndef UCI /* Doens't seem to be used anywhere */
    int SoundBell = FALSE;  /* if false supress bell at end of page  */
#endif
    int sp;                 /* stack pointer                         */
    struct stack_entry stack[STACKSIZE];   /* stack                  */
    int t;                  /* temporary                             */
    char tc;                /* temporary character                   */
    char *tcp, *tcp1;       /* temporary character pointers          */
#ifndef UCI
    register struct char_entry *tcharptr;/* temporary char_entry ptr */
#endif
    int val, val2;          /* temporarys to hold command information*/
    int w;                  /* current horizontal spacing            */
    int x;                  /* current horizontal spacing            */
    int y;                  /* current vertical spacing              */
    int z;                  /* current vertical spacing              */
    nopen = 0;
    strcpy(G_progname, argv[0]);

    if (argc < 2)  {
        fprintf(stderr, "\nusage: %s -binpld +pages dvifile\n\n", G_progname);
        AbortRun(1);
    }

    if (ioctl(1,TIOCGETP,&tty) == -1)
        G_interactive = FALSE;

   /* get enviroment variables that allows for a path name in unix */

    if ((PKpath=getenv("PKFONTS")) == NULL)
        PKpath = FONTAREA;

    argind = 1;
    while (argind < argc && (*argv[argind] == '-' || *argv[argind] == '+'))
    {   
        tcp = argv[argind];
        if (*tcp == '-')
          { tcp++;
            switch(*tcp){
#ifndef UCI
                case 'b':       /* b supresses the bell at end of page */
                    SoundBell = TRUE;
                    break;
#endif  UCI

#ifdef DEBUG
                case 'd':       /* d selects Debug output */
                    Debug = TRUE;
                    break;
#endif

                case 'i':       /* i inverts the output to white-on-black */
                    invert = 1;
                    break;
        
                case 'l':       /* l prohibits logging of errors */
                    G_logging = -1;
                    break;
        
                case 'n':       /* n turns off the drawing of borders */
                    borders = 0;
                    break;
        
                case 'p':       /* p prohibits pre-font loading */
                    PreLoad = 0;
                    break;

                default:
                    printf("%c is not a legal flag\n", *tcp);
                }
        } else
        { sscanf(&argv[argind][1], "%d", &PagesLeft);
          SkipMode = TRUE;
          PagesLeft++;
        }
        argind++;
    }

    filename = (char *) calloc (STRSIZE, 1);
    basename = (char *) calloc (STRSIZE, 1);

    tcp = index(argv[argind], '.');


    strcpy(filename,argv[argind]);
    if (tcp == NULL)  {
      strcpy(basename,filename);
      strcat(filename,".dvi");
    } else
      strncpy(basename,argv[argind],tcp-argv[argind]);

    if ((dvifp=BINARYOPEN(filename,"r")) == NULL)  {
        fprintf(stderr,"\n");
        fprintf(stderr,"%s: can't open \"%s\"\n\n", G_progname, filename);
        AbortRun(1);
    }

    strcpy(G_Logname, filename);
    strcat(G_Logname, "-log");

   /* The terminal characteristics must be saved before any attempt is made */
   /* to process the DVI file, so that if the latter should fail, procedure */
   /* Fatal will be able to correctly restore the terminal state. */
    if (G_interactive)  {
        ioctl(0, TIOCGETC, &tcb);  /* get special characters */
        ioctl(0, TIOCGETP, &G_intty);  /* get modes */
        tty = G_intty;
        tty.sg_flags |= CBREAK;
        tty.sg_flags &= ~ECHO;
        ioctl(0, TIOCSETP, &tty);  /* set modes */
    }
/*
#ifndef ALLOW_INTERRUPT
    signal(SIGINT, SIG_IGN); */  /* ignore interrupts */ /*
#endif
*/
    if ( ! Debug ) InitTerm(); 
/*
#ifdef ALLOW_INTERRUPT
    signal(SIGINT, GotInterrupt); */  /* catch interrupts */ /*
#endif
*/

    xdefault = XDEFAULT; ydefault = YDEFAULT;   /* initialize here */
    xscreen  = XDEFAULT; yscreen  = YDEFAULT;   /* (can't in decl) */

    if ((i = NoSignExtend(dvifp, 1)) != PRE)  {
        fprintf(stderr,"\n");
        Fatal("%s: PRE doesn't occur first--are you sure this is a DVI file?\n\n",
        G_progname);
    }

    i = SignExtend(dvifp, 1);
    if (i != DVIFORMAT)  {
        fprintf(stderr,"\n");
        Fatal("%s: DVI format = %d, can only process DVI format %d files\n\n",
        G_progname, i, DVIFORMAT);
    }

    if (PreLoad)
    {
        ReadPostAmble();
        fseek(dvifp, (long) 14, 0);
    }
    else
    {
        num = NoSignExtend(dvifp, 4);
        den = NoSignExtend(dvifp, 4);
        mag = NoSignExtend(dvifp, 4);
        hconv = DoConv(num, den, hconvRESOLUTION);
        vconv = DoConv(num, den, vconvRESOLUTION);
    }
    k = NoSignExtend(dvifp, 1);
    GetBytes(dvifp, n, k);
    while (TRUE)

        switch (command=NoSignExtend(dvifp, 1))  {

        case SET1:case SET2:case SET3:case SET4:
            val = NoSignExtend(dvifp, command-SET1+1);
            if (!SkipMode) SetChar(val, command);
            break;

        case SET_RULE:
            val = NoSignExtend(dvifp, 4);
            val2 = NoSignExtend(dvifp, 4);
            if (!SkipMode) SetRule(val, val2, 1);
            break;

        case PUT1:case PUT2:case PUT3:case PUT4:
            val = NoSignExtend(dvifp,command-PUT1+1);
            if (!SkipMode) SetChar(val, command);
            break;

        case PUT_RULE:
            val = NoSignExtend(dvifp, 4);
            val2 = NoSignExtend(dvifp, 4);
            if (!SkipMode) SetRule(val, val2, 0);
            break;

        case NOP:
            break;

        case BOP:
            cpagep = ftell(dvifp) - 1;
            for (i=0; i<=9; i++)
                NoSignExtend(dvifp, 4);
            ppagep = NoSignExtend(dvifp, 4);

            pr_rop(display, 0, 0, display->pr_size.x, display->pr_size.y, clr_mode, NULL, 0, 0);
            h = v = w = x = y = z = 0;
            sp = 0;
            fontptr = NULL;
            if (PagesLeft)
                { PagesLeft--;
                  if (PagesLeft <= 0) SkipMode = FALSE;
                };
            break;

        case EOP:
            if (borders && !SkipMode && !SkipEOP)
                draw_borders();
            if (SkipEOP) { SkipMode = FALSE; SkipEOP = FALSE; }
            if (G_interactive && !SkipMode)  {
                t = FALSE;
                while (!t)  {  /* sorry about this flow of control kludge */
                    t = TRUE;
                    tc = GetChar();
                    if (tc==tcb.t_eofc || tc==tcb.t_intrc) AllDone();
                    switch (tc)  {
                    case ' ':           /* normal case - start next page */
                    case '\r':
                    case '\n':
#ifdef  UCI
                          case 'n':     /* Next page, like Previous page */
                          case 'N':
#endif  UCI
                        xscreen = xdefault; yscreen = ydefault;
                        break;
                                
                          case 'U':
                    case 'u':           /* move up and redisplay */
                        yscreen += UD_MOVE;
                        fseek(dvifp, cpagep, 0);
                        break;

                          case 'D':
                    case 'd':           /* move down and redisplay */
                        yscreen -= UD_MOVE;
                        fseek(dvifp, cpagep, 0);
                        break;

                          case 'L':
                    case 'l':           /* move left and redisplay */
                        xscreen += (XSIZE/3);
                        fseek(dvifp, cpagep, 0);
                        break;

                          case 'R':
                    case 'r':           /* move right and redisplay */
                        xscreen -= (XSIZE/3);
                        fseek(dvifp, cpagep, 0);
                        break;

                          case 'P':
                    case 'p':           /* redisplay from previous page */
                        if (ppagep != -1)  
                            fseek(dvifp, ppagep, 0);
                        else  
                            fseek(dvifp, cpagep, 0);
                                xscreen = xdefault; yscreen = ydefault;
                        break;

                    case 'q':           /* quit */
                          case 'Q':
                    case 'e':           /* exit */
                          case 'E':
                        AllDone();
                        break;

                     case '-':
                        /* read in val */
                        val = ReadInt();
#ifdef  UCI
            if (val == 0) val = 1;  /* Just "-" => "-1" */
#endif  UCI
                        while (val-- && ppagep != -1)
                                { fseek(dvifp, ppagep, 0);
                                  NoSignExtend(dvifp, 1);
                                  for(i=0; i<=9; i++)
                                        NoSignExtend(dvifp, 4);
                                  cpagep = ppagep;
                                  ppagep = NoSignExtend(dvifp, 4);
                                }
                        fseek(dvifp, cpagep, 0);
                        xscreen = xdefault; yscreen = ydefault;
                        break;

#ifdef DEBUG
                     case 'f':  /* fine tune y */
                        ydefault = ReadInt();
                        yscreen = ydefault;
                        fseek(dvifp, cpagep, 0);
                        break;

                     case 'h': /* fine tune hconv */
                        hconv = DoConv(num, den, ReadInt());
                        fseek(dvifp, cpagep, 0);
                        break;

                     case 'v': /* fine tune hconv */
                        vconv = DoConv(num, den, ReadInt());
                        fseek(dvifp, cpagep, 0);
                        break;
#endif

                     case '+':
                        /* read in val */
                        val = ReadInt();
                        SkipMode = TRUE;
                        PagesLeft = val;
                        break;

                    default:
                        t = FALSE;
                    }
                }
            }
            break;

        case PUSH:
            if (sp >= STACKSIZE)
                Fatal("stack overflow");
            stack[sp].h = h;
            stack[sp].v = v;
            stack[sp].w = w;
            stack[sp].x = x;
            stack[sp].y = y;
            stack[sp].z = z;
            sp++;
            break;

        case POP:
            --sp;
            if (sp < 0)
                Fatal("stack underflow");
            h = stack[sp].h;
            v = stack[sp].v;
            w = stack[sp].w;
            x = stack[sp].x;
            y = stack[sp].y;
            z = stack[sp].z;
            break;

        case RIGHT1:case RIGHT2:case RIGHT3:case RIGHT4:
            val = SignExtend(dvifp,command-RIGHT1+1);
            if (!SkipMode) MoveOver(val);
            break;

        case W0:
            if (!SkipMode) MoveOver(w);
            break;

        case W1:case W2:case W3:case W4:
            w = SignExtend(dvifp,command-W1+1);
            if (!SkipMode) MoveOver(w);
            break;

        case X0:
            if (!SkipMode) MoveOver(x);
            break;

        case X1:case X2:case X3:case X4:
            x = SignExtend(dvifp,command-X1+1);
            if (!SkipMode) MoveOver(x);
            break;

        case DOWN1:case DOWN2:case DOWN3:case DOWN4:
            val = SignExtend(dvifp,command-DOWN1+1);
            if (!SkipMode) MoveDown(val);
            break;

        case Y0:
            if (!SkipMode) MoveDown(y);
            break;

        case Y1:case Y2:case Y3:case Y4:
            y = SignExtend(dvifp,command-Y1+1);
            if (!SkipMode) MoveDown(y);
            break;

        case Z0:
            if (!SkipMode) MoveDown(z);
            break;

        case Z1:case Z2:case Z3:case Z4:
            z = SignExtend(dvifp,command-Z1+1);
            if (!SkipMode) MoveDown(z);
            break;

        case FNT1:case FNT2:case FNT3:case FNT4:
            if (StdinInputWaiting()) { SkipMode = TRUE; SkipEOP = TRUE;}
            if (!SkipMode) SetFntNum(NoSignExtend(dvifp,command-FNT1+1));
            break;

        case XXX1:case XXX2:case XXX3:case XXX4:
            k = NoSignExtend(dvifp,command-XXX1+1);
#ifndef TPIC
            while (k--)
                NoSignExtend(dvifp, 1);
#else
        if (SkipMode) {
        while (k--) (void) NoSignExtend(dvifp, 1);
        }
        else {
        if (StdinInputWaiting()) { SkipMode = TRUE; SkipEOP = TRUE;}
        else do_special(k);
        }
#endif  TPIC
            break;

        case FNT_DEF1:case FNT_DEF2:case FNT_DEF3:case FNT_DEF4:
            if (PreLoad)
            {
                SkipFontDef (NoSignExtend(dvifp, command-FNT_DEF1+1));
            }
            else
            {
                ReadFontDef (NoSignExtend(dvifp, command-FNT_DEF1+1));
            }
            break;

        case PRE:
            Fatal("PRE occurs within file");
            break;

        case POST:
            fseek(dvifp, cpagep, 0);
            SkipMode = FALSE;
            PagesLeft = 0;
            break;

        case POST_POST:
            Fatal("POST_POST with no preceding POST");
            break;

        default:
            if (command >= FONT_00 && command <= FONT_63)
                {if (StdinInputWaiting()) { SkipMode = TRUE; SkipEOP = TRUE;}
                 if (!SkipMode) SetFntNum(command - FONT_00);}
            else if (command >= SETC_000 && command <= SETC_127)
                {if (!SkipMode) SetChar(command - SETC_000, command);}
            else
                Fatal("%d is an undefined command", command);
            break;

        }
}


/* A new routine added by clp, 7/23/84, tries to explicitly draw in the
 *  page borders... note that we assume 116 pixels/inch is the scale the
 *  document is presented in (the display appears 45% larger than life),
 *  and that pages are offset 1.2 inches from the left and 0.9 inches from
 *  the top by dvi output drivers (our imagen driver does this). Note that
 *  116/inch is only approximate, h/vconvRESOLUTION are the truth.
 */

void
draw_borders()
{
        int  x = xdefault - xscreen;
        int  y = ydefault - yscreen;

        pr_rop(screen,                          /* top horizontal */
                BORDER_WHITE + x,  BORDER_WHITE + y,
                XPAGESIZE + (2 * BORDER_BLACK),  BORDER_BLACK,
                set_mode,
                NULL, 0, 0);

        pr_rop(screen,                       /* bottom horizontal */
                BORDER_WHITE + x,  y + YPAGESIZE,
                XPAGESIZE + (2 * BORDER_BLACK),  BORDER_BLACK,
                set_mode,
                NULL, 0, 0);

        pr_rop(screen,                           /* left vertical */
                BORDER_WHITE + x,        BORDER + y,
                BORDER_BLACK,  YPAGESIZE - BORDER,
                set_mode,
                NULL, 0, 0);

        pr_rop(screen,                          /* right vertical */
                BORDER + x + XPAGESIZE,  BORDER + y,
                BORDER_BLACK,  YPAGESIZE - BORDER,
                set_mode,
                NULL, 0, 0);
}
/* #define UD_MOVE         (YPAGESIZE - (YSIZE - BORDER))         */


/*-->AbortRun*/
/**********************************************************************/
/***************************  AbortRun  *******************************/
/**********************************************************************/

void
AbortRun(code)
int code;
{
    if (tool != NULL)
        tool_destroy(tool);
    exit(code);
}


/*-->ActualFactor*/
/**********************************************************************/
/**************************  ActualFactor  ****************************/
/**********************************************************************/

float           /* compute the actual size factor given the approximation */
ActualFactor(unmodsize)
int unmodsize;  /* actually factor * 1000 */
{
    float realsize;     /* the actual magnification factor */

    realsize = (float)unmodsize / 1000.0;
    /* a real hack to correct for rounding in some cases--rkf */
    if(unmodsize==1095) realsize = 1.095445;    /*stephalf*/
    else if(unmodsize==1315) realsize=1.314534; /*stepihalf*/
    else if(unmodsize==2074) realsize=2.0736;   /*stepiv*/
    else if(unmodsize==2488) realsize=2.48832;  /*stepv*/
    else if(unmodsize==2986) realsize=2.985984; /*stepiv*/
    /* the remaining magnification steps are represented with sufficient
           accuracy already */
    return(realsize);
}


/*-->AllDone*/
/**********************************************************************/
/*****************************  AllDone  ******************************/
/**********************************************************************/

void
AllDone()
{
#ifndef UCI
    char t;
#else
    int t;
#endif

    ResetTerm();
    if (G_errenc && G_logging == 1)  {
#ifndef UCI
        fseek(G_logfp, 0, 0);
#else
    fseek(G_logfp, 0L, 0);
#endif
        while ((t=getc(G_logfp)) != EOF)
            putchar(t);
    }
    if (G_logging == 1) printf("Log file created\n");
    AbortRun(G_errenc);
}

/*-->DoConv*/
/**********************************************************************/
/******************************  DoConv  ******************************/
/**********************************************************************/

int DoConv(num, den, convResolution)
{
    register float conv;
    conv = ((float)num/(float)den) * 
#ifdef USEGLOBALMAG
        ActualFactor(mag) *           /* put back in by clp, 8/22/84 */
/*      ((float) mag/1000.0) *           and this taken out...       */
#endif
        ((float)convResolution/254000.0);
    return((int) (1.0 / conv + 0.5));
}

/*-->Fatal*/
/**********************************************************************/
/******************************  Fatal  *******************************/
/**********************************************************************/
/* VARARGS */
void
Fatal(fmt, args)/* issue a fatal error message */
char *fmt;      /* format */
char *args;     /* arguments */

{
    if (G_logging == 1)
    {
        fprintf(G_logfp, "%s: FATAL--", G_progname);
        _doprnt(fmt, &args, G_logfp);
        fprintf(G_logfp, "\n");
    }

    ResetTerm();
    fprintf(stderr,"\n");
    fprintf(stderr, "%s: FATAL--", G_progname);
    _doprnt(fmt, &args, stderr);
    fprintf(stderr, "\n\n");
#ifdef  UCI
    perror("perror");
#endif  UCI
    if (G_logging == 1) printf("Log file created\n");
    AbortRun(1);
}


/*-->FindPostAmblePtr*/
/**********************************************************************/
/************************  FindPostAmblePtr  **************************/
/**********************************************************************/

void
FindPostAmblePtr(postambleptr)
long    *postambleptr;

/* this routine will move to the end of the file and find the start
    of the postamble */

{
    int     i;

    fseek (dvifp, (long) 0, 2);   /* goto end of file */
    *postambleptr = ftell (dvifp) - 4;
    fseek (dvifp, *postambleptr, 0);

    while (TRUE) {
        fseek (dvifp, --(*postambleptr), 0);
        if (((i = NoSignExtend(dvifp, 1)) != 223) &&
            (i != DVIFORMAT))
            Fatal ("Bad end of DVI file");
        if (i == DVIFORMAT)
            break;
    }
#ifndef UCI
    fseek (dvifp, (*postambleptr) - 4, 0);
#else
    fseek(dvifp, (long) ((*postambleptr) - 4), 0);
#endif
    *postambleptr = NoSignExtend(dvifp, 4);
    fseek (dvifp, *postambleptr, 0);
}


/*-->GetBytes*/
/**********************************************************************/
/*****************************  GetBytes  *****************************/
/**********************************************************************/

void
GetBytes(fp, cp, n)     /* get n bytes from file fp */
register FILE *fp;      /* file pointer  */
register char *cp;      /* character pointer */
register int n;         /* number of bytes  */

{
    while (n--)
        *cp++ = getc(fp);
}


/*-->GetFontDef*/
/**********************************************************************/
/**************************** GetFontDef  *****************************/
/**********************************************************************/

void
GetFontDef()

/***********************************************************************
   Read the font  definitions as they  are in the  postamble of the  DVI
   file.  Note that the font directory  is not yet loaded.  In order  to
   adapt ourselves to the existing "verser" the following font paramters
   are  copied   onto  output   fontno  (4   bytes),  chksum,   fontmag,
   fontnamelength (1 byte), fontname.  At the end, a -1 is put onto  the
   file.
***********************************************************************/

{
#ifndef UCI
    char    str[50], *calloc ();
    unsigned char   byte;
    int     i, fnamelen;
#else
/*    char *calloc(); */
    unsigned char byte;
#endif

    while (((byte = NoSignExtend(dvifp, 1)) >= FNT_DEF1) &&
        (byte <= FNT_DEF4)) {
        switch (byte) {
        case FNT_DEF1:
            ReadFontDef (NoSignExtend(dvifp, 1));
            break;
        case FNT_DEF2:
            ReadFontDef (NoSignExtend(dvifp, 2));
            break;
        case FNT_DEF3:
            ReadFontDef (NoSignExtend(dvifp, 3));
            break;
        case FNT_DEF4:
            ReadFontDef (NoSignExtend(dvifp, 4));
            break;
        default:
            Fatal ("Bad byte value in font defs");
            break;
        }
    }
    if (byte != POST_POST)
        Fatal ("POST_POST missing after fontdefs");
}

/*
#ifdef  ALLOW_INTERRUPT */
/*-->GotInterrupt*/
/**********************************************************************/
/***************************  GotInterrupt  ***************************/
/**********************************************************************/
/* ARGSUSED */ /*
void
GotInterrupt(sig)  */ /* don't leave terminal in a weird state */ /*
int sig;

{
    ResetTerm();
    if (G_logging == 1) printf("Log file created\n");
    AbortRun(G_errenc);
}
#endif
*/

/*-->InitTerm*/
/**********************************************************************/
/*****************************  InitTerm  *****************************/
/**********************************************************************/
/*
 * This routine modified to work under the window system by clp, 7/15/84
 *  A "struct tool *tool" global decl. added and a "tool_destroy(tool)"
 *  put in AbortRun(); include file changed from pixrect to suntool.
 *  Almost all of the following routine is new. Also, I had to change
 *  the getchar() routine to a new GetChar(), which is defined below;
 *  had to change InputWaiting(stdin) to StdinInputWaiting() as well.
 */

static
struct rect     rect = { 0, 0, 0, 0 };  /* X/Y Sizes filled in below */
static
struct cursor   cursor = { 0, 0, PIX_DST, NULL };

#define IN_WINDOW_SYSTEM  (getenv("WINDOW_PARENT") != NULL)
#define WIN_FD            (tool->tl_windowfd)

void
InitTerm()      /* initialize terminal */
{
    int value = 1;
    struct inputmask im;

    if (IN_WINDOW_SYSTEM) {
        rect.r_width  = XSIZE;
        rect.r_height = YSIZE;

        if ((tool = tool_create("", 0, &rect, NULL)) == NULL) {
            perror("pixel (trouble creating tool): ");
            exit(1);
        }
        {
            unsigned char red[2], green[2], blue[2];

            pw_getcolormap(tool->tl_pixwin, 0, 2, red, green, blue);

            if ((!red[0] && !invert) || (red[0] && invert)) {
                draw_mode = (PIX_NOT(PIX_SRC) & PIX_DST);
                set_mode = PIX_COLOR(0) | PIX_SRC;
                clr_mode = PIX_COLOR(1) | PIX_SRC;
            }
        }
        win_setcursor(WIN_FD, &cursor);

        input_imnull(&im);
        im.im_flags |= IM_ASCII | IM_POSASCII;
        win_setinputmask(WIN_FD, &im, NULL, WIN_NULLLINK);

        ioctl(WIN_FD, FIONBIO, &value); /* sets non-blocking input */

        screen = tool->tl_pixwin->pw_pixrect;

        signal(SIGWINCH, SIG_IGN);
        tool_install(tool);

    } else {
    printf("Error not in window system\n");
    ResetTerm();
    exit(-1);
    }
    display = screen;
}

#define NO_INPUT   (input_readevent(WIN_FD, &ie) == -1 && errno == EWOULDBLOCK)

static
struct inputevent  ie;
extern int         errno;

static int         old_char, waiting_char = 0;

GetChar()
{
    int value;

    if (IN_WINDOW_SYSTEM) {
        if (waiting_char) {
            waiting_char = 0;
            return(old_char);
        }
        for (;;) {
            if (NO_INPUT) {
                value = 0;
                ioctl(WIN_FD, FIONBIO, &value);    /* sets blocking input */

                input_readevent(WIN_FD, &ie);

                value = 1;
                ioctl(WIN_FD, FIONBIO, &value);    /* sets non-blocking input */
            }
            if (ie.ie_code >= ASCII_FIRST && ie.ie_code <= ASCII_LAST)
                break;
        }
        return(ie.ie_code - ASCII_FIRST);
    } else
        return(getchar());                      /* standalone */
}

StdinInputWaiting()
{
    if (IN_WINDOW_SYSTEM) {
        if (waiting_char)
            return(TRUE);       /* saw a waiting char since last GetChar() */
        if (NO_INPUT)
            return(FALSE);
        old_char = ie.ie_code - ASCII_FIRST;
        waiting_char = 1;               /* so GetChar() sees it */
        return(TRUE);
    } else
        return(InputWaiting(stdin));            /* standalone */
}


/*-->InputWaiting*/
/**********************************************************************/
/************************* InputWaiting *******************************/
/**********************************************************************/

int
InputWaiting(fp)
FILE *fp;

/*   this routine returns TRUE if there is input waiting to be read
        and FALSE otherwise.  note that it may take time to realize
        that there is input waiting so the routine may occasionaly
        be wrong.   */
{
    long retval;

    if (fp->_cnt != 0) return(TRUE);
    ioctl(fileno(fp), FIONREAD, &retval);
    return((int)retval);
}


/*-->MoveDown*/
/**********************************************************************/
/****************************  MoveDown  ******************************/
/**********************************************************************/

void
MoveDown(a)
int a;
{
    v += a;
}


/*-->MoveOver*/
/**********************************************************************/
/****************************  MoveOver  ******************************/
/**********************************************************************/

void
MoveOver(b)
int b;
{
    h += b;
}


/*-->OpenFontFile*/
/**********************************************************************/
/************************** OpenFontFile  *****************************/
/**********************************************************************/

void
OpenFontFile()
/***********************************************************************
    The original version of this dvi driver reopened the font file  each
    time the font changed, resulting in an enormous number of relatively
    expensive file  openings.   This version  keeps  a cache  of  up  to
    MAXOPEN open files,  so that when  a font change  is made, the  file
    pointer, pkfp, can  usually be  updated from the  cache.  When  the
    file is not found in  the cache, it must  be opened.  In this  case,
    the next empty slot  in the cache  is assigned, or  if the cache  is
    full, the least used font file is closed and its slot reassigned for
    the new file.  Identification of the least used file is based on the
    counts of the number  of times each file  has been "opened" by  this
    routine.  On return, the file pointer is always repositioned to  the
    beginning of the file.

***********************************************************************/
{
    register int i,least_used,current;

#ifdef DEBUG
    if (Debug) printf("Open Font file\n");
#endif
    if (pfontptr == fontptr)
        return;                 /* we need not have been called */

    for (current = 1;
        (current <= nopen) &&
#ifndef UCI
            (pixel_files[current].pixel_file_id != fontptr->font_file_id);
#else
        strcmp(pixel_files[current].name, fontptr->name);
#endif  UCI
        ++current)
        ;                       /* try to find file in open list */

    if (current <= nopen)       /* file already open */
    {
        pkfp = pixel_files[current].pixel_file_id;
#ifndef UCI
        fseek(pkfp,0,0);       /* reposition to start of file */
#else
    fseek(pkfp, 0L, 0);
#endif
    }
    else                        /* file not in open list */
    {
        if (nopen < MAXOPEN)    /* just add it to list */
            current = ++nopen;
        else                    /* list full -- find least used file, */
        {                       /* close it, and reuse slot for new file */
            least_used = 1;
            for (i = 2; i <= MAXOPEN; ++i)
                if (pixel_files[least_used].use_count >
                    pixel_files[i].use_count)
                    least_used = i;
            fclose(pixel_files[least_used].pixel_file_id);
            current = least_used;
        }
        if ((pkfp=BINARYOPEN(fontptr->name,"r")) == NULL)
            Fatal("PK file \"%s\" could not be opened; %d PK files are open",
                fontptr->name,nopen);
        pixel_files[current].pixel_file_id = pkfp;
        pixel_files[current].use_count = 0;
#ifdef  UCI
    strcpy(pixel_files[current].name, fontptr->name);
#endif  UCI
    }
    pfontptr = fontptr;                 /* make previous = current font */
    fontptr->font_file_id = pkfp;      /* set file identifier */
    pixel_files[current].use_count++;   /* update reference count */
}


/*-->PixRound*/
/**********************************************************************/
/*****************************  PixRound  *****************************/
/**********************************************************************/

int
PixRound(x, conv)       /* return rounded number of pixels */
register int x;         /* in DVI units     */
int conv;               /* conversion factor */
{
    return((int)((x + (conv >> 1)) / conv));
}


/*
 * PK font reading functions
 */

/*  ID byte value at the beginning of PK files  */
#define PK_ID       89

/*  PK op codes  */
#define PK_XXX1     240
#define PK_XXX2     241
#define PK_XXX3     242
#define PK_XXX4     243
#define PK_YYY      244
#define PK_POST     245
#define PK_NOP      246
#define PK_PRE      247

#define PK_REPEAT   0xe /*  Repeat last row - repeat count in next nibble  */
#define PK_AGAIN    0xf /*  Repeat only once  */
#define PK_LARGE    0x0 /*  Long run vaule coming up  */


/*
 * the macros that read pk values need these variables.
 */
static u_short  _nyb_buf,
        _nyb_flag,
        _pk_repeat;


/*
 * get the next nybble of the file.  This macro requires
 * that 2 integers named _nyb_buf and _nyb_flag be allocated elsewhere in
 * the program.  In addition, _nyb_flag must be initialized to 0.
 */
#define GET_NYB ((_nyb_flag ^= 1) ? \
            ((_nyb_buf = (unsigned)getc(pkfp)) >> 4) : \
            (_nyb_buf & 0xf))

/*
 * The quantity to be packed into nybbles may require an odd number of
 * nybbles which will cause the nybble fetching macro to get out of
 * sync.  The following macro ``clears'' the state of the nybble fetching
 * routine and should be executed whenever transitioning from nybble to
 * byte (or other) quantities.
 */
#define CLEAR_NYB   _nyb_flag = 0;

/*
 * this macro gets a PK ``packed number'' from fp and puts it into x.
 * It is an adaption of an algorithm presented in Tugboat V. 6, No. 3.
 */
#define GET_PACKED(x) x = get_packed(pkfp, dyn_f)

static u_int
get_packed(pkfp, dyn_f)
    register FILE *pkfp;
    register u_int dyn_f;
{
    register int i, j;

    i = GET_NYB;
    if (i == 0) {
        /*  
         * we have an arbitrarily long number.  scan to
         * find the first non-zero nybble which is the
         * count of the nybbles in this value.
         */
        do {
            j = GET_NYB;
            i++;
        } while (j == 0);

        while (--i >= 0)
            j = (j << 4) + GET_NYB;

        return (j + 193 - 15 * dyn_f);
    } else if (i <= dyn_f) {
        /* this nybble is the number we want. */
        return (i);
    } else if (i < 14) {
        return (i + 15 * (i - dyn_f - 1) + GET_NYB);
    } else if (i == 14) {
        _pk_repeat = get_packed (pkfp, dyn_f);
    } else {
        _pk_repeat = 1;
    }
    return (i);
   }


static u_char bit_mask[] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };

/* The great and unnecessary pain in using almost all the early viewers,
 * and a good many recent ones has been their unfriendly habit of aborting
 * when a font could not be found---even if the name was off by only a
 * single DPI value (128 in place of 129, for instance.).  With the patch
 * added below, such failures will hardly ever happen, but you will be
 * told of any font substitutions that were necessary.  Any program that
 * uses the access() routine can adapt this code quite easily. 
 *
 * Strategy:
 * If the font cannot be found as requested, a FUZZ amount ranging from
 * LO_FONT_FUZZ to HI_FONT_FUZZ is added to the magnification and access()
 * is tried on each resultant font filename.  If that fails, and the 
 * DPI value is greater than the base resolution, the DPI value is
 * divided by one magstep (1.2) and FUZZ is tried again, and again,
 * until the resultant resolution is down at the base resolution given
 * by the defined value of RESOLUTION in this program.  Then, if the
 * design size is > 10, another attempt is made at designsize 10
 * (that is cmsy17 -> cmsy10).  Smaller fonts are all converted to
 * cmr7.  (If you don't have cmr7 at the base resolution, you ARE in trouble.)
 * One last desperate try is made if even designsize 10 cannot be
 * made to work on one of the larger fonts.  The brutal substition
 * of cmr10 at the base resolution is used, no matter what the font
 * may have been supposed to be.  (By that time the document looks
 * pretty strange in any case.)
 * The FUZZ is set very large.  10 units on either side of the requested
 * rasterization value.  This is so that a directory full of *200pk and
 * 240pk fonts can be used when the program asks for 207pk or 248pk.
 * 300pk can be used as a substitute for 298pk in the same way.  The
 * possibilities are quite generous.
 */
/*-->ReadFontDef*/
/**********************************************************************/
/****************************  ReadFontDef  ***************************/
/**********************************************************************/

int
ReadFontDef(k)
int k;
{
    int t;
    register struct font_entry *tfontptr;    /* temporary font_entry pointer   */
    register unsigned c, cc;
    long pl, addr;
    register u_int i;
    u_int hppp, vppp;
    int checksum;
    u_int tfmw;
    double cscale;



    register struct char_entry *tcharptr;/* temporary char_entry pointer  */
    char *direct, *tcp, *tcp1;
    int found;
    char curarea[STRSIZE];
    char requested_font[STRSIZE]; /* P. A. MacKay, 15 November, 1989 */

    if ((tfontptr = NEW(struct font_entry)) == NULL)
        Fatal("can't malloc space for font_entry");
    tfontptr->next = hfontptr;
    fontptr = hfontptr = tfontptr;

    tfontptr->k = k;
    tfontptr->c = NoSignExtend(dvifp, 4); /* checksum */
    tfontptr->s = NoSignExtend(dvifp, 4); /* space size */
    tfontptr->d = NoSignExtend(dvifp, 4); /* design size */
    tfontptr->a = NoSignExtend(dvifp, 1); /* area length for font name */
    tfontptr->l = NoSignExtend(dvifp, 1); /* device length */
    GetBytes(dvifp, tfontptr->n, tfontptr->a+tfontptr->l);
    tfontptr->n[tfontptr->a+tfontptr->l] = '\0';
    tfontptr->font_space = tfontptr->s/6; /* never used */
    tfontptr->font_mag = (int)((ActualFactor((int)(((float)tfontptr->s/
                        (float)tfontptr->d)*1000.0 + 0.5)) * 
#ifdef USEGLOBALMAG
                        ActualFactor(mag) *
#endif
                        (float)RESOLUTION) + 0.5);
    if (tfontptr->a != 0) {
        sprintf(tfontptr->name, "%s.%dpk", tfontptr->n, tfontptr->font_mag);
    } else {
        direct = PKpath;
        found = FALSE;
        do { 
            tcp = index(direct, ':');
            if (tcp == NULL) tcp = strlen(direct) + direct;
            strncpy(curarea, direct, tcp-direct);
            tcp1 = curarea + (tcp - direct);
            *tcp1++ = '/';
            *tcp1++ = '\0';

            sprintf(tfontptr->name, "%s%s.%dpk", curarea, tfontptr->n, tfontptr->font_mag);
            found = (access(tfontptr->name, READ) == 0);
/************ P. A. MacKay, 15 November, 1989: patch for font substitution */
	    if ( ! found ) {
	      (void)sprintf(requested_font, "%s.%dpk", tfontptr->n, tfontptr->font_mag);
	      t = LO_FONT_FUZZ;
	    }
	    while ( (!found) && ( t < HI_FONT_FUZZ+2) ) {
	      (void)sprintf(tfontptr->name, "%s%s.%dpk", curarea, tfontptr->n, ( tfontptr->font_mag + t ) );
	      found = (access(tfontptr->name, READ) == 0);
	      if ( (!found) && ( t < HI_FONT_FUZZ ) ) t++;
	      else {
		t = LO_FONT_FUZZ;
		if ( tfontptr->font_mag < ( RESOLUTION + HI_FONT_FUZZ) ) {
		  if ( (*(tfontptr->n + (strlen(tfontptr->n) -2) ) == '1') &&
		      (*(tfontptr->n + (strlen(tfontptr->n) -1) ) == '0') )
		      t = HI_FONT_FUZZ+2; /* Force exit from loop */
		  if (strncmp(tfontptr->n,"cmr7",4) == 0) 
		      t = HI_FONT_FUZZ+2; /* Force exit from loop */
		  if ( isdigit(*(tfontptr->n + (strlen(tfontptr->n) -2) ) ) ){
		    *(tfontptr->n + (strlen(tfontptr->n) -2) ) = '1';
		    *(tfontptr->n + (strlen(tfontptr->n) -1) ) = '0';
		  }
		  else {
		    (void)strncpy(tfontptr->n,"cmr7'\0'",5);
		    tfontptr->font_mag = RESOLUTION;
		  }
		}
		else {
		  if ( tfontptr->font_mag > ( RESOLUTION + HI_FONT_FUZZ ) ) 
 		    tfontptr->font_mag = (int)(((float)tfontptr->font_mag / 1.2) );
		} /* ENDIF if (tfontptr->font_mag < ( RESOLUTION + HI_FONT_FUZZ) ) */
	      } /* ENDELSE if ( (!found) && ( t < HI_FONT_FUZZ ) ) */
	      if ( (!found) && ( t == HI_FONT_FUZZ+2) && 
		  ( ! (strncmp(tfontptr->n,"cmr",3) == 0) ) ) {
		(void)strncpy(tfontptr->n,"cmr10\0",6);
		t = LO_FONT_FUZZ; /* really desperate */
	      }
	    } /* ENDLOOP while ( (!found) && ( t < HI_FONT_FUZZ+2) ) */
	    if (*tcp) direct = tcp + 1; else direct = tcp;
	  } while ( !found && *direct != '\0');
	if ( requested_font[0] > '\0') {
	  Warning("Substituted %s for %s\n", tfontptr->name, requested_font);
	}
      }
/* P. A. MacKay, 15 November, 1989: end of patch for font substitution */

    if (tfontptr != pfontptr)
        OpenFontFile();
/*    if ((t = NoSignExtend(pkfp, 4)) != PXLID)
        Fatal("PXL ID = %d, can only process PXL ID = %d files",
              t, PXL ID);
*/

    fseek (pkfp, 0L, 0);    /* make sure at the beginning of pk file */

    if (NoSignExtend(pkfp, 1) != PK_PRE) {
        Warning("pk font file %s doesn't start with PRE\n",
             tfontptr->name);
        fclose(tfontptr->font_file_id);
        tfontptr->font_file_id = NO_FILE;
        return TRUE;
    }

    if (NoSignExtend(pkfp, 1) != PK_ID) {
        Warning("pk font file %s wrong version\n",
               tfontptr->name);
        fclose(tfontptr->font_file_id);
        tfontptr->font_file_id = NO_FILE;
        return TRUE;
       }

    fseek (pkfp, (long) NoSignExtend(pkfp, 1), 1);  /* skip comment */
    tfontptr->designsize = NoSignExtend(pkfp, 4);   /* ds[4] */
    checksum = NoSignExtend(pkfp, 4);   /* checksum[4] */
    hppp = NoSignExtend(pkfp, 4);       /* hppp[4] */
    vppp = NoSignExtend(pkfp, 4);       /* vppp[4] */
    cscale = (double) tfontptr->s / (double)(1 << 20);

    while ((c = NoSignExtend(pkfp, 1)) != EOF) {
        if (c >= PK_XXX1) { /* commands are just skipped */
            switch (c) {
            case PK_XXX1:   /* pk_xxx1 k[1] x[k] */
                fseek (pkfp, (long)NoSignExtend(pkfp,1), 1);
                break;
            case PK_XXX2:   /* pk_xxx2 k[2] x[k] */
                fseek (pkfp, (long)NoSignExtend(pkfp,2), 1);
                break;
            case PK_XXX3:   /* pk_xxx3 k[3] x[k] */
                fseek (pkfp, (long)NoSignExtend(pkfp,3), 1);
                break;
            case PK_XXX4:   /* pk_xxx4 k[4] x[4] */
                fseek (pkfp, (long)SignExtend(pkfp,4), 1);
                break;
            case PK_YYY:    /* pk_yyy y[4] */
                (void) NoSignExtend(pkfp, 4);
                break;
            case PK_POST:
                return TRUE;
            case PK_PRE:
                Warning("pk font file %s has extra PRE\n",
                    tfontptr->name);
                fclose(tfontptr->font_file_id);
                tfontptr->font_file_id = NO_FILE;
                return TRUE;
            default: /* do nothing */ ;
            }
        } else {    /* flag byte */
            switch (c & 0x07) { /* check flag byte */
            case 0:/* short form */
            case 1:
            case 2:
            case 3:
                    /* length */
                pl = (long) NoSignExtend(pkfp, 1)
                     + (long) ((c & 0x03) << 8);
                cc = NoSignExtend(pkfp,1);  /* char. code */
                tfmw = NoSignExtend(pkfp,3); /* tfm width */
                (void) NoSignExtend(pkfp,1); /* x-escapement */
                tfontptr->ch[cc].width = NoSignExtend(pkfp, 1);
                tfontptr->ch[cc].height = NoSignExtend(pkfp, 1);
                tfontptr->ch[cc].xOffset = SignExtend(pkfp, 1);
                tfontptr->ch[cc].yOffset = SignExtend(pkfp, 1);
                addr = ftell (pkfp);
                pl -= 8;
                break;

            case 4:/* extended short form */
            case 5:
            case 6:
                pl = (long) NoSignExtend(pkfp, 2)
                     + (long) ((c & 0x03) << 16);
                cc = NoSignExtend(pkfp,1);  /* char. code */
                tfmw = NoSignExtend(pkfp,3); /* tfm width */
                (void) NoSignExtend(pkfp,2); /* x-escapement */
                tfontptr->ch[cc].width = NoSignExtend(pkfp, 2);
                tfontptr->ch[cc].height = NoSignExtend(pkfp, 2);
                tfontptr->ch[cc].xOffset = SignExtend(pkfp, 2);
                tfontptr->ch[cc].yOffset = SignExtend(pkfp, 2);
                addr = ftell (pkfp);
                pl -= 13;
                break;

            case 7:/* long form */
                pl = NoSignExtend(pkfp,4);
                cc = NoSignExtend(pkfp,4);  /* char. code */
                tfmw = NoSignExtend(pkfp,4);
                (void) NoSignExtend(pkfp,4); /* x-escapement */
                tfontptr->ch[cc].width = NoSignExtend(pkfp, 4);
                tfontptr->ch[cc].height = NoSignExtend(pkfp, 4);
                tfontptr->ch[cc].xOffset = SignExtend(pkfp, 4);
                tfontptr->ch[cc].yOffset = SignExtend(pkfp, 4);
                addr = ftell (pkfp);
                pl -= 24;
                break;
               }
            tfontptr->ch[cc].tfmw = (int)(((double)tfmw * cscale) + 0.5);
            tfontptr->ch[cc].where.isloaded = 0;
            tfontptr->ch[cc].where.flags = c;
            tfontptr->ch[cc].where.address.fileOffset = addr;;
            fseek (pkfp, pl, 1);    /* skip until next flag byte */
           }
       }
    if((tfontptr->c != 0) && (checksum != 0) && (tfontptr->c != checksum))
      Warning("Bad font checksum %d != %d; font %s.",
               checksum, tfontptr->c, tfontptr->name);
    return TRUE;
  }


/**********************************************************************/
/****************************  ReadInt  *******************************/
/**********************************************************************/

/*  this routine is used to read in an integer from the stdin stream.
        This routine is necessary since the terminal is running in
        CBREAK mode and therefore will not do editing of the input
        stream for one. */

ReadInt()
{
   int value = 0;
   int byte;

   while ((byte = GetChar()) != /* tcb.t_brkc */ 10 && byte != 13)
        { if (byte >= 48 /* "0" */ && byte <= 57 /* "9" */)
              value = value * 10 + byte - 48;
          if (byte == tty.sg_erase) value = value / 10;
        }
   return(value);
}
                  

/*-->ReadPostAmble*/
/**********************************************************************/
/**************************  ReadPostAmble  ***************************/
/**********************************************************************/

void
ReadPostAmble()
/***********************************************************************
    This  routine  is  used  to  read  in  the  postamble  values.    It
    initializes the magnification and checks  the stack height prior  to
    starting printing the document.
***********************************************************************/
{
    FindPostAmblePtr (&postambleptr);
    if (NoSignExtend(dvifp, 1) != POST)
        Fatal ("POST missing at head of postamble");
#ifdef DEBUG
    if (Debug) fprintf (stderr, "got POST command\n");
#endif
    /*    lastpageptr = */ NoSignExtend(dvifp, 4);
    num = NoSignExtend(dvifp, 4);
    den = NoSignExtend(dvifp, 4);
    mag = NoSignExtend(dvifp, 4);
    hconv = DoConv(num, den, hconvRESOLUTION);
    vconv = DoConv(num, den, vconvRESOLUTION);

    NoSignExtend(dvifp, 4);     /* height-plus-depth of tallest page */
    NoSignExtend(dvifp, 4);     /* width of widest page */
    if (NoSignExtend(dvifp, 2) >= STACKSIZE)
        Fatal ("Stack size is too small");
    NoSignExtend(dvifp, 2);     /* this reads the number of pages in */
    /* the DVI file */
#ifdef DEBUG
    if (Debug) fprintf (stderr, "now reading font defs");
#endif
    GetFontDef ();
}


/*-->ResetTerm*/
/**********************************************************************/
/****************************  ResetTerm  *****************************/
/**********************************************************************/

void
ResetTerm()   /* Reset Terminal */
{
    if (G_interactive)
        ioctl(0, TIOCSETP, &G_intty);  /* restore modes */
}

/*-->LoadAChar*/
/**********************************************************************/
/*****************************  LoadAChar  ****************************/
/**********************************************************************/

int buffer[8];

/*
 * load a PK
 character into memory.
 */
BOOLEAN
LoadAChar( ptr)
    register struct char_entry *ptr;
{
    register struct pixrect *pr;
    int cw; /* character width */
    int ch; /* character height */
    u_int   dyn_f;  /* dynamic factor, part of a PK word. */
    register int i, j;
    register int black, bits, bp, rc;
    int rowp;
      
    if (ptr->where.address.fileOffset == 0) {
        ptr->where.address.pixrectptr = NULL;
        return;
    }

    OpenFontFile();

    if (ptr->width == 0 || ptr->height == 0) {
        ptr->where.address.pixrectptr = (struct pixrect *) 0;
        return;
    }
    fseek(pkfp, (long)ptr->where.address.fileOffset, 0);

    pr = mem_create(ptr->width, ptr->height, 1);
    ptr->where.address.pixrectptr = pr;

    cw = ptr->width; ch = ptr->height;
    pr_rop(pr, 0, 0, cw, ch, PIX_CLR, NULL, 0, 0);

    /*
     * what remains is the data for the image.  It can be packaged
     * either as a run-encoding where successive values are the
     * number of adjacent pixels to paint in the opposite color of
     * the previous painting, or simply as a bitmap with no padding
     * except (possibly) for the very last nybble to round up to a
     * byte value.
     *
     * the data for the character is stored in successive bits with
     * each new horizontal row at a long boundary.  See the PXL
     * file format.
     */

    /*
     * grab the dyn_f out of the flag for this character.
     */
    dyn_f = (ptr->where.flags >> 4) & 0xf;
    
    /*
     * the data returned by calloc is zeroed; we depend on that
     * because we only turn on bits that are supposed to be
     * black.
     */
    if (dyn_f == 14) {
        /*
         * we have a bitmap rather than a run-encoding.
         */
        u_int buf;

        bp = 0;
        
        for (i=0; i < ch; i++) {
            for (j=0; j<cw; j++) {
                if(!(bp & 7))
                    buf = getc(pkfp);
				pr_put(pr, j, i, (buf & bit_mask[bp&7])?1:0);

				
				bp++;
			}
		}

	} else {

		bp = _pk_repeat = rc = rowp = 0;
		black = ptr->where.flags & (1 << 3);
		bits = cw * ch;
		CLEAR_NYB;
		while (bp < bits) {
			GET_PACKED(j);
			
			if (_pk_repeat != 0) {
				rc = _pk_repeat;
				rowp = bp / cw;
				_pk_repeat = 0;
				continue;
			}
			
			/*
			 * we have a run count in j.
			 */
			if (black) {
				register int k,l,m;
				l = bp / cw;            /* starting row */
				m = (bp + j) / cw;      /* ending row */
				k = bp % cw;		/* start bit offset */
				i = (bp + j) % cw;	/* ending bit offset */
				
				if (j <= cw && (k < i || i == 0)) {
					/* we're changing less than a row */
					pr_rop(pr, k, l, j, 1, PIX_SET,
					       NULL, 0, 0);
				} else {
					/* fill any fragment in the current row */
					pr_rop(pr, k, l, cw-k, 1, PIX_SET,
					       NULL, 0, 0);

					/* fill some number of full rows */
					pr_rop(pr, 0, l+1, cw, (j-(cw-k))/cw,  PIX_SET, NULL, 0, 0);
					/* fill any fragment in the last row */
					if (i)
						pr_rop(pr, 0, m, i, 1, PIX_SET,
						       NULL, 0, 0);
}
			      }
			bp += j;
			
			/*
			 * if there's a repeat count and we hit the end of
			 * a row, do the copy.
			 */
			if (rc && (bp - (rowp*cw)) >= cw) {
				i = rowp+1;
				j = rowp+1+rc;
				if ((i*cw) != bp) {
					pr_rop(pr, 0, j, cw, 1, PIX_SRC,
					       pr, 0, i);
				}
				j = rc;
				while(j--) {
					pr_rop(pr, 0, i, cw, 1, PIX_SRC,
					       pr, 0, rowp);
					i++;
				}
				bp += rc * cw;
				rc = 0;
			}
			black = !black;
		      }
	}

	ptr->where.isloaded = TRUE;
}


/*-->SetChar*/
/**********************************************************************/
/*****************************  SetChar  ******************************/
/**********************************************************************/
void
SetChar(c, command)
int c, command;
{
    register struct char_entry *ptr;  /* temporary char_entry pointer */
#ifndef	UCI
    int k;
#endif

    ptr = &(fontptr->ch[c]);
    hh = PixRound(h, hconv);
    vv = PixRound(v, vconv);
    if (!ptr->where.isloaded) LoadAChar(ptr);
    pr_rop(display, hh-ptr->xOffset-xscreen, vv-ptr->yOffset-yscreen, ptr->width, ptr->height, draw_mode, ptr->where.address.pixrectptr, 0, 0);

    if (command <= SET4)
		h += ptr->tfmw;

}


/*-->SetFntNum*/
/**********************************************************************/
/****************************  SetFntNum  *****************************/
/**********************************************************************/

void
SetFntNum(k)
int k;

/*  this routine is used to specify the font to be used in printing future
    characters */

{
  fontptr = hfontptr;
  while ((fontptr!=NULL) && (fontptr->k!=k))
      fontptr = fontptr->next;
  if (fontptr == NULL)
      Fatal("font %d undefined", k);
}


/*-->SetRule*/
/**********************************************************************/
/*****************************  SetRule  ******************************/
/**********************************************************************/

void
SetRule(a, b, Set)
int a, b;
BOOLEAN Set;

{           /*   this routine will draw a rule on the screen */
    int ehh, evv;
    hh = PixRound(h, hconv);
    vv = PixRound(v-a, vconv);
#ifndef	UCI
    ehh = PixRound(h + b, hconv);
    evv = PixRound(v, vconv);
#else
#define	rulepix(x, conv)	(((x) + (conv)-1) / (conv))
    ehh = rulepix(b, hconv) + hh;
    evv = rulepix(a, vconv) + vv;
#endif	UCI
    if (hh == ehh) ehh++;
    if (vv == evv) vv--;
    if ((a > 0) && (b > 0))
        pr_rop(display, hh-xscreen, vv-yscreen, ehh-hh, evv-vv, set_mode, NULL, 0, 0);
    if (Set) {
        h += b;
/*        v += a; */
    }
}

/*-->SkipFontDef*/
/**********************************************************************/
/****************************  SkipFontDef  ***************************/
/**********************************************************************/
/* ARGSUSED */
void
SkipFontDef(k)
int k;
{
    int a, l;
    char n[STRSIZE];

    NoSignExtend(dvifp, 4);
    NoSignExtend(dvifp, 4);
    NoSignExtend(dvifp, 4);
    a = NoSignExtend(dvifp, 1);
    l = NoSignExtend(dvifp, 1);
    GetBytes(dvifp, n, a+l);
}


/*-->Warning*/
/**********************************************************************/
/*****************************  Warning  ******************************/
/**********************************************************************/
/* VARARGS */
void
Warning(fmt, args)  /* issue a warning */
char *fmt;      /* format   */
char *args;     /* arguments */
{
    if (G_logging == 0)
    {
        G_logfp=fopen(G_Logname,"w+");
        G_logging = 1;
#ifndef	UCI
        if (G_logfp == NULL) G_logging = -1;
#else
	if (G_logfp == NULL) {
	    G_logging = -1;
	    Fatal("Unable to open log file\n");
	}
#endif	UCI
    }

    G_errenc = TRUE;
    if (G_logging == 1)
    {
        _doprnt(fmt, &args, G_logfp);
        fprintf(G_logfp,"\n");
    }
}