home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / ddx / mfb / mfbimggblt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-25  |  12.4 KB  |  428 lines

  1. /* Combined Purdue/PurduePlus patches, level 2.0, 1/17/89 */
  2. /***********************************************************
  3. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
  4. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  5.  
  6.                         All Rights Reserved
  7.  
  8. Permission to use, copy, modify, and distribute this software and its 
  9. documentation for any purpose and without fee is hereby granted, 
  10. provided that the above copyright notice appear in all copies and that
  11. both that copyright notice and this permission notice appear in 
  12. supporting documentation, and that the names of Digital or MIT not be
  13. used in advertising or publicity pertaining to distribution of the
  14. software without specific, written prior permission.  
  15.  
  16. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  17. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  18. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  19. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  21. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  22. SOFTWARE.
  23.  
  24. ******************************************************************/
  25. /* $XConsortium: mfbimggblt.c,v 5.6 91/05/26 09:21:19 rws Exp $ */
  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    "scrnintstr.h"
  35. #include    "pixmapstr.h"
  36. #include    "regionstr.h"
  37. #include    "maskbits.h"
  38.  
  39. extern void QueryGlyphExtents();
  40.  
  41. /*
  42.     we should eventually special-case fixed-width fonts for ImageText.
  43.  
  44.     this works for fonts with glyphs <= 32 bits wide.
  45.  
  46.     the clipping calculations are done for worst-case fonts.
  47. we make no assumptions about the heights, widths, or bearings
  48. of the glyphs.  if we knew that the glyphs are all the same height,
  49. we could clip the tops and bottoms per clipping box, rather
  50. than per character per clipping box.  if we knew that the glyphs'
  51. left and right bearings were wlle-behaved, we could clip a single
  52. character at the start, output until the last unclipped
  53. character, and then clip the last one.  this is all straightforward
  54. to determine based on max-bounds and min-bounds from the font.
  55.     there is some inefficiency introduced in the per-character
  56. clipping to make what's going on clearer.
  57.  
  58.     (it is possible, for example, for a font to be defined in which the
  59. next-to-last character in a font would be clipped out, but the last
  60. one wouldn't.  the code below deals with this.)
  61.  
  62.     Image text looks at the bits in the glyph and the fg and bg in the
  63. GC.  it paints a rectangle, as defined in the protocol dcoument,
  64. and the paints the characters.
  65.  
  66.    to avoid source proliferation, this file is compiled
  67. three times:
  68.     MFBIMAGEGLYPHBLT    OPEQ
  69.     mfbImageGlyphBltWhite    |=
  70.     mfbImageGlyphBltBlack    &=~
  71.  
  72.     the register allocations for startmask and endmask may not
  73. be the right thing.  are there two other deserving candidates?
  74. xoff, pdst, pglyph, and tmpSrc seem like the right things, though.
  75. */
  76.  
  77. void
  78. MFBIMAGEGLYPHBLT(pDrawable, pGC, x, y, nglyph, ppci, pglyphBase)
  79.     DrawablePtr pDrawable;
  80.     GC         *pGC;
  81.     int     x, y;
  82.     unsigned int nglyph;
  83.     CharInfoPtr *ppci;        /* array of character info */
  84.     unsigned char *pglyphBase;    /* start of array of glyphs */
  85. {
  86.     ExtentInfoRec info;    /* used by QueryGlyphExtents() */
  87.     BoxRec bbox;    /* string's bounding box */
  88.     xRectangle backrect;/* backing rectangle to paint.
  89.                in the general case, NOT necessarily
  90.                the same as the string's bounding box
  91.             */
  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.  
  119.     register int nFirst;/* bits of glyph in current longword */
  120.     void (* oldFillArea)();
  121.             /* we might temporarily usurp this
  122.                field in devPriv */
  123.  
  124.     if (!(pGC->planemask & 1))
  125.     return;
  126.  
  127.     xorg = pDrawable->x;
  128.     yorg = pDrawable->y;
  129.     if (pDrawable->type == DRAWABLE_WINDOW)
  130.     {
  131.     pdstBase = (unsigned int *)
  132.         (((PixmapPtr)(pDrawable->pScreen->devPrivate))->devPrivate.ptr);
  133.     widthDst = (int)
  134.          (((PixmapPtr)(pDrawable->pScreen->devPrivate))->devKind) >> 2;
  135.     }
  136.     else
  137.     {
  138.     pdstBase = (unsigned int *)(((PixmapPtr)pDrawable)->devPrivate.ptr);
  139.     widthDst = (int)(((PixmapPtr)pDrawable)->devKind) >> 2;
  140.     }
  141.  
  142.     QueryGlyphExtents(pGC->font, ppci, (unsigned long)nglyph, &info);
  143.  
  144.     backrect.x = x;
  145.     backrect.y = y - FONTASCENT(pGC->font);
  146.     backrect.width = info.overallWidth;
  147.     backrect.height = FONTASCENT(pGC->font) + FONTDESCENT(pGC->font);
  148.  
  149.     x += xorg;
  150.     y += yorg;
  151.  
  152.     bbox.x1 = x + info.overallLeft;
  153.     bbox.x2 = x + info.overallRight;
  154.     bbox.y1 = y - info.overallAscent;
  155.     bbox.y2 = y + info.overallDescent;
  156.  
  157.     /* UNCLEAN CODE
  158.        we know the mfbPolyFillRect uses only three fields in
  159.        devPrivate[mfbGCPrivateIndex].ptr, two of which (the rotated
  160.        tile/stipple and the ropFillArea) are 
  161.        irrelevant for solid filling, so we just poke the FillArea
  162.        field.  the GC is now in an inconsistent state, but we'll fix
  163.        it as soon as PolyFillRect returns.  fortunately, the server
  164.        is single threaded.
  165.  
  166.     NOTE:
  167.        if you are not using the standard mfbFillRectangle code, you
  168.        need to poke any fields in the GC the rectangle stuff need
  169.        (probably alu, fgPixel, and fillStyle) and in devPrivate[mfbGCPrivateIndex].ptr
  170.        (probably rop or ropFillArea.)  You could just call ValidateGC,
  171.        but that is usually not a cheap thing to do.
  172.     */
  173.  
  174.     oldFillArea = ((mfbPrivGC *)(pGC->devPrivates[mfbGCPrivateIndex].ptr))->FillArea;
  175.  
  176. /* pcc doesn't like this.  why?
  177.     ((mfbPrivGC *)(pGC->devPrivates[mfbGCPrivateIndex].ptr))->FillArea = 
  178.             ((pGC->bgPixel & 1) ? mfbSolidWhiteArea : mfbSolidBlackArea);
  179. */
  180.     if (pGC->bgPixel & 1)
  181.         ((mfbPrivGC *)(pGC->devPrivates[mfbGCPrivateIndex].ptr))->FillArea = mfbSolidWhiteArea;
  182.     else
  183.         ((mfbPrivGC *)(pGC->devPrivates[mfbGCPrivateIndex].ptr))->FillArea = mfbSolidBlackArea;
  184.  
  185.     mfbPolyFillRect(pDrawable, pGC, 1, &backrect);
  186.     ((mfbPrivGC *)(pGC->devPrivates[mfbGCPrivateIndex].ptr))->FillArea = oldFillArea;
  187.  
  188.     /* the faint-hearted can open their eyes now */
  189.     switch ((*pGC->pScreen->RectIn)(
  190.           ((mfbPrivGC *)(pGC->devPrivates[mfbGCPrivateIndex].ptr))->pCompositeClip, &bbox))
  191.     {
  192.       case rgnOUT:
  193.     break;
  194.       case rgnIN:
  195.         pdstBase = pdstBase + (widthDst * y) + (x >> 5);
  196.         xchar = x & 0x1f;
  197.  
  198.         while(nglyph--)
  199.         {
  200.         pci = *ppci;
  201.         pglyph = FONTGLYPHBITS(pglyphBase, pci);
  202.         w = pci->metrics.rightSideBearing - pci->metrics.leftSideBearing;
  203.         h = pci->metrics.ascent + pci->metrics.descent;
  204.         widthGlyph = GLYPHWIDTHBYTESPADDED(pci);
  205.  
  206.         /* start at top scanline of glyph */
  207.         pdst = pdstBase - (pci->metrics.ascent * widthDst);
  208.  
  209.         /* find correct word in scanline and x offset within it
  210.            for left edge of glyph
  211.         */
  212.         xoff = xchar + pci->metrics.leftSideBearing;
  213.         if (xoff > 31)
  214.         {
  215.             pdst++;
  216.             xoff &= 0x1f;
  217.         }
  218.         else if (xoff < 0)
  219.         {
  220.             xoff += 32;
  221.             pdst--;
  222.         }
  223.  
  224.         if ((xoff + w) <= 32)
  225.         {
  226.             /* glyph all in one longword */
  227.             maskpartialbits(xoff, w, startmask);
  228.             while (h--)
  229.             {
  230.             getleftbits(pglyph, w, tmpSrc);
  231.             *pdst OPEQ (SCRRIGHT(tmpSrc, xoff) & startmask);
  232.             pglyph += widthGlyph;
  233.             pdst += widthDst;
  234.             }
  235.         }
  236.         else
  237.         {
  238.             /* glyph crosses longword boundary */
  239.             mask32bits(xoff, w, startmask, endmask);
  240.             nFirst = 32 - xoff;
  241.             while (h--)
  242.             {
  243.             getleftbits(pglyph, w, tmpSrc);
  244.             *pdst OPEQ (SCRRIGHT(tmpSrc, xoff) & startmask);
  245.             *(pdst+1) OPEQ (SCRLEFT(tmpSrc, nFirst) & endmask);
  246.             pglyph += widthGlyph;
  247.             pdst += widthDst;
  248.             }
  249.         } /* glyph crosses longwords boundary */
  250.  
  251.         /* update character origin */
  252.         x += pci->metrics.characterWidth;
  253.         xchar += pci->metrics.characterWidth;
  254.         if (xchar > 31)
  255.         {
  256.             xchar -= 32;
  257.             pdstBase++;
  258.         }
  259.         else if (xchar < 0)
  260.         {
  261.             xchar += 32;
  262.             pdstBase--;
  263.         }
  264.         ppci++;
  265.         } /* while nglyph-- */
  266.     break;
  267.       case rgnPART:
  268.       {
  269.     TEXTPOS *ppos;
  270.     int nbox;
  271.     BoxPtr pbox;
  272.     RegionPtr cclip;
  273.     int xpos;        /* x position of char origin */
  274.     int i;
  275.     BoxRec clip;
  276.     int leftEdge, rightEdge;
  277.     int topEdge, bottomEdge;
  278.     int glyphRow;        /* first row of glyph not wholly
  279.                    clipped out */
  280.     int glyphCol;        /* leftmost visible column of glyph */
  281.     int getWidth;        /* bits to get from glyph */
  282.  
  283.     if(!(ppos = (TEXTPOS *)ALLOCATE_LOCAL(nglyph * sizeof(TEXTPOS))))
  284.         return;
  285.  
  286.         pdstBase = pdstBase + (widthDst * y) + (x >> 5);
  287.         xpos = x;
  288.     xchar = xpos & 0x1f;
  289.  
  290.     for (i=0; i<nglyph; i++)
  291.     {
  292.         pci = ppci[i];
  293.  
  294.         ppos[i].xpos = xpos;
  295.         ppos[i].xchar = xchar;
  296.         ppos[i].leftEdge = xpos + pci->metrics.leftSideBearing;
  297.         ppos[i].rightEdge = xpos + pci->metrics.rightSideBearing;
  298.         ppos[i].topEdge = y - pci->metrics.ascent;
  299.         ppos[i].bottomEdge = y + pci->metrics.descent;
  300.         ppos[i].pdstBase = pdstBase;
  301.         ppos[i].widthGlyph = GLYPHWIDTHBYTESPADDED(pci);
  302.  
  303.         xpos += pci->metrics.characterWidth;
  304.         xchar += pci->metrics.characterWidth;
  305.         if (xchar > 31)
  306.         {
  307.         xchar &= 0x1f;
  308.         pdstBase++;
  309.         }
  310.         else if (xchar < 0)
  311.         {
  312.         xchar += 32;
  313.         pdstBase--;
  314.         }
  315.     }
  316.  
  317.     cclip = ((mfbPrivGC *)(pGC->devPrivates[mfbGCPrivateIndex].ptr))->pCompositeClip;
  318.     pbox = REGION_RECTS(cclip);
  319.     nbox = REGION_NUM_RECTS(cclip);
  320.  
  321.     /* HACK ALERT
  322.        since we continue out of the loop below so often, it
  323.        is easier to increment pbox at the  top than at the end.
  324.        don't try this at home.
  325.     */
  326.     pbox--;
  327.     while(nbox--)
  328.     {
  329.         pbox++;
  330.         clip.x1 = max(bbox.x1, pbox->x1);
  331.         clip.y1 = max(bbox.y1, pbox->y1);
  332.         clip.x2 = min(bbox.x2, pbox->x2);
  333.         clip.y2 = min(bbox.y2, pbox->y2);
  334.         if ((clip.x2<=clip.x1) || (clip.y2<=clip.y1))
  335.         continue;
  336.  
  337.         for(i=0; i<nglyph; i++)
  338.         {
  339.         pci = ppci[i];
  340.         xchar = ppos[i].xchar;
  341.  
  342.         /* clip the left and right edges */
  343.         if (ppos[i].leftEdge < clip.x1)
  344.             leftEdge = clip.x1;
  345.         else
  346.             leftEdge = ppos[i].leftEdge;
  347.  
  348.         if (ppos[i].rightEdge > clip.x2)
  349.             rightEdge = clip.x2;
  350.         else
  351.             rightEdge = ppos[i].rightEdge;
  352.  
  353.         w = rightEdge - leftEdge;
  354.         if (w <= 0)
  355.             continue;
  356.  
  357.         /* clip the top and bottom edges */
  358.         if (ppos[i].topEdge < clip.y1)
  359.             topEdge = clip.y1;
  360.         else
  361.             topEdge = ppos[i].topEdge;
  362.  
  363.         if (ppos[i].bottomEdge > clip.y2)
  364.             bottomEdge = clip.y2;
  365.         else
  366.             bottomEdge = ppos[i].bottomEdge;
  367.  
  368.         h = bottomEdge - topEdge;
  369.         if (h <= 0)
  370.             continue;
  371.  
  372.         glyphRow = (topEdge - y) + pci->metrics.ascent;
  373.         widthGlyph = ppos[i].widthGlyph;
  374.         pglyph = FONTGLYPHBITS(pglyphBase, pci);
  375.         pglyph += (glyphRow * widthGlyph);
  376.  
  377.         pdst = ppos[i].pdstBase - ((y-topEdge) * widthDst);
  378.  
  379.         glyphCol = (leftEdge - ppos[i].xpos) -
  380.                (pci->metrics.leftSideBearing);
  381.         getWidth = w + glyphCol;
  382.         xoff = xchar + (leftEdge - ppos[i].xpos);
  383.         if (xoff > 31)
  384.         {
  385.             xoff &= 0x1f;
  386.             pdst++;
  387.         }
  388.         else if (xoff < 0)
  389.         {
  390.             xoff += 32;
  391.             pdst--;
  392.         }
  393.  
  394.         if ((xoff + w) <= 32)
  395.         {
  396.             maskpartialbits(xoff, w, startmask);
  397.             while (h--)
  398.             {
  399.             getshiftedleftbits(pglyph, glyphCol, getWidth, tmpSrc);
  400.             *pdst OPEQ (SCRRIGHT(tmpSrc, xoff) & startmask);
  401.             pglyph += widthGlyph;
  402.             pdst += widthDst;
  403.             }
  404.         }
  405.         else
  406.         {
  407.             mask32bits(xoff, w, startmask, endmask);
  408.             nFirst = 32 - xoff;
  409.             while (h--)
  410.             {
  411.             getshiftedleftbits(pglyph, glyphCol, getWidth, tmpSrc);
  412.             *pdst OPEQ (SCRRIGHT(tmpSrc, xoff) & startmask);
  413.             *(pdst+1) OPEQ (SCRLEFT(tmpSrc, nFirst) & endmask);
  414.             pglyph += widthGlyph;
  415.             pdst += widthDst;
  416.             }
  417.         }
  418.         } /* for each glyph */
  419.     } /* while nbox-- */
  420.     DEALLOCATE_LOCAL(ppos);
  421.     break;
  422.       }
  423.       default:
  424.     break;
  425.     }
  426. }
  427.  
  428.