Debian Codegraph @

texk/dvipsk/pprescan.c

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