Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xuserotfont.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * Mesa 3-D graphics library
  3. * Version: 6.1
  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. /* \file xuserotfont.c
  25. *
  26. * A function like glXUseXFont() but takes a 0, 90, 180 or 270 degree
  27. * rotation angle for rotated text display.
  28. *
  29. * Based on Mesa's glXUseXFont implementation written by Thorsten Ohl.
  30. */
  31. #include <assert.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <GL/glx.h>
  35. #include "xuserotfont.h"
  36. /**
  37. * Generate OpenGL-compatible bitmap by drawing an X character glyph
  38. * to an off-screen pixmap, then getting the image and testing pixels.
  39. * \param width bitmap width in pixels
  40. * \param height bitmap height in pixels
  41. */
  42. static void
  43. fill_bitmap(Display *dpy, Pixmap pixmap, GC gc,
  44. unsigned int bitmapWidth, unsigned int bitmapHeight,
  45. unsigned int charWidth, unsigned int charHeight,
  46. int xPos, int yPos, unsigned int c, GLubyte * bitmap,
  47. int rotation)
  48. {
  49. const int bytesPerRow = (bitmapWidth + 7) / 8;
  50. XImage *image;
  51. XChar2b char2b;
  52. /* clear pixmap to 0 */
  53. XSetForeground(dpy, gc, 0);
  54. XFillRectangle(dpy, pixmap, gc, 0, 0, charWidth, charHeight);
  55. /* The glyph is drawn snug up against the left/top edges of the pixmap */
  56. XSetForeground(dpy, gc, 1);
  57. char2b.byte1 = (c >> 8) & 0xff;
  58. char2b.byte2 = (c & 0xff);
  59. XDrawString16(dpy, pixmap, gc, xPos, yPos, &char2b, 1);
  60. /* initialize GL bitmap */
  61. memset(bitmap, 0, bytesPerRow * bitmapHeight);
  62. image = XGetImage(dpy, pixmap, 0, 0, charWidth, charHeight, 1, XYPixmap);
  63. if (image) {
  64. /* Set appropriate bits in the GL bitmap.
  65. * Note: X11 and OpenGL are upside down wrt each other).
  66. */
  67. unsigned int x, y;
  68. if (rotation == 0) {
  69. for (y = 0; y < charHeight; y++) {
  70. for (x = 0; x < charWidth; x++) {
  71. if (XGetPixel(image, x, y)) {
  72. int y2 = bitmapHeight - y - 1;
  73. bitmap[bytesPerRow * y2 + x / 8] |= (1 << (7 - (x % 8)));
  74. }
  75. }
  76. }
  77. }
  78. else if (rotation == 90) {
  79. for (y = 0; y < charHeight; y++) {
  80. for (x = 0; x < charWidth; x++) {
  81. if (XGetPixel(image, x, y)) {
  82. int x2 = y;
  83. int y2 = x;
  84. bitmap[bytesPerRow * y2 + x2 / 8] |= (1 << (7 - (x2 % 8)));
  85. }
  86. }
  87. }
  88. }
  89. else if (rotation == 180) {
  90. for (y = 0; y < charHeight; y++) {
  91. for (x = 0; x < charWidth; x++) {
  92. if (XGetPixel(image, x, y)) {
  93. int x2 = charWidth - x - 1;
  94. bitmap[bytesPerRow * y + x2 / 8] |= (1 << (7 - (x2 % 8)));
  95. }
  96. }
  97. }
  98. }
  99. else {
  100. assert(rotation == 270);
  101. for (y = 0; y < charHeight; y++) {
  102. for (x = 0; x < charWidth; x++) {
  103. if (XGetPixel(image, x, y)) {
  104. int x2 = charHeight - y - 1;
  105. int y2 = charWidth - x - 1;
  106. bitmap[bytesPerRow * y2 + x2 / 8] |= (1 << (7 - (x2 % 8)));
  107. }
  108. }
  109. }
  110. }
  111. XDestroyImage(image);
  112. }
  113. }
  114. /*
  115. * Determine if a given glyph is valid and return the
  116. * corresponding XCharStruct.
  117. */
  118. static const XCharStruct *
  119. isvalid(const XFontStruct * fs, unsigned int which)
  120. {
  121. unsigned int rows, pages;
  122. unsigned int byte1 = 0, byte2 = 0;
  123. int i, valid = 1;
  124. rows = fs->max_byte1 - fs->min_byte1 + 1;
  125. pages = fs->max_char_or_byte2 - fs->min_char_or_byte2 + 1;
  126. if (rows == 1) {
  127. /* "linear" fonts */
  128. if ((fs->min_char_or_byte2 > which) || (fs->max_char_or_byte2 < which))
  129. valid = 0;
  130. }
  131. else {
  132. /* "matrix" fonts */
  133. byte2 = which & 0xff;
  134. byte1 = which >> 8;
  135. if ((fs->min_char_or_byte2 > byte2) ||
  136. (fs->max_char_or_byte2 < byte2) ||
  137. (fs->min_byte1 > byte1) || (fs->max_byte1 < byte1))
  138. valid = 0;
  139. }
  140. if (valid) {
  141. if (fs->per_char) {
  142. if (rows == 1) {
  143. /* "linear" fonts */
  144. return fs->per_char + (which - fs->min_char_or_byte2);
  145. }
  146. else {
  147. /* "matrix" fonts */
  148. i = ((byte1 - fs->min_byte1) * pages) +
  149. (byte2 - fs->min_char_or_byte2);
  150. return fs->per_char + i;
  151. }
  152. }
  153. else {
  154. return &fs->min_bounds;
  155. }
  156. }
  157. return NULL;
  158. }
  159. void
  160. glXUseRotatedXFontMESA(Font font, int first, int count, int listbase,
  161. int rotation)
  162. {
  163. Display *dpy;
  164. Window win;
  165. Pixmap pixmap;
  166. GC gc;
  167. XFontStruct *fs;
  168. GLint swapbytes, lsbfirst, rowlength;
  169. GLint skiprows, skippixels, alignment;
  170. unsigned int maxCharWidth, maxCharHeight;
  171. GLubyte *bm;
  172. int i;
  173. if (rotation != 0 &&
  174. rotation != 90 &&
  175. rotation != 180 &&
  176. rotation != 270)
  177. return;
  178. dpy = glXGetCurrentDisplay();
  179. if (!dpy)
  180. return; /* I guess glXMakeCurrent wasn't called */
  181. win = RootWindow(dpy, DefaultScreen(dpy));
  182. fs = XQueryFont(dpy, font);
  183. if (!fs) {
  184. /*
  185. _mesa_error(NULL, GL_INVALID_VALUE,
  186. "Couldn't get font structure information");
  187. */
  188. return;
  189. }
  190. /* Allocate a GL bitmap that can fit any character */
  191. maxCharWidth = fs->max_bounds.rbearing - fs->min_bounds.lbearing;
  192. maxCharHeight = fs->max_bounds.ascent + fs->max_bounds.descent;
  193. /* use max, in case we're rotating */
  194. if (rotation == 90 || rotation == 270) {
  195. /* swap width/height */
  196. bm = (GLubyte *) malloc((maxCharHeight + 7) / 8 * maxCharWidth);
  197. }
  198. else {
  199. /* normal or upside down */
  200. bm = (GLubyte *) malloc((maxCharWidth + 7) / 8 * maxCharHeight);
  201. }
  202. if (!bm) {
  203. XFreeFontInfo(NULL, fs, 1);
  204. /*
  205. _mesa_error(NULL, GL_OUT_OF_MEMORY,
  206. "Couldn't allocate bitmap in glXUseXFont()");
  207. */
  208. return;
  209. }
  210. #if 0
  211. /* get the page info */
  212. pages = fs->max_char_or_byte2 - fs->min_char_or_byte2 + 1;
  213. firstchar = (fs->min_byte1 << 8) + fs->min_char_or_byte2;
  214. lastchar = (fs->max_byte1 << 8) + fs->max_char_or_byte2;
  215. rows = fs->max_byte1 - fs->min_byte1 + 1;
  216. unsigned int first_char, last_char, pages, rows;
  217. #endif
  218. /* Save the current packing mode for bitmaps. */
  219. glGetIntegerv(GL_UNPACK_SWAP_BYTES, &swapbytes);
  220. glGetIntegerv(GL_UNPACK_LSB_FIRST, &lsbfirst);
  221. glGetIntegerv(GL_UNPACK_ROW_LENGTH, &rowlength);
  222. glGetIntegerv(GL_UNPACK_SKIP_ROWS, &skiprows);
  223. glGetIntegerv(GL_UNPACK_SKIP_PIXELS, &skippixels);
  224. glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment);
  225. /* Enforce a standard packing mode which is compatible with
  226. fill_bitmap() from above. This is actually the default mode,
  227. except for the (non)alignment. */
  228. glPixelStorei(GL_UNPACK_SWAP_BYTES, GL_FALSE);
  229. glPixelStorei(GL_UNPACK_LSB_FIRST, GL_FALSE);
  230. glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  231. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  232. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  233. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  234. /* Create pixmap and GC */
  235. pixmap = XCreatePixmap(dpy, win, maxCharWidth, maxCharHeight, 1);
  236. {
  237. XGCValues values;
  238. unsigned long valuemask;
  239. values.foreground = BlackPixel(dpy, DefaultScreen(dpy));
  240. values.background = WhitePixel(dpy, DefaultScreen(dpy));
  241. values.font = fs->fid;
  242. valuemask = GCForeground | GCBackground | GCFont;
  243. gc = XCreateGC(dpy, pixmap, valuemask, &values);
  244. }
  245. #ifdef DEBUG_XROT
  246. if (debug_xfonts)
  247. dump_font_struct(fs);
  248. #endif
  249. for (i = 0; i < count; i++) {
  250. const unsigned int c = first + i;
  251. const int list = listbase + i;
  252. unsigned int charWidth, charHeight;
  253. unsigned int bitmapWidth = 0, bitmapHeight = 0;
  254. GLfloat xOrig, yOrig, xStep, yStep, dtemp;
  255. const XCharStruct *ch;
  256. int xPos, yPos;
  257. int valid;
  258. /* check on index validity and get the bounds */
  259. ch = isvalid(fs, c);
  260. if (!ch) {
  261. ch = &fs->max_bounds;
  262. valid = 0;
  263. }
  264. else {
  265. valid = 1;
  266. }
  267. #ifdef DEBUG_XROT
  268. if (debug_xfonts) {
  269. char s[7];
  270. sprintf(s, isprint(c) ? "%c> " : "\\%03o> ", c);
  271. dump_char_struct(ch, s);
  272. }
  273. #endif
  274. /* glBitmap()' parameters:
  275. straight from the glXUseXFont(3) manpage. */
  276. charWidth = ch->rbearing - ch->lbearing;
  277. charHeight = ch->ascent + ch->descent;
  278. xOrig = -ch->lbearing;
  279. yOrig = ch->descent;
  280. xStep = ch->width;
  281. yStep = 0;
  282. /* X11's starting point. */
  283. xPos = -ch->lbearing;
  284. yPos = ch->ascent;
  285. /* Apply rotation */
  286. switch (rotation) {
  287. case 0:
  288. /* nothing */
  289. bitmapWidth = charWidth;
  290. bitmapHeight = charHeight;
  291. break;
  292. case 90:
  293. /* xStep, yStep */
  294. dtemp = xStep;
  295. xStep = -yStep;
  296. yStep = dtemp;
  297. /* xOrig, yOrig */
  298. yOrig = xOrig;
  299. xOrig = charHeight - (charHeight - yPos);
  300. /* width, height */
  301. bitmapWidth = charHeight;
  302. bitmapHeight = charWidth;
  303. break;
  304. case 180:
  305. /* xStep, yStep */
  306. xStep = -xStep;
  307. yStep = -yStep;
  308. /* xOrig, yOrig */
  309. xOrig = charWidth - xOrig - 1;
  310. yOrig = charHeight - yOrig - 1;
  311. bitmapWidth = charWidth;
  312. bitmapHeight = charHeight;
  313. break;
  314. case 270:
  315. /* xStep, yStep */
  316. dtemp = xStep;
  317. xStep = yStep;
  318. yStep = -dtemp;
  319. /* xOrig, yOrig */
  320. dtemp = yOrig;
  321. yOrig = charWidth - xOrig;
  322. xOrig = dtemp;
  323. /* width, height */
  324. bitmapWidth = charHeight;
  325. bitmapHeight = charWidth;
  326. break;
  327. default:
  328. /* should never get here */
  329. ;
  330. }
  331. glNewList(list, GL_COMPILE);
  332. if (valid && bitmapWidth > 0 && bitmapHeight > 0) {
  333. fill_bitmap(dpy, pixmap, gc, bitmapWidth, bitmapHeight,
  334. charWidth, charHeight,
  335. xPos, yPos, c, bm, rotation);
  336. glBitmap(bitmapWidth, bitmapHeight, xOrig, yOrig, xStep, yStep, bm);
  337. #ifdef DEBUG_XROT
  338. if (debug_xfonts) {
  339. printf("width/height = %u/%u\n", bitmapWidth, bitmapHeight);
  340. dump_bitmap(bitmapWidth, bitmapHeight, bm);
  341. }
  342. #endif
  343. }
  344. else {
  345. glBitmap(0, 0, 0.0, 0.0, xStep, yStep, NULL);
  346. }
  347. glEndList();
  348. }
  349. free(bm);
  350. XFreeFontInfo(NULL, fs, 1);
  351. XFreePixmap(dpy, pixmap);
  352. XFreeGC(dpy, gc);
  353. /* Restore saved packing modes. */
  354. glPixelStorei(GL_UNPACK_SWAP_BYTES, swapbytes);
  355. glPixelStorei(GL_UNPACK_LSB_FIRST, lsbfirst);
  356. glPixelStorei(GL_UNPACK_ROW_LENGTH, rowlength);
  357. glPixelStorei(GL_UNPACK_SKIP_ROWS, skiprows);
  358. glPixelStorei(GL_UNPACK_SKIP_PIXELS, skippixels);
  359. glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
  360. }