Don't allocate a glyphset until we actually need it.
[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     int ret;
368     gsCacheEntry *entry;
369
370     if((ret = LookupEntry(plfsz)) != -1) return ret;
371
372     ret = AllocEntry();
373     entry = glyphsetCache + ret;
374     entry->lfsz = *plfsz;
375     assert(entry->nrealized == 0);
376
377     if(antialias)
378         entry->aa = AA_Grey;
379     else
380         entry->aa = AA_None;
381
382     entry->font_format = NULL;
383     entry->glyphset = 0;
384     return ret;
385 }
386
387 static void dec_ref_cache(int index)
388 {
389     assert(index >= 0);
390     TRACE("dec'ing entry %d to %d\n", index, glyphsetCache[index].count - 1);
391     assert(glyphsetCache[index].count > 0);
392     glyphsetCache[index].count--;
393 }
394
395 static void lfsz_calc_hash(LFANDSIZE *plfsz)
396 {
397   DWORD hash = 0, *ptr;
398   int i;
399
400   hash ^= plfsz->devsize.cx;
401   hash ^= plfsz->devsize.cy;
402   for(i = 0, ptr = (DWORD*)&plfsz->lf; i < 7; i++, ptr++)
403     hash ^= *ptr;
404   for(i = 0, ptr = (DWORD*)&plfsz->lf.lfFaceName; i < LF_FACESIZE/2; i++, ptr++) {
405     WCHAR *pwc = (WCHAR *)ptr;
406     if(!*pwc) break;
407     hash ^= *ptr;
408     pwc++;
409     if(!*pwc) break;
410   }
411   plfsz->hash = hash;
412   return;
413 }
414
415 /***********************************************************************
416  *   X11DRV_XRender_Finalize
417  */
418 void X11DRV_XRender_Finalize(void)
419 {
420     int i;
421
422     EnterCriticalSection(&xrender_cs);
423     for(i = mru; i >= 0; i = glyphsetCache[i].next)
424         FreeEntry(i);
425     LeaveCriticalSection(&xrender_cs);
426 }
427
428
429 /***********************************************************************
430  *   X11DRV_XRender_SelectFont
431  */
432 BOOL X11DRV_XRender_SelectFont(X11DRV_PDEVICE *physDev, HFONT hfont)
433 {
434     LFANDSIZE lfsz;
435
436     GetObjectW(hfont, sizeof(lfsz.lf), &lfsz.lf);
437     TRACE("h=%ld w=%ld weight=%ld it=%d charset=%d name=%s\n",
438           lfsz.lf.lfHeight, lfsz.lf.lfWidth, lfsz.lf.lfWeight,
439           lfsz.lf.lfItalic, lfsz.lf.lfCharSet, debugstr_w(lfsz.lf.lfFaceName));
440     lfsz.devsize.cx = X11DRV_XWStoDS( physDev, lfsz.lf.lfWidth );
441     lfsz.devsize.cy = X11DRV_YWStoDS( physDev, lfsz.lf.lfHeight );
442     lfsz_calc_hash(&lfsz);
443
444     EnterCriticalSection(&xrender_cs);
445     if(!physDev->xrender) {
446         physDev->xrender = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
447                                      sizeof(*physDev->xrender));
448         physDev->xrender->cache_index = -1;
449     }
450     else if(physDev->xrender->cache_index != -1)
451         dec_ref_cache(physDev->xrender->cache_index);
452     physDev->xrender->cache_index = GetCacheEntry(&lfsz);
453     LeaveCriticalSection(&xrender_cs);
454     return 0;
455 }
456
457 /***********************************************************************
458  *   X11DRV_XRender_DeleteDC
459  */
460 void X11DRV_XRender_DeleteDC(X11DRV_PDEVICE *physDev)
461 {
462     wine_tsx11_lock();
463     if(physDev->xrender->tile_pict)
464         pXRenderFreePicture(gdi_display, physDev->xrender->tile_pict);
465
466     if(physDev->xrender->tile_xpm)
467         XFreePixmap(gdi_display, physDev->xrender->tile_xpm);
468
469     if(physDev->xrender->pict) {
470         TRACE("freeing pict = %lx dc = %p\n", physDev->xrender->pict, physDev->hdc);
471         pXRenderFreePicture(gdi_display, physDev->xrender->pict);
472     }
473     wine_tsx11_unlock();
474
475     EnterCriticalSection(&xrender_cs);
476     if(physDev->xrender->cache_index != -1)
477         dec_ref_cache(physDev->xrender->cache_index);
478     LeaveCriticalSection(&xrender_cs);
479
480     HeapFree(GetProcessHeap(), 0, physDev->xrender);
481     physDev->xrender = NULL;
482     return;
483 }
484
485 /***********************************************************************
486  *   X11DRV_XRender_UpdateDrawable
487  *
488  * This gets called from X11DRV_SetDrawable and X11DRV_SelectBitmap.
489  * It deletes the pict when the drawable changes.
490  */
491 void X11DRV_XRender_UpdateDrawable(X11DRV_PDEVICE *physDev)
492 {
493     if(physDev->xrender->pict) {
494         TRACE("freeing pict %08lx from dc %p drawable %08lx\n", physDev->xrender->pict,
495               physDev->hdc, physDev->drawable);
496         wine_tsx11_lock();
497         XFlush(gdi_display);
498         pXRenderFreePicture(gdi_display, physDev->xrender->pict);
499         wine_tsx11_unlock();
500     }
501     physDev->xrender->pict = 0;
502     return;
503 }
504
505 /************************************************************************
506  *   UploadGlyph
507  *
508  * Helper to ExtTextOut.  Must be called inside xrender_cs
509  */
510 static BOOL UploadGlyph(X11DRV_PDEVICE *physDev, int glyph)
511 {
512     int buflen;
513     char *buf;
514     Glyph gid;
515     GLYPHMETRICS gm;
516     XGlyphInfo gi;
517     gsCacheEntry *entry = glyphsetCache + physDev->xrender->cache_index;
518     UINT ggo_format = GGO_GLYPH_INDEX;
519     XRenderPictFormat pf;
520
521     if(entry->nrealized <= glyph) {
522         entry->nrealized = (glyph / 128 + 1) * 128;
523
524         if (entry->realized)
525             entry->realized = HeapReAlloc(GetProcessHeap(),
526                                       HEAP_ZERO_MEMORY,
527                                       entry->realized,
528                                       entry->nrealized * sizeof(BOOL));
529         else
530             entry->realized = HeapAlloc(GetProcessHeap(),
531                                       HEAP_ZERO_MEMORY,
532                                       entry->nrealized * sizeof(BOOL));
533
534         if(!X11DRV_XRender_Installed) {
535           if (entry->bitmaps)
536             entry->bitmaps = HeapReAlloc(GetProcessHeap(),
537                                       HEAP_ZERO_MEMORY,
538                                       entry->bitmaps,
539                                       entry->nrealized * sizeof(entry->bitmaps[0]));
540           else
541             entry->bitmaps = HeapAlloc(GetProcessHeap(),
542                                       HEAP_ZERO_MEMORY,
543                                       entry->nrealized * sizeof(entry->bitmaps[0]));
544
545           if (entry->gis)
546             entry->gis = HeapReAlloc(GetProcessHeap(),
547                                    HEAP_ZERO_MEMORY,
548                                    entry->gis,
549                                    entry->nrealized * sizeof(entry->gis[0]));
550           else
551             entry->gis = HeapAlloc(GetProcessHeap(),
552                                    HEAP_ZERO_MEMORY,
553                                    entry->nrealized * sizeof(entry->gis[0]));
554         }
555     }
556
557     switch(entry->aa) {
558     case AA_Grey:
559         ggo_format |= WINE_GGO_GRAY16_BITMAP;
560         break;
561
562     default:
563         ERR("aa = %d - not implemented\n", entry->aa);
564     case AA_None:
565         ggo_format |= GGO_BITMAP;
566         break;
567     }
568
569     buflen = GetGlyphOutlineW(physDev->hdc, glyph, ggo_format, &gm, 0, NULL,
570                               NULL);
571     if(buflen == GDI_ERROR) {
572         if(entry->aa != AA_None) {
573             entry->aa = AA_None;
574             ggo_format &= ~WINE_GGO_GRAY16_BITMAP;
575             ggo_format |= GGO_BITMAP;
576             buflen = GetGlyphOutlineW(physDev->hdc, glyph, ggo_format, &gm, 0, NULL,
577                                       NULL);
578         }
579         if(buflen == GDI_ERROR) {
580             LeaveCriticalSection(&xrender_cs);
581             return FALSE;
582         }
583         TRACE("Turning off antialiasing for this monochrome font\n");
584     }
585
586     if(entry->glyphset == 0 && X11DRV_XRender_Installed) {
587         switch(entry->aa) {
588         case AA_Grey:
589             pf.depth = 8;
590             pf.direct.alphaMask = 0xff;
591             break;
592
593         default:
594             ERR("aa = %d - not implemented\n", entry->aa);
595         case AA_None:
596             pf.depth = 1;
597             pf.direct.alphaMask = 1;
598             break;
599         }
600
601         pf.type = PictTypeDirect;
602         pf.direct.alpha = 0;
603
604         wine_tsx11_lock();
605         entry->font_format = pXRenderFindFormat(gdi_display,
606                                                 PictFormatType |
607                                                 PictFormatDepth |
608                                                 PictFormatAlpha |
609                                                 PictFormatAlphaMask,
610                                                 &pf, 0);
611
612         entry->glyphset = pXRenderCreateGlyphSet(gdi_display, entry->font_format);
613         wine_tsx11_unlock();
614     }
615
616
617     buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buflen);
618     GetGlyphOutlineW(physDev->hdc, glyph, ggo_format, &gm, buflen, buf, NULL);
619     entry->realized[glyph] = TRUE;
620
621     TRACE("buflen = %d. Got metrics: %dx%d adv=%d,%d origin=%ld,%ld\n",
622           buflen,
623           gm.gmBlackBoxX, gm.gmBlackBoxY, gm.gmCellIncX, gm.gmCellIncY,
624           gm.gmptGlyphOrigin.x, gm.gmptGlyphOrigin.y);
625
626     gi.width = gm.gmBlackBoxX;
627     gi.height = gm.gmBlackBoxY;
628     gi.x = -gm.gmptGlyphOrigin.x;
629     gi.y = gm.gmptGlyphOrigin.y;
630     gi.xOff = gm.gmCellIncX;
631     gi.yOff = gm.gmCellIncY;
632
633     if(TRACE_ON(xrender)) {
634         int pitch, i, j;
635         char output[300];
636         unsigned char *line;
637
638         if(entry->aa == AA_None) {
639             pitch = ((gi.width + 31) / 32) * 4;
640             for(i = 0; i < gi.height; i++) {
641                 line = buf + i * pitch;
642                 output[0] = '\0';
643                 for(j = 0; j < pitch * 8; j++) {
644                     strcat(output, (line[j / 8] & (1 << (7 - (j % 8)))) ? "#" : " ");
645                 }
646                 strcat(output, "\n");
647                 TRACE(output);
648             }
649         } else {
650             static const char blks[] = " .:;!o*#";
651             char str[2];
652
653             str[1] = '\0';
654             pitch = ((gi.width + 3) / 4) * 4;
655             for(i = 0; i < gi.height; i++) {
656                 line = buf + i * pitch;
657                 output[0] = '\0';
658                 for(j = 0; j < pitch; j++) {
659                     str[0] = blks[line[j] >> 5];
660                     strcat(output, str);
661                 }
662                 strcat(output, "\n");
663                 TRACE(output);
664             }
665         }
666     }
667
668     if(entry->glyphset) {
669         if(entry->aa == AA_None && BitmapBitOrder(gdi_display) != MSBFirst) {
670             unsigned char *byte = buf, c;
671             int i = buflen;
672
673             while(i--) {
674                 c = *byte;
675
676                 /* magic to flip bit order */
677                 c = ((c << 1) & 0xaa) | ((c >> 1) & 0x55);
678                 c = ((c << 2) & 0xcc) | ((c >> 2) & 0x33);
679                 c = ((c << 4) & 0xf0) | ((c >> 4) & 0x0f);
680
681                 *byte++ = c;
682             }
683         }
684         gid = glyph;
685         wine_tsx11_lock();
686         pXRenderAddGlyphs(gdi_display, entry->glyphset, &gid, &gi, 1,
687                           buf, buflen);
688         wine_tsx11_unlock();
689         HeapFree(GetProcessHeap(), 0, buf);
690     } else {
691         entry->bitmaps[glyph] = buf;
692         memcpy(&entry->gis[glyph], &gi, sizeof(gi));
693     }
694     return TRUE;
695 }
696
697 static void SharpGlyphMono(X11DRV_PDEVICE *physDev, INT x, INT y,
698                             void *bitmap, XGlyphInfo *gi)
699 {
700     unsigned char   *srcLine = bitmap, *src;
701     unsigned char   bits, bitsMask;
702     int             width = gi->width;
703     int             stride = ((width + 31) & ~31) >> 3;
704     int             height = gi->height;
705     int             w;
706     int             xspan, lenspan;
707
708     TRACE("%d, %d\n", x, y);
709     x -= gi->x;
710     y -= gi->y;
711     while (height--)
712     {
713         src = srcLine;
714         srcLine += stride;
715         w = width;
716         
717         bitsMask = 0x80;    /* FreeType is always MSB first */
718         bits = *src++;
719         
720         xspan = x;
721         while (w)
722         {
723             if (bits & bitsMask)
724             {
725                 lenspan = 0;
726                 do
727                 {
728                     lenspan++;
729                     if (lenspan == w)
730                         break;
731                     bitsMask = bitsMask >> 1;
732                     if (!bitsMask)
733                     {
734                         bits = *src++;
735                         bitsMask = 0x80;
736                     }
737                 } while (bits & bitsMask);
738                 XFillRectangle (gdi_display, physDev->drawable, 
739                                 physDev->gc, xspan, y, lenspan, 1);
740                 xspan += lenspan;
741                 w -= lenspan;
742             }
743             else
744             {
745                 do
746                 {
747                     w--;
748                     xspan++;
749                     if (!w)
750                         break;
751                     bitsMask = bitsMask >> 1;
752                     if (!bitsMask)
753                     {
754                         bits = *src++;
755                         bitsMask = 0x80;
756                     }
757                 } while (!(bits & bitsMask));
758             }
759         }
760         y++;
761     }
762 }
763
764 static void SharpGlyphGray(X11DRV_PDEVICE *physDev, INT x, INT y,
765                             void *bitmap, XGlyphInfo *gi)
766 {
767     unsigned char   *srcLine = bitmap, *src, bits;
768     int             width = gi->width;
769     int             stride = ((width + 3) & ~3);
770     int             height = gi->height;
771     int             w;
772     int             xspan, lenspan;
773
774     x -= gi->x;
775     y -= gi->y;
776     while (height--)
777     {
778         src = srcLine;
779         srcLine += stride;
780         w = width;
781         
782         bits = *src++;
783         xspan = x;
784         while (w)
785         {
786             if (bits >= 0x80)
787             {
788                 lenspan = 0;
789                 do
790                 {
791                     lenspan++;
792                     if (lenspan == w)
793                         break;
794                     bits = *src++;
795                 } while (bits >= 0x80);
796                 XFillRectangle (gdi_display, physDev->drawable, 
797                                 physDev->gc, xspan, y, lenspan, 1);
798                 xspan += lenspan;
799                 w -= lenspan;
800             }
801             else
802             {
803                 do
804                 {
805                     w--;
806                     xspan++;
807                     if (!w)
808                         break;
809                     bits = *src++;
810                 } while (bits < 0x80);
811             }
812         }
813         y++;
814     }
815 }
816
817
818 static void ExamineBitfield (DWORD mask, int *shift, int *len)
819 {
820     int s, l;
821
822     s = 0;
823     while ((mask & 1) == 0)
824     {
825         mask >>= 1;
826         s++;
827     }
828     l = 0;
829     while ((mask & 1) == 1)
830     {
831         mask >>= 1;
832         l++;
833     }
834     *shift = s;
835     *len = l;
836 }
837
838 static DWORD GetField (DWORD pixel, int shift, int len)
839 {
840     pixel = pixel & (((1 << (len)) - 1) << shift);
841     pixel = pixel << (32 - (shift + len)) >> 24;
842     while (len < 8)
843     {
844         pixel |= (pixel >> len);
845         len <<= 1;
846     }
847     return pixel;
848 }
849
850
851 static DWORD PutField (DWORD pixel, int shift, int len)
852 {
853     shift = shift - (8 - len);
854     if (len <= 8)
855         pixel &= (((1 << len) - 1) << (8 - len));
856     if (shift < 0)
857         pixel >>= -shift;
858     else
859         pixel <<= shift;
860     return pixel;
861 }
862
863 static void SmoothGlyphGray(XImage *image, int x, int y, void *bitmap, XGlyphInfo *gi,
864                             COLORREF color)
865 {
866     int             r_shift, r_len;
867     int             g_shift, g_len;
868     int             b_shift, b_len;
869     BYTE            *maskLine, *mask, m;
870     int             maskStride;
871     DWORD           pixel;
872     int             width, height;
873     int             w, tx;
874     BYTE            src_r, src_g, src_b;
875
876     src_r = GetRValue(color);
877     src_g = GetGValue(color);
878     src_b = GetBValue(color);
879
880     x -= gi->x;
881     y -= gi->y;
882     width = gi->width;
883     height = gi->height;
884
885     maskLine = (unsigned char *) bitmap;
886     maskStride = (width + 3) & ~3;
887
888     ExamineBitfield (image->red_mask, &r_shift, &r_len);
889     ExamineBitfield (image->green_mask, &g_shift, &g_len);
890     ExamineBitfield (image->blue_mask, &b_shift, &b_len);
891     for(; height--; y++)
892     {
893         mask = maskLine;
894         maskLine += maskStride;
895         w = width;
896         tx = x;
897
898         if(y < 0) continue;
899         if(y >= image->height) break;
900
901         for(; w--; tx++)
902         {
903             if(tx >= image->width) break;
904
905             m = *mask++;
906             if(tx < 0) continue;
907
908             if (m == 0xff)
909             {
910                 pixel = (PutField ((src_r), r_shift, r_len) |
911                          PutField ((src_g), g_shift, g_len) |
912                          PutField ((src_b), b_shift, b_len));
913                 XPutPixel (image, tx, y, pixel);
914             }
915             else if (m)
916             {
917                 BYTE r, g, b;
918
919                 pixel = XGetPixel (image, tx, y);
920
921                 r = GetField(pixel, r_shift, r_len);
922                 r = ((BYTE)~m * (WORD)r + (BYTE)m * (WORD)src_r) >> 8;
923                 g = GetField(pixel, g_shift, g_len);
924                 g = ((BYTE)~m * (WORD)g + (BYTE)m * (WORD)src_g) >> 8;
925                 b = GetField(pixel, b_shift, b_len);
926                 b = ((BYTE)~m * (WORD)b + (BYTE)m * (WORD)src_b) >> 8;
927
928                 pixel = (PutField (r, r_shift, r_len) |
929                          PutField (g, g_shift, g_len) |
930                          PutField (b, b_shift, b_len));
931                 XPutPixel (image, tx, y, pixel);
932             }
933         }
934     }
935 }
936
937 static int XRenderErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
938 {
939     return 1;
940 }
941
942 /***********************************************************************
943  *   X11DRV_XRender_ExtTextOut
944  */
945 BOOL X11DRV_XRender_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
946                                 const RECT *lprect, LPCWSTR wstr, UINT count,
947                                 const INT *lpDx, INT breakExtra )
948 {
949     XRenderColor col;
950     int idx;
951     TEXTMETRICW tm;
952     RGNDATA *data;
953     SIZE sz;
954     RECT rc;
955     BOOL done_extents = FALSE;
956     INT width, xwidth, ywidth;
957     double cosEsc, sinEsc;
958     XGCValues xgcval;
959     LOGFONTW lf;
960     int render_op = PictOpOver;
961     WORD *glyphs;
962     POINT pt;
963     gsCacheEntry *entry;
964     BOOL retv = FALSE;
965     HDC hdc = physDev->hdc;
966     int textPixel, backgroundPixel;
967     INT *deltas = NULL;
968     INT char_extra;
969     HRGN saved_region = 0;
970     UINT align = GetTextAlign( hdc );
971     COLORREF textColor = GetTextColor( hdc );
972
973     TRACE("%p, %d, %d, %08x, %p, %s, %d, %p)\n", hdc, x, y, flags,
974           lprect, debugstr_wn(wstr, count), count, lpDx);
975
976     if(flags & ETO_GLYPH_INDEX)
977         glyphs = (LPWORD)wstr;
978     else {
979         glyphs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WCHAR));
980         GetGlyphIndicesW(hdc, wstr, count, glyphs, 0);
981     }
982
983     if(lprect)
984       TRACE("rect: %ld,%ld - %ld,%ld\n", lprect->left, lprect->top, lprect->right,
985             lprect->bottom);
986     TRACE("align = %x bkmode = %x mapmode = %x\n", align, GetBkMode(hdc), GetMapMode(hdc));
987
988     if(align & TA_UPDATECP)
989     {
990         GetCurrentPositionEx( hdc, &pt );
991         x = pt.x;
992         y = pt.y;
993     }
994
995     GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);
996     if(lf.lfEscapement != 0) {
997         cosEsc = cos(lf.lfEscapement * M_PI / 1800);
998         sinEsc = sin(lf.lfEscapement * M_PI / 1800);
999     } else {
1000         cosEsc = 1;
1001         sinEsc = 0;
1002     }
1003
1004     if(flags & (ETO_CLIPPED | ETO_OPAQUE)) {
1005         if(!lprect) {
1006             if(flags & ETO_CLIPPED) return FALSE;
1007                 GetTextExtentPointI(hdc, glyphs, count, &sz);
1008                 done_extents = TRUE;
1009                 rc.left = x;
1010                 rc.top = y;
1011                 rc.right = x + sz.cx;
1012                 rc.bottom = y + sz.cy;
1013         } else {
1014             rc = *lprect;
1015         }
1016
1017         LPtoDP(hdc, (POINT*)&rc, 2);
1018
1019         if(rc.left > rc.right) {INT tmp = rc.left; rc.left = rc.right; rc.right = tmp;}
1020         if(rc.top > rc.bottom) {INT tmp = rc.top; rc.top = rc.bottom; rc.bottom = tmp;}
1021     }
1022
1023     xgcval.function = GXcopy;
1024     xgcval.background = physDev->backgroundPixel;
1025     xgcval.fill_style = FillSolid;
1026     wine_tsx11_lock();
1027     XChangeGC( gdi_display, physDev->gc, GCFunction | GCBackground | GCFillStyle, &xgcval );
1028     wine_tsx11_unlock();
1029
1030     X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
1031
1032     if(physDev->depth == 1) {
1033         if((textColor & 0xffffff) == 0) {
1034             textPixel = 0;
1035             backgroundPixel = 1;
1036         } else {
1037             textPixel = 1;
1038             backgroundPixel = 0;
1039         }
1040     } else {
1041         textPixel = physDev->textPixel;
1042         backgroundPixel = physDev->backgroundPixel;
1043     }
1044
1045     if(flags & ETO_OPAQUE) {
1046         wine_tsx11_lock();
1047         XSetForeground( gdi_display, physDev->gc, backgroundPixel );
1048         XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
1049                         physDev->org.x + rc.left, physDev->org.y + rc.top,
1050                         rc.right - rc.left, rc.bottom - rc.top );
1051         wine_tsx11_unlock();
1052     }
1053
1054     if(count == 0) {
1055         retv =  TRUE;
1056         goto done;
1057     }
1058
1059     pt.x = x;
1060     pt.y = y;
1061     LPtoDP(hdc, &pt, 1);
1062     x = pt.x;
1063     y = pt.y;
1064
1065     TRACE("real x,y %d,%d\n", x, y);
1066
1067     if((char_extra = GetTextCharacterExtra(hdc)) != 0) {
1068         INT i;
1069         SIZE tmpsz;
1070         deltas = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
1071         for(i = 0; i < count; i++) {
1072             if(lpDx)
1073                 deltas[i] = lpDx[i] + char_extra;
1074             else {
1075                 GetTextExtentPointI(hdc, glyphs + i, 1, &tmpsz);
1076                 deltas[i] = tmpsz.cx;
1077             }
1078         }
1079     } else if(lpDx)
1080         deltas = (INT*)lpDx;
1081
1082     if(deltas) {
1083         width = 0;
1084         for(idx = 0; idx < count; idx++)
1085             width += deltas[idx];
1086     } else {
1087         if(!done_extents) {
1088             GetTextExtentPointI(hdc, glyphs, count, &sz);
1089             done_extents = TRUE;
1090         }
1091         width = sz.cx;
1092     }
1093     width = X11DRV_XWStoDS(physDev, width);
1094     xwidth = width * cosEsc;
1095     ywidth = width * sinEsc;
1096
1097     GetTextMetricsW(hdc, &tm);
1098
1099     tm.tmAscent = abs(X11DRV_YWStoDS(physDev, tm.tmAscent));
1100     tm.tmDescent = abs(X11DRV_YWStoDS(physDev, tm.tmDescent));
1101     switch( align & (TA_LEFT | TA_RIGHT | TA_CENTER) ) {
1102     case TA_LEFT:
1103         if (align & TA_UPDATECP) {
1104             pt.x = x + xwidth;
1105             pt.y = y - ywidth;
1106             DPtoLP(hdc, &pt, 1);
1107             MoveToEx(hdc, pt.x, pt.y, NULL);
1108         }
1109         break;
1110
1111     case TA_CENTER:
1112         x -= xwidth / 2;
1113         y += ywidth / 2;
1114         break;
1115
1116     case TA_RIGHT:
1117         x -= xwidth;
1118         y += ywidth;
1119         if (align & TA_UPDATECP) {
1120             pt.x = x;
1121             pt.y = y;
1122             DPtoLP(hdc, &pt, 1);
1123             MoveToEx(hdc, pt.x, pt.y, NULL);
1124         }
1125         break;
1126     }
1127
1128     switch( align & (TA_TOP | TA_BOTTOM | TA_BASELINE) ) {
1129     case TA_TOP:
1130         y += tm.tmAscent * cosEsc;
1131         x += tm.tmAscent * sinEsc;
1132         break;
1133
1134     case TA_BOTTOM:
1135         y -= tm.tmDescent * cosEsc;
1136         x -= tm.tmDescent * sinEsc;
1137         break;
1138
1139     case TA_BASELINE:
1140         break;
1141     }
1142
1143     if (flags & ETO_CLIPPED)
1144     {
1145         HRGN clip_region;
1146         RECT clip_rect = *lprect;
1147
1148         LPtoDP( hdc, (POINT *)&clip_rect, 2 );
1149         clip_region = CreateRectRgnIndirect( &clip_rect );
1150         /* make a copy of the current device region */
1151         saved_region = CreateRectRgn( 0, 0, 0, 0 );
1152         CombineRgn( saved_region, physDev->region, 0, RGN_COPY );
1153         X11DRV_SetDeviceClipping( physDev, saved_region, clip_region );
1154         DeleteObject( clip_region );
1155     }
1156
1157     if(X11DRV_XRender_Installed) {
1158         if(!physDev->xrender->pict) {
1159             XRenderPictureAttributes pa;
1160             pa.subwindow_mode = IncludeInferiors;
1161
1162             wine_tsx11_lock();
1163             physDev->xrender->pict = pXRenderCreatePicture(gdi_display,
1164                                                            physDev->drawable,
1165                                                            (physDev->depth == 1) ?
1166                                                            mono_format : screen_format,
1167                                                            CPSubwindowMode, &pa);
1168             wine_tsx11_unlock();
1169
1170             TRACE("allocing pict = %lx dc = %p drawable = %08lx\n",
1171                   physDev->xrender->pict, hdc, physDev->drawable);
1172         } else {
1173             TRACE("using existing pict = %lx dc = %p drawable = %08lx\n",
1174                   physDev->xrender->pict, hdc, physDev->drawable);
1175         }
1176
1177         if ((data = X11DRV_GetRegionData( physDev->region, 0 )))
1178         {
1179             wine_tsx11_lock();
1180             pXRenderSetPictureClipRectangles( gdi_display, physDev->xrender->pict,
1181                                               physDev->org.x, physDev->org.y,
1182                                               (XRectangle *)data->Buffer, data->rdh.nCount );
1183             wine_tsx11_unlock();
1184             HeapFree( GetProcessHeap(), 0, data );
1185         }
1186     }
1187
1188     if(GetBkMode(hdc) != TRANSPARENT) {
1189         if(!((flags & ETO_CLIPPED) && (flags & ETO_OPAQUE))) {
1190             if(!(flags & ETO_OPAQUE) || x < rc.left || x + width >= rc.right ||
1191                y - tm.tmAscent < rc.top || y + tm.tmDescent >= rc.bottom) {
1192                 wine_tsx11_lock();
1193                 XSetForeground( gdi_display, physDev->gc, backgroundPixel );
1194                 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
1195                                 physDev->org.x + x, physDev->org.y + y - tm.tmAscent,
1196                                 width, tm.tmAscent + tm.tmDescent );
1197                 wine_tsx11_unlock();
1198             }
1199         }
1200     }
1201
1202     if(X11DRV_XRender_Installed) {
1203         /* Create a 1x1 pixmap to tile over the font mask */
1204         if(!physDev->xrender->tile_xpm) {
1205             XRenderPictureAttributes pa;
1206
1207             XRenderPictFormat *format = (physDev->depth == 1) ? mono_format : screen_format;
1208             wine_tsx11_lock();
1209             physDev->xrender->tile_xpm = XCreatePixmap(gdi_display,
1210                                                        physDev->drawable,
1211                                                        1, 1,
1212                                                        format->depth);
1213             pa.repeat = True;
1214             physDev->xrender->tile_pict = pXRenderCreatePicture(gdi_display,
1215                                                                 physDev->xrender->tile_xpm,
1216                                                                 format,
1217                                                                 CPRepeat, &pa);
1218             wine_tsx11_unlock();
1219             TRACE("Created pixmap of depth %d\n", format->depth);
1220             /* init lastTextColor to something different from textColor */
1221             physDev->xrender->lastTextColor = ~textColor;
1222
1223         }
1224
1225         if(textColor != physDev->xrender->lastTextColor) {
1226             if(physDev->depth != 1) {
1227               /* Map 0 -- 0xff onto 0 -- 0xffff */
1228                 col.red = GetRValue(textColor);
1229                 col.red |= col.red << 8;
1230                 col.green = GetGValue(textColor);
1231                 col.green |= col.green << 8;
1232                 col.blue = GetBValue(textColor);
1233                 col.blue |= col.blue << 8;
1234                 col.alpha = 0x0;
1235             } else { /* for a 1bpp bitmap we always need a 1 in the tile */
1236                 col.red = col.green = col.blue = 0;
1237                 col.alpha = 0xffff;
1238             }
1239             wine_tsx11_lock();
1240             pXRenderFillRectangle(gdi_display, PictOpSrc,
1241                                   physDev->xrender->tile_pict,
1242                                   &col, 0, 0, 1, 1);
1243             wine_tsx11_unlock();
1244             physDev->xrender->lastTextColor = textColor;
1245         }
1246
1247         /* FIXME the mapping of Text/BkColor onto 1 or 0 needs investigation.
1248          */
1249         if((physDev->depth == 1) && (textPixel == 0))
1250             render_op = PictOpOutReverse; /* This gives us 'black' text */
1251     }
1252
1253     EnterCriticalSection(&xrender_cs);
1254     entry = glyphsetCache + physDev->xrender->cache_index;
1255
1256     for(idx = 0; idx < count; idx++) {
1257         if(glyphs[idx] >= entry->nrealized || entry->realized[glyphs[idx]] == FALSE) {
1258             UploadGlyph(physDev, glyphs[idx]);
1259         }
1260     }
1261
1262
1263     TRACE("Writing %s at %ld,%ld\n", debugstr_wn(wstr,count),
1264           physDev->org.x + x, physDev->org.y + y);
1265
1266     if(X11DRV_XRender_Installed) {
1267         wine_tsx11_lock();
1268         if(!deltas)
1269             pXRenderCompositeString16(gdi_display, render_op,
1270                                       physDev->xrender->tile_pict,
1271                                       physDev->xrender->pict,
1272                                       entry->font_format, entry->glyphset,
1273                                       0, 0, physDev->org.x + x, physDev->org.y + y,
1274                                       glyphs, count);
1275
1276         else {
1277             INT offset = 0, xoff = 0, yoff = 0;
1278             for(idx = 0; idx < count; idx++) {
1279                 pXRenderCompositeString16(gdi_display, render_op,
1280                                           physDev->xrender->tile_pict,
1281                                           physDev->xrender->pict,
1282                                           entry->font_format, entry->glyphset,
1283                                           0, 0, physDev->org.x + x + xoff,
1284                                           physDev->org.y + y + yoff,
1285                                           glyphs + idx, 1);
1286                 offset += X11DRV_XWStoDS(physDev, deltas[idx]);
1287                 xoff = offset * cosEsc;
1288                 yoff = offset * -sinEsc;
1289             }
1290         }
1291         wine_tsx11_unlock();
1292
1293         if (lf.lfUnderline || lf.lfStrikeOut) {
1294             int linePos;
1295             unsigned int lineWidth;
1296             UINT nMetricsSize = GetOutlineTextMetricsW(hdc, 0, NULL);
1297             OUTLINETEXTMETRICW* otm = HeapAlloc(GetProcessHeap(), 0, nMetricsSize);
1298             if (!otm) goto done;
1299
1300             GetOutlineTextMetricsW(hdc, nMetricsSize, otm);
1301
1302             wine_tsx11_lock();
1303             XSetForeground( gdi_display, physDev->gc, physDev->textPixel );
1304
1305             if (lf.lfUnderline) {
1306                 linePos = X11DRV_YWStoDS(physDev, otm->otmsUnderscorePosition);
1307                 lineWidth = X11DRV_YWStoDS(physDev, otm->otmsUnderscoreSize);
1308
1309                 XSetLineAttributes( gdi_display, physDev->gc, lineWidth,
1310                                     LineSolid, CapProjecting, JoinBevel );
1311                 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
1312                            physDev->org.x + x - linePos * sinEsc,
1313                            physDev->org.y + y - linePos * cosEsc,
1314                            physDev->org.x + x + width * cosEsc - linePos * sinEsc,
1315                            physDev->org.y + y - width * sinEsc - linePos * cosEsc );
1316             }
1317
1318             if (lf.lfStrikeOut) { 
1319                 linePos = X11DRV_YWStoDS(physDev, otm->otmsStrikeoutPosition);
1320                 lineWidth = X11DRV_YWStoDS(physDev, otm->otmsStrikeoutSize);
1321
1322                 XSetLineAttributes( gdi_display, physDev->gc, lineWidth,
1323                                     LineSolid, CapProjecting, JoinBevel );
1324                 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
1325                            physDev->org.x + x, physDev->org.y + y - linePos,
1326                            physDev->org.x + x + width, physDev->org.y + y - linePos );
1327             } 
1328             wine_tsx11_unlock();
1329             HeapFree(GetProcessHeap(), 0, otm);
1330         }
1331
1332     } else {
1333         INT offset = 0, xoff = 0, yoff = 0;
1334         wine_tsx11_lock();
1335         XSetForeground( gdi_display, physDev->gc, textPixel );
1336
1337         if(entry->aa == AA_None) {
1338             for(idx = 0; idx < count; idx++) {
1339                 SharpGlyphMono(physDev, physDev->org.x + x + xoff,
1340                                physDev->org.y + y + yoff,
1341                                entry->bitmaps[glyphs[idx]],
1342                                &entry->gis[glyphs[idx]]);
1343                 if(deltas) {
1344                     offset += X11DRV_XWStoDS(physDev, deltas[idx]);
1345                     xoff = offset * cosEsc;
1346                     yoff = offset * -sinEsc;
1347
1348                 } else {
1349                     xoff += entry->gis[glyphs[idx]].xOff;
1350                     yoff += entry->gis[glyphs[idx]].yOff;
1351                 }
1352             }
1353         } else if(physDev->depth == 1) {
1354             for(idx = 0; idx < count; idx++) {
1355                 SharpGlyphGray(physDev, physDev->org.x + x + xoff,
1356                                physDev->org.y + y + yoff,
1357                                entry->bitmaps[glyphs[idx]],
1358                                &entry->gis[glyphs[idx]]);
1359                 if(deltas) {
1360                     offset += X11DRV_XWStoDS(physDev, deltas[idx]);
1361                     xoff = offset * cosEsc;
1362                     yoff = offset * -sinEsc;
1363
1364                 } else {
1365                     xoff += entry->gis[glyphs[idx]].xOff;
1366                     yoff += entry->gis[glyphs[idx]].yOff;
1367                 }
1368                     
1369             }
1370         } else {
1371             XImage *image;
1372             unsigned int w, h, dummy_uint;
1373             Window dummy_window;
1374             int dummy_int;
1375             int image_x, image_y, image_off_x, image_off_y, image_w, image_h;
1376             RECT extents = {0, 0, 0, 0};
1377             POINT cur = {0, 0};
1378             
1379             
1380             XGetGeometry(gdi_display, physDev->drawable, &dummy_window, &dummy_int, &dummy_int,
1381                          &w, &h, &dummy_uint, &dummy_uint);
1382             TRACE("drawable %dx%d\n", w, h);
1383
1384             for(idx = 0; idx < count; idx++) {
1385                 if(extents.left > cur.x - entry->gis[glyphs[idx]].x)
1386                     extents.left = cur.x - entry->gis[glyphs[idx]].x;
1387                 if(extents.top > cur.y - entry->gis[glyphs[idx]].y)
1388                     extents.top = cur.y - entry->gis[glyphs[idx]].y;
1389                 if(extents.right < cur.x - entry->gis[glyphs[idx]].x + entry->gis[glyphs[idx]].width)
1390                     extents.right = cur.x - entry->gis[glyphs[idx]].x + entry->gis[glyphs[idx]].width;
1391                 if(extents.bottom < cur.y - entry->gis[glyphs[idx]].y + entry->gis[glyphs[idx]].height)
1392                     extents.bottom = cur.y - entry->gis[glyphs[idx]].y + entry->gis[glyphs[idx]].height;
1393                 if(deltas) {
1394                     offset += X11DRV_XWStoDS(physDev, deltas[idx]);
1395                     cur.x = offset * cosEsc;
1396                     cur.y = offset * -sinEsc;
1397                 } else {
1398                     cur.x += entry->gis[glyphs[idx]].xOff;
1399                     cur.y += entry->gis[glyphs[idx]].yOff;
1400                 }
1401             }
1402             TRACE("glyph extents %ld,%ld - %ld,%ld drawable x,y %ld,%ld\n", extents.left, extents.top,
1403                   extents.right, extents.bottom, physDev->org.x + x, physDev->org.y + y);
1404
1405             if(physDev->org.x + x + extents.left >= 0) {
1406                 image_x = physDev->org.x + x + extents.left;
1407                 image_off_x = 0;
1408             } else {
1409                 image_x = 0;
1410                 image_off_x = physDev->org.x + x + extents.left;
1411             }
1412             if(physDev->org.y + y + extents.top >= 0) {
1413                 image_y = physDev->org.y + y + extents.top;
1414                 image_off_y = 0;
1415             } else {
1416                 image_y = 0;
1417                 image_off_y = physDev->org.y + y + extents.top;
1418             }
1419             if(physDev->org.x + x + extents.right < w)
1420                 image_w = physDev->org.x + x + extents.right - image_x;
1421             else
1422                 image_w = w - image_x;
1423             if(physDev->org.y + y + extents.bottom < h)
1424                 image_h = physDev->org.y + y + extents.bottom - image_y;
1425             else
1426                 image_h = h - image_y;
1427
1428             if(image_w <= 0 || image_h <= 0) goto no_image;
1429
1430             X11DRV_expect_error(gdi_display, XRenderErrorHandler, NULL);
1431             image = XGetImage(gdi_display, physDev->drawable,
1432                               image_x, image_y, image_w, image_h,
1433                               AllPlanes, ZPixmap);
1434             X11DRV_check_error();
1435
1436             TRACE("XGetImage(%p, %x, %d, %d, %d, %d, %lx, %x) depth = %d rets %p\n",
1437                   gdi_display, (int)physDev->drawable, image_x, image_y,
1438                   image_w, image_h, AllPlanes, ZPixmap,
1439                   physDev->depth, image);
1440             if(!image) {
1441                 Pixmap xpm = XCreatePixmap(gdi_display, physDev->drawable, image_w, image_h,
1442                                            physDev->depth);
1443                 GC gc;
1444                 XGCValues gcv;
1445
1446                 gcv.graphics_exposures = False;
1447                 gc = XCreateGC(gdi_display, xpm, GCGraphicsExposures, &gcv);
1448                 XCopyArea(gdi_display, physDev->drawable, xpm, gc, image_x, image_y,
1449                           image_w, image_h, 0, 0);
1450                 XFreeGC(gdi_display, gc);
1451                 X11DRV_expect_error(gdi_display, XRenderErrorHandler, NULL);
1452                 image = XGetImage(gdi_display, xpm, 0, 0, image_w, image_h, AllPlanes,
1453                                   ZPixmap);
1454                 X11DRV_check_error();
1455                 XFreePixmap(gdi_display, xpm);
1456             }
1457             if(!image) goto no_image;
1458
1459             image->red_mask = visual->red_mask;
1460             image->green_mask = visual->green_mask;
1461             image->blue_mask = visual->blue_mask;
1462
1463             offset = xoff = yoff = 0;
1464             for(idx = 0; idx < count; idx++) {
1465                 SmoothGlyphGray(image, xoff + image_off_x - extents.left,
1466                                 yoff + image_off_y - extents.top,
1467                                 entry->bitmaps[glyphs[idx]],
1468                                 &entry->gis[glyphs[idx]],
1469                                 textColor);
1470                 if(deltas) {
1471                     offset += X11DRV_XWStoDS(physDev, deltas[idx]);
1472                     xoff = offset * cosEsc;
1473                     yoff = offset * -sinEsc;
1474                 } else {
1475                     xoff += entry->gis[glyphs[idx]].xOff;
1476                     yoff += entry->gis[glyphs[idx]].yOff;
1477                 }
1478                     
1479             }
1480             XPutImage(gdi_display, physDev->drawable, physDev->gc, image, 0, 0,
1481                       image_x, image_y, image_w, image_h);
1482             XDestroyImage(image);
1483         }
1484     no_image:
1485         wine_tsx11_unlock();
1486     }
1487     LeaveCriticalSection(&xrender_cs);
1488
1489     if(deltas && deltas != lpDx)
1490         HeapFree(GetProcessHeap(), 0, deltas);
1491
1492     if (flags & ETO_CLIPPED)
1493     {
1494         /* restore the device region */
1495         X11DRV_SetDeviceClipping( physDev, saved_region, 0 );
1496         DeleteObject( saved_region );
1497     }
1498
1499     retv = TRUE;
1500
1501 done:
1502     X11DRV_UnlockDIBSection( physDev, TRUE );
1503     if(glyphs != wstr) HeapFree(GetProcessHeap(), 0, glyphs);
1504     return retv;
1505 }
1506
1507 #else /* HAVE_X11_EXTENSIONS_XRENDER_H */
1508
1509 void X11DRV_XRender_Init(void)
1510 {
1511     TRACE("XRender support not compiled in.\n");
1512     return;
1513 }
1514
1515 void X11DRV_XRender_Finalize(void)
1516 {
1517 }
1518
1519 BOOL X11DRV_XRender_SelectFont(X11DRV_PDEVICE *physDev, HFONT hfont)
1520 {
1521   assert(0);
1522   return FALSE;
1523 }
1524
1525 void X11DRV_XRender_DeleteDC(X11DRV_PDEVICE *physDev)
1526 {
1527   assert(0);
1528   return;
1529 }
1530
1531 BOOL X11DRV_XRender_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
1532                                 const RECT *lprect, LPCWSTR wstr, UINT count,
1533                                 const INT *lpDx, INT breakExtra )
1534 {
1535   assert(0);
1536   return FALSE;
1537 }
1538
1539 void X11DRV_XRender_UpdateDrawable(X11DRV_PDEVICE *physDev)
1540 {
1541   assert(0);
1542   return;
1543 }
1544
1545 #endif /* HAVE_X11_EXTENSIONS_XRENDER_H */