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