xfont.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
  3. * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice including the dates of first publication and
  13. * either this permission notice or a reference to
  14. * http://oss.sgi.com/projects/FreeB/
  15. * shall be included 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. * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
  22. * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. *
  25. * Except as contained in this notice, the name of Silicon Graphics, Inc.
  26. * shall not be used in advertising or otherwise to promote the sale, use or
  27. * other dealings in this Software without prior written authorization from
  28. * Silicon Graphics, Inc.
  29. */
  30. #ifdef HAVE_DIX_CONFIG_H
  31. #include <dix-config.h>
  32. #endif
  33. #include "glxserver.h"
  34. #include "glxutil.h"
  35. #include "unpack.h"
  36. #include "indirect_dispatch.h"
  37. #include <GL/gl.h>
  38. #include <pixmapstr.h>
  39. #include <windowstr.h>
  40. #include <dixfontstr.h>
  41. /*
  42. ** Make a single GL bitmap from a single X glyph
  43. */
  44. static int
  45. __glXMakeBitmapFromGlyph(FontPtr font, CharInfoPtr pci)
  46. {
  47. int i, j;
  48. int widthPadded; /* width of glyph in bytes, as padded by X */
  49. int allocBytes; /* bytes to allocate to store bitmap */
  50. int w; /* width of glyph in bits */
  51. int h; /* height of glyph */
  52. register unsigned char *pglyph;
  53. register unsigned char *p;
  54. unsigned char *allocbuf;
  55. #define __GL_CHAR_BUF_SIZE 2048
  56. unsigned char buf[__GL_CHAR_BUF_SIZE];
  57. w = GLYPHWIDTHPIXELS(pci);
  58. h = GLYPHHEIGHTPIXELS(pci);
  59. widthPadded = GLYPHWIDTHBYTESPADDED(pci);
  60. /*
  61. ** Use the local buf if possible, otherwise malloc.
  62. */
  63. allocBytes = widthPadded * h;
  64. if (allocBytes <= __GL_CHAR_BUF_SIZE) {
  65. p = buf;
  66. allocbuf = 0;
  67. }
  68. else {
  69. p = (unsigned char *) malloc(allocBytes);
  70. if (!p)
  71. return BadAlloc;
  72. allocbuf = p;
  73. }
  74. /*
  75. ** We have to reverse the picture, top to bottom
  76. */
  77. pglyph = FONTGLYPHBITS(FONTGLYPHS(font), pci) + (h - 1) * widthPadded;
  78. for (j = 0; j < h; j++) {
  79. for (i = 0; i < widthPadded; i++) {
  80. p[i] = pglyph[i];
  81. }
  82. pglyph -= widthPadded;
  83. p += widthPadded;
  84. }
  85. glBitmap(w, h, -pci->metrics.leftSideBearing, pci->metrics.descent,
  86. pci->metrics.characterWidth, 0, allocbuf ? allocbuf : buf);
  87. free(allocbuf);
  88. return Success;
  89. #undef __GL_CHAR_BUF_SIZE
  90. }
  91. /*
  92. ** Create a GL bitmap for each character in the X font. The bitmap is stored
  93. ** in a display list.
  94. */
  95. static int
  96. MakeBitmapsFromFont(FontPtr pFont, int first, int count, int list_base)
  97. {
  98. unsigned long i, nglyphs;
  99. CARD8 chs[2]; /* the font index we are going after */
  100. CharInfoPtr pci;
  101. int rv; /* return value */
  102. int encoding = (FONTLASTROW(pFont) == 0) ? Linear16Bit : TwoD16Bit;
  103. glPixelStorei(GL_UNPACK_SWAP_BYTES, FALSE);
  104. glPixelStorei(GL_UNPACK_LSB_FIRST, BITMAP_BIT_ORDER == LSBFirst);
  105. glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  106. glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
  107. glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
  108. glPixelStorei(GL_UNPACK_ALIGNMENT, GLYPHPADBYTES);
  109. for (i = 0; i < count; i++) {
  110. chs[0] = (first + i) >> 8; /* high byte is first byte */
  111. chs[1] = first + i;
  112. (*pFont->get_glyphs) (pFont, 1, chs, (FontEncoding) encoding,
  113. &nglyphs, &pci);
  114. /*
  115. ** Define a display list containing just a glBitmap() call.
  116. */
  117. glNewList(list_base + i, GL_COMPILE);
  118. if (nglyphs) {
  119. rv = __glXMakeBitmapFromGlyph(pFont, pci);
  120. if (rv) {
  121. return rv;
  122. }
  123. }
  124. glEndList();
  125. }
  126. return Success;
  127. }
  128. /************************************************************************/
  129. int
  130. __glXDisp_UseXFont(__GLXclientState * cl, GLbyte * pc)
  131. {
  132. ClientPtr client = cl->client;
  133. xGLXUseXFontReq *req;
  134. FontPtr pFont;
  135. GLuint currentListIndex;
  136. __GLXcontext *cx;
  137. int error;
  138. REQUEST_SIZE_MATCH(xGLXUseXFontReq);
  139. req = (xGLXUseXFontReq *) pc;
  140. cx = __glXForceCurrent(cl, req->contextTag, &error);
  141. if (!cx) {
  142. return error;
  143. }
  144. glGetIntegerv(GL_LIST_INDEX, (GLint *) &currentListIndex);
  145. if (currentListIndex != 0) {
  146. /*
  147. ** A display list is currently being made. It is an error
  148. ** to try to make a font during another lists construction.
  149. */
  150. client->errorValue = cx->id;
  151. return __glXError(GLXBadContextState);
  152. }
  153. /*
  154. ** Font can actually be either the ID of a font or the ID of a GC
  155. ** containing a font.
  156. */
  157. error = dixLookupFontable(&pFont, req->font, client, DixReadAccess);
  158. if (error != Success)
  159. return error;
  160. return MakeBitmapsFromFont(pFont, req->first, req->count, req->listBase);
  161. }