Debian Codegraph @

texk/xdvipsk/dopage.c

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