Fixed bug in TEXT_WordBreak that was variously throwing Lotus Notes
[wine] / dlls / x11drv / xrender.c
1 /*
2  * Functions to use the XRender extension
3  *
4  * Copyright 2001 Huw D M Davies for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 #include "config.h"
21 #include "wine/port.h"
22
23 #include <assert.h>
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include "winnt.h"
28 #include "x11drv.h"
29 #include "bitmap.h"
30 #include "region.h"
31 #include "wine/unicode.h"
32 #include "wine/debug.h"
33
34 BOOL X11DRV_XRender_Installed = FALSE;
35 WINE_DEFAULT_DEBUG_CHANNEL(xrender);
36
37 #ifdef HAVE_X11_EXTENSIONS_XRENDER_H
38
39 #include "ts_xlib.h"
40 #include <X11/extensions/Xrender.h>
41
42 static XRenderPictFormat *screen_format; /* format of screen */
43 static XRenderPictFormat *mono_format; /* format of mono bitmap */
44
45 typedef struct
46 {
47   LOGFONTW lf;
48   XFORM  xform; /* this is dum as we don't care about offsets */
49   DWORD hash;
50 } LFANDSIZE;
51
52 #define INITIAL_REALIZED_BUF_SIZE 128
53
54
55 typedef struct
56 {
57   LFANDSIZE lfsz;
58   GlyphSet glyphset;
59   XRenderPictFormat *font_format;
60   int nrealized;
61   BOOL *realized;
62   UINT count;
63   INT next;
64 } gsCacheEntry;
65
66 struct tagXRENDERINFO
67 {
68     gsCacheEntry       *cacheEntry;
69     Picture            pict;
70     Picture            tile_pict;
71     Pixmap             tile_xpm;
72     COLORREF           lastTextColor;
73 };
74
75
76 static gsCacheEntry *glyphsetCache = NULL;
77 static DWORD glyphsetCacheSize = 0;
78 static INT lastfree = -1;
79 static INT mru = -1;
80
81 #define INIT_CACHE_SIZE 10
82
83 static int antialias = 1;
84
85 static void *xrender_handle;
86
87 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
88 MAKE_FUNCPTR(XRenderAddGlyphs)
89 MAKE_FUNCPTR(XRenderCompositeString8)
90 MAKE_FUNCPTR(XRenderCompositeString16)
91 MAKE_FUNCPTR(XRenderCompositeString32)
92 MAKE_FUNCPTR(XRenderCreateGlyphSet)
93 MAKE_FUNCPTR(XRenderCreatePicture)
94 MAKE_FUNCPTR(XRenderFillRectangle)
95 MAKE_FUNCPTR(XRenderFindFormat)
96 MAKE_FUNCPTR(XRenderFindVisualFormat)
97 MAKE_FUNCPTR(XRenderFreeGlyphSet)
98 MAKE_FUNCPTR(XRenderFreePicture)
99 MAKE_FUNCPTR(XRenderSetPictureClipRectangles)
100 MAKE_FUNCPTR(XRenderQueryExtension)
101 #undef MAKE_FUNCPTR
102
103 /***********************************************************************
104  *   X11DRV_XRender_Init
105  *
106  * Let's see if our XServer has the extension available
107  *
108  */
109 void X11DRV_XRender_Init(void)
110 {
111     int error_base, event_base, i;
112     XRenderPictFormat pf;
113
114     /* FIXME: should find correct soname at compile time */
115     if (!wine_dlopen("libX11.so", RTLD_NOW|RTLD_GLOBAL, NULL, 0)) return;
116     if (!wine_dlopen("libXext.so", RTLD_NOW|RTLD_GLOBAL, NULL, 0)) return;
117     xrender_handle = wine_dlopen("libXrender.so", RTLD_NOW, NULL, 0);
118     if(!xrender_handle) return;
119
120 #define LOAD_FUNCPTR(f) if((p##f = wine_dlsym(xrender_handle, #f, NULL, 0)) == NULL) goto sym_not_found;
121 LOAD_FUNCPTR(XRenderAddGlyphs)
122 LOAD_FUNCPTR(XRenderCompositeString8)
123 LOAD_FUNCPTR(XRenderCompositeString16)
124 LOAD_FUNCPTR(XRenderCompositeString32)
125 LOAD_FUNCPTR(XRenderCreateGlyphSet)
126 LOAD_FUNCPTR(XRenderCreatePicture)
127 LOAD_FUNCPTR(XRenderFillRectangle)
128 LOAD_FUNCPTR(XRenderFindFormat)
129 LOAD_FUNCPTR(XRenderFindVisualFormat)
130 LOAD_FUNCPTR(XRenderFreeGlyphSet)
131 LOAD_FUNCPTR(XRenderFreePicture)
132 LOAD_FUNCPTR(XRenderSetPictureClipRectangles)
133 LOAD_FUNCPTR(XRenderQueryExtension)
134 #undef LOAD_FUNCPTR
135
136     wine_tsx11_lock();
137     if(pXRenderQueryExtension(gdi_display, &event_base, &error_base)) {
138         X11DRV_XRender_Installed = TRUE;
139         TRACE("Xrender is up and running error_base = %d\n", error_base);
140         screen_format = pXRenderFindVisualFormat(gdi_display, visual);
141         pf.type = PictTypeDirect;
142         pf.depth = 1;
143         pf.direct.alpha = 0;
144         pf.direct.alphaMask = 1;
145         mono_format = pXRenderFindFormat(gdi_display, PictFormatType |
146                                           PictFormatDepth | PictFormatAlpha |
147                                           PictFormatAlphaMask, &pf, 0);
148
149         glyphsetCache = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
150                                   sizeof(*glyphsetCache) * INIT_CACHE_SIZE);
151
152         glyphsetCacheSize = INIT_CACHE_SIZE;
153         lastfree = 0;
154         for(i = 0; i < INIT_CACHE_SIZE; i++) {
155           glyphsetCache[i].next = i + 1;
156           glyphsetCache[i].count = -1;
157         }
158         glyphsetCache[i-1].next = -1;
159     } else {
160         TRACE("Xrender is not available on this server\n");
161     }
162     wine_tsx11_unlock();
163     return;
164
165 sym_not_found:
166     wine_dlclose(xrender_handle, NULL, 0);
167     xrender_handle = NULL;
168 }
169
170 static BOOL fontcmp(LFANDSIZE *p1, LFANDSIZE *p2)
171 {
172   if(p1->hash != p2->hash) return TRUE;
173   if(memcmp(&p1->xform, &p2->xform, sizeof(p1->xform))) return TRUE;
174   if(memcmp(&p1->lf, &p2->lf, offsetof(LOGFONTW, lfFaceName))) return TRUE;
175   return strcmpW(p1->lf.lfFaceName, p2->lf.lfFaceName);
176 }
177
178 #if 0
179 static void walk_cache(void)
180 {
181   int i;
182
183   for(i=mru; i >= 0; i = glyphsetCache[i].next)
184     TRACE("item %d\n", i);
185 }
186 #endif
187
188 static gsCacheEntry *LookupEntry(LFANDSIZE *plfsz)
189 {
190   int i, prev_i = -1;
191
192   for(i = mru; i >= 0; i = glyphsetCache[i].next) {
193     TRACE("%d\n", i);
194     if(glyphsetCache[i].count == -1) { /* reached free list so stop */
195       i = -1;
196       break;
197     }
198
199     if(!fontcmp(&glyphsetCache[i].lfsz, plfsz)) {
200       glyphsetCache[i].count++;
201       if(prev_i >= 0) {
202         glyphsetCache[prev_i].next = glyphsetCache[i].next;
203         glyphsetCache[i].next = mru;
204         mru = i;
205       }
206       TRACE("found font in cache %d\n", i);
207       return glyphsetCache + i;
208     }
209     prev_i = i;
210   }
211   TRACE("font not in cache\n");
212   return NULL;
213 }
214
215 static gsCacheEntry *AllocEntry(void)
216 {
217   int best = -1, prev_best = -1, i, prev_i = -1;
218
219   if(lastfree >= 0) {
220     assert(glyphsetCache[lastfree].count == -1);
221     glyphsetCache[lastfree].count = 1;
222     best = lastfree;
223     lastfree = glyphsetCache[lastfree].next;    
224     assert(best != mru);
225     glyphsetCache[best].next = mru;
226     mru = best;
227
228     TRACE("empty space at %d, next lastfree = %d\n", mru, lastfree);
229     return glyphsetCache + mru;
230   }
231
232   for(i = mru; i >= 0; i = glyphsetCache[i].next) {
233     if(glyphsetCache[i].count == 0) {
234       best = i;
235       prev_best = prev_i;
236     }
237     prev_i = i;
238   }
239
240   if(best >= 0) {
241     TRACE("freeing unused glyphset at cache %d\n", best);
242     wine_tsx11_lock();
243     pXRenderFreeGlyphSet(gdi_display, glyphsetCache[best].glyphset);
244     wine_tsx11_unlock();
245     glyphsetCache[best].glyphset = 0;
246     if(glyphsetCache[best].nrealized) { /* do we really want to do this? */
247       HeapFree(GetProcessHeap(), 0, glyphsetCache[best].realized);
248       glyphsetCache[best].realized = NULL;
249       glyphsetCache[best].nrealized = 0;
250     }
251     glyphsetCache[best].count = 1;
252     if(prev_best >= 0) {
253       glyphsetCache[prev_best].next = glyphsetCache[best].next;
254       glyphsetCache[best].next = mru;
255       mru = best;
256     } else {
257       assert(mru == best);
258     }
259     return glyphsetCache + mru;
260   }
261
262   TRACE("Growing cache\n");
263   glyphsetCache = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
264                               glyphsetCache,
265                               (glyphsetCacheSize + INIT_CACHE_SIZE)
266                               * sizeof(*glyphsetCache));
267   for(best = i = glyphsetCacheSize; i < glyphsetCacheSize + INIT_CACHE_SIZE;
268       i++) {
269     glyphsetCache[i].next = i + 1;
270     glyphsetCache[i].count = -1;
271   }
272   glyphsetCache[i-1].next = -1;
273   glyphsetCacheSize += INIT_CACHE_SIZE;
274
275   lastfree = glyphsetCache[best].next;
276   glyphsetCache[best].count = 1;
277   glyphsetCache[best].next = mru;
278   mru = best;
279   TRACE("new free cache slot at %d\n", mru);
280   return glyphsetCache + mru;
281 }    
282
283 static gsCacheEntry *GetCacheEntry(LFANDSIZE *plfsz)
284 {
285   XRenderPictFormat pf;
286   gsCacheEntry *ret;
287
288   if((ret = LookupEntry(plfsz)) != NULL) return ret;
289
290   ret = AllocEntry();
291   ret->lfsz = *plfsz;
292   assert(ret->nrealized == 0);
293
294
295   if(antialias && abs(plfsz->lf.lfHeight) > 16) {
296     pf.depth = 8;
297     pf.direct.alphaMask = 0xff;
298   } else {
299     pf.depth = 1;
300     pf.direct.alphaMask = 1;
301   }
302   pf.type = PictTypeDirect;
303   pf.direct.alpha = 0;
304
305   wine_tsx11_lock();
306   ret->font_format = pXRenderFindFormat(gdi_display,
307                                          PictFormatType |
308                                          PictFormatDepth |
309                                          PictFormatAlpha |
310                                          PictFormatAlphaMask,
311                                          &pf, 0);
312
313   ret->glyphset = pXRenderCreateGlyphSet(gdi_display, ret->font_format);
314   wine_tsx11_unlock();
315   return ret;
316 }
317
318 static void dec_ref_cache(gsCacheEntry *entry)
319 {
320   TRACE("dec'ing entry %d to %d\n", entry - glyphsetCache, entry->count - 1);
321   assert(entry->count > 0);
322   entry->count--;
323 }
324
325 static void lfsz_calc_hash(LFANDSIZE *plfsz)
326 {
327   DWORD hash = 0, *ptr;
328   int i;
329
330   for(ptr = (DWORD*)&plfsz->xform; ptr < (DWORD*)(&plfsz->xform + 1); ptr++)
331     hash ^= *ptr;
332   for(i = 0, ptr = (DWORD*)&plfsz->lf; i < 7; i++, ptr++)
333     hash ^= *ptr;
334   for(i = 0, ptr = (DWORD*)&plfsz->lf.lfFaceName; i < LF_FACESIZE/2; i++, ptr++) {
335     WCHAR *pwc = (WCHAR *)ptr;
336     if(!*pwc) break;
337     hash ^= *ptr;
338     pwc++;
339     if(!*pwc) break;
340   }
341   plfsz->hash = hash;
342   return;
343 }
344
345 /***********************************************************************
346  *   X11DRV_XRender_Finalize
347  */
348 void X11DRV_XRender_Finalize(void)
349 {
350     FIXME("Free cached glyphsets\n");
351     if (xrender_handle) wine_dlclose(xrender_handle, NULL, 0);
352     xrender_handle = NULL;
353 }
354
355
356 /***********************************************************************
357  *   X11DRV_XRender_SelectFont
358  */
359 BOOL X11DRV_XRender_SelectFont(X11DRV_PDEVICE *physDev, HFONT hfont)
360 {
361     LFANDSIZE lfsz;
362
363     GetObjectW(hfont, sizeof(lfsz.lf), &lfsz.lf);
364     TRACE("h=%ld w=%ld weight=%ld it=%d charset=%d name=%s\n",
365           lfsz.lf.lfHeight, lfsz.lf.lfWidth, lfsz.lf.lfWeight,
366           lfsz.lf.lfItalic, lfsz.lf.lfCharSet, debugstr_w(lfsz.lf.lfFaceName));
367     lfsz.xform = physDev->dc->xformWorld2Vport;
368     lfsz_calc_hash(&lfsz);
369
370     if(!physDev->xrender)
371         physDev->xrender = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
372                                      sizeof(*physDev->xrender));
373
374     else if(physDev->xrender->cacheEntry)
375         dec_ref_cache(physDev->xrender->cacheEntry);
376     physDev->xrender->cacheEntry = GetCacheEntry(&lfsz);
377     return 0;
378 }
379
380 /***********************************************************************
381  *   X11DRV_XRender_DeleteDC
382  */
383 void X11DRV_XRender_DeleteDC(X11DRV_PDEVICE *physDev)
384 {
385     wine_tsx11_lock();
386     if(physDev->xrender->tile_pict)
387         pXRenderFreePicture(gdi_display, physDev->xrender->tile_pict);
388
389     if(physDev->xrender->tile_xpm)
390         XFreePixmap(gdi_display, physDev->xrender->tile_xpm);
391
392     if(physDev->xrender->pict) {
393         TRACE("freeing pict = %lx dc = %p\n", physDev->xrender->pict, physDev->dc);
394         pXRenderFreePicture(gdi_display, physDev->xrender->pict);
395     }
396     wine_tsx11_unlock();
397
398     if(physDev->xrender->cacheEntry)
399         dec_ref_cache(physDev->xrender->cacheEntry);
400     
401     HeapFree(GetProcessHeap(), 0, physDev->xrender);
402     physDev->xrender = NULL;
403     return;
404 }
405
406 /***********************************************************************
407  *   X11DRV_XRender_UpdateDrawable
408  *
409  * This gets called from X11DRV_SetDrawable and deletes the pict when the
410  * drawable changes.  However at the moment we delete the pict at the end of
411  * every ExtTextOut so this is basically a NOP.
412  */
413 void X11DRV_XRender_UpdateDrawable(X11DRV_PDEVICE *physDev)
414 {
415     if(physDev->xrender->pict) {
416         TRACE("freeing pict %08lx from dc %p\n", physDev->xrender->pict, physDev->dc);
417         wine_tsx11_lock();
418         pXRenderFreePicture(gdi_display, physDev->xrender->pict);
419         wine_tsx11_unlock();
420     }
421     physDev->xrender->pict = 0;
422     return;
423 }
424
425 static BOOL UploadGlyph(X11DRV_PDEVICE *physDev, int glyph)
426 {
427     int buflen;
428     char *buf;
429     Glyph gid;
430     GLYPHMETRICS gm;
431     XGlyphInfo gi;
432     gsCacheEntry *entry = physDev->xrender->cacheEntry;
433     UINT ggo_format = GGO_GLYPH_INDEX;
434     BOOL aa;
435
436     if(entry->nrealized <= glyph) {
437         entry->nrealized = (glyph / 128 + 1) * 128;
438         entry->realized = HeapReAlloc(GetProcessHeap(),
439                                       HEAP_ZERO_MEMORY,
440                                       entry->realized,
441                                       entry->nrealized * sizeof(BOOL));
442     }
443     entry->realized[glyph] = TRUE;
444
445     if(entry->font_format->depth == 8) {
446         aa = TRUE;
447         ggo_format |= WINE_GGO_GRAY16_BITMAP;
448     } else {
449         aa = FALSE;
450         ggo_format |= GGO_BITMAP;
451     }
452
453     buflen = GetGlyphOutlineW(physDev->hdc, glyph, ggo_format, &gm, 0, NULL,
454                               NULL);
455     if(buflen == GDI_ERROR)
456         return FALSE;
457
458     buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buflen);
459     GetGlyphOutlineW(physDev->hdc, glyph, ggo_format, &gm, buflen, buf, NULL);
460
461     TRACE("buflen = %d. Got metrics: %dx%d adv=%d,%d origin=%ld,%ld\n",
462           buflen,
463           gm.gmBlackBoxX, gm.gmBlackBoxY, gm.gmCellIncX, gm.gmCellIncY,
464           gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
465
466     gi.width = gm.gmBlackBoxX;
467     gi.height = gm.gmBlackBoxY;
468     gi.x = -gm.gmptGlyphOrigin.x;
469     gi.y = gm.gmptGlyphOrigin.y;
470     gi.xOff = gm.gmCellIncX;
471     gi.yOff = gm.gmCellIncY;
472
473     if(TRACE_ON(xrender)) {
474         int pitch, i, j;
475         char output[300];
476         unsigned char *line;
477
478         if(!aa) {
479             pitch = ((gi.width + 31) / 32) * 4;
480             for(i = 0; i < gi.height; i++) {
481                 line = buf + i * pitch;
482                 output[0] = '\0';
483                 for(j = 0; j < pitch * 8; j++) {
484                     strcat(output, (line[j / 8] & (1 << (7 - (j % 8)))) ? "#" : " ");
485                 }
486                 strcat(output, "\n");
487                 TRACE(output);
488             }
489         } else {
490             char blks[] = " .:;!o*#";
491             char str[2];
492
493             str[1] = '\0';
494             pitch = ((gi.width + 3) / 4) * 4;
495             for(i = 0; i < gi.height; i++) {
496                 line = buf + i * pitch;
497                 output[0] = '\0';
498                 for(j = 0; j < pitch; j++) {
499                     str[0] = blks[line[j] >> 5];
500                     strcat(output, str);
501                 }
502                 strcat(output, "\n");
503                 TRACE(output);
504             }
505         }
506     }
507
508     if(!aa && BitmapBitOrder(gdi_display) != MSBFirst) {
509         unsigned char *byte = buf, c;
510         int i = buflen;
511
512         while(i--) {
513             c = *byte;
514
515             /* magic to flip bit order */
516             c = ((c << 1) & 0xaa) | ((c >> 1) & 0x55);
517             c = ((c << 2) & 0xcc) | ((c >> 2) & 0x33);
518             c = ((c << 4) & 0xf0) | ((c >> 4) & 0x0f);
519
520             *byte++ = c;
521         }
522     }
523     gid = glyph;
524     wine_tsx11_lock();
525     pXRenderAddGlyphs(gdi_display, entry->glyphset, &gid, &gi, 1,
526                        buf, buflen);
527     wine_tsx11_unlock();
528     HeapFree(GetProcessHeap(), 0, buf);
529     return TRUE;
530 }
531
532 /***********************************************************************
533  *   X11DRV_XRender_ExtTextOut
534  */
535 BOOL X11DRV_XRender_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
536                                 const RECT *lprect, LPCWSTR wstr, UINT count,
537                                 const INT *lpDx )
538 {
539     XRenderColor col;
540     int idx;
541     TEXTMETRICW tm;
542     RGNOBJ *obj;
543     XRectangle *pXrect;
544     SIZE sz;
545     RECT rc;
546     BOOL done_extents = FALSE;
547     INT width, xwidth, ywidth;
548     double cosEsc, sinEsc;
549     XGCValues xgcval;
550     LOGFONTW lf;
551     int render_op = PictOpOver;
552     WORD *glyphs;
553     HDC hdc = physDev->hdc;
554     DC *dc = physDev->dc;
555
556     TRACE("%04x, %d, %d, %08x, %p, %s, %d, %p)\n", hdc, x, y, flags,
557           lprect, debugstr_wn(wstr, count), count, lpDx);
558
559     if(flags & ETO_GLYPH_INDEX)
560         glyphs = (LPWORD)wstr;
561     else {
562         glyphs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR));
563         GetGlyphIndicesW(hdc, wstr, count, glyphs, 0);
564     }
565
566     if(lprect)
567       TRACE("rect: %d,%d - %d,%d\n", lprect->left, lprect->top, lprect->right,
568             lprect->bottom);
569     TRACE("align = %x bkmode = %x mapmode = %x\n", dc->textAlign, GetBkMode(hdc), dc->MapMode);
570
571     if(dc->textAlign & TA_UPDATECP) {
572         x = dc->CursPosX;
573         y = dc->CursPosY;
574     }
575
576     GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);
577     if(lf.lfEscapement != 0) {
578         cosEsc = cos(lf.lfEscapement * M_PI / 1800);
579         sinEsc = sin(lf.lfEscapement * M_PI / 1800);
580     } else {
581         cosEsc = 1;
582         sinEsc = 0;
583     }
584
585     if(flags & (ETO_CLIPPED | ETO_OPAQUE)) {
586         if(!lprect) {
587             if(flags & ETO_CLIPPED) return FALSE;
588                 GetTextExtentPointI(hdc, glyphs, count, &sz);
589                 done_extents = TRUE;
590                 rc.left = x;
591                 rc.top = y;
592                 rc.right = x + sz.cx;
593                 rc.bottom = y + sz.cy;
594         } else {
595             rc = *lprect;
596         }
597
598         rc.left = INTERNAL_XWPTODP(dc, rc.left, rc.top);
599         rc.top = INTERNAL_YWPTODP(dc, rc.left, rc.top);
600         rc.right = INTERNAL_XWPTODP(dc, rc.right, rc.bottom);
601         rc.bottom = INTERNAL_YWPTODP(dc, rc.right, rc.bottom);
602
603         if(rc.left > rc.right) {INT tmp = rc.left; rc.left = rc.right; rc.right = tmp;}
604         if(rc.top > rc.bottom) {INT tmp = rc.top; rc.top = rc.bottom; rc.bottom = tmp;}
605     }
606
607     xgcval.function = GXcopy;
608     xgcval.background = physDev->backgroundPixel;
609     xgcval.fill_style = FillSolid;
610     TSXChangeGC( gdi_display, physDev->gc,
611                  GCFunction | GCBackground | GCFillStyle, &xgcval );
612
613     X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
614
615     if(flags & ETO_OPAQUE) {
616         TSXSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
617         TSXFillRectangle( gdi_display, physDev->drawable, physDev->gc,
618                           dc->DCOrgX + rc.left, dc->DCOrgY + rc.top,
619                           rc.right - rc.left, rc.bottom - rc.top );
620     }
621
622     if(count == 0) {
623         X11DRV_UnlockDIBSection( physDev, TRUE );
624         return TRUE;
625     }
626
627     x = INTERNAL_XWPTODP( dc, x, y );
628     y = INTERNAL_YWPTODP( dc, x, y );
629
630     TRACE("real x,y %d,%d\n", x, y);
631
632     if(lpDx) {
633         width = 0;
634         for(idx = 0; idx < count; idx++)
635             width += lpDx[idx];
636     } else {
637         if(!done_extents) {
638             GetTextExtentPointI(hdc, glyphs, count, &sz);
639             done_extents = TRUE;
640         }
641         width = sz.cx;
642     }
643     width = INTERNAL_XWSTODS(dc, width);
644     xwidth = width * cosEsc;
645     ywidth = width * sinEsc;
646
647     GetTextMetricsW(hdc, &tm);
648
649     tm.tmAscent = INTERNAL_YWSTODS(dc, tm.tmAscent);
650     tm.tmDescent = INTERNAL_YWSTODS(dc, tm.tmDescent);
651     switch( dc->textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER) ) {
652     case TA_LEFT:
653         if (dc->textAlign & TA_UPDATECP) {
654             dc->CursPosX = INTERNAL_XDPTOWP( dc, x + xwidth, y - ywidth );
655             dc->CursPosY = INTERNAL_YDPTOWP( dc, x + xwidth, y - ywidth );
656         }
657         break;
658       
659     case TA_CENTER:
660         x -= xwidth / 2;
661         y += ywidth / 2;
662         break;
663
664     case TA_RIGHT:
665         x -= xwidth;
666         y += ywidth;
667         if (dc->textAlign & TA_UPDATECP) {
668             dc->CursPosX = INTERNAL_XDPTOWP( dc, x + xwidth, y - ywidth );
669             dc->CursPosY = INTERNAL_YDPTOWP( dc, x + xwidth, y - ywidth );
670         }
671         break;
672     }
673
674     switch( dc->textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE) ) {
675     case TA_TOP:
676         y += tm.tmAscent * cosEsc;
677         x += tm.tmAscent * sinEsc;
678         break;
679
680     case TA_BOTTOM:
681         y -= tm.tmDescent * cosEsc;
682         x -= tm.tmDescent * sinEsc;
683         break;
684
685     case TA_BASELINE:
686         break;
687     }
688
689     if (flags & ETO_CLIPPED)
690     {
691         SaveVisRgn16( hdc );
692         CLIPPING_IntersectVisRect( dc, rc.left, rc.top, rc.right,
693                                    rc.bottom, FALSE );
694     }
695
696
697
698     if(!physDev->xrender->pict) {
699         XRenderPictureAttributes pa;
700         pa.subwindow_mode = IncludeInferiors;
701
702         wine_tsx11_lock();
703         physDev->xrender->pict = pXRenderCreatePicture(gdi_display,
704                                                        physDev->drawable,
705                                                        (dc->bitsPerPixel == 1) ?
706                                                        mono_format : screen_format,
707                                                        CPSubwindowMode, &pa);
708         wine_tsx11_unlock();
709
710         TRACE("allocing pict = %lx dc = %p drawable = %08lx\n", physDev->xrender->pict, dc, physDev->drawable);
711     } else {
712         TRACE("using existing pict = %lx dc = %p\n", physDev->xrender->pict, dc);
713     }
714
715     obj = (RGNOBJ *) GDI_GetObjPtr(dc->hGCClipRgn, REGION_MAGIC);
716     if (!obj)
717     {
718         ERR("Rgn is 0. Please report this.\n");
719         return FALSE;
720     }
721     
722     if (obj->rgn->numRects > 0)
723     {
724         XRectangle *pXr;
725         RECT *pRect = obj->rgn->rects;
726         RECT *pEndRect = obj->rgn->rects + obj->rgn->numRects;
727
728         pXrect = HeapAlloc( GetProcessHeap(), 0, 
729                             sizeof(*pXrect) * obj->rgn->numRects );
730         if(!pXrect)
731         {
732             WARN("Can't alloc buffer\n");
733             GDI_ReleaseObj( dc->hGCClipRgn );
734             return FALSE;
735         }
736
737         for(pXr = pXrect; pRect < pEndRect; pRect++, pXr++)
738         {
739             pXr->x = pRect->left;
740             pXr->y = pRect->top;
741             pXr->width = pRect->right - pRect->left;
742             pXr->height = pRect->bottom - pRect->top;
743             TRACE("Adding clip rect %d,%d - %d,%d\n", pRect->left, pRect->top,
744                   pRect->right, pRect->bottom);
745         }
746     }
747     else {
748         TRACE("no clip rgn\n");
749         pXrect = NULL;
750     }
751
752     wine_tsx11_lock();
753     pXRenderSetPictureClipRectangles( gdi_display, physDev->xrender->pict,
754                                       0, 0, pXrect, obj->rgn->numRects );
755     wine_tsx11_unlock();
756
757     if(pXrect)
758         HeapFree( GetProcessHeap(), 0, pXrect );
759
760     GDI_ReleaseObj( dc->hGCClipRgn );
761
762     if(GetBkMode(hdc) != TRANSPARENT) {
763         if(!((flags & ETO_CLIPPED) && (flags & ETO_OPAQUE))) {
764             if(!(flags & ETO_OPAQUE) || x < rc.left || x + width >= rc.right ||
765                y - tm.tmAscent < rc.top || y + tm.tmDescent >= rc.bottom) {
766                 TSXSetForeground( gdi_display, physDev->gc, physDev->backgroundPixel );
767                 TSXFillRectangle( gdi_display, physDev->drawable, physDev->gc,
768                                   dc->DCOrgX + x, dc->DCOrgY + y - tm.tmAscent,
769                                   width, tm.tmAscent + tm.tmDescent );
770             }
771         }
772     }
773
774     /* Create a 1x1 pixmap to tile over the font mask */
775     if(!physDev->xrender->tile_xpm) {
776         XRenderPictureAttributes pa;
777
778         XRenderPictFormat *format = (dc->bitsPerPixel == 1) ? mono_format : screen_format;
779         wine_tsx11_lock();
780         physDev->xrender->tile_xpm = XCreatePixmap(gdi_display,
781                                                      physDev->drawable,
782                                                      1, 1,
783                                                      format->depth);
784         pa.repeat = True;
785         physDev->xrender->tile_pict = pXRenderCreatePicture(gdi_display,
786                                                              physDev->xrender->tile_xpm,
787                                                              format,
788                                                              CPRepeat, &pa);
789         wine_tsx11_unlock();
790         TRACE("Created pixmap of depth %d\n", format->depth);
791         /* init lastTextColor to something different from dc->textColor */
792         physDev->xrender->lastTextColor = ~dc->textColor;
793
794     }
795     
796     if(dc->textColor != physDev->xrender->lastTextColor) {
797         if(dc->bitsPerPixel != 1) {
798             /* Map 0 -- 0xff onto 0 -- 0xffff */
799             col.red = GetRValue(dc->textColor);
800             col.red |= col.red << 8;
801             col.green = GetGValue(dc->textColor);
802             col.green |= col.green << 8;
803             col.blue = GetBValue(dc->textColor);
804             col.blue |= col.blue << 8;
805             col.alpha = 0x0;
806         } else { /* for a 1bpp bitmap we always need a 1 in the tile */
807             col.red = col.green = col.blue = 0;
808             col.alpha = 0xffff;
809         }
810         wine_tsx11_lock();
811         pXRenderFillRectangle(gdi_display, PictOpSrc,
812                                physDev->xrender->tile_pict,
813                                &col, 0, 0, 1, 1);
814         wine_tsx11_unlock();
815         physDev->xrender->lastTextColor = dc->textColor;
816     }
817
818     /* FIXME the mapping of Text/BkColor onto 1 or 0 needs investigation.
819      */
820     if((dc->bitsPerPixel == 1) && ((dc->textColor & 0xffffff) == 0))
821         render_op = PictOpOutReverse; /* This gives us 'black' text */
822     
823     for(idx = 0; idx < count; idx++) {
824         if(glyphs[idx] >= physDev->xrender->cacheEntry->nrealized ||
825            physDev->xrender->cacheEntry->realized[glyphs[idx]] == FALSE) {
826             UploadGlyph(physDev, glyphs[idx]);
827         }
828     }
829
830
831     TRACE("Writing %s at %d,%d\n", debugstr_wn(wstr,count), dc->DCOrgX + x,
832           dc->DCOrgY + y);
833
834     wine_tsx11_lock();
835     if(!lpDx)
836         pXRenderCompositeString16(gdi_display, render_op, 
837                                    physDev->xrender->tile_pict,
838                                    physDev->xrender->pict,
839                                    physDev->xrender->cacheEntry->font_format,
840                                    physDev->xrender->cacheEntry->glyphset,
841                                    0, 0, dc->DCOrgX + x, dc->DCOrgY + y,
842                                    glyphs, count);
843
844     else {
845         INT offset = 0, xoff = 0, yoff = 0;
846         for(idx = 0; idx < count; idx++) {
847             pXRenderCompositeString16(gdi_display, render_op, 
848                                        physDev->xrender->tile_pict,
849                                        physDev->xrender->pict,
850                                        physDev->xrender->cacheEntry->font_format,
851                                        physDev->xrender->cacheEntry->glyphset,
852                                        0, 0, dc->DCOrgX + x + xoff,
853                                        dc->DCOrgY + y + yoff,
854                                        glyphs + idx, 1);
855             offset += INTERNAL_XWSTODS(dc, lpDx[idx]);
856             xoff = offset * cosEsc;
857             yoff = offset * -sinEsc;
858         }
859     }
860
861     if(physDev->xrender->pict) {
862         pXRenderFreePicture(gdi_display, physDev->xrender->pict);
863     }
864     physDev->xrender->pict = 0;
865     wine_tsx11_unlock();
866
867     if (flags & ETO_CLIPPED) 
868         RestoreVisRgn16( hdc );
869
870     X11DRV_UnlockDIBSection( physDev, TRUE );
871     if(glyphs != wstr) HeapFree(GetProcessHeap(), 0, glyphs);
872     return TRUE;
873 }
874
875 #else /* HAVE_X11_EXTENSIONS_XRENDER_H */
876
877 void X11DRV_XRender_Init(void)
878 {
879     TRACE("XRender support not compiled in.\n");
880     return;
881 }
882
883 void X11DRV_XRender_Finalize(void)
884 {
885 }
886
887 BOOL X11DRV_XRender_SelectFont(X11DRV_PDEVICE *physDev, HFONT hfont)
888 {
889   assert(0);
890   return FALSE;
891 }
892
893 void X11DRV_XRender_DeleteDC(X11DRV_PDEVICE *physDev)
894 {
895   assert(0);
896   return;
897 }
898
899 BOOL X11DRV_XRender_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
900                                 const RECT *lprect, LPCWSTR wstr, UINT count,
901                                 const INT *lpDx )
902 {
903   assert(0);
904   return FALSE;
905 }
906
907 void X11DRV_XRender_UpdateDrawable(X11DRV_PDEVICE *physDev)
908 {
909   assert(0);
910   return;
911 }
912
913 #endif /* HAVE_X11_EXTENSIONS_XRENDER_H */