xlib: move state tracker to glx/xlib
[mesa.git] / src / gallium / state_trackers / glx / xlib / fakeglx_fonts.c
1
2 /*
3 * Mesa 3-D graphics library
4 * Version: 3.5
5 *
6 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /* xfonts.c -- glXUseXFont() for Mesa written by
28 * Copyright (C) 1995 Thorsten.Ohl @ Physik.TH-Darmstadt.de
29 */
30
31 #include "context.h"
32 #include "imports.h"
33 #include "fakeglx.h"
34 #include <GL/glx.h>
35
36
37 /* Some debugging info. */
38
39 #ifdef DEBUG
40 #undef _R
41 #undef _G
42 #undef _B
43 #include <ctype.h>
44
45 int debug_xfonts = 0;
46
47 static void
48 dump_char_struct(XCharStruct * ch, char *prefix)
49 {
50 printf("%slbearing = %d, rbearing = %d, width = %d\n",
51 prefix, ch->lbearing, ch->rbearing, ch->width);
52 printf("%sascent = %d, descent = %d, attributes = %u\n",
53 prefix, ch->ascent, ch->descent, (unsigned int) ch->attributes);
54 }
55
56 static void
57 dump_font_struct(XFontStruct * font)
58 {
59 printf("ascent = %d, descent = %d\n", font->ascent, font->descent);
60 printf("char_or_byte2 = (%u,%u)\n",
61 font->min_char_or_byte2, font->max_char_or_byte2);
62 printf("byte1 = (%u,%u)\n", font->min_byte1, font->max_byte1);
63 printf("all_chars_exist = %s\n", font->all_chars_exist ? "True" : "False");
64 printf("default_char = %c (\\%03o)\n",
65 (char) (isprint(font->default_char) ? font->default_char : ' '),
66 font->default_char);
67 dump_char_struct(&font->min_bounds, "min> ");
68 dump_char_struct(&font->max_bounds, "max> ");
69 #if 0
70 for (c = font->min_char_or_byte2; c <= font->max_char_or_byte2; c++) {
71 char prefix[8];
72 sprintf(prefix, "%d> ", c);
73 dump_char_struct(&font->per_char[c], prefix);
74 }
75 #endif
76 }
77
78 static void
79 dump_bitmap(unsigned int width, unsigned int height, GLubyte * bitmap)
80 {
81 unsigned int x, y;
82
83 printf(" ");
84 for (x = 0; x < 8 * width; x++)
85 printf("%o", 7 - (x % 8));
86 putchar('\n');
87 for (y = 0; y < height; y++) {
88 printf("%3o:", y);
89 for (x = 0; x < 8 * width; x++)
90 putchar((bitmap[width * (height - y - 1) + x / 8] & (1 << (7 - (x %
91 8))))
92 ? '*' : '.');
93 printf(" ");
94 for (x = 0; x < width; x++)
95 printf("0x%02x, ", bitmap[width * (height - y - 1) + x]);
96 putchar('\n');
97 }
98 }
99 #endif /* DEBUG */
100
101
102 /* Implementation. */
103
104 /* Fill a BITMAP with a character C from thew current font
105 in the graphics context GC. WIDTH is the width in bytes
106 and HEIGHT is the height in bits.
107
108 Note that the generated bitmaps must be used with
109
110 glPixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
111 glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
112 glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
113 glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
114 glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
115 glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
116
117 Possible optimizations:
118
119 * use only one reusable pixmap with the maximum dimensions.
120 * draw the entire font into a single pixmap (careful with
121 proportional fonts!).
122 */
123
124
125 /*
126 * Generate OpenGL-compatible bitmap.
127 */
128 static void
129 fill_bitmap(Display * dpy, Window win, GC gc,
130 unsigned int width, unsigned int height,
131 int x0, int y0, unsigned int c, GLubyte * bitmap)
132 {
133 XImage *image;
134 unsigned int x, y;
135 Pixmap pixmap;
136 XChar2b char2b;
137
138 pixmap = XCreatePixmap(dpy, win, 8 * width, height, 1);
139 XSetForeground(dpy, gc, 0);
140 XFillRectangle(dpy, pixmap, gc, 0, 0, 8 * width, height);
141 XSetForeground(dpy, gc, 1);
142
143 char2b.byte1 = (c >> 8) & 0xff;
144 char2b.byte2 = (c & 0xff);
145
146 XDrawString16(dpy, pixmap, gc, x0, y0, &char2b, 1);
147
148 image = XGetImage(dpy, pixmap, 0, 0, 8 * width, height, 1, XYPixmap);
149 if (image) {
150 /* Fill the bitmap (X11 and OpenGL are upside down wrt each other). */
151 for (y = 0; y < height; y++)
152 for (x = 0; x < 8 * width; x++)
153 if (XGetPixel(image, x, y))
154 bitmap[width * (height - y - 1) + x / 8] |=
155 (1 << (7 - (x % 8)));
156 XDestroyImage(image);
157 }
158
159 XFreePixmap(dpy, pixmap);
160 }
161
162 /*
163 * determine if a given glyph is valid and return the
164 * corresponding XCharStruct.
165 */
166 static XCharStruct *
167 isvalid(XFontStruct * fs, unsigned int which)
168 {
169 unsigned int rows, pages;
170 unsigned int byte1 = 0, byte2 = 0;
171 int i, valid = 1;
172
173 rows = fs->max_byte1 - fs->min_byte1 + 1;
174 pages = fs->max_char_or_byte2 - fs->min_char_or_byte2 + 1;
175
176 if (rows == 1) {
177 /* "linear" fonts */
178 if ((fs->min_char_or_byte2 > which) || (fs->max_char_or_byte2 < which))
179 valid = 0;
180 }
181 else {
182 /* "matrix" fonts */
183 byte2 = which & 0xff;
184 byte1 = which >> 8;
185 if ((fs->min_char_or_byte2 > byte2) ||
186 (fs->max_char_or_byte2 < byte2) ||
187 (fs->min_byte1 > byte1) || (fs->max_byte1 < byte1))
188 valid = 0;
189 }
190
191 if (valid) {
192 if (fs->per_char) {
193 if (rows == 1) {
194 /* "linear" fonts */
195 return (fs->per_char + (which - fs->min_char_or_byte2));
196 }
197 else {
198 /* "matrix" fonts */
199 i = ((byte1 - fs->min_byte1) * pages) +
200 (byte2 - fs->min_char_or_byte2);
201 return (fs->per_char + i);
202 }
203 }
204 else {
205 return (&fs->min_bounds);
206 }
207 }
208 return (NULL);
209 }
210
211
212 void
213 Fake_glXUseXFont(Font font, int first, int count, int listbase)
214 {
215 Display *dpy;
216 Window win;
217 Pixmap pixmap;
218 GC gc;
219 XGCValues values;
220 unsigned long valuemask;
221 XFontStruct *fs;
222 GLint swapbytes, lsbfirst, rowlength;
223 GLint skiprows, skippixels, alignment;
224 unsigned int max_width, max_height, max_bm_width, max_bm_height;
225 GLubyte *bm;
226 int i;
227
228 dpy = glXGetCurrentDisplay();
229 if (!dpy)
230 return; /* I guess glXMakeCurrent wasn't called */
231 win = RootWindow(dpy, DefaultScreen(dpy));
232
233 fs = XQueryFont(dpy, font);
234 if (!fs) {
235 _mesa_error(NULL, GL_INVALID_VALUE,
236 "Couldn't get font structure information");
237 return;
238 }
239
240 /* Allocate a bitmap that can fit all characters. */
241 max_width = fs->max_bounds.rbearing - fs->min_bounds.lbearing;
242 max_height = fs->max_bounds.ascent + fs->max_bounds.descent;
243 max_bm_width = (max_width + 7) / 8;
244 max_bm_height = max_height;
245
246 bm = (GLubyte *) MALLOC((max_bm_width * max_bm_height) * sizeof(GLubyte));
247 if (!bm) {
248 XFreeFontInfo(NULL, fs, 1);
249 _mesa_error(NULL, GL_OUT_OF_MEMORY,
250 "Couldn't allocate bitmap in glXUseXFont()");
251 return;
252 }
253
254 #if 0
255 /* get the page info */
256 pages = fs->max_char_or_byte2 - fs->min_char_or_byte2 + 1;
257 firstchar = (fs->min_byte1 << 8) + fs->min_char_or_byte2;
258 lastchar = (fs->max_byte1 << 8) + fs->max_char_or_byte2;
259 rows = fs->max_byte1 - fs->min_byte1 + 1;
260 unsigned int first_char, last_char, pages, rows;
261 #endif
262
263 /* Save the current packing mode for bitmaps. */
264 glGetIntegerv(GL_UNPACK_SWAP_BYTES, &swapbytes);
265 glGetIntegerv(GL_UNPACK_LSB_FIRST, &lsbfirst);
266 glGetIntegerv(GL_UNPACK_ROW_LENGTH, &rowlength);
267 glGetIntegerv(GL_UNPACK_SKIP_ROWS, &skiprows);
268 glGetIntegerv(GL_UNPACK_SKIP_PIXELS, &skippixels);
269 glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment);
270
271 /* Enforce a standard packing mode which is compatible with
272 fill_bitmap() from above. This is actually the default mode,
273 except for the (non)alignment. */
274 glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE);
275 glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
276 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
277 glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
278 glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
279 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
280
281 pixmap = XCreatePixmap(dpy, win, 10, 10, 1);
282 values.foreground = BlackPixel(dpy, DefaultScreen(dpy));
283 values.background = WhitePixel(dpy, DefaultScreen(dpy));
284 values.font = fs->fid;
285 valuemask = GCForeground | GCBackground | GCFont;
286 gc = XCreateGC(dpy, pixmap, valuemask, &values);
287 XFreePixmap(dpy, pixmap);
288
289 #ifdef DEBUG
290 if (debug_xfonts)
291 dump_font_struct(fs);
292 #endif
293
294 for (i = 0; i < count; i++) {
295 unsigned int width, height, bm_width, bm_height;
296 GLfloat x0, y0, dx, dy;
297 XCharStruct *ch;
298 int x, y;
299 unsigned int c = first + i;
300 int list = listbase + i;
301 int valid;
302
303 /* check on index validity and get the bounds */
304 ch = isvalid(fs, c);
305 if (!ch) {
306 ch = &fs->max_bounds;
307 valid = 0;
308 }
309 else {
310 valid = 1;
311 }
312
313 #ifdef DEBUG
314 if (debug_xfonts) {
315 char s[7];
316 sprintf(s, isprint(c) ? "%c> " : "\\%03o> ", c);
317 dump_char_struct(ch, s);
318 }
319 #endif
320
321 /* glBitmap()' parameters:
322 straight from the glXUseXFont(3) manpage. */
323 width = ch->rbearing - ch->lbearing;
324 height = ch->ascent + ch->descent;
325 x0 = -ch->lbearing;
326 y0 = ch->descent - 0; /* XXX used to subtract 1 here */
327 /* but that caused a conformace failure */
328 dx = ch->width;
329 dy = 0;
330
331 /* X11's starting point. */
332 x = -ch->lbearing;
333 y = ch->ascent;
334
335 /* Round the width to a multiple of eight. We will use this also
336 for the pixmap for capturing the X11 font. This is slightly
337 inefficient, but it makes the OpenGL part real easy. */
338 bm_width = (width + 7) / 8;
339 bm_height = height;
340
341 glNewList(list, GL_COMPILE);
342 if (valid && (bm_width > 0) && (bm_height > 0)) {
343
344 MEMSET(bm, '\0', bm_width * bm_height);
345 fill_bitmap(dpy, win, gc, bm_width, bm_height, x, y, c, bm);
346
347 glBitmap(width, height, x0, y0, dx, dy, bm);
348 #ifdef DEBUG
349 if (debug_xfonts) {
350 printf("width/height = %u/%u\n", width, height);
351 printf("bm_width/bm_height = %u/%u\n", bm_width, bm_height);
352 dump_bitmap(bm_width, bm_height, bm);
353 }
354 #endif
355 }
356 else {
357 glBitmap(0, 0, 0.0, 0.0, dx, dy, NULL);
358 }
359 glEndList();
360 }
361
362 FREE(bm);
363 XFreeFontInfo(NULL, fs, 1);
364 XFreeGC(dpy, gc);
365
366 /* Restore saved packing modes. */
367 glPixelStorei(GL_UNPACK_SWAP_BYTES, swapbytes);
368 glPixelStorei(GL_UNPACK_LSB_FIRST, lsbfirst);
369 glPixelStorei(GL_UNPACK_ROW_LENGTH, rowlength);
370 glPixelStorei(GL_UNPACK_SKIP_ROWS, skiprows);
371 glPixelStorei(GL_UNPACK_SKIP_PIXELS, skippixels);
372 glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
373 }