Don't try to rotate bitmap fonts.
[wine/multimedia.git] / dlls / x11drv / xrender.c
blob5fce5a7774f8b7205f63935657a33fd3c4e1f4a8
1 /*
2 * Functions to use the XRender extension
4 * Copyright 2001, 2002 Huw D M Davies for CodeWeavers
6 * Some parts also:
7 * Copyright 2000 Keith Packard, member of The XFree86 Project, Inc.
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.
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.
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
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <stdlib.h>
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"
40 static BOOL X11DRV_XRender_Installed = FALSE;
41 int using_client_side_fonts = FALSE;
43 WINE_DEFAULT_DEBUG_CHANNEL(xrender);
45 #ifdef HAVE_X11_EXTENSIONS_XRENDER_H
47 #include <X11/Xlib.h>
48 #include <X11/extensions/Xrender.h>
50 /* Older version of the Xrender headers don't define these */
51 #ifndef PictStandardARGB32
53 #define PictStandardARGB32 0
54 XRenderPictFormat * XRenderFindStandardFormat (Display *dpy, int format);
56 #endif
58 static XRenderPictFormat *screen_format; /* format of screen */
59 static XRenderPictFormat *mono_format; /* format of mono bitmap */
61 typedef struct
63 LOGFONTW lf;
64 SIZE devsize; /* size in device coords */
65 DWORD hash;
66 } LFANDSIZE;
68 #define INITIAL_REALIZED_BUF_SIZE 128
70 typedef enum { AA_None, AA_Grey, AA_RGB, AA_BGR, AA_VRGB, AA_VBGR } AA_Type;
72 typedef struct
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;
86 struct tagXRENDERINFO
88 int cache_index;
89 Picture pict;
90 Picture tile_pict;
91 Pixmap tile_xpm;
92 COLORREF lastTextColor;
96 static gsCacheEntry *glyphsetCache = NULL;
97 static DWORD glyphsetCacheSize = 0;
98 static INT lastfree = -1;
99 static INT mru = -1;
101 #define INIT_CACHE_SIZE 10
103 static int antialias = 1;
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
116 static void *xrender_handle;
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
136 static CRITICAL_SECTION xrender_cs;
137 static CRITICAL_SECTION_DEBUG critsect_debug =
139 0, 0, &xrender_cs,
140 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
141 0, 0, { 0, (DWORD)(__FILE__ ": xrender_cs") }
143 static CRITICAL_SECTION xrender_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
146 /***********************************************************************
147 * X11DRV_XRender_Init
149 * Let's see if our XServer has the extension available
152 void X11DRV_XRender_Init(void)
154 int error_base, event_base, i;
155 XRenderPictFormat pf;
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)))
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
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;
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;
207 wine_tsx11_unlock();
210 sym_not_found:
211 if(X11DRV_XRender_Installed || client_side_with_core)
213 glyphsetCache = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
214 sizeof(*glyphsetCache) * INIT_CACHE_SIZE);
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;
222 glyphsetCache[i-1].next = -1;
223 using_client_side_fonts = 1;
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;
234 else TRACE("Using X11 core fonts\n");
237 static BOOL fontcmp(LFANDSIZE *p1, LFANDSIZE *p2)
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);
245 #if 0
246 static void walk_cache(void)
248 int i;
250 EnterCriticalSection(&xrender_cs);
251 for(i=mru; i >= 0; i = glyphsetCache[i].next)
252 TRACE("item %d\n", i);
253 LeaveCriticalSection(&xrender_cs);
255 #endif
257 static int LookupEntry(LFANDSIZE *plfsz)
259 int i, prev_i = -1;
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;
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;
275 TRACE("found font in cache %d\n", i);
276 return i;
278 prev_i = i;
280 TRACE("font not in cache\n");
281 return -1;
284 static void FreeEntry(int entry)
286 int i;
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;
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;
306 glyphsetCache[entry].nrealized = 0;
310 static int AllocEntry(void)
312 int best = -1, prev_best = -1, i, prev_i = -1;
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;
323 TRACE("empty space at %d, next lastfree = %d\n", mru, lastfree);
324 return mru;
327 for(i = mru; i >= 0; i = glyphsetCache[i].next) {
328 if(glyphsetCache[i].count == 0) {
329 best = i;
330 prev_best = prev_i;
332 prev_i = i;
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);
346 return mru;
349 TRACE("Growing cache\n");
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));
361 for(best = i = glyphsetCacheSize; i < glyphsetCacheSize + INIT_CACHE_SIZE;
362 i++) {
363 glyphsetCache[i].next = i + 1;
364 glyphsetCache[i].count = -1;
366 glyphsetCache[i-1].next = -1;
367 glyphsetCacheSize += INIT_CACHE_SIZE;
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;
377 static int GetCacheEntry(LFANDSIZE *plfsz)
379 int ret;
380 gsCacheEntry *entry;
382 if((ret = LookupEntry(plfsz)) != -1) return ret;
384 ret = AllocEntry();
385 entry = glyphsetCache + ret;
386 entry->lfsz = *plfsz;
387 assert(entry->nrealized == 0);
389 if(antialias)
390 entry->aa = AA_Grey;
391 else
392 entry->aa = AA_None;
394 entry->font_format = NULL;
395 entry->glyphset = 0;
396 return ret;
399 static void dec_ref_cache(int index)
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--;
407 static void lfsz_calc_hash(LFANDSIZE *plfsz)
409 DWORD hash = 0, *ptr;
410 int i;
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;
423 plfsz->hash = hash;
424 return;
427 /***********************************************************************
428 * X11DRV_XRender_Finalize
430 void X11DRV_XRender_Finalize(void)
432 int i;
434 EnterCriticalSection(&xrender_cs);
435 for(i = mru; i >= 0; i = glyphsetCache[i].next)
436 FreeEntry(i);
437 LeaveCriticalSection(&xrender_cs);
441 /***********************************************************************
442 * X11DRV_XRender_SelectFont
444 BOOL X11DRV_XRender_SelectFont(X11DRV_PDEVICE *physDev, HFONT hfont)
446 LFANDSIZE lfsz;
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);
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;
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;
469 /***********************************************************************
470 * X11DRV_XRender_DeleteDC
472 void X11DRV_XRender_DeleteDC(X11DRV_PDEVICE *physDev)
474 wine_tsx11_lock();
475 if(physDev->xrender->tile_pict)
476 pXRenderFreePicture(gdi_display, physDev->xrender->tile_pict);
478 if(physDev->xrender->tile_xpm)
479 XFreePixmap(gdi_display, physDev->xrender->tile_xpm);
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);
485 wine_tsx11_unlock();
487 EnterCriticalSection(&xrender_cs);
488 if(physDev->xrender->cache_index != -1)
489 dec_ref_cache(physDev->xrender->cache_index);
490 LeaveCriticalSection(&xrender_cs);
492 HeapFree(GetProcessHeap(), 0, physDev->xrender);
493 physDev->xrender = NULL;
494 return;
497 /***********************************************************************
498 * X11DRV_XRender_UpdateDrawable
500 * This gets called from X11DRV_SetDrawable and X11DRV_SelectBitmap.
501 * It deletes the pict when the drawable changes.
503 void X11DRV_XRender_UpdateDrawable(X11DRV_PDEVICE *physDev)
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();
513 physDev->xrender->pict = 0;
514 return;
517 /************************************************************************
518 * UploadGlyph
520 * Helper to ExtTextOut. Must be called inside xrender_cs
522 static BOOL UploadGlyph(X11DRV_PDEVICE *physDev, int glyph)
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;
533 if(entry->nrealized <= glyph) {
534 entry->nrealized = (glyph / 128 + 1) * 128;
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));
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]));
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]));
569 switch(entry->aa) {
570 case AA_Grey:
571 ggo_format |= WINE_GGO_GRAY16_BITMAP;
572 break;
574 default:
575 ERR("aa = %d - not implemented\n", entry->aa);
576 case AA_None:
577 ggo_format |= GGO_BITMAP;
578 break;
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);
591 if(buflen == GDI_ERROR) {
592 ERR("GetGlyphOutlineW failed\n");
593 return FALSE;
595 TRACE("Turning off antialiasing for this monochrome font\n");
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;
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;
613 pf.type = PictTypeDirect;
614 pf.direct.alpha = 0;
616 wine_tsx11_lock();
617 entry->font_format = pXRenderFindFormat(gdi_display,
618 PictFormatType |
619 PictFormatDepth |
620 PictFormatAlpha |
621 PictFormatAlphaMask,
622 &pf, 0);
624 entry->glyphset = pXRenderCreateGlyphSet(gdi_display, entry->font_format);
625 wine_tsx11_unlock();
629 buf = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, buflen);
630 GetGlyphOutlineW(physDev->hdc, glyph, ggo_format, &gm, buflen, buf, NULL);
631 entry->realized[glyph] = TRUE;
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);
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;
645 if(TRACE_ON(xrender)) {
646 int pitch, i, j;
647 char output[300];
648 unsigned char *line;
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)))) ? "#" : " ");
658 strcat(output, "\n");
659 TRACE(output);
661 } else {
662 static const char blks[] = " .:;!o*#";
663 char str[2];
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);
674 strcat(output, "\n");
675 TRACE(output);
680 if(entry->glyphset) {
681 if(entry->aa == AA_None && BitmapBitOrder(gdi_display) != MSBFirst) {
682 unsigned char *byte = buf, c;
683 int i = buflen;
685 while(i--) {
686 c = *byte;
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);
693 *byte++ = c;
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));
706 return TRUE;
709 static void SharpGlyphMono(X11DRV_PDEVICE *physDev, INT x, INT y,
710 void *bitmap, XGlyphInfo *gi)
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;
720 TRACE("%d, %d\n", x, y);
721 x -= gi->x;
722 y -= gi->y;
723 while (height--)
725 src = srcLine;
726 srcLine += stride;
727 w = width;
729 bitsMask = 0x80; /* FreeType is always MSB first */
730 bits = *src++;
732 xspan = x;
733 while (w)
735 if (bits & bitsMask)
737 lenspan = 0;
740 lenspan++;
741 if (lenspan == w)
742 break;
743 bitsMask = bitsMask >> 1;
744 if (!bitsMask)
746 bits = *src++;
747 bitsMask = 0x80;
749 } while (bits & bitsMask);
750 XFillRectangle (gdi_display, physDev->drawable,
751 physDev->gc, xspan, y, lenspan, 1);
752 xspan += lenspan;
753 w -= lenspan;
755 else
759 w--;
760 xspan++;
761 if (!w)
762 break;
763 bitsMask = bitsMask >> 1;
764 if (!bitsMask)
766 bits = *src++;
767 bitsMask = 0x80;
769 } while (!(bits & bitsMask));
772 y++;
776 static void SharpGlyphGray(X11DRV_PDEVICE *physDev, INT x, INT y,
777 void *bitmap, XGlyphInfo *gi)
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;
786 x -= gi->x;
787 y -= gi->y;
788 while (height--)
790 src = srcLine;
791 srcLine += stride;
792 w = width;
794 bits = *src++;
795 xspan = x;
796 while (w)
798 if (bits >= 0x80)
800 lenspan = 0;
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;
813 else
817 w--;
818 xspan++;
819 if (!w)
820 break;
821 bits = *src++;
822 } while (bits < 0x80);
825 y++;
830 static void ExamineBitfield (DWORD mask, int *shift, int *len)
832 int s, l;
834 s = 0;
835 while ((mask & 1) == 0)
837 mask >>= 1;
838 s++;
840 l = 0;
841 while ((mask & 1) == 1)
843 mask >>= 1;
844 l++;
846 *shift = s;
847 *len = l;
850 static DWORD GetField (DWORD pixel, int shift, int len)
852 pixel = pixel & (((1 << (len)) - 1) << shift);
853 pixel = pixel << (32 - (shift + len)) >> 24;
854 while (len < 8)
856 pixel |= (pixel >> len);
857 len <<= 1;
859 return pixel;
863 static DWORD PutField (DWORD pixel, int shift, int len)
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;
875 static void SmoothGlyphGray(XImage *image, int x, int y, void *bitmap, XGlyphInfo *gi,
876 COLORREF color)
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;
888 src_r = GetRValue(color);
889 src_g = GetGValue(color);
890 src_b = GetBValue(color);
892 x -= gi->x;
893 y -= gi->y;
894 width = gi->width;
895 height = gi->height;
897 maskLine = (unsigned char *) bitmap;
898 maskStride = (width + 3) & ~3;
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++)
905 mask = maskLine;
906 maskLine += maskStride;
907 w = width;
908 tx = x;
910 if(y < 0) continue;
911 if(y >= image->height) break;
913 for(; w--; tx++)
915 if(tx >= image->width) break;
917 m = *mask++;
918 if(tx < 0) continue;
920 if (m == 0xff)
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);
927 else if (m)
929 BYTE r, g, b;
931 pixel = XGetPixel (image, tx, y);
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;
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);
949 static int XRenderErrorHandler(Display *dpy, XErrorEvent *event, void *arg)
951 return 1;
954 /***********************************************************************
955 * X11DRV_XRender_ExtTextOut
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 )
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 );
985 TRACE("%p, %d, %d, %08x, %p, %s, %d, %p)\n", hdc, x, y, flags,
986 lprect, debugstr_wn(wstr, count), count, lpDx);
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);
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));
1000 if(align & TA_UPDATECP)
1002 GetCurrentPositionEx( hdc, &pt );
1003 x = pt.x;
1004 y = pt.y;
1007 GetTextMetricsW(hdc, &tm);
1008 GetObjectW(GetCurrentObject(hdc, OBJ_FONT), sizeof(lf), &lf);
1010 if(!(tm.tmPitchAndFamily & TMPF_VECTOR)) /* Non-scalable fonts shouldn't be rotated */
1011 lf.lfEscapement = 0;
1013 if(lf.lfEscapement != 0) {
1014 cosEsc = cos(lf.lfEscapement * M_PI / 1800);
1015 sinEsc = sin(lf.lfEscapement * M_PI / 1800);
1016 } else {
1017 cosEsc = 1;
1018 sinEsc = 0;
1021 if(flags & (ETO_CLIPPED | ETO_OPAQUE)) {
1022 if(!lprect) {
1023 if(flags & ETO_CLIPPED) return FALSE;
1024 GetTextExtentPointI(hdc, glyphs, count, &sz);
1025 done_extents = TRUE;
1026 rc.left = x;
1027 rc.top = y;
1028 rc.right = x + sz.cx;
1029 rc.bottom = y + sz.cy;
1030 } else {
1031 rc = *lprect;
1034 LPtoDP(hdc, (POINT*)&rc, 2);
1036 if(rc.left > rc.right) {INT tmp = rc.left; rc.left = rc.right; rc.right = tmp;}
1037 if(rc.top > rc.bottom) {INT tmp = rc.top; rc.top = rc.bottom; rc.bottom = tmp;}
1040 xgcval.function = GXcopy;
1041 xgcval.background = physDev->backgroundPixel;
1042 xgcval.fill_style = FillSolid;
1043 wine_tsx11_lock();
1044 XChangeGC( gdi_display, physDev->gc, GCFunction | GCBackground | GCFillStyle, &xgcval );
1045 wine_tsx11_unlock();
1047 X11DRV_LockDIBSection( physDev, DIB_Status_GdiMod, FALSE );
1049 if(physDev->depth == 1) {
1050 if((textColor & 0xffffff) == 0) {
1051 textPixel = 0;
1052 backgroundPixel = 1;
1053 } else {
1054 textPixel = 1;
1055 backgroundPixel = 0;
1057 } else {
1058 textPixel = physDev->textPixel;
1059 backgroundPixel = physDev->backgroundPixel;
1062 if(flags & ETO_OPAQUE) {
1063 wine_tsx11_lock();
1064 XSetForeground( gdi_display, physDev->gc, backgroundPixel );
1065 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
1066 physDev->org.x + rc.left, physDev->org.y + rc.top,
1067 rc.right - rc.left, rc.bottom - rc.top );
1068 wine_tsx11_unlock();
1071 if(count == 0) {
1072 retv = TRUE;
1073 goto done;
1076 pt.x = x;
1077 pt.y = y;
1078 LPtoDP(hdc, &pt, 1);
1079 x = pt.x;
1080 y = pt.y;
1082 TRACE("real x,y %d,%d\n", x, y);
1084 if((char_extra = GetTextCharacterExtra(hdc)) != 0) {
1085 INT i;
1086 SIZE tmpsz;
1087 deltas = HeapAlloc(GetProcessHeap(), 0, count * sizeof(INT));
1088 for(i = 0; i < count; i++) {
1089 if(lpDx)
1090 deltas[i] = lpDx[i] + char_extra;
1091 else {
1092 GetTextExtentPointI(hdc, glyphs + i, 1, &tmpsz);
1093 deltas[i] = tmpsz.cx;
1096 } else if(lpDx)
1097 deltas = (INT*)lpDx;
1099 if(deltas) {
1100 width = 0;
1101 for(idx = 0; idx < count; idx++)
1102 width += deltas[idx];
1103 } else {
1104 if(!done_extents) {
1105 GetTextExtentPointI(hdc, glyphs, count, &sz);
1106 done_extents = TRUE;
1108 width = sz.cx;
1110 width = X11DRV_XWStoDS(physDev, width);
1111 xwidth = width * cosEsc;
1112 ywidth = width * sinEsc;
1114 tm.tmAscent = abs(X11DRV_YWStoDS(physDev, tm.tmAscent));
1115 tm.tmDescent = abs(X11DRV_YWStoDS(physDev, tm.tmDescent));
1116 switch( align & (TA_LEFT | TA_RIGHT | TA_CENTER) ) {
1117 case TA_LEFT:
1118 if (align & TA_UPDATECP) {
1119 pt.x = x + xwidth;
1120 pt.y = y - ywidth;
1121 DPtoLP(hdc, &pt, 1);
1122 MoveToEx(hdc, pt.x, pt.y, NULL);
1124 break;
1126 case TA_CENTER:
1127 x -= xwidth / 2;
1128 y += ywidth / 2;
1129 break;
1131 case TA_RIGHT:
1132 x -= xwidth;
1133 y += ywidth;
1134 if (align & TA_UPDATECP) {
1135 pt.x = x;
1136 pt.y = y;
1137 DPtoLP(hdc, &pt, 1);
1138 MoveToEx(hdc, pt.x, pt.y, NULL);
1140 break;
1143 switch( align & (TA_TOP | TA_BOTTOM | TA_BASELINE) ) {
1144 case TA_TOP:
1145 y += tm.tmAscent * cosEsc;
1146 x += tm.tmAscent * sinEsc;
1147 break;
1149 case TA_BOTTOM:
1150 y -= tm.tmDescent * cosEsc;
1151 x -= tm.tmDescent * sinEsc;
1152 break;
1154 case TA_BASELINE:
1155 break;
1158 if (flags & ETO_CLIPPED)
1160 HRGN clip_region;
1161 RECT clip_rect = *lprect;
1163 LPtoDP( hdc, (POINT *)&clip_rect, 2 );
1164 clip_region = CreateRectRgnIndirect( &clip_rect );
1165 /* make a copy of the current device region */
1166 saved_region = CreateRectRgn( 0, 0, 0, 0 );
1167 CombineRgn( saved_region, physDev->region, 0, RGN_COPY );
1168 X11DRV_SetDeviceClipping( physDev, saved_region, clip_region );
1169 DeleteObject( clip_region );
1172 if(X11DRV_XRender_Installed) {
1173 if(!physDev->xrender->pict) {
1174 XRenderPictureAttributes pa;
1175 pa.subwindow_mode = IncludeInferiors;
1177 wine_tsx11_lock();
1178 physDev->xrender->pict = pXRenderCreatePicture(gdi_display,
1179 physDev->drawable,
1180 (physDev->depth == 1) ?
1181 mono_format : screen_format,
1182 CPSubwindowMode, &pa);
1183 wine_tsx11_unlock();
1185 TRACE("allocing pict = %lx dc = %p drawable = %08lx\n",
1186 physDev->xrender->pict, hdc, physDev->drawable);
1187 } else {
1188 TRACE("using existing pict = %lx dc = %p drawable = %08lx\n",
1189 physDev->xrender->pict, hdc, physDev->drawable);
1192 if ((data = X11DRV_GetRegionData( physDev->region, 0 )))
1194 wine_tsx11_lock();
1195 pXRenderSetPictureClipRectangles( gdi_display, physDev->xrender->pict,
1196 physDev->org.x, physDev->org.y,
1197 (XRectangle *)data->Buffer, data->rdh.nCount );
1198 wine_tsx11_unlock();
1199 HeapFree( GetProcessHeap(), 0, data );
1203 if(GetBkMode(hdc) != TRANSPARENT) {
1204 if(!((flags & ETO_CLIPPED) && (flags & ETO_OPAQUE))) {
1205 if(!(flags & ETO_OPAQUE) || x < rc.left || x + width >= rc.right ||
1206 y - tm.tmAscent < rc.top || y + tm.tmDescent >= rc.bottom) {
1207 wine_tsx11_lock();
1208 XSetForeground( gdi_display, physDev->gc, backgroundPixel );
1209 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
1210 physDev->org.x + x, physDev->org.y + y - tm.tmAscent,
1211 width, tm.tmAscent + tm.tmDescent );
1212 wine_tsx11_unlock();
1217 if(X11DRV_XRender_Installed) {
1218 /* Create a 1x1 pixmap to tile over the font mask */
1219 if(!physDev->xrender->tile_xpm) {
1220 XRenderPictureAttributes pa;
1222 XRenderPictFormat *format = (physDev->depth == 1) ? mono_format : screen_format;
1223 wine_tsx11_lock();
1224 physDev->xrender->tile_xpm = XCreatePixmap(gdi_display,
1225 physDev->drawable,
1226 1, 1,
1227 format->depth);
1228 pa.repeat = True;
1229 physDev->xrender->tile_pict = pXRenderCreatePicture(gdi_display,
1230 physDev->xrender->tile_xpm,
1231 format,
1232 CPRepeat, &pa);
1233 wine_tsx11_unlock();
1234 TRACE("Created pixmap of depth %d\n", format->depth);
1235 /* init lastTextColor to something different from textColor */
1236 physDev->xrender->lastTextColor = ~textColor;
1240 if(textColor != physDev->xrender->lastTextColor) {
1241 if(physDev->depth != 1) {
1242 /* Map 0 -- 0xff onto 0 -- 0xffff */
1243 col.red = GetRValue(textColor);
1244 col.red |= col.red << 8;
1245 col.green = GetGValue(textColor);
1246 col.green |= col.green << 8;
1247 col.blue = GetBValue(textColor);
1248 col.blue |= col.blue << 8;
1249 col.alpha = 0x0;
1250 } else { /* for a 1bpp bitmap we always need a 1 in the tile */
1251 col.red = col.green = col.blue = 0;
1252 col.alpha = 0xffff;
1254 wine_tsx11_lock();
1255 pXRenderFillRectangle(gdi_display, PictOpSrc,
1256 physDev->xrender->tile_pict,
1257 &col, 0, 0, 1, 1);
1258 wine_tsx11_unlock();
1259 physDev->xrender->lastTextColor = textColor;
1262 /* FIXME the mapping of Text/BkColor onto 1 or 0 needs investigation.
1264 if((physDev->depth == 1) && (textPixel == 0))
1265 render_op = PictOpOutReverse; /* This gives us 'black' text */
1268 EnterCriticalSection(&xrender_cs);
1269 entry = glyphsetCache + physDev->xrender->cache_index;
1271 for(idx = 0; idx < count; idx++) {
1272 if(glyphs[idx] >= entry->nrealized || entry->realized[glyphs[idx]] == FALSE) {
1273 UploadGlyph(physDev, glyphs[idx]);
1278 TRACE("Writing %s at %ld,%ld\n", debugstr_wn(wstr,count),
1279 physDev->org.x + x, physDev->org.y + y);
1281 if(X11DRV_XRender_Installed) {
1282 wine_tsx11_lock();
1283 if(!deltas)
1284 pXRenderCompositeString16(gdi_display, render_op,
1285 physDev->xrender->tile_pict,
1286 physDev->xrender->pict,
1287 entry->font_format, entry->glyphset,
1288 0, 0, physDev->org.x + x, physDev->org.y + y,
1289 glyphs, count);
1291 else {
1292 INT offset = 0, xoff = 0, yoff = 0;
1293 for(idx = 0; idx < count; idx++) {
1294 pXRenderCompositeString16(gdi_display, render_op,
1295 physDev->xrender->tile_pict,
1296 physDev->xrender->pict,
1297 entry->font_format, entry->glyphset,
1298 0, 0, physDev->org.x + x + xoff,
1299 physDev->org.y + y + yoff,
1300 glyphs + idx, 1);
1301 offset += X11DRV_XWStoDS(physDev, deltas[idx]);
1302 xoff = offset * cosEsc;
1303 yoff = offset * -sinEsc;
1306 wine_tsx11_unlock();
1308 } else {
1309 INT offset = 0, xoff = 0, yoff = 0;
1310 wine_tsx11_lock();
1311 XSetForeground( gdi_display, physDev->gc, textPixel );
1313 if(entry->aa == AA_None) {
1314 for(idx = 0; idx < count; idx++) {
1315 SharpGlyphMono(physDev, physDev->org.x + x + xoff,
1316 physDev->org.y + y + yoff,
1317 entry->bitmaps[glyphs[idx]],
1318 &entry->gis[glyphs[idx]]);
1319 if(deltas) {
1320 offset += X11DRV_XWStoDS(physDev, deltas[idx]);
1321 xoff = offset * cosEsc;
1322 yoff = offset * -sinEsc;
1324 } else {
1325 xoff += entry->gis[glyphs[idx]].xOff;
1326 yoff += entry->gis[glyphs[idx]].yOff;
1329 } else if(physDev->depth == 1) {
1330 for(idx = 0; idx < count; idx++) {
1331 SharpGlyphGray(physDev, physDev->org.x + x + xoff,
1332 physDev->org.y + y + yoff,
1333 entry->bitmaps[glyphs[idx]],
1334 &entry->gis[glyphs[idx]]);
1335 if(deltas) {
1336 offset += X11DRV_XWStoDS(physDev, deltas[idx]);
1337 xoff = offset * cosEsc;
1338 yoff = offset * -sinEsc;
1340 } else {
1341 xoff += entry->gis[glyphs[idx]].xOff;
1342 yoff += entry->gis[glyphs[idx]].yOff;
1346 } else {
1347 XImage *image;
1348 unsigned int w, h, dummy_uint;
1349 Window dummy_window;
1350 int dummy_int;
1351 int image_x, image_y, image_off_x, image_off_y, image_w, image_h;
1352 RECT extents = {0, 0, 0, 0};
1353 POINT cur = {0, 0};
1356 XGetGeometry(gdi_display, physDev->drawable, &dummy_window, &dummy_int, &dummy_int,
1357 &w, &h, &dummy_uint, &dummy_uint);
1358 TRACE("drawable %dx%d\n", w, h);
1360 for(idx = 0; idx < count; idx++) {
1361 if(extents.left > cur.x - entry->gis[glyphs[idx]].x)
1362 extents.left = cur.x - entry->gis[glyphs[idx]].x;
1363 if(extents.top > cur.y - entry->gis[glyphs[idx]].y)
1364 extents.top = cur.y - entry->gis[glyphs[idx]].y;
1365 if(extents.right < cur.x - entry->gis[glyphs[idx]].x + entry->gis[glyphs[idx]].width)
1366 extents.right = cur.x - entry->gis[glyphs[idx]].x + entry->gis[glyphs[idx]].width;
1367 if(extents.bottom < cur.y - entry->gis[glyphs[idx]].y + entry->gis[glyphs[idx]].height)
1368 extents.bottom = cur.y - entry->gis[glyphs[idx]].y + entry->gis[glyphs[idx]].height;
1369 if(deltas) {
1370 offset += X11DRV_XWStoDS(physDev, deltas[idx]);
1371 cur.x = offset * cosEsc;
1372 cur.y = offset * -sinEsc;
1373 } else {
1374 cur.x += entry->gis[glyphs[idx]].xOff;
1375 cur.y += entry->gis[glyphs[idx]].yOff;
1378 TRACE("glyph extents %ld,%ld - %ld,%ld drawable x,y %ld,%ld\n", extents.left, extents.top,
1379 extents.right, extents.bottom, physDev->org.x + x, physDev->org.y + y);
1381 if(physDev->org.x + x + extents.left >= 0) {
1382 image_x = physDev->org.x + x + extents.left;
1383 image_off_x = 0;
1384 } else {
1385 image_x = 0;
1386 image_off_x = physDev->org.x + x + extents.left;
1388 if(physDev->org.y + y + extents.top >= 0) {
1389 image_y = physDev->org.y + y + extents.top;
1390 image_off_y = 0;
1391 } else {
1392 image_y = 0;
1393 image_off_y = physDev->org.y + y + extents.top;
1395 if(physDev->org.x + x + extents.right < w)
1396 image_w = physDev->org.x + x + extents.right - image_x;
1397 else
1398 image_w = w - image_x;
1399 if(physDev->org.y + y + extents.bottom < h)
1400 image_h = physDev->org.y + y + extents.bottom - image_y;
1401 else
1402 image_h = h - image_y;
1404 if(image_w <= 0 || image_h <= 0) goto no_image;
1406 X11DRV_expect_error(gdi_display, XRenderErrorHandler, NULL);
1407 image = XGetImage(gdi_display, physDev->drawable,
1408 image_x, image_y, image_w, image_h,
1409 AllPlanes, ZPixmap);
1410 X11DRV_check_error();
1412 TRACE("XGetImage(%p, %x, %d, %d, %d, %d, %lx, %x) depth = %d rets %p\n",
1413 gdi_display, (int)physDev->drawable, image_x, image_y,
1414 image_w, image_h, AllPlanes, ZPixmap,
1415 physDev->depth, image);
1416 if(!image) {
1417 Pixmap xpm = XCreatePixmap(gdi_display, physDev->drawable, image_w, image_h,
1418 physDev->depth);
1419 GC gc;
1420 XGCValues gcv;
1422 gcv.graphics_exposures = False;
1423 gc = XCreateGC(gdi_display, xpm, GCGraphicsExposures, &gcv);
1424 XCopyArea(gdi_display, physDev->drawable, xpm, gc, image_x, image_y,
1425 image_w, image_h, 0, 0);
1426 XFreeGC(gdi_display, gc);
1427 X11DRV_expect_error(gdi_display, XRenderErrorHandler, NULL);
1428 image = XGetImage(gdi_display, xpm, 0, 0, image_w, image_h, AllPlanes,
1429 ZPixmap);
1430 X11DRV_check_error();
1431 XFreePixmap(gdi_display, xpm);
1433 if(!image) goto no_image;
1435 image->red_mask = visual->red_mask;
1436 image->green_mask = visual->green_mask;
1437 image->blue_mask = visual->blue_mask;
1439 offset = xoff = yoff = 0;
1440 for(idx = 0; idx < count; idx++) {
1441 SmoothGlyphGray(image, xoff + image_off_x - extents.left,
1442 yoff + image_off_y - extents.top,
1443 entry->bitmaps[glyphs[idx]],
1444 &entry->gis[glyphs[idx]],
1445 textColor);
1446 if(deltas) {
1447 offset += X11DRV_XWStoDS(physDev, deltas[idx]);
1448 xoff = offset * cosEsc;
1449 yoff = offset * -sinEsc;
1450 } else {
1451 xoff += entry->gis[glyphs[idx]].xOff;
1452 yoff += entry->gis[glyphs[idx]].yOff;
1456 XPutImage(gdi_display, physDev->drawable, physDev->gc, image, 0, 0,
1457 image_x, image_y, image_w, image_h);
1458 XDestroyImage(image);
1460 no_image:
1461 wine_tsx11_unlock();
1463 LeaveCriticalSection(&xrender_cs);
1465 if (lf.lfUnderline || lf.lfStrikeOut) {
1466 int linePos;
1467 unsigned int lineWidth;
1468 UINT nMetricsSize = GetOutlineTextMetricsW(hdc, 0, NULL);
1469 OUTLINETEXTMETRICW* otm = HeapAlloc(GetProcessHeap(), 0, nMetricsSize);
1470 if (!otm) goto done;
1472 GetOutlineTextMetricsW(hdc, nMetricsSize, otm);
1474 wine_tsx11_lock();
1475 XSetForeground( gdi_display, physDev->gc, physDev->textPixel );
1477 if (lf.lfUnderline) {
1478 linePos = X11DRV_YWStoDS(physDev, otm->otmsUnderscorePosition);
1479 lineWidth = X11DRV_YWStoDS(physDev, otm->otmsUnderscoreSize);
1481 XSetLineAttributes( gdi_display, physDev->gc, lineWidth,
1482 LineSolid, CapProjecting, JoinBevel );
1483 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
1484 physDev->org.x + x - linePos * sinEsc,
1485 physDev->org.y + y - linePos * cosEsc,
1486 physDev->org.x + x + width * cosEsc - linePos * sinEsc,
1487 physDev->org.y + y - width * sinEsc - linePos * cosEsc );
1490 if (lf.lfStrikeOut) {
1491 linePos = X11DRV_YWStoDS(physDev, otm->otmsStrikeoutPosition);
1492 lineWidth = X11DRV_YWStoDS(physDev, otm->otmsStrikeoutSize);
1494 XSetLineAttributes( gdi_display, physDev->gc, lineWidth,
1495 LineSolid, CapProjecting, JoinBevel );
1496 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
1497 physDev->org.x + x, physDev->org.y + y - linePos,
1498 physDev->org.x + x + width, physDev->org.y + y - linePos );
1500 wine_tsx11_unlock();
1501 HeapFree(GetProcessHeap(), 0, otm);
1504 if(deltas && deltas != lpDx)
1505 HeapFree(GetProcessHeap(), 0, deltas);
1507 if (flags & ETO_CLIPPED)
1509 /* restore the device region */
1510 X11DRV_SetDeviceClipping( physDev, saved_region, 0 );
1511 DeleteObject( saved_region );
1514 retv = TRUE;
1516 done:
1517 X11DRV_UnlockDIBSection( physDev, TRUE );
1518 if(glyphs != wstr) HeapFree(GetProcessHeap(), 0, glyphs);
1519 return retv;
1522 BOOL X11DRV_AlphaBlend(X11DRV_PDEVICE *devDst, INT xDst, INT yDst, INT widthDst, INT heightDst,
1523 X11DRV_PDEVICE *devSrc, INT xSrc, INT ySrc, INT widthSrc, INT heightSrc,
1524 BLENDFUNCTION blendfn)
1526 XRenderPictureAttributes pa;
1527 XRenderPictFormat *src_format;
1528 Picture dst_pict, src_pict;
1529 Pixmap xpm;
1530 HBITMAP hBitmap;
1531 BITMAPOBJ *bmp;
1532 XImage *image;
1533 GC gc;
1534 XGCValues gcv;
1535 char *dstbits, *data;
1536 int y;
1537 POINT pts[2];
1538 BOOL top_down = FALSE;
1540 if(!X11DRV_XRender_Installed) {
1541 FIXME("Unable to AlphaBlend without Xrender\n");
1542 return FALSE;
1544 pts[0].x = xDst;
1545 pts[0].y = yDst;
1546 pts[1].x = xDst + widthDst;
1547 pts[1].y = yDst + heightDst;
1548 LPtoDP(devDst->hdc, pts, 2);
1549 xDst = pts[0].x;
1550 yDst = pts[0].y;
1551 widthDst = pts[1].x - pts[0].x;
1552 heightDst = pts[1].y - pts[0].y;
1554 pts[0].x = xSrc;
1555 pts[0].y = ySrc;
1556 pts[1].x = xSrc + widthSrc;
1557 pts[1].y = ySrc + heightSrc;
1558 LPtoDP(devSrc->hdc, pts, 2);
1559 xSrc = pts[0].x;
1560 ySrc = pts[0].y;
1561 widthSrc = pts[1].x - pts[0].x;
1562 heightSrc = pts[1].y - pts[0].y;
1565 if(widthDst != widthSrc || heightDst != heightSrc) {
1566 FIXME("Unable to Stretch\n");
1567 return FALSE;
1570 hBitmap = GetCurrentObject( devSrc->hdc, OBJ_BITMAP );
1571 bmp = (BITMAPOBJ *)GDI_GetObjPtr( hBitmap, BITMAP_MAGIC );
1572 if(!bmp || !bmp->dib) {
1573 FIXME("not a dibsection\n");
1574 GDI_ReleaseObj( hBitmap );
1575 return FALSE;
1578 if(bmp->dib->dsBm.bmBitsPixel != 32) {
1579 FIXME("not a 32 bpp dibsection\n");
1580 GDI_ReleaseObj( hBitmap );
1581 return FALSE;
1583 dstbits = data = HeapAlloc(GetProcessHeap(), 0, heightSrc * widthSrc * 4);
1585 if(bmp->dib->dsBmih.biHeight < 0) { /* top-down dib */
1586 top_down = TRUE;
1587 dstbits += widthSrc * (heightSrc - 1) * 4;
1589 for(y = ySrc + heightSrc - 1; y >= ySrc; y--) {
1590 memcpy(dstbits, (char *)bmp->dib->dsBm.bmBits + y * bmp->dib->dsBm.bmWidthBytes + xSrc * 4,
1591 widthSrc * 4);
1592 dstbits += (top_down ? -1 : 1) * widthSrc * 4;
1595 wine_tsx11_lock();
1596 image = XCreateImage(gdi_display, visual, 32, ZPixmap, 0,
1597 data, widthSrc, heightSrc, 32, widthSrc * 4);
1599 src_format = pXRenderFindStandardFormat(gdi_display, PictStandardARGB32);
1602 TRACE("src_format %p\n", src_format);
1604 pa.subwindow_mode = IncludeInferiors;
1606 /* FIXME use devDst->xrender->pict ? */
1607 dst_pict = pXRenderCreatePicture(gdi_display,
1608 devDst->drawable,
1609 (devDst->depth == 1) ?
1610 mono_format : screen_format,
1611 CPSubwindowMode, &pa);
1612 TRACE("dst_pict %08lx\n", dst_pict);
1613 TRACE("src_drawable = %08lx\n", devSrc->drawable);
1614 xpm = XCreatePixmap(gdi_display,
1615 devSrc->drawable,
1616 widthSrc, heightSrc, 32);
1617 gcv.graphics_exposures = False;
1618 gc = XCreateGC(gdi_display, xpm, GCGraphicsExposures, &gcv);
1619 TRACE("xpm = %08lx\n", xpm);
1620 XPutImage(gdi_display, xpm, gc, image, 0, 0, 0, 0, widthSrc, heightSrc);
1622 src_pict = pXRenderCreatePicture(gdi_display,
1623 xpm, src_format,
1624 CPSubwindowMode, &pa);
1625 TRACE("src_pict %08lx\n", src_pict);
1627 pXRenderComposite(gdi_display, PictOpOver, src_pict, 0, dst_pict,
1628 xSrc, ySrc, 0, 0,
1629 xDst + devDst->org.x, yDst + devDst->org.y, widthSrc, heightSrc);
1632 pXRenderFreePicture(gdi_display, src_pict);
1633 XFreePixmap(gdi_display, xpm);
1634 XFreeGC(gdi_display, gc);
1635 pXRenderFreePicture(gdi_display, dst_pict);
1636 image->data = NULL;
1637 XDestroyImage(image);
1639 wine_tsx11_unlock();
1640 HeapFree(GetProcessHeap(), 0, data);
1641 GDI_ReleaseObj( hBitmap );
1642 return TRUE;
1645 #else /* HAVE_X11_EXTENSIONS_XRENDER_H */
1647 void X11DRV_XRender_Init(void)
1649 TRACE("XRender support not compiled in.\n");
1650 return;
1653 void X11DRV_XRender_Finalize(void)
1657 BOOL X11DRV_XRender_SelectFont(X11DRV_PDEVICE *physDev, HFONT hfont)
1659 assert(0);
1660 return FALSE;
1663 void X11DRV_XRender_DeleteDC(X11DRV_PDEVICE *physDev)
1665 assert(0);
1666 return;
1669 BOOL X11DRV_XRender_ExtTextOut( X11DRV_PDEVICE *physDev, INT x, INT y, UINT flags,
1670 const RECT *lprect, LPCWSTR wstr, UINT count,
1671 const INT *lpDx, INT breakExtra )
1673 assert(0);
1674 return FALSE;
1677 void X11DRV_XRender_UpdateDrawable(X11DRV_PDEVICE *physDev)
1679 assert(0);
1680 return;
1683 BOOL X11DRV_AlphaBlend(X11DRV_PDEVICE *devDst, INT xDst, INT yDst, INT widthDst, INT heightDst,
1684 X11DRV_PDEVICE *devSrc, INT xSrc, INT ySrc, INT widthSrc, INT heightSrc,
1685 BLENDFUNCTION blendfn)
1687 FIXME("not supported - XRENDER headers were missing at compile time\n");
1688 return FALSE;
1691 #endif /* HAVE_X11_EXTENSIONS_XRENDER_H */