Debian Codegraph @

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