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