/***********************************************************
Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
and the Massachusetts Institute of Technology, Cambridge, Massachusetts.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in 
supporting documentation, and that the names of Digital or MIT not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.  

DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

******************************************************************/
/* $Header: mfbgc.c,v 1.104 87/06/15 15:46:13 drewry Exp $ */
#include "X.h"
#include "Xmd.h"
#include "Xproto.h"
#include "dixfontstr.h"
#include "fontstruct.h"
#include "gcstruct.h"
#include "windowstr.h"
#include "pixmapstr.h"
#include "scrnintstr.h"
#include "region.h"

#include "mfb.h"
#include "mistruct.h"
#include "mi.h"
#include "os.h"
#include "misc.h"
#include "opaque.h"

#include "maskbits.h"
extern XRotatePixmap();

static PixmapPtr BogusPixmap = (PixmapPtr)1;

Bool
mfbCreateGC(pGC)
    register GCPtr pGC;
{
    mfbPrivGC 	*pPriv;
    GCInterestPtr	pQ;

    pGC->clientClip = NULL;
    pGC->clientClipType = CT←NONE;
    
    /* some of the output primitives aren't really necessary, since
       they will be filled in ValidateGC because of dix/CreateGC()
       setting all the change bits.  Others are necessary because although
       they depend on being a monochrome frame buffer, they don't change 
    */

    pGC->FillSpans = mfbWhiteSolidFS;
    pGC->SetSpans = mfbSetSpans;
    pGC->PutImage = mfbPutImage;
    pGC->CopyArea = mfbCopyArea;
    pGC->CopyPlane = mfbCopyPlane;
    pGC->PolyPoint = mfbPolyPoint;

    pGC->Polylines = mfbLineSS;
    pGC->PolySegment = miPolySegment;
    pGC->PolyRectangle = miPolyRectangle;
    pGC->PolyArc = miPolyArc;
    pGC->FillPolygon = miFillPolygon;
    pGC->PolyFillRect = mfbPolyFillRect;
    pGC->PolyFillArc = miPolyFillArc;
    pGC->PolyText8 = miPolyText8;
    pGC->ImageText8 = miImageText8;
    pGC->PolyText16 = miPolyText16;
    pGC->ImageText16 = miImageText16;
    pGC->ImageGlyphBlt = mfbImageGlyphBltWhite;
    pGC->PolyGlyphBlt = mfbPolyGlyphBltInvert;
    pGC->PushPixels = mfbPushPixels;
    pGC->LineHelper = miMiter;
    pGC->ChangeClip = mfbChangeClip;
    pGC->DestroyClip = mfbDestroyClip;

    /* mfb wants to translate before scan convesion */
    pGC->miTranslate = 1;

    pPriv = (mfbPrivGC *)Xalloc(sizeof(mfbPrivGC));
    if (!pPriv)
	return FALSE;
    else
    {
	pPriv->rop = ReduceRop(pGC->alu, pGC->fgPixel);
	pPriv->fExpose = TRUE;
	pGC->devPriv = (pointer)pPriv;
	pPriv->pRotatedTile = NullPixmap;
	pPriv->pRotatedStipple = NullPixmap;
	pPriv->pAbsClientRegion =(* pGC->pScreen->RegionCreate)((BoxRec *)NULL, 1); 
	pPriv->pCompositeClip = (RegionPtr)NULL;
	pPriv->freeCompClip = REPLACE←CC;
	pPriv->ppPixmap = &BogusPixmap;
	pPriv->FillArea = mfbSolidInvertArea;
    }
    pQ = (GCInterestPtr) Xalloc(sizeof(GCInterestRec));
    if(!pQ)
    {
	Xfree(pPriv);
	return FALSE;
    }
     
    /* Now link this device into the GCque */
    pGC->pNextGCInterest = pQ;
    pGC->pLastGCInterest = pQ;
    pQ->pNextGCInterest = (GCInterestPtr) &pGC->pNextGCInterest;
    pQ->pLastGCInterest = (GCInterestPtr) &pGC->pNextGCInterest;
    pQ->length = sizeof(GCInterestRec);
    pQ->owner = 0;		/* server owns this */
    pQ->ValInterestMask = ~0;	/* interested in everything at validate time */
    pQ->ValidateGC = mfbValidateGC;
    pQ->ChangeInterestMask = 0; /* interested in nothing at change time */
    pQ->ChangeGC = (int (*) () ) NULL;
    pQ->CopyGCSource = (void (*) () ) NULL;
    pQ->CopyGCDest = mfbCopyGCDest;
    pQ->DestroyGC = mfbDestroyGC;
    return TRUE;
}

