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