home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / d / dvips551.zip / DVIPS.ZIP / DOSECTIO.C < prev    next >
C/C++ Source or Header  |  1993-01-27  |  8KB  |  312 lines

  1. /*
  2.  *  Code to output PostScript commands for one section of the document.
  3.  */
  4. #include "dvips.h" /* The copyright notice in that file is included too! */
  5. /*
  6.  *   These are the external routines we call.
  7.  */
  8. extern void dopage() ;
  9. extern void download() ;
  10. extern integer signedquad() ;
  11. extern void skipover() ;
  12. extern void cmdout() ;
  13. extern void numout() ;
  14. extern void newline() ;
  15. extern void setup() ;
  16. extern void skipover() ;
  17. extern int skipnop() ;
  18. extern void skippage() ;
  19. extern int InPageList() ;
  20. extern char *mymalloc() ;
  21. /*
  22.  *   These are the external variables we access.
  23.  */
  24. extern shalfword linepos ;
  25. extern FILE *dvifile ;
  26. extern FILE *bitfile ;
  27. extern integer pagenum ;
  28. extern long bytesleft ;
  29. extern quarterword *raster ;
  30. extern int quiet ;
  31. extern Boolean reverse, multiplesects, disablecomments ;
  32. extern Boolean evenpages, oddpages, pagelist ;
  33. extern int actualdpi ;
  34. extern int vactualdpi ;
  35. extern int prettycolumn ;
  36. extern integer hpapersize, vpapersize ;
  37. extern integer pagecopies ;
  38. static int psfont ;
  39. extern integer mag ;
  40. extern char *fulliname ;
  41. /*
  42.  *   Now we have the main procedure.
  43.  */
  44. void
  45. dosection(s, c)
  46.         sectiontype *s ;
  47.         int c ;
  48. {
  49.    charusetype *cu ;
  50.    integer prevptr ;
  51.    int np ;
  52.    int k ;
  53.    integer thispage = 0 ;
  54.    char buf[104];
  55.    extern void cleanres() ;
  56.  
  57.    if (multiplesects) {
  58.       setup() ;
  59.    } else {
  60.       cmdout("TeXDict") ;
  61.       cmdout("begin") ;
  62.    }
  63.    numout(hpapersize) ;
  64.    numout(vpapersize) ;
  65.    numout(mag) ;
  66.    numout((integer)DPI) ;
  67.    numout((integer)VDPI) ;
  68.    sprintf(buf, "(%.99s)", fulliname) ;
  69.    cmdout(buf) ;
  70.    cmdout("@start") ;
  71.    if (multiplesects)
  72.       cmdout("bos") ;
  73. /*
  74.  *   We insure raster is even-word aligned, because download might want that.
  75.  */
  76.    if (bytesleft & 1) {
  77.       bytesleft-- ;
  78.       raster++ ;
  79.    }
  80.    cleanres() ;
  81.    cu = (charusetype *) (s + 1) ;
  82.    psfont = 1 ;
  83.    while (cu->fd) {
  84.       if (cu->psfused)
  85.          cu->fd->psflag = EXISTS ;
  86.       download(cu++, psfont++) ;
  87.    }
  88.    if (! multiplesects) {
  89.       cmdout("end") ;
  90.       setup() ;
  91.    }
  92.    for (cu=(charusetype *)(s+1); cu->fd; cu++)
  93.       cu->fd->psflag = 0 ;
  94.    while (c > 0) {
  95.       c-- ;
  96.       prevptr = s->bos ;
  97.       if (! reverse)
  98.          (void)fseek(dvifile, (long)prevptr, 0) ;
  99.       np = s->numpages ;
  100.       while (np-- != 0) {
  101.          if (reverse)
  102.             (void)fseek(dvifile, (long)prevptr, 0) ;
  103.          pagenum = signedquad() ;
  104.      if ((evenpages && (pagenum & 1)) || (oddpages && (pagenum & 1)==0) ||
  105.       (pagelist && !InPageList(pagenum))) {
  106.         if (reverse) {
  107.                skipover(36) ;
  108.                prevptr = signedquad()+1 ;
  109.         } else {
  110.                skipover(40) ;
  111.            skippage() ;
  112.            skipnop() ;
  113.         }
  114.         ++np ;    /* this page wasn't counted for s->numpages */
  115.         continue;
  116.      }
  117. /*
  118.  *   We want to take the base 10 log of the number.  It's probably
  119.  *   small, so we do it quick.
  120.  */
  121.          if (! quiet) {
  122.             int t = pagenum, i = 0 ;
  123.             if (t < 0) {
  124.                t = -t ;
  125.                i++ ;
  126.             }
  127.             do {
  128.                i++ ;
  129.                t /= 10 ;
  130.             } while (t > 0) ;
  131.             if (pagecopies < 20)
  132.                i += pagecopies - 1 ;
  133.             if (i + prettycolumn > STDOUTSIZE) {
  134.                fprintf(stderr, "\n") ;
  135.                prettycolumn = 0 ;
  136.             }
  137.             prettycolumn += i + 1 ;
  138. #ifdef SHORTINT
  139.             (void)fprintf(stderr, "[%ld", pagenum) ;
  140. #else  /* ~SHORTINT */
  141.             (void)fprintf(stderr, "[%d", pagenum) ;
  142. #endif /* ~SHORTINT */
  143.             (void)fflush(stderr) ;
  144.          }
  145.          skipover(36) ;
  146.          prevptr = signedquad()+1 ;
  147.          for (k=0; k<pagecopies; k++) {
  148.             if (k == 0) {
  149.                if (pagecopies > 1)
  150.                   thispage = ftell(dvifile) ;
  151.             } else {
  152.                (void)fseek(dvifile, (long)thispage, 0) ;
  153.                if (prettycolumn + 1 > STDOUTSIZE) {
  154.                   (void)fprintf(stderr, "\n") ;
  155.                   prettycolumn = 0 ;
  156.                }
  157.                (void)fprintf(stderr, ".") ;
  158.                (void)fflush(stderr) ;
  159.                prettycolumn++ ;
  160.             }
  161.             dopage() ;
  162.          }
  163.          if (! quiet) {
  164.             (void)fprintf(stderr, "] ") ;
  165.             (void)fflush(stderr) ;
  166.             prettycolumn += 2 ;
  167.          }
  168.          if (! reverse)
  169.             (void)skipnop() ;
  170.       }
  171.    }
  172.    if (! multiplesects && ! disablecomments) {
  173.       newline() ;
  174.       (void)fprintf(bitfile, "%%%%Trailer\n") ;
  175.    }
  176.    if (multiplesects) {
  177.       if (! disablecomments) {
  178.          newline() ;
  179.          (void)fprintf(bitfile, "%%DVIPSSectionTrailer\n") ;
  180.       }
  181.       cmdout("eos") ;
  182.    }
  183.    cmdout("end") ;
  184.    if (multiplesects && ! disablecomments) {
  185.       newline() ;
  186.       (void)fprintf(bitfile, "%%DVIPSEndSection\n") ;
  187.       linepos = 0 ;
  188.    }
  189. }
  190. /*
  191.  * Handle a list of pages for dvips.  Code based on dvi2ps 2.49,
  192.  * maintained by Piet van Oostrum, piet@cs.ruu.nl.  Collected and
  193.  * modularized for inclusion in dvips by metcalf@lcs.mit.edu.
  194.  */
  195.  
  196. #include <ctype.h>
  197. #define MAXPAGE (1000000000) /* assume no pages out of this range */
  198. struct p_list_str {
  199.     struct p_list_str *next;    /* next in a series of alternates */
  200.     integer ps_low, ps_high;    /* allowed range */
  201. } *ppages = 0;    /* the list of allowed pages */
  202.  
  203. /*-->InPageList*/
  204. /**********************************************************************/
  205. /******************************  InPageList  **************************/
  206. /**********************************************************************/
  207. /* Return true iff i is one of the desired output pages */
  208.  
  209. int InPageList(i)
  210. integer i ;
  211. {
  212.     register struct p_list_str *pl = ppages;
  213.  
  214.     while (pl) {
  215.         if ( i >= pl -> ps_low && i <= pl -> ps_high)
  216.         return 1;        /* success */
  217.     pl = pl -> next;
  218.     }
  219.     return 0;
  220. }
  221.  
  222. void InstallPL (pslow, pshigh)
  223. integer pslow, pshigh;
  224. {
  225.     register struct p_list_str   *pl;
  226.  
  227.     pl = (struct p_list_str *)mymalloc((integer)(sizeof *pl));
  228.     pl -> next = ppages;
  229.     pl -> ps_low = pslow;
  230.     pl -> ps_high = pshigh;
  231.     ppages = pl;
  232. }
  233.  
  234. /* Parse a string representing a list of pages.  Return 0 iff ok.  As a
  235.    side effect, the page selection(s) is (are) prepended to ppages. */
  236.  
  237. int
  238. ParsePages (s)
  239. register char  *s;
  240. {
  241.     register int    c ;        /* current character */
  242.     register integer  n = 0,    /* current numeric value */
  243.             innumber;    /* true => gathering a number */
  244.     integer ps_low = 0, ps_high = 0 ;
  245.     int     range,        /* true => saw a range indicator */
  246.         negative = 0;    /* true => number being built is negative */
  247.  
  248. #define white(x) ((x) == ' ' || (x) == '\t' || (x) == ',')
  249.  
  250.     range = 0;
  251.     innumber = 0;
  252.     for (;;) {
  253.     c = *s++;
  254.     if ( !innumber && !range) {/* nothing special going on */
  255.         if (c == 0)
  256.         return 0;
  257.         if (white (c))
  258.         continue;
  259.     }
  260.     if (c == '-' && !innumber) {
  261.         innumber++;
  262.         negative++;
  263.         n = 0;
  264.         continue;
  265.     }
  266.     if ('0' <= c && c <= '9') {    /* accumulate numeric value */
  267.         if (!innumber) {
  268.         innumber++;
  269.         negative = 0;
  270.         n = c - '0';
  271.         continue;
  272.         }
  273.         n *= 10;
  274.         n += negative ? '0' - c : c - '0';
  275.         continue;
  276.     }
  277.     if (c == '-' || c == ':') {/* here's a range */
  278.         if (range)
  279.         return (-1);
  280.         if (innumber) {    /* have a lower bound */
  281.         ps_low = n;
  282.         }
  283.         else
  284.         ps_low = -MAXPAGE;
  285.         range++;
  286.         innumber = 0;
  287.         continue;
  288.     }
  289.     if (c == 0 || white (c)) {/* end of this range */
  290.         if (!innumber) {    /* no upper bound */
  291.         ps_high = MAXPAGE;
  292.         if (!range)    /* no lower bound either */
  293.             ps_low = -MAXPAGE;
  294.         }
  295.         else {        /* have an upper bound */
  296.         ps_high = n;
  297.         if (!range) {    /* no range => lower bound == upper */
  298.             ps_low = ps_high;
  299.         }
  300.         }
  301.         InstallPL (ps_low, ps_high);
  302.         if (c == 0)
  303.         return 0;
  304.         range = 0;
  305.         innumber = 0;
  306.         continue;
  307.     }
  308.     return (-1);
  309.     }
  310. #undef white
  311. }
  312.