void
mfbDestroyGC(pGC, pQ)
    GC 			*pGC;
    GCInterestPtr	pQ;

{
    mfbPrivGC *pPriv;

    /* Most GCInterest pointers would free pQ->devPriv.  This one is privileged
     * and allowed to allocate its private data directly in the GC (this
     * saves an indirection).  We must also unlink and free the pQ.
     */
    pQ->pLastGCInterest->pNextGCInterest = pQ->pNextGCInterest;
    pQ->pNextGCInterest->pLastGCInterest = pQ->pLastGCInterest;

    pPriv = (mfbPrivGC *)(pGC->devPriv);
    if (pPriv->pRotatedTile)
	mfbDestroyPixmap(pPriv->pRotatedTile);
    if (pPriv->pRotatedStipple)
	mfbDestroyPixmap(pPriv->pRotatedStipple);
    if (pPriv->freeCompClip == FREE←CC && pPriv->pCompositeClip)
	(*pGC->pScreen->RegionDestroy)(pPriv->pCompositeClip);
    if(pPriv->pAbsClientRegion)
	(*pGC->pScreen->RegionDestroy)(pPriv->pAbsClientRegion);
    Xfree(pGC->devPriv);
    Xfree(pQ);
}

#define WINMOVED(pWin, pGC) \
((pWin->absCorner.x != pGC->lastWinOrg.x) || \
 (pWin->absCorner.y != pGC->lastWinOrg.y))

/* Clipping conventions
	if the drawable is a window
	    CT←REGION ==> pCompositeClip really is the composite
	    CT←other ==> pCompositeClip is the window clip region
	if the drawable is a pixmap
	    CT←REGION ==> pCompositeClip is the translated client region
		clipped to the pixmap boundary
	    CT←other ==> pCompositeClip is the pixmap bounding box
*/

