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