home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / ddx / mfb / mfbplygblt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-27  |  10.5 KB  |  380 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. /* $XConsortium: mfbplygblt.c,v 5.4 91/01/27 13:02:11 keith Exp $ */
  25.  
  26. #include "X.h"
  27. #include "Xmd.h"
  28. #include "Xproto.h"
  29. #include "mfb.h"
  30. #include "fontstruct.h"
  31. #include "dixfontstr.h"
  32. #include "gcstruct.h"
  33. #include "windowstr.h"
  34. #include "pixmapstr.h"
  35. #include "scrnintstr.h"
  36. #include "regionstr.h"
  37. #include "maskbits.h"
  38. #include "miscstruct.h"
  39.  
  40. extern void QueryGlyphExtents();
  41.  
  42. /*
  43.     we should eventually special-case fixed-width fonts, although
  44. its more important for ImageText, which is meant for terminal
  45. emulators.
  46.  
  47.     this works for fonts with glyphs <= 32 bits wide.
  48.  
  49.     the clipping calculations are done for worst-case fonts.
  50. we make no assumptions about the heights, widths, or bearings
  51. of the glyphs.  if we knew that the glyphs are all the same height,
  52. we could clip the tops and bottoms per clipping box, rather
  53. than per character per clipping box.  if we knew that the glyphs'
  54. left and right bearings were well-behaved, we could clip a single
  55. character at the start, output until the last unclipped
  56. character, and then clip the last one.  this is all straightforward
  57. to determine based on max-bounds and min-bounds from the font.
  58.     there is some inefficiency introduced in the per-character
  59. clipping to make what's going on clearer.
  60.  
  61.     (it is possible, for example, for a font to be defined in which the
  62. next-to-last character in a font would be clipped out, but the last
  63. one wouldn't.  the code below deals with this.)
  64.  
  65.     PolyText looks at the fg color and the rasterop; mfbValidateGC
  66. swaps in the right routine after looking at the reduced ratserop
  67. in the private field of the GC.  
  68.  
  69.    the register allocations are provisional; in particualr startmask and
  70. endmask might not be the right things.  pglyph, xoff, pdst, and tmpSrc
  71. are fairly obvious, though.
  72.  
  73.    to avoid source proliferation, this file is compiled
  74. three times:
  75.     MFBPOLYGLYPHBLT        OPEQ
  76.     mfbPolyGlyphBltWhite    |=
  77.     mfbPolyGlyphBltBlack    &=~
  78.     mfbPolyGlyphBltInvert    ^=
  79. */
  80.  
  81. void
  82. MFBPOLYGLYPHBLT(pDrawable, pGC, x, y, nglyph, ppci, pglyphBase)
  83.     DrawablePtr pDrawable;
  84.     GCPtr    pGC;
  85.     int     x, y;
  86.     unsigned int nglyph;
  87.     CharInfoPtr *ppci;        /* array of character info */
  88.     unsigned char *pglyphBase;    /* start of array of glyphs (unused in R5) */
  89. {
  90.     ExtentInfoRec info;    /* used by QueryGlyphExtents() */
  91.     BoxRec bbox;        /* string's bounding box */
  92.  
  93.     CharInfoPtr pci;
  94.     int xorg, yorg;    /* origin of drawable in bitmap */
  95.     int widthDst;    /* width of dst in longwords */
  96.  
  97.             /* these keep track of the character origin */
  98.     unsigned int *pdstBase;
  99.             /* points to longword with character origin */
  100.     int xchar;        /* xorigin of char (mod 32) */
  101.  
  102.             /* these are used for placing the glyph */
  103.     register int xoff;    /* x offset of left edge of glyph (mod 32) */
  104.     register unsigned int *pdst;
  105.             /* pointer to current longword in dst */
  106.  
  107.     int w;        /* width of glyph in bits */
  108.     int h;        /* height of glyph */
  109.     int widthGlyph;    /* width of glyph, in bytes */
  110.     register unsigned char *pglyph;
  111.             /* pointer to current row of glyph */
  112.  
  113.             /* used for putting down glyph */
  114.     register unsigned int tmpSrc;
  115.             /* for getting bits from glyph */
  116.     register int startmask;
  117.     register int endmask;
  118.     register int nFirst;/* bits of glyph in current longword */
  119.  
  120.     if (!(pGC->planemask & 1))
  121.     return;
  122.  
  123.     xorg = pDrawable->x;
  124.     yorg = pDrawable->y;
  125.     if (pDrawable->type == DRAWABLE_WINDOW)
  126.     {
  127.     pdstBase = (unsigned int *)
  128.         (((PixmapPtr)(pDrawable->pScreen->devPrivate))->devPrivate.ptr);
  129.     widthDst = (int)
  130.          (((PixmapPtr)(pDrawable->pScreen->devPrivate))->devKind) >> 2;
  131.     }
  132.     else
  133.     {
  134.     pdstBase = (unsigned int *)(((PixmapPtr)pDrawable)->devPrivate.ptr);
  135.     widthDst = (int)(((PixmapPtr)pDrawable)->devKind) >> 2;
  136.     }
  137.  
  138.     x += xorg;
  139.     y += yorg;
  140.  
  141.     QueryGlyphExtents(pGC->font, ppci, (unsigned long)nglyph, &info);
  142.     bbox.x1 = x + info.overallLeft;
  143.     bbox.x2 = x + info.overallRight;
  144.     bbox.y1 = y - info.overallAscent;
  145.     bbox.y2 = y + info.overallDescent;
  146.  
  147.     switch ((*pGC->pScreen->RectIn)(
  148.                 ((mfbPrivGC *)(pGC->devPrivates[mfbGCPrivateIndex].ptr))->pCompositeClip, &bbox))
  149.     {
  150.       case rgnOUT:
  151.     break;
  152.       case rgnIN:
  153.         pdstBase = pdstBase + (widthDst * y) + (x >> 5);
  154.         xchar = x & 0x1f;
  155.  
  156.         while(nglyph--)
  157.         {
  158.         pci = *ppci;
  159.         pglyph = FONTGLYPHBITS(pglyphBase, pci);
  160.         w = pci->metrics.rightSideBearing - pci->metrics.leftSideBearing;
  161.         h = pci->metrics.ascent + pci->metrics.descent;
  162.         widthGlyph = GLYPHWIDTHBYTESPADDED(pci);
  163.  
  164.         /* start at top scanline of glyph */
  165.         pdst = pdstBase - (pci->metrics.ascent * widthDst);
  166.  
  167.         /* find correct word in scanline and x offset within it
  168.            for left edge of glyph
  169.         */
  170.         xoff = xchar + pci->metrics.leftSideBearing;
  171.         if (xoff > 31)
  172.         {
  173.             pdst++;
  174.             xoff &= 0x1f;
  175.         }
  176.         else if (xoff < 0)
  177.         {
  178.             xoff += 32;
  179.             pdst--;
  180.         }
  181.  
  182.         if ((xoff + w) <= 32)
  183.         {
  184.             /* glyph all in one longword */
  185.             maskpartialbits(xoff, w, startmask);
  186.             while (h--)
  187.             {
  188.             getleftbits(pglyph, w, tmpSrc);
  189.             *pdst OPEQ (SCRRIGHT(tmpSrc, xoff) & startmask);
  190.             pglyph += widthGlyph;
  191.             pdst += widthDst;
  192.             }
  193.         }
  194.         else
  195.         {
  196.             /* glyph crosses longword boundary */
  197.             mask32bits(xoff, w, startmask, endmask);
  198.             nFirst = 32 - xoff;
  199.             while (h--)
  200.             {
  201.             getleftbits(pglyph, w, tmpSrc);
  202.             *pdst OPEQ (SCRRIGHT(tmpSrc, xoff) & startmask);
  203.             *(pdst+1) OPEQ (SCRLEFT(tmpSrc, nFirst) & endmask);
  204.             pglyph += widthGlyph;
  205.             pdst += widthDst;
  206.             }
  207.         } /* glyph crosses longwords boundary */
  208.  
  209.         /* update character origin */
  210.         x += pci->metrics.characterWidth;
  211.         xchar += pci->metrics.characterWidth;
  212.         if (xchar > 31)
  213.         {
  214.             xchar -= 32;
  215.             pdstBase++;
  216.         }
  217.         else if (xchar < 0)
  218.         {
  219.             xchar += 32;
  220.             pdstBase--;
  221.         }
  222.         ppci++;
  223.         } /* while nglyph-- */
  224.     break;
  225.       case rgnPART:
  226.       {
  227.     TEXTPOS *ppos;
  228.     RegionPtr cclip;
  229.     int nbox;
  230.     BoxPtr pbox;
  231.     int xpos;        /* x position of char origin */
  232.     int i;
  233.     BoxRec clip;
  234.     int leftEdge, rightEdge;
  235.     int topEdge, bottomEdge;
  236.     int glyphRow;        /* first row of glyph not wholly
  237.                    clipped out */
  238.     int glyphCol;        /* leftmost visible column of glyph */
  239.     int getWidth;        /* bits to get from glyph */
  240.  
  241.     if(!(ppos = (TEXTPOS *)ALLOCATE_LOCAL(nglyph * sizeof(TEXTPOS))))
  242.         return;
  243.  
  244.         pdstBase = pdstBase + (widthDst * y) + (x >> 5);
  245.         xpos = x;
  246.     xchar = xpos & 0x1f;
  247.  
  248.     for (i=0; i<nglyph; i++)
  249.     {
  250.         pci = ppci[i];
  251.  
  252.         ppos[i].xpos = xpos;
  253.         ppos[i].xchar = xchar;
  254.         ppos[i].leftEdge = xpos + pci->metrics.leftSideBearing;
  255.         ppos[i].rightEdge = xpos + pci->metrics.rightSideBearing;
  256.         ppos[i].topEdge = y - pci->metrics.ascent;
  257.         ppos[i].bottomEdge = y + pci->metrics.descent;
  258.         ppos[i].pdstBase = pdstBase;
  259.         ppos[i].widthGlyph = GLYPHWIDTHBYTESPADDED(pci);
  260.  
  261.         xpos += pci->metrics.characterWidth;
  262.         xchar += pci->metrics.characterWidth;
  263.         if (xchar > 31)
  264.         {
  265.         xchar &= 0x1f;
  266.         pdstBase++;
  267.         }
  268.         else if (xchar < 0)
  269.         {
  270.         xchar += 32;
  271.         pdstBase--;
  272.         }
  273.     }
  274.  
  275.     cclip = ((mfbPrivGC *)(pGC->devPrivates[mfbGCPrivateIndex].ptr))->pCompositeClip;
  276.     pbox = REGION_RECTS(cclip);
  277.     nbox = REGION_NUM_RECTS(cclip);
  278.  
  279.     for (; --nbox >= 0; pbox++)
  280.     {
  281.         clip.x1 = max(bbox.x1, pbox->x1);
  282.         clip.y1 = max(bbox.y1, pbox->y1);
  283.         clip.x2 = min(bbox.x2, pbox->x2);
  284.         clip.y2 = min(bbox.y2, pbox->y2);
  285.         if ((clip.x2<=clip.x1) || (clip.y2<=clip.y1))
  286.         continue;
  287.  
  288.         for(i=0; i<nglyph; i++)
  289.         {
  290.         pci = ppci[i];
  291.         xchar = ppos[i].xchar;
  292.  
  293.         /* clip the left and right edges */
  294.         if (ppos[i].leftEdge < clip.x1)
  295.             leftEdge = clip.x1;
  296.         else
  297.             leftEdge = ppos[i].leftEdge;
  298.  
  299.         if (ppos[i].rightEdge > clip.x2)
  300.             rightEdge = clip.x2;
  301.         else
  302.             rightEdge = ppos[i].rightEdge;
  303.  
  304.         w = rightEdge - leftEdge;
  305.         if (w <= 0)
  306.             continue;
  307.  
  308.         /* clip the top and bottom edges */
  309.         if (ppos[i].topEdge < clip.y1)
  310.             topEdge = clip.y1;
  311.         else
  312.             topEdge = ppos[i].topEdge;
  313.  
  314.         if (ppos[i].bottomEdge > clip.y2)
  315.             bottomEdge = clip.y2;
  316.         else
  317.             bottomEdge = ppos[i].bottomEdge;
  318.  
  319.         h = bottomEdge - topEdge;
  320.         if (h <= 0)
  321.             continue;
  322.  
  323.         glyphRow = (topEdge - y) + pci->metrics.ascent;
  324.         widthGlyph = ppos[i].widthGlyph;
  325.         pglyph = FONTGLYPHBITS(pglyphBase, pci);
  326.         pglyph += (glyphRow * widthGlyph);
  327.  
  328.         pdst = ppos[i].pdstBase - ((y-topEdge) * widthDst);
  329.  
  330.         glyphCol = (leftEdge - ppos[i].xpos) -
  331.                (pci->metrics.leftSideBearing);
  332.         getWidth = w + glyphCol;
  333.         xoff = xchar + (leftEdge - ppos[i].xpos);
  334.         if (xoff > 31)
  335.         {
  336.             xoff &= 0x1f;
  337.             pdst++;
  338.         }
  339.         else if (xoff < 0)
  340.         {
  341.             xoff += 32;
  342.             pdst--;
  343.         }
  344.  
  345.         if ((xoff + w) <= 32)
  346.         {
  347.             maskpartialbits(xoff, w, startmask);
  348.             while (h--)
  349.             {
  350.             getshiftedleftbits(pglyph, glyphCol, getWidth, tmpSrc);
  351.             *pdst OPEQ (SCRRIGHT(tmpSrc, xoff) & startmask);
  352.             pglyph += widthGlyph;
  353.             pdst += widthDst;
  354.             }
  355.         }
  356.         else
  357.         {
  358.             mask32bits(xoff, w, startmask, endmask);
  359.             nFirst = 32 - xoff;
  360.             while (h--)
  361.             {
  362.             getshiftedleftbits(pglyph, glyphCol, getWidth, tmpSrc);
  363.             *pdst OPEQ (SCRRIGHT(tmpSrc, xoff) & startmask);
  364.             *(pdst+1) OPEQ (SCRLEFT(tmpSrc, nFirst) & endmask);
  365.             pglyph += widthGlyph;
  366.             pdst += widthDst;
  367.             }
  368.         }
  369.         } /* for each glyph */
  370.     } /* while nbox-- */
  371.     DEALLOCATE_LOCAL(ppos);
  372.     break;
  373.       }
  374.       default:
  375.     break;
  376.     }
  377. }
  378.  
  379.  
  380.