void
mfbValidateGC(pGC, pQ, changes, pDrawable)
    register GCPtr 	pGC;
    GCInterestPtr	pQ;
    Mask 		changes;
    DrawablePtr 	pDrawable;
{
    register mfbPrivGCPtr	devPriv;
    WindowPtr pWin;
    long mask;			/* stateChanges */
    long index;			/* used for stepping through bitfields */
    int	xrot, yrot;		/* rotations for tile and stipple pattern */
    Bool fRotate = FALSE;	/* True if rotated pixmaps are needed */
    int rrop;			/* reduced rasterop */
				/* flags for changing the proc vector */
    int new←rrop,  new←line, new←text, new←fill;

    if (pDrawable->type == DRAWABLE←WINDOW)
	pWin = (WindowPtr)pDrawable;
    else
	pWin = (WindowPtr)NULL;

    devPriv = ((mfbPrivGCPtr) (pGC->devPriv));
    /*
	if the client clip is different or moved OR
	the subwindowMode has changed OR
	the window's clip has changed since the last validation
	we need to recompute the composite clip
    */

    if ((changes & (GCClipXOrigin|GCClipYOrigin|GCClipMask)) ||
	(changes & GCSubwindowMode) ||
	(pDrawable->serialNumber != (pGC->serialNumber & DRAWABLE←SERIAL←BITS))
       )
    {

        /* if there is a client clip (always a region, for us) AND
	        it has moved or is different OR
	        the window has moved
           we need to (re)translate it.
        */
	if ((pGC->clientClipType == CT←REGION) &&
	    ((changes & (GCClipXOrigin|GCClipYOrigin|GCClipMask)) ||
	     (pWin && WINMOVED(pWin, pGC))
	    )
	   )
	{
	    /* retranslate client clip */
	    (* pGC->pScreen->RegionCopy)( devPriv->pAbsClientRegion, 
			                  pGC->clientClip);

	    if (pWin)
	    {
		pGC->lastWinOrg.x = pWin->absCorner.x;
		pGC->lastWinOrg.y = pWin->absCorner.y;
		(* pGC->pScreen->TranslateRegion)(
	                       devPriv->pAbsClientRegion, 
			       pGC->lastWinOrg.x + pGC->clipOrg.x,
			       pGC->lastWinOrg.y + pGC->clipOrg.y);
	    }
	    else
	    {
		pGC->lastWinOrg.x = 0;
		pGC->lastWinOrg.y = 0;
		(* pGC->pScreen->TranslateRegion)(
	            devPriv->pAbsClientRegion, pGC->clipOrg.x, pGC->clipOrg.y);
	    }
	}

	if (pWin)
	{
	    RegionPtr pregWin;
	    int freeTmpClip, freeCompClip;

	    if (pGC->subWindowMode == IncludeInferiors)
	    {
	        pregWin = NotClippedByChildren(pWin);
		freeTmpClip = FREE←CC;
	    }
	    else
	    {
	        pregWin = pWin->clipList;
		freeTmpClip = REPLACE←CC;
	    }
	    freeCompClip = devPriv->freeCompClip;

	    /* if there is no client clip, we can get by with
	       just keeping the pointer we got, and remembering
	       whether or not should destroy (or maybe re-use)
	       it later.  this way, we avoid unnecessary copying
	       of regions.  (this wins especially if many clients clip
	       by children and have no client clip.)
	    */
	    if (pGC->clientClipType == CT←NONE)
	    {
	        if(freeCompClip == FREE←CC) 
		{
		    (* pGC->pScreen->RegionDestroy) (devPriv->pCompositeClip);
		}
	        devPriv->pCompositeClip = pregWin;
	        devPriv->freeCompClip = freeTmpClip;
	    }
	    else
	    {
		/* we need one 'real' region to put into the composite
		   clip.
			if pregWin the current composite clip 
		   are real, we can get rid of one.
			if pregWin is real and the current composite
		   clip isn't, use pregWin for the composite clip.
			if the current composite clip is real and
		   pregWin isn't, use the current composite clip.
			if neither is real, create a new region.
		*/

		if ((freeTmpClip == FREE←CC) && (freeCompClip == FREE←CC))
		{
		    (* pGC->pScreen->Intersect)(
		        devPriv->pCompositeClip,
			pregWin,
			devPriv->pAbsClientRegion);
		    (* pGC->pScreen->RegionDestroy)(pregWin);
		}
		else if ((freeTmpClip == REPLACE←CC) && 
		        (freeCompClip == FREE←CC))
		{
		    devPriv->pCompositeClip = pregWin;
		    (* pGC->pScreen->Intersect)(
			devPriv->pCompositeClip,
		        devPriv->pCompositeClip,
			devPriv->pAbsClientRegion);
		}
		else if ((freeTmpClip == FREE←CC) &&
		         (freeCompClip == REPLACE←CC))
		{
		    (* pGC->pScreen->Intersect)(
		       devPriv->pCompositeClip,
		       pregWin,
		       devPriv->pAbsClientRegion);
		}
		else if ((freeTmpClip == REPLACE←CC) &&
		         (freeCompClip == REPLACE←CC))
		{
		    devPriv->pCompositeClip = 
			(* pGC->pScreen->RegionCreate)((BoxPtr)NULL, 1);
		    (* pGC->pScreen->Intersect)(
			devPriv->pCompositeClip,
		        pregWin,
			devPriv->pAbsClientRegion);
		}
	    }
	} /* end of composite clip for a window */
	else
	{
	    BoxRec pixbounds;

	    pixbounds.x1 = 0;
	    pixbounds.y1 = 0;
	    pixbounds.x2 = ((PixmapPtr)pDrawable)->width;
	    pixbounds.y2 = ((PixmapPtr)pDrawable)->height;

	    if (devPriv->freeCompClip == FREE←CC)
	        (* pGC->pScreen->RegionReset)(
		    devPriv->pCompositeClip, &pixbounds);
	    else
	    {
		devPriv->freeCompClip = FREE←CC;
		devPriv->pCompositeClip = 
			(* pGC->pScreen->RegionCreate)(&pixbounds, 1);
	    }

	    if (pGC->clientClipType == CT←REGION)
		(* pGC->pScreen->Intersect)(
		   devPriv->pCompositeClip, 
		   devPriv->pCompositeClip,
		   devPriv->pAbsClientRegion);
	} /* end of composute clip for pixmap */
    }

    if (pWin)
    {

	/* rotate tile patterns so that pattern can be combined in word by
	 * word, but the pattern seems to begin aligned with the window */
	xrot = pWin->absCorner.x;
	yrot = pWin->absCorner.y;
    }
    else
    {
        yrot = 0;
	xrot = 0;
    }

    new←rrop = FALSE;
    new←line = FALSE;
    new←text = FALSE;
    new←fill = FALSE;

    mask = changes;
    while (mask)
    {
	index = ffs(mask) - 1;
	mask &= ~(index = (1 << index));

	/* this switch acculmulates a list of which procedures
	   might have to change due to changes in the GC.  in
	   some cases (e.g. changing one 16 bit tile for another)
	   we might not really need a change, but the code is
	   being paranoid.
	   this sort of batching wins if, for example, the alu
	   and the font have been changed, or any other pair
	   of items that both change the same thing.
	*/
	switch (index)
	{
	  case GCFunction:
	  case GCForeground:
	    new←rrop = TRUE;
	    break;
	  case GCPlaneMask:
	    break;
	  case GCBackground:
	    new←rrop = TRUE;	/* for opaque stipples */
	    break;
	  case GCLineStyle:
	    new←line = TRUE;
	    break;
	  case GCLineWidth:
	  case GCCapStyle:
	  case GCJoinStyle:
	    new←line = TRUE;
	    break;
	  case GCFillStyle:
	    new←fill = TRUE;
	    break;
	  case GCFillRule:
	    break;
	  case GCTile:
	    if(pGC->tile == (PixmapPtr)NULL)
		break;
	    mfbPadPixmap(pGC->tile);
	    fRotate = TRUE;
	    new←fill = TRUE;
	    break;

	  case GCStipple:
	    if(pGC->stipple == (PixmapPtr)NULL)
		break;
	    mfbPadPixmap(pGC->stipple);
	    fRotate = TRUE;
	    new←fill = TRUE;
	    break;

	  case GCTileStipXOrigin:
	    fRotate = TRUE;
	    break;

	  case GCTileStipYOrigin:
	    fRotate = TRUE;
	    break;

	  case GCFont:
	    new←text = TRUE;
	    break;
	  case GCSubwindowMode:
	    break;
	  case GCGraphicsExposures:
	    break;
	  case GCClipXOrigin:
	    break;
	  case GCClipYOrigin:
	    break;
	  case GCClipMask:
	    break;
	  case GCDashOffset:
	    break;
	  case GCDashList:
	    break;
	  case GCArcMode:
	    break;
	  default:
	    break;
	}
    }

    /* deal with the changes we've collected .
       new←rrop must be done first because subsequent things
       depend on it.
    */
    if (new←rrop || new←fill)
    {
	rrop = ReduceRop(pGC->alu, pGC->fgPixel);
	devPriv->rop = rrop;
	new←fill = TRUE;

	/* opaque stipples:
	   fg	bg	ropOpStip	fill style
	   1	0	alu		tile
	   0	1	inverseAlu	tile
	   1	1	rrop(fg, alu)	solid
	   0	0	rrop(fg, alu)	solid
	Note that rrop(fg, alu) == mfbPrivGC.rop, so we don't really need to
	compute it.
	*/
        if (pGC->fillStyle == FillOpaqueStippled)
        {
	    if (pGC->fgPixel != pGC->bgPixel)
	    {
	        if (pGC->fgPixel)
		    devPriv->ropOpStip = pGC->alu;
	        else
		    devPriv->ropOpStip = InverseAlu[pGC->alu];
	    }
	    else
	        devPriv->ropOpStip = rrop;
        }
    }
    else
	rrop = devPriv->rop;

    if (new←line || new←fill)
    {
	if (pGC->lineStyle == LineSolid)
	{
	    if(pGC->lineWidth == 0)
	    {
	        if (pGC->fillStyle == FillSolid)
		    pGC->Polylines = mfbLineSS;
	        else
		    pGC->Polylines = miZeroLine;
	    }
	    else
	    {
		pGC->Polylines = miWideLine;
	    }
	}
	else
	    if(pGC->lineWidth == 0)
	        pGC->Polylines = mfbDashLine;
	    else
	        pGC->Polylines = miWideDash;

	switch(pGC->joinStyle)
	{
	  case JoinMiter:
	    pGC->LineHelper = miMiter;
	    break;
	  case JoinRound:
	  case JoinBevel:
	    pGC->LineHelper = miNotMiter;
	    break;
	}
    }

    if (new←text || new←fill)
    {
	if ((pGC->font) &&
	    (pGC->font->pFI->maxbounds.metrics.rightSideBearing -
	     pGC->font->pFI->maxbounds.metrics.leftSideBearing) > BW)
	{
	    pGC->PolyGlyphBlt = miPolyGlyphBlt;
	    pGC->ImageGlyphBlt = miImageGlyphBlt;
	}
	else
	{
	    if (pGC->fgPixel == 0)
		pGC->ImageGlyphBlt = mfbImageGlyphBltBlack;
	    else
		pGC->ImageGlyphBlt = mfbImageGlyphBltWhite;

	    if (pGC->fillStyle == FillSolid ||
		(pGC->fillStyle == FillOpaqueStippled &&
		 pGC->fgPixel == pGC->bgPixel
		)
	       )
	    {
		if (rrop == RROP←WHITE)
		    pGC->PolyGlyphBlt = mfbPolyGlyphBltWhite;
		else if (rrop == RROP←BLACK)
		    pGC->PolyGlyphBlt = mfbPolyGlyphBltBlack;
		else if (rrop == RROP←INVERT)
		    pGC->PolyGlyphBlt = mfbPolyGlyphBltInvert;
		else
		    pGC->PolyGlyphBlt = NoopDDA;
	    }
	    else
	    {
		pGC->PolyGlyphBlt = miPolyGlyphBlt;
	    }
	}
    }

    if (new←fill)
    {
	/* install a suitable fillspans */
	if ((pGC->fillStyle == FillSolid) ||
	    (pGC->fillStyle == FillOpaqueStippled && pGC->fgPixel==pGC->bgPixel)
	   )
	{
	    switch(devPriv->rop)
	    {
	      case RROP←WHITE:
		pGC->FillSpans = mfbWhiteSolidFS;
		break;
	      case RROP←BLACK:
		pGC->FillSpans = mfbBlackSolidFS;
		break;
	      case RROP←INVERT:
		pGC->FillSpans = mfbInvertSolidFS;
		break;
	      case RROP←NOP:
		pGC->FillSpans = NoopDDA;
		break;
	    }
	}
	/* beyond this point, opaqueStippled ==> fg != bg */
	else if ((pGC->fillStyle==FillTiled && pGC->tile->width!=BW) ||
		 (pGC->fillStyle==FillOpaqueStippled && pGC->stipple->width!=BW)
		)
	{
	    pGC->FillSpans = mfbUnnaturalTileFS;
	}
	else if (pGC->fillStyle == FillStippled && pGC->stipple->width != BW)
	{
	    pGC->FillSpans = mfbUnnaturalStippleFS;
	}
	else if (pGC->fillStyle == FillStippled)
	{
	    switch(devPriv->rop)
	    {
	      case RROP←WHITE:
		pGC->FillSpans = mfbWhiteStippleFS;
		break;
	      case RROP←BLACK:
		pGC->FillSpans = mfbBlackStippleFS;
		break;
	      case RROP←INVERT:
		pGC->FillSpans = mfbInvertStippleFS;
		break;
	      case RROP←NOP:
		pGC->FillSpans = NoopDDA;
		break;
	    }
	}
	else /* overload tiles to do parti-colored opaque stipples */
	{
	    pGC->FillSpans = mfbTileFS;
	}

	/* the rectangle code doesn't deal with opaque stipples that
	   are two colors -- we can fool it for fg==bg, though
	 */
	if (((pGC->fillStyle == FillTiled) && (pGC->tile->width!=BW)) ||
	    ((pGC->fillStyle == FillStippled) && (pGC->stipple->width!=BW)) ||
	    ((pGC->fillStyle == FillOpaqueStippled) &&
	     (pGC->fgPixel != pGC->bgPixel))
	   )
	{
	    pGC->PolyFillRect = miPolyFillRect;
	    devPriv->ppPixmap = &BogusPixmap;
	}
	else /* deal with solids and natural stipples and tiles */
	{
	    pGC->PolyFillRect = mfbPolyFillRect;

	    if ((pGC->fillStyle == FillSolid) ||
		(pGC->fillStyle == FillOpaqueStippled &&
		 pGC->fgPixel == pGC->bgPixel)
	       )
	    {
	        devPriv->ppPixmap = &BogusPixmap;
		switch(devPriv->rop)
		{
		  case RROP←WHITE:
		    devPriv->FillArea = mfbSolidWhiteArea;
		    break;
		  case RROP←BLACK:
		    devPriv->FillArea = mfbSolidBlackArea;
		    break;
		  case RROP←INVERT:
		    devPriv->FillArea = mfbSolidInvertArea;
		    break;
		  case RROP←NOP:
		    devPriv->FillArea = NoopDDA;
		    break;
		}
	    }
	    else if (pGC->fillStyle == FillStippled)
	    {
		devPriv->ppPixmap = &devPriv->pRotatedStipple;
		switch(devPriv->rop)
		{
		  case RROP←WHITE:
		    devPriv->FillArea = mfbStippleWhiteArea;
		    break;
		  case RROP←BLACK:
		    devPriv->FillArea = mfbStippleBlackArea;
		    break;
		  case RROP←INVERT:
		    devPriv->FillArea = mfbStippleInvertArea;
		    break;
		  case RROP←NOP:
		    devPriv->FillArea = NoopDDA;
		    break;
		}
	    }
	    else /* deal with tiles */
	    {
		if (pGC->fillStyle == FillTiled)
		    devPriv->ppPixmap = &devPriv->pRotatedTile;
		else
		    devPriv->ppPixmap = &devPriv->pRotatedStipple;
		devPriv->FillArea = mfbTileArea32;
	    }
	} /* end of natural rectangles */
    } /* end of new←fill */


    if(fRotate)
    {
        xrot += pGC->patOrg.x;
        yrot += pGC->patOrg.y;
    }
    if(xrot || yrot || fRotate)
    {

	if(devPriv->pRotatedTile)
	{
	    mfbDestroyPixmap(devPriv->pRotatedTile);
	    devPriv->pRotatedTile = (PixmapPtr)NULL;
	}
	if(devPriv->pRotatedStipple)
	{
	    mfbDestroyPixmap(devPriv->pRotatedStipple);
	    devPriv->pRotatedStipple = (PixmapPtr)NULL;
	}
        if(pGC->tile &&
	   (pGC->tile->width == BW) &&
	   (devPriv->pRotatedTile = mfbCopyPixmap(pGC->tile)) ==
	       (PixmapPtr)NULL)
	    return ;           /* shouldn't happen, internal error */
        if(pGC->stipple &&
	   (pGC->stipple->width == BW) &&
	   (devPriv->pRotatedStipple = mfbCopyPixmap(pGC->stipple)) ==
	       (PixmapPtr)NULL)
	    return ;          /* shouldn't happen, internal error */
    }
    if(xrot)
    {
	if (pGC->tile && pGC->tile->width == BW && 
	    devPriv->pRotatedTile)
	    XRotatePixmap(devPriv->pRotatedTile, xrot); 
	if (pGC->stipple && pGC->stipple->width == BW && 
	    devPriv->pRotatedStipple)
	    XRotatePixmap(devPriv->pRotatedStipple, xrot); 
    }
    if(yrot)
    {
	if (pGC->tile && pGC->tile->width == BW && 
	    devPriv->pRotatedTile)
	    YRotatePixmap(devPriv->pRotatedTile, yrot); 
	if (pGC->tile && pGC->tile->width == BW && 
	    devPriv->pRotatedStipple)
	    YRotatePixmap(devPriv->pRotatedStipple, yrot); 
    }

    return ;
}

