home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / ddx / mfb / mfbline.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-16  |  21.9 KB  |  982 lines

  1. /***********************************************************
  2. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
  3. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Digital or MIT not be
  12. used in advertising or publicity pertaining to distribution of the
  13. software without specific, written prior permission.  
  14.  
  15. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  16. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  17. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  18. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  19. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21. SOFTWARE.
  22.  
  23. ******************************************************************/
  24. /* $XConsortium: mfbline.c,v 5.12 91/08/13 18:49:01 keith Exp $ */
  25. #include "X.h"
  26.  
  27. #include "gcstruct.h"
  28. #include "windowstr.h"
  29. #include "pixmapstr.h"
  30. #include "regionstr.h"
  31. #include "scrnintstr.h"
  32. #include "mistruct.h"
  33.  
  34. #include "mfb.h"
  35. #include "maskbits.h"
  36.  
  37. /* single-pixel lines on a color frame buffer
  38.  
  39.    NON-SLOPED LINES
  40.    horizontal lines are always drawn left to right; we have to
  41. move the endpoints right by one after they're swapped.
  42.    horizontal lines will be confined to a single band of a
  43. region.  the code finds that band (giving up if the lower
  44. bound of the band is above the line we're drawing); then it
  45. finds the first box in that band that contains part of the
  46. line.  we clip the line to subsequent boxes in that band.
  47.    vertical lines are always drawn top to bottom (y-increasing.)
  48. this requires adding one to the y-coordinate of each endpoint
  49. after swapping.
  50.  
  51.    SLOPED LINES
  52.    when clipping a sloped line, we bring the second point inside
  53. the clipping box, rather than one beyond it, and then add 1 to
  54. the length of the line before drawing it.  this lets us use
  55. the same box for finding the outcodes for both endpoints.  since
  56. the equation for clipping the second endpoint to an edge gives us
  57. 1 beyond the edge, we then have to move the point towards the
  58. first point by one step on the major axis.
  59.    eventually, there will be a diagram here to explain what's going
  60. on.  the method uses Cohen-Sutherland outcodes to determine
  61. outsideness, and a method similar to Pike's layers for doing the
  62. actual clipping.
  63.  
  64. */
  65.  
  66. #define OUTCODES(result, x, y, pbox) \
  67.     if (x < pbox->x1) \
  68.     result |= OUT_LEFT; \
  69.     else if (x >= pbox->x2) \
  70.     result |= OUT_RIGHT; \
  71.     if (y < pbox->y1) \
  72.     result |= OUT_ABOVE; \
  73.     else if (y >= pbox->y2) \
  74.     result |= OUT_BELOW;
  75.  
  76. #define round(dividend, divisor) \
  77. ( (((dividend)<<1) + (divisor)) / ((divisor)<<1) )
  78. #define ceiling(m,n)  (((m)-1)/(n) + 1)
  79.  
  80. /*
  81. #define SignTimes(sign, n) ((sign) * ((int)(n)))
  82. */
  83.  
  84. #define SignTimes(sign, n) \
  85.     ( ((sign)<0) ? -(n) : (n) )
  86.  
  87. #define SWAPINT(i, j) \
  88. {  register int _t = i; \
  89.    i = j; \
  90.    j = _t; \
  91. }
  92.  
  93. #define SWAPPT(i, j) \
  94. {  DDXPointRec _t; \
  95.    _t = i; \
  96.    i = j; \
  97.    j = _t; \
  98. }
  99.    
  100.  
  101. void
  102. #ifdef POLYSEGMENT
  103. mfbSegmentSS (pDrawable, pGC, nseg, pSeg)
  104.     DrawablePtr    pDrawable;
  105.     GCPtr    pGC;
  106.     int        nseg;
  107.     register xSegment    *pSeg;
  108. #else
  109. mfbLineSS (pDrawable, pGC, mode, npt, pptInit)
  110.     DrawablePtr pDrawable;
  111.     GCPtr    pGC;
  112.     int        mode;        /* Origin or Previous */
  113.     int        npt;        /* number of points */
  114.     DDXPointPtr pptInit;
  115. #endif
  116. {
  117.     int nboxInit;
  118.     register int nbox;
  119.     BoxPtr pboxInit;
  120.     register BoxPtr pbox;
  121. #ifndef POLYSEGMENT
  122.     register DDXPointPtr ppt;    /* pointer to list of translated points */
  123. #endif
  124.  
  125.     unsigned int oc1;        /* outcode of point 1 */
  126.     unsigned int oc2;        /* outcode of point 2 */
  127.  
  128.     int *addrl;        /* address of destination pixmap */
  129.     int nlwidth;        /* width in longwords of destination pixmap */
  130.     int xorg, yorg;        /* origin of window */
  131.  
  132.     int adx;        /* abs values of dx and dy */
  133.     int ady;
  134.     int signdx;        /* sign of dx and dy */
  135.     int signdy;
  136.     int e, e1, e2;        /* bresenham error and increments */
  137.     int len;            /* length of segment */
  138.     int axis;            /* major axis */
  139.  
  140.                 /* a bunch of temporaries */
  141.     register int y1, y2;
  142.     register int x1, x2;
  143.     RegionPtr cclip;
  144.     int            alu;
  145.  
  146.     if (!(pGC->planemask & 1))
  147.     return;
  148.  
  149.     cclip = ((mfbPrivGC *)(pGC->devPrivates[mfbGCPrivateIndex].ptr))->pCompositeClip;
  150.     alu = ((mfbPrivGC *)(pGC->devPrivates[mfbGCPrivateIndex].ptr))->rop;
  151.     pboxInit = REGION_RECTS(cclip);
  152.     nboxInit = REGION_NUM_RECTS(cclip);
  153.  
  154.     if (pDrawable->type == DRAWABLE_WINDOW)
  155.     {
  156.     addrl = (int *)
  157.         (((PixmapPtr)(pDrawable->pScreen->devPrivate))->devPrivate.ptr);
  158.     nlwidth = (int)
  159.         (((PixmapPtr)(pDrawable->pScreen->devPrivate))->devKind) >> 2;
  160.     }
  161.     else
  162.     {
  163.     addrl = (int *)(((PixmapPtr)pDrawable)->devPrivate.ptr);
  164.     nlwidth = (int)(((PixmapPtr)pDrawable)->devKind) >> 2;
  165.     }
  166.  
  167.     xorg = pDrawable->x;
  168.     yorg = pDrawable->y;
  169. #ifdef POLYSEGMENT
  170.     while (nseg--)
  171. #else
  172.     ppt = pptInit;
  173.     x2 = ppt->x + xorg;
  174.     y2 = ppt->y + yorg;
  175.     while(--npt)
  176. #endif
  177.     {
  178.     nbox = nboxInit;
  179.     pbox = pboxInit;
  180.  
  181. #ifdef POLYSEGMENT
  182.     x1 = pSeg->x1 + xorg;
  183.     y1 = pSeg->y1 + yorg;
  184.     x2 = pSeg->x2 + xorg;
  185.     y2 = pSeg->y2 + yorg;
  186.     pSeg++;
  187. #else
  188.     x1 = x2;
  189.     y1 = y2;
  190.     ++ppt;
  191.     if (mode == CoordModePrevious)
  192.     {
  193.         xorg = x1;
  194.         yorg = y1;
  195.     }
  196.     x2 = ppt->x + xorg;
  197.     y2 = ppt->y + yorg;
  198. #endif
  199.  
  200.     if (x1 == x2)
  201.     {
  202.         /* make the line go top to bottom of screen, keeping
  203.            endpoint semantics
  204.         */
  205.         if (y1 > y2)
  206.         {
  207.         register int tmp;
  208.  
  209.         tmp = y2;
  210.         y2 = y1 + 1;
  211.         y1 = tmp + 1;
  212. #ifdef POLYSEGMENT
  213.         if (pGC->capStyle != CapNotLast)
  214.             y1--;
  215. #endif
  216.         }
  217. #ifdef POLYSEGMENT
  218.         else if (pGC->capStyle != CapNotLast)
  219.         y2++;
  220. #endif
  221.         /* get to first band that might contain part of line */
  222.         while ((nbox) && (pbox->y2 <= y1))
  223.         {
  224.         pbox++;
  225.         nbox--;
  226.         }
  227.  
  228.         if (nbox)
  229.         {
  230.         /* stop when lower edge of box is beyond end of line */
  231.         while((nbox) && (y2 >= pbox->y1))
  232.         {
  233.             if ((x1 >= pbox->x1) && (x1 < pbox->x2))
  234.             {
  235.             int y1t, y2t;
  236.             /* this box has part of the line in it */
  237.             y1t = max(y1, pbox->y1);
  238.             y2t = min(y2, pbox->y2);
  239.             if (y1t != y2t)
  240.             {
  241.                 mfbVertS (alu,
  242.                       addrl, nlwidth, 
  243.                       x1, y1t, y2t-y1t);
  244.             }
  245.             }
  246.             nbox--;
  247.             pbox++;
  248.         }
  249.         }
  250. #ifndef POLYSEGMENT
  251.         y2 = ppt->y + yorg;
  252. #endif
  253.     }
  254.     else if (y1 == y2)
  255.     {
  256.         /* force line from left to right, keeping
  257.            endpoint semantics
  258.         */
  259.         if (x1 > x2)
  260.         {
  261.         register int tmp;
  262.  
  263.         tmp = x2;
  264.         x2 = x1 + 1;
  265.         x1 = tmp + 1;
  266. #ifdef POLYSEGMENT
  267.         if (pGC->capStyle != CapNotLast)
  268.             x1--;
  269. #endif
  270.         }
  271. #ifdef POLYSEGMENT
  272.         else if (pGC->capStyle != CapNotLast)
  273.         x2++;
  274. #endif
  275.  
  276.         /* find the correct band */
  277.         while( (nbox) && (pbox->y2 <= y1))
  278.         {
  279.         pbox++;
  280.         nbox--;
  281.         }
  282.  
  283.         /* try to draw the line, if we haven't gone beyond it */
  284.         if ((nbox) && (pbox->y1 <= y1))
  285.         {
  286.         int tmp;
  287.  
  288.         /* when we leave this band, we're done */
  289.         tmp = pbox->y1;
  290.         while((nbox) && (pbox->y1 == tmp))
  291.         {
  292.             int    x1t, x2t;
  293.  
  294.             if (pbox->x2 <= x1)
  295.             {
  296.             /* skip boxes until one might contain start point */
  297.             nbox--;
  298.             pbox++;
  299.             continue;
  300.             }
  301.  
  302.             /* stop if left of box is beyond right of line */
  303.             if (pbox->x1 >= x2)
  304.             {
  305.             nbox = 0;
  306.             break;
  307.             }
  308.  
  309.             x1t = max(x1, pbox->x1);
  310.             x2t = min(x2, pbox->x2);
  311.             if (x1t != x2t)
  312.             {
  313.             mfbHorzS (alu,
  314.                   addrl, nlwidth, 
  315.                   x1t, y1, x2t-x1t);
  316.             }
  317.             nbox--;
  318.             pbox++;
  319.         }
  320.         }
  321. #ifndef POLYSEGMENT
  322.         x2 = ppt->x + xorg;
  323. #endif
  324.     }
  325.     else    /* sloped line */
  326.     {
  327.         adx = x2 - x1;
  328.         ady = y2 - y1;
  329.         signdx = sign(adx);
  330.         signdy = sign(ady);
  331.         adx = abs(adx);
  332.         ady = abs(ady);
  333.  
  334.         if (adx > ady)
  335.         {
  336.         axis = X_AXIS;
  337.         e1 = ady << 1;
  338.         e2 = e1 - (adx << 1);
  339.         e = e1 - adx;
  340.  
  341.         }
  342.         else
  343.         {
  344.         axis = Y_AXIS;
  345.         e1 = adx << 1;
  346.         e2 = e1 - (ady << 1);
  347.         e = e1 - ady;
  348.         }
  349.  
  350.         /* we have bresenham parameters and two points.
  351.            all we have to do now is clip and draw.
  352.         */
  353.  
  354.         while(nbox--)
  355.         {
  356.         oc1 = 0;
  357.         oc2 = 0;
  358.         OUTCODES(oc1, x1, y1, pbox);
  359.         OUTCODES(oc2, x2, y2, pbox);
  360.         if ((oc1 | oc2) == 0)
  361.         {
  362.             if (axis == X_AXIS)
  363.             len = adx;
  364.             else
  365.             len = ady;
  366. #ifdef POLYSEGMENT
  367.             if (pGC->capStyle != CapNotLast)
  368.             len++;
  369. #endif
  370.             mfbBresS (alu,
  371.               addrl, nlwidth,
  372.               signdx, signdy, axis, x1, y1,
  373.               e, e1, e2, len);
  374.             break;
  375.         }
  376.         else if (oc1 & oc2)
  377.         {
  378.             pbox++;
  379.         }
  380.         else
  381.         {
  382.                 /*
  383.                   * let the mfb helper routine do our work;
  384.                   * better than duplicating code...
  385.                   */
  386.                 BoxRec box;
  387.                     DDXPointRec pt1Copy;    /* clipped start point */
  388.                     DDXPointRec pt2Copy;    /* clipped end point */
  389.                     int err;            /* modified bresenham error term */
  390.                     int clip1, clip2;        /* clippedness of the endpoints */
  391.                 
  392.                     int clipdx, clipdy;        /* difference between clipped and
  393.                                          unclipped start point */
  394.             DDXPointRec    pt1;
  395.                 
  396.         
  397.                 pt1.x = pt1Copy.x = x1;
  398.             pt1.y = pt1Copy.y = y1;
  399.                 pt2Copy.x = x2;
  400.             pt2Copy.y = y2;
  401.                 box.x1 = pbox->x1;
  402.                 box.y1 = pbox->y1;
  403.                 box.x2 = pbox->x2-1;
  404.                 box.y2 = pbox->y2-1;
  405.                 clip1 = 0;
  406.                 clip2 = 0;
  407.         
  408.             if (mfbClipLine (pbox, box,
  409.                      &pt1, &pt1Copy, &pt2Copy, 
  410.                      adx, ady, signdx, signdy, axis,
  411.                      &clip1, &clip2) == 1)
  412.             {
  413.                 if (axis == X_AXIS)
  414.                 len = abs(pt2Copy.x - pt1Copy.x);
  415.                 else
  416.                 len = abs(pt2Copy.y - pt1Copy.y);
  417.     
  418. #ifdef POLYSEGMENT
  419.                 if (clip2 != 0 || pGC->capStyle != CapNotLast)
  420.                 len++;
  421. #else
  422.                 len += (clip2 != 0);
  423. #endif
  424.                 if (len)
  425.                 {
  426.                 /* unwind bresenham error term to first point */
  427.                 if (clip1)
  428.                 {
  429.                     clipdx = abs(pt1Copy.x - x1);
  430.                     clipdy = abs(pt1Copy.y - y1);
  431.                     if (axis == X_AXIS)
  432.                     err = e+((clipdy*e2) + ((clipdx-clipdy)*e1));
  433.                     else
  434.                     err = e+((clipdx*e2) + ((clipdy-clipdx)*e1));
  435.                 }
  436.                 else
  437.                     err = e;
  438.                 mfbBresS   
  439.                      (alu,
  440.                       addrl, nlwidth,
  441.                       signdx, signdy, axis, pt1Copy.x, pt1Copy.y,
  442.                       err, e1, e2, len);
  443.                 }
  444.             }
  445.             pbox++;
  446.         }
  447.         } /* while (nbox--) */
  448.     } /* sloped line */
  449.     } /* while (nline--) */
  450.  
  451. #ifndef POLYSEGMENT
  452.  
  453.     /* paint the last point if the end style isn't CapNotLast.
  454.        (Assume that a projecting, butt, or round cap that is one
  455.         pixel wide is the same as the single pixel of the endpoint.)
  456.     */
  457.  
  458.     if ((pGC->capStyle != CapNotLast) &&
  459.     ((ppt->x + xorg != pptInit->x + pDrawable->x) ||
  460.      (ppt->y + yorg != pptInit->y + pDrawable->y) ||
  461.      (ppt == pptInit + 1)))
  462.     {
  463.     unsigned int _mask;
  464.     int _incr;
  465.  
  466.     if (alu == RROP_BLACK)
  467.         _mask = rmask[x2 & 0x1f];
  468.     else
  469.         _mask = mask[x2 & 0x1f];
  470.     _incr = (y2 * nlwidth) + (x2 >> 5);
  471.  
  472.     nbox = nboxInit;
  473.     pbox = pboxInit;
  474.     while (nbox--)
  475.     {
  476.         if ((x2 >= pbox->x1) &&
  477.         (y2 >= pbox->y1) &&
  478.         (x2 <  pbox->x2) &&
  479.         (y2 <  pbox->y2))
  480.         {
  481.         addrl += _incr;
  482.         switch(alu)
  483.         {
  484.             case RROP_BLACK:
  485.                 *addrl &= _mask;
  486.             break;
  487.             case RROP_WHITE:
  488.                 *addrl |= _mask;
  489.             break;
  490.             case RROP_INVERT:
  491.                 *addrl ^= _mask;
  492.             break;
  493.         }
  494.         break;
  495.         }
  496.         else
  497.         pbox++;
  498.     }
  499.     }
  500. #endif
  501. }
  502.  
  503. /*
  504.  * Draw dashed 1-pixel lines.
  505.  */
  506.  
  507. void
  508. #ifdef POLYSEGMENT
  509. mfbSegmentSD (pDrawable, pGC, nseg, pSeg)
  510.     DrawablePtr    pDrawable;
  511.     register GCPtr    pGC;
  512.     int        nseg;
  513.     register xSegment    *pSeg;
  514. #else
  515. mfbLineSD( pDrawable, pGC, mode, npt, pptInit)
  516.     DrawablePtr pDrawable;
  517.     register GCPtr pGC;
  518.     int mode;        /* Origin or Previous */
  519.     int npt;        /* number of points */
  520.     DDXPointPtr pptInit;
  521. #endif
  522. {
  523.     int nboxInit;
  524.     register int nbox;
  525.     BoxPtr pboxInit;
  526.     register BoxPtr pbox;
  527. #ifndef POLYSEGMENT
  528.     register DDXPointPtr ppt;    /* pointer to list of translated points */
  529. #endif
  530.  
  531.     register unsigned int oc1;    /* outcode of point 1 */
  532.     register unsigned int oc2;    /* outcode of point 2 */
  533.  
  534.     int *addrl;        /* address of destination pixmap */
  535.     int nlwidth;        /* width in longwords of destination pixmap */
  536.     int xorg, yorg;        /* origin of window */
  537.  
  538.     int adx;        /* abs values of dx and dy */
  539.     int ady;
  540.     int signdx;        /* sign of dx and dy */
  541.     int signdy;
  542.     int e, e1, e2;        /* bresenham error and increments */
  543.     int len;            /* length of segment */
  544.     int axis;            /* major axis */
  545.     int x1, x2, y1, y2;
  546.     RegionPtr cclip;
  547.     int            fgrop, bgrop;
  548.     unsigned char   *pDash;
  549.     int            dashOffset;
  550.     int            numInDashList;
  551.     int            dashIndex;
  552.     int            isDoubleDash;
  553.     int            dashIndexTmp, dashOffsetTmp;
  554.     int            unclippedlen;
  555.  
  556.     if (!(pGC->planemask & 1))
  557.     return;
  558.  
  559.     cclip = ((mfbPrivGC *)(pGC->devPrivates[mfbGCPrivateIndex].ptr))->pCompositeClip;
  560.     fgrop = ((mfbPrivGC *)(pGC->devPrivates[mfbGCPrivateIndex].ptr))->rop;
  561.     pboxInit = REGION_RECTS(cclip);
  562.     nboxInit = REGION_NUM_RECTS(cclip);
  563.  
  564.     if (pDrawable->type == DRAWABLE_WINDOW)
  565.     {
  566.     addrl = (int *)
  567.         (((PixmapPtr)(pDrawable->pScreen->devPrivate))->devPrivate.ptr);
  568.     nlwidth = (int)
  569.         (((PixmapPtr)(pDrawable->pScreen->devPrivate))->devKind) >> 2;
  570.     }
  571.     else
  572.     {
  573.     addrl = (int *)(((PixmapPtr)pDrawable)->devPrivate.ptr);
  574.     nlwidth = (int)(((PixmapPtr)pDrawable)->devKind) >> 2;
  575.     }
  576.  
  577.     /* compute initial dash values */
  578.      
  579.     pDash = (unsigned char *) pGC->dash;
  580.     numInDashList = pGC->numInDashList;
  581.     isDoubleDash = (pGC->lineStyle == LineDoubleDash);
  582.     dashIndex = 0;
  583.     dashOffset = 0;
  584.     miStepDash ((int)pGC->dashOffset, &dashIndex, pDash,
  585.         numInDashList, &dashOffset);
  586.  
  587.     if (isDoubleDash)
  588.     bgrop = mfbReduceRop(pGC->alu, pGC->bgPixel);
  589.  
  590.     xorg = pDrawable->x;
  591.     yorg = pDrawable->y;
  592. #ifdef POLYSEGMENT
  593.     while (nseg--)
  594. #else
  595.     ppt = pptInit;
  596.     x2 = ppt->x + xorg;
  597.     y2 = ppt->y + yorg;
  598.     while(--npt)
  599. #endif
  600.     {
  601.     nbox = nboxInit;
  602.     pbox = pboxInit;
  603.  
  604. #ifdef POLYSEGMENT
  605.     x1 = pSeg->x1 + xorg;
  606.     y1 = pSeg->y1 + yorg;
  607.     x2 = pSeg->x2 + xorg;
  608.     y2 = pSeg->y2 + yorg;
  609.     pSeg++;
  610. #else
  611.     x1 = x2;
  612.     y1 = y2;
  613.     ++ppt;
  614.     if (mode == CoordModePrevious)
  615.     {
  616.         xorg = x1;
  617.         yorg = y1;
  618.     }
  619.     x2 = ppt->x + xorg;
  620.     y2 = ppt->y + yorg;
  621. #endif
  622.  
  623.     adx = x2 - x1;
  624.     ady = y2 - y1;
  625.     signdx = sign(adx);
  626.     signdy = sign(ady);
  627.     adx = abs(adx);
  628.     ady = abs(ady);
  629.  
  630.     if (adx > ady)
  631.     {
  632.         axis = X_AXIS;
  633.         e1 = ady << 1;
  634.         e2 = e1 - (adx << 1);
  635.         e = e1 - adx;
  636.         unclippedlen = adx;
  637.     }
  638.     else
  639.     {
  640.         axis = Y_AXIS;
  641.         e1 = adx << 1;
  642.         e2 = e1 - (ady << 1);
  643.         e = e1 - ady;
  644.         unclippedlen = ady;
  645.     }
  646.  
  647.     /* we have bresenham parameters and two points.
  648.        all we have to do now is clip and draw.
  649.     */
  650.  
  651.     while(nbox--)
  652.     {
  653.         oc1 = 0;
  654.         oc2 = 0;
  655.         OUTCODES(oc1, x1, y1, pbox);
  656.         OUTCODES(oc2, x2, y2, pbox);
  657.         if ((oc1 | oc2) == 0)
  658.         {
  659. #ifdef POLYSEGMENT
  660.         if (pGC->capStyle != CapNotLast)
  661.             unclippedlen++;
  662.         dashIndexTmp = dashIndex;
  663.         dashOffsetTmp = dashOffset;
  664.         mfbBresD (fgrop, bgrop,
  665.               &dashIndexTmp, pDash, numInDashList,
  666.               &dashOffsetTmp, isDoubleDash,
  667.               addrl, nlwidth,
  668.               signdx, signdy, axis, x1, y1,
  669.               e, e1, e2, unclippedlen);
  670.         break;
  671. #else
  672.         mfbBresD (fgrop, bgrop,
  673.               &dashIndex, pDash, numInDashList,
  674.               &dashOffset, isDoubleDash,
  675.               addrl, nlwidth,
  676.               signdx, signdy, axis, x1, y1,
  677.               e, e1, e2, unclippedlen);
  678.         goto dontStep;
  679. #endif
  680.         }
  681.         else if (oc1 & oc2)
  682.         {
  683.         pbox++;
  684.         }
  685.         else /* have to clip */
  686.         {
  687.         /*
  688.          * let the mfb helper routine do our work;
  689.          * better than duplicating code...
  690.          */
  691.         BoxRec box;
  692.         DDXPointRec pt1Copy;    /* clipped start point */
  693.         DDXPointRec pt2Copy;    /* clipped end point */
  694.         int err;            /* modified bresenham error term */
  695.         int clip1, clip2;        /* clippedness of the endpoints */
  696.         
  697.         int clipdx, clipdy;        /* difference between clipped and
  698.                            unclipped start point */
  699.         DDXPointRec    pt1;
  700.     
  701.         pt1.x = pt1Copy.x = x1;
  702.         pt1.y = pt1Copy.y = y1;
  703.         pt2Copy.x = x2;
  704.         pt2Copy.y = y2;
  705.         box.x1 = pbox->x1;
  706.         box.y1 = pbox->y1;
  707.         box.x2 = pbox->x2-1;
  708.         box.y2 = pbox->y2-1;
  709.         clip1 = 0;
  710.         clip2 = 0;
  711.     
  712.         if (mfbClipLine (pbox, box,
  713.                        &pt1, &pt1Copy, &pt2Copy, 
  714.                        adx, ady, signdx, signdy, axis,
  715.                        &clip1, &clip2) == 1)
  716.         {
  717.     
  718.             dashIndexTmp = dashIndex;
  719.             dashOffsetTmp = dashOffset;
  720.             if (clip1)
  721.             {
  722.                 int dlen;
  723.     
  724.                 if (axis == X_AXIS)
  725.                 dlen = abs(pt1Copy.x - x1);
  726.                 else
  727.                 dlen = abs(pt1Copy.y - y1);
  728.                 miStepDash (dlen, &dashIndexTmp, pDash,
  729.                     numInDashList, &dashOffsetTmp);
  730.             }
  731.             if (axis == X_AXIS)
  732.                 len = abs(pt2Copy.x - pt1Copy.x);
  733.             else
  734.                 len = abs(pt2Copy.y - pt1Copy.y);
  735.     
  736. #ifdef POLYSEGMENT
  737.             if (clip2 != 0 || pGC->capStyle != CapNotLast)
  738.                 len++;
  739. #else
  740.             len += (clip2 != 0);
  741. #endif
  742.             if (len)
  743.             {
  744.                 /* unwind bresenham error term to first point */
  745.                 if (clip1)
  746.                 {
  747.                 clipdx = abs(pt1Copy.x - x1);
  748.                 clipdy = abs(pt1Copy.y - y1);
  749.                 if (axis == X_AXIS)
  750.                     err = e+((clipdy*e2) + ((clipdx-clipdy)*e1));
  751.                 else
  752.                     err = e+((clipdx*e2) + ((clipdy-clipdx)*e1));
  753.                 }
  754.                 else
  755.                 err = e;
  756.                 mfbBresD (fgrop, bgrop,
  757.                         &dashIndexTmp, pDash, numInDashList,
  758.                         &dashOffsetTmp, isDoubleDash,
  759.                         addrl, nlwidth,
  760.                         signdx, signdy, axis, pt1Copy.x, pt1Copy.y,
  761.                         err, e1, e2, len);
  762.             }
  763.         }
  764.         pbox++;
  765.         }
  766.     } /* while (nbox--) */
  767. #ifndef POLYSEGMENT
  768.     /*
  769.      * walk the dash list around to the next line
  770.      */
  771.     miStepDash (unclippedlen, &dashIndex, pDash,
  772.             numInDashList, &dashOffset);
  773. dontStep:    ;
  774. #endif
  775.     } /* while (nline--) */
  776.  
  777. #ifndef POLYSEGMENT
  778.     /* paint the last point if the end style isn't CapNotLast.
  779.        (Assume that a projecting, butt, or round cap that is one
  780.         pixel wide is the same as the single pixel of the endpoint.)
  781.     */
  782.  
  783.     if ((pGC->capStyle != CapNotLast) &&
  784.         ((dashIndex & 1) == 0 || isDoubleDash) &&
  785.     ((ppt->x + xorg != pptInit->x + pDrawable->x) ||
  786.      (ppt->y + yorg != pptInit->y + pDrawable->y) ||
  787.      (ppt == pptInit + 1)))
  788.     {
  789.     nbox = nboxInit;
  790.     pbox = pboxInit;
  791.     while (nbox--)
  792.     {
  793.         if ((x2 >= pbox->x1) &&
  794.         (y2 >= pbox->y1) &&
  795.         (x2 <  pbox->x2) &&
  796.         (y2 <  pbox->y2))
  797.         {
  798.         unsigned long _mask;
  799.         int rop;
  800.  
  801.         rop = fgrop;
  802.         if (dashIndex & 1)
  803.             rop = bgrop;
  804.         if (rop == RROP_BLACK)
  805.             _mask = rmask[x2 & 0x1f];
  806.         else
  807.             _mask = mask[x2 & 0x1f];
  808.         addrl += (y2 * nlwidth) + (x2 >> 5);
  809.         if (rop == RROP_BLACK)
  810.             *addrl &= _mask;
  811.         else if (rop == RROP_WHITE)
  812.             *addrl |= _mask;
  813.         else
  814.             *addrl ^= _mask;
  815.         break;
  816.         }
  817.         else
  818.         pbox++;
  819.     }
  820.     }
  821. #endif
  822. }
  823.  
  824. #ifndef POLYSEGMENT
  825. /*
  826.     the clipping code could be cleaned up some; most of its
  827. mess derives from originally being inline in the line code,
  828. then pulled out to make clipping dashes easier.
  829. */
  830.  
  831. int
  832. mfbClipLine(pbox, box,
  833.         ppt1Orig, ppt1, ppt2, 
  834.         adx, ady, signdx, signdy, axis,
  835.         pclip1, pclip2)
  836. BoxPtr pbox;            /* box to clip to */
  837. BoxRec box;            /* box to do calculations with */
  838. DDXPointPtr ppt1Orig, ppt1, ppt2;
  839. int adx, ady;
  840. int signdx, signdy;
  841. register int axis;
  842. int *pclip1, *pclip2;
  843. {
  844.     DDXPointRec pt1Orig, pt1, pt2;
  845.     register int swapped = 0;
  846.     int clipDone = 0;
  847.     register unsigned long utmp;
  848.     register int oc1, oc2;
  849.     int clip1, clip2;
  850.  
  851.     pt1Orig = *ppt1Orig;
  852.     pt1 = *ppt1;
  853.     pt2 = *ppt2;
  854.     clip1 = 0;
  855.     clip2 = 0;
  856.  
  857.     do
  858.     {
  859.         oc1 = 0;
  860.         oc2 = 0;
  861.         OUTCODES(oc1, pt1.x, pt1.y, pbox);
  862.         OUTCODES(oc2, pt2.x, pt2.y, pbox);
  863.  
  864.         if (oc1 & oc2)
  865.         clipDone = -1;
  866.         else if ((oc1 | oc2) == 0)
  867.         {
  868.         clipDone = 1;
  869.         if (swapped)
  870.         {
  871.             SWAPPT(pt1, pt2);
  872.             SWAPINT(oc1, oc2);
  873.             SWAPINT(clip1, clip2);
  874.         }
  875.         }
  876.         else /* have to clip */
  877.         {
  878.         /* only clip one point at a time */
  879.         if (!oc1)
  880.         {
  881.             SWAPPT(pt1, pt2);
  882.             SWAPINT(oc1, oc2);
  883.             SWAPINT(clip1, clip2);
  884.             swapped = !swapped;
  885.         }
  886.     
  887.         clip1 |= oc1;
  888.         if (oc1 & OUT_LEFT)
  889.         {
  890.           pt1.x = box.x1;
  891.           utmp = abs(box.x1 - pt1Orig.x);
  892.           utmp *= ady;
  893.           if(axis==X_AXIS)
  894.           {
  895.             pt1.y = pt1Orig.y + SignTimes(signdy, round(utmp, adx));
  896.           }
  897.           else
  898.           {
  899.         utmp <<= 1;
  900.         if (swapped)
  901.             utmp += ady;
  902.         else
  903.             utmp -= ady;
  904.         pt1.y = pt1Orig.y + SignTimes(signdy, ceiling(utmp, 2*adx));
  905.         if (swapped)
  906.             pt1.y -= signdy;
  907.           }
  908.         }
  909.         else if (oc1 & OUT_ABOVE)
  910.         {
  911.           pt1.y = box.y1;
  912.           utmp = abs(box.y1 - pt1Orig.y);
  913.           utmp *= adx;
  914.           if (axis == Y_AXIS)
  915.           {
  916.             pt1.x = pt1Orig.x + SignTimes(signdx, round(utmp, ady));
  917.           }
  918.           else
  919.           {
  920.         utmp <<= 1;
  921.         if (swapped)
  922.             utmp += adx;
  923.         else
  924.             utmp -= adx;
  925.         pt1.x = pt1Orig.x + SignTimes(signdx, ceiling(utmp, 2*ady));
  926.         if (swapped)
  927.             pt1.x -= signdx;
  928.           }
  929.         }
  930.         else if (oc1 & OUT_RIGHT)
  931.         {
  932.           pt1.x = box.x2;
  933.           utmp = abs(pt1Orig.x - box.x2);
  934.           utmp *= ady;
  935.           if (axis == X_AXIS)
  936.           {
  937.             pt1.y = pt1Orig.y + SignTimes(signdy, round(utmp, adx));
  938.           }
  939.           else
  940.           {
  941.         utmp <<= 1;
  942.         if (swapped)
  943.             utmp += ady;
  944.         else
  945.             utmp -= ady;
  946.         pt1.y = pt1Orig.y + SignTimes(signdy, ceiling(utmp, 2*adx));
  947.         if (swapped)
  948.             pt1.y -= signdy;
  949.           }
  950.         }
  951.         else if (oc1 & OUT_BELOW)
  952.         {
  953.           pt1.y = box.y2;
  954.           utmp = abs(pt1Orig.y - box.y2);
  955.           utmp *= adx;
  956.           if (axis == Y_AXIS)
  957.           {
  958.             pt1.x = pt1Orig.x + SignTimes(signdx, round(utmp, ady));
  959.           }
  960.           else
  961.           {
  962.         utmp <<= 1;
  963.         if (swapped)
  964.             utmp += adx;
  965.         else
  966.             utmp -= adx;
  967.         pt1.x = pt1Orig.x + SignTimes(signdx, ceiling(utmp, 2*ady));
  968.         if (swapped)
  969.             pt1.x -= signdx;
  970.           }
  971.         }
  972.         } /* else have to clip */
  973.     } while(!clipDone);
  974.     *ppt1 = pt1;
  975.     *ppt2 = pt2;
  976.     *pclip1 = clip1;
  977.     *pclip2 = clip2;
  978.  
  979.     return clipDone;
  980. }
  981. #endif
  982.