1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
3*4882a593Smuzhiyun * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Permission is hereby granted, free of charge, to any person obtaining a
6*4882a593Smuzhiyun * copy of this software and associated documentation files (the "Software"),
7*4882a593Smuzhiyun * to deal in the Software without restriction, including without limitation
8*4882a593Smuzhiyun * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9*4882a593Smuzhiyun * and/or sell copies of the Software, and to permit persons to whom the
10*4882a593Smuzhiyun * Software is furnished to do so, subject to the following conditions:
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * The above copyright notice including the dates of first publication and
13*4882a593Smuzhiyun * either this permission notice or a reference to
14*4882a593Smuzhiyun * http://oss.sgi.com/projects/FreeB/
15*4882a593Smuzhiyun * shall be included in all copies or substantial portions of the Software.
16*4882a593Smuzhiyun *
17*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18*4882a593Smuzhiyun * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19*4882a593Smuzhiyun * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20*4882a593Smuzhiyun * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21*4882a593Smuzhiyun * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
22*4882a593Smuzhiyun * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23*4882a593Smuzhiyun * SOFTWARE.
24*4882a593Smuzhiyun *
25*4882a593Smuzhiyun * Except as contained in this notice, the name of Silicon Graphics, Inc.
26*4882a593Smuzhiyun * shall not be used in advertising or otherwise to promote the sale, use or
27*4882a593Smuzhiyun * other dealings in this Software without prior written authorization from
28*4882a593Smuzhiyun * Silicon Graphics, Inc.
29*4882a593Smuzhiyun */
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #ifdef HAVE_DIX_CONFIG_H
32*4882a593Smuzhiyun #include <dix-config.h>
33*4882a593Smuzhiyun #endif
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun #include "glxserver.h"
36*4882a593Smuzhiyun #include "glxutil.h"
37*4882a593Smuzhiyun #include "unpack.h"
38*4882a593Smuzhiyun #include "indirect_dispatch.h"
39*4882a593Smuzhiyun #include <GL/gl.h>
40*4882a593Smuzhiyun #include <pixmapstr.h>
41*4882a593Smuzhiyun #include <windowstr.h>
42*4882a593Smuzhiyun #include <dixfontstr.h>
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun /*
45*4882a593Smuzhiyun ** Make a single GL bitmap from a single X glyph
46*4882a593Smuzhiyun */
47*4882a593Smuzhiyun static int
__glXMakeBitmapFromGlyph(FontPtr font,CharInfoPtr pci)48*4882a593Smuzhiyun __glXMakeBitmapFromGlyph(FontPtr font, CharInfoPtr pci)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun int i, j;
51*4882a593Smuzhiyun int widthPadded; /* width of glyph in bytes, as padded by X */
52*4882a593Smuzhiyun int allocBytes; /* bytes to allocate to store bitmap */
53*4882a593Smuzhiyun int w; /* width of glyph in bits */
54*4882a593Smuzhiyun int h; /* height of glyph */
55*4882a593Smuzhiyun register unsigned char *pglyph;
56*4882a593Smuzhiyun register unsigned char *p;
57*4882a593Smuzhiyun unsigned char *allocbuf;
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun #define __GL_CHAR_BUF_SIZE 2048
60*4882a593Smuzhiyun unsigned char buf[__GL_CHAR_BUF_SIZE];
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun w = GLYPHWIDTHPIXELS(pci);
63*4882a593Smuzhiyun h = GLYPHHEIGHTPIXELS(pci);
64*4882a593Smuzhiyun widthPadded = GLYPHWIDTHBYTESPADDED(pci);
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun /*
67*4882a593Smuzhiyun ** Use the local buf if possible, otherwise malloc.
68*4882a593Smuzhiyun */
69*4882a593Smuzhiyun allocBytes = widthPadded * h;
70*4882a593Smuzhiyun if (allocBytes <= __GL_CHAR_BUF_SIZE) {
71*4882a593Smuzhiyun p = buf;
72*4882a593Smuzhiyun allocbuf = 0;
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun else {
75*4882a593Smuzhiyun p = (unsigned char *) malloc(allocBytes);
76*4882a593Smuzhiyun if (!p)
77*4882a593Smuzhiyun return BadAlloc;
78*4882a593Smuzhiyun allocbuf = p;
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun /*
82*4882a593Smuzhiyun ** We have to reverse the picture, top to bottom
83*4882a593Smuzhiyun */
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun pglyph = FONTGLYPHBITS(FONTGLYPHS(font), pci) + (h - 1) * widthPadded;
86*4882a593Smuzhiyun for (j = 0; j < h; j++) {
87*4882a593Smuzhiyun for (i = 0; i < widthPadded; i++) {
88*4882a593Smuzhiyun p[i] = pglyph[i];
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun pglyph -= widthPadded;
91*4882a593Smuzhiyun p += widthPadded;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun glBitmap(w, h, -pci->metrics.leftSideBearing, pci->metrics.descent,
94*4882a593Smuzhiyun pci->metrics.characterWidth, 0, allocbuf ? allocbuf : buf);
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun free(allocbuf);
97*4882a593Smuzhiyun return Success;
98*4882a593Smuzhiyun #undef __GL_CHAR_BUF_SIZE
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun /*
102*4882a593Smuzhiyun ** Create a GL bitmap for each character in the X font. The bitmap is stored
103*4882a593Smuzhiyun ** in a display list.
104*4882a593Smuzhiyun */
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun static int
MakeBitmapsFromFont(FontPtr pFont,int first,int count,int list_base)107*4882a593Smuzhiyun MakeBitmapsFromFont(FontPtr pFont, int first, int count, int list_base)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun unsigned long i, nglyphs;
110*4882a593Smuzhiyun CARD8 chs[2]; /* the font index we are going after */
111*4882a593Smuzhiyun CharInfoPtr pci;
112*4882a593Smuzhiyun int rv; /* return value */
113*4882a593Smuzhiyun int encoding = (FONTLASTROW(pFont) == 0) ? Linear16Bit : TwoD16Bit;
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun glPixelStorei(GL_UNPACK_SWAP_BYTES, FALSE);
116*4882a593Smuzhiyun glPixelStorei(GL_UNPACK_LSB_FIRST, BITMAP_BIT_ORDER == LSBFirst);
117*4882a593Smuzhiyun glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
118*4882a593Smuzhiyun glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
119*4882a593Smuzhiyun glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
120*4882a593Smuzhiyun glPixelStorei(GL_UNPACK_ALIGNMENT, GLYPHPADBYTES);
121*4882a593Smuzhiyun for (i = 0; i < count; i++) {
122*4882a593Smuzhiyun chs[0] = (first + i) >> 8; /* high byte is first byte */
123*4882a593Smuzhiyun chs[1] = first + i;
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun (*pFont->get_glyphs) (pFont, 1, chs, (FontEncoding) encoding,
126*4882a593Smuzhiyun &nglyphs, &pci);
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun /*
129*4882a593Smuzhiyun ** Define a display list containing just a glBitmap() call.
130*4882a593Smuzhiyun */
131*4882a593Smuzhiyun glNewList(list_base + i, GL_COMPILE);
132*4882a593Smuzhiyun if (nglyphs) {
133*4882a593Smuzhiyun rv = __glXMakeBitmapFromGlyph(pFont, pci);
134*4882a593Smuzhiyun if (rv) {
135*4882a593Smuzhiyun return rv;
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun glEndList();
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun return Success;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun /************************************************************************/
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun int
__glXDisp_UseXFont(__GLXclientState * cl,GLbyte * pc)146*4882a593Smuzhiyun __glXDisp_UseXFont(__GLXclientState * cl, GLbyte * pc)
147*4882a593Smuzhiyun {
148*4882a593Smuzhiyun ClientPtr client = cl->client;
149*4882a593Smuzhiyun xGLXUseXFontReq *req;
150*4882a593Smuzhiyun FontPtr pFont;
151*4882a593Smuzhiyun GLuint currentListIndex;
152*4882a593Smuzhiyun __GLXcontext *cx;
153*4882a593Smuzhiyun int error;
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun req = (xGLXUseXFontReq *) pc;
156*4882a593Smuzhiyun cx = __glXForceCurrent(cl, req->contextTag, &error);
157*4882a593Smuzhiyun if (!cx) {
158*4882a593Smuzhiyun return error;
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun glGetIntegerv(GL_LIST_INDEX, (GLint *) ¤tListIndex);
162*4882a593Smuzhiyun if (currentListIndex != 0) {
163*4882a593Smuzhiyun /*
164*4882a593Smuzhiyun ** A display list is currently being made. It is an error
165*4882a593Smuzhiyun ** to try to make a font during another lists construction.
166*4882a593Smuzhiyun */
167*4882a593Smuzhiyun client->errorValue = cx->id;
168*4882a593Smuzhiyun return __glXError(GLXBadContextState);
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun /*
172*4882a593Smuzhiyun ** Font can actually be either the ID of a font or the ID of a GC
173*4882a593Smuzhiyun ** containing a font.
174*4882a593Smuzhiyun */
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun error = dixLookupFontable(&pFont, req->font, client, DixReadAccess);
177*4882a593Smuzhiyun if (error != Success)
178*4882a593Smuzhiyun return error;
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun return MakeBitmapsFromFont(pFont, req->first, req->count, req->listBase);
181*4882a593Smuzhiyun }
182