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