/* table to map alu(src, dst) to alu(~src, dst) */
int InverseAlu[16] = {
	GXclear,
	GXandInverted,
	GXnor,
	GXcopyInverted,
	GXand,
	GXnoop,
	GXequiv,
	GXorInverted,
	GXandReverse,
	GXxor,
	GXinvert,
	GXnand,
	GXcopy,
	GXor,
	GXorReverse,
	GXclear
	};

ReduceRop(alu, src)
    register int alu;
    register unsigned long src;
{
    int rop;
    if (src == 0)	/* src is black */
    {
	switch(alu)
	{
	  case GXclear:
	    rop = RROP←BLACK;
	    break;
	  case GXand:
	    rop = RROP←BLACK;
	    break;
	  case GXandReverse:
	    rop = RROP←BLACK;
	    break;
	  case GXcopy:
	    rop = RROP←BLACK;
	    break;
	  case GXandInverted:
	    rop = RROP←NOP;
	    break;
	  case GXnoop:
	    rop = RROP←NOP;
	    break;
	  case GXxor:
	    rop = RROP←NOP;
	    break;
	  case GXor:
	    rop = RROP←NOP;
	    break;
	  case GXnor:
	    rop = RROP←INVERT;
	    break;
	  case GXequiv:
	    rop = RROP←INVERT;
	    break;
	  case GXinvert:
	    rop = RROP←INVERT;
	    break;
	  case GXorReverse:
	    rop = RROP←INVERT;
	    break;
	  case GXcopyInverted:
	    rop = RROP←WHITE;
	    break;
	  case GXorInverted:
	    rop = RROP←WHITE;
	    break;
	  case GXnand:
	    rop = RROP←WHITE;
	    break;
	  case GXset:
	    rop = RROP←WHITE;
	    break;
	}
    }
    else /* src is white */
    {
	switch(alu)
	{
	  case GXclear:
	    rop = RROP←BLACK;
	    break;
	  case GXand:
	    rop = RROP←NOP;
	    break;
	  case GXandReverse:
	    rop = RROP←INVERT;
	    break;
	  case GXcopy:
	    rop = RROP←WHITE;
	    break;
	  case GXandInverted:
	    rop = RROP←BLACK;
	    break;
	  case GXnoop:
	    rop = RROP←NOP;
	    break;
	  case GXxor:
	    rop = RROP←INVERT;
	    break;
	  case GXor:
	    rop = RROP←WHITE;
	    break;
	  case GXnor:
	    rop = RROP←BLACK;
	    break;
	  case GXequiv:
	    rop = RROP←NOP;
	    break;
	  case GXinvert:
	    rop = RROP←INVERT;
	    break;
	  case GXorReverse:
	    rop = RROP←WHITE;
	    break;
	  case GXcopyInverted:
	    rop = RROP←BLACK;
	    break;
	  case GXorInverted:
	    rop = RROP←NOP;
	    break;
	  case GXnand:
	    rop = RROP←INVERT;
	    break;
	  case GXset:
	    rop = RROP←WHITE;
	    break;
	}
    }
    return rop;
}

