glyphcurs.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /************************************************************************
  2. Copyright 1987, 1998 The Open Group
  3. Permission to use, copy, modify, distribute, and sell this software and its
  4. documentation for any purpose is hereby granted without fee, provided that
  5. the above copyright notice appear in all copies and that both that
  6. copyright notice and this permission notice appear in supporting
  7. documentation.
  8. The above copyright notice and this permission notice shall be included in
  9. all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  14. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. Except as contained in this notice, the name of The Open Group shall not be
  17. used in advertising or otherwise to promote the sale, use or other dealings
  18. in this Software without prior written authorization from The Open Group.
  19. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
  20. All Rights Reserved
  21. Permission to use, copy, modify, and distribute this software and its
  22. documentation for any purpose and without fee is hereby granted,
  23. provided that the above copyright notice appear in all copies and that
  24. both that copyright notice and this permission notice appear in
  25. supporting documentation, and that the name of Digital not be
  26. used in advertising or publicity pertaining to distribution of the
  27. software without specific, written prior permission.
  28. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  29. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  30. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  31. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  32. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  33. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  34. SOFTWARE.
  35. ************************************************************************/
  36. #ifdef HAVE_DIX_CONFIG_H
  37. #include <dix-config.h>
  38. #endif
  39. #include "misc.h"
  40. #include <X11/fonts/fontstruct.h>
  41. #include "dixfontstr.h"
  42. #include "scrnintstr.h"
  43. #include "gcstruct.h"
  44. #include "resource.h"
  45. #include "dix.h"
  46. #include "cursorstr.h"
  47. #include "opaque.h"
  48. #include "servermd.h"
  49. /*
  50. get the bits out of the font in a portable way. to avoid
  51. dealing with padding and such-like, we draw the glyph into
  52. a bitmap, then read the bits out with GetImage, which
  53. uses server-natural format.
  54. since all screens return the same bitmap format, we'll just use
  55. the first one we find.
  56. the character origin lines up with the hotspot in the
  57. cursor metrics.
  58. */
  59. int
  60. ServerBitsFromGlyph(FontPtr pfont, unsigned ch, register CursorMetricPtr cm,
  61. unsigned char **ppbits)
  62. {
  63. ScreenPtr pScreen;
  64. GCPtr pGC;
  65. xRectangle rect;
  66. PixmapPtr ppix;
  67. long nby;
  68. char *pbits;
  69. ChangeGCVal gcval[3];
  70. unsigned char char2b[2];
  71. /* turn glyph index into a protocol-format char2b */
  72. char2b[0] = (unsigned char) (ch >> 8);
  73. char2b[1] = (unsigned char) (ch & 0xff);
  74. pScreen = screenInfo.screens[0];
  75. nby = BitmapBytePad(cm->width) * (long) cm->height;
  76. pbits = malloc(nby);
  77. if (!pbits)
  78. return BadAlloc;
  79. /* zeroing the (pad) bits seems to help some ddx cursor handling */
  80. bzero(pbits, nby);
  81. ppix = (PixmapPtr) (*pScreen->CreatePixmap) (pScreen, cm->width,
  82. cm->height, 1);
  83. pGC = GetScratchGC(1, pScreen);
  84. if (!ppix || !pGC) {
  85. if (ppix)
  86. (*pScreen->DestroyPixmap) (ppix);
  87. if (pGC)
  88. FreeScratchGC(pGC);
  89. free(pbits);
  90. return BadAlloc;
  91. }
  92. rect.x = 0;
  93. rect.y = 0;
  94. rect.width = cm->width;
  95. rect.height = cm->height;
  96. /* fill the pixmap with 0 */
  97. gcval[0].val = GXcopy;
  98. gcval[1].val = 0;
  99. gcval[2].ptr = (pointer) pfont;
  100. dixChangeGC(NullClient, pGC, GCFunction | GCForeground | GCFont,
  101. NULL, gcval);
  102. ValidateGC((DrawablePtr) ppix, pGC);
  103. (*pGC->ops->PolyFillRect) ((DrawablePtr) ppix, pGC, 1, &rect);
  104. /* draw the glyph */
  105. gcval[0].val = 1;
  106. dixChangeGC(NullClient, pGC, GCForeground, NULL, gcval);
  107. ValidateGC((DrawablePtr) ppix, pGC);
  108. (*pGC->ops->PolyText16) ((DrawablePtr) ppix, pGC, cm->xhot, cm->yhot,
  109. 1, (unsigned short *) char2b);
  110. (*pScreen->GetImage) ((DrawablePtr) ppix, 0, 0, cm->width, cm->height,
  111. XYPixmap, 1, pbits);
  112. *ppbits = (unsigned char *) pbits;
  113. FreeScratchGC(pGC);
  114. (*pScreen->DestroyPixmap) (ppix);
  115. return Success;
  116. }
  117. Bool
  118. CursorMetricsFromGlyph(register FontPtr pfont, unsigned ch,
  119. register CursorMetricPtr cm)
  120. {
  121. CharInfoPtr pci;
  122. unsigned long nglyphs;
  123. CARD8 chs[2];
  124. FontEncoding encoding;
  125. chs[0] = ch >> 8;
  126. chs[1] = ch;
  127. encoding = (FONTLASTROW(pfont) == 0) ? Linear16Bit : TwoD16Bit;
  128. if (encoding == Linear16Bit) {
  129. if (ch < pfont->info.firstCol || pfont->info.lastCol < ch)
  130. return FALSE;
  131. }
  132. else {
  133. if (chs[0] < pfont->info.firstRow || pfont->info.lastRow < chs[0])
  134. return FALSE;
  135. if (chs[1] < pfont->info.firstCol || pfont->info.lastCol < chs[1])
  136. return FALSE;
  137. }
  138. (*pfont->get_glyphs) (pfont, 1, chs, encoding, &nglyphs, &pci);
  139. if (nglyphs == 0)
  140. return FALSE;
  141. cm->width = pci->metrics.rightSideBearing - pci->metrics.leftSideBearing;
  142. cm->height = pci->metrics.descent + pci->metrics.ascent;
  143. if (pci->metrics.leftSideBearing > 0) {
  144. cm->width += pci->metrics.leftSideBearing;
  145. cm->xhot = 0;
  146. }
  147. else {
  148. cm->xhot = -pci->metrics.leftSideBearing;
  149. if (pci->metrics.rightSideBearing < 0)
  150. cm->width -= pci->metrics.rightSideBearing;
  151. }
  152. if (pci->metrics.ascent < 0) {
  153. cm->height -= pci->metrics.ascent;
  154. cm->yhot = 0;
  155. }
  156. else {
  157. cm->yhot = pci->metrics.ascent;
  158. if (pci->metrics.descent < 0)
  159. cm->height -= pci->metrics.descent;
  160. }
  161. return TRUE;
  162. }