home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / DVIM72-Mac 1.9.6 / source / dvifile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-14  |  30.7 KB  |  1,066 lines  |  [TEXT/R*ch]

  1. /* -*-C-*- dvifile.h */
  2. /*-->dvifile*/
  3. #define JDEB 0
  4. #include "dvihead.h"
  5. #include "commands.h"
  6. #include "gendefs.h"
  7. #include "gblprocs.h"
  8. #include "egblvars.h"
  9. #include "m72.h"
  10. #include "mac-specific.h"
  11. #include "sillymalloc.h"
  12.  
  13. void rollbeachball( void );
  14. /**********************************************************************/
  15. /****************************** dvifile.c *******************************/
  16. /**********************************************************************/
  17.  
  18. #if OS_THINKC
  19. void
  20. dvifile( filestr )
  21. char* filestr;            /* DVI filename to process */
  22.  
  23. #else /* not OS_THINKC */
  24. void
  25. dvifile(argc,argv,filestr)
  26. int   argc;            /* argument count */
  27. char* argv[];            /* argument strings */
  28. char* filestr;            /* DVI filename to process */
  29. #endif /* OS_THINKC */
  30. {
  31.  
  32. #define PAGENUMBER(p) ((p) < 0 ? (page_count + 1 + (p)) : (p))
  33.  
  34.     register INT16 i,j,m;    /* loop indices */
  35.     register INT16 swap;    /* swap storage */
  36.  
  37.     INT16 m_begin,m_end,m_step;    /* loop limits and increment */
  38.     INT16 list[3];        /* index list for sort */
  39.     char tempstr[MAXSTR];    /* temporary string storage */
  40.  
  41.  
  42.     /*
  43.     Establish the default  font file  search order.  This  is done  here
  44.     instead of  in  initglob()  or  option(),  because  both  could  set
  45.     fontlist[].
  46.  
  47.     By default, the search list contains  names for the PK, GF, and  PXL
  48.     font  files;  that  order   corresponds  to  increasing  size   (and
  49.     therefore, presumably, increasing  processing cost)  of font  raster
  50.     information.  This search  order may  be overridden at  run time  by
  51.     defining an alternate one in the environment variable FONTLIST;  the
  52.     order of  the  strings  "PK",  "GF",  and  "PXL"  in  that  variable
  53.     determines the search  order, and  letter case  is NOT  significant.
  54.     The  substrings  may  be  separated   by  zero  or  more   arbitrary
  55.     characters, so  the  values  "PK-GF-PXL", "PK  GF  PXL",  "PKGFPXL",
  56.     "PK;GF;PXL", "PK/GF/PXL"  are all  acceptable,  and all  choose  the
  57.     default search  order.   This  flexibility  in  separator  character
  58.     choice is occasioned  by the  requirement on some  systems that  the
  59.     environment variable have the syntax of a file name, or be a  single
  60.     "word".  If  any  substring  is  omitted, then  that  font  will  be
  61.     excluded from consideration.  Thus, "GF" permits the use only of  GF
  62.     font files.
  63.  
  64.     The indexes gf_index, pk_index, and pxl_index are in -1 .. 2, and at
  65.     least one must be non-negative.  A negative index excludes that font
  66.     file type from the search.
  67.     */
  68.  
  69.  
  70.     /* Note that non-negative entries in list[] are UNIQUE. */
  71.     list[0] = gf_index = (INT16)strid2(fontlist,"GF");
  72.     list[1] = pk_index = (INT16)strid2(fontlist,"PK");
  73.     list[2] = pxl_index = (INT16)strid2(fontlist,"PXL");
  74.  
  75.     for (i = 0; i < 3; ++i)    /* put list in non-decreasing order */
  76.     for (j = i+1; j < 3; ++j)
  77.         if (list[i] > list[j])
  78.         {
  79.         swap = list[i];
  80.         list[i] = list[j];
  81.         list[j] = swap;
  82.         }
  83.     for (i = 0; i < 3; ++i)    /* assign indexes 0,1,2 */
  84.     if (list[i] >= 0)
  85.     {
  86.             if (list[i] == gf_index)
  87.         gf_index = i;
  88.             else if (list[i] == pk_index)
  89.         pk_index = i;
  90.             else if (list[i] == pxl_index)
  91.         pxl_index = i;
  92.     }
  93.  
  94.     if ((gf_index < 0) && (pk_index < 0) && (pxl_index < 0))
  95.     (void)fatal("dvifile():  FONTLIST does not define at least one of \
  96. GF, PK, or PXL fonts");
  97.  
  98.     (void)dviinit(filestr);    /* initialize DVI file processing */
  99.     if (g_abort_dvi)
  100.         return;
  101. #if OS_THINKC
  102. #else
  103.     (void)devinit(argc,argv);    /* initialize device output */
  104. #endif /* OS_THINKC */
  105.  
  106.     (void)readpost();
  107.     if (g_abort_dvi)
  108.     {
  109.         (void)dviterm();        /* terminate DVI file processing */
  110.         (void)Close_printer();        /* terminate device output */
  111.         return;
  112.     }
  113.     rollbeachball(); /* OS_THINKC */
  114.     (void)FSEEK(dvifp, 14L, 0); /* position past preamble */
  115.     (void)getbytes(dvifp, tempstr,
  116.     (BYTE)nosignex(dvifp,(BYTE)1)); /* flush DVI comment */
  117.  
  118.     cur_page_number = 0;
  119.  
  120.  
  121. #if    (HPLASERJET|HPJETPLUS|GOLDENDAWNGL100|POSTSCRIPT|IMPRESS|CANON_A2)
  122.     /* print pages in reverse order because of laser printer */
  123.     /* page stacking */
  124.     if (backwards)
  125.     {
  126.     m_begin = 1;
  127.     m_end = page_count;
  128.     m_step = 1;
  129.     }
  130.     else    /* normal device order */
  131.     {
  132.     m_begin = page_count;
  133.     m_end = 1;
  134.     m_step = -1;
  135.     }
  136.  
  137. #else
  138.   /* NOT (HPLASERJET|HPJETPLUS|GOLDENDAWNGL100|POSTSCRIPT|IMPRESS|CANON_A2) */
  139.     /* print pages in forward order for most devices */
  140.     if (backwards)
  141.     {
  142.     m_begin = page_count;
  143.     m_end = 1;
  144.     m_step = -1;
  145.     }
  146.     else
  147.     {
  148.     m_begin = 1;
  149.     m_end = page_count;
  150.     m_step = 1;
  151.     }
  152. #endif /* (HPLASERJET|HPJETPLUS|GOLDENDAWNGL100|POSTSCRIPT|IMPRESS|CANON_A2) */
  153.  
  154.     for (i = 0; i < npage; ++i)        /* make page numbers positive */
  155.     {                    /* and order pairs non-decreasing */
  156.     page_begin[i] = PAGENUMBER(page_begin[i]);
  157.     page_end[i] = PAGENUMBER(page_end[i]);
  158.     if (page_begin[i] > page_end[i])
  159.     {
  160.         swap = page_begin[i];
  161.         page_begin[i] = page_end[i];
  162.         page_end[i] = swap;
  163.     }
  164.     }
  165.     rollbeachball(); /* OS_THINKC */
  166.  
  167.     for (m = m_begin; ; m += m_step)
  168.     {
  169. #if OS_THINKC
  170.         if (g_abort_dvi)
  171.             break;
  172. #endif /* OS_THINKC */
  173.         for (i = 0; i < npage; ++i)    /* search page list */
  174.             if ( IN(page_begin[i],m,page_end[i]) &&
  175.                 (((m - page_begin[i]) % page_step[i]) == 0) )
  176.             {
  177.                 if (!quiet)        /* start progress report */
  178.                 {
  179.                        (void)printf("[%d", m);
  180.                        /* (void) fflush(stderr); */
  181.                    }
  182.                 cur_page_number++;/* sequential page number 1..N */
  183.                 cur_index = m-1;    /* remember index globally */
  184.                 prtpage(page_ptr[cur_index]);
  185.                 if (!quiet)        /* finish progress report */
  186.                     (void)printf("] "); /* (void)fflush(stderr); */
  187.                 break;
  188.             }
  189.         if ( (m == m_end) || g_abort_dvi )    /* exit loop after last page */
  190.             break;
  191.     }
  192.  
  193.     rollbeachball(); /* OS_THINKC */
  194.  
  195.     (void)dviterm();        /* terminate DVI file processing */
  196.     (void)Close_printer();        /* terminate device output */
  197.  
  198. }
  199.  
  200. /* -*-C-*- readpost.h */
  201. /*-->readpost*/
  202. /**********************************************************************/
  203. /****************************** readpost.h ******************************/
  204. /**********************************************************************/
  205.  
  206. void
  207. readpost()
  208.  
  209. /***********************************************************************
  210. This routine is used  to read in the  postamble values.  It  initializes
  211. the magnification and checks the stack height prior to starting printing
  212. the document.
  213. ***********************************************************************/
  214.  
  215. {
  216.     long lastpageptr;        /* byte pointer to last physical page */
  217.     int the_page_count;        /* page count from DVI file */
  218.  
  219.     findpost();
  220.     if (g_abort_dvi)
  221.         return;
  222.     if ((BYTE)nosignex(dvifp,(BYTE)1) != POST)
  223.     {
  224.         (void)Kill_dvi("readpost():  POST missing at head of postamble");
  225.         return;
  226.     }
  227.     lastpageptr = (long)nosignex(dvifp,(BYTE)4);
  228.     num = (UNSIGN32)nosignex(dvifp,(BYTE)4);
  229.     den = (UNSIGN32)nosignex(dvifp,(BYTE)4);
  230.     mag = (UNSIGN32)nosignex(dvifp,(BYTE)4);
  231.     conv = ((float)num/(float)den) *
  232.        ((float)runmag/(float)STDMAG) *
  233.  
  234. #if    USEGLOBALMAG
  235.         actfact(mag) *
  236. #endif
  237.  
  238.         ((float)RESOLUTION/254000.0);
  239.     /* denominator/numerator here since will be dividing by the conversion
  240.        factor */
  241.     (void) nosignex(dvifp,(BYTE)4);    /* height-plus-depth of tallest page */
  242.     (void) nosignex(dvifp,(BYTE)4);    /* width of widest page */
  243.     if ((int)nosignex(dvifp,(BYTE)2) >= STACKSIZE)
  244.     {
  245.         (void)Kill_dvi("readpost():  Stack size is too small");
  246.         return;
  247.     }
  248.  
  249.     the_page_count = (int)nosignex(dvifp,(BYTE)2);
  250.  
  251.     if (!quiet)
  252.     {
  253.         (void)printf("[%d pages]\n",the_page_count);
  254.     
  255.         (void)printf("[%d magnification, fonts %ddpi, printer %ddpi]\n",
  256.             (int)runmag, g_dpi, g_printer_dpi);
  257.     }
  258.  
  259.     if (preload)
  260.         getfntdf();
  261.     if (!g_abort_dvi)
  262.         getpgtab(lastpageptr);
  263. }
  264.  
  265. /* -*-C-*- findpost.h */
  266. /*-->findpost*/
  267. /**********************************************************************/
  268. /****************************** findpost.h ******************************/
  269. /**********************************************************************/
  270.  
  271. void
  272. findpost()
  273.  
  274. {
  275.     register long       postambleptr;
  276.     register BYTE       i;
  277.     register UNSIGN16 the_char;         /* loop index */
  278.  
  279.     (void) FSEEK (dvifp, 0L, 2);        /* goto end of file */
  280.  
  281.     /* VAX VMS binary files are stored with NUL padding to the next
  282.     512 byte multiple.  We therefore search backwards to the last
  283.     non-NULL byte to find the real end-of-file, then move back from
  284.     that.  Even if we are not on a VAX VMS system, the DVI file might
  285.     have passed through one on its way to the current host, so we
  286.     ignore trailing NULs on all machines. */
  287.  
  288.     while (FSEEK(dvifp,-1L,1) == 0)
  289.     {
  290.         the_char = (UNSIGN16)fgetc(dvifp);
  291.         if (the_char)
  292.           break;                /* exit leaving pointer PAST last non-NUL */
  293.         UNGETC((char)the_char,dvifp);
  294.     }
  295.  
  296.     postambleptr = FTELL(dvifp) - 4;
  297.  
  298. #if    OS_VAXVMS
  299.     /* There is a problem with FSEEK() that I cannot fix just now.  A
  300.     request to position to end-of-file cannot be made to work correctly,
  301.     so FSEEK() positions in front of the last byte, instead of past it.
  302.     We therefore modify postambleptr accordingly to account for this. */
  303.  
  304.     postambleptr++;
  305. #endif
  306.  
  307.     (void) FSEEK (dvifp, postambleptr, 0);
  308.     while (TRUE)
  309.     {
  310.         (void) FSEEK (dvifp, --(postambleptr), 0);
  311. #if JDEB
  312.     printf("[nosignex(dvi)");
  313. #endif
  314.         rollbeachball(); /* OS_THINKC */
  315.         if (((i = (BYTE)nosignex(dvifp,(BYTE)1)) != 223) &&
  316.             (i != DVIFORMAT))
  317.          {
  318.              Kill_dvi( "findpost():  Bad end of DVI file" );
  319.              return;
  320.          }
  321.          if (i == DVIFORMAT)
  322.             break;
  323.     }
  324.     (void) FSEEK (dvifp, postambleptr - 4, 0);
  325. #if JDEB
  326.     printf("[nosignex(dvi)");
  327. #endif
  328.     (void) FSEEK (dvifp, (long)nosignex(dvifp,(BYTE)4), 0);
  329. }
  330.  
  331.  
  332.  
  333. /* -*-C-*- getfntdf.h */
  334. /*-->getfntdf*/
  335. /**********************************************************************/
  336. /****************************** getfntdf.h ******************************/
  337. /**********************************************************************/
  338.  
  339. void
  340. getfntdf()
  341.  
  342. /***********************************************************************
  343. Read the font definitions as they are in the postamble of the DVI  file.
  344. Note that the font directory is not yet loaded.
  345. ***********************************************************************/
  346.  
  347. {
  348.     register BYTE    byte;
  349.  
  350. #if JDEB
  351.     printf("[nosignex(dvi) getfntdf");
  352. #endif
  353.     while (((byte = (BYTE)nosignex(dvifp,(BYTE)1)) >= FNT_DEF1)
  354.         && (byte <= FNT_DEF4))
  355.     {
  356.         if (User_wants_out())
  357.             return;
  358.         readfont ((INT32)nosignex(dvifp,(BYTE)(byte-FNT_DEF1+1)));
  359.         if (g_abort_dvi)
  360.             return;
  361.     }
  362.     if (byte != POST_POST)
  363.         Kill_dvi ("getfntdf():  POST_POST missing after fontdefs");
  364. }
  365.  
  366.  
  367. /* -*-C-*- readfont.h */
  368. /*-->readfont*/ /* called by getfntdef */
  369. /**********************************************************************/
  370. /****************************** readfont.h ******************************/
  371. /**********************************************************************/
  372.  
  373. void
  374. readfont(font_k)
  375. INT32 font_k;
  376. {
  377.     BYTE a, l;
  378.     UNSIGN32 c;                /* checksum */
  379.     UNSIGN32 d;                /* design size */
  380.     char n[MAXSTR];
  381.     UNSIGN32 s;                /* scale factor */
  382.     struct font_entry *tfontptr;     /* temporary font_entry pointer */
  383.  
  384. #if JDEB
  385.     printf("[nosignex(dvi) readfont");
  386. #endif
  387.     c = (UNSIGN32)nosignex(dvifp,(BYTE)4);
  388.     s = (UNSIGN32)nosignex(dvifp,(BYTE)4);
  389.     d = (UNSIGN32)nosignex(dvifp,(BYTE)4);
  390.     a = (BYTE)nosignex(dvifp,(BYTE)1);
  391.     l = (BYTE)nosignex(dvifp,(BYTE)1);
  392.     (void)getbytes(dvifp, n, (BYTE)(a+l));
  393.     n[a+l] = '\0';
  394.     tfontptr = (struct font_entry*)MALLOC((unsigned)sizeof(struct font_entry));
  395.     if (tfontptr == (struct font_entry *)NULL)
  396.     {
  397.         Kill_dvi("readfont():  No allocable memory space left for font_entry");
  398.         return;
  399.     }
  400.     tfontptr->next = hfontptr;
  401.  
  402.     fontptr = hfontptr = tfontptr;
  403.     fontptr->k = font_k;
  404.     fontptr->c = c;
  405.     fontptr->s = s;
  406.     fontptr->d = d;
  407.     fontptr->a = a;
  408.     fontptr->l = l;
  409.     fontptr->font_file_id = NULL; /* JWW fixed subtle bug */
  410.     (void)strcpy(fontptr->n, n);
  411.     fontptr->font_space = (INT32)(s/6);
  412.     (void)reldfont(fontptr);
  413. }
  414.  
  415. /* -*-C-*- reldfont.h */
  416. /*-->reldfont*/
  417. /**********************************************************************/
  418. /****************************** reldfont.h ******************************/
  419. /**********************************************************************/
  420.  
  421. void
  422. reldfont(tfontptr)        /* load (or reload) font parameters */
  423. struct font_entry *tfontptr;
  424. {
  425.     register UNSIGN16 the_char;    /* loop index */
  426.     int err_code;
  427.     register struct char_entry *tcharptr;/* temporary char_entry pointer */
  428.  
  429.     tfontptr->font_mag = (UNSIGN32)((actfact(
  430.         MAGSIZE((float)tfontptr->s/(float)tfontptr->d)) *
  431.     ((float)runmag/(float)STDMAG) *
  432.  
  433. #if    USEGLOBALMAG
  434.     actfact(mag) *
  435. #endif
  436.  
  437.     (float)RESOLUTION * 5.0) + 0.5);
  438.  
  439.     tfontptr->designsize = (UNSIGN32)0L;
  440.     tfontptr->hppp = (UNSIGN32)0L;
  441.     tfontptr->vppp = (UNSIGN32)0L;
  442.     tfontptr->min_m = (INT32)0L;
  443.     tfontptr->max_m = (INT32)0L;
  444.     tfontptr->min_n = (INT32)0L;
  445.     tfontptr->max_n = (INT32)0L;
  446.  
  447.     for (the_char = FIRSTPXLCHAR; the_char <= LASTPXLCHAR; the_char++)
  448.     {
  449.     tcharptr = &(tfontptr->ch[the_char]);
  450.     tcharptr->dx = (INT32)0L;
  451.     tcharptr->dy = (INT32)0L;
  452.     tcharptr->hp = (COORDINATE)0;
  453.     tcharptr->fontrp = -1L;
  454.     tcharptr->pxlw = (UNSIGN16)0;
  455.     tcharptr->rasters = (UNSIGN32*)NULL;
  456.     tcharptr->refcount = 0;
  457.     tcharptr->tfmw = 0L;
  458.     tcharptr->wp = (COORDINATE)0;
  459.     tcharptr->xoffp = (COORDINATE)0;
  460.     tcharptr->yoffp = (COORDINATE)0;
  461.     }
  462.  
  463.     if (tfontptr != pfontptr)
  464.     (void)openfont(tfontptr->n);
  465.  
  466.     if (fontfp == (FILE *)NULL)    /* have empty font with zero metrics */
  467.     return;
  468.  
  469.     for (;;)        /* fake one-trip loop */
  470.     {    /* test for font types PK, GF, and PXL in order of preference */
  471.         (void)REWIND(fontfp);    /* position to beginning-of-file */
  472. #if JDEB
  473.     printf("[nosignex(font) reldfont");
  474. #endif
  475.         if ( ((BYTE)nosignex(fontfp,(BYTE)1) == (BYTE)PKPRE) &&
  476.             ((BYTE)nosignex(fontfp,(BYTE)1) == (BYTE)PKID) )
  477.         {
  478.             tfontptr->font_type = (BYTE)FT_PK;
  479.             tfontptr->charxx = (void(*)())charpk;
  480.             err_code = readpk();
  481.             if (g_abort_dvi)
  482.                 return;
  483.             break;
  484.         }
  485.  
  486.         (void)REWIND(fontfp);    /* position to beginning-of-file */
  487. #if JDEB
  488.     printf("[nosignex(font) reldfont");
  489. #endif
  490. #if !OS_THINKC /* GF fonts? We don't need no stinking GF fonts! */
  491.         if ( ((BYTE)nosignex(fontfp,(BYTE)1) == (BYTE)GFPRE) &&
  492.         ((BYTE)nosignex(fontfp,(BYTE)1) == (BYTE)GFID) )
  493.         {
  494.         tfontptr->font_type = (BYTE)FT_GF;
  495.         tfontptr->charxx = (void(*)())chargf;
  496.         err_code = readgf();
  497.         break;
  498.         }
  499.  
  500.     (void)REWIND(fontfp);    /* position to beginning-of-file */
  501. #if JDEB
  502.     printf("[nosignex(font) reldfont");
  503. #endif
  504.     if ((UNSIGN32)nosignex(fontfp,(BYTE)4) == (UNSIGN32)PXLID)
  505.     {
  506.         tfontptr->font_type = (BYTE)FT_PXL;
  507.         tfontptr->charxx = (void(*)())charpxl;
  508.         err_code = readpxl();
  509.         break;
  510.     }
  511. #endif /* not OS_THINKC */
  512.  
  513.     err_code = (int)EOF;
  514.     break;
  515.     } /* end one-trip loop */
  516.  
  517.     if (err_code)
  518.     {
  519.         (void)sprintf(message,
  520.         "reldfont():  Font file [%s] is not a valid GF, PK, or PXL file",
  521.         tfontptr->name);
  522.         Kill_dvi(message);
  523.     }
  524. }
  525.  
  526.  
  527. /* -*-C-*- readpk.h */
  528. /*-->readpk*/
  529. /**********************************************************************/
  530. /******************************* readpk *******************************/
  531. /**********************************************************************/
  532.  
  533. int
  534. readpk()    /* return 0 on success, EOF on failure */
  535. {
  536.     UNSIGN32 checksum;
  537.     long end_of_packet;    /* pointer to byte following character packet */
  538.     /* register */ BYTE flag_byte;
  539.     UNSIGN32 packet_length;
  540.     long start_of_packet;    /* pointer to start of character packet */
  541.     register struct char_entry *tcharptr;/* temporary char_entry pointer */
  542.     register UNSIGN32 the_char;
  543.  
  544.     /* Read a PK file, extracting character metrics and raster
  545.        locations. */
  546.     /******************************************************************/
  547.  
  548.     /* Process the preamble parameters */
  549.  
  550.     (void)REWIND(fontfp);    /* start at beginning of file */
  551.     if ((BYTE)nosignex(fontfp,(BYTE)1) != PKPRE)
  552.     {
  553.     (void)warning("readpk():  PK font file does not start with PRE byte");
  554.     return(EOF);
  555.     }
  556.     if ((BYTE)nosignex(fontfp,(BYTE)1) != PKID)
  557.     {
  558.     (void)warning(
  559.         "readpk():  PK font file PRE byte not followed by ID byte");
  560.     return(EOF);
  561.     }
  562.  
  563. #if JDEB
  564.     printf("[nosignex(font) readpk");
  565. #endif
  566.     if (FSEEK(fontfp,(long)nosignex(fontfp,(BYTE)1),1))
  567.     {
  568.     (void)warning("readpk():  PK font file has garbled comment string");
  569.     return(EOF);        /* skip comment string */
  570.     }
  571.  
  572. #if JDEB
  573.     printf(" (seek rel) [nosignex(font) readpk");
  574. #endif
  575.     fontptr->designsize = (UNSIGN32)nosignex(fontfp,(BYTE)4);
  576.  
  577. #if JDEB
  578.     printf("[nosignex(font) readpk");
  579. #endif
  580.     checksum = (UNSIGN32)nosignex(fontfp,(BYTE)4);    /* checksum */
  581.     if ((fontptr->c != 0L) && (checksum != 0L) && (fontptr->c != checksum))
  582.     {
  583.     (void)sprintf(message,
  584.     "readpk():  font [%s] has checksum = 10#%010lu [16#%08lx] [8#%011lo] \
  585. different from DVI checksum = 10#%010lu [16#%08lx] [8#%011lo].  \
  586. TeX preloaded .fmt file is probably out-of-date with respect to new fonts.",
  587.         fontptr->name, fontptr->c, fontptr->c, fontptr->c,
  588.         checksum, checksum, checksum);
  589.     (void)warning(message);
  590.     }
  591.  
  592. #if JDEB
  593.     printf("[nosignex(font) readpk");
  594. #endif
  595.     fontptr->hppp = (UNSIGN32)nosignex(fontfp,(BYTE)4);
  596. #if JDEB
  597.     printf("[nosignex(font) readpk");
  598. #endif
  599.     fontptr->vppp = (UNSIGN32)nosignex(fontfp,(BYTE)4);
  600.  
  601.     fontptr->min_m = 0L;    /* these are unused in PK format */
  602.     fontptr->max_m = 0L;
  603.     fontptr->min_n = 0L;
  604.     fontptr->max_n = 0L;
  605.  
  606.     fontptr->magnification = (UNSIGN32)( 0.5 + 5.0 * 72.27 *
  607.     (float)(fontptr->hppp) / 65536.0 );    /* from GFtoPXL Section 53 */
  608.  
  609.     /******************************************************************/
  610.     /* Process the characters until the POST byte is reached */
  611.  
  612.     (void)skpkspec();    /* skip any PK specials */
  613.     if (g_abort_dvi)
  614.         return;
  615.     start_of_packet = (long)FTELL(fontfp);
  616. #if JDEB
  617.     printf("[nosignex(font) readpk (start packet = %ld)", start_of_packet);
  618. #endif
  619.     while ((flag_byte = (BYTE)nosignex(fontfp,(BYTE)1)) != PKPOST)
  620.     {
  621.         rollbeachball(); /* OS_THINKC */
  622.     flag_byte &= 0x07;
  623.     switch(flag_byte)    /* flag_byte is in 0..7 */
  624.     {
  625.     case 0:
  626.     case 1:
  627.     case 2:
  628.     case 3:        /* short character preamble */
  629.         packet_length = (UNSIGN32)flag_byte;
  630.         packet_length <<= 8;
  631.         packet_length += (UNSIGN32)nosignex(fontfp,(BYTE)1);
  632. #if JDEB
  633.     printf("\n(case 3 packet_length = %ld)\n", packet_length);
  634. #endif
  635.         the_char = (UNSIGN32)nosignex(fontfp,(BYTE)1);
  636.         if (the_char >= NPXLCHARS)
  637.         {
  638.         (void)warning(
  639.         "readpk():  PK font file character number is too big for me");
  640.             return(EOF);
  641.         }
  642.         tcharptr = &(fontptr->ch[the_char]);
  643.         end_of_packet = (long)FTELL(fontfp) + (long)packet_length;
  644. #if JDEB
  645.     printf("[nosignex(font) readpk");
  646. #endif
  647.         tcharptr->tfmw = (UNSIGN32)(((float)nosignex(fontfp,(BYTE)3) *
  648.         (float)fontptr->s) / (float)(1L << 20));
  649.         tcharptr->dx = (INT32)nosignex(fontfp,(BYTE)1) << 16;
  650.         tcharptr->dy = 0L;
  651.         tcharptr->pxlw = (UNSIGN16)PIXROUND(tcharptr->dx, 1.0/65536.0);
  652.         tcharptr->wp = (COORDINATE)nosignex(fontfp,(BYTE)1);
  653.         tcharptr->hp = (COORDINATE)nosignex(fontfp,(BYTE)1);
  654.         tcharptr->xoffp = (COORDINATE)signex(fontfp,(BYTE)1);
  655.         tcharptr->yoffp = (COORDINATE)signex(fontfp,(BYTE)1);
  656.         break;
  657.  
  658.     case 4:
  659.     case 5:
  660.     case 6:        /* extended short character preamble */
  661.         packet_length = (UNSIGN32)(flag_byte & 0x03);
  662.         packet_length <<= 16;
  663. #if JDEB
  664.         printf("(pre packet_length = %ld)\n", packet_length);
  665. #endif
  666.         packet_length += (UNSIGN32)nosignex(fontfp,(BYTE)2);
  667. #if JDEB
  668.     printf("[nosignex(font) readpk (case 6 packet_length = %ld)\n",
  669.         packet_length);
  670.     printf("(flag_byte = %ld)\n", flag_byte);
  671. #endif
  672.         the_char = (UNSIGN32)nosignex(fontfp,(BYTE)1);
  673.         if (the_char >= NPXLCHARS)
  674.         {
  675.         (void)warning(
  676.         "readpk():  PK font file character number is too big for me");
  677.             return(EOF);
  678.         }
  679.         tcharptr = &(fontptr->ch[the_char]);
  680.         end_of_packet = (long)FTELL(fontfp) + (long)packet_length;
  681. #if JDEB
  682.     printf("[nosignex(font) readpk");
  683. #endif
  684.         tcharptr->tfmw = (UNSIGN32)(((float)nosignex(fontfp,(BYTE)3) *
  685.         (float)fontptr->s) / (float)(1L << 20));
  686.         tcharptr->dx = (INT32)nosignex(fontfp,(BYTE)2) << 16;
  687.         tcharptr->dy = 0L;
  688.         tcharptr->pxlw = (UNSIGN16)PIXROUND(tcharptr->dx, 1.0/65536.0);
  689.         tcharptr->wp = (COORDINATE)nosignex(fontfp,(BYTE)2);
  690.         tcharptr->hp = (COORDINATE)nosignex(fontfp,(BYTE)2);
  691.         tcharptr->xoffp = (COORDINATE)signex(fontfp,(BYTE)2);
  692.         tcharptr->yoffp = (COORDINATE)signex(fontfp,(BYTE)2);
  693.         break;
  694.  
  695.     case 7:        /* long character preamble */
  696.         packet_length = (UNSIGN32)nosignex(fontfp,(BYTE)4);
  697. #if JDEB
  698.     printf("[nosignex(font) readpk (case 7 packet length= %ld)\n",
  699.         packet_length);
  700. #endif
  701. #if JDEB
  702.     printf("[nosignex(font) readpk (chars)");
  703. #endif
  704.         the_char = (UNSIGN32)nosignex(fontfp,(BYTE)4);
  705.         if (the_char >= NPXLCHARS)
  706.         {
  707.         (void)warning(
  708.         "readpk():  PK font file character number is too big for me");
  709.             return(EOF);
  710.         }
  711.         tcharptr = &(fontptr->ch[the_char]);
  712.         end_of_packet = (long)FTELL(fontfp) + (long)packet_length;
  713.         tcharptr->tfmw = (UNSIGN32)(((float)nosignex(fontfp,(BYTE)4) *
  714.         (float)fontptr->s) / (float)(1L << 20));
  715.         tcharptr->dx = (INT32)signex(fontfp,(BYTE)4);
  716.         tcharptr->dy = (INT32)signex(fontfp,(BYTE)4);
  717.         tcharptr->pxlw = (UNSIGN16)PIXROUND(tcharptr->dx, 1.0/65536.0);
  718.         tcharptr->wp = (COORDINATE)nosignex(fontfp,(BYTE)4);
  719.         tcharptr->hp = (COORDINATE)nosignex(fontfp,(BYTE)4);
  720.         tcharptr->xoffp = (COORDINATE)signex(fontfp,(BYTE)4);
  721.         tcharptr->yoffp = (COORDINATE)signex(fontfp,(BYTE)4);
  722.         break;
  723.     } /* end switch */
  724.     tcharptr->fontrp = start_of_packet;
  725. #if JDEB
  726.     printf("(seek abs %ld by %ld)\n", end_of_packet, packet_length);
  727. #endif
  728.     (void)FSEEK(fontfp,end_of_packet,0); /* position to end of packet */
  729.     (void)skpkspec();    /* skip any PK specials */
  730.     if (g_abort_dvi)
  731.         return;
  732.     start_of_packet = (long)FTELL(fontfp);
  733.     } /* end while */
  734.  
  735. #if    (BBNBITGRAPH | HPJETPLUS | POSTSCRIPT | IMPRESS | CANON_A2)
  736.     (void)newfont();
  737. #endif
  738.  
  739.     return(0);
  740. }
  741.  
  742. /* -*-C-*- skpkspec.h */
  743. /*-->skpkspec*/
  744. /**********************************************************************/
  745. /****************************** skpkspec ******************************/
  746. /**********************************************************************/
  747.  
  748. void
  749. skpkspec()    /* Skip PK font file specials */
  750. {
  751.     BYTE the_byte;
  752.  
  753. #if JDEB
  754.     printf("\nSkpkspec starting.\n");
  755. #endif
  756.  
  757. #if JDEB
  758.     printf("[nosignex(font) Skpkspec A");
  759. #endif
  760.     the_byte = (BYTE)nosignex(fontfp,(BYTE)1);
  761. #if JDEB
  762.     printf("(the_byte = %.2X)", the_byte);
  763. #endif
  764.     while ((the_byte >= (BYTE)PKXXX1) && (the_byte != PKPOST))
  765.     {
  766.     switch(the_byte)
  767.     {
  768.     case PKXXX1:
  769.         (void)FSEEK(fontfp,(long)nosignex(fontfp,(BYTE)1),1);
  770. #if JDEB
  771.         printf("PKXXX1\n");
  772. #endif
  773.         break;
  774.  
  775.     case PKXXX2:
  776.         (void)FSEEK(fontfp,(long)nosignex(fontfp,(BYTE)2),1);
  777. #if JDEB
  778.         printf("PKXXX2\n");
  779. #endif
  780.         break;
  781.  
  782.     case PKXXX3:
  783.         (void)FSEEK(fontfp,(long)nosignex(fontfp,(BYTE)3),1);
  784. #if JDEB
  785.         printf("PKXXX3\n");
  786. #endif
  787.         break;
  788.  
  789.     case PKXXX4:
  790.         (void)FSEEK(fontfp,(long)nosignex(fontfp,(BYTE)4),1);
  791. #if JDEB
  792.         printf("PKXXX4\n");
  793. #endif
  794.         break;
  795.  
  796.     case PKYYY:
  797.         (void)nosignex(fontfp,(BYTE)4);
  798. #if JDEB
  799.         printf("PKYYY\n");
  800. #endif
  801.         break;
  802.  
  803.     case PKNOOP:
  804. #if JDEB
  805.         printf("PKNOOP\n");
  806. #endif
  807.         break;
  808.  
  809.     default:
  810.         (void)sprintf(message,"skpkspec():  Bad PK font file [%s] [%hX]",
  811.         fontptr->name, the_byte);
  812.         Kill_dvi(message);
  813.         return;
  814.     }
  815.     the_byte = (BYTE)nosignex(fontfp,(BYTE)1);
  816. #if JDEB
  817.     printf("(the_byte = %.2X)", the_byte);
  818. #endif
  819.     }
  820. #if JDEB
  821.     printf("(ungetc)", the_byte);
  822. #endif
  823.     (void)UNGETC((char)the_byte,fontfp);    /* put back lookahead byte */
  824. #if JDEB
  825.     printf("\nSkpkspec ending.\n");
  826. #endif
  827. }
  828.  
  829. /* -*-C-*- nosignex.h */
  830. /*-->nosignex*/
  831. /**********************************************************************/
  832. /****************************** nosignex ******************************/
  833. /**********************************************************************/
  834.  
  835. UNSIGN32
  836. nosignex(fp, n)    /* return n byte quantity from file fd */
  837. register FILE *fp;    /* file pointer    */
  838. register BYTE n;    /* number of bytes (1..4) */
  839.  
  840. {
  841.     register UNSIGN32 number;    /* number being constructed */
  842.  
  843.     number = 0;
  844.     while (n--)
  845.     {
  846.         number <<= 8;
  847.         number |= getc(fp);
  848.     }
  849.     return(number);
  850. }
  851.  
  852. /* -*-C-*- signex.h */
  853. /*-->signex*/
  854. /**********************************************************************/
  855. /******************************* signex *******************************/
  856. /**********************************************************************/
  857.  
  858. INT32
  859. signex(fp, n)     /* return n byte quantity from file fd */
  860. register FILE *fp;    /* file pointer     */
  861. register BYTE n;      /* number of bytes */
  862.  
  863. {
  864.     register BYTE n1; /* number of bytes      */
  865.     register INT32 number; /* number being constructed */
  866.  
  867.     number = (INT32)getc(fp);/* get first (high-order) byte */
  868.  
  869. #if    PCC_20
  870.     if (number > 127) /* sign bit is set, pad top of word with sign bit */
  871.     number |= 0xfffffff00;
  872.               /* this constant could be written for any */
  873.               /* 2's-complement machine as -1 & ~0xff, but */
  874.               /* PCC-20 does not collapse constant expressions */
  875.               /* at compile time, sigh... */
  876. #endif /* PCC_20 */
  877.  
  878. #if    (OS_ATARI | OS_UNIX)    /* 4.1BSD C compiler uses logical shift too */
  879.     if (number > 127) /* sign bit is set, pad top of word with sign bit */
  880.     number |= 0xffffff00;
  881. #endif /* OS_UNIX */
  882.  
  883.     n1 = n--;
  884.     while (n--)
  885.     {
  886.     number <<= 8;
  887.     number |= (INT32)getc(fp);
  888.     }
  889.  
  890. #if    (OS_ATARI | PCC_20 | OS_UNIX)
  891. #else /* NOT (OS_ATARI | PCC_20 | OS_UNIX) */
  892.  
  893.     /* NOTE: This code assumes that the right-shift is an arithmetic, rather
  894.     than logical, shift which will propagate the sign bit right.   According
  895.     to Kernighan and Ritchie, this is compiler dependent! */
  896.  
  897.     number<<=HOST_WORD_SIZE-8*n1;
  898.  
  899. #if    ARITHRSHIFT
  900.     number>>=HOST_WORD_SIZE-8*n1;  /* sign extend */
  901. #else /* NOT ARITHRSHIFT */
  902.     (void)fatal("signex():  no code implemented for logical right shift");
  903. #endif /* ARITHRSHIFT */
  904.  
  905. #endif /* (OS_ATARI | PCC_20 | OS_UNIX) */
  906. #if JDEB
  907.     printf("[signex(%d) <%ld>]", n, (INT32)number);
  908. #endif
  909.     return((INT32)number);
  910. }
  911.  
  912.  
  913. /**********************************************************************/
  914. /****************************** dviterm *******************************/
  915. /**********************************************************************/
  916.  
  917. void
  918. dviterm()            /* terminate DVI file processing */
  919. {
  920.     register INT16 k;        /* loop index */
  921.  
  922.  
  923.     /* Close all files and free dynamically-allocated font space.  Stdin
  924.        is never closed, however, because it might be needed later for
  925.        ioctl(). */
  926.  
  927.     if ((dvifp != (FILE*)NULL) && (dvifp != stdin))
  928.         (void)fclose(dvifp);
  929.  
  930.     for (k = 1; k <= (INT16)nopen; ++k) /* k started at 1, said <= (JWW) */
  931.     {
  932.         if (font_files[k].font_id != (FILE *)NULL)
  933.         {
  934.             (void)fclose(font_files[k].font_id);
  935.             font_files[k].font_id = (FILE *)NULL;
  936.         }
  937.     }
  938.  
  939. #if OS_THINKC
  940.     sillyfree();
  941.     g_page_window->grafProcs = NIL;
  942. #else
  943.     fontptr = hfontptr;
  944.     while (fontptr != (struct font_entry *)NULL)
  945.     {
  946.         for (k = 0; k < NPXLCHARS; ++k)
  947.             if ((fontptr->ch[k].rasters) != (UNSIGN32 *)NULL)
  948.             (void)free((char *)fontptr->ch[k].rasters);
  949.         pfontptr = fontptr;
  950.         fontptr = fontptr->next;
  951.         (void)free((char *)pfontptr);
  952.     }
  953. #endif
  954.  
  955.     if (!quiet)
  956.     {
  957.         /* fflush(stdout); */
  958.         (void)printf(" [OK]");
  959.         printf("\n");
  960.     }
  961.  
  962. }
  963.  
  964. /**********************************************************************/
  965. /****************************** fontfile ******************************/
  966. /**********************************************************************/
  967.  
  968. void
  969. fontfile(filelist,font_path,font_name,magnification)
  970. char *filelist[MAXFORMATS];    /* output filename list */
  971. char *font_path;        /* host font file pathname */
  972. char *font_name;        /* TeX font_name */
  973. int magnification;        /* magnification value */
  974. {
  975.  
  976.     /*
  977.     Given a TeX font_name and a magnification value, construct a list of
  978.     system-dependent  host  filenames,  returning  them  in  the   first
  979.     argument.  The files are not guaranteed to exist.  The font names by
  980.     default contains names for the PK,  GF, and PXL font files, but  the
  981.     selection, and search order, may be changed at run time by  defining
  982.     a value for the environment  variable FONTLIST.  See initglob()  for
  983.     details.
  984.     */
  985.  
  986.     register int m;            /* index into filelist[] */
  987.     register INT16 k;            /* loop index */
  988.     float fltdpi;            /* dots/inch */
  989.     int dpi;                /* dots/inch for filename */
  990.     char *nameformat;        /* flags for possible font name formats */
  991.  
  992.     /*
  993.     We need to convert an old-style ROUNDED integer magnification based
  994.     on 1000 units = 200 dpi to an actual rounded dpi value.
  995.  
  996.     We have
  997.     magnification = round(real_mag) = trunc(real_mag + 0.5)
  998.     which implies
  999.     float(magnification) - 0.5 <= real_mag < float(magnification) + 0.5
  1000.  
  1001.     We want
  1002.     dpi = round(real_mag/5.0)
  1003.     which implies
  1004.     float(dpi) - 0.5 <= real_mag/5.0 < float(dpi) + 0.5
  1005.     or
  1006.     5.0*float(dpi) - 2.5 <= real_mag < 5.0*float(dpi) + 2.5
  1007.  
  1008.     We can combine the two to conclude that
  1009.     5.0*float(dpi) - 2.5 < float(magnification) + 0.5
  1010.     or
  1011.     5.0*float(dpi) - 3 < float(magnification)
  1012.     which gives the STRICT inequality
  1013.     float(dpi) < (float(magnification) + 3.0)/5.0
  1014.     */
  1015.  
  1016.     fltdpi = (((float)magnification) + 3.0)/5.0;
  1017.     dpi = (int)fltdpi;
  1018.     if (fltdpi == (float)dpi)
  1019.     dpi--;                /* enforce inequality */
  1020.  
  1021. #define MAXFTYPES 1
  1022.  
  1023.     for (k = 0; k < MAXFORMATS; ++k) /* loop over possible file types */
  1024.       *filelist[k] = '\0';    /* Initially, all filenames are empty */
  1025.       
  1026.     m = 0;                /* index in filelist[] */
  1027.     for (k = 0; k < MAXFTYPES; ++k) /* loop over possible file types */
  1028.     {
  1029.     
  1030.     /* Typical results:
  1031.         texfonts:72:cmr10.pk
  1032.         texfonts:72:cmr10
  1033.         texfonts:cmr10.72pk
  1034.         texfonts:72:cmr10.72pk
  1035.     */
  1036.  
  1037.     nameformat = *get_str_resource( "NAMEFORMAT" );
  1038.     /* if (k == pk_index) */
  1039.     if (nameformat[0] == '1')
  1040.         (void)sprintf(filelist[m++], "%s%s.%dpk",        /* cmr8.72pk */
  1041.             font_path, font_name, dpi );
  1042.     if (nameformat[2] == '1')
  1043.         (void)sprintf(filelist[m++], "%s%d:%s",            /* 72:cmr8 */
  1044.             font_path, dpi, font_name);
  1045.     if (nameformat[3] == '1')
  1046.         (void)sprintf(filelist[m++], "%s%d:%s.pk",        /* 72:cmr8.pk */
  1047.             font_path, dpi, font_name);
  1048.     if (nameformat[4] == '1')
  1049.         (void)sprintf( filelist[m++], "%s%d:%s.%dpk",    /* 72:cmr8.72pk */
  1050.             font_path, dpi, font_name, dpi );
  1051.     if (nameformat[6] == '1')
  1052.         (void)sprintf(filelist[m++], "%s%d/%s",            /* 72/cmr8 */
  1053.             font_path, dpi, font_name);
  1054.     if (nameformat[7] == '1')
  1055.         (void)sprintf(filelist[m++], "%s%d/%s.pk",        /* 72/cmr8.pk */
  1056.             font_path, dpi, font_name);
  1057.     if (nameformat[8] == '1')
  1058.         (void)sprintf( filelist[m++], "%s%d/%s.%dpk",    /* 72/cmr8.72pk */
  1059.             font_path, dpi, font_name, dpi );
  1060.     /* (void)printf("In fontfile: Path = %s\n",filelist[m - 1]); */
  1061.  
  1062.     }
  1063.  
  1064. }
  1065.  
  1066.