Debian Codegraph @

texk/xdvipsk/pprescan.c

raw
1 /*
2 * This is the main routine for the first (prescanning) pass.
3 */
4 #ifndef XDVIPSK
5 #include "dvips.h" /* The copyright notice in that file is included too! */
6 #else
7 #include "xdvips.h" /* The copyright notice in that file is included too! */
8 #endif /* XDVIPSK */
9
10 /*
11 * The external declarations:
12 */
13 #include "protos.h"
14
15 /*
16 * We declare this to tell everyone we are prescanning early.
17 */
18 Boolean pprescan;
19 /*
20 * When a font is selected during the pre-prescan, this routine makes sure
21 * that the tfm or vf (but not pk) file is loaded.
22 */
23 static void
24 ppreselectfont(fontdesctype *f)
25 {
26 int i;
27
28 curfnt = f;
29 if (curfnt->loaded == 0) {
30 if (!residentfont(curfnt))
31 if (!virtualfont(curfnt)) {
32 #ifndef XDVIPSK
33 for (i=0; i<256; i++)
34 curfnt->chardesc[i].flags = 0;
35 #else
36 for (i = 0; i<256; i++) {
37 chardesctype *cd = add_chardesc(curfnt, i);
38 cd->flags = 0;
39 }
40 #endif /* XDVIPSK */
41 curfnt->loaded = 3; /* we're scanning for sizes */
42 }
43 }
44 }
45 /*
46 * Now our scanpage routine.
47 */
48 static void
49 pscanpage(void)
50 {
51 register shalfword cmd;
52 register chardesctype *cd;
53 register fontmaptype *cfnt = 0;
54 integer fnt;
55 integer mychar;
56 register frametype *frp = frames;
57 #ifdef XDVIPSK
58 static chardesctype ctmp;
59 #endif /* XDVIPSK */
60
61 #ifdef DEBUG
62 if (dd(D_PAGE))
63 #ifdef SHORTINT
64 fprintf(stderr,"PPrescanning page %ld\n", pagenum);
65 #else /* ~SHORTINT */
66 fprintf(stderr,"PPrescanning page %d\n", pagenum);
67 #endif /* ~SHORTINT */
68 #endif /* DEBUG */
69 curfnt = NULL;
70 curpos = NULL;
71 bopcolor(0);
72 while (1) {
73 switch (cmd=dvibyte()) {
74 case 255: /* pTeX's dir or undefined */
75 if (!noptex) {
76 dvibyte();
77 break;
78 }
79 /* illegal commands */
80 #ifndef XDVIPSK
81 case 131: case 136: case 139: /* set4, put4, bop */
82 case 247: case 248: case 249: /* pre, post, post_post */
83 #else
84 case 139: case 247: case 248: case 249: /* bop, pre, post, post_post */
85 #endif /* XDVIPSK */
86 case 250: case 251: case 252: case 253: case 254: /* undefined */
87 sprintf(errbuf,
88 "! DVI file contains unexpected command (%d)",cmd);
89 error(errbuf);
90 case 132: case 137: /* eight-byte commands setrule, putrule */
91 dvibyte();
92 dvibyte();
93 dvibyte();
94 dvibyte();
95 case 146: case 151: case 156: case 160: case 165: case 170:
96 /* four-byte commands right4, w4, x4, down4, y4, z4 */
97 dvibyte();
98 case 145: case 150: case 155: case 159: case 164: case 169:
99 /* three-byte commands right3, w3, x3, down3, y3, z3 */
100 dvibyte();
101 case 144: case 149: case 154: case 158: case 163: case 168:
102 /* two-byte commands right2, w2, x2, down2, y2, z2 */
103 dvibyte();
104 case 143: case 148: case 153: case 157: case 162: case 167:
105 /* one-byte commands right1, w1, x1, down1, y1, z1 */
106 dvibyte();
107 case 147: case 152: case 161: case 166: /* w0, x0, y0, z0 */
108 case 138: case 141: case 142: /* nop, push, pop */
109 break;
110 #ifdef XDVIPSK
111 case 131: case 136: /* set4, put4 */
112 if (noptex && noluatex) {
113 sprintf(errbuf,
114 "! DVI file contains unexpected pTeX command (%d)",cmd);
115 error(errbuf);
116 }
117 mychar = dvibyte();
118 mychar = (mychar << 8) + dvibyte();
119 mychar = (mychar << 8) + dvibyte();
120 mychar = (mychar << 8) + dvibyte();
121 goto dochar;
122 #endif /* XDVIPSK */
123 case 130: case 135: /* set3, put3 */
124 if (noptex) {
125 sprintf(errbuf,
126 "! DVI file contains unexpected pTeX command (%d)",cmd);
127 error(errbuf);
128 }
129 mychar = dvibyte(); mychar = (mychar << 8) + dvibyte();
130 mychar = (mychar << 8) + dvibyte();
131 goto dochar;
132 case 129: case 134: /* set2, put2 */
133 #ifndef XDVIPSK
134 if (noomega && noptex) {
135 #else
136 if (noomega && noptex && noluatex) {
137 #endif /* XDVIPSK */
138 sprintf(errbuf,
139 "! DVI file contains unexpected command (%d)",cmd);
140 error(errbuf);
141 }
142 mychar = dvibyte(); mychar = (mychar << 8) + dvibyte();
143 goto dochar;
144 case 128: case 133: cmd = dvibyte(); /* set1, put1 commands drops through */
145 default: /* these are commands 0 (setchar0) thru 127 (setchar 127) */
146 mychar = cmd;
147 dochar:
148 if (curfnt==NULL)
149 error("! Bad DVI file: no font selected");
150 if (mychar>=curfnt->maxchars) {
151 if (!noptex && mychar<0x1000000 && curfnt->loaded == 2 && curfnt->kind == VF_PTEX) {
152 /* fallback */
153 } else {
154 sprintf(errbuf,"! [pscanpage] invalid char %d from font %.500s",
155 mychar, curfnt->name);
156 error(errbuf);
157 }
158 }
159 if (curfnt->loaded == 2) { /* scanning a virtual font character */
160 struct tft *ffont0;
161 frp->curp = curpos;
162 frp->curl = curlim;
163 frp->ff = ffont;
164 frp->curf = curfnt;
165 if (++frp == &frames[MAXFRAME] )
166 error("! virtual recursion stack overflow");
167 #ifndef XDVIPSK
168 cd = curfnt->chardesc + mychar;
169 #else
170 cd = find_chardesc(curfnt, mychar);
171 if (!cd)
172 {
173 PRINTF_PR("Error: character %d not found in the font %s\n", mychar, curfnt->name);
174 cd = &ctmp;
175 }
176 #endif /* XDVIPSK */
177 if (mychar>=curfnt->maxchars || cd->packptr == NULL) {
178 if (!noptex && mychar<0x1000000 && curfnt->kind == VF_PTEX) { /* fallback */
179 #ifdef DEBUG
180 if (dd(D_FONTS))
181 fprintf_str(stderr,
182 "We will fallback pTeX vf:%s to %s:%s\n",
183 curfnt->name, curfnt->localfonts->desc->kind==OFM_OMEGA ? "ofm" : "tfm",
184 curfnt->localfonts->desc->name);
185 #endif /* DEBUG */
186 } else {
187 fprintf(stderr, "Wrong char code: %04X\n", mychar);
188 error("! a non-existent virtual character is being used; check vf/tfm files");
189 }
190 ffont0 = curfnt->localfonts;
191 if (ffont0==NULL)
192 curfnt = NULL;
193 else
194 ppreselectfont(ffont0->desc);
195 --frp;
196 curfnt = frp->curf;
197 ffont = frp->ff;
198 curlim = frp->curl;
199 curpos = frp->curp;
200 } else {
201 curpos = cd->packptr + 2;
202 curlim = curpos + (256*(long)(*cd->packptr)+(*(cd->packptr+1)));
203 ffont = curfnt->localfonts;
204 if (ffont==NULL)
205 curfnt = NULL;
206 else
207 ppreselectfont(ffont->desc);
208 }
209 #ifndef XDVIPSK
210 } else if (curfnt->loaded == 3)
211 curfnt->chardesc[mychar].flags = EXISTS;
212 #else
213 } else if (curfnt->loaded == 3) {
214 cd = find_chardesc(curfnt, mychar);
215 if (!cd)
216 {
217 PRINTF_PR("Error: character %d not found in the font %s\n", mychar, curfnt->name);
218 cd = &ctmp;
219 }
220 cd->flags = EXISTS;
221 }
222 #endif /* XDVIPSK */
223 break;
224 case 171: case 172: case 173: case 174: case 175: case 176: case 177:
225 case 178: case 179: case 180: case 181: case 182: case 183: case 184:
226 case 185: case 186: case 187: case 188: case 189: case 190: case 191:
227 case 192: case 193: case 194: case 195: case 196: case 197: case 198:
228 case 199: case 200: case 201: case 202: case 203: case 204: case 205:
229 case 206: case 207: case 208: case 209: case 210: case 211: case 212:
230 case 213: case 214: case 215: case 216: case 217: case 218: case 219:
231 case 220: case 221: case 222: case 223: case 224: case 225: case 226:
232 case 227: case 228: case 229: case 230: case 231: case 232: case 233:
233 case 234: case 235: case 236: case 237: case 238: /* font selection commands */
234 if (cmd < 235) fnt = cmd - 171; /* fntnum0 thru fntnum63 */
235 else {
236 fnt = dvibyte(); /* fnt1 */
237 while (cmd-- > 235)
238 fnt = (fnt << 8) + dvibyte();
239 }
240 for (cfnt=ffont; cfnt; cfnt = cfnt->next)
241 if (cfnt->fontnum == fnt) goto fontfound;
242 error("! no font selected");
243 fontfound: curfnt = cfnt->desc;
244 ppreselectfont(curfnt);
245 break;
246 #ifndef XDVIPSK
247 case 239: predospecial((integer)dvibyte(), 1); break; /* xxx1 */
248 case 240: predospecial((integer)twobytes(), 1); break; /* xxx2 */
249 case 241: predospecial(threebytes(), 1); break; /* xxx3 */
250 case 242: predospecial(signedquad(), 1); break; /* xxx4 */
251 #else /* XDVIPSK */
252 case 239: predospecial((integer)dvibyte(), 1, FALSE); break; /* xxx1 */
253 case 240: predospecial((integer)twobytes(), 1, FALSE); break; /* xxx2 */
254 case 241: predospecial(threebytes(), 1, FALSE); break; /* xxx3 */
255 case 242: predospecial(signedquad(), 1, FALSE); break; /* xxx4 */
256 #endif /* XDVIPSK */
257 case 243: case 244: case 245: case 246: fontdef(cmd-242); break; /* fntdef1 */
258 case 140: /* eop or end of virtual char */
259 if (curpos) {
260 --frp;
261 curfnt = frp->curf;
262 ffont = frp->ff;
263 curlim = frp->curl;
264 curpos = frp->curp;
265 break;
266 }
267 return;
268 }
269 }
270 }
271 /*
272 * Finally, here's our main pprescan routine.
273 */
274 static integer firstmatch = -1, lastmatch = -1;
275 void
276 pprescanpages(void)
277 {
278 register int cmd;
279 integer lmaxpages = maxpages;
280 integer mpagenum;
281 integer pageseq = 0;
282 int ntfirst = notfirst;
283
284 #ifdef XDVIPSK
285 firstmatch = -1;
286 lastmatch = -1;
287 #endif /* XDVIPSK */
288 pprescan = 1;
289 readpreamble();
290 /*
291 * Now we look for the first page to process. If we get to the end of
292 * the file before the page, we complain (fatally).
293 * Incidentally, we don't use the DVI file's bop backpointer to skip
294 * over pages at high speed, because we want to look to for special
295 * header that might be in skipped pages.
296 */
297 while (1) {
298 cmd = skipnop();
299 if (cmd==248)
300 error("! End of document before first specified page");
301 if (cmd!=139)
302 error("! Bad DVI file: expected bop");
303 pagenum = signedquad();
304 pageseq++;
305 mpagenum = abspage ? pageseq : pagenum;
306 if (mpagenum == firstpage && ntfirst)
307 firstmatch++;
308 if (mpagenum == lastpage && notlast)
309 lastmatch++;
310 if (ntfirst && mpagenum == firstpage && firstmatch == firstseq)
311 ntfirst = 0;
312 if (ntfirst ||
313 ((evenpages && (pagenum & 1)) || (oddpages && (pagenum & 1)==0) ||
314 (pagelist && !InPageList(pagenum)))) {
315 skipover(40);
316 skippage();
317 } else {
318 if (notlast && mpagenum == lastpage)
319 lastmatch--;
320 break;
321 }
322 }
323 /*
324 * Here we scan for each of the sections. First we initialize some of
325 * the variables we need. We don't have to be as smart as before in
326 * skipping pages.
327 */
328 skipover(40);
329 while (lmaxpages > 0) {
330 if (!(evenpages && (pagenum & 1)) &&
331 !(oddpages && (pagenum & 1)==0) &&
332 !(pagelist && !InPageList(pagenum))) {
333 pscanpage();
334 lmaxpages--;
335 } else
336 skippage();
337 mpagenum = abspage ? pageseq : pagenum;
338 if (mpagenum == lastpage && notlast)
339 lastmatch++;
340 if (notlast && mpagenum == lastpage && lastmatch == lastseq)
341 lmaxpages = -1; /* we are done after this page. */
342 cmd=skipnop();
343 if (cmd==248) break;
344 if (cmd!=139)
345 error("! Bad DVI file: expected bop");
346 pagenum = signedquad();
347 skipover(40);
348 pageseq++;
349 }
350 fseek(dvifile, 0L, 0);
351 pprescan = 0;
352 }