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