]> git.sesse.net Git - rdpsrv/blob - Xserver/programs/Xserver/mi/midash.c
d566ff0834d88518eddb574aaf0aa45601778adb
[rdpsrv] / Xserver / programs / Xserver / mi / midash.c
1 /***********************************************************
2
3 Copyright (c) 1987  X Consortium
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 Except as contained in this notice, the name of the X Consortium shall not be
23 used in advertising or otherwise to promote the sale, use or other dealings
24 in this Software without prior written authorization from the X Consortium.
25
26
27 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
28
29                         All Rights Reserved
30
31 Permission to use, copy, modify, and distribute this software and its 
32 documentation for any purpose and without fee is hereby granted, 
33 provided that the above copyright notice appear in all copies and that
34 both that copyright notice and this permission notice appear in 
35 supporting documentation, and that the name of Digital not be
36 used in advertising or publicity pertaining to distribution of the
37 software without specific, written prior permission.  
38
39 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
40 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
41 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
42 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
43 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
44 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
45 SOFTWARE.
46
47 ******************************************************************/
48 /* $XConsortium: midash.c,v 5.5 94/04/17 20:27:27 dpw Exp $ */
49 #include "miscstruct.h"
50 #include "mistruct.h"
51 #include "mifpoly.h"
52
53 static miDashPtr CheckDashStorage();
54
55 /* return a list of DashRec.  there will be an extra
56 entry at the end holding the last point of the polyline.
57    this means that the code that actually draws dashes can
58 get a pair of points for every dash.  only the point in the last
59 dash record is useful; the other fields are not used.
60    nseg is the number of segments, not the number of points.
61
62 example:
63
64    dash1.start
65    dash2.start
66    dash3.start
67    last-point
68
69 defines a list of segments
70    (dash1.pt, dash2.pt)
71    (dash2.pt, dash3.pt)
72    (dash3.pt, last-point)
73 and nseg == 3.
74
75 NOTE:
76     EVEN_DASH == ~ODD_DASH
77
78 NOTE ALSO:
79     miDashLines may return 0 segments, going from pt[0] to pt[0] with one dash.
80 */
81
82 miDashPtr
83 miDashLine(npt, ppt, nDash, pDash, offset, pnseg)
84 int npt;
85 DDXPointPtr ppt;
86 unsigned int nDash;
87 unsigned char *pDash;
88 unsigned int offset;
89 int *pnseg;
90 {
91     DDXPointRec pt1, pt2;
92     int lenCur;         /* npt used from this dash */
93     int lenMax;         /* npt in this dash */
94     int iDash = 0;      /* index of current dash */
95     int which;          /* EVEN_DASH or ODD_DASH */
96     miDashPtr pseg;     /* list of dash segments */
97     miDashPtr psegBase; /* start of list */
98     int nseg = 0;       /* number of dashes so far */
99     int nsegMax = 0;    /* num segs we can fit in this list */
100
101     int x, y, len;
102     int adx, ady, signdx, signdy;
103     int du, dv, e1, e2, e, base_e = 0;
104
105     lenCur = offset;
106     which = EVEN_DASH;
107     while(lenCur >= pDash[iDash])
108     {
109         lenCur -= pDash[iDash];
110         iDash++;
111         if (iDash >= nDash)
112             iDash = 0;
113         which = ~which;
114     }
115     lenMax = pDash[iDash];
116
117     psegBase = (miDashPtr)NULL;
118     pt2 = ppt[0];               /* just in case there is only one point */
119
120     while(--npt)
121     {
122         if (PtEqual(ppt[0], ppt[1]))
123         {
124             ppt++;
125             continue;           /* no duplicated points in polyline */
126         }
127         pt1 = *ppt++;
128         pt2 = *ppt;
129
130         adx = pt2.x - pt1.x;
131         ady = pt2.y - pt1.y;
132         signdx = sign(adx);
133         signdy = sign(ady);
134         adx = abs(adx);
135         ady = abs(ady);
136
137         if (adx > ady)
138         {
139             du = adx;
140             dv = ady;
141             len = adx;
142         }
143         else
144         {
145             du = ady;
146             dv = adx;
147             len = ady;
148         }
149
150         e1 = dv * 2;
151         e2 = e1 - 2*du;
152         e = e1 - du;
153         x = pt1.x;
154         y = pt1.y;
155
156         nseg++;
157         pseg = CheckDashStorage(&psegBase, nseg, &nsegMax);
158         if (!pseg)
159             return (miDashPtr)NULL;
160         pseg->pt = pt1;
161         pseg->e1 = e1;
162         pseg->e2 = e2;
163         base_e = pseg->e = e;
164         pseg->which = which;
165         pseg->newLine = 1;
166
167         while (len--)
168         {
169             if (adx > ady)
170             {
171                 /* X_AXIS */
172                 if (((signdx > 0) && (e < 0)) ||
173                     ((signdx <=0) && (e <=0))
174                    )
175                 {
176                     e += e1;
177                 }
178                 else
179                 {
180                     y += signdy;
181                     e += e2;
182                 }
183                 x += signdx;
184             }
185             else
186             {
187                 /* Y_AXIS */
188                 if (((signdx > 0) && (e < 0)) ||
189                     ((signdx <=0) && (e <=0))
190                    )
191                 {
192                     e +=e1;
193                 }
194                 else
195                 {
196                     x += signdx;
197                     e += e2;
198                 }
199                 y += signdy;
200             }
201
202             lenCur++;
203             if (lenCur >= lenMax && (len || npt <= 1))
204             {
205                 nseg++;
206                 pseg = CheckDashStorage(&psegBase, nseg, &nsegMax);
207                 if (!pseg)
208                     return (miDashPtr)NULL;
209                 pseg->pt.x = x;
210                 pseg->pt.y = y;
211                 pseg->e1 = e1;
212                 pseg->e2 = e2;
213                 pseg->e = e;
214                 which = ~which;
215                 pseg->which = which;
216                 pseg->newLine = 0;
217
218                 /* move on to next dash */
219                 iDash++;
220                 if (iDash >= nDash)
221                     iDash = 0;
222                 lenMax = pDash[iDash];
223                 lenCur = 0;
224             }
225         } /* while len-- */
226     } /* while --npt */
227
228     if (lenCur == 0 && nseg != 0)
229     {
230         nseg--;
231         which = ~which;
232     }
233     *pnseg = nseg;
234     pseg = CheckDashStorage(&psegBase, nseg+1, &nsegMax);
235     if (!pseg)
236         return (miDashPtr)NULL;
237     pseg->pt = pt2;
238     pseg->e = base_e;
239     pseg->which = which;
240     pseg->newLine = 0;
241     return psegBase;
242
243
244
245 #define NSEGDELTA 16
246
247 /* returns a pointer to the pseg[nseg-1], growing the storage as
248 necessary.  this interface seems unnecessarily cumbersome.
249
250 */
251
252 static
253 miDashPtr
254 CheckDashStorage(ppseg, nseg, pnsegMax)
255 miDashPtr *ppseg;               /* base pointer */
256 int nseg;                       /* number of segment we want to write to */
257 int *pnsegMax;                  /* size (in segments) of list so far */
258 {
259     if (nseg > *pnsegMax)
260     {
261         miDashPtr newppseg;
262
263         *pnsegMax += NSEGDELTA;
264         newppseg = (miDashPtr)xrealloc(*ppseg,
265                                        (*pnsegMax)*sizeof(miDashRec));
266         if (!newppseg)
267         {
268             xfree(*ppseg);
269             return (miDashPtr)NULL;
270         }
271         *ppseg = newppseg;
272     }
273     return(*ppseg+(nseg-1));
274 }
275
276 void
277 miStepDash (dist, pDashIndex, pDash, numInDashList, pDashOffset)
278     int dist;                   /* distance to step */
279     int *pDashIndex;            /* current dash */
280     unsigned char *pDash;       /* dash list */
281     int numInDashList;          /* total length of dash list */
282     int *pDashOffset;           /* offset into current dash */
283 {
284     int dashIndex, dashOffset;
285     int totallen;
286     int i;
287     
288     dashIndex = *pDashIndex;
289     dashOffset = *pDashOffset;
290     if (dist < pDash[dashIndex] - dashOffset)
291     {
292         *pDashOffset = dashOffset + dist;
293         return;
294     }
295     dist -= pDash[dashIndex] - dashOffset;
296     if (++dashIndex == numInDashList)
297         dashIndex = 0;
298     totallen = 0;
299     for (i = 0; i < numInDashList; i++)
300         totallen += pDash[i];
301     if (totallen <= dist)
302         dist = dist % totallen;
303     while (dist >= pDash[dashIndex])
304     {
305         dist -= pDash[dashIndex];
306         if (++dashIndex == numInDashList)
307             dashIndex = 0;
308     }
309     *pDashIndex = dashIndex;
310     *pDashOffset = dist;
311 }