1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /* xfonts.c -- glXUseXFont() for Mesa written by
27 * Copyright (C) 1995 Thorsten.Ohl @ Physik.TH-Darmstadt.de
28 */
29
30 /*
31 This was take from Mesa and modified to work in the real GLX structure.
32 It provides a fully client side implementation of glXUseXFont and is
33 called by that routine when direct rendering is enabled.
34 */
35
36 #ifdef GLX_DIRECT_RENDERING
37
38 #include "glxclient.h"
39
40 /* Implementation. */
41
42 /* Fill a BITMAP with a character C from thew current font
43 in the graphics context GC. WIDTH is the width in bytes
44 and HEIGHT is the height in bits.
45
46 Note that the generated bitmaps must be used with
47
48 glPixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
49 glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
50 glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
51 glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
52 glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
53 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
54
55 Possible optimizations:
56
57 * use only one reusable pixmap with the maximum dimensions.
58 * draw the entire font into a single pixmap (careful with
59 proportional fonts!).
60 */
61
62
63 /*
64 * Generate OpenGL-compatible bitmap.
65 */
66 static void
fill_bitmap(Display * dpy,int screen,GC gc,unsigned int width,unsigned int height,int x0,int y0,unsigned int c,GLubyte * bitmap)67 fill_bitmap(Display * dpy, int screen, GC gc,
68 unsigned int width, unsigned int height,
69 int x0, int y0, unsigned int c, GLubyte * bitmap)
70 {
71 XImage *image;
72 unsigned int x, y;
73 Pixmap pixmap;
74 XChar2b char2b;
75
76 pixmap = XCreatePixmap(dpy, RootWindow(dpy, screen), 8 * width, height, 1);
77 XSetForeground(dpy, gc, 0);
78 XFillRectangle(dpy, pixmap, gc, 0, 0, 8 * width, height);
79 XSetForeground(dpy, gc, 1);
80
81 char2b.byte1 = (c >> 8) & 0xff;
82 char2b.byte2 = (c & 0xff);
83
84 XDrawString16(dpy, pixmap, gc, x0, y0, &char2b, 1);
85
86 image = XGetImage(dpy, pixmap, 0, 0, 8 * width, height, 1, XYPixmap);
87 if (image) {
88 /* Fill the bitmap (X11 and OpenGL are upside down wrt each other). */
89 for (y = 0; y < height; y++)
90 for (x = 0; x < 8 * width; x++)
91 if (XGetPixel(image, x, y))
92 bitmap[width * (height - y - 1) + x / 8] |=
93 (1 << (7 - (x % 8)));
94 XDestroyImage(image);
95 }
96
97 XFreePixmap(dpy, pixmap);
98 }
99
100 /*
101 * determine if a given glyph is valid and return the
102 * corresponding XCharStruct.
103 */
104 static XCharStruct *
isvalid(XFontStruct * fs,int which)105 isvalid(XFontStruct * fs, int which)
106 {
107 unsigned int rows, pages;
108 int byte1 = 0, byte2 = 0;
109 int i, valid = 1;
110
111 rows = fs->max_byte1 - fs->min_byte1 + 1;
112 pages = fs->max_char_or_byte2 - fs->min_char_or_byte2 + 1;
113
114 if (rows == 1) {
115 /* "linear" fonts */
116 if ((fs->min_char_or_byte2 > which) || (fs->max_char_or_byte2 < which))
117 valid = 0;
118 }
119 else {
120 /* "matrix" fonts */
121 byte2 = which & 0xff;
122 byte1 = which >> 8;
123 if ((fs->min_char_or_byte2 > byte2) ||
124 (fs->max_char_or_byte2 < byte2) ||
125 (fs->min_byte1 > byte1) || (fs->max_byte1 < byte1))
126 valid = 0;
127 }
128
129 if (valid) {
130 if (fs->per_char) {
131 if (rows == 1) {
132 /* "linear" fonts */
133 return (fs->per_char + (which - fs->min_char_or_byte2));
134 }
135 else {
136 /* "matrix" fonts */
137 i = ((byte1 - fs->min_byte1) * pages) +
138 (byte2 - fs->min_char_or_byte2);
139 return (fs->per_char + i);
140 }
141 }
142 else {
143 return (&fs->min_bounds);
144 }
145 }
146 return (NULL);
147 }
148
149 _X_HIDDEN void
DRI_glXUseXFont(struct glx_context * CC,Font font,int first,int count,int listbase)150 DRI_glXUseXFont(struct glx_context *CC, Font font, int first, int count, int listbase)
151 {
152 Display *dpy;
153 int screen;
154 Pixmap pixmap;
155 GC gc;
156 XGCValues values;
157 unsigned long valuemask;
158 XFontStruct *fs;
159
160 GLint swapbytes, lsbfirst, rowlength;
161 GLint skiprows, skippixels, alignment;
162
163 unsigned int max_width, max_height, max_bm_width, max_bm_height;
164 GLubyte *bm;
165
166 int i;
167
168 dpy = CC->currentDpy;
169 screen = CC->psc->scr;
170
171 fs = XQueryFont(dpy, font);
172 if (!fs) {
173 __glXSetError(CC, GL_INVALID_VALUE);
174 return;
175 }
176
177 /* Allocate a bitmap that can fit all characters. */
178 max_width = fs->max_bounds.rbearing - fs->min_bounds.lbearing;
179 max_height = fs->max_bounds.ascent + fs->max_bounds.descent;
180 max_bm_width = (max_width + 7) / 8;
181 max_bm_height = max_height;
182
183 bm = malloc((max_bm_width * max_bm_height) * sizeof(GLubyte));
184 if (!bm) {
185 XFreeFontInfo(NULL, fs, 1);
186 __glXSetError(CC, GL_OUT_OF_MEMORY);
187 return;
188 }
189
190 #if 0
191 /* get the page info */
192 pages = fs->max_char_or_byte2 - fs->min_char_or_byte2 + 1;
193 firstchar = (fs->min_byte1 << 8) + fs->min_char_or_byte2;
194 lastchar = (fs->max_byte1 << 8) + fs->max_char_or_byte2;
195 rows = fs->max_byte1 - fs->min_byte1 + 1;
196 unsigned int first_char, last_char, pages, rows;
197 #endif
198
199 /* Save the current packing mode for bitmaps. */
200 glGetIntegerv(GL_UNPACK_SWAP_BYTES, &swapbytes);
201 glGetIntegerv(GL_UNPACK_LSB_FIRST, &lsbfirst);
202 glGetIntegerv(GL_UNPACK_ROW_LENGTH, &rowlength);
203 glGetIntegerv(GL_UNPACK_SKIP_ROWS, &skiprows);
204 glGetIntegerv(GL_UNPACK_SKIP_PIXELS, &skippixels);
205 glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment);
206
207 /* Enforce a standard packing mode which is compatible with
208 fill_bitmap() from above. This is actually the default mode,
209 except for the (non)alignment. */
210 glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE);
211 glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
212 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
213 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
214 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
215 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
216
217 pixmap = XCreatePixmap(dpy, RootWindow(dpy, screen), 10, 10, 1);
218 values.foreground = BlackPixel(dpy, DefaultScreen(dpy));
219 values.background = WhitePixel(dpy, DefaultScreen(dpy));
220 values.font = fs->fid;
221 valuemask = GCForeground | GCBackground | GCFont;
222 gc = XCreateGC(dpy, pixmap, valuemask, &values);
223 XFreePixmap(dpy, pixmap);
224
225 for (i = 0; i < count; i++) {
226 unsigned int width, height, bm_width, bm_height;
227 GLfloat x0, y0, dx, dy;
228 XCharStruct *ch;
229 int x, y;
230 unsigned int c = first + i;
231 int list = listbase + i;
232 int valid;
233
234 /* check on index validity and get the bounds */
235 ch = isvalid(fs, c);
236 if (!ch) {
237 ch = &fs->max_bounds;
238 valid = 0;
239 }
240 else {
241 valid = 1;
242 }
243
244 /* glBitmap()' parameters:
245 straight from the glXUseXFont(3) manpage. */
246 width = ch->rbearing - ch->lbearing;
247 height = ch->ascent + ch->descent;
248 x0 = -ch->lbearing;
249 y0 = ch->descent - 1;
250 dx = ch->width;
251 dy = 0;
252
253 /* X11's starting point. */
254 x = -ch->lbearing;
255 y = ch->ascent;
256
257 /* Round the width to a multiple of eight. We will use this also
258 for the pixmap for capturing the X11 font. This is slightly
259 inefficient, but it makes the OpenGL part real easy. */
260 bm_width = (width + 7) / 8;
261 bm_height = height;
262
263 glNewList(list, GL_COMPILE);
264 if (valid && (bm_width > 0) && (bm_height > 0)) {
265
266 memset(bm, '\0', bm_width * bm_height);
267 fill_bitmap(dpy, screen, gc, bm_width, bm_height, x, y, c, bm);
268
269 glBitmap(width, height, x0, y0, dx, dy, bm);
270 }
271 else {
272 glBitmap(0, 0, 0.0, 0.0, dx, dy, NULL);
273 }
274 glEndList();
275 }
276
277 free(bm);
278 XFreeFontInfo(NULL, fs, 1);
279 XFreeGC(dpy, gc);
280
281 /* Restore saved packing modes. */
282 glPixelStorei(GL_UNPACK_SWAP_BYTES, swapbytes);
283 glPixelStorei(GL_UNPACK_LSB_FIRST, lsbfirst);
284 glPixelStorei(GL_UNPACK_ROW_LENGTH, rowlength);
285 glPixelStorei(GL_UNPACK_SKIP_ROWS, skiprows);
286 glPixelStorei(GL_UNPACK_SKIP_PIXELS, skippixels);
287 glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
288 }
289
290 #endif
291