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
48706f2543Smrg
49706f2543Smrg#ifdef HAVE_DIX_CONFIG_H
50706f2543Smrg#include <dix-config.h>
51706f2543Smrg#endif
52706f2543Smrg
53706f2543Smrg#include "misc.h"
54706f2543Smrg#include <X11/fonts/fontstruct.h>
55706f2543Smrg#include "dixfontstr.h"
56706f2543Smrg#include "scrnintstr.h"
57706f2543Smrg#include "gcstruct.h"
58706f2543Smrg#include "resource.h"
59706f2543Smrg#include "dix.h"
60706f2543Smrg#include "cursorstr.h"
61706f2543Smrg#include "opaque.h"
62706f2543Smrg#include "servermd.h"
63706f2543Smrg
64706f2543Smrg
65706f2543Smrg/*
66706f2543Smrg    get the bits out of the font in a portable way.  to avoid
67706f2543Smrgdealing with padding and such-like, we draw the glyph into
68706f2543Smrga bitmap, then read the bits out with GetImage, which
69706f2543Smrguses server-natural format.
70706f2543Smrg    since all screens return the same bitmap format, we'll just use
71706f2543Smrgthe first one we find.
72706f2543Smrg    the character origin lines up with the hotspot in the
73706f2543Smrgcursor metrics.
74706f2543Smrg*/
75706f2543Smrg
76706f2543Smrgint
77706f2543SmrgServerBitsFromGlyph(FontPtr pfont, unsigned ch, CursorMetricPtr cm, unsigned char **ppbits)
78706f2543Smrg{
79706f2543Smrg    ScreenPtr pScreen;
80706f2543Smrg    GCPtr pGC;
81706f2543Smrg    xRectangle rect;
82706f2543Smrg    PixmapPtr ppix;
83706f2543Smrg    long nby;
84706f2543Smrg    char *pbits;
85706f2543Smrg    ChangeGCVal gcval[3];
86706f2543Smrg    unsigned char char2b[2];
87706f2543Smrg
88706f2543Smrg    /* turn glyph index into a protocol-format char2b */
89706f2543Smrg    char2b[0] = (unsigned char)(ch >> 8);
90706f2543Smrg    char2b[1] = (unsigned char)(ch & 0xff);
91706f2543Smrg
92706f2543Smrg    pScreen = screenInfo.screens[0];
93706f2543Smrg    nby = BitmapBytePad(cm->width) * (long)cm->height;
94706f2543Smrg    pbits = calloc(1, nby);
95706f2543Smrg    if (!pbits)
96706f2543Smrg	return BadAlloc;
97706f2543Smrg
98706f2543Smrg    ppix = (PixmapPtr)(*pScreen->CreatePixmap)(pScreen, cm->width,
99706f2543Smrg					       cm->height, 1,
100706f2543Smrg					       CREATE_PIXMAP_USAGE_SCRATCH);
101706f2543Smrg    pGC = GetScratchGC(1, pScreen);
102706f2543Smrg    if (!ppix || !pGC)
103706f2543Smrg    {
104706f2543Smrg	if (ppix)
105706f2543Smrg	    (*pScreen->DestroyPixmap)(ppix);
106706f2543Smrg	if (pGC)
107706f2543Smrg	    FreeScratchGC(pGC);
108706f2543Smrg	free(pbits);
109706f2543Smrg	return BadAlloc;
110706f2543Smrg    }
111706f2543Smrg
112706f2543Smrg    rect.x = 0;
113706f2543Smrg    rect.y = 0;
114706f2543Smrg    rect.width = cm->width;
115706f2543Smrg    rect.height = cm->height;
116706f2543Smrg
117706f2543Smrg    /* fill the pixmap with 0 */
118706f2543Smrg    gcval[0].val = GXcopy;
119706f2543Smrg    gcval[1].val = 0;
120706f2543Smrg    gcval[2].ptr = (pointer)pfont;
121706f2543Smrg    ChangeGC(NullClient, pGC, GCFunction | GCForeground | GCFont, gcval);
122706f2543Smrg    ValidateGC((DrawablePtr)ppix, pGC);
123706f2543Smrg    (*pGC->ops->PolyFillRect)((DrawablePtr)ppix, pGC, 1, &rect);
124706f2543Smrg
125706f2543Smrg    /* draw the glyph */
126706f2543Smrg    gcval[0].val = 1;
127706f2543Smrg    ChangeGC(NullClient, pGC, GCForeground, gcval);
128706f2543Smrg    ValidateGC((DrawablePtr)ppix, pGC);
129706f2543Smrg    (*pGC->ops->PolyText16)((DrawablePtr)ppix, pGC, cm->xhot, cm->yhot,
130706f2543Smrg			    1, (unsigned short *)char2b);
131706f2543Smrg    (*pScreen->GetImage)((DrawablePtr)ppix, 0, 0, cm->width, cm->height,
132706f2543Smrg			 XYPixmap, 1, pbits);
133706f2543Smrg    *ppbits = (unsigned char *)pbits;
134706f2543Smrg    FreeScratchGC(pGC);
135706f2543Smrg    (*pScreen->DestroyPixmap)(ppix);
136706f2543Smrg    return Success;
137706f2543Smrg}
138706f2543Smrg
139706f2543Smrg
140706f2543SmrgBool
141706f2543SmrgCursorMetricsFromGlyph(FontPtr pfont, unsigned ch, CursorMetricPtr cm)
142706f2543Smrg{
143706f2543Smrg    CharInfoPtr 	pci;
144706f2543Smrg    unsigned long	nglyphs;
145706f2543Smrg    CARD8		chs[2];
146706f2543Smrg    FontEncoding	encoding;
147706f2543Smrg
148706f2543Smrg    chs[0] = ch >> 8;
149706f2543Smrg    chs[1] = ch;
150706f2543Smrg    encoding = (FONTLASTROW(pfont) == 0) ? Linear16Bit : TwoD16Bit;
151706f2543Smrg    if (encoding == Linear16Bit)
152706f2543Smrg    {
153706f2543Smrg	if (ch < pfont->info.firstCol || pfont->info.lastCol < ch)
154706f2543Smrg	    return FALSE;
155706f2543Smrg    }
156706f2543Smrg    else
157706f2543Smrg    {
158706f2543Smrg	if (chs[0] < pfont->info.firstRow || pfont->info.lastRow < chs[0])
159706f2543Smrg	    return FALSE;
160706f2543Smrg	if (chs[1] < pfont->info.firstCol || pfont->info.lastCol < chs[1])
161706f2543Smrg	    return FALSE;
162706f2543Smrg    }
163706f2543Smrg    (*pfont->get_glyphs) (pfont, 1, chs, encoding, &nglyphs, &pci);
164706f2543Smrg    if (nglyphs == 0)
165706f2543Smrg	return FALSE;
166706f2543Smrg    cm->width = pci->metrics.rightSideBearing - pci->metrics.leftSideBearing;
167706f2543Smrg    cm->height = pci->metrics.descent + pci->metrics.ascent;
168706f2543Smrg    if (pci->metrics.leftSideBearing > 0)
169706f2543Smrg    {
170706f2543Smrg	cm->width += pci->metrics.leftSideBearing;
171706f2543Smrg	cm->xhot = 0;
172706f2543Smrg    }
173706f2543Smrg    else
174706f2543Smrg    {
175706f2543Smrg	cm->xhot = -pci->metrics.leftSideBearing;
176706f2543Smrg	if (pci->metrics.rightSideBearing < 0)
177706f2543Smrg	    cm->width -= pci->metrics.rightSideBearing;
178706f2543Smrg    }
179706f2543Smrg    if (pci->metrics.ascent < 0)
180706f2543Smrg    {
181706f2543Smrg	cm->height -= pci->metrics.ascent;
182706f2543Smrg	cm->yhot = 0;
183706f2543Smrg    }
184706f2543Smrg    else
185706f2543Smrg    {
186706f2543Smrg	cm->yhot = pci->metrics.ascent;
187706f2543Smrg	if (pci->metrics.descent < 0)
188706f2543Smrg	    cm->height -= pci->metrics.descent;
189706f2543Smrg    }
190706f2543Smrg    return TRUE;
191706f2543Smrg}
192