home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / ddx / mi / midash.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-14  |  6.4 KB  |  287 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: midash.c,v 5.3 89/09/14 19:14:48 rws Exp $ */
  25. #include "miscstruct.h"
  26. #include "mistruct.h"
  27. #include "mifpoly.h"
  28.  
  29. static miDashPtr CheckDashStorage();
  30.  
  31. /* return a list of DashRec.  there will be an extra
  32. entry at the end holding the last point of the polyline.
  33.    this means that the code that actually draws dashes can
  34. get a pair of points for every dash.  only the point in the last
  35. dash record is useful; the other fields are not used.
  36.    nseg is the number of segments, not the number of points.
  37.  
  38. example:
  39.  
  40.    dash1.start
  41.    dash2.start
  42.    dash3.start
  43.    last-point
  44.  
  45. defines a list of segments
  46.    (dash1.pt, dash2.pt)
  47.    (dash2.pt, dash3.pt)
  48.    (dash3.pt, last-point)
  49. and nseg == 3.
  50.  
  51. NOTE:
  52.     EVEN_DASH == ~ODD_DASH
  53.  
  54. NOTE ALSO:
  55.     miDashLines may return 0 segments, going from pt[0] to pt[0] with one dash.
  56. */
  57.  
  58. miDashPtr
  59. miDashLine(npt, ppt, nDash, pDash, offset, pnseg)
  60. int npt;
  61. DDXPointPtr ppt;
  62. unsigned int nDash;
  63. unsigned char *pDash;
  64. unsigned int offset;
  65. int *pnseg;
  66. {
  67.     DDXPointRec pt1, pt2;
  68.     int lenCur;        /* npt used from this dash */
  69.     int lenMax;        /* npt in this dash */
  70.     int iDash = 0;    /* index of current dash */
  71.     int which;        /* EVEN_DASH or ODD_DASH */
  72.     miDashPtr pseg;    /* list of dash segments */
  73.     miDashPtr psegBase;    /* start of list */
  74.     int nseg = 0;    /* number of dashes so far */
  75.     int nsegMax = 0;    /* num segs we can fit in this list */
  76.  
  77.     int x, y, len;
  78.     int adx, ady, signdx, signdy;
  79.     int du, dv, e1, e2, e, base_e = 0;
  80.  
  81.     lenCur = offset;
  82.     which = EVEN_DASH;
  83.     while(lenCur >= pDash[iDash])
  84.     {
  85.     lenCur -= pDash[iDash];
  86.     iDash++;
  87.     if (iDash >= nDash)
  88.         iDash = 0;
  89.     which = ~which;
  90.     }
  91.     lenMax = pDash[iDash];
  92.  
  93.     psegBase = (miDashPtr)NULL;
  94.     pt2 = ppt[0];        /* just in case there is only one point */
  95.  
  96.     while(--npt)
  97.     {
  98.     if (PtEqual(ppt[0], ppt[1]))
  99.     {
  100.         ppt++;
  101.         continue;        /* no duplicated points in polyline */
  102.     }
  103.     pt1 = *ppt++;
  104.     pt2 = *ppt;
  105.  
  106.     adx = pt2.x - pt1.x;
  107.     ady = pt2.y - pt1.y;
  108.     signdx = sign(adx);
  109.     signdy = sign(ady);
  110.     adx = abs(adx);
  111.     ady = abs(ady);
  112.  
  113.     if (adx > ady)
  114.     {
  115.         du = adx;
  116.         dv = ady;
  117.         len = adx;
  118.     }
  119.     else
  120.     {
  121.         du = ady;
  122.         dv = adx;
  123.         len = ady;
  124.     }
  125.  
  126.     e1 = dv * 2;
  127.     e2 = e1 - 2*du;
  128.     e = e1 - du;
  129.     x = pt1.x;
  130.     y = pt1.y;
  131.  
  132.     nseg++;
  133.     pseg = CheckDashStorage(&psegBase, nseg, &nsegMax);
  134.     if (!pseg)
  135.         return (miDashPtr)NULL;
  136.     pseg->pt = pt1;
  137.     pseg->e1 = e1;
  138.     pseg->e2 = e2;
  139.     base_e = pseg->e = e;
  140.     pseg->which = which;
  141.     pseg->newLine = 1;
  142.  
  143.     while (len--)
  144.     {
  145.         if (adx > ady)
  146.         {
  147.         /* X_AXIS */
  148.         if (((signdx > 0) && (e < 0)) ||
  149.             ((signdx <=0) && (e <=0))
  150.            )
  151.         {
  152.             e += e1;
  153.         }
  154.         else
  155.         {
  156.             y += signdy;
  157.             e += e2;
  158.         }
  159.         x += signdx;
  160.         }
  161.         else
  162.         {
  163.         /* Y_AXIS */
  164.         if (((signdx > 0) && (e < 0)) ||
  165.             ((signdx <=0) && (e <=0))
  166.            )
  167.         {
  168.             e +=e1;
  169.         }
  170.         else
  171.         {
  172.             x += signdx;
  173.             e += e2;
  174.         }
  175.         y += signdy;
  176.         }
  177.  
  178.         lenCur++;
  179.         if (lenCur >= lenMax && (len || npt <= 1))
  180.         {
  181.         nseg++;
  182.         pseg = CheckDashStorage(&psegBase, nseg, &nsegMax);
  183.         if (!pseg)
  184.             return (miDashPtr)NULL;
  185.         pseg->pt.x = x;
  186.         pseg->pt.y = y;
  187.         pseg->e1 = e1;
  188.         pseg->e2 = e2;
  189.         pseg->e = e;
  190.         which = ~which;
  191.         pseg->which = which;
  192.         pseg->newLine = 0;
  193.  
  194.         /* move on to next dash */
  195.         iDash++;
  196.         if (iDash >= nDash)
  197.             iDash = 0;
  198.         lenMax = pDash[iDash];
  199.         lenCur = 0;
  200.         }
  201.     } /* while len-- */
  202.     } /* while --npt */
  203.  
  204.     if (lenCur == 0 && nseg != 0)
  205.     {
  206.     nseg--;
  207.     which = ~which;
  208.     }
  209.     *pnseg = nseg;
  210.     pseg = CheckDashStorage(&psegBase, nseg+1, &nsegMax);
  211.     if (!pseg)
  212.     return (miDashPtr)NULL;
  213.     pseg->pt = pt2;
  214.     pseg->e = base_e;
  215.     pseg->which = which;
  216.     pseg->newLine = 0;
  217.     return psegBase;
  218.  
  219.  
  220. #define NSEGDELTA 16
  221.  
  222. /* returns a pointer to the pseg[nseg-1], growing the storage as
  223. necessary.  this interface seems unnecessarily cumbersome.
  224.  
  225. */
  226.  
  227. static
  228. miDashPtr
  229. CheckDashStorage(ppseg, nseg, pnsegMax)
  230. miDashPtr *ppseg;        /* base pointer */
  231. int nseg;            /* number of segment we want to write to */
  232. int *pnsegMax;            /* size (in segments) of list so far */
  233. {
  234.     if (nseg > *pnsegMax)
  235.     {
  236.     miDashPtr newppseg;
  237.  
  238.     *pnsegMax += NSEGDELTA;
  239.     newppseg = (miDashPtr)xrealloc(*ppseg,
  240.                        (*pnsegMax)*sizeof(miDashRec));
  241.     if (!newppseg)
  242.     {
  243.         xfree(*ppseg);
  244.         return (miDashPtr)NULL;
  245.     }
  246.     *ppseg = newppseg;
  247.     }
  248.     return(*ppseg+(nseg-1));
  249. }
  250.  
  251. miStepDash (dist, pDashIndex, pDash, numInDashList, pDashOffset)
  252.     int dist;            /* distance to step */
  253.     int *pDashIndex;        /* current dash */
  254.     unsigned char *pDash;    /* dash list */
  255.     int numInDashList;        /* total length of dash list */
  256.     int *pDashOffset;        /* offset into current dash */
  257. {
  258.     int    dashIndex, dashOffset;
  259.     int totallen;
  260.     int    i;
  261.     
  262.     dashIndex = *pDashIndex;
  263.     dashOffset = *pDashOffset;
  264.     if (dist < pDash[dashIndex] - dashOffset)
  265.     {
  266.     *pDashOffset = dashOffset + dist;
  267.     return;
  268.     }
  269.     dist -= pDash[dashIndex] - dashOffset;
  270.     if (++dashIndex == numInDashList)
  271.     dashIndex = 0;
  272.     totallen = 0;
  273.     for (i = 0; i < numInDashList; i++)
  274.     totallen += pDash[i];
  275.     if (totallen <= dist)
  276.     dist = dist % totallen;
  277.     while (dist >= pDash[dashIndex])
  278.     {
  279.     dist -= pDash[dashIndex];
  280.     if (++dashIndex == numInDashList)
  281.         dashIndex = 0;
  282.     }
  283.     *pDashIndex = dashIndex;
  284.     *pDashOffset = dist;
  285. }
  286.