summaryrefslogtreecommitdiff
path: root/Build/source/texk/dvipsk/dosection.c
blob: c7987130e49dacf9fc8d11d399d943d2f664be34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 *  Code to output PostScript commands for one section of the document.
 */
#include "dvips.h" /* The copyright notice in that file is included too! */
/*
 *   The external declarations:
 */
#include "protos.h"

static int psfont;
#ifdef HPS
int pagecounter;
#endif

/*
 *   Now we have the main procedure.
 */
void
dosection(sectiontype *s, int c)
{
   charusetype *cu;
   integer prevptr;
   int np;
   int k;
   integer thispage = 0;
   char buf[104];

   dopsfont(s);
#ifdef HPS
	 if (HPS_FLAG) pagecounter = 0;
#endif

   if (multiplesects) {
     setup();
   }
   cmdout("TeXDict");
   cmdout("begin");
   numout(hpapersize);
   numout(vpapersize);
   doubleout(mag);
   numout((integer)DPI);
   numout((integer)VDPI);
   snprintf(buf, sizeof(buf), "(%.99s)", fulliname);
   cmdout(buf);
   newline();
   cmdout("@start");
   if (multiplesects)
      cmdout("bos");
/*
 *   We insure raster is even-word aligned, because download might want that.
 */
   if (bytesleft & 1) {
      bytesleft--;
      raster++;
   }
   cleanres();
   cu = (charusetype *) (s + 1);
   psfont = 1;
   while (cu->fd) {
      if (cu->psfused)
         cu->fd->psflag = EXISTS;
      download(cu++, psfont++);
   }
   fonttableout();
   if (! multiplesects) {
      cmdout("end");
      setup();
   }
   for (cu=(charusetype *)(s+1); cu->fd; cu++)
      cu->fd->psflag = 0;
   while (c > 0) {
      c--;
      prevptr = s->bos;
      if (! reverse)
         fseek(dvifile, (long)prevptr, 0);
      np = s->numpages;
      while (np-- != 0) {
         if (reverse)
            fseek(dvifile, (long)prevptr, 0);
         pagenum = signedquad();
	 if ((evenpages && (pagenum & 1)) || (oddpages && (pagenum & 1)==0) ||
	  (pagelist && !InPageList(pagenum))) {
	    if (reverse) {
               skipover(36);
               prevptr = signedquad()+1;
	    } else {
               skipover(40);
	       skippage();
	       skipnop();
	    }
	    ++np;	/* this page wasn't counted for s->numpages */
	    continue;
	 }
/*
 *   We want to take the base 10 log of the number.  It's probably
 *   small, so we do it quick.
 */
         if (! quiet) {
            int t = pagenum, i = 0;
            if (t < 0) {
               t = -t;
               i++;
            }
            do {
               i++;
               t /= 10;
            } while (t > 0);
            if (pagecopies < 20)
               i += pagecopies - 1;
            if (i + prettycolumn > STDOUTSIZE) {
               fprintf(stderr, "\n");
               prettycolumn = 0;
            }
            prettycolumn += i + 1;
#ifdef SHORTINT
            fprintf(stderr, "[%ld", pagenum);
#else  /* ~SHORTINT */
            fprintf(stderr, "[%d", pagenum);
#endif /* ~SHORTINT */
            fflush(stderr);
         }
         skipover(36);
         prevptr = signedquad()+1;
         for (k=0; k<pagecopies; k++) {
            if (k == 0) {
               if (pagecopies > 1)
                  thispage = ftell(dvifile);
            } else {
               fseek(dvifile, (long)thispage, 0);
               if (prettycolumn + 1 > STDOUTSIZE) {
                  fprintf(stderr, "\n");
                  prettycolumn = 0;
               }
               fprintf(stderr, ".");
               fflush(stderr);
               prettycolumn++;
            }
            dopage();
         }
         if (! quiet) {
            fprintf(stderr, "] ");
            fflush(stderr);
            prettycolumn += 2;
         }
         if (! reverse)
            skipnop();
      }
   }
   if (! multiplesects && ! disablecomments) {
      newline();
      fprintf(bitfile, "%%%%Trailer\n");
   }
   if (multiplesects) {
      if (! disablecomments) {
         newline();
         fprintf(bitfile, "%%DVIPSSectionTrailer\n");
      }
      cmdout("eos");
      cmdout("end");
   }
#ifdef HPS
   if (HPS_FLAG) cmdout("\nend"); /* close off HPSDict */
#endif
   if (multiplesects && ! disablecomments) {
      newline();
      fprintf(bitfile, "%%DVIPSEndSection\n");
      linepos = 0;
   }
}
/*
 * Handle a list of pages for dvips.  Code based on dvi2ps 2.49,
 * maintained by Piet van Oostrum, piet@cs.ruu.nl.  Collected and
 * modularized for inclusion in dvips by metcalf@lcs.mit.edu.
 */

