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