1706f2543Smrg/***********************************************************
2706f2543Smrg
3706f2543SmrgCopyright 1987, 1998  The Open Group
4706f2543Smrg
5706f2543SmrgPermission to use, copy, modify, distribute, and sell this software and its
6706f2543Smrgdocumentation for any purpose is hereby granted without fee, provided that
7706f2543Smrgthe above copyright notice appear in all copies and that both that
8706f2543Smrgcopyright notice and this permission notice appear in supporting
9706f2543Smrgdocumentation.
10706f2543Smrg
11706f2543SmrgThe above copyright notice and this permission notice shall be included in
12706f2543Smrgall copies or substantial portions of the Software.
13706f2543Smrg
14706f2543SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15706f2543SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16706f2543SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17706f2543SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18706f2543SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19706f2543SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20706f2543Smrg
21706f2543SmrgExcept as contained in this notice, the name of The Open Group shall not be
22706f2543Smrgused in advertising or otherwise to promote the sale, use or other dealings
23706f2543Smrgin this Software without prior written authorization from The Open Group.
24706f2543Smrg
25706f2543Smrg
26706f2543SmrgCopyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
27706f2543Smrg
28706f2543Smrg                        All Rights Reserved
29706f2543Smrg
30706f2543SmrgPermission to use, copy, modify, and distribute this software and its
31706f2543Smrgdocumentation for any purpose and without fee is hereby granted,
32706f2543Smrgprovided that the above copyright notice appear in all copies and that
33706f2543Smrgboth that copyright notice and this permission notice appear in
34706f2543Smrgsupporting documentation, and that the name of Digital not be
35706f2543Smrgused in advertising or publicity pertaining to distribution of the
36706f2543Smrgsoftware without specific, written prior permission.
37706f2543Smrg
38706f2543SmrgDIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
39706f2543SmrgALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
40706f2543SmrgDIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
41706f2543SmrgANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
42706f2543SmrgWHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
43706f2543SmrgARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
44706f2543SmrgSOFTWARE.
45706f2543Smrg
46706f2543Smrg******************************************************************/
47706f2543Smrg#ifdef HAVE_DIX_CONFIG_H
48706f2543Smrg#include <dix-config.h>
49706f2543Smrg#endif
50706f2543Smrg
51706f2543Smrg#include <math.h>
52706f2543Smrg#include <X11/X.h>
53706f2543Smrg#include "gcstruct.h"
54706f2543Smrg#include "windowstr.h"
55706f2543Smrg#include "pixmapstr.h"
56706f2543Smrg#include "mifpoly.h"
57706f2543Smrg
58706f2543Smrgstatic int GetFPolyYBounds(SppPointPtr pts, int n, double yFtrans,
59706f2543Smrg			   int *by, int *ty);
60706f2543Smrg
61706f2543Smrg/*
62706f2543Smrg *	Written by Todd Newman; April. 1987.
63706f2543Smrg *
64706f2543Smrg *	Fill a convex polygon.  If the given polygon
65706f2543Smrg *	is not convex, then the result is undefined.
66706f2543Smrg *	The algorithm is to order the edges from smallest
67706f2543Smrg *	y to largest by partitioning the array into a left
68706f2543Smrg *	edge list and a right edge list.  The algorithm used
69706f2543Smrg *	to traverse each edge is digital differencing analyzer
70706f2543Smrg *	line algorithm with y as the major axis. There's some funny linear
71706f2543Smrg *	interpolation involved because of the subpixel postioning.
72706f2543Smrg */
73706f2543Smrgvoid
74706f2543SmrgmiFillSppPoly(
75706f2543Smrg    DrawablePtr		dst,
76706f2543Smrg    GCPtr		pgc,
77706f2543Smrg    int			count,          /* number of points */
78706f2543Smrg    SppPointPtr		ptsIn,          /* the points */
79706f2543Smrg    int			xTrans, int yTrans,	/* Translate each point by this */
80706f2543Smrg    double		xFtrans,
81706f2543Smrg    double		yFtrans                 /* translate before conversion
82706f2543Smrg						   by this amount.  This provides
83706f2543Smrg						   a mechanism to match rounding
84706f2543Smrg						   errors with any shape that must
85706f2543Smrg						   meet the polygon exactly.
86706f2543Smrg						 */
87706f2543Smrg    )
88706f2543Smrg{
89706f2543Smrg    double		xl = 0.0, xr = 0.0,	/* x vals of left and right edges */
90706f2543Smrg          		ml = 0.0,      	/* left edge slope */
91706f2543Smrg          		mr = 0.0,       /* right edge slope */
92706f2543Smrg          		dy,             /* delta y */
93706f2543Smrg    			i;              /* loop counter */
94706f2543Smrg    int			y,              /* current scanline */
95706f2543Smrg    			j,
96706f2543Smrg    			imin,           /* index of vertex with smallest y */
97706f2543Smrg    			ymin,           /* y-extents of polygon */
98706f2543Smrg    			ymax,
99706f2543Smrg    			*width,
100706f2543Smrg    			*FirstWidth,    /* output buffer */
101706f2543Smrg    	 		*Marked;	/* set if this vertex has been used */
102706f2543Smrg    int			left, right,	/* indices to first endpoints */
103706f2543Smrg    			nextleft,
104706f2543Smrg                 	nextright;	/* indices to second endpoints */
105706f2543Smrg    DDXPointPtr 	ptsOut,
106706f2543Smrg    			FirstPoint;	/* output buffer */
107706f2543Smrg
108706f2543Smrg    if (pgc->miTranslate)
109706f2543Smrg    {
110706f2543Smrg	xTrans += dst->x;
111706f2543Smrg	yTrans += dst->y;
112706f2543Smrg    }
113706f2543Smrg
114706f2543Smrg    imin = GetFPolyYBounds(ptsIn, count, yFtrans, &ymin, &ymax);
115706f2543Smrg
116706f2543Smrg    y = ymax - ymin + 1;
117706f2543Smrg    if ((count < 3) || (y <= 0))
118706f2543Smrg	return;
119706f2543Smrg    ptsOut = FirstPoint = malloc(sizeof(DDXPointRec) * y);
120706f2543Smrg    width = FirstWidth = malloc(sizeof(int) * y);
121706f2543Smrg    Marked = malloc(sizeof(int) * count);
122706f2543Smrg
123706f2543Smrg    if(!ptsOut || !width || !Marked)
124706f2543Smrg    {
125706f2543Smrg	free(Marked);
126706f2543Smrg	free(width);
127706f2543Smrg	free(ptsOut);
128706f2543Smrg	return;
129706f2543Smrg    }
130706f2543Smrg
131706f2543Smrg    for(j = 0; j < count; j++)
132706f2543Smrg	Marked[j] = 0;
133706f2543Smrg    nextleft = nextright = imin;
134706f2543Smrg    Marked[imin] = -1;
135706f2543Smrg    y = ICEIL(ptsIn[nextleft].y + yFtrans);
136706f2543Smrg
137706f2543Smrg    /*
138706f2543Smrg     *  loop through all edges of the polygon
139706f2543Smrg     */
140706f2543Smrg    do
141706f2543Smrg    {
142706f2543Smrg        /* add a left edge if we need to */
143706f2543Smrg        if ((y > (ptsIn[nextleft].y + yFtrans) ||
144706f2543Smrg 	     ISEQUAL(y, ptsIn[nextleft].y + yFtrans)) &&
145706f2543Smrg	     Marked[nextleft] != 1)
146706f2543Smrg	{
147706f2543Smrg	    Marked[nextleft]++;
148706f2543Smrg            left = nextleft++;
149706f2543Smrg
150706f2543Smrg            /* find the next edge, considering the end conditions */
151706f2543Smrg            if (nextleft >= count)
152706f2543Smrg                nextleft = 0;
153706f2543Smrg
154706f2543Smrg            /* now compute the starting point and slope */
155706f2543Smrg	    dy = ptsIn[nextleft].y - ptsIn[left].y;
156706f2543Smrg	    if (dy != 0.0)
157706f2543Smrg	    {
158706f2543Smrg		ml = (ptsIn[nextleft].x - ptsIn[left].x) / dy;
159706f2543Smrg		dy = y - (ptsIn[left].y + yFtrans);
160706f2543Smrg		xl = (ptsIn[left].x + xFtrans) + ml * max(dy, 0);
161706f2543Smrg	    }
162706f2543Smrg        }
163706f2543Smrg
164706f2543Smrg        /* add a right edge if we need to */
165706f2543Smrg        if ((y > ptsIn[nextright].y + yFtrans) ||
166706f2543Smrg 	     (ISEQUAL(y, ptsIn[nextright].y + yFtrans)
167706f2543Smrg	     && Marked[nextright] != 1))
168706f2543Smrg	{
169706f2543Smrg	    Marked[nextright]++;
170706f2543Smrg            right = nextright--;
171706f2543Smrg
172706f2543Smrg            /* find the next edge, considering the end conditions */
173706f2543Smrg            if (nextright < 0)
174706f2543Smrg                nextright = count - 1;
175706f2543Smrg
176706f2543Smrg            /* now compute the starting point and slope */
177706f2543Smrg	    dy = ptsIn[nextright].y - ptsIn[right].y;
178706f2543Smrg	    if (dy != 0.0)
179706f2543Smrg	    {
180706f2543Smrg		mr = (ptsIn[nextright].x - ptsIn[right].x) / dy;
181706f2543Smrg		dy = y - (ptsIn[right].y + yFtrans);
182706f2543Smrg		xr = (ptsIn[right].x + xFtrans) + mr * max(dy, 0);
183706f2543Smrg	    }
184706f2543Smrg        }
185706f2543Smrg
186706f2543Smrg
187706f2543Smrg        /*
188706f2543Smrg         *  generate scans to fill while we still have
189706f2543Smrg         *  a right edge as well as a left edge.
190706f2543Smrg         */
191706f2543Smrg        i = (min(ptsIn[nextleft].y, ptsIn[nextright].y) + yFtrans) - y;
192706f2543Smrg
193706f2543Smrg	if (i < EPSILON)
194706f2543Smrg	{
195706f2543Smrg	    if(Marked[nextleft] && Marked[nextright])
196706f2543Smrg	    {
197706f2543Smrg	        /* Arrgh, we're trapped! (no more points)
198706f2543Smrg	         * Out, we've got to get out of here before this decadence saps
199706f2543Smrg	         * our will completely! */
200706f2543Smrg	        break;
201706f2543Smrg	    }
202706f2543Smrg	    continue;
203706f2543Smrg	}
204706f2543Smrg	else
205706f2543Smrg	{
206706f2543Smrg		j = (int) i;
207706f2543Smrg		if(!j)
208706f2543Smrg		    j++;
209706f2543Smrg	}
210706f2543Smrg        while (j > 0)
211706f2543Smrg        {
212706f2543Smrg	    int cxl, cxr;
213706f2543Smrg
214706f2543Smrg            ptsOut->y = (y) + yTrans;
215706f2543Smrg
216706f2543Smrg	    cxl = ICEIL(xl);
217706f2543Smrg	    cxr = ICEIL(xr);
218706f2543Smrg            /* reverse the edges if necessary */
219706f2543Smrg            if (xl < xr)
220706f2543Smrg            {
221706f2543Smrg                *(width++) = cxr - cxl;
222706f2543Smrg                (ptsOut++)->x = cxl + xTrans;
223706f2543Smrg            }
224706f2543Smrg            else
225706f2543Smrg            {
226706f2543Smrg                *(width++) = cxl - cxr;
227706f2543Smrg                (ptsOut++)->x = cxr + xTrans;
228706f2543Smrg            }
229706f2543Smrg            y++;
230706f2543Smrg
231706f2543Smrg            /* increment down the edges */
232706f2543Smrg	    xl += ml;
233706f2543Smrg	    xr += mr;
234706f2543Smrg	    j--;
235706f2543Smrg        }
236706f2543Smrg    }  while (y <= ymax);
237706f2543Smrg
238706f2543Smrg    /* Finally, fill the spans we've collected */
239706f2543Smrg    (*pgc->ops->FillSpans)(dst, pgc,
240706f2543Smrg		      ptsOut-FirstPoint, FirstPoint, FirstWidth, 1);
241706f2543Smrg    free(Marked);
242706f2543Smrg    free(FirstWidth);
243706f2543Smrg    free(FirstPoint);
244706f2543Smrg}
245706f2543Smrg
246706f2543Smrg
247706f2543Smrg/* Find the index of the point with the smallest y.also return the
248706f2543Smrg * smallest and largest y */
249706f2543Smrgstatic
250706f2543Smrgint
251706f2543SmrgGetFPolyYBounds(
252706f2543Smrg    SppPointPtr			pts,
253706f2543Smrg    int 			n,
254706f2543Smrg    double			yFtrans,
255706f2543Smrg    int 			*by,
256706f2543Smrg    int				*ty)
257706f2543Smrg{
258706f2543Smrg    SppPointPtr			ptMin;
259706f2543Smrg    double 			ymin, ymax;
260706f2543Smrg    SppPointPtr			ptsStart = pts;
261706f2543Smrg
262706f2543Smrg    ptMin = pts;
263706f2543Smrg    ymin = ymax = (pts++)->y;
264706f2543Smrg
265706f2543Smrg    while (--n > 0) {
266706f2543Smrg        if (pts->y < ymin)
267706f2543Smrg	{
268706f2543Smrg            ptMin = pts;
269706f2543Smrg            ymin = pts->y;
270706f2543Smrg        }
271706f2543Smrg	if(pts->y > ymax)
272706f2543Smrg            ymax = pts->y;
273706f2543Smrg
274706f2543Smrg        pts++;
275706f2543Smrg    }
276706f2543Smrg
277706f2543Smrg    *by = ICEIL(ymin + yFtrans);
278706f2543Smrg    *ty = ICEIL(ymax + yFtrans - 1);
279706f2543Smrg    return ptMin-ptsStart;
280706f2543Smrg}
281