home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / PRINTING / DVIPS54.ZIP / DVIPS / SCANPAGE.C < prev    next >
C/C++ Source or Header  |  1990-11-25  |  10KB  |  282 lines

  1. /*
  2.  *   This routine scans a page for font definitions and character usage.
  3.  *   It keeps an estimate of the memory remaining in the printer, and
  4.  *   returns false when that memory is exhausted, unless scanning the
  5.  *   first page. Otherwise it returns 1 if no problems were found,
  6.  *   or 2 if it thinks the first page of a section may overflow memory.
  7.  */
  8. #include "structures.h" /* The copyright notice in that file is included too! */
  9. /*
  10.  *   These are the external routines we need.
  11.  */
  12. extern shalfword dvibyte() ;
  13. extern halfword twobytes() ;
  14. extern integer threebytes() ;
  15. extern integer signedquad() ;
  16. extern shalfword signedbyte() ;
  17. extern shalfword signedpair() ;
  18. extern integer signedtrio() ;
  19. extern void skipover() ;
  20. extern void fontdef() ;
  21. extern void predospecial() ;
  22. extern Boolean residentfont() ;
  23. extern Boolean virtualfont() ;
  24. extern void loadfont() ;
  25. extern void error() ;
  26. extern long getlong() ;
  27. extern void repack() ;
  28. /*
  29.  *   And, of course, the globals it uses.
  30.  */
  31. #ifdef DEBUG
  32. extern integer debug_flag;
  33. #endif  /* DEBUG */
  34. extern fontdesctype *curfnt ;
  35. extern fontdesctype *baseFonts[] ;
  36. extern fontmaptype *ffont ;
  37. extern quarterword *curpos, *curlim ;
  38. extern integer fontmem ;
  39. extern integer pagecount ;
  40. extern integer pagenum ;
  41. extern Boolean compressed ;
  42. extern char errbuf[] ;
  43. extern frametype frames[] ;
  44. extern integer pagecost ;
  45. /*
  46.  * Charge pagecost for the cost of showing the character that *cd refers to
  47.  * and charge fontmem for the cost of downloading the character if necessary.
  48.  * A zero result indicates that we have run out of memory.
  49.  *   If the character is small (a small packet), we save 43 bytes (since we
  50.  *   use a single string instead of an array with integers in the PostScript.)
  51.  */
  52. Boolean
  53. prescanchar(cd)
  54. chardesctype *cd ;
  55. {
  56.    register quarterword *cras ;
  57.    register integer thischar ;
  58.  
  59.    if ((cd->flags & (PREVPAGE | THISPAGE)) == 0) {
  60.       /* the character hasn't been used before in the current section */
  61.       cras = cd->packptr ;
  62.       if (cras != NULL) {
  63.          thischar = CHARCOST ;
  64.          if (cd->flags & BIGCHAR)
  65.             thischar += 43 ;
  66.          if (compressed) {
  67.             if ((cd->flags & REPACKED) == 0) {
  68.                repack(cd) ;
  69.                cras = cd->packptr ;
  70.             }
  71.             if ((*cras & 4))
  72.                if ((*cras & 7) == 7)
  73.                   thischar += getlong(cras + 17) ;
  74.                else
  75.                   thischar += getlong(cras + 9) ;
  76.             else
  77.                thischar += getlong(cras + 5) ;
  78.          } else {
  79.             if ((*cras & 4))
  80.                if ((*cras & 7) == 7)
  81.                   thischar += getlong(cras + 1)
  82.                                           * ((getlong(cras + 5) + 7) >> 3) ;
  83.                else
  84.                   thischar += (cras[3] * 256 + cras[4]) *
  85.                                  ((cras[1] * 256 + cras[2] + 7) >> 3) ;
  86.             else
  87.                thischar += ((int)(cras[2] * ((cras[1] + 7) >> 3))) ;
  88.          }
  89.          cd->flags |= THISPAGE ;
  90.          fontmem -= thischar ;
  91.          if (fontmem <= pagecost) {
  92.             if (pagecount > 0)
  93.                return(0) ;
  94.          }
  95.       }
  96.    }
  97.    return(1) ;
  98. }
  99. /*
  100.  * When a font is selected during the prescan, this routine makes sure that
  101.  * the tfm or pk file is loaded and charges fontmem for the VM overhead in
  102.  * creating the font.  The result is true unless the section must be aborted
  103.  * in order to keep this font from overflowing VM.
  104.  */
  105. Boolean
  106. preselectfont(f)
  107. fontdesctype *f ;
  108. {
  109.    curfnt = f ;
  110.    if (curfnt->loaded == 0) {
  111.       if (!residentfont(curfnt))
  112.          if (!virtualfont(curfnt))
  113.             loadfont(curfnt) ;
  114.    }
  115.    if (curfnt->psname == 0) {
  116.       if (curfnt->loaded < 2) /* virtual font costs nothing (yet) */
  117.          if (curfnt->resfont)
  118.             fontmem -= PSFONTCOST ;
  119.          else {
  120.             fontmem -= FONTCOST + curfnt->maxchars ;
  121.             if (curfnt->loadeddpi != curfnt->dpi)
  122.                fontmem -= 48 ; /* for the new font matrix */
  123.          }
  124.       curfnt->psname = 1 ;
  125.       if (fontmem <= pagecost) {
  126.          if (pagecount > 0)
  127.             return(0) ;
  128.       }
  129.    }
  130.    return(1) ;
  131. }
  132. /*
  133.  *   Now our scanpage routine.
  134.  */
  135. short
  136. scanpage()
  137. {
  138.    register shalfword cmd ;
  139.    register chardesctype *cd ;
  140.    register fontmaptype *cfnt = 0 ;
  141.    integer fnt ;
  142.    register frametype *frp = frames ;
  143.  
  144.    pagecost = 0 ;
  145. #ifdef DEBUG
  146.    if (dd(D_PAGE))
  147. #ifdef SHORTINT
  148.    (void)fprintf(stderr,"Scanning page %ld\n", pagenum) ;
  149. #else   /* ~SHORTINT */
  150.    (void)fprintf(stderr,"Scanning page %d\n", pagenum) ;
  151. #endif  /* ~SHORTINT */
  152. #endif  /* DEBUG */
  153.    curfnt = NULL ;
  154.    curpos = NULL ;
  155.    while (1) {
  156.       switch (cmd=dvibyte()) {
  157. case 129: case 130: case 131: case 134: case 135: case 136: case 139:
  158. case 247: case 248: case 249: case 250: case 251: case 252: case 253:
  159. case 254: case 255: /* unimplemented or illegal commands */
  160.          (void)sprintf(errbuf,
  161.             "! DVI file contains unexpected command (%d)",cmd) ;
  162.          error(errbuf) ;
  163. case 132: case 137: /* eight-byte commands setrule, putrule */
  164.          (void)dvibyte() ;
  165.          (void)dvibyte() ;
  166.          (void)dvibyte() ;
  167.          (void)dvibyte() ;
  168. case 146: case 151: case 156: case 160: case 165: case 170:
  169.    /* four-byte commands right4, w4, x4, down4, y4, z4 */
  170.          (void)dvibyte() ;
  171. case 145: case 150: case 155: case 159: case 164: case 169:
  172.    /* three-byte commands right3, w3, x3, down3, y3, z3 */
  173.          (void)dvibyte() ;
  174. case 144: case 149: case 154: case 158: case 163: case 168:
  175.    /* two-byte commands right2, w2, x2, down2, y2, z2 */
  176.          (void)dvibyte() ;
  177. case 143: case 148: case 153: case 157: case 162: case 167:
  178.    /* one-byte commands right1, w1, x1, down1, y1, z1 */
  179.          (void)dvibyte() ;
  180. case 147: case 152: case 161: case 166: /* w0, x0, y0, z0 */
  181. case 138: case 141: case 142: /* nop, push, pop */
  182.          break ;
  183. case 133: case 128: cmd = dvibyte() ; /* set1 commands drops through */
  184. default:    /* these are commands 0 (setchar0) thru 127 (setchar 127) */
  185. /*
  186.  *   We are going to approximate that each string of consecutive characters
  187.  *   requires (length of string) bytes of PostScript VM.
  188.  */
  189.          if (curfnt==NULL)
  190.             error("! Bad DVI file: no font selected") ;
  191.          if (curfnt->loaded == 2) { /* scanning a virtual font character */
  192.             frp->curp = curpos ;
  193.             frp->curl = curlim ;
  194.             frp->ff = ffont ;
  195.             frp->curf = curfnt ;
  196.             if (++frp == &frames[MAXFRAME] )
  197.                error("! virtual recursion stack overflow") ;
  198.             cd = curfnt->chardesc + cmd ;
  199.             curpos = cd->packptr + 2 ;
  200.             curlim = curpos + (256*(long)(*cd->packptr)+(*(cd->packptr+1))) ;
  201.             ffont = curfnt->localfonts ;
  202.             if (ffont==NULL)
  203.                curfnt = NULL ;
  204.             else if (!preselectfont(ffont->desc))
  205.                goto outofmem ;
  206.          } else {
  207.             pagecost++ ;
  208.             if (curfnt->resfont == NULL) {
  209.                if (!prescanchar(curfnt->chardesc + cmd))
  210.                   goto outofmem ;
  211.             }
  212.          }
  213.          break ;        
  214. case 171: case 172: case 173: case 174: case 175: case 176: case 177:
  215. case 178: case 179: case 180: case 181: case 182: case 183: case 184:
  216. case 185: case 186: case 187: case 188: case 189: case 190: case 191:
  217. case 192: case 193: case 194: case 195: case 196: case 197: case 198:
  218. case 199: case 200: case 201: case 202: case 203: case 204: case 205:
  219. case 206: case 207: case 208: case 209: case 210: case 211: case 212:
  220. case 213: case 214: case 215: case 216: case 217: case 218: case 219:
  221. case 220: case 221: case 222: case 223: case 224: case 225: case 226:
  222. case 227: case 228: case 229: case 230: case 231: case 232: case 233:
  223. case 234: case 235: case 236: case 237: case 238: /* font selection commands */
  224.          if (cmd < 235) fnt = cmd - 171 ; /* fntnum0 thru fntnum63 */
  225.          else {
  226.             fnt = dvibyte() ; /* fnt1 */
  227.             while (cmd-- > 235)
  228.                fnt = (fnt << 8) + dvibyte() ;
  229.          }
  230.          if (curpos || fnt > 255) {
  231.             for (cfnt=ffont; cfnt; cfnt = cfnt->next)
  232.                if (cfnt->fontnum == fnt) goto fontfound ;
  233.          } else
  234.             if (curfnt = baseFonts[fnt])
  235.                goto fontfound2 ;
  236.             error("! no font selected") ;
  237. fontfound: curfnt = cfnt->desc ;
  238. fontfound2: if (!preselectfont(curfnt))
  239.               goto outofmem ;
  240.             break ;
  241. case 239: predospecial((integer)dvibyte(), 1) ; break ; /* xxx1 */
  242. case 240: predospecial((integer)twobytes(), 1) ; break ; /* xxx2 */
  243. case 241: predospecial(threebytes(), 1) ; break ; /* xxx3 */
  244. case 242: predospecial(signedquad(), 1) ; break ; /* xxx4 */
  245. case 243: case 244: case 245: case 246: fontdef(cmd-242) ; break ; /* fntdef1 */
  246. case 140: /* eop or end of virtual char */
  247.          if (curpos) {
  248.             --frp ;
  249.             curfnt = frp->curf ;
  250.             ffont = frp->ff ;
  251.             curlim = frp->curl ;
  252.             curpos = frp->curp ;
  253.             break ;
  254.          }
  255.          goto endofpage ; /* eop */
  256.       }
  257.    }
  258. endofpage:
  259.    if (fontmem > pagecost)
  260.       return(1) ;
  261.    if (pagecount > 0)
  262.       return(0) ;
  263. #ifdef SHORTINT
  264.       (void)fprintf(stderr, "Page %ld may be too complex to print\n", pagenum) ;
  265. #else   /* ~SHORTINT */
  266.       (void)fprintf(stderr, "Page %d may be too complex to print\n", pagenum) ;
  267. #endif  /* ~SHORTINT */
  268. /*
  269.  *   This case should be rare indeed.  Even with only 200K of virtual memory,
  270.  *   at 11 bytes per char, you can have 18K characters on a page.
  271.  */
  272.    return(2) ;
  273. outofmem:
  274.    if (frp != frames) {
  275.       curpos = frames->curp ;
  276.       curlim = frames->curl ;
  277.       ffont = frames->ff ;
  278.       curfnt = frames->curf ;
  279.    }
  280.    return 0 ;
  281. }
  282.