home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d7xx / d795 / pstools.lha / PSTools / PSTools1.lha / source / prescan.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-14  |  7.9 KB  |  255 lines

  1. /*
  2.  *   This is the main routine for the first (prescanning) pass.
  3.  */
  4. #include "structures.h" /* The copyright notice in that file is included too! */
  5. /*
  6.  *   These are all the external routines it calls:
  7.  */
  8. extern void error() ;
  9. extern shalfword dvibyte() ;
  10. extern integer signedquad() ;
  11. extern int skipnop() ;
  12. extern void skipover() ;
  13. extern short scanpage() ;
  14. extern void skippage() ;
  15. /*
  16.  *   These are the globals it accesses.
  17.  */
  18. #ifdef DEBUG
  19. extern integer debug_flag;
  20. #endif  /* DEBUG */
  21. extern fontdesctype *fonthead ;
  22. extern real conv ;
  23. extern real vconv ;
  24. extern real alpha ;
  25. extern integer firstpage, lastpage ;
  26. extern integer firstseq, lastseq ;
  27. extern Boolean notfirst, notlast ;
  28. extern integer fontmem ;
  29. extern integer pagecount ;
  30. extern integer pagenum ;
  31. extern integer maxpages ;
  32. extern sectiontype *sections ;
  33. extern FILE *dvifile ;
  34. extern integer mag ;
  35. extern Boolean overridemag ;
  36. extern integer swmem ;
  37. extern int quiet ;
  38. extern int actualdpi ;
  39. extern int vactualdpi ;
  40. extern Boolean reverse ;
  41. extern int totalpages ;
  42. extern integer fsizetol ;
  43. extern char *oname ;
  44. /*
  45.  *   This routine handles the processing of the preamble in the dvi file.
  46.  */
  47. void
  48. readpreamble()
  49. {
  50.    register integer num, den;
  51.    register int i ;
  52.  
  53.    if (dvibyte()!=247) error("! Bad DVI file: first byte not preamble") ;
  54.    if (dvibyte()!=2) error("! Bad DVI file: id byte not 2") ;
  55.    num = signedquad() ;
  56.    den = signedquad() ;
  57.    if (overridemag) (void)signedquad() ;
  58.    else mag = signedquad() ;
  59.    conv = (real) num * DPI * (real) mag / ( den * 254000000.0 ) ; 
  60.    vconv = (real) num * VDPI * (real) mag / ( den * 254000000.0 ) ; 
  61.    alpha = (((real)den / 7227.0) / 0x100000) * (25400000.0 / (real) num) ;
  62.    fsizetol = 1 + (integer)(DPI/(72270.0 * conv)) ;
  63.    if (! quiet) {
  64.       (void)fprintf(stderr, "'") ;
  65.       for(i=dvibyte();i>0;i--) (void)fputc(dvibyte(), stderr) ;
  66.       (void)fprintf(stderr, "' -> %s\n", oname) ;
  67.    } else
  68.       skipover(dvibyte()) ;
  69. }
  70.  
  71. /*
  72.  *   Finally, here's our main prescan routine.
  73.  */
  74. static integer firstmatch = -1, lastmatch = -1 ;
  75. void
  76. prescanpages()
  77. {
  78.    register int cmd ;
  79.    short ret ;
  80.    register integer thispageloc, thissecloc ;
  81.    register fontdesctype *f ;
  82.    register shalfword c ;
  83.    register long thissectionmem = 0 ;
  84.  
  85.    readpreamble() ;
  86. /*
  87.  *   Now we look for the first page to process.  If we get to the end of
  88.  *   the file before the page, we complain (fatally).
  89.  *   Incidentally, we don't use the DVI file's bop backpointer to skip
  90.  *   over pages at high speed, because we want to look to for special
  91.  *   header that might be in skipped pages.
  92.  */
  93.    while (1) {
  94.       cmd = skipnop() ;
  95.       if (cmd==248)
  96.          error("! End of document before first specified page") ;
  97.       if (cmd!=139)
  98.          error("! Bad DVI file: expected bop 1") ;
  99.       thispageloc = ftell(dvifile) ; /* the location FOLLOWING the bop */
  100. #ifdef DEBUG
  101.       if (dd(D_PAGE))
  102. #ifdef SHORTINT
  103.       (void)fprintf(stderr,"bop at %ld\n", thispageloc) ;
  104. #else   /* ~SHORTINT */
  105.       (void)fprintf(stderr,"bop at %d\n", thispageloc) ;
  106. #endif  /* ~SHORTINT */
  107. #endif  /* DEBUG */
  108.       pagenum = signedquad() ;
  109.       if (pagenum == firstpage && notfirst)
  110.          firstmatch++ ;
  111.       if (pagenum == lastpage && notlast)
  112.          lastmatch++ ;
  113.       if (notfirst && (pagenum != firstpage || firstmatch != firstseq))
  114.          skippage() ;
  115.       else {
  116.          if (notlast && pagenum == lastpage)
  117.             lastmatch-- ;
  118.          break ;
  119.       }
  120.    }
  121. /*
  122.  *   Here we scan for each of the sections.  First we initialize some of
  123.  *   the variables we need.
  124.  */
  125.    while (maxpages > 0 && cmd != 248) {
  126.       for (f=fonthead; f; f=f->next) {
  127.          f->psname = 0 ;
  128.          if (f->loaded==1)
  129.             for (c=255; c>=0; c--)
  130.                f->chardesc[c].flags &= (STATUSFLAGS) ;
  131.       }
  132.       fontmem = swmem - OVERCOST ;
  133.       if (fontmem <= 1000)
  134.          error("! Too little VM in printer") ;
  135.  
  136. /*   The section begins at the bop command just before thispageloc (which may
  137.  *   be a page that was aborted because the previous section overflowed memory).
  138.  */
  139.       pagecount = 0 ;
  140.       (void)fseek(dvifile, (long)thispageloc, 0) ;
  141.       pagenum = signedquad() ;
  142.       skipover(40) ;
  143.       thissecloc = thispageloc ;
  144. /*
  145.  *   Now we have the loop that actually scans the pages.  The scanpage routine
  146.  *   returns 1 if the page scans okay; it returns 2 if the memory ran out
  147.  *   before any pages were completed (in which case we'll try to carry on
  148.  *   and hope for the best); it returns 0 if a page was aborted for lack
  149.  *   of memory. After each page, we mark the characters seen on that page
  150.  *   as seen for this section so that they will be downloaded.
  151.  */
  152.       while (maxpages>0 && (ret=scanpage())) {
  153.          thissectionmem = swmem - fontmem - OVERCOST ;
  154.          if (pagenum == lastpage && notlast)
  155.             lastmatch++ ;
  156.          if (notlast && pagenum == lastpage && lastmatch == lastseq)
  157.             maxpages = -1 ; /* we are done after this page. */
  158.          if (reverse)
  159.             thissecloc = thispageloc ;
  160.          pagecount++ ;
  161.          maxpages-- ;
  162.          for (f=fonthead; f; f=f->next)
  163.             if (f->loaded==1) {
  164.                if (f->psflag & THISPAGE)
  165.                   f->psflag = PREVPAGE ;
  166.                for (c=255; c>=0; c--)
  167.                   if (f->chardesc[c].flags & THISPAGE)
  168.                      f->chardesc[c].flags = PREVPAGE |
  169.                (f->chardesc[c].flags & (STATUSFLAGS)) ;
  170.             }
  171.          cmd=skipnop() ;
  172.          if (cmd==248) break ;
  173.          if (cmd!=139)
  174.             error("! Bad DVI file: expected bop 2") ;
  175.          thispageloc = ftell(dvifile) ;
  176. #ifdef DEBUG
  177.          if (dd(D_PAGE))
  178. #ifdef SHORTINT
  179.          (void)fprintf(stderr,"bop at %ld\n", thispageloc) ;
  180. #else   /* ~SHORTINT */
  181.          (void)fprintf(stderr,"bop at %d\n", thispageloc) ;
  182. #endif  /* ~SHORTINT */
  183. #endif  /* DEBUG */
  184.          pagenum = signedquad() ;
  185.          skipover(40) ;
  186.          if (ret==2) break ;
  187.       }
  188. /*
  189.  *   Now we have reached the end of a section for some reason.
  190.  *   If there are any pages, we save the pagecount, section location,
  191.  *   and continue.
  192.  */
  193.       if (pagecount>0) {
  194.          register int fc = 0 ;
  195.          register sectiontype *sp ;
  196.          register charusetype *cp ;
  197.  
  198.          totalpages += pagecount ;
  199.          for (f=fonthead; f; f=f->next)
  200.             if (f->loaded==1 && f->psname)
  201.                fc++ ;
  202.          sp = (sectiontype *)malloc((unsigned int)(sizeof(sectiontype) + 
  203.             fc * sizeof(charusetype) + sizeof(fontdesctype *))) ;
  204.          if (sp==NULL)
  205.             error("! out of memory") ;
  206.          sp->bos = thissecloc ;
  207.          if (reverse) {
  208.             sp->next = sections ;
  209.             sections = sp ;
  210.          } else {
  211.             register sectiontype *p ;
  212.  
  213.             sp->next = NULL ;
  214.             if (sections == NULL)
  215.                sections = sp ;
  216.             else {
  217.                for (p=sections; p->next != NULL; p = p->next) ;
  218.                p->next = sp ;
  219.             }
  220.          }
  221.          sp->numpages = pagecount ;
  222. #ifdef DEBUG
  223.         if (dd(D_PAGE))
  224. #ifdef SHORTINT
  225.          (void)fprintf(stderr,"Have a section: %ld pages at %ld fontmem %ld\n", 
  226. #else   /* ~SHORTINT */
  227.          (void)fprintf(stderr,"Have a section: %d pages at %d fontmem %d\n", 
  228. #endif  /* ~SHORTINT */
  229.              pagecount, thissecloc, thissectionmem) ;
  230. #endif  /* DEBUG */
  231.          cp = (charusetype *) (sp + 1) ;
  232.          fc = 0 ;
  233.          for (f=fonthead; f; f=f->next)
  234.             if (f->loaded==1 && f->psname) {
  235.                register halfword b, bit ;
  236.  
  237.                cp->psfused = (f->psflag & PREVPAGE) ;
  238.                f->psflag = 0 ;
  239.                cp->fd = f ;
  240.                c = 0 ;
  241.                for (b=0; b<16; b++) {
  242.                   cp->bitmap[b] = 0 ;
  243.                   for (bit=32768; bit!=0; bit>>=1) {
  244.                      if (f->chardesc[c].flags & PREVPAGE)
  245.                         cp->bitmap[b] |= bit ;
  246.                   c++ ;
  247.                   }
  248.                }
  249.                cp++ ;
  250.             }
  251.          cp->fd = NULL ;
  252.       }
  253.    }
  254. }
  255.