Debian Codegraph @

texk/dvipsk/dopage.c

raw
1 /*
2 * Main page drawing procedure. Interprets the page commands. A simple
3 * (if lengthy) case statement interpreter.
4 */
5 #include "dvips.h" /* The copyright notice in that file is included too! */
6 #include <math.h>
7 /*
8 * The external declarations:
9 */
10 #include "protos_add.h"
11
12 #if defined(XENIX) || defined(__THINK__)
13 #define PixRound(x) ((integer)(x + (iconv >> 1)) / iconv)
14 #define VPixRound(x) ((integer)(x + (viconv >> 1)) / viconv)
15 #else
16 #define PixRound(x) ((integer)(floor(((x) * conv) + 0.5)))
17 #define VPixRound(x) ((integer)(floor(((x) * vconv) + 0.5)))
18 #endif
19 /*
20 * Now we have the dopage procedure.
21 * Most error checking is suppressed because the prescan has already
22 * verified that the DVI data is OK....except for stack over/underflow.
23 */
24 struct dvistack stack[STACKSIZE];
25 integer dir;
26 #ifdef HPS
27 integer hhmem, vvmem;
28 integer pushcount = 0;
29 Boolean PAGEUS_INTERUPPTUS = 0;
30 Boolean NEED_NEW_BOX = 0;
31 integer H_BREAK; /* An empirical parameter for guessing line breaks; needs
32 dpi dependence */
33 #endif
34
35 void
36 dopage(void)
37 {
38 register shalfword cmd;
39 register integer p;
40 register chardesctype *cd;
41 register integer h;
42 register fontmaptype *cfnt;
43 register frametype *frp = frames;
44 integer fnt;
45 integer mychar;
46 int charmove;
47 struct dvistack *sp = stack;
48 integer v, w, x, y, z;
49 integer roundpos;
50 integer thinspace;
51 integer vertsmallspace;
52 #if defined(XENIX) || defined(__THINK__)
53 integer iconv;
54 integer viconv;
55
56 iconv = (integer)(1.0 / conv + 0.5);
57 viconv = (integer)(1.0 / vconv + 0.5);
58 #endif
59 #ifdef EMTEX
60 emclear();
61 #endif
62 pageinit();
63
64 bopcolor(1);
65 thinspace = (integer)(0.025*DPI/conv); /* 0.025 inches */
66 vertsmallspace = (integer)(0.025*VDPI/vconv); /* 0.025 inches */
67 #ifdef HPS
68 if (HPS_FLAG) pagecounter++;
69 H_BREAK = (30 * DPI / 400 ); /* 30 seems to have worked well at 400 dpi */
70 #endif
71 w = x = y = z = dir = rdir = fdir = 0;
72 h = (integer) (DPI / conv * hoff / 4736286.72);
73 v = (integer) (DPI / conv * voff / 4736286.72);
74 hh = PixRound(h);
75 vv = PixRound(v);
76 curfnt = NULL;
77 curpos = NULL;
78 charmove = 0;
79 beginloop:
80 switch (cmd=dvibyte()) {
81 case 138: goto beginloop; /* nop command does nuttin */
82 /*
83 * For put1 commands, we subtract the width of the character before
84 * dropping through to the normal character setting routines. This
85 */
86 case 135: /* put3 */
87 if (noptex) error("! synch");
88 mychar = dvibyte();
89 mychar = (mychar << 8) + dvibyte();
90 mychar = (mychar << 8) + dvibyte();
91 charmove = 0;
92 goto dochar;
93 case 130: /* set3 */
94 if (noptex) error("! synch");
95 mychar = dvibyte();
96 mychar = (mychar << 8) + dvibyte();
97 mychar = (mychar << 8) + dvibyte();
98 charmove = 1;
99 goto dochar;
100 case 134: /* put2 */
101 if (noomega && noptex) error("! synch");
102 mychar = dvibyte();
103 mychar = (mychar << 8) + dvibyte();
104 charmove = 0;
105 goto dochar;
106 case 129: /* set2 */
107 if (noomega && noptex) error("! synch");
108 mychar = dvibyte();
109 mychar = (mychar << 8) + dvibyte();
110 charmove = 1;
111 goto dochar;
112 case 133: /* put1 */
113 mychar = dvibyte();
114 charmove = 0;
115 goto dochar;
116 case 128: cmd = dvibyte(); /* set1 command drops through to setchar */
117 default: /* these are commands 0 (setchar0) thru 127 (setchar127) */
118 mychar = cmd;
119 charmove = 1;
120 dochar:
121 #ifdef HPS
122 if (HPS_FLAG && PAGEUS_INTERUPPTUS) {
123 HREF_COUNT--;
124 start_new_box();
125 PAGEUS_INTERUPPTUS = 0;
126 }
127 if (HPS_FLAG && NEED_NEW_BOX) {
128 vertical_in_hps();
129 NEED_NEW_BOX = 0;
130 }
131 #endif
132 if (mychar<curfnt->maxchars)
133 cd = &(curfnt->chardesc[mychar]);
134 if (!noptex && mychar<0x1000000 && curfnt->loaded == 2 && curfnt->kind == VF_PTEX) {
135 if (mychar>=curfnt->maxchars || !(cd->flags & EXISTS)) {
136 /* try fallback */
137 chardesctype *cd0;
138 int kind0;
139 cd0 = curfnt->localfonts->desc->chardesc;
140 kind0 = curfnt->localfonts->desc->kind;
141 if (mychar<curfnt->localfonts->desc->maxchars && (cd0[mychar].flags2 & EXISTS)) {
142 #ifdef DEBUG
143 if (dd(D_FONTS))
144 fprintf_str(stderr,
145 "Fallback pTeX vf:%s char=%d(0x%06x) to %s:%s\n",
146 curfnt->name, mychar, mychar, kind0==OFM_OMEGA ? "ofm" : "tfm",
147 curfnt->localfonts->desc->name);
148 #endif /* DEBUG */
149 cd0 = &(cd0[mychar]);
150 } else {
151 fprintf_str(stderr,
152 "Warning: Failed to fallback pTeX vf:%s char=%d(0x%06x) to %s:%s\n",
153 curfnt->name, mychar, mychar, kind0==OFM_OMEGA ? "ofm" : "tfm",
154 curfnt->localfonts->desc->name);
155 }
156 if (charmove) {
157 if (!dir) {
158 sp->hh = hh + cd0->pixelwidth;
159 sp->h = h + cd0->TFMwidth;
160 } else {
161 sp->v = v + cd0->TFMwidth;
162 sp->vv = PixRound(sp->v);
163 }
164 } else {
165 if (!dir) {
166 sp->hh = hh; sp->h = h;
167 } else {
168 sp->vv = vv; sp->v = v;
169 }
170 }
171 if (!dir) {
172 sp->vv = vv; sp->v = v;
173 } else {
174 sp->hh = hh; sp->h = h;
175 }
176 sp->w = w; sp->x = x; sp->y = y; sp->z = z; sp->dir = dir;
177 if (++sp >= &stack[STACKSIZE]) error("! Out of stack space");
178 w = x = y = z = 0; /* will be in relative units at new stack level */
179 frp->ff = ffont;
180 frp->curf = curfnt;
181 if (++frp == &frames[MAXFRAME] )
182 error("! virtual recursion stack overflow");
183 ffont = curfnt->localfonts;
184 if (ffont) {
185 curfnt = ffont->desc;
186 thinspace = curfnt->thinspace;
187 } else {
188 curfnt = NULL;
189 thinspace = vertsmallspace;
190 }
191 drawchar(cd, mychar);
192 goto end_of_vf;
193 }
194 }
195 if (cd->flags & EXISTS) {
196 if (curfnt->loaded == 2) { /* virtual character being typeset */
197 if (charmove) {
198 if (!dir) {
199 sp->hh = hh + cd->pixelwidth;
200 sp->h = h + cd->TFMwidth;
201 } else {
202 sp->v = v + cd->TFMwidth;
203 sp->vv = PixRound(sp->v);
204 }
205 } else {
206 if (!dir) {
207 sp->hh = hh; sp->h = h;
208 } else {
209 sp->vv = vv; sp->v = v;
210 }
211 }
212 if (!dir) {
213 sp->vv = vv; sp->v = v;
214 } else {
215 sp->hh = hh; sp->h = h;
216 }
217 sp->w = w; sp->x = x; sp->y = y; sp->z = z; sp->dir = dir;
218 if (++sp >= &stack[STACKSIZE]) error("! Out of stack space");
219 w = x = y = z = 0; /* will be in relative units at new stack level */
220 frp->curp = curpos;
221 frp->curl = curlim;
222 frp->ff = ffont;
223 frp->curf = curfnt;
224 if (++frp == &frames[MAXFRAME] )
225 error("! virtual recursion stack overflow");
226 curpos = cd->packptr + 2;
227 curlim = curpos + (256*(long)(*cd->packptr)+(*(cd->packptr+1)));
228 ffont = curfnt->localfonts;
229 if (ffont) {
230 curfnt = ffont->desc;
231 thinspace = curfnt->thinspace;
232 } else {
233 curfnt = NULL;
234 thinspace = vertsmallspace;
235 }
236 goto beginloop;
237 }
238 drawchar(cd, mychar);
239 }
240 if (charmove) {
241 if (!dir) {
242 h += cd->TFMwidth;
243 hh += cd->pixelwidth;
244 } else {
245 v += cd->TFMwidth;
246 vv += cd->pixelwidth;
247 }
248 }
249 goto setmotion;
250 case 255: /* pTeX's dir or undefined */
251 if (!noptex) {
252 dir = dvibyte();
253 cmddir();
254 goto beginloop;
255 }
256 /* illegal commands */
257 case 131: case 136: case 139: /* set4, put4, bop */
258 case 247: case 248: case 249: /* pre, post, post_post */
259 case 250: case 251: case 252: case 253: case 254: /* undefined */
260 error("! synch");
261 case 132: case 137: /* rules */
262 { integer ry, rx , rxx, ryy;
263 ry = signedquad(); rx = signedquad();
264 if (rx>0 && ry>0) {
265 if (curpos) {
266 rx = scalewidth(rx, (frp-1)->curf->scaledsize);
267 ry = scalewidth(ry, (frp-1)->curf->scaledsize);
268 }
269 if (!dir) {
270 rxx = (int)(conv * rx + 0.9999999);
271 ryy = (int)(vconv * ry + 0.9999999);
272 }
273 else {
274 rxx = (int)(vconv * rx + 0.9999999);
275 ryy = (int)(conv * ry + 0.9999999);
276 }
277 /* Heiko Oberdiek 2001/06/03: synchronisation added for vertical rules
278 because of alignment reasons.
279 */
280 if (ry > rx) {
281 hh = PixRound(h);
282 }
283 drawrule(rxx, ryy);
284 } else
285 rxx = 0;
286 if (cmd == 137) goto beginloop;
287 if (!dir) {
288 h += rx; hh += rxx;
289 }
290 else {
291 v += rx; vv += rxx;
292 }
293 goto setmotion;
294 }
295 case 141: /* push */
296 #ifdef HPS
297 if (HPS_FLAG) pushcount++;
298 /* if (HPS_FLAG && PAGEUS_INTERUPPTUS) {
299 HREF_COUNT--;
300 start_new_box();
301 PAGEUS_INTERUPPTUS = 0;
302 } */
303 if (HPS_FLAG && NEED_NEW_BOX) {
304 vertical_in_hps();
305 NEED_NEW_BOX = 0;
306 }
307 /* printf("push %i, %i\n", pushcount, inHTMLregion); */
308 #endif
309 sp->hh = hh; sp->vv = vv; sp->h = h; sp->v = v;
310 sp->w = w; sp->x = x; sp->y = y; sp->z = z; sp->dir = dir;
311 if (++sp >= &stack[STACKSIZE]) error("! Out of stack space");
312 goto beginloop;
313 case 140: /* eop or end of virtual character */
314 if (curpos == NULL) { /* eop */
315 #ifdef HPS
316 if (HPS_FLAG && inHTMLregion) PAGEUS_INTERUPPTUS = 1;
317 /* printf("Page interrupted"); */
318 #endif
319 break;
320 }
321 end_of_vf:
322 --frp;
323 curfnt = frp->curf;
324 thinspace = (curfnt) ? curfnt->thinspace : vertsmallspace;
325 ffont = frp->ff;
326 curlim = frp->curl;
327 curpos = frp->curp;
328 if (hh < (sp-1)->hh+2 && hh > (sp-1)->hh-2)
329 (sp-1)->hh = hh; /* retain `intelligence' of pixel width, if close */
330 /* falls through */
331 case 142: /* pop */
332 #ifdef HPS
333 pushcount--;
334 /* printf("pop %i\n", pushcount); */
335 #endif
336 if (--sp < stack) error("! More pops than pushes");
337 #ifdef HPS
338 if (HPS_FLAG) {
339 hhmem = hh; vvmem = vv;
340 }
341 #endif
342 hh = sp->hh; vv = sp->vv; h = sp->h; v = sp->v;
343 w = sp->w; x = sp->x; y = sp->y; z = sp->z; dir = sp->dir;
344 #ifdef HPS
345 if (HPS_FLAG && inHTMLregion && (hhmem - hh > H_BREAK) && (pushcount > 0) &&
346 (pushcount < current_pushcount))
347 end_current_box();
348 #endif
349 cmddir();
350 goto beginloop;
351 case 143: /* right1 */
352 p = signedbyte(); goto horizontalmotion;
353 case 144: /* right2 */
354 p = signedpair(); goto horizontalmotion;
355 case 145: /* right3 */
356 p = signedtrio(); goto horizontalmotion;
357 case 146: /* right4 */
358 p = signedquad(); goto horizontalmotion;
359 case 147: /* w0 */
360 p = w; goto horizontalmotion;
361 case 148: /* w1 */
362 p = w = signedbyte(); goto horizontalmotion;
363 case 149: /* w2 */
364 p = w = signedpair(); goto horizontalmotion;
365 case 150: /* w3 */
366 p = w = signedtrio(); goto horizontalmotion;
367 case 151: /* w4 */
368 p = w = signedquad(); goto horizontalmotion;
369 case 152: /* x0 */
370 p = x; goto horizontalmotion;
371 case 153: /* x1 */
372 p = x = signedbyte(); goto horizontalmotion;
373 case 154: /* x2 */
374 p = x = signedpair(); goto horizontalmotion;
375 case 155: /* x3 */
376 p = x = signedtrio(); goto horizontalmotion;
377 case 156: /* x4 */
378 p = x = signedquad(); goto horizontalmotion;
379 case 157: /* down1 */
380 p = signedbyte(); goto verticalmotion;
381 case 158: /* down2 */
382 p = signedpair(); goto verticalmotion;
383 case 159: /* down3 */
384 p = signedtrio(); goto verticalmotion;
385 case 160: /* down4 */
386 p = signedquad(); goto verticalmotion;
387 case 161: /* y0 */
388 p = y; goto verticalmotion;
389 case 162: /* y1 */
390 p = y = signedbyte(); goto verticalmotion;
391 case 163: /* y2 */
392 p = y = signedpair(); goto verticalmotion;
393 case 164: /* y3 */
394 p = y = signedtrio(); goto verticalmotion;
395 case 165: /* y4 */
396 p = y = signedquad(); goto verticalmotion;
397 case 166: /* z0 */
398 p = z; goto verticalmotion;
399 case 167: /* z1 */
400 p = z = signedbyte(); goto verticalmotion;
401 case 168: /* z2 */
402 p = z = signedpair(); goto verticalmotion;
403 case 169: /* z3 */
404 p = z = signedtrio(); goto verticalmotion;
405 case 170: /* z4 */
406 p = z = signedquad(); goto verticalmotion;
407 case 171: case 172: case 173: case 174: case 175: case 176: case 177:
408 case 178: case 179: case 180: case 181: case 182: case 183: case 184:
409 case 185: case 186: case 187: case 188: case 189: case 190: case 191:
410 case 192: case 193: case 194: case 195: case 196: case 197: case 198:
411 case 199: case 200: case 201: case 202: case 203: case 204: case 205:
412 case 206: case 207: case 208: case 209: case 210: case 211: case 212:
413 case 213: case 214: case 215: case 216: case 217: case 218: case 219:
414 case 220: case 221: case 222: case 223: case 224: case 225: case 226:
415 case 227: case 228: case 229: case 230: case 231: case 232: case 233:
416 case 234: case 235: case 236: case 237: case 238: /* font selection commands */
417 if (cmd < 235) fnt = cmd - 171; /* fntnum0 thru fntnum63 */
418 else {
419 fnt = dvibyte();
420 while (cmd-- > 235)
421 fnt = (fnt << 8) + dvibyte();
422 }
423 for (cfnt=ffont; cfnt; cfnt = cfnt->next)
424 if (cfnt->fontnum == fnt) break;
425 curfnt = cfnt->desc;
426 thinspace = curfnt->thinspace;
427 goto beginloop;
428 case 243: case 244: case 245: case 246: /*fntdef1 */
429 skipover(cmd - 230);
430 skipover(dvibyte() + dvibyte());
431 goto beginloop;
432 case 239: /* xxx1 */
433 p = dvibyte();
434 dospecial(p);
435 goto beginloop;
436 case 240: /* xxx2 */
437 p = twobytes();
438 dospecial(p);
439 goto beginloop;
440 case 241: /* xxx3 */
441 p = threebytes();
442 dospecial(p);
443 goto beginloop;
444 case 242: /* xxx4 */
445 p = signedquad();
446 dospecial(p);
447 goto beginloop;
448
449 /*
450 * The calculations here are crucial to the appearance of the document.
451 * If the motion is small, we round the amount of relative motion; otherwise,
452 * we update the position and round the new position. Then we check to
453 * insure that the rounded position didn't accumulate an error that was
454 * greater than maxdrift.
455 */
456 verticalmotion:
457 /* vertical motion cases */
458 if (curpos)
459 p = scalewidth(p, (frp-1)->curf->scaledsize);
460 if (!dir) {
461 v += p;
462 if (p >= vertsmallspace) vv = VPixRound(v);
463 else if (p <= -vertsmallspace) vv = VPixRound(v);
464 else
465 { vv += VPixRound(p);
466 roundpos = VPixRound(v);
467 if (roundpos - vv > vmaxdrift) vv = roundpos - vmaxdrift;
468 else if (vv - roundpos > vmaxdrift) vv = roundpos + vmaxdrift;
469 }
470 } else {
471 h -= p;
472 if (p >= vertsmallspace) hh = VPixRound(h);
473 else if (p <= -vertsmallspace) hh = VPixRound(h);
474 else
475 { hh -= VPixRound(p);
476 roundpos = VPixRound(h);
477 if (roundpos - hh > vmaxdrift) hh = roundpos - vmaxdrift;
478 else if (hh - roundpos > vmaxdrift) hh = roundpos + vmaxdrift;
479 }
480 }
481 #ifdef HPS
482 /* printf("Doing vertical motion: p = %i, v = %i, vv = %i\n",p,v,vv); */
483 /* printf("inHTMLregion %i\n", inHTMLregion); */
484 if (HPS_FLAG && inHTMLregion) NEED_NEW_BOX = 1 /* vertical_in_hps() */;
485 #endif
486 goto beginloop;
487 /*
488 * Horizontal motion is analogous. We know the exact width of each
489 * character in pixels. Kerning is distinguished from space between
490 * words if it's less than a thinspace and not more negative than would
491 * occur when an accent is being positioned by backspacing.
492 */
493 horizontalmotion:
494 /* horizontal motion cases */
495 if (curpos)
496 p = scalewidth(p, (frp-1)->curf->scaledsize);
497 if (!dir) {
498 h += p;
499 if (p >= thinspace || p <= -6 * thinspace) {
500 hh = PixRound(h); goto beginloop;
501 }
502 else hh += PixRound(p);
503 } else {
504 v += p;
505 if (p >= thinspace || p <= -6 * thinspace) {
506 vv = PixRound(v); goto beginloop;
507 }
508 else vv += PixRound(p);
509 }
510 #ifdef HPS
511 /* printf("Doing horizontal motion: p = %i, h = %i, hh = %i\n",p,h,hh); */
512 #endif
513 setmotion:
514 if (!dir) {
515 roundpos = PixRound(h);
516 if (roundpos - hh > maxdrift) { hh = roundpos - maxdrift; }
517 else if (hh - roundpos > maxdrift) { hh = roundpos + maxdrift; }
518 } else {
519 roundpos = PixRound(v);
520 if (roundpos - vv > maxdrift) { vv = roundpos - maxdrift; }
521 else if (vv - roundpos > maxdrift) { vv = roundpos + maxdrift; }
522 }
523 goto beginloop;
524
525 } /* end of the big switch */
526 pageend();
527 }