#include <ctype.h>
#define MAXPAGE (1000000000) /* assume no pages out of this range */
struct p_list_str {
    struct p_list_str *next;	/* next in a series of alternates */
    integer ps_low, ps_high;	/* allowed range */
} *ppages = 0;	/* the list of allowed pages */

/*-->InPageList*/
/**********************************************************************/
/******************************  InPageList  **************************/
/**********************************************************************/
/* Return true iff i is one of the desired output pages */

int
InPageList(integer i)
{
    register struct p_list_str *pl = ppages;

    while (pl) {
	    if ( i >= pl -> ps_low && i <= pl -> ps_high)
		return 1;		/* success */
	pl = pl -> next;
    }
    return 0;
}

static void
InstallPL(integer pslow, integer pshigh)
{
    register struct p_list_str   *pl;

    pl = (struct p_list_str *)mymalloc((integer)(sizeof *pl));
    pl -> next = ppages;
    pl -> ps_low = pslow;
    pl -> ps_high = pshigh;
    ppages = pl;
}

/* Parse a string representing a list of pages.  Return 0 iff ok.  As a
   side effect, the page selection(s) is (are) prepended to ppages. */

int
ParsePages(register char *s)
{
    register int    c;		/* current character */
    register integer  n = 0,	/* current numeric value */
		    innumber;	/* true => gathering a number */
    integer ps_low = 0, ps_high = 0;
    int     range,		/* true => saw a range indicator */
	    negative = 0;	/* true => number being built is negative */

#define white(x) ((x) == ' ' || (x) == '\t' || (x) == ',')

    range = 0;
    innumber = 0;
    for (;;) {
	c = *s++;
	if ( !innumber && !range) {/* nothing special going on */
	    if (c == 0)
		return 0;
	    if (white (c))
		continue;
	}
	if (c == '-' && !innumber) {
		innumber++;
		negative++;
		n = 0;
		continue;
	}
	if ('0' <= c && c <= '9') {	/* accumulate numeric value */
	    if (!innumber) {
		innumber++;
		negative = 0;
		n = c - '0';
		continue;
	    }
	    n *= 10;
	    n += negative ? '0' - c : c - '0';
	    continue;
	}
	if (c == '-' || c == ':') {/* here's a range */
	    if (range)
		return (-1);
	    if (innumber) {	/* have a lower bound */
		ps_low = n;
	    }
	    else
		ps_low = -MAXPAGE;
	    range++;
	    innumber = 0;
	    continue;
	}
	if (c == 0 || white (c)) {/* end of this range */
	    if (!innumber) {	/* no upper bound */
		ps_high = MAXPAGE;
		if (!range)	/* no lower bound either */
		    ps_low = -MAXPAGE;
	    }
	    else {		/* have an upper bound */
		ps_high = n;
		if (!range) {	/* no range => lower bound == upper */
		    ps_low = ps_high;
		}
	    }
	    InstallPL (ps_low, ps_high);
	    if (c == 0)
		return 0;
	    range = 0;
	    innumber = 0;
	    continue;
	}
	return (-1);
    }
#undef white
}