/* $Log:	prescanpage.c,v $
 * Revision 0.8  92/11/23  19:46:55  19:46:55  bt (Bo Thide')
 * Fixed resolution bug. Portable downloading. Added/changed options. PJXL color support
 * 
 * Revision 0.7  92/11/13  02:41:40  02:41:40  bt (Bo Thide')
 * More bug fixes and improvements. Support for PaintJet XL
 * 
 * Revision 0.6  92/11/10  21:48:37  21:48:37  bt (Bo Thide')
 * Bug fixes. Added -R option. Better font handling.
 * 
 * Revision 0.5  92/11/09  16:25:43  16:25:43  bt (Bo Thide')
 * Rewrite of dospecial.c. Extended \special support
 * 
 * Revision 0.4  92/11/08  02:46:00  02:46:00  bt (Bo Thide')
 * Changed to portable bit manipulations. Replaced strrstr for non-POSIX compliant C. Fixed numerous bugs. Added support for more \special's.
 * 
 * Revision 0.3  92/08/24  12:43:21  12:43:21  bt (Bo Thide')
 * Fixed 8 bit (dc font) support.
 * 
 * Revision 0.2  92/08/23  17:29:00  17:29:00  bt (Bo Thide')
 * Source cleaned up.  Changed certain function calls.  Removed globals.
 * 
 * Revision 0.1  92/08/22  23:58:48  23:58:48  bt (Bo Thide')
 * First Release.
 *  */

/*
 * This routine will scan a page from 'bop' to 'eop', maintaining information
 * about font usage. It also catches errors like illegal font numbers or
 * characters. Finally, it also counts the number of pages actually processed,
 * so if the file is scanned backwards, it will end correctly.
 */

#include <stdio.h>
#include "dvi.h"
#include "globals.h"
#include "macros.h"

static char rcsid[] = "$Header: prescanpage.c,v 0.8 92/11/23 19:46:55 bt Exp $";

prescanpage(bitfile, dvifile, pcllevel, resolution)
FILE	*bitfile;
FILE	*dvifile;
short	pcllevel, resolution;
{
  bool	badchar;
  int	k;
  int	q;
  int	font_num;
  int	font_used[MAXFONTS];
  long	p;
  long	hh=0L;				/* Pixel analog of DVI unit h */
  long	vv=0L; 				/* Pixel analog of DVI unit v */

  actualpagecount++;

  for(k = 0 ; k < MAXFONTS ; k++)
    font_used[k] = FALSE;
  font_num = 0;

  while (TRUE){
    if((k = (int)getubyte(dvifile)) < DVI_SET1)
      (cbase + k)->use_count++;
    else {
      p = firstpar(dvifile, k);

#ifdef DEBUG2
  fprintf(stderr,"\nprescanpage: p = %d,k = %d", p, k);
#endif /* DEBUG2 */

      switch (k) {
      case DVI_BOP:
        scanbop(dvifile);
        break;
      case DVI_EOP:
        goto end;
        break;
      case DVI_SET1:
      case DVI_SET2:
      case DVI_SET3:
      case DVI_SET4:
      case DVI_PUT1:
      case DVI_PUT2:
      case DVI_PUT3:
      case DVI_PUT4:
        if(p > 255)
          prerror("Char > 255 encountered\n");
        else
          (cbase +  p)->use_count++;
        break;
      case DVI_SET_RULE:
      case DVI_PUT_RULE:
        getsquad(dvifile);
        break;
      case DVI_FNT_DEF1:
      case DVI_FNT_DEF2:
      case DVI_FNT_DEF3:
      case DVI_FNT_DEF4:
        definefont(dvifile, p);
        break;
      case DVI_XXX1:
      case DVI_XXX2:
      case DVI_XXX3:
      case DVI_XXX4:
        badchar = FALSE;
        if(p + nextnamesfree  - names > NAMESIZE)
          prerror("Out of string space during special\n");
        for (k=0; k<(int)p; k++)
        {
          q = (int)getubyte(dvifile);
          if((q < ' ') || (q > '~'))
            badchar = TRUE;
          *(nextnamesfree + k) = q;
        }
        *(nextnamesfree + p) = '\0';
        if(badchar)
          fprintf(stderr,"non-ASCII character in DVI_XXX command\n");
        dospecial(bitfile, PRESCAN_ON, pcllevel, hh, vv, resolution);
        break;
      case DVI_POST:
        inpostamble = TRUE;
        goto end;
        break;
      default:
        if((k >= DVI_FNT_NUM_0) && (k <= DVI_FNT4))
        {
          if(p > MAXFONTS)
	    valerror("Max number of allowed fonts is %3d\n", MAXFONTS);
          else {
            font = fontptr[p];
            if(font == NULL)
              prerror("Undefined font\n");
            else {
              cbase = font->chr;
              if(!font_used[p]) {
                font_used[p] = TRUE;
                font_num++;
              }
            }
          }
        }
        break;
      }
    }
  }
end:
  if(font_num > 16)
  {
    fprintf(stderr,"Warning: ");
    printbop(startvals, count);
    fprintf(stderr,"prescanpage: %d [>16] fonts on page\n",font_num);
  }
}
