Brian Paul | b4b35bc | 2004-01-24 16:50:35 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Mesa 3-D graphics library |
| 3 | * Version: 6.0 |
| 4 | * |
| 5 | * Copyright (C) 1999-2004 Brian Paul All Rights Reserved. |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 8 | * copy of this software and associated documentation files (the "Software"), |
| 9 | * to deal in the Software without restriction, including without limitation |
| 10 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 | * and/or sell copies of the Software, and to permit persons to whom the |
| 12 | * Software is furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included |
| 15 | * in all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 18 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 21 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | /* \file xuserotfont.c |
| 27 | * |
| 28 | * A function like glXUseXFont() but takes a 0, 90, 180 or 270 degree |
| 29 | * rotation angle for rotated text display. |
| 30 | * |
| 31 | * Based on Mesa's glXUseXFont implementation written by Thorsten Ohl. |
| 32 | */ |
| 33 | |
| 34 | #include <assert.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
| 37 | #include <GL/glx.h> |
| 38 | #include "xuserotfont.h" |
| 39 | |
| 40 | |
| 41 | /** |
| 42 | * Generate OpenGL-compatible bitmap by drawing an X character glyph |
| 43 | * to an off-screen pixmap, then getting the image and testing pixels. |
| 44 | * \param width bitmap width in pixels |
| 45 | * \param height bitmap height in pixels |
| 46 | */ |
| 47 | static void |
| 48 | fill_bitmap(Display *dpy, Pixmap pixmap, GC gc, |
| 49 | unsigned int bitmapWidth, unsigned int bitmapHeight, |
| 50 | unsigned int charWidth, unsigned int charHeight, |
| 51 | int xPos, int yPos, unsigned int c, GLubyte * bitmap, |
| 52 | int rotation) |
| 53 | { |
| 54 | const int bytesPerRow = (bitmapWidth + 7) / 8; |
| 55 | XImage *image; |
| 56 | XChar2b char2b; |
| 57 | |
| 58 | /* clear pixmap to 0 */ |
| 59 | XSetForeground(dpy, gc, 0); |
| 60 | XFillRectangle(dpy, pixmap, gc, 0, 0, charWidth, charHeight); |
| 61 | |
| 62 | /* The glyph is drawn snug up against the left/top edges of the pixmap */ |
| 63 | XSetForeground(dpy, gc, 1); |
| 64 | char2b.byte1 = (c >> 8) & 0xff; |
| 65 | char2b.byte2 = (c & 0xff); |
| 66 | XDrawString16(dpy, pixmap, gc, xPos, yPos, &char2b, 1); |
| 67 | |
| 68 | /* initialize GL bitmap */ |
| 69 | memset(bitmap, 0, bytesPerRow * bitmapHeight); |
| 70 | |
| 71 | image = XGetImage(dpy, pixmap, 0, 0, charWidth, charHeight, 1, XYPixmap); |
| 72 | if (image) { |
| 73 | /* Set appropriate bits in the GL bitmap. |
| 74 | * Note: X11 and OpenGL are upside down wrt each other). |
| 75 | */ |
| 76 | unsigned int x, y; |
| 77 | if (rotation == 0) { |
| 78 | for (y = 0; y < charHeight; y++) { |
| 79 | for (x = 0; x < charWidth; x++) { |
| 80 | if (XGetPixel(image, x, y)) { |
| 81 | int y2 = bitmapHeight - y - 1; |
| 82 | bitmap[bytesPerRow * y2 + x / 8] |= (1 << (7 - (x % 8))); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | else if (rotation == 90) { |
| 88 | for (y = 0; y < charHeight; y++) { |
| 89 | for (x = 0; x < charWidth; x++) { |
| 90 | if (XGetPixel(image, x, y)) { |
| 91 | int x2 = y; |
| 92 | int y2 = x; |
| 93 | bitmap[bytesPerRow * y2 + x2 / 8] |= (1 << (7 - (x2 % 8))); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | else if (rotation == 180) { |
| 99 | for (y = 0; y < charHeight; y++) { |
| 100 | for (x = 0; x < charWidth; x++) { |
| 101 | if (XGetPixel(image, x, y)) { |
| 102 | int x2 = charWidth - x - 1; |
| 103 | bitmap[bytesPerRow * y + x2 / 8] |= (1 << (7 - (x2 % 8))); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | else { |
| 109 | assert(rotation == 270); |
| 110 | for (y = 0; y < charHeight; y++) { |
| 111 | for (x = 0; x < charWidth; x++) { |
| 112 | if (XGetPixel(image, x, y)) { |
| 113 | int x2 = charHeight - y - 1; |
| 114 | int y2 = charWidth - x - 1; |
| 115 | bitmap[bytesPerRow * y2 + x2 / 8] |= (1 << (7 - (x2 % 8))); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | XDestroyImage(image); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | |
| 125 | /* |
| 126 | * Determine if a given glyph is valid and return the |
| 127 | * corresponding XCharStruct. |
| 128 | */ |
| 129 | static const XCharStruct * |
| 130 | isvalid(const XFontStruct * fs, unsigned int which) |
| 131 | { |
| 132 | unsigned int rows, pages; |
| 133 | unsigned int byte1 = 0, byte2 = 0; |
| 134 | int i, valid = 1; |
| 135 | |
| 136 | rows = fs->max_byte1 - fs->min_byte1 + 1; |
| 137 | pages = fs->max_char_or_byte2 - fs->min_char_or_byte2 + 1; |
| 138 | |
| 139 | if (rows == 1) { |
| 140 | /* "linear" fonts */ |
| 141 | if ((fs->min_char_or_byte2 > which) || (fs->max_char_or_byte2 < which)) |
| 142 | valid = 0; |
| 143 | } |
| 144 | else { |
| 145 | /* "matrix" fonts */ |
| 146 | byte2 = which & 0xff; |
| 147 | byte1 = which >> 8; |
| 148 | if ((fs->min_char_or_byte2 > byte2) || |
| 149 | (fs->max_char_or_byte2 < byte2) || |
| 150 | (fs->min_byte1 > byte1) || (fs->max_byte1 < byte1)) |
| 151 | valid = 0; |
| 152 | } |
| 153 | |
| 154 | if (valid) { |
| 155 | if (fs->per_char) { |
| 156 | if (rows == 1) { |
| 157 | /* "linear" fonts */ |
| 158 | return fs->per_char + (which - fs->min_char_or_byte2); |
| 159 | } |
| 160 | else { |
| 161 | /* "matrix" fonts */ |
| 162 | i = ((byte1 - fs->min_byte1) * pages) + |
| 163 | (byte2 - fs->min_char_or_byte2); |
| 164 | return fs->per_char + i; |
| 165 | } |
| 166 | } |
| 167 | else { |
| 168 | return &fs->min_bounds; |
| 169 | } |
| 170 | } |
| 171 | return NULL; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | void |
| 176 | glXUseRotatedXFontMESA(Font font, int first, int count, int listbase, |
| 177 | int rotation) |
| 178 | { |
| 179 | Display *dpy; |
| 180 | Window win; |
| 181 | Pixmap pixmap; |
| 182 | GC gc; |
| 183 | XFontStruct *fs; |
| 184 | GLint swapbytes, lsbfirst, rowlength; |
| 185 | GLint skiprows, skippixels, alignment; |
| 186 | unsigned int maxCharWidth, maxCharHeight; |
| 187 | GLubyte *bm; |
| 188 | int i; |
| 189 | |
| 190 | if (rotation != 0 && |
| 191 | rotation != 90 && |
| 192 | rotation != 180 && |
| 193 | rotation != 270) |
| 194 | return; |
| 195 | |
| 196 | dpy = glXGetCurrentDisplay(); |
| 197 | if (!dpy) |
| 198 | return; /* I guess glXMakeCurrent wasn't called */ |
| 199 | win = RootWindow(dpy, DefaultScreen(dpy)); |
| 200 | |
| 201 | fs = XQueryFont(dpy, font); |
| 202 | if (!fs) { |
| 203 | /* |
| 204 | _mesa_error(NULL, GL_INVALID_VALUE, |
| 205 | "Couldn't get font structure information"); |
| 206 | */ |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | /* Allocate a GL bitmap that can fit any character */ |
| 211 | maxCharWidth = fs->max_bounds.rbearing - fs->min_bounds.lbearing; |
| 212 | maxCharHeight = fs->max_bounds.ascent + fs->max_bounds.descent; |
| 213 | /* use max, in case we're rotating */ |
| 214 | if (rotation == 90 || rotation == 270) { |
| 215 | /* swap width/height */ |
| 216 | bm = (GLubyte *) malloc((maxCharHeight + 7) / 8 * maxCharWidth); |
| 217 | } |
| 218 | else { |
| 219 | /* normal or upside down */ |
| 220 | bm = (GLubyte *) malloc((maxCharWidth + 7) / 8 * maxCharHeight); |
| 221 | } |
| 222 | if (!bm) { |
| 223 | XFreeFontInfo(NULL, fs, 1); |
| 224 | /* |
| 225 | _mesa_error(NULL, GL_OUT_OF_MEMORY, |
| 226 | "Couldn't allocate bitmap in glXUseXFont()"); |
| 227 | */ |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | #if 0 |
| 232 | /* get the page info */ |
| 233 | pages = fs->max_char_or_byte2 - fs->min_char_or_byte2 + 1; |
| 234 | firstchar = (fs->min_byte1 << 8) + fs->min_char_or_byte2; |
| 235 | lastchar = (fs->max_byte1 << 8) + fs->max_char_or_byte2; |
| 236 | rows = fs->max_byte1 - fs->min_byte1 + 1; |
| 237 | unsigned int first_char, last_char, pages, rows; |
| 238 | #endif |
| 239 | |
| 240 | /* Save the current packing mode for bitmaps. */ |
| 241 | glGetIntegerv(GL_UNPACK_SWAP_BYTES, &swapbytes); |
| 242 | glGetIntegerv(GL_UNPACK_LSB_FIRST, &lsbfirst); |
| 243 | glGetIntegerv(GL_UNPACK_ROW_LENGTH, &rowlength); |
| 244 | glGetIntegerv(GL_UNPACK_SKIP_ROWS, &skiprows); |
| 245 | glGetIntegerv(GL_UNPACK_SKIP_PIXELS, &skippixels); |
| 246 | glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment); |
| 247 | |
| 248 | /* Enforce a standard packing mode which is compatible with |
| 249 | fill_bitmap() from above. This is actually the default mode, |
| 250 | except for the (non)alignment. */ |
| 251 | glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE); |
| 252 | glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE); |
| 253 | glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); |
| 254 | glPixelStorei(GL_UNPACK_SKIP_ROWS, 0); |
| 255 | glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0); |
| 256 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 257 | |
| 258 | /* Create pixmap and GC */ |
| 259 | pixmap = XCreatePixmap(dpy, win, maxCharWidth, maxCharHeight, 1); |
| 260 | { |
| 261 | XGCValues values; |
| 262 | unsigned long valuemask; |
| 263 | values.foreground = BlackPixel(dpy, DefaultScreen(dpy)); |
| 264 | values.background = WhitePixel(dpy, DefaultScreen(dpy)); |
| 265 | values.font = fs->fid; |
| 266 | valuemask = GCForeground | GCBackground | GCFont; |
| 267 | gc = XCreateGC(dpy, pixmap, valuemask, &values); |
| 268 | } |
| 269 | |
| 270 | #ifdef DEBUG_XROT |
| 271 | if (debug_xfonts) |
| 272 | dump_font_struct(fs); |
| 273 | #endif |
| 274 | |
| 275 | for (i = 0; i < count; i++) { |
| 276 | const unsigned int c = first + i; |
| 277 | const int list = listbase + i; |
| 278 | unsigned int charWidth, charHeight; |
| 279 | unsigned int bitmapWidth, bitmapHeight; |
| 280 | GLfloat xOrig, yOrig, xStep, yStep, dtemp; |
| 281 | const XCharStruct *ch; |
| 282 | int xPos, yPos; |
| 283 | int valid; |
| 284 | |
| 285 | /* check on index validity and get the bounds */ |
| 286 | ch = isvalid(fs, c); |
| 287 | if (!ch) { |
| 288 | ch = &fs->max_bounds; |
| 289 | valid = 0; |
| 290 | } |
| 291 | else { |
| 292 | valid = 1; |
| 293 | } |
| 294 | |
| 295 | #ifdef DEBUG_XROT |
| 296 | if (debug_xfonts) { |
| 297 | char s[7]; |
| 298 | sprintf(s, isprint(c) ? "%c> " : "\\%03o> ", c); |
| 299 | dump_char_struct(ch, s); |
| 300 | } |
| 301 | #endif |
| 302 | |
| 303 | /* glBitmap()' parameters: |
| 304 | straight from the glXUseXFont(3) manpage. */ |
| 305 | charWidth = ch->rbearing - ch->lbearing; |
| 306 | charHeight = ch->ascent + ch->descent; |
| 307 | xOrig = -ch->lbearing; |
| 308 | yOrig = ch->descent; |
| 309 | xStep = ch->width; |
| 310 | yStep = 0; |
| 311 | |
| 312 | /* X11's starting point. */ |
| 313 | xPos = -ch->lbearing; |
| 314 | yPos = ch->ascent; |
| 315 | |
| 316 | /* Apply rotation */ |
| 317 | switch (rotation) { |
| 318 | case 0: |
| 319 | /* nothing */ |
| 320 | bitmapWidth = charWidth; |
| 321 | bitmapHeight = charHeight; |
| 322 | break; |
| 323 | case 90: |
| 324 | /* xStep, yStep */ |
| 325 | dtemp = xStep; |
| 326 | xStep = -yStep; |
| 327 | yStep = dtemp; |
| 328 | /* xOrig, yOrig */ |
| 329 | yOrig = xOrig; |
| 330 | xOrig = charHeight - (charHeight - yPos); |
| 331 | /* width, height */ |
| 332 | bitmapWidth = charHeight; |
| 333 | bitmapHeight = charWidth; |
| 334 | break; |
| 335 | case 180: |
| 336 | /* xStep, yStep */ |
| 337 | xStep = -xStep; |
| 338 | yStep = -yStep; |
| 339 | /* xOrig, yOrig */ |
| 340 | xOrig = charWidth - xOrig - 1; |
| 341 | yOrig = charHeight - yOrig - 1; |
| 342 | bitmapWidth = charWidth; |
| 343 | bitmapHeight = charHeight; |
| 344 | break; |
| 345 | case 270: |
| 346 | /* xStep, yStep */ |
| 347 | dtemp = xStep; |
| 348 | xStep = yStep; |
| 349 | yStep = -dtemp; |
| 350 | /* xOrig, yOrig */ |
| 351 | dtemp = yOrig; |
| 352 | yOrig = charWidth - xOrig; |
| 353 | xOrig = dtemp; |
| 354 | /* width, height */ |
| 355 | bitmapWidth = charHeight; |
| 356 | bitmapHeight = charWidth; |
| 357 | break; |
| 358 | default: |
| 359 | /* should never get here */ |
| 360 | ; |
| 361 | } |
| 362 | |
| 363 | glNewList(list, GL_COMPILE); |
| 364 | if (valid && bitmapWidth > 0 && bitmapHeight > 0) { |
| 365 | |
| 366 | fill_bitmap(dpy, pixmap, gc, bitmapWidth, bitmapHeight, |
| 367 | charWidth, charHeight, |
| 368 | xPos, yPos, c, bm, rotation); |
| 369 | |
| 370 | glBitmap(bitmapWidth, bitmapHeight, xOrig, yOrig, xStep, yStep, bm); |
| 371 | |
| 372 | #ifdef DEBUG_XROT |
| 373 | if (debug_xfonts) { |
| 374 | printf("width/height = %u/%u\n", bitmapWidth, bitmapHeight); |
| 375 | dump_bitmap(bitmapWidth, bitmapHeight, bm); |
| 376 | } |
| 377 | #endif |
| 378 | } |
| 379 | else { |
| 380 | glBitmap(0, 0, 0.0, 0.0, xStep, yStep, NULL); |
| 381 | } |
| 382 | glEndList(); |
| 383 | } |
| 384 | |
| 385 | free(bm); |
| 386 | XFreeFontInfo(NULL, fs, 1); |
| 387 | XFreePixmap(dpy, pixmap); |
| 388 | XFreeGC(dpy, gc); |
| 389 | |
| 390 | /* Restore saved packing modes. */ |
| 391 | glPixelStorei(GL_UNPACK_SWAP_BYTES, swapbytes); |
| 392 | glPixelStorei(GL_UNPACK_LSB_FIRST, lsbfirst); |
| 393 | glPixelStorei(GL_UNPACK_ROW_LENGTH, rowlength); |
| 394 | glPixelStorei(GL_UNPACK_SKIP_ROWS, skiprows); |
| 395 | glPixelStorei(GL_UNPACK_SKIP_PIXELS, skippixels); |
| 396 | glPixelStorei(GL_UNPACK_ALIGNMENT, alignment); |
| 397 | } |
| 398 | |
| 399 | |