void
mfbDestroyClip(pGC)
    GCPtr	pGC;
{
    if(pGC->clientClipType == CT←NONE)
	return;
    (*pGC->pScreen->RegionDestroy)(pGC->clientClip);
    pGC->clientClip = NULL;
    pGC->clientClipType = CT←NONE;
}

void
mfbChangeClip(pGC, type, pvalue, nrects)
    GCPtr	pGC;
    int		type;
    pointer	pvalue;
    int		nrects;
{
    mfbDestroyClip(pGC);
    if(type == CT←PIXMAP)
    {
	pGC->clientClip = (pointer) mfbPixmapToRegion((PixmapPtr) pvalue);
	(*pGC->pScreen->DestroyPixmap)(pvalue);
    }
    else if (type == CT←REGION)
	pGC->clientClip = pvalue;
    else if (type != CT←NONE)
    {
	pGC->clientClip = (pointer) miRectsToRegion(pGC, nrects, (xRectangle *) pvalue, type);
	Xfree(pvalue);
    }
    pGC->clientClipType = (pGC->clientClip) ? CT←REGION : CT←NONE;
}

void
mfbCopyGCDest (pGC, pQ, changes, pGCSrc)
    GCPtr	pGC;
    GCInterestPtr	pQ;
    Mask 		changes;
    GCPtr		pGCSrc;
{
    RegionPtr		pClip;

    if(changes & GCClipMask)
    {
	if(pGC->clientClipType == CT←PIXMAP)
	{
	    ((PixmapPtr)pGC->clientClip)->refcnt++;
	}
	else if(pGC->clientClipType == CT←REGION)
	{
	    BoxRec pixbounds;

	    pixbounds.x1 = 0;
	    pixbounds.y1 = 0;
	    pixbounds.x2 = 0;
	    pixbounds.y2 = 0;

	    pClip = (RegionPtr) pGC->clientClip;
	    pGC->clientClip =
	        (pointer)(* pGC->pScreen->RegionCreate)(&pixbounds, 1);
	    (* pGC->pScreen->RegionCopy)(pGC->clientClip, pClip);
	}
    }
}