home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / dix / glyphcurs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-26  |  5.0 KB  |  176 lines

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