home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / PRINTING / DVIPS386.ZIP / DOPAGE.C < prev    next >
C/C++ Source or Header  |  1990-11-25  |  11KB  |  348 lines

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