/************************************************************************
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: glyphcurs.c,v 1.5 87/06/22 21:02:43 toddb Exp $ */

#include "X.h"
#include "Xmd.h"
#include "Xproto.h"
#include "dixfontstr.h"
#include "fontstruct.h"
#include "scrnintstr.h"
#include "gcstruct.h"
#include "resource.h"
#include "dix.h"
#include "cursorstr.h"
#include "misc.h"
#include "opaque.h"


/*
    get the bits out of the font in a portable way.  to avoid
dealing with padding and such-like, we draw the glyph into
a bitmap, then read the bits out with GetImage, which
uses server-natural format.
    since all screens return the same bitmap format, we'll just use
the first one we find.
    if the font isn't there, it's probably been sent from
the mask part of CreateGlyphCursor, so we return a bitmap
filled with 1s.
    the character origin lines up with the hotspot in the
cursor metrics.
*/

int
ServerBitsFromGlyph(fontID, pfont, ch, cm, ppbits)
    XID		fontID;
    FontPtr	pfont;
    unsigned short ch;
    register CursorMetricPtr cm;
    unsigned char **ppbits;
{
    register ScreenPtr pScreen;
    register GCPtr pGC;
    xRectangle rect;
    PixmapPtr ppix;
    int nby;
    unsigned char *pbits;
    long gcval[3];
/* next few lines are temporary.
   calling PolyText16 with the cursor font breaks, alas.
   we call PolyText8 (ok since only 162 cursor glyphs so far),
   and so need a char to take the address of.
   when fixed, change polytext call.
*/
unsigned char chtmp;
chtmp = (unsigned char)ch;


    pScreen = &screenInfo.screen[0];
    nby = PixmapBytePad(cm->width, 1) * cm->height;
    pbits = (unsigned char *)Xalloc(nby);
    if (!pbits)
	return BadAlloc;

    ppix = (PixmapPtr)(*pScreen->CreatePixmap)(pScreen, cm->width,
					       cm->height, 1);
    if (!ppix)
    {
	Xfree(pbits);
	return BadAlloc;
    }
    pGC = GetScratchGC(1, pScreen);

    rect.x = 0;
    rect.y = 0;
    rect.width = cm->width;
    rect.height = cm->height;

    if (!pfont)
    {
	/* fill the pixmap with 1 */
	gcval[0] = GXcopy;
	gcval[1] = 1;
	ChangeGC(pGC, GCFunction | GCForeground, gcval);
	ValidateGC((DrawablePtr)ppix, pGC);
	(*pGC->PolyFillRect)(ppix, pGC, 1, &rect);
    }
    else
    {
	/* fill the pixmap with 0 */
	gcval[0] = GXcopy;
	gcval[1] = 0;
	gcval[2] = fontID;
	ChangeGC(pGC, GCFunction | GCForeground | GCFont, gcval);
	ValidateGC((DrawablePtr)ppix, pGC);
	(*pGC->PolyFillRect)(ppix, pGC, 1, &rect);

	/* draw the glyph */
	gcval[0] = 1;
	ChangeGC(pGC, GCForeground, gcval);
	ValidateGC((DrawablePtr)ppix, pGC);
	(*pGC->PolyText8)(ppix, pGC, cm->xhot, cm->yhot, 1, &chtmp);
    }
    (*pScreen->GetImage)(ppix, 0, 0, cm->width, cm->height,
			 ZPixmap, 0xffff, (int *)pbits);
    *ppbits = pbits;
    FreeScratchGC(pGC);
    (*pScreen->DestroyPixmap)(ppix);
    return Success;
}


Bool
CursorMetricsFromGlyph( pfont, ch, cm)
    register FontPtr 	pfont;
    int		ch;
    register CursorMetricPtr cm;
{
    register CharInfoPtr 	pci = ADDRXTHISCHARINFO(pfont, ch);

    if (   ch < pfont->pFI->chFirst
	|| ch >= pfont->pFI->chFirst + n1dChars(pfont->pFI))
    {
	cm->width = 0;
	cm->height = 0;
	cm->xhot = 0;
	cm->yhot = 0;
	return FALSE;
    }
    cm->xhot = - pci->metrics.leftSideBearing;
    cm->yhot =   pci->metrics.ascent;
    cm->width = pci->metrics.rightSideBearing + cm->xhot;
    cm->height = pci->metrics.descent + cm->yhot;

    return TRUE;
}