First import
[xorg_rtime.git] / xorg-server-1.4 / afb / afbplygblt.c
blob289d50e1b12679d75777603be49e968e46d152f0
1 /* Combined Purdue/PurduePlus patches, level 2.0, 1/17/89 */
2 /***********************************************************
4 Copyright (c) 1987 X Consortium
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:
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
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.
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.
28 Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
30 All Rights Reserved
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.
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.
48 ******************************************************************/
50 #ifdef HAVE_DIX_CONFIG_H
51 #include <dix-config.h>
52 #endif
54 #include <X11/X.h>
55 #include <X11/Xmd.h>
56 #include <X11/Xproto.h>
57 #include "afb.h"
58 #include <X11/fonts/fontstruct.h>
59 #include "dixfontstr.h"
60 #include "gcstruct.h"
61 #include "windowstr.h"
62 #include "scrnintstr.h"
63 #include "pixmapstr.h"
64 #include "regionstr.h"
65 #include "maskbits.h"
68 this works for fonts with glyphs <= 32 bits wide.
70 the clipping calculations are done for worst-case fonts.
71 we make no assumptions about the heights, widths, or bearings
72 of the glyphs. if we knew that the glyphs are all the same height,
73 we could clip the tops and bottoms per clipping box, rather
74 than per character per clipping box. if we knew that the glyphs'
75 left and right bearings were wlle-behaved, we could clip a single
76 character at the start, output until the last unclipped
77 character, and then clip the last one. this is all straightforward
78 to determine based on max-bounds and min-bounds from the font.
79 there is some inefficiency introduced in the per-character
80 clipping to make what's going on clearer.
82 (it is possible, for example, for a font to be defined in which the
83 next-to-last character in a font would be clipped out, but the last
84 one wouldn't. the code below deals with this.)
88 void
89 afbPolyGlyphBlt (pDrawable, pGC, x, y, nglyph, ppci, pglyphBase)
90 DrawablePtr pDrawable;
91 GC *pGC;
92 int x, y;
93 unsigned int nglyph;
94 CharInfoPtr *ppci; /* array of character info */
95 pointer pglyphBase; /* start of array of glyphs */
97 ExtentInfoRec info; /* used by QueryGlyphExtents() */
98 BoxRec bbox; /* string's bounding box */
100 CharInfoPtr pci;
101 int xorg, yorg; /* origin of drawable in bitmap */
102 int widthDst; /* width of dst in longwords */
104 /* these keep track of the character origin */
105 PixelType *pdstBase;
106 /* points to longword with character origin */
107 int xchar; /* xorigin of char (mod 32) */
109 /* these are used for placing the glyph */
110 register int xoff; /* x offset of left edge of glyph (mod 32) */
111 register PixelType *pdst; /* pointer to current longword in dst */
113 register int d;
114 int depthDst;
115 int sizeDst;
116 int hSave;
117 int w; /* width of glyph in bits */
118 int h; /* height of glyph */
119 int widthGlyph; /* width of glyph, in bytes */
120 unsigned char *rrops;
121 register unsigned char *pglyph;
122 /* pointer to current row of glyph */
123 unsigned char *pglyphSave;
125 /* used for putting down glyph */
126 register PixelType tmpSrc;
127 /* for getting bits from glyph */
128 register PixelType startmask;
129 register PixelType endmask;
131 register int nFirst; /* bits of glyph in current longword */
132 PixelType *pdstSave;
134 xorg = pDrawable->x;
135 yorg = pDrawable->y;
136 afbGetPixelWidthSizeDepthAndPointer(pDrawable, widthDst, sizeDst, depthDst,
137 pdstBase);
139 QueryGlyphExtents(pGC->font, ppci, (unsigned long)nglyph, &info);
141 x += xorg;
142 y += yorg;
144 bbox.x1 = x + info.overallLeft;
145 bbox.x2 = x + info.overallRight;
146 bbox.y1 = y - info.overallAscent;
147 bbox.y2 = y + info.overallDescent;
149 rrops = ((afbPrivGCPtr) pGC->devPrivates[afbGCPrivateIndex].ptr)->rrops;
151 switch (RECT_IN_REGION(pGC->pScreen, pGC->pCompositeClip, &bbox)) {
152 case rgnOUT:
153 break;
154 case rgnIN:
155 pdstBase = afbScanlineNoBankSwitch(pdstBase, x, y, widthDst);
156 xchar = x & PIM;
158 while(nglyph--) {
159 pci = *ppci;
160 pglyphSave = FONTGLYPHBITS(pglyphBase, pci);
161 w = pci->metrics.rightSideBearing - pci->metrics.leftSideBearing;
162 hSave = pci->metrics.ascent + pci->metrics.descent;
163 widthGlyph = GLYPHWIDTHBYTESPADDED(pci);
164 /* start at top scanline of glyph */
165 pdstSave = afbScanlineDelta(pdstBase, -pci->metrics.ascent,
166 widthDst);
168 /* find correct word in scanline and x offset within it
169 for left edge of glyph
171 xoff = xchar + pci->metrics.leftSideBearing;
172 if (xoff > PLST) {
173 pdstSave++;
174 xoff &= PIM;
175 } else if (xoff < 0) {
176 xoff += PPW;
177 pdstSave--;
180 for (d = 0; d < depthDst; d++) {
181 h = hSave;
182 pdst = pdstSave;
183 pdstSave += sizeDst; /* @@@ NEXT PLANE @@@ */
184 pglyph = pglyphSave;
186 if ((xoff + w) <= PPW) {
187 /* glyph all in one longword */
188 maskpartialbits(xoff, w, startmask);
189 switch (rrops[d]) {
190 case RROP_WHITE:
191 while (h--) {
192 getleftbits(pglyph, w, tmpSrc);
193 *pdst |= (SCRRIGHT(tmpSrc, xoff) & startmask);
194 pglyph += widthGlyph;
195 afbScanlineInc(pdst, widthDst);
197 break;
198 case RROP_BLACK:
199 while (h--) {
200 getleftbits(pglyph, w, tmpSrc);
201 *pdst &= ~(SCRRIGHT(tmpSrc, xoff) & startmask);
202 pglyph += widthGlyph;
203 afbScanlineInc(pdst, widthDst);
205 break;
206 case RROP_INVERT:
207 while (h--) {
208 getleftbits(pglyph, w, tmpSrc);
209 *pdst ^= (SCRRIGHT(tmpSrc, xoff) & startmask);
210 pglyph += widthGlyph;
211 afbScanlineInc(pdst, widthDst);
213 break;
215 } else {
216 /* glyph crosses longword boundary */
217 maskPPWbits(xoff, w, startmask, endmask);
218 nFirst = PPW - xoff;
219 switch (rrops[d]) {
220 case RROP_WHITE:
221 while (h--) {
222 getleftbits(pglyph, w, tmpSrc);
223 *pdst |= (SCRRIGHT(tmpSrc, xoff) & startmask);
224 *(pdst+1) |= (SCRLEFT(tmpSrc, nFirst) & endmask);
225 pglyph += widthGlyph;
226 afbScanlineInc(pdst, widthDst);
228 break;
229 case RROP_BLACK:
230 while (h--) {
231 getleftbits(pglyph, w, tmpSrc);
232 *pdst &= ~(SCRRIGHT(tmpSrc, xoff) & startmask);
233 *(pdst+1) &= ~(SCRLEFT(tmpSrc, nFirst) & endmask);
234 pglyph += widthGlyph;
235 afbScanlineInc(pdst, widthDst);
237 break;
238 case RROP_INVERT:
239 while (h--) {
240 getleftbits(pglyph, w, tmpSrc);
241 *pdst ^= (SCRRIGHT(tmpSrc, xoff) & startmask);
242 *(pdst+1) ^= (SCRLEFT(tmpSrc, nFirst) & endmask);
243 pglyph += widthGlyph;
244 afbScanlineInc(pdst, widthDst);
246 break;
248 } /* glyph crosses longwords boundary */
249 } /* depth loop */
250 /* update character origin */
251 x += pci->metrics.characterWidth;
252 xchar += pci->metrics.characterWidth;
253 if (xchar > PLST) {
254 xchar -= PPW;
255 pdstBase++;
256 } else if (xchar < 0) {
257 xchar += PPW;
258 pdstBase--;
260 ppci++;
261 } /* while nglyph-- */
262 break;
263 case rgnPART:
265 afbTEXTPOS *ppos;
266 int nbox;
267 BoxPtr pbox;
268 RegionPtr cclip;
269 int xpos; /* x position of char origin */
270 int i;
271 BoxRec clip;
272 int leftEdge, rightEdge;
273 int topEdge, bottomEdge;
274 int glyphRow; /* first row of glyph not wholly
275 clipped out */
276 int glyphCol; /* leftmost visible column of glyph */
277 #if GETLEFTBITS_ALIGNMENT > 1
278 int getWidth; /* bits to get from glyph */
279 #endif
281 if(!(ppos = (afbTEXTPOS *)ALLOCATE_LOCAL(nglyph * sizeof(afbTEXTPOS))))
282 return;
284 pdstBase = afbScanlineNoBankSwitch(pdstBase, x, y, widthDst);
285 xpos = x;
286 xchar = xpos & PIM;
288 for (i = 0; i < nglyph; i++) {
289 pci = ppci[i];
291 ppos[i].xpos = xpos;
292 ppos[i].xchar = xchar;
293 ppos[i].leftEdge = xpos + pci->metrics.leftSideBearing;
294 ppos[i].rightEdge = xpos + pci->metrics.rightSideBearing;
295 ppos[i].topEdge = y - pci->metrics.ascent;
296 ppos[i].bottomEdge = y + pci->metrics.descent;
297 ppos[i].pdstBase = pdstBase;
298 ppos[i].widthGlyph = GLYPHWIDTHBYTESPADDED(pci);
300 xpos += pci->metrics.characterWidth;
301 xchar += pci->metrics.characterWidth;
302 if (xchar > PLST) {
303 xchar &= PIM;
304 pdstBase++;
305 } else if (xchar < 0) {
306 xchar += PPW;
307 pdstBase--;
311 cclip = pGC->pCompositeClip;
312 pbox = REGION_RECTS(cclip);
313 nbox = REGION_NUM_RECTS(cclip);
315 /* HACK ALERT
316 since we continue out of the loop below so often, it
317 is easier to increment pbox at the top than at the end.
318 don't try this at home.
320 pbox--;
321 while(nbox--) {
322 pbox++;
323 clip.x1 = max(bbox.x1, pbox->x1);
324 clip.y1 = max(bbox.y1, pbox->y1);
325 clip.x2 = min(bbox.x2, pbox->x2);
326 clip.y2 = min(bbox.y2, pbox->y2);
327 if ((clip.x2<=clip.x1) || (clip.y2<=clip.y1))
328 continue;
330 for(i=0; i<nglyph; i++) {
331 pci = ppci[i];
332 xchar = ppos[i].xchar;
334 /* clip the left and right edges */
335 if (ppos[i].leftEdge < clip.x1)
336 leftEdge = clip.x1;
337 else
338 leftEdge = ppos[i].leftEdge;
340 if (ppos[i].rightEdge > clip.x2)
341 rightEdge = clip.x2;
342 else
343 rightEdge = ppos[i].rightEdge;
345 w = rightEdge - leftEdge;
346 if (w <= 0)
347 continue;
349 /* clip the top and bottom edges */
350 if (ppos[i].topEdge < clip.y1)
351 topEdge = clip.y1;
352 else
353 topEdge = ppos[i].topEdge;
355 if (ppos[i].bottomEdge > clip.y2)
356 bottomEdge = clip.y2;
357 else
358 bottomEdge = ppos[i].bottomEdge;
360 hSave = bottomEdge - topEdge;
361 if (hSave <= 0)
362 continue;
364 glyphRow = (topEdge - y) + pci->metrics.ascent;
365 widthGlyph = ppos[i].widthGlyph;
366 pglyphSave = FONTGLYPHBITS(pglyphBase, pci);
367 pglyphSave += (glyphRow * widthGlyph);
369 glyphCol = (leftEdge - ppos[i].xpos) -
370 (pci->metrics.leftSideBearing);
371 #if GETLEFTBITS_ALIGNMENT > 1
372 getWidth = w + glyphCol;
373 #endif
375 pdstSave = afbScanlineDelta(ppos[i].pdstBase, -(y-topEdge),
376 widthDst);
377 xoff = xchar + (leftEdge - ppos[i].xpos);
378 if (xoff > PLST) {
379 xoff &= PIM;
380 pdstSave++;
381 } else if (xoff < 0) {
382 xoff += PPW;
383 pdstSave--;
386 for (d = 0; d < depthDst; d++) {
387 h = hSave;
388 pdst = pdstSave;
389 pdstSave += sizeDst; /* @@@ NEXT PLANE @@@ */
390 pglyph = pglyphSave;
392 if ((xoff + w) <= PPW) {
393 maskpartialbits(xoff, w, startmask);
394 switch (rrops[d]) {
395 case RROP_WHITE:
396 while (h--) {
397 getshiftedleftbits(pglyph, glyphCol, getWidth, tmpSrc);
398 *pdst |= (SCRRIGHT(tmpSrc, xoff) & startmask);
399 pglyph += widthGlyph;
400 afbScanlineInc(pdst, widthDst);
402 break;
403 case RROP_BLACK:
404 while (h--) {
405 getshiftedleftbits(pglyph, glyphCol, getWidth, tmpSrc);
406 *pdst &= ~(SCRRIGHT(tmpSrc, xoff) & startmask);
407 pglyph += widthGlyph;
408 afbScanlineInc(pdst, widthDst);
410 break;
411 case RROP_INVERT:
412 while (h--) {
413 getshiftedleftbits(pglyph, glyphCol, getWidth, tmpSrc);
414 *pdst ^= (SCRRIGHT(tmpSrc, xoff) & startmask);
415 pglyph += widthGlyph;
416 afbScanlineInc(pdst, widthDst);
418 break;
420 } else {
421 maskPPWbits(xoff, w, startmask, endmask);
422 nFirst = PPW - xoff;
423 switch (rrops[d]) {
424 case RROP_WHITE:
425 while (h--) {
426 getshiftedleftbits(pglyph, glyphCol, getWidth, tmpSrc);
427 *pdst |= (SCRRIGHT(tmpSrc, xoff) & startmask);
428 *(pdst+1) |= (SCRLEFT(tmpSrc, nFirst) & endmask);
429 pglyph += widthGlyph;
430 afbScanlineInc(pdst, widthDst);
432 break;
433 case RROP_BLACK:
434 while (h--) {
435 getshiftedleftbits(pglyph, glyphCol, getWidth, tmpSrc);
436 *pdst &= ~(SCRRIGHT(tmpSrc, xoff) & startmask);
437 *(pdst+1) &= ~(SCRLEFT(tmpSrc, nFirst) & endmask);
438 pglyph += widthGlyph;
439 afbScanlineInc(pdst, widthDst);
441 break;
442 case RROP_INVERT:
443 while (h--) {
444 getshiftedleftbits(pglyph, glyphCol, getWidth, tmpSrc);
445 *pdst ^= (SCRRIGHT(tmpSrc, xoff) & startmask);
446 *(pdst+1) ^= (SCRLEFT(tmpSrc, nFirst) & endmask);
447 pglyph += widthGlyph;
448 afbScanlineInc(pdst, widthDst);
450 break;
453 } /* depth */
454 } /* for each glyph */
455 } /* while nbox-- */
456 DEALLOCATE_LOCAL(ppos);
457 break;
460 default:
461 break;