home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / unixtex-6.1b-src.tgz / tar.out / contrib / unixtex / dvipsk / dopage.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  11KB  |  363 lines

  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 routines we use:
  9.  */
  10. extern void error() ;
  11. extern void pageinit() ;
  12. extern void drawrule() ;
  13. extern shalfword dvibyte() ;
  14. extern halfword twobytes() ;
  15. extern integer threebytes() ;
  16. extern integer signedquad() ;
  17. extern shalfword signedbyte() ;
  18. extern shalfword signedpair() ;
  19. extern integer signedtrio() ;
  20. extern void dospecial() ;
  21. extern void drawchar() ;
  22. extern integer scalewidth() ;
  23. extern void skipover() ;
  24. extern void bopcolor() ; /* IBM: color */
  25. extern void fontdef() ;
  26. extern void pageend() ;
  27. /*
  28.  *   Now the external variables.
  29.  */
  30. extern fontdesctype *curfnt ;
  31. extern fontdesctype *baseFonts[] ;
  32. extern fontmaptype *ffont ;
  33. extern quarterword *curpos, *curlim ;
  34. extern integer hh, vv ;
  35. extern integer hoff, voff ;
  36. /*
  37.  * CONVENTION:  conv -> horizontial converter
  38.  *        vconv -> vertical converter
  39.  */
  40. extern real conv ;
  41. extern real vconv ;
  42.  
  43. extern FILE *bitfile ;
  44. extern int actualdpi ;
  45. extern int vactualdpi ;
  46. extern frametype frames[] ;
  47. extern int maxdrift ;
  48. extern int vmaxdrift ;
  49. #ifdef EMTEX
  50. extern void emclear() ;
  51. #endif
  52.  
  53. #ifdef XENIX
  54. #define PixRound(x) ((integer)(x + (iconv >> 1)) / iconv)
  55. #define VPixRound(x) ((integer)(x + (viconv >> 1)) / viconv)
  56. #else
  57. #ifdef __THINK__
  58. #define  PixRound(x) ((integer)(x +  (iconv >> 1)) /  iconv)
  59. #define VPixRound(x) ((integer)(x + (viconv >> 1)) / viconv)
  60. #else
  61. #define PixRound(x) ((integer)(floor(((x) * conv) + 0.5)))
  62. #define VPixRound(x) ((integer)(floor(((x) * vconv) + 0.5)))
  63. #endif
  64. #endif
  65. /*
  66.  *   Now we have the dopage procedure.
  67.  *   Most error checking is suppressed because the prescan has already
  68.  *   verified that the DVI data is OK....except for stack over/underflow.
  69.  */
  70. struct dvistack {
  71.   integer hh, vv ;
  72.   integer h, v, w, x, y, z ;
  73. } stack[STACKSIZE] ;
  74. void
  75. dopage()
  76. {
  77.    register shalfword cmd ;
  78.    register integer p ;
  79.    register chardesctype *cd ;
  80.    register integer h ;
  81.    register fontmaptype *cfnt ;
  82.    register frametype *frp = frames ;
  83.    integer fnt ;
  84.    int charmove ;
  85.    struct dvistack *sp = stack ;
  86.    integer v, w, x, y, z ;
  87.    integer roundpos ;
  88.    integer thinspace ;
  89.    integer vertsmallspace ;
  90. #ifdef XENIX
  91.    integer iconv ;
  92.    integer viconv ;
  93.  
  94.    iconv = (integer)(1.0 / conv + 0.5) ;
  95.    viconv = (integer)(1.0 / vconv + 0.5) ;
  96. #else
  97. #ifdef __THINK__
  98.    integer  iconv ;
  99.    integer viconv ;
  100.  
  101.     iconv = (integer)(1.0 /  conv + 0.5) ;
  102.    viconv = (integer)(1.0 / vconv + 0.5) ;
  103. #endif
  104. #endif
  105. #ifdef EMTEX
  106.    emclear() ;
  107. #endif
  108.    pageinit() ;
  109.  
  110.    bopcolor(1) ;
  111.    thinspace =  (integer)(0.025*DPI/conv) ; /* 0.025 inches */
  112.    vertsmallspace = (integer)(0.025*VDPI/vconv) ; /* 0.025 inches */
  113.  
  114.    w = x = y = z = 0 ;
  115.    h = DPI / conv * hoff / 4736286.72 ;
  116.    v = DPI / conv * voff / 4736286.72 ;
  117.    hh = PixRound(h) ;
  118.    vv = PixRound(v) ;
  119.    curfnt = NULL ;
  120.    curpos = NULL ;
  121.    charmove = 0 ;
  122. beginloop:
  123.    switch (cmd=dvibyte()) {
  124. case 138: goto beginloop ; /* nop command does nuttin */
  125. /*
  126.  *   For put1 commands, we subtract the width of the character before
  127.  *   dropping through to the normal character setting routines.  This
  128.  */
  129. case 133: /* put1 */
  130.    cmd = dvibyte() ;
  131.    charmove = 0 ;
  132.    goto dochar ;
  133. case 128: cmd = dvibyte() ; /* set1 command drops through to setchar */
  134. default: /* these are commands 0 (setchar0) thru 127 (setchar127) */
  135.    charmove = 1 ;
  136. dochar:
  137.    cd = &(curfnt->chardesc[cmd]) ;
  138.    if (cd->flags & EXISTS) {
  139.       if (curfnt->loaded == 2) { /* virtual character being typeset */
  140.          if (charmove) {
  141.             sp->hh = hh + cd->pixelwidth ;
  142.             sp->h = h + cd->TFMwidth ;
  143.          } else {
  144.             sp->hh = hh ; sp->h = h ;
  145.          }
  146.          sp->vv = vv ; sp-> v = v ;
  147.          sp->w = w ; sp->x = x ; sp->y = y ; sp->z = z ;
  148.          if (++sp >= &stack[STACKSIZE]) error("! Out of stack space") ;
  149.          w = x = y = z = 0 ; /* will be in relative units at new stack level */
  150.          frp->curp = curpos ;
  151.          frp->curl = curlim ;
  152.          frp->ff = ffont ;
  153.          frp->curf = curfnt ;
  154.          if (++frp == &frames[MAXFRAME] )
  155.             error("! virtual recursion stack overflow") ;
  156.          curpos = cd->packptr + 2 ;
  157.          curlim = curpos + (256*(long)(*cd->packptr)+(*(cd->packptr+1))) ;
  158.          ffont = curfnt->localfonts ;
  159.          if (ffont) {
  160.             curfnt = ffont->desc ;
  161.             thinspace = curfnt->thinspace ;
  162.          } else {
  163.             curfnt = NULL ;
  164.             thinspace = vertsmallspace ;
  165.          }
  166.          goto beginloop ;
  167.       }
  168.       drawchar(cd, cmd) ;
  169.    }
  170.    if (charmove) {
  171.       h += cd->TFMwidth ;
  172.       hh += cd->pixelwidth ;
  173.    }
  174.    goto setmotion ;
  175. case 129: case 130: case 131: case 134: case 135: case 136: case 139: 
  176. case 247: case 248: case 249: case 250: case 251: case 252: case 253:
  177. case 254: case 255: /* unimplemented or illegal commands */
  178.    error("! synch") ;
  179. case 132: case 137: /* rules */
  180.  { integer ry, rx , rxx, ryy ;
  181.    ry = signedquad() ; rx = signedquad() ;
  182.    if (rx>0 && ry>0) {
  183.       if (curpos) {
  184.          rx = scalewidth(rx, (frp-1)->curf->scaledsize) ;
  185.          ry = scalewidth(ry, (frp-1)->curf->scaledsize) ;
  186.       }
  187.       rxx = (int)(conv * rx + 0.9999999) ;
  188.       ryy = (int)(vconv * ry + 0.9999999) ;
  189.       drawrule(rxx, ryy) ;
  190.    } else
  191.       rxx = 0 ;
  192.    if (cmd == 137) goto beginloop ;
  193.    h += rx ; hh += rxx ;
  194.    goto setmotion ;
  195.  }
  196. case 141: /* push */
  197.    sp->hh = hh ; sp->vv = vv ; sp->h = h ; sp->v = v ;
  198.    sp->w = w ; sp->x = x ; sp->y = y ; sp->z = z ;
  199.    if (++sp >= &stack[STACKSIZE]) error("! Out of stack space") ;
  200.    goto beginloop ;
  201. case 140: /* eop or end of virtual character */
  202.    if (curpos == NULL) break ; /* eop */
  203.    --frp ;
  204.    curfnt = frp->curf ;
  205.    thinspace = (curfnt) ? curfnt->thinspace : vertsmallspace ;
  206.    ffont = frp->ff ;
  207.    curlim = frp->curl ;
  208.    curpos = frp->curp ;
  209.    if (hh < (sp-1)->hh+2 && hh > (sp-1)->hh-2)
  210.       (sp-1)->hh = hh; /* retain `intelligence' of pixel width, if close */ 
  211.    /* falls through */
  212. case 142: /* pop */
  213.    if (--sp < stack) error("! More pops than pushes") ;
  214.    hh = sp->hh ; vv = sp->vv ; h = sp->h ; v = sp->v ;
  215.    w = sp->w ; x = sp->x ; y = sp->y ; z = sp->z ;
  216.    goto beginloop ;
  217. case 143: /* right1 */
  218.    p = signedbyte() ; goto horizontalmotion ;
  219. case 144: /* right2 */
  220.    p = signedpair() ; goto horizontalmotion ;
  221. case 145: /* right3 */
  222.    p = signedtrio() ; goto horizontalmotion ;
  223. case 146: /* right4 */
  224.    p = signedquad() ; goto horizontalmotion ;
  225. case 147: /* w0 */
  226.    p = w ; goto horizontalmotion ;
  227. case 148: /* w1 */
  228.    p = w = signedbyte() ; goto horizontalmotion ;
  229. case 149: /* w2 */
  230.    p = w = signedpair() ; goto horizontalmotion ;
  231. case 150: /* w3 */
  232.    p = w = signedtrio() ; goto horizontalmotion ;
  233. case 151: /* w4 */
  234.    p = w = signedquad() ; goto horizontalmotion ;
  235. case 152: /* x0 */
  236.    p = x ; goto horizontalmotion ;
  237. case 153: /* x1 */
  238.    p = x = signedbyte() ; goto horizontalmotion ;
  239. case 154: /* x2 */
  240.    p = x = signedpair() ; goto horizontalmotion ;
  241. case 155: /* x3 */
  242.    p = x = signedtrio() ; goto horizontalmotion ;
  243. case 156: /* x4 */
  244.    p = x = signedquad() ; goto horizontalmotion ;
  245. case 157: /* down1 */
  246.    p = signedbyte() ; goto verticalmotion ;
  247. case 158: /* down2 */
  248.    p = signedpair() ; goto verticalmotion ;
  249. case 159: /* down3 */
  250.    p = signedtrio() ; goto verticalmotion ;
  251. case 160: /* down4 */
  252.    p = signedquad() ; goto verticalmotion ;
  253. case 161: /* y0 */
  254.    p = y ; goto verticalmotion ;
  255. case 162: /* y1 */
  256.    p = y = signedbyte() ; goto verticalmotion ;
  257. case 163: /* y2 */
  258.    p = y = signedpair() ; goto verticalmotion ;
  259. case 164: /* y3 */
  260.    p = y = signedtrio() ; goto verticalmotion ;
  261. case 165: /* y4 */
  262.    p = y = signedquad() ; goto verticalmotion ;
  263. case 166: /* z0 */
  264.    p = z ; goto verticalmotion ;
  265. case 167: /* z1 */
  266.    p = z = signedbyte() ; goto verticalmotion ;
  267. case 168: /* z2 */
  268.    p = z = signedpair() ; goto verticalmotion ;
  269. case 169: /* z3 */
  270.    p = z = signedtrio() ; goto verticalmotion ;
  271. case 170: /* z4 */
  272.    p = z = signedquad() ; goto verticalmotion ;
  273. case 171: case 172: case 173: case 174: case 175: case 176: case 177:
  274. case 178: case 179: case 180: case 181: case 182: case 183: case 184:
  275. case 185: case 186: case 187: case 188: case 189: case 190: case 191:
  276. case 192: case 193: case 194: case 195: case 196: case 197: case 198:
  277. case 199: case 200: case 201: case 202: case 203: case 204: case 205:
  278. case 206: case 207: case 208: case 209: case 210: case 211: case 212:
  279. case 213: case 214: case 215: case 216: case 217: case 218: case 219:
  280. case 220: case 221: case 222: case 223: case 224: case 225: case 226:
  281. case 227: case 228: case 229: case 230: case 231: case 232: case 233:
  282. case 234: case 235: case 236: case 237: case 238: /* font selection commands */
  283.    if (cmd < 235) fnt = cmd - 171 ; /* fntnum0 thru fntnum63 */
  284.    else {
  285.       fnt = dvibyte() ;
  286.       while (cmd-- > 235)
  287.          fnt = (fnt << 8) + dvibyte() ;
  288.    }
  289.    if (curpos || fnt > 255) {
  290.       for (cfnt=ffont; cfnt; cfnt = cfnt->next)
  291.          if (cfnt->fontnum == fnt) break ;
  292.       curfnt = cfnt->desc ;
  293.    } else
  294.       curfnt = baseFonts[fnt] ;
  295.    thinspace = curfnt->thinspace ;
  296.    goto beginloop ;
  297. case 243: case 244: case 245: case 246: /*fntdef1 */
  298.    skipover(cmd - 230) ;
  299.    skipover(dvibyte() + dvibyte()) ;
  300.    goto beginloop ;
  301. case 239: /* xxx1 */
  302.    p = dvibyte() ;
  303.    dospecial(p) ;
  304.    goto beginloop ;
  305. case 240: /* xxx2 */
  306.    p = twobytes() ;
  307.    dospecial(p) ;
  308.    goto beginloop ;
  309. case 241: /* xxx3 */
  310.    p = threebytes() ;
  311.    dospecial(p) ;
  312.    goto beginloop ;
  313. case 242: /* xxx4 */
  314.    p = signedquad() ;
  315.    dospecial(p) ;
  316.    goto beginloop ;
  317.  
  318. /*
  319.  *   The calculations here are crucial to the appearance of the document.
  320.  *   If the motion is small, we round the amount of relative motion; otherwise,
  321.  *   we update the position and round the new position.  Then we check to
  322.  *   insure that the rounded position didn't accumulate an error that was
  323.  *   greater than maxdrift.
  324.  */
  325. verticalmotion:
  326. /* vertical motion cases */
  327.       if (curpos)
  328.          p = scalewidth(p, (frp-1)->curf->scaledsize) ;
  329.       v += p ;
  330.       if (p >= vertsmallspace) vv = VPixRound(v) ;
  331.       else if (p <= -vertsmallspace) vv = VPixRound(v) ;
  332.       else 
  333.       { vv += VPixRound(p) ;
  334.         roundpos = VPixRound(v) ;
  335.         if (roundpos - vv > vmaxdrift) vv = roundpos - vmaxdrift ;
  336.         else if (vv - roundpos > vmaxdrift) vv = roundpos + vmaxdrift ;
  337.       }
  338.       goto beginloop ;
  339. /*
  340.  *   Horizontal motion is analogous. We know the exact width of each
  341.  *   character in pixels. Kerning is distinguished from space between
  342.  *   words if it's less than a thinspace and not more negative than would
  343.  *   occur when an accent is being positioned by backspacing.
  344.  */
  345. horizontalmotion:
  346. /* horizontal motion cases */
  347.       if (curpos)
  348.          p = scalewidth(p, (frp-1)->curf->scaledsize) ;
  349.       h += p ;
  350.       if (p >= thinspace || p <= -6 * thinspace) {
  351.          hh = PixRound(h) ; goto beginloop ;
  352.       }
  353.       else hh += PixRound(p) ;
  354. setmotion:
  355.       roundpos = PixRound(h) ;
  356.       if (roundpos - hh > maxdrift) { hh = roundpos - maxdrift ; }
  357.       else if (hh - roundpos > maxdrift) { hh = roundpos + maxdrift ; }
  358. goto beginloop ;
  359.  
  360.    } /* end of the big switch */
  361.    pageend() ;
  362. }
  363.