mifillarc.c revision 6747b715
105b261ecSmrg/************************************************************
205b261ecSmrg
305b261ecSmrgCopyright 1989, 1998  The Open Group
405b261ecSmrg
505b261ecSmrgPermission to use, copy, modify, distribute, and sell this software and its
605b261ecSmrgdocumentation for any purpose is hereby granted without fee, provided that
705b261ecSmrgthe above copyright notice appear in all copies and that both that
805b261ecSmrgcopyright notice and this permission notice appear in supporting
905b261ecSmrgdocumentation.
1005b261ecSmrg
1105b261ecSmrgThe above copyright notice and this permission notice shall be included in
1205b261ecSmrgall copies or substantial portions of the Software.
1305b261ecSmrg
1405b261ecSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1505b261ecSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1605b261ecSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
1705b261ecSmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
1805b261ecSmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1905b261ecSmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2005b261ecSmrg
2105b261ecSmrgExcept as contained in this notice, the name of The Open Group shall not be
2205b261ecSmrgused in advertising or otherwise to promote the sale, use or other dealings
2305b261ecSmrgin this Software without prior written authorization from The Open Group.
2405b261ecSmrg
2505b261ecSmrgAuthor:  Bob Scheifler, MIT X Consortium
2605b261ecSmrg
2705b261ecSmrg********************************************************/
2805b261ecSmrg
2905b261ecSmrg
3005b261ecSmrg#ifdef HAVE_DIX_CONFIG_H
3105b261ecSmrg#include <dix-config.h>
3205b261ecSmrg#endif
3305b261ecSmrg
3405b261ecSmrg#include <math.h>
3505b261ecSmrg#include <X11/X.h>
3605b261ecSmrg#include <X11/Xprotostr.h>
3705b261ecSmrg#include "regionstr.h"
3805b261ecSmrg#include "gcstruct.h"
3905b261ecSmrg#include "pixmapstr.h"
4005b261ecSmrg#include "mifpoly.h"
4105b261ecSmrg#include "mi.h"
4205b261ecSmrg#include "mifillarc.h"
4305b261ecSmrg
4405b261ecSmrg#define QUADRANT (90 * 64)
4505b261ecSmrg#define HALFCIRCLE (180 * 64)
4605b261ecSmrg#define QUADRANT3 (270 * 64)
4705b261ecSmrg
4805b261ecSmrg#ifndef M_PI
4905b261ecSmrg#define M_PI	3.14159265358979323846
5005b261ecSmrg#endif
5105b261ecSmrg
5205b261ecSmrg#define Dsin(d)	sin((double)d*(M_PI/11520.0))
5305b261ecSmrg#define Dcos(d)	cos((double)d*(M_PI/11520.0))
5405b261ecSmrg
556747b715Smrgvoid
564642e01fSmrgmiFillArcSetup(xArc *arc, miFillArcRec *info)
5705b261ecSmrg{
5805b261ecSmrg    info->y = arc->height >> 1;
5905b261ecSmrg    info->dy = arc->height & 1;
6005b261ecSmrg    info->yorg = arc->y + info->y;
6105b261ecSmrg    info->dx = arc->width & 1;
6205b261ecSmrg    info->xorg = arc->x + (arc->width >> 1) + info->dx;
6305b261ecSmrg    info->dx = 1 - info->dx;
6405b261ecSmrg    if (arc->width == arc->height)
6505b261ecSmrg    {
6605b261ecSmrg	/* (2x - 2xorg)^2 = d^2 - (2y - 2yorg)^2 */
6705b261ecSmrg	/* even: xorg = yorg = 0   odd:  xorg = .5, yorg = -.5 */
6805b261ecSmrg	info->ym = 8;
6905b261ecSmrg	info->xm = 8;
7005b261ecSmrg	info->yk = info->y << 3;
7105b261ecSmrg	if (!info->dx)
7205b261ecSmrg	{
7305b261ecSmrg	    info->xk = 0;
7405b261ecSmrg	    info->e = -1;
7505b261ecSmrg	}
7605b261ecSmrg	else
7705b261ecSmrg	{
7805b261ecSmrg	    info->y++;
7905b261ecSmrg	    info->yk += 4;
8005b261ecSmrg	    info->xk = -4;
8105b261ecSmrg	    info->e = - (info->y << 3);
8205b261ecSmrg	}
8305b261ecSmrg    }
8405b261ecSmrg    else
8505b261ecSmrg    {
8605b261ecSmrg	/* h^2 * (2x - 2xorg)^2 = w^2 * h^2 - w^2 * (2y - 2yorg)^2 */
8705b261ecSmrg	/* even: xorg = yorg = 0   odd:  xorg = .5, yorg = -.5 */
8805b261ecSmrg	info->ym = (arc->width * arc->width) << 3;
8905b261ecSmrg	info->xm = (arc->height * arc->height) << 3;
9005b261ecSmrg	info->yk = info->y * info->ym;
9105b261ecSmrg	if (!info->dy)
9205b261ecSmrg	    info->yk -= info->ym >> 1;
9305b261ecSmrg	if (!info->dx)
9405b261ecSmrg	{
9505b261ecSmrg	    info->xk = 0;
9605b261ecSmrg	    info->e = - (info->xm >> 3);
9705b261ecSmrg	}
9805b261ecSmrg	else
9905b261ecSmrg	{
10005b261ecSmrg	    info->y++;
10105b261ecSmrg	    info->yk += info->ym;
10205b261ecSmrg	    info->xk = -(info->xm >> 1);
10305b261ecSmrg	    info->e = info->xk - info->yk;
10405b261ecSmrg	}
10505b261ecSmrg    }
10605b261ecSmrg}
10705b261ecSmrg
10805b261ecSmrgstatic void
10905b261ecSmrgmiFillArcDSetup(xArc *arc, miFillArcDRec *info)
11005b261ecSmrg{
11105b261ecSmrg    /* h^2 * (2x - 2xorg)^2 = w^2 * h^2 - w^2 * (2y - 2yorg)^2 */
11205b261ecSmrg    /* even: xorg = yorg = 0   odd:  xorg = .5, yorg = -.5 */
11305b261ecSmrg    info->y = arc->height >> 1;
11405b261ecSmrg    info->dy = arc->height & 1;
11505b261ecSmrg    info->yorg = arc->y + info->y;
11605b261ecSmrg    info->dx = arc->width & 1;
11705b261ecSmrg    info->xorg = arc->x + (arc->width >> 1) + info->dx;
11805b261ecSmrg    info->dx = 1 - info->dx;
11905b261ecSmrg    info->ym = ((double)arc->width) * (arc->width * 8);
12005b261ecSmrg    info->xm = ((double)arc->height) * (arc->height * 8);
12105b261ecSmrg    info->yk = info->y * info->ym;
12205b261ecSmrg    if (!info->dy)
12305b261ecSmrg	info->yk -= info->ym / 2.0;
12405b261ecSmrg    if (!info->dx)
12505b261ecSmrg    {
12605b261ecSmrg	info->xk = 0;
12705b261ecSmrg	info->e = - (info->xm / 8.0);
12805b261ecSmrg    }
12905b261ecSmrg    else
13005b261ecSmrg    {
13105b261ecSmrg	info->y++;
13205b261ecSmrg	info->yk += info->ym;
13305b261ecSmrg	info->xk = -info->xm / 2.0;
13405b261ecSmrg	info->e = info->xk - info->yk;
13505b261ecSmrg    }
13605b261ecSmrg}
13705b261ecSmrg
13805b261ecSmrgstatic void
13905b261ecSmrgmiGetArcEdge(
14005b261ecSmrg	     xArc *arc,
14105b261ecSmrg	     miSliceEdgePtr edge,
14205b261ecSmrg	     int k,
14305b261ecSmrg	     Bool top,
14405b261ecSmrg	     Bool left )
14505b261ecSmrg{
14605b261ecSmrg    int xady, y;
14705b261ecSmrg
14805b261ecSmrg    y = arc->height >> 1;
14905b261ecSmrg    if (!(arc->width & 1))
15005b261ecSmrg	y++;
15105b261ecSmrg    if (!top)
15205b261ecSmrg    {
15305b261ecSmrg	y = -y;
15405b261ecSmrg	if (arc->height & 1)
15505b261ecSmrg	    y--;
15605b261ecSmrg    }
15705b261ecSmrg    xady = k + y * edge->dx;
15805b261ecSmrg    if (xady <= 0)
15905b261ecSmrg	edge->x = - ((-xady) / edge->dy + 1);
16005b261ecSmrg    else
16105b261ecSmrg	edge->x = (xady - 1) / edge->dy;
16205b261ecSmrg    edge->e = xady - edge->x * edge->dy;
16305b261ecSmrg    if ((top && (edge->dx < 0)) || (!top && (edge->dx > 0)))
16405b261ecSmrg	edge->e = edge->dy - edge->e + 1;
16505b261ecSmrg    if (left)
16605b261ecSmrg	edge->x++;
16705b261ecSmrg    edge->x += arc->x + (arc->width >> 1);
16805b261ecSmrg    if (edge->dx > 0)
16905b261ecSmrg    {
17005b261ecSmrg	edge->deltax = 1;
17105b261ecSmrg	edge->stepx = edge->dx / edge->dy;
17205b261ecSmrg	edge->dx = edge->dx % edge->dy;
17305b261ecSmrg    }
17405b261ecSmrg    else
17505b261ecSmrg    {
17605b261ecSmrg	edge->deltax = -1;
17705b261ecSmrg	edge->stepx = - ((-edge->dx) / edge->dy);
17805b261ecSmrg	edge->dx = (-edge->dx) % edge->dy;
17905b261ecSmrg    }
18005b261ecSmrg    if (!top)
18105b261ecSmrg    {
18205b261ecSmrg	edge->deltax = -edge->deltax;
18305b261ecSmrg	edge->stepx = -edge->stepx;
18405b261ecSmrg    }
18505b261ecSmrg}
18605b261ecSmrg
18705b261ecSmrgstatic void
18805b261ecSmrgmiEllipseAngleToSlope (int angle, int width, int height, int *dxp, int *dyp,
18905b261ecSmrg		       double *d_dxp, double *d_dyp)
19005b261ecSmrg{
19105b261ecSmrg    int	    dx, dy;
19205b261ecSmrg    double  d_dx, d_dy, scale;
19305b261ecSmrg    Bool    negative_dx, negative_dy;
19405b261ecSmrg
19505b261ecSmrg    switch (angle) {
19605b261ecSmrg    case 0:
19705b261ecSmrg	*dxp = -1;
19805b261ecSmrg	*dyp = 0;
19905b261ecSmrg	if (d_dxp) {
20005b261ecSmrg	    *d_dxp = width / 2.0;
20105b261ecSmrg	    *d_dyp = 0;
20205b261ecSmrg	}
20305b261ecSmrg	break;
20405b261ecSmrg    case QUADRANT:
20505b261ecSmrg	*dxp = 0;
20605b261ecSmrg	*dyp = 1;
20705b261ecSmrg	if (d_dxp) {
20805b261ecSmrg	    *d_dxp = 0;
20905b261ecSmrg	    *d_dyp = - height / 2.0;
21005b261ecSmrg	}
21105b261ecSmrg	break;
21205b261ecSmrg    case HALFCIRCLE:
21305b261ecSmrg	*dxp = 1;
21405b261ecSmrg	*dyp = 0;
21505b261ecSmrg	if (d_dxp) {
21605b261ecSmrg	    *d_dxp = - width / 2.0;
21705b261ecSmrg	    *d_dyp = 0;
21805b261ecSmrg	}
21905b261ecSmrg	break;
22005b261ecSmrg    case QUADRANT3:
22105b261ecSmrg	*dxp = 0;
22205b261ecSmrg	*dyp = -1;
22305b261ecSmrg	if (d_dxp) {
22405b261ecSmrg	    *d_dxp = 0;
22505b261ecSmrg	    *d_dyp = height / 2.0;
22605b261ecSmrg	}
22705b261ecSmrg	break;
22805b261ecSmrg    default:
22905b261ecSmrg	d_dx = Dcos(angle) * width;
23005b261ecSmrg	d_dy = Dsin(angle) * height;
23105b261ecSmrg	if (d_dxp) {
23205b261ecSmrg	    *d_dxp = d_dx / 2.0;
23305b261ecSmrg	    *d_dyp = - d_dy / 2.0;
23405b261ecSmrg	}
23505b261ecSmrg	negative_dx = FALSE;
23605b261ecSmrg	if (d_dx < 0.0)
23705b261ecSmrg	{
23805b261ecSmrg	    d_dx = -d_dx;
23905b261ecSmrg	    negative_dx = TRUE;
24005b261ecSmrg	}
24105b261ecSmrg	negative_dy = FALSE;
24205b261ecSmrg	if (d_dy < 0.0)
24305b261ecSmrg	{
24405b261ecSmrg	    d_dy = -d_dy;
24505b261ecSmrg	    negative_dy = TRUE;
24605b261ecSmrg	}
24705b261ecSmrg	scale = d_dx;
24805b261ecSmrg	if (d_dy > d_dx)
24905b261ecSmrg	    scale = d_dy;
25005b261ecSmrg	dx = floor ((d_dx * 32768) / scale + 0.5);
25105b261ecSmrg	if (negative_dx)
25205b261ecSmrg	    dx = -dx;
25305b261ecSmrg	*dxp = dx;
25405b261ecSmrg	dy = floor ((d_dy * 32768) / scale + 0.5);
25505b261ecSmrg	if (negative_dy)
25605b261ecSmrg	    dy = -dy;
25705b261ecSmrg	*dyp = dy;
25805b261ecSmrg	break;
25905b261ecSmrg    }
26005b261ecSmrg}
26105b261ecSmrg
26205b261ecSmrgstatic void
26305b261ecSmrgmiGetPieEdge(
26405b261ecSmrg	     xArc *arc,
26505b261ecSmrg	     int angle,
26605b261ecSmrg	     miSliceEdgePtr edge,
26705b261ecSmrg	     Bool top,
26805b261ecSmrg	     Bool left )
26905b261ecSmrg{
27005b261ecSmrg    int k;
27105b261ecSmrg    int	dx, dy;
27205b261ecSmrg
27305b261ecSmrg    miEllipseAngleToSlope (angle, arc->width, arc->height, &dx, &dy, 0, 0);
27405b261ecSmrg
27505b261ecSmrg    if (dy == 0)
27605b261ecSmrg    {
27705b261ecSmrg	edge->x = left ? -65536 : 65536;
27805b261ecSmrg	edge->stepx = 0;
27905b261ecSmrg	edge->e = 0;
28005b261ecSmrg	edge->dx = -1;
28105b261ecSmrg	return;
28205b261ecSmrg    }
28305b261ecSmrg    if (dx == 0)
28405b261ecSmrg    {
28505b261ecSmrg	edge->x = arc->x + (arc->width >> 1);
28605b261ecSmrg	if (left && (arc->width & 1))
28705b261ecSmrg	    edge->x++;
28805b261ecSmrg	else if (!left && !(arc->width & 1))
28905b261ecSmrg	    edge->x--;
29005b261ecSmrg	edge->stepx = 0;
29105b261ecSmrg	edge->e = 0;
29205b261ecSmrg	edge->dx = -1;
29305b261ecSmrg	return;
29405b261ecSmrg    }
29505b261ecSmrg    if (dy < 0) {
29605b261ecSmrg	dx = -dx;
29705b261ecSmrg	dy = -dy;
29805b261ecSmrg    }
29905b261ecSmrg    k = (arc->height & 1) ? dx : 0;
30005b261ecSmrg    if (arc->width & 1)
30105b261ecSmrg	k += dy;
30205b261ecSmrg    edge->dx = dx << 1;
30305b261ecSmrg    edge->dy = dy << 1;
30405b261ecSmrg    miGetArcEdge(arc, edge, k, top, left);
30505b261ecSmrg}
30605b261ecSmrg
3076747b715Smrgvoid
3084642e01fSmrgmiFillArcSliceSetup(xArc *arc, miArcSliceRec *slice, GCPtr pGC)
30905b261ecSmrg{
31005b261ecSmrg    int angle1, angle2;
31105b261ecSmrg
31205b261ecSmrg    angle1 = arc->angle1;
31305b261ecSmrg    if (arc->angle2 < 0)
31405b261ecSmrg    {
31505b261ecSmrg	angle2 = angle1;
31605b261ecSmrg	angle1 += arc->angle2;
31705b261ecSmrg    }
31805b261ecSmrg    else
31905b261ecSmrg	angle2 = angle1 + arc->angle2;
32005b261ecSmrg    while (angle1 < 0)
32105b261ecSmrg	angle1 += FULLCIRCLE;
32205b261ecSmrg    while (angle1 >= FULLCIRCLE)
32305b261ecSmrg	angle1 -= FULLCIRCLE;
32405b261ecSmrg    while (angle2 < 0)
32505b261ecSmrg	angle2 += FULLCIRCLE;
32605b261ecSmrg    while (angle2 >= FULLCIRCLE)
32705b261ecSmrg	angle2 -= FULLCIRCLE;
32805b261ecSmrg    slice->min_top_y = 0;
32905b261ecSmrg    slice->max_top_y = arc->height >> 1;
33005b261ecSmrg    slice->min_bot_y = 1 - (arc->height & 1);
33105b261ecSmrg    slice->max_bot_y = slice->max_top_y - 1;
33205b261ecSmrg    slice->flip_top = FALSE;
33305b261ecSmrg    slice->flip_bot = FALSE;
33405b261ecSmrg    if (pGC->arcMode == ArcPieSlice)
33505b261ecSmrg    {
33605b261ecSmrg	slice->edge1_top = (angle1 < HALFCIRCLE);
33705b261ecSmrg	slice->edge2_top = (angle2 <= HALFCIRCLE);
33805b261ecSmrg	if ((angle2 == 0) || (angle1 == HALFCIRCLE))
33905b261ecSmrg	{
34005b261ecSmrg	    if (angle2 ? slice->edge2_top : slice->edge1_top)
34105b261ecSmrg		slice->min_top_y = slice->min_bot_y;
34205b261ecSmrg	    else
34305b261ecSmrg		slice->min_top_y = arc->height;
34405b261ecSmrg	    slice->min_bot_y = 0;
34505b261ecSmrg	}
34605b261ecSmrg	else if ((angle1 == 0) || (angle2 == HALFCIRCLE))
34705b261ecSmrg	{
34805b261ecSmrg	    slice->min_top_y = slice->min_bot_y;
34905b261ecSmrg	    if (angle1 ? slice->edge1_top : slice->edge2_top)
35005b261ecSmrg		slice->min_bot_y = arc->height;
35105b261ecSmrg	    else
35205b261ecSmrg		slice->min_bot_y = 0;
35305b261ecSmrg	}
35405b261ecSmrg	else if (slice->edge1_top == slice->edge2_top)
35505b261ecSmrg	{
35605b261ecSmrg	    if (angle2 < angle1)
35705b261ecSmrg	    {
35805b261ecSmrg		slice->flip_top = slice->edge1_top;
35905b261ecSmrg		slice->flip_bot = !slice->edge1_top;
36005b261ecSmrg	    }
36105b261ecSmrg	    else if (slice->edge1_top)
36205b261ecSmrg	    {
36305b261ecSmrg		slice->min_top_y = 1;
36405b261ecSmrg		slice->min_bot_y = arc->height;
36505b261ecSmrg	    }
36605b261ecSmrg	    else
36705b261ecSmrg	    {
36805b261ecSmrg		slice->min_bot_y = 0;
36905b261ecSmrg		slice->min_top_y = arc->height;
37005b261ecSmrg	    }
37105b261ecSmrg	}
37205b261ecSmrg	miGetPieEdge(arc, angle1, &slice->edge1,
37305b261ecSmrg		     slice->edge1_top, !slice->edge1_top);
37405b261ecSmrg	miGetPieEdge(arc, angle2, &slice->edge2,
37505b261ecSmrg		     slice->edge2_top, slice->edge2_top);
37605b261ecSmrg    }
37705b261ecSmrg    else
37805b261ecSmrg    {
37905b261ecSmrg	double w2, h2, x1, y1, x2, y2, dx, dy, scale;
38005b261ecSmrg	int signdx, signdy, y, k;
38105b261ecSmrg	Bool isInt1 = TRUE, isInt2 = TRUE;
38205b261ecSmrg
38305b261ecSmrg	w2 = (double)arc->width / 2.0;
38405b261ecSmrg	h2 = (double)arc->height / 2.0;
38505b261ecSmrg	if ((angle1 == 0) || (angle1 == HALFCIRCLE))
38605b261ecSmrg	{
38705b261ecSmrg	    x1 = angle1 ? -w2 : w2;
38805b261ecSmrg	    y1 = 0.0;
38905b261ecSmrg	}
39005b261ecSmrg	else if ((angle1 == QUADRANT) || (angle1 == QUADRANT3))
39105b261ecSmrg	{
39205b261ecSmrg	    x1 = 0.0;
39305b261ecSmrg	    y1 = (angle1 == QUADRANT) ? h2 : -h2;
39405b261ecSmrg	}
39505b261ecSmrg	else
39605b261ecSmrg	{
39705b261ecSmrg	    isInt1 = FALSE;
39805b261ecSmrg	    x1 = Dcos(angle1) * w2;
39905b261ecSmrg	    y1 = Dsin(angle1) * h2;
40005b261ecSmrg	}
40105b261ecSmrg	if ((angle2 == 0) || (angle2 == HALFCIRCLE))
40205b261ecSmrg	{
40305b261ecSmrg	    x2 = angle2 ? -w2 : w2;
40405b261ecSmrg	    y2 = 0.0;
40505b261ecSmrg	}
40605b261ecSmrg	else if ((angle2 == QUADRANT) || (angle2 == QUADRANT3))
40705b261ecSmrg	{
40805b261ecSmrg	    x2 = 0.0;
40905b261ecSmrg	    y2 = (angle2 == QUADRANT) ? h2 : -h2;
41005b261ecSmrg	}
41105b261ecSmrg	else
41205b261ecSmrg	{
41305b261ecSmrg	    isInt2 = FALSE;
41405b261ecSmrg	    x2 = Dcos(angle2) * w2;
41505b261ecSmrg	    y2 = Dsin(angle2) * h2;
41605b261ecSmrg	}
41705b261ecSmrg	dx = x2 - x1;
41805b261ecSmrg	dy = y2 - y1;
41905b261ecSmrg	if (arc->height & 1)
42005b261ecSmrg	{
42105b261ecSmrg	    y1 -= 0.5;
42205b261ecSmrg	    y2 -= 0.5;
42305b261ecSmrg	}
42405b261ecSmrg	if (arc->width & 1)
42505b261ecSmrg	{
42605b261ecSmrg	    x1 += 0.5;
42705b261ecSmrg	    x2 += 0.5;
42805b261ecSmrg	}
42905b261ecSmrg	if (dy < 0.0)
43005b261ecSmrg	{
43105b261ecSmrg	    dy = -dy;
43205b261ecSmrg	    signdy = -1;
43305b261ecSmrg	}
43405b261ecSmrg	else
43505b261ecSmrg	    signdy = 1;
43605b261ecSmrg	if (dx < 0.0)
43705b261ecSmrg	{
43805b261ecSmrg	    dx = -dx;
43905b261ecSmrg	    signdx = -1;
44005b261ecSmrg	}
44105b261ecSmrg	else
44205b261ecSmrg	    signdx = 1;
44305b261ecSmrg	if (isInt1 && isInt2)
44405b261ecSmrg	{
44505b261ecSmrg	    slice->edge1.dx = dx * 2;
44605b261ecSmrg	    slice->edge1.dy = dy * 2;
44705b261ecSmrg	}
44805b261ecSmrg	else
44905b261ecSmrg	{
45005b261ecSmrg	    scale = (dx > dy) ? dx : dy;
45105b261ecSmrg	    slice->edge1.dx = floor((dx * 32768) / scale + .5);
45205b261ecSmrg	    slice->edge1.dy = floor((dy * 32768) / scale + .5);
45305b261ecSmrg	}
45405b261ecSmrg	if (!slice->edge1.dy)
45505b261ecSmrg	{
45605b261ecSmrg	    if (signdx < 0)
45705b261ecSmrg	    {
45805b261ecSmrg		y = floor(y1 + 1.0);
45905b261ecSmrg		if (y >= 0)
46005b261ecSmrg		{
46105b261ecSmrg		    slice->min_top_y = y;
46205b261ecSmrg		    slice->min_bot_y = arc->height;
46305b261ecSmrg		}
46405b261ecSmrg		else
46505b261ecSmrg		{
46605b261ecSmrg		    slice->max_bot_y = -y - (arc->height & 1);
46705b261ecSmrg		}
46805b261ecSmrg	    }
46905b261ecSmrg	    else
47005b261ecSmrg	    {
47105b261ecSmrg		y = floor(y1);
47205b261ecSmrg		if (y >= 0)
47305b261ecSmrg		    slice->max_top_y = y;
47405b261ecSmrg		else
47505b261ecSmrg		{
47605b261ecSmrg		    slice->min_top_y = arc->height;
47705b261ecSmrg		    slice->min_bot_y = -y - (arc->height & 1);
47805b261ecSmrg		}
47905b261ecSmrg	    }
48005b261ecSmrg	    slice->edge1_top = TRUE;
48105b261ecSmrg	    slice->edge1.x = 65536;
48205b261ecSmrg	    slice->edge1.stepx = 0;
48305b261ecSmrg	    slice->edge1.e = 0;
48405b261ecSmrg	    slice->edge1.dx = -1;
48505b261ecSmrg	    slice->edge2 = slice->edge1;
48605b261ecSmrg	    slice->edge2_top = FALSE;
48705b261ecSmrg	}
48805b261ecSmrg	else if (!slice->edge1.dx)
48905b261ecSmrg	{
49005b261ecSmrg	    if (signdy < 0)
49105b261ecSmrg		x1 -= 1.0;
49205b261ecSmrg	    slice->edge1.x = ceil(x1);
49305b261ecSmrg	    slice->edge1_top = signdy < 0;
49405b261ecSmrg	    slice->edge1.x += arc->x + (arc->width >> 1);
49505b261ecSmrg	    slice->edge1.stepx = 0;
49605b261ecSmrg	    slice->edge1.e = 0;
49705b261ecSmrg	    slice->edge1.dx = -1;
49805b261ecSmrg	    slice->edge2_top = !slice->edge1_top;
49905b261ecSmrg	    slice->edge2 = slice->edge1;
50005b261ecSmrg	}
50105b261ecSmrg	else
50205b261ecSmrg	{
50305b261ecSmrg	    if (signdx < 0)
50405b261ecSmrg		slice->edge1.dx = -slice->edge1.dx;
50505b261ecSmrg	    if (signdy < 0)
50605b261ecSmrg		slice->edge1.dx = -slice->edge1.dx;
50705b261ecSmrg	    k = ceil(((x1 + x2) * slice->edge1.dy - (y1 + y2) * slice->edge1.dx) / 2.0);
50805b261ecSmrg	    slice->edge2.dx = slice->edge1.dx;
50905b261ecSmrg	    slice->edge2.dy = slice->edge1.dy;
51005b261ecSmrg	    slice->edge1_top = signdy < 0;
51105b261ecSmrg	    slice->edge2_top = !slice->edge1_top;
51205b261ecSmrg	    miGetArcEdge(arc, &slice->edge1, k,
51305b261ecSmrg			 slice->edge1_top, !slice->edge1_top);
51405b261ecSmrg	    miGetArcEdge(arc, &slice->edge2, k,
51505b261ecSmrg			 slice->edge2_top, slice->edge2_top);
51605b261ecSmrg	}
51705b261ecSmrg    }
51805b261ecSmrg}
51905b261ecSmrg
52005b261ecSmrg#define ADDSPANS() \
52105b261ecSmrg    pts->x = xorg - x; \
52205b261ecSmrg    pts->y = yorg - y; \
52305b261ecSmrg    *wids = slw; \
52405b261ecSmrg    pts++; \
52505b261ecSmrg    wids++; \
52605b261ecSmrg    if (miFillArcLower(slw)) \
52705b261ecSmrg    { \
52805b261ecSmrg	pts->x = xorg - x; \
52905b261ecSmrg	pts->y = yorg + y + dy; \
53005b261ecSmrg	pts++; \
53105b261ecSmrg	*wids++ = slw; \
53205b261ecSmrg    }
53305b261ecSmrg
53405b261ecSmrgstatic void
53505b261ecSmrgmiFillEllipseI(
53605b261ecSmrg	       DrawablePtr pDraw,
53705b261ecSmrg	       GCPtr pGC,
53805b261ecSmrg	       xArc *arc )
53905b261ecSmrg{
54005b261ecSmrg    int x, y, e;
54105b261ecSmrg    int yk, xk, ym, xm, dx, dy, xorg, yorg;
54205b261ecSmrg    int slw;
54305b261ecSmrg    miFillArcRec info;
54405b261ecSmrg    DDXPointPtr points;
54505b261ecSmrg    DDXPointPtr pts;
54605b261ecSmrg    int *widths;
54705b261ecSmrg    int *wids;
54805b261ecSmrg
5496747b715Smrg    points = malloc(sizeof(DDXPointRec) * arc->height);
55005b261ecSmrg    if (!points)
55105b261ecSmrg	return;
5526747b715Smrg    widths = malloc(sizeof(int) * arc->height);
55305b261ecSmrg    if (!widths)
55405b261ecSmrg    {
5556747b715Smrg	free(points);
55605b261ecSmrg	return;
55705b261ecSmrg    }
55805b261ecSmrg    miFillArcSetup(arc, &info);
55905b261ecSmrg    MIFILLARCSETUP();
56005b261ecSmrg    if (pGC->miTranslate)
56105b261ecSmrg    {
56205b261ecSmrg	xorg += pDraw->x;
56305b261ecSmrg	yorg += pDraw->y;
56405b261ecSmrg    }
56505b261ecSmrg    pts = points;
56605b261ecSmrg    wids = widths;
56705b261ecSmrg    while (y > 0)
56805b261ecSmrg    {
56905b261ecSmrg	MIFILLARCSTEP(slw);
57005b261ecSmrg	ADDSPANS();
57105b261ecSmrg    }
57205b261ecSmrg    (*pGC->ops->FillSpans)(pDraw, pGC, pts - points, points, widths, FALSE);
5736747b715Smrg    free(widths);
5746747b715Smrg    free(points);
57505b261ecSmrg}
57605b261ecSmrg
57705b261ecSmrgstatic void
57805b261ecSmrgmiFillEllipseD(
57905b261ecSmrg	       DrawablePtr pDraw,
58005b261ecSmrg	       GCPtr pGC,
58105b261ecSmrg	       xArc *arc )
58205b261ecSmrg{
58305b261ecSmrg    int x, y;
58405b261ecSmrg    int xorg, yorg, dx, dy, slw;
58505b261ecSmrg    double e, yk, xk, ym, xm;
58605b261ecSmrg    miFillArcDRec info;
58705b261ecSmrg    DDXPointPtr points;
58805b261ecSmrg    DDXPointPtr pts;
58905b261ecSmrg    int *widths;
59005b261ecSmrg    int *wids;
59105b261ecSmrg
5926747b715Smrg    points = malloc(sizeof(DDXPointRec) * arc->height);
59305b261ecSmrg    if (!points)
59405b261ecSmrg	return;
5956747b715Smrg    widths = malloc(sizeof(int) * arc->height);
59605b261ecSmrg    if (!widths)
59705b261ecSmrg    {
5986747b715Smrg	free(points);
59905b261ecSmrg	return;
60005b261ecSmrg    }
60105b261ecSmrg    miFillArcDSetup(arc, &info);
60205b261ecSmrg    MIFILLARCSETUP();
60305b261ecSmrg    if (pGC->miTranslate)
60405b261ecSmrg    {
60505b261ecSmrg	xorg += pDraw->x;
60605b261ecSmrg	yorg += pDraw->y;
60705b261ecSmrg    }
60805b261ecSmrg    pts = points;
60905b261ecSmrg    wids = widths;
61005b261ecSmrg    while (y > 0)
61105b261ecSmrg    {
61205b261ecSmrg	MIFILLARCSTEP(slw);
61305b261ecSmrg	ADDSPANS();
61405b261ecSmrg    }
61505b261ecSmrg    (*pGC->ops->FillSpans)(pDraw, pGC, pts - points, points, widths, FALSE);
6166747b715Smrg    free(widths);
6176747b715Smrg    free(points);
61805b261ecSmrg}
61905b261ecSmrg
62005b261ecSmrg#define ADDSPAN(l,r) \
62105b261ecSmrg    if (r >= l) \
62205b261ecSmrg    { \
62305b261ecSmrg	pts->x = l; \
62405b261ecSmrg	pts->y = ya; \
62505b261ecSmrg	pts++; \
62605b261ecSmrg	*wids++ = r - l + 1; \
62705b261ecSmrg    }
62805b261ecSmrg
62905b261ecSmrg#define ADDSLICESPANS(flip) \
63005b261ecSmrg    if (!flip) \
63105b261ecSmrg    { \
63205b261ecSmrg	ADDSPAN(xl, xr); \
63305b261ecSmrg    } \
63405b261ecSmrg    else \
63505b261ecSmrg    { \
63605b261ecSmrg	xc = xorg - x; \
63705b261ecSmrg	ADDSPAN(xc, xr); \
63805b261ecSmrg	xc += slw - 1; \
63905b261ecSmrg	ADDSPAN(xl, xc); \
64005b261ecSmrg    }
64105b261ecSmrg
64205b261ecSmrgstatic void
64305b261ecSmrgmiFillArcSliceI(
64405b261ecSmrg		DrawablePtr pDraw,
64505b261ecSmrg		GCPtr pGC,
64605b261ecSmrg		xArc *arc )
64705b261ecSmrg{
64805b261ecSmrg    int yk, xk, ym, xm, dx, dy, xorg, yorg, slw;
64905b261ecSmrg    int x, y, e;
65005b261ecSmrg    miFillArcRec info;
65105b261ecSmrg    miArcSliceRec slice;
65205b261ecSmrg    int ya, xl, xr, xc;
65305b261ecSmrg    DDXPointPtr points;
65405b261ecSmrg    DDXPointPtr pts;
65505b261ecSmrg    int *widths;
65605b261ecSmrg    int *wids;
65705b261ecSmrg
65805b261ecSmrg    miFillArcSetup(arc, &info);
65905b261ecSmrg    miFillArcSliceSetup(arc, &slice, pGC);
66005b261ecSmrg    MIFILLARCSETUP();
66105b261ecSmrg    slw = arc->height;
66205b261ecSmrg    if (slice.flip_top || slice.flip_bot)
66305b261ecSmrg	slw += (arc->height >> 1) + 1;
6646747b715Smrg    points = malloc(sizeof(DDXPointRec) * slw);
66505b261ecSmrg    if (!points)
66605b261ecSmrg	return;
6676747b715Smrg    widths = malloc(sizeof(int) * slw);
66805b261ecSmrg    if (!widths)
66905b261ecSmrg    {
6706747b715Smrg	free(points);
67105b261ecSmrg	return;
67205b261ecSmrg    }
67305b261ecSmrg    if (pGC->miTranslate)
67405b261ecSmrg    {
67505b261ecSmrg	xorg += pDraw->x;
67605b261ecSmrg	yorg += pDraw->y;
67705b261ecSmrg	slice.edge1.x += pDraw->x;
67805b261ecSmrg	slice.edge2.x += pDraw->x;
67905b261ecSmrg    }
68005b261ecSmrg    pts = points;
68105b261ecSmrg    wids = widths;
68205b261ecSmrg    while (y > 0)
68305b261ecSmrg    {
68405b261ecSmrg	MIFILLARCSTEP(slw);
68505b261ecSmrg	MIARCSLICESTEP(slice.edge1);
68605b261ecSmrg	MIARCSLICESTEP(slice.edge2);
68705b261ecSmrg	if (miFillSliceUpper(slice))
68805b261ecSmrg	{
68905b261ecSmrg	    ya = yorg - y;
69005b261ecSmrg	    MIARCSLICEUPPER(xl, xr, slice, slw);
69105b261ecSmrg	    ADDSLICESPANS(slice.flip_top);
69205b261ecSmrg	}
69305b261ecSmrg	if (miFillSliceLower(slice))
69405b261ecSmrg	{
69505b261ecSmrg	    ya = yorg + y + dy;
69605b261ecSmrg	    MIARCSLICELOWER(xl, xr, slice, slw);
69705b261ecSmrg	    ADDSLICESPANS(slice.flip_bot);
69805b261ecSmrg	}
69905b261ecSmrg    }
70005b261ecSmrg    (*pGC->ops->FillSpans)(pDraw, pGC, pts - points, points, widths, FALSE);
7016747b715Smrg    free(widths);
7026747b715Smrg    free(points);
70305b261ecSmrg}
70405b261ecSmrg
70505b261ecSmrgstatic void
70605b261ecSmrgmiFillArcSliceD(
70705b261ecSmrg		DrawablePtr pDraw,
70805b261ecSmrg		GCPtr pGC,
70905b261ecSmrg		xArc *arc )
71005b261ecSmrg{
71105b261ecSmrg    int x, y;
71205b261ecSmrg    int dx, dy, xorg, yorg, slw;
71305b261ecSmrg    double e, yk, xk, ym, xm;
71405b261ecSmrg    miFillArcDRec info;
71505b261ecSmrg    miArcSliceRec slice;
71605b261ecSmrg    int ya, xl, xr, xc;
71705b261ecSmrg    DDXPointPtr points;
71805b261ecSmrg    DDXPointPtr pts;
71905b261ecSmrg    int *widths;
72005b261ecSmrg    int *wids;
72105b261ecSmrg
72205b261ecSmrg    miFillArcDSetup(arc, &info);
72305b261ecSmrg    miFillArcSliceSetup(arc, &slice, pGC);
72405b261ecSmrg    MIFILLARCSETUP();
72505b261ecSmrg    slw = arc->height;
72605b261ecSmrg    if (slice.flip_top || slice.flip_bot)
72705b261ecSmrg	slw += (arc->height >> 1) + 1;
7286747b715Smrg    points = malloc(sizeof(DDXPointRec) * slw);
72905b261ecSmrg    if (!points)
73005b261ecSmrg	return;
7316747b715Smrg    widths = malloc(sizeof(int) * slw);
73205b261ecSmrg    if (!widths)
73305b261ecSmrg    {
7346747b715Smrg	free(points);
73505b261ecSmrg	return;
73605b261ecSmrg    }
73705b261ecSmrg    if (pGC->miTranslate)
73805b261ecSmrg    {
73905b261ecSmrg	xorg += pDraw->x;
74005b261ecSmrg	yorg += pDraw->y;
74105b261ecSmrg	slice.edge1.x += pDraw->x;
74205b261ecSmrg	slice.edge2.x += pDraw->x;
74305b261ecSmrg    }
74405b261ecSmrg    pts = points;
74505b261ecSmrg    wids = widths;
74605b261ecSmrg    while (y > 0)
74705b261ecSmrg    {
74805b261ecSmrg	MIFILLARCSTEP(slw);
74905b261ecSmrg	MIARCSLICESTEP(slice.edge1);
75005b261ecSmrg	MIARCSLICESTEP(slice.edge2);
75105b261ecSmrg	if (miFillSliceUpper(slice))
75205b261ecSmrg	{
75305b261ecSmrg	    ya = yorg - y;
75405b261ecSmrg	    MIARCSLICEUPPER(xl, xr, slice, slw);
75505b261ecSmrg	    ADDSLICESPANS(slice.flip_top);
75605b261ecSmrg	}
75705b261ecSmrg	if (miFillSliceLower(slice))
75805b261ecSmrg	{
75905b261ecSmrg	    ya = yorg + y + dy;
76005b261ecSmrg	    MIARCSLICELOWER(xl, xr, slice, slw);
76105b261ecSmrg	    ADDSLICESPANS(slice.flip_bot);
76205b261ecSmrg	}
76305b261ecSmrg    }
76405b261ecSmrg    (*pGC->ops->FillSpans)(pDraw, pGC, pts - points, points, widths, FALSE);
7656747b715Smrg    free(widths);
7666747b715Smrg    free(points);
76705b261ecSmrg}
76805b261ecSmrg
76905b261ecSmrg/* MIPOLYFILLARC -- The public entry for the PolyFillArc request.
77005b261ecSmrg * Since we don't have to worry about overlapping segments, we can just
77105b261ecSmrg * fill each arc as it comes.
77205b261ecSmrg */
7736747b715Smrgvoid
7744642e01fSmrgmiPolyFillArc(DrawablePtr pDraw, GCPtr pGC, int narcs, xArc *parcs)
77505b261ecSmrg{
77605b261ecSmrg    int i;
77705b261ecSmrg    xArc *arc;
77805b261ecSmrg
77905b261ecSmrg    for(i = narcs, arc = parcs; --i >= 0; arc++)
78005b261ecSmrg    {
78105b261ecSmrg	if (miFillArcEmpty(arc))
7824642e01fSmrg	    continue;
78305b261ecSmrg	if ((arc->angle2 >= FULLCIRCLE) || (arc->angle2 <= -FULLCIRCLE))
78405b261ecSmrg	{
78505b261ecSmrg	    if (miCanFillArc(arc))
78605b261ecSmrg		miFillEllipseI(pDraw, pGC, arc);
78705b261ecSmrg	    else
78805b261ecSmrg		miFillEllipseD(pDraw, pGC, arc);
78905b261ecSmrg	}
79005b261ecSmrg	else
79105b261ecSmrg	{
79205b261ecSmrg	    if (miCanFillArc(arc))
79305b261ecSmrg		miFillArcSliceI(pDraw, pGC, arc);
79405b261ecSmrg	    else
79505b261ecSmrg		miFillArcSliceD(pDraw, pGC, arc);
79605b261ecSmrg	}
79705b261ecSmrg    }
79805b261ecSmrg}
799