Debian Codegraph @

texk/xdvipsk/scanpage.c

raw
1 /*
2 * This routine scans a page for font definitions and character usage.
3 * It keeps an estimate of the memory remaining in the printer, and
4 * returns false when that memory is exhausted, unless scanning the
5 * first page. Otherwise it returns 1 if no problems were found,
6 * or 2 if it thinks the first page of a section may overflow memory.
7 */
8 #ifndef XDVIPSK
9 #include "dvips.h" /* The copyright notice in that file is included too! */
10 #else
11 #include "xdvips.h" /* The copyright notice in that file is included too! */
12 #endif /* XDVIPSK */
13
14 /*
15 * The external declarations:
16 */
17 #include "protos.h"
18
19 /*
20 * Charge pagecost for the cost of showing the character that *cd refers to
21 * and charge fontmem for the cost of downloading the character if necessary.
22 * A zero result indicates that we have run out of memory.
23 * If the character is small (a small packet), we save 43 bytes (since we
24 * use a single string instead of an array with integers in the PostScript.)
25 */
26 Boolean
27 prescanchar(chardesctype *cd)
28 {
29 register quarterword *cras;
30 register integer thischar;
31
32 #ifdef XDVIPSK
33 assert(cd);
34 #endif /* XDVIPSK */
35 if ((cd->flags & (PREVPAGE | THISPAGE)) == 0) {
36 /* the character hasn't been used before in the current section */
37 cras = cd->packptr;
38 if (curfnt->resfont) {
39 thischar = PSCHARCOST;
40 cd->flags |= THISPAGE;
41 } else if (cras != NULL) {
42 thischar = CHARCOST;
43 if (cd->flags & BIGCHAR)
44 thischar += 43;
45 if (compressed) {
46 if ((cd->flags & REPACKED) == 0) {
47 repack(cd);
48 cras = cd->packptr;
49 }
50 if ((*cras & 4))
51 if ((*cras & 7) == 7)
52 thischar += getlong(cras + 17);
53 else
54 thischar += getlong(cras + 9);
55 else
56 thischar += getlong(cras + 5);
57 } else {
58 if ((*cras & 4))
59 if ((*cras & 7) == 7)
60 thischar += getlong(cras + 1)
61 * ((getlong(cras + 5) + 7) >> 3);
62 else
63 thischar += (cras[3] * 256L + cras[4]) *
64 ((cras[1] * 256L + cras[2] + 7) >> 3);
65 else
66 thischar += ((long)(cras[2] * ((cras[1] + 7) >> 3)));
67 }
68 cd->flags |= THISPAGE;
69 } else
70 thischar = 0;
71 fontmem -= thischar;
72 if (fontmem <= pagecost) {
73 if (pagecount > 0)
74 return(0);
75 }
76 }
77 return(1);
78 }
79 /*
80 * When a font is selected during the prescan, this routine makes sure that
81 * the tfm or pk file is loaded and charges fontmem for the VM overhead in
82 * creating the font. The result is true unless the section must be aborted
83 * in order to keep this font from overflowing VM.
84 */
85 Boolean
86 preselectfont(fontdesctype *f)
87 {
88 curfnt = f;
89 if (curfnt->loaded == 0 || curfnt->loaded == 3) {
90 if (!residentfont(curfnt))
91 if (!virtualfont(curfnt))
92 loadfont(curfnt);
93 }
94 if (curfnt->psname == 0) {
95 if (curfnt->loaded < 2) { /* virtual font costs nothing (yet) */
96 if (curfnt->resfont)
97 fontmem -= PSFONTCOST;
98 else {
99 fontmem -= FONTCOST + curfnt->maxchars;
100 if (curfnt->loadeddpi != curfnt->dpi)
101 fontmem -= 48; /* for the new font matrix */
102 }
103 }
104 curfnt->psname = 1;
105 if (fontmem <= pagecost) {
106 if (pagecount > 0)
107 return(0);
108 }
109 }
110 return(1);
111 }
112 /*
113 * Now our scanpage routine.
114 */
115 short
116 #ifdef XDVIPSK
117 scanpage(boolean parse_maps)
118 #else
119 scanpage(void)
120 #endif /* XDVIPSK */
121 {
122 #ifdef XDVIPSK
123 char buf[1024];
124 static chardesctype ctmp;
125 #endif /* XDVIPSK */
126 register shalfword cmd;
127 register chardesctype *cd;
128 register fontmaptype *cfnt = 0;
129 integer fnt;
130 integer mychar;
131 register frametype *frp = frames;
132
133 if (firstboploc == 0)
134 firstboploc = ftell(dvifile);
135 pagecost = 0;
136 #ifdef DEBUG
137 if (dd(D_PAGE))
138 #ifdef SHORTINT
139 fprintf(stderr,"Scanning page %ld\n", pagenum);
140 #else /* ~SHORTINT */
141 fprintf(stderr,"Scanning page %d\n", pagenum);
142 #endif /* ~SHORTINT */
143 #endif /* DEBUG */
144 curfnt = NULL;
145 curpos = NULL;
146
147 #ifdef XDVIPSK
148 if (!parse_maps)
149 #endif /* XDVIPSK */
150 bopcolor(0); /* IBM: color - put current colorstack to bopstackdepth */
151
152 while (1) {
153 switch (cmd=dvibyte()) {
154 case 255: /* pTeX's dir or undefined */
155 if (!noptex) {
156 dvibyte();
157 break;
158 }
159 /* illegal commands */
160 #ifndef XDVIPSK
161 case 131: case 136: case 139: /* set4, put4, bop */
162 case 247: case 248: case 249: /* pre, post, post_post */
163 #else
164 case 139: case 247: case 248: case 249: /* bop, pre, post, post_post */
165 #endif /* XDVIPSK */
166 case 250: case 251: case 252: case 253: case 254: /* undefined */
167 sprintf(errbuf,
168 "! DVI file contains unexpected command (%d)",cmd);
169 error(errbuf);
170 case 132: case 137: /* eight-byte commands setrule, putrule */
171 dvibyte();
172 dvibyte();
173 dvibyte();
174 dvibyte();
175 case 146: case 151: case 156: case 160: case 165: case 170:
176 /* four-byte commands right4, w4, x4, down4, y4, z4 */
177 dvibyte();
178 case 145: case 150: case 155: case 159: case 164: case 169:
179 /* three-byte commands right3, w3, x3, down3, y3, z3 */
180 dvibyte();
181 case 144: case 149: case 154: case 158: case 163: case 168:
182 /* two-byte commands right2, w2, x2, down2, y2, z2 */
183 dvibyte();
184 case 143: case 148: case 153: case 157: case 162: case 167:
185 /* one-byte commands right1, w1, x1, down1, y1, z1 */
186 dvibyte();
187 case 147: case 152: case 161: case 166: /* w0, x0, y0, z0 */
188 case 138: case 141: case 142: /* nop, push, pop */
189 break;
190 #ifdef XDVIPSK
191 case 131: case 136: /* set4, put4 */
192 if (noptex && noluatex) {
193 sprintf(errbuf,
194 "! DVI file contains unexpected command (%d)",cmd);
195 error(errbuf);
196 }
197 mychar = dvibyte();
198 mychar = (mychar << 8) + dvibyte();
199 mychar = (mychar << 8) + dvibyte();
200 mychar = (mychar << 8) + dvibyte();
201 goto dochar;
202 #endif /* XDVIPSK */
203 case 130: case 135: /* set3, put3 */
204 #ifndef XDVIPSK
205 if (noptex) {
206 #else
207 if (noptex && noluatex) {
208 #endif /* XDVIPSK */
209 sprintf(errbuf,
210 "! DVI file contains unexpected pTeX command (%d)",cmd);
211 error(errbuf);
212 }
213 mychar = dvibyte(); mychar = (mychar << 8) + dvibyte();
214 mychar = (mychar << 8) + dvibyte();
215 goto dochar;
216 case 129: case 134: /* set2, put2 */
217 #ifndef XDVIPSK
218 if (noomega && noptex) {
219 #else
220 if (noomega && noptex && noluatex) {
221 #endif /* XDVIPSK */
222 sprintf(errbuf,
223 "! DVI file contains unexpected command (%d)",cmd);
224 error(errbuf);
225 }
226 mychar = dvibyte(); mychar = (mychar << 8) + dvibyte();
227 goto dochar;
228 case 128: case 133: cmd = dvibyte(); /* set1, put1 commands drops through */
229 default: /* these are commands 0 (setchar0) thru 127 (setchar 127) */
230 mychar = cmd;
231 dochar:
232 #ifdef XDVIPSK
233 if (!parse_maps) {
234 #endif
235 /*
236 * We are going to approximate that each string of consecutive characters
237 * requires (length of string) bytes of PostScript VM.
238 */
239 if (curfnt==NULL)
240 error("! Bad DVI file: no font selected");
241 if (mychar>=curfnt->maxchars) {
242 #ifndef XDVIPSK
243 if (!noptex && mychar<0x1000000 && curfnt->loaded == 2 && curfnt->kind == VF_PTEX) {
244 #else
245 if ((!noptex && mychar<0x1000000 && curfnt->loaded == 2 && curfnt->kind == VF_PTEX) || !noluatex) {
246 #endif /* XDVIPSK */
247 /* fallback */
248 } else {
249 sprintf(errbuf,"! [scanpage] invalid char %d from font %.500s",
250 mychar, curfnt->name);
251 error(errbuf);
252 }
253 }
254 if (curfnt->loaded == 2) { /* scanning a virtual font character */
255 struct tft *ffont0;
256 frp->curp = curpos;
257 frp->curl = curlim;
258 frp->ff = ffont;
259 frp->curf = curfnt;
260 if (++frp == &frames[MAXFRAME] )
261 error("! virtual recursion stack overflow");
262 #ifndef XDVIPSK
263 cd = curfnt->chardesc + mychar;
264 #else
265 cd = find_chardesc(curfnt, mychar);
266 if (!cd)
267 {
268 PRINTF_PR("Error: character %d not found in the font %s\n", mychar, curfnt->name);
269 cd = &ctmp;
270 }
271 #endif /* XDVIPSK */
272 if (mychar>=curfnt->maxchars || cd->packptr == NULL) {
273 if (!noptex && mychar<0x1000000 && curfnt->kind == VF_PTEX) { /* fallback */
274 #ifdef DEBUG
275 if (dd(D_FONTS))
276 fprintf_str(stderr,
277 "We will fallback pTeX vf:%s to %s:%s\n",
278 curfnt->name, curfnt->localfonts->desc->kind==OFM_OMEGA ? "ofm" : "tfm",
279 curfnt->localfonts->desc->name);
280 #endif /* DEBUG */
281 } else {
282 fprintf(stderr, "Wrong char code: %04X\n", mychar);
283 error("! a non-existent virtual character is being used; check vf/tfm files");
284 }
285 ffont0 = curfnt->localfonts;
286 if (ffont0==NULL)
287 curfnt = NULL;
288 else if (!preselectfont(ffont0->desc))
289 goto outofmem;
290 --frp;
291 curfnt = frp->curf;
292 ffont = frp->ff;
293 curlim = frp->curl;
294 curpos = frp->curp;
295 } else {
296 curpos = cd->packptr + 2;
297 curlim = curpos + (256*(long)(*cd->packptr)+(*(cd->packptr+1)));
298 ffont = curfnt->localfonts;
299 if (ffont==NULL)
300 curfnt = NULL;
301 else if (!preselectfont(ffont->desc))
302 goto outofmem;
303 }
304 } else {
305 pagecost++;
306 #ifndef XDVIPSK
307 if (!prescanchar(curfnt->chardesc + mychar))
308 #else
309 cd = find_chardesc(curfnt, mychar);
310 if (cd == NULL) {
311 mychar = '?';
312 cd = find_chardesc(curfnt, mychar);
313 }
314 if (cd && !prescanchar(cd))
315 #endif /* XDVIPSK */
316 goto outofmem;
317 }
318 #ifdef XDVIPSK
319 }
320 #endif /* XDVIPSK */
321 break;
322 case 171: case 172: case 173: case 174: case 175: case 176: case 177:
323 case 178: case 179: case 180: case 181: case 182: case 183: case 184:
324 case 185: case 186: case 187: case 188: case 189: case 190: case 191:
325 case 192: case 193: case 194: case 195: case 196: case 197: case 198:
326 case 199: case 200: case 201: case 202: case 203: case 204: case 205:
327 case 206: case 207: case 208: case 209: case 210: case 211: case 212:
328 case 213: case 214: case 215: case 216: case 217: case 218: case 219:
329 case 220: case 221: case 222: case 223: case 224: case 225: case 226:
330 case 227: case 228: case 229: case 230: case 231: case 232: case 233:
331 case 234: case 235: case 236: case 237: case 238: /* font selection commands */
332 #ifdef XDVIPSK
333 if (parse_maps)
334 {
335 if (cmd >= 235)
336 {
337 dvibyte();
338 while (cmd-- > 235)
339 dvibyte();
340 }
341 }
342 else
343 #endif /* XDVIPSK */
344 {
345 if (cmd < 235) fnt = cmd - 171; /* fntnum0 thru fntnum63 */
346 else {
347 fnt = dvibyte(); /* fnt1 */
348 while (cmd-- > 235)
349 fnt = (fnt << 8) + dvibyte();
350 }
351 for (cfnt=ffont; cfnt; cfnt = cfnt->next)
352 if (cfnt->fontnum == fnt) goto fontfound;
353 printf("Font number %d not found\n", fnt);
354 error("! no font selected");
355 fontfound: curfnt = cfnt->desc;
356 if (!preselectfont(curfnt))
357 goto outofmem;
358 }
359 break;
360 #ifdef XDVIPSK
361 case 239: predospecial((integer)dvibyte(), 1, parse_maps); break; /* xxx1 */
362 case 240: predospecial((integer)twobytes(), 1, parse_maps); break; /* xxx2 */
363 case 241: predospecial(threebytes(), 1, parse_maps); break; /* xxx3 */
364 case 242: predospecial(signedquad(), 1, parse_maps); break; /* xxx4 */
365 #else
366 case 239: predospecial((integer)dvibyte(), 1); break; /* xxx1 */
367 case 240: predospecial((integer)twobytes(), 1); break; /* xxx2 */
368 case 241: predospecial(threebytes(), 1); break; /* xxx3 */
369 case 242: predospecial(signedquad(), 1); break; /* xxx4 */
370 #endif /* XDVIPSK */
371 case 243: case 244: case 245: case 246: /* fntdef1 */
372 #ifdef XDVIPSK
373 if (parse_maps)
374 {
375 int siz = cmd - 242;
376 dvibyte();
377 while (siz-- > 1)
378 dvibyte();
379 skipover(12);
380 skipover(dvibyte() + dvibyte());
381 }
382 else
383 #endif /* XDVIPSK */
384 fontdef(cmd-242);
385 break;
386 case 140: /* eop or end of virtual char */
387 if (curpos) {
388 --frp;
389 curfnt = frp->curf;
390 ffont = frp->ff;
391 curlim = frp->curl;
392 curpos = frp->curp;
393 break;
394 }
395 goto endofpage; /* eop */
396 }
397 }
398 endofpage:
399 if (fontmem > pagecost)
400 return(1);
401 if (pagecount > 0) {
402 return(0);
403 } /* IBM: color */
404 #ifdef SHORTINT
405 #ifndef XDVIPSK
406 fprintf(stderr, "Page %ld may be too complex to print\n", pagenum);
407 #else
408 sprintf(buf, "Page %d may be too complex to print", pagenum);
409 error(buf);
410 #endif /* XDVIPSK */
411 #else /* ~SHORTINT */
412 #ifndef XDVIPSK
413 fprintf(stderr, "Page %d may be too complex to print\n", pagenum);
414 #else
415 sprintf(buf, "Page %d may be too complex to print", pagenum);
416 error(buf);
417 #endif /* XDVIPSK */
418 #endif /* ~SHORTINT */
419 /*
420 * This case should be rare indeed. Even with only 200K of virtual memory,
421 * at 11 bytes per char, you can have 18K characters on a page.
422 */
423 return(2);
424 outofmem:
425 if (frp != frames) {
426 curpos = frames->curp;
427 curlim = frames->curl;
428 ffont = frames->ff;
429 curfnt = frames->curf;
430 }
431 return 0;
432 }