Fixed buffer overflow.
[wine] / dlls / x11drv / xrender.c
1 /*
2  * Functions to use the XRender extension
3  *
4  * Copyright 2001, 2002 Huw D M Davies for CodeWeavers
5  *
6  * Some parts also:
7  * Copyright 2000 Keith Packard, member of The XFree86 Project, Inc.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <assert.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <stdlib.h>
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wownt32.h"
34 #include "x11drv.h"
35 #include "gdi.h"
36 #include "wine/library.h"
37 #include "wine/unicode.h"
38 #include "wine/debug.h"
39
40 static BOOL X11DRV_XRender_Installed = FALSE;
41 int using_client_side_fonts = FALSE;
42
43 WINE_DEFAULT_DEBUG_CHANNEL(xrender);
44
45 #ifdef HAVE_X11_EXTENSIONS_XRENDER_H
46
47 #include <X11/Xlib.h>
48 #include <X11/extensions/Xrender.h>
49
50 static XRenderPictFormat *screen_format; /* format of screen */
51 static XRenderPictFormat *mono_format; /* format of mono bitmap */
52
53 typedef struct
54 {
55     LOGFONTW lf;
56     SIZE     devsize;  /* size in device coords */
57     DWORD    hash;
58 } LFANDSIZE;
59
60 #define INITIAL_REALIZED_BUF_SIZE 128
61
62 typedef enum { AA_None, AA_Grey, AA_RGB, AA_BGR, AA_VRGB, AA_VBGR } AA_Type;
63
64 typedef struct
65 {
66     LFANDSIZE lfsz;
67     AA_Type aa;
68     GlyphSet glyphset;
69     XRenderPictFormat *font_format;
70     int nrealized;
71     BOOL *realized;
72     void **bitmaps;
73     XGlyphInfo *gis;
74     UINT count;
75     INT next;
76 } gsCacheEntry;
77
78 struct tagXRENDERINFO
79 {
80     int                cache_index;
81     Picture            pict;
82     Picture            tile_pict;
83     Pixmap             tile_xpm;
84     COLORREF           lastTextColor;
85 };
86
87
88 static gsCacheEntry *glyphsetCache = NULL;
89 static DWORD glyphsetCacheSize = 0;
90 static INT lastfree = -1;
91 static INT mru = -1;
92
93 #define INIT_CACHE_SIZE 10
94
95 static int antialias = 1;
96
97 /* some default values just in case */
98 #ifndef SONAME_LIBX11
99 #define SONAME_LIBX11 "libX11.so"
100 #endif
101 #ifndef SONAME_LIBXEXT
102 #define SONAME_LIBXEXT "libXext.so"
103 #endif
104 #ifndef SONAME_LIBXRENDER
105 #define SONAME_LIBXRENDER "libXrender.so"
106 #endif
107
108 static void *xrender_handle;
109
110 #define MAKE_FUNCPTR(f) static typeof(f) * p##f;
111 MAKE_FUNCPTR(XRenderAddGlyphs)
112 MAKE_FUNCPTR(XRenderCompositeString8)
113 MAKE_FUNCPTR(XRenderCompositeString16)
114 MAKE_FUNCPTR(XRenderCompositeString32)
115 MAKE_FUNCPTR(XRenderCreateGlyphSet)
116 MAKE_FUNCPTR(XRenderCreatePicture)
117 MAKE_FUNCPTR(XRenderFillRectangle)
118 MAKE_FUNCPTR(XRenderFindFormat)
119 MAKE_FUNCPTR(XRenderFindVisualFormat)
120 MAKE_FUNCPTR(XRenderFreeGlyphSet)
121 MAKE_FUNCPTR(XRenderFreePicture)
122 MAKE_FUNCPTR(XRenderSetPictureClipRectangles)
123 MAKE_FUNCPTR(XRenderQueryExtension)
124 #undef MAKE_FUNCPTR
125
126 static CRITICAL_SECTION xrender_cs;
127 static CRITICAL_SECTION_DEBUG critsect_debug =
128 {
129     0, 0, &xrender_cs,
130     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
131       0, 0, { 0, (DWORD)(__FILE__ ": xrender_cs") }
132 };
133 static CRITICAL_SECTION xrender_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
134
135
136 /***********************************************************************
137  *   X11DRV_XRender_Init
138  *
139  * Let's see if our XServer has the extension available
140  *
141  */
142 void X11DRV_XRender_Init(void)
143 {
144     int error_base, event_base, i;
145     XRenderPictFormat pf;
146
147     if (client_side_with_render &&
148         wine_dlopen(SONAME_LIBX11, RTLD_NOW|RTLD_GLOBAL, NULL, 0) &&
149         wine_dlopen(SONAME_LIBXEXT, RTLD_NOW|RTLD_GLOBAL, NULL, 0) && 
150         (xrender_handle = wine_dlopen(SONAME_LIBXRENDER, RTLD_NOW, NULL, 0)))
151     {
152
153 #define LOAD_FUNCPTR(f) if((p##f = wine_dlsym(xrender_handle, #f, NULL, 0)) == NULL) goto sym_not_found;
154 LOAD_FUNCPTR(XRenderAddGlyphs)
155 LOAD_FUNCPTR(XRenderCompositeString8)
156 LOAD_FUNCPTR(XRenderCompositeString16)
157 LOAD_FUNCPTR(XRenderCompositeString32)
158 LOAD_FUNCPTR(XRenderCreateGlyphSet)
159 LOAD_FUNCPTR(XRenderCreatePicture)
160 LOAD_FUNCPTR(XRenderFillRectangle)
161 LOAD_FUNCPTR(XRenderFindFormat)
162 LOAD_FUNCPTR(XRenderFindVisualFormat)
163 LOAD_FUNCPTR(XRenderFreeGlyphSet)
164 LOAD_FUNCPTR(XRenderFreePicture)
165 LOAD_FUNCPTR(XRenderSetPictureClipRectangles)
166 LOAD_FUNCPTR(XRenderQueryExtension)
167 #undef LOAD_FUNCPTR
168
169         wine_tsx11_lock();
170         if(pXRenderQueryExtension(gdi_display, &event_base, &error_base)) {
171             X11DRV_XRender_Installed = TRUE;
172             TRACE("Xrender is up and running error_base = %d\n", error_base);
173             screen_format = pXRenderFindVisualFormat(gdi_display, visual);
174             if(!screen_format) { /* This fails in buggy versions of libXrender.so */
175                 wine_tsx11_unlock();
176                 WINE_MESSAGE(
177                     "Wine has detected that you probably have a buggy version\n"
178                     "of libXrender.so .  Because of this client side font rendering\n"
179                     "will be disabled.  Please upgrade this library.\n");
180                 X11DRV_XRender_Installed = FALSE;
181                 return;
182             }
183             pf.type = PictTypeDirect;
184             pf.depth = 1;
185             pf.direct.alpha = 0;
186             pf.direct.alphaMask = 1;
187             mono_format = pXRenderFindFormat(gdi_display, PictFormatType |
188                                              PictFormatDepth | PictFormatAlpha |
189                                              PictFormatAlphaMask, &pf, 0);
190             if(!mono_format) {
191                 ERR("mono_format == NULL?\n");
192                 X11DRV_XRender_Installed = FALSE;
193             }
194         }
195         wine_tsx11_unlock();
196     }
197
198 sym_not_found:
199     if(X11DRV_XRender_Installed || client_side_with_core)
200     {
201         glyphsetCache = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
202                                   sizeof(*glyphsetCache) * INIT_CACHE_SIZE);
203
204         glyphsetCacheSize = INIT_CACHE_SIZE;
205         lastfree = 0;
206         for(i = 0; i < INIT_CACHE_SIZE; i++) {
207           glyphsetCache[i].next = i + 1;
208           glyphsetCache[i].count = -1;
209         }
210         glyphsetCache[i-1].next = -1;
211         using_client_side_fonts = 1;
212
213         if(!X11DRV_XRender_Installed) {
214             TRACE("Xrender is not available on your XServer, client side rendering with the core protocol instead.\n");
215             if(screen_depth <= 8 || !client_side_antialias_with_core)
216                 antialias = 0;
217         } else {
218             if(screen_depth <= 8 || !client_side_antialias_with_render)
219                 antialias = 0;
220         }
221     }
222     else TRACE("Using X11 core fonts\n");
223 }
224
225 static BOOL fontcmp(LFANDSIZE *p1, LFANDSIZE *p2)
226 {
227   if(p1->hash != p2->hash) return TRUE;
228   if(memcmp(&p1->devsize, &p2->devsize, sizeof(p1->devsize))) return TRUE;
229   if(memcmp(&p1->lf, &p2->lf, offsetof(LOGFONTW, lfFaceName))) return TRUE;
230   return strcmpW(p1->lf.lfFaceName, p2->lf.lfFaceName);
231 }
232
233 #if 0
234 static void walk_cache(void)
235 {
236   int i;
237
238   EnterCriticalSection(&xrender_cs);
239   for(i=mru; i >= 0; i = glyphsetCache[i].next)
240     TRACE("item %d\n", i);
241   LeaveCriticalSection(&xrender_cs);
242 }
243 #endif
244
245 static int LookupEntry(LFANDSIZE *plfsz)
246 {
247   int i, prev_i = -1;
248
249   for(i = mru; i >= 0; i = glyphsetCache[i].next) {
250     TRACE("%d\n", i);
251     if(glyphsetCache[i].count == -1) { /* reached free list so stop */
252       i = -1;
253       break;
254     }
255
256     if(!fontcmp(&glyphsetCache[i].lfsz, plfsz)) {
257       glyphsetCache[i].count++;
258       if(prev_i >= 0) {
259         glyphsetCache[prev_i].next = glyphsetCache[i].next;
260         glyphsetCache[i].next = mru;
261         mru = i;
262       }
263       TRACE("found font in cache %d\n", i);
264       return i;
265     }
266     prev_i = i;
267   }
268   TRACE("font not in cache\n");
269   return -1;
270 }
271
272 static void FreeEntry(int entry)
273 {
274     int i;
275
276     if(glyphsetCache[entry].glyphset) {
277         wine_tsx11_lock();
278         pXRenderFreeGlyphSet(gdi_display, glyphsetCache[entry].glyphset);
279         wine_tsx11_unlock();
280         glyphsetCache[entry].glyphset = 0;
281     }
282     if(glyphsetCache[entry].nrealized) {
283         HeapFree(GetProcessHeap(), 0, glyphsetCache[entry].realized);
284         glyphsetCache[entry].realized = NULL;
285         if(glyphsetCache[entry].bitmaps) {
286             for(i = 0; i < glyphsetCache[entry].nrealized; i++)
287                 if(glyphsetCache[entry].bitmaps[i])
288                     HeapFree(GetProcessHeap(), 0, glyphsetCache[entry].bitmaps[i]);
289             HeapFree(GetProcessHeap(), 0, glyphsetCache[entry].bitmaps);
290             glyphsetCache[entry].bitmaps = NULL;
291             HeapFree(GetProcessHeap(), 0, glyphsetCache[entry].gis);
292             glyphsetCache[entry].gis = NULL;
293         }
294         glyphsetCache[entry].nrealized = 0;
295     }
296 }
297
298 static int AllocEntry(void)
299 {
300   int best = -1, prev_best = -1, i, prev_i = -1;
301
302   if(lastfree >= 0) {
303     assert(glyphsetCache[lastfree].count == -1);
304     glyphsetCache[lastfree].count = 1;
305     best = lastfree;
306     lastfree = glyphsetCache[lastfree].next;
307     assert(best != mru);
308     glyphsetCache[best].next = mru;
309     mru = best;
310
311     TRACE("empty space at %d, next lastfree = %d\n", mru, lastfree);
312     return mru;
313   }
314
315   for(i = mru; i >= 0; i = glyphsetCache[i].next) {
316     if(glyphsetCache[i].count == 0) {
317       best = i;
318       prev_best = prev_i;
319     }
320     prev_i = i;
321   }
322
323   if(best >= 0) {
324     TRACE("freeing unused glyphset at cache %d\n", best);
325     FreeEntry(best);
326     glyphsetCache[best].count = 1;
327     if(prev_best >= 0) {
328       glyphsetCache[prev_best].next = glyphsetCache[best].next;
329       glyphsetCache[best].next = mru;
330       mru = best;
331     } else {
332       assert(mru == best);
333     }
334     return mru;
335   }
336
337   TRACE("Growing cache\n");
338   
339   if (glyphsetCache)
340     glyphsetCache = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
341                               glyphsetCache,
342                               (glyphsetCacheSize + INIT_CACHE_SIZE)
343                               * sizeof(*glyphsetCache));
344   else
345     glyphsetCache = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
346                               (glyphsetCacheSize + INIT_CACHE_SIZE)
347                               * sizeof(*glyphsetCache));
348
349   for(best = i = glyphsetCacheSize; i < glyphsetCacheSize + INIT_CACHE_SIZE;
350       i++) {
351     glyphsetCache[i].next = i + 1;
352     glyphsetCache[i].count = -1;
353   }
354   glyphsetCache[i-1].next = -1;
355   glyphsetCacheSize += INIT_CACHE_SIZE;
356
357   lastfree = glyphsetCache[best].next;
358   glyphsetCache[best].count = 1;
359   glyphsetCache[best].next = mru;
360   mru = best;
361   TRACE("new free cache slot at %d\n", mru);
362   return mru;
363 }
364
365 static int GetCacheEntry(LFANDSIZE *plfsz)
366 {
367     XRenderPictFormat pf;
368     int ret;
369     gsCacheEntry *entry;
370
371     if((ret = LookupEntry(plfsz)) != -1) return ret;
372
373     ret = AllocEntry();
374     entry = glyphsetCache + ret;
375     entry->lfsz = *plfsz;
376     assert(entry->nrealized == 0);
377
378     if(antialias)
379         entry->aa = AA_Grey;
380     else
381         entry->aa = AA_None;
382
383     if(X11DRV_XRender_Installed) {
384         switch(entry->aa) {
385         case AA_Grey:
386             pf.depth = 8;
387             pf.direct.alphaMask = 0xff;
388             break;
389
390         default:
391             ERR("aa = %d - not implemented\n", entry->aa);
392         case AA_None:
393             pf.depth = 1;
394             pf.direct.alphaMask = 1;
395             break;
396         }
397
398         pf.type = PictTypeDirect;
399         pf.direct.alpha = 0;
400
401         wine_tsx11_lock();
402         entry->font_format = pXRenderFindFormat(gdi_display,
403                                                 PictFormatType |
404                                                 PictFormatDepth |
405                                                 PictFormatAlpha |
406                                                 PictFormatAlphaMask,
407                                                 &pf, 0);
408
409         entry->glyphset = pXRenderCreateGlyphSet(gdi_display, entry->font_format);
410         wine_tsx11_unlock();
411     }
412     return ret;
413 }
414
415 static void dec_ref_cache(int index)
416 {
417     assert(index >= 0);
418     TRACE("dec'ing entry %d to %d\n", index, glyphsetCache[index].count - 1);
419     assert(glyphsetCache[index].count > 0);
420     glyphsetCache[index].count--;
421 }
422
423 static void lfsz_calc_hash(LFANDSIZE *plfsz)
424 {
425   DWORD hash = 0, *ptr;
426   int i;
427
428   hash ^= plfsz->devsize.cx;
429   hash ^= plfsz->devsize.cy;
430   for(i = 0, ptr = (DWORD*)&plfsz->lf; i < 7; i++, ptr++)
431     hash ^= *ptr;
432   for(i = 0, ptr = (DWORD*)&plfsz->lf.lfFaceName; i < LF_FACESIZE/2; i++, ptr++) {
433     WCHAR *pwc = (WCHAR *)ptr;
434     if(!*pwc) break;
435     hash ^= *ptr;
436     pwc++;
437     if(!*pwc) break;
438   }
439   plfsz->hash = hash;
440   return;
441 }
442
443 /***********************************************************************
444  *   X11DRV_XRender_Finalize
445  */
446 void X11DRV_XRender_Finalize(void)
447 {
448     int i;
449
450     EnterCriticalSection(&xrender_cs);
451     for(i = mru; i >= 0; i = glyphsetCache[i].next)
452         FreeEntry(i);
453     LeaveCriticalSection(&xrender_cs);
454 }
455
456
457 /***********************************************************************
458  *   X11DRV_XRender_SelectFont
459  */
460 BOOL X11DRV_XRender_SelectFont(X11DRV_PDEVICE *physDev, HFONT hfont)
461 {
462     LFANDSIZE lfsz;
463
464     GetObjectW(hfont, sizeof(lfsz.lf), &lfsz.lf);
465     TRACE("h=%ld w=%ld weight=%ld it=%d charset=%d name=%s\n",
466           lfsz.lf.lfHeight, lfsz.lf.lfWidth, lfsz.lf.lfWeight,
467           lfsz.lf.lfItalic, lfsz.lf.lfCharSet, debugstr_w(lfsz.lf.lfFaceName));
468     lfsz.devsize.cx = X11DRV_XWStoDS( physDev, lfsz.lf.lfWidth );
469     lfsz.devsize.cy = X11DRV_YWStoDS( physDev, lfsz.lf.lfHeight );
470     lfsz_calc_hash(&lfsz);
471
472     EnterCriticalSection(&xrender_cs);
473     if(!physDev->xrender) {
474         physDev->xrender = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
475                                      sizeof(*physDev->xrender));
476         physDev->xrender->cache_index = -1;
477     }
478     else if(physDev->xrender->cache_index != -1)
479         dec_ref_cache(physDev->xrender->cache_index);
480     physDev->xrender->cache_index = GetCacheEntry(&lfsz);
481     LeaveCriticalSection(&xrender_cs);
482     return 0;
483 }
484
485 /***********************************************************************
486  *   X11DRV_XRender_DeleteDC
487  */
488 void X11DRV_XRender_DeleteDC(X11DRV_PDEVICE *physDev)
489 {
490     wine_tsx11_lock();
491     if(physDev->xrender->tile_pict)
492         pXRenderFreePicture(gdi_display, physDev->xrender->tile_pict);
493
494     if(physDev->xrender->tile_xpm)
495         XFreePixmap(gdi_display, physDev->xrender->tile_xpm);
496
497     if(physDev->xrender->pict) {
498         TRACE("freeing pict = %lx dc = %p\n", physDev->xrender->pict, physDev->hdc);
499         pXRenderFreePicture(gdi_display, physDev->xrender->pict);
500     }
501     wine_tsx11_unlock();
502
503     EnterCriticalSection(&xrender_cs);
504     if(physDev->xrender->cache_index != -1)
505         dec_ref_cache(physDev->xrender->cache_index);
506     LeaveCriticalSection(&xrender_cs);
507
508     HeapFree(GetProcessHeap(), 0, physDev->xrender);
509     physDev->xrender = NULL;
510     return;
511 }
512
513 /***********************************************************************
514  *   X11DRV_XRender_UpdateDrawable
515  *
516  * This gets called from X11DRV_SetDrawable and X11DRV_SelectBitmap.
517  * It deletes the pict when the drawable changes.
518  */
519 void X11DRV_XRender_UpdateDrawable(X11DRV_PDEVICE *physDev)
520 {
521     if(physDev->xrender->pict) {
522         TRACE("freeing pict %08lx from dc %p drawable %08lx\n", physDev->xrender->pict,
523               physDev->hdc, physDev->drawable);
524         wine_tsx11_lock();
525         XFlush(gdi_display);
526         pXRenderFreePicture(gdi_display, physDev->xrender->pict);
527         wine_tsx11_unlock();
528     }
529     physDev->xrender->pict = 0;
530     return;
531 }
532
533 /************************************************************************
534  *   UploadGlyph
535  *
536  * Helper to ExtTextOut.  Must be called inside xrender_cs
537  */
538 static BOOL UploadGlyph(X11DRV_PDEVICE *physDev, int glyph)
539 {
540     int buflen;
541     char *buf;
542     Glyph gid;
543     GLYPHMETRICS gm;
544     XGlyphInfo gi;
545     gsCacheEntry *entry = glyphsetCache + physDev->xrender->cache_index;
546     UINT ggo_format = GGO_GLYPH_INDEX;
547
548     if(entry->nrealized <= glyph) {
549         entry->nrealized = (glyph / 128 + 1) * 128;
550
551         if (entry->realized)
552             entry->realized = HeapReAlloc(GetProcessHeap(),
553                                       HEAP_ZERO_MEMORY,
554                                       entry->realized,
555                                       entry->nrealized * sizeof(BOOL));
556         else
557             entry->realized = HeapAlloc(GetProcessHeap(),
558                                       HEAP_ZERO_MEMORY,
559                                       entry->nrealized * sizeof(BOOL));
560
561         if(entry->glyphset == 0) {
562           if (entry->bitmaps)
563             entry->bitmaps = HeapReAlloc(GetProcessHeap(),
564                                       HEAP_ZERO_MEMORY,
565                                       entry->bitmaps,
566                                       entry->nrealized * sizeof(entry->bitmaps[0]));
567           else
568             entry->bitmaps = HeapAlloc(GetProcessHeap(),
569                                       HEAP_ZERO_MEMORY,
570                                       entry->nrealized * sizeof(entry->bitmaps[0]));
571
572           if (entry->gis)
573             entry->gis = HeapReAlloc(GetProcessHeap(),
574                                    HEAP_ZERO_MEMORY,
575                                    entry->gis,
576                                    entry->nrealized * sizeof(entry->gis[0]));
577           else
578             entry->gis = HeapAlloc(GetProcessHeap(),
579                                    HEAP_ZERO_MEMORY,
580                                    entry->nrealized * sizeof(entry->gis[0]));
581         }
582     }
583
584     switch(entry->aa) {
585     case AA_Grey:
586         ggo_format |= WINE_GGO_GRAY16_BITMAP;
587         break;
588
589     default:
590         ERR("aa = %d - not implemented\n", entry->aa);
591     case AA_None:
592         ggo_format |= GGO_BITMAP;
593         break;
594     }
595
596     buflen = GetGlyphOutlineW(physDev->hdc, glyph, ggo_format, &gm, 0, NULL,
597                               NULL);
598     if(buflen == GDI_ERROR) {
599         LeaveCriticalSection(&xrender_cs);
600         return FALSE;
601     }
602
603     entry->realized[glyph] = TRUE;
604
605     buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buflen);
606     GetGlyphOutlineW(physDev->hdc, glyph, ggo_format, &gm, buflen, buf, NULL);
607
608     TRACE("buflen = %d. Got metrics: %dx%d adv=%d,%d origin=%ld,%ld\n",
609           buflen,
610           gm.gmBlackBoxX, gm.gmBlackBoxY, gm.gmCellIncX, gm.gmCellIncY,
611           gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
612
613     gi.width = gm.gmBlackBoxX;
614     gi.height = gm.gmBlackBoxY;
615     gi.x = -gm.gmptGlyphOrigin.x;
616     gi.y = gm.gmptGlyphOrigin.y;
617     gi.xOff = gm.gmCellIncX;
618     gi.yOff = gm.gmCellIncY;
619
620     if(TRACE_ON(xrender)) {
621         int pitch, i, j;
622         char output[300];
623         unsigned char *line;
624
625         if(entry->aa == AA_None) {
626             pitch = ((gi.width + 31) / 32) * 4;
627             for(i = 0; i < gi.height; i++) {
628                 line = buf + i * pitch;
629                 output[0] = '\0';
630                 for(j = 0; j < pitch * 8; j++) {
631                     strcat(output, (line[j / 8] & (1 << (7 - (j % 8)))) ? "#" : " ");
632                 }
633                 strcat(output, "\n");
634                 TRACE(output);
635             }
636         } else {
637             char blks[] = " .:;!o*#";
638             char str[2];
639
640             str[1] = '\0';
641             pitch = ((gi.width + 3) / 4) * 4;
642             for(i = 0; i < gi.height; i++) {
643                 line = buf + i * pitch;
644                 output[0] = '\0';
645                 for(j = 0; j < pitch; j++) {
646                     str[0] = blks[line[j] >> 5];
647                     strcat(output, str);
648                 }
649                 strcat(output, "\n");
650                 TRACE(output);
651             }
652         }
653     }
654
655     if(entry->glyphset) {
656         if(entry->aa == AA_None && BitmapBitOrder(gdi_display) != MSBFirst) {
657             unsigned char *byte = buf, c;
658             int i = buflen;
659
660             while(i--) {
661                 c = *byte;
662
663                 /* magic to flip bit order */
664                 c = ((c << 1) & 0xaa) | ((c >> 1) & 0x55);
665                 c = ((c << 2) & 0xcc) | ((c >> 2) & 0x33);
666                 c = ((c << 4) & 0xf0) | ((c >> 4) & 0x0f);
667
668                 *byte++ = c;
669             }
670         }
671         gid = glyph;
672         wine_tsx11_lock();
673         pXRenderAddGlyphs(gdi_display, entry->glyphset, &gid, &gi, 1,
674                           buf, buflen);
675         wine_tsx11_unlock();
676         HeapFree(GetProcessHeap(), 0, buf);
677     } else {
678         entry->bitmaps[glyph] = buf;
679         memcpy(&entry->gis[glyph], &gi, sizeof(gi));
680     }
681     return TRUE;
682 }
683
684 static void SharpGlyphMono(X11DRV_PDEVICE *physDev, INT x, INT y,
685                             void *bitmap, XGlyphInfo *gi)
686 {
687     unsigned char   *srcLine = bitmap, *src;
688     unsigned char   bits, bitsMask;
689     int             width = gi->width;
690     int             stride = ((width + 31) & ~31) >> 3;
691     int             height = gi->height;
692     int             w;
693     int             xspan, lenspan;
694
695     TRACE("%d, %d\n", x, y);
696     x -= gi->x;
697     y -= gi->y;
698     while (height--)
699     {
700         src = srcLine;
701         srcLine += stride;
702         w = width;
703         
704         bitsMask = 0x80;    /* FreeType is always MSB first */
705         bits = *src++;
706         
707         xspan = x;
708         while (w)
709         {
710             if (bits & bitsMask)
711             {
712                 lenspan = 0;
713                 do
714                 {
715                     lenspan++;
716                     if (lenspan == w)
717                         break;
718                     bitsMask = bitsMask >> 1;
719                     if (!bitsMask)
720                     {
721                         bits = *src++;
722                         bitsMask = 0x80;
723                     }
724                 } while (bits & bitsMask);
725                 XFillRectangle (gdi_display, physDev->drawable, 
726                                 physDev->gc, xspan, y, lenspan, 1);
727                 xspan += lenspan;
728                 w -= lenspan;
729             }
730             else
731             {
732                 do
733                 {
734                     w--;
735                     xspan++;
736                     if (!w)
737                         break;
738                     bitsMask = bitsMask >> 1;
739                     if (!bitsMask)
740                     {
741                         bits = *src++;
742                         bitsMask = 0x80;
743                     }
744                 } while (!(bits & bitsMask));
745             }
746         }
747         y++;
748     }
749 }
750
751 static void SharpGlyphGray(X11DRV_PDEVICE *physDev, INT x, INT y,
752                             void *bitmap, XGlyphInfo *gi)
753 {
754     unsigned char   *srcLine = bitmap, *src, bits;
755     int             width = gi->width;
756     int             stride = ((width + 3) & ~3);
757     int             height = gi->height;
758     int             w;
759     int             xspan, lenspan;
760
761     x -= gi->x;
762     y -= gi->y;
763     while (height--)
764     {
765         src = srcLine;
766         srcLine += stride;
767         w = width;
768         
769         bits = *src++;
770         xspan = x;
771         while (w)
772         {
773             if (bits >= 0x80)
774             {
775                 lenspan = 0;
776                 do
777                 {
778                     lenspan++;
779                     if (lenspan == w)
780                         break;
781                     bits = *src++;
782                 } while (bits >= 0x80);
783                 XFillRectangle (gdi_display, physDev->drawable, 
784                                 physDev->gc, xspan, y, lenspan, 1);
785                 xspan += lenspan;
786                 w -= lenspan;
787             }
788             else
789             {
790                 do
791                 {
792                     w--;
793                     xspan++;
794                     if (!w)
795                         break;
796                     bits = *src++;
797                 } while (bits < 0x80);
798             }
799         }
800         y++;
801     }
802 }
803
804
805 static void ExamineBitfield (DWORD mask, int *shift, int *len)
806 {
807     int s, l;
808
809     s = 0;
810     while ((mask & 1) == 0)
811     {
812         mask >>= 1;
813         s++;
814     }
815     l = 0;
816     while ((mask & 1) == 1)
817     {
818         mask >>= 1;
819         l++;
820     }
821     *shift = s;
822     *len = l;
823 }
824
825 static DWORD GetField (DWORD pixel, int shift, int len)
826 {
827     pixel = pixel & (((1 << (len)) - 1) << shift);
828     pixel = pixel << (32 - (shift + len)) >> 24;
829     while (len < 8)
830     {
831         pixel |= (pixel >> len);
832         len <<= 1;
833     }
834     return pixel;
835 }
836
837
838 static DWORD PutField (DWORD pixel, int shift, int len)
839 {
840     shift = shift - (8 - len);
841     if (len <= 8)
842         pixel &= (((1 << len) - 1) << (8 - len));
843     if (shift < 0)
844         pixel >>= -shift;
845     else
846         pixel <<= shift;
847     return pixel;
848 }
849
850 static void SmoothGlyphGray(XImage *image, int x, int y, void *bitmap, XGlyphInfo *gi,
851                             COLORREF color)
852 {
853     int             r_shift, r_len;
854     int             g_shift, g_len;
855     int             b_shift, b_len;
856     BYTE            *maskLine, *mask, m;
857     int             maskStride;
858     DWORD           pixel;
859     int             width, height;
860     int             w, tx;
861     BYTE            src_r, src_g, src_b;
862
863     src_r = GetRValue(color);
864     src_g = GetGValue(color);
865     src_b = GetBValue(color);
866
867     x -= gi->x;
868     y -= gi->y;
869     width = gi->width;
870     height = gi->height;
871
872     maskLine = (unsigned char *) bitmap;
873     maskStride = (width + 3) & ~3;
874
875     ExamineBitfield (image->red_mask, &r_shift, &r_len);
876     ExamineBitfield (image->green_mask, &g_shift, &g_len);
877     ExamineBitfield (image->blue_mask, &b_shift, &b_len);
878     for(; height--; y++)
879     {
880         mask = maskLine;
881         maskLine += maskStride;
882         w = width;
883         tx = x;
884
885         if(y < 0) continue;
886         if(y >= image->height) break;
887
888         for(; w--; tx++)
889         {
890             if(tx >= image->width) break;
891
892             m = *mask++;
893             if(tx < 0) continue;
894
895             if (m == 0xff)
896             {
897                 pixel = (PutField ((src_r), r_shift, r_len) |
898                          PutField ((src_g), g_shift, g_len) |
899                          PutField ((src_b), b_shift, b_len));
900                 XPutPixel (image, tx, y, pixel);
901             }
902             else if (m)
903             {
904                 BYTE r, g, b;
905
906                 pixel = XGetPixel (image, tx, y);
907
908                 r = GetField(pixel, r_shift, r_len);
909                 r = ((BYTE)~m * (WORD)r + (BYTE)m * (WORD)src_r) >> 8;
910                 g = GetField(pixel, g_shift, g_len);
911                 g = ((BYTE)~m * (WORD)g + (BYTE)m * (WORD)src_g) >> 8;
912                 b = GetField(pixel, b_shift, b_len);
913                 b = ((BYTE)~m * (WORD)b + (BYTE)m * (WORD)src_b) >> 8;
914
915                 pixel = (PutField (r, r_shift, r_len) |
916                          PutField (g, g_shift, g_len) |
917                          PutField (b, b_shift, b_len));
918                 XPutPixel (image, tx, y, pixel);
919             }
920         }
921     }
922 }
923
924 static int XRenderErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
925 {
926     return 1;
927 }
928
929 /***********************************************************************
930  *   X11DRV_XRender_ExtTextOut
931  */
932 BOOL X11DRV_XRender_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
933                                 const RECT *lprect, LPCWSTR wstr, UINT count,
934                                 const INT *lpDx, INT breakExtra )
935 {
936     XRenderColor col;
937     int idx;
938     TEXTMETRICW tm;
939     RGNDATA *data;
940     SIZE sz;
941     RECT rc;
942     BOOL done_extents = FALSE;
943     INT width, xwidth, ywidth;
944     double cosEsc, sinEsc;
945     XGCValues xgcval;
946     LOGFONTW lf;
947     int render_op = PictOpOver;
948     WORD *glyphs;
949     POINT pt;
950     gsCacheEntry *entry;
951     BOOL retv = FALSE;
952     HDC hdc = physDev->hdc;
953     int textPixel, backgroundPixel;
954     INT *deltas = NULL;
955     INT char_extra;
956     HRGN saved_region = 0;
957     UINT align = GetTextAlign( hdc );
958     COLORREF textColor = GetTextColor( hdc );
959
960     TRACE("%p, %d, %d, %08x, %p, %s, %d, %p)\n", hdc, x, y, flags,
961           lprect, debugstr_wn(wstr, count), count, lpDx);
962
963     if(flags & ETO_GLYPH_INDEX)
964         glyphs = (LPWORD)wstr;
965     else {
966         glyphs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR));
967         GetGlyphIndicesW(hdc, wstr, count, glyphs, 0);
968     }
969
970     if(lprect)
971       TRACE("rect: %ld,%ld - %ld,%ld\n", lprect->left, lprect->top, lprect->right,
972             lprect->bottom);
973     TRACE("align = %x bkmode = %x mapmode = %x\n", align, GetBkMode(hdc), GetMapMode(hdc));
974
975     if(align & TA_UPDATECP)
976     {
977         GetCurrentPositionEx( hdc, &pt );
978         x = pt.x;
979         y = pt.y;
980     }
981
982     GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);
983     if(lf.lfEscapement != 0) {
984         cosEsc = cos(lf.lfEscapement * M_PI / 1800);
985         sinEsc = sin(lf.lfEscapement * M_PI / 1800);
986     } else {
987         cosEsc = 1;
988         sinEsc = 0;
989     }
990
991     if(flags & (ETO_CLIPPED | ETO_OPAQUE)) {
992         if(!lprect) {
993             if(flags & ETO_CLIPPED) return FALSE;
994                 GetTextExtentPointI(hdc, glyphs, count, &sz);
995                 done_extents = TRUE;
996                 rc.left = x;
997                 rc.top = y;
998                 rc.right = x + sz.cx;
999                 rc.bottom = y + sz.cy;
1000         } else {
1001             rc = *lprect;
1002         }
1003
1004         LPtoDP(hdc, (POINT*)&rc, 2);
1005
1006         if(rc.left > rc.right) {INT tmp = rc.left; rc.left = rc.right; rc.right = tmp;}
1007         if(rc.top > rc.bottom) {INT tmp = rc.top; rc.top = rc.bottom; rc.bottom = tmp;}
1008     }
1009
1010     xgcval.function = GXcopy;
1011     xgcval.background = physDev->backgroundPixel;
1012     xgcval.fill_style = FillSolid;
1013     wine_tsx11_lock();
1014     XChangeGC( gdi_display, physDev->gc, GCFunction | GCBackground | GCFillStyle, &xgcval );
1015     wine_tsx11_unlock();
1016
1017     X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
1018
1019     if(physDev->depth == 1) {
1020         if((textColor & 0xffffff) == 0) {
1021             textPixel = 0;
1022             backgroundPixel = 1;
1023         } else {
1024             textPixel = 1;
1025             backgroundPixel = 0;
1026         }
1027     } else {
1028         textPixel = physDev->textPixel;
1029         backgroundPixel = physDev->backgroundPixel;
1030     }
1031
1032     if(flags & ETO_OPAQUE) {
1033         wine_tsx11_lock();
1034         XSetForeground( gdi_display, physDev->gc, backgroundPixel );
1035         XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
1036                         physDev->org.x + rc.left, physDev->org.y + rc.top,
1037                         rc.right - rc.left, rc.bottom - rc.top );
1038         wine_tsx11_unlock();
1039     }
1040
1041     if(count == 0) {
1042         retv =  TRUE;
1043         goto done;
1044     }
1045
1046     pt.x = x;
1047     pt.y = y;
1048     LPtoDP(hdc, &pt, 1);
1049     x = pt.x;
1050     y = pt.y;
1051
1052     TRACE("real x,y %d,%d\n", x, y);
1053
1054     if((char_extra = GetTextCharacterExtra(hdc)) != 0) {
1055         INT i;
1056         SIZE tmpsz;
1057         deltas = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
1058         for(i = 0; i < count; i++) {
1059             if(lpDx)
1060                 deltas[i] = lpDx[i] + char_extra;
1061             else {
1062                 GetTextExtentPointI(hdc, glyphs + i, 1, &tmpsz);
1063                 deltas[i] = tmpsz.cx;
1064             }
1065         }
1066     } else if(lpDx)
1067         deltas = (INT*)lpDx;
1068
1069     if(deltas) {
1070         width = 0;
1071         for(idx = 0; idx < count; idx++)
1072             width += deltas[idx];
1073     } else {
1074         if(!done_extents) {
1075             GetTextExtentPointI(hdc, glyphs, count, &sz);
1076             done_extents = TRUE;
1077         }
1078         width = sz.cx;
1079     }
1080     width = X11DRV_XWStoDS(physDev, width);
1081     xwidth = width * cosEsc;
1082     ywidth = width * sinEsc;
1083
1084     GetTextMetricsW(hdc, &tm);
1085
1086     tm.tmAscent = abs(X11DRV_YWStoDS(physDev, tm.tmAscent));
1087     tm.tmDescent = abs(X11DRV_YWStoDS(physDev, tm.tmDescent));
1088     switch( align & (TA_LEFT | TA_RIGHT | TA_CENTER) ) {
1089     case TA_LEFT:
1090         if (align & TA_UPDATECP) {
1091             pt.x = x + xwidth;
1092             pt.y = y - ywidth;
1093             DPtoLP(hdc, &pt, 1);
1094             MoveToEx(hdc, pt.x, pt.y, NULL);
1095         }
1096         break;
1097
1098     case TA_CENTER:
1099         x -= xwidth / 2;
1100         y += ywidth / 2;
1101         break;
1102
1103     case TA_RIGHT:
1104         x -= xwidth;
1105         y += ywidth;
1106         if (align & TA_UPDATECP) {
1107             pt.x = x;
1108             pt.y = y;
1109             DPtoLP(hdc, &pt, 1);
1110             MoveToEx(hdc, pt.x, pt.y, NULL);
1111         }
1112         break;
1113     }
1114
1115     switch( align & (TA_TOP | TA_BOTTOM | TA_BASELINE) ) {
1116     case TA_TOP:
1117         y += tm.tmAscent * cosEsc;
1118         x += tm.tmAscent * sinEsc;
1119         break;
1120
1121     case TA_BOTTOM:
1122         y -= tm.tmDescent * cosEsc;
1123         x -= tm.tmDescent * sinEsc;
1124         break;
1125
1126     case TA_BASELINE:
1127         break;
1128     }
1129
1130     if (flags & ETO_CLIPPED)
1131     {
1132         HRGN clip_region;
1133         RECT clip_rect = *lprect;
1134
1135         LPtoDP( hdc, (POINT *)&clip_rect, 2 );
1136         clip_region = CreateRectRgnIndirect( &clip_rect );
1137         /* make a copy of the current device region */
1138         saved_region = CreateRectRgn( 0, 0, 0, 0 );
1139         CombineRgn( saved_region, physDev->region, 0, RGN_COPY );
1140         X11DRV_SetDeviceClipping( physDev, saved_region, clip_region );
1141         DeleteObject( clip_region );
1142     }
1143
1144     if(X11DRV_XRender_Installed) {
1145         if(!physDev->xrender->pict) {
1146             XRenderPictureAttributes pa;
1147             pa.subwindow_mode = IncludeInferiors;
1148
1149             wine_tsx11_lock();
1150             physDev->xrender->pict = pXRenderCreatePicture(gdi_display,
1151                                                            physDev->drawable,
1152                                                            (physDev->depth == 1) ?
1153                                                            mono_format : screen_format,
1154                                                            CPSubwindowMode, &pa);
1155             wine_tsx11_unlock();
1156
1157             TRACE("allocing pict = %lx dc = %p drawable = %08lx\n",
1158                   physDev->xrender->pict, hdc, physDev->drawable);
1159         } else {
1160             TRACE("using existing pict = %lx dc = %p drawable = %08lx\n",
1161                   physDev->xrender->pict, hdc, physDev->drawable);
1162         }
1163
1164         if ((data = X11DRV_GetRegionData( physDev->region, 0 )))
1165         {
1166             wine_tsx11_lock();
1167             pXRenderSetPictureClipRectangles( gdi_display, physDev->xrender->pict,
1168                                               physDev->org.x, physDev->org.y,
1169                                               (XRectangle *)data->Buffer, data->rdh.nCount );
1170             wine_tsx11_unlock();
1171             HeapFree( GetProcessHeap(), 0, data );
1172         }
1173     }
1174
1175     if(GetBkMode(hdc) != TRANSPARENT) {
1176         if(!((flags & ETO_CLIPPED) && (flags & ETO_OPAQUE))) {
1177             if(!(flags & ETO_OPAQUE) || x < rc.left || x + width >= rc.right ||
1178                y - tm.tmAscent < rc.top || y + tm.tmDescent >= rc.bottom) {
1179                 wine_tsx11_lock();
1180                 XSetForeground( gdi_display, physDev->gc, backgroundPixel );
1181                 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
1182                                 physDev->org.x + x, physDev->org.y + y - tm.tmAscent,
1183                                 width, tm.tmAscent + tm.tmDescent );
1184                 wine_tsx11_unlock();
1185             }
1186         }
1187     }
1188
1189     if(X11DRV_XRender_Installed) {
1190         /* Create a 1x1 pixmap to tile over the font mask */
1191         if(!physDev->xrender->tile_xpm) {
1192             XRenderPictureAttributes pa;
1193
1194             XRenderPictFormat *format = (physDev->depth == 1) ? mono_format : screen_format;
1195             wine_tsx11_lock();
1196             physDev->xrender->tile_xpm = XCreatePixmap(gdi_display,
1197                                                        physDev->drawable,
1198                                                        1, 1,
1199                                                        format->depth);
1200             pa.repeat = True;
1201             physDev->xrender->tile_pict = pXRenderCreatePicture(gdi_display,
1202                                                                 physDev->xrender->tile_xpm,
1203                                                                 format,
1204                                                                 CPRepeat, &pa);
1205             wine_tsx11_unlock();
1206             TRACE("Created pixmap of depth %d\n", format->depth);
1207             /* init lastTextColor to something different from textColor */
1208             physDev->xrender->lastTextColor = ~textColor;
1209
1210         }
1211
1212         if(textColor != physDev->xrender->lastTextColor) {
1213             if(physDev->depth != 1) {
1214               /* Map 0 -- 0xff onto 0 -- 0xffff */
1215                 col.red = GetRValue(textColor);
1216                 col.red |= col.red << 8;
1217                 col.green = GetGValue(textColor);
1218                 col.green |= col.green << 8;
1219                 col.blue = GetBValue(textColor);
1220                 col.blue |= col.blue << 8;
1221                 col.alpha = 0x0;
1222             } else { /* for a 1bpp bitmap we always need a 1 in the tile */
1223                 col.red = col.green = col.blue = 0;
1224                 col.alpha = 0xffff;
1225             }
1226             wine_tsx11_lock();
1227             pXRenderFillRectangle(gdi_display, PictOpSrc,
1228                                   physDev->xrender->tile_pict,
1229                                   &col, 0, 0, 1, 1);
1230             wine_tsx11_unlock();
1231             physDev->xrender->lastTextColor = textColor;
1232         }
1233
1234         /* FIXME the mapping of Text/BkColor onto 1 or 0 needs investigation.
1235          */
1236         if((physDev->depth == 1) && (textPixel == 0))
1237             render_op = PictOpOutReverse; /* This gives us 'black' text */
1238     }
1239
1240     EnterCriticalSection(&xrender_cs);
1241     entry = glyphsetCache + physDev->xrender->cache_index;
1242
1243     for(idx = 0; idx < count; idx++) {
1244         if(glyphs[idx] >= entry->nrealized || entry->realized[glyphs[idx]] == FALSE) {
1245             UploadGlyph(physDev, glyphs[idx]);
1246         }
1247     }
1248
1249
1250     TRACE("Writing %s at %ld,%ld\n", debugstr_wn(wstr,count),
1251           physDev->org.x + x, physDev->org.y + y);
1252
1253     if(X11DRV_XRender_Installed) {
1254         wine_tsx11_lock();
1255         if(!deltas)
1256             pXRenderCompositeString16(gdi_display, render_op,
1257                                       physDev->xrender->tile_pict,
1258                                       physDev->xrender->pict,
1259                                       entry->font_format, entry->glyphset,
1260                                       0, 0, physDev->org.x + x, physDev->org.y + y,
1261                                       glyphs, count);
1262
1263         else {
1264             INT offset = 0, xoff = 0, yoff = 0;
1265             for(idx = 0; idx < count; idx++) {
1266                 pXRenderCompositeString16(gdi_display, render_op,
1267                                           physDev->xrender->tile_pict,
1268                                           physDev->xrender->pict,
1269                                           entry->font_format, entry->glyphset,
1270                                           0, 0, physDev->org.x + x + xoff,
1271                                           physDev->org.y + y + yoff,
1272                                           glyphs + idx, 1);
1273                 offset += X11DRV_XWStoDS(physDev, deltas[idx]);
1274                 xoff = offset * cosEsc;
1275                 yoff = offset * -sinEsc;
1276             }
1277         }
1278         wine_tsx11_unlock();
1279
1280         if (lf.lfUnderline || lf.lfStrikeOut) {
1281             int linePos;
1282             unsigned int lineWidth;
1283             UINT nMetricsSize = GetOutlineTextMetricsW(hdc, 0, NULL);
1284             OUTLINETEXTMETRICW* otm = HeapAlloc(GetProcessHeap(), 0, nMetricsSize);
1285             if (!otm) goto done;
1286
1287             GetOutlineTextMetricsW(hdc, nMetricsSize, otm);
1288
1289             wine_tsx11_lock();
1290             XSetForeground( gdi_display, physDev->gc, physDev->textPixel );
1291
1292             if (lf.lfUnderline) {
1293                 linePos = X11DRV_YWStoDS(physDev, otm->otmsUnderscorePosition);
1294                 lineWidth = X11DRV_YWStoDS(physDev, otm->otmsUnderscoreSize);
1295
1296                 XSetLineAttributes( gdi_display, physDev->gc, lineWidth,
1297                                     LineSolid, CapProjecting, JoinBevel );
1298                 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
1299                            physDev->org.x + x, physDev->org.y + y - linePos,
1300                            physDev->org.x + x + width, physDev->org.y + y - linePos );
1301             }
1302
1303             if (lf.lfStrikeOut) { 
1304                 linePos = X11DRV_YWStoDS(physDev, otm->otmsStrikeoutPosition);
1305                 lineWidth = X11DRV_YWStoDS(physDev, otm->otmsStrikeoutSize);
1306
1307                 XSetLineAttributes( gdi_display, physDev->gc, lineWidth,
1308                                     LineSolid, CapProjecting, JoinBevel );
1309                 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
1310                            physDev->org.x + x, physDev->org.y + y - linePos,
1311                            physDev->org.x + x + width, physDev->org.y + y - linePos );
1312             } 
1313             wine_tsx11_unlock();
1314             HeapFree(GetProcessHeap(), 0, otm);
1315         }
1316
1317     } else {
1318         INT offset = 0, xoff = 0, yoff = 0;
1319         wine_tsx11_lock();
1320         XSetForeground( gdi_display, physDev->gc, textPixel );
1321
1322         if(entry->aa == AA_None) {
1323             for(idx = 0; idx < count; idx++) {
1324                 SharpGlyphMono(physDev, physDev->org.x + x + xoff,
1325                                physDev->org.y + y + yoff,
1326                                entry->bitmaps[glyphs[idx]],
1327                                &entry->gis[glyphs[idx]]);
1328                 if(deltas) {
1329                     offset += X11DRV_XWStoDS(physDev, deltas[idx]);
1330                     xoff = offset * cosEsc;
1331                     yoff = offset * -sinEsc;
1332
1333                 } else {
1334                     xoff += entry->gis[glyphs[idx]].xOff;
1335                     yoff += entry->gis[glyphs[idx]].yOff;
1336                 }
1337             }
1338         } else if(physDev->depth == 1) {
1339             for(idx = 0; idx < count; idx++) {
1340                 SharpGlyphGray(physDev, physDev->org.x + x + xoff,
1341                                physDev->org.y + y + yoff,
1342                                entry->bitmaps[glyphs[idx]],
1343                                &entry->gis[glyphs[idx]]);
1344                 if(deltas) {
1345                     offset += X11DRV_XWStoDS(physDev, deltas[idx]);
1346                     xoff = offset * cosEsc;
1347                     yoff = offset * -sinEsc;
1348
1349                 } else {
1350                     xoff += entry->gis[glyphs[idx]].xOff;
1351                     yoff += entry->gis[glyphs[idx]].yOff;
1352                 }
1353                     
1354             }
1355         } else {
1356             XImage *image;
1357             unsigned int w, h, dummy_uint;
1358             Window dummy_window;
1359             int dummy_int;
1360             int image_x, image_y, image_off_x, image_off_y, image_w, image_h;
1361             RECT extents = {0, 0, 0, 0};
1362             POINT cur = {0, 0};
1363             
1364             
1365             XGetGeometry(gdi_display, physDev->drawable, &dummy_window, &dummy_int, &dummy_int,
1366                          &w, &h, &dummy_uint, &dummy_uint);
1367             TRACE("drawable %dx%d\n", w, h);
1368
1369             for(idx = 0; idx < count; idx++) {
1370                 if(extents.left > cur.x - entry->gis[glyphs[idx]].x)
1371                     extents.left = cur.x - entry->gis[glyphs[idx]].x;
1372                 if(extents.top > cur.y - entry->gis[glyphs[idx]].y)
1373                     extents.top = cur.y - entry->gis[glyphs[idx]].y;
1374                 if(extents.right < cur.x - entry->gis[glyphs[idx]].x + entry->gis[glyphs[idx]].width)
1375                     extents.right = cur.x - entry->gis[glyphs[idx]].x + entry->gis[glyphs[idx]].width;
1376                 if(extents.bottom < cur.y - entry->gis[glyphs[idx]].y + entry->gis[glyphs[idx]].height)
1377                     extents.bottom = cur.y - entry->gis[glyphs[idx]].y + entry->gis[glyphs[idx]].height;
1378                 if(deltas) {
1379                     offset += X11DRV_XWStoDS(physDev, deltas[idx]);
1380                     cur.x = offset * cosEsc;
1381                     cur.y = offset * -sinEsc;
1382                 } else {
1383                     cur.x += entry->gis[glyphs[idx]].xOff;
1384                     cur.y += entry->gis[glyphs[idx]].yOff;
1385                 }
1386             }
1387             TRACE("glyph extents %ld,%ld - %ld,%ld drawable x,y %ld,%ld\n", extents.left, extents.top,
1388                   extents.right, extents.bottom, physDev->org.x + x, physDev->org.y + y);
1389
1390             if(physDev->org.x + x + extents.left >= 0) {
1391                 image_x = physDev->org.x + x + extents.left;
1392                 image_off_x = 0;
1393             } else {
1394                 image_x = 0;
1395                 image_off_x = physDev->org.x + x + extents.left;
1396             }
1397             if(physDev->org.y + y + extents.top >= 0) {
1398                 image_y = physDev->org.y + y + extents.top;
1399                 image_off_y = 0;
1400             } else {
1401                 image_y = 0;
1402                 image_off_y = physDev->org.y + y + extents.top;
1403             }
1404             if(physDev->org.x + x + extents.right < w)
1405                 image_w = physDev->org.x + x + extents.right - image_x;
1406             else
1407                 image_w = w - image_x;
1408             if(physDev->org.y + y + extents.bottom < h)
1409                 image_h = physDev->org.y + y + extents.bottom - image_y;
1410             else
1411                 image_h = h - image_y;
1412
1413             if(image_w <= 0 || image_h <= 0) goto no_image;
1414
1415             X11DRV_expect_error(gdi_display, XRenderErrorHandler, NULL);
1416             image = XGetImage(gdi_display, physDev->drawable,
1417                               image_x, image_y, image_w, image_h,
1418                               AllPlanes, ZPixmap);
1419             X11DRV_check_error();
1420
1421             TRACE("XGetImage(%p, %x, %d, %d, %d, %d, %lx, %x) depth = %d rets %p\n",
1422                   gdi_display, (int)physDev->drawable, image_x, image_y,
1423                   image_w, image_h, AllPlanes, ZPixmap,
1424                   physDev->depth, image);
1425             if(!image) {
1426                 Pixmap xpm = XCreatePixmap(gdi_display, physDev->drawable, image_w, image_h,
1427                                            physDev->depth);
1428                 GC gc;
1429                 XGCValues gcv;
1430
1431                 gcv.graphics_exposures = False;
1432                 gc = XCreateGC(gdi_display, xpm, GCGraphicsExposures, &gcv);
1433                 XCopyArea(gdi_display, physDev->drawable, xpm, gc, image_x, image_y,
1434                           image_w, image_h, 0, 0);
1435                 XFreeGC(gdi_display, gc);
1436                 X11DRV_expect_error(gdi_display, XRenderErrorHandler, NULL);
1437                 image = XGetImage(gdi_display, xpm, 0, 0, image_w, image_h, AllPlanes,
1438                                   ZPixmap);
1439                 X11DRV_check_error();
1440                 XFreePixmap(gdi_display, xpm);
1441             }
1442             if(!image) goto no_image;
1443
1444             image->red_mask = visual->red_mask;
1445             image->green_mask = visual->green_mask;
1446             image->blue_mask = visual->blue_mask;
1447
1448             offset = xoff = yoff = 0;
1449             for(idx = 0; idx < count; idx++) {
1450                 SmoothGlyphGray(image, xoff + image_off_x - extents.left,
1451                                 yoff + image_off_y - extents.top,
1452                                 entry->bitmaps[glyphs[idx]],
1453                                 &entry->gis[glyphs[idx]],
1454                                 textColor);
1455                 if(deltas) {
1456                     offset += X11DRV_XWStoDS(physDev, deltas[idx]);
1457                     xoff = offset * cosEsc;
1458                     yoff = offset * -sinEsc;
1459                 } else {
1460                     xoff += entry->gis[glyphs[idx]].xOff;
1461                     yoff += entry->gis[glyphs[idx]].yOff;
1462                 }
1463                     
1464             }
1465             XPutImage(gdi_display, physDev->drawable, physDev->gc, image, 0, 0,
1466                       image_x, image_y, image_w, image_h);
1467             XDestroyImage(image);
1468         }
1469     no_image:
1470         wine_tsx11_unlock();
1471     }
1472     LeaveCriticalSection(&xrender_cs);
1473
1474     if(deltas && deltas != lpDx)
1475         HeapFree(GetProcessHeap(), 0, deltas);
1476
1477     if (flags & ETO_CLIPPED)
1478     {
1479         /* restore the device region */
1480         X11DRV_SetDeviceClipping( physDev, saved_region, 0 );
1481         DeleteObject( saved_region );
1482     }
1483
1484     retv = TRUE;
1485
1486 done:
1487     X11DRV_UnlockDIBSection( physDev, TRUE );
1488     if(glyphs != wstr) HeapFree(GetProcessHeap(), 0, glyphs);
1489     return retv;
1490 }
1491
1492 #else /* HAVE_X11_EXTENSIONS_XRENDER_H */
1493
1494 void X11DRV_XRender_Init(void)
1495 {
1496     TRACE("XRender support not compiled in.\n");
1497     return;
1498 }
1499
1500 void X11DRV_XRender_Finalize(void)
1501 {
1502 }
1503
1504 BOOL X11DRV_XRender_SelectFont(X11DRV_PDEVICE *physDev, HFONT hfont)
1505 {
1506   assert(0);
1507   return FALSE;
1508 }
1509
1510 void X11DRV_XRender_DeleteDC(X11DRV_PDEVICE *physDev)
1511 {
1512   assert(0);
1513   return;
1514 }
1515
1516 BOOL X11DRV_XRender_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
1517                                 const RECT *lprect, LPCWSTR wstr, UINT count,
1518                                 const INT *lpDx, INT breakExtra )
1519 {
1520   assert(0);
1521   return FALSE;
1522 }
1523
1524 void X11DRV_XRender_UpdateDrawable(X11DRV_PDEVICE *physDev)
1525 {
1526   assert(0);
1527   return;
1528 }
1529
1530 #endif /* HAVE_X11_EXTENSIONS_XRENDER_H */