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